blob: c21e30b0fcd08a97d70ff12e49fdf9fa9dbdaf10 [file] [log] [blame]
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001/*
2 * sfe_ipv4.c
3 * Shortcut forwarding engine - IPv4 edition.
4 *
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05305 * Copyright (c) 2013-2016, 2019-2020, The Linux Foundation. All rights reserved.
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +05306 * Copyright (c) 2021,2022 Qualcomm Innovation Center, Inc. All rights reserved.
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05307 *
8 * Permission to use, copy, modify, and/or distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
11 *
Xiaoping Fana42c68b2015-08-07 18:00:39 -070012 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +053017 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010019 */
Matthew McClintocka3221942014-01-16 11:44:26 -060020
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010021#include <linux/module.h>
Dave Hudsondcd08fb2013-11-22 09:25:16 -060022#include <linux/sysfs.h>
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010023#include <linux/skbuff.h>
24#include <linux/icmp.h>
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010025#include <net/tcp.h>
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>
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010035
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +053036#include "sfe_debug.h"
Ratheesh Kannoth89302a72021-10-20 08:10:37 +053037#include "sfe_api.h"
Dave Hudsondcd08fb2013-11-22 09:25:16 -060038#include "sfe.h"
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +053039#include "sfe_flow_cookie.h"
40#include "sfe_ipv4.h"
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +053041#include "sfe_ipv4_udp.h"
42#include "sfe_ipv4_tcp.h"
43#include "sfe_ipv4_icmp.h"
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010044
45static char *sfe_ipv4_exception_events_string[SFE_IPV4_EXCEPTION_EVENT_LAST] = {
46 "UDP_HEADER_INCOMPLETE",
47 "UDP_NO_CONNECTION",
48 "UDP_IP_OPTIONS_OR_INITIAL_FRAGMENT",
49 "UDP_SMALL_TTL",
50 "UDP_NEEDS_FRAGMENTATION",
51 "TCP_HEADER_INCOMPLETE",
52 "TCP_NO_CONNECTION_SLOW_FLAGS",
53 "TCP_NO_CONNECTION_FAST_FLAGS",
54 "TCP_IP_OPTIONS_OR_INITIAL_FRAGMENT",
55 "TCP_SMALL_TTL",
56 "TCP_NEEDS_FRAGMENTATION",
57 "TCP_FLAGS",
58 "TCP_SEQ_EXCEEDS_RIGHT_EDGE",
59 "TCP_SMALL_DATA_OFFS",
60 "TCP_BAD_SACK",
61 "TCP_BIG_DATA_OFFS",
62 "TCP_SEQ_BEFORE_LEFT_EDGE",
63 "TCP_ACK_EXCEEDS_RIGHT_EDGE",
64 "TCP_ACK_BEFORE_LEFT_EDGE",
65 "ICMP_HEADER_INCOMPLETE",
66 "ICMP_UNHANDLED_TYPE",
67 "ICMP_IPV4_HEADER_INCOMPLETE",
68 "ICMP_IPV4_NON_V4",
69 "ICMP_IPV4_IP_OPTIONS_INCOMPLETE",
70 "ICMP_IPV4_UDP_HEADER_INCOMPLETE",
71 "ICMP_IPV4_TCP_HEADER_INCOMPLETE",
72 "ICMP_IPV4_UNHANDLED_PROTOCOL",
73 "ICMP_NO_CONNECTION",
74 "ICMP_FLUSHED_CONNECTION",
75 "HEADER_INCOMPLETE",
Ratheesh Kannoth43d64f82021-10-20 08:23:29 +053076 "HEADER_CSUM_BAD",
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010077 "BAD_TOTAL_LENGTH",
78 "NON_V4",
79 "NON_INITIAL_FRAGMENT",
80 "DATAGRAM_INCOMPLETE",
81 "IP_OPTIONS_INCOMPLETE",
82 "UNHANDLED_PROTOCOL"
83};
84
Xiaoping Fan6a1672f2016-08-17 19:58:12 -070085static struct sfe_ipv4 __si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010086
87/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010088 * sfe_ipv4_gen_ip_csum()
89 * Generate the IP checksum for an IPv4 header.
90 *
91 * Note that this function assumes that we have only 20 bytes of IP header.
92 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +053093u16 sfe_ipv4_gen_ip_csum(struct iphdr *iph)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010094{
Xiaoping Fan6a1672f2016-08-17 19:58:12 -070095 u32 sum;
96 u16 *i = (u16 *)iph;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010097
98 iph->check = 0;
99
100 /*
101 * Generate the sum.
102 */
103 sum = i[0] + i[1] + i[2] + i[3] + i[4] + i[5] + i[6] + i[7] + i[8] + i[9];
104
105 /*
106 * Fold it to ones-complement form.
107 */
108 sum = (sum & 0xffff) + (sum >> 16);
109 sum = (sum & 0xffff) + (sum >> 16);
110
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700111 return (u16)sum ^ 0xffff;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100112}
113
114/*
115 * sfe_ipv4_get_connection_match_hash()
116 * Generate the hash used in connection match lookups.
117 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700118static inline unsigned int sfe_ipv4_get_connection_match_hash(struct net_device *dev, u8 protocol,
Dave Hudson87973cd2013-10-22 16:00:04 +0100119 __be32 src_ip, __be16 src_port,
120 __be32 dest_ip, __be16 dest_port)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100121{
122 size_t dev_addr = (size_t)dev;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700123 u32 hash = ((u32)dev_addr) ^ ntohl(src_ip ^ dest_ip) ^ protocol ^ ntohs(src_port ^ dest_port);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100124 return ((hash >> SFE_IPV4_CONNECTION_HASH_SHIFT) ^ hash) & SFE_IPV4_CONNECTION_HASH_MASK;
125}
126
127/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530128 * sfe_ipv4_find_connection_match_rcu()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100129 * Get the IPv4 flow match info that corresponds to a particular 5-tuple.
130 *
131 * On entry we must be holding the lock that protects the hash table.
132 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530133struct sfe_ipv4_connection_match *
134sfe_ipv4_find_connection_match_rcu(struct sfe_ipv4 *si, struct net_device *dev, u8 protocol,
Dave Hudson87973cd2013-10-22 16:00:04 +0100135 __be32 src_ip, __be16 src_port,
136 __be32 dest_ip, __be16 dest_port)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100137{
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530138 struct sfe_ipv4_connection_match *cm = NULL;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100139 unsigned int conn_match_idx;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530140 struct hlist_head *lhead;
141
142 WARN_ON_ONCE(!rcu_read_lock_held());
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100143
144 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 +0100145
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530146 lhead = &si->hlist_conn_match_hash_head[conn_match_idx];
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100147
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530148 hlist_for_each_entry_rcu(cm, lhead, hnode) {
149 if (cm->match_src_port != src_port
150 || cm->match_dest_port != dest_port
151 || cm->match_src_ip != src_ip
152 || cm->match_dest_ip != dest_ip
153 || cm->match_protocol != protocol
154 || cm->match_dev != dev) {
155 continue;
156 }
157
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530158 this_cpu_inc(si->stats_pcpu->connection_match_hash_hits64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100159
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530160 break;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100161 }
162
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100163 return cm;
164}
165
166/*
167 * sfe_ipv4_connection_match_update_summary_stats()
168 * Update the summary stats for a connection match entry.
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530169 *
170 * Stats are incremented atomically. So use atomic substraction to update summary
171 * stats.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100172 */
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530173static inline void sfe_ipv4_connection_match_update_summary_stats(struct sfe_ipv4_connection_match *cm,
174 u32 *packets, u32 *bytes)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100175{
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530176 u32 packet_count, byte_count;
177
178 packet_count = atomic_read(&cm->rx_packet_count);
179 cm->rx_packet_count64 += packet_count;
180 atomic_sub(packet_count, &cm->rx_packet_count);
181
182 byte_count = atomic_read(&cm->rx_byte_count);
183 cm->rx_byte_count64 += byte_count;
184 atomic_sub(byte_count, &cm->rx_byte_count);
185
186 *packets = packet_count;
187 *bytes = byte_count;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100188}
189
190/*
191 * sfe_ipv4_connection_match_compute_translations()
192 * Compute port and address translations for a connection match entry.
193 */
194static void sfe_ipv4_connection_match_compute_translations(struct sfe_ipv4_connection_match *cm)
195{
196 /*
197 * Before we insert the entry look to see if this is tagged as doing address
198 * translations. If it is then work out the adjustment that we need to apply
199 * to the transport checksum.
200 */
201 if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC) {
202 /*
203 * Precompute an incremental checksum adjustment so we can
204 * edit packets in this stream very quickly. The algorithm is from RFC1624.
205 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700206 u16 src_ip_hi = cm->match_src_ip >> 16;
207 u16 src_ip_lo = cm->match_src_ip & 0xffff;
208 u32 xlate_src_ip = ~cm->xlate_src_ip;
209 u16 xlate_src_ip_hi = xlate_src_ip >> 16;
210 u16 xlate_src_ip_lo = xlate_src_ip & 0xffff;
211 u16 xlate_src_port = ~cm->xlate_src_port;
212 u32 adj;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100213
214 /*
215 * When we compute this fold it down to a 16-bit offset
216 * as that way we can avoid having to do a double
217 * folding of the twos-complement result because the
218 * addition of 2 16-bit values cannot cause a double
219 * wrap-around!
220 */
221 adj = src_ip_hi + src_ip_lo + cm->match_src_port
222 + xlate_src_ip_hi + xlate_src_ip_lo + xlate_src_port;
223 adj = (adj & 0xffff) + (adj >> 16);
224 adj = (adj & 0xffff) + (adj >> 16);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700225 cm->xlate_src_csum_adjustment = (u16)adj;
Nicolas Costaac2979c2014-01-14 10:35:24 -0600226
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100227 }
228
229 if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST) {
230 /*
231 * Precompute an incremental checksum adjustment so we can
232 * edit packets in this stream very quickly. The algorithm is from RFC1624.
233 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700234 u16 dest_ip_hi = cm->match_dest_ip >> 16;
235 u16 dest_ip_lo = cm->match_dest_ip & 0xffff;
236 u32 xlate_dest_ip = ~cm->xlate_dest_ip;
237 u16 xlate_dest_ip_hi = xlate_dest_ip >> 16;
238 u16 xlate_dest_ip_lo = xlate_dest_ip & 0xffff;
239 u16 xlate_dest_port = ~cm->xlate_dest_port;
240 u32 adj;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100241
242 /*
243 * When we compute this fold it down to a 16-bit offset
244 * as that way we can avoid having to do a double
245 * folding of the twos-complement result because the
246 * addition of 2 16-bit values cannot cause a double
247 * wrap-around!
248 */
249 adj = dest_ip_hi + dest_ip_lo + cm->match_dest_port
250 + xlate_dest_ip_hi + xlate_dest_ip_lo + xlate_dest_port;
251 adj = (adj & 0xffff) + (adj >> 16);
252 adj = (adj & 0xffff) + (adj >> 16);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700253 cm->xlate_dest_csum_adjustment = (u16)adj;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100254 }
Xiaoping Fanad755af2015-04-01 16:58:46 -0700255
256 if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC) {
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700257 u32 adj = ~cm->match_src_ip + cm->xlate_src_ip;
Xiaoping Fanad755af2015-04-01 16:58:46 -0700258 if (adj < cm->xlate_src_ip) {
259 adj++;
260 }
261
262 adj = (adj & 0xffff) + (adj >> 16);
263 adj = (adj & 0xffff) + (adj >> 16);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700264 cm->xlate_src_partial_csum_adjustment = (u16)adj;
Xiaoping Fanad755af2015-04-01 16:58:46 -0700265 }
266
267 if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST) {
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700268 u32 adj = ~cm->match_dest_ip + cm->xlate_dest_ip;
Xiaoping Fanad755af2015-04-01 16:58:46 -0700269 if (adj < cm->xlate_dest_ip) {
270 adj++;
271 }
272
273 adj = (adj & 0xffff) + (adj >> 16);
274 adj = (adj & 0xffff) + (adj >> 16);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700275 cm->xlate_dest_partial_csum_adjustment = (u16)adj;
Xiaoping Fanad755af2015-04-01 16:58:46 -0700276 }
277
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100278}
279
280/*
281 * sfe_ipv4_update_summary_stats()
282 * Update the summary stats.
283 */
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530284static void sfe_ipv4_update_summary_stats(struct sfe_ipv4 *si, struct sfe_ipv4_stats *stats)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100285{
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530286 int i = 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100287
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530288 memset(stats, 0, sizeof(*stats));
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100289
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530290 for_each_possible_cpu(i) {
291 const struct sfe_ipv4_stats *s = per_cpu_ptr(si->stats_pcpu, i);
292
293 stats->connection_create_requests64 += s->connection_create_requests64;
294 stats->connection_create_collisions64 += s->connection_create_collisions64;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530295 stats->connection_create_failures64 += s->connection_create_failures64;
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530296 stats->connection_destroy_requests64 += s->connection_destroy_requests64;
297 stats->connection_destroy_misses64 += s->connection_destroy_misses64;
298 stats->connection_match_hash_hits64 += s->connection_match_hash_hits64;
299 stats->connection_match_hash_reorders64 += s->connection_match_hash_reorders64;
300 stats->connection_flushes64 += s->connection_flushes64;
Amitesh Anand63be37d2021-12-24 20:51:48 +0530301 stats->packets_dropped64 += s->packets_dropped64;
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530302 stats->packets_forwarded64 += s->packets_forwarded64;
303 stats->packets_not_forwarded64 += s->packets_not_forwarded64;
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +0530304 stats->pppoe_encap_packets_forwarded64 += s->pppoe_encap_packets_forwarded64;
305 stats->pppoe_decap_packets_forwarded64 += s->pppoe_decap_packets_forwarded64;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100306 }
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530307
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100308}
309
310/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530311 * sfe_ipv4_insert_connection_match()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100312 * Insert a connection match into the hash.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100313 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530314static inline void sfe_ipv4_insert_connection_match(struct sfe_ipv4 *si,
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700315 struct sfe_ipv4_connection_match *cm)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100316{
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100317 unsigned int conn_match_idx
318 = sfe_ipv4_get_connection_match_hash(cm->match_dev, cm->match_protocol,
319 cm->match_src_ip, cm->match_src_port,
320 cm->match_dest_ip, cm->match_dest_port);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700321
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530322 lockdep_assert_held(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100323
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530324 hlist_add_head_rcu(&cm->hnode, &si->hlist_conn_match_hash_head[conn_match_idx]);
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800325#ifdef CONFIG_NF_FLOW_COOKIE
Xiaoping Fan640faf42015-08-28 15:50:55 -0700326 if (!si->flow_cookie_enable)
327 return;
328
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800329 /*
330 * Configure hardware to put a flow cookie in packet of this flow,
331 * then we can accelerate the lookup process when we received this packet.
332 */
333 for (conn_match_idx = 1; conn_match_idx < SFE_FLOW_COOKIE_SIZE; conn_match_idx++) {
334 struct sfe_flow_cookie_entry *entry = &si->sfe_flow_cookie_table[conn_match_idx];
335
336 if ((NULL == entry->match) && time_is_before_jiffies(entry->last_clean_time + HZ)) {
337 flow_cookie_set_func_t func;
338
339 rcu_read_lock();
340 func = rcu_dereference(si->flow_cookie_set_func);
341 if (func) {
Xiaoping Fan59176422015-05-22 15:58:10 -0700342 if (!func(cm->match_protocol, cm->match_src_ip, cm->match_src_port,
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800343 cm->match_dest_ip, cm->match_dest_port, conn_match_idx)) {
344 entry->match = cm;
345 cm->flow_cookie = conn_match_idx;
346 }
347 }
348 rcu_read_unlock();
349
350 break;
351 }
352 }
353#endif
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100354}
355
356/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530357 * sfe_ipv4_remove_connection_match()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100358 * Remove a connection match object from the hash.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100359 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530360static inline void sfe_ipv4_remove_connection_match(struct sfe_ipv4 *si, struct sfe_ipv4_connection_match *cm)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100361{
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530362
363 lockdep_assert_held(&si->lock);
364
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800365#ifdef CONFIG_NF_FLOW_COOKIE
Xiaoping Fan640faf42015-08-28 15:50:55 -0700366 if (si->flow_cookie_enable) {
367 /*
368 * Tell hardware that we no longer need a flow cookie in packet of this flow
369 */
370 unsigned int conn_match_idx;
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800371
Xiaoping Fan640faf42015-08-28 15:50:55 -0700372 for (conn_match_idx = 1; conn_match_idx < SFE_FLOW_COOKIE_SIZE; conn_match_idx++) {
373 struct sfe_flow_cookie_entry *entry = &si->sfe_flow_cookie_table[conn_match_idx];
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800374
Xiaoping Fan640faf42015-08-28 15:50:55 -0700375 if (cm == entry->match) {
376 flow_cookie_set_func_t func;
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800377
Xiaoping Fan640faf42015-08-28 15:50:55 -0700378 rcu_read_lock();
379 func = rcu_dereference(si->flow_cookie_set_func);
380 if (func) {
381 func(cm->match_protocol, cm->match_src_ip, cm->match_src_port,
382 cm->match_dest_ip, cm->match_dest_port, 0);
383 }
384 rcu_read_unlock();
385
386 cm->flow_cookie = 0;
387 entry->match = NULL;
388 entry->last_clean_time = jiffies;
389 break;
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800390 }
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800391 }
392 }
393#endif
394
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530395 hlist_del_init_rcu(&cm->hnode);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100396
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100397}
398
399/*
400 * sfe_ipv4_get_connection_hash()
401 * Generate the hash used in connection lookups.
402 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700403static inline unsigned int sfe_ipv4_get_connection_hash(u8 protocol, __be32 src_ip, __be16 src_port,
Dave Hudson87973cd2013-10-22 16:00:04 +0100404 __be32 dest_ip, __be16 dest_port)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100405{
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700406 u32 hash = ntohl(src_ip ^ dest_ip) ^ protocol ^ ntohs(src_port ^ dest_port);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100407 return ((hash >> SFE_IPV4_CONNECTION_HASH_SHIFT) ^ hash) & SFE_IPV4_CONNECTION_HASH_MASK;
408}
409
410/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530411 * sfe_ipv4_find_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100412 * Get the IPv4 connection info that corresponds to a particular 5-tuple.
413 *
414 * On entry we must be holding the lock that protects the hash table.
415 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530416static inline struct sfe_ipv4_connection *sfe_ipv4_find_connection(struct sfe_ipv4 *si, u32 protocol,
Dave Hudson87973cd2013-10-22 16:00:04 +0100417 __be32 src_ip, __be16 src_port,
418 __be32 dest_ip, __be16 dest_port)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100419{
420 struct sfe_ipv4_connection *c;
421 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 +0530422
423 lockdep_assert_held(&si->lock);
424
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100425 c = si->conn_hash[conn_idx];
426
427 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100428 * Will need connection entry for next create/destroy metadata,
429 * So no need to re-order entry for these requests
430 */
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530431 while (c) {
432 if ((c->src_port == src_port)
433 && (c->dest_port == dest_port)
434 && (c->src_ip == src_ip)
435 && (c->dest_ip == dest_ip)
436 && (c->protocol == protocol)) {
437 return c;
438 }
439
440 c = c->next;
441 }
442
443 return NULL;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100444}
445
446/*
Matthew McClintockbe7b47d2013-11-27 13:26:23 -0600447 * sfe_ipv4_mark_rule()
448 * Updates the mark for a current offloaded connection
449 *
450 * Will take hash lock upon entry
451 */
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700452void sfe_ipv4_mark_rule(struct sfe_connection_mark *mark)
Matthew McClintockbe7b47d2013-11-27 13:26:23 -0600453{
454 struct sfe_ipv4 *si = &__si;
455 struct sfe_ipv4_connection *c;
Matthew McClintockdb5ac512014-01-16 17:01:40 -0600456
Xiaoping Fan3c423e32015-07-03 03:09:29 -0700457 spin_lock_bh(&si->lock);
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530458 c = sfe_ipv4_find_connection(si, mark->protocol,
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700459 mark->src_ip.ip, mark->src_port,
460 mark->dest_ip.ip, mark->dest_port);
Matthew McClintockbe7b47d2013-11-27 13:26:23 -0600461 if (c) {
Nicolas Costaf53d6fe2014-01-13 16:03:46 -0600462 WARN_ON((0 != c->mark) && (0 == mark->mark));
Matthew McClintockbe7b47d2013-11-27 13:26:23 -0600463 c->mark = mark->mark;
464 }
Xiaoping Fan3c423e32015-07-03 03:09:29 -0700465 spin_unlock_bh(&si->lock);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700466
467 if (c) {
468 DEBUG_TRACE("Matching connection found for mark, "
469 "setting from %08x to %08x\n",
470 c->mark, mark->mark);
471 }
Matthew McClintockbe7b47d2013-11-27 13:26:23 -0600472}
473
474/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530475 * sfe_ipv4_insert_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100476 * Insert a connection into the hash.
477 *
478 * On entry we must be holding the lock that protects the hash table.
479 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530480static void sfe_ipv4_insert_connection(struct sfe_ipv4 *si, struct sfe_ipv4_connection *c)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100481{
482 struct sfe_ipv4_connection **hash_head;
483 struct sfe_ipv4_connection *prev_head;
484 unsigned int conn_idx;
485
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530486 lockdep_assert_held(&si->lock);
487
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100488 /*
489 * Insert entry into the connection hash.
490 */
491 conn_idx = sfe_ipv4_get_connection_hash(c->protocol, c->src_ip, c->src_port,
492 c->dest_ip, c->dest_port);
493 hash_head = &si->conn_hash[conn_idx];
494 prev_head = *hash_head;
495 c->prev = NULL;
496 if (prev_head) {
497 prev_head->prev = c;
498 }
499
500 c->next = prev_head;
501 *hash_head = c;
502
503 /*
504 * Insert entry into the "all connections" list.
505 */
506 if (si->all_connections_tail) {
507 c->all_connections_prev = si->all_connections_tail;
508 si->all_connections_tail->all_connections_next = c;
509 } else {
510 c->all_connections_prev = NULL;
511 si->all_connections_head = c;
512 }
513
514 si->all_connections_tail = c;
515 c->all_connections_next = NULL;
516 si->num_connections++;
517
518 /*
519 * Insert the connection match objects too.
520 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530521 sfe_ipv4_insert_connection_match(si, c->original_match);
522 sfe_ipv4_insert_connection_match(si, c->reply_match);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100523}
524
525/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530526 * sfe_ipv4_remove_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100527 * Remove a sfe_ipv4_connection object from the hash.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100528 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530529bool sfe_ipv4_remove_connection(struct sfe_ipv4 *si, struct sfe_ipv4_connection *c)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100530{
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530531 lockdep_assert_held(&si->lock);
532
533 if (c->removed) {
534 DEBUG_ERROR("%px: Connection has been removed already\n", c);
535 return false;
536 }
537
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100538 /*
539 * Remove the connection match objects.
540 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530541 sfe_ipv4_remove_connection_match(si, c->reply_match);
542 sfe_ipv4_remove_connection_match(si, c->original_match);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100543
544 /*
545 * Unlink the connection.
546 */
547 if (c->prev) {
548 c->prev->next = c->next;
549 } else {
550 unsigned int conn_idx = sfe_ipv4_get_connection_hash(c->protocol, c->src_ip, c->src_port,
551 c->dest_ip, c->dest_port);
552 si->conn_hash[conn_idx] = c->next;
553 }
554
555 if (c->next) {
556 c->next->prev = c->prev;
557 }
Xiaoping Fan34586472015-07-03 02:20:35 -0700558
559 /*
560 * Unlink connection from all_connections list
561 */
562 if (c->all_connections_prev) {
563 c->all_connections_prev->all_connections_next = c->all_connections_next;
564 } else {
565 si->all_connections_head = c->all_connections_next;
566 }
567
568 if (c->all_connections_next) {
569 c->all_connections_next->all_connections_prev = c->all_connections_prev;
570 } else {
571 si->all_connections_tail = c->all_connections_prev;
572 }
573
Ken Zhudc423672021-09-02 18:27:01 -0700574 /*
575 * If I am the next sync connection, move the sync to my next or head.
576 */
577 if (unlikely(si->wc_next == c)) {
578 si->wc_next = c->all_connections_next;
579 }
580
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530581 c->removed = true;
Xiaoping Fan34586472015-07-03 02:20:35 -0700582 si->num_connections--;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530583 return true;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100584}
585
586/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530587 * sfe_ipv4_gen_sync_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100588 * Sync a connection.
589 *
590 * On entry to this function we expect that the lock for the connection is either
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530591 * already held (while called from sfe_ipv4_periodic_sync() or isn't required
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530592 * (while called from sfe_ipv4_flush_connection())
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100593 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530594static void sfe_ipv4_gen_sync_connection(struct sfe_ipv4 *si, struct sfe_ipv4_connection *c,
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700595 struct sfe_connection_sync *sis, sfe_sync_reason_t reason,
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700596 u64 now_jiffies)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100597{
598 struct sfe_ipv4_connection_match *original_cm;
599 struct sfe_ipv4_connection_match *reply_cm;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530600 u32 packet_count, byte_count;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100601
602 /*
603 * Fill in the update message.
604 */
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700605 sis->is_v6 = 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100606 sis->protocol = c->protocol;
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700607 sis->src_ip.ip = c->src_ip;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700608 sis->src_ip_xlate.ip = c->src_ip_xlate;
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700609 sis->dest_ip.ip = c->dest_ip;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700610 sis->dest_ip_xlate.ip = c->dest_ip_xlate;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100611 sis->src_port = c->src_port;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700612 sis->src_port_xlate = c->src_port_xlate;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100613 sis->dest_port = c->dest_port;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700614 sis->dest_port_xlate = c->dest_port_xlate;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100615
616 original_cm = c->original_match;
617 reply_cm = c->reply_match;
618 sis->src_td_max_window = original_cm->protocol_state.tcp.max_win;
619 sis->src_td_end = original_cm->protocol_state.tcp.end;
620 sis->src_td_max_end = original_cm->protocol_state.tcp.max_end;
621 sis->dest_td_max_window = reply_cm->protocol_state.tcp.max_win;
622 sis->dest_td_end = reply_cm->protocol_state.tcp.end;
623 sis->dest_td_max_end = reply_cm->protocol_state.tcp.max_end;
624
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530625 sfe_ipv4_connection_match_update_summary_stats(original_cm, &packet_count, &byte_count);
626 sis->src_new_packet_count = packet_count;
627 sis->src_new_byte_count = byte_count;
Matthew McClintockd0cdb802014-02-24 16:30:35 -0600628
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530629 sfe_ipv4_connection_match_update_summary_stats(reply_cm, &packet_count, &byte_count);
630 sis->dest_new_packet_count = packet_count;
631 sis->dest_new_byte_count = byte_count;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100632
Matthew McClintockd0cdb802014-02-24 16:30:35 -0600633 sis->src_dev = original_cm->match_dev;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100634 sis->src_packet_count = original_cm->rx_packet_count64;
635 sis->src_byte_count = original_cm->rx_byte_count64;
Matthew McClintockd0cdb802014-02-24 16:30:35 -0600636
637 sis->dest_dev = reply_cm->match_dev;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100638 sis->dest_packet_count = reply_cm->rx_packet_count64;
639 sis->dest_byte_count = reply_cm->rx_byte_count64;
640
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700641 sis->reason = reason;
642
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100643 /*
644 * Get the time increment since our last sync.
645 */
646 sis->delta_jiffies = now_jiffies - c->last_sync_jiffies;
647 c->last_sync_jiffies = now_jiffies;
648}
649
650/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530651 * sfe_ipv4_free_connection_rcu()
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530652 * Called at RCU qs state to free the connection object.
653 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530654static void sfe_ipv4_free_connection_rcu(struct rcu_head *head)
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530655{
656 struct sfe_ipv4_connection *c;
Amitesh Anand63be37d2021-12-24 20:51:48 +0530657 struct udp_sock *up;
658 struct sock *sk;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530659
660 /*
661 * We dont need spin lock as the connection is already removed from link list
662 */
663 c = container_of(head, struct sfe_ipv4_connection, rcu);
664
665 BUG_ON(!c->removed);
666
667 DEBUG_TRACE("%px: connecton has been deleted\n", c);
668
669 /*
Amitesh Anand63be37d2021-12-24 20:51:48 +0530670 * Decrease the refcount taken in function sfe_ipv4_create_rule(),
671 * during call of __udp4_lib_lookup()
672 */
673 up = c->reply_match->up;
674 if (up) {
675 sk = (struct sock *)up;
676 sock_put(sk);
677 }
678
679 /*
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530680 * Release our hold of the source and dest devices and free the memory
681 * for our connection objects.
682 */
683 dev_put(c->original_dev);
684 dev_put(c->reply_dev);
685 kfree(c->original_match);
686 kfree(c->reply_match);
687 kfree(c);
688}
689
690/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530691 * sfe_ipv4_flush_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100692 * Flush a connection and free all associated resources.
693 *
694 * We need to be called with bottom halves disabled locally as we need to acquire
695 * the connection hash lock and release it again. In general we're actually called
696 * from within a BH and so we're fine, but we're also called when connections are
697 * torn down.
698 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530699void sfe_ipv4_flush_connection(struct sfe_ipv4 *si,
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700700 struct sfe_ipv4_connection *c,
701 sfe_sync_reason_t reason)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100702{
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700703 u64 now_jiffies;
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700704 sfe_sync_rule_callback_t sync_rule_callback;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100705
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530706 BUG_ON(!c->removed);
707
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530708 this_cpu_inc(si->stats_pcpu->connection_flushes64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100709
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530710 rcu_read_lock();
711 sync_rule_callback = rcu_dereference(si->sync_rule_callback);
712
713 /*
714 * Generate a sync message and then sync.
715 */
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600716 if (sync_rule_callback) {
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530717 struct sfe_connection_sync sis;
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600718 now_jiffies = get_jiffies_64();
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530719 sfe_ipv4_gen_sync_connection(si, c, &sis, reason, now_jiffies);
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600720 sync_rule_callback(&sis);
721 }
722
723 rcu_read_unlock();
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100724
725 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100726 * Release our hold of the source and dest devices and free the memory
727 * for our connection objects.
728 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530729 call_rcu(&c->rcu, sfe_ipv4_free_connection_rcu);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100730}
731
732/*
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530733 * sfe_ipv4_exception_stats_inc()
734 * Increment exception stats.
735 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530736void sfe_ipv4_exception_stats_inc(struct sfe_ipv4 *si, enum sfe_ipv4_exception_events reason)
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530737{
738 struct sfe_ipv4_stats *stats = this_cpu_ptr(si->stats_pcpu);
739 stats->exception_events64[reason]++;
740 stats->packets_not_forwarded64++;
741}
742
743/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100744 * sfe_ipv4_recv()
Matthew McClintocka8ad7962014-01-16 16:49:30 -0600745 * Handle packet receives and forwaring.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100746 *
747 * Returns 1 if the packet is forwarded or 0 if it isn't.
748 */
Amitesh Anand63be37d2021-12-24 20:51:48 +0530749int 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 +0100750{
751 struct sfe_ipv4 *si = &__si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100752 unsigned int len;
753 unsigned int tot_len;
754 unsigned int frag_off;
755 unsigned int ihl;
756 bool flush_on_find;
757 bool ip_options;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530758 struct iphdr *iph;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700759 u32 protocol;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100760
761 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100762 * Check that we have space for an IP header here.
763 */
764 len = skb->len;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530765 if (unlikely(!pskb_may_pull(skb, sizeof(struct iphdr)))) {
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530766 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_HEADER_INCOMPLETE);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100767 DEBUG_TRACE("len: %u is too short\n", len);
768 return 0;
769 }
770
771 /*
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +0530772 * Validate ip csum if necessary. If ip_summed is set to CHECKSUM_UNNECESSARY, it is assumed
773 * that the L3 checksum is validated by the Rx interface or the tunnel interface that has
774 * generated the packet.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100775 */
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530776 iph = (struct iphdr *)skb->data;
Ratheesh Kannoth43d64f82021-10-20 08:23:29 +0530777 if (unlikely(skb->ip_summed != CHECKSUM_UNNECESSARY) && (ip_fast_csum((u8 *)iph, iph->ihl))) {
778 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_HEADER_CSUM_BAD);
779
780 DEBUG_TRACE("Bad IPv4 header csum: 0x%x\n", iph->check);
781 return 0;
782 }
783
784 /*
785 * Check that our "total length" is large enough for an IP header.
786 */
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100787 tot_len = ntohs(iph->tot_len);
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530788 if (unlikely(tot_len < sizeof(struct iphdr))) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100789
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530790 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_BAD_TOTAL_LENGTH);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100791 DEBUG_TRACE("tot_len: %u is too short\n", tot_len);
792 return 0;
793 }
794
795 /*
796 * Is our IP version wrong?
797 */
798 if (unlikely(iph->version != 4)) {
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530799 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_NON_V4);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100800 DEBUG_TRACE("IP version: %u\n", iph->version);
801 return 0;
802 }
803
804 /*
805 * Does our datagram fit inside the skb?
806 */
807 if (unlikely(tot_len > len)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100808 DEBUG_TRACE("tot_len: %u, exceeds len: %u\n", tot_len, len);
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530809 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_DATAGRAM_INCOMPLETE);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100810 return 0;
811 }
812
813 /*
814 * Do we have a non-initial fragment?
Nicolas Costaac2979c2014-01-14 10:35:24 -0600815 */
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100816 frag_off = ntohs(iph->frag_off);
817 if (unlikely(frag_off & IP_OFFSET)) {
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530818 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_NON_INITIAL_FRAGMENT);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100819 DEBUG_TRACE("non-initial fragment\n");
820 return 0;
821 }
822
823 /*
824 * If we have a (first) fragment then mark it to cause any connection to flush.
825 */
826 flush_on_find = unlikely(frag_off & IP_MF) ? true : false;
827
828 /*
829 * Do we have any IP options? That's definite a slow path! If we do have IP
830 * options we need to recheck our header size.
831 */
832 ihl = iph->ihl << 2;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530833 ip_options = unlikely(ihl != sizeof(struct iphdr)) ? true : false;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100834 if (unlikely(ip_options)) {
835 if (unlikely(len < ihl)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100836
837 DEBUG_TRACE("len: %u is too short for header of size: %u\n", len, ihl);
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530838 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_IP_OPTIONS_INCOMPLETE);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100839 return 0;
840 }
841
842 flush_on_find = true;
843 }
844
845 protocol = iph->protocol;
846 if (IPPROTO_UDP == protocol) {
Amitesh Anand63be37d2021-12-24 20:51:48 +0530847 return sfe_ipv4_recv_udp(si, skb, dev, len, iph, ihl, flush_on_find, l2_info, tun_outer);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100848 }
849
850 if (IPPROTO_TCP == protocol) {
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +0530851 return sfe_ipv4_recv_tcp(si, skb, dev, len, iph, ihl, flush_on_find, l2_info);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100852 }
853
854 if (IPPROTO_ICMP == protocol) {
855 return sfe_ipv4_recv_icmp(si, skb, dev, len, iph, ihl);
856 }
857
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530858 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_UNHANDLED_PROTOCOL);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100859
860 DEBUG_TRACE("not UDP, TCP or ICMP: %u\n", protocol);
861 return 0;
862}
863
Nicolas Costa436926b2014-01-14 10:36:22 -0600864static void
865sfe_ipv4_update_tcp_state(struct sfe_ipv4_connection *c,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530866 struct sfe_ipv4_rule_create_msg *msg)
Nicolas Costa436926b2014-01-14 10:36:22 -0600867{
868 struct sfe_ipv4_connection_match *orig_cm;
869 struct sfe_ipv4_connection_match *repl_cm;
870 struct sfe_ipv4_tcp_connection_match *orig_tcp;
871 struct sfe_ipv4_tcp_connection_match *repl_tcp;
872
873 orig_cm = c->original_match;
874 repl_cm = c->reply_match;
875 orig_tcp = &orig_cm->protocol_state.tcp;
876 repl_tcp = &repl_cm->protocol_state.tcp;
877
878 /* update orig */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530879 if (orig_tcp->max_win < msg->tcp_rule.flow_max_window) {
880 orig_tcp->max_win = msg->tcp_rule.flow_max_window;
Nicolas Costa436926b2014-01-14 10:36:22 -0600881 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530882 if ((s32)(orig_tcp->end - msg->tcp_rule.flow_end) < 0) {
883 orig_tcp->end = msg->tcp_rule.flow_end;
Nicolas Costa436926b2014-01-14 10:36:22 -0600884 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530885 if ((s32)(orig_tcp->max_end - msg->tcp_rule.flow_max_end) < 0) {
886 orig_tcp->max_end = msg->tcp_rule.flow_max_end;
Nicolas Costa436926b2014-01-14 10:36:22 -0600887 }
888
889 /* update reply */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530890 if (repl_tcp->max_win < msg->tcp_rule.return_max_window) {
891 repl_tcp->max_win = msg->tcp_rule.return_max_window;
Nicolas Costa436926b2014-01-14 10:36:22 -0600892 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530893 if ((s32)(repl_tcp->end - msg->tcp_rule.return_end) < 0) {
894 repl_tcp->end = msg->tcp_rule.return_end;
Nicolas Costa436926b2014-01-14 10:36:22 -0600895 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530896 if ((s32)(repl_tcp->max_end - msg->tcp_rule.return_max_end) < 0) {
897 repl_tcp->max_end = msg->tcp_rule.return_max_end;
Nicolas Costa436926b2014-01-14 10:36:22 -0600898 }
899
900 /* update match flags */
901 orig_cm->flags &= ~SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
902 repl_cm->flags &= ~SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530903 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_NO_SEQ_CHECK) {
904
Nicolas Costa436926b2014-01-14 10:36:22 -0600905 orig_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
906 repl_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
907 }
908}
909
910static void
911sfe_ipv4_update_protocol_state(struct sfe_ipv4_connection *c,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530912 struct sfe_ipv4_rule_create_msg *msg)
Nicolas Costa436926b2014-01-14 10:36:22 -0600913{
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530914 switch (msg->tuple.protocol) {
Nicolas Costa436926b2014-01-14 10:36:22 -0600915 case IPPROTO_TCP:
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530916 sfe_ipv4_update_tcp_state(c, msg);
Nicolas Costa436926b2014-01-14 10:36:22 -0600917 break;
918 }
919}
920
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530921void sfe_ipv4_update_rule(struct sfe_ipv4_rule_create_msg *msg)
Nicolas Costa436926b2014-01-14 10:36:22 -0600922{
923 struct sfe_ipv4_connection *c;
924 struct sfe_ipv4 *si = &__si;
925
926 spin_lock_bh(&si->lock);
927
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530928 c = sfe_ipv4_find_connection(si,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530929 msg->tuple.protocol,
930 msg->tuple.flow_ip,
931 msg->tuple.flow_ident,
932 msg->tuple.return_ip,
933 msg->tuple.return_ident);
Nicolas Costa436926b2014-01-14 10:36:22 -0600934 if (c != NULL) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530935 sfe_ipv4_update_protocol_state(c, msg);
Nicolas Costa436926b2014-01-14 10:36:22 -0600936 }
937
938 spin_unlock_bh(&si->lock);
939}
940
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100941/*
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +0530942 * sfe_ipv4_xmit_eth_type_check()
943 * Checking if MAC header has to be written.
944 */
945static inline bool sfe_ipv4_xmit_eth_type_check(struct net_device *dev, u32 cm_flags)
946{
947 if (!(dev->flags & IFF_NOARP)) {
948 return true;
949 }
950
951 /*
952 * For PPPoE, since we are now supporting PPPoE encapsulation, we are writing L2 header.
953 */
954 if (unlikely(cm_flags & SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_ENCAP)) {
955 return true;
956 }
957
958 return false;
959}
960
961/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100962 * sfe_ipv4_create_rule()
963 * Create a forwarding rule.
964 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530965int sfe_ipv4_create_rule(struct sfe_ipv4_rule_create_msg *msg)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100966{
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600967 struct sfe_ipv4 *si = &__si;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530968 struct sfe_ipv4_connection *c, *c_old;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100969 struct sfe_ipv4_connection_match *original_cm;
970 struct sfe_ipv4_connection_match *reply_cm;
Matthew McClintockdb5ac512014-01-16 17:01:40 -0600971 struct net_device *dest_dev;
972 struct net_device *src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530973 struct sfe_ipv4_5tuple *tuple = &msg->tuple;
Suruchi Sumanc1a4a612021-10-21 14:50:23 +0530974 s32 flow_interface_num = msg->conn_rule.flow_top_interface_num;
975 s32 return_interface_num = msg->conn_rule.return_top_interface_num;
Amitesh Anand63be37d2021-12-24 20:51:48 +0530976 struct net *net;
977 struct sock *sk;
978 unsigned int src_if_idx;
Matthew McClintockdb5ac512014-01-16 17:01:40 -0600979
Suruchi Sumanc1a4a612021-10-21 14:50:23 +0530980 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_FLOW_BOTTOM_INTERFACE) {
981 flow_interface_num = msg->conn_rule.flow_interface_num;
982 }
983
984 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_RETURN_BOTTOM_INTERFACE) {
985 return_interface_num = msg->conn_rule.return_interface_num;
986 }
987
988 src_dev = dev_get_by_index(&init_net, flow_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530989 if (!src_dev) {
990 DEBUG_WARN("%px: Unable to find src_dev corresponding to %d\n", msg,
Suruchi Sumanc1a4a612021-10-21 14:50:23 +0530991 flow_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530992 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
993 return -EINVAL;
994 }
995
Suruchi Sumanc1a4a612021-10-21 14:50:23 +0530996 dest_dev = dev_get_by_index(&init_net, return_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530997 if (!dest_dev) {
998 DEBUG_WARN("%px: Unable to find dest_dev corresponding to %d\n", msg,
Suruchi Sumanc1a4a612021-10-21 14:50:23 +0530999 return_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301000 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
1001 dev_put(src_dev);
1002 return -EINVAL;
1003 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001004
Matthew McClintock389b42a2014-09-24 14:05:51 -05001005 if (unlikely((dest_dev->reg_state != NETREG_REGISTERED) ||
1006 (src_dev->reg_state != NETREG_REGISTERED))) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301007 dev_put(src_dev);
1008 dev_put(dest_dev);
1009 DEBUG_WARN("%px: src_dev=%s and dest_dev=%s are unregistered\n", msg,
1010 src_dev->name, dest_dev->name);
1011 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
Matthew McClintock389b42a2014-09-24 14:05:51 -05001012 return -EINVAL;
1013 }
1014
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301015 /*
1016 * Allocate the various connection tracking objects.
1017 */
1018 c = (struct sfe_ipv4_connection *)kmalloc(sizeof(struct sfe_ipv4_connection), GFP_ATOMIC);
1019 if (unlikely(!c)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301020 DEBUG_WARN("%px: memory allocation of connection entry failed\n", msg);
1021 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
1022 dev_put(src_dev);
1023 dev_put(dest_dev);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301024 return -ENOMEM;
1025 }
1026
1027 original_cm = (struct sfe_ipv4_connection_match *)kmalloc(sizeof(struct sfe_ipv4_connection_match), GFP_ATOMIC);
1028 if (unlikely(!original_cm)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301029 DEBUG_WARN("%px: memory allocation of connection match entry failed\n", msg);
1030 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301031 kfree(c);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301032 dev_put(src_dev);
1033 dev_put(dest_dev);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301034 return -ENOMEM;
1035 }
1036
1037 reply_cm = (struct sfe_ipv4_connection_match *)kmalloc(sizeof(struct sfe_ipv4_connection_match), GFP_ATOMIC);
1038 if (unlikely(!reply_cm)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301039 DEBUG_WARN("%px: memory allocation of connection match entry failed\n", msg);
1040 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301041 kfree(original_cm);
1042 kfree(c);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301043 dev_put(src_dev);
1044 dev_put(dest_dev);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301045 return -ENOMEM;
1046 }
1047
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301048 this_cpu_inc(si->stats_pcpu->connection_create_requests64);
1049
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001050 spin_lock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001051
1052 /*
Nicolas Costa436926b2014-01-14 10:36:22 -06001053 * Check to see if there is already a flow that matches the rule we're
1054 * trying to create. If there is then we can't create a new one.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001055 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301056 c_old = sfe_ipv4_find_connection(si,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301057 msg->tuple.protocol,
1058 msg->tuple.flow_ip,
1059 msg->tuple.flow_ident,
1060 msg->tuple.return_ip,
1061 msg->tuple.return_ident);
1062
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301063 if (c_old != NULL) {
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301064 this_cpu_inc(si->stats_pcpu->connection_create_collisions64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001065
1066 /*
Nicolas Costa436926b2014-01-14 10:36:22 -06001067 * If we already have the flow then it's likely that this
1068 * request to create the connection rule contains more
1069 * up-to-date information. Check and update accordingly.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001070 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301071 sfe_ipv4_update_protocol_state(c, msg);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001072 spin_unlock_bh(&si->lock);
1073
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301074 kfree(reply_cm);
1075 kfree(original_cm);
1076 kfree(c);
1077
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301078 dev_put(src_dev);
1079 dev_put(dest_dev);
1080
Amitesh Anand63be37d2021-12-24 20:51:48 +05301081 DEBUG_TRACE("%px: connection already exists - p:%d\n"
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301082 " s: %s:%pM:%pI4:%u, d: %s:%pM:%pI4:%u\n",
Amitesh Anand63be37d2021-12-24 20:51:48 +05301083 msg, tuple->protocol,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301084 src_dev->name, msg->conn_rule.flow_mac, &tuple->flow_ip, ntohs(tuple->flow_ident),
1085 dest_dev->name, msg->conn_rule.return_mac, &tuple->return_ip, ntohs(tuple->return_ident));
1086
Nicolas Costa514fde02014-01-13 15:50:29 -06001087 return -EADDRINUSE;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001088 }
1089
1090 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001091 * Fill in the "original" direction connection matching object.
1092 * Note that the transmit MAC address is "dest_mac_xlate" because
1093 * we always know both ends of a connection by their translated
1094 * addresses and not their public addresses.
1095 */
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001096 original_cm->match_dev = src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301097 original_cm->match_protocol = tuple->protocol;
1098 original_cm->match_src_ip = tuple->flow_ip;
1099 original_cm->match_src_port = tuple->flow_ident;
1100 original_cm->match_dest_ip = tuple->return_ip;
1101 original_cm->match_dest_port = tuple->return_ident;
1102
1103 original_cm->xlate_src_ip = msg->conn_rule.flow_ip_xlate;
1104 original_cm->xlate_src_port = msg->conn_rule.flow_ident_xlate;
1105 original_cm->xlate_dest_ip = msg->conn_rule.return_ip_xlate;
1106 original_cm->xlate_dest_port =msg->conn_rule.return_ident_xlate;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301107 atomic_set(&original_cm->rx_packet_count, 0);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001108 original_cm->rx_packet_count64 = 0;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301109 atomic_set(&original_cm->rx_byte_count, 0);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001110 original_cm->rx_byte_count64 = 0;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301111
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001112 original_cm->xmit_dev = dest_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301113 original_cm->xmit_dev_mtu = msg->conn_rule.return_mtu;
1114
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001115 original_cm->connection = c;
1116 original_cm->counter_match = reply_cm;
1117 original_cm->flags = 0;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301118
Amitesh Anand63be37d2021-12-24 20:51:48 +05301119 /*
1120 * UDP Socket is valid only in decap direction.
1121 */
1122 RCU_INIT_POINTER(original_cm->up, NULL);
1123
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301124 if (msg->valid_flags & SFE_RULE_CREATE_QOS_VALID) {
1125 original_cm->priority = msg->qos_rule.flow_qos_tag;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001126 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PRIORITY_REMARK;
1127 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301128
1129 if (msg->valid_flags & SFE_RULE_CREATE_DSCP_MARKING_VALID) {
1130 original_cm->dscp = msg->dscp_rule.flow_dscp << SFE_IPV4_DSCP_SHIFT;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001131 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_DSCP_REMARK;
1132 }
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301133
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301134 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1135 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_BRIDGE_FLOW;
1136 }
1137
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001138#ifdef CONFIG_NF_FLOW_COOKIE
1139 original_cm->flow_cookie = 0;
1140#endif
Zhi Chen8748eb32015-06-18 12:58:48 -07001141#ifdef CONFIG_XFRM
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301142 if (msg->valid_flags & SFE_RULE_CREATE_DIRECTION_VALID) {
1143 original_cm->flow_accel = msg->direction_rule.flow_accel;
1144 } else {
1145 original_cm->flow_accel = 1;
1146 }
Zhi Chen8748eb32015-06-18 12:58:48 -07001147#endif
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301148 /*
1149 * If l2_features are disabled and flow uses l2 features such as macvlan/bridge/pppoe/vlan,
1150 * bottom interfaces are expected to be disabled in the flow rule and always top interfaces
1151 * are used. In such cases, do not use HW csum offload. csum offload is used only when we
1152 * are sending directly to the destination interface that supports it.
1153 */
Suruchi Sumanf2077182022-01-13 21:35:23 +05301154 if (likely(dest_dev->features & NETIF_F_HW_CSUM) && !netif_is_vxlan(dest_dev)) {
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301155 if ((msg->conn_rule.return_top_interface_num == msg->conn_rule.return_interface_num) ||
1156 (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_RETURN_BOTTOM_INTERFACE)) {
Suruchi Sumanf2077182022-01-13 21:35:23 +05301157 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_CSUM_OFFLOAD;
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301158 }
1159 }
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001160
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +05301161 reply_cm->flags = 0;
1162
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001163 /*
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301164 * Adding PPPoE parameters to original and reply entries based on the direction where
1165 * PPPoE header is valid in ECM rule.
1166 *
1167 * If PPPoE is valid in flow direction (from interface is PPPoE), then
1168 * original cm will have PPPoE at ingress (strip PPPoE header)
1169 * reply cm will have PPPoE at egress (add PPPoE header)
1170 *
1171 * If PPPoE is valid in return direction (to interface is PPPoE), then
1172 * original cm will have PPPoE at egress (add PPPoE header)
1173 * reply cm will have PPPoE at ingress (strip PPPoE header)
1174 */
1175 if (msg->valid_flags & SFE_RULE_CREATE_PPPOE_DECAP_VALID) {
1176 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_DECAP;
1177 original_cm->pppoe_session_id = msg->pppoe_rule.flow_pppoe_session_id;
1178 ether_addr_copy(original_cm->pppoe_remote_mac, msg->pppoe_rule.flow_pppoe_remote_mac);
1179
1180 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_ENCAP;
1181 reply_cm->pppoe_session_id = msg->pppoe_rule.flow_pppoe_session_id;
1182 ether_addr_copy(reply_cm->pppoe_remote_mac, msg->pppoe_rule.flow_pppoe_remote_mac);
1183 }
1184
1185 if (msg->valid_flags & SFE_RULE_CREATE_PPPOE_ENCAP_VALID) {
1186 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_ENCAP;
1187 original_cm->pppoe_session_id = msg->pppoe_rule.return_pppoe_session_id;
1188 ether_addr_copy(original_cm->pppoe_remote_mac, msg->pppoe_rule.return_pppoe_remote_mac);
1189
1190 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_DECAP;
1191 reply_cm->pppoe_session_id = msg->pppoe_rule.return_pppoe_session_id;
1192 ether_addr_copy(reply_cm->pppoe_remote_mac, msg->pppoe_rule.return_pppoe_remote_mac);
1193 }
1194
1195 /*
Ken Zhubbf49652021-09-12 15:33:09 -07001196 * For the non-arp interface, we don't write L2 HDR.
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001197 */
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +05301198 if (sfe_ipv4_xmit_eth_type_check(dest_dev, original_cm->flags)) {
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301199
1200 /*
1201 * Check whether the rule has configured a specific source MAC address to use.
1202 * This is needed when virtual L3 interfaces such as br-lan, macvlan, vlan are used during egress
1203 */
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301204
1205 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1206 ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)msg->conn_rule.flow_mac);
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301207 } else {
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301208 if ((msg->valid_flags & SFE_RULE_CREATE_SRC_MAC_VALID) &&
1209 (msg->src_mac_rule.mac_valid_flags & SFE_SRC_MAC_RETURN_VALID)) {
1210 ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)msg->src_mac_rule.return_src_mac);
1211 } else {
1212 ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)dest_dev->dev_addr);
1213 }
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301214 }
1215
1216 ether_addr_copy((u8 *)original_cm->xmit_dest_mac, (u8 *)msg->conn_rule.return_mac);
1217
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001218 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_L2_HDR;
1219
1220 /*
1221 * If our dev writes Ethernet headers then we can write a really fast
1222 * version.
1223 */
1224 if (dest_dev->header_ops) {
1225 if (dest_dev->header_ops->create == eth_header) {
1226 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR;
1227 }
1228 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001229 }
1230
1231 /*
1232 * Fill in the "reply" direction connection matching object.
1233 */
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001234 reply_cm->match_dev = dest_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301235 reply_cm->match_protocol = tuple->protocol;
1236 reply_cm->match_src_ip = msg->conn_rule.return_ip_xlate;
Amitesh Anand63be37d2021-12-24 20:51:48 +05301237
1238 /*
1239 * Keep source port as 0 for VxLAN tunnels.
1240 */
1241 if (netif_is_vxlan(src_dev) || netif_is_vxlan(dest_dev)) {
1242 reply_cm->match_src_port = 0;
1243 } else {
1244 reply_cm->match_src_port = msg->conn_rule.return_ident_xlate;
1245 }
1246
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301247 reply_cm->match_dest_ip = msg->conn_rule.flow_ip_xlate;
1248 reply_cm->match_dest_port = msg->conn_rule.flow_ident_xlate;
1249
1250 reply_cm->xlate_src_ip = tuple->return_ip;
1251 reply_cm->xlate_src_port = tuple->return_ident;
1252 reply_cm->xlate_dest_ip = tuple->flow_ip;
1253 reply_cm->xlate_dest_port = tuple->flow_ident;;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301254
1255 atomic_set(&reply_cm->rx_packet_count, 0);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001256 reply_cm->rx_packet_count64 = 0;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301257 atomic_set(&reply_cm->rx_byte_count, 0);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001258 reply_cm->rx_byte_count64 = 0;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301259
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001260 reply_cm->xmit_dev = src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301261 reply_cm->xmit_dev_mtu = msg->conn_rule.flow_mtu;
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301262
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001263 reply_cm->connection = c;
1264 reply_cm->counter_match = original_cm;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301265 if (msg->valid_flags & SFE_RULE_CREATE_QOS_VALID) {
1266 reply_cm->priority = msg->qos_rule.return_qos_tag;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001267 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PRIORITY_REMARK;
1268 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301269 if (msg->valid_flags & SFE_RULE_CREATE_DSCP_MARKING_VALID) {
1270 reply_cm->dscp = msg->dscp_rule.return_dscp << SFE_IPV4_DSCP_SHIFT;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001271 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_DSCP_REMARK;
1272 }
Amitesh Anand63be37d2021-12-24 20:51:48 +05301273
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301274 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1275 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_BRIDGE_FLOW;
1276 }
1277
Amitesh Anand63be37d2021-12-24 20:51:48 +05301278 /*
1279 * Setup UDP Socket if found to be valid for decap.
1280 */
1281 RCU_INIT_POINTER(reply_cm->up, NULL);
1282 net = dev_net(reply_cm->match_dev);
1283 src_if_idx = src_dev->ifindex;
1284
1285 rcu_read_lock();
1286
1287 /*
1288 * Look for the associated sock object.
1289 * __udp4_lib_lookup() holds a reference for this sock object,
1290 * which will be released in sfe_ipv4_free_connection_rcu()
1291 */
1292#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
1293 sk = __udp4_lib_lookup(net, reply_cm->match_dest_ip, reply_cm->match_dest_port,
1294 reply_cm->xlate_src_ip, reply_cm->xlate_src_port, src_if_idx, &udp_table);
1295#else
1296 sk = __udp4_lib_lookup(net, reply_cm->match_dest_ip, reply_cm->match_dest_port,
1297 reply_cm->xlate_src_ip, reply_cm->xlate_src_port, src_if_idx, 0, &udp_table, NULL);
1298#endif
1299
1300 rcu_read_unlock();
1301
1302 /*
1303 * We set the UDP sock pointer as valid only for decap direction.
1304 */
1305 if (sk && udp_sk(sk)->encap_type) {
1306#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
1307 if (!atomic_add_unless(&sk->sk_refcnt, 1, 0)) {
1308#else
1309 if (!refcount_inc_not_zero(&sk->sk_refcnt)) {
1310#endif
1311 kfree(reply_cm);
1312 kfree(original_cm);
1313 kfree(c);
1314
1315 DEBUG_TRACE("%px: sfe: unable to take reference for socket(%px) p:%d\n"
1316 " s: %s:%pM:%pI4:%u, d: %s:%pM:%pI4:%u\n",
1317 msg, sk, tuple->protocol,
1318 src_dev->name, msg->conn_rule.flow_mac, &tuple->flow_ip, ntohs(tuple->flow_ident),
1319 dest_dev->name, msg->conn_rule.return_mac, &tuple->return_ip, ntohs(tuple->return_ident));
1320
1321 dev_put(src_dev);
1322 dev_put(dest_dev);
1323
1324 return -ESHUTDOWN;
1325 }
1326
1327 rcu_assign_pointer(reply_cm->up, udp_sk(sk));
1328
1329 DEBUG_INFO("%px: Sock(%px) lookup success with reply_cm direction\n", msg, sk);
1330 DEBUG_INFO("%px: SFE connection -\n"
1331 " s: %s:%pI4(%pI4):%u(%u)\n"
1332 " d: %s:%pI4(%pI4):%u(%u)\n",
1333 msg, reply_cm->match_dev->name, &reply_cm->match_src_ip, &reply_cm->xlate_src_ip,
1334 ntohs(reply_cm->match_src_port), ntohs(reply_cm->xlate_src_port),
1335 reply_cm->xmit_dev->name, &reply_cm->match_dest_ip, &reply_cm->xlate_dest_ip,
1336 ntohs(reply_cm->match_dest_port), ntohs(reply_cm->xlate_dest_port));
1337 }
1338
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001339#ifdef CONFIG_NF_FLOW_COOKIE
1340 reply_cm->flow_cookie = 0;
1341#endif
Zhi Chen8748eb32015-06-18 12:58:48 -07001342#ifdef CONFIG_XFRM
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301343 if (msg->valid_flags & SFE_RULE_CREATE_DIRECTION_VALID) {
1344 reply_cm->flow_accel = msg->direction_rule.return_accel;
1345 } else {
1346 reply_cm->flow_accel = 1;
1347 }
1348
Zhi Chen8748eb32015-06-18 12:58:48 -07001349#endif
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301350 /*
1351 * If l2_features are disabled and flow uses l2 features such as macvlan/bridge/pppoe/vlan,
1352 * bottom interfaces are expected to be disabled in the flow rule and always top interfaces
1353 * are used. In such cases, do not use HW csum offload. csum offload is used only when we
1354 * are sending directly to the destination interface that supports it.
1355 */
Suruchi Sumanf2077182022-01-13 21:35:23 +05301356 if (likely(src_dev->features & NETIF_F_HW_CSUM) && !(netif_is_vxlan(src_dev) || netif_is_vxlan(dest_dev))) {
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301357 if ((msg->conn_rule.flow_top_interface_num == msg->conn_rule.flow_interface_num) ||
1358 (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_FLOW_BOTTOM_INTERFACE)) {
1359 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_CSUM_OFFLOAD;
1360 }
1361 }
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001362
1363 /*
Ken Zhubbf49652021-09-12 15:33:09 -07001364 * For the non-arp interface, we don't write L2 HDR.
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001365 */
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +05301366 if (sfe_ipv4_xmit_eth_type_check(src_dev, reply_cm->flags)) {
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301367
1368 /*
1369 * Check whether the rule has configured a specific source MAC address to use.
1370 * This is needed when virtual L3 interfaces such as br-lan, macvlan, vlan are used during egress
1371 */
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301372
1373 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1374 ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)msg->conn_rule.return_mac);
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301375 } else {
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301376 if ((msg->valid_flags & SFE_RULE_CREATE_SRC_MAC_VALID) &&
1377 (msg->src_mac_rule.mac_valid_flags & SFE_SRC_MAC_FLOW_VALID)) {
1378 ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)msg->src_mac_rule.flow_src_mac);
1379 } else {
1380 ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)src_dev->dev_addr);
1381 }
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301382 }
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301383
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301384 ether_addr_copy((u8 *)reply_cm->xmit_dest_mac, (u8 *)msg->conn_rule.flow_mac);
1385
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001386 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_L2_HDR;
1387
1388 /*
1389 * If our dev writes Ethernet headers then we can write a really fast
1390 * version.
1391 */
1392 if (src_dev->header_ops) {
1393 if (src_dev->header_ops->create == eth_header) {
1394 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR;
1395 }
1396 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001397 }
1398
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301399 if ((tuple->return_ip != msg->conn_rule.return_ip_xlate) ||
1400 (tuple->return_ident != msg->conn_rule.return_ident_xlate)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001401 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST;
1402 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC;
1403 }
1404
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301405 if ((tuple->flow_ip != msg->conn_rule.flow_ip_xlate) ||
1406 (tuple->flow_ident != msg->conn_rule.flow_ident_xlate)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001407 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC;
1408 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST;
1409 }
1410
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301411 c->protocol = tuple->protocol;
1412 c->src_ip = tuple->flow_ip;
1413 c->src_ip_xlate = msg->conn_rule.flow_ip_xlate;
1414 c->src_port = tuple->flow_ident;
1415 c->src_port_xlate = msg->conn_rule.flow_ident_xlate;
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001416 c->original_dev = src_dev;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001417 c->original_match = original_cm;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301418 c->dest_ip = tuple->return_ip;
1419 c->dest_ip_xlate = msg->conn_rule.return_ip_xlate;
1420 c->dest_port = tuple->return_ident;
1421 c->dest_port_xlate = msg->conn_rule.return_ident_xlate;
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001422 c->reply_dev = dest_dev;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001423 c->reply_match = reply_cm;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301424 c->mark = 0; /* TODO : no mark setting for create rule */
Xiaoping Fan34586472015-07-03 02:20:35 -07001425 c->debug_read_seq = 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001426 c->last_sync_jiffies = get_jiffies_64();
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301427 c->removed = false;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001428
1429 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001430 * Initialize the protocol-specific information that we track.
1431 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301432 switch (tuple->protocol) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001433 case IPPROTO_TCP:
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301434 original_cm->protocol_state.tcp.win_scale = msg->tcp_rule.flow_window_scale;
1435 original_cm->protocol_state.tcp.max_win = msg->tcp_rule.flow_max_window ? msg->tcp_rule.flow_max_window : 1;
1436 original_cm->protocol_state.tcp.end = msg->tcp_rule.flow_end;
1437 original_cm->protocol_state.tcp.max_end = msg->tcp_rule.flow_max_end;
1438
1439 reply_cm->protocol_state.tcp.win_scale = msg->tcp_rule.return_window_scale;
1440 reply_cm->protocol_state.tcp.max_win = msg->tcp_rule.return_max_window ? msg->tcp_rule.return_max_window : 1;
1441 reply_cm->protocol_state.tcp.end = msg->tcp_rule.return_end;
1442 reply_cm->protocol_state.tcp.max_end = msg->tcp_rule.return_max_end;
1443
1444 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_NO_SEQ_CHECK) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001445 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
1446 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
1447 }
1448 break;
1449 }
1450
1451 sfe_ipv4_connection_match_compute_translations(original_cm);
1452 sfe_ipv4_connection_match_compute_translations(reply_cm);
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301453 sfe_ipv4_insert_connection(si, c);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001454
1455 spin_unlock_bh(&si->lock);
1456
1457 /*
1458 * We have everything we need!
1459 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301460 DEBUG_INFO("new connection - p: %d\n"
Tian Yang45f39c82020-10-06 14:07:47 -07001461 " s: %s:%pxM(%pxM):%pI4(%pI4):%u(%u)\n"
1462 " d: %s:%pxM(%pxM):%pI4(%pI4):%u(%u)\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301463 tuple->protocol,
1464 src_dev->name, msg->conn_rule.flow_mac, NULL,
1465 &tuple->flow_ip, &msg->conn_rule.flow_ip_xlate, ntohs(tuple->flow_ident), ntohs(msg->conn_rule.flow_ident_xlate),
1466 dest_dev->name, NULL, msg->conn_rule.return_mac,
1467 &tuple->return_ip, &msg->conn_rule.return_ip_xlate, ntohs(tuple->return_ident), ntohs(msg->conn_rule.return_ident_xlate));
Nicolas Costa514fde02014-01-13 15:50:29 -06001468
1469 return 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001470}
1471
1472/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001473 * sfe_ipv4_destroy_rule()
1474 * Destroy a forwarding rule.
1475 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301476void sfe_ipv4_destroy_rule(struct sfe_ipv4_rule_destroy_msg *msg)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001477{
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001478 struct sfe_ipv4 *si = &__si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001479 struct sfe_ipv4_connection *c;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301480 bool ret;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301481 struct sfe_ipv4_5tuple *tuple = &msg->tuple;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001482
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301483 this_cpu_inc(si->stats_pcpu->connection_destroy_requests64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001484 spin_lock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001485
1486 /*
1487 * Check to see if we have a flow that matches the rule we're trying
1488 * to destroy. If there isn't then we can't destroy it.
1489 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301490 c = sfe_ipv4_find_connection(si, tuple->protocol, tuple->flow_ip, tuple->flow_ident,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301491 tuple->return_ip, tuple->return_ident);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001492 if (!c) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001493 spin_unlock_bh(&si->lock);
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301494 this_cpu_inc(si->stats_pcpu->connection_destroy_misses64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001495
1496 DEBUG_TRACE("connection does not exist - p: %d, s: %pI4:%u, d: %pI4:%u\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301497 tuple->protocol, &tuple->flow_ip, ntohs(tuple->flow_ident),
1498 &tuple->return_ip, ntohs(tuple->return_ident));
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001499 return;
1500 }
1501
1502 /*
1503 * Remove our connection details from the hash tables.
1504 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301505 ret = sfe_ipv4_remove_connection(si, c);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001506 spin_unlock_bh(&si->lock);
1507
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301508 if (ret) {
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301509 sfe_ipv4_flush_connection(si, c, SFE_SYNC_REASON_DESTROY);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301510 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001511
1512 DEBUG_INFO("connection destroyed - p: %d, s: %pI4:%u, d: %pI4:%u\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301513 tuple->protocol, &tuple->flow_ip, ntohs(tuple->flow_ident),
1514 &tuple->return_ip, ntohs(tuple->return_ident));
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001515}
1516
1517/*
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001518 * sfe_ipv4_register_sync_rule_callback()
1519 * Register a callback for rule synchronization.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001520 */
Xiaoping Fand44a5b42015-05-26 17:37:37 -07001521void sfe_ipv4_register_sync_rule_callback(sfe_sync_rule_callback_t sync_rule_callback)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001522{
1523 struct sfe_ipv4 *si = &__si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001524
1525 spin_lock_bh(&si->lock);
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001526 rcu_assign_pointer(si->sync_rule_callback, sync_rule_callback);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001527 spin_unlock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001528}
1529
1530/*
1531 * sfe_ipv4_get_debug_dev()
1532 */
1533static ssize_t sfe_ipv4_get_debug_dev(struct device *dev,
1534 struct device_attribute *attr,
1535 char *buf)
1536{
1537 struct sfe_ipv4 *si = &__si;
1538 ssize_t count;
1539 int num;
1540
1541 spin_lock_bh(&si->lock);
1542 num = si->debug_dev;
1543 spin_unlock_bh(&si->lock);
1544
1545 count = snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", num);
1546 return count;
1547}
1548
1549/*
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001550 * sysfs attributes.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001551 */
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001552static const struct device_attribute sfe_ipv4_debug_dev_attr =
Xiaoping Fane70da412016-02-26 16:47:57 -08001553 __ATTR(debug_dev, S_IWUSR | S_IRUGO, sfe_ipv4_get_debug_dev, NULL);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001554
1555/*
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001556 * sfe_ipv4_destroy_all_rules_for_dev()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001557 * Destroy all connections that match a particular device.
1558 *
1559 * If we pass dev as NULL then this destroys all connections.
1560 */
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001561void sfe_ipv4_destroy_all_rules_for_dev(struct net_device *dev)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001562{
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001563 struct sfe_ipv4 *si = &__si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001564 struct sfe_ipv4_connection *c;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301565 bool ret;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001566
Xiaoping Fan34586472015-07-03 02:20:35 -07001567another_round:
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001568 spin_lock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001569
Xiaoping Fan34586472015-07-03 02:20:35 -07001570 for (c = si->all_connections_head; c; c = c->all_connections_next) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001571 /*
Xiaoping Fan34586472015-07-03 02:20:35 -07001572 * Does this connection relate to the device we are destroying?
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001573 */
1574 if (!dev
1575 || (dev == c->original_dev)
1576 || (dev == c->reply_dev)) {
Xiaoping Fan34586472015-07-03 02:20:35 -07001577 break;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001578 }
Xiaoping Fan34586472015-07-03 02:20:35 -07001579 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001580
Xiaoping Fan34586472015-07-03 02:20:35 -07001581 if (c) {
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301582 ret = sfe_ipv4_remove_connection(si, c);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001583 }
1584
1585 spin_unlock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07001586
1587 if (c) {
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301588 if (ret) {
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301589 sfe_ipv4_flush_connection(si, c, SFE_SYNC_REASON_DESTROY);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301590 }
Xiaoping Fan34586472015-07-03 02:20:35 -07001591 goto another_round;
1592 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001593}
1594
1595/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001596 * sfe_ipv4_periodic_sync()
1597 */
Ken Zhu137722d2021-09-23 17:57:36 -07001598static void sfe_ipv4_periodic_sync(struct work_struct *work)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001599{
Ken Zhu137722d2021-09-23 17:57:36 -07001600 struct sfe_ipv4 *si = container_of((struct delayed_work *)work, struct sfe_ipv4, sync_dwork);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07001601 u64 now_jiffies;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001602 int quota;
Xiaoping Fand44a5b42015-05-26 17:37:37 -07001603 sfe_sync_rule_callback_t sync_rule_callback;
Ken Zhudc423672021-09-02 18:27:01 -07001604 struct sfe_ipv4_connection *c;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001605
1606 now_jiffies = get_jiffies_64();
1607
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001608 rcu_read_lock();
1609 sync_rule_callback = rcu_dereference(si->sync_rule_callback);
1610 if (!sync_rule_callback) {
1611 rcu_read_unlock();
1612 goto done;
1613 }
1614
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001615 spin_lock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001616
1617 /*
Ken Zhudc423672021-09-02 18:27:01 -07001618 * If we have reached the end of the connection list, walk from
1619 * the connection head.
1620 */
1621 c = si->wc_next;
1622 if (unlikely(!c)) {
1623 c = si->all_connections_head;
1624 }
1625
1626 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001627 * Get an estimate of the number of connections to parse in this sync.
1628 */
1629 quota = (si->num_connections + 63) / 64;
1630
1631 /*
Ken Zhudc423672021-09-02 18:27:01 -07001632 * Walk the "all connection" list and sync the connection state.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001633 */
Ken Zhudc423672021-09-02 18:27:01 -07001634 while (likely(c && quota)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001635 struct sfe_ipv4_connection_match *cm;
1636 struct sfe_ipv4_connection_match *counter_cm;
Xiaoping Fand44a5b42015-05-26 17:37:37 -07001637 struct sfe_connection_sync sis;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001638
Ken Zhudc423672021-09-02 18:27:01 -07001639 cm = c->original_match;
1640 counter_cm = c->reply_match;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001641
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001642 /*
Ken Zhudc423672021-09-02 18:27:01 -07001643 * Didn't receive packets in the original direction or reply
1644 * direction, move to the next connection.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001645 */
Ken Zhudc423672021-09-02 18:27:01 -07001646 if ((!atomic_read(&cm->rx_packet_count)) && !(atomic_read(&counter_cm->rx_packet_count))) {
1647 c = c->all_connections_next;
1648 continue;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001649 }
1650
Ken Zhudc423672021-09-02 18:27:01 -07001651 quota--;
Matthew McClintockaf48f1e2014-01-23 15:29:19 -06001652
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301653 sfe_ipv4_gen_sync_connection(si, c, &sis, SFE_SYNC_REASON_STATS, now_jiffies);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001654
Ken Zhudc423672021-09-02 18:27:01 -07001655 si->wc_next = c->all_connections_next;
1656
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001657 /*
1658 * We don't want to be holding the lock when we sync!
1659 */
1660 spin_unlock_bh(&si->lock);
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001661 sync_rule_callback(&sis);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001662 spin_lock_bh(&si->lock);
Ken Zhudc423672021-09-02 18:27:01 -07001663
1664 /*
1665 * c must be set and used in the same lock/unlock window;
1666 * because c could be removed when we don't hold the lock,
1667 * so delay grabbing until after the callback and relock.
1668 */
1669 c = si->wc_next;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001670 }
1671
Ken Zhudc423672021-09-02 18:27:01 -07001672 /*
1673 * At the end of the sync, put the wc_next to the connection we left.
1674 */
1675 si->wc_next = c;
1676
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001677 spin_unlock_bh(&si->lock);
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001678 rcu_read_unlock();
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001679
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001680done:
Ken Zhu137722d2021-09-23 17:57:36 -07001681 schedule_delayed_work_on(si->work_cpu, (struct delayed_work *)work, ((HZ + 99) / 100));
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001682}
1683
1684#define CHAR_DEV_MSG_SIZE 768
1685
1686/*
1687 * sfe_ipv4_debug_dev_read_start()
1688 * Generate part of the XML output.
1689 */
1690static bool sfe_ipv4_debug_dev_read_start(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
1691 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
1692{
1693 int bytes_read;
1694
Xiaoping Fan34586472015-07-03 02:20:35 -07001695 si->debug_read_seq++;
1696
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001697 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "<sfe_ipv4>\n");
1698 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1699 return false;
1700 }
1701
1702 *length -= bytes_read;
1703 *total_read += bytes_read;
1704
1705 ws->state++;
1706 return true;
1707}
1708
1709/*
1710 * sfe_ipv4_debug_dev_read_connections_start()
1711 * Generate part of the XML output.
1712 */
1713static bool sfe_ipv4_debug_dev_read_connections_start(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
1714 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
1715{
1716 int bytes_read;
1717
1718 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<connections>\n");
1719 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1720 return false;
1721 }
1722
1723 *length -= bytes_read;
1724 *total_read += bytes_read;
1725
1726 ws->state++;
1727 return true;
1728}
1729
1730/*
1731 * sfe_ipv4_debug_dev_read_connections_connection()
1732 * Generate part of the XML output.
1733 */
1734static bool sfe_ipv4_debug_dev_read_connections_connection(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
1735 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
1736{
1737 struct sfe_ipv4_connection *c;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001738 struct sfe_ipv4_connection_match *original_cm;
1739 struct sfe_ipv4_connection_match *reply_cm;
1740 int bytes_read;
1741 int protocol;
1742 struct net_device *src_dev;
Dave Hudson87973cd2013-10-22 16:00:04 +01001743 __be32 src_ip;
1744 __be32 src_ip_xlate;
1745 __be16 src_port;
1746 __be16 src_port_xlate;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07001747 u64 src_rx_packets;
1748 u64 src_rx_bytes;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001749 struct net_device *dest_dev;
Dave Hudson87973cd2013-10-22 16:00:04 +01001750 __be32 dest_ip;
1751 __be32 dest_ip_xlate;
1752 __be16 dest_port;
1753 __be16 dest_port_xlate;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07001754 u64 dest_rx_packets;
1755 u64 dest_rx_bytes;
1756 u64 last_sync_jiffies;
1757 u32 mark, src_priority, dest_priority, src_dscp, dest_dscp;
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301758 u32 packet, byte, original_cm_flags;
1759 u16 pppoe_session_id;
1760 u8 pppoe_remote_mac[ETH_ALEN];
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001761#ifdef CONFIG_NF_FLOW_COOKIE
1762 int src_flow_cookie, dst_flow_cookie;
1763#endif
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001764
1765 spin_lock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07001766
1767 for (c = si->all_connections_head; c; c = c->all_connections_next) {
1768 if (c->debug_read_seq < si->debug_read_seq) {
1769 c->debug_read_seq = si->debug_read_seq;
1770 break;
1771 }
1772 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001773
1774 /*
Xiaoping Fan34586472015-07-03 02:20:35 -07001775 * If there were no connections then move to the next state.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001776 */
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301777 if (!c || c->removed) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001778 spin_unlock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07001779 ws->state++;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001780 return true;
1781 }
1782
1783 original_cm = c->original_match;
1784 reply_cm = c->reply_match;
1785
1786 protocol = c->protocol;
1787 src_dev = c->original_dev;
1788 src_ip = c->src_ip;
1789 src_ip_xlate = c->src_ip_xlate;
1790 src_port = c->src_port;
1791 src_port_xlate = c->src_port_xlate;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001792 src_priority = original_cm->priority;
1793 src_dscp = original_cm->dscp >> SFE_IPV4_DSCP_SHIFT;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001794
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301795 sfe_ipv4_connection_match_update_summary_stats(original_cm, &packet, &byte);
1796 sfe_ipv4_connection_match_update_summary_stats(reply_cm, &packet, &byte);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001797
1798 src_rx_packets = original_cm->rx_packet_count64;
1799 src_rx_bytes = original_cm->rx_byte_count64;
1800 dest_dev = c->reply_dev;
1801 dest_ip = c->dest_ip;
1802 dest_ip_xlate = c->dest_ip_xlate;
1803 dest_port = c->dest_port;
1804 dest_port_xlate = c->dest_port_xlate;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001805 dest_priority = reply_cm->priority;
1806 dest_dscp = reply_cm->dscp >> SFE_IPV4_DSCP_SHIFT;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001807 dest_rx_packets = reply_cm->rx_packet_count64;
1808 dest_rx_bytes = reply_cm->rx_byte_count64;
1809 last_sync_jiffies = get_jiffies_64() - c->last_sync_jiffies;
Cristian Prundeanu592265e2013-12-26 11:01:22 -06001810 mark = c->mark;
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301811 original_cm_flags = original_cm->flags;
1812 pppoe_session_id = original_cm->pppoe_session_id;
1813 ether_addr_copy(pppoe_remote_mac, original_cm->pppoe_remote_mac);
1814
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001815#ifdef CONFIG_NF_FLOW_COOKIE
1816 src_flow_cookie = original_cm->flow_cookie;
1817 dst_flow_cookie = reply_cm->flow_cookie;
1818#endif
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001819 spin_unlock_bh(&si->lock);
1820
1821 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t\t<connection "
1822 "protocol=\"%u\" "
1823 "src_dev=\"%s\" "
1824 "src_ip=\"%pI4\" src_ip_xlate=\"%pI4\" "
1825 "src_port=\"%u\" src_port_xlate=\"%u\" "
Xiaoping Fane1963d42015-08-25 17:06:19 -07001826 "src_priority=\"%u\" src_dscp=\"%u\" "
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001827 "src_rx_pkts=\"%llu\" src_rx_bytes=\"%llu\" "
1828 "dest_dev=\"%s\" "
1829 "dest_ip=\"%pI4\" dest_ip_xlate=\"%pI4\" "
1830 "dest_port=\"%u\" dest_port_xlate=\"%u\" "
Xiaoping Fane1963d42015-08-25 17:06:19 -07001831 "dest_priority=\"%u\" dest_dscp=\"%u\" "
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001832 "dest_rx_pkts=\"%llu\" dest_rx_bytes=\"%llu\" "
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001833#ifdef CONFIG_NF_FLOW_COOKIE
1834 "src_flow_cookie=\"%d\" dst_flow_cookie=\"%d\" "
1835#endif
Cristian Prundeanu592265e2013-12-26 11:01:22 -06001836 "last_sync=\"%llu\" "
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301837 "mark=\"%08x\" ",
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001838 protocol,
1839 src_dev->name,
1840 &src_ip, &src_ip_xlate,
Dave Hudson87973cd2013-10-22 16:00:04 +01001841 ntohs(src_port), ntohs(src_port_xlate),
Xiaoping Fane1963d42015-08-25 17:06:19 -07001842 src_priority, src_dscp,
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001843 src_rx_packets, src_rx_bytes,
1844 dest_dev->name,
1845 &dest_ip, &dest_ip_xlate,
Dave Hudson87973cd2013-10-22 16:00:04 +01001846 ntohs(dest_port), ntohs(dest_port_xlate),
Xiaoping Fane1963d42015-08-25 17:06:19 -07001847 dest_priority, dest_dscp,
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001848 dest_rx_packets, dest_rx_bytes,
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001849#ifdef CONFIG_NF_FLOW_COOKIE
1850 src_flow_cookie, dst_flow_cookie,
1851#endif
Cristian Prundeanu592265e2013-12-26 11:01:22 -06001852 last_sync_jiffies, mark);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001853
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301854 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 +05301855 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 +05301856 pppoe_session_id, pppoe_remote_mac);
1857 }
1858
1859 bytes_read += snprintf(msg + bytes_read, CHAR_DEV_MSG_SIZE, "/>\n");
1860
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001861 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1862 return false;
1863 }
1864
1865 *length -= bytes_read;
1866 *total_read += bytes_read;
1867
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001868 return true;
1869}
1870
1871/*
1872 * sfe_ipv4_debug_dev_read_connections_end()
1873 * Generate part of the XML output.
1874 */
1875static bool sfe_ipv4_debug_dev_read_connections_end(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
1876 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
1877{
1878 int bytes_read;
1879
1880 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t</connections>\n");
1881 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1882 return false;
1883 }
1884
1885 *length -= bytes_read;
1886 *total_read += bytes_read;
1887
1888 ws->state++;
1889 return true;
1890}
1891
1892/*
1893 * sfe_ipv4_debug_dev_read_exceptions_start()
1894 * Generate part of the XML output.
1895 */
1896static bool sfe_ipv4_debug_dev_read_exceptions_start(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
1897 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
1898{
1899 int bytes_read;
1900
1901 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<exceptions>\n");
1902 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1903 return false;
1904 }
1905
1906 *length -= bytes_read;
1907 *total_read += bytes_read;
1908
1909 ws->state++;
1910 return true;
1911}
1912
1913/*
1914 * sfe_ipv4_debug_dev_read_exceptions_exception()
1915 * Generate part of the XML output.
1916 */
1917static bool sfe_ipv4_debug_dev_read_exceptions_exception(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
1918 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
1919{
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301920 int i;
1921 u64 val = 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001922
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301923 for_each_possible_cpu(i) {
1924 const struct sfe_ipv4_stats *s = per_cpu_ptr(si->stats_pcpu, i);
1925 val += s->exception_events64[ws->iter_exception];
1926 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001927
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301928 if (val) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001929 int bytes_read;
1930
1931 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE,
1932 "\t\t<exception name=\"%s\" count=\"%llu\" />\n",
1933 sfe_ipv4_exception_events_string[ws->iter_exception],
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301934 val);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001935 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1936 return false;
1937 }
1938
1939 *length -= bytes_read;
1940 *total_read += bytes_read;
1941 }
1942
1943 ws->iter_exception++;
1944 if (ws->iter_exception >= SFE_IPV4_EXCEPTION_EVENT_LAST) {
1945 ws->iter_exception = 0;
1946 ws->state++;
1947 }
1948
1949 return true;
1950}
1951
1952/*
1953 * sfe_ipv4_debug_dev_read_exceptions_end()
1954 * Generate part of the XML output.
1955 */
1956static bool sfe_ipv4_debug_dev_read_exceptions_end(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
1957 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
1958{
1959 int bytes_read;
1960
1961 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t</exceptions>\n");
1962 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1963 return false;
1964 }
1965
1966 *length -= bytes_read;
1967 *total_read += bytes_read;
1968
1969 ws->state++;
1970 return true;
1971}
1972
1973/*
1974 * sfe_ipv4_debug_dev_read_stats()
1975 * Generate part of the XML output.
1976 */
1977static bool sfe_ipv4_debug_dev_read_stats(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
1978 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
1979{
1980 int bytes_read;
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301981 struct sfe_ipv4_stats stats;
1982 unsigned int num_conn;
1983
1984 sfe_ipv4_update_summary_stats(si, &stats);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001985
1986 spin_lock_bh(&si->lock);
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301987 num_conn = si->num_connections;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001988 spin_unlock_bh(&si->lock);
1989
1990 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<stats "
1991 "num_connections=\"%u\" "
Amitesh Anand63be37d2021-12-24 20:51:48 +05301992 "pkts_dropped=\"%llu\" "
Xiaoping Fan59176422015-05-22 15:58:10 -07001993 "pkts_forwarded=\"%llu\" pkts_not_forwarded=\"%llu\" "
1994 "create_requests=\"%llu\" create_collisions=\"%llu\" "
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301995 "create_failures=\"%llu\" "
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001996 "destroy_requests=\"%llu\" destroy_misses=\"%llu\" "
1997 "flushes=\"%llu\" "
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +05301998 "hash_hits=\"%llu\" hash_reorders=\"%llu\" "
1999 "pppoe_encap_pkts_fwded=\"%llu\" "
2000 "pppoe_decap_pkts_fwded=\"%llu\" />\n",
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302001 num_conn,
Amitesh Anand63be37d2021-12-24 20:51:48 +05302002 stats.packets_dropped64,
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302003 stats.packets_forwarded64,
2004 stats.packets_not_forwarded64,
2005 stats.connection_create_requests64,
2006 stats.connection_create_collisions64,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05302007 stats.connection_create_failures64,
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302008 stats.connection_destroy_requests64,
2009 stats.connection_destroy_misses64,
2010 stats.connection_flushes64,
2011 stats.connection_match_hash_hits64,
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +05302012 stats.connection_match_hash_reorders64,
2013 stats.pppoe_encap_packets_forwarded64,
2014 stats.pppoe_decap_packets_forwarded64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002015 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2016 return false;
2017 }
2018
2019 *length -= bytes_read;
2020 *total_read += bytes_read;
2021
2022 ws->state++;
2023 return true;
2024}
2025
2026/*
2027 * sfe_ipv4_debug_dev_read_end()
2028 * Generate part of the XML output.
2029 */
2030static bool sfe_ipv4_debug_dev_read_end(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2031 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2032{
2033 int bytes_read;
2034
2035 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "</sfe_ipv4>\n");
2036 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2037 return false;
2038 }
2039
2040 *length -= bytes_read;
2041 *total_read += bytes_read;
2042
2043 ws->state++;
2044 return true;
2045}
2046
2047/*
2048 * Array of write functions that write various XML elements that correspond to
2049 * our XML output state machine.
2050 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07002051static 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 +01002052 sfe_ipv4_debug_dev_read_start,
2053 sfe_ipv4_debug_dev_read_connections_start,
2054 sfe_ipv4_debug_dev_read_connections_connection,
2055 sfe_ipv4_debug_dev_read_connections_end,
2056 sfe_ipv4_debug_dev_read_exceptions_start,
2057 sfe_ipv4_debug_dev_read_exceptions_exception,
2058 sfe_ipv4_debug_dev_read_exceptions_end,
2059 sfe_ipv4_debug_dev_read_stats,
2060 sfe_ipv4_debug_dev_read_end,
2061};
2062
2063/*
2064 * sfe_ipv4_debug_dev_read()
2065 * Send info to userspace upon read request from user
2066 */
2067static ssize_t sfe_ipv4_debug_dev_read(struct file *filp, char *buffer, size_t length, loff_t *offset)
2068{
2069 char msg[CHAR_DEV_MSG_SIZE];
2070 int total_read = 0;
2071 struct sfe_ipv4_debug_xml_write_state *ws;
2072 struct sfe_ipv4 *si = &__si;
2073
2074 ws = (struct sfe_ipv4_debug_xml_write_state *)filp->private_data;
2075 while ((ws->state != SFE_IPV4_DEBUG_XML_STATE_DONE) && (length > CHAR_DEV_MSG_SIZE)) {
2076 if ((sfe_ipv4_debug_xml_write_methods[ws->state])(si, buffer, msg, &length, &total_read, ws)) {
2077 continue;
2078 }
2079 }
2080
2081 return total_read;
2082}
2083
2084/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002085 * sfe_ipv4_debug_dev_open()
2086 */
2087static int sfe_ipv4_debug_dev_open(struct inode *inode, struct file *file)
2088{
2089 struct sfe_ipv4_debug_xml_write_state *ws;
2090
2091 ws = (struct sfe_ipv4_debug_xml_write_state *)file->private_data;
2092 if (!ws) {
2093 ws = kzalloc(sizeof(struct sfe_ipv4_debug_xml_write_state), GFP_KERNEL);
2094 if (!ws) {
2095 return -ENOMEM;
2096 }
2097
2098 ws->state = SFE_IPV4_DEBUG_XML_STATE_START;
2099 file->private_data = ws;
2100 }
2101
2102 return 0;
2103}
2104
2105/*
2106 * sfe_ipv4_debug_dev_release()
2107 */
2108static int sfe_ipv4_debug_dev_release(struct inode *inode, struct file *file)
2109{
2110 struct sfe_ipv4_debug_xml_write_state *ws;
2111
2112 ws = (struct sfe_ipv4_debug_xml_write_state *)file->private_data;
2113 if (ws) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002114 /*
2115 * We've finished with our output so free the write state.
2116 */
2117 kfree(ws);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05302118 file->private_data = NULL;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002119 }
2120
2121 return 0;
2122}
2123
2124/*
2125 * File operations used in the debug char device
2126 */
2127static struct file_operations sfe_ipv4_debug_dev_fops = {
2128 .read = sfe_ipv4_debug_dev_read,
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002129 .open = sfe_ipv4_debug_dev_open,
2130 .release = sfe_ipv4_debug_dev_release
2131};
2132
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08002133#ifdef CONFIG_NF_FLOW_COOKIE
2134/*
2135 * sfe_register_flow_cookie_cb
2136 * register a function in SFE to let SFE use this function to configure flow cookie for a flow
2137 *
2138 * Hardware driver which support flow cookie should register a callback function in SFE. Then SFE
2139 * can use this function to configure flow cookie for a flow.
2140 * return: 0, success; !=0, fail
2141 */
2142int sfe_register_flow_cookie_cb(flow_cookie_set_func_t cb)
2143{
2144 struct sfe_ipv4 *si = &__si;
2145
2146 BUG_ON(!cb);
2147
2148 if (si->flow_cookie_set_func) {
2149 return -1;
2150 }
2151
2152 rcu_assign_pointer(si->flow_cookie_set_func, cb);
2153 return 0;
2154}
2155
2156/*
2157 * sfe_unregister_flow_cookie_cb
2158 * unregister function which is used to configure flow cookie for a flow
2159 *
2160 * return: 0, success; !=0, fail
2161 */
2162int sfe_unregister_flow_cookie_cb(flow_cookie_set_func_t cb)
2163{
2164 struct sfe_ipv4 *si = &__si;
2165
2166 RCU_INIT_POINTER(si->flow_cookie_set_func, NULL);
2167 return 0;
2168}
Xiaoping Fan640faf42015-08-28 15:50:55 -07002169
2170/*
2171 * sfe_ipv4_get_flow_cookie()
2172 */
2173static ssize_t sfe_ipv4_get_flow_cookie(struct device *dev,
2174 struct device_attribute *attr,
2175 char *buf)
2176{
2177 struct sfe_ipv4 *si = &__si;
Xiaoping Fan01c67cc2015-11-09 11:31:57 -08002178 return snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", si->flow_cookie_enable);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002179}
2180
2181/*
2182 * sfe_ipv4_set_flow_cookie()
2183 */
2184static ssize_t sfe_ipv4_set_flow_cookie(struct device *dev,
2185 struct device_attribute *attr,
2186 const char *buf, size_t size)
2187{
2188 struct sfe_ipv4 *si = &__si;
Ken Zhu137722d2021-09-23 17:57:36 -07002189 si->flow_cookie_enable = simple_strtol(buf, NULL, 0);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002190
2191 return size;
2192}
2193
2194/*
2195 * sysfs attributes.
2196 */
2197static const struct device_attribute sfe_ipv4_flow_cookie_attr =
Xiaoping Fane70da412016-02-26 16:47:57 -08002198 __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 -08002199#endif /*CONFIG_NF_FLOW_COOKIE*/
2200
Ken Zhu137722d2021-09-23 17:57:36 -07002201/*
2202 * sfe_ipv4_get_cpu()
2203 */
2204static ssize_t sfe_ipv4_get_cpu(struct device *dev,
2205 struct device_attribute *attr,
2206 char *buf)
2207{
2208 struct sfe_ipv4 *si = &__si;
2209 return snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", si->work_cpu);
2210}
2211
2212/*
2213 * sfe_ipv4_set_cpu()
2214 */
2215static ssize_t sfe_ipv4_set_cpu(struct device *dev,
2216 struct device_attribute *attr,
2217 const char *buf, size_t size)
2218{
2219 struct sfe_ipv4 *si = &__si;
2220 int work_cpu;
2221 work_cpu = simple_strtol(buf, NULL, 0);
2222 if ((work_cpu >= 0) && (work_cpu <= NR_CPUS)) {
2223 si->work_cpu = work_cpu;
2224 } else {
2225 dev_err(dev, "%s is not in valid range[0,%d]", buf, NR_CPUS);
2226 }
2227 return size;
2228}
2229/*
2230 * sysfs attributes.
2231 */
2232static const struct device_attribute sfe_ipv4_cpu_attr =
2233 __ATTR(stats_work_cpu, S_IWUSR | S_IRUGO, sfe_ipv4_get_cpu, sfe_ipv4_set_cpu);
2234
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05302235 /*
2236 * sfe_ipv4_conn_match_hash_init()
2237 * Initialize conn match hash lists
2238 */
2239static void sfe_ipv4_conn_match_hash_init(struct sfe_ipv4 *si, int len)
2240{
2241 struct hlist_head *hash_list = si->hlist_conn_match_hash_head;
2242 int i;
2243
2244 for (i = 0; i < len; i++) {
2245 INIT_HLIST_HEAD(&hash_list[i]);
2246 }
2247}
2248
Amitesh Anand63be37d2021-12-24 20:51:48 +05302249#ifdef SFE_PROCESS_LOCAL_OUT
2250/*
2251 * sfe_ipv4_local_out()
2252 * Called for packets from ip_local_out() - post encapsulation & other packets
2253 */
2254static unsigned int sfe_ipv4_local_out(void *priv, struct sk_buff *skb, const struct nf_hook_state *nhs)
2255{
2256 DEBUG_TRACE("%px: sfe: sfe_ipv4_local_out hook called.\n", skb);
2257
2258 if (likely(skb->skb_iif)) {
2259 return sfe_ipv4_recv(skb->dev, skb, NULL, true) ? NF_STOLEN : NF_ACCEPT;
2260 }
2261
2262 return NF_ACCEPT;
2263}
2264
2265/*
2266 * struct nf_hook_ops sfe_ipv4_ops_local_out[]
2267 * Hooks into netfilter local out packet monitoring points.
2268 */
2269static struct nf_hook_ops sfe_ipv4_ops_local_out[] __read_mostly = {
2270
2271 /*
2272 * Local out routing hook is used to monitor packets.
2273 */
2274 {
2275 .hook = sfe_ipv4_local_out,
2276 .pf = PF_INET,
2277 .hooknum = NF_INET_LOCAL_OUT,
2278 .priority = NF_IP_PRI_FIRST,
2279 },
2280};
2281#endif
2282
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002283/*
Dave Hudson87973cd2013-10-22 16:00:04 +01002284 * sfe_ipv4_init()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002285 */
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05302286int sfe_ipv4_init(void)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002287{
2288 struct sfe_ipv4 *si = &__si;
2289 int result = -1;
2290
Dave Hudsondcd08fb2013-11-22 09:25:16 -06002291 DEBUG_INFO("SFE IPv4 init\n");
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002292
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05302293 sfe_ipv4_conn_match_hash_init(si, ARRAY_SIZE(si->hlist_conn_match_hash_head));
2294
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302295 si->stats_pcpu = alloc_percpu_gfp(struct sfe_ipv4_stats, GFP_KERNEL | __GFP_ZERO);
2296 if (!si->stats_pcpu) {
2297 DEBUG_ERROR("failed to allocate stats memory for sfe_ipv4\n");
2298 goto exit0;
2299 }
2300
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002301 /*
2302 * Create sys/sfe_ipv4
2303 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302304 si->sys_ipv4 = kobject_create_and_add("sfe_ipv4", NULL);
2305 if (!si->sys_ipv4) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002306 DEBUG_ERROR("failed to register sfe_ipv4\n");
2307 goto exit1;
2308 }
2309
2310 /*
2311 * Create files, one for each parameter supported by this module.
2312 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302313 result = sysfs_create_file(si->sys_ipv4, &sfe_ipv4_debug_dev_attr.attr);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002314 if (result) {
2315 DEBUG_ERROR("failed to register debug dev file: %d\n", result);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002316 goto exit2;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002317 }
2318
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302319 result = sysfs_create_file(si->sys_ipv4, &sfe_ipv4_cpu_attr.attr);
Ken Zhu137722d2021-09-23 17:57:36 -07002320 if (result) {
2321 DEBUG_ERROR("failed to register debug dev file: %d\n", result);
2322 goto exit3;
2323 }
2324
Xiaoping Fan640faf42015-08-28 15:50:55 -07002325#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302326 result = sysfs_create_file(si->sys_ipv4, &sfe_ipv4_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002327 if (result) {
2328 DEBUG_ERROR("failed to register flow cookie enable file: %d\n", result);
Ken Zhu137722d2021-09-23 17:57:36 -07002329 goto exit4;
Xiaoping Fan640faf42015-08-28 15:50:55 -07002330 }
2331#endif /* CONFIG_NF_FLOW_COOKIE */
2332
Amitesh Anand63be37d2021-12-24 20:51:48 +05302333#ifdef SFE_PROCESS_LOCAL_OUT
2334#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
2335 result = nf_register_hooks(sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2336#else
2337 result = nf_register_net_hooks(&init_net, sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2338#endif
2339 if (result < 0) {
2340 DEBUG_ERROR("can't register nf local out hook: %d\n", result);
2341 goto exit5;
2342 }
2343 DEBUG_INFO("Register nf local out hook success: %d\n", result);
2344#endif
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002345 /*
2346 * Register our debug char device.
2347 */
2348 result = register_chrdev(0, "sfe_ipv4", &sfe_ipv4_debug_dev_fops);
2349 if (result < 0) {
2350 DEBUG_ERROR("Failed to register chrdev: %d\n", result);
Amitesh Anand63be37d2021-12-24 20:51:48 +05302351 goto exit6;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002352 }
2353
2354 si->debug_dev = result;
Ken Zhu137722d2021-09-23 17:57:36 -07002355 si->work_cpu = WORK_CPU_UNBOUND;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002356
2357 /*
Ken Zhu137722d2021-09-23 17:57:36 -07002358 * Create a work to handle periodic statistics.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002359 */
Ken Zhu137722d2021-09-23 17:57:36 -07002360 INIT_DELAYED_WORK(&(si->sync_dwork), sfe_ipv4_periodic_sync);
2361 schedule_delayed_work_on(si->work_cpu, &(si->sync_dwork), ((HZ + 99) / 100));
2362
Dave Hudson87973cd2013-10-22 16:00:04 +01002363 spin_lock_init(&si->lock);
Dave Hudson87973cd2013-10-22 16:00:04 +01002364 return 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002365
Amitesh Anand63be37d2021-12-24 20:51:48 +05302366exit6:
2367#ifdef SFE_PROCESS_LOCAL_OUT
2368 DEBUG_TRACE("sfe: Unregister local out hook\n");
2369#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
2370 nf_unregister_hooks(sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2371#else
2372 nf_unregister_net_hooks(&init_net, sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2373#endif
Ken Zhu137722d2021-09-23 17:57:36 -07002374exit5:
Amitesh Anand63be37d2021-12-24 20:51:48 +05302375#endif
Xiaoping Fan640faf42015-08-28 15:50:55 -07002376#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302377 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002378
Ken Zhu137722d2021-09-23 17:57:36 -07002379exit4:
Xiaoping Fan640faf42015-08-28 15:50:55 -07002380#endif /* CONFIG_NF_FLOW_COOKIE */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302381 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_cpu_attr.attr);
Ken Zhu137722d2021-09-23 17:57:36 -07002382exit3:
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302383 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_debug_dev_attr.attr);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002384
Xiaoping Fan640faf42015-08-28 15:50:55 -07002385exit2:
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302386 kobject_put(si->sys_ipv4);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002387
2388exit1:
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302389 free_percpu(si->stats_pcpu);
2390
2391exit0:
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002392 return result;
2393}
2394
2395/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002396 * sfe_ipv4_exit()
2397 */
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05302398void sfe_ipv4_exit(void)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002399{
Dave Hudson87973cd2013-10-22 16:00:04 +01002400 struct sfe_ipv4 *si = &__si;
2401
Dave Hudsondcd08fb2013-11-22 09:25:16 -06002402 DEBUG_INFO("SFE IPv4 exit\n");
Dave Hudson87973cd2013-10-22 16:00:04 +01002403 /*
2404 * Destroy all connections.
2405 */
Dave Hudsondcd08fb2013-11-22 09:25:16 -06002406 sfe_ipv4_destroy_all_rules_for_dev(NULL);
Dave Hudson87973cd2013-10-22 16:00:04 +01002407
Ken Zhu137722d2021-09-23 17:57:36 -07002408 cancel_delayed_work_sync(&si->sync_dwork);
Dave Hudson87973cd2013-10-22 16:00:04 +01002409
Dave Hudson87973cd2013-10-22 16:00:04 +01002410 unregister_chrdev(si->debug_dev, "sfe_ipv4");
2411
Amitesh Anand63be37d2021-12-24 20:51:48 +05302412#ifdef SFE_PROCESS_LOCAL_OUT
2413 DEBUG_TRACE("sfe: Unregister local out hook\n");
2414#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
2415 nf_unregister_hooks(sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2416#else
2417 nf_unregister_net_hooks(&init_net, sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2418#endif
2419#endif
2420
Xiaoping Fan640faf42015-08-28 15:50:55 -07002421#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302422 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002423#endif /* CONFIG_NF_FLOW_COOKIE */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302424 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_debug_dev_attr.attr);
2425 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_cpu_attr.attr);
Dave Hudson87973cd2013-10-22 16:00:04 +01002426
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302427 kobject_put(si->sys_ipv4);
Dave Hudson87973cd2013-10-22 16:00:04 +01002428
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302429 free_percpu(si->stats_pcpu);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002430}
2431
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08002432#ifdef CONFIG_NF_FLOW_COOKIE
2433EXPORT_SYMBOL(sfe_register_flow_cookie_cb);
2434EXPORT_SYMBOL(sfe_unregister_flow_cookie_cb);
2435#endif