blob: a5bacddda82256331b254d12d5e8b2e40fccb39a [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/*
Jackson Bockus3fafbf32022-02-13 17:15:26 -08001057 * sfe_ipv4_service_class_stats_pcpu_get()
1058 * Gets one CPU's service class statistics.
1059 */
1060static inline bool sfe_ipv4_service_class_stats_pcpu_get(struct sfe_ipv4_per_service_class_stats *sc_stats, uint64_t *bytes, uint64_t *packets)
1061{
1062 uint32_t retries = 0;
1063 uint32_t seq;
1064 uint64_t bytes_tmp, packets_tmp;
1065
1066 do {
1067 seq = read_seqcount_begin(&sc_stats->seq);
1068 bytes_tmp = sc_stats->tx_bytes;
1069 packets_tmp = sc_stats->tx_packets;
1070 } while (read_seqcount_retry(&sc_stats->seq, seq) && ++retries < SFE_SERVICE_CLASS_STATS_MAX_RETRY);
1071
1072 *bytes += bytes_tmp;
1073 *packets += packets_tmp;
1074
1075 return retries < SFE_SERVICE_CLASS_STATS_MAX_RETRY;
1076}
1077
1078/*
1079 * sfe_ipv4_service_class_stats_get()
1080 * Copy the ipv4 statistics for the given service class.
1081 */
1082bool sfe_ipv4_service_class_stats_get(uint8_t sid, uint64_t *bytes, uint64_t *packets)
1083{
1084 struct sfe_ipv4 *si = &__si;
1085 uint32_t cpu = 0;
1086
1087 for_each_possible_cpu(cpu) {
1088 struct sfe_ipv4_service_class_stats_db *stats_db = per_cpu_ptr(si->stats_pcpu_psc, cpu);
1089 struct sfe_ipv4_per_service_class_stats *sc_stats = &stats_db->psc_stats[sid];
1090
1091 if (!sfe_ipv4_service_class_stats_pcpu_get(sc_stats, bytes, packets)) {
1092 return false;
1093 }
1094 }
1095
1096 return true;
1097}
1098
1099/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001100 * sfe_ipv4_create_rule()
1101 * Create a forwarding rule.
1102 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301103int sfe_ipv4_create_rule(struct sfe_ipv4_rule_create_msg *msg)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001104{
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001105 struct sfe_ipv4 *si = &__si;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301106 struct sfe_ipv4_connection *c, *c_old;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001107 struct sfe_ipv4_connection_match *original_cm;
1108 struct sfe_ipv4_connection_match *reply_cm;
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001109 struct net_device *dest_dev;
1110 struct net_device *src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301111 struct sfe_ipv4_5tuple *tuple = &msg->tuple;
Suruchi Sumanc1a4a612021-10-21 14:50:23 +05301112 s32 flow_interface_num = msg->conn_rule.flow_top_interface_num;
1113 s32 return_interface_num = msg->conn_rule.return_top_interface_num;
Amitesh Anand63be37d2021-12-24 20:51:48 +05301114 struct net *net;
1115 struct sock *sk;
1116 unsigned int src_if_idx;
Parikshit Guned31a8202022-01-05 22:15:04 +05301117 u32 flow_sawf_tag;
1118 u32 return_sawf_tag;
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001119
Suruchi Sumanc1a4a612021-10-21 14:50:23 +05301120 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_FLOW_BOTTOM_INTERFACE) {
1121 flow_interface_num = msg->conn_rule.flow_interface_num;
1122 }
1123
1124 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_RETURN_BOTTOM_INTERFACE) {
1125 return_interface_num = msg->conn_rule.return_interface_num;
1126 }
1127
1128 src_dev = dev_get_by_index(&init_net, flow_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301129 if (!src_dev) {
1130 DEBUG_WARN("%px: Unable to find src_dev corresponding to %d\n", msg,
Suruchi Sumanc1a4a612021-10-21 14:50:23 +05301131 flow_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301132 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
1133 return -EINVAL;
1134 }
1135
Suruchi Sumanc1a4a612021-10-21 14:50:23 +05301136 dest_dev = dev_get_by_index(&init_net, return_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301137 if (!dest_dev) {
1138 DEBUG_WARN("%px: Unable to find dest_dev corresponding to %d\n", msg,
Suruchi Sumanc1a4a612021-10-21 14:50:23 +05301139 return_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301140 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
1141 dev_put(src_dev);
1142 return -EINVAL;
1143 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001144
Matthew McClintock389b42a2014-09-24 14:05:51 -05001145 if (unlikely((dest_dev->reg_state != NETREG_REGISTERED) ||
1146 (src_dev->reg_state != NETREG_REGISTERED))) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301147 dev_put(src_dev);
1148 dev_put(dest_dev);
1149 DEBUG_WARN("%px: src_dev=%s and dest_dev=%s are unregistered\n", msg,
1150 src_dev->name, dest_dev->name);
1151 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
Matthew McClintock389b42a2014-09-24 14:05:51 -05001152 return -EINVAL;
1153 }
1154
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301155 /*
1156 * Allocate the various connection tracking objects.
1157 */
1158 c = (struct sfe_ipv4_connection *)kmalloc(sizeof(struct sfe_ipv4_connection), GFP_ATOMIC);
1159 if (unlikely(!c)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301160 DEBUG_WARN("%px: memory allocation of connection entry failed\n", msg);
1161 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
1162 dev_put(src_dev);
1163 dev_put(dest_dev);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301164 return -ENOMEM;
1165 }
1166
1167 original_cm = (struct sfe_ipv4_connection_match *)kmalloc(sizeof(struct sfe_ipv4_connection_match), GFP_ATOMIC);
1168 if (unlikely(!original_cm)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301169 DEBUG_WARN("%px: memory allocation of connection match entry failed\n", msg);
1170 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301171 kfree(c);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301172 dev_put(src_dev);
1173 dev_put(dest_dev);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301174 return -ENOMEM;
1175 }
1176
1177 reply_cm = (struct sfe_ipv4_connection_match *)kmalloc(sizeof(struct sfe_ipv4_connection_match), GFP_ATOMIC);
1178 if (unlikely(!reply_cm)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301179 DEBUG_WARN("%px: memory allocation of connection match entry failed\n", msg);
1180 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301181 kfree(original_cm);
1182 kfree(c);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301183 dev_put(src_dev);
1184 dev_put(dest_dev);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301185 return -ENOMEM;
1186 }
1187
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301188 this_cpu_inc(si->stats_pcpu->connection_create_requests64);
1189
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001190 spin_lock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001191
1192 /*
Nicolas Costa436926b2014-01-14 10:36:22 -06001193 * Check to see if there is already a flow that matches the rule we're
1194 * trying to create. If there is then we can't create a new one.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001195 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301196 c_old = sfe_ipv4_find_connection(si,
Wayne Tanbb7f1782021-12-13 11:16:04 -08001197 msg->tuple.protocol,
1198 msg->tuple.flow_ip,
1199 msg->tuple.flow_ident,
1200 msg->tuple.return_ip,
1201 msg->tuple.return_ident);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301202
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301203 if (c_old != NULL) {
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301204 this_cpu_inc(si->stats_pcpu->connection_create_collisions64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001205
1206 /*
Nicolas Costa436926b2014-01-14 10:36:22 -06001207 * If we already have the flow then it's likely that this
1208 * request to create the connection rule contains more
1209 * up-to-date information. Check and update accordingly.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001210 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301211 sfe_ipv4_update_protocol_state(c, msg);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001212 spin_unlock_bh(&si->lock);
1213
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301214 kfree(reply_cm);
1215 kfree(original_cm);
1216 kfree(c);
1217
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301218 dev_put(src_dev);
1219 dev_put(dest_dev);
1220
Amitesh Anand63be37d2021-12-24 20:51:48 +05301221 DEBUG_TRACE("%px: connection already exists - p:%d\n"
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301222 " s: %s:%pM:%pI4:%u, d: %s:%pM:%pI4:%u\n",
Amitesh Anand63be37d2021-12-24 20:51:48 +05301223 msg, tuple->protocol,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301224 src_dev->name, msg->conn_rule.flow_mac, &tuple->flow_ip, ntohs(tuple->flow_ident),
1225 dest_dev->name, msg->conn_rule.return_mac, &tuple->return_ip, ntohs(tuple->return_ident));
1226
Nicolas Costa514fde02014-01-13 15:50:29 -06001227 return -EADDRINUSE;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001228 }
1229
1230 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001231 * Fill in the "original" direction connection matching object.
1232 * Note that the transmit MAC address is "dest_mac_xlate" because
1233 * we always know both ends of a connection by their translated
1234 * addresses and not their public addresses.
1235 */
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001236 original_cm->match_dev = src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301237 original_cm->match_protocol = tuple->protocol;
1238 original_cm->match_src_ip = tuple->flow_ip;
Suruchi Suman66609a72022-01-20 02:34:25 +05301239 original_cm->match_src_port = netif_is_vxlan(src_dev) ? 0 : tuple->flow_ident;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301240 original_cm->match_dest_ip = tuple->return_ip;
1241 original_cm->match_dest_port = tuple->return_ident;
1242
1243 original_cm->xlate_src_ip = msg->conn_rule.flow_ip_xlate;
1244 original_cm->xlate_src_port = msg->conn_rule.flow_ident_xlate;
1245 original_cm->xlate_dest_ip = msg->conn_rule.return_ip_xlate;
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301246 original_cm->xlate_dest_port = msg->conn_rule.return_ident_xlate;
1247
1248 if (tuple->protocol == IPPROTO_GRE) {
1249 /*
1250 * the PPTP is 4 tuple lookup.
1251 * During th rule lookup destination call id from packet
1252 * is matched against destination port in cm.
1253 */
1254 original_cm->match_src_port = 0;
1255 original_cm->xlate_src_port = 0;
1256 }
Wayne Tanbb7f1782021-12-13 11:16:04 -08001257
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301258 atomic_set(&original_cm->rx_packet_count, 0);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001259 original_cm->rx_packet_count64 = 0;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301260 atomic_set(&original_cm->rx_byte_count, 0);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001261 original_cm->rx_byte_count64 = 0;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301262
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001263 original_cm->xmit_dev = dest_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301264 original_cm->xmit_dev_mtu = msg->conn_rule.return_mtu;
1265
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001266 original_cm->connection = c;
1267 original_cm->counter_match = reply_cm;
Wayne Tanbb7f1782021-12-13 11:16:04 -08001268 original_cm->l2_hdr_size = 0;
1269 original_cm->flags = 0;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301270
Amitesh Anand63be37d2021-12-24 20:51:48 +05301271 /*
1272 * UDP Socket is valid only in decap direction.
1273 */
1274 RCU_INIT_POINTER(original_cm->up, NULL);
1275
Ken Zhu37040ea2021-09-09 21:11:15 -07001276 if (msg->valid_flags & SFE_RULE_CREATE_MARK_VALID) {
1277 original_cm->mark = msg->mark_rule.flow_mark;
1278 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_MARK;
1279 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301280 if (msg->valid_flags & SFE_RULE_CREATE_QOS_VALID) {
1281 original_cm->priority = msg->qos_rule.flow_qos_tag;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001282 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PRIORITY_REMARK;
1283 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301284 if (msg->valid_flags & SFE_RULE_CREATE_DSCP_MARKING_VALID) {
1285 original_cm->dscp = msg->dscp_rule.flow_dscp << SFE_IPV4_DSCP_SHIFT;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001286 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_DSCP_REMARK;
1287 }
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301288 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1289 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_BRIDGE_FLOW;
1290 }
Ken Zhu7e38d1a2021-11-30 17:31:46 -08001291 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_FLOW_TRANSMIT_FAST) {
1292 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_FAST_XMIT_DEV_ADMISSION;
1293 }
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301294
Wayne Tanbb7f1782021-12-13 11:16:04 -08001295 /*
Parikshit Guned31a8202022-01-05 22:15:04 +05301296 * Mark SAWF metadata if the sawf tag is valid and set.
1297 */
1298 original_cm->sawf_valid = false;
1299 flow_sawf_tag = SFE_GET_SAWF_TAG(msg->sawf_rule.flow_mark);
1300 if (likely(SFE_SAWF_TAG_IS_VALID(flow_sawf_tag))) {
1301 original_cm->mark = msg->sawf_rule.flow_mark;
1302 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_MARK;
1303 original_cm->sawf_valid = true;
1304 }
1305
1306 /*
Wayne Tanbb7f1782021-12-13 11:16:04 -08001307 * Add VLAN rule to original_cm
1308 */
1309 if (msg->valid_flags & SFE_RULE_CREATE_VLAN_VALID) {
1310 struct sfe_vlan_rule *vlan_primary_rule = &msg->vlan_primary_rule;
1311 struct sfe_vlan_rule *vlan_secondary_rule = &msg->vlan_secondary_rule;
1312 sfe_ipv4_match_entry_set_vlan(original_cm,
1313 vlan_primary_rule->ingress_vlan_tag,
1314 vlan_primary_rule->egress_vlan_tag,
1315 vlan_secondary_rule->ingress_vlan_tag,
1316 vlan_secondary_rule->egress_vlan_tag);
1317
1318 if ((msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_RETURN_BOTTOM_INTERFACE) &&
1319 original_cm->egress_vlan_hdr_cnt > 0) {
1320 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_INSERT_EGRESS_VLAN_TAG;
1321 original_cm->l2_hdr_size += original_cm->egress_vlan_hdr_cnt * VLAN_HLEN;
1322 }
1323 }
1324
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301325 if ((IPPROTO_GRE == tuple->protocol) && !sfe_ipv4_is_local_ip(si, original_cm->match_dest_ip)) {
1326 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PASSTHROUGH;
1327 }
1328
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001329#ifdef CONFIG_NF_FLOW_COOKIE
1330 original_cm->flow_cookie = 0;
1331#endif
Zhi Chen8748eb32015-06-18 12:58:48 -07001332#ifdef CONFIG_XFRM
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301333 if (msg->valid_flags & SFE_RULE_CREATE_DIRECTION_VALID) {
1334 original_cm->flow_accel = msg->direction_rule.flow_accel;
1335 } else {
1336 original_cm->flow_accel = 1;
1337 }
Zhi Chen8748eb32015-06-18 12:58:48 -07001338#endif
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301339 /*
1340 * If l2_features are disabled and flow uses l2 features such as macvlan/bridge/pppoe/vlan,
1341 * bottom interfaces are expected to be disabled in the flow rule and always top interfaces
1342 * are used. In such cases, do not use HW csum offload. csum offload is used only when we
1343 * are sending directly to the destination interface that supports it.
1344 */
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301345 if (likely(dest_dev->features & NETIF_F_HW_CSUM) && sfe_dev_has_hw_csum(dest_dev)) {
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301346 if ((msg->conn_rule.return_top_interface_num == msg->conn_rule.return_interface_num) ||
1347 (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_RETURN_BOTTOM_INTERFACE)) {
Ratheesh Kannoth48445532022-02-07 16:19:00 +05301348
1349 /*
1350 * Dont enable CSUM offload
1351 */
1352#if 0
Suruchi Sumanf2077182022-01-13 21:35:23 +05301353 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_CSUM_OFFLOAD;
Ratheesh Kannoth48445532022-02-07 16:19:00 +05301354#endif
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301355 }
1356 }
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001357
Wayne Tanbb7f1782021-12-13 11:16:04 -08001358 reply_cm->l2_hdr_size = 0;
Ratheesh Kannoth5dee3772022-01-18 11:27:14 +05301359 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_SRC_INTERFACE_CHECK) {
1360 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_SRC_INTERFACE_CHECK;
1361 }
1362
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +05301363 reply_cm->flags = 0;
1364
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001365 /*
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301366 * Adding PPPoE parameters to original and reply entries based on the direction where
1367 * PPPoE header is valid in ECM rule.
1368 *
1369 * If PPPoE is valid in flow direction (from interface is PPPoE), then
1370 * original cm will have PPPoE at ingress (strip PPPoE header)
1371 * reply cm will have PPPoE at egress (add PPPoE header)
1372 *
1373 * If PPPoE is valid in return direction (to interface is PPPoE), then
1374 * original cm will have PPPoE at egress (add PPPoE header)
1375 * reply cm will have PPPoE at ingress (strip PPPoE header)
1376 */
1377 if (msg->valid_flags & SFE_RULE_CREATE_PPPOE_DECAP_VALID) {
1378 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_DECAP;
1379 original_cm->pppoe_session_id = msg->pppoe_rule.flow_pppoe_session_id;
1380 ether_addr_copy(original_cm->pppoe_remote_mac, msg->pppoe_rule.flow_pppoe_remote_mac);
1381
1382 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_ENCAP;
Wayne Tanbb7f1782021-12-13 11:16:04 -08001383 reply_cm->l2_hdr_size += SFE_PPPOE_SESSION_HEADER_SIZE;
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301384 reply_cm->pppoe_session_id = msg->pppoe_rule.flow_pppoe_session_id;
1385 ether_addr_copy(reply_cm->pppoe_remote_mac, msg->pppoe_rule.flow_pppoe_remote_mac);
1386 }
1387
1388 if (msg->valid_flags & SFE_RULE_CREATE_PPPOE_ENCAP_VALID) {
1389 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_ENCAP;
Wayne Tanbb7f1782021-12-13 11:16:04 -08001390 original_cm->l2_hdr_size += SFE_PPPOE_SESSION_HEADER_SIZE;
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301391 original_cm->pppoe_session_id = msg->pppoe_rule.return_pppoe_session_id;
1392 ether_addr_copy(original_cm->pppoe_remote_mac, msg->pppoe_rule.return_pppoe_remote_mac);
1393
1394 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_DECAP;
1395 reply_cm->pppoe_session_id = msg->pppoe_rule.return_pppoe_session_id;
1396 ether_addr_copy(reply_cm->pppoe_remote_mac, msg->pppoe_rule.return_pppoe_remote_mac);
1397 }
1398
Ratheesh Kannoth5dee3772022-01-18 11:27:14 +05301399 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_SRC_INTERFACE_CHECK) {
1400 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_SRC_INTERFACE_CHECK;
1401 }
1402
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301403 /*
Ken Zhubbf49652021-09-12 15:33:09 -07001404 * For the non-arp interface, we don't write L2 HDR.
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001405 */
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +05301406 if (sfe_ipv4_xmit_eth_type_check(dest_dev, original_cm->flags)) {
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301407
1408 /*
1409 * Check whether the rule has configured a specific source MAC address to use.
1410 * This is needed when virtual L3 interfaces such as br-lan, macvlan, vlan are used during egress
1411 */
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301412
1413 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1414 ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)msg->conn_rule.flow_mac);
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301415 } else {
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301416 if ((msg->valid_flags & SFE_RULE_CREATE_SRC_MAC_VALID) &&
1417 (msg->src_mac_rule.mac_valid_flags & SFE_SRC_MAC_RETURN_VALID)) {
1418 ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)msg->src_mac_rule.return_src_mac);
1419 } else {
1420 ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)dest_dev->dev_addr);
1421 }
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301422 }
1423
1424 ether_addr_copy((u8 *)original_cm->xmit_dest_mac, (u8 *)msg->conn_rule.return_mac);
1425
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001426 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_L2_HDR;
Wayne Tanbb7f1782021-12-13 11:16:04 -08001427 original_cm->l2_hdr_size += ETH_HLEN;
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001428
1429 /*
1430 * If our dev writes Ethernet headers then we can write a really fast
1431 * version.
1432 */
1433 if (dest_dev->header_ops) {
1434 if (dest_dev->header_ops->create == eth_header) {
1435 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR;
1436 }
1437 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001438 }
1439
1440 /*
1441 * Fill in the "reply" direction connection matching object.
1442 */
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001443 reply_cm->match_dev = dest_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301444 reply_cm->match_protocol = tuple->protocol;
1445 reply_cm->match_src_ip = msg->conn_rule.return_ip_xlate;
Amitesh Anand63be37d2021-12-24 20:51:48 +05301446
1447 /*
1448 * Keep source port as 0 for VxLAN tunnels.
1449 */
1450 if (netif_is_vxlan(src_dev) || netif_is_vxlan(dest_dev)) {
1451 reply_cm->match_src_port = 0;
1452 } else {
1453 reply_cm->match_src_port = msg->conn_rule.return_ident_xlate;
1454 }
1455
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301456 reply_cm->match_dest_ip = msg->conn_rule.flow_ip_xlate;
1457 reply_cm->match_dest_port = msg->conn_rule.flow_ident_xlate;
1458
1459 reply_cm->xlate_src_ip = tuple->return_ip;
1460 reply_cm->xlate_src_port = tuple->return_ident;
1461 reply_cm->xlate_dest_ip = tuple->flow_ip;
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301462 reply_cm->xlate_dest_port = tuple->flow_ident;
1463
1464 if (tuple->protocol == IPPROTO_GRE) {
1465 /*
1466 * the PPTP is 4 tuple lookup.
1467 * During th rule lookup destination call id from packet
1468 * is matched against destination port in cm.
1469 */
1470 reply_cm->match_src_port = 0;
1471 reply_cm->xlate_src_port = 0;
1472 }
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301473
1474 atomic_set(&reply_cm->rx_packet_count, 0);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001475 reply_cm->rx_packet_count64 = 0;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301476 atomic_set(&reply_cm->rx_byte_count, 0);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001477 reply_cm->rx_byte_count64 = 0;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301478
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001479 reply_cm->xmit_dev = src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301480 reply_cm->xmit_dev_mtu = msg->conn_rule.flow_mtu;
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301481
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001482 reply_cm->connection = c;
1483 reply_cm->counter_match = original_cm;
Ken Zhu37040ea2021-09-09 21:11:15 -07001484
Ken Zhu37040ea2021-09-09 21:11:15 -07001485 if (msg->valid_flags & SFE_RULE_CREATE_MARK_VALID) {
1486 reply_cm->mark = msg->mark_rule.return_mark;
1487 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_MARK;
1488 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301489 if (msg->valid_flags & SFE_RULE_CREATE_QOS_VALID) {
1490 reply_cm->priority = msg->qos_rule.return_qos_tag;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001491 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PRIORITY_REMARK;
1492 }
Wayne Tanbb7f1782021-12-13 11:16:04 -08001493
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301494 if (msg->valid_flags & SFE_RULE_CREATE_DSCP_MARKING_VALID) {
1495 reply_cm->dscp = msg->dscp_rule.return_dscp << SFE_IPV4_DSCP_SHIFT;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001496 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_DSCP_REMARK;
1497 }
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301498 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1499 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_BRIDGE_FLOW;
1500 }
Ken Zhu7e38d1a2021-11-30 17:31:46 -08001501 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_RETURN_TRANSMIT_FAST) {
1502 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_FAST_XMIT_DEV_ADMISSION;
1503 }
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301504
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301505 if ((IPPROTO_GRE == tuple->protocol) && !sfe_ipv4_is_local_ip(si, reply_cm->match_dest_ip)) {
1506 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PASSTHROUGH;
1507 }
1508
Amitesh Anand63be37d2021-12-24 20:51:48 +05301509 /*
Parikshit Guned31a8202022-01-05 22:15:04 +05301510 * Mark SAWF metadata in reply match if the sawf tag is valid.
1511 */
1512 reply_cm->sawf_valid = false;
1513 return_sawf_tag = SFE_GET_SAWF_TAG(msg->sawf_rule.return_mark);
1514 if (likely(SFE_SAWF_TAG_IS_VALID(return_sawf_tag))) {
1515 reply_cm->mark = msg->sawf_rule.return_mark;
1516 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_MARK;
1517 reply_cm->sawf_valid = true;
1518 }
1519
1520 /*
Amitesh Anand63be37d2021-12-24 20:51:48 +05301521 * Setup UDP Socket if found to be valid for decap.
1522 */
1523 RCU_INIT_POINTER(reply_cm->up, NULL);
1524 net = dev_net(reply_cm->match_dev);
1525 src_if_idx = src_dev->ifindex;
1526
1527 rcu_read_lock();
1528
1529 /*
1530 * Look for the associated sock object.
1531 * __udp4_lib_lookup() holds a reference for this sock object,
1532 * which will be released in sfe_ipv4_free_connection_rcu()
1533 */
1534#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
1535 sk = __udp4_lib_lookup(net, reply_cm->match_dest_ip, reply_cm->match_dest_port,
1536 reply_cm->xlate_src_ip, reply_cm->xlate_src_port, src_if_idx, &udp_table);
1537#else
1538 sk = __udp4_lib_lookup(net, reply_cm->match_dest_ip, reply_cm->match_dest_port,
1539 reply_cm->xlate_src_ip, reply_cm->xlate_src_port, src_if_idx, 0, &udp_table, NULL);
1540#endif
1541
1542 rcu_read_unlock();
1543
1544 /*
1545 * We set the UDP sock pointer as valid only for decap direction.
1546 */
1547 if (sk && udp_sk(sk)->encap_type) {
1548#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
1549 if (!atomic_add_unless(&sk->sk_refcnt, 1, 0)) {
1550#else
1551 if (!refcount_inc_not_zero(&sk->sk_refcnt)) {
1552#endif
Wayne Tanbb7f1782021-12-13 11:16:04 -08001553 spin_unlock_bh(&si->lock);
Amitesh Anand63be37d2021-12-24 20:51:48 +05301554 kfree(reply_cm);
1555 kfree(original_cm);
1556 kfree(c);
1557
1558 DEBUG_TRACE("%px: sfe: unable to take reference for socket(%px) p:%d\n"
1559 " s: %s:%pM:%pI4:%u, d: %s:%pM:%pI4:%u\n",
1560 msg, sk, tuple->protocol,
1561 src_dev->name, msg->conn_rule.flow_mac, &tuple->flow_ip, ntohs(tuple->flow_ident),
1562 dest_dev->name, msg->conn_rule.return_mac, &tuple->return_ip, ntohs(tuple->return_ident));
1563
1564 dev_put(src_dev);
1565 dev_put(dest_dev);
1566
1567 return -ESHUTDOWN;
1568 }
1569
1570 rcu_assign_pointer(reply_cm->up, udp_sk(sk));
1571
1572 DEBUG_INFO("%px: Sock(%px) lookup success with reply_cm direction\n", msg, sk);
1573 DEBUG_INFO("%px: SFE connection -\n"
1574 " s: %s:%pI4(%pI4):%u(%u)\n"
1575 " d: %s:%pI4(%pI4):%u(%u)\n",
1576 msg, reply_cm->match_dev->name, &reply_cm->match_src_ip, &reply_cm->xlate_src_ip,
1577 ntohs(reply_cm->match_src_port), ntohs(reply_cm->xlate_src_port),
1578 reply_cm->xmit_dev->name, &reply_cm->match_dest_ip, &reply_cm->xlate_dest_ip,
1579 ntohs(reply_cm->match_dest_port), ntohs(reply_cm->xlate_dest_port));
1580 }
1581
Wayne Tanbb7f1782021-12-13 11:16:04 -08001582 /*
1583 * Add VLAN rule to reply_cm
1584 */
1585 if (msg->valid_flags & SFE_RULE_CREATE_VLAN_VALID) {
1586 struct sfe_vlan_rule *vlan_primary_rule = &msg->vlan_primary_rule;
1587 struct sfe_vlan_rule *vlan_secondary_rule = &msg->vlan_secondary_rule;
1588 sfe_ipv4_match_entry_set_vlan(reply_cm,
1589 vlan_primary_rule->egress_vlan_tag,
1590 vlan_primary_rule->ingress_vlan_tag,
1591 vlan_secondary_rule->egress_vlan_tag,
1592 vlan_secondary_rule->ingress_vlan_tag);
1593
1594 if ((msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_FLOW_BOTTOM_INTERFACE) &&
1595 reply_cm->egress_vlan_hdr_cnt > 0) {
1596 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_INSERT_EGRESS_VLAN_TAG;
1597 reply_cm->l2_hdr_size += reply_cm->egress_vlan_hdr_cnt * VLAN_HLEN;
1598 }
1599 }
1600
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301601 /*
1602 * the net_protocol handler will be used only in decap path
1603 * for non passthrough case.
1604 */
1605 original_cm->proto = NULL;
1606 reply_cm->proto = NULL;
1607
1608#ifdef SFE_GRE_TUN_ENABLE
1609 if ((IPPROTO_GRE == tuple->protocol) && !(reply_cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_PASSTHROUGH)) {
1610 rcu_read_lock();
1611 reply_cm->proto = rcu_dereference(inet_protos[IPPROTO_GRE]);
1612 rcu_read_unlock();
1613
1614 if (unlikely(!reply_cm->proto)) {
1615 kfree(reply_cm);
1616 kfree(original_cm);
1617 kfree(c);
1618 dev_put(src_dev);
1619 dev_put(dest_dev);
1620 DEBUG_WARN("sfe: GRE proto handler is not registered\n");
1621 return -EPERM;
1622 }
1623 }
1624#endif
1625
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001626#ifdef CONFIG_NF_FLOW_COOKIE
1627 reply_cm->flow_cookie = 0;
1628#endif
Zhi Chen8748eb32015-06-18 12:58:48 -07001629#ifdef CONFIG_XFRM
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301630 if (msg->valid_flags & SFE_RULE_CREATE_DIRECTION_VALID) {
1631 reply_cm->flow_accel = msg->direction_rule.return_accel;
1632 } else {
1633 reply_cm->flow_accel = 1;
1634 }
1635
Zhi Chen8748eb32015-06-18 12:58:48 -07001636#endif
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301637 /*
1638 * If l2_features are disabled and flow uses l2 features such as macvlan/bridge/pppoe/vlan,
1639 * bottom interfaces are expected to be disabled in the flow rule and always top interfaces
1640 * are used. In such cases, do not use HW csum offload. csum offload is used only when we
1641 * are sending directly to the destination interface that supports it.
1642 */
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301643 if (likely(src_dev->features & NETIF_F_HW_CSUM) && sfe_dev_has_hw_csum(src_dev)) {
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301644 if ((msg->conn_rule.flow_top_interface_num == msg->conn_rule.flow_interface_num) ||
1645 (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_FLOW_BOTTOM_INTERFACE)) {
Ratheesh Kannoth48445532022-02-07 16:19:00 +05301646 /*
1647 * Dont enable CSUM offload
1648 */
1649#if 0
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301650 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_CSUM_OFFLOAD;
Ratheesh Kannoth48445532022-02-07 16:19:00 +05301651#endif
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301652 }
1653 }
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001654
1655 /*
Ken Zhubbf49652021-09-12 15:33:09 -07001656 * For the non-arp interface, we don't write L2 HDR.
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001657 */
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +05301658 if (sfe_ipv4_xmit_eth_type_check(src_dev, reply_cm->flags)) {
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301659
1660 /*
1661 * Check whether the rule has configured a specific source MAC address to use.
1662 * This is needed when virtual L3 interfaces such as br-lan, macvlan, vlan are used during egress
1663 */
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301664
1665 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1666 ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)msg->conn_rule.return_mac);
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301667 } else {
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301668 if ((msg->valid_flags & SFE_RULE_CREATE_SRC_MAC_VALID) &&
1669 (msg->src_mac_rule.mac_valid_flags & SFE_SRC_MAC_FLOW_VALID)) {
1670 ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)msg->src_mac_rule.flow_src_mac);
1671 } else {
1672 ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)src_dev->dev_addr);
1673 }
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301674 }
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301675
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301676 ether_addr_copy((u8 *)reply_cm->xmit_dest_mac, (u8 *)msg->conn_rule.flow_mac);
1677
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001678 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_L2_HDR;
Wayne Tanbb7f1782021-12-13 11:16:04 -08001679 reply_cm->l2_hdr_size += ETH_HLEN;
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001680
1681 /*
1682 * If our dev writes Ethernet headers then we can write a really fast
1683 * version.
1684 */
1685 if (src_dev->header_ops) {
1686 if (src_dev->header_ops->create == eth_header) {
1687 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR;
1688 }
1689 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001690 }
1691
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301692 if ((tuple->return_ip != msg->conn_rule.return_ip_xlate) ||
1693 (tuple->return_ident != msg->conn_rule.return_ident_xlate)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001694 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST;
1695 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC;
1696 }
1697
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301698 if ((tuple->flow_ip != msg->conn_rule.flow_ip_xlate) ||
1699 (tuple->flow_ident != msg->conn_rule.flow_ident_xlate)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001700 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC;
1701 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST;
1702 }
1703
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001704 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001705 * Initialize the protocol-specific information that we track.
1706 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301707 switch (tuple->protocol) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001708 case IPPROTO_TCP:
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301709 original_cm->protocol_state.tcp.win_scale = msg->tcp_rule.flow_window_scale;
1710 original_cm->protocol_state.tcp.max_win = msg->tcp_rule.flow_max_window ? msg->tcp_rule.flow_max_window : 1;
1711 original_cm->protocol_state.tcp.end = msg->tcp_rule.flow_end;
1712 original_cm->protocol_state.tcp.max_end = msg->tcp_rule.flow_max_end;
1713
1714 reply_cm->protocol_state.tcp.win_scale = msg->tcp_rule.return_window_scale;
1715 reply_cm->protocol_state.tcp.max_win = msg->tcp_rule.return_max_window ? msg->tcp_rule.return_max_window : 1;
1716 reply_cm->protocol_state.tcp.end = msg->tcp_rule.return_end;
1717 reply_cm->protocol_state.tcp.max_end = msg->tcp_rule.return_max_end;
1718
1719 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_NO_SEQ_CHECK) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001720 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
1721 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
1722 }
1723 break;
1724 }
1725
Wayne Tanbb7f1782021-12-13 11:16:04 -08001726 /*
1727 * Fill in the ipv4_connection object.
1728 */
1729 c->protocol = tuple->protocol;
1730 c->src_ip = tuple->flow_ip;
1731 c->src_ip_xlate = msg->conn_rule.flow_ip_xlate;
1732 c->src_port = tuple->flow_ident;
1733 c->src_port_xlate = msg->conn_rule.flow_ident_xlate;
1734 c->original_dev = src_dev;
1735 c->original_match = original_cm;
1736 c->dest_ip = tuple->return_ip;
1737 c->dest_ip_xlate = msg->conn_rule.return_ip_xlate;
1738 c->dest_port = tuple->return_ident;
1739 c->dest_port_xlate = msg->conn_rule.return_ident_xlate;
1740 c->reply_dev = dest_dev;
1741 c->reply_match = reply_cm;
1742 c->debug_read_seq = 0;
1743 c->last_sync_jiffies = get_jiffies_64();
1744 c->removed = false;
1745
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001746 sfe_ipv4_connection_match_compute_translations(original_cm);
1747 sfe_ipv4_connection_match_compute_translations(reply_cm);
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301748 sfe_ipv4_insert_connection(si, c);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001749
1750 spin_unlock_bh(&si->lock);
1751
1752 /*
1753 * We have everything we need!
1754 */
Wayne Tanbb7f1782021-12-13 11:16:04 -08001755 DEBUG_INFO("NEW connection - p: %d\n"
1756 "original_cm: match_dev=src_dev: %s %d %pM\n"
1757 " xmit_dev=dest_dev: %s %d %pM\n"
1758 " xmit_src_mac: %pM\n"
1759 " xmit_dest_mac: %pM\n"
1760 " flags: %x l2_hdr: %u\n"
1761 "flow_ip: %pI4:%u\n"
1762 "flow_ip_xlate: %pI4:%u\n"
1763 "flow_mac: %pM\n"
1764 "reply_cm: match_dev=dest_dev: %s %d %pM\n"
1765 " xmit_dev=src_dev: %s %d %pM\n"
1766 " xmit_src_mac: %pM\n"
1767 " xmit_dest_mac: %pM\n"
1768 " flags: %x l2_hdr: %u\n"
1769 "return_ip: %pI4:%u\n"
1770 "return_ip_xlate: %pI4:%u\n"
1771 "return_mac: %pM\n"
1772 "flags: valid=%x src_mac_valid=%x\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301773 tuple->protocol,
Wayne Tanbb7f1782021-12-13 11:16:04 -08001774 original_cm->match_dev->name, original_cm->match_dev->ifindex, original_cm->match_dev->dev_addr,
1775 original_cm->xmit_dev->name, original_cm->xmit_dev->ifindex, original_cm->xmit_dev->dev_addr,
1776 original_cm->xmit_src_mac, original_cm->xmit_dest_mac, original_cm->flags, original_cm->l2_hdr_size,
1777 &tuple->flow_ip, ntohs(tuple->flow_ident),
1778 &msg->conn_rule.flow_ip_xlate, ntohs(msg->conn_rule.flow_ident_xlate),
1779 msg->conn_rule.flow_mac,
1780 reply_cm->match_dev->name, reply_cm->match_dev->ifindex, reply_cm->match_dev->dev_addr,
1781 reply_cm->xmit_dev->name, reply_cm->xmit_dev->ifindex, reply_cm->xmit_dev->dev_addr,
1782 reply_cm->xmit_src_mac, reply_cm->xmit_dest_mac, reply_cm->flags, reply_cm->l2_hdr_size,
1783 &tuple->return_ip, ntohs(tuple->return_ident),
1784 &msg->conn_rule.return_ip_xlate, ntohs(msg->conn_rule.return_ident_xlate),
1785 msg->conn_rule.return_mac,
1786 msg->valid_flags, msg->src_mac_rule.mac_valid_flags);
Nicolas Costa514fde02014-01-13 15:50:29 -06001787
1788 return 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001789}
1790
1791/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001792 * sfe_ipv4_destroy_rule()
1793 * Destroy a forwarding rule.
1794 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301795void sfe_ipv4_destroy_rule(struct sfe_ipv4_rule_destroy_msg *msg)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001796{
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001797 struct sfe_ipv4 *si = &__si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001798 struct sfe_ipv4_connection *c;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301799 bool ret;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301800 struct sfe_ipv4_5tuple *tuple = &msg->tuple;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001801
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301802 this_cpu_inc(si->stats_pcpu->connection_destroy_requests64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001803 spin_lock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001804
1805 /*
1806 * Check to see if we have a flow that matches the rule we're trying
1807 * to destroy. If there isn't then we can't destroy it.
1808 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301809 c = sfe_ipv4_find_connection(si, tuple->protocol, tuple->flow_ip, tuple->flow_ident,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301810 tuple->return_ip, tuple->return_ident);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001811 if (!c) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001812 spin_unlock_bh(&si->lock);
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301813 this_cpu_inc(si->stats_pcpu->connection_destroy_misses64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001814
1815 DEBUG_TRACE("connection does not exist - p: %d, s: %pI4:%u, d: %pI4:%u\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301816 tuple->protocol, &tuple->flow_ip, ntohs(tuple->flow_ident),
1817 &tuple->return_ip, ntohs(tuple->return_ident));
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001818 return;
1819 }
1820
1821 /*
1822 * Remove our connection details from the hash tables.
1823 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301824 ret = sfe_ipv4_remove_connection(si, c);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001825 spin_unlock_bh(&si->lock);
1826
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301827 if (ret) {
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301828 sfe_ipv4_flush_connection(si, c, SFE_SYNC_REASON_DESTROY);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301829 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001830
1831 DEBUG_INFO("connection destroyed - p: %d, s: %pI4:%u, d: %pI4:%u\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301832 tuple->protocol, &tuple->flow_ip, ntohs(tuple->flow_ident),
1833 &tuple->return_ip, ntohs(tuple->return_ident));
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001834}
1835
1836/*
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001837 * sfe_ipv4_register_sync_rule_callback()
1838 * Register a callback for rule synchronization.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001839 */
Xiaoping Fand44a5b42015-05-26 17:37:37 -07001840void sfe_ipv4_register_sync_rule_callback(sfe_sync_rule_callback_t sync_rule_callback)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001841{
1842 struct sfe_ipv4 *si = &__si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001843
1844 spin_lock_bh(&si->lock);
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001845 rcu_assign_pointer(si->sync_rule_callback, sync_rule_callback);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001846 spin_unlock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001847}
1848
1849/*
1850 * sfe_ipv4_get_debug_dev()
1851 */
1852static ssize_t sfe_ipv4_get_debug_dev(struct device *dev,
1853 struct device_attribute *attr,
1854 char *buf)
1855{
1856 struct sfe_ipv4 *si = &__si;
1857 ssize_t count;
1858 int num;
1859
1860 spin_lock_bh(&si->lock);
1861 num = si->debug_dev;
1862 spin_unlock_bh(&si->lock);
1863
1864 count = snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", num);
1865 return count;
1866}
1867
1868/*
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001869 * sysfs attributes.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001870 */
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001871static const struct device_attribute sfe_ipv4_debug_dev_attr =
Xiaoping Fane70da412016-02-26 16:47:57 -08001872 __ATTR(debug_dev, S_IWUSR | S_IRUGO, sfe_ipv4_get_debug_dev, NULL);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001873
1874/*
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001875 * sfe_ipv4_destroy_all_rules_for_dev()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001876 * Destroy all connections that match a particular device.
1877 *
1878 * If we pass dev as NULL then this destroys all connections.
1879 */
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001880void sfe_ipv4_destroy_all_rules_for_dev(struct net_device *dev)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001881{
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001882 struct sfe_ipv4 *si = &__si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001883 struct sfe_ipv4_connection *c;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301884 bool ret;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001885
Xiaoping Fan34586472015-07-03 02:20:35 -07001886another_round:
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001887 spin_lock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001888
Xiaoping Fan34586472015-07-03 02:20:35 -07001889 for (c = si->all_connections_head; c; c = c->all_connections_next) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001890 /*
Xiaoping Fan34586472015-07-03 02:20:35 -07001891 * Does this connection relate to the device we are destroying?
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001892 */
1893 if (!dev
1894 || (dev == c->original_dev)
1895 || (dev == c->reply_dev)) {
Xiaoping Fan34586472015-07-03 02:20:35 -07001896 break;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001897 }
Xiaoping Fan34586472015-07-03 02:20:35 -07001898 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001899
Xiaoping Fan34586472015-07-03 02:20:35 -07001900 if (c) {
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301901 ret = sfe_ipv4_remove_connection(si, c);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001902 }
1903
1904 spin_unlock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07001905
1906 if (c) {
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301907 if (ret) {
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301908 sfe_ipv4_flush_connection(si, c, SFE_SYNC_REASON_DESTROY);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301909 }
Xiaoping Fan34586472015-07-03 02:20:35 -07001910 goto another_round;
1911 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001912}
1913
1914/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001915 * sfe_ipv4_periodic_sync()
1916 */
Ken Zhu137722d2021-09-23 17:57:36 -07001917static void sfe_ipv4_periodic_sync(struct work_struct *work)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001918{
Ken Zhu137722d2021-09-23 17:57:36 -07001919 struct sfe_ipv4 *si = container_of((struct delayed_work *)work, struct sfe_ipv4, sync_dwork);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07001920 u64 now_jiffies;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001921 int quota;
Xiaoping Fand44a5b42015-05-26 17:37:37 -07001922 sfe_sync_rule_callback_t sync_rule_callback;
Ken Zhudc423672021-09-02 18:27:01 -07001923 struct sfe_ipv4_connection *c;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001924
1925 now_jiffies = get_jiffies_64();
1926
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001927 rcu_read_lock();
1928 sync_rule_callback = rcu_dereference(si->sync_rule_callback);
1929 if (!sync_rule_callback) {
1930 rcu_read_unlock();
1931 goto done;
1932 }
1933
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001934 spin_lock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001935
1936 /*
Ken Zhudc423672021-09-02 18:27:01 -07001937 * If we have reached the end of the connection list, walk from
1938 * the connection head.
1939 */
1940 c = si->wc_next;
1941 if (unlikely(!c)) {
1942 c = si->all_connections_head;
1943 }
1944
1945 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001946 * Get an estimate of the number of connections to parse in this sync.
1947 */
1948 quota = (si->num_connections + 63) / 64;
1949
1950 /*
Ken Zhudc423672021-09-02 18:27:01 -07001951 * Walk the "all connection" list and sync the connection state.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001952 */
Ken Zhudc423672021-09-02 18:27:01 -07001953 while (likely(c && quota)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001954 struct sfe_ipv4_connection_match *cm;
1955 struct sfe_ipv4_connection_match *counter_cm;
Xiaoping Fand44a5b42015-05-26 17:37:37 -07001956 struct sfe_connection_sync sis;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001957
Ken Zhudc423672021-09-02 18:27:01 -07001958 cm = c->original_match;
1959 counter_cm = c->reply_match;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001960
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001961 /*
Ken Zhudc423672021-09-02 18:27:01 -07001962 * Didn't receive packets in the original direction or reply
1963 * direction, move to the next connection.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001964 */
Ken Zhudc423672021-09-02 18:27:01 -07001965 if ((!atomic_read(&cm->rx_packet_count)) && !(atomic_read(&counter_cm->rx_packet_count))) {
1966 c = c->all_connections_next;
1967 continue;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001968 }
1969
Ken Zhudc423672021-09-02 18:27:01 -07001970 quota--;
Matthew McClintockaf48f1e2014-01-23 15:29:19 -06001971
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301972 sfe_ipv4_gen_sync_connection(si, c, &sis, SFE_SYNC_REASON_STATS, now_jiffies);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001973
Ken Zhudc423672021-09-02 18:27:01 -07001974 si->wc_next = c->all_connections_next;
1975
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001976 /*
1977 * We don't want to be holding the lock when we sync!
1978 */
1979 spin_unlock_bh(&si->lock);
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001980 sync_rule_callback(&sis);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001981 spin_lock_bh(&si->lock);
Ken Zhudc423672021-09-02 18:27:01 -07001982
1983 /*
1984 * c must be set and used in the same lock/unlock window;
1985 * because c could be removed when we don't hold the lock,
1986 * so delay grabbing until after the callback and relock.
1987 */
1988 c = si->wc_next;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001989 }
1990
Ken Zhudc423672021-09-02 18:27:01 -07001991 /*
1992 * At the end of the sync, put the wc_next to the connection we left.
1993 */
1994 si->wc_next = c;
1995
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001996 spin_unlock_bh(&si->lock);
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001997 rcu_read_unlock();
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001998
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001999done:
Ken Zhu137722d2021-09-23 17:57:36 -07002000 schedule_delayed_work_on(si->work_cpu, (struct delayed_work *)work, ((HZ + 99) / 100));
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002001}
2002
2003#define CHAR_DEV_MSG_SIZE 768
2004
2005/*
2006 * sfe_ipv4_debug_dev_read_start()
2007 * Generate part of the XML output.
2008 */
2009static bool sfe_ipv4_debug_dev_read_start(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2010 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2011{
2012 int bytes_read;
2013
Xiaoping Fan34586472015-07-03 02:20:35 -07002014 si->debug_read_seq++;
2015
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002016 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "<sfe_ipv4>\n");
2017 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2018 return false;
2019 }
2020
2021 *length -= bytes_read;
2022 *total_read += bytes_read;
2023
2024 ws->state++;
2025 return true;
2026}
2027
2028/*
2029 * sfe_ipv4_debug_dev_read_connections_start()
2030 * Generate part of the XML output.
2031 */
2032static bool sfe_ipv4_debug_dev_read_connections_start(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2033 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2034{
2035 int bytes_read;
2036
2037 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<connections>\n");
2038 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2039 return false;
2040 }
2041
2042 *length -= bytes_read;
2043 *total_read += bytes_read;
2044
2045 ws->state++;
2046 return true;
2047}
2048
2049/*
2050 * sfe_ipv4_debug_dev_read_connections_connection()
2051 * Generate part of the XML output.
2052 */
2053static bool sfe_ipv4_debug_dev_read_connections_connection(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2054 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2055{
2056 struct sfe_ipv4_connection *c;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002057 struct sfe_ipv4_connection_match *original_cm;
2058 struct sfe_ipv4_connection_match *reply_cm;
2059 int bytes_read;
2060 int protocol;
2061 struct net_device *src_dev;
Dave Hudson87973cd2013-10-22 16:00:04 +01002062 __be32 src_ip;
2063 __be32 src_ip_xlate;
2064 __be16 src_port;
2065 __be16 src_port_xlate;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07002066 u64 src_rx_packets;
2067 u64 src_rx_bytes;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002068 struct net_device *dest_dev;
Dave Hudson87973cd2013-10-22 16:00:04 +01002069 __be32 dest_ip;
2070 __be32 dest_ip_xlate;
2071 __be16 dest_port;
2072 __be16 dest_port_xlate;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07002073 u64 dest_rx_packets;
2074 u64 dest_rx_bytes;
2075 u64 last_sync_jiffies;
Ken Zhu37040ea2021-09-09 21:11:15 -07002076 u32 src_mark, dest_mark, src_priority, dest_priority, src_dscp, dest_dscp;
Parikshit Guned31a8202022-01-05 22:15:04 +05302077 bool original_cm_sawf_valid, reply_cm_sawf_valid;
2078 u32 flow_service_class, return_service_class;
2079 u32 flow_msduq, return_msduq;
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05302080 u32 packet, byte, original_cm_flags;
2081 u16 pppoe_session_id;
2082 u8 pppoe_remote_mac[ETH_ALEN];
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002083 u32 original_fast_xmit, reply_fast_xmit;
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08002084#ifdef CONFIG_NF_FLOW_COOKIE
2085 int src_flow_cookie, dst_flow_cookie;
2086#endif
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002087
2088 spin_lock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07002089
2090 for (c = si->all_connections_head; c; c = c->all_connections_next) {
2091 if (c->debug_read_seq < si->debug_read_seq) {
2092 c->debug_read_seq = si->debug_read_seq;
2093 break;
2094 }
2095 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002096
2097 /*
Xiaoping Fan34586472015-07-03 02:20:35 -07002098 * If there were no connections then move to the next state.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002099 */
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05302100 if (!c || c->removed) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002101 spin_unlock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07002102 ws->state++;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002103 return true;
2104 }
2105
2106 original_cm = c->original_match;
2107 reply_cm = c->reply_match;
2108
2109 protocol = c->protocol;
2110 src_dev = c->original_dev;
2111 src_ip = c->src_ip;
2112 src_ip_xlate = c->src_ip_xlate;
2113 src_port = c->src_port;
2114 src_port_xlate = c->src_port_xlate;
Xiaoping Fane1963d42015-08-25 17:06:19 -07002115 src_priority = original_cm->priority;
2116 src_dscp = original_cm->dscp >> SFE_IPV4_DSCP_SHIFT;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002117
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05302118 sfe_ipv4_connection_match_update_summary_stats(original_cm, &packet, &byte);
2119 sfe_ipv4_connection_match_update_summary_stats(reply_cm, &packet, &byte);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002120
2121 src_rx_packets = original_cm->rx_packet_count64;
2122 src_rx_bytes = original_cm->rx_byte_count64;
Ken Zhu37040ea2021-09-09 21:11:15 -07002123 src_mark = original_cm->mark;
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002124 original_fast_xmit = (original_cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_FAST_XMIT);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002125 dest_dev = c->reply_dev;
2126 dest_ip = c->dest_ip;
2127 dest_ip_xlate = c->dest_ip_xlate;
2128 dest_port = c->dest_port;
2129 dest_port_xlate = c->dest_port_xlate;
Xiaoping Fane1963d42015-08-25 17:06:19 -07002130 dest_priority = reply_cm->priority;
2131 dest_dscp = reply_cm->dscp >> SFE_IPV4_DSCP_SHIFT;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002132 dest_rx_packets = reply_cm->rx_packet_count64;
2133 dest_rx_bytes = reply_cm->rx_byte_count64;
Ken Zhu37040ea2021-09-09 21:11:15 -07002134 dest_mark = reply_cm->mark;
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002135 reply_fast_xmit = (reply_cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_FAST_XMIT);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002136 last_sync_jiffies = get_jiffies_64() - c->last_sync_jiffies;
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05302137 original_cm_flags = original_cm->flags;
2138 pppoe_session_id = original_cm->pppoe_session_id;
2139 ether_addr_copy(pppoe_remote_mac, original_cm->pppoe_remote_mac);
Parikshit Guned31a8202022-01-05 22:15:04 +05302140 original_cm_sawf_valid = original_cm->sawf_valid;
2141 reply_cm_sawf_valid = reply_cm->sawf_valid;
2142 flow_service_class = SFE_GET_SAWF_SERVICE_CLASS(original_cm->mark);
2143 flow_msduq = SFE_GET_SAWF_MSDUQ(original_cm->mark);
2144 return_service_class = SFE_GET_SAWF_SERVICE_CLASS(reply_cm->mark);
2145 return_msduq = SFE_GET_SAWF_MSDUQ(reply_cm->mark);
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08002146#ifdef CONFIG_NF_FLOW_COOKIE
2147 src_flow_cookie = original_cm->flow_cookie;
2148 dst_flow_cookie = reply_cm->flow_cookie;
2149#endif
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002150 spin_unlock_bh(&si->lock);
2151
2152 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t\t<connection "
2153 "protocol=\"%u\" "
2154 "src_dev=\"%s\" "
2155 "src_ip=\"%pI4\" src_ip_xlate=\"%pI4\" "
2156 "src_port=\"%u\" src_port_xlate=\"%u\" "
Xiaoping Fane1963d42015-08-25 17:06:19 -07002157 "src_priority=\"%u\" src_dscp=\"%u\" "
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002158 "src_rx_pkts=\"%llu\" src_rx_bytes=\"%llu\" "
Ken Zhu37040ea2021-09-09 21:11:15 -07002159 "src_mark=\"%08x\" "
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002160 "src_fast_xmit=\"%s\" "
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002161 "dest_dev=\"%s\" "
2162 "dest_ip=\"%pI4\" dest_ip_xlate=\"%pI4\" "
2163 "dest_port=\"%u\" dest_port_xlate=\"%u\" "
Xiaoping Fane1963d42015-08-25 17:06:19 -07002164 "dest_priority=\"%u\" dest_dscp=\"%u\" "
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002165 "dest_rx_pkts=\"%llu\" dest_rx_bytes=\"%llu\" "
Ken Zhu37040ea2021-09-09 21:11:15 -07002166 "dest_mark=\"%08x\" "
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002167 "reply_fast_xmit=\"%s\" "
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08002168#ifdef CONFIG_NF_FLOW_COOKIE
2169 "src_flow_cookie=\"%d\" dst_flow_cookie=\"%d\" "
2170#endif
Ken Zhu37040ea2021-09-09 21:11:15 -07002171 "last_sync=\"%llu\" ",
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002172 protocol,
2173 src_dev->name,
2174 &src_ip, &src_ip_xlate,
Dave Hudson87973cd2013-10-22 16:00:04 +01002175 ntohs(src_port), ntohs(src_port_xlate),
Xiaoping Fane1963d42015-08-25 17:06:19 -07002176 src_priority, src_dscp,
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002177 src_rx_packets, src_rx_bytes,
Ken Zhu37040ea2021-09-09 21:11:15 -07002178 src_mark,
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002179 original_fast_xmit ? "Yes" : "No",
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002180 dest_dev->name,
2181 &dest_ip, &dest_ip_xlate,
Dave Hudson87973cd2013-10-22 16:00:04 +01002182 ntohs(dest_port), ntohs(dest_port_xlate),
Xiaoping Fane1963d42015-08-25 17:06:19 -07002183 dest_priority, dest_dscp,
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002184 dest_rx_packets, dest_rx_bytes,
Ken Zhu37040ea2021-09-09 21:11:15 -07002185 dest_mark,
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002186 reply_fast_xmit ? "Yes" : "No",
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08002187#ifdef CONFIG_NF_FLOW_COOKIE
2188 src_flow_cookie, dst_flow_cookie,
2189#endif
Ken Zhu37040ea2021-09-09 21:11:15 -07002190 last_sync_jiffies);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002191
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05302192 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 +05302193 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 +05302194 pppoe_session_id, pppoe_remote_mac);
2195 }
2196
Parikshit Guned31a8202022-01-05 22:15:04 +05302197 if (original_cm_sawf_valid) {
2198 bytes_read += snprintf(msg + bytes_read, CHAR_DEV_MSG_SIZE, "flow_service_class=\"%d\" flow_msduq=\"%d\" ",
2199 flow_service_class, flow_msduq);
2200 }
2201
2202 if (reply_cm_sawf_valid) {
2203 bytes_read += snprintf(msg + bytes_read, CHAR_DEV_MSG_SIZE, "return_service_class=\"%d\" return_msduq=\"%d\" ",
2204 return_service_class, return_msduq);
2205 }
2206
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05302207 bytes_read += snprintf(msg + bytes_read, CHAR_DEV_MSG_SIZE, "/>\n");
2208
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002209 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2210 return false;
2211 }
2212
2213 *length -= bytes_read;
2214 *total_read += bytes_read;
2215
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002216 return true;
2217}
2218
2219/*
2220 * sfe_ipv4_debug_dev_read_connections_end()
2221 * Generate part of the XML output.
2222 */
2223static bool sfe_ipv4_debug_dev_read_connections_end(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2224 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2225{
2226 int bytes_read;
2227
2228 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t</connections>\n");
2229 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2230 return false;
2231 }
2232
2233 *length -= bytes_read;
2234 *total_read += bytes_read;
2235
2236 ws->state++;
2237 return true;
2238}
2239
2240/*
2241 * sfe_ipv4_debug_dev_read_exceptions_start()
2242 * Generate part of the XML output.
2243 */
2244static bool sfe_ipv4_debug_dev_read_exceptions_start(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2245 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2246{
2247 int bytes_read;
2248
2249 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<exceptions>\n");
2250 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2251 return false;
2252 }
2253
2254 *length -= bytes_read;
2255 *total_read += bytes_read;
2256
2257 ws->state++;
2258 return true;
2259}
2260
2261/*
2262 * sfe_ipv4_debug_dev_read_exceptions_exception()
2263 * Generate part of the XML output.
2264 */
2265static bool sfe_ipv4_debug_dev_read_exceptions_exception(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2266 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2267{
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302268 int i;
2269 u64 val = 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002270
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302271 for_each_possible_cpu(i) {
2272 const struct sfe_ipv4_stats *s = per_cpu_ptr(si->stats_pcpu, i);
2273 val += s->exception_events64[ws->iter_exception];
2274 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002275
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302276 if (val) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002277 int bytes_read;
2278
2279 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE,
2280 "\t\t<exception name=\"%s\" count=\"%llu\" />\n",
2281 sfe_ipv4_exception_events_string[ws->iter_exception],
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302282 val);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002283 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2284 return false;
2285 }
2286
2287 *length -= bytes_read;
2288 *total_read += bytes_read;
2289 }
2290
2291 ws->iter_exception++;
2292 if (ws->iter_exception >= SFE_IPV4_EXCEPTION_EVENT_LAST) {
2293 ws->iter_exception = 0;
2294 ws->state++;
2295 }
2296
2297 return true;
2298}
2299
2300/*
2301 * sfe_ipv4_debug_dev_read_exceptions_end()
2302 * Generate part of the XML output.
2303 */
2304static bool sfe_ipv4_debug_dev_read_exceptions_end(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2305 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2306{
2307 int bytes_read;
2308
2309 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t</exceptions>\n");
2310 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2311 return false;
2312 }
2313
2314 *length -= bytes_read;
2315 *total_read += bytes_read;
2316
2317 ws->state++;
2318 return true;
2319}
2320
2321/*
2322 * sfe_ipv4_debug_dev_read_stats()
2323 * Generate part of the XML output.
2324 */
2325static bool sfe_ipv4_debug_dev_read_stats(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2326 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2327{
2328 int bytes_read;
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302329 struct sfe_ipv4_stats stats;
2330 unsigned int num_conn;
2331
2332 sfe_ipv4_update_summary_stats(si, &stats);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002333
2334 spin_lock_bh(&si->lock);
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302335 num_conn = si->num_connections;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002336 spin_unlock_bh(&si->lock);
2337
2338 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<stats "
2339 "num_connections=\"%u\" "
Amitesh Anand63be37d2021-12-24 20:51:48 +05302340 "pkts_dropped=\"%llu\" "
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002341 "pkts_fast_xmited=\"%llu\" "
Xiaoping Fan59176422015-05-22 15:58:10 -07002342 "pkts_forwarded=\"%llu\" pkts_not_forwarded=\"%llu\" "
2343 "create_requests=\"%llu\" create_collisions=\"%llu\" "
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05302344 "create_failures=\"%llu\" "
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002345 "destroy_requests=\"%llu\" destroy_misses=\"%llu\" "
2346 "flushes=\"%llu\" "
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +05302347 "hash_hits=\"%llu\" hash_reorders=\"%llu\" "
2348 "pppoe_encap_pkts_fwded=\"%llu\" "
Guduri Prathyusha034d6352022-01-12 16:49:04 +05302349 "pppoe_decap_pkts_fwded=\"%llu\" "
2350 "pppoe_bridge_pkts_fwded=\"%llu\" />\n",
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302351 num_conn,
Amitesh Anand63be37d2021-12-24 20:51:48 +05302352 stats.packets_dropped64,
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002353 stats.packets_fast_xmited64,
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302354 stats.packets_forwarded64,
2355 stats.packets_not_forwarded64,
2356 stats.connection_create_requests64,
2357 stats.connection_create_collisions64,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05302358 stats.connection_create_failures64,
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302359 stats.connection_destroy_requests64,
2360 stats.connection_destroy_misses64,
2361 stats.connection_flushes64,
2362 stats.connection_match_hash_hits64,
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +05302363 stats.connection_match_hash_reorders64,
2364 stats.pppoe_encap_packets_forwarded64,
Guduri Prathyusha034d6352022-01-12 16:49:04 +05302365 stats.pppoe_decap_packets_forwarded64,
2366 stats.pppoe_bridge_packets_forwarded64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002367 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2368 return false;
2369 }
2370
2371 *length -= bytes_read;
2372 *total_read += bytes_read;
2373
2374 ws->state++;
2375 return true;
2376}
2377
2378/*
2379 * sfe_ipv4_debug_dev_read_end()
2380 * Generate part of the XML output.
2381 */
2382static bool sfe_ipv4_debug_dev_read_end(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2383 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2384{
2385 int bytes_read;
2386
2387 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "</sfe_ipv4>\n");
2388 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2389 return false;
2390 }
2391
2392 *length -= bytes_read;
2393 *total_read += bytes_read;
2394
2395 ws->state++;
2396 return true;
2397}
2398
2399/*
2400 * Array of write functions that write various XML elements that correspond to
2401 * our XML output state machine.
2402 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07002403static 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 +01002404 sfe_ipv4_debug_dev_read_start,
2405 sfe_ipv4_debug_dev_read_connections_start,
2406 sfe_ipv4_debug_dev_read_connections_connection,
2407 sfe_ipv4_debug_dev_read_connections_end,
2408 sfe_ipv4_debug_dev_read_exceptions_start,
2409 sfe_ipv4_debug_dev_read_exceptions_exception,
2410 sfe_ipv4_debug_dev_read_exceptions_end,
2411 sfe_ipv4_debug_dev_read_stats,
2412 sfe_ipv4_debug_dev_read_end,
2413};
2414
2415/*
2416 * sfe_ipv4_debug_dev_read()
2417 * Send info to userspace upon read request from user
2418 */
2419static ssize_t sfe_ipv4_debug_dev_read(struct file *filp, char *buffer, size_t length, loff_t *offset)
2420{
2421 char msg[CHAR_DEV_MSG_SIZE];
2422 int total_read = 0;
2423 struct sfe_ipv4_debug_xml_write_state *ws;
2424 struct sfe_ipv4 *si = &__si;
2425
2426 ws = (struct sfe_ipv4_debug_xml_write_state *)filp->private_data;
2427 while ((ws->state != SFE_IPV4_DEBUG_XML_STATE_DONE) && (length > CHAR_DEV_MSG_SIZE)) {
2428 if ((sfe_ipv4_debug_xml_write_methods[ws->state])(si, buffer, msg, &length, &total_read, ws)) {
2429 continue;
2430 }
2431 }
2432
2433 return total_read;
2434}
2435
2436/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002437 * sfe_ipv4_debug_dev_open()
2438 */
2439static int sfe_ipv4_debug_dev_open(struct inode *inode, struct file *file)
2440{
2441 struct sfe_ipv4_debug_xml_write_state *ws;
2442
2443 ws = (struct sfe_ipv4_debug_xml_write_state *)file->private_data;
2444 if (!ws) {
2445 ws = kzalloc(sizeof(struct sfe_ipv4_debug_xml_write_state), GFP_KERNEL);
2446 if (!ws) {
2447 return -ENOMEM;
2448 }
2449
2450 ws->state = SFE_IPV4_DEBUG_XML_STATE_START;
2451 file->private_data = ws;
2452 }
2453
2454 return 0;
2455}
2456
2457/*
2458 * sfe_ipv4_debug_dev_release()
2459 */
2460static int sfe_ipv4_debug_dev_release(struct inode *inode, struct file *file)
2461{
2462 struct sfe_ipv4_debug_xml_write_state *ws;
2463
2464 ws = (struct sfe_ipv4_debug_xml_write_state *)file->private_data;
2465 if (ws) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002466 /*
2467 * We've finished with our output so free the write state.
2468 */
2469 kfree(ws);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05302470 file->private_data = NULL;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002471 }
2472
2473 return 0;
2474}
2475
2476/*
2477 * File operations used in the debug char device
2478 */
2479static struct file_operations sfe_ipv4_debug_dev_fops = {
2480 .read = sfe_ipv4_debug_dev_read,
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002481 .open = sfe_ipv4_debug_dev_open,
2482 .release = sfe_ipv4_debug_dev_release
2483};
2484
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08002485#ifdef CONFIG_NF_FLOW_COOKIE
2486/*
2487 * sfe_register_flow_cookie_cb
2488 * register a function in SFE to let SFE use this function to configure flow cookie for a flow
2489 *
2490 * Hardware driver which support flow cookie should register a callback function in SFE. Then SFE
2491 * can use this function to configure flow cookie for a flow.
2492 * return: 0, success; !=0, fail
2493 */
2494int sfe_register_flow_cookie_cb(flow_cookie_set_func_t cb)
2495{
2496 struct sfe_ipv4 *si = &__si;
2497
2498 BUG_ON(!cb);
2499
2500 if (si->flow_cookie_set_func) {
2501 return -1;
2502 }
2503
2504 rcu_assign_pointer(si->flow_cookie_set_func, cb);
2505 return 0;
2506}
2507
2508/*
2509 * sfe_unregister_flow_cookie_cb
2510 * unregister function which is used to configure flow cookie for a flow
2511 *
2512 * return: 0, success; !=0, fail
2513 */
2514int sfe_unregister_flow_cookie_cb(flow_cookie_set_func_t cb)
2515{
2516 struct sfe_ipv4 *si = &__si;
2517
2518 RCU_INIT_POINTER(si->flow_cookie_set_func, NULL);
2519 return 0;
2520}
Xiaoping Fan640faf42015-08-28 15:50:55 -07002521
2522/*
2523 * sfe_ipv4_get_flow_cookie()
2524 */
2525static ssize_t sfe_ipv4_get_flow_cookie(struct device *dev,
2526 struct device_attribute *attr,
2527 char *buf)
2528{
2529 struct sfe_ipv4 *si = &__si;
Xiaoping Fan01c67cc2015-11-09 11:31:57 -08002530 return snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", si->flow_cookie_enable);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002531}
2532
2533/*
2534 * sfe_ipv4_set_flow_cookie()
2535 */
2536static ssize_t sfe_ipv4_set_flow_cookie(struct device *dev,
2537 struct device_attribute *attr,
2538 const char *buf, size_t size)
2539{
2540 struct sfe_ipv4 *si = &__si;
Ken Zhu137722d2021-09-23 17:57:36 -07002541 si->flow_cookie_enable = simple_strtol(buf, NULL, 0);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002542
2543 return size;
2544}
2545
2546/*
2547 * sysfs attributes.
2548 */
2549static const struct device_attribute sfe_ipv4_flow_cookie_attr =
Xiaoping Fane70da412016-02-26 16:47:57 -08002550 __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 -08002551#endif /*CONFIG_NF_FLOW_COOKIE*/
2552
Ken Zhu137722d2021-09-23 17:57:36 -07002553/*
2554 * sfe_ipv4_get_cpu()
2555 */
2556static ssize_t sfe_ipv4_get_cpu(struct device *dev,
2557 struct device_attribute *attr,
2558 char *buf)
2559{
2560 struct sfe_ipv4 *si = &__si;
2561 return snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", si->work_cpu);
2562}
2563
2564/*
2565 * sfe_ipv4_set_cpu()
2566 */
2567static ssize_t sfe_ipv4_set_cpu(struct device *dev,
2568 struct device_attribute *attr,
2569 const char *buf, size_t size)
2570{
2571 struct sfe_ipv4 *si = &__si;
2572 int work_cpu;
2573 work_cpu = simple_strtol(buf, NULL, 0);
2574 if ((work_cpu >= 0) && (work_cpu <= NR_CPUS)) {
2575 si->work_cpu = work_cpu;
2576 } else {
2577 dev_err(dev, "%s is not in valid range[0,%d]", buf, NR_CPUS);
2578 }
2579 return size;
2580}
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002581
Ken Zhu137722d2021-09-23 17:57:36 -07002582/*
2583 * sysfs attributes.
2584 */
2585static const struct device_attribute sfe_ipv4_cpu_attr =
2586 __ATTR(stats_work_cpu, S_IWUSR | S_IRUGO, sfe_ipv4_get_cpu, sfe_ipv4_set_cpu);
2587
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002588/*
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05302589 * sfe_ipv4_conn_match_hash_init()
2590 * Initialize conn match hash lists
2591 */
2592static void sfe_ipv4_conn_match_hash_init(struct sfe_ipv4 *si, int len)
2593{
2594 struct hlist_head *hash_list = si->hlist_conn_match_hash_head;
2595 int i;
2596
2597 for (i = 0; i < len; i++) {
2598 INIT_HLIST_HEAD(&hash_list[i]);
2599 }
2600}
2601
Amitesh Anand63be37d2021-12-24 20:51:48 +05302602#ifdef SFE_PROCESS_LOCAL_OUT
2603/*
2604 * sfe_ipv4_local_out()
2605 * Called for packets from ip_local_out() - post encapsulation & other packets
2606 */
2607static unsigned int sfe_ipv4_local_out(void *priv, struct sk_buff *skb, const struct nf_hook_state *nhs)
2608{
Nitin Shettyc28f8172022-02-04 16:23:46 +05302609 struct sfe_l2_info l2_info = {0};
Guduri Prathyusha5f27e232022-01-06 14:39:04 +05302610
Amitesh Anand63be37d2021-12-24 20:51:48 +05302611 DEBUG_TRACE("%px: sfe: sfe_ipv4_local_out hook called.\n", skb);
2612
2613 if (likely(skb->skb_iif)) {
Guduri Prathyusha5f27e232022-01-06 14:39:04 +05302614 return sfe_ipv4_recv(skb->dev, skb, &l2_info, true) ? NF_STOLEN : NF_ACCEPT;
Amitesh Anand63be37d2021-12-24 20:51:48 +05302615 }
2616
2617 return NF_ACCEPT;
2618}
2619
2620/*
2621 * struct nf_hook_ops sfe_ipv4_ops_local_out[]
2622 * Hooks into netfilter local out packet monitoring points.
2623 */
2624static struct nf_hook_ops sfe_ipv4_ops_local_out[] __read_mostly = {
2625
2626 /*
2627 * Local out routing hook is used to monitor packets.
2628 */
2629 {
2630 .hook = sfe_ipv4_local_out,
2631 .pf = PF_INET,
2632 .hooknum = NF_INET_LOCAL_OUT,
2633 .priority = NF_IP_PRI_FIRST,
2634 },
2635};
2636#endif
2637
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002638/*
Dave Hudson87973cd2013-10-22 16:00:04 +01002639 * sfe_ipv4_init()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002640 */
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05302641int sfe_ipv4_init(void)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002642{
2643 struct sfe_ipv4 *si = &__si;
2644 int result = -1;
2645
Dave Hudsondcd08fb2013-11-22 09:25:16 -06002646 DEBUG_INFO("SFE IPv4 init\n");
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002647
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05302648 sfe_ipv4_conn_match_hash_init(si, ARRAY_SIZE(si->hlist_conn_match_hash_head));
2649
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302650 si->stats_pcpu = alloc_percpu_gfp(struct sfe_ipv4_stats, GFP_KERNEL | __GFP_ZERO);
2651 if (!si->stats_pcpu) {
2652 DEBUG_ERROR("failed to allocate stats memory for sfe_ipv4\n");
2653 goto exit0;
2654 }
2655
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002656 /*
Parikshit Guned31a8202022-01-05 22:15:04 +05302657 * Allocate per cpu per service class memory.
2658 */
2659 si->stats_pcpu_psc = alloc_percpu_gfp(struct sfe_ipv4_service_class_stats_db,
2660 GFP_KERNEL | __GFP_ZERO);
2661 if (!si->stats_pcpu_psc) {
2662 DEBUG_ERROR("failed to allocate per cpu per service clas stats memory\n");
2663 goto exit1;
2664 }
2665
2666 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002667 * Create sys/sfe_ipv4
2668 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302669 si->sys_ipv4 = kobject_create_and_add("sfe_ipv4", NULL);
2670 if (!si->sys_ipv4) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002671 DEBUG_ERROR("failed to register sfe_ipv4\n");
Parikshit Guned31a8202022-01-05 22:15:04 +05302672 goto exit2;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002673 }
2674
2675 /*
2676 * Create files, one for each parameter supported by this module.
2677 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302678 result = sysfs_create_file(si->sys_ipv4, &sfe_ipv4_debug_dev_attr.attr);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002679 if (result) {
2680 DEBUG_ERROR("failed to register debug dev file: %d\n", result);
Parikshit Guned31a8202022-01-05 22:15:04 +05302681 goto exit3;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002682 }
2683
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302684 result = sysfs_create_file(si->sys_ipv4, &sfe_ipv4_cpu_attr.attr);
Ken Zhu137722d2021-09-23 17:57:36 -07002685 if (result) {
2686 DEBUG_ERROR("failed to register debug dev file: %d\n", result);
Parikshit Guned31a8202022-01-05 22:15:04 +05302687 goto exit4;
Ken Zhu137722d2021-09-23 17:57:36 -07002688 }
2689
Xiaoping Fan640faf42015-08-28 15:50:55 -07002690#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302691 result = sysfs_create_file(si->sys_ipv4, &sfe_ipv4_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002692 if (result) {
2693 DEBUG_ERROR("failed to register flow cookie enable file: %d\n", result);
Parikshit Guned31a8202022-01-05 22:15:04 +05302694 goto exit5;
Xiaoping Fan640faf42015-08-28 15:50:55 -07002695 }
2696#endif /* CONFIG_NF_FLOW_COOKIE */
2697
Amitesh Anand63be37d2021-12-24 20:51:48 +05302698#ifdef SFE_PROCESS_LOCAL_OUT
2699#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
2700 result = nf_register_hooks(sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2701#else
2702 result = nf_register_net_hooks(&init_net, sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2703#endif
2704 if (result < 0) {
2705 DEBUG_ERROR("can't register nf local out hook: %d\n", result);
Parikshit Guned31a8202022-01-05 22:15:04 +05302706 goto exit6;
Amitesh Anand63be37d2021-12-24 20:51:48 +05302707 }
2708 DEBUG_INFO("Register nf local out hook success: %d\n", result);
2709#endif
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002710 /*
2711 * Register our debug char device.
2712 */
2713 result = register_chrdev(0, "sfe_ipv4", &sfe_ipv4_debug_dev_fops);
2714 if (result < 0) {
2715 DEBUG_ERROR("Failed to register chrdev: %d\n", result);
Parikshit Guned31a8202022-01-05 22:15:04 +05302716 goto exit7;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002717 }
2718
2719 si->debug_dev = result;
Ken Zhu137722d2021-09-23 17:57:36 -07002720 si->work_cpu = WORK_CPU_UNBOUND;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002721
2722 /*
Ken Zhu137722d2021-09-23 17:57:36 -07002723 * Create a work to handle periodic statistics.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002724 */
Ken Zhu137722d2021-09-23 17:57:36 -07002725 INIT_DELAYED_WORK(&(si->sync_dwork), sfe_ipv4_periodic_sync);
2726 schedule_delayed_work_on(si->work_cpu, &(si->sync_dwork), ((HZ + 99) / 100));
2727
Dave Hudson87973cd2013-10-22 16:00:04 +01002728 spin_lock_init(&si->lock);
Dave Hudson87973cd2013-10-22 16:00:04 +01002729 return 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002730
Parikshit Guned31a8202022-01-05 22:15:04 +05302731exit7:
Amitesh Anand63be37d2021-12-24 20:51:48 +05302732#ifdef SFE_PROCESS_LOCAL_OUT
2733 DEBUG_TRACE("sfe: Unregister local out hook\n");
2734#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
2735 nf_unregister_hooks(sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2736#else
2737 nf_unregister_net_hooks(&init_net, sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2738#endif
Parikshit Guned31a8202022-01-05 22:15:04 +05302739exit6:
Amitesh Anand63be37d2021-12-24 20:51:48 +05302740#endif
Xiaoping Fan640faf42015-08-28 15:50:55 -07002741#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302742 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002743
Parikshit Guned31a8202022-01-05 22:15:04 +05302744exit5:
Xiaoping Fan640faf42015-08-28 15:50:55 -07002745#endif /* CONFIG_NF_FLOW_COOKIE */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302746 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_cpu_attr.attr);
Parikshit Guned31a8202022-01-05 22:15:04 +05302747exit4:
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302748 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_debug_dev_attr.attr);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002749
Parikshit Guned31a8202022-01-05 22:15:04 +05302750exit3:
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302751 kobject_put(si->sys_ipv4);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002752
Parikshit Guned31a8202022-01-05 22:15:04 +05302753exit2:
2754 free_percpu(si->stats_pcpu_psc);
2755
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002756exit1:
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302757 free_percpu(si->stats_pcpu);
2758
2759exit0:
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002760 return result;
2761}
2762
2763/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002764 * sfe_ipv4_exit()
2765 */
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05302766void sfe_ipv4_exit(void)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002767{
Dave Hudson87973cd2013-10-22 16:00:04 +01002768 struct sfe_ipv4 *si = &__si;
2769
Dave Hudsondcd08fb2013-11-22 09:25:16 -06002770 DEBUG_INFO("SFE IPv4 exit\n");
Dave Hudson87973cd2013-10-22 16:00:04 +01002771 /*
2772 * Destroy all connections.
2773 */
Dave Hudsondcd08fb2013-11-22 09:25:16 -06002774 sfe_ipv4_destroy_all_rules_for_dev(NULL);
Dave Hudson87973cd2013-10-22 16:00:04 +01002775
Ken Zhu137722d2021-09-23 17:57:36 -07002776 cancel_delayed_work_sync(&si->sync_dwork);
Dave Hudson87973cd2013-10-22 16:00:04 +01002777
Dave Hudson87973cd2013-10-22 16:00:04 +01002778 unregister_chrdev(si->debug_dev, "sfe_ipv4");
2779
Amitesh Anand63be37d2021-12-24 20:51:48 +05302780#ifdef SFE_PROCESS_LOCAL_OUT
2781 DEBUG_TRACE("sfe: Unregister local out hook\n");
2782#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
2783 nf_unregister_hooks(sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2784#else
2785 nf_unregister_net_hooks(&init_net, sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2786#endif
2787#endif
2788
Xiaoping Fan640faf42015-08-28 15:50:55 -07002789#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302790 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002791#endif /* CONFIG_NF_FLOW_COOKIE */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302792 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_debug_dev_attr.attr);
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002793
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302794 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_cpu_attr.attr);
Dave Hudson87973cd2013-10-22 16:00:04 +01002795
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302796 kobject_put(si->sys_ipv4);
Dave Hudson87973cd2013-10-22 16:00:04 +01002797
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302798 free_percpu(si->stats_pcpu);
Parikshit Guned31a8202022-01-05 22:15:04 +05302799 free_percpu(si->stats_pcpu_psc);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002800}
2801
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08002802#ifdef CONFIG_NF_FLOW_COOKIE
2803EXPORT_SYMBOL(sfe_register_flow_cookie_cb);
2804EXPORT_SYMBOL(sfe_unregister_flow_cookie_cb);
2805#endif