blob: 6315e62050728511c8eecc82e6680c27d77bfb77 [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 Prathyusha5f27e232022-01-06 14:39:04 +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>
Amitesh Anand63be37d2021-12-24 20:51:48 +053026#include <net/udp.h>
27#include <net/vxlan.h>
Dave Hudsondcd08fb2013-11-22 09:25:16 -060028#include <linux/etherdevice.h>
Tian Yang45f39c82020-10-06 14:07:47 -070029#include <linux/version.h>
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +053030#include <linux/lockdep.h>
Amitesh Anand63be37d2021-12-24 20:51:48 +053031#include <linux/refcount.h>
32#include <linux/netfilter.h>
33#include <linux/inetdevice.h>
34#include <linux/netfilter_ipv4.h>
Nitin Shettye6ed5b52021-12-27 14:50:11 +053035#include <net/protocol.h>
36#include <net/gre.h>
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010037
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +053038#include "sfe_debug.h"
Ratheesh Kannoth89302a72021-10-20 08:10:37 +053039#include "sfe_api.h"
Dave Hudsondcd08fb2013-11-22 09:25:16 -060040#include "sfe.h"
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +053041#include "sfe_flow_cookie.h"
42#include "sfe_ipv4.h"
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +053043#include "sfe_ipv4_udp.h"
44#include "sfe_ipv4_tcp.h"
45#include "sfe_ipv4_icmp.h"
Wayne Tanbb7f1782021-12-13 11:16:04 -080046#include "sfe_pppoe.h"
Nitin Shettye6ed5b52021-12-27 14:50:11 +053047#include "sfe_ipv4_gre.h"
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010048
49static char *sfe_ipv4_exception_events_string[SFE_IPV4_EXCEPTION_EVENT_LAST] = {
50 "UDP_HEADER_INCOMPLETE",
51 "UDP_NO_CONNECTION",
52 "UDP_IP_OPTIONS_OR_INITIAL_FRAGMENT",
53 "UDP_SMALL_TTL",
54 "UDP_NEEDS_FRAGMENTATION",
55 "TCP_HEADER_INCOMPLETE",
56 "TCP_NO_CONNECTION_SLOW_FLAGS",
57 "TCP_NO_CONNECTION_FAST_FLAGS",
58 "TCP_IP_OPTIONS_OR_INITIAL_FRAGMENT",
59 "TCP_SMALL_TTL",
60 "TCP_NEEDS_FRAGMENTATION",
61 "TCP_FLAGS",
62 "TCP_SEQ_EXCEEDS_RIGHT_EDGE",
63 "TCP_SMALL_DATA_OFFS",
64 "TCP_BAD_SACK",
65 "TCP_BIG_DATA_OFFS",
66 "TCP_SEQ_BEFORE_LEFT_EDGE",
67 "TCP_ACK_EXCEEDS_RIGHT_EDGE",
68 "TCP_ACK_BEFORE_LEFT_EDGE",
69 "ICMP_HEADER_INCOMPLETE",
70 "ICMP_UNHANDLED_TYPE",
71 "ICMP_IPV4_HEADER_INCOMPLETE",
72 "ICMP_IPV4_NON_V4",
73 "ICMP_IPV4_IP_OPTIONS_INCOMPLETE",
74 "ICMP_IPV4_UDP_HEADER_INCOMPLETE",
75 "ICMP_IPV4_TCP_HEADER_INCOMPLETE",
76 "ICMP_IPV4_UNHANDLED_PROTOCOL",
77 "ICMP_NO_CONNECTION",
78 "ICMP_FLUSHED_CONNECTION",
79 "HEADER_INCOMPLETE",
Ratheesh Kannoth43d64f82021-10-20 08:23:29 +053080 "HEADER_CSUM_BAD",
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010081 "BAD_TOTAL_LENGTH",
82 "NON_V4",
83 "NON_INITIAL_FRAGMENT",
84 "DATAGRAM_INCOMPLETE",
85 "IP_OPTIONS_INCOMPLETE",
Ratheesh Kannoth5dee3772022-01-18 11:27:14 +053086 "UNHANDLED_PROTOCOL",
87 "INVALID_SOURCE_INTERFACE",
Nitin Shettye6ed5b52021-12-27 14:50:11 +053088 "GRE_HEADER_INCOMPLETE",
89 "GRE_NO_CONNECTION",
90 "GRE_IP_OPTIONS_OR_INITIAL_FRAGMENT",
91 "GRE_SMALL_TTL",
92 "GRE_NEEDS_FRAGMENTATION"
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010093};
94
Xiaoping Fan6a1672f2016-08-17 19:58:12 -070095static struct sfe_ipv4 __si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010096
97/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010098 * sfe_ipv4_gen_ip_csum()
99 * Generate the IP checksum for an IPv4 header.
100 *
101 * Note that this function assumes that we have only 20 bytes of IP header.
102 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530103u16 sfe_ipv4_gen_ip_csum(struct iphdr *iph)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100104{
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700105 u32 sum;
106 u16 *i = (u16 *)iph;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100107
108 iph->check = 0;
109
110 /*
111 * Generate the sum.
112 */
113 sum = i[0] + i[1] + i[2] + i[3] + i[4] + i[5] + i[6] + i[7] + i[8] + i[9];
114
115 /*
116 * Fold it to ones-complement form.
117 */
118 sum = (sum & 0xffff) + (sum >> 16);
119 sum = (sum & 0xffff) + (sum >> 16);
120
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700121 return (u16)sum ^ 0xffff;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100122}
123
124/*
125 * sfe_ipv4_get_connection_match_hash()
126 * Generate the hash used in connection match lookups.
127 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700128static inline unsigned int sfe_ipv4_get_connection_match_hash(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 Kannoth5dee3772022-01-18 11:27:14 +0530132 u32 hash = ntohl(src_ip ^ dest_ip) ^ protocol ^ ntohs(src_port ^ dest_port);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100133 return ((hash >> SFE_IPV4_CONNECTION_HASH_SHIFT) ^ hash) & SFE_IPV4_CONNECTION_HASH_MASK;
134}
135
136/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530137 * sfe_ipv4_find_connection_match_rcu()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100138 * Get the IPv4 flow match info that corresponds to a particular 5-tuple.
139 *
140 * On entry we must be holding the lock that protects the hash table.
141 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530142struct sfe_ipv4_connection_match *
143sfe_ipv4_find_connection_match_rcu(struct sfe_ipv4 *si, struct net_device *dev, u8 protocol,
Dave Hudson87973cd2013-10-22 16:00:04 +0100144 __be32 src_ip, __be16 src_port,
145 __be32 dest_ip, __be16 dest_port)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100146{
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530147 struct sfe_ipv4_connection_match *cm = NULL;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100148 unsigned int conn_match_idx;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530149 struct hlist_head *lhead;
150
151 WARN_ON_ONCE(!rcu_read_lock_held());
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100152
153 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 +0100154
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530155 lhead = &si->hlist_conn_match_hash_head[conn_match_idx];
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100156
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530157 hlist_for_each_entry_rcu(cm, lhead, hnode) {
158 if (cm->match_src_port != src_port
159 || cm->match_dest_port != dest_port
160 || cm->match_src_ip != src_ip
161 || cm->match_dest_ip != dest_ip
Ratheesh Kannoth5dee3772022-01-18 11:27:14 +0530162 || cm->match_protocol != protocol) {
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530163 continue;
164 }
165
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530166 this_cpu_inc(si->stats_pcpu->connection_match_hash_hits64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100167
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530168 break;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100169 }
170
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100171 return cm;
172}
173
174/*
175 * sfe_ipv4_connection_match_update_summary_stats()
176 * Update the summary stats for a connection match entry.
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530177 *
178 * Stats are incremented atomically. So use atomic substraction to update summary
179 * stats.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100180 */
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530181static inline void sfe_ipv4_connection_match_update_summary_stats(struct sfe_ipv4_connection_match *cm,
182 u32 *packets, u32 *bytes)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100183{
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530184 u32 packet_count, byte_count;
185
186 packet_count = atomic_read(&cm->rx_packet_count);
187 cm->rx_packet_count64 += packet_count;
188 atomic_sub(packet_count, &cm->rx_packet_count);
189
190 byte_count = atomic_read(&cm->rx_byte_count);
191 cm->rx_byte_count64 += byte_count;
192 atomic_sub(byte_count, &cm->rx_byte_count);
193
194 *packets = packet_count;
195 *bytes = byte_count;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100196}
197
198/*
199 * sfe_ipv4_connection_match_compute_translations()
200 * Compute port and address translations for a connection match entry.
201 */
202static void sfe_ipv4_connection_match_compute_translations(struct sfe_ipv4_connection_match *cm)
203{
204 /*
205 * Before we insert the entry look to see if this is tagged as doing address
206 * translations. If it is then work out the adjustment that we need to apply
207 * to the transport checksum.
208 */
209 if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC) {
210 /*
211 * Precompute an incremental checksum adjustment so we can
212 * edit packets in this stream very quickly. The algorithm is from RFC1624.
213 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700214 u16 src_ip_hi = cm->match_src_ip >> 16;
215 u16 src_ip_lo = cm->match_src_ip & 0xffff;
216 u32 xlate_src_ip = ~cm->xlate_src_ip;
217 u16 xlate_src_ip_hi = xlate_src_ip >> 16;
218 u16 xlate_src_ip_lo = xlate_src_ip & 0xffff;
219 u16 xlate_src_port = ~cm->xlate_src_port;
220 u32 adj;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100221
222 /*
223 * When we compute this fold it down to a 16-bit offset
224 * as that way we can avoid having to do a double
225 * folding of the twos-complement result because the
226 * addition of 2 16-bit values cannot cause a double
227 * wrap-around!
228 */
229 adj = src_ip_hi + src_ip_lo + cm->match_src_port
230 + xlate_src_ip_hi + xlate_src_ip_lo + xlate_src_port;
231 adj = (adj & 0xffff) + (adj >> 16);
232 adj = (adj & 0xffff) + (adj >> 16);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700233 cm->xlate_src_csum_adjustment = (u16)adj;
Nicolas Costaac2979c2014-01-14 10:35:24 -0600234
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100235 }
236
237 if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST) {
238 /*
239 * Precompute an incremental checksum adjustment so we can
240 * edit packets in this stream very quickly. The algorithm is from RFC1624.
241 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700242 u16 dest_ip_hi = cm->match_dest_ip >> 16;
243 u16 dest_ip_lo = cm->match_dest_ip & 0xffff;
244 u32 xlate_dest_ip = ~cm->xlate_dest_ip;
245 u16 xlate_dest_ip_hi = xlate_dest_ip >> 16;
246 u16 xlate_dest_ip_lo = xlate_dest_ip & 0xffff;
247 u16 xlate_dest_port = ~cm->xlate_dest_port;
248 u32 adj;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100249
250 /*
251 * When we compute this fold it down to a 16-bit offset
252 * as that way we can avoid having to do a double
253 * folding of the twos-complement result because the
254 * addition of 2 16-bit values cannot cause a double
255 * wrap-around!
256 */
257 adj = dest_ip_hi + dest_ip_lo + cm->match_dest_port
258 + xlate_dest_ip_hi + xlate_dest_ip_lo + xlate_dest_port;
259 adj = (adj & 0xffff) + (adj >> 16);
260 adj = (adj & 0xffff) + (adj >> 16);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700261 cm->xlate_dest_csum_adjustment = (u16)adj;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100262 }
Xiaoping Fanad755af2015-04-01 16:58:46 -0700263
264 if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC) {
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700265 u32 adj = ~cm->match_src_ip + cm->xlate_src_ip;
Xiaoping Fanad755af2015-04-01 16:58:46 -0700266 if (adj < cm->xlate_src_ip) {
267 adj++;
268 }
269
270 adj = (adj & 0xffff) + (adj >> 16);
271 adj = (adj & 0xffff) + (adj >> 16);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700272 cm->xlate_src_partial_csum_adjustment = (u16)adj;
Xiaoping Fanad755af2015-04-01 16:58:46 -0700273 }
274
275 if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST) {
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700276 u32 adj = ~cm->match_dest_ip + cm->xlate_dest_ip;
Xiaoping Fanad755af2015-04-01 16:58:46 -0700277 if (adj < cm->xlate_dest_ip) {
278 adj++;
279 }
280
281 adj = (adj & 0xffff) + (adj >> 16);
282 adj = (adj & 0xffff) + (adj >> 16);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700283 cm->xlate_dest_partial_csum_adjustment = (u16)adj;
Xiaoping Fanad755af2015-04-01 16:58:46 -0700284 }
285
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100286}
287
288/*
289 * sfe_ipv4_update_summary_stats()
290 * Update the summary stats.
291 */
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530292static void sfe_ipv4_update_summary_stats(struct sfe_ipv4 *si, struct sfe_ipv4_stats *stats)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100293{
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530294 int i = 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100295
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530296 memset(stats, 0, sizeof(*stats));
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100297
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530298 for_each_possible_cpu(i) {
299 const struct sfe_ipv4_stats *s = per_cpu_ptr(si->stats_pcpu, i);
300
301 stats->connection_create_requests64 += s->connection_create_requests64;
302 stats->connection_create_collisions64 += s->connection_create_collisions64;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530303 stats->connection_create_failures64 += s->connection_create_failures64;
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530304 stats->connection_destroy_requests64 += s->connection_destroy_requests64;
305 stats->connection_destroy_misses64 += s->connection_destroy_misses64;
306 stats->connection_match_hash_hits64 += s->connection_match_hash_hits64;
307 stats->connection_match_hash_reorders64 += s->connection_match_hash_reorders64;
308 stats->connection_flushes64 += s->connection_flushes64;
Amitesh Anand63be37d2021-12-24 20:51:48 +0530309 stats->packets_dropped64 += s->packets_dropped64;
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530310 stats->packets_forwarded64 += s->packets_forwarded64;
Ken Zhu7e38d1a2021-11-30 17:31:46 -0800311 stats->packets_fast_xmited64 += s->packets_fast_xmited64;
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530312 stats->packets_not_forwarded64 += s->packets_not_forwarded64;
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +0530313 stats->pppoe_encap_packets_forwarded64 += s->pppoe_encap_packets_forwarded64;
314 stats->pppoe_decap_packets_forwarded64 += s->pppoe_decap_packets_forwarded64;
Guduri Prathyusha034d6352022-01-12 16:49:04 +0530315 stats->pppoe_bridge_packets_forwarded64 += s->pppoe_bridge_packets_forwarded64;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100316 }
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530317
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100318}
319
320/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530321 * sfe_ipv4_insert_connection_match()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100322 * Insert a connection match into the hash.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100323 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530324static inline void sfe_ipv4_insert_connection_match(struct sfe_ipv4 *si,
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700325 struct sfe_ipv4_connection_match *cm)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100326{
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100327 unsigned int conn_match_idx
328 = sfe_ipv4_get_connection_match_hash(cm->match_dev, cm->match_protocol,
329 cm->match_src_ip, cm->match_src_port,
330 cm->match_dest_ip, cm->match_dest_port);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700331
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530332 lockdep_assert_held(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100333
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530334 hlist_add_head_rcu(&cm->hnode, &si->hlist_conn_match_hash_head[conn_match_idx]);
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800335#ifdef CONFIG_NF_FLOW_COOKIE
Xiaoping Fan640faf42015-08-28 15:50:55 -0700336 if (!si->flow_cookie_enable)
337 return;
338
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800339 /*
340 * Configure hardware to put a flow cookie in packet of this flow,
341 * then we can accelerate the lookup process when we received this packet.
342 */
343 for (conn_match_idx = 1; conn_match_idx < SFE_FLOW_COOKIE_SIZE; conn_match_idx++) {
344 struct sfe_flow_cookie_entry *entry = &si->sfe_flow_cookie_table[conn_match_idx];
345
346 if ((NULL == entry->match) && time_is_before_jiffies(entry->last_clean_time + HZ)) {
347 flow_cookie_set_func_t func;
348
349 rcu_read_lock();
350 func = rcu_dereference(si->flow_cookie_set_func);
351 if (func) {
Xiaoping Fan59176422015-05-22 15:58:10 -0700352 if (!func(cm->match_protocol, cm->match_src_ip, cm->match_src_port,
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800353 cm->match_dest_ip, cm->match_dest_port, conn_match_idx)) {
354 entry->match = cm;
355 cm->flow_cookie = conn_match_idx;
356 }
357 }
358 rcu_read_unlock();
359
360 break;
361 }
362 }
363#endif
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100364}
365
366/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530367 * sfe_ipv4_remove_connection_match()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100368 * Remove a connection match object from the hash.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100369 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530370static inline void sfe_ipv4_remove_connection_match(struct sfe_ipv4 *si, struct sfe_ipv4_connection_match *cm)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100371{
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530372
373 lockdep_assert_held(&si->lock);
374
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800375#ifdef CONFIG_NF_FLOW_COOKIE
Xiaoping Fan640faf42015-08-28 15:50:55 -0700376 if (si->flow_cookie_enable) {
377 /*
378 * Tell hardware that we no longer need a flow cookie in packet of this flow
379 */
380 unsigned int conn_match_idx;
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800381
Xiaoping Fan640faf42015-08-28 15:50:55 -0700382 for (conn_match_idx = 1; conn_match_idx < SFE_FLOW_COOKIE_SIZE; conn_match_idx++) {
383 struct sfe_flow_cookie_entry *entry = &si->sfe_flow_cookie_table[conn_match_idx];
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800384
Xiaoping Fan640faf42015-08-28 15:50:55 -0700385 if (cm == entry->match) {
386 flow_cookie_set_func_t func;
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800387
Xiaoping Fan640faf42015-08-28 15:50:55 -0700388 rcu_read_lock();
389 func = rcu_dereference(si->flow_cookie_set_func);
390 if (func) {
391 func(cm->match_protocol, cm->match_src_ip, cm->match_src_port,
392 cm->match_dest_ip, cm->match_dest_port, 0);
393 }
394 rcu_read_unlock();
395
396 cm->flow_cookie = 0;
397 entry->match = NULL;
398 entry->last_clean_time = jiffies;
399 break;
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800400 }
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800401 }
402 }
403#endif
404
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530405 hlist_del_init_rcu(&cm->hnode);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100406
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100407}
408
409/*
410 * sfe_ipv4_get_connection_hash()
411 * Generate the hash used in connection lookups.
412 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700413static inline unsigned int sfe_ipv4_get_connection_hash(u8 protocol, __be32 src_ip, __be16 src_port,
Dave Hudson87973cd2013-10-22 16:00:04 +0100414 __be32 dest_ip, __be16 dest_port)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100415{
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700416 u32 hash = ntohl(src_ip ^ dest_ip) ^ protocol ^ ntohs(src_port ^ dest_port);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100417 return ((hash >> SFE_IPV4_CONNECTION_HASH_SHIFT) ^ hash) & SFE_IPV4_CONNECTION_HASH_MASK;
418}
419
420/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530421 * sfe_ipv4_find_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100422 * Get the IPv4 connection info that corresponds to a particular 5-tuple.
423 *
424 * On entry we must be holding the lock that protects the hash table.
425 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530426static inline struct sfe_ipv4_connection *sfe_ipv4_find_connection(struct sfe_ipv4 *si, u32 protocol,
Dave Hudson87973cd2013-10-22 16:00:04 +0100427 __be32 src_ip, __be16 src_port,
428 __be32 dest_ip, __be16 dest_port)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100429{
430 struct sfe_ipv4_connection *c;
431 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 +0530432
433 lockdep_assert_held(&si->lock);
434
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100435 c = si->conn_hash[conn_idx];
436
437 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100438 * Will need connection entry for next create/destroy metadata,
439 * So no need to re-order entry for these requests
440 */
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530441 while (c) {
442 if ((c->src_port == src_port)
443 && (c->dest_port == dest_port)
444 && (c->src_ip == src_ip)
445 && (c->dest_ip == dest_ip)
446 && (c->protocol == protocol)) {
447 return c;
448 }
449
450 c = c->next;
451 }
452
453 return NULL;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100454}
455
456/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530457 * sfe_ipv4_insert_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100458 * Insert a connection into the hash.
459 *
460 * On entry we must be holding the lock that protects the hash table.
461 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530462static void sfe_ipv4_insert_connection(struct sfe_ipv4 *si, struct sfe_ipv4_connection *c)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100463{
464 struct sfe_ipv4_connection **hash_head;
465 struct sfe_ipv4_connection *prev_head;
466 unsigned int conn_idx;
467
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530468 lockdep_assert_held(&si->lock);
469
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100470 /*
471 * Insert entry into the connection hash.
472 */
473 conn_idx = sfe_ipv4_get_connection_hash(c->protocol, c->src_ip, c->src_port,
474 c->dest_ip, c->dest_port);
475 hash_head = &si->conn_hash[conn_idx];
476 prev_head = *hash_head;
477 c->prev = NULL;
478 if (prev_head) {
479 prev_head->prev = c;
480 }
481
482 c->next = prev_head;
483 *hash_head = c;
484
485 /*
486 * Insert entry into the "all connections" list.
487 */
488 if (si->all_connections_tail) {
489 c->all_connections_prev = si->all_connections_tail;
490 si->all_connections_tail->all_connections_next = c;
491 } else {
492 c->all_connections_prev = NULL;
493 si->all_connections_head = c;
494 }
495
496 si->all_connections_tail = c;
497 c->all_connections_next = NULL;
498 si->num_connections++;
499
500 /*
501 * Insert the connection match objects too.
502 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530503 sfe_ipv4_insert_connection_match(si, c->original_match);
504 sfe_ipv4_insert_connection_match(si, c->reply_match);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100505}
506
507/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530508 * sfe_ipv4_remove_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100509 * Remove a sfe_ipv4_connection object from the hash.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100510 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530511bool sfe_ipv4_remove_connection(struct sfe_ipv4 *si, struct sfe_ipv4_connection *c)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100512{
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530513 lockdep_assert_held(&si->lock);
514
515 if (c->removed) {
516 DEBUG_ERROR("%px: Connection has been removed already\n", c);
517 return false;
518 }
519
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100520 /*
521 * Remove the connection match objects.
522 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530523 sfe_ipv4_remove_connection_match(si, c->reply_match);
524 sfe_ipv4_remove_connection_match(si, c->original_match);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100525
526 /*
527 * Unlink the connection.
528 */
529 if (c->prev) {
530 c->prev->next = c->next;
531 } else {
532 unsigned int conn_idx = sfe_ipv4_get_connection_hash(c->protocol, c->src_ip, c->src_port,
533 c->dest_ip, c->dest_port);
534 si->conn_hash[conn_idx] = c->next;
535 }
536
537 if (c->next) {
538 c->next->prev = c->prev;
539 }
Xiaoping Fan34586472015-07-03 02:20:35 -0700540
541 /*
542 * Unlink connection from all_connections list
543 */
544 if (c->all_connections_prev) {
545 c->all_connections_prev->all_connections_next = c->all_connections_next;
546 } else {
547 si->all_connections_head = c->all_connections_next;
548 }
549
550 if (c->all_connections_next) {
551 c->all_connections_next->all_connections_prev = c->all_connections_prev;
552 } else {
553 si->all_connections_tail = c->all_connections_prev;
554 }
555
Ken Zhudc423672021-09-02 18:27:01 -0700556 /*
557 * If I am the next sync connection, move the sync to my next or head.
558 */
559 if (unlikely(si->wc_next == c)) {
560 si->wc_next = c->all_connections_next;
561 }
562
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530563 c->removed = true;
Xiaoping Fan34586472015-07-03 02:20:35 -0700564 si->num_connections--;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530565 return true;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100566}
567
568/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530569 * sfe_ipv4_gen_sync_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100570 * Sync a connection.
571 *
572 * On entry to this function we expect that the lock for the connection is either
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530573 * already held (while called from sfe_ipv4_periodic_sync() or isn't required
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530574 * (while called from sfe_ipv4_flush_connection())
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100575 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530576static void sfe_ipv4_gen_sync_connection(struct sfe_ipv4 *si, struct sfe_ipv4_connection *c,
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700577 struct sfe_connection_sync *sis, sfe_sync_reason_t reason,
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700578 u64 now_jiffies)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100579{
580 struct sfe_ipv4_connection_match *original_cm;
581 struct sfe_ipv4_connection_match *reply_cm;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530582 u32 packet_count, byte_count;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100583
584 /*
585 * Fill in the update message.
586 */
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700587 sis->is_v6 = 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100588 sis->protocol = c->protocol;
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700589 sis->src_ip.ip = c->src_ip;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700590 sis->src_ip_xlate.ip = c->src_ip_xlate;
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700591 sis->dest_ip.ip = c->dest_ip;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700592 sis->dest_ip_xlate.ip = c->dest_ip_xlate;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100593 sis->src_port = c->src_port;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700594 sis->src_port_xlate = c->src_port_xlate;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100595 sis->dest_port = c->dest_port;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700596 sis->dest_port_xlate = c->dest_port_xlate;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100597
598 original_cm = c->original_match;
599 reply_cm = c->reply_match;
600 sis->src_td_max_window = original_cm->protocol_state.tcp.max_win;
601 sis->src_td_end = original_cm->protocol_state.tcp.end;
602 sis->src_td_max_end = original_cm->protocol_state.tcp.max_end;
603 sis->dest_td_max_window = reply_cm->protocol_state.tcp.max_win;
604 sis->dest_td_end = reply_cm->protocol_state.tcp.end;
605 sis->dest_td_max_end = reply_cm->protocol_state.tcp.max_end;
606
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530607 sfe_ipv4_connection_match_update_summary_stats(original_cm, &packet_count, &byte_count);
608 sis->src_new_packet_count = packet_count;
609 sis->src_new_byte_count = byte_count;
Matthew McClintockd0cdb802014-02-24 16:30:35 -0600610
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530611 sfe_ipv4_connection_match_update_summary_stats(reply_cm, &packet_count, &byte_count);
612 sis->dest_new_packet_count = packet_count;
613 sis->dest_new_byte_count = byte_count;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100614
Matthew McClintockd0cdb802014-02-24 16:30:35 -0600615 sis->src_dev = original_cm->match_dev;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100616 sis->src_packet_count = original_cm->rx_packet_count64;
617 sis->src_byte_count = original_cm->rx_byte_count64;
Matthew McClintockd0cdb802014-02-24 16:30:35 -0600618
619 sis->dest_dev = reply_cm->match_dev;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100620 sis->dest_packet_count = reply_cm->rx_packet_count64;
621 sis->dest_byte_count = reply_cm->rx_byte_count64;
622
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700623 sis->reason = reason;
624
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100625 /*
626 * Get the time increment since our last sync.
627 */
628 sis->delta_jiffies = now_jiffies - c->last_sync_jiffies;
629 c->last_sync_jiffies = now_jiffies;
630}
631
632/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530633 * sfe_ipv4_free_connection_rcu()
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530634 * Called at RCU qs state to free the connection object.
635 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530636static void sfe_ipv4_free_connection_rcu(struct rcu_head *head)
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530637{
638 struct sfe_ipv4_connection *c;
Amitesh Anand63be37d2021-12-24 20:51:48 +0530639 struct udp_sock *up;
640 struct sock *sk;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530641
642 /*
643 * We dont need spin lock as the connection is already removed from link list
644 */
645 c = container_of(head, struct sfe_ipv4_connection, rcu);
646
647 BUG_ON(!c->removed);
648
649 DEBUG_TRACE("%px: connecton has been deleted\n", c);
650
651 /*
Amitesh Anand63be37d2021-12-24 20:51:48 +0530652 * Decrease the refcount taken in function sfe_ipv4_create_rule(),
653 * during call of __udp4_lib_lookup()
654 */
655 up = c->reply_match->up;
656 if (up) {
657 sk = (struct sock *)up;
658 sock_put(sk);
659 }
660
661 /*
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530662 * Release our hold of the source and dest devices and free the memory
663 * for our connection objects.
664 */
665 dev_put(c->original_dev);
666 dev_put(c->reply_dev);
667 kfree(c->original_match);
668 kfree(c->reply_match);
669 kfree(c);
670}
671
672/*
Ken Zhu88c58152021-12-09 15:12:06 -0800673 * sfe_ipv4_sync_status()
674 * update a connection status to its connection manager.
675 *
676 * si: the ipv4 context
677 * c: which connection to be notified
678 * reason: what kind of notification: flush, stats or destroy
679 */
680void sfe_ipv4_sync_status(struct sfe_ipv4 *si,
681 struct sfe_ipv4_connection *c,
682 sfe_sync_reason_t reason)
683{
684 struct sfe_connection_sync sis;
685 u64 now_jiffies;
686 sfe_sync_rule_callback_t sync_rule_callback;
687
688 rcu_read_lock();
689 sync_rule_callback = rcu_dereference(si->sync_rule_callback);
690 if (!sync_rule_callback) {
691 rcu_read_unlock();
692 return;
693 }
694
695 /*
696 * Generate a sync message and then sync.
697 */
698 now_jiffies = get_jiffies_64();
699 sfe_ipv4_gen_sync_connection(si, c, &sis, reason, now_jiffies);
700 sync_rule_callback(&sis);
701
702 rcu_read_unlock();
703}
704
705/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530706 * sfe_ipv4_flush_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100707 * Flush a connection and free all associated resources.
708 *
709 * We need to be called with bottom halves disabled locally as we need to acquire
710 * the connection hash lock and release it again. In general we're actually called
711 * from within a BH and so we're fine, but we're also called when connections are
712 * torn down.
713 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530714void sfe_ipv4_flush_connection(struct sfe_ipv4 *si,
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700715 struct sfe_ipv4_connection *c,
716 sfe_sync_reason_t reason)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100717{
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530718 BUG_ON(!c->removed);
719
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530720 this_cpu_inc(si->stats_pcpu->connection_flushes64);
Ken Zhu88c58152021-12-09 15:12:06 -0800721 sfe_ipv4_sync_status(si, c, reason);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100722
723 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100724 * Release our hold of the source and dest devices and free the memory
725 * for our connection objects.
726 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530727 call_rcu(&c->rcu, sfe_ipv4_free_connection_rcu);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100728}
729
730/*
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530731 * sfe_ipv4_exception_stats_inc()
732 * Increment exception stats.
733 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530734void sfe_ipv4_exception_stats_inc(struct sfe_ipv4 *si, enum sfe_ipv4_exception_events reason)
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530735{
736 struct sfe_ipv4_stats *stats = this_cpu_ptr(si->stats_pcpu);
737 stats->exception_events64[reason]++;
738 stats->packets_not_forwarded64++;
739}
740
741/*
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530742 * sfe_ipv4_is_loal_ip()
743 * Returns true if IP is local; returns false otherwise.
744 */
745static bool sfe_ipv4_is_local_ip(struct sfe_ipv4 *si, __be32 ip_addr)
746{
747 struct net_device *dev;
748
749 dev = ip_dev_find(&init_net, ip_addr);
750 if (dev) {
751 dev_put(dev);
752 return true;
753 }
754
755 return false;
756}
757
758/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100759 * sfe_ipv4_recv()
Matthew McClintocka8ad7962014-01-16 16:49:30 -0600760 * Handle packet receives and forwaring.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100761 *
762 * Returns 1 if the packet is forwarded or 0 if it isn't.
763 */
Amitesh Anand63be37d2021-12-24 20:51:48 +0530764int sfe_ipv4_recv(struct net_device *dev, struct sk_buff *skb, struct sfe_l2_info *l2_info, bool tun_outer)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100765{
766 struct sfe_ipv4 *si = &__si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100767 unsigned int len;
768 unsigned int tot_len;
769 unsigned int frag_off;
770 unsigned int ihl;
Ken Zhu88c58152021-12-09 15:12:06 -0800771 bool sync_on_find;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100772 bool ip_options;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530773 struct iphdr *iph;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700774 u32 protocol;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100775
776 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100777 * Check that we have space for an IP header here.
778 */
779 len = skb->len;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530780 if (unlikely(!pskb_may_pull(skb, sizeof(struct iphdr)))) {
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530781 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_HEADER_INCOMPLETE);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100782 DEBUG_TRACE("len: %u is too short\n", len);
783 return 0;
784 }
785
786 /*
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +0530787 * Validate ip csum if necessary. If ip_summed is set to CHECKSUM_UNNECESSARY, it is assumed
788 * that the L3 checksum is validated by the Rx interface or the tunnel interface that has
789 * generated the packet.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100790 */
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530791 iph = (struct iphdr *)skb->data;
Ratheesh Kannoth43d64f82021-10-20 08:23:29 +0530792 if (unlikely(skb->ip_summed != CHECKSUM_UNNECESSARY) && (ip_fast_csum((u8 *)iph, iph->ihl))) {
793 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_HEADER_CSUM_BAD);
794
795 DEBUG_TRACE("Bad IPv4 header csum: 0x%x\n", iph->check);
796 return 0;
797 }
798
799 /*
800 * Check that our "total length" is large enough for an IP header.
801 */
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100802 tot_len = ntohs(iph->tot_len);
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530803 if (unlikely(tot_len < sizeof(struct iphdr))) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100804
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530805 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_BAD_TOTAL_LENGTH);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100806 DEBUG_TRACE("tot_len: %u is too short\n", tot_len);
807 return 0;
808 }
809
810 /*
811 * Is our IP version wrong?
812 */
813 if (unlikely(iph->version != 4)) {
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530814 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_NON_V4);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100815 DEBUG_TRACE("IP version: %u\n", iph->version);
816 return 0;
817 }
818
819 /*
820 * Does our datagram fit inside the skb?
821 */
822 if (unlikely(tot_len > len)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100823 DEBUG_TRACE("tot_len: %u, exceeds len: %u\n", tot_len, len);
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530824 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_DATAGRAM_INCOMPLETE);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100825 return 0;
826 }
827
828 /*
829 * Do we have a non-initial fragment?
Nicolas Costaac2979c2014-01-14 10:35:24 -0600830 */
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100831 frag_off = ntohs(iph->frag_off);
832 if (unlikely(frag_off & IP_OFFSET)) {
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530833 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_NON_INITIAL_FRAGMENT);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100834 DEBUG_TRACE("non-initial fragment\n");
835 return 0;
836 }
837
838 /*
839 * If we have a (first) fragment then mark it to cause any connection to flush.
840 */
Ken Zhu88c58152021-12-09 15:12:06 -0800841 sync_on_find = unlikely(frag_off & IP_MF) ? true : false;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100842
843 /*
844 * Do we have any IP options? That's definite a slow path! If we do have IP
845 * options we need to recheck our header size.
846 */
847 ihl = iph->ihl << 2;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530848 ip_options = unlikely(ihl != sizeof(struct iphdr)) ? true : false;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100849 if (unlikely(ip_options)) {
850 if (unlikely(len < ihl)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100851
852 DEBUG_TRACE("len: %u is too short for header of size: %u\n", len, ihl);
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530853 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_IP_OPTIONS_INCOMPLETE);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100854 return 0;
855 }
856
Ken Zhu88c58152021-12-09 15:12:06 -0800857 sync_on_find = true;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100858 }
859
860 protocol = iph->protocol;
861 if (IPPROTO_UDP == protocol) {
Ken Zhu88c58152021-12-09 15:12:06 -0800862 return sfe_ipv4_recv_udp(si, skb, dev, len, iph, ihl, sync_on_find, l2_info, tun_outer);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100863 }
864
865 if (IPPROTO_TCP == protocol) {
Ken Zhu88c58152021-12-09 15:12:06 -0800866 return sfe_ipv4_recv_tcp(si, skb, dev, len, iph, ihl, sync_on_find, l2_info);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100867 }
868
869 if (IPPROTO_ICMP == protocol) {
870 return sfe_ipv4_recv_icmp(si, skb, dev, len, iph, ihl);
871 }
872
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530873#ifdef SFE_GRE_TUN_ENABLE
874 if (IPPROTO_GRE == protocol) {
Nitin Shetty2114a892022-01-28 20:03:56 +0530875 return sfe_ipv4_recv_gre(si, skb, dev, len, iph, ihl, sync_on_find, l2_info, tun_outer);
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530876 }
877#endif
878
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530879 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_UNHANDLED_PROTOCOL);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100880
881 DEBUG_TRACE("not UDP, TCP or ICMP: %u\n", protocol);
882 return 0;
883}
884
Nicolas Costa436926b2014-01-14 10:36:22 -0600885static void
886sfe_ipv4_update_tcp_state(struct sfe_ipv4_connection *c,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530887 struct sfe_ipv4_rule_create_msg *msg)
Nicolas Costa436926b2014-01-14 10:36:22 -0600888{
889 struct sfe_ipv4_connection_match *orig_cm;
890 struct sfe_ipv4_connection_match *repl_cm;
891 struct sfe_ipv4_tcp_connection_match *orig_tcp;
892 struct sfe_ipv4_tcp_connection_match *repl_tcp;
893
894 orig_cm = c->original_match;
895 repl_cm = c->reply_match;
896 orig_tcp = &orig_cm->protocol_state.tcp;
897 repl_tcp = &repl_cm->protocol_state.tcp;
898
899 /* update orig */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530900 if (orig_tcp->max_win < msg->tcp_rule.flow_max_window) {
901 orig_tcp->max_win = msg->tcp_rule.flow_max_window;
Nicolas Costa436926b2014-01-14 10:36:22 -0600902 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530903 if ((s32)(orig_tcp->end - msg->tcp_rule.flow_end) < 0) {
904 orig_tcp->end = msg->tcp_rule.flow_end;
Nicolas Costa436926b2014-01-14 10:36:22 -0600905 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530906 if ((s32)(orig_tcp->max_end - msg->tcp_rule.flow_max_end) < 0) {
907 orig_tcp->max_end = msg->tcp_rule.flow_max_end;
Nicolas Costa436926b2014-01-14 10:36:22 -0600908 }
909
910 /* update reply */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530911 if (repl_tcp->max_win < msg->tcp_rule.return_max_window) {
912 repl_tcp->max_win = msg->tcp_rule.return_max_window;
Nicolas Costa436926b2014-01-14 10:36:22 -0600913 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530914 if ((s32)(repl_tcp->end - msg->tcp_rule.return_end) < 0) {
915 repl_tcp->end = msg->tcp_rule.return_end;
Nicolas Costa436926b2014-01-14 10:36:22 -0600916 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530917 if ((s32)(repl_tcp->max_end - msg->tcp_rule.return_max_end) < 0) {
918 repl_tcp->max_end = msg->tcp_rule.return_max_end;
Nicolas Costa436926b2014-01-14 10:36:22 -0600919 }
920
921 /* update match flags */
922 orig_cm->flags &= ~SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
923 repl_cm->flags &= ~SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530924 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_NO_SEQ_CHECK) {
925
Nicolas Costa436926b2014-01-14 10:36:22 -0600926 orig_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
927 repl_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
928 }
929}
930
931static void
932sfe_ipv4_update_protocol_state(struct sfe_ipv4_connection *c,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530933 struct sfe_ipv4_rule_create_msg *msg)
Nicolas Costa436926b2014-01-14 10:36:22 -0600934{
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530935 switch (msg->tuple.protocol) {
Nicolas Costa436926b2014-01-14 10:36:22 -0600936 case IPPROTO_TCP:
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530937 sfe_ipv4_update_tcp_state(c, msg);
Nicolas Costa436926b2014-01-14 10:36:22 -0600938 break;
939 }
940}
941
Wayne Tanbb7f1782021-12-13 11:16:04 -0800942/*
943 * sfe_ipv4_match_entry_set_vlan()
944 */
945static void sfe_ipv4_match_entry_set_vlan(
946 struct sfe_ipv4_connection_match *cm,
947 u32 primary_ingress_vlan_tag,
948 u32 primary_egress_vlan_tag,
949 u32 secondary_ingress_vlan_tag,
950 u32 secondary_egress_vlan_tag)
951{
952 u16 tpid;
953 /*
954 * Prevent stacking header counts when updating.
955 */
956 cm->ingress_vlan_hdr_cnt = 0;
957 cm->egress_vlan_hdr_cnt = 0;
958 memset(cm->ingress_vlan_hdr, 0, sizeof(cm->ingress_vlan_hdr));
959 memset(cm->egress_vlan_hdr, 0, sizeof(cm->egress_vlan_hdr));
960
961 /*
962 * vlan_hdr[0] corresponds to outer tag
963 * vlan_hdr[1] corresponds to inner tag
964 * Extract the vlan information (tpid and tci) from rule message
965 */
966 if ((primary_ingress_vlan_tag & VLAN_VID_MASK) != SFE_VLAN_ID_NOT_CONFIGURED) {
967 tpid = (u16)(primary_ingress_vlan_tag >> 16);
968 cm->ingress_vlan_hdr[0].tpid = ntohs(tpid);
969 cm->ingress_vlan_hdr[0].tci = (u16)primary_ingress_vlan_tag;
970 cm->ingress_vlan_hdr_cnt++;
971 }
972
973 if ((secondary_ingress_vlan_tag & VLAN_VID_MASK) != SFE_VLAN_ID_NOT_CONFIGURED) {
974 tpid = (u16)(secondary_ingress_vlan_tag >> 16);
975 cm->ingress_vlan_hdr[1].tpid = ntohs(tpid);
976 cm->ingress_vlan_hdr[1].tci = (u16)secondary_ingress_vlan_tag;
977 cm->ingress_vlan_hdr_cnt++;
978 }
979
980 if ((primary_egress_vlan_tag & VLAN_VID_MASK) != SFE_VLAN_ID_NOT_CONFIGURED) {
981 tpid = (u16)(primary_egress_vlan_tag >> 16);
982 cm->egress_vlan_hdr[0].tpid = ntohs(tpid);
983 cm->egress_vlan_hdr[0].tci = (u16)primary_egress_vlan_tag;
984 cm->egress_vlan_hdr_cnt++;
985 }
986
987 if ((secondary_egress_vlan_tag & VLAN_VID_MASK) != SFE_VLAN_ID_NOT_CONFIGURED) {
988 tpid = (u16)(secondary_egress_vlan_tag >> 16);
989 cm->egress_vlan_hdr[1].tpid = ntohs(tpid);
990 cm->egress_vlan_hdr[1].tci = (u16)secondary_egress_vlan_tag;
991 cm->egress_vlan_hdr_cnt++;
992 }
993}
994
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530995void sfe_ipv4_update_rule(struct sfe_ipv4_rule_create_msg *msg)
Nicolas Costa436926b2014-01-14 10:36:22 -0600996{
997 struct sfe_ipv4_connection *c;
998 struct sfe_ipv4 *si = &__si;
999
1000 spin_lock_bh(&si->lock);
1001
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301002 c = sfe_ipv4_find_connection(si,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301003 msg->tuple.protocol,
1004 msg->tuple.flow_ip,
1005 msg->tuple.flow_ident,
1006 msg->tuple.return_ip,
1007 msg->tuple.return_ident);
Nicolas Costa436926b2014-01-14 10:36:22 -06001008 if (c != NULL) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301009 sfe_ipv4_update_protocol_state(c, msg);
Nicolas Costa436926b2014-01-14 10:36:22 -06001010 }
1011
1012 spin_unlock_bh(&si->lock);
1013}
1014
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001015/*
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +05301016 * sfe_ipv4_xmit_eth_type_check()
1017 * Checking if MAC header has to be written.
1018 */
1019static inline bool sfe_ipv4_xmit_eth_type_check(struct net_device *dev, u32 cm_flags)
1020{
1021 if (!(dev->flags & IFF_NOARP)) {
1022 return true;
1023 }
1024
1025 /*
1026 * For PPPoE, since we are now supporting PPPoE encapsulation, we are writing L2 header.
1027 */
1028 if (unlikely(cm_flags & SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_ENCAP)) {
1029 return true;
1030 }
1031
1032 return false;
1033}
1034
1035/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001036 * sfe_ipv4_create_rule()
1037 * Create a forwarding rule.
1038 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301039int sfe_ipv4_create_rule(struct sfe_ipv4_rule_create_msg *msg)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001040{
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001041 struct sfe_ipv4 *si = &__si;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301042 struct sfe_ipv4_connection *c, *c_old;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001043 struct sfe_ipv4_connection_match *original_cm;
1044 struct sfe_ipv4_connection_match *reply_cm;
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001045 struct net_device *dest_dev;
1046 struct net_device *src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301047 struct sfe_ipv4_5tuple *tuple = &msg->tuple;
Suruchi Sumanc1a4a612021-10-21 14:50:23 +05301048 s32 flow_interface_num = msg->conn_rule.flow_top_interface_num;
1049 s32 return_interface_num = msg->conn_rule.return_top_interface_num;
Amitesh Anand63be37d2021-12-24 20:51:48 +05301050 struct net *net;
1051 struct sock *sk;
1052 unsigned int src_if_idx;
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001053
Suruchi Sumanc1a4a612021-10-21 14:50:23 +05301054 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_FLOW_BOTTOM_INTERFACE) {
1055 flow_interface_num = msg->conn_rule.flow_interface_num;
1056 }
1057
1058 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_RETURN_BOTTOM_INTERFACE) {
1059 return_interface_num = msg->conn_rule.return_interface_num;
1060 }
1061
1062 src_dev = dev_get_by_index(&init_net, flow_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301063 if (!src_dev) {
1064 DEBUG_WARN("%px: Unable to find src_dev corresponding to %d\n", msg,
Suruchi Sumanc1a4a612021-10-21 14:50:23 +05301065 flow_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301066 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
1067 return -EINVAL;
1068 }
1069
Suruchi Sumanc1a4a612021-10-21 14:50:23 +05301070 dest_dev = dev_get_by_index(&init_net, return_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301071 if (!dest_dev) {
1072 DEBUG_WARN("%px: Unable to find dest_dev corresponding to %d\n", msg,
Suruchi Sumanc1a4a612021-10-21 14:50:23 +05301073 return_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301074 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
1075 dev_put(src_dev);
1076 return -EINVAL;
1077 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001078
Matthew McClintock389b42a2014-09-24 14:05:51 -05001079 if (unlikely((dest_dev->reg_state != NETREG_REGISTERED) ||
1080 (src_dev->reg_state != NETREG_REGISTERED))) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301081 dev_put(src_dev);
1082 dev_put(dest_dev);
1083 DEBUG_WARN("%px: src_dev=%s and dest_dev=%s are unregistered\n", msg,
1084 src_dev->name, dest_dev->name);
1085 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
Matthew McClintock389b42a2014-09-24 14:05:51 -05001086 return -EINVAL;
1087 }
1088
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301089 /*
1090 * Allocate the various connection tracking objects.
1091 */
1092 c = (struct sfe_ipv4_connection *)kmalloc(sizeof(struct sfe_ipv4_connection), GFP_ATOMIC);
1093 if (unlikely(!c)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301094 DEBUG_WARN("%px: memory allocation of connection entry failed\n", msg);
1095 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
1096 dev_put(src_dev);
1097 dev_put(dest_dev);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301098 return -ENOMEM;
1099 }
1100
1101 original_cm = (struct sfe_ipv4_connection_match *)kmalloc(sizeof(struct sfe_ipv4_connection_match), GFP_ATOMIC);
1102 if (unlikely(!original_cm)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301103 DEBUG_WARN("%px: memory allocation of connection match entry failed\n", msg);
1104 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301105 kfree(c);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301106 dev_put(src_dev);
1107 dev_put(dest_dev);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301108 return -ENOMEM;
1109 }
1110
1111 reply_cm = (struct sfe_ipv4_connection_match *)kmalloc(sizeof(struct sfe_ipv4_connection_match), GFP_ATOMIC);
1112 if (unlikely(!reply_cm)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301113 DEBUG_WARN("%px: memory allocation of connection match entry failed\n", msg);
1114 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301115 kfree(original_cm);
1116 kfree(c);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301117 dev_put(src_dev);
1118 dev_put(dest_dev);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301119 return -ENOMEM;
1120 }
1121
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301122 this_cpu_inc(si->stats_pcpu->connection_create_requests64);
1123
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001124 spin_lock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001125
1126 /*
Nicolas Costa436926b2014-01-14 10:36:22 -06001127 * Check to see if there is already a flow that matches the rule we're
1128 * trying to create. If there is then we can't create a new one.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001129 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301130 c_old = sfe_ipv4_find_connection(si,
Wayne Tanbb7f1782021-12-13 11:16:04 -08001131 msg->tuple.protocol,
1132 msg->tuple.flow_ip,
1133 msg->tuple.flow_ident,
1134 msg->tuple.return_ip,
1135 msg->tuple.return_ident);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301136
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301137 if (c_old != NULL) {
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301138 this_cpu_inc(si->stats_pcpu->connection_create_collisions64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001139
1140 /*
Nicolas Costa436926b2014-01-14 10:36:22 -06001141 * If we already have the flow then it's likely that this
1142 * request to create the connection rule contains more
1143 * up-to-date information. Check and update accordingly.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001144 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301145 sfe_ipv4_update_protocol_state(c, msg);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001146 spin_unlock_bh(&si->lock);
1147
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301148 kfree(reply_cm);
1149 kfree(original_cm);
1150 kfree(c);
1151
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301152 dev_put(src_dev);
1153 dev_put(dest_dev);
1154
Amitesh Anand63be37d2021-12-24 20:51:48 +05301155 DEBUG_TRACE("%px: connection already exists - p:%d\n"
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301156 " s: %s:%pM:%pI4:%u, d: %s:%pM:%pI4:%u\n",
Amitesh Anand63be37d2021-12-24 20:51:48 +05301157 msg, tuple->protocol,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301158 src_dev->name, msg->conn_rule.flow_mac, &tuple->flow_ip, ntohs(tuple->flow_ident),
1159 dest_dev->name, msg->conn_rule.return_mac, &tuple->return_ip, ntohs(tuple->return_ident));
1160
Nicolas Costa514fde02014-01-13 15:50:29 -06001161 return -EADDRINUSE;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001162 }
1163
1164 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001165 * Fill in the "original" direction connection matching object.
1166 * Note that the transmit MAC address is "dest_mac_xlate" because
1167 * we always know both ends of a connection by their translated
1168 * addresses and not their public addresses.
1169 */
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001170 original_cm->match_dev = src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301171 original_cm->match_protocol = tuple->protocol;
1172 original_cm->match_src_ip = tuple->flow_ip;
Suruchi Suman66609a72022-01-20 02:34:25 +05301173 original_cm->match_src_port = netif_is_vxlan(src_dev) ? 0 : tuple->flow_ident;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301174 original_cm->match_dest_ip = tuple->return_ip;
1175 original_cm->match_dest_port = tuple->return_ident;
1176
1177 original_cm->xlate_src_ip = msg->conn_rule.flow_ip_xlate;
1178 original_cm->xlate_src_port = msg->conn_rule.flow_ident_xlate;
1179 original_cm->xlate_dest_ip = msg->conn_rule.return_ip_xlate;
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301180 original_cm->xlate_dest_port = msg->conn_rule.return_ident_xlate;
1181
1182 if (tuple->protocol == IPPROTO_GRE) {
1183 /*
1184 * the PPTP is 4 tuple lookup.
1185 * During th rule lookup destination call id from packet
1186 * is matched against destination port in cm.
1187 */
1188 original_cm->match_src_port = 0;
1189 original_cm->xlate_src_port = 0;
1190 }
Wayne Tanbb7f1782021-12-13 11:16:04 -08001191
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301192 atomic_set(&original_cm->rx_packet_count, 0);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001193 original_cm->rx_packet_count64 = 0;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301194 atomic_set(&original_cm->rx_byte_count, 0);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001195 original_cm->rx_byte_count64 = 0;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301196
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001197 original_cm->xmit_dev = dest_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301198 original_cm->xmit_dev_mtu = msg->conn_rule.return_mtu;
1199
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001200 original_cm->connection = c;
1201 original_cm->counter_match = reply_cm;
Wayne Tanbb7f1782021-12-13 11:16:04 -08001202 original_cm->l2_hdr_size = 0;
1203 original_cm->flags = 0;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301204
Amitesh Anand63be37d2021-12-24 20:51:48 +05301205 /*
1206 * UDP Socket is valid only in decap direction.
1207 */
1208 RCU_INIT_POINTER(original_cm->up, NULL);
1209
Ken Zhu37040ea2021-09-09 21:11:15 -07001210 if (msg->valid_flags & SFE_RULE_CREATE_MARK_VALID) {
1211 original_cm->mark = msg->mark_rule.flow_mark;
1212 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_MARK;
1213 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301214 if (msg->valid_flags & SFE_RULE_CREATE_QOS_VALID) {
1215 original_cm->priority = msg->qos_rule.flow_qos_tag;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001216 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PRIORITY_REMARK;
1217 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301218 if (msg->valid_flags & SFE_RULE_CREATE_DSCP_MARKING_VALID) {
1219 original_cm->dscp = msg->dscp_rule.flow_dscp << SFE_IPV4_DSCP_SHIFT;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001220 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_DSCP_REMARK;
1221 }
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301222 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1223 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_BRIDGE_FLOW;
1224 }
Ken Zhu7e38d1a2021-11-30 17:31:46 -08001225 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_FLOW_TRANSMIT_FAST) {
1226 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_FAST_XMIT_DEV_ADMISSION;
1227 }
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301228
Wayne Tanbb7f1782021-12-13 11:16:04 -08001229 /*
1230 * Add VLAN rule to original_cm
1231 */
1232 if (msg->valid_flags & SFE_RULE_CREATE_VLAN_VALID) {
1233 struct sfe_vlan_rule *vlan_primary_rule = &msg->vlan_primary_rule;
1234 struct sfe_vlan_rule *vlan_secondary_rule = &msg->vlan_secondary_rule;
1235 sfe_ipv4_match_entry_set_vlan(original_cm,
1236 vlan_primary_rule->ingress_vlan_tag,
1237 vlan_primary_rule->egress_vlan_tag,
1238 vlan_secondary_rule->ingress_vlan_tag,
1239 vlan_secondary_rule->egress_vlan_tag);
1240
1241 if ((msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_RETURN_BOTTOM_INTERFACE) &&
1242 original_cm->egress_vlan_hdr_cnt > 0) {
1243 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_INSERT_EGRESS_VLAN_TAG;
1244 original_cm->l2_hdr_size += original_cm->egress_vlan_hdr_cnt * VLAN_HLEN;
1245 }
1246 }
1247
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301248 if ((IPPROTO_GRE == tuple->protocol) && !sfe_ipv4_is_local_ip(si, original_cm->match_dest_ip)) {
1249 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PASSTHROUGH;
1250 }
1251
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001252#ifdef CONFIG_NF_FLOW_COOKIE
1253 original_cm->flow_cookie = 0;
1254#endif
Zhi Chen8748eb32015-06-18 12:58:48 -07001255#ifdef CONFIG_XFRM
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301256 if (msg->valid_flags & SFE_RULE_CREATE_DIRECTION_VALID) {
1257 original_cm->flow_accel = msg->direction_rule.flow_accel;
1258 } else {
1259 original_cm->flow_accel = 1;
1260 }
Zhi Chen8748eb32015-06-18 12:58:48 -07001261#endif
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301262 /*
1263 * If l2_features are disabled and flow uses l2 features such as macvlan/bridge/pppoe/vlan,
1264 * bottom interfaces are expected to be disabled in the flow rule and always top interfaces
1265 * are used. In such cases, do not use HW csum offload. csum offload is used only when we
1266 * are sending directly to the destination interface that supports it.
1267 */
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301268 if (likely(dest_dev->features & NETIF_F_HW_CSUM) && sfe_dev_has_hw_csum(dest_dev)) {
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301269 if ((msg->conn_rule.return_top_interface_num == msg->conn_rule.return_interface_num) ||
1270 (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_RETURN_BOTTOM_INTERFACE)) {
Ratheesh Kannoth48445532022-02-07 16:19:00 +05301271
1272 /*
1273 * Dont enable CSUM offload
1274 */
1275#if 0
Suruchi Sumanf2077182022-01-13 21:35:23 +05301276 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_CSUM_OFFLOAD;
Ratheesh Kannoth48445532022-02-07 16:19:00 +05301277#endif
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301278 }
1279 }
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001280
Wayne Tanbb7f1782021-12-13 11:16:04 -08001281 reply_cm->l2_hdr_size = 0;
Ratheesh Kannoth5dee3772022-01-18 11:27:14 +05301282 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_SRC_INTERFACE_CHECK) {
1283 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_SRC_INTERFACE_CHECK;
1284 }
1285
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +05301286 reply_cm->flags = 0;
1287
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001288 /*
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301289 * Adding PPPoE parameters to original and reply entries based on the direction where
1290 * PPPoE header is valid in ECM rule.
1291 *
1292 * If PPPoE is valid in flow direction (from interface is PPPoE), then
1293 * original cm will have PPPoE at ingress (strip PPPoE header)
1294 * reply cm will have PPPoE at egress (add PPPoE header)
1295 *
1296 * If PPPoE is valid in return direction (to interface is PPPoE), then
1297 * original cm will have PPPoE at egress (add PPPoE header)
1298 * reply cm will have PPPoE at ingress (strip PPPoE header)
1299 */
1300 if (msg->valid_flags & SFE_RULE_CREATE_PPPOE_DECAP_VALID) {
1301 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_DECAP;
1302 original_cm->pppoe_session_id = msg->pppoe_rule.flow_pppoe_session_id;
1303 ether_addr_copy(original_cm->pppoe_remote_mac, msg->pppoe_rule.flow_pppoe_remote_mac);
1304
1305 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_ENCAP;
Wayne Tanbb7f1782021-12-13 11:16:04 -08001306 reply_cm->l2_hdr_size += SFE_PPPOE_SESSION_HEADER_SIZE;
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301307 reply_cm->pppoe_session_id = msg->pppoe_rule.flow_pppoe_session_id;
1308 ether_addr_copy(reply_cm->pppoe_remote_mac, msg->pppoe_rule.flow_pppoe_remote_mac);
1309 }
1310
1311 if (msg->valid_flags & SFE_RULE_CREATE_PPPOE_ENCAP_VALID) {
1312 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_ENCAP;
Wayne Tanbb7f1782021-12-13 11:16:04 -08001313 original_cm->l2_hdr_size += SFE_PPPOE_SESSION_HEADER_SIZE;
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301314 original_cm->pppoe_session_id = msg->pppoe_rule.return_pppoe_session_id;
1315 ether_addr_copy(original_cm->pppoe_remote_mac, msg->pppoe_rule.return_pppoe_remote_mac);
1316
1317 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_DECAP;
1318 reply_cm->pppoe_session_id = msg->pppoe_rule.return_pppoe_session_id;
1319 ether_addr_copy(reply_cm->pppoe_remote_mac, msg->pppoe_rule.return_pppoe_remote_mac);
1320 }
1321
Ratheesh Kannoth5dee3772022-01-18 11:27:14 +05301322 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_SRC_INTERFACE_CHECK) {
1323 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_SRC_INTERFACE_CHECK;
1324 }
1325
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301326 /*
Ken Zhubbf49652021-09-12 15:33:09 -07001327 * For the non-arp interface, we don't write L2 HDR.
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001328 */
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +05301329 if (sfe_ipv4_xmit_eth_type_check(dest_dev, original_cm->flags)) {
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301330
1331 /*
1332 * Check whether the rule has configured a specific source MAC address to use.
1333 * This is needed when virtual L3 interfaces such as br-lan, macvlan, vlan are used during egress
1334 */
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301335
1336 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1337 ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)msg->conn_rule.flow_mac);
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301338 } else {
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301339 if ((msg->valid_flags & SFE_RULE_CREATE_SRC_MAC_VALID) &&
1340 (msg->src_mac_rule.mac_valid_flags & SFE_SRC_MAC_RETURN_VALID)) {
1341 ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)msg->src_mac_rule.return_src_mac);
1342 } else {
1343 ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)dest_dev->dev_addr);
1344 }
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301345 }
1346
1347 ether_addr_copy((u8 *)original_cm->xmit_dest_mac, (u8 *)msg->conn_rule.return_mac);
1348
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001349 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_L2_HDR;
Wayne Tanbb7f1782021-12-13 11:16:04 -08001350 original_cm->l2_hdr_size += ETH_HLEN;
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001351
1352 /*
1353 * If our dev writes Ethernet headers then we can write a really fast
1354 * version.
1355 */
1356 if (dest_dev->header_ops) {
1357 if (dest_dev->header_ops->create == eth_header) {
1358 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR;
1359 }
1360 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001361 }
1362
1363 /*
1364 * Fill in the "reply" direction connection matching object.
1365 */
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001366 reply_cm->match_dev = dest_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301367 reply_cm->match_protocol = tuple->protocol;
1368 reply_cm->match_src_ip = msg->conn_rule.return_ip_xlate;
Amitesh Anand63be37d2021-12-24 20:51:48 +05301369
1370 /*
1371 * Keep source port as 0 for VxLAN tunnels.
1372 */
1373 if (netif_is_vxlan(src_dev) || netif_is_vxlan(dest_dev)) {
1374 reply_cm->match_src_port = 0;
1375 } else {
1376 reply_cm->match_src_port = msg->conn_rule.return_ident_xlate;
1377 }
1378
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301379 reply_cm->match_dest_ip = msg->conn_rule.flow_ip_xlate;
1380 reply_cm->match_dest_port = msg->conn_rule.flow_ident_xlate;
1381
1382 reply_cm->xlate_src_ip = tuple->return_ip;
1383 reply_cm->xlate_src_port = tuple->return_ident;
1384 reply_cm->xlate_dest_ip = tuple->flow_ip;
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301385 reply_cm->xlate_dest_port = tuple->flow_ident;
1386
1387 if (tuple->protocol == IPPROTO_GRE) {
1388 /*
1389 * the PPTP is 4 tuple lookup.
1390 * During th rule lookup destination call id from packet
1391 * is matched against destination port in cm.
1392 */
1393 reply_cm->match_src_port = 0;
1394 reply_cm->xlate_src_port = 0;
1395 }
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301396
1397 atomic_set(&reply_cm->rx_packet_count, 0);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001398 reply_cm->rx_packet_count64 = 0;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301399 atomic_set(&reply_cm->rx_byte_count, 0);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001400 reply_cm->rx_byte_count64 = 0;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301401
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001402 reply_cm->xmit_dev = src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301403 reply_cm->xmit_dev_mtu = msg->conn_rule.flow_mtu;
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301404
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001405 reply_cm->connection = c;
1406 reply_cm->counter_match = original_cm;
Ken Zhu37040ea2021-09-09 21:11:15 -07001407
Ken Zhu37040ea2021-09-09 21:11:15 -07001408 if (msg->valid_flags & SFE_RULE_CREATE_MARK_VALID) {
1409 reply_cm->mark = msg->mark_rule.return_mark;
1410 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_MARK;
1411 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301412 if (msg->valid_flags & SFE_RULE_CREATE_QOS_VALID) {
1413 reply_cm->priority = msg->qos_rule.return_qos_tag;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001414 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PRIORITY_REMARK;
1415 }
Wayne Tanbb7f1782021-12-13 11:16:04 -08001416
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301417 if (msg->valid_flags & SFE_RULE_CREATE_DSCP_MARKING_VALID) {
1418 reply_cm->dscp = msg->dscp_rule.return_dscp << SFE_IPV4_DSCP_SHIFT;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001419 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_DSCP_REMARK;
1420 }
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301421 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1422 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_BRIDGE_FLOW;
1423 }
Ken Zhu7e38d1a2021-11-30 17:31:46 -08001424 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_RETURN_TRANSMIT_FAST) {
1425 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_FAST_XMIT_DEV_ADMISSION;
1426 }
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301427
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301428 if ((IPPROTO_GRE == tuple->protocol) && !sfe_ipv4_is_local_ip(si, reply_cm->match_dest_ip)) {
1429 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PASSTHROUGH;
1430 }
1431
Amitesh Anand63be37d2021-12-24 20:51:48 +05301432 /*
1433 * Setup UDP Socket if found to be valid for decap.
1434 */
1435 RCU_INIT_POINTER(reply_cm->up, NULL);
1436 net = dev_net(reply_cm->match_dev);
1437 src_if_idx = src_dev->ifindex;
1438
1439 rcu_read_lock();
1440
1441 /*
1442 * Look for the associated sock object.
1443 * __udp4_lib_lookup() holds a reference for this sock object,
1444 * which will be released in sfe_ipv4_free_connection_rcu()
1445 */
1446#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
1447 sk = __udp4_lib_lookup(net, reply_cm->match_dest_ip, reply_cm->match_dest_port,
1448 reply_cm->xlate_src_ip, reply_cm->xlate_src_port, src_if_idx, &udp_table);
1449#else
1450 sk = __udp4_lib_lookup(net, reply_cm->match_dest_ip, reply_cm->match_dest_port,
1451 reply_cm->xlate_src_ip, reply_cm->xlate_src_port, src_if_idx, 0, &udp_table, NULL);
1452#endif
1453
1454 rcu_read_unlock();
1455
1456 /*
1457 * We set the UDP sock pointer as valid only for decap direction.
1458 */
1459 if (sk && udp_sk(sk)->encap_type) {
1460#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
1461 if (!atomic_add_unless(&sk->sk_refcnt, 1, 0)) {
1462#else
1463 if (!refcount_inc_not_zero(&sk->sk_refcnt)) {
1464#endif
Wayne Tanbb7f1782021-12-13 11:16:04 -08001465 spin_unlock_bh(&si->lock);
Amitesh Anand63be37d2021-12-24 20:51:48 +05301466 kfree(reply_cm);
1467 kfree(original_cm);
1468 kfree(c);
1469
1470 DEBUG_TRACE("%px: sfe: unable to take reference for socket(%px) p:%d\n"
1471 " s: %s:%pM:%pI4:%u, d: %s:%pM:%pI4:%u\n",
1472 msg, sk, tuple->protocol,
1473 src_dev->name, msg->conn_rule.flow_mac, &tuple->flow_ip, ntohs(tuple->flow_ident),
1474 dest_dev->name, msg->conn_rule.return_mac, &tuple->return_ip, ntohs(tuple->return_ident));
1475
1476 dev_put(src_dev);
1477 dev_put(dest_dev);
1478
1479 return -ESHUTDOWN;
1480 }
1481
1482 rcu_assign_pointer(reply_cm->up, udp_sk(sk));
1483
1484 DEBUG_INFO("%px: Sock(%px) lookup success with reply_cm direction\n", msg, sk);
1485 DEBUG_INFO("%px: SFE connection -\n"
1486 " s: %s:%pI4(%pI4):%u(%u)\n"
1487 " d: %s:%pI4(%pI4):%u(%u)\n",
1488 msg, reply_cm->match_dev->name, &reply_cm->match_src_ip, &reply_cm->xlate_src_ip,
1489 ntohs(reply_cm->match_src_port), ntohs(reply_cm->xlate_src_port),
1490 reply_cm->xmit_dev->name, &reply_cm->match_dest_ip, &reply_cm->xlate_dest_ip,
1491 ntohs(reply_cm->match_dest_port), ntohs(reply_cm->xlate_dest_port));
1492 }
1493
Wayne Tanbb7f1782021-12-13 11:16:04 -08001494 /*
1495 * Add VLAN rule to reply_cm
1496 */
1497 if (msg->valid_flags & SFE_RULE_CREATE_VLAN_VALID) {
1498 struct sfe_vlan_rule *vlan_primary_rule = &msg->vlan_primary_rule;
1499 struct sfe_vlan_rule *vlan_secondary_rule = &msg->vlan_secondary_rule;
1500 sfe_ipv4_match_entry_set_vlan(reply_cm,
1501 vlan_primary_rule->egress_vlan_tag,
1502 vlan_primary_rule->ingress_vlan_tag,
1503 vlan_secondary_rule->egress_vlan_tag,
1504 vlan_secondary_rule->ingress_vlan_tag);
1505
1506 if ((msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_FLOW_BOTTOM_INTERFACE) &&
1507 reply_cm->egress_vlan_hdr_cnt > 0) {
1508 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_INSERT_EGRESS_VLAN_TAG;
1509 reply_cm->l2_hdr_size += reply_cm->egress_vlan_hdr_cnt * VLAN_HLEN;
1510 }
1511 }
1512
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301513 /*
1514 * the net_protocol handler will be used only in decap path
1515 * for non passthrough case.
1516 */
1517 original_cm->proto = NULL;
1518 reply_cm->proto = NULL;
1519
1520#ifdef SFE_GRE_TUN_ENABLE
1521 if ((IPPROTO_GRE == tuple->protocol) && !(reply_cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_PASSTHROUGH)) {
1522 rcu_read_lock();
1523 reply_cm->proto = rcu_dereference(inet_protos[IPPROTO_GRE]);
1524 rcu_read_unlock();
1525
1526 if (unlikely(!reply_cm->proto)) {
1527 kfree(reply_cm);
1528 kfree(original_cm);
1529 kfree(c);
1530 dev_put(src_dev);
1531 dev_put(dest_dev);
1532 DEBUG_WARN("sfe: GRE proto handler is not registered\n");
1533 return -EPERM;
1534 }
1535 }
1536#endif
1537
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001538#ifdef CONFIG_NF_FLOW_COOKIE
1539 reply_cm->flow_cookie = 0;
1540#endif
Zhi Chen8748eb32015-06-18 12:58:48 -07001541#ifdef CONFIG_XFRM
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301542 if (msg->valid_flags & SFE_RULE_CREATE_DIRECTION_VALID) {
1543 reply_cm->flow_accel = msg->direction_rule.return_accel;
1544 } else {
1545 reply_cm->flow_accel = 1;
1546 }
1547
Zhi Chen8748eb32015-06-18 12:58:48 -07001548#endif
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301549 /*
1550 * If l2_features are disabled and flow uses l2 features such as macvlan/bridge/pppoe/vlan,
1551 * bottom interfaces are expected to be disabled in the flow rule and always top interfaces
1552 * are used. In such cases, do not use HW csum offload. csum offload is used only when we
1553 * are sending directly to the destination interface that supports it.
1554 */
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301555 if (likely(src_dev->features & NETIF_F_HW_CSUM) && sfe_dev_has_hw_csum(src_dev)) {
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301556 if ((msg->conn_rule.flow_top_interface_num == msg->conn_rule.flow_interface_num) ||
1557 (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_FLOW_BOTTOM_INTERFACE)) {
Ratheesh Kannoth48445532022-02-07 16:19:00 +05301558 /*
1559 * Dont enable CSUM offload
1560 */
1561#if 0
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301562 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_CSUM_OFFLOAD;
Ratheesh Kannoth48445532022-02-07 16:19:00 +05301563#endif
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301564 }
1565 }
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001566
1567 /*
Ken Zhubbf49652021-09-12 15:33:09 -07001568 * For the non-arp interface, we don't write L2 HDR.
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001569 */
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +05301570 if (sfe_ipv4_xmit_eth_type_check(src_dev, reply_cm->flags)) {
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301571
1572 /*
1573 * Check whether the rule has configured a specific source MAC address to use.
1574 * This is needed when virtual L3 interfaces such as br-lan, macvlan, vlan are used during egress
1575 */
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301576
1577 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1578 ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)msg->conn_rule.return_mac);
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301579 } else {
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301580 if ((msg->valid_flags & SFE_RULE_CREATE_SRC_MAC_VALID) &&
1581 (msg->src_mac_rule.mac_valid_flags & SFE_SRC_MAC_FLOW_VALID)) {
1582 ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)msg->src_mac_rule.flow_src_mac);
1583 } else {
1584 ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)src_dev->dev_addr);
1585 }
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301586 }
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301587
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301588 ether_addr_copy((u8 *)reply_cm->xmit_dest_mac, (u8 *)msg->conn_rule.flow_mac);
1589
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001590 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_L2_HDR;
Wayne Tanbb7f1782021-12-13 11:16:04 -08001591 reply_cm->l2_hdr_size += ETH_HLEN;
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001592
1593 /*
1594 * If our dev writes Ethernet headers then we can write a really fast
1595 * version.
1596 */
1597 if (src_dev->header_ops) {
1598 if (src_dev->header_ops->create == eth_header) {
1599 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR;
1600 }
1601 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001602 }
1603
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301604 if ((tuple->return_ip != msg->conn_rule.return_ip_xlate) ||
1605 (tuple->return_ident != msg->conn_rule.return_ident_xlate)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001606 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST;
1607 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC;
1608 }
1609
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301610 if ((tuple->flow_ip != msg->conn_rule.flow_ip_xlate) ||
1611 (tuple->flow_ident != msg->conn_rule.flow_ident_xlate)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001612 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC;
1613 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST;
1614 }
1615
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001616 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001617 * Initialize the protocol-specific information that we track.
1618 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301619 switch (tuple->protocol) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001620 case IPPROTO_TCP:
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301621 original_cm->protocol_state.tcp.win_scale = msg->tcp_rule.flow_window_scale;
1622 original_cm->protocol_state.tcp.max_win = msg->tcp_rule.flow_max_window ? msg->tcp_rule.flow_max_window : 1;
1623 original_cm->protocol_state.tcp.end = msg->tcp_rule.flow_end;
1624 original_cm->protocol_state.tcp.max_end = msg->tcp_rule.flow_max_end;
1625
1626 reply_cm->protocol_state.tcp.win_scale = msg->tcp_rule.return_window_scale;
1627 reply_cm->protocol_state.tcp.max_win = msg->tcp_rule.return_max_window ? msg->tcp_rule.return_max_window : 1;
1628 reply_cm->protocol_state.tcp.end = msg->tcp_rule.return_end;
1629 reply_cm->protocol_state.tcp.max_end = msg->tcp_rule.return_max_end;
1630
1631 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_NO_SEQ_CHECK) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001632 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
1633 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
1634 }
1635 break;
1636 }
1637
Wayne Tanbb7f1782021-12-13 11:16:04 -08001638 /*
1639 * Fill in the ipv4_connection object.
1640 */
1641 c->protocol = tuple->protocol;
1642 c->src_ip = tuple->flow_ip;
1643 c->src_ip_xlate = msg->conn_rule.flow_ip_xlate;
1644 c->src_port = tuple->flow_ident;
1645 c->src_port_xlate = msg->conn_rule.flow_ident_xlate;
1646 c->original_dev = src_dev;
1647 c->original_match = original_cm;
1648 c->dest_ip = tuple->return_ip;
1649 c->dest_ip_xlate = msg->conn_rule.return_ip_xlate;
1650 c->dest_port = tuple->return_ident;
1651 c->dest_port_xlate = msg->conn_rule.return_ident_xlate;
1652 c->reply_dev = dest_dev;
1653 c->reply_match = reply_cm;
1654 c->debug_read_seq = 0;
1655 c->last_sync_jiffies = get_jiffies_64();
1656 c->removed = false;
1657
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001658 sfe_ipv4_connection_match_compute_translations(original_cm);
1659 sfe_ipv4_connection_match_compute_translations(reply_cm);
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301660 sfe_ipv4_insert_connection(si, c);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001661
1662 spin_unlock_bh(&si->lock);
1663
1664 /*
1665 * We have everything we need!
1666 */
Wayne Tanbb7f1782021-12-13 11:16:04 -08001667 DEBUG_INFO("NEW connection - p: %d\n"
1668 "original_cm: match_dev=src_dev: %s %d %pM\n"
1669 " xmit_dev=dest_dev: %s %d %pM\n"
1670 " xmit_src_mac: %pM\n"
1671 " xmit_dest_mac: %pM\n"
1672 " flags: %x l2_hdr: %u\n"
1673 "flow_ip: %pI4:%u\n"
1674 "flow_ip_xlate: %pI4:%u\n"
1675 "flow_mac: %pM\n"
1676 "reply_cm: match_dev=dest_dev: %s %d %pM\n"
1677 " xmit_dev=src_dev: %s %d %pM\n"
1678 " xmit_src_mac: %pM\n"
1679 " xmit_dest_mac: %pM\n"
1680 " flags: %x l2_hdr: %u\n"
1681 "return_ip: %pI4:%u\n"
1682 "return_ip_xlate: %pI4:%u\n"
1683 "return_mac: %pM\n"
1684 "flags: valid=%x src_mac_valid=%x\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301685 tuple->protocol,
Wayne Tanbb7f1782021-12-13 11:16:04 -08001686 original_cm->match_dev->name, original_cm->match_dev->ifindex, original_cm->match_dev->dev_addr,
1687 original_cm->xmit_dev->name, original_cm->xmit_dev->ifindex, original_cm->xmit_dev->dev_addr,
1688 original_cm->xmit_src_mac, original_cm->xmit_dest_mac, original_cm->flags, original_cm->l2_hdr_size,
1689 &tuple->flow_ip, ntohs(tuple->flow_ident),
1690 &msg->conn_rule.flow_ip_xlate, ntohs(msg->conn_rule.flow_ident_xlate),
1691 msg->conn_rule.flow_mac,
1692 reply_cm->match_dev->name, reply_cm->match_dev->ifindex, reply_cm->match_dev->dev_addr,
1693 reply_cm->xmit_dev->name, reply_cm->xmit_dev->ifindex, reply_cm->xmit_dev->dev_addr,
1694 reply_cm->xmit_src_mac, reply_cm->xmit_dest_mac, reply_cm->flags, reply_cm->l2_hdr_size,
1695 &tuple->return_ip, ntohs(tuple->return_ident),
1696 &msg->conn_rule.return_ip_xlate, ntohs(msg->conn_rule.return_ident_xlate),
1697 msg->conn_rule.return_mac,
1698 msg->valid_flags, msg->src_mac_rule.mac_valid_flags);
Nicolas Costa514fde02014-01-13 15:50:29 -06001699
1700 return 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001701}
1702
1703/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001704 * sfe_ipv4_destroy_rule()
1705 * Destroy a forwarding rule.
1706 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301707void sfe_ipv4_destroy_rule(struct sfe_ipv4_rule_destroy_msg *msg)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001708{
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001709 struct sfe_ipv4 *si = &__si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001710 struct sfe_ipv4_connection *c;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301711 bool ret;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301712 struct sfe_ipv4_5tuple *tuple = &msg->tuple;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001713
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301714 this_cpu_inc(si->stats_pcpu->connection_destroy_requests64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001715 spin_lock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001716
1717 /*
1718 * Check to see if we have a flow that matches the rule we're trying
1719 * to destroy. If there isn't then we can't destroy it.
1720 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301721 c = sfe_ipv4_find_connection(si, tuple->protocol, tuple->flow_ip, tuple->flow_ident,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301722 tuple->return_ip, tuple->return_ident);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001723 if (!c) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001724 spin_unlock_bh(&si->lock);
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301725 this_cpu_inc(si->stats_pcpu->connection_destroy_misses64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001726
1727 DEBUG_TRACE("connection does not exist - p: %d, s: %pI4:%u, d: %pI4:%u\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301728 tuple->protocol, &tuple->flow_ip, ntohs(tuple->flow_ident),
1729 &tuple->return_ip, ntohs(tuple->return_ident));
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001730 return;
1731 }
1732
1733 /*
1734 * Remove our connection details from the hash tables.
1735 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301736 ret = sfe_ipv4_remove_connection(si, c);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001737 spin_unlock_bh(&si->lock);
1738
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301739 if (ret) {
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301740 sfe_ipv4_flush_connection(si, c, SFE_SYNC_REASON_DESTROY);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301741 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001742
1743 DEBUG_INFO("connection destroyed - p: %d, s: %pI4:%u, d: %pI4:%u\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301744 tuple->protocol, &tuple->flow_ip, ntohs(tuple->flow_ident),
1745 &tuple->return_ip, ntohs(tuple->return_ident));
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001746}
1747
1748/*
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001749 * sfe_ipv4_register_sync_rule_callback()
1750 * Register a callback for rule synchronization.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001751 */
Xiaoping Fand44a5b42015-05-26 17:37:37 -07001752void sfe_ipv4_register_sync_rule_callback(sfe_sync_rule_callback_t sync_rule_callback)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001753{
1754 struct sfe_ipv4 *si = &__si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001755
1756 spin_lock_bh(&si->lock);
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001757 rcu_assign_pointer(si->sync_rule_callback, sync_rule_callback);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001758 spin_unlock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001759}
1760
1761/*
1762 * sfe_ipv4_get_debug_dev()
1763 */
1764static ssize_t sfe_ipv4_get_debug_dev(struct device *dev,
1765 struct device_attribute *attr,
1766 char *buf)
1767{
1768 struct sfe_ipv4 *si = &__si;
1769 ssize_t count;
1770 int num;
1771
1772 spin_lock_bh(&si->lock);
1773 num = si->debug_dev;
1774 spin_unlock_bh(&si->lock);
1775
1776 count = snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", num);
1777 return count;
1778}
1779
1780/*
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001781 * sysfs attributes.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001782 */
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001783static const struct device_attribute sfe_ipv4_debug_dev_attr =
Xiaoping Fane70da412016-02-26 16:47:57 -08001784 __ATTR(debug_dev, S_IWUSR | S_IRUGO, sfe_ipv4_get_debug_dev, NULL);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001785
1786/*
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001787 * sfe_ipv4_destroy_all_rules_for_dev()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001788 * Destroy all connections that match a particular device.
1789 *
1790 * If we pass dev as NULL then this destroys all connections.
1791 */
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001792void sfe_ipv4_destroy_all_rules_for_dev(struct net_device *dev)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001793{
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001794 struct sfe_ipv4 *si = &__si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001795 struct sfe_ipv4_connection *c;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301796 bool ret;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001797
Xiaoping Fan34586472015-07-03 02:20:35 -07001798another_round:
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001799 spin_lock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001800
Xiaoping Fan34586472015-07-03 02:20:35 -07001801 for (c = si->all_connections_head; c; c = c->all_connections_next) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001802 /*
Xiaoping Fan34586472015-07-03 02:20:35 -07001803 * Does this connection relate to the device we are destroying?
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001804 */
1805 if (!dev
1806 || (dev == c->original_dev)
1807 || (dev == c->reply_dev)) {
Xiaoping Fan34586472015-07-03 02:20:35 -07001808 break;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001809 }
Xiaoping Fan34586472015-07-03 02:20:35 -07001810 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001811
Xiaoping Fan34586472015-07-03 02:20:35 -07001812 if (c) {
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301813 ret = sfe_ipv4_remove_connection(si, c);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001814 }
1815
1816 spin_unlock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07001817
1818 if (c) {
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301819 if (ret) {
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301820 sfe_ipv4_flush_connection(si, c, SFE_SYNC_REASON_DESTROY);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301821 }
Xiaoping Fan34586472015-07-03 02:20:35 -07001822 goto another_round;
1823 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001824}
1825
1826/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001827 * sfe_ipv4_periodic_sync()
1828 */
Ken Zhu137722d2021-09-23 17:57:36 -07001829static void sfe_ipv4_periodic_sync(struct work_struct *work)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001830{
Ken Zhu137722d2021-09-23 17:57:36 -07001831 struct sfe_ipv4 *si = container_of((struct delayed_work *)work, struct sfe_ipv4, sync_dwork);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07001832 u64 now_jiffies;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001833 int quota;
Xiaoping Fand44a5b42015-05-26 17:37:37 -07001834 sfe_sync_rule_callback_t sync_rule_callback;
Ken Zhudc423672021-09-02 18:27:01 -07001835 struct sfe_ipv4_connection *c;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001836
1837 now_jiffies = get_jiffies_64();
1838
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001839 rcu_read_lock();
1840 sync_rule_callback = rcu_dereference(si->sync_rule_callback);
1841 if (!sync_rule_callback) {
1842 rcu_read_unlock();
1843 goto done;
1844 }
1845
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001846 spin_lock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001847
1848 /*
Ken Zhudc423672021-09-02 18:27:01 -07001849 * If we have reached the end of the connection list, walk from
1850 * the connection head.
1851 */
1852 c = si->wc_next;
1853 if (unlikely(!c)) {
1854 c = si->all_connections_head;
1855 }
1856
1857 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001858 * Get an estimate of the number of connections to parse in this sync.
1859 */
1860 quota = (si->num_connections + 63) / 64;
1861
1862 /*
Ken Zhudc423672021-09-02 18:27:01 -07001863 * Walk the "all connection" list and sync the connection state.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001864 */
Ken Zhudc423672021-09-02 18:27:01 -07001865 while (likely(c && quota)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001866 struct sfe_ipv4_connection_match *cm;
1867 struct sfe_ipv4_connection_match *counter_cm;
Xiaoping Fand44a5b42015-05-26 17:37:37 -07001868 struct sfe_connection_sync sis;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001869
Ken Zhudc423672021-09-02 18:27:01 -07001870 cm = c->original_match;
1871 counter_cm = c->reply_match;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001872
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001873 /*
Ken Zhudc423672021-09-02 18:27:01 -07001874 * Didn't receive packets in the original direction or reply
1875 * direction, move to the next connection.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001876 */
Ken Zhudc423672021-09-02 18:27:01 -07001877 if ((!atomic_read(&cm->rx_packet_count)) && !(atomic_read(&counter_cm->rx_packet_count))) {
1878 c = c->all_connections_next;
1879 continue;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001880 }
1881
Ken Zhudc423672021-09-02 18:27:01 -07001882 quota--;
Matthew McClintockaf48f1e2014-01-23 15:29:19 -06001883
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301884 sfe_ipv4_gen_sync_connection(si, c, &sis, SFE_SYNC_REASON_STATS, now_jiffies);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001885
Ken Zhudc423672021-09-02 18:27:01 -07001886 si->wc_next = c->all_connections_next;
1887
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001888 /*
1889 * We don't want to be holding the lock when we sync!
1890 */
1891 spin_unlock_bh(&si->lock);
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001892 sync_rule_callback(&sis);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001893 spin_lock_bh(&si->lock);
Ken Zhudc423672021-09-02 18:27:01 -07001894
1895 /*
1896 * c must be set and used in the same lock/unlock window;
1897 * because c could be removed when we don't hold the lock,
1898 * so delay grabbing until after the callback and relock.
1899 */
1900 c = si->wc_next;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001901 }
1902
Ken Zhudc423672021-09-02 18:27:01 -07001903 /*
1904 * At the end of the sync, put the wc_next to the connection we left.
1905 */
1906 si->wc_next = c;
1907
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001908 spin_unlock_bh(&si->lock);
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001909 rcu_read_unlock();
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001910
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001911done:
Ken Zhu137722d2021-09-23 17:57:36 -07001912 schedule_delayed_work_on(si->work_cpu, (struct delayed_work *)work, ((HZ + 99) / 100));
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001913}
1914
1915#define CHAR_DEV_MSG_SIZE 768
1916
1917/*
1918 * sfe_ipv4_debug_dev_read_start()
1919 * Generate part of the XML output.
1920 */
1921static bool sfe_ipv4_debug_dev_read_start(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
1922 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
1923{
1924 int bytes_read;
1925
Xiaoping Fan34586472015-07-03 02:20:35 -07001926 si->debug_read_seq++;
1927
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001928 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "<sfe_ipv4>\n");
1929 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1930 return false;
1931 }
1932
1933 *length -= bytes_read;
1934 *total_read += bytes_read;
1935
1936 ws->state++;
1937 return true;
1938}
1939
1940/*
1941 * sfe_ipv4_debug_dev_read_connections_start()
1942 * Generate part of the XML output.
1943 */
1944static bool sfe_ipv4_debug_dev_read_connections_start(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
1945 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
1946{
1947 int bytes_read;
1948
1949 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<connections>\n");
1950 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1951 return false;
1952 }
1953
1954 *length -= bytes_read;
1955 *total_read += bytes_read;
1956
1957 ws->state++;
1958 return true;
1959}
1960
1961/*
1962 * sfe_ipv4_debug_dev_read_connections_connection()
1963 * Generate part of the XML output.
1964 */
1965static bool sfe_ipv4_debug_dev_read_connections_connection(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
1966 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
1967{
1968 struct sfe_ipv4_connection *c;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001969 struct sfe_ipv4_connection_match *original_cm;
1970 struct sfe_ipv4_connection_match *reply_cm;
1971 int bytes_read;
1972 int protocol;
1973 struct net_device *src_dev;
Dave Hudson87973cd2013-10-22 16:00:04 +01001974 __be32 src_ip;
1975 __be32 src_ip_xlate;
1976 __be16 src_port;
1977 __be16 src_port_xlate;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07001978 u64 src_rx_packets;
1979 u64 src_rx_bytes;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001980 struct net_device *dest_dev;
Dave Hudson87973cd2013-10-22 16:00:04 +01001981 __be32 dest_ip;
1982 __be32 dest_ip_xlate;
1983 __be16 dest_port;
1984 __be16 dest_port_xlate;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07001985 u64 dest_rx_packets;
1986 u64 dest_rx_bytes;
1987 u64 last_sync_jiffies;
Ken Zhu37040ea2021-09-09 21:11:15 -07001988 u32 src_mark, dest_mark, src_priority, dest_priority, src_dscp, dest_dscp;
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301989 u32 packet, byte, original_cm_flags;
1990 u16 pppoe_session_id;
1991 u8 pppoe_remote_mac[ETH_ALEN];
Ken Zhu7e38d1a2021-11-30 17:31:46 -08001992 u32 original_fast_xmit, reply_fast_xmit;
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001993#ifdef CONFIG_NF_FLOW_COOKIE
1994 int src_flow_cookie, dst_flow_cookie;
1995#endif
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001996
1997 spin_lock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07001998
1999 for (c = si->all_connections_head; c; c = c->all_connections_next) {
2000 if (c->debug_read_seq < si->debug_read_seq) {
2001 c->debug_read_seq = si->debug_read_seq;
2002 break;
2003 }
2004 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002005
2006 /*
Xiaoping Fan34586472015-07-03 02:20:35 -07002007 * If there were no connections then move to the next state.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002008 */
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05302009 if (!c || c->removed) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002010 spin_unlock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07002011 ws->state++;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002012 return true;
2013 }
2014
2015 original_cm = c->original_match;
2016 reply_cm = c->reply_match;
2017
2018 protocol = c->protocol;
2019 src_dev = c->original_dev;
2020 src_ip = c->src_ip;
2021 src_ip_xlate = c->src_ip_xlate;
2022 src_port = c->src_port;
2023 src_port_xlate = c->src_port_xlate;
Xiaoping Fane1963d42015-08-25 17:06:19 -07002024 src_priority = original_cm->priority;
2025 src_dscp = original_cm->dscp >> SFE_IPV4_DSCP_SHIFT;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002026
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05302027 sfe_ipv4_connection_match_update_summary_stats(original_cm, &packet, &byte);
2028 sfe_ipv4_connection_match_update_summary_stats(reply_cm, &packet, &byte);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002029
2030 src_rx_packets = original_cm->rx_packet_count64;
2031 src_rx_bytes = original_cm->rx_byte_count64;
Ken Zhu37040ea2021-09-09 21:11:15 -07002032 src_mark = original_cm->mark;
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002033 original_fast_xmit = (original_cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_FAST_XMIT);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002034 dest_dev = c->reply_dev;
2035 dest_ip = c->dest_ip;
2036 dest_ip_xlate = c->dest_ip_xlate;
2037 dest_port = c->dest_port;
2038 dest_port_xlate = c->dest_port_xlate;
Xiaoping Fane1963d42015-08-25 17:06:19 -07002039 dest_priority = reply_cm->priority;
2040 dest_dscp = reply_cm->dscp >> SFE_IPV4_DSCP_SHIFT;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002041 dest_rx_packets = reply_cm->rx_packet_count64;
2042 dest_rx_bytes = reply_cm->rx_byte_count64;
Ken Zhu37040ea2021-09-09 21:11:15 -07002043 dest_mark = reply_cm->mark;
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002044 reply_fast_xmit = (reply_cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_FAST_XMIT);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002045 last_sync_jiffies = get_jiffies_64() - c->last_sync_jiffies;
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05302046 original_cm_flags = original_cm->flags;
2047 pppoe_session_id = original_cm->pppoe_session_id;
2048 ether_addr_copy(pppoe_remote_mac, original_cm->pppoe_remote_mac);
2049
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08002050#ifdef CONFIG_NF_FLOW_COOKIE
2051 src_flow_cookie = original_cm->flow_cookie;
2052 dst_flow_cookie = reply_cm->flow_cookie;
2053#endif
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002054 spin_unlock_bh(&si->lock);
2055
2056 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t\t<connection "
2057 "protocol=\"%u\" "
2058 "src_dev=\"%s\" "
2059 "src_ip=\"%pI4\" src_ip_xlate=\"%pI4\" "
2060 "src_port=\"%u\" src_port_xlate=\"%u\" "
Xiaoping Fane1963d42015-08-25 17:06:19 -07002061 "src_priority=\"%u\" src_dscp=\"%u\" "
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002062 "src_rx_pkts=\"%llu\" src_rx_bytes=\"%llu\" "
Ken Zhu37040ea2021-09-09 21:11:15 -07002063 "src_mark=\"%08x\" "
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002064 "src_fast_xmit=\"%s\" "
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002065 "dest_dev=\"%s\" "
2066 "dest_ip=\"%pI4\" dest_ip_xlate=\"%pI4\" "
2067 "dest_port=\"%u\" dest_port_xlate=\"%u\" "
Xiaoping Fane1963d42015-08-25 17:06:19 -07002068 "dest_priority=\"%u\" dest_dscp=\"%u\" "
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002069 "dest_rx_pkts=\"%llu\" dest_rx_bytes=\"%llu\" "
Ken Zhu37040ea2021-09-09 21:11:15 -07002070 "dest_mark=\"%08x\" "
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002071 "reply_fast_xmit=\"%s\" "
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08002072#ifdef CONFIG_NF_FLOW_COOKIE
2073 "src_flow_cookie=\"%d\" dst_flow_cookie=\"%d\" "
2074#endif
Ken Zhu37040ea2021-09-09 21:11:15 -07002075 "last_sync=\"%llu\" ",
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002076 protocol,
2077 src_dev->name,
2078 &src_ip, &src_ip_xlate,
Dave Hudson87973cd2013-10-22 16:00:04 +01002079 ntohs(src_port), ntohs(src_port_xlate),
Xiaoping Fane1963d42015-08-25 17:06:19 -07002080 src_priority, src_dscp,
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002081 src_rx_packets, src_rx_bytes,
Ken Zhu37040ea2021-09-09 21:11:15 -07002082 src_mark,
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002083 original_fast_xmit ? "Yes" : "No",
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002084 dest_dev->name,
2085 &dest_ip, &dest_ip_xlate,
Dave Hudson87973cd2013-10-22 16:00:04 +01002086 ntohs(dest_port), ntohs(dest_port_xlate),
Xiaoping Fane1963d42015-08-25 17:06:19 -07002087 dest_priority, dest_dscp,
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002088 dest_rx_packets, dest_rx_bytes,
Ken Zhu37040ea2021-09-09 21:11:15 -07002089 dest_mark,
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002090 reply_fast_xmit ? "Yes" : "No",
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08002091#ifdef CONFIG_NF_FLOW_COOKIE
2092 src_flow_cookie, dst_flow_cookie,
2093#endif
Ken Zhu37040ea2021-09-09 21:11:15 -07002094 last_sync_jiffies);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002095
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05302096 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 +05302097 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 +05302098 pppoe_session_id, pppoe_remote_mac);
2099 }
2100
2101 bytes_read += snprintf(msg + bytes_read, CHAR_DEV_MSG_SIZE, "/>\n");
2102
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002103 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2104 return false;
2105 }
2106
2107 *length -= bytes_read;
2108 *total_read += bytes_read;
2109
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002110 return true;
2111}
2112
2113/*
2114 * sfe_ipv4_debug_dev_read_connections_end()
2115 * Generate part of the XML output.
2116 */
2117static bool sfe_ipv4_debug_dev_read_connections_end(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2118 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2119{
2120 int bytes_read;
2121
2122 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t</connections>\n");
2123 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2124 return false;
2125 }
2126
2127 *length -= bytes_read;
2128 *total_read += bytes_read;
2129
2130 ws->state++;
2131 return true;
2132}
2133
2134/*
2135 * sfe_ipv4_debug_dev_read_exceptions_start()
2136 * Generate part of the XML output.
2137 */
2138static bool sfe_ipv4_debug_dev_read_exceptions_start(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2139 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2140{
2141 int bytes_read;
2142
2143 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<exceptions>\n");
2144 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2145 return false;
2146 }
2147
2148 *length -= bytes_read;
2149 *total_read += bytes_read;
2150
2151 ws->state++;
2152 return true;
2153}
2154
2155/*
2156 * sfe_ipv4_debug_dev_read_exceptions_exception()
2157 * Generate part of the XML output.
2158 */
2159static bool sfe_ipv4_debug_dev_read_exceptions_exception(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2160 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2161{
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302162 int i;
2163 u64 val = 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002164
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302165 for_each_possible_cpu(i) {
2166 const struct sfe_ipv4_stats *s = per_cpu_ptr(si->stats_pcpu, i);
2167 val += s->exception_events64[ws->iter_exception];
2168 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002169
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302170 if (val) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002171 int bytes_read;
2172
2173 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE,
2174 "\t\t<exception name=\"%s\" count=\"%llu\" />\n",
2175 sfe_ipv4_exception_events_string[ws->iter_exception],
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302176 val);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002177 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2178 return false;
2179 }
2180
2181 *length -= bytes_read;
2182 *total_read += bytes_read;
2183 }
2184
2185 ws->iter_exception++;
2186 if (ws->iter_exception >= SFE_IPV4_EXCEPTION_EVENT_LAST) {
2187 ws->iter_exception = 0;
2188 ws->state++;
2189 }
2190
2191 return true;
2192}
2193
2194/*
2195 * sfe_ipv4_debug_dev_read_exceptions_end()
2196 * Generate part of the XML output.
2197 */
2198static bool sfe_ipv4_debug_dev_read_exceptions_end(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2199 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2200{
2201 int bytes_read;
2202
2203 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t</exceptions>\n");
2204 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2205 return false;
2206 }
2207
2208 *length -= bytes_read;
2209 *total_read += bytes_read;
2210
2211 ws->state++;
2212 return true;
2213}
2214
2215/*
2216 * sfe_ipv4_debug_dev_read_stats()
2217 * Generate part of the XML output.
2218 */
2219static bool sfe_ipv4_debug_dev_read_stats(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2220 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2221{
2222 int bytes_read;
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302223 struct sfe_ipv4_stats stats;
2224 unsigned int num_conn;
2225
2226 sfe_ipv4_update_summary_stats(si, &stats);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002227
2228 spin_lock_bh(&si->lock);
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302229 num_conn = si->num_connections;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002230 spin_unlock_bh(&si->lock);
2231
2232 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<stats "
2233 "num_connections=\"%u\" "
Amitesh Anand63be37d2021-12-24 20:51:48 +05302234 "pkts_dropped=\"%llu\" "
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002235 "pkts_fast_xmited=\"%llu\" "
Xiaoping Fan59176422015-05-22 15:58:10 -07002236 "pkts_forwarded=\"%llu\" pkts_not_forwarded=\"%llu\" "
2237 "create_requests=\"%llu\" create_collisions=\"%llu\" "
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05302238 "create_failures=\"%llu\" "
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002239 "destroy_requests=\"%llu\" destroy_misses=\"%llu\" "
2240 "flushes=\"%llu\" "
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +05302241 "hash_hits=\"%llu\" hash_reorders=\"%llu\" "
2242 "pppoe_encap_pkts_fwded=\"%llu\" "
Guduri Prathyusha034d6352022-01-12 16:49:04 +05302243 "pppoe_decap_pkts_fwded=\"%llu\" "
2244 "pppoe_bridge_pkts_fwded=\"%llu\" />\n",
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302245 num_conn,
Amitesh Anand63be37d2021-12-24 20:51:48 +05302246 stats.packets_dropped64,
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002247 stats.packets_fast_xmited64,
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302248 stats.packets_forwarded64,
2249 stats.packets_not_forwarded64,
2250 stats.connection_create_requests64,
2251 stats.connection_create_collisions64,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05302252 stats.connection_create_failures64,
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302253 stats.connection_destroy_requests64,
2254 stats.connection_destroy_misses64,
2255 stats.connection_flushes64,
2256 stats.connection_match_hash_hits64,
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +05302257 stats.connection_match_hash_reorders64,
2258 stats.pppoe_encap_packets_forwarded64,
Guduri Prathyusha034d6352022-01-12 16:49:04 +05302259 stats.pppoe_decap_packets_forwarded64,
2260 stats.pppoe_bridge_packets_forwarded64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002261 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2262 return false;
2263 }
2264
2265 *length -= bytes_read;
2266 *total_read += bytes_read;
2267
2268 ws->state++;
2269 return true;
2270}
2271
2272/*
2273 * sfe_ipv4_debug_dev_read_end()
2274 * Generate part of the XML output.
2275 */
2276static bool sfe_ipv4_debug_dev_read_end(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2277 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2278{
2279 int bytes_read;
2280
2281 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "</sfe_ipv4>\n");
2282 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2283 return false;
2284 }
2285
2286 *length -= bytes_read;
2287 *total_read += bytes_read;
2288
2289 ws->state++;
2290 return true;
2291}
2292
2293/*
2294 * Array of write functions that write various XML elements that correspond to
2295 * our XML output state machine.
2296 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07002297static 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 +01002298 sfe_ipv4_debug_dev_read_start,
2299 sfe_ipv4_debug_dev_read_connections_start,
2300 sfe_ipv4_debug_dev_read_connections_connection,
2301 sfe_ipv4_debug_dev_read_connections_end,
2302 sfe_ipv4_debug_dev_read_exceptions_start,
2303 sfe_ipv4_debug_dev_read_exceptions_exception,
2304 sfe_ipv4_debug_dev_read_exceptions_end,
2305 sfe_ipv4_debug_dev_read_stats,
2306 sfe_ipv4_debug_dev_read_end,
2307};
2308
2309/*
2310 * sfe_ipv4_debug_dev_read()
2311 * Send info to userspace upon read request from user
2312 */
2313static ssize_t sfe_ipv4_debug_dev_read(struct file *filp, char *buffer, size_t length, loff_t *offset)
2314{
2315 char msg[CHAR_DEV_MSG_SIZE];
2316 int total_read = 0;
2317 struct sfe_ipv4_debug_xml_write_state *ws;
2318 struct sfe_ipv4 *si = &__si;
2319
2320 ws = (struct sfe_ipv4_debug_xml_write_state *)filp->private_data;
2321 while ((ws->state != SFE_IPV4_DEBUG_XML_STATE_DONE) && (length > CHAR_DEV_MSG_SIZE)) {
2322 if ((sfe_ipv4_debug_xml_write_methods[ws->state])(si, buffer, msg, &length, &total_read, ws)) {
2323 continue;
2324 }
2325 }
2326
2327 return total_read;
2328}
2329
2330/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002331 * sfe_ipv4_debug_dev_open()
2332 */
2333static int sfe_ipv4_debug_dev_open(struct inode *inode, struct file *file)
2334{
2335 struct sfe_ipv4_debug_xml_write_state *ws;
2336
2337 ws = (struct sfe_ipv4_debug_xml_write_state *)file->private_data;
2338 if (!ws) {
2339 ws = kzalloc(sizeof(struct sfe_ipv4_debug_xml_write_state), GFP_KERNEL);
2340 if (!ws) {
2341 return -ENOMEM;
2342 }
2343
2344 ws->state = SFE_IPV4_DEBUG_XML_STATE_START;
2345 file->private_data = ws;
2346 }
2347
2348 return 0;
2349}
2350
2351/*
2352 * sfe_ipv4_debug_dev_release()
2353 */
2354static int sfe_ipv4_debug_dev_release(struct inode *inode, struct file *file)
2355{
2356 struct sfe_ipv4_debug_xml_write_state *ws;
2357
2358 ws = (struct sfe_ipv4_debug_xml_write_state *)file->private_data;
2359 if (ws) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002360 /*
2361 * We've finished with our output so free the write state.
2362 */
2363 kfree(ws);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05302364 file->private_data = NULL;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002365 }
2366
2367 return 0;
2368}
2369
2370/*
2371 * File operations used in the debug char device
2372 */
2373static struct file_operations sfe_ipv4_debug_dev_fops = {
2374 .read = sfe_ipv4_debug_dev_read,
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002375 .open = sfe_ipv4_debug_dev_open,
2376 .release = sfe_ipv4_debug_dev_release
2377};
2378
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08002379#ifdef CONFIG_NF_FLOW_COOKIE
2380/*
2381 * sfe_register_flow_cookie_cb
2382 * register a function in SFE to let SFE use this function to configure flow cookie for a flow
2383 *
2384 * Hardware driver which support flow cookie should register a callback function in SFE. Then SFE
2385 * can use this function to configure flow cookie for a flow.
2386 * return: 0, success; !=0, fail
2387 */
2388int sfe_register_flow_cookie_cb(flow_cookie_set_func_t cb)
2389{
2390 struct sfe_ipv4 *si = &__si;
2391
2392 BUG_ON(!cb);
2393
2394 if (si->flow_cookie_set_func) {
2395 return -1;
2396 }
2397
2398 rcu_assign_pointer(si->flow_cookie_set_func, cb);
2399 return 0;
2400}
2401
2402/*
2403 * sfe_unregister_flow_cookie_cb
2404 * unregister function which is used to configure flow cookie for a flow
2405 *
2406 * return: 0, success; !=0, fail
2407 */
2408int sfe_unregister_flow_cookie_cb(flow_cookie_set_func_t cb)
2409{
2410 struct sfe_ipv4 *si = &__si;
2411
2412 RCU_INIT_POINTER(si->flow_cookie_set_func, NULL);
2413 return 0;
2414}
Xiaoping Fan640faf42015-08-28 15:50:55 -07002415
2416/*
2417 * sfe_ipv4_get_flow_cookie()
2418 */
2419static ssize_t sfe_ipv4_get_flow_cookie(struct device *dev,
2420 struct device_attribute *attr,
2421 char *buf)
2422{
2423 struct sfe_ipv4 *si = &__si;
Xiaoping Fan01c67cc2015-11-09 11:31:57 -08002424 return snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", si->flow_cookie_enable);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002425}
2426
2427/*
2428 * sfe_ipv4_set_flow_cookie()
2429 */
2430static ssize_t sfe_ipv4_set_flow_cookie(struct device *dev,
2431 struct device_attribute *attr,
2432 const char *buf, size_t size)
2433{
2434 struct sfe_ipv4 *si = &__si;
Ken Zhu137722d2021-09-23 17:57:36 -07002435 si->flow_cookie_enable = simple_strtol(buf, NULL, 0);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002436
2437 return size;
2438}
2439
2440/*
2441 * sysfs attributes.
2442 */
2443static const struct device_attribute sfe_ipv4_flow_cookie_attr =
Xiaoping Fane70da412016-02-26 16:47:57 -08002444 __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 -08002445#endif /*CONFIG_NF_FLOW_COOKIE*/
2446
Ken Zhu137722d2021-09-23 17:57:36 -07002447/*
2448 * sfe_ipv4_get_cpu()
2449 */
2450static ssize_t sfe_ipv4_get_cpu(struct device *dev,
2451 struct device_attribute *attr,
2452 char *buf)
2453{
2454 struct sfe_ipv4 *si = &__si;
2455 return snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", si->work_cpu);
2456}
2457
2458/*
2459 * sfe_ipv4_set_cpu()
2460 */
2461static ssize_t sfe_ipv4_set_cpu(struct device *dev,
2462 struct device_attribute *attr,
2463 const char *buf, size_t size)
2464{
2465 struct sfe_ipv4 *si = &__si;
2466 int work_cpu;
2467 work_cpu = simple_strtol(buf, NULL, 0);
2468 if ((work_cpu >= 0) && (work_cpu <= NR_CPUS)) {
2469 si->work_cpu = work_cpu;
2470 } else {
2471 dev_err(dev, "%s is not in valid range[0,%d]", buf, NR_CPUS);
2472 }
2473 return size;
2474}
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002475
Ken Zhu137722d2021-09-23 17:57:36 -07002476/*
2477 * sysfs attributes.
2478 */
2479static const struct device_attribute sfe_ipv4_cpu_attr =
2480 __ATTR(stats_work_cpu, S_IWUSR | S_IRUGO, sfe_ipv4_get_cpu, sfe_ipv4_set_cpu);
2481
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002482/*
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05302483 * sfe_ipv4_conn_match_hash_init()
2484 * Initialize conn match hash lists
2485 */
2486static void sfe_ipv4_conn_match_hash_init(struct sfe_ipv4 *si, int len)
2487{
2488 struct hlist_head *hash_list = si->hlist_conn_match_hash_head;
2489 int i;
2490
2491 for (i = 0; i < len; i++) {
2492 INIT_HLIST_HEAD(&hash_list[i]);
2493 }
2494}
2495
Amitesh Anand63be37d2021-12-24 20:51:48 +05302496#ifdef SFE_PROCESS_LOCAL_OUT
2497/*
2498 * sfe_ipv4_local_out()
2499 * Called for packets from ip_local_out() - post encapsulation & other packets
2500 */
2501static unsigned int sfe_ipv4_local_out(void *priv, struct sk_buff *skb, const struct nf_hook_state *nhs)
2502{
Nitin Shettyc28f8172022-02-04 16:23:46 +05302503 struct sfe_l2_info l2_info = {0};
Guduri Prathyusha5f27e232022-01-06 14:39:04 +05302504
Amitesh Anand63be37d2021-12-24 20:51:48 +05302505 DEBUG_TRACE("%px: sfe: sfe_ipv4_local_out hook called.\n", skb);
2506
2507 if (likely(skb->skb_iif)) {
Guduri Prathyusha5f27e232022-01-06 14:39:04 +05302508 return sfe_ipv4_recv(skb->dev, skb, &l2_info, true) ? NF_STOLEN : NF_ACCEPT;
Amitesh Anand63be37d2021-12-24 20:51:48 +05302509 }
2510
2511 return NF_ACCEPT;
2512}
2513
2514/*
2515 * struct nf_hook_ops sfe_ipv4_ops_local_out[]
2516 * Hooks into netfilter local out packet monitoring points.
2517 */
2518static struct nf_hook_ops sfe_ipv4_ops_local_out[] __read_mostly = {
2519
2520 /*
2521 * Local out routing hook is used to monitor packets.
2522 */
2523 {
2524 .hook = sfe_ipv4_local_out,
2525 .pf = PF_INET,
2526 .hooknum = NF_INET_LOCAL_OUT,
2527 .priority = NF_IP_PRI_FIRST,
2528 },
2529};
2530#endif
2531
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002532/*
Dave Hudson87973cd2013-10-22 16:00:04 +01002533 * sfe_ipv4_init()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002534 */
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05302535int sfe_ipv4_init(void)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002536{
2537 struct sfe_ipv4 *si = &__si;
2538 int result = -1;
2539
Dave Hudsondcd08fb2013-11-22 09:25:16 -06002540 DEBUG_INFO("SFE IPv4 init\n");
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002541
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05302542 sfe_ipv4_conn_match_hash_init(si, ARRAY_SIZE(si->hlist_conn_match_hash_head));
2543
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302544 si->stats_pcpu = alloc_percpu_gfp(struct sfe_ipv4_stats, GFP_KERNEL | __GFP_ZERO);
2545 if (!si->stats_pcpu) {
2546 DEBUG_ERROR("failed to allocate stats memory for sfe_ipv4\n");
2547 goto exit0;
2548 }
2549
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002550 /*
2551 * Create sys/sfe_ipv4
2552 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302553 si->sys_ipv4 = kobject_create_and_add("sfe_ipv4", NULL);
2554 if (!si->sys_ipv4) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002555 DEBUG_ERROR("failed to register sfe_ipv4\n");
2556 goto exit1;
2557 }
2558
2559 /*
2560 * Create files, one for each parameter supported by this module.
2561 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302562 result = sysfs_create_file(si->sys_ipv4, &sfe_ipv4_debug_dev_attr.attr);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002563 if (result) {
2564 DEBUG_ERROR("failed to register debug dev file: %d\n", result);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002565 goto exit2;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002566 }
2567
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302568 result = sysfs_create_file(si->sys_ipv4, &sfe_ipv4_cpu_attr.attr);
Ken Zhu137722d2021-09-23 17:57:36 -07002569 if (result) {
2570 DEBUG_ERROR("failed to register debug dev file: %d\n", result);
2571 goto exit3;
2572 }
2573
Xiaoping Fan640faf42015-08-28 15:50:55 -07002574#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302575 result = sysfs_create_file(si->sys_ipv4, &sfe_ipv4_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002576 if (result) {
2577 DEBUG_ERROR("failed to register flow cookie enable file: %d\n", result);
Ken Zhu137722d2021-09-23 17:57:36 -07002578 goto exit4;
Xiaoping Fan640faf42015-08-28 15:50:55 -07002579 }
2580#endif /* CONFIG_NF_FLOW_COOKIE */
2581
Amitesh Anand63be37d2021-12-24 20:51:48 +05302582#ifdef SFE_PROCESS_LOCAL_OUT
2583#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
2584 result = nf_register_hooks(sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2585#else
2586 result = nf_register_net_hooks(&init_net, sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2587#endif
2588 if (result < 0) {
2589 DEBUG_ERROR("can't register nf local out hook: %d\n", result);
2590 goto exit5;
2591 }
2592 DEBUG_INFO("Register nf local out hook success: %d\n", result);
2593#endif
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002594 /*
2595 * Register our debug char device.
2596 */
2597 result = register_chrdev(0, "sfe_ipv4", &sfe_ipv4_debug_dev_fops);
2598 if (result < 0) {
2599 DEBUG_ERROR("Failed to register chrdev: %d\n", result);
Amitesh Anand63be37d2021-12-24 20:51:48 +05302600 goto exit6;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002601 }
2602
2603 si->debug_dev = result;
Ken Zhu137722d2021-09-23 17:57:36 -07002604 si->work_cpu = WORK_CPU_UNBOUND;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002605
2606 /*
Ken Zhu137722d2021-09-23 17:57:36 -07002607 * Create a work to handle periodic statistics.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002608 */
Ken Zhu137722d2021-09-23 17:57:36 -07002609 INIT_DELAYED_WORK(&(si->sync_dwork), sfe_ipv4_periodic_sync);
2610 schedule_delayed_work_on(si->work_cpu, &(si->sync_dwork), ((HZ + 99) / 100));
2611
Dave Hudson87973cd2013-10-22 16:00:04 +01002612 spin_lock_init(&si->lock);
Dave Hudson87973cd2013-10-22 16:00:04 +01002613 return 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002614
Amitesh Anand63be37d2021-12-24 20:51:48 +05302615exit6:
2616#ifdef SFE_PROCESS_LOCAL_OUT
2617 DEBUG_TRACE("sfe: Unregister local out hook\n");
2618#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
2619 nf_unregister_hooks(sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2620#else
2621 nf_unregister_net_hooks(&init_net, sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2622#endif
Ken Zhu137722d2021-09-23 17:57:36 -07002623exit5:
Amitesh Anand63be37d2021-12-24 20:51:48 +05302624#endif
Xiaoping Fan640faf42015-08-28 15:50:55 -07002625#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302626 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002627
Ken Zhu137722d2021-09-23 17:57:36 -07002628exit4:
Xiaoping Fan640faf42015-08-28 15:50:55 -07002629#endif /* CONFIG_NF_FLOW_COOKIE */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302630 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_cpu_attr.attr);
Ken Zhu137722d2021-09-23 17:57:36 -07002631exit3:
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302632 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_debug_dev_attr.attr);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002633
Xiaoping Fan640faf42015-08-28 15:50:55 -07002634exit2:
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302635 kobject_put(si->sys_ipv4);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002636
2637exit1:
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302638 free_percpu(si->stats_pcpu);
2639
2640exit0:
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002641 return result;
2642}
2643
2644/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002645 * sfe_ipv4_exit()
2646 */
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05302647void sfe_ipv4_exit(void)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002648{
Dave Hudson87973cd2013-10-22 16:00:04 +01002649 struct sfe_ipv4 *si = &__si;
2650
Dave Hudsondcd08fb2013-11-22 09:25:16 -06002651 DEBUG_INFO("SFE IPv4 exit\n");
Dave Hudson87973cd2013-10-22 16:00:04 +01002652 /*
2653 * Destroy all connections.
2654 */
Dave Hudsondcd08fb2013-11-22 09:25:16 -06002655 sfe_ipv4_destroy_all_rules_for_dev(NULL);
Dave Hudson87973cd2013-10-22 16:00:04 +01002656
Ken Zhu137722d2021-09-23 17:57:36 -07002657 cancel_delayed_work_sync(&si->sync_dwork);
Dave Hudson87973cd2013-10-22 16:00:04 +01002658
Dave Hudson87973cd2013-10-22 16:00:04 +01002659 unregister_chrdev(si->debug_dev, "sfe_ipv4");
2660
Amitesh Anand63be37d2021-12-24 20:51:48 +05302661#ifdef SFE_PROCESS_LOCAL_OUT
2662 DEBUG_TRACE("sfe: Unregister local out hook\n");
2663#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
2664 nf_unregister_hooks(sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2665#else
2666 nf_unregister_net_hooks(&init_net, sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2667#endif
2668#endif
2669
Xiaoping Fan640faf42015-08-28 15:50:55 -07002670#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302671 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002672#endif /* CONFIG_NF_FLOW_COOKIE */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302673 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_debug_dev_attr.attr);
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002674
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302675 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_cpu_attr.attr);
Dave Hudson87973cd2013-10-22 16:00:04 +01002676
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302677 kobject_put(si->sys_ipv4);
Dave Hudson87973cd2013-10-22 16:00:04 +01002678
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302679 free_percpu(si->stats_pcpu);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002680}
2681
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08002682#ifdef CONFIG_NF_FLOW_COOKIE
2683EXPORT_SYMBOL(sfe_register_flow_cookie_cb);
2684EXPORT_SYMBOL(sfe_unregister_flow_cookie_cb);
2685#endif