blob: 25bbd046f807aae9427d2d317a0e3a2defe44c96 [file] [log] [blame]
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001/*
2 * sfe_ipv4.c
3 * Shortcut forwarding engine - IPv4 edition.
4 *
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05305 * Copyright (c) 2013-2016, 2019-2020, The Linux Foundation. All rights reserved.
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +05306 * Copyright (c) 2021,2022 Qualcomm Innovation Center, Inc. All rights reserved.
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05307 *
8 * Permission to use, copy, modify, and/or distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
11 *
Xiaoping Fana42c68b2015-08-07 18:00:39 -070012 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +053017 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010019 */
Matthew McClintocka3221942014-01-16 11:44:26 -060020
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010021#include <linux/module.h>
Dave Hudsondcd08fb2013-11-22 09:25:16 -060022#include <linux/sysfs.h>
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010023#include <linux/skbuff.h>
24#include <linux/icmp.h>
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010025#include <net/tcp.h>
Dave Hudsondcd08fb2013-11-22 09:25:16 -060026#include <linux/etherdevice.h>
Tian Yang45f39c82020-10-06 14:07:47 -070027#include <linux/version.h>
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +053028#include <linux/lockdep.h>
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010029
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +053030#include "sfe_debug.h"
Ratheesh Kannoth89302a72021-10-20 08:10:37 +053031#include "sfe_api.h"
Dave Hudsondcd08fb2013-11-22 09:25:16 -060032#include "sfe.h"
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +053033#include "sfe_flow_cookie.h"
34#include "sfe_ipv4.h"
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +053035#include "sfe_ipv4_udp.h"
36#include "sfe_ipv4_tcp.h"
37#include "sfe_ipv4_icmp.h"
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010038
39static char *sfe_ipv4_exception_events_string[SFE_IPV4_EXCEPTION_EVENT_LAST] = {
40 "UDP_HEADER_INCOMPLETE",
41 "UDP_NO_CONNECTION",
42 "UDP_IP_OPTIONS_OR_INITIAL_FRAGMENT",
43 "UDP_SMALL_TTL",
44 "UDP_NEEDS_FRAGMENTATION",
45 "TCP_HEADER_INCOMPLETE",
46 "TCP_NO_CONNECTION_SLOW_FLAGS",
47 "TCP_NO_CONNECTION_FAST_FLAGS",
48 "TCP_IP_OPTIONS_OR_INITIAL_FRAGMENT",
49 "TCP_SMALL_TTL",
50 "TCP_NEEDS_FRAGMENTATION",
51 "TCP_FLAGS",
52 "TCP_SEQ_EXCEEDS_RIGHT_EDGE",
53 "TCP_SMALL_DATA_OFFS",
54 "TCP_BAD_SACK",
55 "TCP_BIG_DATA_OFFS",
56 "TCP_SEQ_BEFORE_LEFT_EDGE",
57 "TCP_ACK_EXCEEDS_RIGHT_EDGE",
58 "TCP_ACK_BEFORE_LEFT_EDGE",
59 "ICMP_HEADER_INCOMPLETE",
60 "ICMP_UNHANDLED_TYPE",
61 "ICMP_IPV4_HEADER_INCOMPLETE",
62 "ICMP_IPV4_NON_V4",
63 "ICMP_IPV4_IP_OPTIONS_INCOMPLETE",
64 "ICMP_IPV4_UDP_HEADER_INCOMPLETE",
65 "ICMP_IPV4_TCP_HEADER_INCOMPLETE",
66 "ICMP_IPV4_UNHANDLED_PROTOCOL",
67 "ICMP_NO_CONNECTION",
68 "ICMP_FLUSHED_CONNECTION",
69 "HEADER_INCOMPLETE",
Ratheesh Kannoth43d64f82021-10-20 08:23:29 +053070 "HEADER_CSUM_BAD",
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010071 "BAD_TOTAL_LENGTH",
72 "NON_V4",
73 "NON_INITIAL_FRAGMENT",
74 "DATAGRAM_INCOMPLETE",
75 "IP_OPTIONS_INCOMPLETE",
76 "UNHANDLED_PROTOCOL"
77};
78
Xiaoping Fan6a1672f2016-08-17 19:58:12 -070079static struct sfe_ipv4 __si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010080
81/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010082 * sfe_ipv4_gen_ip_csum()
83 * Generate the IP checksum for an IPv4 header.
84 *
85 * Note that this function assumes that we have only 20 bytes of IP header.
86 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +053087u16 sfe_ipv4_gen_ip_csum(struct iphdr *iph)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010088{
Xiaoping Fan6a1672f2016-08-17 19:58:12 -070089 u32 sum;
90 u16 *i = (u16 *)iph;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010091
92 iph->check = 0;
93
94 /*
95 * Generate the sum.
96 */
97 sum = i[0] + i[1] + i[2] + i[3] + i[4] + i[5] + i[6] + i[7] + i[8] + i[9];
98
99 /*
100 * Fold it to ones-complement form.
101 */
102 sum = (sum & 0xffff) + (sum >> 16);
103 sum = (sum & 0xffff) + (sum >> 16);
104
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700105 return (u16)sum ^ 0xffff;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100106}
107
108/*
109 * sfe_ipv4_get_connection_match_hash()
110 * Generate the hash used in connection match lookups.
111 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700112static inline unsigned int sfe_ipv4_get_connection_match_hash(struct net_device *dev, u8 protocol,
Dave Hudson87973cd2013-10-22 16:00:04 +0100113 __be32 src_ip, __be16 src_port,
114 __be32 dest_ip, __be16 dest_port)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100115{
116 size_t dev_addr = (size_t)dev;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700117 u32 hash = ((u32)dev_addr) ^ ntohl(src_ip ^ dest_ip) ^ protocol ^ ntohs(src_port ^ dest_port);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100118 return ((hash >> SFE_IPV4_CONNECTION_HASH_SHIFT) ^ hash) & SFE_IPV4_CONNECTION_HASH_MASK;
119}
120
121/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530122 * sfe_ipv4_find_connection_match_rcu()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100123 * Get the IPv4 flow match info that corresponds to a particular 5-tuple.
124 *
125 * On entry we must be holding the lock that protects the hash table.
126 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530127struct sfe_ipv4_connection_match *
128sfe_ipv4_find_connection_match_rcu(struct sfe_ipv4 *si, struct net_device *dev, u8 protocol,
Dave Hudson87973cd2013-10-22 16:00:04 +0100129 __be32 src_ip, __be16 src_port,
130 __be32 dest_ip, __be16 dest_port)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100131{
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530132 struct sfe_ipv4_connection_match *cm = NULL;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100133 unsigned int conn_match_idx;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530134 struct hlist_head *lhead;
135
136 WARN_ON_ONCE(!rcu_read_lock_held());
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100137
138 conn_match_idx = sfe_ipv4_get_connection_match_hash(dev, protocol, src_ip, src_port, dest_ip, dest_port);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100139
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530140 lhead = &si->hlist_conn_match_hash_head[conn_match_idx];
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100141
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530142 hlist_for_each_entry_rcu(cm, lhead, hnode) {
143 if (cm->match_src_port != src_port
144 || cm->match_dest_port != dest_port
145 || cm->match_src_ip != src_ip
146 || cm->match_dest_ip != dest_ip
147 || cm->match_protocol != protocol
148 || cm->match_dev != dev) {
149 continue;
150 }
151
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530152 this_cpu_inc(si->stats_pcpu->connection_match_hash_hits64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100153
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530154 break;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100155 }
156
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100157 return cm;
158}
159
160/*
161 * sfe_ipv4_connection_match_update_summary_stats()
162 * Update the summary stats for a connection match entry.
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530163 *
164 * Stats are incremented atomically. So use atomic substraction to update summary
165 * stats.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100166 */
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530167static inline void sfe_ipv4_connection_match_update_summary_stats(struct sfe_ipv4_connection_match *cm,
168 u32 *packets, u32 *bytes)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100169{
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530170 u32 packet_count, byte_count;
171
172 packet_count = atomic_read(&cm->rx_packet_count);
173 cm->rx_packet_count64 += packet_count;
174 atomic_sub(packet_count, &cm->rx_packet_count);
175
176 byte_count = atomic_read(&cm->rx_byte_count);
177 cm->rx_byte_count64 += byte_count;
178 atomic_sub(byte_count, &cm->rx_byte_count);
179
180 *packets = packet_count;
181 *bytes = byte_count;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100182}
183
184/*
185 * sfe_ipv4_connection_match_compute_translations()
186 * Compute port and address translations for a connection match entry.
187 */
188static void sfe_ipv4_connection_match_compute_translations(struct sfe_ipv4_connection_match *cm)
189{
190 /*
191 * Before we insert the entry look to see if this is tagged as doing address
192 * translations. If it is then work out the adjustment that we need to apply
193 * to the transport checksum.
194 */
195 if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC) {
196 /*
197 * Precompute an incremental checksum adjustment so we can
198 * edit packets in this stream very quickly. The algorithm is from RFC1624.
199 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700200 u16 src_ip_hi = cm->match_src_ip >> 16;
201 u16 src_ip_lo = cm->match_src_ip & 0xffff;
202 u32 xlate_src_ip = ~cm->xlate_src_ip;
203 u16 xlate_src_ip_hi = xlate_src_ip >> 16;
204 u16 xlate_src_ip_lo = xlate_src_ip & 0xffff;
205 u16 xlate_src_port = ~cm->xlate_src_port;
206 u32 adj;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100207
208 /*
209 * When we compute this fold it down to a 16-bit offset
210 * as that way we can avoid having to do a double
211 * folding of the twos-complement result because the
212 * addition of 2 16-bit values cannot cause a double
213 * wrap-around!
214 */
215 adj = src_ip_hi + src_ip_lo + cm->match_src_port
216 + xlate_src_ip_hi + xlate_src_ip_lo + xlate_src_port;
217 adj = (adj & 0xffff) + (adj >> 16);
218 adj = (adj & 0xffff) + (adj >> 16);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700219 cm->xlate_src_csum_adjustment = (u16)adj;
Nicolas Costaac2979c2014-01-14 10:35:24 -0600220
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100221 }
222
223 if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST) {
224 /*
225 * Precompute an incremental checksum adjustment so we can
226 * edit packets in this stream very quickly. The algorithm is from RFC1624.
227 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700228 u16 dest_ip_hi = cm->match_dest_ip >> 16;
229 u16 dest_ip_lo = cm->match_dest_ip & 0xffff;
230 u32 xlate_dest_ip = ~cm->xlate_dest_ip;
231 u16 xlate_dest_ip_hi = xlate_dest_ip >> 16;
232 u16 xlate_dest_ip_lo = xlate_dest_ip & 0xffff;
233 u16 xlate_dest_port = ~cm->xlate_dest_port;
234 u32 adj;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100235
236 /*
237 * When we compute this fold it down to a 16-bit offset
238 * as that way we can avoid having to do a double
239 * folding of the twos-complement result because the
240 * addition of 2 16-bit values cannot cause a double
241 * wrap-around!
242 */
243 adj = dest_ip_hi + dest_ip_lo + cm->match_dest_port
244 + xlate_dest_ip_hi + xlate_dest_ip_lo + xlate_dest_port;
245 adj = (adj & 0xffff) + (adj >> 16);
246 adj = (adj & 0xffff) + (adj >> 16);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700247 cm->xlate_dest_csum_adjustment = (u16)adj;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100248 }
Xiaoping Fanad755af2015-04-01 16:58:46 -0700249
250 if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC) {
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700251 u32 adj = ~cm->match_src_ip + cm->xlate_src_ip;
Xiaoping Fanad755af2015-04-01 16:58:46 -0700252 if (adj < cm->xlate_src_ip) {
253 adj++;
254 }
255
256 adj = (adj & 0xffff) + (adj >> 16);
257 adj = (adj & 0xffff) + (adj >> 16);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700258 cm->xlate_src_partial_csum_adjustment = (u16)adj;
Xiaoping Fanad755af2015-04-01 16:58:46 -0700259 }
260
261 if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST) {
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700262 u32 adj = ~cm->match_dest_ip + cm->xlate_dest_ip;
Xiaoping Fanad755af2015-04-01 16:58:46 -0700263 if (adj < cm->xlate_dest_ip) {
264 adj++;
265 }
266
267 adj = (adj & 0xffff) + (adj >> 16);
268 adj = (adj & 0xffff) + (adj >> 16);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700269 cm->xlate_dest_partial_csum_adjustment = (u16)adj;
Xiaoping Fanad755af2015-04-01 16:58:46 -0700270 }
271
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100272}
273
274/*
275 * sfe_ipv4_update_summary_stats()
276 * Update the summary stats.
277 */
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530278static void sfe_ipv4_update_summary_stats(struct sfe_ipv4 *si, struct sfe_ipv4_stats *stats)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100279{
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530280 int i = 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100281
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530282 memset(stats, 0, sizeof(*stats));
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100283
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530284 for_each_possible_cpu(i) {
285 const struct sfe_ipv4_stats *s = per_cpu_ptr(si->stats_pcpu, i);
286
287 stats->connection_create_requests64 += s->connection_create_requests64;
288 stats->connection_create_collisions64 += s->connection_create_collisions64;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530289 stats->connection_create_failures64 += s->connection_create_failures64;
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530290 stats->connection_destroy_requests64 += s->connection_destroy_requests64;
291 stats->connection_destroy_misses64 += s->connection_destroy_misses64;
292 stats->connection_match_hash_hits64 += s->connection_match_hash_hits64;
293 stats->connection_match_hash_reorders64 += s->connection_match_hash_reorders64;
294 stats->connection_flushes64 += s->connection_flushes64;
295 stats->packets_forwarded64 += s->packets_forwarded64;
296 stats->packets_not_forwarded64 += s->packets_not_forwarded64;
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +0530297 stats->pppoe_encap_packets_forwarded64 += s->pppoe_encap_packets_forwarded64;
298 stats->pppoe_decap_packets_forwarded64 += s->pppoe_decap_packets_forwarded64;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100299 }
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530300
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100301}
302
303/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530304 * sfe_ipv4_insert_connection_match()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100305 * Insert a connection match into the hash.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100306 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530307static inline void sfe_ipv4_insert_connection_match(struct sfe_ipv4 *si,
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700308 struct sfe_ipv4_connection_match *cm)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100309{
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100310 unsigned int conn_match_idx
311 = sfe_ipv4_get_connection_match_hash(cm->match_dev, cm->match_protocol,
312 cm->match_src_ip, cm->match_src_port,
313 cm->match_dest_ip, cm->match_dest_port);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700314
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530315 lockdep_assert_held(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100316
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530317 hlist_add_head_rcu(&cm->hnode, &si->hlist_conn_match_hash_head[conn_match_idx]);
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800318#ifdef CONFIG_NF_FLOW_COOKIE
Xiaoping Fan640faf42015-08-28 15:50:55 -0700319 if (!si->flow_cookie_enable)
320 return;
321
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800322 /*
323 * Configure hardware to put a flow cookie in packet of this flow,
324 * then we can accelerate the lookup process when we received this packet.
325 */
326 for (conn_match_idx = 1; conn_match_idx < SFE_FLOW_COOKIE_SIZE; conn_match_idx++) {
327 struct sfe_flow_cookie_entry *entry = &si->sfe_flow_cookie_table[conn_match_idx];
328
329 if ((NULL == entry->match) && time_is_before_jiffies(entry->last_clean_time + HZ)) {
330 flow_cookie_set_func_t func;
331
332 rcu_read_lock();
333 func = rcu_dereference(si->flow_cookie_set_func);
334 if (func) {
Xiaoping Fan59176422015-05-22 15:58:10 -0700335 if (!func(cm->match_protocol, cm->match_src_ip, cm->match_src_port,
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800336 cm->match_dest_ip, cm->match_dest_port, conn_match_idx)) {
337 entry->match = cm;
338 cm->flow_cookie = conn_match_idx;
339 }
340 }
341 rcu_read_unlock();
342
343 break;
344 }
345 }
346#endif
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100347}
348
349/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530350 * sfe_ipv4_remove_connection_match()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100351 * Remove a connection match object from the hash.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100352 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530353static inline void sfe_ipv4_remove_connection_match(struct sfe_ipv4 *si, struct sfe_ipv4_connection_match *cm)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100354{
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530355
356 lockdep_assert_held(&si->lock);
357
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800358#ifdef CONFIG_NF_FLOW_COOKIE
Xiaoping Fan640faf42015-08-28 15:50:55 -0700359 if (si->flow_cookie_enable) {
360 /*
361 * Tell hardware that we no longer need a flow cookie in packet of this flow
362 */
363 unsigned int conn_match_idx;
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800364
Xiaoping Fan640faf42015-08-28 15:50:55 -0700365 for (conn_match_idx = 1; conn_match_idx < SFE_FLOW_COOKIE_SIZE; conn_match_idx++) {
366 struct sfe_flow_cookie_entry *entry = &si->sfe_flow_cookie_table[conn_match_idx];
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800367
Xiaoping Fan640faf42015-08-28 15:50:55 -0700368 if (cm == entry->match) {
369 flow_cookie_set_func_t func;
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800370
Xiaoping Fan640faf42015-08-28 15:50:55 -0700371 rcu_read_lock();
372 func = rcu_dereference(si->flow_cookie_set_func);
373 if (func) {
374 func(cm->match_protocol, cm->match_src_ip, cm->match_src_port,
375 cm->match_dest_ip, cm->match_dest_port, 0);
376 }
377 rcu_read_unlock();
378
379 cm->flow_cookie = 0;
380 entry->match = NULL;
381 entry->last_clean_time = jiffies;
382 break;
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800383 }
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800384 }
385 }
386#endif
387
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530388 hlist_del_init_rcu(&cm->hnode);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100389
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100390}
391
392/*
393 * sfe_ipv4_get_connection_hash()
394 * Generate the hash used in connection lookups.
395 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700396static inline unsigned int sfe_ipv4_get_connection_hash(u8 protocol, __be32 src_ip, __be16 src_port,
Dave Hudson87973cd2013-10-22 16:00:04 +0100397 __be32 dest_ip, __be16 dest_port)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100398{
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700399 u32 hash = ntohl(src_ip ^ dest_ip) ^ protocol ^ ntohs(src_port ^ dest_port);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100400 return ((hash >> SFE_IPV4_CONNECTION_HASH_SHIFT) ^ hash) & SFE_IPV4_CONNECTION_HASH_MASK;
401}
402
403/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530404 * sfe_ipv4_find_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100405 * Get the IPv4 connection info that corresponds to a particular 5-tuple.
406 *
407 * On entry we must be holding the lock that protects the hash table.
408 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530409static inline struct sfe_ipv4_connection *sfe_ipv4_find_connection(struct sfe_ipv4 *si, u32 protocol,
Dave Hudson87973cd2013-10-22 16:00:04 +0100410 __be32 src_ip, __be16 src_port,
411 __be32 dest_ip, __be16 dest_port)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100412{
413 struct sfe_ipv4_connection *c;
414 unsigned int conn_idx = sfe_ipv4_get_connection_hash(protocol, src_ip, src_port, dest_ip, dest_port);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530415
416 lockdep_assert_held(&si->lock);
417
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100418 c = si->conn_hash[conn_idx];
419
420 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100421 * Will need connection entry for next create/destroy metadata,
422 * So no need to re-order entry for these requests
423 */
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530424 while (c) {
425 if ((c->src_port == src_port)
426 && (c->dest_port == dest_port)
427 && (c->src_ip == src_ip)
428 && (c->dest_ip == dest_ip)
429 && (c->protocol == protocol)) {
430 return c;
431 }
432
433 c = c->next;
434 }
435
436 return NULL;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100437}
438
439/*
Matthew McClintockbe7b47d2013-11-27 13:26:23 -0600440 * sfe_ipv4_mark_rule()
441 * Updates the mark for a current offloaded connection
442 *
443 * Will take hash lock upon entry
444 */
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700445void sfe_ipv4_mark_rule(struct sfe_connection_mark *mark)
Matthew McClintockbe7b47d2013-11-27 13:26:23 -0600446{
447 struct sfe_ipv4 *si = &__si;
448 struct sfe_ipv4_connection *c;
Matthew McClintockdb5ac512014-01-16 17:01:40 -0600449
Xiaoping Fan3c423e32015-07-03 03:09:29 -0700450 spin_lock_bh(&si->lock);
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530451 c = sfe_ipv4_find_connection(si, mark->protocol,
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700452 mark->src_ip.ip, mark->src_port,
453 mark->dest_ip.ip, mark->dest_port);
Matthew McClintockbe7b47d2013-11-27 13:26:23 -0600454 if (c) {
Nicolas Costaf53d6fe2014-01-13 16:03:46 -0600455 WARN_ON((0 != c->mark) && (0 == mark->mark));
Matthew McClintockbe7b47d2013-11-27 13:26:23 -0600456 c->mark = mark->mark;
457 }
Xiaoping Fan3c423e32015-07-03 03:09:29 -0700458 spin_unlock_bh(&si->lock);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700459
460 if (c) {
461 DEBUG_TRACE("Matching connection found for mark, "
462 "setting from %08x to %08x\n",
463 c->mark, mark->mark);
464 }
Matthew McClintockbe7b47d2013-11-27 13:26:23 -0600465}
466
467/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530468 * sfe_ipv4_insert_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100469 * Insert a connection into the hash.
470 *
471 * On entry we must be holding the lock that protects the hash table.
472 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530473static void sfe_ipv4_insert_connection(struct sfe_ipv4 *si, struct sfe_ipv4_connection *c)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100474{
475 struct sfe_ipv4_connection **hash_head;
476 struct sfe_ipv4_connection *prev_head;
477 unsigned int conn_idx;
478
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530479 lockdep_assert_held(&si->lock);
480
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100481 /*
482 * Insert entry into the connection hash.
483 */
484 conn_idx = sfe_ipv4_get_connection_hash(c->protocol, c->src_ip, c->src_port,
485 c->dest_ip, c->dest_port);
486 hash_head = &si->conn_hash[conn_idx];
487 prev_head = *hash_head;
488 c->prev = NULL;
489 if (prev_head) {
490 prev_head->prev = c;
491 }
492
493 c->next = prev_head;
494 *hash_head = c;
495
496 /*
497 * Insert entry into the "all connections" list.
498 */
499 if (si->all_connections_tail) {
500 c->all_connections_prev = si->all_connections_tail;
501 si->all_connections_tail->all_connections_next = c;
502 } else {
503 c->all_connections_prev = NULL;
504 si->all_connections_head = c;
505 }
506
507 si->all_connections_tail = c;
508 c->all_connections_next = NULL;
509 si->num_connections++;
510
511 /*
512 * Insert the connection match objects too.
513 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530514 sfe_ipv4_insert_connection_match(si, c->original_match);
515 sfe_ipv4_insert_connection_match(si, c->reply_match);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100516}
517
518/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530519 * sfe_ipv4_remove_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100520 * Remove a sfe_ipv4_connection object from the hash.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100521 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530522bool sfe_ipv4_remove_connection(struct sfe_ipv4 *si, struct sfe_ipv4_connection *c)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100523{
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530524 lockdep_assert_held(&si->lock);
525
526 if (c->removed) {
527 DEBUG_ERROR("%px: Connection has been removed already\n", c);
528 return false;
529 }
530
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100531 /*
532 * Remove the connection match objects.
533 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530534 sfe_ipv4_remove_connection_match(si, c->reply_match);
535 sfe_ipv4_remove_connection_match(si, c->original_match);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100536
537 /*
538 * Unlink the connection.
539 */
540 if (c->prev) {
541 c->prev->next = c->next;
542 } else {
543 unsigned int conn_idx = sfe_ipv4_get_connection_hash(c->protocol, c->src_ip, c->src_port,
544 c->dest_ip, c->dest_port);
545 si->conn_hash[conn_idx] = c->next;
546 }
547
548 if (c->next) {
549 c->next->prev = c->prev;
550 }
Xiaoping Fan34586472015-07-03 02:20:35 -0700551
552 /*
553 * Unlink connection from all_connections list
554 */
555 if (c->all_connections_prev) {
556 c->all_connections_prev->all_connections_next = c->all_connections_next;
557 } else {
558 si->all_connections_head = c->all_connections_next;
559 }
560
561 if (c->all_connections_next) {
562 c->all_connections_next->all_connections_prev = c->all_connections_prev;
563 } else {
564 si->all_connections_tail = c->all_connections_prev;
565 }
566
Ken Zhudc423672021-09-02 18:27:01 -0700567 /*
568 * If I am the next sync connection, move the sync to my next or head.
569 */
570 if (unlikely(si->wc_next == c)) {
571 si->wc_next = c->all_connections_next;
572 }
573
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530574 c->removed = true;
Xiaoping Fan34586472015-07-03 02:20:35 -0700575 si->num_connections--;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530576 return true;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100577}
578
579/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530580 * sfe_ipv4_gen_sync_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100581 * Sync a connection.
582 *
583 * On entry to this function we expect that the lock for the connection is either
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530584 * already held (while called from sfe_ipv4_periodic_sync() or isn't required
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530585 * (while called from sfe_ipv4_flush_connection())
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100586 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530587static void sfe_ipv4_gen_sync_connection(struct sfe_ipv4 *si, struct sfe_ipv4_connection *c,
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700588 struct sfe_connection_sync *sis, sfe_sync_reason_t reason,
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700589 u64 now_jiffies)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100590{
591 struct sfe_ipv4_connection_match *original_cm;
592 struct sfe_ipv4_connection_match *reply_cm;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530593 u32 packet_count, byte_count;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100594
595 /*
596 * Fill in the update message.
597 */
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700598 sis->is_v6 = 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100599 sis->protocol = c->protocol;
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700600 sis->src_ip.ip = c->src_ip;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700601 sis->src_ip_xlate.ip = c->src_ip_xlate;
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700602 sis->dest_ip.ip = c->dest_ip;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700603 sis->dest_ip_xlate.ip = c->dest_ip_xlate;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100604 sis->src_port = c->src_port;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700605 sis->src_port_xlate = c->src_port_xlate;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100606 sis->dest_port = c->dest_port;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700607 sis->dest_port_xlate = c->dest_port_xlate;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100608
609 original_cm = c->original_match;
610 reply_cm = c->reply_match;
611 sis->src_td_max_window = original_cm->protocol_state.tcp.max_win;
612 sis->src_td_end = original_cm->protocol_state.tcp.end;
613 sis->src_td_max_end = original_cm->protocol_state.tcp.max_end;
614 sis->dest_td_max_window = reply_cm->protocol_state.tcp.max_win;
615 sis->dest_td_end = reply_cm->protocol_state.tcp.end;
616 sis->dest_td_max_end = reply_cm->protocol_state.tcp.max_end;
617
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530618 sfe_ipv4_connection_match_update_summary_stats(original_cm, &packet_count, &byte_count);
619 sis->src_new_packet_count = packet_count;
620 sis->src_new_byte_count = byte_count;
Matthew McClintockd0cdb802014-02-24 16:30:35 -0600621
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530622 sfe_ipv4_connection_match_update_summary_stats(reply_cm, &packet_count, &byte_count);
623 sis->dest_new_packet_count = packet_count;
624 sis->dest_new_byte_count = byte_count;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100625
Matthew McClintockd0cdb802014-02-24 16:30:35 -0600626 sis->src_dev = original_cm->match_dev;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100627 sis->src_packet_count = original_cm->rx_packet_count64;
628 sis->src_byte_count = original_cm->rx_byte_count64;
Matthew McClintockd0cdb802014-02-24 16:30:35 -0600629
630 sis->dest_dev = reply_cm->match_dev;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100631 sis->dest_packet_count = reply_cm->rx_packet_count64;
632 sis->dest_byte_count = reply_cm->rx_byte_count64;
633
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700634 sis->reason = reason;
635
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100636 /*
637 * Get the time increment since our last sync.
638 */
639 sis->delta_jiffies = now_jiffies - c->last_sync_jiffies;
640 c->last_sync_jiffies = now_jiffies;
641}
642
643/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530644 * sfe_ipv4_free_connection_rcu()
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530645 * Called at RCU qs state to free the connection object.
646 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530647static void sfe_ipv4_free_connection_rcu(struct rcu_head *head)
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530648{
649 struct sfe_ipv4_connection *c;
650
651 /*
652 * We dont need spin lock as the connection is already removed from link list
653 */
654 c = container_of(head, struct sfe_ipv4_connection, rcu);
655
656 BUG_ON(!c->removed);
657
658 DEBUG_TRACE("%px: connecton has been deleted\n", c);
659
660 /*
661 * Release our hold of the source and dest devices and free the memory
662 * for our connection objects.
663 */
664 dev_put(c->original_dev);
665 dev_put(c->reply_dev);
666 kfree(c->original_match);
667 kfree(c->reply_match);
668 kfree(c);
669}
670
671/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530672 * sfe_ipv4_flush_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100673 * Flush a connection and free all associated resources.
674 *
675 * We need to be called with bottom halves disabled locally as we need to acquire
676 * the connection hash lock and release it again. In general we're actually called
677 * from within a BH and so we're fine, but we're also called when connections are
678 * torn down.
679 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530680void sfe_ipv4_flush_connection(struct sfe_ipv4 *si,
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700681 struct sfe_ipv4_connection *c,
682 sfe_sync_reason_t reason)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100683{
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700684 u64 now_jiffies;
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700685 sfe_sync_rule_callback_t sync_rule_callback;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100686
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530687 BUG_ON(!c->removed);
688
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530689 this_cpu_inc(si->stats_pcpu->connection_flushes64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100690
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530691 rcu_read_lock();
692 sync_rule_callback = rcu_dereference(si->sync_rule_callback);
693
694 /*
695 * Generate a sync message and then sync.
696 */
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600697 if (sync_rule_callback) {
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530698 struct sfe_connection_sync sis;
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600699 now_jiffies = get_jiffies_64();
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530700 sfe_ipv4_gen_sync_connection(si, c, &sis, reason, now_jiffies);
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600701 sync_rule_callback(&sis);
702 }
703
704 rcu_read_unlock();
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100705
706 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100707 * Release our hold of the source and dest devices and free the memory
708 * for our connection objects.
709 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530710 call_rcu(&c->rcu, sfe_ipv4_free_connection_rcu);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100711}
712
713/*
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530714 * sfe_ipv4_exception_stats_inc()
715 * Increment exception stats.
716 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530717void sfe_ipv4_exception_stats_inc(struct sfe_ipv4 *si, enum sfe_ipv4_exception_events reason)
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530718{
719 struct sfe_ipv4_stats *stats = this_cpu_ptr(si->stats_pcpu);
720 stats->exception_events64[reason]++;
721 stats->packets_not_forwarded64++;
722}
723
724/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100725 * sfe_ipv4_recv()
Matthew McClintocka8ad7962014-01-16 16:49:30 -0600726 * Handle packet receives and forwaring.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100727 *
728 * Returns 1 if the packet is forwarded or 0 if it isn't.
729 */
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +0530730int sfe_ipv4_recv(struct net_device *dev, struct sk_buff *skb, struct sfe_l2_info *l2_info)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100731{
732 struct sfe_ipv4 *si = &__si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100733 unsigned int len;
734 unsigned int tot_len;
735 unsigned int frag_off;
736 unsigned int ihl;
737 bool flush_on_find;
738 bool ip_options;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530739 struct iphdr *iph;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700740 u32 protocol;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100741
742 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100743 * Check that we have space for an IP header here.
744 */
745 len = skb->len;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530746 if (unlikely(!pskb_may_pull(skb, sizeof(struct iphdr)))) {
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530747 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_HEADER_INCOMPLETE);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100748 DEBUG_TRACE("len: %u is too short\n", len);
749 return 0;
750 }
751
752 /*
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +0530753 * Validate ip csum if necessary. If ip_summed is set to CHECKSUM_UNNECESSARY, it is assumed
754 * that the L3 checksum is validated by the Rx interface or the tunnel interface that has
755 * generated the packet.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100756 */
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530757 iph = (struct iphdr *)skb->data;
Ratheesh Kannoth43d64f82021-10-20 08:23:29 +0530758 if (unlikely(skb->ip_summed != CHECKSUM_UNNECESSARY) && (ip_fast_csum((u8 *)iph, iph->ihl))) {
759 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_HEADER_CSUM_BAD);
760
761 DEBUG_TRACE("Bad IPv4 header csum: 0x%x\n", iph->check);
762 return 0;
763 }
764
765 /*
766 * Check that our "total length" is large enough for an IP header.
767 */
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100768 tot_len = ntohs(iph->tot_len);
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530769 if (unlikely(tot_len < sizeof(struct iphdr))) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100770
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530771 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_BAD_TOTAL_LENGTH);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100772 DEBUG_TRACE("tot_len: %u is too short\n", tot_len);
773 return 0;
774 }
775
776 /*
777 * Is our IP version wrong?
778 */
779 if (unlikely(iph->version != 4)) {
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530780 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_NON_V4);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100781 DEBUG_TRACE("IP version: %u\n", iph->version);
782 return 0;
783 }
784
785 /*
786 * Does our datagram fit inside the skb?
787 */
788 if (unlikely(tot_len > len)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100789
790 DEBUG_TRACE("tot_len: %u, exceeds len: %u\n", tot_len, len);
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530791 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_DATAGRAM_INCOMPLETE);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100792 return 0;
793 }
794
795 /*
796 * Do we have a non-initial fragment?
Nicolas Costaac2979c2014-01-14 10:35:24 -0600797 */
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100798 frag_off = ntohs(iph->frag_off);
799 if (unlikely(frag_off & IP_OFFSET)) {
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530800 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_NON_INITIAL_FRAGMENT);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100801 DEBUG_TRACE("non-initial fragment\n");
802 return 0;
803 }
804
805 /*
806 * If we have a (first) fragment then mark it to cause any connection to flush.
807 */
808 flush_on_find = unlikely(frag_off & IP_MF) ? true : false;
809
810 /*
811 * Do we have any IP options? That's definite a slow path! If we do have IP
812 * options we need to recheck our header size.
813 */
814 ihl = iph->ihl << 2;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530815 ip_options = unlikely(ihl != sizeof(struct iphdr)) ? true : false;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100816 if (unlikely(ip_options)) {
817 if (unlikely(len < ihl)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100818
819 DEBUG_TRACE("len: %u is too short for header of size: %u\n", len, ihl);
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530820 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_IP_OPTIONS_INCOMPLETE);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100821 return 0;
822 }
823
824 flush_on_find = true;
825 }
826
827 protocol = iph->protocol;
828 if (IPPROTO_UDP == protocol) {
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +0530829 return sfe_ipv4_recv_udp(si, skb, dev, len, iph, ihl, flush_on_find, l2_info);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100830 }
831
832 if (IPPROTO_TCP == protocol) {
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +0530833 return sfe_ipv4_recv_tcp(si, skb, dev, len, iph, ihl, flush_on_find, l2_info);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100834 }
835
836 if (IPPROTO_ICMP == protocol) {
837 return sfe_ipv4_recv_icmp(si, skb, dev, len, iph, ihl);
838 }
839
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530840 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_UNHANDLED_PROTOCOL);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100841
842 DEBUG_TRACE("not UDP, TCP or ICMP: %u\n", protocol);
843 return 0;
844}
845
Nicolas Costa436926b2014-01-14 10:36:22 -0600846static void
847sfe_ipv4_update_tcp_state(struct sfe_ipv4_connection *c,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530848 struct sfe_ipv4_rule_create_msg *msg)
Nicolas Costa436926b2014-01-14 10:36:22 -0600849{
850 struct sfe_ipv4_connection_match *orig_cm;
851 struct sfe_ipv4_connection_match *repl_cm;
852 struct sfe_ipv4_tcp_connection_match *orig_tcp;
853 struct sfe_ipv4_tcp_connection_match *repl_tcp;
854
855 orig_cm = c->original_match;
856 repl_cm = c->reply_match;
857 orig_tcp = &orig_cm->protocol_state.tcp;
858 repl_tcp = &repl_cm->protocol_state.tcp;
859
860 /* update orig */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530861 if (orig_tcp->max_win < msg->tcp_rule.flow_max_window) {
862 orig_tcp->max_win = msg->tcp_rule.flow_max_window;
Nicolas Costa436926b2014-01-14 10:36:22 -0600863 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530864 if ((s32)(orig_tcp->end - msg->tcp_rule.flow_end) < 0) {
865 orig_tcp->end = msg->tcp_rule.flow_end;
Nicolas Costa436926b2014-01-14 10:36:22 -0600866 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530867 if ((s32)(orig_tcp->max_end - msg->tcp_rule.flow_max_end) < 0) {
868 orig_tcp->max_end = msg->tcp_rule.flow_max_end;
Nicolas Costa436926b2014-01-14 10:36:22 -0600869 }
870
871 /* update reply */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530872 if (repl_tcp->max_win < msg->tcp_rule.return_max_window) {
873 repl_tcp->max_win = msg->tcp_rule.return_max_window;
Nicolas Costa436926b2014-01-14 10:36:22 -0600874 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530875 if ((s32)(repl_tcp->end - msg->tcp_rule.return_end) < 0) {
876 repl_tcp->end = msg->tcp_rule.return_end;
Nicolas Costa436926b2014-01-14 10:36:22 -0600877 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530878 if ((s32)(repl_tcp->max_end - msg->tcp_rule.return_max_end) < 0) {
879 repl_tcp->max_end = msg->tcp_rule.return_max_end;
Nicolas Costa436926b2014-01-14 10:36:22 -0600880 }
881
882 /* update match flags */
883 orig_cm->flags &= ~SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
884 repl_cm->flags &= ~SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530885 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_NO_SEQ_CHECK) {
886
Nicolas Costa436926b2014-01-14 10:36:22 -0600887 orig_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
888 repl_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
889 }
890}
891
892static void
893sfe_ipv4_update_protocol_state(struct sfe_ipv4_connection *c,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530894 struct sfe_ipv4_rule_create_msg *msg)
Nicolas Costa436926b2014-01-14 10:36:22 -0600895{
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530896 switch (msg->tuple.protocol) {
Nicolas Costa436926b2014-01-14 10:36:22 -0600897 case IPPROTO_TCP:
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530898 sfe_ipv4_update_tcp_state(c, msg);
Nicolas Costa436926b2014-01-14 10:36:22 -0600899 break;
900 }
901}
902
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530903void sfe_ipv4_update_rule(struct sfe_ipv4_rule_create_msg *msg)
Nicolas Costa436926b2014-01-14 10:36:22 -0600904{
905 struct sfe_ipv4_connection *c;
906 struct sfe_ipv4 *si = &__si;
907
908 spin_lock_bh(&si->lock);
909
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530910 c = sfe_ipv4_find_connection(si,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530911 msg->tuple.protocol,
912 msg->tuple.flow_ip,
913 msg->tuple.flow_ident,
914 msg->tuple.return_ip,
915 msg->tuple.return_ident);
Nicolas Costa436926b2014-01-14 10:36:22 -0600916 if (c != NULL) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530917 sfe_ipv4_update_protocol_state(c, msg);
Nicolas Costa436926b2014-01-14 10:36:22 -0600918 }
919
920 spin_unlock_bh(&si->lock);
921}
922
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100923/*
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +0530924 * sfe_ipv4_xmit_eth_type_check()
925 * Checking if MAC header has to be written.
926 */
927static inline bool sfe_ipv4_xmit_eth_type_check(struct net_device *dev, u32 cm_flags)
928{
929 if (!(dev->flags & IFF_NOARP)) {
930 return true;
931 }
932
933 /*
934 * For PPPoE, since we are now supporting PPPoE encapsulation, we are writing L2 header.
935 */
936 if (unlikely(cm_flags & SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_ENCAP)) {
937 return true;
938 }
939
940 return false;
941}
942
943/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100944 * sfe_ipv4_create_rule()
945 * Create a forwarding rule.
946 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530947int sfe_ipv4_create_rule(struct sfe_ipv4_rule_create_msg *msg)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100948{
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600949 struct sfe_ipv4 *si = &__si;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530950 struct sfe_ipv4_connection *c, *c_old;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100951 struct sfe_ipv4_connection_match *original_cm;
952 struct sfe_ipv4_connection_match *reply_cm;
Matthew McClintockdb5ac512014-01-16 17:01:40 -0600953 struct net_device *dest_dev;
954 struct net_device *src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530955 struct sfe_ipv4_5tuple *tuple = &msg->tuple;
Suruchi Sumanc1a4a612021-10-21 14:50:23 +0530956 s32 flow_interface_num = msg->conn_rule.flow_top_interface_num;
957 s32 return_interface_num = msg->conn_rule.return_top_interface_num;
Matthew McClintockdb5ac512014-01-16 17:01:40 -0600958
Suruchi Sumanc1a4a612021-10-21 14:50:23 +0530959 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_FLOW_BOTTOM_INTERFACE) {
960 flow_interface_num = msg->conn_rule.flow_interface_num;
961 }
962
963 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_RETURN_BOTTOM_INTERFACE) {
964 return_interface_num = msg->conn_rule.return_interface_num;
965 }
966
967 src_dev = dev_get_by_index(&init_net, flow_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530968 if (!src_dev) {
969 DEBUG_WARN("%px: Unable to find src_dev corresponding to %d\n", msg,
Suruchi Sumanc1a4a612021-10-21 14:50:23 +0530970 flow_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530971 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
972 return -EINVAL;
973 }
974
Suruchi Sumanc1a4a612021-10-21 14:50:23 +0530975 dest_dev = dev_get_by_index(&init_net, return_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530976 if (!dest_dev) {
977 DEBUG_WARN("%px: Unable to find dest_dev corresponding to %d\n", msg,
Suruchi Sumanc1a4a612021-10-21 14:50:23 +0530978 return_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530979 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
980 dev_put(src_dev);
981 return -EINVAL;
982 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100983
Matthew McClintock389b42a2014-09-24 14:05:51 -0500984 if (unlikely((dest_dev->reg_state != NETREG_REGISTERED) ||
985 (src_dev->reg_state != NETREG_REGISTERED))) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530986 dev_put(src_dev);
987 dev_put(dest_dev);
988 DEBUG_WARN("%px: src_dev=%s and dest_dev=%s are unregistered\n", msg,
989 src_dev->name, dest_dev->name);
990 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
Matthew McClintock389b42a2014-09-24 14:05:51 -0500991 return -EINVAL;
992 }
993
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530994 /*
995 * Allocate the various connection tracking objects.
996 */
997 c = (struct sfe_ipv4_connection *)kmalloc(sizeof(struct sfe_ipv4_connection), GFP_ATOMIC);
998 if (unlikely(!c)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530999 DEBUG_WARN("%px: memory allocation of connection entry failed\n", msg);
1000 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
1001 dev_put(src_dev);
1002 dev_put(dest_dev);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301003 return -ENOMEM;
1004 }
1005
1006 original_cm = (struct sfe_ipv4_connection_match *)kmalloc(sizeof(struct sfe_ipv4_connection_match), GFP_ATOMIC);
1007 if (unlikely(!original_cm)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301008 DEBUG_WARN("%px: memory allocation of connection match entry failed\n", msg);
1009 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301010 kfree(c);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301011 dev_put(src_dev);
1012 dev_put(dest_dev);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301013 return -ENOMEM;
1014 }
1015
1016 reply_cm = (struct sfe_ipv4_connection_match *)kmalloc(sizeof(struct sfe_ipv4_connection_match), GFP_ATOMIC);
1017 if (unlikely(!reply_cm)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301018 DEBUG_WARN("%px: memory allocation of connection match entry failed\n", msg);
1019 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301020 kfree(original_cm);
1021 kfree(c);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301022 dev_put(src_dev);
1023 dev_put(dest_dev);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301024 return -ENOMEM;
1025 }
1026
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301027 this_cpu_inc(si->stats_pcpu->connection_create_requests64);
1028
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001029 spin_lock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001030
1031 /*
Nicolas Costa436926b2014-01-14 10:36:22 -06001032 * Check to see if there is already a flow that matches the rule we're
1033 * trying to create. If there is then we can't create a new one.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001034 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301035 c_old = sfe_ipv4_find_connection(si,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301036 msg->tuple.protocol,
1037 msg->tuple.flow_ip,
1038 msg->tuple.flow_ident,
1039 msg->tuple.return_ip,
1040 msg->tuple.return_ident);
1041
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301042 if (c_old != NULL) {
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301043 this_cpu_inc(si->stats_pcpu->connection_create_collisions64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001044
1045 /*
Nicolas Costa436926b2014-01-14 10:36:22 -06001046 * If we already have the flow then it's likely that this
1047 * request to create the connection rule contains more
1048 * up-to-date information. Check and update accordingly.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001049 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301050 sfe_ipv4_update_protocol_state(c, msg);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001051 spin_unlock_bh(&si->lock);
1052
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301053 kfree(reply_cm);
1054 kfree(original_cm);
1055 kfree(c);
1056
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301057 dev_put(src_dev);
1058 dev_put(dest_dev);
1059
1060 DEBUG_TRACE("connection already exists - p:%d\n"
1061 " s: %s:%pM:%pI4:%u, d: %s:%pM:%pI4:%u\n",
1062 tuple->protocol,
1063 src_dev->name, msg->conn_rule.flow_mac, &tuple->flow_ip, ntohs(tuple->flow_ident),
1064 dest_dev->name, msg->conn_rule.return_mac, &tuple->return_ip, ntohs(tuple->return_ident));
1065
Nicolas Costa514fde02014-01-13 15:50:29 -06001066 return -EADDRINUSE;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001067 }
1068
1069 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001070 * Fill in the "original" direction connection matching object.
1071 * Note that the transmit MAC address is "dest_mac_xlate" because
1072 * we always know both ends of a connection by their translated
1073 * addresses and not their public addresses.
1074 */
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001075 original_cm->match_dev = src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301076 original_cm->match_protocol = tuple->protocol;
1077 original_cm->match_src_ip = tuple->flow_ip;
1078 original_cm->match_src_port = tuple->flow_ident;
1079 original_cm->match_dest_ip = tuple->return_ip;
1080 original_cm->match_dest_port = tuple->return_ident;
1081
1082 original_cm->xlate_src_ip = msg->conn_rule.flow_ip_xlate;
1083 original_cm->xlate_src_port = msg->conn_rule.flow_ident_xlate;
1084 original_cm->xlate_dest_ip = msg->conn_rule.return_ip_xlate;
1085 original_cm->xlate_dest_port =msg->conn_rule.return_ident_xlate;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301086 atomic_set(&original_cm->rx_packet_count, 0);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001087 original_cm->rx_packet_count64 = 0;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301088 atomic_set(&original_cm->rx_byte_count, 0);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001089 original_cm->rx_byte_count64 = 0;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301090
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001091 original_cm->xmit_dev = dest_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301092 original_cm->xmit_dev_mtu = msg->conn_rule.return_mtu;
1093
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001094 original_cm->connection = c;
1095 original_cm->counter_match = reply_cm;
1096 original_cm->flags = 0;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301097
1098 if (msg->valid_flags & SFE_RULE_CREATE_QOS_VALID) {
1099 original_cm->priority = msg->qos_rule.flow_qos_tag;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001100 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PRIORITY_REMARK;
1101 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301102
1103 if (msg->valid_flags & SFE_RULE_CREATE_DSCP_MARKING_VALID) {
1104 original_cm->dscp = msg->dscp_rule.flow_dscp << SFE_IPV4_DSCP_SHIFT;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001105 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_DSCP_REMARK;
1106 }
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301107
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001108#ifdef CONFIG_NF_FLOW_COOKIE
1109 original_cm->flow_cookie = 0;
1110#endif
Zhi Chen8748eb32015-06-18 12:58:48 -07001111#ifdef CONFIG_XFRM
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301112 if (msg->valid_flags & SFE_RULE_CREATE_DIRECTION_VALID) {
1113 original_cm->flow_accel = msg->direction_rule.flow_accel;
1114 } else {
1115 original_cm->flow_accel = 1;
1116 }
Zhi Chen8748eb32015-06-18 12:58:48 -07001117#endif
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301118 /*
1119 * If l2_features are disabled and flow uses l2 features such as macvlan/bridge/pppoe/vlan,
1120 * bottom interfaces are expected to be disabled in the flow rule and always top interfaces
1121 * are used. In such cases, do not use HW csum offload. csum offload is used only when we
1122 * are sending directly to the destination interface that supports it.
1123 */
1124 if (likely(dest_dev->features & NETIF_F_HW_CSUM)) {
1125 if ((msg->conn_rule.return_top_interface_num == msg->conn_rule.return_interface_num) ||
1126 (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_RETURN_BOTTOM_INTERFACE)) {
1127 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_CSUM_OFFLOAD;
1128 }
1129 }
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001130
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +05301131 reply_cm->flags = 0;
1132
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001133 /*
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301134 * Adding PPPoE parameters to original and reply entries based on the direction where
1135 * PPPoE header is valid in ECM rule.
1136 *
1137 * If PPPoE is valid in flow direction (from interface is PPPoE), then
1138 * original cm will have PPPoE at ingress (strip PPPoE header)
1139 * reply cm will have PPPoE at egress (add PPPoE header)
1140 *
1141 * If PPPoE is valid in return direction (to interface is PPPoE), then
1142 * original cm will have PPPoE at egress (add PPPoE header)
1143 * reply cm will have PPPoE at ingress (strip PPPoE header)
1144 */
1145 if (msg->valid_flags & SFE_RULE_CREATE_PPPOE_DECAP_VALID) {
1146 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_DECAP;
1147 original_cm->pppoe_session_id = msg->pppoe_rule.flow_pppoe_session_id;
1148 ether_addr_copy(original_cm->pppoe_remote_mac, msg->pppoe_rule.flow_pppoe_remote_mac);
1149
1150 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_ENCAP;
1151 reply_cm->pppoe_session_id = msg->pppoe_rule.flow_pppoe_session_id;
1152 ether_addr_copy(reply_cm->pppoe_remote_mac, msg->pppoe_rule.flow_pppoe_remote_mac);
1153 }
1154
1155 if (msg->valid_flags & SFE_RULE_CREATE_PPPOE_ENCAP_VALID) {
1156 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_ENCAP;
1157 original_cm->pppoe_session_id = msg->pppoe_rule.return_pppoe_session_id;
1158 ether_addr_copy(original_cm->pppoe_remote_mac, msg->pppoe_rule.return_pppoe_remote_mac);
1159
1160 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_DECAP;
1161 reply_cm->pppoe_session_id = msg->pppoe_rule.return_pppoe_session_id;
1162 ether_addr_copy(reply_cm->pppoe_remote_mac, msg->pppoe_rule.return_pppoe_remote_mac);
1163 }
1164
1165 /*
Ken Zhubbf49652021-09-12 15:33:09 -07001166 * For the non-arp interface, we don't write L2 HDR.
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001167 */
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +05301168 if (sfe_ipv4_xmit_eth_type_check(dest_dev, original_cm->flags)) {
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301169
1170 /*
1171 * Check whether the rule has configured a specific source MAC address to use.
1172 * This is needed when virtual L3 interfaces such as br-lan, macvlan, vlan are used during egress
1173 */
1174 if ((msg->valid_flags & SFE_RULE_CREATE_SRC_MAC_VALID) &&
1175 (msg->src_mac_rule.mac_valid_flags & SFE_SRC_MAC_RETURN_VALID)) {
1176 ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)msg->src_mac_rule.return_src_mac);
1177 } else {
1178 ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)dest_dev->dev_addr);
1179 }
1180
1181 ether_addr_copy((u8 *)original_cm->xmit_dest_mac, (u8 *)msg->conn_rule.return_mac);
1182
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001183 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_L2_HDR;
1184
1185 /*
1186 * If our dev writes Ethernet headers then we can write a really fast
1187 * version.
1188 */
1189 if (dest_dev->header_ops) {
1190 if (dest_dev->header_ops->create == eth_header) {
1191 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR;
1192 }
1193 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001194 }
1195
1196 /*
1197 * Fill in the "reply" direction connection matching object.
1198 */
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001199 reply_cm->match_dev = dest_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301200 reply_cm->match_protocol = tuple->protocol;
1201 reply_cm->match_src_ip = msg->conn_rule.return_ip_xlate;
1202 reply_cm->match_src_port = msg->conn_rule.return_ident_xlate;
1203 reply_cm->match_dest_ip = msg->conn_rule.flow_ip_xlate;
1204 reply_cm->match_dest_port = msg->conn_rule.flow_ident_xlate;
1205
1206 reply_cm->xlate_src_ip = tuple->return_ip;
1207 reply_cm->xlate_src_port = tuple->return_ident;
1208 reply_cm->xlate_dest_ip = tuple->flow_ip;
1209 reply_cm->xlate_dest_port = tuple->flow_ident;;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301210
1211 atomic_set(&reply_cm->rx_packet_count, 0);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001212 reply_cm->rx_packet_count64 = 0;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301213 atomic_set(&reply_cm->rx_byte_count, 0);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001214 reply_cm->rx_byte_count64 = 0;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301215
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001216 reply_cm->xmit_dev = src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301217 reply_cm->xmit_dev_mtu = msg->conn_rule.flow_mtu;
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301218
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001219 reply_cm->connection = c;
1220 reply_cm->counter_match = original_cm;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301221 if (msg->valid_flags & SFE_RULE_CREATE_QOS_VALID) {
1222 reply_cm->priority = msg->qos_rule.return_qos_tag;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001223 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PRIORITY_REMARK;
1224 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301225 if (msg->valid_flags & SFE_RULE_CREATE_DSCP_MARKING_VALID) {
1226 reply_cm->dscp = msg->dscp_rule.return_dscp << SFE_IPV4_DSCP_SHIFT;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001227 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_DSCP_REMARK;
1228 }
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001229#ifdef CONFIG_NF_FLOW_COOKIE
1230 reply_cm->flow_cookie = 0;
1231#endif
Zhi Chen8748eb32015-06-18 12:58:48 -07001232#ifdef CONFIG_XFRM
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301233 if (msg->valid_flags & SFE_RULE_CREATE_DIRECTION_VALID) {
1234 reply_cm->flow_accel = msg->direction_rule.return_accel;
1235 } else {
1236 reply_cm->flow_accel = 1;
1237 }
1238
Zhi Chen8748eb32015-06-18 12:58:48 -07001239#endif
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301240 /*
1241 * If l2_features are disabled and flow uses l2 features such as macvlan/bridge/pppoe/vlan,
1242 * bottom interfaces are expected to be disabled in the flow rule and always top interfaces
1243 * are used. In such cases, do not use HW csum offload. csum offload is used only when we
1244 * are sending directly to the destination interface that supports it.
1245 */
1246 if (likely(src_dev->features & NETIF_F_HW_CSUM)) {
1247 if ((msg->conn_rule.flow_top_interface_num == msg->conn_rule.flow_interface_num) ||
1248 (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_FLOW_BOTTOM_INTERFACE)) {
1249 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_CSUM_OFFLOAD;
1250 }
1251 }
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001252
1253 /*
Ken Zhubbf49652021-09-12 15:33:09 -07001254 * For the non-arp interface, we don't write L2 HDR.
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001255 */
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +05301256 if (sfe_ipv4_xmit_eth_type_check(src_dev, reply_cm->flags)) {
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301257
1258 /*
1259 * Check whether the rule has configured a specific source MAC address to use.
1260 * This is needed when virtual L3 interfaces such as br-lan, macvlan, vlan are used during egress
1261 */
1262 if ((msg->valid_flags & SFE_RULE_CREATE_SRC_MAC_VALID) &&
1263 (msg->src_mac_rule.mac_valid_flags & SFE_SRC_MAC_FLOW_VALID)) {
1264 ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)msg->src_mac_rule.flow_src_mac);
1265 } else {
1266 ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)src_dev->dev_addr);
1267 }
1268 ether_addr_copy((u8 *)reply_cm->xmit_dest_mac, (u8 *)msg->conn_rule.flow_mac);
1269
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001270 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_L2_HDR;
1271
1272 /*
1273 * If our dev writes Ethernet headers then we can write a really fast
1274 * version.
1275 */
1276 if (src_dev->header_ops) {
1277 if (src_dev->header_ops->create == eth_header) {
1278 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR;
1279 }
1280 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001281 }
1282
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301283 if ((tuple->return_ip != msg->conn_rule.return_ip_xlate) ||
1284 (tuple->return_ident != msg->conn_rule.return_ident_xlate)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001285 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST;
1286 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC;
1287 }
1288
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301289 if ((tuple->flow_ip != msg->conn_rule.flow_ip_xlate) ||
1290 (tuple->flow_ident != msg->conn_rule.flow_ident_xlate)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001291 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC;
1292 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST;
1293 }
1294
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301295 c->protocol = tuple->protocol;
1296 c->src_ip = tuple->flow_ip;
1297 c->src_ip_xlate = msg->conn_rule.flow_ip_xlate;
1298 c->src_port = tuple->flow_ident;
1299 c->src_port_xlate = msg->conn_rule.flow_ident_xlate;
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001300 c->original_dev = src_dev;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001301 c->original_match = original_cm;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301302 c->dest_ip = tuple->return_ip;
1303 c->dest_ip_xlate = msg->conn_rule.return_ip_xlate;
1304 c->dest_port = tuple->return_ident;
1305 c->dest_port_xlate = msg->conn_rule.return_ident_xlate;
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001306 c->reply_dev = dest_dev;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001307 c->reply_match = reply_cm;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301308 c->mark = 0; /* TODO : no mark setting for create rule */
Xiaoping Fan34586472015-07-03 02:20:35 -07001309 c->debug_read_seq = 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001310 c->last_sync_jiffies = get_jiffies_64();
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301311 c->removed = false;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001312
1313 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001314 * Initialize the protocol-specific information that we track.
1315 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301316 switch (tuple->protocol) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001317 case IPPROTO_TCP:
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301318 original_cm->protocol_state.tcp.win_scale = msg->tcp_rule.flow_window_scale;
1319 original_cm->protocol_state.tcp.max_win = msg->tcp_rule.flow_max_window ? msg->tcp_rule.flow_max_window : 1;
1320 original_cm->protocol_state.tcp.end = msg->tcp_rule.flow_end;
1321 original_cm->protocol_state.tcp.max_end = msg->tcp_rule.flow_max_end;
1322
1323 reply_cm->protocol_state.tcp.win_scale = msg->tcp_rule.return_window_scale;
1324 reply_cm->protocol_state.tcp.max_win = msg->tcp_rule.return_max_window ? msg->tcp_rule.return_max_window : 1;
1325 reply_cm->protocol_state.tcp.end = msg->tcp_rule.return_end;
1326 reply_cm->protocol_state.tcp.max_end = msg->tcp_rule.return_max_end;
1327
1328 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_NO_SEQ_CHECK) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001329 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
1330 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
1331 }
1332 break;
1333 }
1334
1335 sfe_ipv4_connection_match_compute_translations(original_cm);
1336 sfe_ipv4_connection_match_compute_translations(reply_cm);
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301337 sfe_ipv4_insert_connection(si, c);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001338
1339 spin_unlock_bh(&si->lock);
1340
1341 /*
1342 * We have everything we need!
1343 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301344 DEBUG_INFO("new connection - p: %d\n"
Tian Yang45f39c82020-10-06 14:07:47 -07001345 " s: %s:%pxM(%pxM):%pI4(%pI4):%u(%u)\n"
1346 " d: %s:%pxM(%pxM):%pI4(%pI4):%u(%u)\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301347 tuple->protocol,
1348 src_dev->name, msg->conn_rule.flow_mac, NULL,
1349 &tuple->flow_ip, &msg->conn_rule.flow_ip_xlate, ntohs(tuple->flow_ident), ntohs(msg->conn_rule.flow_ident_xlate),
1350 dest_dev->name, NULL, msg->conn_rule.return_mac,
1351 &tuple->return_ip, &msg->conn_rule.return_ip_xlate, ntohs(tuple->return_ident), ntohs(msg->conn_rule.return_ident_xlate));
Nicolas Costa514fde02014-01-13 15:50:29 -06001352
1353 return 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001354}
1355
1356/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001357 * sfe_ipv4_destroy_rule()
1358 * Destroy a forwarding rule.
1359 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301360void sfe_ipv4_destroy_rule(struct sfe_ipv4_rule_destroy_msg *msg)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001361{
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001362 struct sfe_ipv4 *si = &__si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001363 struct sfe_ipv4_connection *c;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301364 bool ret;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301365 struct sfe_ipv4_5tuple *tuple = &msg->tuple;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001366
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301367 this_cpu_inc(si->stats_pcpu->connection_destroy_requests64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001368 spin_lock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001369
1370 /*
1371 * Check to see if we have a flow that matches the rule we're trying
1372 * to destroy. If there isn't then we can't destroy it.
1373 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301374 c = sfe_ipv4_find_connection(si, tuple->protocol, tuple->flow_ip, tuple->flow_ident,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301375 tuple->return_ip, tuple->return_ident);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001376 if (!c) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001377 spin_unlock_bh(&si->lock);
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301378 this_cpu_inc(si->stats_pcpu->connection_destroy_misses64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001379
1380 DEBUG_TRACE("connection does not exist - p: %d, s: %pI4:%u, d: %pI4:%u\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301381 tuple->protocol, &tuple->flow_ip, ntohs(tuple->flow_ident),
1382 &tuple->return_ip, ntohs(tuple->return_ident));
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001383 return;
1384 }
1385
1386 /*
1387 * Remove our connection details from the hash tables.
1388 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301389 ret = sfe_ipv4_remove_connection(si, c);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001390 spin_unlock_bh(&si->lock);
1391
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301392 if (ret) {
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301393 sfe_ipv4_flush_connection(si, c, SFE_SYNC_REASON_DESTROY);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301394 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001395
1396 DEBUG_INFO("connection destroyed - p: %d, s: %pI4:%u, d: %pI4:%u\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301397 tuple->protocol, &tuple->flow_ip, ntohs(tuple->flow_ident),
1398 &tuple->return_ip, ntohs(tuple->return_ident));
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001399}
1400
1401/*
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001402 * sfe_ipv4_register_sync_rule_callback()
1403 * Register a callback for rule synchronization.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001404 */
Xiaoping Fand44a5b42015-05-26 17:37:37 -07001405void sfe_ipv4_register_sync_rule_callback(sfe_sync_rule_callback_t sync_rule_callback)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001406{
1407 struct sfe_ipv4 *si = &__si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001408
1409 spin_lock_bh(&si->lock);
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001410 rcu_assign_pointer(si->sync_rule_callback, sync_rule_callback);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001411 spin_unlock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001412}
1413
1414/*
1415 * sfe_ipv4_get_debug_dev()
1416 */
1417static ssize_t sfe_ipv4_get_debug_dev(struct device *dev,
1418 struct device_attribute *attr,
1419 char *buf)
1420{
1421 struct sfe_ipv4 *si = &__si;
1422 ssize_t count;
1423 int num;
1424
1425 spin_lock_bh(&si->lock);
1426 num = si->debug_dev;
1427 spin_unlock_bh(&si->lock);
1428
1429 count = snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", num);
1430 return count;
1431}
1432
1433/*
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001434 * sysfs attributes.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001435 */
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001436static const struct device_attribute sfe_ipv4_debug_dev_attr =
Xiaoping Fane70da412016-02-26 16:47:57 -08001437 __ATTR(debug_dev, S_IWUSR | S_IRUGO, sfe_ipv4_get_debug_dev, NULL);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001438
1439/*
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001440 * sfe_ipv4_destroy_all_rules_for_dev()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001441 * Destroy all connections that match a particular device.
1442 *
1443 * If we pass dev as NULL then this destroys all connections.
1444 */
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001445void sfe_ipv4_destroy_all_rules_for_dev(struct net_device *dev)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001446{
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001447 struct sfe_ipv4 *si = &__si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001448 struct sfe_ipv4_connection *c;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301449 bool ret;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001450
Xiaoping Fan34586472015-07-03 02:20:35 -07001451another_round:
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001452 spin_lock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001453
Xiaoping Fan34586472015-07-03 02:20:35 -07001454 for (c = si->all_connections_head; c; c = c->all_connections_next) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001455 /*
Xiaoping Fan34586472015-07-03 02:20:35 -07001456 * Does this connection relate to the device we are destroying?
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001457 */
1458 if (!dev
1459 || (dev == c->original_dev)
1460 || (dev == c->reply_dev)) {
Xiaoping Fan34586472015-07-03 02:20:35 -07001461 break;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001462 }
Xiaoping Fan34586472015-07-03 02:20:35 -07001463 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001464
Xiaoping Fan34586472015-07-03 02:20:35 -07001465 if (c) {
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301466 ret = sfe_ipv4_remove_connection(si, c);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001467 }
1468
1469 spin_unlock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07001470
1471 if (c) {
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301472 if (ret) {
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301473 sfe_ipv4_flush_connection(si, c, SFE_SYNC_REASON_DESTROY);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301474 }
Xiaoping Fan34586472015-07-03 02:20:35 -07001475 goto another_round;
1476 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001477}
1478
1479/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001480 * sfe_ipv4_periodic_sync()
1481 */
Ken Zhu137722d2021-09-23 17:57:36 -07001482static void sfe_ipv4_periodic_sync(struct work_struct *work)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001483{
Ken Zhu137722d2021-09-23 17:57:36 -07001484 struct sfe_ipv4 *si = container_of((struct delayed_work *)work, struct sfe_ipv4, sync_dwork);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07001485 u64 now_jiffies;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001486 int quota;
Xiaoping Fand44a5b42015-05-26 17:37:37 -07001487 sfe_sync_rule_callback_t sync_rule_callback;
Ken Zhudc423672021-09-02 18:27:01 -07001488 struct sfe_ipv4_connection *c;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001489
1490 now_jiffies = get_jiffies_64();
1491
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001492 rcu_read_lock();
1493 sync_rule_callback = rcu_dereference(si->sync_rule_callback);
1494 if (!sync_rule_callback) {
1495 rcu_read_unlock();
1496 goto done;
1497 }
1498
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001499 spin_lock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001500
1501 /*
Ken Zhudc423672021-09-02 18:27:01 -07001502 * If we have reached the end of the connection list, walk from
1503 * the connection head.
1504 */
1505 c = si->wc_next;
1506 if (unlikely(!c)) {
1507 c = si->all_connections_head;
1508 }
1509
1510 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001511 * Get an estimate of the number of connections to parse in this sync.
1512 */
1513 quota = (si->num_connections + 63) / 64;
1514
1515 /*
Ken Zhudc423672021-09-02 18:27:01 -07001516 * Walk the "all connection" list and sync the connection state.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001517 */
Ken Zhudc423672021-09-02 18:27:01 -07001518 while (likely(c && quota)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001519 struct sfe_ipv4_connection_match *cm;
1520 struct sfe_ipv4_connection_match *counter_cm;
Xiaoping Fand44a5b42015-05-26 17:37:37 -07001521 struct sfe_connection_sync sis;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001522
Ken Zhudc423672021-09-02 18:27:01 -07001523 cm = c->original_match;
1524 counter_cm = c->reply_match;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001525
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001526 /*
Ken Zhudc423672021-09-02 18:27:01 -07001527 * Didn't receive packets in the original direction or reply
1528 * direction, move to the next connection.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001529 */
Ken Zhudc423672021-09-02 18:27:01 -07001530 if ((!atomic_read(&cm->rx_packet_count)) && !(atomic_read(&counter_cm->rx_packet_count))) {
1531 c = c->all_connections_next;
1532 continue;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001533 }
1534
Ken Zhudc423672021-09-02 18:27:01 -07001535 quota--;
Matthew McClintockaf48f1e2014-01-23 15:29:19 -06001536
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301537 sfe_ipv4_gen_sync_connection(si, c, &sis, SFE_SYNC_REASON_STATS, now_jiffies);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001538
Ken Zhudc423672021-09-02 18:27:01 -07001539 si->wc_next = c->all_connections_next;
1540
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001541 /*
1542 * We don't want to be holding the lock when we sync!
1543 */
1544 spin_unlock_bh(&si->lock);
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001545 sync_rule_callback(&sis);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001546 spin_lock_bh(&si->lock);
Ken Zhudc423672021-09-02 18:27:01 -07001547
1548 /*
1549 * c must be set and used in the same lock/unlock window;
1550 * because c could be removed when we don't hold the lock,
1551 * so delay grabbing until after the callback and relock.
1552 */
1553 c = si->wc_next;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001554 }
1555
Ken Zhudc423672021-09-02 18:27:01 -07001556 /*
1557 * At the end of the sync, put the wc_next to the connection we left.
1558 */
1559 si->wc_next = c;
1560
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001561 spin_unlock_bh(&si->lock);
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001562 rcu_read_unlock();
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001563
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001564done:
Ken Zhu137722d2021-09-23 17:57:36 -07001565 schedule_delayed_work_on(si->work_cpu, (struct delayed_work *)work, ((HZ + 99) / 100));
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001566}
1567
1568#define CHAR_DEV_MSG_SIZE 768
1569
1570/*
1571 * sfe_ipv4_debug_dev_read_start()
1572 * Generate part of the XML output.
1573 */
1574static bool sfe_ipv4_debug_dev_read_start(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
1575 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
1576{
1577 int bytes_read;
1578
Xiaoping Fan34586472015-07-03 02:20:35 -07001579 si->debug_read_seq++;
1580
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001581 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "<sfe_ipv4>\n");
1582 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1583 return false;
1584 }
1585
1586 *length -= bytes_read;
1587 *total_read += bytes_read;
1588
1589 ws->state++;
1590 return true;
1591}
1592
1593/*
1594 * sfe_ipv4_debug_dev_read_connections_start()
1595 * Generate part of the XML output.
1596 */
1597static bool sfe_ipv4_debug_dev_read_connections_start(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
1598 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
1599{
1600 int bytes_read;
1601
1602 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<connections>\n");
1603 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1604 return false;
1605 }
1606
1607 *length -= bytes_read;
1608 *total_read += bytes_read;
1609
1610 ws->state++;
1611 return true;
1612}
1613
1614/*
1615 * sfe_ipv4_debug_dev_read_connections_connection()
1616 * Generate part of the XML output.
1617 */
1618static bool sfe_ipv4_debug_dev_read_connections_connection(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
1619 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
1620{
1621 struct sfe_ipv4_connection *c;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001622 struct sfe_ipv4_connection_match *original_cm;
1623 struct sfe_ipv4_connection_match *reply_cm;
1624 int bytes_read;
1625 int protocol;
1626 struct net_device *src_dev;
Dave Hudson87973cd2013-10-22 16:00:04 +01001627 __be32 src_ip;
1628 __be32 src_ip_xlate;
1629 __be16 src_port;
1630 __be16 src_port_xlate;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07001631 u64 src_rx_packets;
1632 u64 src_rx_bytes;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001633 struct net_device *dest_dev;
Dave Hudson87973cd2013-10-22 16:00:04 +01001634 __be32 dest_ip;
1635 __be32 dest_ip_xlate;
1636 __be16 dest_port;
1637 __be16 dest_port_xlate;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07001638 u64 dest_rx_packets;
1639 u64 dest_rx_bytes;
1640 u64 last_sync_jiffies;
1641 u32 mark, src_priority, dest_priority, src_dscp, dest_dscp;
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301642 u32 packet, byte, original_cm_flags;
1643 u16 pppoe_session_id;
1644 u8 pppoe_remote_mac[ETH_ALEN];
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001645#ifdef CONFIG_NF_FLOW_COOKIE
1646 int src_flow_cookie, dst_flow_cookie;
1647#endif
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001648
1649 spin_lock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07001650
1651 for (c = si->all_connections_head; c; c = c->all_connections_next) {
1652 if (c->debug_read_seq < si->debug_read_seq) {
1653 c->debug_read_seq = si->debug_read_seq;
1654 break;
1655 }
1656 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001657
1658 /*
Xiaoping Fan34586472015-07-03 02:20:35 -07001659 * If there were no connections then move to the next state.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001660 */
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301661 if (!c || c->removed) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001662 spin_unlock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07001663 ws->state++;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001664 return true;
1665 }
1666
1667 original_cm = c->original_match;
1668 reply_cm = c->reply_match;
1669
1670 protocol = c->protocol;
1671 src_dev = c->original_dev;
1672 src_ip = c->src_ip;
1673 src_ip_xlate = c->src_ip_xlate;
1674 src_port = c->src_port;
1675 src_port_xlate = c->src_port_xlate;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001676 src_priority = original_cm->priority;
1677 src_dscp = original_cm->dscp >> SFE_IPV4_DSCP_SHIFT;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001678
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301679 sfe_ipv4_connection_match_update_summary_stats(original_cm, &packet, &byte);
1680 sfe_ipv4_connection_match_update_summary_stats(reply_cm, &packet, &byte);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001681
1682 src_rx_packets = original_cm->rx_packet_count64;
1683 src_rx_bytes = original_cm->rx_byte_count64;
1684 dest_dev = c->reply_dev;
1685 dest_ip = c->dest_ip;
1686 dest_ip_xlate = c->dest_ip_xlate;
1687 dest_port = c->dest_port;
1688 dest_port_xlate = c->dest_port_xlate;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001689 dest_priority = reply_cm->priority;
1690 dest_dscp = reply_cm->dscp >> SFE_IPV4_DSCP_SHIFT;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001691 dest_rx_packets = reply_cm->rx_packet_count64;
1692 dest_rx_bytes = reply_cm->rx_byte_count64;
1693 last_sync_jiffies = get_jiffies_64() - c->last_sync_jiffies;
Cristian Prundeanu592265e2013-12-26 11:01:22 -06001694 mark = c->mark;
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301695 original_cm_flags = original_cm->flags;
1696 pppoe_session_id = original_cm->pppoe_session_id;
1697 ether_addr_copy(pppoe_remote_mac, original_cm->pppoe_remote_mac);
1698
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001699#ifdef CONFIG_NF_FLOW_COOKIE
1700 src_flow_cookie = original_cm->flow_cookie;
1701 dst_flow_cookie = reply_cm->flow_cookie;
1702#endif
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001703 spin_unlock_bh(&si->lock);
1704
1705 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t\t<connection "
1706 "protocol=\"%u\" "
1707 "src_dev=\"%s\" "
1708 "src_ip=\"%pI4\" src_ip_xlate=\"%pI4\" "
1709 "src_port=\"%u\" src_port_xlate=\"%u\" "
Xiaoping Fane1963d42015-08-25 17:06:19 -07001710 "src_priority=\"%u\" src_dscp=\"%u\" "
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001711 "src_rx_pkts=\"%llu\" src_rx_bytes=\"%llu\" "
1712 "dest_dev=\"%s\" "
1713 "dest_ip=\"%pI4\" dest_ip_xlate=\"%pI4\" "
1714 "dest_port=\"%u\" dest_port_xlate=\"%u\" "
Xiaoping Fane1963d42015-08-25 17:06:19 -07001715 "dest_priority=\"%u\" dest_dscp=\"%u\" "
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001716 "dest_rx_pkts=\"%llu\" dest_rx_bytes=\"%llu\" "
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001717#ifdef CONFIG_NF_FLOW_COOKIE
1718 "src_flow_cookie=\"%d\" dst_flow_cookie=\"%d\" "
1719#endif
Cristian Prundeanu592265e2013-12-26 11:01:22 -06001720 "last_sync=\"%llu\" "
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301721 "mark=\"%08x\" ",
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001722 protocol,
1723 src_dev->name,
1724 &src_ip, &src_ip_xlate,
Dave Hudson87973cd2013-10-22 16:00:04 +01001725 ntohs(src_port), ntohs(src_port_xlate),
Xiaoping Fane1963d42015-08-25 17:06:19 -07001726 src_priority, src_dscp,
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001727 src_rx_packets, src_rx_bytes,
1728 dest_dev->name,
1729 &dest_ip, &dest_ip_xlate,
Dave Hudson87973cd2013-10-22 16:00:04 +01001730 ntohs(dest_port), ntohs(dest_port_xlate),
Xiaoping Fane1963d42015-08-25 17:06:19 -07001731 dest_priority, dest_dscp,
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001732 dest_rx_packets, dest_rx_bytes,
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001733#ifdef CONFIG_NF_FLOW_COOKIE
1734 src_flow_cookie, dst_flow_cookie,
1735#endif
Cristian Prundeanu592265e2013-12-26 11:01:22 -06001736 last_sync_jiffies, mark);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001737
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301738 if (original_cm_flags &= (SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_DECAP | SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_ENCAP)) {
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +05301739 bytes_read += snprintf(msg + bytes_read, CHAR_DEV_MSG_SIZE, "pppoe_session_id=\"%u\" pppoe_server MAC=\"%pM\" ",
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301740 pppoe_session_id, pppoe_remote_mac);
1741 }
1742
1743 bytes_read += snprintf(msg + bytes_read, CHAR_DEV_MSG_SIZE, "/>\n");
1744
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001745 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1746 return false;
1747 }
1748
1749 *length -= bytes_read;
1750 *total_read += bytes_read;
1751
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001752 return true;
1753}
1754
1755/*
1756 * sfe_ipv4_debug_dev_read_connections_end()
1757 * Generate part of the XML output.
1758 */
1759static bool sfe_ipv4_debug_dev_read_connections_end(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
1760 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
1761{
1762 int bytes_read;
1763
1764 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t</connections>\n");
1765 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1766 return false;
1767 }
1768
1769 *length -= bytes_read;
1770 *total_read += bytes_read;
1771
1772 ws->state++;
1773 return true;
1774}
1775
1776/*
1777 * sfe_ipv4_debug_dev_read_exceptions_start()
1778 * Generate part of the XML output.
1779 */
1780static bool sfe_ipv4_debug_dev_read_exceptions_start(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
1781 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
1782{
1783 int bytes_read;
1784
1785 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<exceptions>\n");
1786 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1787 return false;
1788 }
1789
1790 *length -= bytes_read;
1791 *total_read += bytes_read;
1792
1793 ws->state++;
1794 return true;
1795}
1796
1797/*
1798 * sfe_ipv4_debug_dev_read_exceptions_exception()
1799 * Generate part of the XML output.
1800 */
1801static bool sfe_ipv4_debug_dev_read_exceptions_exception(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
1802 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
1803{
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301804 int i;
1805 u64 val = 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001806
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301807 for_each_possible_cpu(i) {
1808 const struct sfe_ipv4_stats *s = per_cpu_ptr(si->stats_pcpu, i);
1809 val += s->exception_events64[ws->iter_exception];
1810 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001811
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301812 if (val) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001813 int bytes_read;
1814
1815 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE,
1816 "\t\t<exception name=\"%s\" count=\"%llu\" />\n",
1817 sfe_ipv4_exception_events_string[ws->iter_exception],
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301818 val);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001819 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1820 return false;
1821 }
1822
1823 *length -= bytes_read;
1824 *total_read += bytes_read;
1825 }
1826
1827 ws->iter_exception++;
1828 if (ws->iter_exception >= SFE_IPV4_EXCEPTION_EVENT_LAST) {
1829 ws->iter_exception = 0;
1830 ws->state++;
1831 }
1832
1833 return true;
1834}
1835
1836/*
1837 * sfe_ipv4_debug_dev_read_exceptions_end()
1838 * Generate part of the XML output.
1839 */
1840static bool sfe_ipv4_debug_dev_read_exceptions_end(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
1841 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
1842{
1843 int bytes_read;
1844
1845 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t</exceptions>\n");
1846 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1847 return false;
1848 }
1849
1850 *length -= bytes_read;
1851 *total_read += bytes_read;
1852
1853 ws->state++;
1854 return true;
1855}
1856
1857/*
1858 * sfe_ipv4_debug_dev_read_stats()
1859 * Generate part of the XML output.
1860 */
1861static bool sfe_ipv4_debug_dev_read_stats(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
1862 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
1863{
1864 int bytes_read;
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301865 struct sfe_ipv4_stats stats;
1866 unsigned int num_conn;
1867
1868 sfe_ipv4_update_summary_stats(si, &stats);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001869
1870 spin_lock_bh(&si->lock);
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301871 num_conn = si->num_connections;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001872 spin_unlock_bh(&si->lock);
1873
1874 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<stats "
1875 "num_connections=\"%u\" "
Xiaoping Fan59176422015-05-22 15:58:10 -07001876 "pkts_forwarded=\"%llu\" pkts_not_forwarded=\"%llu\" "
1877 "create_requests=\"%llu\" create_collisions=\"%llu\" "
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301878 "create_failures=\"%llu\" "
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001879 "destroy_requests=\"%llu\" destroy_misses=\"%llu\" "
1880 "flushes=\"%llu\" "
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +05301881 "hash_hits=\"%llu\" hash_reorders=\"%llu\" "
1882 "pppoe_encap_pkts_fwded=\"%llu\" "
1883 "pppoe_decap_pkts_fwded=\"%llu\" />\n",
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301884 num_conn,
1885 stats.packets_forwarded64,
1886 stats.packets_not_forwarded64,
1887 stats.connection_create_requests64,
1888 stats.connection_create_collisions64,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301889 stats.connection_create_failures64,
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301890 stats.connection_destroy_requests64,
1891 stats.connection_destroy_misses64,
1892 stats.connection_flushes64,
1893 stats.connection_match_hash_hits64,
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +05301894 stats.connection_match_hash_reorders64,
1895 stats.pppoe_encap_packets_forwarded64,
1896 stats.pppoe_decap_packets_forwarded64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001897 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1898 return false;
1899 }
1900
1901 *length -= bytes_read;
1902 *total_read += bytes_read;
1903
1904 ws->state++;
1905 return true;
1906}
1907
1908/*
1909 * sfe_ipv4_debug_dev_read_end()
1910 * Generate part of the XML output.
1911 */
1912static bool sfe_ipv4_debug_dev_read_end(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
1913 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
1914{
1915 int bytes_read;
1916
1917 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "</sfe_ipv4>\n");
1918 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1919 return false;
1920 }
1921
1922 *length -= bytes_read;
1923 *total_read += bytes_read;
1924
1925 ws->state++;
1926 return true;
1927}
1928
1929/*
1930 * Array of write functions that write various XML elements that correspond to
1931 * our XML output state machine.
1932 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07001933static sfe_ipv4_debug_xml_write_method_t sfe_ipv4_debug_xml_write_methods[SFE_IPV4_DEBUG_XML_STATE_DONE] = {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001934 sfe_ipv4_debug_dev_read_start,
1935 sfe_ipv4_debug_dev_read_connections_start,
1936 sfe_ipv4_debug_dev_read_connections_connection,
1937 sfe_ipv4_debug_dev_read_connections_end,
1938 sfe_ipv4_debug_dev_read_exceptions_start,
1939 sfe_ipv4_debug_dev_read_exceptions_exception,
1940 sfe_ipv4_debug_dev_read_exceptions_end,
1941 sfe_ipv4_debug_dev_read_stats,
1942 sfe_ipv4_debug_dev_read_end,
1943};
1944
1945/*
1946 * sfe_ipv4_debug_dev_read()
1947 * Send info to userspace upon read request from user
1948 */
1949static ssize_t sfe_ipv4_debug_dev_read(struct file *filp, char *buffer, size_t length, loff_t *offset)
1950{
1951 char msg[CHAR_DEV_MSG_SIZE];
1952 int total_read = 0;
1953 struct sfe_ipv4_debug_xml_write_state *ws;
1954 struct sfe_ipv4 *si = &__si;
1955
1956 ws = (struct sfe_ipv4_debug_xml_write_state *)filp->private_data;
1957 while ((ws->state != SFE_IPV4_DEBUG_XML_STATE_DONE) && (length > CHAR_DEV_MSG_SIZE)) {
1958 if ((sfe_ipv4_debug_xml_write_methods[ws->state])(si, buffer, msg, &length, &total_read, ws)) {
1959 continue;
1960 }
1961 }
1962
1963 return total_read;
1964}
1965
1966/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001967 * sfe_ipv4_debug_dev_open()
1968 */
1969static int sfe_ipv4_debug_dev_open(struct inode *inode, struct file *file)
1970{
1971 struct sfe_ipv4_debug_xml_write_state *ws;
1972
1973 ws = (struct sfe_ipv4_debug_xml_write_state *)file->private_data;
1974 if (!ws) {
1975 ws = kzalloc(sizeof(struct sfe_ipv4_debug_xml_write_state), GFP_KERNEL);
1976 if (!ws) {
1977 return -ENOMEM;
1978 }
1979
1980 ws->state = SFE_IPV4_DEBUG_XML_STATE_START;
1981 file->private_data = ws;
1982 }
1983
1984 return 0;
1985}
1986
1987/*
1988 * sfe_ipv4_debug_dev_release()
1989 */
1990static int sfe_ipv4_debug_dev_release(struct inode *inode, struct file *file)
1991{
1992 struct sfe_ipv4_debug_xml_write_state *ws;
1993
1994 ws = (struct sfe_ipv4_debug_xml_write_state *)file->private_data;
1995 if (ws) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001996 /*
1997 * We've finished with our output so free the write state.
1998 */
1999 kfree(ws);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05302000 file->private_data = NULL;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002001 }
2002
2003 return 0;
2004}
2005
2006/*
2007 * File operations used in the debug char device
2008 */
2009static struct file_operations sfe_ipv4_debug_dev_fops = {
2010 .read = sfe_ipv4_debug_dev_read,
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002011 .open = sfe_ipv4_debug_dev_open,
2012 .release = sfe_ipv4_debug_dev_release
2013};
2014
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08002015#ifdef CONFIG_NF_FLOW_COOKIE
2016/*
2017 * sfe_register_flow_cookie_cb
2018 * register a function in SFE to let SFE use this function to configure flow cookie for a flow
2019 *
2020 * Hardware driver which support flow cookie should register a callback function in SFE. Then SFE
2021 * can use this function to configure flow cookie for a flow.
2022 * return: 0, success; !=0, fail
2023 */
2024int sfe_register_flow_cookie_cb(flow_cookie_set_func_t cb)
2025{
2026 struct sfe_ipv4 *si = &__si;
2027
2028 BUG_ON(!cb);
2029
2030 if (si->flow_cookie_set_func) {
2031 return -1;
2032 }
2033
2034 rcu_assign_pointer(si->flow_cookie_set_func, cb);
2035 return 0;
2036}
2037
2038/*
2039 * sfe_unregister_flow_cookie_cb
2040 * unregister function which is used to configure flow cookie for a flow
2041 *
2042 * return: 0, success; !=0, fail
2043 */
2044int sfe_unregister_flow_cookie_cb(flow_cookie_set_func_t cb)
2045{
2046 struct sfe_ipv4 *si = &__si;
2047
2048 RCU_INIT_POINTER(si->flow_cookie_set_func, NULL);
2049 return 0;
2050}
Xiaoping Fan640faf42015-08-28 15:50:55 -07002051
2052/*
2053 * sfe_ipv4_get_flow_cookie()
2054 */
2055static ssize_t sfe_ipv4_get_flow_cookie(struct device *dev,
2056 struct device_attribute *attr,
2057 char *buf)
2058{
2059 struct sfe_ipv4 *si = &__si;
Xiaoping Fan01c67cc2015-11-09 11:31:57 -08002060 return snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", si->flow_cookie_enable);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002061}
2062
2063/*
2064 * sfe_ipv4_set_flow_cookie()
2065 */
2066static ssize_t sfe_ipv4_set_flow_cookie(struct device *dev,
2067 struct device_attribute *attr,
2068 const char *buf, size_t size)
2069{
2070 struct sfe_ipv4 *si = &__si;
Ken Zhu137722d2021-09-23 17:57:36 -07002071 si->flow_cookie_enable = simple_strtol(buf, NULL, 0);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002072
2073 return size;
2074}
2075
2076/*
2077 * sysfs attributes.
2078 */
2079static const struct device_attribute sfe_ipv4_flow_cookie_attr =
Xiaoping Fane70da412016-02-26 16:47:57 -08002080 __ATTR(flow_cookie_enable, S_IWUSR | S_IRUGO, sfe_ipv4_get_flow_cookie, sfe_ipv4_set_flow_cookie);
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08002081#endif /*CONFIG_NF_FLOW_COOKIE*/
2082
Ken Zhu137722d2021-09-23 17:57:36 -07002083/*
2084 * sfe_ipv4_get_cpu()
2085 */
2086static ssize_t sfe_ipv4_get_cpu(struct device *dev,
2087 struct device_attribute *attr,
2088 char *buf)
2089{
2090 struct sfe_ipv4 *si = &__si;
2091 return snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", si->work_cpu);
2092}
2093
2094/*
2095 * sfe_ipv4_set_cpu()
2096 */
2097static ssize_t sfe_ipv4_set_cpu(struct device *dev,
2098 struct device_attribute *attr,
2099 const char *buf, size_t size)
2100{
2101 struct sfe_ipv4 *si = &__si;
2102 int work_cpu;
2103 work_cpu = simple_strtol(buf, NULL, 0);
2104 if ((work_cpu >= 0) && (work_cpu <= NR_CPUS)) {
2105 si->work_cpu = work_cpu;
2106 } else {
2107 dev_err(dev, "%s is not in valid range[0,%d]", buf, NR_CPUS);
2108 }
2109 return size;
2110}
2111/*
2112 * sysfs attributes.
2113 */
2114static const struct device_attribute sfe_ipv4_cpu_attr =
2115 __ATTR(stats_work_cpu, S_IWUSR | S_IRUGO, sfe_ipv4_get_cpu, sfe_ipv4_set_cpu);
2116
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05302117 /*
2118 * sfe_ipv4_conn_match_hash_init()
2119 * Initialize conn match hash lists
2120 */
2121static void sfe_ipv4_conn_match_hash_init(struct sfe_ipv4 *si, int len)
2122{
2123 struct hlist_head *hash_list = si->hlist_conn_match_hash_head;
2124 int i;
2125
2126 for (i = 0; i < len; i++) {
2127 INIT_HLIST_HEAD(&hash_list[i]);
2128 }
2129}
2130
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002131/*
Dave Hudson87973cd2013-10-22 16:00:04 +01002132 * sfe_ipv4_init()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002133 */
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05302134int sfe_ipv4_init(void)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002135{
2136 struct sfe_ipv4 *si = &__si;
2137 int result = -1;
2138
Dave Hudsondcd08fb2013-11-22 09:25:16 -06002139 DEBUG_INFO("SFE IPv4 init\n");
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002140
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05302141 sfe_ipv4_conn_match_hash_init(si, ARRAY_SIZE(si->hlist_conn_match_hash_head));
2142
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302143 si->stats_pcpu = alloc_percpu_gfp(struct sfe_ipv4_stats, GFP_KERNEL | __GFP_ZERO);
2144 if (!si->stats_pcpu) {
2145 DEBUG_ERROR("failed to allocate stats memory for sfe_ipv4\n");
2146 goto exit0;
2147 }
2148
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002149 /*
2150 * Create sys/sfe_ipv4
2151 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302152 si->sys_ipv4 = kobject_create_and_add("sfe_ipv4", NULL);
2153 if (!si->sys_ipv4) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002154 DEBUG_ERROR("failed to register sfe_ipv4\n");
2155 goto exit1;
2156 }
2157
2158 /*
2159 * Create files, one for each parameter supported by this module.
2160 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302161 result = sysfs_create_file(si->sys_ipv4, &sfe_ipv4_debug_dev_attr.attr);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002162 if (result) {
2163 DEBUG_ERROR("failed to register debug dev file: %d\n", result);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002164 goto exit2;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002165 }
2166
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302167 result = sysfs_create_file(si->sys_ipv4, &sfe_ipv4_cpu_attr.attr);
Ken Zhu137722d2021-09-23 17:57:36 -07002168 if (result) {
2169 DEBUG_ERROR("failed to register debug dev file: %d\n", result);
2170 goto exit3;
2171 }
2172
Xiaoping Fan640faf42015-08-28 15:50:55 -07002173#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302174 result = sysfs_create_file(si->sys_ipv4, &sfe_ipv4_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002175 if (result) {
2176 DEBUG_ERROR("failed to register flow cookie enable file: %d\n", result);
Ken Zhu137722d2021-09-23 17:57:36 -07002177 goto exit4;
Xiaoping Fan640faf42015-08-28 15:50:55 -07002178 }
2179#endif /* CONFIG_NF_FLOW_COOKIE */
2180
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002181 /*
2182 * Register our debug char device.
2183 */
2184 result = register_chrdev(0, "sfe_ipv4", &sfe_ipv4_debug_dev_fops);
2185 if (result < 0) {
2186 DEBUG_ERROR("Failed to register chrdev: %d\n", result);
Ken Zhu137722d2021-09-23 17:57:36 -07002187 goto exit5;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002188 }
2189
2190 si->debug_dev = result;
Ken Zhu137722d2021-09-23 17:57:36 -07002191 si->work_cpu = WORK_CPU_UNBOUND;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002192
2193 /*
Ken Zhu137722d2021-09-23 17:57:36 -07002194 * Create a work to handle periodic statistics.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002195 */
Ken Zhu137722d2021-09-23 17:57:36 -07002196 INIT_DELAYED_WORK(&(si->sync_dwork), sfe_ipv4_periodic_sync);
2197 schedule_delayed_work_on(si->work_cpu, &(si->sync_dwork), ((HZ + 99) / 100));
2198
Dave Hudson87973cd2013-10-22 16:00:04 +01002199 spin_lock_init(&si->lock);
Dave Hudson87973cd2013-10-22 16:00:04 +01002200 return 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002201
Ken Zhu137722d2021-09-23 17:57:36 -07002202exit5:
Xiaoping Fan640faf42015-08-28 15:50:55 -07002203#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302204 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002205
Ken Zhu137722d2021-09-23 17:57:36 -07002206exit4:
Xiaoping Fan640faf42015-08-28 15:50:55 -07002207#endif /* CONFIG_NF_FLOW_COOKIE */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302208 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_cpu_attr.attr);
Ken Zhu137722d2021-09-23 17:57:36 -07002209exit3:
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302210 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_debug_dev_attr.attr);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002211
Xiaoping Fan640faf42015-08-28 15:50:55 -07002212exit2:
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302213 kobject_put(si->sys_ipv4);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002214
2215exit1:
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302216 free_percpu(si->stats_pcpu);
2217
2218exit0:
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002219 return result;
2220}
2221
2222/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002223 * sfe_ipv4_exit()
2224 */
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05302225void sfe_ipv4_exit(void)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002226{
Dave Hudson87973cd2013-10-22 16:00:04 +01002227 struct sfe_ipv4 *si = &__si;
2228
Dave Hudsondcd08fb2013-11-22 09:25:16 -06002229 DEBUG_INFO("SFE IPv4 exit\n");
Dave Hudson87973cd2013-10-22 16:00:04 +01002230 /*
2231 * Destroy all connections.
2232 */
Dave Hudsondcd08fb2013-11-22 09:25:16 -06002233 sfe_ipv4_destroy_all_rules_for_dev(NULL);
Dave Hudson87973cd2013-10-22 16:00:04 +01002234
Ken Zhu137722d2021-09-23 17:57:36 -07002235 cancel_delayed_work_sync(&si->sync_dwork);
Dave Hudson87973cd2013-10-22 16:00:04 +01002236
Dave Hudson87973cd2013-10-22 16:00:04 +01002237 unregister_chrdev(si->debug_dev, "sfe_ipv4");
2238
Xiaoping Fan640faf42015-08-28 15:50:55 -07002239#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302240 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002241#endif /* CONFIG_NF_FLOW_COOKIE */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302242 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_debug_dev_attr.attr);
2243 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_cpu_attr.attr);
Dave Hudson87973cd2013-10-22 16:00:04 +01002244
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302245 kobject_put(si->sys_ipv4);
Dave Hudson87973cd2013-10-22 16:00:04 +01002246
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302247 free_percpu(si->stats_pcpu);
2248
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002249}
2250
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08002251#ifdef CONFIG_NF_FLOW_COOKIE
2252EXPORT_SYMBOL(sfe_register_flow_cookie_cb);
2253EXPORT_SYMBOL(sfe_unregister_flow_cookie_cb);
2254#endif