blob: b5416d04250c2221f29e36f82cb1ad46fd73d19f [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>
Parikshit Guned31a8202022-01-05 22:15:04 +053035#include <linux/seqlock.h>
Nitin Shettye6ed5b52021-12-27 14:50:11 +053036#include <net/protocol.h>
37#include <net/gre.h>
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010038
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +053039#include "sfe_debug.h"
Ratheesh Kannoth89302a72021-10-20 08:10:37 +053040#include "sfe_api.h"
Dave Hudsondcd08fb2013-11-22 09:25:16 -060041#include "sfe.h"
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +053042#include "sfe_flow_cookie.h"
43#include "sfe_ipv4.h"
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +053044#include "sfe_ipv4_udp.h"
45#include "sfe_ipv4_tcp.h"
46#include "sfe_ipv4_icmp.h"
Wayne Tanbb7f1782021-12-13 11:16:04 -080047#include "sfe_pppoe.h"
Nitin Shettye6ed5b52021-12-27 14:50:11 +053048#include "sfe_ipv4_gre.h"
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010049
50static char *sfe_ipv4_exception_events_string[SFE_IPV4_EXCEPTION_EVENT_LAST] = {
51 "UDP_HEADER_INCOMPLETE",
52 "UDP_NO_CONNECTION",
53 "UDP_IP_OPTIONS_OR_INITIAL_FRAGMENT",
54 "UDP_SMALL_TTL",
55 "UDP_NEEDS_FRAGMENTATION",
56 "TCP_HEADER_INCOMPLETE",
57 "TCP_NO_CONNECTION_SLOW_FLAGS",
58 "TCP_NO_CONNECTION_FAST_FLAGS",
59 "TCP_IP_OPTIONS_OR_INITIAL_FRAGMENT",
60 "TCP_SMALL_TTL",
61 "TCP_NEEDS_FRAGMENTATION",
62 "TCP_FLAGS",
63 "TCP_SEQ_EXCEEDS_RIGHT_EDGE",
64 "TCP_SMALL_DATA_OFFS",
65 "TCP_BAD_SACK",
66 "TCP_BIG_DATA_OFFS",
67 "TCP_SEQ_BEFORE_LEFT_EDGE",
68 "TCP_ACK_EXCEEDS_RIGHT_EDGE",
69 "TCP_ACK_BEFORE_LEFT_EDGE",
70 "ICMP_HEADER_INCOMPLETE",
71 "ICMP_UNHANDLED_TYPE",
72 "ICMP_IPV4_HEADER_INCOMPLETE",
73 "ICMP_IPV4_NON_V4",
74 "ICMP_IPV4_IP_OPTIONS_INCOMPLETE",
75 "ICMP_IPV4_UDP_HEADER_INCOMPLETE",
76 "ICMP_IPV4_TCP_HEADER_INCOMPLETE",
77 "ICMP_IPV4_UNHANDLED_PROTOCOL",
78 "ICMP_NO_CONNECTION",
79 "ICMP_FLUSHED_CONNECTION",
80 "HEADER_INCOMPLETE",
Ratheesh Kannoth43d64f82021-10-20 08:23:29 +053081 "HEADER_CSUM_BAD",
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010082 "BAD_TOTAL_LENGTH",
83 "NON_V4",
84 "NON_INITIAL_FRAGMENT",
85 "DATAGRAM_INCOMPLETE",
86 "IP_OPTIONS_INCOMPLETE",
Ratheesh Kannoth5dee3772022-01-18 11:27:14 +053087 "UNHANDLED_PROTOCOL",
Nitin Shetty16ab38d2022-02-09 01:26:19 +053088 "NO_HEADROOM",
89 "INVALID_PPPOE_SESSION",
90 "INCORRECT_PPPOE_PARSING",
91 "PPPOE_NOT_SET_IN_CME",
92 "INGRESS_VLAN_TAG_MISMATCH",
Ratheesh Kannoth5dee3772022-01-18 11:27:14 +053093 "INVALID_SOURCE_INTERFACE",
Nitin Shettye6ed5b52021-12-27 14:50:11 +053094 "GRE_HEADER_INCOMPLETE",
95 "GRE_NO_CONNECTION",
96 "GRE_IP_OPTIONS_OR_INITIAL_FRAGMENT",
97 "GRE_SMALL_TTL",
98 "GRE_NEEDS_FRAGMENTATION"
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010099};
100
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700101static struct sfe_ipv4 __si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100102
103/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100104 * sfe_ipv4_gen_ip_csum()
105 * Generate the IP checksum for an IPv4 header.
106 *
107 * Note that this function assumes that we have only 20 bytes of IP header.
108 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530109u16 sfe_ipv4_gen_ip_csum(struct iphdr *iph)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100110{
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700111 u32 sum;
112 u16 *i = (u16 *)iph;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100113
114 iph->check = 0;
115
116 /*
117 * Generate the sum.
118 */
119 sum = i[0] + i[1] + i[2] + i[3] + i[4] + i[5] + i[6] + i[7] + i[8] + i[9];
120
121 /*
122 * Fold it to ones-complement form.
123 */
124 sum = (sum & 0xffff) + (sum >> 16);
125 sum = (sum & 0xffff) + (sum >> 16);
126
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700127 return (u16)sum ^ 0xffff;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100128}
129
130/*
131 * sfe_ipv4_get_connection_match_hash()
132 * Generate the hash used in connection match lookups.
133 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700134static inline unsigned int sfe_ipv4_get_connection_match_hash(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 Kannoth5dee3772022-01-18 11:27:14 +0530138 u32 hash = ntohl(src_ip ^ dest_ip) ^ protocol ^ ntohs(src_port ^ dest_port);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100139 return ((hash >> SFE_IPV4_CONNECTION_HASH_SHIFT) ^ hash) & SFE_IPV4_CONNECTION_HASH_MASK;
140}
141
142/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530143 * sfe_ipv4_find_connection_match_rcu()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100144 * Get the IPv4 flow match info that corresponds to a particular 5-tuple.
145 *
146 * On entry we must be holding the lock that protects the hash table.
147 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530148struct sfe_ipv4_connection_match *
149sfe_ipv4_find_connection_match_rcu(struct sfe_ipv4 *si, struct net_device *dev, u8 protocol,
Dave Hudson87973cd2013-10-22 16:00:04 +0100150 __be32 src_ip, __be16 src_port,
151 __be32 dest_ip, __be16 dest_port)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100152{
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530153 struct sfe_ipv4_connection_match *cm = NULL;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100154 unsigned int conn_match_idx;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530155 struct hlist_head *lhead;
156
157 WARN_ON_ONCE(!rcu_read_lock_held());
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100158
159 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 +0100160
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530161 lhead = &si->hlist_conn_match_hash_head[conn_match_idx];
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100162
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530163 hlist_for_each_entry_rcu(cm, lhead, hnode) {
164 if (cm->match_src_port != src_port
165 || cm->match_dest_port != dest_port
166 || cm->match_src_ip != src_ip
167 || cm->match_dest_ip != dest_ip
Ratheesh Kannoth5dee3772022-01-18 11:27:14 +0530168 || cm->match_protocol != protocol) {
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530169 continue;
170 }
171
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530172 this_cpu_inc(si->stats_pcpu->connection_match_hash_hits64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100173
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530174 break;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100175 }
176
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100177 return cm;
178}
179
180/*
181 * sfe_ipv4_connection_match_update_summary_stats()
182 * Update the summary stats for a connection match entry.
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530183 *
184 * Stats are incremented atomically. So use atomic substraction to update summary
185 * stats.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100186 */
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530187static inline void sfe_ipv4_connection_match_update_summary_stats(struct sfe_ipv4_connection_match *cm,
188 u32 *packets, u32 *bytes)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100189{
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530190 u32 packet_count, byte_count;
191
192 packet_count = atomic_read(&cm->rx_packet_count);
193 cm->rx_packet_count64 += packet_count;
194 atomic_sub(packet_count, &cm->rx_packet_count);
195
196 byte_count = atomic_read(&cm->rx_byte_count);
197 cm->rx_byte_count64 += byte_count;
198 atomic_sub(byte_count, &cm->rx_byte_count);
199
200 *packets = packet_count;
201 *bytes = byte_count;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100202}
203
204/*
205 * sfe_ipv4_connection_match_compute_translations()
206 * Compute port and address translations for a connection match entry.
207 */
208static void sfe_ipv4_connection_match_compute_translations(struct sfe_ipv4_connection_match *cm)
209{
210 /*
211 * Before we insert the entry look to see if this is tagged as doing address
212 * translations. If it is then work out the adjustment that we need to apply
213 * to the transport checksum.
214 */
215 if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC) {
216 /*
217 * Precompute an incremental checksum adjustment so we can
218 * edit packets in this stream very quickly. The algorithm is from RFC1624.
219 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700220 u16 src_ip_hi = cm->match_src_ip >> 16;
221 u16 src_ip_lo = cm->match_src_ip & 0xffff;
222 u32 xlate_src_ip = ~cm->xlate_src_ip;
223 u16 xlate_src_ip_hi = xlate_src_ip >> 16;
224 u16 xlate_src_ip_lo = xlate_src_ip & 0xffff;
225 u16 xlate_src_port = ~cm->xlate_src_port;
226 u32 adj;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100227
228 /*
229 * When we compute this fold it down to a 16-bit offset
230 * as that way we can avoid having to do a double
231 * folding of the twos-complement result because the
232 * addition of 2 16-bit values cannot cause a double
233 * wrap-around!
234 */
235 adj = src_ip_hi + src_ip_lo + cm->match_src_port
236 + xlate_src_ip_hi + xlate_src_ip_lo + xlate_src_port;
237 adj = (adj & 0xffff) + (adj >> 16);
238 adj = (adj & 0xffff) + (adj >> 16);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700239 cm->xlate_src_csum_adjustment = (u16)adj;
Nicolas Costaac2979c2014-01-14 10:35:24 -0600240
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100241 }
242
243 if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST) {
244 /*
245 * Precompute an incremental checksum adjustment so we can
246 * edit packets in this stream very quickly. The algorithm is from RFC1624.
247 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700248 u16 dest_ip_hi = cm->match_dest_ip >> 16;
249 u16 dest_ip_lo = cm->match_dest_ip & 0xffff;
250 u32 xlate_dest_ip = ~cm->xlate_dest_ip;
251 u16 xlate_dest_ip_hi = xlate_dest_ip >> 16;
252 u16 xlate_dest_ip_lo = xlate_dest_ip & 0xffff;
253 u16 xlate_dest_port = ~cm->xlate_dest_port;
254 u32 adj;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100255
256 /*
257 * When we compute this fold it down to a 16-bit offset
258 * as that way we can avoid having to do a double
259 * folding of the twos-complement result because the
260 * addition of 2 16-bit values cannot cause a double
261 * wrap-around!
262 */
263 adj = dest_ip_hi + dest_ip_lo + cm->match_dest_port
264 + xlate_dest_ip_hi + xlate_dest_ip_lo + xlate_dest_port;
265 adj = (adj & 0xffff) + (adj >> 16);
266 adj = (adj & 0xffff) + (adj >> 16);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700267 cm->xlate_dest_csum_adjustment = (u16)adj;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100268 }
Xiaoping Fanad755af2015-04-01 16:58:46 -0700269
270 if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC) {
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700271 u32 adj = ~cm->match_src_ip + cm->xlate_src_ip;
Xiaoping Fanad755af2015-04-01 16:58:46 -0700272 if (adj < cm->xlate_src_ip) {
273 adj++;
274 }
275
276 adj = (adj & 0xffff) + (adj >> 16);
277 adj = (adj & 0xffff) + (adj >> 16);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700278 cm->xlate_src_partial_csum_adjustment = (u16)adj;
Xiaoping Fanad755af2015-04-01 16:58:46 -0700279 }
280
281 if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST) {
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700282 u32 adj = ~cm->match_dest_ip + cm->xlate_dest_ip;
Xiaoping Fanad755af2015-04-01 16:58:46 -0700283 if (adj < cm->xlate_dest_ip) {
284 adj++;
285 }
286
287 adj = (adj & 0xffff) + (adj >> 16);
288 adj = (adj & 0xffff) + (adj >> 16);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700289 cm->xlate_dest_partial_csum_adjustment = (u16)adj;
Xiaoping Fanad755af2015-04-01 16:58:46 -0700290 }
291
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100292}
293
294/*
295 * sfe_ipv4_update_summary_stats()
296 * Update the summary stats.
297 */
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530298static void sfe_ipv4_update_summary_stats(struct sfe_ipv4 *si, struct sfe_ipv4_stats *stats)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100299{
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530300 int i = 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100301
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530302 memset(stats, 0, sizeof(*stats));
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100303
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530304 for_each_possible_cpu(i) {
305 const struct sfe_ipv4_stats *s = per_cpu_ptr(si->stats_pcpu, i);
306
307 stats->connection_create_requests64 += s->connection_create_requests64;
308 stats->connection_create_collisions64 += s->connection_create_collisions64;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530309 stats->connection_create_failures64 += s->connection_create_failures64;
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530310 stats->connection_destroy_requests64 += s->connection_destroy_requests64;
311 stats->connection_destroy_misses64 += s->connection_destroy_misses64;
312 stats->connection_match_hash_hits64 += s->connection_match_hash_hits64;
313 stats->connection_match_hash_reorders64 += s->connection_match_hash_reorders64;
314 stats->connection_flushes64 += s->connection_flushes64;
Amitesh Anand63be37d2021-12-24 20:51:48 +0530315 stats->packets_dropped64 += s->packets_dropped64;
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530316 stats->packets_forwarded64 += s->packets_forwarded64;
Ken Zhu7e38d1a2021-11-30 17:31:46 -0800317 stats->packets_fast_xmited64 += s->packets_fast_xmited64;
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530318 stats->packets_not_forwarded64 += s->packets_not_forwarded64;
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +0530319 stats->pppoe_encap_packets_forwarded64 += s->pppoe_encap_packets_forwarded64;
320 stats->pppoe_decap_packets_forwarded64 += s->pppoe_decap_packets_forwarded64;
Guduri Prathyusha034d6352022-01-12 16:49:04 +0530321 stats->pppoe_bridge_packets_forwarded64 += s->pppoe_bridge_packets_forwarded64;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100322 }
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530323
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100324}
325
326/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530327 * sfe_ipv4_insert_connection_match()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100328 * Insert a connection match into the hash.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100329 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530330static inline void sfe_ipv4_insert_connection_match(struct sfe_ipv4 *si,
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700331 struct sfe_ipv4_connection_match *cm)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100332{
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100333 unsigned int conn_match_idx
334 = sfe_ipv4_get_connection_match_hash(cm->match_dev, cm->match_protocol,
335 cm->match_src_ip, cm->match_src_port,
336 cm->match_dest_ip, cm->match_dest_port);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700337
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530338 lockdep_assert_held(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100339
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530340 hlist_add_head_rcu(&cm->hnode, &si->hlist_conn_match_hash_head[conn_match_idx]);
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800341#ifdef CONFIG_NF_FLOW_COOKIE
Xiaoping Fan640faf42015-08-28 15:50:55 -0700342 if (!si->flow_cookie_enable)
343 return;
344
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800345 /*
346 * Configure hardware to put a flow cookie in packet of this flow,
347 * then we can accelerate the lookup process when we received this packet.
348 */
349 for (conn_match_idx = 1; conn_match_idx < SFE_FLOW_COOKIE_SIZE; conn_match_idx++) {
350 struct sfe_flow_cookie_entry *entry = &si->sfe_flow_cookie_table[conn_match_idx];
351
352 if ((NULL == entry->match) && time_is_before_jiffies(entry->last_clean_time + HZ)) {
353 flow_cookie_set_func_t func;
354
355 rcu_read_lock();
356 func = rcu_dereference(si->flow_cookie_set_func);
357 if (func) {
Xiaoping Fan59176422015-05-22 15:58:10 -0700358 if (!func(cm->match_protocol, cm->match_src_ip, cm->match_src_port,
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800359 cm->match_dest_ip, cm->match_dest_port, conn_match_idx)) {
360 entry->match = cm;
361 cm->flow_cookie = conn_match_idx;
362 }
363 }
364 rcu_read_unlock();
365
366 break;
367 }
368 }
369#endif
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100370}
371
372/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530373 * sfe_ipv4_remove_connection_match()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100374 * Remove a connection match object from the hash.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100375 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530376static inline void sfe_ipv4_remove_connection_match(struct sfe_ipv4 *si, struct sfe_ipv4_connection_match *cm)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100377{
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530378
379 lockdep_assert_held(&si->lock);
380
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800381#ifdef CONFIG_NF_FLOW_COOKIE
Xiaoping Fan640faf42015-08-28 15:50:55 -0700382 if (si->flow_cookie_enable) {
383 /*
384 * Tell hardware that we no longer need a flow cookie in packet of this flow
385 */
386 unsigned int conn_match_idx;
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800387
Xiaoping Fan640faf42015-08-28 15:50:55 -0700388 for (conn_match_idx = 1; conn_match_idx < SFE_FLOW_COOKIE_SIZE; conn_match_idx++) {
389 struct sfe_flow_cookie_entry *entry = &si->sfe_flow_cookie_table[conn_match_idx];
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800390
Xiaoping Fan640faf42015-08-28 15:50:55 -0700391 if (cm == entry->match) {
392 flow_cookie_set_func_t func;
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800393
Xiaoping Fan640faf42015-08-28 15:50:55 -0700394 rcu_read_lock();
395 func = rcu_dereference(si->flow_cookie_set_func);
396 if (func) {
397 func(cm->match_protocol, cm->match_src_ip, cm->match_src_port,
398 cm->match_dest_ip, cm->match_dest_port, 0);
399 }
400 rcu_read_unlock();
401
402 cm->flow_cookie = 0;
403 entry->match = NULL;
404 entry->last_clean_time = jiffies;
405 break;
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800406 }
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800407 }
408 }
409#endif
410
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530411 hlist_del_init_rcu(&cm->hnode);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100412
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100413}
414
415/*
416 * sfe_ipv4_get_connection_hash()
417 * Generate the hash used in connection lookups.
418 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700419static inline unsigned int sfe_ipv4_get_connection_hash(u8 protocol, __be32 src_ip, __be16 src_port,
Dave Hudson87973cd2013-10-22 16:00:04 +0100420 __be32 dest_ip, __be16 dest_port)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100421{
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700422 u32 hash = ntohl(src_ip ^ dest_ip) ^ protocol ^ ntohs(src_port ^ dest_port);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100423 return ((hash >> SFE_IPV4_CONNECTION_HASH_SHIFT) ^ hash) & SFE_IPV4_CONNECTION_HASH_MASK;
424}
425
426/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530427 * sfe_ipv4_find_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100428 * Get the IPv4 connection info that corresponds to a particular 5-tuple.
429 *
430 * On entry we must be holding the lock that protects the hash table.
431 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530432static inline struct sfe_ipv4_connection *sfe_ipv4_find_connection(struct sfe_ipv4 *si, u32 protocol,
Dave Hudson87973cd2013-10-22 16:00:04 +0100433 __be32 src_ip, __be16 src_port,
434 __be32 dest_ip, __be16 dest_port)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100435{
436 struct sfe_ipv4_connection *c;
437 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 +0530438
439 lockdep_assert_held(&si->lock);
440
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100441 c = si->conn_hash[conn_idx];
442
443 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100444 * Will need connection entry for next create/destroy metadata,
445 * So no need to re-order entry for these requests
446 */
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530447 while (c) {
448 if ((c->src_port == src_port)
449 && (c->dest_port == dest_port)
450 && (c->src_ip == src_ip)
451 && (c->dest_ip == dest_ip)
452 && (c->protocol == protocol)) {
453 return c;
454 }
455
456 c = c->next;
457 }
458
459 return NULL;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100460}
461
462/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530463 * sfe_ipv4_insert_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100464 * Insert a connection into the hash.
465 *
466 * On entry we must be holding the lock that protects the hash table.
467 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530468static void sfe_ipv4_insert_connection(struct sfe_ipv4 *si, struct sfe_ipv4_connection *c)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100469{
470 struct sfe_ipv4_connection **hash_head;
471 struct sfe_ipv4_connection *prev_head;
472 unsigned int conn_idx;
473
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530474 lockdep_assert_held(&si->lock);
475
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100476 /*
477 * Insert entry into the connection hash.
478 */
479 conn_idx = sfe_ipv4_get_connection_hash(c->protocol, c->src_ip, c->src_port,
480 c->dest_ip, c->dest_port);
481 hash_head = &si->conn_hash[conn_idx];
482 prev_head = *hash_head;
483 c->prev = NULL;
484 if (prev_head) {
485 prev_head->prev = c;
486 }
487
488 c->next = prev_head;
489 *hash_head = c;
490
491 /*
492 * Insert entry into the "all connections" list.
493 */
494 if (si->all_connections_tail) {
495 c->all_connections_prev = si->all_connections_tail;
496 si->all_connections_tail->all_connections_next = c;
497 } else {
498 c->all_connections_prev = NULL;
499 si->all_connections_head = c;
500 }
501
502 si->all_connections_tail = c;
503 c->all_connections_next = NULL;
504 si->num_connections++;
505
506 /*
507 * Insert the connection match objects too.
508 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530509 sfe_ipv4_insert_connection_match(si, c->original_match);
510 sfe_ipv4_insert_connection_match(si, c->reply_match);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100511}
512
513/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530514 * sfe_ipv4_remove_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100515 * Remove a sfe_ipv4_connection object from the hash.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100516 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530517bool sfe_ipv4_remove_connection(struct sfe_ipv4 *si, struct sfe_ipv4_connection *c)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100518{
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530519 lockdep_assert_held(&si->lock);
520
521 if (c->removed) {
522 DEBUG_ERROR("%px: Connection has been removed already\n", c);
523 return false;
524 }
525
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100526 /*
527 * Remove the connection match objects.
528 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530529 sfe_ipv4_remove_connection_match(si, c->reply_match);
530 sfe_ipv4_remove_connection_match(si, c->original_match);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100531
532 /*
533 * Unlink the connection.
534 */
535 if (c->prev) {
536 c->prev->next = c->next;
537 } else {
538 unsigned int conn_idx = sfe_ipv4_get_connection_hash(c->protocol, c->src_ip, c->src_port,
539 c->dest_ip, c->dest_port);
540 si->conn_hash[conn_idx] = c->next;
541 }
542
543 if (c->next) {
544 c->next->prev = c->prev;
545 }
Xiaoping Fan34586472015-07-03 02:20:35 -0700546
547 /*
548 * Unlink connection from all_connections list
549 */
550 if (c->all_connections_prev) {
551 c->all_connections_prev->all_connections_next = c->all_connections_next;
552 } else {
553 si->all_connections_head = c->all_connections_next;
554 }
555
556 if (c->all_connections_next) {
557 c->all_connections_next->all_connections_prev = c->all_connections_prev;
558 } else {
559 si->all_connections_tail = c->all_connections_prev;
560 }
561
Ken Zhudc423672021-09-02 18:27:01 -0700562 /*
563 * If I am the next sync connection, move the sync to my next or head.
564 */
565 if (unlikely(si->wc_next == c)) {
566 si->wc_next = c->all_connections_next;
567 }
568
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530569 c->removed = true;
Xiaoping Fan34586472015-07-03 02:20:35 -0700570 si->num_connections--;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530571 return true;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100572}
573
574/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530575 * sfe_ipv4_gen_sync_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100576 * Sync a connection.
577 *
578 * On entry to this function we expect that the lock for the connection is either
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530579 * already held (while called from sfe_ipv4_periodic_sync() or isn't required
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530580 * (while called from sfe_ipv4_flush_connection())
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100581 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530582static void sfe_ipv4_gen_sync_connection(struct sfe_ipv4 *si, struct sfe_ipv4_connection *c,
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700583 struct sfe_connection_sync *sis, sfe_sync_reason_t reason,
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700584 u64 now_jiffies)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100585{
586 struct sfe_ipv4_connection_match *original_cm;
587 struct sfe_ipv4_connection_match *reply_cm;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530588 u32 packet_count, byte_count;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100589
590 /*
591 * Fill in the update message.
592 */
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700593 sis->is_v6 = 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100594 sis->protocol = c->protocol;
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700595 sis->src_ip.ip = c->src_ip;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700596 sis->src_ip_xlate.ip = c->src_ip_xlate;
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700597 sis->dest_ip.ip = c->dest_ip;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700598 sis->dest_ip_xlate.ip = c->dest_ip_xlate;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100599 sis->src_port = c->src_port;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700600 sis->src_port_xlate = c->src_port_xlate;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100601 sis->dest_port = c->dest_port;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700602 sis->dest_port_xlate = c->dest_port_xlate;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100603
604 original_cm = c->original_match;
605 reply_cm = c->reply_match;
606 sis->src_td_max_window = original_cm->protocol_state.tcp.max_win;
607 sis->src_td_end = original_cm->protocol_state.tcp.end;
608 sis->src_td_max_end = original_cm->protocol_state.tcp.max_end;
609 sis->dest_td_max_window = reply_cm->protocol_state.tcp.max_win;
610 sis->dest_td_end = reply_cm->protocol_state.tcp.end;
611 sis->dest_td_max_end = reply_cm->protocol_state.tcp.max_end;
612
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530613 sfe_ipv4_connection_match_update_summary_stats(original_cm, &packet_count, &byte_count);
614 sis->src_new_packet_count = packet_count;
615 sis->src_new_byte_count = byte_count;
Matthew McClintockd0cdb802014-02-24 16:30:35 -0600616
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530617 sfe_ipv4_connection_match_update_summary_stats(reply_cm, &packet_count, &byte_count);
618 sis->dest_new_packet_count = packet_count;
619 sis->dest_new_byte_count = byte_count;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100620
Matthew McClintockd0cdb802014-02-24 16:30:35 -0600621 sis->src_dev = original_cm->match_dev;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100622 sis->src_packet_count = original_cm->rx_packet_count64;
623 sis->src_byte_count = original_cm->rx_byte_count64;
Matthew McClintockd0cdb802014-02-24 16:30:35 -0600624
625 sis->dest_dev = reply_cm->match_dev;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100626 sis->dest_packet_count = reply_cm->rx_packet_count64;
627 sis->dest_byte_count = reply_cm->rx_byte_count64;
628
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700629 sis->reason = reason;
630
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100631 /*
632 * Get the time increment since our last sync.
633 */
634 sis->delta_jiffies = now_jiffies - c->last_sync_jiffies;
635 c->last_sync_jiffies = now_jiffies;
636}
637
638/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530639 * sfe_ipv4_free_connection_rcu()
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530640 * Called at RCU qs state to free the connection object.
641 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530642static void sfe_ipv4_free_connection_rcu(struct rcu_head *head)
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530643{
644 struct sfe_ipv4_connection *c;
Amitesh Anand63be37d2021-12-24 20:51:48 +0530645 struct udp_sock *up;
646 struct sock *sk;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530647
648 /*
649 * We dont need spin lock as the connection is already removed from link list
650 */
651 c = container_of(head, struct sfe_ipv4_connection, rcu);
652
653 BUG_ON(!c->removed);
654
655 DEBUG_TRACE("%px: connecton has been deleted\n", c);
656
657 /*
Amitesh Anand63be37d2021-12-24 20:51:48 +0530658 * Decrease the refcount taken in function sfe_ipv4_create_rule(),
659 * during call of __udp4_lib_lookup()
660 */
661 up = c->reply_match->up;
662 if (up) {
663 sk = (struct sock *)up;
664 sock_put(sk);
665 }
666
667 /*
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530668 * Release our hold of the source and dest devices and free the memory
669 * for our connection objects.
670 */
671 dev_put(c->original_dev);
672 dev_put(c->reply_dev);
673 kfree(c->original_match);
674 kfree(c->reply_match);
675 kfree(c);
676}
677
678/*
Ken Zhu88c58152021-12-09 15:12:06 -0800679 * sfe_ipv4_sync_status()
680 * update a connection status to its connection manager.
681 *
682 * si: the ipv4 context
683 * c: which connection to be notified
684 * reason: what kind of notification: flush, stats or destroy
685 */
686void sfe_ipv4_sync_status(struct sfe_ipv4 *si,
687 struct sfe_ipv4_connection *c,
688 sfe_sync_reason_t reason)
689{
690 struct sfe_connection_sync sis;
691 u64 now_jiffies;
692 sfe_sync_rule_callback_t sync_rule_callback;
693
694 rcu_read_lock();
695 sync_rule_callback = rcu_dereference(si->sync_rule_callback);
696 if (!sync_rule_callback) {
697 rcu_read_unlock();
698 return;
699 }
700
701 /*
702 * Generate a sync message and then sync.
703 */
704 now_jiffies = get_jiffies_64();
705 sfe_ipv4_gen_sync_connection(si, c, &sis, reason, now_jiffies);
706 sync_rule_callback(&sis);
707
708 rcu_read_unlock();
709}
710
711/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530712 * sfe_ipv4_flush_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100713 * Flush a connection and free all associated resources.
714 *
715 * We need to be called with bottom halves disabled locally as we need to acquire
716 * the connection hash lock and release it again. In general we're actually called
717 * from within a BH and so we're fine, but we're also called when connections are
718 * torn down.
719 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530720void sfe_ipv4_flush_connection(struct sfe_ipv4 *si,
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700721 struct sfe_ipv4_connection *c,
722 sfe_sync_reason_t reason)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100723{
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530724 BUG_ON(!c->removed);
725
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530726 this_cpu_inc(si->stats_pcpu->connection_flushes64);
Ken Zhu88c58152021-12-09 15:12:06 -0800727 sfe_ipv4_sync_status(si, c, reason);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100728
729 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100730 * Release our hold of the source and dest devices and free the memory
731 * for our connection objects.
732 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530733 call_rcu(&c->rcu, sfe_ipv4_free_connection_rcu);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100734}
735
736/*
Parikshit Guned31a8202022-01-05 22:15:04 +0530737 * sfe_ipv4_service_class_stats_inc()
738 * Increment per cpu per service class stats.
739 */
740void sfe_ipv4_service_class_stats_inc(struct sfe_ipv4 *si, uint8_t sid, uint64_t bytes)
741{
742 struct sfe_ipv4_service_class_stats_db *sc_stats_db = this_cpu_ptr(si->stats_pcpu_psc);
743 struct sfe_ipv4_per_service_class_stats *sc_stats = &sc_stats_db->psc_stats[sid];
744
745 write_seqcount_begin(&sc_stats->seq);
746 sc_stats->tx_bytes += bytes;
747 sc_stats->tx_packets++;
748 write_seqcount_end(&sc_stats->seq);
749}
750
751/*
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530752 * sfe_ipv4_exception_stats_inc()
753 * Increment exception stats.
754 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530755void sfe_ipv4_exception_stats_inc(struct sfe_ipv4 *si, enum sfe_ipv4_exception_events reason)
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530756{
757 struct sfe_ipv4_stats *stats = this_cpu_ptr(si->stats_pcpu);
758 stats->exception_events64[reason]++;
759 stats->packets_not_forwarded64++;
760}
761
762/*
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530763 * sfe_ipv4_is_loal_ip()
764 * Returns true if IP is local; returns false otherwise.
765 */
766static bool sfe_ipv4_is_local_ip(struct sfe_ipv4 *si, __be32 ip_addr)
767{
768 struct net_device *dev;
769
770 dev = ip_dev_find(&init_net, ip_addr);
771 if (dev) {
772 dev_put(dev);
773 return true;
774 }
775
776 return false;
777}
778
779/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100780 * sfe_ipv4_recv()
Matthew McClintocka8ad7962014-01-16 16:49:30 -0600781 * Handle packet receives and forwaring.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100782 *
783 * Returns 1 if the packet is forwarded or 0 if it isn't.
784 */
Amitesh Anand63be37d2021-12-24 20:51:48 +0530785int 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 +0100786{
787 struct sfe_ipv4 *si = &__si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100788 unsigned int len;
789 unsigned int tot_len;
790 unsigned int frag_off;
791 unsigned int ihl;
Ken Zhu88c58152021-12-09 15:12:06 -0800792 bool sync_on_find;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100793 bool ip_options;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530794 struct iphdr *iph;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700795 u32 protocol;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100796
797 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100798 * Check that we have space for an IP header here.
799 */
800 len = skb->len;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530801 if (unlikely(!pskb_may_pull(skb, sizeof(struct iphdr)))) {
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530802 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_HEADER_INCOMPLETE);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100803 DEBUG_TRACE("len: %u is too short\n", len);
804 return 0;
805 }
806
807 /*
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +0530808 * Validate ip csum if necessary. If ip_summed is set to CHECKSUM_UNNECESSARY, it is assumed
809 * that the L3 checksum is validated by the Rx interface or the tunnel interface that has
810 * generated the packet.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100811 */
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530812 iph = (struct iphdr *)skb->data;
Ratheesh Kannoth43d64f82021-10-20 08:23:29 +0530813 if (unlikely(skb->ip_summed != CHECKSUM_UNNECESSARY) && (ip_fast_csum((u8 *)iph, iph->ihl))) {
814 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_HEADER_CSUM_BAD);
815
816 DEBUG_TRACE("Bad IPv4 header csum: 0x%x\n", iph->check);
817 return 0;
818 }
819
820 /*
821 * Check that our "total length" is large enough for an IP header.
822 */
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100823 tot_len = ntohs(iph->tot_len);
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530824 if (unlikely(tot_len < sizeof(struct iphdr))) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100825
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530826 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_BAD_TOTAL_LENGTH);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100827 DEBUG_TRACE("tot_len: %u is too short\n", tot_len);
828 return 0;
829 }
830
831 /*
832 * Is our IP version wrong?
833 */
834 if (unlikely(iph->version != 4)) {
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530835 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_NON_V4);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100836 DEBUG_TRACE("IP version: %u\n", iph->version);
837 return 0;
838 }
839
840 /*
841 * Does our datagram fit inside the skb?
842 */
843 if (unlikely(tot_len > len)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100844 DEBUG_TRACE("tot_len: %u, exceeds len: %u\n", tot_len, len);
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530845 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_DATAGRAM_INCOMPLETE);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100846 return 0;
847 }
848
849 /*
850 * Do we have a non-initial fragment?
Nicolas Costaac2979c2014-01-14 10:35:24 -0600851 */
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100852 frag_off = ntohs(iph->frag_off);
853 if (unlikely(frag_off & IP_OFFSET)) {
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530854 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_NON_INITIAL_FRAGMENT);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100855 DEBUG_TRACE("non-initial fragment\n");
856 return 0;
857 }
858
859 /*
860 * If we have a (first) fragment then mark it to cause any connection to flush.
861 */
Ken Zhu88c58152021-12-09 15:12:06 -0800862 sync_on_find = unlikely(frag_off & IP_MF) ? true : false;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100863
864 /*
865 * Do we have any IP options? That's definite a slow path! If we do have IP
866 * options we need to recheck our header size.
867 */
868 ihl = iph->ihl << 2;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530869 ip_options = unlikely(ihl != sizeof(struct iphdr)) ? true : false;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100870 if (unlikely(ip_options)) {
871 if (unlikely(len < ihl)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100872
873 DEBUG_TRACE("len: %u is too short for header of size: %u\n", len, ihl);
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530874 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_IP_OPTIONS_INCOMPLETE);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100875 return 0;
876 }
877
Ken Zhu88c58152021-12-09 15:12:06 -0800878 sync_on_find = true;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100879 }
880
881 protocol = iph->protocol;
882 if (IPPROTO_UDP == protocol) {
Ken Zhu88c58152021-12-09 15:12:06 -0800883 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 +0100884 }
885
886 if (IPPROTO_TCP == protocol) {
Ken Zhu88c58152021-12-09 15:12:06 -0800887 return sfe_ipv4_recv_tcp(si, skb, dev, len, iph, ihl, sync_on_find, l2_info);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100888 }
889
890 if (IPPROTO_ICMP == protocol) {
891 return sfe_ipv4_recv_icmp(si, skb, dev, len, iph, ihl);
892 }
893
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530894#ifdef SFE_GRE_TUN_ENABLE
895 if (IPPROTO_GRE == protocol) {
Nitin Shetty2114a892022-01-28 20:03:56 +0530896 return sfe_ipv4_recv_gre(si, skb, dev, len, iph, ihl, sync_on_find, l2_info, tun_outer);
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530897 }
898#endif
899
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530900 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_UNHANDLED_PROTOCOL);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100901
902 DEBUG_TRACE("not UDP, TCP or ICMP: %u\n", protocol);
903 return 0;
904}
905
Nicolas Costa436926b2014-01-14 10:36:22 -0600906static void
907sfe_ipv4_update_tcp_state(struct sfe_ipv4_connection *c,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530908 struct sfe_ipv4_rule_create_msg *msg)
Nicolas Costa436926b2014-01-14 10:36:22 -0600909{
910 struct sfe_ipv4_connection_match *orig_cm;
911 struct sfe_ipv4_connection_match *repl_cm;
912 struct sfe_ipv4_tcp_connection_match *orig_tcp;
913 struct sfe_ipv4_tcp_connection_match *repl_tcp;
914
915 orig_cm = c->original_match;
916 repl_cm = c->reply_match;
917 orig_tcp = &orig_cm->protocol_state.tcp;
918 repl_tcp = &repl_cm->protocol_state.tcp;
919
920 /* update orig */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530921 if (orig_tcp->max_win < msg->tcp_rule.flow_max_window) {
922 orig_tcp->max_win = msg->tcp_rule.flow_max_window;
Nicolas Costa436926b2014-01-14 10:36:22 -0600923 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530924 if ((s32)(orig_tcp->end - msg->tcp_rule.flow_end) < 0) {
925 orig_tcp->end = msg->tcp_rule.flow_end;
Nicolas Costa436926b2014-01-14 10:36:22 -0600926 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530927 if ((s32)(orig_tcp->max_end - msg->tcp_rule.flow_max_end) < 0) {
928 orig_tcp->max_end = msg->tcp_rule.flow_max_end;
Nicolas Costa436926b2014-01-14 10:36:22 -0600929 }
930
931 /* update reply */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530932 if (repl_tcp->max_win < msg->tcp_rule.return_max_window) {
933 repl_tcp->max_win = msg->tcp_rule.return_max_window;
Nicolas Costa436926b2014-01-14 10:36:22 -0600934 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530935 if ((s32)(repl_tcp->end - msg->tcp_rule.return_end) < 0) {
936 repl_tcp->end = msg->tcp_rule.return_end;
Nicolas Costa436926b2014-01-14 10:36:22 -0600937 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530938 if ((s32)(repl_tcp->max_end - msg->tcp_rule.return_max_end) < 0) {
939 repl_tcp->max_end = msg->tcp_rule.return_max_end;
Nicolas Costa436926b2014-01-14 10:36:22 -0600940 }
941
942 /* update match flags */
943 orig_cm->flags &= ~SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
944 repl_cm->flags &= ~SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530945 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_NO_SEQ_CHECK) {
946
Nicolas Costa436926b2014-01-14 10:36:22 -0600947 orig_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
948 repl_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
949 }
950}
951
952static void
953sfe_ipv4_update_protocol_state(struct sfe_ipv4_connection *c,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530954 struct sfe_ipv4_rule_create_msg *msg)
Nicolas Costa436926b2014-01-14 10:36:22 -0600955{
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530956 switch (msg->tuple.protocol) {
Nicolas Costa436926b2014-01-14 10:36:22 -0600957 case IPPROTO_TCP:
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530958 sfe_ipv4_update_tcp_state(c, msg);
Nicolas Costa436926b2014-01-14 10:36:22 -0600959 break;
960 }
961}
962
Wayne Tanbb7f1782021-12-13 11:16:04 -0800963/*
964 * sfe_ipv4_match_entry_set_vlan()
965 */
966static void sfe_ipv4_match_entry_set_vlan(
967 struct sfe_ipv4_connection_match *cm,
968 u32 primary_ingress_vlan_tag,
969 u32 primary_egress_vlan_tag,
970 u32 secondary_ingress_vlan_tag,
971 u32 secondary_egress_vlan_tag)
972{
973 u16 tpid;
974 /*
975 * Prevent stacking header counts when updating.
976 */
977 cm->ingress_vlan_hdr_cnt = 0;
978 cm->egress_vlan_hdr_cnt = 0;
979 memset(cm->ingress_vlan_hdr, 0, sizeof(cm->ingress_vlan_hdr));
980 memset(cm->egress_vlan_hdr, 0, sizeof(cm->egress_vlan_hdr));
981
982 /*
983 * vlan_hdr[0] corresponds to outer tag
984 * vlan_hdr[1] corresponds to inner tag
985 * Extract the vlan information (tpid and tci) from rule message
986 */
987 if ((primary_ingress_vlan_tag & VLAN_VID_MASK) != SFE_VLAN_ID_NOT_CONFIGURED) {
988 tpid = (u16)(primary_ingress_vlan_tag >> 16);
989 cm->ingress_vlan_hdr[0].tpid = ntohs(tpid);
990 cm->ingress_vlan_hdr[0].tci = (u16)primary_ingress_vlan_tag;
991 cm->ingress_vlan_hdr_cnt++;
992 }
993
994 if ((secondary_ingress_vlan_tag & VLAN_VID_MASK) != SFE_VLAN_ID_NOT_CONFIGURED) {
995 tpid = (u16)(secondary_ingress_vlan_tag >> 16);
996 cm->ingress_vlan_hdr[1].tpid = ntohs(tpid);
997 cm->ingress_vlan_hdr[1].tci = (u16)secondary_ingress_vlan_tag;
998 cm->ingress_vlan_hdr_cnt++;
999 }
1000
1001 if ((primary_egress_vlan_tag & VLAN_VID_MASK) != SFE_VLAN_ID_NOT_CONFIGURED) {
1002 tpid = (u16)(primary_egress_vlan_tag >> 16);
1003 cm->egress_vlan_hdr[0].tpid = ntohs(tpid);
1004 cm->egress_vlan_hdr[0].tci = (u16)primary_egress_vlan_tag;
1005 cm->egress_vlan_hdr_cnt++;
1006 }
1007
1008 if ((secondary_egress_vlan_tag & VLAN_VID_MASK) != SFE_VLAN_ID_NOT_CONFIGURED) {
1009 tpid = (u16)(secondary_egress_vlan_tag >> 16);
1010 cm->egress_vlan_hdr[1].tpid = ntohs(tpid);
1011 cm->egress_vlan_hdr[1].tci = (u16)secondary_egress_vlan_tag;
1012 cm->egress_vlan_hdr_cnt++;
1013 }
1014}
1015
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301016void sfe_ipv4_update_rule(struct sfe_ipv4_rule_create_msg *msg)
Nicolas Costa436926b2014-01-14 10:36:22 -06001017{
1018 struct sfe_ipv4_connection *c;
1019 struct sfe_ipv4 *si = &__si;
1020
1021 spin_lock_bh(&si->lock);
1022
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301023 c = sfe_ipv4_find_connection(si,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301024 msg->tuple.protocol,
1025 msg->tuple.flow_ip,
1026 msg->tuple.flow_ident,
1027 msg->tuple.return_ip,
1028 msg->tuple.return_ident);
Nicolas Costa436926b2014-01-14 10:36:22 -06001029 if (c != NULL) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301030 sfe_ipv4_update_protocol_state(c, msg);
Nicolas Costa436926b2014-01-14 10:36:22 -06001031 }
1032
1033 spin_unlock_bh(&si->lock);
1034}
1035
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001036/*
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +05301037 * sfe_ipv4_xmit_eth_type_check()
1038 * Checking if MAC header has to be written.
1039 */
1040static inline bool sfe_ipv4_xmit_eth_type_check(struct net_device *dev, u32 cm_flags)
1041{
1042 if (!(dev->flags & IFF_NOARP)) {
1043 return true;
1044 }
1045
1046 /*
1047 * For PPPoE, since we are now supporting PPPoE encapsulation, we are writing L2 header.
1048 */
1049 if (unlikely(cm_flags & SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_ENCAP)) {
1050 return true;
1051 }
1052
1053 return false;
1054}
1055
1056/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001057 * sfe_ipv4_create_rule()
1058 * Create a forwarding rule.
1059 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301060int sfe_ipv4_create_rule(struct sfe_ipv4_rule_create_msg *msg)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001061{
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001062 struct sfe_ipv4 *si = &__si;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301063 struct sfe_ipv4_connection *c, *c_old;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001064 struct sfe_ipv4_connection_match *original_cm;
1065 struct sfe_ipv4_connection_match *reply_cm;
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001066 struct net_device *dest_dev;
1067 struct net_device *src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301068 struct sfe_ipv4_5tuple *tuple = &msg->tuple;
Suruchi Sumanc1a4a612021-10-21 14:50:23 +05301069 s32 flow_interface_num = msg->conn_rule.flow_top_interface_num;
1070 s32 return_interface_num = msg->conn_rule.return_top_interface_num;
Amitesh Anand63be37d2021-12-24 20:51:48 +05301071 struct net *net;
1072 struct sock *sk;
1073 unsigned int src_if_idx;
Parikshit Guned31a8202022-01-05 22:15:04 +05301074 u32 flow_sawf_tag;
1075 u32 return_sawf_tag;
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001076
Suruchi Sumanc1a4a612021-10-21 14:50:23 +05301077 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_FLOW_BOTTOM_INTERFACE) {
1078 flow_interface_num = msg->conn_rule.flow_interface_num;
1079 }
1080
1081 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_RETURN_BOTTOM_INTERFACE) {
1082 return_interface_num = msg->conn_rule.return_interface_num;
1083 }
1084
1085 src_dev = dev_get_by_index(&init_net, flow_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301086 if (!src_dev) {
1087 DEBUG_WARN("%px: Unable to find src_dev corresponding to %d\n", msg,
Suruchi Sumanc1a4a612021-10-21 14:50:23 +05301088 flow_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301089 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
1090 return -EINVAL;
1091 }
1092
Suruchi Sumanc1a4a612021-10-21 14:50:23 +05301093 dest_dev = dev_get_by_index(&init_net, return_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301094 if (!dest_dev) {
1095 DEBUG_WARN("%px: Unable to find dest_dev corresponding to %d\n", msg,
Suruchi Sumanc1a4a612021-10-21 14:50:23 +05301096 return_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301097 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
1098 dev_put(src_dev);
1099 return -EINVAL;
1100 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001101
Matthew McClintock389b42a2014-09-24 14:05:51 -05001102 if (unlikely((dest_dev->reg_state != NETREG_REGISTERED) ||
1103 (src_dev->reg_state != NETREG_REGISTERED))) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301104 dev_put(src_dev);
1105 dev_put(dest_dev);
1106 DEBUG_WARN("%px: src_dev=%s and dest_dev=%s are unregistered\n", msg,
1107 src_dev->name, dest_dev->name);
1108 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
Matthew McClintock389b42a2014-09-24 14:05:51 -05001109 return -EINVAL;
1110 }
1111
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301112 /*
1113 * Allocate the various connection tracking objects.
1114 */
1115 c = (struct sfe_ipv4_connection *)kmalloc(sizeof(struct sfe_ipv4_connection), GFP_ATOMIC);
1116 if (unlikely(!c)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301117 DEBUG_WARN("%px: memory allocation of connection entry failed\n", msg);
1118 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
1119 dev_put(src_dev);
1120 dev_put(dest_dev);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301121 return -ENOMEM;
1122 }
1123
1124 original_cm = (struct sfe_ipv4_connection_match *)kmalloc(sizeof(struct sfe_ipv4_connection_match), GFP_ATOMIC);
1125 if (unlikely(!original_cm)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301126 DEBUG_WARN("%px: memory allocation of connection match entry failed\n", msg);
1127 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301128 kfree(c);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301129 dev_put(src_dev);
1130 dev_put(dest_dev);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301131 return -ENOMEM;
1132 }
1133
1134 reply_cm = (struct sfe_ipv4_connection_match *)kmalloc(sizeof(struct sfe_ipv4_connection_match), GFP_ATOMIC);
1135 if (unlikely(!reply_cm)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301136 DEBUG_WARN("%px: memory allocation of connection match entry failed\n", msg);
1137 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301138 kfree(original_cm);
1139 kfree(c);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301140 dev_put(src_dev);
1141 dev_put(dest_dev);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301142 return -ENOMEM;
1143 }
1144
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301145 this_cpu_inc(si->stats_pcpu->connection_create_requests64);
1146
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001147 spin_lock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001148
1149 /*
Nicolas Costa436926b2014-01-14 10:36:22 -06001150 * Check to see if there is already a flow that matches the rule we're
1151 * trying to create. If there is then we can't create a new one.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001152 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301153 c_old = sfe_ipv4_find_connection(si,
Wayne Tanbb7f1782021-12-13 11:16:04 -08001154 msg->tuple.protocol,
1155 msg->tuple.flow_ip,
1156 msg->tuple.flow_ident,
1157 msg->tuple.return_ip,
1158 msg->tuple.return_ident);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301159
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301160 if (c_old != NULL) {
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301161 this_cpu_inc(si->stats_pcpu->connection_create_collisions64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001162
1163 /*
Nicolas Costa436926b2014-01-14 10:36:22 -06001164 * If we already have the flow then it's likely that this
1165 * request to create the connection rule contains more
1166 * up-to-date information. Check and update accordingly.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001167 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301168 sfe_ipv4_update_protocol_state(c, msg);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001169 spin_unlock_bh(&si->lock);
1170
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301171 kfree(reply_cm);
1172 kfree(original_cm);
1173 kfree(c);
1174
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301175 dev_put(src_dev);
1176 dev_put(dest_dev);
1177
Amitesh Anand63be37d2021-12-24 20:51:48 +05301178 DEBUG_TRACE("%px: connection already exists - p:%d\n"
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301179 " s: %s:%pM:%pI4:%u, d: %s:%pM:%pI4:%u\n",
Amitesh Anand63be37d2021-12-24 20:51:48 +05301180 msg, tuple->protocol,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301181 src_dev->name, msg->conn_rule.flow_mac, &tuple->flow_ip, ntohs(tuple->flow_ident),
1182 dest_dev->name, msg->conn_rule.return_mac, &tuple->return_ip, ntohs(tuple->return_ident));
1183
Nicolas Costa514fde02014-01-13 15:50:29 -06001184 return -EADDRINUSE;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001185 }
1186
1187 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001188 * Fill in the "original" direction connection matching object.
1189 * Note that the transmit MAC address is "dest_mac_xlate" because
1190 * we always know both ends of a connection by their translated
1191 * addresses and not their public addresses.
1192 */
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001193 original_cm->match_dev = src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301194 original_cm->match_protocol = tuple->protocol;
1195 original_cm->match_src_ip = tuple->flow_ip;
Suruchi Suman66609a72022-01-20 02:34:25 +05301196 original_cm->match_src_port = netif_is_vxlan(src_dev) ? 0 : tuple->flow_ident;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301197 original_cm->match_dest_ip = tuple->return_ip;
1198 original_cm->match_dest_port = tuple->return_ident;
1199
1200 original_cm->xlate_src_ip = msg->conn_rule.flow_ip_xlate;
1201 original_cm->xlate_src_port = msg->conn_rule.flow_ident_xlate;
1202 original_cm->xlate_dest_ip = msg->conn_rule.return_ip_xlate;
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301203 original_cm->xlate_dest_port = msg->conn_rule.return_ident_xlate;
1204
1205 if (tuple->protocol == IPPROTO_GRE) {
1206 /*
1207 * the PPTP is 4 tuple lookup.
1208 * During th rule lookup destination call id from packet
1209 * is matched against destination port in cm.
1210 */
1211 original_cm->match_src_port = 0;
1212 original_cm->xlate_src_port = 0;
1213 }
Wayne Tanbb7f1782021-12-13 11:16:04 -08001214
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301215 atomic_set(&original_cm->rx_packet_count, 0);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001216 original_cm->rx_packet_count64 = 0;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301217 atomic_set(&original_cm->rx_byte_count, 0);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001218 original_cm->rx_byte_count64 = 0;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301219
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001220 original_cm->xmit_dev = dest_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301221 original_cm->xmit_dev_mtu = msg->conn_rule.return_mtu;
1222
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001223 original_cm->connection = c;
1224 original_cm->counter_match = reply_cm;
Wayne Tanbb7f1782021-12-13 11:16:04 -08001225 original_cm->l2_hdr_size = 0;
1226 original_cm->flags = 0;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301227
Amitesh Anand63be37d2021-12-24 20:51:48 +05301228 /*
1229 * UDP Socket is valid only in decap direction.
1230 */
1231 RCU_INIT_POINTER(original_cm->up, NULL);
1232
Ken Zhu37040ea2021-09-09 21:11:15 -07001233 if (msg->valid_flags & SFE_RULE_CREATE_MARK_VALID) {
1234 original_cm->mark = msg->mark_rule.flow_mark;
1235 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_MARK;
1236 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301237 if (msg->valid_flags & SFE_RULE_CREATE_QOS_VALID) {
1238 original_cm->priority = msg->qos_rule.flow_qos_tag;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001239 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PRIORITY_REMARK;
1240 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301241 if (msg->valid_flags & SFE_RULE_CREATE_DSCP_MARKING_VALID) {
1242 original_cm->dscp = msg->dscp_rule.flow_dscp << SFE_IPV4_DSCP_SHIFT;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001243 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_DSCP_REMARK;
1244 }
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301245 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1246 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_BRIDGE_FLOW;
1247 }
Ken Zhu7e38d1a2021-11-30 17:31:46 -08001248 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_FLOW_TRANSMIT_FAST) {
1249 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_FAST_XMIT_DEV_ADMISSION;
1250 }
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301251
Wayne Tanbb7f1782021-12-13 11:16:04 -08001252 /*
Parikshit Guned31a8202022-01-05 22:15:04 +05301253 * Mark SAWF metadata if the sawf tag is valid and set.
1254 */
1255 original_cm->sawf_valid = false;
1256 flow_sawf_tag = SFE_GET_SAWF_TAG(msg->sawf_rule.flow_mark);
1257 if (likely(SFE_SAWF_TAG_IS_VALID(flow_sawf_tag))) {
1258 original_cm->mark = msg->sawf_rule.flow_mark;
1259 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_MARK;
1260 original_cm->sawf_valid = true;
1261 }
1262
1263 /*
Wayne Tanbb7f1782021-12-13 11:16:04 -08001264 * Add VLAN rule to original_cm
1265 */
1266 if (msg->valid_flags & SFE_RULE_CREATE_VLAN_VALID) {
1267 struct sfe_vlan_rule *vlan_primary_rule = &msg->vlan_primary_rule;
1268 struct sfe_vlan_rule *vlan_secondary_rule = &msg->vlan_secondary_rule;
1269 sfe_ipv4_match_entry_set_vlan(original_cm,
1270 vlan_primary_rule->ingress_vlan_tag,
1271 vlan_primary_rule->egress_vlan_tag,
1272 vlan_secondary_rule->ingress_vlan_tag,
1273 vlan_secondary_rule->egress_vlan_tag);
1274
1275 if ((msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_RETURN_BOTTOM_INTERFACE) &&
1276 original_cm->egress_vlan_hdr_cnt > 0) {
1277 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_INSERT_EGRESS_VLAN_TAG;
1278 original_cm->l2_hdr_size += original_cm->egress_vlan_hdr_cnt * VLAN_HLEN;
1279 }
1280 }
1281
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301282 if ((IPPROTO_GRE == tuple->protocol) && !sfe_ipv4_is_local_ip(si, original_cm->match_dest_ip)) {
1283 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PASSTHROUGH;
1284 }
1285
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001286#ifdef CONFIG_NF_FLOW_COOKIE
1287 original_cm->flow_cookie = 0;
1288#endif
Zhi Chen8748eb32015-06-18 12:58:48 -07001289#ifdef CONFIG_XFRM
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301290 if (msg->valid_flags & SFE_RULE_CREATE_DIRECTION_VALID) {
1291 original_cm->flow_accel = msg->direction_rule.flow_accel;
1292 } else {
1293 original_cm->flow_accel = 1;
1294 }
Zhi Chen8748eb32015-06-18 12:58:48 -07001295#endif
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301296 /*
1297 * If l2_features are disabled and flow uses l2 features such as macvlan/bridge/pppoe/vlan,
1298 * bottom interfaces are expected to be disabled in the flow rule and always top interfaces
1299 * are used. In such cases, do not use HW csum offload. csum offload is used only when we
1300 * are sending directly to the destination interface that supports it.
1301 */
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301302 if (likely(dest_dev->features & NETIF_F_HW_CSUM) && sfe_dev_has_hw_csum(dest_dev)) {
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301303 if ((msg->conn_rule.return_top_interface_num == msg->conn_rule.return_interface_num) ||
1304 (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_RETURN_BOTTOM_INTERFACE)) {
Ratheesh Kannoth48445532022-02-07 16:19:00 +05301305
1306 /*
1307 * Dont enable CSUM offload
1308 */
1309#if 0
Suruchi Sumanf2077182022-01-13 21:35:23 +05301310 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_CSUM_OFFLOAD;
Ratheesh Kannoth48445532022-02-07 16:19:00 +05301311#endif
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301312 }
1313 }
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001314
Wayne Tanbb7f1782021-12-13 11:16:04 -08001315 reply_cm->l2_hdr_size = 0;
Ratheesh Kannoth5dee3772022-01-18 11:27:14 +05301316 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_SRC_INTERFACE_CHECK) {
1317 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_SRC_INTERFACE_CHECK;
1318 }
1319
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +05301320 reply_cm->flags = 0;
1321
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001322 /*
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301323 * Adding PPPoE parameters to original and reply entries based on the direction where
1324 * PPPoE header is valid in ECM rule.
1325 *
1326 * If PPPoE is valid in flow direction (from interface is PPPoE), then
1327 * original cm will have PPPoE at ingress (strip PPPoE header)
1328 * reply cm will have PPPoE at egress (add PPPoE header)
1329 *
1330 * If PPPoE is valid in return direction (to interface is PPPoE), then
1331 * original cm will have PPPoE at egress (add PPPoE header)
1332 * reply cm will have PPPoE at ingress (strip PPPoE header)
1333 */
1334 if (msg->valid_flags & SFE_RULE_CREATE_PPPOE_DECAP_VALID) {
1335 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_DECAP;
1336 original_cm->pppoe_session_id = msg->pppoe_rule.flow_pppoe_session_id;
1337 ether_addr_copy(original_cm->pppoe_remote_mac, msg->pppoe_rule.flow_pppoe_remote_mac);
1338
1339 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_ENCAP;
Wayne Tanbb7f1782021-12-13 11:16:04 -08001340 reply_cm->l2_hdr_size += SFE_PPPOE_SESSION_HEADER_SIZE;
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301341 reply_cm->pppoe_session_id = msg->pppoe_rule.flow_pppoe_session_id;
1342 ether_addr_copy(reply_cm->pppoe_remote_mac, msg->pppoe_rule.flow_pppoe_remote_mac);
1343 }
1344
1345 if (msg->valid_flags & SFE_RULE_CREATE_PPPOE_ENCAP_VALID) {
1346 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_ENCAP;
Wayne Tanbb7f1782021-12-13 11:16:04 -08001347 original_cm->l2_hdr_size += SFE_PPPOE_SESSION_HEADER_SIZE;
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301348 original_cm->pppoe_session_id = msg->pppoe_rule.return_pppoe_session_id;
1349 ether_addr_copy(original_cm->pppoe_remote_mac, msg->pppoe_rule.return_pppoe_remote_mac);
1350
1351 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_DECAP;
1352 reply_cm->pppoe_session_id = msg->pppoe_rule.return_pppoe_session_id;
1353 ether_addr_copy(reply_cm->pppoe_remote_mac, msg->pppoe_rule.return_pppoe_remote_mac);
1354 }
1355
Ratheesh Kannoth5dee3772022-01-18 11:27:14 +05301356 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_SRC_INTERFACE_CHECK) {
1357 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_SRC_INTERFACE_CHECK;
1358 }
1359
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301360 /*
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(dest_dev, original_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 *)original_cm->xmit_src_mac, (u8 *)msg->conn_rule.flow_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_RETURN_VALID)) {
1375 ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)msg->src_mac_rule.return_src_mac);
1376 } else {
1377 ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)dest_dev->dev_addr);
1378 }
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301379 }
1380
1381 ether_addr_copy((u8 *)original_cm->xmit_dest_mac, (u8 *)msg->conn_rule.return_mac);
1382
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001383 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_L2_HDR;
Wayne Tanbb7f1782021-12-13 11:16:04 -08001384 original_cm->l2_hdr_size += ETH_HLEN;
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001385
1386 /*
1387 * If our dev writes Ethernet headers then we can write a really fast
1388 * version.
1389 */
1390 if (dest_dev->header_ops) {
1391 if (dest_dev->header_ops->create == eth_header) {
1392 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR;
1393 }
1394 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001395 }
1396
1397 /*
1398 * Fill in the "reply" direction connection matching object.
1399 */
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001400 reply_cm->match_dev = dest_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301401 reply_cm->match_protocol = tuple->protocol;
1402 reply_cm->match_src_ip = msg->conn_rule.return_ip_xlate;
Amitesh Anand63be37d2021-12-24 20:51:48 +05301403
1404 /*
1405 * Keep source port as 0 for VxLAN tunnels.
1406 */
1407 if (netif_is_vxlan(src_dev) || netif_is_vxlan(dest_dev)) {
1408 reply_cm->match_src_port = 0;
1409 } else {
1410 reply_cm->match_src_port = msg->conn_rule.return_ident_xlate;
1411 }
1412
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301413 reply_cm->match_dest_ip = msg->conn_rule.flow_ip_xlate;
1414 reply_cm->match_dest_port = msg->conn_rule.flow_ident_xlate;
1415
1416 reply_cm->xlate_src_ip = tuple->return_ip;
1417 reply_cm->xlate_src_port = tuple->return_ident;
1418 reply_cm->xlate_dest_ip = tuple->flow_ip;
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301419 reply_cm->xlate_dest_port = tuple->flow_ident;
1420
1421 if (tuple->protocol == IPPROTO_GRE) {
1422 /*
1423 * the PPTP is 4 tuple lookup.
1424 * During th rule lookup destination call id from packet
1425 * is matched against destination port in cm.
1426 */
1427 reply_cm->match_src_port = 0;
1428 reply_cm->xlate_src_port = 0;
1429 }
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301430
1431 atomic_set(&reply_cm->rx_packet_count, 0);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001432 reply_cm->rx_packet_count64 = 0;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301433 atomic_set(&reply_cm->rx_byte_count, 0);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001434 reply_cm->rx_byte_count64 = 0;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301435
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001436 reply_cm->xmit_dev = src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301437 reply_cm->xmit_dev_mtu = msg->conn_rule.flow_mtu;
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301438
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001439 reply_cm->connection = c;
1440 reply_cm->counter_match = original_cm;
Ken Zhu37040ea2021-09-09 21:11:15 -07001441
Ken Zhu37040ea2021-09-09 21:11:15 -07001442 if (msg->valid_flags & SFE_RULE_CREATE_MARK_VALID) {
1443 reply_cm->mark = msg->mark_rule.return_mark;
1444 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_MARK;
1445 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301446 if (msg->valid_flags & SFE_RULE_CREATE_QOS_VALID) {
1447 reply_cm->priority = msg->qos_rule.return_qos_tag;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001448 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PRIORITY_REMARK;
1449 }
Wayne Tanbb7f1782021-12-13 11:16:04 -08001450
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301451 if (msg->valid_flags & SFE_RULE_CREATE_DSCP_MARKING_VALID) {
1452 reply_cm->dscp = msg->dscp_rule.return_dscp << SFE_IPV4_DSCP_SHIFT;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001453 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_DSCP_REMARK;
1454 }
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301455 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1456 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_BRIDGE_FLOW;
1457 }
Ken Zhu7e38d1a2021-11-30 17:31:46 -08001458 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_RETURN_TRANSMIT_FAST) {
1459 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_FAST_XMIT_DEV_ADMISSION;
1460 }
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301461
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301462 if ((IPPROTO_GRE == tuple->protocol) && !sfe_ipv4_is_local_ip(si, reply_cm->match_dest_ip)) {
1463 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PASSTHROUGH;
1464 }
1465
Amitesh Anand63be37d2021-12-24 20:51:48 +05301466 /*
Parikshit Guned31a8202022-01-05 22:15:04 +05301467 * Mark SAWF metadata in reply match if the sawf tag is valid.
1468 */
1469 reply_cm->sawf_valid = false;
1470 return_sawf_tag = SFE_GET_SAWF_TAG(msg->sawf_rule.return_mark);
1471 if (likely(SFE_SAWF_TAG_IS_VALID(return_sawf_tag))) {
1472 reply_cm->mark = msg->sawf_rule.return_mark;
1473 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_MARK;
1474 reply_cm->sawf_valid = true;
1475 }
1476
1477 /*
Amitesh Anand63be37d2021-12-24 20:51:48 +05301478 * Setup UDP Socket if found to be valid for decap.
1479 */
1480 RCU_INIT_POINTER(reply_cm->up, NULL);
1481 net = dev_net(reply_cm->match_dev);
1482 src_if_idx = src_dev->ifindex;
1483
1484 rcu_read_lock();
1485
1486 /*
1487 * Look for the associated sock object.
1488 * __udp4_lib_lookup() holds a reference for this sock object,
1489 * which will be released in sfe_ipv4_free_connection_rcu()
1490 */
1491#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
1492 sk = __udp4_lib_lookup(net, reply_cm->match_dest_ip, reply_cm->match_dest_port,
1493 reply_cm->xlate_src_ip, reply_cm->xlate_src_port, src_if_idx, &udp_table);
1494#else
1495 sk = __udp4_lib_lookup(net, reply_cm->match_dest_ip, reply_cm->match_dest_port,
1496 reply_cm->xlate_src_ip, reply_cm->xlate_src_port, src_if_idx, 0, &udp_table, NULL);
1497#endif
1498
1499 rcu_read_unlock();
1500
1501 /*
1502 * We set the UDP sock pointer as valid only for decap direction.
1503 */
1504 if (sk && udp_sk(sk)->encap_type) {
1505#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
1506 if (!atomic_add_unless(&sk->sk_refcnt, 1, 0)) {
1507#else
1508 if (!refcount_inc_not_zero(&sk->sk_refcnt)) {
1509#endif
Wayne Tanbb7f1782021-12-13 11:16:04 -08001510 spin_unlock_bh(&si->lock);
Amitesh Anand63be37d2021-12-24 20:51:48 +05301511 kfree(reply_cm);
1512 kfree(original_cm);
1513 kfree(c);
1514
1515 DEBUG_TRACE("%px: sfe: unable to take reference for socket(%px) p:%d\n"
1516 " s: %s:%pM:%pI4:%u, d: %s:%pM:%pI4:%u\n",
1517 msg, sk, tuple->protocol,
1518 src_dev->name, msg->conn_rule.flow_mac, &tuple->flow_ip, ntohs(tuple->flow_ident),
1519 dest_dev->name, msg->conn_rule.return_mac, &tuple->return_ip, ntohs(tuple->return_ident));
1520
1521 dev_put(src_dev);
1522 dev_put(dest_dev);
1523
1524 return -ESHUTDOWN;
1525 }
1526
1527 rcu_assign_pointer(reply_cm->up, udp_sk(sk));
1528
1529 DEBUG_INFO("%px: Sock(%px) lookup success with reply_cm direction\n", msg, sk);
1530 DEBUG_INFO("%px: SFE connection -\n"
1531 " s: %s:%pI4(%pI4):%u(%u)\n"
1532 " d: %s:%pI4(%pI4):%u(%u)\n",
1533 msg, reply_cm->match_dev->name, &reply_cm->match_src_ip, &reply_cm->xlate_src_ip,
1534 ntohs(reply_cm->match_src_port), ntohs(reply_cm->xlate_src_port),
1535 reply_cm->xmit_dev->name, &reply_cm->match_dest_ip, &reply_cm->xlate_dest_ip,
1536 ntohs(reply_cm->match_dest_port), ntohs(reply_cm->xlate_dest_port));
1537 }
1538
Wayne Tanbb7f1782021-12-13 11:16:04 -08001539 /*
1540 * Add VLAN rule to reply_cm
1541 */
1542 if (msg->valid_flags & SFE_RULE_CREATE_VLAN_VALID) {
1543 struct sfe_vlan_rule *vlan_primary_rule = &msg->vlan_primary_rule;
1544 struct sfe_vlan_rule *vlan_secondary_rule = &msg->vlan_secondary_rule;
1545 sfe_ipv4_match_entry_set_vlan(reply_cm,
1546 vlan_primary_rule->egress_vlan_tag,
1547 vlan_primary_rule->ingress_vlan_tag,
1548 vlan_secondary_rule->egress_vlan_tag,
1549 vlan_secondary_rule->ingress_vlan_tag);
1550
1551 if ((msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_FLOW_BOTTOM_INTERFACE) &&
1552 reply_cm->egress_vlan_hdr_cnt > 0) {
1553 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_INSERT_EGRESS_VLAN_TAG;
1554 reply_cm->l2_hdr_size += reply_cm->egress_vlan_hdr_cnt * VLAN_HLEN;
1555 }
1556 }
1557
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301558 /*
1559 * the net_protocol handler will be used only in decap path
1560 * for non passthrough case.
1561 */
1562 original_cm->proto = NULL;
1563 reply_cm->proto = NULL;
1564
1565#ifdef SFE_GRE_TUN_ENABLE
1566 if ((IPPROTO_GRE == tuple->protocol) && !(reply_cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_PASSTHROUGH)) {
1567 rcu_read_lock();
1568 reply_cm->proto = rcu_dereference(inet_protos[IPPROTO_GRE]);
1569 rcu_read_unlock();
1570
1571 if (unlikely(!reply_cm->proto)) {
1572 kfree(reply_cm);
1573 kfree(original_cm);
1574 kfree(c);
1575 dev_put(src_dev);
1576 dev_put(dest_dev);
1577 DEBUG_WARN("sfe: GRE proto handler is not registered\n");
1578 return -EPERM;
1579 }
1580 }
1581#endif
1582
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001583#ifdef CONFIG_NF_FLOW_COOKIE
1584 reply_cm->flow_cookie = 0;
1585#endif
Zhi Chen8748eb32015-06-18 12:58:48 -07001586#ifdef CONFIG_XFRM
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301587 if (msg->valid_flags & SFE_RULE_CREATE_DIRECTION_VALID) {
1588 reply_cm->flow_accel = msg->direction_rule.return_accel;
1589 } else {
1590 reply_cm->flow_accel = 1;
1591 }
1592
Zhi Chen8748eb32015-06-18 12:58:48 -07001593#endif
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301594 /*
1595 * If l2_features are disabled and flow uses l2 features such as macvlan/bridge/pppoe/vlan,
1596 * bottom interfaces are expected to be disabled in the flow rule and always top interfaces
1597 * are used. In such cases, do not use HW csum offload. csum offload is used only when we
1598 * are sending directly to the destination interface that supports it.
1599 */
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301600 if (likely(src_dev->features & NETIF_F_HW_CSUM) && sfe_dev_has_hw_csum(src_dev)) {
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301601 if ((msg->conn_rule.flow_top_interface_num == msg->conn_rule.flow_interface_num) ||
1602 (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_FLOW_BOTTOM_INTERFACE)) {
Ratheesh Kannoth48445532022-02-07 16:19:00 +05301603 /*
1604 * Dont enable CSUM offload
1605 */
1606#if 0
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301607 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_CSUM_OFFLOAD;
Ratheesh Kannoth48445532022-02-07 16:19:00 +05301608#endif
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301609 }
1610 }
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001611
1612 /*
Ken Zhubbf49652021-09-12 15:33:09 -07001613 * For the non-arp interface, we don't write L2 HDR.
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001614 */
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +05301615 if (sfe_ipv4_xmit_eth_type_check(src_dev, reply_cm->flags)) {
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301616
1617 /*
1618 * Check whether the rule has configured a specific source MAC address to use.
1619 * This is needed when virtual L3 interfaces such as br-lan, macvlan, vlan are used during egress
1620 */
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301621
1622 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1623 ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)msg->conn_rule.return_mac);
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301624 } else {
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301625 if ((msg->valid_flags & SFE_RULE_CREATE_SRC_MAC_VALID) &&
1626 (msg->src_mac_rule.mac_valid_flags & SFE_SRC_MAC_FLOW_VALID)) {
1627 ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)msg->src_mac_rule.flow_src_mac);
1628 } else {
1629 ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)src_dev->dev_addr);
1630 }
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301631 }
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301632
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301633 ether_addr_copy((u8 *)reply_cm->xmit_dest_mac, (u8 *)msg->conn_rule.flow_mac);
1634
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001635 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_L2_HDR;
Wayne Tanbb7f1782021-12-13 11:16:04 -08001636 reply_cm->l2_hdr_size += ETH_HLEN;
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001637
1638 /*
1639 * If our dev writes Ethernet headers then we can write a really fast
1640 * version.
1641 */
1642 if (src_dev->header_ops) {
1643 if (src_dev->header_ops->create == eth_header) {
1644 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR;
1645 }
1646 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001647 }
1648
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301649 if ((tuple->return_ip != msg->conn_rule.return_ip_xlate) ||
1650 (tuple->return_ident != msg->conn_rule.return_ident_xlate)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001651 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST;
1652 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC;
1653 }
1654
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301655 if ((tuple->flow_ip != msg->conn_rule.flow_ip_xlate) ||
1656 (tuple->flow_ident != msg->conn_rule.flow_ident_xlate)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001657 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC;
1658 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST;
1659 }
1660
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001661 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001662 * Initialize the protocol-specific information that we track.
1663 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301664 switch (tuple->protocol) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001665 case IPPROTO_TCP:
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301666 original_cm->protocol_state.tcp.win_scale = msg->tcp_rule.flow_window_scale;
1667 original_cm->protocol_state.tcp.max_win = msg->tcp_rule.flow_max_window ? msg->tcp_rule.flow_max_window : 1;
1668 original_cm->protocol_state.tcp.end = msg->tcp_rule.flow_end;
1669 original_cm->protocol_state.tcp.max_end = msg->tcp_rule.flow_max_end;
1670
1671 reply_cm->protocol_state.tcp.win_scale = msg->tcp_rule.return_window_scale;
1672 reply_cm->protocol_state.tcp.max_win = msg->tcp_rule.return_max_window ? msg->tcp_rule.return_max_window : 1;
1673 reply_cm->protocol_state.tcp.end = msg->tcp_rule.return_end;
1674 reply_cm->protocol_state.tcp.max_end = msg->tcp_rule.return_max_end;
1675
1676 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_NO_SEQ_CHECK) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001677 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
1678 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
1679 }
1680 break;
1681 }
1682
Wayne Tanbb7f1782021-12-13 11:16:04 -08001683 /*
1684 * Fill in the ipv4_connection object.
1685 */
1686 c->protocol = tuple->protocol;
1687 c->src_ip = tuple->flow_ip;
1688 c->src_ip_xlate = msg->conn_rule.flow_ip_xlate;
1689 c->src_port = tuple->flow_ident;
1690 c->src_port_xlate = msg->conn_rule.flow_ident_xlate;
1691 c->original_dev = src_dev;
1692 c->original_match = original_cm;
1693 c->dest_ip = tuple->return_ip;
1694 c->dest_ip_xlate = msg->conn_rule.return_ip_xlate;
1695 c->dest_port = tuple->return_ident;
1696 c->dest_port_xlate = msg->conn_rule.return_ident_xlate;
1697 c->reply_dev = dest_dev;
1698 c->reply_match = reply_cm;
1699 c->debug_read_seq = 0;
1700 c->last_sync_jiffies = get_jiffies_64();
1701 c->removed = false;
1702
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001703 sfe_ipv4_connection_match_compute_translations(original_cm);
1704 sfe_ipv4_connection_match_compute_translations(reply_cm);
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301705 sfe_ipv4_insert_connection(si, c);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001706
1707 spin_unlock_bh(&si->lock);
1708
1709 /*
1710 * We have everything we need!
1711 */
Wayne Tanbb7f1782021-12-13 11:16:04 -08001712 DEBUG_INFO("NEW connection - p: %d\n"
1713 "original_cm: match_dev=src_dev: %s %d %pM\n"
1714 " xmit_dev=dest_dev: %s %d %pM\n"
1715 " xmit_src_mac: %pM\n"
1716 " xmit_dest_mac: %pM\n"
1717 " flags: %x l2_hdr: %u\n"
1718 "flow_ip: %pI4:%u\n"
1719 "flow_ip_xlate: %pI4:%u\n"
1720 "flow_mac: %pM\n"
1721 "reply_cm: match_dev=dest_dev: %s %d %pM\n"
1722 " xmit_dev=src_dev: %s %d %pM\n"
1723 " xmit_src_mac: %pM\n"
1724 " xmit_dest_mac: %pM\n"
1725 " flags: %x l2_hdr: %u\n"
1726 "return_ip: %pI4:%u\n"
1727 "return_ip_xlate: %pI4:%u\n"
1728 "return_mac: %pM\n"
1729 "flags: valid=%x src_mac_valid=%x\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301730 tuple->protocol,
Wayne Tanbb7f1782021-12-13 11:16:04 -08001731 original_cm->match_dev->name, original_cm->match_dev->ifindex, original_cm->match_dev->dev_addr,
1732 original_cm->xmit_dev->name, original_cm->xmit_dev->ifindex, original_cm->xmit_dev->dev_addr,
1733 original_cm->xmit_src_mac, original_cm->xmit_dest_mac, original_cm->flags, original_cm->l2_hdr_size,
1734 &tuple->flow_ip, ntohs(tuple->flow_ident),
1735 &msg->conn_rule.flow_ip_xlate, ntohs(msg->conn_rule.flow_ident_xlate),
1736 msg->conn_rule.flow_mac,
1737 reply_cm->match_dev->name, reply_cm->match_dev->ifindex, reply_cm->match_dev->dev_addr,
1738 reply_cm->xmit_dev->name, reply_cm->xmit_dev->ifindex, reply_cm->xmit_dev->dev_addr,
1739 reply_cm->xmit_src_mac, reply_cm->xmit_dest_mac, reply_cm->flags, reply_cm->l2_hdr_size,
1740 &tuple->return_ip, ntohs(tuple->return_ident),
1741 &msg->conn_rule.return_ip_xlate, ntohs(msg->conn_rule.return_ident_xlate),
1742 msg->conn_rule.return_mac,
1743 msg->valid_flags, msg->src_mac_rule.mac_valid_flags);
Nicolas Costa514fde02014-01-13 15:50:29 -06001744
1745 return 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001746}
1747
1748/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001749 * sfe_ipv4_destroy_rule()
1750 * Destroy a forwarding rule.
1751 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301752void sfe_ipv4_destroy_rule(struct sfe_ipv4_rule_destroy_msg *msg)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001753{
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001754 struct sfe_ipv4 *si = &__si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001755 struct sfe_ipv4_connection *c;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301756 bool ret;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301757 struct sfe_ipv4_5tuple *tuple = &msg->tuple;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001758
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301759 this_cpu_inc(si->stats_pcpu->connection_destroy_requests64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001760 spin_lock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001761
1762 /*
1763 * Check to see if we have a flow that matches the rule we're trying
1764 * to destroy. If there isn't then we can't destroy it.
1765 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301766 c = sfe_ipv4_find_connection(si, tuple->protocol, tuple->flow_ip, tuple->flow_ident,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301767 tuple->return_ip, tuple->return_ident);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001768 if (!c) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001769 spin_unlock_bh(&si->lock);
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301770 this_cpu_inc(si->stats_pcpu->connection_destroy_misses64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001771
1772 DEBUG_TRACE("connection does not exist - p: %d, s: %pI4:%u, d: %pI4:%u\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301773 tuple->protocol, &tuple->flow_ip, ntohs(tuple->flow_ident),
1774 &tuple->return_ip, ntohs(tuple->return_ident));
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001775 return;
1776 }
1777
1778 /*
1779 * Remove our connection details from the hash tables.
1780 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301781 ret = sfe_ipv4_remove_connection(si, c);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001782 spin_unlock_bh(&si->lock);
1783
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301784 if (ret) {
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301785 sfe_ipv4_flush_connection(si, c, SFE_SYNC_REASON_DESTROY);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301786 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001787
1788 DEBUG_INFO("connection destroyed - p: %d, s: %pI4:%u, d: %pI4:%u\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301789 tuple->protocol, &tuple->flow_ip, ntohs(tuple->flow_ident),
1790 &tuple->return_ip, ntohs(tuple->return_ident));
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001791}
1792
1793/*
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001794 * sfe_ipv4_register_sync_rule_callback()
1795 * Register a callback for rule synchronization.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001796 */
Xiaoping Fand44a5b42015-05-26 17:37:37 -07001797void sfe_ipv4_register_sync_rule_callback(sfe_sync_rule_callback_t sync_rule_callback)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001798{
1799 struct sfe_ipv4 *si = &__si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001800
1801 spin_lock_bh(&si->lock);
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001802 rcu_assign_pointer(si->sync_rule_callback, sync_rule_callback);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001803 spin_unlock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001804}
1805
1806/*
1807 * sfe_ipv4_get_debug_dev()
1808 */
1809static ssize_t sfe_ipv4_get_debug_dev(struct device *dev,
1810 struct device_attribute *attr,
1811 char *buf)
1812{
1813 struct sfe_ipv4 *si = &__si;
1814 ssize_t count;
1815 int num;
1816
1817 spin_lock_bh(&si->lock);
1818 num = si->debug_dev;
1819 spin_unlock_bh(&si->lock);
1820
1821 count = snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", num);
1822 return count;
1823}
1824
1825/*
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001826 * sysfs attributes.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001827 */
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001828static const struct device_attribute sfe_ipv4_debug_dev_attr =
Xiaoping Fane70da412016-02-26 16:47:57 -08001829 __ATTR(debug_dev, S_IWUSR | S_IRUGO, sfe_ipv4_get_debug_dev, NULL);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001830
1831/*
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001832 * sfe_ipv4_destroy_all_rules_for_dev()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001833 * Destroy all connections that match a particular device.
1834 *
1835 * If we pass dev as NULL then this destroys all connections.
1836 */
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001837void sfe_ipv4_destroy_all_rules_for_dev(struct net_device *dev)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001838{
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001839 struct sfe_ipv4 *si = &__si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001840 struct sfe_ipv4_connection *c;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301841 bool ret;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001842
Xiaoping Fan34586472015-07-03 02:20:35 -07001843another_round:
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001844 spin_lock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001845
Xiaoping Fan34586472015-07-03 02:20:35 -07001846 for (c = si->all_connections_head; c; c = c->all_connections_next) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001847 /*
Xiaoping Fan34586472015-07-03 02:20:35 -07001848 * Does this connection relate to the device we are destroying?
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001849 */
1850 if (!dev
1851 || (dev == c->original_dev)
1852 || (dev == c->reply_dev)) {
Xiaoping Fan34586472015-07-03 02:20:35 -07001853 break;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001854 }
Xiaoping Fan34586472015-07-03 02:20:35 -07001855 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001856
Xiaoping Fan34586472015-07-03 02:20:35 -07001857 if (c) {
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301858 ret = sfe_ipv4_remove_connection(si, c);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001859 }
1860
1861 spin_unlock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07001862
1863 if (c) {
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301864 if (ret) {
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301865 sfe_ipv4_flush_connection(si, c, SFE_SYNC_REASON_DESTROY);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301866 }
Xiaoping Fan34586472015-07-03 02:20:35 -07001867 goto another_round;
1868 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001869}
1870
1871/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001872 * sfe_ipv4_periodic_sync()
1873 */
Ken Zhu137722d2021-09-23 17:57:36 -07001874static void sfe_ipv4_periodic_sync(struct work_struct *work)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001875{
Ken Zhu137722d2021-09-23 17:57:36 -07001876 struct sfe_ipv4 *si = container_of((struct delayed_work *)work, struct sfe_ipv4, sync_dwork);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07001877 u64 now_jiffies;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001878 int quota;
Xiaoping Fand44a5b42015-05-26 17:37:37 -07001879 sfe_sync_rule_callback_t sync_rule_callback;
Ken Zhudc423672021-09-02 18:27:01 -07001880 struct sfe_ipv4_connection *c;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001881
1882 now_jiffies = get_jiffies_64();
1883
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001884 rcu_read_lock();
1885 sync_rule_callback = rcu_dereference(si->sync_rule_callback);
1886 if (!sync_rule_callback) {
1887 rcu_read_unlock();
1888 goto done;
1889 }
1890
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001891 spin_lock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001892
1893 /*
Ken Zhudc423672021-09-02 18:27:01 -07001894 * If we have reached the end of the connection list, walk from
1895 * the connection head.
1896 */
1897 c = si->wc_next;
1898 if (unlikely(!c)) {
1899 c = si->all_connections_head;
1900 }
1901
1902 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001903 * Get an estimate of the number of connections to parse in this sync.
1904 */
1905 quota = (si->num_connections + 63) / 64;
1906
1907 /*
Ken Zhudc423672021-09-02 18:27:01 -07001908 * Walk the "all connection" list and sync the connection state.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001909 */
Ken Zhudc423672021-09-02 18:27:01 -07001910 while (likely(c && quota)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001911 struct sfe_ipv4_connection_match *cm;
1912 struct sfe_ipv4_connection_match *counter_cm;
Xiaoping Fand44a5b42015-05-26 17:37:37 -07001913 struct sfe_connection_sync sis;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001914
Ken Zhudc423672021-09-02 18:27:01 -07001915 cm = c->original_match;
1916 counter_cm = c->reply_match;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001917
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001918 /*
Ken Zhudc423672021-09-02 18:27:01 -07001919 * Didn't receive packets in the original direction or reply
1920 * direction, move to the next connection.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001921 */
Ken Zhudc423672021-09-02 18:27:01 -07001922 if ((!atomic_read(&cm->rx_packet_count)) && !(atomic_read(&counter_cm->rx_packet_count))) {
1923 c = c->all_connections_next;
1924 continue;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001925 }
1926
Ken Zhudc423672021-09-02 18:27:01 -07001927 quota--;
Matthew McClintockaf48f1e2014-01-23 15:29:19 -06001928
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301929 sfe_ipv4_gen_sync_connection(si, c, &sis, SFE_SYNC_REASON_STATS, now_jiffies);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001930
Ken Zhudc423672021-09-02 18:27:01 -07001931 si->wc_next = c->all_connections_next;
1932
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001933 /*
1934 * We don't want to be holding the lock when we sync!
1935 */
1936 spin_unlock_bh(&si->lock);
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001937 sync_rule_callback(&sis);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001938 spin_lock_bh(&si->lock);
Ken Zhudc423672021-09-02 18:27:01 -07001939
1940 /*
1941 * c must be set and used in the same lock/unlock window;
1942 * because c could be removed when we don't hold the lock,
1943 * so delay grabbing until after the callback and relock.
1944 */
1945 c = si->wc_next;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001946 }
1947
Ken Zhudc423672021-09-02 18:27:01 -07001948 /*
1949 * At the end of the sync, put the wc_next to the connection we left.
1950 */
1951 si->wc_next = c;
1952
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001953 spin_unlock_bh(&si->lock);
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001954 rcu_read_unlock();
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001955
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001956done:
Ken Zhu137722d2021-09-23 17:57:36 -07001957 schedule_delayed_work_on(si->work_cpu, (struct delayed_work *)work, ((HZ + 99) / 100));
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001958}
1959
1960#define CHAR_DEV_MSG_SIZE 768
1961
1962/*
1963 * sfe_ipv4_debug_dev_read_start()
1964 * Generate part of the XML output.
1965 */
1966static bool sfe_ipv4_debug_dev_read_start(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
1967 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
1968{
1969 int bytes_read;
1970
Xiaoping Fan34586472015-07-03 02:20:35 -07001971 si->debug_read_seq++;
1972
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001973 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "<sfe_ipv4>\n");
1974 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1975 return false;
1976 }
1977
1978 *length -= bytes_read;
1979 *total_read += bytes_read;
1980
1981 ws->state++;
1982 return true;
1983}
1984
1985/*
1986 * sfe_ipv4_debug_dev_read_connections_start()
1987 * Generate part of the XML output.
1988 */
1989static bool sfe_ipv4_debug_dev_read_connections_start(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
1990 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
1991{
1992 int bytes_read;
1993
1994 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<connections>\n");
1995 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1996 return false;
1997 }
1998
1999 *length -= bytes_read;
2000 *total_read += bytes_read;
2001
2002 ws->state++;
2003 return true;
2004}
2005
2006/*
2007 * sfe_ipv4_debug_dev_read_connections_connection()
2008 * Generate part of the XML output.
2009 */
2010static bool sfe_ipv4_debug_dev_read_connections_connection(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2011 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2012{
2013 struct sfe_ipv4_connection *c;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002014 struct sfe_ipv4_connection_match *original_cm;
2015 struct sfe_ipv4_connection_match *reply_cm;
2016 int bytes_read;
2017 int protocol;
2018 struct net_device *src_dev;
Dave Hudson87973cd2013-10-22 16:00:04 +01002019 __be32 src_ip;
2020 __be32 src_ip_xlate;
2021 __be16 src_port;
2022 __be16 src_port_xlate;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07002023 u64 src_rx_packets;
2024 u64 src_rx_bytes;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002025 struct net_device *dest_dev;
Dave Hudson87973cd2013-10-22 16:00:04 +01002026 __be32 dest_ip;
2027 __be32 dest_ip_xlate;
2028 __be16 dest_port;
2029 __be16 dest_port_xlate;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07002030 u64 dest_rx_packets;
2031 u64 dest_rx_bytes;
2032 u64 last_sync_jiffies;
Ken Zhu37040ea2021-09-09 21:11:15 -07002033 u32 src_mark, dest_mark, src_priority, dest_priority, src_dscp, dest_dscp;
Parikshit Guned31a8202022-01-05 22:15:04 +05302034 bool original_cm_sawf_valid, reply_cm_sawf_valid;
2035 u32 flow_service_class, return_service_class;
2036 u32 flow_msduq, return_msduq;
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05302037 u32 packet, byte, original_cm_flags;
2038 u16 pppoe_session_id;
2039 u8 pppoe_remote_mac[ETH_ALEN];
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002040 u32 original_fast_xmit, reply_fast_xmit;
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08002041#ifdef CONFIG_NF_FLOW_COOKIE
2042 int src_flow_cookie, dst_flow_cookie;
2043#endif
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002044
2045 spin_lock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07002046
2047 for (c = si->all_connections_head; c; c = c->all_connections_next) {
2048 if (c->debug_read_seq < si->debug_read_seq) {
2049 c->debug_read_seq = si->debug_read_seq;
2050 break;
2051 }
2052 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002053
2054 /*
Xiaoping Fan34586472015-07-03 02:20:35 -07002055 * If there were no connections then move to the next state.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002056 */
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05302057 if (!c || c->removed) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002058 spin_unlock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07002059 ws->state++;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002060 return true;
2061 }
2062
2063 original_cm = c->original_match;
2064 reply_cm = c->reply_match;
2065
2066 protocol = c->protocol;
2067 src_dev = c->original_dev;
2068 src_ip = c->src_ip;
2069 src_ip_xlate = c->src_ip_xlate;
2070 src_port = c->src_port;
2071 src_port_xlate = c->src_port_xlate;
Xiaoping Fane1963d42015-08-25 17:06:19 -07002072 src_priority = original_cm->priority;
2073 src_dscp = original_cm->dscp >> SFE_IPV4_DSCP_SHIFT;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002074
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05302075 sfe_ipv4_connection_match_update_summary_stats(original_cm, &packet, &byte);
2076 sfe_ipv4_connection_match_update_summary_stats(reply_cm, &packet, &byte);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002077
2078 src_rx_packets = original_cm->rx_packet_count64;
2079 src_rx_bytes = original_cm->rx_byte_count64;
Ken Zhu37040ea2021-09-09 21:11:15 -07002080 src_mark = original_cm->mark;
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002081 original_fast_xmit = (original_cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_FAST_XMIT);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002082 dest_dev = c->reply_dev;
2083 dest_ip = c->dest_ip;
2084 dest_ip_xlate = c->dest_ip_xlate;
2085 dest_port = c->dest_port;
2086 dest_port_xlate = c->dest_port_xlate;
Xiaoping Fane1963d42015-08-25 17:06:19 -07002087 dest_priority = reply_cm->priority;
2088 dest_dscp = reply_cm->dscp >> SFE_IPV4_DSCP_SHIFT;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002089 dest_rx_packets = reply_cm->rx_packet_count64;
2090 dest_rx_bytes = reply_cm->rx_byte_count64;
Ken Zhu37040ea2021-09-09 21:11:15 -07002091 dest_mark = reply_cm->mark;
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002092 reply_fast_xmit = (reply_cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_FAST_XMIT);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002093 last_sync_jiffies = get_jiffies_64() - c->last_sync_jiffies;
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05302094 original_cm_flags = original_cm->flags;
2095 pppoe_session_id = original_cm->pppoe_session_id;
2096 ether_addr_copy(pppoe_remote_mac, original_cm->pppoe_remote_mac);
Parikshit Guned31a8202022-01-05 22:15:04 +05302097 original_cm_sawf_valid = original_cm->sawf_valid;
2098 reply_cm_sawf_valid = reply_cm->sawf_valid;
2099 flow_service_class = SFE_GET_SAWF_SERVICE_CLASS(original_cm->mark);
2100 flow_msduq = SFE_GET_SAWF_MSDUQ(original_cm->mark);
2101 return_service_class = SFE_GET_SAWF_SERVICE_CLASS(reply_cm->mark);
2102 return_msduq = SFE_GET_SAWF_MSDUQ(reply_cm->mark);
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08002103#ifdef CONFIG_NF_FLOW_COOKIE
2104 src_flow_cookie = original_cm->flow_cookie;
2105 dst_flow_cookie = reply_cm->flow_cookie;
2106#endif
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002107 spin_unlock_bh(&si->lock);
2108
2109 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t\t<connection "
2110 "protocol=\"%u\" "
2111 "src_dev=\"%s\" "
2112 "src_ip=\"%pI4\" src_ip_xlate=\"%pI4\" "
2113 "src_port=\"%u\" src_port_xlate=\"%u\" "
Xiaoping Fane1963d42015-08-25 17:06:19 -07002114 "src_priority=\"%u\" src_dscp=\"%u\" "
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002115 "src_rx_pkts=\"%llu\" src_rx_bytes=\"%llu\" "
Ken Zhu37040ea2021-09-09 21:11:15 -07002116 "src_mark=\"%08x\" "
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002117 "src_fast_xmit=\"%s\" "
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002118 "dest_dev=\"%s\" "
2119 "dest_ip=\"%pI4\" dest_ip_xlate=\"%pI4\" "
2120 "dest_port=\"%u\" dest_port_xlate=\"%u\" "
Xiaoping Fane1963d42015-08-25 17:06:19 -07002121 "dest_priority=\"%u\" dest_dscp=\"%u\" "
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002122 "dest_rx_pkts=\"%llu\" dest_rx_bytes=\"%llu\" "
Ken Zhu37040ea2021-09-09 21:11:15 -07002123 "dest_mark=\"%08x\" "
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002124 "reply_fast_xmit=\"%s\" "
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08002125#ifdef CONFIG_NF_FLOW_COOKIE
2126 "src_flow_cookie=\"%d\" dst_flow_cookie=\"%d\" "
2127#endif
Ken Zhu37040ea2021-09-09 21:11:15 -07002128 "last_sync=\"%llu\" ",
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002129 protocol,
2130 src_dev->name,
2131 &src_ip, &src_ip_xlate,
Dave Hudson87973cd2013-10-22 16:00:04 +01002132 ntohs(src_port), ntohs(src_port_xlate),
Xiaoping Fane1963d42015-08-25 17:06:19 -07002133 src_priority, src_dscp,
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002134 src_rx_packets, src_rx_bytes,
Ken Zhu37040ea2021-09-09 21:11:15 -07002135 src_mark,
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002136 original_fast_xmit ? "Yes" : "No",
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002137 dest_dev->name,
2138 &dest_ip, &dest_ip_xlate,
Dave Hudson87973cd2013-10-22 16:00:04 +01002139 ntohs(dest_port), ntohs(dest_port_xlate),
Xiaoping Fane1963d42015-08-25 17:06:19 -07002140 dest_priority, dest_dscp,
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002141 dest_rx_packets, dest_rx_bytes,
Ken Zhu37040ea2021-09-09 21:11:15 -07002142 dest_mark,
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002143 reply_fast_xmit ? "Yes" : "No",
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08002144#ifdef CONFIG_NF_FLOW_COOKIE
2145 src_flow_cookie, dst_flow_cookie,
2146#endif
Ken Zhu37040ea2021-09-09 21:11:15 -07002147 last_sync_jiffies);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002148
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05302149 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 +05302150 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 +05302151 pppoe_session_id, pppoe_remote_mac);
2152 }
2153
Parikshit Guned31a8202022-01-05 22:15:04 +05302154 if (original_cm_sawf_valid) {
2155 bytes_read += snprintf(msg + bytes_read, CHAR_DEV_MSG_SIZE, "flow_service_class=\"%d\" flow_msduq=\"%d\" ",
2156 flow_service_class, flow_msduq);
2157 }
2158
2159 if (reply_cm_sawf_valid) {
2160 bytes_read += snprintf(msg + bytes_read, CHAR_DEV_MSG_SIZE, "return_service_class=\"%d\" return_msduq=\"%d\" ",
2161 return_service_class, return_msduq);
2162 }
2163
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05302164 bytes_read += snprintf(msg + bytes_read, CHAR_DEV_MSG_SIZE, "/>\n");
2165
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002166 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2167 return false;
2168 }
2169
2170 *length -= bytes_read;
2171 *total_read += bytes_read;
2172
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002173 return true;
2174}
2175
2176/*
2177 * sfe_ipv4_debug_dev_read_connections_end()
2178 * Generate part of the XML output.
2179 */
2180static bool sfe_ipv4_debug_dev_read_connections_end(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2181 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2182{
2183 int bytes_read;
2184
2185 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t</connections>\n");
2186 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2187 return false;
2188 }
2189
2190 *length -= bytes_read;
2191 *total_read += bytes_read;
2192
2193 ws->state++;
2194 return true;
2195}
2196
2197/*
2198 * sfe_ipv4_debug_dev_read_exceptions_start()
2199 * Generate part of the XML output.
2200 */
2201static bool sfe_ipv4_debug_dev_read_exceptions_start(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2202 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2203{
2204 int bytes_read;
2205
2206 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<exceptions>\n");
2207 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2208 return false;
2209 }
2210
2211 *length -= bytes_read;
2212 *total_read += bytes_read;
2213
2214 ws->state++;
2215 return true;
2216}
2217
2218/*
2219 * sfe_ipv4_debug_dev_read_exceptions_exception()
2220 * Generate part of the XML output.
2221 */
2222static bool sfe_ipv4_debug_dev_read_exceptions_exception(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2223 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2224{
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302225 int i;
2226 u64 val = 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002227
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302228 for_each_possible_cpu(i) {
2229 const struct sfe_ipv4_stats *s = per_cpu_ptr(si->stats_pcpu, i);
2230 val += s->exception_events64[ws->iter_exception];
2231 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002232
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302233 if (val) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002234 int bytes_read;
2235
2236 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE,
2237 "\t\t<exception name=\"%s\" count=\"%llu\" />\n",
2238 sfe_ipv4_exception_events_string[ws->iter_exception],
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302239 val);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002240 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2241 return false;
2242 }
2243
2244 *length -= bytes_read;
2245 *total_read += bytes_read;
2246 }
2247
2248 ws->iter_exception++;
2249 if (ws->iter_exception >= SFE_IPV4_EXCEPTION_EVENT_LAST) {
2250 ws->iter_exception = 0;
2251 ws->state++;
2252 }
2253
2254 return true;
2255}
2256
2257/*
2258 * sfe_ipv4_debug_dev_read_exceptions_end()
2259 * Generate part of the XML output.
2260 */
2261static bool sfe_ipv4_debug_dev_read_exceptions_end(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2262 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2263{
2264 int bytes_read;
2265
2266 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t</exceptions>\n");
2267 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2268 return false;
2269 }
2270
2271 *length -= bytes_read;
2272 *total_read += bytes_read;
2273
2274 ws->state++;
2275 return true;
2276}
2277
2278/*
2279 * sfe_ipv4_debug_dev_read_stats()
2280 * Generate part of the XML output.
2281 */
2282static bool sfe_ipv4_debug_dev_read_stats(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2283 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2284{
2285 int bytes_read;
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302286 struct sfe_ipv4_stats stats;
2287 unsigned int num_conn;
2288
2289 sfe_ipv4_update_summary_stats(si, &stats);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002290
2291 spin_lock_bh(&si->lock);
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302292 num_conn = si->num_connections;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002293 spin_unlock_bh(&si->lock);
2294
2295 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<stats "
2296 "num_connections=\"%u\" "
Amitesh Anand63be37d2021-12-24 20:51:48 +05302297 "pkts_dropped=\"%llu\" "
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002298 "pkts_fast_xmited=\"%llu\" "
Xiaoping Fan59176422015-05-22 15:58:10 -07002299 "pkts_forwarded=\"%llu\" pkts_not_forwarded=\"%llu\" "
2300 "create_requests=\"%llu\" create_collisions=\"%llu\" "
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05302301 "create_failures=\"%llu\" "
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002302 "destroy_requests=\"%llu\" destroy_misses=\"%llu\" "
2303 "flushes=\"%llu\" "
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +05302304 "hash_hits=\"%llu\" hash_reorders=\"%llu\" "
2305 "pppoe_encap_pkts_fwded=\"%llu\" "
Guduri Prathyusha034d6352022-01-12 16:49:04 +05302306 "pppoe_decap_pkts_fwded=\"%llu\" "
2307 "pppoe_bridge_pkts_fwded=\"%llu\" />\n",
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302308 num_conn,
Amitesh Anand63be37d2021-12-24 20:51:48 +05302309 stats.packets_dropped64,
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002310 stats.packets_fast_xmited64,
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302311 stats.packets_forwarded64,
2312 stats.packets_not_forwarded64,
2313 stats.connection_create_requests64,
2314 stats.connection_create_collisions64,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05302315 stats.connection_create_failures64,
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302316 stats.connection_destroy_requests64,
2317 stats.connection_destroy_misses64,
2318 stats.connection_flushes64,
2319 stats.connection_match_hash_hits64,
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +05302320 stats.connection_match_hash_reorders64,
2321 stats.pppoe_encap_packets_forwarded64,
Guduri Prathyusha034d6352022-01-12 16:49:04 +05302322 stats.pppoe_decap_packets_forwarded64,
2323 stats.pppoe_bridge_packets_forwarded64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002324 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2325 return false;
2326 }
2327
2328 *length -= bytes_read;
2329 *total_read += bytes_read;
2330
2331 ws->state++;
2332 return true;
2333}
2334
2335/*
2336 * sfe_ipv4_debug_dev_read_end()
2337 * Generate part of the XML output.
2338 */
2339static bool sfe_ipv4_debug_dev_read_end(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2340 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2341{
2342 int bytes_read;
2343
2344 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "</sfe_ipv4>\n");
2345 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2346 return false;
2347 }
2348
2349 *length -= bytes_read;
2350 *total_read += bytes_read;
2351
2352 ws->state++;
2353 return true;
2354}
2355
2356/*
2357 * Array of write functions that write various XML elements that correspond to
2358 * our XML output state machine.
2359 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07002360static 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 +01002361 sfe_ipv4_debug_dev_read_start,
2362 sfe_ipv4_debug_dev_read_connections_start,
2363 sfe_ipv4_debug_dev_read_connections_connection,
2364 sfe_ipv4_debug_dev_read_connections_end,
2365 sfe_ipv4_debug_dev_read_exceptions_start,
2366 sfe_ipv4_debug_dev_read_exceptions_exception,
2367 sfe_ipv4_debug_dev_read_exceptions_end,
2368 sfe_ipv4_debug_dev_read_stats,
2369 sfe_ipv4_debug_dev_read_end,
2370};
2371
2372/*
2373 * sfe_ipv4_debug_dev_read()
2374 * Send info to userspace upon read request from user
2375 */
2376static ssize_t sfe_ipv4_debug_dev_read(struct file *filp, char *buffer, size_t length, loff_t *offset)
2377{
2378 char msg[CHAR_DEV_MSG_SIZE];
2379 int total_read = 0;
2380 struct sfe_ipv4_debug_xml_write_state *ws;
2381 struct sfe_ipv4 *si = &__si;
2382
2383 ws = (struct sfe_ipv4_debug_xml_write_state *)filp->private_data;
2384 while ((ws->state != SFE_IPV4_DEBUG_XML_STATE_DONE) && (length > CHAR_DEV_MSG_SIZE)) {
2385 if ((sfe_ipv4_debug_xml_write_methods[ws->state])(si, buffer, msg, &length, &total_read, ws)) {
2386 continue;
2387 }
2388 }
2389
2390 return total_read;
2391}
2392
2393/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002394 * sfe_ipv4_debug_dev_open()
2395 */
2396static int sfe_ipv4_debug_dev_open(struct inode *inode, struct file *file)
2397{
2398 struct sfe_ipv4_debug_xml_write_state *ws;
2399
2400 ws = (struct sfe_ipv4_debug_xml_write_state *)file->private_data;
2401 if (!ws) {
2402 ws = kzalloc(sizeof(struct sfe_ipv4_debug_xml_write_state), GFP_KERNEL);
2403 if (!ws) {
2404 return -ENOMEM;
2405 }
2406
2407 ws->state = SFE_IPV4_DEBUG_XML_STATE_START;
2408 file->private_data = ws;
2409 }
2410
2411 return 0;
2412}
2413
2414/*
2415 * sfe_ipv4_debug_dev_release()
2416 */
2417static int sfe_ipv4_debug_dev_release(struct inode *inode, struct file *file)
2418{
2419 struct sfe_ipv4_debug_xml_write_state *ws;
2420
2421 ws = (struct sfe_ipv4_debug_xml_write_state *)file->private_data;
2422 if (ws) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002423 /*
2424 * We've finished with our output so free the write state.
2425 */
2426 kfree(ws);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05302427 file->private_data = NULL;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002428 }
2429
2430 return 0;
2431}
2432
2433/*
2434 * File operations used in the debug char device
2435 */
2436static struct file_operations sfe_ipv4_debug_dev_fops = {
2437 .read = sfe_ipv4_debug_dev_read,
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002438 .open = sfe_ipv4_debug_dev_open,
2439 .release = sfe_ipv4_debug_dev_release
2440};
2441
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08002442#ifdef CONFIG_NF_FLOW_COOKIE
2443/*
2444 * sfe_register_flow_cookie_cb
2445 * register a function in SFE to let SFE use this function to configure flow cookie for a flow
2446 *
2447 * Hardware driver which support flow cookie should register a callback function in SFE. Then SFE
2448 * can use this function to configure flow cookie for a flow.
2449 * return: 0, success; !=0, fail
2450 */
2451int sfe_register_flow_cookie_cb(flow_cookie_set_func_t cb)
2452{
2453 struct sfe_ipv4 *si = &__si;
2454
2455 BUG_ON(!cb);
2456
2457 if (si->flow_cookie_set_func) {
2458 return -1;
2459 }
2460
2461 rcu_assign_pointer(si->flow_cookie_set_func, cb);
2462 return 0;
2463}
2464
2465/*
2466 * sfe_unregister_flow_cookie_cb
2467 * unregister function which is used to configure flow cookie for a flow
2468 *
2469 * return: 0, success; !=0, fail
2470 */
2471int sfe_unregister_flow_cookie_cb(flow_cookie_set_func_t cb)
2472{
2473 struct sfe_ipv4 *si = &__si;
2474
2475 RCU_INIT_POINTER(si->flow_cookie_set_func, NULL);
2476 return 0;
2477}
Xiaoping Fan640faf42015-08-28 15:50:55 -07002478
2479/*
2480 * sfe_ipv4_get_flow_cookie()
2481 */
2482static ssize_t sfe_ipv4_get_flow_cookie(struct device *dev,
2483 struct device_attribute *attr,
2484 char *buf)
2485{
2486 struct sfe_ipv4 *si = &__si;
Xiaoping Fan01c67cc2015-11-09 11:31:57 -08002487 return snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", si->flow_cookie_enable);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002488}
2489
2490/*
2491 * sfe_ipv4_set_flow_cookie()
2492 */
2493static ssize_t sfe_ipv4_set_flow_cookie(struct device *dev,
2494 struct device_attribute *attr,
2495 const char *buf, size_t size)
2496{
2497 struct sfe_ipv4 *si = &__si;
Ken Zhu137722d2021-09-23 17:57:36 -07002498 si->flow_cookie_enable = simple_strtol(buf, NULL, 0);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002499
2500 return size;
2501}
2502
2503/*
2504 * sysfs attributes.
2505 */
2506static const struct device_attribute sfe_ipv4_flow_cookie_attr =
Xiaoping Fane70da412016-02-26 16:47:57 -08002507 __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 -08002508#endif /*CONFIG_NF_FLOW_COOKIE*/
2509
Ken Zhu137722d2021-09-23 17:57:36 -07002510/*
2511 * sfe_ipv4_get_cpu()
2512 */
2513static ssize_t sfe_ipv4_get_cpu(struct device *dev,
2514 struct device_attribute *attr,
2515 char *buf)
2516{
2517 struct sfe_ipv4 *si = &__si;
2518 return snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", si->work_cpu);
2519}
2520
2521/*
2522 * sfe_ipv4_set_cpu()
2523 */
2524static ssize_t sfe_ipv4_set_cpu(struct device *dev,
2525 struct device_attribute *attr,
2526 const char *buf, size_t size)
2527{
2528 struct sfe_ipv4 *si = &__si;
2529 int work_cpu;
2530 work_cpu = simple_strtol(buf, NULL, 0);
2531 if ((work_cpu >= 0) && (work_cpu <= NR_CPUS)) {
2532 si->work_cpu = work_cpu;
2533 } else {
2534 dev_err(dev, "%s is not in valid range[0,%d]", buf, NR_CPUS);
2535 }
2536 return size;
2537}
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002538
Ken Zhu137722d2021-09-23 17:57:36 -07002539/*
2540 * sysfs attributes.
2541 */
2542static const struct device_attribute sfe_ipv4_cpu_attr =
2543 __ATTR(stats_work_cpu, S_IWUSR | S_IRUGO, sfe_ipv4_get_cpu, sfe_ipv4_set_cpu);
2544
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002545/*
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05302546 * sfe_ipv4_conn_match_hash_init()
2547 * Initialize conn match hash lists
2548 */
2549static void sfe_ipv4_conn_match_hash_init(struct sfe_ipv4 *si, int len)
2550{
2551 struct hlist_head *hash_list = si->hlist_conn_match_hash_head;
2552 int i;
2553
2554 for (i = 0; i < len; i++) {
2555 INIT_HLIST_HEAD(&hash_list[i]);
2556 }
2557}
2558
Amitesh Anand63be37d2021-12-24 20:51:48 +05302559#ifdef SFE_PROCESS_LOCAL_OUT
2560/*
2561 * sfe_ipv4_local_out()
2562 * Called for packets from ip_local_out() - post encapsulation & other packets
2563 */
2564static unsigned int sfe_ipv4_local_out(void *priv, struct sk_buff *skb, const struct nf_hook_state *nhs)
2565{
Nitin Shettyc28f8172022-02-04 16:23:46 +05302566 struct sfe_l2_info l2_info = {0};
Guduri Prathyusha5f27e232022-01-06 14:39:04 +05302567
Amitesh Anand63be37d2021-12-24 20:51:48 +05302568 DEBUG_TRACE("%px: sfe: sfe_ipv4_local_out hook called.\n", skb);
2569
2570 if (likely(skb->skb_iif)) {
Guduri Prathyusha5f27e232022-01-06 14:39:04 +05302571 return sfe_ipv4_recv(skb->dev, skb, &l2_info, true) ? NF_STOLEN : NF_ACCEPT;
Amitesh Anand63be37d2021-12-24 20:51:48 +05302572 }
2573
2574 return NF_ACCEPT;
2575}
2576
2577/*
2578 * struct nf_hook_ops sfe_ipv4_ops_local_out[]
2579 * Hooks into netfilter local out packet monitoring points.
2580 */
2581static struct nf_hook_ops sfe_ipv4_ops_local_out[] __read_mostly = {
2582
2583 /*
2584 * Local out routing hook is used to monitor packets.
2585 */
2586 {
2587 .hook = sfe_ipv4_local_out,
2588 .pf = PF_INET,
2589 .hooknum = NF_INET_LOCAL_OUT,
2590 .priority = NF_IP_PRI_FIRST,
2591 },
2592};
2593#endif
2594
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002595/*
Dave Hudson87973cd2013-10-22 16:00:04 +01002596 * sfe_ipv4_init()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002597 */
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05302598int sfe_ipv4_init(void)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002599{
2600 struct sfe_ipv4 *si = &__si;
2601 int result = -1;
2602
Dave Hudsondcd08fb2013-11-22 09:25:16 -06002603 DEBUG_INFO("SFE IPv4 init\n");
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002604
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05302605 sfe_ipv4_conn_match_hash_init(si, ARRAY_SIZE(si->hlist_conn_match_hash_head));
2606
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302607 si->stats_pcpu = alloc_percpu_gfp(struct sfe_ipv4_stats, GFP_KERNEL | __GFP_ZERO);
2608 if (!si->stats_pcpu) {
2609 DEBUG_ERROR("failed to allocate stats memory for sfe_ipv4\n");
2610 goto exit0;
2611 }
2612
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002613 /*
Parikshit Guned31a8202022-01-05 22:15:04 +05302614 * Allocate per cpu per service class memory.
2615 */
2616 si->stats_pcpu_psc = alloc_percpu_gfp(struct sfe_ipv4_service_class_stats_db,
2617 GFP_KERNEL | __GFP_ZERO);
2618 if (!si->stats_pcpu_psc) {
2619 DEBUG_ERROR("failed to allocate per cpu per service clas stats memory\n");
2620 goto exit1;
2621 }
2622
2623 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002624 * Create sys/sfe_ipv4
2625 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302626 si->sys_ipv4 = kobject_create_and_add("sfe_ipv4", NULL);
2627 if (!si->sys_ipv4) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002628 DEBUG_ERROR("failed to register sfe_ipv4\n");
Parikshit Guned31a8202022-01-05 22:15:04 +05302629 goto exit2;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002630 }
2631
2632 /*
2633 * Create files, one for each parameter supported by this module.
2634 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302635 result = sysfs_create_file(si->sys_ipv4, &sfe_ipv4_debug_dev_attr.attr);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002636 if (result) {
2637 DEBUG_ERROR("failed to register debug dev file: %d\n", result);
Parikshit Guned31a8202022-01-05 22:15:04 +05302638 goto exit3;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002639 }
2640
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302641 result = sysfs_create_file(si->sys_ipv4, &sfe_ipv4_cpu_attr.attr);
Ken Zhu137722d2021-09-23 17:57:36 -07002642 if (result) {
2643 DEBUG_ERROR("failed to register debug dev file: %d\n", result);
Parikshit Guned31a8202022-01-05 22:15:04 +05302644 goto exit4;
Ken Zhu137722d2021-09-23 17:57:36 -07002645 }
2646
Xiaoping Fan640faf42015-08-28 15:50:55 -07002647#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302648 result = sysfs_create_file(si->sys_ipv4, &sfe_ipv4_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002649 if (result) {
2650 DEBUG_ERROR("failed to register flow cookie enable file: %d\n", result);
Parikshit Guned31a8202022-01-05 22:15:04 +05302651 goto exit5;
Xiaoping Fan640faf42015-08-28 15:50:55 -07002652 }
2653#endif /* CONFIG_NF_FLOW_COOKIE */
2654
Amitesh Anand63be37d2021-12-24 20:51:48 +05302655#ifdef SFE_PROCESS_LOCAL_OUT
2656#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
2657 result = nf_register_hooks(sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2658#else
2659 result = nf_register_net_hooks(&init_net, sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2660#endif
2661 if (result < 0) {
2662 DEBUG_ERROR("can't register nf local out hook: %d\n", result);
Parikshit Guned31a8202022-01-05 22:15:04 +05302663 goto exit6;
Amitesh Anand63be37d2021-12-24 20:51:48 +05302664 }
2665 DEBUG_INFO("Register nf local out hook success: %d\n", result);
2666#endif
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002667 /*
2668 * Register our debug char device.
2669 */
2670 result = register_chrdev(0, "sfe_ipv4", &sfe_ipv4_debug_dev_fops);
2671 if (result < 0) {
2672 DEBUG_ERROR("Failed to register chrdev: %d\n", result);
Parikshit Guned31a8202022-01-05 22:15:04 +05302673 goto exit7;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002674 }
2675
2676 si->debug_dev = result;
Ken Zhu137722d2021-09-23 17:57:36 -07002677 si->work_cpu = WORK_CPU_UNBOUND;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002678
2679 /*
Ken Zhu137722d2021-09-23 17:57:36 -07002680 * Create a work to handle periodic statistics.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002681 */
Ken Zhu137722d2021-09-23 17:57:36 -07002682 INIT_DELAYED_WORK(&(si->sync_dwork), sfe_ipv4_periodic_sync);
2683 schedule_delayed_work_on(si->work_cpu, &(si->sync_dwork), ((HZ + 99) / 100));
2684
Dave Hudson87973cd2013-10-22 16:00:04 +01002685 spin_lock_init(&si->lock);
Dave Hudson87973cd2013-10-22 16:00:04 +01002686 return 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002687
Parikshit Guned31a8202022-01-05 22:15:04 +05302688exit7:
Amitesh Anand63be37d2021-12-24 20:51:48 +05302689#ifdef SFE_PROCESS_LOCAL_OUT
2690 DEBUG_TRACE("sfe: Unregister local out hook\n");
2691#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
2692 nf_unregister_hooks(sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2693#else
2694 nf_unregister_net_hooks(&init_net, sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2695#endif
Parikshit Guned31a8202022-01-05 22:15:04 +05302696exit6:
Amitesh Anand63be37d2021-12-24 20:51:48 +05302697#endif
Xiaoping Fan640faf42015-08-28 15:50:55 -07002698#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302699 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002700
Parikshit Guned31a8202022-01-05 22:15:04 +05302701exit5:
Xiaoping Fan640faf42015-08-28 15:50:55 -07002702#endif /* CONFIG_NF_FLOW_COOKIE */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302703 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_cpu_attr.attr);
Parikshit Guned31a8202022-01-05 22:15:04 +05302704exit4:
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302705 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_debug_dev_attr.attr);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002706
Parikshit Guned31a8202022-01-05 22:15:04 +05302707exit3:
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302708 kobject_put(si->sys_ipv4);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002709
Parikshit Guned31a8202022-01-05 22:15:04 +05302710exit2:
2711 free_percpu(si->stats_pcpu_psc);
2712
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002713exit1:
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302714 free_percpu(si->stats_pcpu);
2715
2716exit0:
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002717 return result;
2718}
2719
2720/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002721 * sfe_ipv4_exit()
2722 */
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05302723void sfe_ipv4_exit(void)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002724{
Dave Hudson87973cd2013-10-22 16:00:04 +01002725 struct sfe_ipv4 *si = &__si;
2726
Dave Hudsondcd08fb2013-11-22 09:25:16 -06002727 DEBUG_INFO("SFE IPv4 exit\n");
Dave Hudson87973cd2013-10-22 16:00:04 +01002728 /*
2729 * Destroy all connections.
2730 */
Dave Hudsondcd08fb2013-11-22 09:25:16 -06002731 sfe_ipv4_destroy_all_rules_for_dev(NULL);
Dave Hudson87973cd2013-10-22 16:00:04 +01002732
Ken Zhu137722d2021-09-23 17:57:36 -07002733 cancel_delayed_work_sync(&si->sync_dwork);
Dave Hudson87973cd2013-10-22 16:00:04 +01002734
Dave Hudson87973cd2013-10-22 16:00:04 +01002735 unregister_chrdev(si->debug_dev, "sfe_ipv4");
2736
Amitesh Anand63be37d2021-12-24 20:51:48 +05302737#ifdef SFE_PROCESS_LOCAL_OUT
2738 DEBUG_TRACE("sfe: Unregister local out hook\n");
2739#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
2740 nf_unregister_hooks(sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2741#else
2742 nf_unregister_net_hooks(&init_net, sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2743#endif
2744#endif
2745
Xiaoping Fan640faf42015-08-28 15:50:55 -07002746#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302747 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002748#endif /* CONFIG_NF_FLOW_COOKIE */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302749 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_debug_dev_attr.attr);
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002750
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302751 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_cpu_attr.attr);
Dave Hudson87973cd2013-10-22 16:00:04 +01002752
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302753 kobject_put(si->sys_ipv4);
Dave Hudson87973cd2013-10-22 16:00:04 +01002754
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302755 free_percpu(si->stats_pcpu);
Parikshit Guned31a8202022-01-05 22:15:04 +05302756 free_percpu(si->stats_pcpu_psc);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002757}
2758
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08002759#ifdef CONFIG_NF_FLOW_COOKIE
2760EXPORT_SYMBOL(sfe_register_flow_cookie_cb);
2761EXPORT_SYMBOL(sfe_unregister_flow_cookie_cb);
2762#endif