blob: 0e124413b3c88f150132acd68382ad7388edb059 [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"
Tian Yangd98d91b2022-03-09 14:50:12 -080049#include "sfe_ipv4_tun6rd.h"
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010050
51static char *sfe_ipv4_exception_events_string[SFE_IPV4_EXCEPTION_EVENT_LAST] = {
52 "UDP_HEADER_INCOMPLETE",
53 "UDP_NO_CONNECTION",
54 "UDP_IP_OPTIONS_OR_INITIAL_FRAGMENT",
55 "UDP_SMALL_TTL",
56 "UDP_NEEDS_FRAGMENTATION",
57 "TCP_HEADER_INCOMPLETE",
58 "TCP_NO_CONNECTION_SLOW_FLAGS",
59 "TCP_NO_CONNECTION_FAST_FLAGS",
60 "TCP_IP_OPTIONS_OR_INITIAL_FRAGMENT",
61 "TCP_SMALL_TTL",
62 "TCP_NEEDS_FRAGMENTATION",
63 "TCP_FLAGS",
64 "TCP_SEQ_EXCEEDS_RIGHT_EDGE",
65 "TCP_SMALL_DATA_OFFS",
66 "TCP_BAD_SACK",
67 "TCP_BIG_DATA_OFFS",
68 "TCP_SEQ_BEFORE_LEFT_EDGE",
69 "TCP_ACK_EXCEEDS_RIGHT_EDGE",
70 "TCP_ACK_BEFORE_LEFT_EDGE",
71 "ICMP_HEADER_INCOMPLETE",
72 "ICMP_UNHANDLED_TYPE",
73 "ICMP_IPV4_HEADER_INCOMPLETE",
74 "ICMP_IPV4_NON_V4",
75 "ICMP_IPV4_IP_OPTIONS_INCOMPLETE",
76 "ICMP_IPV4_UDP_HEADER_INCOMPLETE",
77 "ICMP_IPV4_TCP_HEADER_INCOMPLETE",
78 "ICMP_IPV4_UNHANDLED_PROTOCOL",
79 "ICMP_NO_CONNECTION",
80 "ICMP_FLUSHED_CONNECTION",
81 "HEADER_INCOMPLETE",
Ratheesh Kannoth43d64f82021-10-20 08:23:29 +053082 "HEADER_CSUM_BAD",
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010083 "BAD_TOTAL_LENGTH",
84 "NON_V4",
85 "NON_INITIAL_FRAGMENT",
86 "DATAGRAM_INCOMPLETE",
87 "IP_OPTIONS_INCOMPLETE",
Ratheesh Kannoth5dee3772022-01-18 11:27:14 +053088 "UNHANDLED_PROTOCOL",
Nitin Shetty16ab38d2022-02-09 01:26:19 +053089 "NO_HEADROOM",
90 "INVALID_PPPOE_SESSION",
91 "INCORRECT_PPPOE_PARSING",
92 "PPPOE_NOT_SET_IN_CME",
93 "INGRESS_VLAN_TAG_MISMATCH",
Ratheesh Kannoth5dee3772022-01-18 11:27:14 +053094 "INVALID_SOURCE_INTERFACE",
Tian Yangd98d91b2022-03-09 14:50:12 -080095 "TUN6RD_NO_CONNECTION",
96 "TUN6RD_NEEDS_FRAGMENTATION",
97 "TUN6RD_SYNC_ON_FIND",
Nitin Shettye6ed5b52021-12-27 14:50:11 +053098 "GRE_HEADER_INCOMPLETE",
99 "GRE_NO_CONNECTION",
100 "GRE_IP_OPTIONS_OR_INITIAL_FRAGMENT",
101 "GRE_SMALL_TTL",
102 "GRE_NEEDS_FRAGMENTATION"
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100103};
104
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700105static struct sfe_ipv4 __si;
Ken Zhu7a43d882022-01-04 10:51:44 -0800106struct sfe_ipv4_msg *sfe_ipv4_sync_many_msg;
107uint32_t sfe_ipv4_sync_max_number;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100108
109/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100110 * sfe_ipv4_gen_ip_csum()
111 * Generate the IP checksum for an IPv4 header.
112 *
113 * Note that this function assumes that we have only 20 bytes of IP header.
114 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530115u16 sfe_ipv4_gen_ip_csum(struct iphdr *iph)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100116{
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700117 u32 sum;
118 u16 *i = (u16 *)iph;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100119
120 iph->check = 0;
121
122 /*
123 * Generate the sum.
124 */
125 sum = i[0] + i[1] + i[2] + i[3] + i[4] + i[5] + i[6] + i[7] + i[8] + i[9];
126
127 /*
128 * Fold it to ones-complement form.
129 */
130 sum = (sum & 0xffff) + (sum >> 16);
131 sum = (sum & 0xffff) + (sum >> 16);
132
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700133 return (u16)sum ^ 0xffff;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100134}
135
136/*
137 * sfe_ipv4_get_connection_match_hash()
138 * Generate the hash used in connection match lookups.
139 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700140static inline unsigned int sfe_ipv4_get_connection_match_hash(struct net_device *dev, u8 protocol,
Dave Hudson87973cd2013-10-22 16:00:04 +0100141 __be32 src_ip, __be16 src_port,
142 __be32 dest_ip, __be16 dest_port)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100143{
Ratheesh Kannoth5dee3772022-01-18 11:27:14 +0530144 u32 hash = ntohl(src_ip ^ dest_ip) ^ protocol ^ ntohs(src_port ^ dest_port);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100145 return ((hash >> SFE_IPV4_CONNECTION_HASH_SHIFT) ^ hash) & SFE_IPV4_CONNECTION_HASH_MASK;
146}
147
148/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530149 * sfe_ipv4_find_connection_match_rcu()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100150 * Get the IPv4 flow match info that corresponds to a particular 5-tuple.
151 *
152 * On entry we must be holding the lock that protects the hash table.
153 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530154struct sfe_ipv4_connection_match *
155sfe_ipv4_find_connection_match_rcu(struct sfe_ipv4 *si, struct net_device *dev, u8 protocol,
Dave Hudson87973cd2013-10-22 16:00:04 +0100156 __be32 src_ip, __be16 src_port,
157 __be32 dest_ip, __be16 dest_port)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100158{
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530159 struct sfe_ipv4_connection_match *cm = NULL;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100160 unsigned int conn_match_idx;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530161 struct hlist_head *lhead;
162
163 WARN_ON_ONCE(!rcu_read_lock_held());
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100164
165 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 +0100166
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530167 lhead = &si->hlist_conn_match_hash_head[conn_match_idx];
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100168
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530169 hlist_for_each_entry_rcu(cm, lhead, hnode) {
170 if (cm->match_src_port != src_port
171 || cm->match_dest_port != dest_port
172 || cm->match_src_ip != src_ip
173 || cm->match_dest_ip != dest_ip
Ratheesh Kannoth5dee3772022-01-18 11:27:14 +0530174 || cm->match_protocol != protocol) {
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530175 continue;
176 }
177
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530178 this_cpu_inc(si->stats_pcpu->connection_match_hash_hits64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100179
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530180 break;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100181 }
182
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100183 return cm;
184}
185
186/*
187 * sfe_ipv4_connection_match_update_summary_stats()
188 * Update the summary stats for a connection match entry.
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530189 *
190 * Stats are incremented atomically. So use atomic substraction to update summary
191 * stats.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100192 */
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530193static inline void sfe_ipv4_connection_match_update_summary_stats(struct sfe_ipv4_connection_match *cm,
194 u32 *packets, u32 *bytes)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100195{
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530196 u32 packet_count, byte_count;
197
198 packet_count = atomic_read(&cm->rx_packet_count);
199 cm->rx_packet_count64 += packet_count;
200 atomic_sub(packet_count, &cm->rx_packet_count);
201
202 byte_count = atomic_read(&cm->rx_byte_count);
203 cm->rx_byte_count64 += byte_count;
204 atomic_sub(byte_count, &cm->rx_byte_count);
205
206 *packets = packet_count;
207 *bytes = byte_count;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100208}
209
210/*
211 * sfe_ipv4_connection_match_compute_translations()
212 * Compute port and address translations for a connection match entry.
213 */
214static void sfe_ipv4_connection_match_compute_translations(struct sfe_ipv4_connection_match *cm)
215{
216 /*
217 * Before we insert the entry look to see if this is tagged as doing address
218 * translations. If it is then work out the adjustment that we need to apply
219 * to the transport checksum.
220 */
221 if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC) {
222 /*
223 * Precompute an incremental checksum adjustment so we can
224 * edit packets in this stream very quickly. The algorithm is from RFC1624.
225 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700226 u16 src_ip_hi = cm->match_src_ip >> 16;
227 u16 src_ip_lo = cm->match_src_ip & 0xffff;
228 u32 xlate_src_ip = ~cm->xlate_src_ip;
229 u16 xlate_src_ip_hi = xlate_src_ip >> 16;
230 u16 xlate_src_ip_lo = xlate_src_ip & 0xffff;
231 u16 xlate_src_port = ~cm->xlate_src_port;
232 u32 adj;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100233
234 /*
235 * When we compute this fold it down to a 16-bit offset
236 * as that way we can avoid having to do a double
237 * folding of the twos-complement result because the
238 * addition of 2 16-bit values cannot cause a double
239 * wrap-around!
240 */
241 adj = src_ip_hi + src_ip_lo + cm->match_src_port
242 + xlate_src_ip_hi + xlate_src_ip_lo + xlate_src_port;
243 adj = (adj & 0xffff) + (adj >> 16);
244 adj = (adj & 0xffff) + (adj >> 16);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700245 cm->xlate_src_csum_adjustment = (u16)adj;
Nicolas Costaac2979c2014-01-14 10:35:24 -0600246
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100247 }
248
249 if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST) {
250 /*
251 * Precompute an incremental checksum adjustment so we can
252 * edit packets in this stream very quickly. The algorithm is from RFC1624.
253 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700254 u16 dest_ip_hi = cm->match_dest_ip >> 16;
255 u16 dest_ip_lo = cm->match_dest_ip & 0xffff;
256 u32 xlate_dest_ip = ~cm->xlate_dest_ip;
257 u16 xlate_dest_ip_hi = xlate_dest_ip >> 16;
258 u16 xlate_dest_ip_lo = xlate_dest_ip & 0xffff;
259 u16 xlate_dest_port = ~cm->xlate_dest_port;
260 u32 adj;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100261
262 /*
263 * When we compute this fold it down to a 16-bit offset
264 * as that way we can avoid having to do a double
265 * folding of the twos-complement result because the
266 * addition of 2 16-bit values cannot cause a double
267 * wrap-around!
268 */
269 adj = dest_ip_hi + dest_ip_lo + cm->match_dest_port
270 + xlate_dest_ip_hi + xlate_dest_ip_lo + xlate_dest_port;
271 adj = (adj & 0xffff) + (adj >> 16);
272 adj = (adj & 0xffff) + (adj >> 16);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700273 cm->xlate_dest_csum_adjustment = (u16)adj;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100274 }
Xiaoping Fanad755af2015-04-01 16:58:46 -0700275
276 if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC) {
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700277 u32 adj = ~cm->match_src_ip + cm->xlate_src_ip;
Xiaoping Fanad755af2015-04-01 16:58:46 -0700278 if (adj < cm->xlate_src_ip) {
279 adj++;
280 }
281
282 adj = (adj & 0xffff) + (adj >> 16);
283 adj = (adj & 0xffff) + (adj >> 16);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700284 cm->xlate_src_partial_csum_adjustment = (u16)adj;
Xiaoping Fanad755af2015-04-01 16:58:46 -0700285 }
286
287 if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST) {
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700288 u32 adj = ~cm->match_dest_ip + cm->xlate_dest_ip;
Xiaoping Fanad755af2015-04-01 16:58:46 -0700289 if (adj < cm->xlate_dest_ip) {
290 adj++;
291 }
292
293 adj = (adj & 0xffff) + (adj >> 16);
294 adj = (adj & 0xffff) + (adj >> 16);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700295 cm->xlate_dest_partial_csum_adjustment = (u16)adj;
Xiaoping Fanad755af2015-04-01 16:58:46 -0700296 }
297
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100298}
299
300/*
301 * sfe_ipv4_update_summary_stats()
302 * Update the summary stats.
303 */
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530304static void sfe_ipv4_update_summary_stats(struct sfe_ipv4 *si, struct sfe_ipv4_stats *stats)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100305{
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530306 int i = 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100307
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530308 memset(stats, 0, sizeof(*stats));
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100309
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530310 for_each_possible_cpu(i) {
311 const struct sfe_ipv4_stats *s = per_cpu_ptr(si->stats_pcpu, i);
312
313 stats->connection_create_requests64 += s->connection_create_requests64;
314 stats->connection_create_collisions64 += s->connection_create_collisions64;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530315 stats->connection_create_failures64 += s->connection_create_failures64;
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530316 stats->connection_destroy_requests64 += s->connection_destroy_requests64;
317 stats->connection_destroy_misses64 += s->connection_destroy_misses64;
318 stats->connection_match_hash_hits64 += s->connection_match_hash_hits64;
319 stats->connection_match_hash_reorders64 += s->connection_match_hash_reorders64;
320 stats->connection_flushes64 += s->connection_flushes64;
Amitesh Anand63be37d2021-12-24 20:51:48 +0530321 stats->packets_dropped64 += s->packets_dropped64;
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530322 stats->packets_forwarded64 += s->packets_forwarded64;
Ken Zhu7e38d1a2021-11-30 17:31:46 -0800323 stats->packets_fast_xmited64 += s->packets_fast_xmited64;
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530324 stats->packets_not_forwarded64 += s->packets_not_forwarded64;
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +0530325 stats->pppoe_encap_packets_forwarded64 += s->pppoe_encap_packets_forwarded64;
326 stats->pppoe_decap_packets_forwarded64 += s->pppoe_decap_packets_forwarded64;
Guduri Prathyusha034d6352022-01-12 16:49:04 +0530327 stats->pppoe_bridge_packets_forwarded64 += s->pppoe_bridge_packets_forwarded64;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100328 }
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530329
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100330}
331
332/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530333 * sfe_ipv4_insert_connection_match()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100334 * Insert a connection match into the hash.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100335 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530336static inline void sfe_ipv4_insert_connection_match(struct sfe_ipv4 *si,
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700337 struct sfe_ipv4_connection_match *cm)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100338{
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100339 unsigned int conn_match_idx
340 = sfe_ipv4_get_connection_match_hash(cm->match_dev, cm->match_protocol,
341 cm->match_src_ip, cm->match_src_port,
342 cm->match_dest_ip, cm->match_dest_port);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700343
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530344 lockdep_assert_held(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100345
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530346 hlist_add_head_rcu(&cm->hnode, &si->hlist_conn_match_hash_head[conn_match_idx]);
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800347#ifdef CONFIG_NF_FLOW_COOKIE
Xiaoping Fan640faf42015-08-28 15:50:55 -0700348 if (!si->flow_cookie_enable)
349 return;
350
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800351 /*
352 * Configure hardware to put a flow cookie in packet of this flow,
353 * then we can accelerate the lookup process when we received this packet.
354 */
355 for (conn_match_idx = 1; conn_match_idx < SFE_FLOW_COOKIE_SIZE; conn_match_idx++) {
356 struct sfe_flow_cookie_entry *entry = &si->sfe_flow_cookie_table[conn_match_idx];
357
358 if ((NULL == entry->match) && time_is_before_jiffies(entry->last_clean_time + HZ)) {
359 flow_cookie_set_func_t func;
360
361 rcu_read_lock();
362 func = rcu_dereference(si->flow_cookie_set_func);
363 if (func) {
Xiaoping Fan59176422015-05-22 15:58:10 -0700364 if (!func(cm->match_protocol, cm->match_src_ip, cm->match_src_port,
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800365 cm->match_dest_ip, cm->match_dest_port, conn_match_idx)) {
366 entry->match = cm;
367 cm->flow_cookie = conn_match_idx;
368 }
369 }
370 rcu_read_unlock();
371
372 break;
373 }
374 }
375#endif
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100376}
377
378/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530379 * sfe_ipv4_remove_connection_match()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100380 * Remove a connection match object from the hash.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100381 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530382static inline void sfe_ipv4_remove_connection_match(struct sfe_ipv4 *si, struct sfe_ipv4_connection_match *cm)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100383{
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530384
385 lockdep_assert_held(&si->lock);
386
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800387#ifdef CONFIG_NF_FLOW_COOKIE
Xiaoping Fan640faf42015-08-28 15:50:55 -0700388 if (si->flow_cookie_enable) {
389 /*
390 * Tell hardware that we no longer need a flow cookie in packet of this flow
391 */
392 unsigned int conn_match_idx;
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800393
Xiaoping Fan640faf42015-08-28 15:50:55 -0700394 for (conn_match_idx = 1; conn_match_idx < SFE_FLOW_COOKIE_SIZE; conn_match_idx++) {
395 struct sfe_flow_cookie_entry *entry = &si->sfe_flow_cookie_table[conn_match_idx];
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800396
Xiaoping Fan640faf42015-08-28 15:50:55 -0700397 if (cm == entry->match) {
398 flow_cookie_set_func_t func;
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800399
Xiaoping Fan640faf42015-08-28 15:50:55 -0700400 rcu_read_lock();
401 func = rcu_dereference(si->flow_cookie_set_func);
402 if (func) {
403 func(cm->match_protocol, cm->match_src_ip, cm->match_src_port,
404 cm->match_dest_ip, cm->match_dest_port, 0);
405 }
406 rcu_read_unlock();
407
408 cm->flow_cookie = 0;
409 entry->match = NULL;
410 entry->last_clean_time = jiffies;
411 break;
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800412 }
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800413 }
414 }
415#endif
416
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530417 hlist_del_init_rcu(&cm->hnode);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100418
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100419}
420
421/*
422 * sfe_ipv4_get_connection_hash()
423 * Generate the hash used in connection lookups.
424 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700425static inline unsigned int sfe_ipv4_get_connection_hash(u8 protocol, __be32 src_ip, __be16 src_port,
Dave Hudson87973cd2013-10-22 16:00:04 +0100426 __be32 dest_ip, __be16 dest_port)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100427{
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700428 u32 hash = ntohl(src_ip ^ dest_ip) ^ protocol ^ ntohs(src_port ^ dest_port);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100429 return ((hash >> SFE_IPV4_CONNECTION_HASH_SHIFT) ^ hash) & SFE_IPV4_CONNECTION_HASH_MASK;
430}
431
432/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530433 * sfe_ipv4_find_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100434 * Get the IPv4 connection info that corresponds to a particular 5-tuple.
435 *
436 * On entry we must be holding the lock that protects the hash table.
437 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530438static inline struct sfe_ipv4_connection *sfe_ipv4_find_connection(struct sfe_ipv4 *si, u32 protocol,
Dave Hudson87973cd2013-10-22 16:00:04 +0100439 __be32 src_ip, __be16 src_port,
440 __be32 dest_ip, __be16 dest_port)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100441{
442 struct sfe_ipv4_connection *c;
443 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 +0530444
445 lockdep_assert_held(&si->lock);
446
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100447 c = si->conn_hash[conn_idx];
448
449 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100450 * Will need connection entry for next create/destroy metadata,
451 * So no need to re-order entry for these requests
452 */
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530453 while (c) {
454 if ((c->src_port == src_port)
455 && (c->dest_port == dest_port)
456 && (c->src_ip == src_ip)
457 && (c->dest_ip == dest_ip)
458 && (c->protocol == protocol)) {
459 return c;
460 }
461
462 c = c->next;
463 }
464
465 return NULL;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100466}
467
468/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530469 * sfe_ipv4_insert_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100470 * Insert a connection into the hash.
471 *
472 * On entry we must be holding the lock that protects the hash table.
473 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530474static void sfe_ipv4_insert_connection(struct sfe_ipv4 *si, struct sfe_ipv4_connection *c)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100475{
476 struct sfe_ipv4_connection **hash_head;
477 struct sfe_ipv4_connection *prev_head;
478 unsigned int conn_idx;
479
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530480 lockdep_assert_held(&si->lock);
481
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100482 /*
483 * Insert entry into the connection hash.
484 */
485 conn_idx = sfe_ipv4_get_connection_hash(c->protocol, c->src_ip, c->src_port,
486 c->dest_ip, c->dest_port);
487 hash_head = &si->conn_hash[conn_idx];
488 prev_head = *hash_head;
489 c->prev = NULL;
490 if (prev_head) {
491 prev_head->prev = c;
492 }
493
494 c->next = prev_head;
495 *hash_head = c;
496
497 /*
498 * Insert entry into the "all connections" list.
499 */
500 if (si->all_connections_tail) {
501 c->all_connections_prev = si->all_connections_tail;
502 si->all_connections_tail->all_connections_next = c;
503 } else {
504 c->all_connections_prev = NULL;
505 si->all_connections_head = c;
506 }
507
508 si->all_connections_tail = c;
509 c->all_connections_next = NULL;
510 si->num_connections++;
511
512 /*
513 * Insert the connection match objects too.
514 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530515 sfe_ipv4_insert_connection_match(si, c->original_match);
516 sfe_ipv4_insert_connection_match(si, c->reply_match);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100517}
518
519/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530520 * sfe_ipv4_remove_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100521 * Remove a sfe_ipv4_connection object from the hash.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100522 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530523bool sfe_ipv4_remove_connection(struct sfe_ipv4 *si, struct sfe_ipv4_connection *c)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100524{
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530525 lockdep_assert_held(&si->lock);
526
527 if (c->removed) {
528 DEBUG_ERROR("%px: Connection has been removed already\n", c);
529 return false;
530 }
531
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100532 /*
Tian Yang46d6eb02022-03-31 10:26:16 -0700533 * dereference the decap direction top_interface_dev
534 */
535 if (c->reply_match->top_interface_dev) {
536 dev_put(c->reply_match->top_interface_dev);
537 }
538
539 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100540 * Remove the connection match objects.
541 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530542 sfe_ipv4_remove_connection_match(si, c->reply_match);
543 sfe_ipv4_remove_connection_match(si, c->original_match);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100544
545 /*
546 * Unlink the connection.
547 */
548 if (c->prev) {
549 c->prev->next = c->next;
550 } else {
551 unsigned int conn_idx = sfe_ipv4_get_connection_hash(c->protocol, c->src_ip, c->src_port,
552 c->dest_ip, c->dest_port);
553 si->conn_hash[conn_idx] = c->next;
554 }
555
556 if (c->next) {
557 c->next->prev = c->prev;
558 }
Xiaoping Fan34586472015-07-03 02:20:35 -0700559
560 /*
561 * Unlink connection from all_connections list
562 */
563 if (c->all_connections_prev) {
564 c->all_connections_prev->all_connections_next = c->all_connections_next;
565 } else {
566 si->all_connections_head = c->all_connections_next;
567 }
568
569 if (c->all_connections_next) {
570 c->all_connections_next->all_connections_prev = c->all_connections_prev;
571 } else {
572 si->all_connections_tail = c->all_connections_prev;
573 }
574
Ken Zhudc423672021-09-02 18:27:01 -0700575 /*
576 * If I am the next sync connection, move the sync to my next or head.
577 */
578 if (unlikely(si->wc_next == c)) {
579 si->wc_next = c->all_connections_next;
580 }
581
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530582 c->removed = true;
Xiaoping Fan34586472015-07-03 02:20:35 -0700583 si->num_connections--;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530584 return true;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100585}
586
587/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530588 * sfe_ipv4_gen_sync_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100589 * Sync a connection.
590 *
591 * On entry to this function we expect that the lock for the connection is either
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530592 * already held (while called from sfe_ipv4_periodic_sync() or isn't required
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530593 * (while called from sfe_ipv4_flush_connection())
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100594 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530595static void sfe_ipv4_gen_sync_connection(struct sfe_ipv4 *si, struct sfe_ipv4_connection *c,
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700596 struct sfe_connection_sync *sis, sfe_sync_reason_t reason,
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700597 u64 now_jiffies)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100598{
599 struct sfe_ipv4_connection_match *original_cm;
600 struct sfe_ipv4_connection_match *reply_cm;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530601 u32 packet_count, byte_count;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100602
603 /*
604 * Fill in the update message.
605 */
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700606 sis->is_v6 = 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100607 sis->protocol = c->protocol;
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700608 sis->src_ip.ip = c->src_ip;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700609 sis->src_ip_xlate.ip = c->src_ip_xlate;
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700610 sis->dest_ip.ip = c->dest_ip;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700611 sis->dest_ip_xlate.ip = c->dest_ip_xlate;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100612 sis->src_port = c->src_port;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700613 sis->src_port_xlate = c->src_port_xlate;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100614 sis->dest_port = c->dest_port;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700615 sis->dest_port_xlate = c->dest_port_xlate;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100616
617 original_cm = c->original_match;
618 reply_cm = c->reply_match;
619 sis->src_td_max_window = original_cm->protocol_state.tcp.max_win;
620 sis->src_td_end = original_cm->protocol_state.tcp.end;
621 sis->src_td_max_end = original_cm->protocol_state.tcp.max_end;
622 sis->dest_td_max_window = reply_cm->protocol_state.tcp.max_win;
623 sis->dest_td_end = reply_cm->protocol_state.tcp.end;
624 sis->dest_td_max_end = reply_cm->protocol_state.tcp.max_end;
625
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530626 sfe_ipv4_connection_match_update_summary_stats(original_cm, &packet_count, &byte_count);
627 sis->src_new_packet_count = packet_count;
628 sis->src_new_byte_count = byte_count;
Matthew McClintockd0cdb802014-02-24 16:30:35 -0600629
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530630 sfe_ipv4_connection_match_update_summary_stats(reply_cm, &packet_count, &byte_count);
631 sis->dest_new_packet_count = packet_count;
632 sis->dest_new_byte_count = byte_count;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100633
Matthew McClintockd0cdb802014-02-24 16:30:35 -0600634 sis->src_dev = original_cm->match_dev;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100635 sis->src_packet_count = original_cm->rx_packet_count64;
636 sis->src_byte_count = original_cm->rx_byte_count64;
Matthew McClintockd0cdb802014-02-24 16:30:35 -0600637
638 sis->dest_dev = reply_cm->match_dev;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100639 sis->dest_packet_count = reply_cm->rx_packet_count64;
640 sis->dest_byte_count = reply_cm->rx_byte_count64;
641
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700642 sis->reason = reason;
643
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100644 /*
645 * Get the time increment since our last sync.
646 */
647 sis->delta_jiffies = now_jiffies - c->last_sync_jiffies;
648 c->last_sync_jiffies = now_jiffies;
649}
650
651/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530652 * sfe_ipv4_free_connection_rcu()
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530653 * Called at RCU qs state to free the connection object.
654 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530655static void sfe_ipv4_free_connection_rcu(struct rcu_head *head)
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530656{
657 struct sfe_ipv4_connection *c;
Amitesh Anand63be37d2021-12-24 20:51:48 +0530658 struct udp_sock *up;
659 struct sock *sk;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530660
661 /*
662 * We dont need spin lock as the connection is already removed from link list
663 */
664 c = container_of(head, struct sfe_ipv4_connection, rcu);
665
666 BUG_ON(!c->removed);
667
668 DEBUG_TRACE("%px: connecton has been deleted\n", c);
669
670 /*
Amitesh Anand63be37d2021-12-24 20:51:48 +0530671 * Decrease the refcount taken in function sfe_ipv4_create_rule(),
672 * during call of __udp4_lib_lookup()
673 */
674 up = c->reply_match->up;
675 if (up) {
676 sk = (struct sock *)up;
677 sock_put(sk);
678 }
679
680 /*
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530681 * Release our hold of the source and dest devices and free the memory
682 * for our connection objects.
683 */
684 dev_put(c->original_dev);
685 dev_put(c->reply_dev);
686 kfree(c->original_match);
687 kfree(c->reply_match);
688 kfree(c);
689}
690
691/*
Ken Zhu88c58152021-12-09 15:12:06 -0800692 * sfe_ipv4_sync_status()
693 * update a connection status to its connection manager.
694 *
695 * si: the ipv4 context
696 * c: which connection to be notified
697 * reason: what kind of notification: flush, stats or destroy
698 */
699void sfe_ipv4_sync_status(struct sfe_ipv4 *si,
700 struct sfe_ipv4_connection *c,
701 sfe_sync_reason_t reason)
702{
703 struct sfe_connection_sync sis;
704 u64 now_jiffies;
705 sfe_sync_rule_callback_t sync_rule_callback;
706
707 rcu_read_lock();
708 sync_rule_callback = rcu_dereference(si->sync_rule_callback);
Ken Zhu7a43d882022-01-04 10:51:44 -0800709 rcu_read_unlock();
Ken Zhu88c58152021-12-09 15:12:06 -0800710 if (!sync_rule_callback) {
Ken Zhu88c58152021-12-09 15:12:06 -0800711 return;
712 }
713
714 /*
715 * Generate a sync message and then sync.
716 */
717 now_jiffies = get_jiffies_64();
718 sfe_ipv4_gen_sync_connection(si, c, &sis, reason, now_jiffies);
719 sync_rule_callback(&sis);
Ken Zhu88c58152021-12-09 15:12:06 -0800720}
721
722/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530723 * sfe_ipv4_flush_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100724 * Flush a connection and free all associated resources.
725 *
726 * We need to be called with bottom halves disabled locally as we need to acquire
727 * the connection hash lock and release it again. In general we're actually called
728 * from within a BH and so we're fine, but we're also called when connections are
729 * torn down.
730 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530731void sfe_ipv4_flush_connection(struct sfe_ipv4 *si,
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700732 struct sfe_ipv4_connection *c,
733 sfe_sync_reason_t reason)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100734{
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530735 BUG_ON(!c->removed);
736
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530737 this_cpu_inc(si->stats_pcpu->connection_flushes64);
Ken Zhu88c58152021-12-09 15:12:06 -0800738 sfe_ipv4_sync_status(si, c, reason);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100739
740 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100741 * Release our hold of the source and dest devices and free the memory
742 * for our connection objects.
743 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530744 call_rcu(&c->rcu, sfe_ipv4_free_connection_rcu);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100745}
746
747/*
Parikshit Guned31a8202022-01-05 22:15:04 +0530748 * sfe_ipv4_service_class_stats_inc()
749 * Increment per cpu per service class stats.
750 */
751void sfe_ipv4_service_class_stats_inc(struct sfe_ipv4 *si, uint8_t sid, uint64_t bytes)
752{
753 struct sfe_ipv4_service_class_stats_db *sc_stats_db = this_cpu_ptr(si->stats_pcpu_psc);
754 struct sfe_ipv4_per_service_class_stats *sc_stats = &sc_stats_db->psc_stats[sid];
755
756 write_seqcount_begin(&sc_stats->seq);
757 sc_stats->tx_bytes += bytes;
758 sc_stats->tx_packets++;
759 write_seqcount_end(&sc_stats->seq);
760}
761
762/*
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530763 * sfe_ipv4_exception_stats_inc()
764 * Increment exception stats.
765 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530766void sfe_ipv4_exception_stats_inc(struct sfe_ipv4 *si, enum sfe_ipv4_exception_events reason)
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530767{
768 struct sfe_ipv4_stats *stats = this_cpu_ptr(si->stats_pcpu);
769 stats->exception_events64[reason]++;
770 stats->packets_not_forwarded64++;
771}
772
773/*
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530774 * sfe_ipv4_is_loal_ip()
775 * Returns true if IP is local; returns false otherwise.
776 */
777static bool sfe_ipv4_is_local_ip(struct sfe_ipv4 *si, __be32 ip_addr)
778{
779 struct net_device *dev;
780
781 dev = ip_dev_find(&init_net, ip_addr);
782 if (dev) {
783 dev_put(dev);
784 return true;
785 }
786
787 return false;
788}
789
790/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100791 * sfe_ipv4_recv()
Matthew McClintocka8ad7962014-01-16 16:49:30 -0600792 * Handle packet receives and forwaring.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100793 *
794 * Returns 1 if the packet is forwarded or 0 if it isn't.
795 */
Amitesh Anand63be37d2021-12-24 20:51:48 +0530796int 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 +0100797{
798 struct sfe_ipv4 *si = &__si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100799 unsigned int len;
800 unsigned int tot_len;
801 unsigned int frag_off;
802 unsigned int ihl;
Ken Zhu88c58152021-12-09 15:12:06 -0800803 bool sync_on_find;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100804 bool ip_options;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530805 struct iphdr *iph;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700806 u32 protocol;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100807
808 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100809 * Check that we have space for an IP header here.
810 */
811 len = skb->len;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530812 if (unlikely(!pskb_may_pull(skb, sizeof(struct iphdr)))) {
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530813 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_HEADER_INCOMPLETE);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100814 DEBUG_TRACE("len: %u is too short\n", len);
815 return 0;
816 }
817
818 /*
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +0530819 * Validate ip csum if necessary. If ip_summed is set to CHECKSUM_UNNECESSARY, it is assumed
820 * that the L3 checksum is validated by the Rx interface or the tunnel interface that has
821 * generated the packet.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100822 */
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530823 iph = (struct iphdr *)skb->data;
Ratheesh Kannoth43d64f82021-10-20 08:23:29 +0530824 if (unlikely(skb->ip_summed != CHECKSUM_UNNECESSARY) && (ip_fast_csum((u8 *)iph, iph->ihl))) {
825 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_HEADER_CSUM_BAD);
826
827 DEBUG_TRACE("Bad IPv4 header csum: 0x%x\n", iph->check);
828 return 0;
829 }
830
831 /*
832 * Check that our "total length" is large enough for an IP header.
833 */
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100834 tot_len = ntohs(iph->tot_len);
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530835 if (unlikely(tot_len < sizeof(struct iphdr))) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100836
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530837 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_BAD_TOTAL_LENGTH);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100838 DEBUG_TRACE("tot_len: %u is too short\n", tot_len);
839 return 0;
840 }
841
842 /*
843 * Is our IP version wrong?
844 */
845 if (unlikely(iph->version != 4)) {
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530846 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_NON_V4);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100847 DEBUG_TRACE("IP version: %u\n", iph->version);
848 return 0;
849 }
850
851 /*
852 * Does our datagram fit inside the skb?
853 */
854 if (unlikely(tot_len > len)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100855 DEBUG_TRACE("tot_len: %u, exceeds len: %u\n", tot_len, len);
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530856 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_DATAGRAM_INCOMPLETE);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100857 return 0;
858 }
859
860 /*
861 * Do we have a non-initial fragment?
Nicolas Costaac2979c2014-01-14 10:35:24 -0600862 */
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100863 frag_off = ntohs(iph->frag_off);
864 if (unlikely(frag_off & IP_OFFSET)) {
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530865 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_NON_INITIAL_FRAGMENT);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100866 DEBUG_TRACE("non-initial fragment\n");
867 return 0;
868 }
869
870 /*
871 * If we have a (first) fragment then mark it to cause any connection to flush.
872 */
Ken Zhu88c58152021-12-09 15:12:06 -0800873 sync_on_find = unlikely(frag_off & IP_MF) ? true : false;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100874
875 /*
876 * Do we have any IP options? That's definite a slow path! If we do have IP
877 * options we need to recheck our header size.
878 */
879 ihl = iph->ihl << 2;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530880 ip_options = unlikely(ihl != sizeof(struct iphdr)) ? true : false;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100881 if (unlikely(ip_options)) {
882 if (unlikely(len < ihl)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100883
884 DEBUG_TRACE("len: %u is too short for header of size: %u\n", len, ihl);
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530885 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_IP_OPTIONS_INCOMPLETE);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100886 return 0;
887 }
888
Ken Zhu88c58152021-12-09 15:12:06 -0800889 sync_on_find = true;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100890 }
891
892 protocol = iph->protocol;
893 if (IPPROTO_UDP == protocol) {
Ken Zhu88c58152021-12-09 15:12:06 -0800894 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 +0100895 }
896
897 if (IPPROTO_TCP == protocol) {
Ken Zhu88c58152021-12-09 15:12:06 -0800898 return sfe_ipv4_recv_tcp(si, skb, dev, len, iph, ihl, sync_on_find, l2_info);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100899 }
900
901 if (IPPROTO_ICMP == protocol) {
902 return sfe_ipv4_recv_icmp(si, skb, dev, len, iph, ihl);
903 }
904
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530905#ifdef SFE_GRE_TUN_ENABLE
906 if (IPPROTO_GRE == protocol) {
Nitin Shetty2114a892022-01-28 20:03:56 +0530907 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 +0530908 }
909#endif
Tian Yangd98d91b2022-03-09 14:50:12 -0800910 if (IPPROTO_IPV6 == protocol) {
911 return sfe_ipv4_recv_tun6rd(si, skb, dev, len, iph, ihl, sync_on_find, l2_info, true);
912 }
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530913
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530914 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_UNHANDLED_PROTOCOL);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100915
916 DEBUG_TRACE("not UDP, TCP or ICMP: %u\n", protocol);
917 return 0;
918}
919
Nicolas Costa436926b2014-01-14 10:36:22 -0600920static void
921sfe_ipv4_update_tcp_state(struct sfe_ipv4_connection *c,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530922 struct sfe_ipv4_rule_create_msg *msg)
Nicolas Costa436926b2014-01-14 10:36:22 -0600923{
924 struct sfe_ipv4_connection_match *orig_cm;
925 struct sfe_ipv4_connection_match *repl_cm;
926 struct sfe_ipv4_tcp_connection_match *orig_tcp;
927 struct sfe_ipv4_tcp_connection_match *repl_tcp;
928
929 orig_cm = c->original_match;
930 repl_cm = c->reply_match;
931 orig_tcp = &orig_cm->protocol_state.tcp;
932 repl_tcp = &repl_cm->protocol_state.tcp;
933
934 /* update orig */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530935 if (orig_tcp->max_win < msg->tcp_rule.flow_max_window) {
936 orig_tcp->max_win = msg->tcp_rule.flow_max_window;
Nicolas Costa436926b2014-01-14 10:36:22 -0600937 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530938 if ((s32)(orig_tcp->end - msg->tcp_rule.flow_end) < 0) {
939 orig_tcp->end = msg->tcp_rule.flow_end;
Nicolas Costa436926b2014-01-14 10:36:22 -0600940 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530941 if ((s32)(orig_tcp->max_end - msg->tcp_rule.flow_max_end) < 0) {
942 orig_tcp->max_end = msg->tcp_rule.flow_max_end;
Nicolas Costa436926b2014-01-14 10:36:22 -0600943 }
944
945 /* update reply */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530946 if (repl_tcp->max_win < msg->tcp_rule.return_max_window) {
947 repl_tcp->max_win = msg->tcp_rule.return_max_window;
Nicolas Costa436926b2014-01-14 10:36:22 -0600948 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530949 if ((s32)(repl_tcp->end - msg->tcp_rule.return_end) < 0) {
950 repl_tcp->end = msg->tcp_rule.return_end;
Nicolas Costa436926b2014-01-14 10:36:22 -0600951 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530952 if ((s32)(repl_tcp->max_end - msg->tcp_rule.return_max_end) < 0) {
953 repl_tcp->max_end = msg->tcp_rule.return_max_end;
Nicolas Costa436926b2014-01-14 10:36:22 -0600954 }
955
956 /* update match flags */
957 orig_cm->flags &= ~SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
958 repl_cm->flags &= ~SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530959 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_NO_SEQ_CHECK) {
960
Nicolas Costa436926b2014-01-14 10:36:22 -0600961 orig_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
962 repl_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
963 }
964}
965
966static void
967sfe_ipv4_update_protocol_state(struct sfe_ipv4_connection *c,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530968 struct sfe_ipv4_rule_create_msg *msg)
Nicolas Costa436926b2014-01-14 10:36:22 -0600969{
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530970 switch (msg->tuple.protocol) {
Nicolas Costa436926b2014-01-14 10:36:22 -0600971 case IPPROTO_TCP:
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530972 sfe_ipv4_update_tcp_state(c, msg);
Nicolas Costa436926b2014-01-14 10:36:22 -0600973 break;
974 }
975}
976
Wayne Tanbb7f1782021-12-13 11:16:04 -0800977/*
978 * sfe_ipv4_match_entry_set_vlan()
979 */
980static void sfe_ipv4_match_entry_set_vlan(
981 struct sfe_ipv4_connection_match *cm,
982 u32 primary_ingress_vlan_tag,
983 u32 primary_egress_vlan_tag,
984 u32 secondary_ingress_vlan_tag,
985 u32 secondary_egress_vlan_tag)
986{
987 u16 tpid;
988 /*
989 * Prevent stacking header counts when updating.
990 */
991 cm->ingress_vlan_hdr_cnt = 0;
992 cm->egress_vlan_hdr_cnt = 0;
993 memset(cm->ingress_vlan_hdr, 0, sizeof(cm->ingress_vlan_hdr));
994 memset(cm->egress_vlan_hdr, 0, sizeof(cm->egress_vlan_hdr));
995
996 /*
997 * vlan_hdr[0] corresponds to outer tag
998 * vlan_hdr[1] corresponds to inner tag
999 * Extract the vlan information (tpid and tci) from rule message
1000 */
1001 if ((primary_ingress_vlan_tag & VLAN_VID_MASK) != SFE_VLAN_ID_NOT_CONFIGURED) {
1002 tpid = (u16)(primary_ingress_vlan_tag >> 16);
1003 cm->ingress_vlan_hdr[0].tpid = ntohs(tpid);
1004 cm->ingress_vlan_hdr[0].tci = (u16)primary_ingress_vlan_tag;
1005 cm->ingress_vlan_hdr_cnt++;
1006 }
1007
1008 if ((secondary_ingress_vlan_tag & VLAN_VID_MASK) != SFE_VLAN_ID_NOT_CONFIGURED) {
1009 tpid = (u16)(secondary_ingress_vlan_tag >> 16);
1010 cm->ingress_vlan_hdr[1].tpid = ntohs(tpid);
1011 cm->ingress_vlan_hdr[1].tci = (u16)secondary_ingress_vlan_tag;
1012 cm->ingress_vlan_hdr_cnt++;
1013 }
1014
1015 if ((primary_egress_vlan_tag & VLAN_VID_MASK) != SFE_VLAN_ID_NOT_CONFIGURED) {
1016 tpid = (u16)(primary_egress_vlan_tag >> 16);
1017 cm->egress_vlan_hdr[0].tpid = ntohs(tpid);
1018 cm->egress_vlan_hdr[0].tci = (u16)primary_egress_vlan_tag;
1019 cm->egress_vlan_hdr_cnt++;
1020 }
1021
1022 if ((secondary_egress_vlan_tag & VLAN_VID_MASK) != SFE_VLAN_ID_NOT_CONFIGURED) {
1023 tpid = (u16)(secondary_egress_vlan_tag >> 16);
1024 cm->egress_vlan_hdr[1].tpid = ntohs(tpid);
1025 cm->egress_vlan_hdr[1].tci = (u16)secondary_egress_vlan_tag;
1026 cm->egress_vlan_hdr_cnt++;
1027 }
1028}
1029
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301030void sfe_ipv4_update_rule(struct sfe_ipv4_rule_create_msg *msg)
Nicolas Costa436926b2014-01-14 10:36:22 -06001031{
1032 struct sfe_ipv4_connection *c;
1033 struct sfe_ipv4 *si = &__si;
1034
1035 spin_lock_bh(&si->lock);
1036
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301037 c = sfe_ipv4_find_connection(si,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301038 msg->tuple.protocol,
1039 msg->tuple.flow_ip,
1040 msg->tuple.flow_ident,
1041 msg->tuple.return_ip,
1042 msg->tuple.return_ident);
Nicolas Costa436926b2014-01-14 10:36:22 -06001043 if (c != NULL) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301044 sfe_ipv4_update_protocol_state(c, msg);
Nicolas Costa436926b2014-01-14 10:36:22 -06001045 }
1046
1047 spin_unlock_bh(&si->lock);
1048}
1049
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001050/*
Murat Sezginef190392022-04-04 17:56:31 -07001051 * sfe_ipv4_mark_rule_update()
1052 * Updates the mark values of match entries.
1053 */
1054void sfe_ipv4_mark_rule_update(struct sfe_connection_mark *mark)
1055{
1056 struct sfe_ipv4_connection *c;
1057 struct sfe_ipv4 *si = &__si;
1058
1059 spin_lock_bh(&si->lock);
1060 c = sfe_ipv4_find_connection(si, mark->protocol,
1061 mark->src_ip[0],
1062 mark->src_port,
1063 mark->dest_ip[0],
1064 mark->dest_port);
1065 if (!c) {
1066 spin_unlock_bh(&si->lock);
1067 DEBUG_WARN("%px: connection not found for mark update\n", mark);
1068 return;
1069 }
1070 c->original_match->mark = mark->mark;
1071 c->reply_match->mark = mark->mark;
1072 spin_unlock_bh(&si->lock);
1073 DEBUG_TRACE("%px: connection mark updated with %d\n", mark, mark->mark);
1074}
1075EXPORT_SYMBOL(sfe_ipv4_mark_rule_update);
1076
1077/*
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +05301078 * sfe_ipv4_xmit_eth_type_check()
1079 * Checking if MAC header has to be written.
1080 */
1081static inline bool sfe_ipv4_xmit_eth_type_check(struct net_device *dev, u32 cm_flags)
1082{
1083 if (!(dev->flags & IFF_NOARP)) {
1084 return true;
1085 }
1086
1087 /*
1088 * For PPPoE, since we are now supporting PPPoE encapsulation, we are writing L2 header.
1089 */
1090 if (unlikely(cm_flags & SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_ENCAP)) {
1091 return true;
1092 }
1093
1094 return false;
1095}
1096
1097/*
Jackson Bockus3fafbf32022-02-13 17:15:26 -08001098 * sfe_ipv4_service_class_stats_pcpu_get()
1099 * Gets one CPU's service class statistics.
1100 */
1101static inline bool sfe_ipv4_service_class_stats_pcpu_get(struct sfe_ipv4_per_service_class_stats *sc_stats, uint64_t *bytes, uint64_t *packets)
1102{
1103 uint32_t retries = 0;
1104 uint32_t seq;
1105 uint64_t bytes_tmp, packets_tmp;
1106
1107 do {
1108 seq = read_seqcount_begin(&sc_stats->seq);
1109 bytes_tmp = sc_stats->tx_bytes;
1110 packets_tmp = sc_stats->tx_packets;
1111 } while (read_seqcount_retry(&sc_stats->seq, seq) && ++retries < SFE_SERVICE_CLASS_STATS_MAX_RETRY);
1112
1113 *bytes += bytes_tmp;
1114 *packets += packets_tmp;
1115
1116 return retries < SFE_SERVICE_CLASS_STATS_MAX_RETRY;
1117}
1118
1119/*
1120 * sfe_ipv4_service_class_stats_get()
1121 * Copy the ipv4 statistics for the given service class.
1122 */
1123bool sfe_ipv4_service_class_stats_get(uint8_t sid, uint64_t *bytes, uint64_t *packets)
1124{
1125 struct sfe_ipv4 *si = &__si;
1126 uint32_t cpu = 0;
1127
1128 for_each_possible_cpu(cpu) {
1129 struct sfe_ipv4_service_class_stats_db *stats_db = per_cpu_ptr(si->stats_pcpu_psc, cpu);
1130 struct sfe_ipv4_per_service_class_stats *sc_stats = &stats_db->psc_stats[sid];
1131
1132 if (!sfe_ipv4_service_class_stats_pcpu_get(sc_stats, bytes, packets)) {
1133 return false;
1134 }
1135 }
1136
1137 return true;
1138}
1139
1140/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001141 * sfe_ipv4_create_rule()
1142 * Create a forwarding rule.
1143 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301144int sfe_ipv4_create_rule(struct sfe_ipv4_rule_create_msg *msg)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001145{
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001146 struct sfe_ipv4 *si = &__si;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301147 struct sfe_ipv4_connection *c, *c_old;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001148 struct sfe_ipv4_connection_match *original_cm;
1149 struct sfe_ipv4_connection_match *reply_cm;
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001150 struct net_device *dest_dev;
1151 struct net_device *src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301152 struct sfe_ipv4_5tuple *tuple = &msg->tuple;
Suruchi Sumanc1a4a612021-10-21 14:50:23 +05301153 s32 flow_interface_num = msg->conn_rule.flow_top_interface_num;
1154 s32 return_interface_num = msg->conn_rule.return_top_interface_num;
Amitesh Anand63be37d2021-12-24 20:51:48 +05301155 struct net *net;
1156 struct sock *sk;
1157 unsigned int src_if_idx;
Parikshit Guned31a8202022-01-05 22:15:04 +05301158 u32 flow_sawf_tag;
1159 u32 return_sawf_tag;
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001160
Suruchi Sumanc1a4a612021-10-21 14:50:23 +05301161 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_FLOW_BOTTOM_INTERFACE) {
1162 flow_interface_num = msg->conn_rule.flow_interface_num;
1163 }
1164
1165 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_RETURN_BOTTOM_INTERFACE) {
1166 return_interface_num = msg->conn_rule.return_interface_num;
1167 }
1168
1169 src_dev = dev_get_by_index(&init_net, flow_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301170 if (!src_dev) {
1171 DEBUG_WARN("%px: Unable to find src_dev corresponding to %d\n", msg,
Suruchi Sumanc1a4a612021-10-21 14:50:23 +05301172 flow_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301173 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
1174 return -EINVAL;
1175 }
1176
Suruchi Sumanc1a4a612021-10-21 14:50:23 +05301177 dest_dev = dev_get_by_index(&init_net, return_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301178 if (!dest_dev) {
1179 DEBUG_WARN("%px: Unable to find dest_dev corresponding to %d\n", msg,
Suruchi Sumanc1a4a612021-10-21 14:50:23 +05301180 return_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301181 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
1182 dev_put(src_dev);
1183 return -EINVAL;
1184 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001185
Matthew McClintock389b42a2014-09-24 14:05:51 -05001186 if (unlikely((dest_dev->reg_state != NETREG_REGISTERED) ||
1187 (src_dev->reg_state != NETREG_REGISTERED))) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301188 dev_put(src_dev);
1189 dev_put(dest_dev);
1190 DEBUG_WARN("%px: src_dev=%s and dest_dev=%s are unregistered\n", msg,
1191 src_dev->name, dest_dev->name);
1192 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
Matthew McClintock389b42a2014-09-24 14:05:51 -05001193 return -EINVAL;
1194 }
1195
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301196 /*
1197 * Allocate the various connection tracking objects.
1198 */
Parikshit Guneef1664c2022-03-24 14:15:42 +05301199 c = (struct sfe_ipv4_connection *)kzalloc(sizeof(struct sfe_ipv4_connection), GFP_ATOMIC);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301200 if (unlikely(!c)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301201 DEBUG_WARN("%px: memory allocation of connection entry failed\n", msg);
1202 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
1203 dev_put(src_dev);
1204 dev_put(dest_dev);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301205 return -ENOMEM;
1206 }
1207
Parikshit Guneef1664c2022-03-24 14:15:42 +05301208 original_cm = (struct sfe_ipv4_connection_match *)kzalloc(sizeof(struct sfe_ipv4_connection_match), GFP_ATOMIC);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301209 if (unlikely(!original_cm)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301210 DEBUG_WARN("%px: memory allocation of connection match entry failed\n", msg);
1211 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301212 kfree(c);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301213 dev_put(src_dev);
1214 dev_put(dest_dev);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301215 return -ENOMEM;
1216 }
1217
Parikshit Guneef1664c2022-03-24 14:15:42 +05301218 reply_cm = (struct sfe_ipv4_connection_match *)kzalloc(sizeof(struct sfe_ipv4_connection_match), GFP_ATOMIC);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301219 if (unlikely(!reply_cm)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301220 DEBUG_WARN("%px: memory allocation of connection match entry failed\n", msg);
1221 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301222 kfree(original_cm);
1223 kfree(c);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301224 dev_put(src_dev);
1225 dev_put(dest_dev);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301226 return -ENOMEM;
1227 }
1228
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301229 this_cpu_inc(si->stats_pcpu->connection_create_requests64);
1230
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001231 spin_lock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001232
1233 /*
Nicolas Costa436926b2014-01-14 10:36:22 -06001234 * Check to see if there is already a flow that matches the rule we're
1235 * trying to create. If there is then we can't create a new one.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001236 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301237 c_old = sfe_ipv4_find_connection(si,
Wayne Tanbb7f1782021-12-13 11:16:04 -08001238 msg->tuple.protocol,
1239 msg->tuple.flow_ip,
1240 msg->tuple.flow_ident,
1241 msg->tuple.return_ip,
1242 msg->tuple.return_ident);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301243
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301244 if (c_old != NULL) {
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301245 this_cpu_inc(si->stats_pcpu->connection_create_collisions64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001246
1247 /*
Nicolas Costa436926b2014-01-14 10:36:22 -06001248 * If we already have the flow then it's likely that this
1249 * request to create the connection rule contains more
1250 * up-to-date information. Check and update accordingly.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001251 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301252 sfe_ipv4_update_protocol_state(c, msg);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001253 spin_unlock_bh(&si->lock);
1254
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301255 kfree(reply_cm);
1256 kfree(original_cm);
1257 kfree(c);
1258
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301259 dev_put(src_dev);
1260 dev_put(dest_dev);
1261
Amitesh Anand63be37d2021-12-24 20:51:48 +05301262 DEBUG_TRACE("%px: connection already exists - p:%d\n"
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301263 " s: %s:%pM:%pI4:%u, d: %s:%pM:%pI4:%u\n",
Amitesh Anand63be37d2021-12-24 20:51:48 +05301264 msg, tuple->protocol,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301265 src_dev->name, msg->conn_rule.flow_mac, &tuple->flow_ip, ntohs(tuple->flow_ident),
1266 dest_dev->name, msg->conn_rule.return_mac, &tuple->return_ip, ntohs(tuple->return_ident));
1267
Nicolas Costa514fde02014-01-13 15:50:29 -06001268 return -EADDRINUSE;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001269 }
1270
1271 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001272 * Fill in the "original" direction connection matching object.
1273 * Note that the transmit MAC address is "dest_mac_xlate" because
1274 * we always know both ends of a connection by their translated
1275 * addresses and not their public addresses.
1276 */
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001277 original_cm->match_dev = src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301278 original_cm->match_protocol = tuple->protocol;
1279 original_cm->match_src_ip = tuple->flow_ip;
Suruchi Suman66609a72022-01-20 02:34:25 +05301280 original_cm->match_src_port = netif_is_vxlan(src_dev) ? 0 : tuple->flow_ident;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301281 original_cm->match_dest_ip = tuple->return_ip;
1282 original_cm->match_dest_port = tuple->return_ident;
1283
1284 original_cm->xlate_src_ip = msg->conn_rule.flow_ip_xlate;
1285 original_cm->xlate_src_port = msg->conn_rule.flow_ident_xlate;
1286 original_cm->xlate_dest_ip = msg->conn_rule.return_ip_xlate;
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301287 original_cm->xlate_dest_port = msg->conn_rule.return_ident_xlate;
1288
1289 if (tuple->protocol == IPPROTO_GRE) {
1290 /*
1291 * the PPTP is 4 tuple lookup.
1292 * During th rule lookup destination call id from packet
1293 * is matched against destination port in cm.
1294 */
1295 original_cm->match_src_port = 0;
1296 original_cm->xlate_src_port = 0;
1297 }
Wayne Tanbb7f1782021-12-13 11:16:04 -08001298
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001299 original_cm->xmit_dev = dest_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301300 original_cm->xmit_dev_mtu = msg->conn_rule.return_mtu;
1301
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001302 original_cm->connection = c;
1303 original_cm->counter_match = reply_cm;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301304
Amitesh Anand63be37d2021-12-24 20:51:48 +05301305 /*
1306 * UDP Socket is valid only in decap direction.
1307 */
1308 RCU_INIT_POINTER(original_cm->up, NULL);
1309
Ken Zhu37040ea2021-09-09 21:11:15 -07001310 if (msg->valid_flags & SFE_RULE_CREATE_MARK_VALID) {
1311 original_cm->mark = msg->mark_rule.flow_mark;
1312 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_MARK;
1313 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301314 if (msg->valid_flags & SFE_RULE_CREATE_QOS_VALID) {
1315 original_cm->priority = msg->qos_rule.flow_qos_tag;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001316 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PRIORITY_REMARK;
1317 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301318 if (msg->valid_flags & SFE_RULE_CREATE_DSCP_MARKING_VALID) {
1319 original_cm->dscp = msg->dscp_rule.flow_dscp << SFE_IPV4_DSCP_SHIFT;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001320 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_DSCP_REMARK;
1321 }
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301322 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1323 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_BRIDGE_FLOW;
1324 }
Ken Zhu7e38d1a2021-11-30 17:31:46 -08001325 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_FLOW_TRANSMIT_FAST) {
1326 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_FAST_XMIT_DEV_ADMISSION;
1327 }
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301328
Wayne Tanbb7f1782021-12-13 11:16:04 -08001329 /*
Parikshit Guned31a8202022-01-05 22:15:04 +05301330 * Mark SAWF metadata if the sawf tag is valid and set.
1331 */
1332 original_cm->sawf_valid = false;
1333 flow_sawf_tag = SFE_GET_SAWF_TAG(msg->sawf_rule.flow_mark);
1334 if (likely(SFE_SAWF_TAG_IS_VALID(flow_sawf_tag))) {
1335 original_cm->mark = msg->sawf_rule.flow_mark;
1336 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_MARK;
1337 original_cm->sawf_valid = true;
1338 }
1339
1340 /*
Wayne Tanbb7f1782021-12-13 11:16:04 -08001341 * Add VLAN rule to original_cm
1342 */
1343 if (msg->valid_flags & SFE_RULE_CREATE_VLAN_VALID) {
1344 struct sfe_vlan_rule *vlan_primary_rule = &msg->vlan_primary_rule;
1345 struct sfe_vlan_rule *vlan_secondary_rule = &msg->vlan_secondary_rule;
1346 sfe_ipv4_match_entry_set_vlan(original_cm,
1347 vlan_primary_rule->ingress_vlan_tag,
1348 vlan_primary_rule->egress_vlan_tag,
1349 vlan_secondary_rule->ingress_vlan_tag,
1350 vlan_secondary_rule->egress_vlan_tag);
1351
1352 if ((msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_RETURN_BOTTOM_INTERFACE) &&
1353 original_cm->egress_vlan_hdr_cnt > 0) {
1354 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_INSERT_EGRESS_VLAN_TAG;
1355 original_cm->l2_hdr_size += original_cm->egress_vlan_hdr_cnt * VLAN_HLEN;
1356 }
1357 }
1358
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301359 if ((IPPROTO_GRE == tuple->protocol) && !sfe_ipv4_is_local_ip(si, original_cm->match_dest_ip)) {
1360 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PASSTHROUGH;
1361 }
1362
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001363#ifdef CONFIG_NF_FLOW_COOKIE
1364 original_cm->flow_cookie = 0;
1365#endif
Zhi Chen8748eb32015-06-18 12:58:48 -07001366#ifdef CONFIG_XFRM
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301367 if (msg->valid_flags & SFE_RULE_CREATE_DIRECTION_VALID) {
1368 original_cm->flow_accel = msg->direction_rule.flow_accel;
1369 } else {
1370 original_cm->flow_accel = 1;
1371 }
Zhi Chen8748eb32015-06-18 12:58:48 -07001372#endif
Tian Yangd98d91b2022-03-09 14:50:12 -08001373
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301374 /*
1375 * If l2_features are disabled and flow uses l2 features such as macvlan/bridge/pppoe/vlan,
1376 * bottom interfaces are expected to be disabled in the flow rule and always top interfaces
1377 * are used. In such cases, do not use HW csum offload. csum offload is used only when we
1378 * are sending directly to the destination interface that supports it.
1379 */
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301380 if (likely(dest_dev->features & NETIF_F_HW_CSUM) && sfe_dev_has_hw_csum(dest_dev)) {
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301381 if ((msg->conn_rule.return_top_interface_num == msg->conn_rule.return_interface_num) ||
1382 (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_RETURN_BOTTOM_INTERFACE)) {
Ratheesh Kannoth48445532022-02-07 16:19:00 +05301383
1384 /*
1385 * Dont enable CSUM offload
1386 */
1387#if 0
Suruchi Sumanf2077182022-01-13 21:35:23 +05301388 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_CSUM_OFFLOAD;
Ratheesh Kannoth48445532022-02-07 16:19:00 +05301389#endif
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301390 }
1391 }
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001392
Ratheesh Kannoth5dee3772022-01-18 11:27:14 +05301393 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_SRC_INTERFACE_CHECK) {
1394 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_SRC_INTERFACE_CHECK;
1395 }
1396
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001397 /*
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301398 * Adding PPPoE parameters to original and reply entries based on the direction where
1399 * PPPoE header is valid in ECM rule.
1400 *
1401 * If PPPoE is valid in flow direction (from interface is PPPoE), then
1402 * original cm will have PPPoE at ingress (strip PPPoE header)
1403 * reply cm will have PPPoE at egress (add PPPoE header)
1404 *
1405 * If PPPoE is valid in return direction (to interface is PPPoE), then
1406 * original cm will have PPPoE at egress (add PPPoE header)
1407 * reply cm will have PPPoE at ingress (strip PPPoE header)
1408 */
1409 if (msg->valid_flags & SFE_RULE_CREATE_PPPOE_DECAP_VALID) {
1410 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_DECAP;
1411 original_cm->pppoe_session_id = msg->pppoe_rule.flow_pppoe_session_id;
1412 ether_addr_copy(original_cm->pppoe_remote_mac, msg->pppoe_rule.flow_pppoe_remote_mac);
1413
1414 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_ENCAP;
Wayne Tanbb7f1782021-12-13 11:16:04 -08001415 reply_cm->l2_hdr_size += SFE_PPPOE_SESSION_HEADER_SIZE;
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301416 reply_cm->pppoe_session_id = msg->pppoe_rule.flow_pppoe_session_id;
1417 ether_addr_copy(reply_cm->pppoe_remote_mac, msg->pppoe_rule.flow_pppoe_remote_mac);
1418 }
1419
1420 if (msg->valid_flags & SFE_RULE_CREATE_PPPOE_ENCAP_VALID) {
1421 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_ENCAP;
Wayne Tanbb7f1782021-12-13 11:16:04 -08001422 original_cm->l2_hdr_size += SFE_PPPOE_SESSION_HEADER_SIZE;
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301423 original_cm->pppoe_session_id = msg->pppoe_rule.return_pppoe_session_id;
1424 ether_addr_copy(original_cm->pppoe_remote_mac, msg->pppoe_rule.return_pppoe_remote_mac);
1425
1426 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_DECAP;
1427 reply_cm->pppoe_session_id = msg->pppoe_rule.return_pppoe_session_id;
1428 ether_addr_copy(reply_cm->pppoe_remote_mac, msg->pppoe_rule.return_pppoe_remote_mac);
1429 }
1430
Ratheesh Kannoth5dee3772022-01-18 11:27:14 +05301431 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_SRC_INTERFACE_CHECK) {
1432 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_SRC_INTERFACE_CHECK;
1433 }
1434
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301435 /*
Ken Zhubbf49652021-09-12 15:33:09 -07001436 * For the non-arp interface, we don't write L2 HDR.
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001437 */
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +05301438 if (sfe_ipv4_xmit_eth_type_check(dest_dev, original_cm->flags)) {
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301439
1440 /*
1441 * Check whether the rule has configured a specific source MAC address to use.
1442 * This is needed when virtual L3 interfaces such as br-lan, macvlan, vlan are used during egress
1443 */
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301444
1445 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1446 ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)msg->conn_rule.flow_mac);
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301447 } else {
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301448 if ((msg->valid_flags & SFE_RULE_CREATE_SRC_MAC_VALID) &&
1449 (msg->src_mac_rule.mac_valid_flags & SFE_SRC_MAC_RETURN_VALID)) {
1450 ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)msg->src_mac_rule.return_src_mac);
1451 } else {
1452 ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)dest_dev->dev_addr);
1453 }
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301454 }
1455
1456 ether_addr_copy((u8 *)original_cm->xmit_dest_mac, (u8 *)msg->conn_rule.return_mac);
1457
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001458 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_L2_HDR;
Wayne Tanbb7f1782021-12-13 11:16:04 -08001459 original_cm->l2_hdr_size += ETH_HLEN;
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001460
1461 /*
1462 * If our dev writes Ethernet headers then we can write a really fast
1463 * version.
1464 */
1465 if (dest_dev->header_ops) {
1466 if (dest_dev->header_ops->create == eth_header) {
1467 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR;
1468 }
1469 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001470 }
1471
1472 /*
1473 * Fill in the "reply" direction connection matching object.
1474 */
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001475 reply_cm->match_dev = dest_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301476 reply_cm->match_protocol = tuple->protocol;
1477 reply_cm->match_src_ip = msg->conn_rule.return_ip_xlate;
Amitesh Anand63be37d2021-12-24 20:51:48 +05301478
1479 /*
1480 * Keep source port as 0 for VxLAN tunnels.
1481 */
1482 if (netif_is_vxlan(src_dev) || netif_is_vxlan(dest_dev)) {
1483 reply_cm->match_src_port = 0;
1484 } else {
1485 reply_cm->match_src_port = msg->conn_rule.return_ident_xlate;
1486 }
1487
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301488 reply_cm->match_dest_ip = msg->conn_rule.flow_ip_xlate;
1489 reply_cm->match_dest_port = msg->conn_rule.flow_ident_xlate;
1490
1491 reply_cm->xlate_src_ip = tuple->return_ip;
1492 reply_cm->xlate_src_port = tuple->return_ident;
1493 reply_cm->xlate_dest_ip = tuple->flow_ip;
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301494 reply_cm->xlate_dest_port = tuple->flow_ident;
1495
1496 if (tuple->protocol == IPPROTO_GRE) {
1497 /*
1498 * the PPTP is 4 tuple lookup.
1499 * During th rule lookup destination call id from packet
1500 * is matched against destination port in cm.
1501 */
1502 reply_cm->match_src_port = 0;
1503 reply_cm->xlate_src_port = 0;
1504 }
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301505
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001506 reply_cm->xmit_dev = src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301507 reply_cm->xmit_dev_mtu = msg->conn_rule.flow_mtu;
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301508
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001509 reply_cm->connection = c;
1510 reply_cm->counter_match = original_cm;
Ken Zhu37040ea2021-09-09 21:11:15 -07001511
Ken Zhu37040ea2021-09-09 21:11:15 -07001512 if (msg->valid_flags & SFE_RULE_CREATE_MARK_VALID) {
1513 reply_cm->mark = msg->mark_rule.return_mark;
1514 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_MARK;
1515 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301516 if (msg->valid_flags & SFE_RULE_CREATE_QOS_VALID) {
1517 reply_cm->priority = msg->qos_rule.return_qos_tag;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001518 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PRIORITY_REMARK;
1519 }
Wayne Tanbb7f1782021-12-13 11:16:04 -08001520
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301521 if (msg->valid_flags & SFE_RULE_CREATE_DSCP_MARKING_VALID) {
1522 reply_cm->dscp = msg->dscp_rule.return_dscp << SFE_IPV4_DSCP_SHIFT;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001523 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_DSCP_REMARK;
1524 }
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301525 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1526 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_BRIDGE_FLOW;
1527 }
Ken Zhu7e38d1a2021-11-30 17:31:46 -08001528 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_RETURN_TRANSMIT_FAST) {
1529 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_FAST_XMIT_DEV_ADMISSION;
1530 }
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301531
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301532 if ((IPPROTO_GRE == tuple->protocol) && !sfe_ipv4_is_local_ip(si, reply_cm->match_dest_ip)) {
1533 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PASSTHROUGH;
1534 }
1535
Amitesh Anand63be37d2021-12-24 20:51:48 +05301536 /*
Parikshit Guned31a8202022-01-05 22:15:04 +05301537 * Mark SAWF metadata in reply match if the sawf tag is valid.
1538 */
1539 reply_cm->sawf_valid = false;
1540 return_sawf_tag = SFE_GET_SAWF_TAG(msg->sawf_rule.return_mark);
1541 if (likely(SFE_SAWF_TAG_IS_VALID(return_sawf_tag))) {
1542 reply_cm->mark = msg->sawf_rule.return_mark;
1543 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_MARK;
1544 reply_cm->sawf_valid = true;
1545 }
1546
1547 /*
Amitesh Anand63be37d2021-12-24 20:51:48 +05301548 * Setup UDP Socket if found to be valid for decap.
1549 */
1550 RCU_INIT_POINTER(reply_cm->up, NULL);
1551 net = dev_net(reply_cm->match_dev);
1552 src_if_idx = src_dev->ifindex;
1553
1554 rcu_read_lock();
1555
1556 /*
1557 * Look for the associated sock object.
1558 * __udp4_lib_lookup() holds a reference for this sock object,
1559 * which will be released in sfe_ipv4_free_connection_rcu()
1560 */
1561#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
1562 sk = __udp4_lib_lookup(net, reply_cm->match_dest_ip, reply_cm->match_dest_port,
1563 reply_cm->xlate_src_ip, reply_cm->xlate_src_port, src_if_idx, &udp_table);
1564#else
1565 sk = __udp4_lib_lookup(net, reply_cm->match_dest_ip, reply_cm->match_dest_port,
1566 reply_cm->xlate_src_ip, reply_cm->xlate_src_port, src_if_idx, 0, &udp_table, NULL);
1567#endif
1568
1569 rcu_read_unlock();
1570
1571 /*
1572 * We set the UDP sock pointer as valid only for decap direction.
1573 */
1574 if (sk && udp_sk(sk)->encap_type) {
1575#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
1576 if (!atomic_add_unless(&sk->sk_refcnt, 1, 0)) {
1577#else
1578 if (!refcount_inc_not_zero(&sk->sk_refcnt)) {
1579#endif
Nitin Shetty9ab15622022-04-11 08:04:06 +05301580 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
Wayne Tanbb7f1782021-12-13 11:16:04 -08001581 spin_unlock_bh(&si->lock);
Amitesh Anand63be37d2021-12-24 20:51:48 +05301582 kfree(reply_cm);
1583 kfree(original_cm);
1584 kfree(c);
1585
1586 DEBUG_TRACE("%px: sfe: unable to take reference for socket(%px) p:%d\n"
1587 " s: %s:%pM:%pI4:%u, d: %s:%pM:%pI4:%u\n",
1588 msg, sk, tuple->protocol,
1589 src_dev->name, msg->conn_rule.flow_mac, &tuple->flow_ip, ntohs(tuple->flow_ident),
1590 dest_dev->name, msg->conn_rule.return_mac, &tuple->return_ip, ntohs(tuple->return_ident));
1591
1592 dev_put(src_dev);
1593 dev_put(dest_dev);
1594
1595 return -ESHUTDOWN;
1596 }
1597
1598 rcu_assign_pointer(reply_cm->up, udp_sk(sk));
1599
1600 DEBUG_INFO("%px: Sock(%px) lookup success with reply_cm direction\n", msg, sk);
1601 DEBUG_INFO("%px: SFE connection -\n"
1602 " s: %s:%pI4(%pI4):%u(%u)\n"
1603 " d: %s:%pI4(%pI4):%u(%u)\n",
1604 msg, reply_cm->match_dev->name, &reply_cm->match_src_ip, &reply_cm->xlate_src_ip,
1605 ntohs(reply_cm->match_src_port), ntohs(reply_cm->xlate_src_port),
1606 reply_cm->xmit_dev->name, &reply_cm->match_dest_ip, &reply_cm->xlate_dest_ip,
1607 ntohs(reply_cm->match_dest_port), ntohs(reply_cm->xlate_dest_port));
1608 }
1609
Wayne Tanbb7f1782021-12-13 11:16:04 -08001610 /*
1611 * Add VLAN rule to reply_cm
1612 */
1613 if (msg->valid_flags & SFE_RULE_CREATE_VLAN_VALID) {
1614 struct sfe_vlan_rule *vlan_primary_rule = &msg->vlan_primary_rule;
1615 struct sfe_vlan_rule *vlan_secondary_rule = &msg->vlan_secondary_rule;
1616 sfe_ipv4_match_entry_set_vlan(reply_cm,
1617 vlan_primary_rule->egress_vlan_tag,
1618 vlan_primary_rule->ingress_vlan_tag,
1619 vlan_secondary_rule->egress_vlan_tag,
1620 vlan_secondary_rule->ingress_vlan_tag);
1621
1622 if ((msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_FLOW_BOTTOM_INTERFACE) &&
1623 reply_cm->egress_vlan_hdr_cnt > 0) {
1624 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_INSERT_EGRESS_VLAN_TAG;
1625 reply_cm->l2_hdr_size += reply_cm->egress_vlan_hdr_cnt * VLAN_HLEN;
1626 }
1627 }
1628
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301629 /*
1630 * the net_protocol handler will be used only in decap path
1631 * for non passthrough case.
1632 */
1633 original_cm->proto = NULL;
1634 reply_cm->proto = NULL;
Tian Yang46d6eb02022-03-31 10:26:16 -07001635 original_cm->top_interface_dev = NULL;
1636 reply_cm->top_interface_dev = NULL;
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301637
1638#ifdef SFE_GRE_TUN_ENABLE
1639 if ((IPPROTO_GRE == tuple->protocol) && !(reply_cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_PASSTHROUGH)) {
1640 rcu_read_lock();
1641 reply_cm->proto = rcu_dereference(inet_protos[IPPROTO_GRE]);
1642 rcu_read_unlock();
1643
1644 if (unlikely(!reply_cm->proto)) {
Nitin Shetty9ab15622022-04-11 08:04:06 +05301645 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
1646 spin_unlock_bh(&si->lock);
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301647 kfree(reply_cm);
1648 kfree(original_cm);
1649 kfree(c);
1650 dev_put(src_dev);
1651 dev_put(dest_dev);
1652 DEBUG_WARN("sfe: GRE proto handler is not registered\n");
1653 return -EPERM;
1654 }
1655 }
1656#endif
1657
Tian Yangd98d91b2022-03-09 14:50:12 -08001658 if (IPPROTO_IPV6 == tuple->protocol) {
1659 original_cm->proto = NULL;
1660 rcu_read_lock();
1661 reply_cm->proto = rcu_dereference(inet_protos[IPPROTO_IPV6]);
1662 rcu_read_unlock();
Tian Yang46d6eb02022-03-31 10:26:16 -07001663 reply_cm->top_interface_dev = dev_get_by_index(&init_net, msg->conn_rule.return_top_interface_num);
1664
1665 if (unlikely(!reply_cm->top_interface_dev)) {
1666 DEBUG_WARN("%px: Unable to find top_interface_dev corresponding to %d\n", msg,
1667 msg->conn_rule.return_top_interface_num);
1668 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
1669 spin_unlock_bh(&si->lock);
1670 kfree(reply_cm);
1671 kfree(original_cm);
1672 kfree(c);
1673 dev_put(src_dev);
1674 dev_put(dest_dev);
1675 return -EINVAL;
1676 }
Tian Yangd98d91b2022-03-09 14:50:12 -08001677 }
1678
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001679#ifdef CONFIG_NF_FLOW_COOKIE
1680 reply_cm->flow_cookie = 0;
1681#endif
Zhi Chen8748eb32015-06-18 12:58:48 -07001682#ifdef CONFIG_XFRM
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301683 if (msg->valid_flags & SFE_RULE_CREATE_DIRECTION_VALID) {
1684 reply_cm->flow_accel = msg->direction_rule.return_accel;
1685 } else {
1686 reply_cm->flow_accel = 1;
1687 }
1688
Zhi Chen8748eb32015-06-18 12:58:48 -07001689#endif
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301690 /*
1691 * If l2_features are disabled and flow uses l2 features such as macvlan/bridge/pppoe/vlan,
1692 * bottom interfaces are expected to be disabled in the flow rule and always top interfaces
1693 * are used. In such cases, do not use HW csum offload. csum offload is used only when we
1694 * are sending directly to the destination interface that supports it.
1695 */
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301696 if (likely(src_dev->features & NETIF_F_HW_CSUM) && sfe_dev_has_hw_csum(src_dev)) {
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301697 if ((msg->conn_rule.flow_top_interface_num == msg->conn_rule.flow_interface_num) ||
1698 (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_FLOW_BOTTOM_INTERFACE)) {
Ratheesh Kannoth48445532022-02-07 16:19:00 +05301699 /*
1700 * Dont enable CSUM offload
1701 */
1702#if 0
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301703 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_CSUM_OFFLOAD;
Ratheesh Kannoth48445532022-02-07 16:19:00 +05301704#endif
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301705 }
1706 }
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001707
1708 /*
Ken Zhubbf49652021-09-12 15:33:09 -07001709 * For the non-arp interface, we don't write L2 HDR.
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001710 */
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +05301711 if (sfe_ipv4_xmit_eth_type_check(src_dev, reply_cm->flags)) {
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301712
1713 /*
1714 * Check whether the rule has configured a specific source MAC address to use.
1715 * This is needed when virtual L3 interfaces such as br-lan, macvlan, vlan are used during egress
1716 */
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301717
1718 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1719 ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)msg->conn_rule.return_mac);
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301720 } else {
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301721 if ((msg->valid_flags & SFE_RULE_CREATE_SRC_MAC_VALID) &&
1722 (msg->src_mac_rule.mac_valid_flags & SFE_SRC_MAC_FLOW_VALID)) {
1723 ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)msg->src_mac_rule.flow_src_mac);
1724 } else {
1725 ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)src_dev->dev_addr);
1726 }
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301727 }
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301728
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301729 ether_addr_copy((u8 *)reply_cm->xmit_dest_mac, (u8 *)msg->conn_rule.flow_mac);
1730
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001731 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_L2_HDR;
Wayne Tanbb7f1782021-12-13 11:16:04 -08001732 reply_cm->l2_hdr_size += ETH_HLEN;
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001733
1734 /*
1735 * If our dev writes Ethernet headers then we can write a really fast
1736 * version.
1737 */
1738 if (src_dev->header_ops) {
1739 if (src_dev->header_ops->create == eth_header) {
1740 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR;
1741 }
1742 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001743 }
1744
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301745 if ((tuple->return_ip != msg->conn_rule.return_ip_xlate) ||
1746 (tuple->return_ident != msg->conn_rule.return_ident_xlate)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001747 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST;
1748 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC;
1749 }
1750
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301751 if ((tuple->flow_ip != msg->conn_rule.flow_ip_xlate) ||
1752 (tuple->flow_ident != msg->conn_rule.flow_ident_xlate)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001753 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC;
1754 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST;
1755 }
1756
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001757 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001758 * Initialize the protocol-specific information that we track.
1759 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301760 switch (tuple->protocol) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001761 case IPPROTO_TCP:
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301762 original_cm->protocol_state.tcp.win_scale = msg->tcp_rule.flow_window_scale;
1763 original_cm->protocol_state.tcp.max_win = msg->tcp_rule.flow_max_window ? msg->tcp_rule.flow_max_window : 1;
1764 original_cm->protocol_state.tcp.end = msg->tcp_rule.flow_end;
1765 original_cm->protocol_state.tcp.max_end = msg->tcp_rule.flow_max_end;
1766
1767 reply_cm->protocol_state.tcp.win_scale = msg->tcp_rule.return_window_scale;
1768 reply_cm->protocol_state.tcp.max_win = msg->tcp_rule.return_max_window ? msg->tcp_rule.return_max_window : 1;
1769 reply_cm->protocol_state.tcp.end = msg->tcp_rule.return_end;
1770 reply_cm->protocol_state.tcp.max_end = msg->tcp_rule.return_max_end;
1771
1772 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_NO_SEQ_CHECK) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001773 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
1774 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
1775 }
1776 break;
1777 }
1778
Wayne Tanbb7f1782021-12-13 11:16:04 -08001779 /*
1780 * Fill in the ipv4_connection object.
1781 */
1782 c->protocol = tuple->protocol;
1783 c->src_ip = tuple->flow_ip;
1784 c->src_ip_xlate = msg->conn_rule.flow_ip_xlate;
1785 c->src_port = tuple->flow_ident;
1786 c->src_port_xlate = msg->conn_rule.flow_ident_xlate;
1787 c->original_dev = src_dev;
1788 c->original_match = original_cm;
1789 c->dest_ip = tuple->return_ip;
1790 c->dest_ip_xlate = msg->conn_rule.return_ip_xlate;
1791 c->dest_port = tuple->return_ident;
1792 c->dest_port_xlate = msg->conn_rule.return_ident_xlate;
1793 c->reply_dev = dest_dev;
1794 c->reply_match = reply_cm;
1795 c->debug_read_seq = 0;
1796 c->last_sync_jiffies = get_jiffies_64();
1797 c->removed = false;
1798
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001799 sfe_ipv4_connection_match_compute_translations(original_cm);
1800 sfe_ipv4_connection_match_compute_translations(reply_cm);
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301801 sfe_ipv4_insert_connection(si, c);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001802
1803 spin_unlock_bh(&si->lock);
1804
1805 /*
1806 * We have everything we need!
1807 */
Wayne Tanbb7f1782021-12-13 11:16:04 -08001808 DEBUG_INFO("NEW connection - p: %d\n"
1809 "original_cm: match_dev=src_dev: %s %d %pM\n"
1810 " xmit_dev=dest_dev: %s %d %pM\n"
1811 " xmit_src_mac: %pM\n"
1812 " xmit_dest_mac: %pM\n"
1813 " flags: %x l2_hdr: %u\n"
1814 "flow_ip: %pI4:%u\n"
1815 "flow_ip_xlate: %pI4:%u\n"
1816 "flow_mac: %pM\n"
1817 "reply_cm: match_dev=dest_dev: %s %d %pM\n"
1818 " xmit_dev=src_dev: %s %d %pM\n"
1819 " xmit_src_mac: %pM\n"
1820 " xmit_dest_mac: %pM\n"
1821 " flags: %x l2_hdr: %u\n"
1822 "return_ip: %pI4:%u\n"
1823 "return_ip_xlate: %pI4:%u\n"
1824 "return_mac: %pM\n"
1825 "flags: valid=%x src_mac_valid=%x\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301826 tuple->protocol,
Wayne Tanbb7f1782021-12-13 11:16:04 -08001827 original_cm->match_dev->name, original_cm->match_dev->ifindex, original_cm->match_dev->dev_addr,
1828 original_cm->xmit_dev->name, original_cm->xmit_dev->ifindex, original_cm->xmit_dev->dev_addr,
1829 original_cm->xmit_src_mac, original_cm->xmit_dest_mac, original_cm->flags, original_cm->l2_hdr_size,
1830 &tuple->flow_ip, ntohs(tuple->flow_ident),
1831 &msg->conn_rule.flow_ip_xlate, ntohs(msg->conn_rule.flow_ident_xlate),
1832 msg->conn_rule.flow_mac,
1833 reply_cm->match_dev->name, reply_cm->match_dev->ifindex, reply_cm->match_dev->dev_addr,
1834 reply_cm->xmit_dev->name, reply_cm->xmit_dev->ifindex, reply_cm->xmit_dev->dev_addr,
1835 reply_cm->xmit_src_mac, reply_cm->xmit_dest_mac, reply_cm->flags, reply_cm->l2_hdr_size,
1836 &tuple->return_ip, ntohs(tuple->return_ident),
1837 &msg->conn_rule.return_ip_xlate, ntohs(msg->conn_rule.return_ident_xlate),
1838 msg->conn_rule.return_mac,
1839 msg->valid_flags, msg->src_mac_rule.mac_valid_flags);
Nicolas Costa514fde02014-01-13 15:50:29 -06001840
1841 return 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001842}
1843
1844/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001845 * sfe_ipv4_destroy_rule()
1846 * Destroy a forwarding rule.
1847 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301848void sfe_ipv4_destroy_rule(struct sfe_ipv4_rule_destroy_msg *msg)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001849{
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001850 struct sfe_ipv4 *si = &__si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001851 struct sfe_ipv4_connection *c;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301852 bool ret;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301853 struct sfe_ipv4_5tuple *tuple = &msg->tuple;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001854
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301855 this_cpu_inc(si->stats_pcpu->connection_destroy_requests64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001856 spin_lock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001857
1858 /*
1859 * Check to see if we have a flow that matches the rule we're trying
1860 * to destroy. If there isn't then we can't destroy it.
1861 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301862 c = sfe_ipv4_find_connection(si, tuple->protocol, tuple->flow_ip, tuple->flow_ident,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301863 tuple->return_ip, tuple->return_ident);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001864 if (!c) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001865 spin_unlock_bh(&si->lock);
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301866 this_cpu_inc(si->stats_pcpu->connection_destroy_misses64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001867
1868 DEBUG_TRACE("connection does not exist - p: %d, s: %pI4:%u, d: %pI4:%u\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301869 tuple->protocol, &tuple->flow_ip, ntohs(tuple->flow_ident),
1870 &tuple->return_ip, ntohs(tuple->return_ident));
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001871 return;
1872 }
1873
1874 /*
1875 * Remove our connection details from the hash tables.
1876 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301877 ret = sfe_ipv4_remove_connection(si, c);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001878 spin_unlock_bh(&si->lock);
1879
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301880 if (ret) {
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301881 sfe_ipv4_flush_connection(si, c, SFE_SYNC_REASON_DESTROY);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301882 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001883
1884 DEBUG_INFO("connection destroyed - p: %d, s: %pI4:%u, d: %pI4:%u\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301885 tuple->protocol, &tuple->flow_ip, ntohs(tuple->flow_ident),
1886 &tuple->return_ip, ntohs(tuple->return_ident));
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001887}
1888
1889/*
Ken Zhu7a43d882022-01-04 10:51:44 -08001890 * sfe_ipv4_sync_invoke()
1891 * Schedule many sync stats.
1892 */
1893bool sfe_ipv4_sync_invoke(uint16_t index)
1894{
1895 struct sfe_ipv4 *si = &__si;
1896 DEBUG_INFO("Request for a sync with index[%d]\n", index);
1897 return schedule_delayed_work_on(si->work_cpu, &(si->sync_dwork), 0);
1898}
1899
1900/*
1901 * sfe_ipv4_register_sync_rule_callback()
1902 * Register a callback for many rule synchronization.
1903 */
1904void sfe_ipv4_register_many_sync_callback(sfe_ipv4_many_sync_callback_t cb)
1905{
1906 struct sfe_ipv4 *si = &__si;
1907
1908 spin_lock_bh(&si->lock);
1909 rcu_assign_pointer(si->many_sync_callback, cb);
1910 spin_unlock_bh(&si->lock);
1911}
1912
1913/*
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001914 * sfe_ipv4_register_sync_rule_callback()
1915 * Register a callback for rule synchronization.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001916 */
Xiaoping Fand44a5b42015-05-26 17:37:37 -07001917void sfe_ipv4_register_sync_rule_callback(sfe_sync_rule_callback_t sync_rule_callback)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001918{
1919 struct sfe_ipv4 *si = &__si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001920
1921 spin_lock_bh(&si->lock);
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001922 rcu_assign_pointer(si->sync_rule_callback, sync_rule_callback);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001923 spin_unlock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001924}
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001925/*
1926 * sfe_ipv4_get_debug_dev()
1927 */
1928static ssize_t sfe_ipv4_get_debug_dev(struct device *dev,
1929 struct device_attribute *attr,
1930 char *buf)
1931{
1932 struct sfe_ipv4 *si = &__si;
1933 ssize_t count;
1934 int num;
1935
1936 spin_lock_bh(&si->lock);
1937 num = si->debug_dev;
1938 spin_unlock_bh(&si->lock);
1939
1940 count = snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", num);
1941 return count;
1942}
1943
1944/*
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001945 * sysfs attributes.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001946 */
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001947static const struct device_attribute sfe_ipv4_debug_dev_attr =
Xiaoping Fane70da412016-02-26 16:47:57 -08001948 __ATTR(debug_dev, S_IWUSR | S_IRUGO, sfe_ipv4_get_debug_dev, NULL);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001949
1950/*
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001951 * sfe_ipv4_destroy_all_rules_for_dev()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001952 * Destroy all connections that match a particular device.
1953 *
1954 * If we pass dev as NULL then this destroys all connections.
1955 */
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001956void sfe_ipv4_destroy_all_rules_for_dev(struct net_device *dev)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001957{
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001958 struct sfe_ipv4 *si = &__si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001959 struct sfe_ipv4_connection *c;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301960 bool ret;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001961
Xiaoping Fan34586472015-07-03 02:20:35 -07001962another_round:
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001963 spin_lock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001964
Xiaoping Fan34586472015-07-03 02:20:35 -07001965 for (c = si->all_connections_head; c; c = c->all_connections_next) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001966 /*
Xiaoping Fan34586472015-07-03 02:20:35 -07001967 * Does this connection relate to the device we are destroying?
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001968 */
1969 if (!dev
1970 || (dev == c->original_dev)
1971 || (dev == c->reply_dev)) {
Xiaoping Fan34586472015-07-03 02:20:35 -07001972 break;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001973 }
Xiaoping Fan34586472015-07-03 02:20:35 -07001974 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001975
Xiaoping Fan34586472015-07-03 02:20:35 -07001976 if (c) {
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301977 ret = sfe_ipv4_remove_connection(si, c);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001978 }
1979
1980 spin_unlock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07001981
1982 if (c) {
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301983 if (ret) {
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301984 sfe_ipv4_flush_connection(si, c, SFE_SYNC_REASON_DESTROY);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301985 }
Xiaoping Fan34586472015-07-03 02:20:35 -07001986 goto another_round;
1987 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001988}
1989
1990/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001991 * sfe_ipv4_periodic_sync()
1992 */
Ken Zhu137722d2021-09-23 17:57:36 -07001993static void sfe_ipv4_periodic_sync(struct work_struct *work)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001994{
Ken Zhu137722d2021-09-23 17:57:36 -07001995 struct sfe_ipv4 *si = container_of((struct delayed_work *)work, struct sfe_ipv4, sync_dwork);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07001996 u64 now_jiffies;
Ken Zhu7a43d882022-01-04 10:51:44 -08001997 int quota,count;
1998 sfe_ipv4_many_sync_callback_t sync_rule_callback;
Ken Zhudc423672021-09-02 18:27:01 -07001999 struct sfe_ipv4_connection *c;
Ken Zhu7a43d882022-01-04 10:51:44 -08002000 struct sfe_ipv4_conn_sync *conn_sync;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002001
2002 now_jiffies = get_jiffies_64();
2003
Dave Hudsondcd08fb2013-11-22 09:25:16 -06002004 rcu_read_lock();
Ken Zhu7a43d882022-01-04 10:51:44 -08002005 sync_rule_callback = rcu_dereference(si->many_sync_callback);
2006 rcu_read_unlock();
Dave Hudsondcd08fb2013-11-22 09:25:16 -06002007 if (!sync_rule_callback) {
Ken Zhu7a43d882022-01-04 10:51:44 -08002008 return;
Dave Hudsondcd08fb2013-11-22 09:25:16 -06002009 }
2010
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002011 spin_lock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002012
2013 /*
Ken Zhudc423672021-09-02 18:27:01 -07002014 * If we have reached the end of the connection list, walk from
2015 * the connection head.
2016 */
2017 c = si->wc_next;
2018 if (unlikely(!c)) {
2019 c = si->all_connections_head;
2020 }
2021
2022 /*
Ken Zhu7a43d882022-01-04 10:51:44 -08002023 * Get the max number of connections to be put in this sync msg.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002024 */
Ken Zhu7a43d882022-01-04 10:51:44 -08002025 quota = sfe_ipv4_sync_max_number;
2026 conn_sync = sfe_ipv4_sync_many_msg->msg.conn_stats_many.conn_sync;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002027
2028 /*
Ken Zhudc423672021-09-02 18:27:01 -07002029 * Walk the "all connection" list and sync the connection state.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002030 */
Ken Zhudc423672021-09-02 18:27:01 -07002031 while (likely(c && quota)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002032 struct sfe_ipv4_connection_match *cm;
2033 struct sfe_ipv4_connection_match *counter_cm;
Xiaoping Fand44a5b42015-05-26 17:37:37 -07002034 struct sfe_connection_sync sis;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002035
Ken Zhudc423672021-09-02 18:27:01 -07002036 cm = c->original_match;
2037 counter_cm = c->reply_match;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002038
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002039 /*
Ken Zhudc423672021-09-02 18:27:01 -07002040 * Didn't receive packets in the original direction or reply
2041 * direction, move to the next connection.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002042 */
Ken Zhudc423672021-09-02 18:27:01 -07002043 if ((!atomic_read(&cm->rx_packet_count)) && !(atomic_read(&counter_cm->rx_packet_count))) {
2044 c = c->all_connections_next;
2045 continue;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002046 }
2047
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302048 sfe_ipv4_gen_sync_connection(si, c, &sis, SFE_SYNC_REASON_STATS, now_jiffies);
Ken Zhu7a43d882022-01-04 10:51:44 -08002049 sfe_ipv4_stats_convert(conn_sync, &sis);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002050
Ken Zhu7a43d882022-01-04 10:51:44 -08002051 quota--;
2052 conn_sync++;
2053 c = c->all_connections_next;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002054 }
2055
Ken Zhudc423672021-09-02 18:27:01 -07002056 /*
2057 * At the end of the sync, put the wc_next to the connection we left.
2058 */
2059 si->wc_next = c;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002060 spin_unlock_bh(&si->lock);
2061
Ken Zhu7a43d882022-01-04 10:51:44 -08002062 count = sfe_ipv4_sync_max_number - quota;
2063 /*
2064 * Tell ecm sync round done if at the end of all connection
2065 * otherwise tell the number in the msg.
2066 */
2067 if (c == NULL) {
2068 DEBUG_INFO("Synced all connections.\n");
2069 sfe_ipv4_sync_many_msg->msg.conn_stats_many.next = 0;
2070 } else {
2071 DEBUG_INFO("Some connections left.\n");
2072 sfe_ipv4_sync_many_msg->msg.conn_stats_many.next = count;
2073 }
2074 DEBUG_INFO("Sync %d connections\n", count);
2075 sfe_ipv4_sync_many_msg->msg.conn_stats_many.count = count;
2076 sfe_ipv4_sync_many_msg->cm.response = SFE_CMN_RESPONSE_ACK;
2077
2078 sync_rule_callback(sfe_ipv4_sync_many_msg);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002079}
2080
2081#define CHAR_DEV_MSG_SIZE 768
2082
2083/*
2084 * sfe_ipv4_debug_dev_read_start()
2085 * Generate part of the XML output.
2086 */
2087static bool sfe_ipv4_debug_dev_read_start(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
Ken Zhu7a43d882022-01-04 10:51:44 -08002088 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002089{
2090 int bytes_read;
2091
Xiaoping Fan34586472015-07-03 02:20:35 -07002092 si->debug_read_seq++;
2093
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002094 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "<sfe_ipv4>\n");
2095 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2096 return false;
2097 }
2098
2099 *length -= bytes_read;
2100 *total_read += bytes_read;
2101
2102 ws->state++;
2103 return true;
2104}
2105
2106/*
2107 * sfe_ipv4_debug_dev_read_connections_start()
2108 * Generate part of the XML output.
2109 */
2110static bool sfe_ipv4_debug_dev_read_connections_start(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2111 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2112{
2113 int bytes_read;
2114
2115 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<connections>\n");
2116 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2117 return false;
2118 }
2119
2120 *length -= bytes_read;
2121 *total_read += bytes_read;
2122
2123 ws->state++;
2124 return true;
2125}
2126
2127/*
2128 * sfe_ipv4_debug_dev_read_connections_connection()
2129 * Generate part of the XML output.
2130 */
2131static bool sfe_ipv4_debug_dev_read_connections_connection(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2132 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2133{
2134 struct sfe_ipv4_connection *c;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002135 struct sfe_ipv4_connection_match *original_cm;
2136 struct sfe_ipv4_connection_match *reply_cm;
2137 int bytes_read;
2138 int protocol;
2139 struct net_device *src_dev;
Dave Hudson87973cd2013-10-22 16:00:04 +01002140 __be32 src_ip;
2141 __be32 src_ip_xlate;
2142 __be16 src_port;
2143 __be16 src_port_xlate;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07002144 u64 src_rx_packets;
2145 u64 src_rx_bytes;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002146 struct net_device *dest_dev;
Dave Hudson87973cd2013-10-22 16:00:04 +01002147 __be32 dest_ip;
2148 __be32 dest_ip_xlate;
2149 __be16 dest_port;
2150 __be16 dest_port_xlate;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07002151 u64 dest_rx_packets;
2152 u64 dest_rx_bytes;
2153 u64 last_sync_jiffies;
Ken Zhu37040ea2021-09-09 21:11:15 -07002154 u32 src_mark, dest_mark, src_priority, dest_priority, src_dscp, dest_dscp;
Parikshit Guned31a8202022-01-05 22:15:04 +05302155 bool original_cm_sawf_valid, reply_cm_sawf_valid;
2156 u32 flow_service_class, return_service_class;
2157 u32 flow_msduq, return_msduq;
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05302158 u32 packet, byte, original_cm_flags;
2159 u16 pppoe_session_id;
2160 u8 pppoe_remote_mac[ETH_ALEN];
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002161 u32 original_fast_xmit, reply_fast_xmit;
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08002162#ifdef CONFIG_NF_FLOW_COOKIE
2163 int src_flow_cookie, dst_flow_cookie;
2164#endif
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002165
2166 spin_lock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07002167
2168 for (c = si->all_connections_head; c; c = c->all_connections_next) {
2169 if (c->debug_read_seq < si->debug_read_seq) {
2170 c->debug_read_seq = si->debug_read_seq;
2171 break;
2172 }
2173 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002174
2175 /*
Xiaoping Fan34586472015-07-03 02:20:35 -07002176 * If there were no connections then move to the next state.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002177 */
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05302178 if (!c || c->removed) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002179 spin_unlock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07002180 ws->state++;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002181 return true;
2182 }
2183
2184 original_cm = c->original_match;
2185 reply_cm = c->reply_match;
2186
2187 protocol = c->protocol;
2188 src_dev = c->original_dev;
2189 src_ip = c->src_ip;
2190 src_ip_xlate = c->src_ip_xlate;
2191 src_port = c->src_port;
2192 src_port_xlate = c->src_port_xlate;
Xiaoping Fane1963d42015-08-25 17:06:19 -07002193 src_priority = original_cm->priority;
2194 src_dscp = original_cm->dscp >> SFE_IPV4_DSCP_SHIFT;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002195
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05302196 sfe_ipv4_connection_match_update_summary_stats(original_cm, &packet, &byte);
2197 sfe_ipv4_connection_match_update_summary_stats(reply_cm, &packet, &byte);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002198
2199 src_rx_packets = original_cm->rx_packet_count64;
2200 src_rx_bytes = original_cm->rx_byte_count64;
Ken Zhu37040ea2021-09-09 21:11:15 -07002201 src_mark = original_cm->mark;
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002202 original_fast_xmit = (original_cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_FAST_XMIT);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002203 dest_dev = c->reply_dev;
2204 dest_ip = c->dest_ip;
2205 dest_ip_xlate = c->dest_ip_xlate;
2206 dest_port = c->dest_port;
2207 dest_port_xlate = c->dest_port_xlate;
Xiaoping Fane1963d42015-08-25 17:06:19 -07002208 dest_priority = reply_cm->priority;
2209 dest_dscp = reply_cm->dscp >> SFE_IPV4_DSCP_SHIFT;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002210 dest_rx_packets = reply_cm->rx_packet_count64;
2211 dest_rx_bytes = reply_cm->rx_byte_count64;
Ken Zhu37040ea2021-09-09 21:11:15 -07002212 dest_mark = reply_cm->mark;
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002213 reply_fast_xmit = (reply_cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_FAST_XMIT);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002214 last_sync_jiffies = get_jiffies_64() - c->last_sync_jiffies;
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05302215 original_cm_flags = original_cm->flags;
2216 pppoe_session_id = original_cm->pppoe_session_id;
2217 ether_addr_copy(pppoe_remote_mac, original_cm->pppoe_remote_mac);
Parikshit Guned31a8202022-01-05 22:15:04 +05302218 original_cm_sawf_valid = original_cm->sawf_valid;
2219 reply_cm_sawf_valid = reply_cm->sawf_valid;
2220 flow_service_class = SFE_GET_SAWF_SERVICE_CLASS(original_cm->mark);
2221 flow_msduq = SFE_GET_SAWF_MSDUQ(original_cm->mark);
2222 return_service_class = SFE_GET_SAWF_SERVICE_CLASS(reply_cm->mark);
2223 return_msduq = SFE_GET_SAWF_MSDUQ(reply_cm->mark);
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08002224#ifdef CONFIG_NF_FLOW_COOKIE
2225 src_flow_cookie = original_cm->flow_cookie;
2226 dst_flow_cookie = reply_cm->flow_cookie;
2227#endif
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002228 spin_unlock_bh(&si->lock);
2229
2230 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t\t<connection "
2231 "protocol=\"%u\" "
2232 "src_dev=\"%s\" "
2233 "src_ip=\"%pI4\" src_ip_xlate=\"%pI4\" "
2234 "src_port=\"%u\" src_port_xlate=\"%u\" "
Xiaoping Fane1963d42015-08-25 17:06:19 -07002235 "src_priority=\"%u\" src_dscp=\"%u\" "
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002236 "src_rx_pkts=\"%llu\" src_rx_bytes=\"%llu\" "
Ken Zhu37040ea2021-09-09 21:11:15 -07002237 "src_mark=\"%08x\" "
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002238 "src_fast_xmit=\"%s\" "
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002239 "dest_dev=\"%s\" "
2240 "dest_ip=\"%pI4\" dest_ip_xlate=\"%pI4\" "
2241 "dest_port=\"%u\" dest_port_xlate=\"%u\" "
Xiaoping Fane1963d42015-08-25 17:06:19 -07002242 "dest_priority=\"%u\" dest_dscp=\"%u\" "
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002243 "dest_rx_pkts=\"%llu\" dest_rx_bytes=\"%llu\" "
Ken Zhu37040ea2021-09-09 21:11:15 -07002244 "dest_mark=\"%08x\" "
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002245 "reply_fast_xmit=\"%s\" "
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08002246#ifdef CONFIG_NF_FLOW_COOKIE
2247 "src_flow_cookie=\"%d\" dst_flow_cookie=\"%d\" "
2248#endif
Ken Zhu37040ea2021-09-09 21:11:15 -07002249 "last_sync=\"%llu\" ",
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002250 protocol,
2251 src_dev->name,
2252 &src_ip, &src_ip_xlate,
Dave Hudson87973cd2013-10-22 16:00:04 +01002253 ntohs(src_port), ntohs(src_port_xlate),
Xiaoping Fane1963d42015-08-25 17:06:19 -07002254 src_priority, src_dscp,
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002255 src_rx_packets, src_rx_bytes,
Ken Zhu37040ea2021-09-09 21:11:15 -07002256 src_mark,
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002257 original_fast_xmit ? "Yes" : "No",
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002258 dest_dev->name,
2259 &dest_ip, &dest_ip_xlate,
Dave Hudson87973cd2013-10-22 16:00:04 +01002260 ntohs(dest_port), ntohs(dest_port_xlate),
Xiaoping Fane1963d42015-08-25 17:06:19 -07002261 dest_priority, dest_dscp,
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002262 dest_rx_packets, dest_rx_bytes,
Ken Zhu37040ea2021-09-09 21:11:15 -07002263 dest_mark,
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002264 reply_fast_xmit ? "Yes" : "No",
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08002265#ifdef CONFIG_NF_FLOW_COOKIE
2266 src_flow_cookie, dst_flow_cookie,
2267#endif
Ken Zhu37040ea2021-09-09 21:11:15 -07002268 last_sync_jiffies);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002269
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05302270 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 +05302271 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 +05302272 pppoe_session_id, pppoe_remote_mac);
2273 }
2274
Parikshit Guned31a8202022-01-05 22:15:04 +05302275 if (original_cm_sawf_valid) {
Parikshit Gunefdd98652022-03-14 17:33:01 +05302276 bytes_read += snprintf(msg + bytes_read, CHAR_DEV_MSG_SIZE, "flow_service_class=\"%d\" flow_msduq = \"0x%x\" ",
Parikshit Guned31a8202022-01-05 22:15:04 +05302277 flow_service_class, flow_msduq);
2278 }
2279
2280 if (reply_cm_sawf_valid) {
Parikshit Gunefdd98652022-03-14 17:33:01 +05302281 bytes_read += snprintf(msg + bytes_read, CHAR_DEV_MSG_SIZE, "return_service_class=\"%d\" return_msduq = \"0x%x\" ",
Parikshit Guned31a8202022-01-05 22:15:04 +05302282 return_service_class, return_msduq);
2283 }
2284
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05302285 bytes_read += snprintf(msg + bytes_read, CHAR_DEV_MSG_SIZE, "/>\n");
2286
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002287 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2288 return false;
2289 }
2290
2291 *length -= bytes_read;
2292 *total_read += bytes_read;
2293
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002294 return true;
2295}
2296
2297/*
2298 * sfe_ipv4_debug_dev_read_connections_end()
2299 * Generate part of the XML output.
2300 */
2301static bool sfe_ipv4_debug_dev_read_connections_end(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2302 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2303{
2304 int bytes_read;
2305
2306 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t</connections>\n");
2307 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2308 return false;
2309 }
2310
2311 *length -= bytes_read;
2312 *total_read += bytes_read;
2313
2314 ws->state++;
2315 return true;
2316}
2317
2318/*
2319 * sfe_ipv4_debug_dev_read_exceptions_start()
2320 * Generate part of the XML output.
2321 */
2322static bool sfe_ipv4_debug_dev_read_exceptions_start(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2323 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2324{
2325 int bytes_read;
2326
2327 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<exceptions>\n");
2328 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2329 return false;
2330 }
2331
2332 *length -= bytes_read;
2333 *total_read += bytes_read;
2334
2335 ws->state++;
2336 return true;
2337}
2338
2339/*
2340 * sfe_ipv4_debug_dev_read_exceptions_exception()
2341 * Generate part of the XML output.
2342 */
2343static bool sfe_ipv4_debug_dev_read_exceptions_exception(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2344 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2345{
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302346 int i;
2347 u64 val = 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002348
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302349 for_each_possible_cpu(i) {
2350 const struct sfe_ipv4_stats *s = per_cpu_ptr(si->stats_pcpu, i);
2351 val += s->exception_events64[ws->iter_exception];
2352 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002353
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302354 if (val) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002355 int bytes_read;
2356
2357 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE,
2358 "\t\t<exception name=\"%s\" count=\"%llu\" />\n",
2359 sfe_ipv4_exception_events_string[ws->iter_exception],
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302360 val);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002361 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2362 return false;
2363 }
2364
2365 *length -= bytes_read;
2366 *total_read += bytes_read;
2367 }
2368
2369 ws->iter_exception++;
2370 if (ws->iter_exception >= SFE_IPV4_EXCEPTION_EVENT_LAST) {
2371 ws->iter_exception = 0;
2372 ws->state++;
2373 }
2374
2375 return true;
2376}
2377
2378/*
2379 * sfe_ipv4_debug_dev_read_exceptions_end()
2380 * Generate part of the XML output.
2381 */
2382static bool sfe_ipv4_debug_dev_read_exceptions_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, "\t</exceptions>\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 * sfe_ipv4_debug_dev_read_stats()
2401 * Generate part of the XML output.
2402 */
2403static bool sfe_ipv4_debug_dev_read_stats(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2404 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2405{
2406 int bytes_read;
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302407 struct sfe_ipv4_stats stats;
2408 unsigned int num_conn;
2409
2410 sfe_ipv4_update_summary_stats(si, &stats);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002411
2412 spin_lock_bh(&si->lock);
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302413 num_conn = si->num_connections;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002414 spin_unlock_bh(&si->lock);
2415
2416 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<stats "
2417 "num_connections=\"%u\" "
Amitesh Anand63be37d2021-12-24 20:51:48 +05302418 "pkts_dropped=\"%llu\" "
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002419 "pkts_fast_xmited=\"%llu\" "
Xiaoping Fan59176422015-05-22 15:58:10 -07002420 "pkts_forwarded=\"%llu\" pkts_not_forwarded=\"%llu\" "
2421 "create_requests=\"%llu\" create_collisions=\"%llu\" "
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05302422 "create_failures=\"%llu\" "
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002423 "destroy_requests=\"%llu\" destroy_misses=\"%llu\" "
2424 "flushes=\"%llu\" "
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +05302425 "hash_hits=\"%llu\" hash_reorders=\"%llu\" "
2426 "pppoe_encap_pkts_fwded=\"%llu\" "
Guduri Prathyusha034d6352022-01-12 16:49:04 +05302427 "pppoe_decap_pkts_fwded=\"%llu\" "
2428 "pppoe_bridge_pkts_fwded=\"%llu\" />\n",
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302429 num_conn,
Amitesh Anand63be37d2021-12-24 20:51:48 +05302430 stats.packets_dropped64,
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002431 stats.packets_fast_xmited64,
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302432 stats.packets_forwarded64,
2433 stats.packets_not_forwarded64,
2434 stats.connection_create_requests64,
2435 stats.connection_create_collisions64,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05302436 stats.connection_create_failures64,
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302437 stats.connection_destroy_requests64,
2438 stats.connection_destroy_misses64,
2439 stats.connection_flushes64,
2440 stats.connection_match_hash_hits64,
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +05302441 stats.connection_match_hash_reorders64,
2442 stats.pppoe_encap_packets_forwarded64,
Guduri Prathyusha034d6352022-01-12 16:49:04 +05302443 stats.pppoe_decap_packets_forwarded64,
2444 stats.pppoe_bridge_packets_forwarded64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002445 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2446 return false;
2447 }
2448
2449 *length -= bytes_read;
2450 *total_read += bytes_read;
2451
2452 ws->state++;
2453 return true;
2454}
2455
2456/*
2457 * sfe_ipv4_debug_dev_read_end()
2458 * Generate part of the XML output.
2459 */
2460static bool sfe_ipv4_debug_dev_read_end(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2461 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2462{
2463 int bytes_read;
2464
2465 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "</sfe_ipv4>\n");
2466 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2467 return false;
2468 }
2469
2470 *length -= bytes_read;
2471 *total_read += bytes_read;
2472
2473 ws->state++;
2474 return true;
2475}
2476
2477/*
2478 * Array of write functions that write various XML elements that correspond to
2479 * our XML output state machine.
2480 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07002481static 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 +01002482 sfe_ipv4_debug_dev_read_start,
2483 sfe_ipv4_debug_dev_read_connections_start,
2484 sfe_ipv4_debug_dev_read_connections_connection,
2485 sfe_ipv4_debug_dev_read_connections_end,
2486 sfe_ipv4_debug_dev_read_exceptions_start,
2487 sfe_ipv4_debug_dev_read_exceptions_exception,
2488 sfe_ipv4_debug_dev_read_exceptions_end,
2489 sfe_ipv4_debug_dev_read_stats,
2490 sfe_ipv4_debug_dev_read_end,
2491};
2492
2493/*
2494 * sfe_ipv4_debug_dev_read()
2495 * Send info to userspace upon read request from user
2496 */
2497static ssize_t sfe_ipv4_debug_dev_read(struct file *filp, char *buffer, size_t length, loff_t *offset)
2498{
2499 char msg[CHAR_DEV_MSG_SIZE];
2500 int total_read = 0;
2501 struct sfe_ipv4_debug_xml_write_state *ws;
2502 struct sfe_ipv4 *si = &__si;
2503
2504 ws = (struct sfe_ipv4_debug_xml_write_state *)filp->private_data;
2505 while ((ws->state != SFE_IPV4_DEBUG_XML_STATE_DONE) && (length > CHAR_DEV_MSG_SIZE)) {
2506 if ((sfe_ipv4_debug_xml_write_methods[ws->state])(si, buffer, msg, &length, &total_read, ws)) {
2507 continue;
2508 }
2509 }
2510
2511 return total_read;
2512}
2513
2514/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002515 * sfe_ipv4_debug_dev_open()
2516 */
2517static int sfe_ipv4_debug_dev_open(struct inode *inode, struct file *file)
2518{
2519 struct sfe_ipv4_debug_xml_write_state *ws;
2520
2521 ws = (struct sfe_ipv4_debug_xml_write_state *)file->private_data;
2522 if (!ws) {
2523 ws = kzalloc(sizeof(struct sfe_ipv4_debug_xml_write_state), GFP_KERNEL);
2524 if (!ws) {
2525 return -ENOMEM;
2526 }
2527
2528 ws->state = SFE_IPV4_DEBUG_XML_STATE_START;
2529 file->private_data = ws;
2530 }
2531
2532 return 0;
2533}
2534
2535/*
2536 * sfe_ipv4_debug_dev_release()
2537 */
2538static int sfe_ipv4_debug_dev_release(struct inode *inode, struct file *file)
2539{
2540 struct sfe_ipv4_debug_xml_write_state *ws;
2541
2542 ws = (struct sfe_ipv4_debug_xml_write_state *)file->private_data;
2543 if (ws) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002544 /*
2545 * We've finished with our output so free the write state.
2546 */
2547 kfree(ws);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05302548 file->private_data = NULL;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002549 }
2550
2551 return 0;
2552}
2553
2554/*
2555 * File operations used in the debug char device
2556 */
2557static struct file_operations sfe_ipv4_debug_dev_fops = {
2558 .read = sfe_ipv4_debug_dev_read,
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002559 .open = sfe_ipv4_debug_dev_open,
2560 .release = sfe_ipv4_debug_dev_release
2561};
2562
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08002563#ifdef CONFIG_NF_FLOW_COOKIE
2564/*
2565 * sfe_register_flow_cookie_cb
2566 * register a function in SFE to let SFE use this function to configure flow cookie for a flow
2567 *
2568 * Hardware driver which support flow cookie should register a callback function in SFE. Then SFE
2569 * can use this function to configure flow cookie for a flow.
2570 * return: 0, success; !=0, fail
2571 */
2572int sfe_register_flow_cookie_cb(flow_cookie_set_func_t cb)
2573{
2574 struct sfe_ipv4 *si = &__si;
2575
2576 BUG_ON(!cb);
2577
2578 if (si->flow_cookie_set_func) {
2579 return -1;
2580 }
2581
2582 rcu_assign_pointer(si->flow_cookie_set_func, cb);
2583 return 0;
2584}
2585
2586/*
2587 * sfe_unregister_flow_cookie_cb
2588 * unregister function which is used to configure flow cookie for a flow
2589 *
2590 * return: 0, success; !=0, fail
2591 */
2592int sfe_unregister_flow_cookie_cb(flow_cookie_set_func_t cb)
2593{
2594 struct sfe_ipv4 *si = &__si;
2595
2596 RCU_INIT_POINTER(si->flow_cookie_set_func, NULL);
2597 return 0;
2598}
Xiaoping Fan640faf42015-08-28 15:50:55 -07002599
2600/*
2601 * sfe_ipv4_get_flow_cookie()
2602 */
2603static ssize_t sfe_ipv4_get_flow_cookie(struct device *dev,
2604 struct device_attribute *attr,
2605 char *buf)
2606{
2607 struct sfe_ipv4 *si = &__si;
Xiaoping Fan01c67cc2015-11-09 11:31:57 -08002608 return snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", si->flow_cookie_enable);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002609}
2610
2611/*
2612 * sfe_ipv4_set_flow_cookie()
2613 */
2614static ssize_t sfe_ipv4_set_flow_cookie(struct device *dev,
2615 struct device_attribute *attr,
2616 const char *buf, size_t size)
2617{
2618 struct sfe_ipv4 *si = &__si;
Ken Zhu137722d2021-09-23 17:57:36 -07002619 si->flow_cookie_enable = simple_strtol(buf, NULL, 0);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002620
2621 return size;
2622}
2623
2624/*
2625 * sysfs attributes.
2626 */
2627static const struct device_attribute sfe_ipv4_flow_cookie_attr =
Xiaoping Fane70da412016-02-26 16:47:57 -08002628 __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 -08002629#endif /*CONFIG_NF_FLOW_COOKIE*/
2630
Ken Zhu137722d2021-09-23 17:57:36 -07002631/*
2632 * sfe_ipv4_get_cpu()
2633 */
2634static ssize_t sfe_ipv4_get_cpu(struct device *dev,
2635 struct device_attribute *attr,
2636 char *buf)
2637{
2638 struct sfe_ipv4 *si = &__si;
2639 return snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", si->work_cpu);
2640}
2641
2642/*
2643 * sfe_ipv4_set_cpu()
2644 */
2645static ssize_t sfe_ipv4_set_cpu(struct device *dev,
2646 struct device_attribute *attr,
2647 const char *buf, size_t size)
2648{
2649 struct sfe_ipv4 *si = &__si;
2650 int work_cpu;
2651 work_cpu = simple_strtol(buf, NULL, 0);
2652 if ((work_cpu >= 0) && (work_cpu <= NR_CPUS)) {
2653 si->work_cpu = work_cpu;
2654 } else {
2655 dev_err(dev, "%s is not in valid range[0,%d]", buf, NR_CPUS);
2656 }
2657 return size;
2658}
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002659
Ken Zhu137722d2021-09-23 17:57:36 -07002660/*
2661 * sysfs attributes.
2662 */
2663static const struct device_attribute sfe_ipv4_cpu_attr =
2664 __ATTR(stats_work_cpu, S_IWUSR | S_IRUGO, sfe_ipv4_get_cpu, sfe_ipv4_set_cpu);
2665
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002666/*
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05302667 * sfe_ipv4_conn_match_hash_init()
2668 * Initialize conn match hash lists
2669 */
2670static void sfe_ipv4_conn_match_hash_init(struct sfe_ipv4 *si, int len)
2671{
2672 struct hlist_head *hash_list = si->hlist_conn_match_hash_head;
2673 int i;
2674
2675 for (i = 0; i < len; i++) {
2676 INIT_HLIST_HEAD(&hash_list[i]);
2677 }
2678}
2679
Amitesh Anand63be37d2021-12-24 20:51:48 +05302680#ifdef SFE_PROCESS_LOCAL_OUT
2681/*
2682 * sfe_ipv4_local_out()
2683 * Called for packets from ip_local_out() - post encapsulation & other packets
2684 */
2685static unsigned int sfe_ipv4_local_out(void *priv, struct sk_buff *skb, const struct nf_hook_state *nhs)
2686{
Nitin Shettyc28f8172022-02-04 16:23:46 +05302687 struct sfe_l2_info l2_info = {0};
Guduri Prathyusha5f27e232022-01-06 14:39:04 +05302688
Amitesh Anand63be37d2021-12-24 20:51:48 +05302689 DEBUG_TRACE("%px: sfe: sfe_ipv4_local_out hook called.\n", skb);
2690
2691 if (likely(skb->skb_iif)) {
Guduri Prathyusha5f27e232022-01-06 14:39:04 +05302692 return sfe_ipv4_recv(skb->dev, skb, &l2_info, true) ? NF_STOLEN : NF_ACCEPT;
Amitesh Anand63be37d2021-12-24 20:51:48 +05302693 }
2694
2695 return NF_ACCEPT;
2696}
2697
2698/*
2699 * struct nf_hook_ops sfe_ipv4_ops_local_out[]
2700 * Hooks into netfilter local out packet monitoring points.
2701 */
2702static struct nf_hook_ops sfe_ipv4_ops_local_out[] __read_mostly = {
2703
2704 /*
2705 * Local out routing hook is used to monitor packets.
2706 */
2707 {
2708 .hook = sfe_ipv4_local_out,
2709 .pf = PF_INET,
2710 .hooknum = NF_INET_LOCAL_OUT,
2711 .priority = NF_IP_PRI_FIRST,
2712 },
2713};
2714#endif
2715
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002716/*
Dave Hudson87973cd2013-10-22 16:00:04 +01002717 * sfe_ipv4_init()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002718 */
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05302719int sfe_ipv4_init(void)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002720{
2721 struct sfe_ipv4 *si = &__si;
2722 int result = -1;
2723
Dave Hudsondcd08fb2013-11-22 09:25:16 -06002724 DEBUG_INFO("SFE IPv4 init\n");
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002725
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05302726 sfe_ipv4_conn_match_hash_init(si, ARRAY_SIZE(si->hlist_conn_match_hash_head));
2727
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302728 si->stats_pcpu = alloc_percpu_gfp(struct sfe_ipv4_stats, GFP_KERNEL | __GFP_ZERO);
2729 if (!si->stats_pcpu) {
2730 DEBUG_ERROR("failed to allocate stats memory for sfe_ipv4\n");
2731 goto exit0;
2732 }
2733
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002734 /*
Parikshit Guned31a8202022-01-05 22:15:04 +05302735 * Allocate per cpu per service class memory.
2736 */
2737 si->stats_pcpu_psc = alloc_percpu_gfp(struct sfe_ipv4_service_class_stats_db,
2738 GFP_KERNEL | __GFP_ZERO);
2739 if (!si->stats_pcpu_psc) {
2740 DEBUG_ERROR("failed to allocate per cpu per service clas stats memory\n");
2741 goto exit1;
2742 }
2743
2744 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002745 * Create sys/sfe_ipv4
2746 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302747 si->sys_ipv4 = kobject_create_and_add("sfe_ipv4", NULL);
2748 if (!si->sys_ipv4) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002749 DEBUG_ERROR("failed to register sfe_ipv4\n");
Parikshit Guned31a8202022-01-05 22:15:04 +05302750 goto exit2;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002751 }
2752
2753 /*
2754 * Create files, one for each parameter supported by this module.
2755 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302756 result = sysfs_create_file(si->sys_ipv4, &sfe_ipv4_debug_dev_attr.attr);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002757 if (result) {
2758 DEBUG_ERROR("failed to register debug dev file: %d\n", result);
Parikshit Guned31a8202022-01-05 22:15:04 +05302759 goto exit3;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002760 }
2761
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302762 result = sysfs_create_file(si->sys_ipv4, &sfe_ipv4_cpu_attr.attr);
Ken Zhu137722d2021-09-23 17:57:36 -07002763 if (result) {
2764 DEBUG_ERROR("failed to register debug dev file: %d\n", result);
Parikshit Guned31a8202022-01-05 22:15:04 +05302765 goto exit4;
Ken Zhu137722d2021-09-23 17:57:36 -07002766 }
2767
Xiaoping Fan640faf42015-08-28 15:50:55 -07002768#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302769 result = sysfs_create_file(si->sys_ipv4, &sfe_ipv4_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002770 if (result) {
2771 DEBUG_ERROR("failed to register flow cookie enable file: %d\n", result);
Parikshit Guned31a8202022-01-05 22:15:04 +05302772 goto exit5;
Xiaoping Fan640faf42015-08-28 15:50:55 -07002773 }
2774#endif /* CONFIG_NF_FLOW_COOKIE */
2775
Amitesh Anand63be37d2021-12-24 20:51:48 +05302776#ifdef SFE_PROCESS_LOCAL_OUT
2777#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
2778 result = nf_register_hooks(sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2779#else
2780 result = nf_register_net_hooks(&init_net, sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2781#endif
2782 if (result < 0) {
2783 DEBUG_ERROR("can't register nf local out hook: %d\n", result);
Parikshit Guned31a8202022-01-05 22:15:04 +05302784 goto exit6;
Amitesh Anand63be37d2021-12-24 20:51:48 +05302785 }
2786 DEBUG_INFO("Register nf local out hook success: %d\n", result);
2787#endif
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002788 /*
2789 * Register our debug char device.
2790 */
2791 result = register_chrdev(0, "sfe_ipv4", &sfe_ipv4_debug_dev_fops);
2792 if (result < 0) {
2793 DEBUG_ERROR("Failed to register chrdev: %d\n", result);
Parikshit Guned31a8202022-01-05 22:15:04 +05302794 goto exit7;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002795 }
2796
2797 si->debug_dev = result;
Ken Zhu137722d2021-09-23 17:57:36 -07002798 si->work_cpu = WORK_CPU_UNBOUND;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002799
2800 /*
Ken Zhu7a43d882022-01-04 10:51:44 -08002801 * Create a work to handle pull message from ecm.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002802 */
Ken Zhu137722d2021-09-23 17:57:36 -07002803 INIT_DELAYED_WORK(&(si->sync_dwork), sfe_ipv4_periodic_sync);
Ken Zhu7a43d882022-01-04 10:51:44 -08002804 /*
2805 * Allocate a message for stats sync many
2806 */
2807 sfe_ipv4_sync_many_msg = kzalloc(PAGE_SIZE, GFP_KERNEL);
2808 if(!sfe_ipv4_sync_many_msg) {
2809 goto exit8;
2810 }
2811
2812 sfe_ipv4_msg_init(sfe_ipv4_sync_many_msg, SFE_SPECIAL_INTERFACE_IPV4,
2813 SFE_TX_CONN_STATS_SYNC_MANY_MSG,
2814 sizeof(struct sfe_ipv4_conn_sync_many_msg),
2815 NULL,
2816 NULL);
2817 sfe_ipv4_sync_max_number = (PAGE_SIZE - sizeof(struct sfe_ipv4_msg)) / sizeof(struct sfe_ipv4_conn_sync);
Ken Zhu137722d2021-09-23 17:57:36 -07002818
Dave Hudson87973cd2013-10-22 16:00:04 +01002819 spin_lock_init(&si->lock);
Dave Hudson87973cd2013-10-22 16:00:04 +01002820 return 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002821
Ken Zhu7a43d882022-01-04 10:51:44 -08002822exit8:
2823 unregister_chrdev(si->debug_dev, "sfe_ipv4");
2824
Parikshit Guned31a8202022-01-05 22:15:04 +05302825exit7:
Amitesh Anand63be37d2021-12-24 20:51:48 +05302826#ifdef SFE_PROCESS_LOCAL_OUT
2827 DEBUG_TRACE("sfe: Unregister local out hook\n");
2828#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
2829 nf_unregister_hooks(sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2830#else
2831 nf_unregister_net_hooks(&init_net, sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2832#endif
Parikshit Guned31a8202022-01-05 22:15:04 +05302833exit6:
Amitesh Anand63be37d2021-12-24 20:51:48 +05302834#endif
Xiaoping Fan640faf42015-08-28 15:50:55 -07002835#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302836 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002837
Parikshit Guned31a8202022-01-05 22:15:04 +05302838exit5:
Xiaoping Fan640faf42015-08-28 15:50:55 -07002839#endif /* CONFIG_NF_FLOW_COOKIE */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302840 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_cpu_attr.attr);
Parikshit Guned31a8202022-01-05 22:15:04 +05302841exit4:
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302842 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_debug_dev_attr.attr);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002843
Parikshit Guned31a8202022-01-05 22:15:04 +05302844exit3:
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302845 kobject_put(si->sys_ipv4);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002846
Parikshit Guned31a8202022-01-05 22:15:04 +05302847exit2:
2848 free_percpu(si->stats_pcpu_psc);
2849
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002850exit1:
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302851 free_percpu(si->stats_pcpu);
2852
2853exit0:
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002854 return result;
2855}
2856
2857/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002858 * sfe_ipv4_exit()
2859 */
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05302860void sfe_ipv4_exit(void)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002861{
Dave Hudson87973cd2013-10-22 16:00:04 +01002862 struct sfe_ipv4 *si = &__si;
2863
Dave Hudsondcd08fb2013-11-22 09:25:16 -06002864 DEBUG_INFO("SFE IPv4 exit\n");
Dave Hudson87973cd2013-10-22 16:00:04 +01002865 /*
2866 * Destroy all connections.
2867 */
Dave Hudsondcd08fb2013-11-22 09:25:16 -06002868 sfe_ipv4_destroy_all_rules_for_dev(NULL);
Dave Hudson87973cd2013-10-22 16:00:04 +01002869
Ken Zhu137722d2021-09-23 17:57:36 -07002870 cancel_delayed_work_sync(&si->sync_dwork);
Dave Hudson87973cd2013-10-22 16:00:04 +01002871
Dave Hudson87973cd2013-10-22 16:00:04 +01002872 unregister_chrdev(si->debug_dev, "sfe_ipv4");
2873
Amitesh Anand63be37d2021-12-24 20:51:48 +05302874#ifdef SFE_PROCESS_LOCAL_OUT
2875 DEBUG_TRACE("sfe: Unregister local out hook\n");
2876#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
2877 nf_unregister_hooks(sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2878#else
2879 nf_unregister_net_hooks(&init_net, sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2880#endif
2881#endif
2882
Xiaoping Fan640faf42015-08-28 15:50:55 -07002883#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302884 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002885#endif /* CONFIG_NF_FLOW_COOKIE */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302886 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_debug_dev_attr.attr);
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002887
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302888 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_cpu_attr.attr);
Dave Hudson87973cd2013-10-22 16:00:04 +01002889
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302890 kobject_put(si->sys_ipv4);
Dave Hudson87973cd2013-10-22 16:00:04 +01002891
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302892 free_percpu(si->stats_pcpu);
Parikshit Guned31a8202022-01-05 22:15:04 +05302893 free_percpu(si->stats_pcpu_psc);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002894}
2895
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08002896#ifdef CONFIG_NF_FLOW_COOKIE
2897EXPORT_SYMBOL(sfe_register_flow_cookie_cb);
2898EXPORT_SYMBOL(sfe_unregister_flow_cookie_cb);
2899#endif