blob: 815f494c8d2f60dbfb4d17f248569e93bfe90c29 [file] [log] [blame]
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001/*
2 * sfe_ipv4.c
3 * Shortcut forwarding engine - IPv4 edition.
4 *
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05305 * Copyright (c) 2013-2016, 2019-2020, The Linux Foundation. All rights reserved.
Guduri Prathyusha5f27e232022-01-06 14:39:04 +05306 * Copyright (c) 2021-2022 Qualcomm Innovation Center, Inc. All rights reserved.
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05307 *
8 * Permission to use, copy, modify, and/or distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
11 *
Xiaoping Fana42c68b2015-08-07 18:00:39 -070012 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +053017 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010019 */
Matthew McClintocka3221942014-01-16 11:44:26 -060020
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010021#include <linux/module.h>
Dave Hudsondcd08fb2013-11-22 09:25:16 -060022#include <linux/sysfs.h>
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010023#include <linux/skbuff.h>
24#include <linux/icmp.h>
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010025#include <net/tcp.h>
Amitesh Anand63be37d2021-12-24 20:51:48 +053026#include <net/udp.h>
27#include <net/vxlan.h>
Dave Hudsondcd08fb2013-11-22 09:25:16 -060028#include <linux/etherdevice.h>
Tian Yang45f39c82020-10-06 14:07:47 -070029#include <linux/version.h>
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +053030#include <linux/lockdep.h>
Amitesh Anand63be37d2021-12-24 20:51:48 +053031#include <linux/refcount.h>
32#include <linux/netfilter.h>
33#include <linux/inetdevice.h>
34#include <linux/netfilter_ipv4.h>
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/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530447 * sfe_ipv4_insert_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100448 * Insert a connection into the hash.
449 *
450 * On entry we must be holding the lock that protects the hash table.
451 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530452static void sfe_ipv4_insert_connection(struct sfe_ipv4 *si, struct sfe_ipv4_connection *c)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100453{
454 struct sfe_ipv4_connection **hash_head;
455 struct sfe_ipv4_connection *prev_head;
456 unsigned int conn_idx;
457
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530458 lockdep_assert_held(&si->lock);
459
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100460 /*
461 * Insert entry into the connection hash.
462 */
463 conn_idx = sfe_ipv4_get_connection_hash(c->protocol, c->src_ip, c->src_port,
464 c->dest_ip, c->dest_port);
465 hash_head = &si->conn_hash[conn_idx];
466 prev_head = *hash_head;
467 c->prev = NULL;
468 if (prev_head) {
469 prev_head->prev = c;
470 }
471
472 c->next = prev_head;
473 *hash_head = c;
474
475 /*
476 * Insert entry into the "all connections" list.
477 */
478 if (si->all_connections_tail) {
479 c->all_connections_prev = si->all_connections_tail;
480 si->all_connections_tail->all_connections_next = c;
481 } else {
482 c->all_connections_prev = NULL;
483 si->all_connections_head = c;
484 }
485
486 si->all_connections_tail = c;
487 c->all_connections_next = NULL;
488 si->num_connections++;
489
490 /*
491 * Insert the connection match objects too.
492 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530493 sfe_ipv4_insert_connection_match(si, c->original_match);
494 sfe_ipv4_insert_connection_match(si, c->reply_match);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100495}
496
497/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530498 * sfe_ipv4_remove_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100499 * Remove a sfe_ipv4_connection object from the hash.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100500 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530501bool sfe_ipv4_remove_connection(struct sfe_ipv4 *si, struct sfe_ipv4_connection *c)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100502{
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530503 lockdep_assert_held(&si->lock);
504
505 if (c->removed) {
506 DEBUG_ERROR("%px: Connection has been removed already\n", c);
507 return false;
508 }
509
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100510 /*
511 * Remove the connection match objects.
512 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530513 sfe_ipv4_remove_connection_match(si, c->reply_match);
514 sfe_ipv4_remove_connection_match(si, c->original_match);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100515
516 /*
517 * Unlink the connection.
518 */
519 if (c->prev) {
520 c->prev->next = c->next;
521 } else {
522 unsigned int conn_idx = sfe_ipv4_get_connection_hash(c->protocol, c->src_ip, c->src_port,
523 c->dest_ip, c->dest_port);
524 si->conn_hash[conn_idx] = c->next;
525 }
526
527 if (c->next) {
528 c->next->prev = c->prev;
529 }
Xiaoping Fan34586472015-07-03 02:20:35 -0700530
531 /*
532 * Unlink connection from all_connections list
533 */
534 if (c->all_connections_prev) {
535 c->all_connections_prev->all_connections_next = c->all_connections_next;
536 } else {
537 si->all_connections_head = c->all_connections_next;
538 }
539
540 if (c->all_connections_next) {
541 c->all_connections_next->all_connections_prev = c->all_connections_prev;
542 } else {
543 si->all_connections_tail = c->all_connections_prev;
544 }
545
Ken Zhudc423672021-09-02 18:27:01 -0700546 /*
547 * If I am the next sync connection, move the sync to my next or head.
548 */
549 if (unlikely(si->wc_next == c)) {
550 si->wc_next = c->all_connections_next;
551 }
552
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530553 c->removed = true;
Xiaoping Fan34586472015-07-03 02:20:35 -0700554 si->num_connections--;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530555 return true;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100556}
557
558/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530559 * sfe_ipv4_gen_sync_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100560 * Sync a connection.
561 *
562 * On entry to this function we expect that the lock for the connection is either
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530563 * already held (while called from sfe_ipv4_periodic_sync() or isn't required
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530564 * (while called from sfe_ipv4_flush_connection())
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100565 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530566static void sfe_ipv4_gen_sync_connection(struct sfe_ipv4 *si, struct sfe_ipv4_connection *c,
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700567 struct sfe_connection_sync *sis, sfe_sync_reason_t reason,
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700568 u64 now_jiffies)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100569{
570 struct sfe_ipv4_connection_match *original_cm;
571 struct sfe_ipv4_connection_match *reply_cm;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530572 u32 packet_count, byte_count;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100573
574 /*
575 * Fill in the update message.
576 */
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700577 sis->is_v6 = 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100578 sis->protocol = c->protocol;
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700579 sis->src_ip.ip = c->src_ip;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700580 sis->src_ip_xlate.ip = c->src_ip_xlate;
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700581 sis->dest_ip.ip = c->dest_ip;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700582 sis->dest_ip_xlate.ip = c->dest_ip_xlate;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100583 sis->src_port = c->src_port;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700584 sis->src_port_xlate = c->src_port_xlate;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100585 sis->dest_port = c->dest_port;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700586 sis->dest_port_xlate = c->dest_port_xlate;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100587
588 original_cm = c->original_match;
589 reply_cm = c->reply_match;
590 sis->src_td_max_window = original_cm->protocol_state.tcp.max_win;
591 sis->src_td_end = original_cm->protocol_state.tcp.end;
592 sis->src_td_max_end = original_cm->protocol_state.tcp.max_end;
593 sis->dest_td_max_window = reply_cm->protocol_state.tcp.max_win;
594 sis->dest_td_end = reply_cm->protocol_state.tcp.end;
595 sis->dest_td_max_end = reply_cm->protocol_state.tcp.max_end;
596
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530597 sfe_ipv4_connection_match_update_summary_stats(original_cm, &packet_count, &byte_count);
598 sis->src_new_packet_count = packet_count;
599 sis->src_new_byte_count = byte_count;
Matthew McClintockd0cdb802014-02-24 16:30:35 -0600600
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530601 sfe_ipv4_connection_match_update_summary_stats(reply_cm, &packet_count, &byte_count);
602 sis->dest_new_packet_count = packet_count;
603 sis->dest_new_byte_count = byte_count;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100604
Matthew McClintockd0cdb802014-02-24 16:30:35 -0600605 sis->src_dev = original_cm->match_dev;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100606 sis->src_packet_count = original_cm->rx_packet_count64;
607 sis->src_byte_count = original_cm->rx_byte_count64;
Matthew McClintockd0cdb802014-02-24 16:30:35 -0600608
609 sis->dest_dev = reply_cm->match_dev;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100610 sis->dest_packet_count = reply_cm->rx_packet_count64;
611 sis->dest_byte_count = reply_cm->rx_byte_count64;
612
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700613 sis->reason = reason;
614
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100615 /*
616 * Get the time increment since our last sync.
617 */
618 sis->delta_jiffies = now_jiffies - c->last_sync_jiffies;
619 c->last_sync_jiffies = now_jiffies;
620}
621
622/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530623 * sfe_ipv4_free_connection_rcu()
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530624 * Called at RCU qs state to free the connection object.
625 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530626static void sfe_ipv4_free_connection_rcu(struct rcu_head *head)
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530627{
628 struct sfe_ipv4_connection *c;
Amitesh Anand63be37d2021-12-24 20:51:48 +0530629 struct udp_sock *up;
630 struct sock *sk;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530631
632 /*
633 * We dont need spin lock as the connection is already removed from link list
634 */
635 c = container_of(head, struct sfe_ipv4_connection, rcu);
636
637 BUG_ON(!c->removed);
638
639 DEBUG_TRACE("%px: connecton has been deleted\n", c);
640
641 /*
Amitesh Anand63be37d2021-12-24 20:51:48 +0530642 * Decrease the refcount taken in function sfe_ipv4_create_rule(),
643 * during call of __udp4_lib_lookup()
644 */
645 up = c->reply_match->up;
646 if (up) {
647 sk = (struct sock *)up;
648 sock_put(sk);
649 }
650
651 /*
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530652 * Release our hold of the source and dest devices and free the memory
653 * for our connection objects.
654 */
655 dev_put(c->original_dev);
656 dev_put(c->reply_dev);
657 kfree(c->original_match);
658 kfree(c->reply_match);
659 kfree(c);
660}
661
662/*
Ken Zhu88c58152021-12-09 15:12:06 -0800663 * sfe_ipv4_sync_status()
664 * update a connection status to its connection manager.
665 *
666 * si: the ipv4 context
667 * c: which connection to be notified
668 * reason: what kind of notification: flush, stats or destroy
669 */
670void sfe_ipv4_sync_status(struct sfe_ipv4 *si,
671 struct sfe_ipv4_connection *c,
672 sfe_sync_reason_t reason)
673{
674 struct sfe_connection_sync sis;
675 u64 now_jiffies;
676 sfe_sync_rule_callback_t sync_rule_callback;
677
678 rcu_read_lock();
679 sync_rule_callback = rcu_dereference(si->sync_rule_callback);
680 if (!sync_rule_callback) {
681 rcu_read_unlock();
682 return;
683 }
684
685 /*
686 * Generate a sync message and then sync.
687 */
688 now_jiffies = get_jiffies_64();
689 sfe_ipv4_gen_sync_connection(si, c, &sis, reason, now_jiffies);
690 sync_rule_callback(&sis);
691
692 rcu_read_unlock();
693}
694
695/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530696 * sfe_ipv4_flush_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100697 * Flush a connection and free all associated resources.
698 *
699 * We need to be called with bottom halves disabled locally as we need to acquire
700 * the connection hash lock and release it again. In general we're actually called
701 * from within a BH and so we're fine, but we're also called when connections are
702 * torn down.
703 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530704void sfe_ipv4_flush_connection(struct sfe_ipv4 *si,
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700705 struct sfe_ipv4_connection *c,
706 sfe_sync_reason_t reason)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100707{
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530708 BUG_ON(!c->removed);
709
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530710 this_cpu_inc(si->stats_pcpu->connection_flushes64);
Ken Zhu88c58152021-12-09 15:12:06 -0800711 sfe_ipv4_sync_status(si, c, reason);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100712
713 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100714 * Release our hold of the source and dest devices and free the memory
715 * for our connection objects.
716 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530717 call_rcu(&c->rcu, sfe_ipv4_free_connection_rcu);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100718}
719
720/*
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530721 * sfe_ipv4_exception_stats_inc()
722 * Increment exception stats.
723 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530724void sfe_ipv4_exception_stats_inc(struct sfe_ipv4 *si, enum sfe_ipv4_exception_events reason)
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530725{
726 struct sfe_ipv4_stats *stats = this_cpu_ptr(si->stats_pcpu);
727 stats->exception_events64[reason]++;
728 stats->packets_not_forwarded64++;
729}
730
731/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100732 * sfe_ipv4_recv()
Matthew McClintocka8ad7962014-01-16 16:49:30 -0600733 * Handle packet receives and forwaring.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100734 *
735 * Returns 1 if the packet is forwarded or 0 if it isn't.
736 */
Amitesh Anand63be37d2021-12-24 20:51:48 +0530737int 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 +0100738{
739 struct sfe_ipv4 *si = &__si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100740 unsigned int len;
741 unsigned int tot_len;
742 unsigned int frag_off;
743 unsigned int ihl;
Ken Zhu88c58152021-12-09 15:12:06 -0800744 bool sync_on_find;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100745 bool ip_options;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530746 struct iphdr *iph;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700747 u32 protocol;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100748
749 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100750 * Check that we have space for an IP header here.
751 */
752 len = skb->len;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530753 if (unlikely(!pskb_may_pull(skb, sizeof(struct iphdr)))) {
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530754 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_HEADER_INCOMPLETE);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100755 DEBUG_TRACE("len: %u is too short\n", len);
756 return 0;
757 }
758
759 /*
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +0530760 * Validate ip csum if necessary. If ip_summed is set to CHECKSUM_UNNECESSARY, it is assumed
761 * that the L3 checksum is validated by the Rx interface or the tunnel interface that has
762 * generated the packet.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100763 */
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530764 iph = (struct iphdr *)skb->data;
Ratheesh Kannoth43d64f82021-10-20 08:23:29 +0530765 if (unlikely(skb->ip_summed != CHECKSUM_UNNECESSARY) && (ip_fast_csum((u8 *)iph, iph->ihl))) {
766 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_HEADER_CSUM_BAD);
767
768 DEBUG_TRACE("Bad IPv4 header csum: 0x%x\n", iph->check);
769 return 0;
770 }
771
772 /*
773 * Check that our "total length" is large enough for an IP header.
774 */
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100775 tot_len = ntohs(iph->tot_len);
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530776 if (unlikely(tot_len < sizeof(struct iphdr))) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100777
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530778 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_BAD_TOTAL_LENGTH);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100779 DEBUG_TRACE("tot_len: %u is too short\n", tot_len);
780 return 0;
781 }
782
783 /*
784 * Is our IP version wrong?
785 */
786 if (unlikely(iph->version != 4)) {
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530787 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_NON_V4);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100788 DEBUG_TRACE("IP version: %u\n", iph->version);
789 return 0;
790 }
791
792 /*
793 * Does our datagram fit inside the skb?
794 */
795 if (unlikely(tot_len > len)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100796 DEBUG_TRACE("tot_len: %u, exceeds len: %u\n", tot_len, len);
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530797 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_DATAGRAM_INCOMPLETE);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100798 return 0;
799 }
800
801 /*
802 * Do we have a non-initial fragment?
Nicolas Costaac2979c2014-01-14 10:35:24 -0600803 */
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100804 frag_off = ntohs(iph->frag_off);
805 if (unlikely(frag_off & IP_OFFSET)) {
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530806 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_NON_INITIAL_FRAGMENT);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100807 DEBUG_TRACE("non-initial fragment\n");
808 return 0;
809 }
810
811 /*
812 * If we have a (first) fragment then mark it to cause any connection to flush.
813 */
Ken Zhu88c58152021-12-09 15:12:06 -0800814 sync_on_find = unlikely(frag_off & IP_MF) ? true : false;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100815
816 /*
817 * Do we have any IP options? That's definite a slow path! If we do have IP
818 * options we need to recheck our header size.
819 */
820 ihl = iph->ihl << 2;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530821 ip_options = unlikely(ihl != sizeof(struct iphdr)) ? true : false;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100822 if (unlikely(ip_options)) {
823 if (unlikely(len < ihl)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100824
825 DEBUG_TRACE("len: %u is too short for header of size: %u\n", len, ihl);
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530826 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_IP_OPTIONS_INCOMPLETE);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100827 return 0;
828 }
829
Ken Zhu88c58152021-12-09 15:12:06 -0800830 sync_on_find = true;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100831 }
832
833 protocol = iph->protocol;
834 if (IPPROTO_UDP == protocol) {
Ken Zhu88c58152021-12-09 15:12:06 -0800835 return sfe_ipv4_recv_udp(si, skb, dev, len, iph, ihl, sync_on_find, l2_info, tun_outer);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100836 }
837
838 if (IPPROTO_TCP == protocol) {
Ken Zhu88c58152021-12-09 15:12:06 -0800839 return sfe_ipv4_recv_tcp(si, skb, dev, len, iph, ihl, sync_on_find, l2_info);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100840 }
841
842 if (IPPROTO_ICMP == protocol) {
843 return sfe_ipv4_recv_icmp(si, skb, dev, len, iph, ihl);
844 }
845
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530846 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_UNHANDLED_PROTOCOL);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100847
848 DEBUG_TRACE("not UDP, TCP or ICMP: %u\n", protocol);
849 return 0;
850}
851
Nicolas Costa436926b2014-01-14 10:36:22 -0600852static void
853sfe_ipv4_update_tcp_state(struct sfe_ipv4_connection *c,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530854 struct sfe_ipv4_rule_create_msg *msg)
Nicolas Costa436926b2014-01-14 10:36:22 -0600855{
856 struct sfe_ipv4_connection_match *orig_cm;
857 struct sfe_ipv4_connection_match *repl_cm;
858 struct sfe_ipv4_tcp_connection_match *orig_tcp;
859 struct sfe_ipv4_tcp_connection_match *repl_tcp;
860
861 orig_cm = c->original_match;
862 repl_cm = c->reply_match;
863 orig_tcp = &orig_cm->protocol_state.tcp;
864 repl_tcp = &repl_cm->protocol_state.tcp;
865
866 /* update orig */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530867 if (orig_tcp->max_win < msg->tcp_rule.flow_max_window) {
868 orig_tcp->max_win = msg->tcp_rule.flow_max_window;
Nicolas Costa436926b2014-01-14 10:36:22 -0600869 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530870 if ((s32)(orig_tcp->end - msg->tcp_rule.flow_end) < 0) {
871 orig_tcp->end = msg->tcp_rule.flow_end;
Nicolas Costa436926b2014-01-14 10:36:22 -0600872 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530873 if ((s32)(orig_tcp->max_end - msg->tcp_rule.flow_max_end) < 0) {
874 orig_tcp->max_end = msg->tcp_rule.flow_max_end;
Nicolas Costa436926b2014-01-14 10:36:22 -0600875 }
876
877 /* update reply */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530878 if (repl_tcp->max_win < msg->tcp_rule.return_max_window) {
879 repl_tcp->max_win = msg->tcp_rule.return_max_window;
Nicolas Costa436926b2014-01-14 10:36:22 -0600880 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530881 if ((s32)(repl_tcp->end - msg->tcp_rule.return_end) < 0) {
882 repl_tcp->end = msg->tcp_rule.return_end;
Nicolas Costa436926b2014-01-14 10:36:22 -0600883 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530884 if ((s32)(repl_tcp->max_end - msg->tcp_rule.return_max_end) < 0) {
885 repl_tcp->max_end = msg->tcp_rule.return_max_end;
Nicolas Costa436926b2014-01-14 10:36:22 -0600886 }
887
888 /* update match flags */
889 orig_cm->flags &= ~SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
890 repl_cm->flags &= ~SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530891 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_NO_SEQ_CHECK) {
892
Nicolas Costa436926b2014-01-14 10:36:22 -0600893 orig_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
894 repl_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
895 }
896}
897
898static void
899sfe_ipv4_update_protocol_state(struct sfe_ipv4_connection *c,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530900 struct sfe_ipv4_rule_create_msg *msg)
Nicolas Costa436926b2014-01-14 10:36:22 -0600901{
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530902 switch (msg->tuple.protocol) {
Nicolas Costa436926b2014-01-14 10:36:22 -0600903 case IPPROTO_TCP:
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530904 sfe_ipv4_update_tcp_state(c, msg);
Nicolas Costa436926b2014-01-14 10:36:22 -0600905 break;
906 }
907}
908
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530909void sfe_ipv4_update_rule(struct sfe_ipv4_rule_create_msg *msg)
Nicolas Costa436926b2014-01-14 10:36:22 -0600910{
911 struct sfe_ipv4_connection *c;
912 struct sfe_ipv4 *si = &__si;
913
914 spin_lock_bh(&si->lock);
915
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530916 c = sfe_ipv4_find_connection(si,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530917 msg->tuple.protocol,
918 msg->tuple.flow_ip,
919 msg->tuple.flow_ident,
920 msg->tuple.return_ip,
921 msg->tuple.return_ident);
Nicolas Costa436926b2014-01-14 10:36:22 -0600922 if (c != NULL) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530923 sfe_ipv4_update_protocol_state(c, msg);
Nicolas Costa436926b2014-01-14 10:36:22 -0600924 }
925
926 spin_unlock_bh(&si->lock);
927}
928
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100929/*
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +0530930 * sfe_ipv4_xmit_eth_type_check()
931 * Checking if MAC header has to be written.
932 */
933static inline bool sfe_ipv4_xmit_eth_type_check(struct net_device *dev, u32 cm_flags)
934{
935 if (!(dev->flags & IFF_NOARP)) {
936 return true;
937 }
938
939 /*
940 * For PPPoE, since we are now supporting PPPoE encapsulation, we are writing L2 header.
941 */
942 if (unlikely(cm_flags & SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_ENCAP)) {
943 return true;
944 }
945
946 return false;
947}
948
949/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100950 * sfe_ipv4_create_rule()
951 * Create a forwarding rule.
952 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530953int sfe_ipv4_create_rule(struct sfe_ipv4_rule_create_msg *msg)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100954{
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600955 struct sfe_ipv4 *si = &__si;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530956 struct sfe_ipv4_connection *c, *c_old;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100957 struct sfe_ipv4_connection_match *original_cm;
958 struct sfe_ipv4_connection_match *reply_cm;
Matthew McClintockdb5ac512014-01-16 17:01:40 -0600959 struct net_device *dest_dev;
960 struct net_device *src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530961 struct sfe_ipv4_5tuple *tuple = &msg->tuple;
Suruchi Sumanc1a4a612021-10-21 14:50:23 +0530962 s32 flow_interface_num = msg->conn_rule.flow_top_interface_num;
963 s32 return_interface_num = msg->conn_rule.return_top_interface_num;
Amitesh Anand63be37d2021-12-24 20:51:48 +0530964 struct net *net;
965 struct sock *sk;
966 unsigned int src_if_idx;
Matthew McClintockdb5ac512014-01-16 17:01:40 -0600967
Suruchi Sumanc1a4a612021-10-21 14:50:23 +0530968 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_FLOW_BOTTOM_INTERFACE) {
969 flow_interface_num = msg->conn_rule.flow_interface_num;
970 }
971
972 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_RETURN_BOTTOM_INTERFACE) {
973 return_interface_num = msg->conn_rule.return_interface_num;
974 }
975
976 src_dev = dev_get_by_index(&init_net, flow_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530977 if (!src_dev) {
978 DEBUG_WARN("%px: Unable to find src_dev corresponding to %d\n", msg,
Suruchi Sumanc1a4a612021-10-21 14:50:23 +0530979 flow_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530980 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
981 return -EINVAL;
982 }
983
Suruchi Sumanc1a4a612021-10-21 14:50:23 +0530984 dest_dev = dev_get_by_index(&init_net, return_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530985 if (!dest_dev) {
986 DEBUG_WARN("%px: Unable to find dest_dev corresponding to %d\n", msg,
Suruchi Sumanc1a4a612021-10-21 14:50:23 +0530987 return_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530988 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
989 dev_put(src_dev);
990 return -EINVAL;
991 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100992
Matthew McClintock389b42a2014-09-24 14:05:51 -0500993 if (unlikely((dest_dev->reg_state != NETREG_REGISTERED) ||
994 (src_dev->reg_state != NETREG_REGISTERED))) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530995 dev_put(src_dev);
996 dev_put(dest_dev);
997 DEBUG_WARN("%px: src_dev=%s and dest_dev=%s are unregistered\n", msg,
998 src_dev->name, dest_dev->name);
999 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
Matthew McClintock389b42a2014-09-24 14:05:51 -05001000 return -EINVAL;
1001 }
1002
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301003 /*
1004 * Allocate the various connection tracking objects.
1005 */
1006 c = (struct sfe_ipv4_connection *)kmalloc(sizeof(struct sfe_ipv4_connection), GFP_ATOMIC);
1007 if (unlikely(!c)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301008 DEBUG_WARN("%px: memory allocation of connection entry failed\n", msg);
1009 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
1010 dev_put(src_dev);
1011 dev_put(dest_dev);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301012 return -ENOMEM;
1013 }
1014
1015 original_cm = (struct sfe_ipv4_connection_match *)kmalloc(sizeof(struct sfe_ipv4_connection_match), GFP_ATOMIC);
1016 if (unlikely(!original_cm)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301017 DEBUG_WARN("%px: memory allocation of connection match entry failed\n", msg);
1018 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301019 kfree(c);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301020 dev_put(src_dev);
1021 dev_put(dest_dev);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301022 return -ENOMEM;
1023 }
1024
1025 reply_cm = (struct sfe_ipv4_connection_match *)kmalloc(sizeof(struct sfe_ipv4_connection_match), GFP_ATOMIC);
1026 if (unlikely(!reply_cm)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301027 DEBUG_WARN("%px: memory allocation of connection match entry failed\n", msg);
1028 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301029 kfree(original_cm);
1030 kfree(c);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301031 dev_put(src_dev);
1032 dev_put(dest_dev);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301033 return -ENOMEM;
1034 }
1035
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301036 this_cpu_inc(si->stats_pcpu->connection_create_requests64);
1037
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001038 spin_lock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001039
1040 /*
Nicolas Costa436926b2014-01-14 10:36:22 -06001041 * Check to see if there is already a flow that matches the rule we're
1042 * trying to create. If there is then we can't create a new one.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001043 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301044 c_old = sfe_ipv4_find_connection(si,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301045 msg->tuple.protocol,
1046 msg->tuple.flow_ip,
1047 msg->tuple.flow_ident,
1048 msg->tuple.return_ip,
1049 msg->tuple.return_ident);
1050
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301051 if (c_old != NULL) {
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301052 this_cpu_inc(si->stats_pcpu->connection_create_collisions64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001053
1054 /*
Nicolas Costa436926b2014-01-14 10:36:22 -06001055 * If we already have the flow then it's likely that this
1056 * request to create the connection rule contains more
1057 * up-to-date information. Check and update accordingly.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001058 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301059 sfe_ipv4_update_protocol_state(c, msg);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001060 spin_unlock_bh(&si->lock);
1061
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301062 kfree(reply_cm);
1063 kfree(original_cm);
1064 kfree(c);
1065
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301066 dev_put(src_dev);
1067 dev_put(dest_dev);
1068
Amitesh Anand63be37d2021-12-24 20:51:48 +05301069 DEBUG_TRACE("%px: connection already exists - p:%d\n"
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301070 " s: %s:%pM:%pI4:%u, d: %s:%pM:%pI4:%u\n",
Amitesh Anand63be37d2021-12-24 20:51:48 +05301071 msg, tuple->protocol,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301072 src_dev->name, msg->conn_rule.flow_mac, &tuple->flow_ip, ntohs(tuple->flow_ident),
1073 dest_dev->name, msg->conn_rule.return_mac, &tuple->return_ip, ntohs(tuple->return_ident));
1074
Nicolas Costa514fde02014-01-13 15:50:29 -06001075 return -EADDRINUSE;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001076 }
1077
1078 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001079 * Fill in the "original" direction connection matching object.
1080 * Note that the transmit MAC address is "dest_mac_xlate" because
1081 * we always know both ends of a connection by their translated
1082 * addresses and not their public addresses.
1083 */
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001084 original_cm->match_dev = src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301085 original_cm->match_protocol = tuple->protocol;
1086 original_cm->match_src_ip = tuple->flow_ip;
1087 original_cm->match_src_port = tuple->flow_ident;
1088 original_cm->match_dest_ip = tuple->return_ip;
1089 original_cm->match_dest_port = tuple->return_ident;
1090
1091 original_cm->xlate_src_ip = msg->conn_rule.flow_ip_xlate;
1092 original_cm->xlate_src_port = msg->conn_rule.flow_ident_xlate;
1093 original_cm->xlate_dest_ip = msg->conn_rule.return_ip_xlate;
1094 original_cm->xlate_dest_port =msg->conn_rule.return_ident_xlate;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301095 atomic_set(&original_cm->rx_packet_count, 0);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001096 original_cm->rx_packet_count64 = 0;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301097 atomic_set(&original_cm->rx_byte_count, 0);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001098 original_cm->rx_byte_count64 = 0;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301099
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001100 original_cm->xmit_dev = dest_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301101 original_cm->xmit_dev_mtu = msg->conn_rule.return_mtu;
1102
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001103 original_cm->connection = c;
1104 original_cm->counter_match = reply_cm;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301105
Amitesh Anand63be37d2021-12-24 20:51:48 +05301106 /*
1107 * UDP Socket is valid only in decap direction.
1108 */
1109 RCU_INIT_POINTER(original_cm->up, NULL);
1110
Ken Zhu37040ea2021-09-09 21:11:15 -07001111 original_cm->flags = 0;
1112 if (msg->valid_flags & SFE_RULE_CREATE_MARK_VALID) {
1113 original_cm->mark = msg->mark_rule.flow_mark;
1114 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_MARK;
1115 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301116 if (msg->valid_flags & SFE_RULE_CREATE_QOS_VALID) {
1117 original_cm->priority = msg->qos_rule.flow_qos_tag;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001118 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PRIORITY_REMARK;
1119 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301120 if (msg->valid_flags & SFE_RULE_CREATE_DSCP_MARKING_VALID) {
1121 original_cm->dscp = msg->dscp_rule.flow_dscp << SFE_IPV4_DSCP_SHIFT;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001122 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_DSCP_REMARK;
1123 }
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301124
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301125 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1126 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_BRIDGE_FLOW;
1127 }
1128
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001129#ifdef CONFIG_NF_FLOW_COOKIE
1130 original_cm->flow_cookie = 0;
1131#endif
Zhi Chen8748eb32015-06-18 12:58:48 -07001132#ifdef CONFIG_XFRM
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301133 if (msg->valid_flags & SFE_RULE_CREATE_DIRECTION_VALID) {
1134 original_cm->flow_accel = msg->direction_rule.flow_accel;
1135 } else {
1136 original_cm->flow_accel = 1;
1137 }
Zhi Chen8748eb32015-06-18 12:58:48 -07001138#endif
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301139 /*
1140 * If l2_features are disabled and flow uses l2 features such as macvlan/bridge/pppoe/vlan,
1141 * bottom interfaces are expected to be disabled in the flow rule and always top interfaces
1142 * are used. In such cases, do not use HW csum offload. csum offload is used only when we
1143 * are sending directly to the destination interface that supports it.
1144 */
Suruchi Sumanf2077182022-01-13 21:35:23 +05301145 if (likely(dest_dev->features & NETIF_F_HW_CSUM) && !netif_is_vxlan(dest_dev)) {
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301146 if ((msg->conn_rule.return_top_interface_num == msg->conn_rule.return_interface_num) ||
1147 (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_RETURN_BOTTOM_INTERFACE)) {
Suruchi Sumanf2077182022-01-13 21:35:23 +05301148 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_CSUM_OFFLOAD;
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301149 }
1150 }
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001151
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +05301152 reply_cm->flags = 0;
1153
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001154 /*
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301155 * Adding PPPoE parameters to original and reply entries based on the direction where
1156 * PPPoE header is valid in ECM rule.
1157 *
1158 * If PPPoE is valid in flow direction (from interface is PPPoE), then
1159 * original cm will have PPPoE at ingress (strip PPPoE header)
1160 * reply cm will have PPPoE at egress (add PPPoE header)
1161 *
1162 * If PPPoE is valid in return direction (to interface is PPPoE), then
1163 * original cm will have PPPoE at egress (add PPPoE header)
1164 * reply cm will have PPPoE at ingress (strip PPPoE header)
1165 */
1166 if (msg->valid_flags & SFE_RULE_CREATE_PPPOE_DECAP_VALID) {
1167 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_DECAP;
1168 original_cm->pppoe_session_id = msg->pppoe_rule.flow_pppoe_session_id;
1169 ether_addr_copy(original_cm->pppoe_remote_mac, msg->pppoe_rule.flow_pppoe_remote_mac);
1170
1171 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_ENCAP;
1172 reply_cm->pppoe_session_id = msg->pppoe_rule.flow_pppoe_session_id;
1173 ether_addr_copy(reply_cm->pppoe_remote_mac, msg->pppoe_rule.flow_pppoe_remote_mac);
1174 }
1175
1176 if (msg->valid_flags & SFE_RULE_CREATE_PPPOE_ENCAP_VALID) {
1177 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_ENCAP;
1178 original_cm->pppoe_session_id = msg->pppoe_rule.return_pppoe_session_id;
1179 ether_addr_copy(original_cm->pppoe_remote_mac, msg->pppoe_rule.return_pppoe_remote_mac);
1180
1181 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_DECAP;
1182 reply_cm->pppoe_session_id = msg->pppoe_rule.return_pppoe_session_id;
1183 ether_addr_copy(reply_cm->pppoe_remote_mac, msg->pppoe_rule.return_pppoe_remote_mac);
1184 }
1185
1186 /*
Ken Zhubbf49652021-09-12 15:33:09 -07001187 * For the non-arp interface, we don't write L2 HDR.
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001188 */
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +05301189 if (sfe_ipv4_xmit_eth_type_check(dest_dev, original_cm->flags)) {
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301190
1191 /*
1192 * Check whether the rule has configured a specific source MAC address to use.
1193 * This is needed when virtual L3 interfaces such as br-lan, macvlan, vlan are used during egress
1194 */
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301195
1196 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1197 ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)msg->conn_rule.flow_mac);
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301198 } else {
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301199 if ((msg->valid_flags & SFE_RULE_CREATE_SRC_MAC_VALID) &&
1200 (msg->src_mac_rule.mac_valid_flags & SFE_SRC_MAC_RETURN_VALID)) {
1201 ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)msg->src_mac_rule.return_src_mac);
1202 } else {
1203 ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)dest_dev->dev_addr);
1204 }
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301205 }
1206
1207 ether_addr_copy((u8 *)original_cm->xmit_dest_mac, (u8 *)msg->conn_rule.return_mac);
1208
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001209 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_L2_HDR;
1210
1211 /*
1212 * If our dev writes Ethernet headers then we can write a really fast
1213 * version.
1214 */
1215 if (dest_dev->header_ops) {
1216 if (dest_dev->header_ops->create == eth_header) {
1217 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR;
1218 }
1219 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001220 }
1221
1222 /*
1223 * Fill in the "reply" direction connection matching object.
1224 */
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001225 reply_cm->match_dev = dest_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301226 reply_cm->match_protocol = tuple->protocol;
1227 reply_cm->match_src_ip = msg->conn_rule.return_ip_xlate;
Amitesh Anand63be37d2021-12-24 20:51:48 +05301228
1229 /*
1230 * Keep source port as 0 for VxLAN tunnels.
1231 */
1232 if (netif_is_vxlan(src_dev) || netif_is_vxlan(dest_dev)) {
1233 reply_cm->match_src_port = 0;
1234 } else {
1235 reply_cm->match_src_port = msg->conn_rule.return_ident_xlate;
1236 }
1237
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301238 reply_cm->match_dest_ip = msg->conn_rule.flow_ip_xlate;
1239 reply_cm->match_dest_port = msg->conn_rule.flow_ident_xlate;
1240
1241 reply_cm->xlate_src_ip = tuple->return_ip;
1242 reply_cm->xlate_src_port = tuple->return_ident;
1243 reply_cm->xlate_dest_ip = tuple->flow_ip;
1244 reply_cm->xlate_dest_port = tuple->flow_ident;;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301245
1246 atomic_set(&reply_cm->rx_packet_count, 0);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001247 reply_cm->rx_packet_count64 = 0;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301248 atomic_set(&reply_cm->rx_byte_count, 0);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001249 reply_cm->rx_byte_count64 = 0;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301250
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001251 reply_cm->xmit_dev = src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301252 reply_cm->xmit_dev_mtu = msg->conn_rule.flow_mtu;
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301253
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001254 reply_cm->connection = c;
1255 reply_cm->counter_match = original_cm;
Ken Zhu37040ea2021-09-09 21:11:15 -07001256
1257 reply_cm->flags = 0;
1258 if (msg->valid_flags & SFE_RULE_CREATE_MARK_VALID) {
1259 reply_cm->mark = msg->mark_rule.return_mark;
1260 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_MARK;
1261 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301262 if (msg->valid_flags & SFE_RULE_CREATE_QOS_VALID) {
1263 reply_cm->priority = msg->qos_rule.return_qos_tag;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001264 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PRIORITY_REMARK;
1265 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301266 if (msg->valid_flags & SFE_RULE_CREATE_DSCP_MARKING_VALID) {
1267 reply_cm->dscp = msg->dscp_rule.return_dscp << SFE_IPV4_DSCP_SHIFT;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001268 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_DSCP_REMARK;
1269 }
Amitesh Anand63be37d2021-12-24 20:51:48 +05301270
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301271 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1272 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_BRIDGE_FLOW;
1273 }
1274
Amitesh Anand63be37d2021-12-24 20:51:48 +05301275 /*
1276 * Setup UDP Socket if found to be valid for decap.
1277 */
1278 RCU_INIT_POINTER(reply_cm->up, NULL);
1279 net = dev_net(reply_cm->match_dev);
1280 src_if_idx = src_dev->ifindex;
1281
1282 rcu_read_lock();
1283
1284 /*
1285 * Look for the associated sock object.
1286 * __udp4_lib_lookup() holds a reference for this sock object,
1287 * which will be released in sfe_ipv4_free_connection_rcu()
1288 */
1289#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
1290 sk = __udp4_lib_lookup(net, reply_cm->match_dest_ip, reply_cm->match_dest_port,
1291 reply_cm->xlate_src_ip, reply_cm->xlate_src_port, src_if_idx, &udp_table);
1292#else
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, 0, &udp_table, NULL);
1295#endif
1296
1297 rcu_read_unlock();
1298
1299 /*
1300 * We set the UDP sock pointer as valid only for decap direction.
1301 */
1302 if (sk && udp_sk(sk)->encap_type) {
1303#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
1304 if (!atomic_add_unless(&sk->sk_refcnt, 1, 0)) {
1305#else
1306 if (!refcount_inc_not_zero(&sk->sk_refcnt)) {
1307#endif
1308 kfree(reply_cm);
1309 kfree(original_cm);
1310 kfree(c);
1311
1312 DEBUG_TRACE("%px: sfe: unable to take reference for socket(%px) p:%d\n"
1313 " s: %s:%pM:%pI4:%u, d: %s:%pM:%pI4:%u\n",
1314 msg, sk, tuple->protocol,
1315 src_dev->name, msg->conn_rule.flow_mac, &tuple->flow_ip, ntohs(tuple->flow_ident),
1316 dest_dev->name, msg->conn_rule.return_mac, &tuple->return_ip, ntohs(tuple->return_ident));
1317
1318 dev_put(src_dev);
1319 dev_put(dest_dev);
1320
1321 return -ESHUTDOWN;
1322 }
1323
1324 rcu_assign_pointer(reply_cm->up, udp_sk(sk));
1325
1326 DEBUG_INFO("%px: Sock(%px) lookup success with reply_cm direction\n", msg, sk);
1327 DEBUG_INFO("%px: SFE connection -\n"
1328 " s: %s:%pI4(%pI4):%u(%u)\n"
1329 " d: %s:%pI4(%pI4):%u(%u)\n",
1330 msg, reply_cm->match_dev->name, &reply_cm->match_src_ip, &reply_cm->xlate_src_ip,
1331 ntohs(reply_cm->match_src_port), ntohs(reply_cm->xlate_src_port),
1332 reply_cm->xmit_dev->name, &reply_cm->match_dest_ip, &reply_cm->xlate_dest_ip,
1333 ntohs(reply_cm->match_dest_port), ntohs(reply_cm->xlate_dest_port));
1334 }
1335
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001336#ifdef CONFIG_NF_FLOW_COOKIE
1337 reply_cm->flow_cookie = 0;
1338#endif
Zhi Chen8748eb32015-06-18 12:58:48 -07001339#ifdef CONFIG_XFRM
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301340 if (msg->valid_flags & SFE_RULE_CREATE_DIRECTION_VALID) {
1341 reply_cm->flow_accel = msg->direction_rule.return_accel;
1342 } else {
1343 reply_cm->flow_accel = 1;
1344 }
1345
Zhi Chen8748eb32015-06-18 12:58:48 -07001346#endif
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301347 /*
1348 * If l2_features are disabled and flow uses l2 features such as macvlan/bridge/pppoe/vlan,
1349 * bottom interfaces are expected to be disabled in the flow rule and always top interfaces
1350 * are used. In such cases, do not use HW csum offload. csum offload is used only when we
1351 * are sending directly to the destination interface that supports it.
1352 */
Suruchi Sumanf2077182022-01-13 21:35:23 +05301353 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 +05301354 if ((msg->conn_rule.flow_top_interface_num == msg->conn_rule.flow_interface_num) ||
1355 (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_FLOW_BOTTOM_INTERFACE)) {
1356 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_CSUM_OFFLOAD;
1357 }
1358 }
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001359
1360 /*
Ken Zhubbf49652021-09-12 15:33:09 -07001361 * For the non-arp interface, we don't write L2 HDR.
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001362 */
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +05301363 if (sfe_ipv4_xmit_eth_type_check(src_dev, reply_cm->flags)) {
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301364
1365 /*
1366 * Check whether the rule has configured a specific source MAC address to use.
1367 * This is needed when virtual L3 interfaces such as br-lan, macvlan, vlan are used during egress
1368 */
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301369
1370 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1371 ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)msg->conn_rule.return_mac);
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301372 } else {
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301373 if ((msg->valid_flags & SFE_RULE_CREATE_SRC_MAC_VALID) &&
1374 (msg->src_mac_rule.mac_valid_flags & SFE_SRC_MAC_FLOW_VALID)) {
1375 ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)msg->src_mac_rule.flow_src_mac);
1376 } else {
1377 ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)src_dev->dev_addr);
1378 }
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301379 }
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301380
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301381 ether_addr_copy((u8 *)reply_cm->xmit_dest_mac, (u8 *)msg->conn_rule.flow_mac);
1382
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001383 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_L2_HDR;
1384
1385 /*
1386 * If our dev writes Ethernet headers then we can write a really fast
1387 * version.
1388 */
1389 if (src_dev->header_ops) {
1390 if (src_dev->header_ops->create == eth_header) {
1391 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR;
1392 }
1393 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001394 }
1395
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301396 if ((tuple->return_ip != msg->conn_rule.return_ip_xlate) ||
1397 (tuple->return_ident != msg->conn_rule.return_ident_xlate)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001398 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST;
1399 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC;
1400 }
1401
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301402 if ((tuple->flow_ip != msg->conn_rule.flow_ip_xlate) ||
1403 (tuple->flow_ident != msg->conn_rule.flow_ident_xlate)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001404 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC;
1405 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST;
1406 }
1407
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301408 c->protocol = tuple->protocol;
1409 c->src_ip = tuple->flow_ip;
1410 c->src_ip_xlate = msg->conn_rule.flow_ip_xlate;
1411 c->src_port = tuple->flow_ident;
1412 c->src_port_xlate = msg->conn_rule.flow_ident_xlate;
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001413 c->original_dev = src_dev;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001414 c->original_match = original_cm;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301415 c->dest_ip = tuple->return_ip;
1416 c->dest_ip_xlate = msg->conn_rule.return_ip_xlate;
1417 c->dest_port = tuple->return_ident;
1418 c->dest_port_xlate = msg->conn_rule.return_ident_xlate;
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001419 c->reply_dev = dest_dev;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001420 c->reply_match = reply_cm;
Xiaoping Fan34586472015-07-03 02:20:35 -07001421 c->debug_read_seq = 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001422 c->last_sync_jiffies = get_jiffies_64();
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301423 c->removed = false;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001424
1425 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001426 * Initialize the protocol-specific information that we track.
1427 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301428 switch (tuple->protocol) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001429 case IPPROTO_TCP:
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301430 original_cm->protocol_state.tcp.win_scale = msg->tcp_rule.flow_window_scale;
1431 original_cm->protocol_state.tcp.max_win = msg->tcp_rule.flow_max_window ? msg->tcp_rule.flow_max_window : 1;
1432 original_cm->protocol_state.tcp.end = msg->tcp_rule.flow_end;
1433 original_cm->protocol_state.tcp.max_end = msg->tcp_rule.flow_max_end;
1434
1435 reply_cm->protocol_state.tcp.win_scale = msg->tcp_rule.return_window_scale;
1436 reply_cm->protocol_state.tcp.max_win = msg->tcp_rule.return_max_window ? msg->tcp_rule.return_max_window : 1;
1437 reply_cm->protocol_state.tcp.end = msg->tcp_rule.return_end;
1438 reply_cm->protocol_state.tcp.max_end = msg->tcp_rule.return_max_end;
1439
1440 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_NO_SEQ_CHECK) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001441 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
1442 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
1443 }
1444 break;
1445 }
1446
1447 sfe_ipv4_connection_match_compute_translations(original_cm);
1448 sfe_ipv4_connection_match_compute_translations(reply_cm);
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301449 sfe_ipv4_insert_connection(si, c);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001450
1451 spin_unlock_bh(&si->lock);
1452
1453 /*
1454 * We have everything we need!
1455 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301456 DEBUG_INFO("new connection - p: %d\n"
Tian Yang45f39c82020-10-06 14:07:47 -07001457 " s: %s:%pxM(%pxM):%pI4(%pI4):%u(%u)\n"
1458 " d: %s:%pxM(%pxM):%pI4(%pI4):%u(%u)\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301459 tuple->protocol,
1460 src_dev->name, msg->conn_rule.flow_mac, NULL,
1461 &tuple->flow_ip, &msg->conn_rule.flow_ip_xlate, ntohs(tuple->flow_ident), ntohs(msg->conn_rule.flow_ident_xlate),
1462 dest_dev->name, NULL, msg->conn_rule.return_mac,
1463 &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 -06001464
1465 return 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001466}
1467
1468/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001469 * sfe_ipv4_destroy_rule()
1470 * Destroy a forwarding rule.
1471 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301472void sfe_ipv4_destroy_rule(struct sfe_ipv4_rule_destroy_msg *msg)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001473{
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001474 struct sfe_ipv4 *si = &__si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001475 struct sfe_ipv4_connection *c;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301476 bool ret;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301477 struct sfe_ipv4_5tuple *tuple = &msg->tuple;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001478
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301479 this_cpu_inc(si->stats_pcpu->connection_destroy_requests64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001480 spin_lock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001481
1482 /*
1483 * Check to see if we have a flow that matches the rule we're trying
1484 * to destroy. If there isn't then we can't destroy it.
1485 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301486 c = sfe_ipv4_find_connection(si, tuple->protocol, tuple->flow_ip, tuple->flow_ident,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301487 tuple->return_ip, tuple->return_ident);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001488 if (!c) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001489 spin_unlock_bh(&si->lock);
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301490 this_cpu_inc(si->stats_pcpu->connection_destroy_misses64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001491
1492 DEBUG_TRACE("connection does not exist - p: %d, s: %pI4:%u, d: %pI4:%u\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301493 tuple->protocol, &tuple->flow_ip, ntohs(tuple->flow_ident),
1494 &tuple->return_ip, ntohs(tuple->return_ident));
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001495 return;
1496 }
1497
1498 /*
1499 * Remove our connection details from the hash tables.
1500 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301501 ret = sfe_ipv4_remove_connection(si, c);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001502 spin_unlock_bh(&si->lock);
1503
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301504 if (ret) {
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301505 sfe_ipv4_flush_connection(si, c, SFE_SYNC_REASON_DESTROY);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301506 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001507
1508 DEBUG_INFO("connection destroyed - p: %d, s: %pI4:%u, d: %pI4:%u\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301509 tuple->protocol, &tuple->flow_ip, ntohs(tuple->flow_ident),
1510 &tuple->return_ip, ntohs(tuple->return_ident));
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001511}
1512
1513/*
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001514 * sfe_ipv4_register_sync_rule_callback()
1515 * Register a callback for rule synchronization.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001516 */
Xiaoping Fand44a5b42015-05-26 17:37:37 -07001517void sfe_ipv4_register_sync_rule_callback(sfe_sync_rule_callback_t sync_rule_callback)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001518{
1519 struct sfe_ipv4 *si = &__si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001520
1521 spin_lock_bh(&si->lock);
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001522 rcu_assign_pointer(si->sync_rule_callback, sync_rule_callback);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001523 spin_unlock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001524}
1525
1526/*
1527 * sfe_ipv4_get_debug_dev()
1528 */
1529static ssize_t sfe_ipv4_get_debug_dev(struct device *dev,
1530 struct device_attribute *attr,
1531 char *buf)
1532{
1533 struct sfe_ipv4 *si = &__si;
1534 ssize_t count;
1535 int num;
1536
1537 spin_lock_bh(&si->lock);
1538 num = si->debug_dev;
1539 spin_unlock_bh(&si->lock);
1540
1541 count = snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", num);
1542 return count;
1543}
1544
1545/*
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001546 * sysfs attributes.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001547 */
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001548static const struct device_attribute sfe_ipv4_debug_dev_attr =
Xiaoping Fane70da412016-02-26 16:47:57 -08001549 __ATTR(debug_dev, S_IWUSR | S_IRUGO, sfe_ipv4_get_debug_dev, NULL);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001550
1551/*
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001552 * sfe_ipv4_destroy_all_rules_for_dev()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001553 * Destroy all connections that match a particular device.
1554 *
1555 * If we pass dev as NULL then this destroys all connections.
1556 */
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001557void sfe_ipv4_destroy_all_rules_for_dev(struct net_device *dev)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001558{
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001559 struct sfe_ipv4 *si = &__si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001560 struct sfe_ipv4_connection *c;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301561 bool ret;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001562
Xiaoping Fan34586472015-07-03 02:20:35 -07001563another_round:
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001564 spin_lock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001565
Xiaoping Fan34586472015-07-03 02:20:35 -07001566 for (c = si->all_connections_head; c; c = c->all_connections_next) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001567 /*
Xiaoping Fan34586472015-07-03 02:20:35 -07001568 * Does this connection relate to the device we are destroying?
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001569 */
1570 if (!dev
1571 || (dev == c->original_dev)
1572 || (dev == c->reply_dev)) {
Xiaoping Fan34586472015-07-03 02:20:35 -07001573 break;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001574 }
Xiaoping Fan34586472015-07-03 02:20:35 -07001575 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001576
Xiaoping Fan34586472015-07-03 02:20:35 -07001577 if (c) {
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301578 ret = sfe_ipv4_remove_connection(si, c);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001579 }
1580
1581 spin_unlock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07001582
1583 if (c) {
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301584 if (ret) {
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301585 sfe_ipv4_flush_connection(si, c, SFE_SYNC_REASON_DESTROY);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301586 }
Xiaoping Fan34586472015-07-03 02:20:35 -07001587 goto another_round;
1588 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001589}
1590
1591/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001592 * sfe_ipv4_periodic_sync()
1593 */
Ken Zhu137722d2021-09-23 17:57:36 -07001594static void sfe_ipv4_periodic_sync(struct work_struct *work)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001595{
Ken Zhu137722d2021-09-23 17:57:36 -07001596 struct sfe_ipv4 *si = container_of((struct delayed_work *)work, struct sfe_ipv4, sync_dwork);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07001597 u64 now_jiffies;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001598 int quota;
Xiaoping Fand44a5b42015-05-26 17:37:37 -07001599 sfe_sync_rule_callback_t sync_rule_callback;
Ken Zhudc423672021-09-02 18:27:01 -07001600 struct sfe_ipv4_connection *c;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001601
1602 now_jiffies = get_jiffies_64();
1603
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001604 rcu_read_lock();
1605 sync_rule_callback = rcu_dereference(si->sync_rule_callback);
1606 if (!sync_rule_callback) {
1607 rcu_read_unlock();
1608 goto done;
1609 }
1610
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001611 spin_lock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001612
1613 /*
Ken Zhudc423672021-09-02 18:27:01 -07001614 * If we have reached the end of the connection list, walk from
1615 * the connection head.
1616 */
1617 c = si->wc_next;
1618 if (unlikely(!c)) {
1619 c = si->all_connections_head;
1620 }
1621
1622 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001623 * Get an estimate of the number of connections to parse in this sync.
1624 */
1625 quota = (si->num_connections + 63) / 64;
1626
1627 /*
Ken Zhudc423672021-09-02 18:27:01 -07001628 * Walk the "all connection" list and sync the connection state.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001629 */
Ken Zhudc423672021-09-02 18:27:01 -07001630 while (likely(c && quota)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001631 struct sfe_ipv4_connection_match *cm;
1632 struct sfe_ipv4_connection_match *counter_cm;
Xiaoping Fand44a5b42015-05-26 17:37:37 -07001633 struct sfe_connection_sync sis;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001634
Ken Zhudc423672021-09-02 18:27:01 -07001635 cm = c->original_match;
1636 counter_cm = c->reply_match;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001637
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001638 /*
Ken Zhudc423672021-09-02 18:27:01 -07001639 * Didn't receive packets in the original direction or reply
1640 * direction, move to the next connection.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001641 */
Ken Zhudc423672021-09-02 18:27:01 -07001642 if ((!atomic_read(&cm->rx_packet_count)) && !(atomic_read(&counter_cm->rx_packet_count))) {
1643 c = c->all_connections_next;
1644 continue;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001645 }
1646
Ken Zhudc423672021-09-02 18:27:01 -07001647 quota--;
Matthew McClintockaf48f1e2014-01-23 15:29:19 -06001648
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301649 sfe_ipv4_gen_sync_connection(si, c, &sis, SFE_SYNC_REASON_STATS, now_jiffies);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001650
Ken Zhudc423672021-09-02 18:27:01 -07001651 si->wc_next = c->all_connections_next;
1652
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001653 /*
1654 * We don't want to be holding the lock when we sync!
1655 */
1656 spin_unlock_bh(&si->lock);
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001657 sync_rule_callback(&sis);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001658 spin_lock_bh(&si->lock);
Ken Zhudc423672021-09-02 18:27:01 -07001659
1660 /*
1661 * c must be set and used in the same lock/unlock window;
1662 * because c could be removed when we don't hold the lock,
1663 * so delay grabbing until after the callback and relock.
1664 */
1665 c = si->wc_next;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001666 }
1667
Ken Zhudc423672021-09-02 18:27:01 -07001668 /*
1669 * At the end of the sync, put the wc_next to the connection we left.
1670 */
1671 si->wc_next = c;
1672
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001673 spin_unlock_bh(&si->lock);
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001674 rcu_read_unlock();
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001675
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001676done:
Ken Zhu137722d2021-09-23 17:57:36 -07001677 schedule_delayed_work_on(si->work_cpu, (struct delayed_work *)work, ((HZ + 99) / 100));
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001678}
1679
1680#define CHAR_DEV_MSG_SIZE 768
1681
1682/*
1683 * sfe_ipv4_debug_dev_read_start()
1684 * Generate part of the XML output.
1685 */
1686static bool sfe_ipv4_debug_dev_read_start(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
1687 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
1688{
1689 int bytes_read;
1690
Xiaoping Fan34586472015-07-03 02:20:35 -07001691 si->debug_read_seq++;
1692
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001693 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "<sfe_ipv4>\n");
1694 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1695 return false;
1696 }
1697
1698 *length -= bytes_read;
1699 *total_read += bytes_read;
1700
1701 ws->state++;
1702 return true;
1703}
1704
1705/*
1706 * sfe_ipv4_debug_dev_read_connections_start()
1707 * Generate part of the XML output.
1708 */
1709static bool sfe_ipv4_debug_dev_read_connections_start(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
1710 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
1711{
1712 int bytes_read;
1713
1714 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<connections>\n");
1715 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1716 return false;
1717 }
1718
1719 *length -= bytes_read;
1720 *total_read += bytes_read;
1721
1722 ws->state++;
1723 return true;
1724}
1725
1726/*
1727 * sfe_ipv4_debug_dev_read_connections_connection()
1728 * Generate part of the XML output.
1729 */
1730static bool sfe_ipv4_debug_dev_read_connections_connection(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
1731 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
1732{
1733 struct sfe_ipv4_connection *c;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001734 struct sfe_ipv4_connection_match *original_cm;
1735 struct sfe_ipv4_connection_match *reply_cm;
1736 int bytes_read;
1737 int protocol;
1738 struct net_device *src_dev;
Dave Hudson87973cd2013-10-22 16:00:04 +01001739 __be32 src_ip;
1740 __be32 src_ip_xlate;
1741 __be16 src_port;
1742 __be16 src_port_xlate;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07001743 u64 src_rx_packets;
1744 u64 src_rx_bytes;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001745 struct net_device *dest_dev;
Dave Hudson87973cd2013-10-22 16:00:04 +01001746 __be32 dest_ip;
1747 __be32 dest_ip_xlate;
1748 __be16 dest_port;
1749 __be16 dest_port_xlate;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07001750 u64 dest_rx_packets;
1751 u64 dest_rx_bytes;
1752 u64 last_sync_jiffies;
Ken Zhu37040ea2021-09-09 21:11:15 -07001753 u32 src_mark, dest_mark, src_priority, dest_priority, src_dscp, dest_dscp;
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301754 u32 packet, byte, original_cm_flags;
1755 u16 pppoe_session_id;
1756 u8 pppoe_remote_mac[ETH_ALEN];
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001757#ifdef CONFIG_NF_FLOW_COOKIE
1758 int src_flow_cookie, dst_flow_cookie;
1759#endif
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001760
1761 spin_lock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07001762
1763 for (c = si->all_connections_head; c; c = c->all_connections_next) {
1764 if (c->debug_read_seq < si->debug_read_seq) {
1765 c->debug_read_seq = si->debug_read_seq;
1766 break;
1767 }
1768 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001769
1770 /*
Xiaoping Fan34586472015-07-03 02:20:35 -07001771 * If there were no connections then move to the next state.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001772 */
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301773 if (!c || c->removed) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001774 spin_unlock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07001775 ws->state++;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001776 return true;
1777 }
1778
1779 original_cm = c->original_match;
1780 reply_cm = c->reply_match;
1781
1782 protocol = c->protocol;
1783 src_dev = c->original_dev;
1784 src_ip = c->src_ip;
1785 src_ip_xlate = c->src_ip_xlate;
1786 src_port = c->src_port;
1787 src_port_xlate = c->src_port_xlate;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001788 src_priority = original_cm->priority;
1789 src_dscp = original_cm->dscp >> SFE_IPV4_DSCP_SHIFT;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001790
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301791 sfe_ipv4_connection_match_update_summary_stats(original_cm, &packet, &byte);
1792 sfe_ipv4_connection_match_update_summary_stats(reply_cm, &packet, &byte);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001793
1794 src_rx_packets = original_cm->rx_packet_count64;
1795 src_rx_bytes = original_cm->rx_byte_count64;
Ken Zhu37040ea2021-09-09 21:11:15 -07001796 src_mark = original_cm->mark;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001797 dest_dev = c->reply_dev;
1798 dest_ip = c->dest_ip;
1799 dest_ip_xlate = c->dest_ip_xlate;
1800 dest_port = c->dest_port;
1801 dest_port_xlate = c->dest_port_xlate;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001802 dest_priority = reply_cm->priority;
1803 dest_dscp = reply_cm->dscp >> SFE_IPV4_DSCP_SHIFT;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001804 dest_rx_packets = reply_cm->rx_packet_count64;
1805 dest_rx_bytes = reply_cm->rx_byte_count64;
Ken Zhu37040ea2021-09-09 21:11:15 -07001806 dest_mark = reply_cm->mark;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001807 last_sync_jiffies = get_jiffies_64() - c->last_sync_jiffies;
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301808 original_cm_flags = original_cm->flags;
1809 pppoe_session_id = original_cm->pppoe_session_id;
1810 ether_addr_copy(pppoe_remote_mac, original_cm->pppoe_remote_mac);
1811
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001812#ifdef CONFIG_NF_FLOW_COOKIE
1813 src_flow_cookie = original_cm->flow_cookie;
1814 dst_flow_cookie = reply_cm->flow_cookie;
1815#endif
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001816 spin_unlock_bh(&si->lock);
1817
1818 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t\t<connection "
1819 "protocol=\"%u\" "
1820 "src_dev=\"%s\" "
1821 "src_ip=\"%pI4\" src_ip_xlate=\"%pI4\" "
1822 "src_port=\"%u\" src_port_xlate=\"%u\" "
Xiaoping Fane1963d42015-08-25 17:06:19 -07001823 "src_priority=\"%u\" src_dscp=\"%u\" "
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001824 "src_rx_pkts=\"%llu\" src_rx_bytes=\"%llu\" "
Ken Zhu37040ea2021-09-09 21:11:15 -07001825 "src_mark=\"%08x\" "
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001826 "dest_dev=\"%s\" "
1827 "dest_ip=\"%pI4\" dest_ip_xlate=\"%pI4\" "
1828 "dest_port=\"%u\" dest_port_xlate=\"%u\" "
Xiaoping Fane1963d42015-08-25 17:06:19 -07001829 "dest_priority=\"%u\" dest_dscp=\"%u\" "
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001830 "dest_rx_pkts=\"%llu\" dest_rx_bytes=\"%llu\" "
Ken Zhu37040ea2021-09-09 21:11:15 -07001831 "dest_mark=\"%08x\" "
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001832#ifdef CONFIG_NF_FLOW_COOKIE
1833 "src_flow_cookie=\"%d\" dst_flow_cookie=\"%d\" "
1834#endif
Ken Zhu37040ea2021-09-09 21:11:15 -07001835 "last_sync=\"%llu\" ",
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001836 protocol,
1837 src_dev->name,
1838 &src_ip, &src_ip_xlate,
Dave Hudson87973cd2013-10-22 16:00:04 +01001839 ntohs(src_port), ntohs(src_port_xlate),
Xiaoping Fane1963d42015-08-25 17:06:19 -07001840 src_priority, src_dscp,
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001841 src_rx_packets, src_rx_bytes,
Ken Zhu37040ea2021-09-09 21:11:15 -07001842 src_mark,
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001843 dest_dev->name,
1844 &dest_ip, &dest_ip_xlate,
Dave Hudson87973cd2013-10-22 16:00:04 +01001845 ntohs(dest_port), ntohs(dest_port_xlate),
Xiaoping Fane1963d42015-08-25 17:06:19 -07001846 dest_priority, dest_dscp,
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001847 dest_rx_packets, dest_rx_bytes,
Ken Zhu37040ea2021-09-09 21:11:15 -07001848 dest_mark,
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001849#ifdef CONFIG_NF_FLOW_COOKIE
1850 src_flow_cookie, dst_flow_cookie,
1851#endif
Ken Zhu37040ea2021-09-09 21:11:15 -07001852 last_sync_jiffies);
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{
Guduri Prathyusha5f27e232022-01-06 14:39:04 +05302256 struct sfe_l2_info l2_info;
2257 l2_info.parse_flags = 0;
2258
Amitesh Anand63be37d2021-12-24 20:51:48 +05302259 DEBUG_TRACE("%px: sfe: sfe_ipv4_local_out hook called.\n", skb);
2260
2261 if (likely(skb->skb_iif)) {
Guduri Prathyusha5f27e232022-01-06 14:39:04 +05302262 return sfe_ipv4_recv(skb->dev, skb, &l2_info, true) ? NF_STOLEN : NF_ACCEPT;
Amitesh Anand63be37d2021-12-24 20:51:48 +05302263 }
2264
2265 return NF_ACCEPT;
2266}
2267
2268/*
2269 * struct nf_hook_ops sfe_ipv4_ops_local_out[]
2270 * Hooks into netfilter local out packet monitoring points.
2271 */
2272static struct nf_hook_ops sfe_ipv4_ops_local_out[] __read_mostly = {
2273
2274 /*
2275 * Local out routing hook is used to monitor packets.
2276 */
2277 {
2278 .hook = sfe_ipv4_local_out,
2279 .pf = PF_INET,
2280 .hooknum = NF_INET_LOCAL_OUT,
2281 .priority = NF_IP_PRI_FIRST,
2282 },
2283};
2284#endif
2285
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002286/*
Dave Hudson87973cd2013-10-22 16:00:04 +01002287 * sfe_ipv4_init()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002288 */
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05302289int sfe_ipv4_init(void)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002290{
2291 struct sfe_ipv4 *si = &__si;
2292 int result = -1;
2293
Dave Hudsondcd08fb2013-11-22 09:25:16 -06002294 DEBUG_INFO("SFE IPv4 init\n");
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002295
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05302296 sfe_ipv4_conn_match_hash_init(si, ARRAY_SIZE(si->hlist_conn_match_hash_head));
2297
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302298 si->stats_pcpu = alloc_percpu_gfp(struct sfe_ipv4_stats, GFP_KERNEL | __GFP_ZERO);
2299 if (!si->stats_pcpu) {
2300 DEBUG_ERROR("failed to allocate stats memory for sfe_ipv4\n");
2301 goto exit0;
2302 }
2303
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002304 /*
2305 * Create sys/sfe_ipv4
2306 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302307 si->sys_ipv4 = kobject_create_and_add("sfe_ipv4", NULL);
2308 if (!si->sys_ipv4) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002309 DEBUG_ERROR("failed to register sfe_ipv4\n");
2310 goto exit1;
2311 }
2312
2313 /*
2314 * Create files, one for each parameter supported by this module.
2315 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302316 result = sysfs_create_file(si->sys_ipv4, &sfe_ipv4_debug_dev_attr.attr);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002317 if (result) {
2318 DEBUG_ERROR("failed to register debug dev file: %d\n", result);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002319 goto exit2;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002320 }
2321
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302322 result = sysfs_create_file(si->sys_ipv4, &sfe_ipv4_cpu_attr.attr);
Ken Zhu137722d2021-09-23 17:57:36 -07002323 if (result) {
2324 DEBUG_ERROR("failed to register debug dev file: %d\n", result);
2325 goto exit3;
2326 }
2327
Xiaoping Fan640faf42015-08-28 15:50:55 -07002328#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302329 result = sysfs_create_file(si->sys_ipv4, &sfe_ipv4_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002330 if (result) {
2331 DEBUG_ERROR("failed to register flow cookie enable file: %d\n", result);
Ken Zhu137722d2021-09-23 17:57:36 -07002332 goto exit4;
Xiaoping Fan640faf42015-08-28 15:50:55 -07002333 }
2334#endif /* CONFIG_NF_FLOW_COOKIE */
2335
Amitesh Anand63be37d2021-12-24 20:51:48 +05302336#ifdef SFE_PROCESS_LOCAL_OUT
2337#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
2338 result = nf_register_hooks(sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2339#else
2340 result = nf_register_net_hooks(&init_net, sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2341#endif
2342 if (result < 0) {
2343 DEBUG_ERROR("can't register nf local out hook: %d\n", result);
2344 goto exit5;
2345 }
2346 DEBUG_INFO("Register nf local out hook success: %d\n", result);
2347#endif
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002348 /*
2349 * Register our debug char device.
2350 */
2351 result = register_chrdev(0, "sfe_ipv4", &sfe_ipv4_debug_dev_fops);
2352 if (result < 0) {
2353 DEBUG_ERROR("Failed to register chrdev: %d\n", result);
Amitesh Anand63be37d2021-12-24 20:51:48 +05302354 goto exit6;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002355 }
2356
2357 si->debug_dev = result;
Ken Zhu137722d2021-09-23 17:57:36 -07002358 si->work_cpu = WORK_CPU_UNBOUND;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002359
2360 /*
Ken Zhu137722d2021-09-23 17:57:36 -07002361 * Create a work to handle periodic statistics.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002362 */
Ken Zhu137722d2021-09-23 17:57:36 -07002363 INIT_DELAYED_WORK(&(si->sync_dwork), sfe_ipv4_periodic_sync);
2364 schedule_delayed_work_on(si->work_cpu, &(si->sync_dwork), ((HZ + 99) / 100));
2365
Dave Hudson87973cd2013-10-22 16:00:04 +01002366 spin_lock_init(&si->lock);
Dave Hudson87973cd2013-10-22 16:00:04 +01002367 return 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002368
Amitesh Anand63be37d2021-12-24 20:51:48 +05302369exit6:
2370#ifdef SFE_PROCESS_LOCAL_OUT
2371 DEBUG_TRACE("sfe: Unregister local out hook\n");
2372#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
2373 nf_unregister_hooks(sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2374#else
2375 nf_unregister_net_hooks(&init_net, sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2376#endif
Ken Zhu137722d2021-09-23 17:57:36 -07002377exit5:
Amitesh Anand63be37d2021-12-24 20:51:48 +05302378#endif
Xiaoping Fan640faf42015-08-28 15:50:55 -07002379#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302380 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002381
Ken Zhu137722d2021-09-23 17:57:36 -07002382exit4:
Xiaoping Fan640faf42015-08-28 15:50:55 -07002383#endif /* CONFIG_NF_FLOW_COOKIE */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302384 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_cpu_attr.attr);
Ken Zhu137722d2021-09-23 17:57:36 -07002385exit3:
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302386 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_debug_dev_attr.attr);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002387
Xiaoping Fan640faf42015-08-28 15:50:55 -07002388exit2:
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302389 kobject_put(si->sys_ipv4);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002390
2391exit1:
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302392 free_percpu(si->stats_pcpu);
2393
2394exit0:
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002395 return result;
2396}
2397
2398/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002399 * sfe_ipv4_exit()
2400 */
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05302401void sfe_ipv4_exit(void)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002402{
Dave Hudson87973cd2013-10-22 16:00:04 +01002403 struct sfe_ipv4 *si = &__si;
2404
Dave Hudsondcd08fb2013-11-22 09:25:16 -06002405 DEBUG_INFO("SFE IPv4 exit\n");
Dave Hudson87973cd2013-10-22 16:00:04 +01002406 /*
2407 * Destroy all connections.
2408 */
Dave Hudsondcd08fb2013-11-22 09:25:16 -06002409 sfe_ipv4_destroy_all_rules_for_dev(NULL);
Dave Hudson87973cd2013-10-22 16:00:04 +01002410
Ken Zhu137722d2021-09-23 17:57:36 -07002411 cancel_delayed_work_sync(&si->sync_dwork);
Dave Hudson87973cd2013-10-22 16:00:04 +01002412
Dave Hudson87973cd2013-10-22 16:00:04 +01002413 unregister_chrdev(si->debug_dev, "sfe_ipv4");
2414
Amitesh Anand63be37d2021-12-24 20:51:48 +05302415#ifdef SFE_PROCESS_LOCAL_OUT
2416 DEBUG_TRACE("sfe: Unregister local out hook\n");
2417#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
2418 nf_unregister_hooks(sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2419#else
2420 nf_unregister_net_hooks(&init_net, sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2421#endif
2422#endif
2423
Xiaoping Fan640faf42015-08-28 15:50:55 -07002424#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302425 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002426#endif /* CONFIG_NF_FLOW_COOKIE */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302427 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_debug_dev_attr.attr);
2428 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_cpu_attr.attr);
Dave Hudson87973cd2013-10-22 16:00:04 +01002429
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302430 kobject_put(si->sys_ipv4);
Dave Hudson87973cd2013-10-22 16:00:04 +01002431
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302432 free_percpu(si->stats_pcpu);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002433}
2434
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08002435#ifdef CONFIG_NF_FLOW_COOKIE
2436EXPORT_SYMBOL(sfe_register_flow_cookie_cb);
2437EXPORT_SYMBOL(sfe_unregister_flow_cookie_cb);
2438#endif