blob: ce6f1f98cb87f3b995d6a19dfaad33ee5fa55614 [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>
Nitin Shettye6ed5b52021-12-27 14:50:11 +053035#include <net/protocol.h>
36#include <net/gre.h>
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010037
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +053038#include "sfe_debug.h"
Ratheesh Kannoth89302a72021-10-20 08:10:37 +053039#include "sfe_api.h"
Dave Hudsondcd08fb2013-11-22 09:25:16 -060040#include "sfe.h"
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +053041#include "sfe_flow_cookie.h"
42#include "sfe_ipv4.h"
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +053043#include "sfe_ipv4_udp.h"
44#include "sfe_ipv4_tcp.h"
45#include "sfe_ipv4_icmp.h"
Wayne Tanbb7f1782021-12-13 11:16:04 -080046#include "sfe_pppoe.h"
Nitin Shettye6ed5b52021-12-27 14:50:11 +053047#include "sfe_ipv4_gre.h"
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010048
49static char *sfe_ipv4_exception_events_string[SFE_IPV4_EXCEPTION_EVENT_LAST] = {
50 "UDP_HEADER_INCOMPLETE",
51 "UDP_NO_CONNECTION",
52 "UDP_IP_OPTIONS_OR_INITIAL_FRAGMENT",
53 "UDP_SMALL_TTL",
54 "UDP_NEEDS_FRAGMENTATION",
55 "TCP_HEADER_INCOMPLETE",
56 "TCP_NO_CONNECTION_SLOW_FLAGS",
57 "TCP_NO_CONNECTION_FAST_FLAGS",
58 "TCP_IP_OPTIONS_OR_INITIAL_FRAGMENT",
59 "TCP_SMALL_TTL",
60 "TCP_NEEDS_FRAGMENTATION",
61 "TCP_FLAGS",
62 "TCP_SEQ_EXCEEDS_RIGHT_EDGE",
63 "TCP_SMALL_DATA_OFFS",
64 "TCP_BAD_SACK",
65 "TCP_BIG_DATA_OFFS",
66 "TCP_SEQ_BEFORE_LEFT_EDGE",
67 "TCP_ACK_EXCEEDS_RIGHT_EDGE",
68 "TCP_ACK_BEFORE_LEFT_EDGE",
69 "ICMP_HEADER_INCOMPLETE",
70 "ICMP_UNHANDLED_TYPE",
71 "ICMP_IPV4_HEADER_INCOMPLETE",
72 "ICMP_IPV4_NON_V4",
73 "ICMP_IPV4_IP_OPTIONS_INCOMPLETE",
74 "ICMP_IPV4_UDP_HEADER_INCOMPLETE",
75 "ICMP_IPV4_TCP_HEADER_INCOMPLETE",
76 "ICMP_IPV4_UNHANDLED_PROTOCOL",
77 "ICMP_NO_CONNECTION",
78 "ICMP_FLUSHED_CONNECTION",
79 "HEADER_INCOMPLETE",
Ratheesh Kannoth43d64f82021-10-20 08:23:29 +053080 "HEADER_CSUM_BAD",
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010081 "BAD_TOTAL_LENGTH",
82 "NON_V4",
83 "NON_INITIAL_FRAGMENT",
84 "DATAGRAM_INCOMPLETE",
85 "IP_OPTIONS_INCOMPLETE",
Ratheesh Kannoth5dee3772022-01-18 11:27:14 +053086 "UNHANDLED_PROTOCOL",
87 "INVALID_SOURCE_INTERFACE",
Nitin Shettye6ed5b52021-12-27 14:50:11 +053088 "GRE_HEADER_INCOMPLETE",
89 "GRE_NO_CONNECTION",
90 "GRE_IP_OPTIONS_OR_INITIAL_FRAGMENT",
91 "GRE_SMALL_TTL",
92 "GRE_NEEDS_FRAGMENTATION"
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010093};
94
Xiaoping Fan6a1672f2016-08-17 19:58:12 -070095static struct sfe_ipv4 __si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010096
97/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010098 * sfe_ipv4_gen_ip_csum()
99 * Generate the IP checksum for an IPv4 header.
100 *
101 * Note that this function assumes that we have only 20 bytes of IP header.
102 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530103u16 sfe_ipv4_gen_ip_csum(struct iphdr *iph)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100104{
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700105 u32 sum;
106 u16 *i = (u16 *)iph;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100107
108 iph->check = 0;
109
110 /*
111 * Generate the sum.
112 */
113 sum = i[0] + i[1] + i[2] + i[3] + i[4] + i[5] + i[6] + i[7] + i[8] + i[9];
114
115 /*
116 * Fold it to ones-complement form.
117 */
118 sum = (sum & 0xffff) + (sum >> 16);
119 sum = (sum & 0xffff) + (sum >> 16);
120
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700121 return (u16)sum ^ 0xffff;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100122}
123
124/*
125 * sfe_ipv4_get_connection_match_hash()
126 * Generate the hash used in connection match lookups.
127 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700128static inline unsigned int sfe_ipv4_get_connection_match_hash(struct net_device *dev, u8 protocol,
Dave Hudson87973cd2013-10-22 16:00:04 +0100129 __be32 src_ip, __be16 src_port,
130 __be32 dest_ip, __be16 dest_port)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100131{
Ratheesh Kannoth5dee3772022-01-18 11:27:14 +0530132 u32 hash = ntohl(src_ip ^ dest_ip) ^ protocol ^ ntohs(src_port ^ dest_port);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100133 return ((hash >> SFE_IPV4_CONNECTION_HASH_SHIFT) ^ hash) & SFE_IPV4_CONNECTION_HASH_MASK;
134}
135
136/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530137 * sfe_ipv4_find_connection_match_rcu()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100138 * Get the IPv4 flow match info that corresponds to a particular 5-tuple.
139 *
140 * On entry we must be holding the lock that protects the hash table.
141 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530142struct sfe_ipv4_connection_match *
143sfe_ipv4_find_connection_match_rcu(struct sfe_ipv4 *si, struct net_device *dev, u8 protocol,
Dave Hudson87973cd2013-10-22 16:00:04 +0100144 __be32 src_ip, __be16 src_port,
145 __be32 dest_ip, __be16 dest_port)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100146{
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530147 struct sfe_ipv4_connection_match *cm = NULL;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100148 unsigned int conn_match_idx;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530149 struct hlist_head *lhead;
150
151 WARN_ON_ONCE(!rcu_read_lock_held());
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100152
153 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 +0100154
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530155 lhead = &si->hlist_conn_match_hash_head[conn_match_idx];
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100156
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530157 hlist_for_each_entry_rcu(cm, lhead, hnode) {
158 if (cm->match_src_port != src_port
159 || cm->match_dest_port != dest_port
160 || cm->match_src_ip != src_ip
161 || cm->match_dest_ip != dest_ip
Ratheesh Kannoth5dee3772022-01-18 11:27:14 +0530162 || cm->match_protocol != protocol) {
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530163 continue;
164 }
165
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530166 this_cpu_inc(si->stats_pcpu->connection_match_hash_hits64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100167
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530168 break;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100169 }
170
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100171 return cm;
172}
173
174/*
175 * sfe_ipv4_connection_match_update_summary_stats()
176 * Update the summary stats for a connection match entry.
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530177 *
178 * Stats are incremented atomically. So use atomic substraction to update summary
179 * stats.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100180 */
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530181static inline void sfe_ipv4_connection_match_update_summary_stats(struct sfe_ipv4_connection_match *cm,
182 u32 *packets, u32 *bytes)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100183{
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530184 u32 packet_count, byte_count;
185
186 packet_count = atomic_read(&cm->rx_packet_count);
187 cm->rx_packet_count64 += packet_count;
188 atomic_sub(packet_count, &cm->rx_packet_count);
189
190 byte_count = atomic_read(&cm->rx_byte_count);
191 cm->rx_byte_count64 += byte_count;
192 atomic_sub(byte_count, &cm->rx_byte_count);
193
194 *packets = packet_count;
195 *bytes = byte_count;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100196}
197
198/*
199 * sfe_ipv4_connection_match_compute_translations()
200 * Compute port and address translations for a connection match entry.
201 */
202static void sfe_ipv4_connection_match_compute_translations(struct sfe_ipv4_connection_match *cm)
203{
204 /*
205 * Before we insert the entry look to see if this is tagged as doing address
206 * translations. If it is then work out the adjustment that we need to apply
207 * to the transport checksum.
208 */
209 if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC) {
210 /*
211 * Precompute an incremental checksum adjustment so we can
212 * edit packets in this stream very quickly. The algorithm is from RFC1624.
213 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700214 u16 src_ip_hi = cm->match_src_ip >> 16;
215 u16 src_ip_lo = cm->match_src_ip & 0xffff;
216 u32 xlate_src_ip = ~cm->xlate_src_ip;
217 u16 xlate_src_ip_hi = xlate_src_ip >> 16;
218 u16 xlate_src_ip_lo = xlate_src_ip & 0xffff;
219 u16 xlate_src_port = ~cm->xlate_src_port;
220 u32 adj;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100221
222 /*
223 * When we compute this fold it down to a 16-bit offset
224 * as that way we can avoid having to do a double
225 * folding of the twos-complement result because the
226 * addition of 2 16-bit values cannot cause a double
227 * wrap-around!
228 */
229 adj = src_ip_hi + src_ip_lo + cm->match_src_port
230 + xlate_src_ip_hi + xlate_src_ip_lo + xlate_src_port;
231 adj = (adj & 0xffff) + (adj >> 16);
232 adj = (adj & 0xffff) + (adj >> 16);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700233 cm->xlate_src_csum_adjustment = (u16)adj;
Nicolas Costaac2979c2014-01-14 10:35:24 -0600234
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100235 }
236
237 if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST) {
238 /*
239 * Precompute an incremental checksum adjustment so we can
240 * edit packets in this stream very quickly. The algorithm is from RFC1624.
241 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700242 u16 dest_ip_hi = cm->match_dest_ip >> 16;
243 u16 dest_ip_lo = cm->match_dest_ip & 0xffff;
244 u32 xlate_dest_ip = ~cm->xlate_dest_ip;
245 u16 xlate_dest_ip_hi = xlate_dest_ip >> 16;
246 u16 xlate_dest_ip_lo = xlate_dest_ip & 0xffff;
247 u16 xlate_dest_port = ~cm->xlate_dest_port;
248 u32 adj;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100249
250 /*
251 * When we compute this fold it down to a 16-bit offset
252 * as that way we can avoid having to do a double
253 * folding of the twos-complement result because the
254 * addition of 2 16-bit values cannot cause a double
255 * wrap-around!
256 */
257 adj = dest_ip_hi + dest_ip_lo + cm->match_dest_port
258 + xlate_dest_ip_hi + xlate_dest_ip_lo + xlate_dest_port;
259 adj = (adj & 0xffff) + (adj >> 16);
260 adj = (adj & 0xffff) + (adj >> 16);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700261 cm->xlate_dest_csum_adjustment = (u16)adj;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100262 }
Xiaoping Fanad755af2015-04-01 16:58:46 -0700263
264 if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC) {
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700265 u32 adj = ~cm->match_src_ip + cm->xlate_src_ip;
Xiaoping Fanad755af2015-04-01 16:58:46 -0700266 if (adj < cm->xlate_src_ip) {
267 adj++;
268 }
269
270 adj = (adj & 0xffff) + (adj >> 16);
271 adj = (adj & 0xffff) + (adj >> 16);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700272 cm->xlate_src_partial_csum_adjustment = (u16)adj;
Xiaoping Fanad755af2015-04-01 16:58:46 -0700273 }
274
275 if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST) {
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700276 u32 adj = ~cm->match_dest_ip + cm->xlate_dest_ip;
Xiaoping Fanad755af2015-04-01 16:58:46 -0700277 if (adj < cm->xlate_dest_ip) {
278 adj++;
279 }
280
281 adj = (adj & 0xffff) + (adj >> 16);
282 adj = (adj & 0xffff) + (adj >> 16);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700283 cm->xlate_dest_partial_csum_adjustment = (u16)adj;
Xiaoping Fanad755af2015-04-01 16:58:46 -0700284 }
285
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100286}
287
288/*
289 * sfe_ipv4_update_summary_stats()
290 * Update the summary stats.
291 */
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530292static void sfe_ipv4_update_summary_stats(struct sfe_ipv4 *si, struct sfe_ipv4_stats *stats)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100293{
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530294 int i = 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100295
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530296 memset(stats, 0, sizeof(*stats));
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100297
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530298 for_each_possible_cpu(i) {
299 const struct sfe_ipv4_stats *s = per_cpu_ptr(si->stats_pcpu, i);
300
301 stats->connection_create_requests64 += s->connection_create_requests64;
302 stats->connection_create_collisions64 += s->connection_create_collisions64;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530303 stats->connection_create_failures64 += s->connection_create_failures64;
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530304 stats->connection_destroy_requests64 += s->connection_destroy_requests64;
305 stats->connection_destroy_misses64 += s->connection_destroy_misses64;
306 stats->connection_match_hash_hits64 += s->connection_match_hash_hits64;
307 stats->connection_match_hash_reorders64 += s->connection_match_hash_reorders64;
308 stats->connection_flushes64 += s->connection_flushes64;
Amitesh Anand63be37d2021-12-24 20:51:48 +0530309 stats->packets_dropped64 += s->packets_dropped64;
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530310 stats->packets_forwarded64 += s->packets_forwarded64;
311 stats->packets_not_forwarded64 += s->packets_not_forwarded64;
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +0530312 stats->pppoe_encap_packets_forwarded64 += s->pppoe_encap_packets_forwarded64;
313 stats->pppoe_decap_packets_forwarded64 += s->pppoe_decap_packets_forwarded64;
Guduri Prathyusha034d6352022-01-12 16:49:04 +0530314 stats->pppoe_bridge_packets_forwarded64 += s->pppoe_bridge_packets_forwarded64;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100315 }
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530316
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100317}
318
319/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530320 * sfe_ipv4_insert_connection_match()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100321 * Insert a connection match into the hash.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100322 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530323static inline void sfe_ipv4_insert_connection_match(struct sfe_ipv4 *si,
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700324 struct sfe_ipv4_connection_match *cm)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100325{
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100326 unsigned int conn_match_idx
327 = sfe_ipv4_get_connection_match_hash(cm->match_dev, cm->match_protocol,
328 cm->match_src_ip, cm->match_src_port,
329 cm->match_dest_ip, cm->match_dest_port);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700330
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530331 lockdep_assert_held(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100332
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530333 hlist_add_head_rcu(&cm->hnode, &si->hlist_conn_match_hash_head[conn_match_idx]);
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800334#ifdef CONFIG_NF_FLOW_COOKIE
Xiaoping Fan640faf42015-08-28 15:50:55 -0700335 if (!si->flow_cookie_enable)
336 return;
337
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800338 /*
339 * Configure hardware to put a flow cookie in packet of this flow,
340 * then we can accelerate the lookup process when we received this packet.
341 */
342 for (conn_match_idx = 1; conn_match_idx < SFE_FLOW_COOKIE_SIZE; conn_match_idx++) {
343 struct sfe_flow_cookie_entry *entry = &si->sfe_flow_cookie_table[conn_match_idx];
344
345 if ((NULL == entry->match) && time_is_before_jiffies(entry->last_clean_time + HZ)) {
346 flow_cookie_set_func_t func;
347
348 rcu_read_lock();
349 func = rcu_dereference(si->flow_cookie_set_func);
350 if (func) {
Xiaoping Fan59176422015-05-22 15:58:10 -0700351 if (!func(cm->match_protocol, cm->match_src_ip, cm->match_src_port,
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800352 cm->match_dest_ip, cm->match_dest_port, conn_match_idx)) {
353 entry->match = cm;
354 cm->flow_cookie = conn_match_idx;
355 }
356 }
357 rcu_read_unlock();
358
359 break;
360 }
361 }
362#endif
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100363}
364
365/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530366 * sfe_ipv4_remove_connection_match()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100367 * Remove a connection match object from the hash.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100368 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530369static inline void sfe_ipv4_remove_connection_match(struct sfe_ipv4 *si, struct sfe_ipv4_connection_match *cm)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100370{
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530371
372 lockdep_assert_held(&si->lock);
373
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800374#ifdef CONFIG_NF_FLOW_COOKIE
Xiaoping Fan640faf42015-08-28 15:50:55 -0700375 if (si->flow_cookie_enable) {
376 /*
377 * Tell hardware that we no longer need a flow cookie in packet of this flow
378 */
379 unsigned int conn_match_idx;
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800380
Xiaoping Fan640faf42015-08-28 15:50:55 -0700381 for (conn_match_idx = 1; conn_match_idx < SFE_FLOW_COOKIE_SIZE; conn_match_idx++) {
382 struct sfe_flow_cookie_entry *entry = &si->sfe_flow_cookie_table[conn_match_idx];
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800383
Xiaoping Fan640faf42015-08-28 15:50:55 -0700384 if (cm == entry->match) {
385 flow_cookie_set_func_t func;
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800386
Xiaoping Fan640faf42015-08-28 15:50:55 -0700387 rcu_read_lock();
388 func = rcu_dereference(si->flow_cookie_set_func);
389 if (func) {
390 func(cm->match_protocol, cm->match_src_ip, cm->match_src_port,
391 cm->match_dest_ip, cm->match_dest_port, 0);
392 }
393 rcu_read_unlock();
394
395 cm->flow_cookie = 0;
396 entry->match = NULL;
397 entry->last_clean_time = jiffies;
398 break;
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800399 }
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800400 }
401 }
402#endif
403
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530404 hlist_del_init_rcu(&cm->hnode);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100405
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100406}
407
408/*
409 * sfe_ipv4_get_connection_hash()
410 * Generate the hash used in connection lookups.
411 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700412static inline unsigned int sfe_ipv4_get_connection_hash(u8 protocol, __be32 src_ip, __be16 src_port,
Dave Hudson87973cd2013-10-22 16:00:04 +0100413 __be32 dest_ip, __be16 dest_port)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100414{
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700415 u32 hash = ntohl(src_ip ^ dest_ip) ^ protocol ^ ntohs(src_port ^ dest_port);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100416 return ((hash >> SFE_IPV4_CONNECTION_HASH_SHIFT) ^ hash) & SFE_IPV4_CONNECTION_HASH_MASK;
417}
418
419/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530420 * sfe_ipv4_find_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100421 * Get the IPv4 connection info that corresponds to a particular 5-tuple.
422 *
423 * On entry we must be holding the lock that protects the hash table.
424 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530425static inline struct sfe_ipv4_connection *sfe_ipv4_find_connection(struct sfe_ipv4 *si, u32 protocol,
Dave Hudson87973cd2013-10-22 16:00:04 +0100426 __be32 src_ip, __be16 src_port,
427 __be32 dest_ip, __be16 dest_port)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100428{
429 struct sfe_ipv4_connection *c;
430 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 +0530431
432 lockdep_assert_held(&si->lock);
433
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100434 c = si->conn_hash[conn_idx];
435
436 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100437 * Will need connection entry for next create/destroy metadata,
438 * So no need to re-order entry for these requests
439 */
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530440 while (c) {
441 if ((c->src_port == src_port)
442 && (c->dest_port == dest_port)
443 && (c->src_ip == src_ip)
444 && (c->dest_ip == dest_ip)
445 && (c->protocol == protocol)) {
446 return c;
447 }
448
449 c = c->next;
450 }
451
452 return NULL;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100453}
454
455/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530456 * sfe_ipv4_insert_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100457 * Insert a connection into the hash.
458 *
459 * On entry we must be holding the lock that protects the hash table.
460 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530461static void sfe_ipv4_insert_connection(struct sfe_ipv4 *si, struct sfe_ipv4_connection *c)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100462{
463 struct sfe_ipv4_connection **hash_head;
464 struct sfe_ipv4_connection *prev_head;
465 unsigned int conn_idx;
466
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530467 lockdep_assert_held(&si->lock);
468
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100469 /*
470 * Insert entry into the connection hash.
471 */
472 conn_idx = sfe_ipv4_get_connection_hash(c->protocol, c->src_ip, c->src_port,
473 c->dest_ip, c->dest_port);
474 hash_head = &si->conn_hash[conn_idx];
475 prev_head = *hash_head;
476 c->prev = NULL;
477 if (prev_head) {
478 prev_head->prev = c;
479 }
480
481 c->next = prev_head;
482 *hash_head = c;
483
484 /*
485 * Insert entry into the "all connections" list.
486 */
487 if (si->all_connections_tail) {
488 c->all_connections_prev = si->all_connections_tail;
489 si->all_connections_tail->all_connections_next = c;
490 } else {
491 c->all_connections_prev = NULL;
492 si->all_connections_head = c;
493 }
494
495 si->all_connections_tail = c;
496 c->all_connections_next = NULL;
497 si->num_connections++;
498
499 /*
500 * Insert the connection match objects too.
501 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530502 sfe_ipv4_insert_connection_match(si, c->original_match);
503 sfe_ipv4_insert_connection_match(si, c->reply_match);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100504}
505
506/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530507 * sfe_ipv4_remove_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100508 * Remove a sfe_ipv4_connection object from the hash.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100509 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530510bool sfe_ipv4_remove_connection(struct sfe_ipv4 *si, struct sfe_ipv4_connection *c)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100511{
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530512 lockdep_assert_held(&si->lock);
513
514 if (c->removed) {
515 DEBUG_ERROR("%px: Connection has been removed already\n", c);
516 return false;
517 }
518
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100519 /*
520 * Remove the connection match objects.
521 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530522 sfe_ipv4_remove_connection_match(si, c->reply_match);
523 sfe_ipv4_remove_connection_match(si, c->original_match);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100524
525 /*
526 * Unlink the connection.
527 */
528 if (c->prev) {
529 c->prev->next = c->next;
530 } else {
531 unsigned int conn_idx = sfe_ipv4_get_connection_hash(c->protocol, c->src_ip, c->src_port,
532 c->dest_ip, c->dest_port);
533 si->conn_hash[conn_idx] = c->next;
534 }
535
536 if (c->next) {
537 c->next->prev = c->prev;
538 }
Xiaoping Fan34586472015-07-03 02:20:35 -0700539
540 /*
541 * Unlink connection from all_connections list
542 */
543 if (c->all_connections_prev) {
544 c->all_connections_prev->all_connections_next = c->all_connections_next;
545 } else {
546 si->all_connections_head = c->all_connections_next;
547 }
548
549 if (c->all_connections_next) {
550 c->all_connections_next->all_connections_prev = c->all_connections_prev;
551 } else {
552 si->all_connections_tail = c->all_connections_prev;
553 }
554
Ken Zhudc423672021-09-02 18:27:01 -0700555 /*
556 * If I am the next sync connection, move the sync to my next or head.
557 */
558 if (unlikely(si->wc_next == c)) {
559 si->wc_next = c->all_connections_next;
560 }
561
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530562 c->removed = true;
Xiaoping Fan34586472015-07-03 02:20:35 -0700563 si->num_connections--;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530564 return true;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100565}
566
567/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530568 * sfe_ipv4_gen_sync_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100569 * Sync a connection.
570 *
571 * On entry to this function we expect that the lock for the connection is either
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530572 * already held (while called from sfe_ipv4_periodic_sync() or isn't required
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530573 * (while called from sfe_ipv4_flush_connection())
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100574 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530575static void sfe_ipv4_gen_sync_connection(struct sfe_ipv4 *si, struct sfe_ipv4_connection *c,
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700576 struct sfe_connection_sync *sis, sfe_sync_reason_t reason,
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700577 u64 now_jiffies)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100578{
579 struct sfe_ipv4_connection_match *original_cm;
580 struct sfe_ipv4_connection_match *reply_cm;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530581 u32 packet_count, byte_count;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100582
583 /*
584 * Fill in the update message.
585 */
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700586 sis->is_v6 = 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100587 sis->protocol = c->protocol;
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700588 sis->src_ip.ip = c->src_ip;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700589 sis->src_ip_xlate.ip = c->src_ip_xlate;
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700590 sis->dest_ip.ip = c->dest_ip;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700591 sis->dest_ip_xlate.ip = c->dest_ip_xlate;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100592 sis->src_port = c->src_port;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700593 sis->src_port_xlate = c->src_port_xlate;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100594 sis->dest_port = c->dest_port;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700595 sis->dest_port_xlate = c->dest_port_xlate;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100596
597 original_cm = c->original_match;
598 reply_cm = c->reply_match;
599 sis->src_td_max_window = original_cm->protocol_state.tcp.max_win;
600 sis->src_td_end = original_cm->protocol_state.tcp.end;
601 sis->src_td_max_end = original_cm->protocol_state.tcp.max_end;
602 sis->dest_td_max_window = reply_cm->protocol_state.tcp.max_win;
603 sis->dest_td_end = reply_cm->protocol_state.tcp.end;
604 sis->dest_td_max_end = reply_cm->protocol_state.tcp.max_end;
605
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530606 sfe_ipv4_connection_match_update_summary_stats(original_cm, &packet_count, &byte_count);
607 sis->src_new_packet_count = packet_count;
608 sis->src_new_byte_count = byte_count;
Matthew McClintockd0cdb802014-02-24 16:30:35 -0600609
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530610 sfe_ipv4_connection_match_update_summary_stats(reply_cm, &packet_count, &byte_count);
611 sis->dest_new_packet_count = packet_count;
612 sis->dest_new_byte_count = byte_count;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100613
Matthew McClintockd0cdb802014-02-24 16:30:35 -0600614 sis->src_dev = original_cm->match_dev;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100615 sis->src_packet_count = original_cm->rx_packet_count64;
616 sis->src_byte_count = original_cm->rx_byte_count64;
Matthew McClintockd0cdb802014-02-24 16:30:35 -0600617
618 sis->dest_dev = reply_cm->match_dev;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100619 sis->dest_packet_count = reply_cm->rx_packet_count64;
620 sis->dest_byte_count = reply_cm->rx_byte_count64;
621
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700622 sis->reason = reason;
623
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100624 /*
625 * Get the time increment since our last sync.
626 */
627 sis->delta_jiffies = now_jiffies - c->last_sync_jiffies;
628 c->last_sync_jiffies = now_jiffies;
629}
630
631/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530632 * sfe_ipv4_free_connection_rcu()
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530633 * Called at RCU qs state to free the connection object.
634 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530635static void sfe_ipv4_free_connection_rcu(struct rcu_head *head)
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530636{
637 struct sfe_ipv4_connection *c;
Amitesh Anand63be37d2021-12-24 20:51:48 +0530638 struct udp_sock *up;
639 struct sock *sk;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530640
641 /*
642 * We dont need spin lock as the connection is already removed from link list
643 */
644 c = container_of(head, struct sfe_ipv4_connection, rcu);
645
646 BUG_ON(!c->removed);
647
648 DEBUG_TRACE("%px: connecton has been deleted\n", c);
649
650 /*
Amitesh Anand63be37d2021-12-24 20:51:48 +0530651 * Decrease the refcount taken in function sfe_ipv4_create_rule(),
652 * during call of __udp4_lib_lookup()
653 */
654 up = c->reply_match->up;
655 if (up) {
656 sk = (struct sock *)up;
657 sock_put(sk);
658 }
659
660 /*
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530661 * Release our hold of the source and dest devices and free the memory
662 * for our connection objects.
663 */
664 dev_put(c->original_dev);
665 dev_put(c->reply_dev);
666 kfree(c->original_match);
667 kfree(c->reply_match);
668 kfree(c);
669}
670
671/*
Ken Zhu88c58152021-12-09 15:12:06 -0800672 * sfe_ipv4_sync_status()
673 * update a connection status to its connection manager.
674 *
675 * si: the ipv4 context
676 * c: which connection to be notified
677 * reason: what kind of notification: flush, stats or destroy
678 */
679void sfe_ipv4_sync_status(struct sfe_ipv4 *si,
680 struct sfe_ipv4_connection *c,
681 sfe_sync_reason_t reason)
682{
683 struct sfe_connection_sync sis;
684 u64 now_jiffies;
685 sfe_sync_rule_callback_t sync_rule_callback;
686
687 rcu_read_lock();
688 sync_rule_callback = rcu_dereference(si->sync_rule_callback);
689 if (!sync_rule_callback) {
690 rcu_read_unlock();
691 return;
692 }
693
694 /*
695 * Generate a sync message and then sync.
696 */
697 now_jiffies = get_jiffies_64();
698 sfe_ipv4_gen_sync_connection(si, c, &sis, reason, now_jiffies);
699 sync_rule_callback(&sis);
700
701 rcu_read_unlock();
702}
703
704/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530705 * sfe_ipv4_flush_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100706 * Flush a connection and free all associated resources.
707 *
708 * We need to be called with bottom halves disabled locally as we need to acquire
709 * the connection hash lock and release it again. In general we're actually called
710 * from within a BH and so we're fine, but we're also called when connections are
711 * torn down.
712 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530713void sfe_ipv4_flush_connection(struct sfe_ipv4 *si,
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700714 struct sfe_ipv4_connection *c,
715 sfe_sync_reason_t reason)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100716{
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530717 BUG_ON(!c->removed);
718
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530719 this_cpu_inc(si->stats_pcpu->connection_flushes64);
Ken Zhu88c58152021-12-09 15:12:06 -0800720 sfe_ipv4_sync_status(si, c, reason);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100721
722 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100723 * Release our hold of the source and dest devices and free the memory
724 * for our connection objects.
725 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530726 call_rcu(&c->rcu, sfe_ipv4_free_connection_rcu);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100727}
728
729/*
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530730 * sfe_ipv4_exception_stats_inc()
731 * Increment exception stats.
732 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530733void sfe_ipv4_exception_stats_inc(struct sfe_ipv4 *si, enum sfe_ipv4_exception_events reason)
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530734{
735 struct sfe_ipv4_stats *stats = this_cpu_ptr(si->stats_pcpu);
736 stats->exception_events64[reason]++;
737 stats->packets_not_forwarded64++;
738}
739
740/*
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530741 * sfe_ipv4_is_loal_ip()
742 * Returns true if IP is local; returns false otherwise.
743 */
744static bool sfe_ipv4_is_local_ip(struct sfe_ipv4 *si, __be32 ip_addr)
745{
746 struct net_device *dev;
747
748 dev = ip_dev_find(&init_net, ip_addr);
749 if (dev) {
750 dev_put(dev);
751 return true;
752 }
753
754 return false;
755}
756
757/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100758 * sfe_ipv4_recv()
Matthew McClintocka8ad7962014-01-16 16:49:30 -0600759 * Handle packet receives and forwaring.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100760 *
761 * Returns 1 if the packet is forwarded or 0 if it isn't.
762 */
Amitesh Anand63be37d2021-12-24 20:51:48 +0530763int 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 +0100764{
765 struct sfe_ipv4 *si = &__si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100766 unsigned int len;
767 unsigned int tot_len;
768 unsigned int frag_off;
769 unsigned int ihl;
Ken Zhu88c58152021-12-09 15:12:06 -0800770 bool sync_on_find;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100771 bool ip_options;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530772 struct iphdr *iph;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700773 u32 protocol;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100774
775 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100776 * Check that we have space for an IP header here.
777 */
778 len = skb->len;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530779 if (unlikely(!pskb_may_pull(skb, sizeof(struct iphdr)))) {
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530780 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_HEADER_INCOMPLETE);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100781 DEBUG_TRACE("len: %u is too short\n", len);
782 return 0;
783 }
784
785 /*
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +0530786 * Validate ip csum if necessary. If ip_summed is set to CHECKSUM_UNNECESSARY, it is assumed
787 * that the L3 checksum is validated by the Rx interface or the tunnel interface that has
788 * generated the packet.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100789 */
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530790 iph = (struct iphdr *)skb->data;
Ratheesh Kannoth43d64f82021-10-20 08:23:29 +0530791 if (unlikely(skb->ip_summed != CHECKSUM_UNNECESSARY) && (ip_fast_csum((u8 *)iph, iph->ihl))) {
792 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_HEADER_CSUM_BAD);
793
794 DEBUG_TRACE("Bad IPv4 header csum: 0x%x\n", iph->check);
795 return 0;
796 }
797
798 /*
799 * Check that our "total length" is large enough for an IP header.
800 */
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100801 tot_len = ntohs(iph->tot_len);
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530802 if (unlikely(tot_len < sizeof(struct iphdr))) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100803
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530804 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_BAD_TOTAL_LENGTH);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100805 DEBUG_TRACE("tot_len: %u is too short\n", tot_len);
806 return 0;
807 }
808
809 /*
810 * Is our IP version wrong?
811 */
812 if (unlikely(iph->version != 4)) {
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530813 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_NON_V4);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100814 DEBUG_TRACE("IP version: %u\n", iph->version);
815 return 0;
816 }
817
818 /*
819 * Does our datagram fit inside the skb?
820 */
821 if (unlikely(tot_len > len)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100822 DEBUG_TRACE("tot_len: %u, exceeds len: %u\n", tot_len, len);
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530823 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_DATAGRAM_INCOMPLETE);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100824 return 0;
825 }
826
827 /*
828 * Do we have a non-initial fragment?
Nicolas Costaac2979c2014-01-14 10:35:24 -0600829 */
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100830 frag_off = ntohs(iph->frag_off);
831 if (unlikely(frag_off & IP_OFFSET)) {
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530832 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_NON_INITIAL_FRAGMENT);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100833 DEBUG_TRACE("non-initial fragment\n");
834 return 0;
835 }
836
837 /*
838 * If we have a (first) fragment then mark it to cause any connection to flush.
839 */
Ken Zhu88c58152021-12-09 15:12:06 -0800840 sync_on_find = unlikely(frag_off & IP_MF) ? true : false;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100841
842 /*
843 * Do we have any IP options? That's definite a slow path! If we do have IP
844 * options we need to recheck our header size.
845 */
846 ihl = iph->ihl << 2;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530847 ip_options = unlikely(ihl != sizeof(struct iphdr)) ? true : false;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100848 if (unlikely(ip_options)) {
849 if (unlikely(len < ihl)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100850
851 DEBUG_TRACE("len: %u is too short for header of size: %u\n", len, ihl);
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530852 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_IP_OPTIONS_INCOMPLETE);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100853 return 0;
854 }
855
Ken Zhu88c58152021-12-09 15:12:06 -0800856 sync_on_find = true;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100857 }
858
859 protocol = iph->protocol;
860 if (IPPROTO_UDP == protocol) {
Ken Zhu88c58152021-12-09 15:12:06 -0800861 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 +0100862 }
863
864 if (IPPROTO_TCP == protocol) {
Ken Zhu88c58152021-12-09 15:12:06 -0800865 return sfe_ipv4_recv_tcp(si, skb, dev, len, iph, ihl, sync_on_find, l2_info);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100866 }
867
868 if (IPPROTO_ICMP == protocol) {
869 return sfe_ipv4_recv_icmp(si, skb, dev, len, iph, ihl);
870 }
871
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530872#ifdef SFE_GRE_TUN_ENABLE
873 if (IPPROTO_GRE == protocol) {
874 return sfe_ipv4_recv_gre(si, skb, dev, len, iph, ihl, sync_on_find, tun_outer);
875 }
876#endif
877
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530878 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_UNHANDLED_PROTOCOL);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100879
880 DEBUG_TRACE("not UDP, TCP or ICMP: %u\n", protocol);
881 return 0;
882}
883
Nicolas Costa436926b2014-01-14 10:36:22 -0600884static void
885sfe_ipv4_update_tcp_state(struct sfe_ipv4_connection *c,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530886 struct sfe_ipv4_rule_create_msg *msg)
Nicolas Costa436926b2014-01-14 10:36:22 -0600887{
888 struct sfe_ipv4_connection_match *orig_cm;
889 struct sfe_ipv4_connection_match *repl_cm;
890 struct sfe_ipv4_tcp_connection_match *orig_tcp;
891 struct sfe_ipv4_tcp_connection_match *repl_tcp;
892
893 orig_cm = c->original_match;
894 repl_cm = c->reply_match;
895 orig_tcp = &orig_cm->protocol_state.tcp;
896 repl_tcp = &repl_cm->protocol_state.tcp;
897
898 /* update orig */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530899 if (orig_tcp->max_win < msg->tcp_rule.flow_max_window) {
900 orig_tcp->max_win = msg->tcp_rule.flow_max_window;
Nicolas Costa436926b2014-01-14 10:36:22 -0600901 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530902 if ((s32)(orig_tcp->end - msg->tcp_rule.flow_end) < 0) {
903 orig_tcp->end = msg->tcp_rule.flow_end;
Nicolas Costa436926b2014-01-14 10:36:22 -0600904 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530905 if ((s32)(orig_tcp->max_end - msg->tcp_rule.flow_max_end) < 0) {
906 orig_tcp->max_end = msg->tcp_rule.flow_max_end;
Nicolas Costa436926b2014-01-14 10:36:22 -0600907 }
908
909 /* update reply */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530910 if (repl_tcp->max_win < msg->tcp_rule.return_max_window) {
911 repl_tcp->max_win = msg->tcp_rule.return_max_window;
Nicolas Costa436926b2014-01-14 10:36:22 -0600912 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530913 if ((s32)(repl_tcp->end - msg->tcp_rule.return_end) < 0) {
914 repl_tcp->end = msg->tcp_rule.return_end;
Nicolas Costa436926b2014-01-14 10:36:22 -0600915 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530916 if ((s32)(repl_tcp->max_end - msg->tcp_rule.return_max_end) < 0) {
917 repl_tcp->max_end = msg->tcp_rule.return_max_end;
Nicolas Costa436926b2014-01-14 10:36:22 -0600918 }
919
920 /* update match flags */
921 orig_cm->flags &= ~SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
922 repl_cm->flags &= ~SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530923 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_NO_SEQ_CHECK) {
924
Nicolas Costa436926b2014-01-14 10:36:22 -0600925 orig_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
926 repl_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
927 }
928}
929
930static void
931sfe_ipv4_update_protocol_state(struct sfe_ipv4_connection *c,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530932 struct sfe_ipv4_rule_create_msg *msg)
Nicolas Costa436926b2014-01-14 10:36:22 -0600933{
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530934 switch (msg->tuple.protocol) {
Nicolas Costa436926b2014-01-14 10:36:22 -0600935 case IPPROTO_TCP:
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530936 sfe_ipv4_update_tcp_state(c, msg);
Nicolas Costa436926b2014-01-14 10:36:22 -0600937 break;
938 }
939}
940
Wayne Tanbb7f1782021-12-13 11:16:04 -0800941/*
942 * sfe_ipv4_match_entry_set_vlan()
943 */
944static void sfe_ipv4_match_entry_set_vlan(
945 struct sfe_ipv4_connection_match *cm,
946 u32 primary_ingress_vlan_tag,
947 u32 primary_egress_vlan_tag,
948 u32 secondary_ingress_vlan_tag,
949 u32 secondary_egress_vlan_tag)
950{
951 u16 tpid;
952 /*
953 * Prevent stacking header counts when updating.
954 */
955 cm->ingress_vlan_hdr_cnt = 0;
956 cm->egress_vlan_hdr_cnt = 0;
957 memset(cm->ingress_vlan_hdr, 0, sizeof(cm->ingress_vlan_hdr));
958 memset(cm->egress_vlan_hdr, 0, sizeof(cm->egress_vlan_hdr));
959
960 /*
961 * vlan_hdr[0] corresponds to outer tag
962 * vlan_hdr[1] corresponds to inner tag
963 * Extract the vlan information (tpid and tci) from rule message
964 */
965 if ((primary_ingress_vlan_tag & VLAN_VID_MASK) != SFE_VLAN_ID_NOT_CONFIGURED) {
966 tpid = (u16)(primary_ingress_vlan_tag >> 16);
967 cm->ingress_vlan_hdr[0].tpid = ntohs(tpid);
968 cm->ingress_vlan_hdr[0].tci = (u16)primary_ingress_vlan_tag;
969 cm->ingress_vlan_hdr_cnt++;
970 }
971
972 if ((secondary_ingress_vlan_tag & VLAN_VID_MASK) != SFE_VLAN_ID_NOT_CONFIGURED) {
973 tpid = (u16)(secondary_ingress_vlan_tag >> 16);
974 cm->ingress_vlan_hdr[1].tpid = ntohs(tpid);
975 cm->ingress_vlan_hdr[1].tci = (u16)secondary_ingress_vlan_tag;
976 cm->ingress_vlan_hdr_cnt++;
977 }
978
979 if ((primary_egress_vlan_tag & VLAN_VID_MASK) != SFE_VLAN_ID_NOT_CONFIGURED) {
980 tpid = (u16)(primary_egress_vlan_tag >> 16);
981 cm->egress_vlan_hdr[0].tpid = ntohs(tpid);
982 cm->egress_vlan_hdr[0].tci = (u16)primary_egress_vlan_tag;
983 cm->egress_vlan_hdr_cnt++;
984 }
985
986 if ((secondary_egress_vlan_tag & VLAN_VID_MASK) != SFE_VLAN_ID_NOT_CONFIGURED) {
987 tpid = (u16)(secondary_egress_vlan_tag >> 16);
988 cm->egress_vlan_hdr[1].tpid = ntohs(tpid);
989 cm->egress_vlan_hdr[1].tci = (u16)secondary_egress_vlan_tag;
990 cm->egress_vlan_hdr_cnt++;
991 }
992}
993
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530994void sfe_ipv4_update_rule(struct sfe_ipv4_rule_create_msg *msg)
Nicolas Costa436926b2014-01-14 10:36:22 -0600995{
996 struct sfe_ipv4_connection *c;
997 struct sfe_ipv4 *si = &__si;
998
999 spin_lock_bh(&si->lock);
1000
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301001 c = sfe_ipv4_find_connection(si,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301002 msg->tuple.protocol,
1003 msg->tuple.flow_ip,
1004 msg->tuple.flow_ident,
1005 msg->tuple.return_ip,
1006 msg->tuple.return_ident);
Nicolas Costa436926b2014-01-14 10:36:22 -06001007 if (c != NULL) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301008 sfe_ipv4_update_protocol_state(c, msg);
Nicolas Costa436926b2014-01-14 10:36:22 -06001009 }
1010
1011 spin_unlock_bh(&si->lock);
1012}
1013
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001014/*
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +05301015 * sfe_ipv4_xmit_eth_type_check()
1016 * Checking if MAC header has to be written.
1017 */
1018static inline bool sfe_ipv4_xmit_eth_type_check(struct net_device *dev, u32 cm_flags)
1019{
1020 if (!(dev->flags & IFF_NOARP)) {
1021 return true;
1022 }
1023
1024 /*
1025 * For PPPoE, since we are now supporting PPPoE encapsulation, we are writing L2 header.
1026 */
1027 if (unlikely(cm_flags & SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_ENCAP)) {
1028 return true;
1029 }
1030
1031 return false;
1032}
1033
1034/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001035 * sfe_ipv4_create_rule()
1036 * Create a forwarding rule.
1037 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301038int sfe_ipv4_create_rule(struct sfe_ipv4_rule_create_msg *msg)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001039{
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001040 struct sfe_ipv4 *si = &__si;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301041 struct sfe_ipv4_connection *c, *c_old;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001042 struct sfe_ipv4_connection_match *original_cm;
1043 struct sfe_ipv4_connection_match *reply_cm;
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001044 struct net_device *dest_dev;
1045 struct net_device *src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301046 struct sfe_ipv4_5tuple *tuple = &msg->tuple;
Suruchi Sumanc1a4a612021-10-21 14:50:23 +05301047 s32 flow_interface_num = msg->conn_rule.flow_top_interface_num;
1048 s32 return_interface_num = msg->conn_rule.return_top_interface_num;
Amitesh Anand63be37d2021-12-24 20:51:48 +05301049 struct net *net;
1050 struct sock *sk;
1051 unsigned int src_if_idx;
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001052
Suruchi Sumanc1a4a612021-10-21 14:50:23 +05301053 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_FLOW_BOTTOM_INTERFACE) {
1054 flow_interface_num = msg->conn_rule.flow_interface_num;
1055 }
1056
1057 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_RETURN_BOTTOM_INTERFACE) {
1058 return_interface_num = msg->conn_rule.return_interface_num;
1059 }
1060
1061 src_dev = dev_get_by_index(&init_net, flow_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301062 if (!src_dev) {
1063 DEBUG_WARN("%px: Unable to find src_dev corresponding to %d\n", msg,
Suruchi Sumanc1a4a612021-10-21 14:50:23 +05301064 flow_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301065 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
1066 return -EINVAL;
1067 }
1068
Suruchi Sumanc1a4a612021-10-21 14:50:23 +05301069 dest_dev = dev_get_by_index(&init_net, return_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301070 if (!dest_dev) {
1071 DEBUG_WARN("%px: Unable to find dest_dev corresponding to %d\n", msg,
Suruchi Sumanc1a4a612021-10-21 14:50:23 +05301072 return_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301073 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
1074 dev_put(src_dev);
1075 return -EINVAL;
1076 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001077
Matthew McClintock389b42a2014-09-24 14:05:51 -05001078 if (unlikely((dest_dev->reg_state != NETREG_REGISTERED) ||
1079 (src_dev->reg_state != NETREG_REGISTERED))) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301080 dev_put(src_dev);
1081 dev_put(dest_dev);
1082 DEBUG_WARN("%px: src_dev=%s and dest_dev=%s are unregistered\n", msg,
1083 src_dev->name, dest_dev->name);
1084 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
Matthew McClintock389b42a2014-09-24 14:05:51 -05001085 return -EINVAL;
1086 }
1087
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301088 /*
1089 * Allocate the various connection tracking objects.
1090 */
1091 c = (struct sfe_ipv4_connection *)kmalloc(sizeof(struct sfe_ipv4_connection), GFP_ATOMIC);
1092 if (unlikely(!c)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301093 DEBUG_WARN("%px: memory allocation of connection entry failed\n", msg);
1094 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
1095 dev_put(src_dev);
1096 dev_put(dest_dev);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301097 return -ENOMEM;
1098 }
1099
1100 original_cm = (struct sfe_ipv4_connection_match *)kmalloc(sizeof(struct sfe_ipv4_connection_match), GFP_ATOMIC);
1101 if (unlikely(!original_cm)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301102 DEBUG_WARN("%px: memory allocation of connection match entry failed\n", msg);
1103 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301104 kfree(c);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301105 dev_put(src_dev);
1106 dev_put(dest_dev);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301107 return -ENOMEM;
1108 }
1109
1110 reply_cm = (struct sfe_ipv4_connection_match *)kmalloc(sizeof(struct sfe_ipv4_connection_match), GFP_ATOMIC);
1111 if (unlikely(!reply_cm)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301112 DEBUG_WARN("%px: memory allocation of connection match entry failed\n", msg);
1113 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301114 kfree(original_cm);
1115 kfree(c);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301116 dev_put(src_dev);
1117 dev_put(dest_dev);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301118 return -ENOMEM;
1119 }
1120
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301121 this_cpu_inc(si->stats_pcpu->connection_create_requests64);
1122
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001123 spin_lock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001124
1125 /*
Nicolas Costa436926b2014-01-14 10:36:22 -06001126 * Check to see if there is already a flow that matches the rule we're
1127 * trying to create. If there is then we can't create a new one.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001128 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301129 c_old = sfe_ipv4_find_connection(si,
Wayne Tanbb7f1782021-12-13 11:16:04 -08001130 msg->tuple.protocol,
1131 msg->tuple.flow_ip,
1132 msg->tuple.flow_ident,
1133 msg->tuple.return_ip,
1134 msg->tuple.return_ident);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301135
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301136 if (c_old != NULL) {
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301137 this_cpu_inc(si->stats_pcpu->connection_create_collisions64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001138
1139 /*
Nicolas Costa436926b2014-01-14 10:36:22 -06001140 * If we already have the flow then it's likely that this
1141 * request to create the connection rule contains more
1142 * up-to-date information. Check and update accordingly.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001143 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301144 sfe_ipv4_update_protocol_state(c, msg);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001145 spin_unlock_bh(&si->lock);
1146
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301147 kfree(reply_cm);
1148 kfree(original_cm);
1149 kfree(c);
1150
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301151 dev_put(src_dev);
1152 dev_put(dest_dev);
1153
Amitesh Anand63be37d2021-12-24 20:51:48 +05301154 DEBUG_TRACE("%px: connection already exists - p:%d\n"
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301155 " s: %s:%pM:%pI4:%u, d: %s:%pM:%pI4:%u\n",
Amitesh Anand63be37d2021-12-24 20:51:48 +05301156 msg, tuple->protocol,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301157 src_dev->name, msg->conn_rule.flow_mac, &tuple->flow_ip, ntohs(tuple->flow_ident),
1158 dest_dev->name, msg->conn_rule.return_mac, &tuple->return_ip, ntohs(tuple->return_ident));
1159
Nicolas Costa514fde02014-01-13 15:50:29 -06001160 return -EADDRINUSE;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001161 }
1162
1163 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001164 * Fill in the "original" direction connection matching object.
1165 * Note that the transmit MAC address is "dest_mac_xlate" because
1166 * we always know both ends of a connection by their translated
1167 * addresses and not their public addresses.
1168 */
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001169 original_cm->match_dev = src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301170 original_cm->match_protocol = tuple->protocol;
1171 original_cm->match_src_ip = tuple->flow_ip;
Suruchi Suman66609a72022-01-20 02:34:25 +05301172 original_cm->match_src_port = netif_is_vxlan(src_dev) ? 0 : tuple->flow_ident;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301173 original_cm->match_dest_ip = tuple->return_ip;
1174 original_cm->match_dest_port = tuple->return_ident;
1175
1176 original_cm->xlate_src_ip = msg->conn_rule.flow_ip_xlate;
1177 original_cm->xlate_src_port = msg->conn_rule.flow_ident_xlate;
1178 original_cm->xlate_dest_ip = msg->conn_rule.return_ip_xlate;
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301179 original_cm->xlate_dest_port = msg->conn_rule.return_ident_xlate;
1180
1181 if (tuple->protocol == IPPROTO_GRE) {
1182 /*
1183 * the PPTP is 4 tuple lookup.
1184 * During th rule lookup destination call id from packet
1185 * is matched against destination port in cm.
1186 */
1187 original_cm->match_src_port = 0;
1188 original_cm->xlate_src_port = 0;
1189 }
Wayne Tanbb7f1782021-12-13 11:16:04 -08001190
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301191 atomic_set(&original_cm->rx_packet_count, 0);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001192 original_cm->rx_packet_count64 = 0;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301193 atomic_set(&original_cm->rx_byte_count, 0);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001194 original_cm->rx_byte_count64 = 0;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301195
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001196 original_cm->xmit_dev = dest_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301197 original_cm->xmit_dev_mtu = msg->conn_rule.return_mtu;
1198
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001199 original_cm->connection = c;
1200 original_cm->counter_match = reply_cm;
Wayne Tanbb7f1782021-12-13 11:16:04 -08001201 original_cm->l2_hdr_size = 0;
1202 original_cm->flags = 0;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301203
Amitesh Anand63be37d2021-12-24 20:51:48 +05301204 /*
1205 * UDP Socket is valid only in decap direction.
1206 */
1207 RCU_INIT_POINTER(original_cm->up, NULL);
1208
Ken Zhu37040ea2021-09-09 21:11:15 -07001209 if (msg->valid_flags & SFE_RULE_CREATE_MARK_VALID) {
1210 original_cm->mark = msg->mark_rule.flow_mark;
1211 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_MARK;
1212 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301213 if (msg->valid_flags & SFE_RULE_CREATE_QOS_VALID) {
1214 original_cm->priority = msg->qos_rule.flow_qos_tag;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001215 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PRIORITY_REMARK;
1216 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301217 if (msg->valid_flags & SFE_RULE_CREATE_DSCP_MARKING_VALID) {
1218 original_cm->dscp = msg->dscp_rule.flow_dscp << SFE_IPV4_DSCP_SHIFT;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001219 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_DSCP_REMARK;
1220 }
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301221
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301222 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1223 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_BRIDGE_FLOW;
1224 }
1225
Wayne Tanbb7f1782021-12-13 11:16:04 -08001226 /*
1227 * Add VLAN rule to original_cm
1228 */
1229 if (msg->valid_flags & SFE_RULE_CREATE_VLAN_VALID) {
1230 struct sfe_vlan_rule *vlan_primary_rule = &msg->vlan_primary_rule;
1231 struct sfe_vlan_rule *vlan_secondary_rule = &msg->vlan_secondary_rule;
1232 sfe_ipv4_match_entry_set_vlan(original_cm,
1233 vlan_primary_rule->ingress_vlan_tag,
1234 vlan_primary_rule->egress_vlan_tag,
1235 vlan_secondary_rule->ingress_vlan_tag,
1236 vlan_secondary_rule->egress_vlan_tag);
1237
1238 if ((msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_RETURN_BOTTOM_INTERFACE) &&
1239 original_cm->egress_vlan_hdr_cnt > 0) {
1240 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_INSERT_EGRESS_VLAN_TAG;
1241 original_cm->l2_hdr_size += original_cm->egress_vlan_hdr_cnt * VLAN_HLEN;
1242 }
1243 }
1244
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301245 if ((IPPROTO_GRE == tuple->protocol) && !sfe_ipv4_is_local_ip(si, original_cm->match_dest_ip)) {
1246 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PASSTHROUGH;
1247 }
1248
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001249#ifdef CONFIG_NF_FLOW_COOKIE
1250 original_cm->flow_cookie = 0;
1251#endif
Zhi Chen8748eb32015-06-18 12:58:48 -07001252#ifdef CONFIG_XFRM
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301253 if (msg->valid_flags & SFE_RULE_CREATE_DIRECTION_VALID) {
1254 original_cm->flow_accel = msg->direction_rule.flow_accel;
1255 } else {
1256 original_cm->flow_accel = 1;
1257 }
Zhi Chen8748eb32015-06-18 12:58:48 -07001258#endif
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301259 /*
1260 * If l2_features are disabled and flow uses l2 features such as macvlan/bridge/pppoe/vlan,
1261 * bottom interfaces are expected to be disabled in the flow rule and always top interfaces
1262 * are used. In such cases, do not use HW csum offload. csum offload is used only when we
1263 * are sending directly to the destination interface that supports it.
1264 */
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301265 if (likely(dest_dev->features & NETIF_F_HW_CSUM) && sfe_dev_has_hw_csum(dest_dev)) {
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301266 if ((msg->conn_rule.return_top_interface_num == msg->conn_rule.return_interface_num) ||
1267 (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_RETURN_BOTTOM_INTERFACE)) {
Suruchi Sumanf2077182022-01-13 21:35:23 +05301268 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_CSUM_OFFLOAD;
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301269 }
1270 }
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001271
Wayne Tanbb7f1782021-12-13 11:16:04 -08001272 reply_cm->l2_hdr_size = 0;
Ratheesh Kannoth5dee3772022-01-18 11:27:14 +05301273 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_SRC_INTERFACE_CHECK) {
1274 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_SRC_INTERFACE_CHECK;
1275 }
1276
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +05301277 reply_cm->flags = 0;
1278
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001279 /*
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301280 * Adding PPPoE parameters to original and reply entries based on the direction where
1281 * PPPoE header is valid in ECM rule.
1282 *
1283 * If PPPoE is valid in flow direction (from interface is PPPoE), then
1284 * original cm will have PPPoE at ingress (strip PPPoE header)
1285 * reply cm will have PPPoE at egress (add PPPoE header)
1286 *
1287 * If PPPoE is valid in return direction (to interface is PPPoE), then
1288 * original cm will have PPPoE at egress (add PPPoE header)
1289 * reply cm will have PPPoE at ingress (strip PPPoE header)
1290 */
1291 if (msg->valid_flags & SFE_RULE_CREATE_PPPOE_DECAP_VALID) {
1292 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_DECAP;
1293 original_cm->pppoe_session_id = msg->pppoe_rule.flow_pppoe_session_id;
1294 ether_addr_copy(original_cm->pppoe_remote_mac, msg->pppoe_rule.flow_pppoe_remote_mac);
1295
1296 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_ENCAP;
Wayne Tanbb7f1782021-12-13 11:16:04 -08001297 reply_cm->l2_hdr_size += SFE_PPPOE_SESSION_HEADER_SIZE;
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301298 reply_cm->pppoe_session_id = msg->pppoe_rule.flow_pppoe_session_id;
1299 ether_addr_copy(reply_cm->pppoe_remote_mac, msg->pppoe_rule.flow_pppoe_remote_mac);
1300 }
1301
1302 if (msg->valid_flags & SFE_RULE_CREATE_PPPOE_ENCAP_VALID) {
1303 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_ENCAP;
Wayne Tanbb7f1782021-12-13 11:16:04 -08001304 original_cm->l2_hdr_size += SFE_PPPOE_SESSION_HEADER_SIZE;
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301305 original_cm->pppoe_session_id = msg->pppoe_rule.return_pppoe_session_id;
1306 ether_addr_copy(original_cm->pppoe_remote_mac, msg->pppoe_rule.return_pppoe_remote_mac);
1307
1308 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_DECAP;
1309 reply_cm->pppoe_session_id = msg->pppoe_rule.return_pppoe_session_id;
1310 ether_addr_copy(reply_cm->pppoe_remote_mac, msg->pppoe_rule.return_pppoe_remote_mac);
1311 }
1312
Ratheesh Kannoth5dee3772022-01-18 11:27:14 +05301313 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_SRC_INTERFACE_CHECK) {
1314 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_SRC_INTERFACE_CHECK;
1315 }
1316
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301317 /*
Ken Zhubbf49652021-09-12 15:33:09 -07001318 * For the non-arp interface, we don't write L2 HDR.
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001319 */
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +05301320 if (sfe_ipv4_xmit_eth_type_check(dest_dev, original_cm->flags)) {
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301321
1322 /*
1323 * Check whether the rule has configured a specific source MAC address to use.
1324 * This is needed when virtual L3 interfaces such as br-lan, macvlan, vlan are used during egress
1325 */
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301326
1327 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1328 ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)msg->conn_rule.flow_mac);
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301329 } else {
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301330 if ((msg->valid_flags & SFE_RULE_CREATE_SRC_MAC_VALID) &&
1331 (msg->src_mac_rule.mac_valid_flags & SFE_SRC_MAC_RETURN_VALID)) {
1332 ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)msg->src_mac_rule.return_src_mac);
1333 } else {
1334 ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)dest_dev->dev_addr);
1335 }
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301336 }
1337
1338 ether_addr_copy((u8 *)original_cm->xmit_dest_mac, (u8 *)msg->conn_rule.return_mac);
1339
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001340 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_L2_HDR;
Wayne Tanbb7f1782021-12-13 11:16:04 -08001341 original_cm->l2_hdr_size += ETH_HLEN;
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001342
1343 /*
1344 * If our dev writes Ethernet headers then we can write a really fast
1345 * version.
1346 */
1347 if (dest_dev->header_ops) {
1348 if (dest_dev->header_ops->create == eth_header) {
1349 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR;
1350 }
1351 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001352 }
1353
1354 /*
1355 * Fill in the "reply" direction connection matching object.
1356 */
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001357 reply_cm->match_dev = dest_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301358 reply_cm->match_protocol = tuple->protocol;
1359 reply_cm->match_src_ip = msg->conn_rule.return_ip_xlate;
Amitesh Anand63be37d2021-12-24 20:51:48 +05301360
1361 /*
1362 * Keep source port as 0 for VxLAN tunnels.
1363 */
1364 if (netif_is_vxlan(src_dev) || netif_is_vxlan(dest_dev)) {
1365 reply_cm->match_src_port = 0;
1366 } else {
1367 reply_cm->match_src_port = msg->conn_rule.return_ident_xlate;
1368 }
1369
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301370 reply_cm->match_dest_ip = msg->conn_rule.flow_ip_xlate;
1371 reply_cm->match_dest_port = msg->conn_rule.flow_ident_xlate;
1372
1373 reply_cm->xlate_src_ip = tuple->return_ip;
1374 reply_cm->xlate_src_port = tuple->return_ident;
1375 reply_cm->xlate_dest_ip = tuple->flow_ip;
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301376 reply_cm->xlate_dest_port = tuple->flow_ident;
1377
1378 if (tuple->protocol == IPPROTO_GRE) {
1379 /*
1380 * the PPTP is 4 tuple lookup.
1381 * During th rule lookup destination call id from packet
1382 * is matched against destination port in cm.
1383 */
1384 reply_cm->match_src_port = 0;
1385 reply_cm->xlate_src_port = 0;
1386 }
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301387
1388 atomic_set(&reply_cm->rx_packet_count, 0);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001389 reply_cm->rx_packet_count64 = 0;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301390 atomic_set(&reply_cm->rx_byte_count, 0);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001391 reply_cm->rx_byte_count64 = 0;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301392
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001393 reply_cm->xmit_dev = src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301394 reply_cm->xmit_dev_mtu = msg->conn_rule.flow_mtu;
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301395
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001396 reply_cm->connection = c;
1397 reply_cm->counter_match = original_cm;
Ken Zhu37040ea2021-09-09 21:11:15 -07001398
Ken Zhu37040ea2021-09-09 21:11:15 -07001399 if (msg->valid_flags & SFE_RULE_CREATE_MARK_VALID) {
1400 reply_cm->mark = msg->mark_rule.return_mark;
1401 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_MARK;
1402 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301403 if (msg->valid_flags & SFE_RULE_CREATE_QOS_VALID) {
1404 reply_cm->priority = msg->qos_rule.return_qos_tag;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001405 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PRIORITY_REMARK;
1406 }
Wayne Tanbb7f1782021-12-13 11:16:04 -08001407
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301408 if (msg->valid_flags & SFE_RULE_CREATE_DSCP_MARKING_VALID) {
1409 reply_cm->dscp = msg->dscp_rule.return_dscp << SFE_IPV4_DSCP_SHIFT;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001410 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_DSCP_REMARK;
1411 }
Amitesh Anand63be37d2021-12-24 20:51:48 +05301412
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301413 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1414 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_BRIDGE_FLOW;
1415 }
1416
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301417 if ((IPPROTO_GRE == tuple->protocol) && !sfe_ipv4_is_local_ip(si, reply_cm->match_dest_ip)) {
1418 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PASSTHROUGH;
1419 }
1420
Amitesh Anand63be37d2021-12-24 20:51:48 +05301421 /*
1422 * Setup UDP Socket if found to be valid for decap.
1423 */
1424 RCU_INIT_POINTER(reply_cm->up, NULL);
1425 net = dev_net(reply_cm->match_dev);
1426 src_if_idx = src_dev->ifindex;
1427
1428 rcu_read_lock();
1429
1430 /*
1431 * Look for the associated sock object.
1432 * __udp4_lib_lookup() holds a reference for this sock object,
1433 * which will be released in sfe_ipv4_free_connection_rcu()
1434 */
1435#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
1436 sk = __udp4_lib_lookup(net, reply_cm->match_dest_ip, reply_cm->match_dest_port,
1437 reply_cm->xlate_src_ip, reply_cm->xlate_src_port, src_if_idx, &udp_table);
1438#else
1439 sk = __udp4_lib_lookup(net, reply_cm->match_dest_ip, reply_cm->match_dest_port,
1440 reply_cm->xlate_src_ip, reply_cm->xlate_src_port, src_if_idx, 0, &udp_table, NULL);
1441#endif
1442
1443 rcu_read_unlock();
1444
1445 /*
1446 * We set the UDP sock pointer as valid only for decap direction.
1447 */
1448 if (sk && udp_sk(sk)->encap_type) {
1449#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
1450 if (!atomic_add_unless(&sk->sk_refcnt, 1, 0)) {
1451#else
1452 if (!refcount_inc_not_zero(&sk->sk_refcnt)) {
1453#endif
Wayne Tanbb7f1782021-12-13 11:16:04 -08001454 spin_unlock_bh(&si->lock);
Amitesh Anand63be37d2021-12-24 20:51:48 +05301455 kfree(reply_cm);
1456 kfree(original_cm);
1457 kfree(c);
1458
1459 DEBUG_TRACE("%px: sfe: unable to take reference for socket(%px) p:%d\n"
1460 " s: %s:%pM:%pI4:%u, d: %s:%pM:%pI4:%u\n",
1461 msg, sk, tuple->protocol,
1462 src_dev->name, msg->conn_rule.flow_mac, &tuple->flow_ip, ntohs(tuple->flow_ident),
1463 dest_dev->name, msg->conn_rule.return_mac, &tuple->return_ip, ntohs(tuple->return_ident));
1464
1465 dev_put(src_dev);
1466 dev_put(dest_dev);
1467
1468 return -ESHUTDOWN;
1469 }
1470
1471 rcu_assign_pointer(reply_cm->up, udp_sk(sk));
1472
1473 DEBUG_INFO("%px: Sock(%px) lookup success with reply_cm direction\n", msg, sk);
1474 DEBUG_INFO("%px: SFE connection -\n"
1475 " s: %s:%pI4(%pI4):%u(%u)\n"
1476 " d: %s:%pI4(%pI4):%u(%u)\n",
1477 msg, reply_cm->match_dev->name, &reply_cm->match_src_ip, &reply_cm->xlate_src_ip,
1478 ntohs(reply_cm->match_src_port), ntohs(reply_cm->xlate_src_port),
1479 reply_cm->xmit_dev->name, &reply_cm->match_dest_ip, &reply_cm->xlate_dest_ip,
1480 ntohs(reply_cm->match_dest_port), ntohs(reply_cm->xlate_dest_port));
1481 }
1482
Wayne Tanbb7f1782021-12-13 11:16:04 -08001483 /*
1484 * Add VLAN rule to reply_cm
1485 */
1486 if (msg->valid_flags & SFE_RULE_CREATE_VLAN_VALID) {
1487 struct sfe_vlan_rule *vlan_primary_rule = &msg->vlan_primary_rule;
1488 struct sfe_vlan_rule *vlan_secondary_rule = &msg->vlan_secondary_rule;
1489 sfe_ipv4_match_entry_set_vlan(reply_cm,
1490 vlan_primary_rule->egress_vlan_tag,
1491 vlan_primary_rule->ingress_vlan_tag,
1492 vlan_secondary_rule->egress_vlan_tag,
1493 vlan_secondary_rule->ingress_vlan_tag);
1494
1495 if ((msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_FLOW_BOTTOM_INTERFACE) &&
1496 reply_cm->egress_vlan_hdr_cnt > 0) {
1497 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_INSERT_EGRESS_VLAN_TAG;
1498 reply_cm->l2_hdr_size += reply_cm->egress_vlan_hdr_cnt * VLAN_HLEN;
1499 }
1500 }
1501
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301502 /*
1503 * the net_protocol handler will be used only in decap path
1504 * for non passthrough case.
1505 */
1506 original_cm->proto = NULL;
1507 reply_cm->proto = NULL;
1508
1509#ifdef SFE_GRE_TUN_ENABLE
1510 if ((IPPROTO_GRE == tuple->protocol) && !(reply_cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_PASSTHROUGH)) {
1511 rcu_read_lock();
1512 reply_cm->proto = rcu_dereference(inet_protos[IPPROTO_GRE]);
1513 rcu_read_unlock();
1514
1515 if (unlikely(!reply_cm->proto)) {
1516 kfree(reply_cm);
1517 kfree(original_cm);
1518 kfree(c);
1519 dev_put(src_dev);
1520 dev_put(dest_dev);
1521 DEBUG_WARN("sfe: GRE proto handler is not registered\n");
1522 return -EPERM;
1523 }
1524 }
1525#endif
1526
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001527#ifdef CONFIG_NF_FLOW_COOKIE
1528 reply_cm->flow_cookie = 0;
1529#endif
Zhi Chen8748eb32015-06-18 12:58:48 -07001530#ifdef CONFIG_XFRM
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301531 if (msg->valid_flags & SFE_RULE_CREATE_DIRECTION_VALID) {
1532 reply_cm->flow_accel = msg->direction_rule.return_accel;
1533 } else {
1534 reply_cm->flow_accel = 1;
1535 }
1536
Zhi Chen8748eb32015-06-18 12:58:48 -07001537#endif
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301538 /*
1539 * If l2_features are disabled and flow uses l2 features such as macvlan/bridge/pppoe/vlan,
1540 * bottom interfaces are expected to be disabled in the flow rule and always top interfaces
1541 * are used. In such cases, do not use HW csum offload. csum offload is used only when we
1542 * are sending directly to the destination interface that supports it.
1543 */
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301544 if (likely(src_dev->features & NETIF_F_HW_CSUM) && sfe_dev_has_hw_csum(src_dev)) {
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301545 if ((msg->conn_rule.flow_top_interface_num == msg->conn_rule.flow_interface_num) ||
1546 (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_FLOW_BOTTOM_INTERFACE)) {
1547 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_CSUM_OFFLOAD;
1548 }
1549 }
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001550
1551 /*
Ken Zhubbf49652021-09-12 15:33:09 -07001552 * For the non-arp interface, we don't write L2 HDR.
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001553 */
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +05301554 if (sfe_ipv4_xmit_eth_type_check(src_dev, reply_cm->flags)) {
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301555
1556 /*
1557 * Check whether the rule has configured a specific source MAC address to use.
1558 * This is needed when virtual L3 interfaces such as br-lan, macvlan, vlan are used during egress
1559 */
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301560
1561 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1562 ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)msg->conn_rule.return_mac);
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301563 } else {
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301564 if ((msg->valid_flags & SFE_RULE_CREATE_SRC_MAC_VALID) &&
1565 (msg->src_mac_rule.mac_valid_flags & SFE_SRC_MAC_FLOW_VALID)) {
1566 ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)msg->src_mac_rule.flow_src_mac);
1567 } else {
1568 ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)src_dev->dev_addr);
1569 }
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301570 }
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301571
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301572 ether_addr_copy((u8 *)reply_cm->xmit_dest_mac, (u8 *)msg->conn_rule.flow_mac);
1573
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001574 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_L2_HDR;
Wayne Tanbb7f1782021-12-13 11:16:04 -08001575 reply_cm->l2_hdr_size += ETH_HLEN;
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001576
1577 /*
1578 * If our dev writes Ethernet headers then we can write a really fast
1579 * version.
1580 */
1581 if (src_dev->header_ops) {
1582 if (src_dev->header_ops->create == eth_header) {
1583 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR;
1584 }
1585 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001586 }
1587
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301588 if ((tuple->return_ip != msg->conn_rule.return_ip_xlate) ||
1589 (tuple->return_ident != msg->conn_rule.return_ident_xlate)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001590 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST;
1591 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC;
1592 }
1593
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301594 if ((tuple->flow_ip != msg->conn_rule.flow_ip_xlate) ||
1595 (tuple->flow_ident != msg->conn_rule.flow_ident_xlate)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001596 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC;
1597 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST;
1598 }
1599
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001600 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001601 * Initialize the protocol-specific information that we track.
1602 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301603 switch (tuple->protocol) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001604 case IPPROTO_TCP:
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301605 original_cm->protocol_state.tcp.win_scale = msg->tcp_rule.flow_window_scale;
1606 original_cm->protocol_state.tcp.max_win = msg->tcp_rule.flow_max_window ? msg->tcp_rule.flow_max_window : 1;
1607 original_cm->protocol_state.tcp.end = msg->tcp_rule.flow_end;
1608 original_cm->protocol_state.tcp.max_end = msg->tcp_rule.flow_max_end;
1609
1610 reply_cm->protocol_state.tcp.win_scale = msg->tcp_rule.return_window_scale;
1611 reply_cm->protocol_state.tcp.max_win = msg->tcp_rule.return_max_window ? msg->tcp_rule.return_max_window : 1;
1612 reply_cm->protocol_state.tcp.end = msg->tcp_rule.return_end;
1613 reply_cm->protocol_state.tcp.max_end = msg->tcp_rule.return_max_end;
1614
1615 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_NO_SEQ_CHECK) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001616 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
1617 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
1618 }
1619 break;
1620 }
1621
Wayne Tanbb7f1782021-12-13 11:16:04 -08001622 /*
1623 * Fill in the ipv4_connection object.
1624 */
1625 c->protocol = tuple->protocol;
1626 c->src_ip = tuple->flow_ip;
1627 c->src_ip_xlate = msg->conn_rule.flow_ip_xlate;
1628 c->src_port = tuple->flow_ident;
1629 c->src_port_xlate = msg->conn_rule.flow_ident_xlate;
1630 c->original_dev = src_dev;
1631 c->original_match = original_cm;
1632 c->dest_ip = tuple->return_ip;
1633 c->dest_ip_xlate = msg->conn_rule.return_ip_xlate;
1634 c->dest_port = tuple->return_ident;
1635 c->dest_port_xlate = msg->conn_rule.return_ident_xlate;
1636 c->reply_dev = dest_dev;
1637 c->reply_match = reply_cm;
1638 c->debug_read_seq = 0;
1639 c->last_sync_jiffies = get_jiffies_64();
1640 c->removed = false;
1641
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001642 sfe_ipv4_connection_match_compute_translations(original_cm);
1643 sfe_ipv4_connection_match_compute_translations(reply_cm);
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301644 sfe_ipv4_insert_connection(si, c);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001645
1646 spin_unlock_bh(&si->lock);
1647
1648 /*
1649 * We have everything we need!
1650 */
Wayne Tanbb7f1782021-12-13 11:16:04 -08001651 DEBUG_INFO("NEW connection - p: %d\n"
1652 "original_cm: match_dev=src_dev: %s %d %pM\n"
1653 " xmit_dev=dest_dev: %s %d %pM\n"
1654 " xmit_src_mac: %pM\n"
1655 " xmit_dest_mac: %pM\n"
1656 " flags: %x l2_hdr: %u\n"
1657 "flow_ip: %pI4:%u\n"
1658 "flow_ip_xlate: %pI4:%u\n"
1659 "flow_mac: %pM\n"
1660 "reply_cm: match_dev=dest_dev: %s %d %pM\n"
1661 " xmit_dev=src_dev: %s %d %pM\n"
1662 " xmit_src_mac: %pM\n"
1663 " xmit_dest_mac: %pM\n"
1664 " flags: %x l2_hdr: %u\n"
1665 "return_ip: %pI4:%u\n"
1666 "return_ip_xlate: %pI4:%u\n"
1667 "return_mac: %pM\n"
1668 "flags: valid=%x src_mac_valid=%x\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301669 tuple->protocol,
Wayne Tanbb7f1782021-12-13 11:16:04 -08001670 original_cm->match_dev->name, original_cm->match_dev->ifindex, original_cm->match_dev->dev_addr,
1671 original_cm->xmit_dev->name, original_cm->xmit_dev->ifindex, original_cm->xmit_dev->dev_addr,
1672 original_cm->xmit_src_mac, original_cm->xmit_dest_mac, original_cm->flags, original_cm->l2_hdr_size,
1673 &tuple->flow_ip, ntohs(tuple->flow_ident),
1674 &msg->conn_rule.flow_ip_xlate, ntohs(msg->conn_rule.flow_ident_xlate),
1675 msg->conn_rule.flow_mac,
1676 reply_cm->match_dev->name, reply_cm->match_dev->ifindex, reply_cm->match_dev->dev_addr,
1677 reply_cm->xmit_dev->name, reply_cm->xmit_dev->ifindex, reply_cm->xmit_dev->dev_addr,
1678 reply_cm->xmit_src_mac, reply_cm->xmit_dest_mac, reply_cm->flags, reply_cm->l2_hdr_size,
1679 &tuple->return_ip, ntohs(tuple->return_ident),
1680 &msg->conn_rule.return_ip_xlate, ntohs(msg->conn_rule.return_ident_xlate),
1681 msg->conn_rule.return_mac,
1682 msg->valid_flags, msg->src_mac_rule.mac_valid_flags);
Nicolas Costa514fde02014-01-13 15:50:29 -06001683
1684 return 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001685}
1686
1687/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001688 * sfe_ipv4_destroy_rule()
1689 * Destroy a forwarding rule.
1690 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301691void sfe_ipv4_destroy_rule(struct sfe_ipv4_rule_destroy_msg *msg)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001692{
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001693 struct sfe_ipv4 *si = &__si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001694 struct sfe_ipv4_connection *c;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301695 bool ret;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301696 struct sfe_ipv4_5tuple *tuple = &msg->tuple;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001697
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301698 this_cpu_inc(si->stats_pcpu->connection_destroy_requests64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001699 spin_lock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001700
1701 /*
1702 * Check to see if we have a flow that matches the rule we're trying
1703 * to destroy. If there isn't then we can't destroy it.
1704 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301705 c = sfe_ipv4_find_connection(si, tuple->protocol, tuple->flow_ip, tuple->flow_ident,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301706 tuple->return_ip, tuple->return_ident);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001707 if (!c) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001708 spin_unlock_bh(&si->lock);
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301709 this_cpu_inc(si->stats_pcpu->connection_destroy_misses64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001710
1711 DEBUG_TRACE("connection does not exist - p: %d, s: %pI4:%u, d: %pI4:%u\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301712 tuple->protocol, &tuple->flow_ip, ntohs(tuple->flow_ident),
1713 &tuple->return_ip, ntohs(tuple->return_ident));
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001714 return;
1715 }
1716
1717 /*
1718 * Remove our connection details from the hash tables.
1719 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301720 ret = sfe_ipv4_remove_connection(si, c);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001721 spin_unlock_bh(&si->lock);
1722
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301723 if (ret) {
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301724 sfe_ipv4_flush_connection(si, c, SFE_SYNC_REASON_DESTROY);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301725 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001726
1727 DEBUG_INFO("connection destroyed - p: %d, s: %pI4:%u, d: %pI4:%u\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301728 tuple->protocol, &tuple->flow_ip, ntohs(tuple->flow_ident),
1729 &tuple->return_ip, ntohs(tuple->return_ident));
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001730}
1731
1732/*
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001733 * sfe_ipv4_register_sync_rule_callback()
1734 * Register a callback for rule synchronization.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001735 */
Xiaoping Fand44a5b42015-05-26 17:37:37 -07001736void sfe_ipv4_register_sync_rule_callback(sfe_sync_rule_callback_t sync_rule_callback)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001737{
1738 struct sfe_ipv4 *si = &__si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001739
1740 spin_lock_bh(&si->lock);
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001741 rcu_assign_pointer(si->sync_rule_callback, sync_rule_callback);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001742 spin_unlock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001743}
1744
1745/*
1746 * sfe_ipv4_get_debug_dev()
1747 */
1748static ssize_t sfe_ipv4_get_debug_dev(struct device *dev,
1749 struct device_attribute *attr,
1750 char *buf)
1751{
1752 struct sfe_ipv4 *si = &__si;
1753 ssize_t count;
1754 int num;
1755
1756 spin_lock_bh(&si->lock);
1757 num = si->debug_dev;
1758 spin_unlock_bh(&si->lock);
1759
1760 count = snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", num);
1761 return count;
1762}
1763
1764/*
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001765 * sysfs attributes.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001766 */
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001767static const struct device_attribute sfe_ipv4_debug_dev_attr =
Xiaoping Fane70da412016-02-26 16:47:57 -08001768 __ATTR(debug_dev, S_IWUSR | S_IRUGO, sfe_ipv4_get_debug_dev, NULL);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001769
1770/*
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001771 * sfe_ipv4_destroy_all_rules_for_dev()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001772 * Destroy all connections that match a particular device.
1773 *
1774 * If we pass dev as NULL then this destroys all connections.
1775 */
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001776void sfe_ipv4_destroy_all_rules_for_dev(struct net_device *dev)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001777{
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001778 struct sfe_ipv4 *si = &__si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001779 struct sfe_ipv4_connection *c;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301780 bool ret;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001781
Xiaoping Fan34586472015-07-03 02:20:35 -07001782another_round:
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001783 spin_lock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001784
Xiaoping Fan34586472015-07-03 02:20:35 -07001785 for (c = si->all_connections_head; c; c = c->all_connections_next) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001786 /*
Xiaoping Fan34586472015-07-03 02:20:35 -07001787 * Does this connection relate to the device we are destroying?
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001788 */
1789 if (!dev
1790 || (dev == c->original_dev)
1791 || (dev == c->reply_dev)) {
Xiaoping Fan34586472015-07-03 02:20:35 -07001792 break;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001793 }
Xiaoping Fan34586472015-07-03 02:20:35 -07001794 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001795
Xiaoping Fan34586472015-07-03 02:20:35 -07001796 if (c) {
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301797 ret = sfe_ipv4_remove_connection(si, c);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001798 }
1799
1800 spin_unlock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07001801
1802 if (c) {
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301803 if (ret) {
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301804 sfe_ipv4_flush_connection(si, c, SFE_SYNC_REASON_DESTROY);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301805 }
Xiaoping Fan34586472015-07-03 02:20:35 -07001806 goto another_round;
1807 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001808}
1809
1810/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001811 * sfe_ipv4_periodic_sync()
1812 */
Ken Zhu137722d2021-09-23 17:57:36 -07001813static void sfe_ipv4_periodic_sync(struct work_struct *work)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001814{
Ken Zhu137722d2021-09-23 17:57:36 -07001815 struct sfe_ipv4 *si = container_of((struct delayed_work *)work, struct sfe_ipv4, sync_dwork);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07001816 u64 now_jiffies;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001817 int quota;
Xiaoping Fand44a5b42015-05-26 17:37:37 -07001818 sfe_sync_rule_callback_t sync_rule_callback;
Ken Zhudc423672021-09-02 18:27:01 -07001819 struct sfe_ipv4_connection *c;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001820
1821 now_jiffies = get_jiffies_64();
1822
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001823 rcu_read_lock();
1824 sync_rule_callback = rcu_dereference(si->sync_rule_callback);
1825 if (!sync_rule_callback) {
1826 rcu_read_unlock();
1827 goto done;
1828 }
1829
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001830 spin_lock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001831
1832 /*
Ken Zhudc423672021-09-02 18:27:01 -07001833 * If we have reached the end of the connection list, walk from
1834 * the connection head.
1835 */
1836 c = si->wc_next;
1837 if (unlikely(!c)) {
1838 c = si->all_connections_head;
1839 }
1840
1841 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001842 * Get an estimate of the number of connections to parse in this sync.
1843 */
1844 quota = (si->num_connections + 63) / 64;
1845
1846 /*
Ken Zhudc423672021-09-02 18:27:01 -07001847 * Walk the "all connection" list and sync the connection state.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001848 */
Ken Zhudc423672021-09-02 18:27:01 -07001849 while (likely(c && quota)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001850 struct sfe_ipv4_connection_match *cm;
1851 struct sfe_ipv4_connection_match *counter_cm;
Xiaoping Fand44a5b42015-05-26 17:37:37 -07001852 struct sfe_connection_sync sis;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001853
Ken Zhudc423672021-09-02 18:27:01 -07001854 cm = c->original_match;
1855 counter_cm = c->reply_match;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001856
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001857 /*
Ken Zhudc423672021-09-02 18:27:01 -07001858 * Didn't receive packets in the original direction or reply
1859 * direction, move to the next connection.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001860 */
Ken Zhudc423672021-09-02 18:27:01 -07001861 if ((!atomic_read(&cm->rx_packet_count)) && !(atomic_read(&counter_cm->rx_packet_count))) {
1862 c = c->all_connections_next;
1863 continue;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001864 }
1865
Ken Zhudc423672021-09-02 18:27:01 -07001866 quota--;
Matthew McClintockaf48f1e2014-01-23 15:29:19 -06001867
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301868 sfe_ipv4_gen_sync_connection(si, c, &sis, SFE_SYNC_REASON_STATS, now_jiffies);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001869
Ken Zhudc423672021-09-02 18:27:01 -07001870 si->wc_next = c->all_connections_next;
1871
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001872 /*
1873 * We don't want to be holding the lock when we sync!
1874 */
1875 spin_unlock_bh(&si->lock);
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001876 sync_rule_callback(&sis);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001877 spin_lock_bh(&si->lock);
Ken Zhudc423672021-09-02 18:27:01 -07001878
1879 /*
1880 * c must be set and used in the same lock/unlock window;
1881 * because c could be removed when we don't hold the lock,
1882 * so delay grabbing until after the callback and relock.
1883 */
1884 c = si->wc_next;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001885 }
1886
Ken Zhudc423672021-09-02 18:27:01 -07001887 /*
1888 * At the end of the sync, put the wc_next to the connection we left.
1889 */
1890 si->wc_next = c;
1891
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001892 spin_unlock_bh(&si->lock);
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001893 rcu_read_unlock();
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001894
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001895done:
Ken Zhu137722d2021-09-23 17:57:36 -07001896 schedule_delayed_work_on(si->work_cpu, (struct delayed_work *)work, ((HZ + 99) / 100));
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001897}
1898
1899#define CHAR_DEV_MSG_SIZE 768
1900
1901/*
1902 * sfe_ipv4_debug_dev_read_start()
1903 * Generate part of the XML output.
1904 */
1905static bool sfe_ipv4_debug_dev_read_start(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
1906 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
1907{
1908 int bytes_read;
1909
Xiaoping Fan34586472015-07-03 02:20:35 -07001910 si->debug_read_seq++;
1911
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001912 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "<sfe_ipv4>\n");
1913 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1914 return false;
1915 }
1916
1917 *length -= bytes_read;
1918 *total_read += bytes_read;
1919
1920 ws->state++;
1921 return true;
1922}
1923
1924/*
1925 * sfe_ipv4_debug_dev_read_connections_start()
1926 * Generate part of the XML output.
1927 */
1928static bool sfe_ipv4_debug_dev_read_connections_start(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
1929 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
1930{
1931 int bytes_read;
1932
1933 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<connections>\n");
1934 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1935 return false;
1936 }
1937
1938 *length -= bytes_read;
1939 *total_read += bytes_read;
1940
1941 ws->state++;
1942 return true;
1943}
1944
1945/*
1946 * sfe_ipv4_debug_dev_read_connections_connection()
1947 * Generate part of the XML output.
1948 */
1949static bool sfe_ipv4_debug_dev_read_connections_connection(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
1950 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
1951{
1952 struct sfe_ipv4_connection *c;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001953 struct sfe_ipv4_connection_match *original_cm;
1954 struct sfe_ipv4_connection_match *reply_cm;
1955 int bytes_read;
1956 int protocol;
1957 struct net_device *src_dev;
Dave Hudson87973cd2013-10-22 16:00:04 +01001958 __be32 src_ip;
1959 __be32 src_ip_xlate;
1960 __be16 src_port;
1961 __be16 src_port_xlate;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07001962 u64 src_rx_packets;
1963 u64 src_rx_bytes;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001964 struct net_device *dest_dev;
Dave Hudson87973cd2013-10-22 16:00:04 +01001965 __be32 dest_ip;
1966 __be32 dest_ip_xlate;
1967 __be16 dest_port;
1968 __be16 dest_port_xlate;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07001969 u64 dest_rx_packets;
1970 u64 dest_rx_bytes;
1971 u64 last_sync_jiffies;
Ken Zhu37040ea2021-09-09 21:11:15 -07001972 u32 src_mark, dest_mark, src_priority, dest_priority, src_dscp, dest_dscp;
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301973 u32 packet, byte, original_cm_flags;
1974 u16 pppoe_session_id;
1975 u8 pppoe_remote_mac[ETH_ALEN];
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001976#ifdef CONFIG_NF_FLOW_COOKIE
1977 int src_flow_cookie, dst_flow_cookie;
1978#endif
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001979
1980 spin_lock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07001981
1982 for (c = si->all_connections_head; c; c = c->all_connections_next) {
1983 if (c->debug_read_seq < si->debug_read_seq) {
1984 c->debug_read_seq = si->debug_read_seq;
1985 break;
1986 }
1987 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001988
1989 /*
Xiaoping Fan34586472015-07-03 02:20:35 -07001990 * If there were no connections then move to the next state.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001991 */
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301992 if (!c || c->removed) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001993 spin_unlock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07001994 ws->state++;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001995 return true;
1996 }
1997
1998 original_cm = c->original_match;
1999 reply_cm = c->reply_match;
2000
2001 protocol = c->protocol;
2002 src_dev = c->original_dev;
2003 src_ip = c->src_ip;
2004 src_ip_xlate = c->src_ip_xlate;
2005 src_port = c->src_port;
2006 src_port_xlate = c->src_port_xlate;
Xiaoping Fane1963d42015-08-25 17:06:19 -07002007 src_priority = original_cm->priority;
2008 src_dscp = original_cm->dscp >> SFE_IPV4_DSCP_SHIFT;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002009
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05302010 sfe_ipv4_connection_match_update_summary_stats(original_cm, &packet, &byte);
2011 sfe_ipv4_connection_match_update_summary_stats(reply_cm, &packet, &byte);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002012
2013 src_rx_packets = original_cm->rx_packet_count64;
2014 src_rx_bytes = original_cm->rx_byte_count64;
Ken Zhu37040ea2021-09-09 21:11:15 -07002015 src_mark = original_cm->mark;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002016 dest_dev = c->reply_dev;
2017 dest_ip = c->dest_ip;
2018 dest_ip_xlate = c->dest_ip_xlate;
2019 dest_port = c->dest_port;
2020 dest_port_xlate = c->dest_port_xlate;
Xiaoping Fane1963d42015-08-25 17:06:19 -07002021 dest_priority = reply_cm->priority;
2022 dest_dscp = reply_cm->dscp >> SFE_IPV4_DSCP_SHIFT;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002023 dest_rx_packets = reply_cm->rx_packet_count64;
2024 dest_rx_bytes = reply_cm->rx_byte_count64;
Ken Zhu37040ea2021-09-09 21:11:15 -07002025 dest_mark = reply_cm->mark;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002026 last_sync_jiffies = get_jiffies_64() - c->last_sync_jiffies;
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05302027 original_cm_flags = original_cm->flags;
2028 pppoe_session_id = original_cm->pppoe_session_id;
2029 ether_addr_copy(pppoe_remote_mac, original_cm->pppoe_remote_mac);
2030
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08002031#ifdef CONFIG_NF_FLOW_COOKIE
2032 src_flow_cookie = original_cm->flow_cookie;
2033 dst_flow_cookie = reply_cm->flow_cookie;
2034#endif
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002035 spin_unlock_bh(&si->lock);
2036
2037 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t\t<connection "
2038 "protocol=\"%u\" "
2039 "src_dev=\"%s\" "
2040 "src_ip=\"%pI4\" src_ip_xlate=\"%pI4\" "
2041 "src_port=\"%u\" src_port_xlate=\"%u\" "
Xiaoping Fane1963d42015-08-25 17:06:19 -07002042 "src_priority=\"%u\" src_dscp=\"%u\" "
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002043 "src_rx_pkts=\"%llu\" src_rx_bytes=\"%llu\" "
Ken Zhu37040ea2021-09-09 21:11:15 -07002044 "src_mark=\"%08x\" "
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002045 "dest_dev=\"%s\" "
2046 "dest_ip=\"%pI4\" dest_ip_xlate=\"%pI4\" "
2047 "dest_port=\"%u\" dest_port_xlate=\"%u\" "
Xiaoping Fane1963d42015-08-25 17:06:19 -07002048 "dest_priority=\"%u\" dest_dscp=\"%u\" "
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002049 "dest_rx_pkts=\"%llu\" dest_rx_bytes=\"%llu\" "
Ken Zhu37040ea2021-09-09 21:11:15 -07002050 "dest_mark=\"%08x\" "
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08002051#ifdef CONFIG_NF_FLOW_COOKIE
2052 "src_flow_cookie=\"%d\" dst_flow_cookie=\"%d\" "
2053#endif
Ken Zhu37040ea2021-09-09 21:11:15 -07002054 "last_sync=\"%llu\" ",
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002055 protocol,
2056 src_dev->name,
2057 &src_ip, &src_ip_xlate,
Dave Hudson87973cd2013-10-22 16:00:04 +01002058 ntohs(src_port), ntohs(src_port_xlate),
Xiaoping Fane1963d42015-08-25 17:06:19 -07002059 src_priority, src_dscp,
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002060 src_rx_packets, src_rx_bytes,
Ken Zhu37040ea2021-09-09 21:11:15 -07002061 src_mark,
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002062 dest_dev->name,
2063 &dest_ip, &dest_ip_xlate,
Dave Hudson87973cd2013-10-22 16:00:04 +01002064 ntohs(dest_port), ntohs(dest_port_xlate),
Xiaoping Fane1963d42015-08-25 17:06:19 -07002065 dest_priority, dest_dscp,
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002066 dest_rx_packets, dest_rx_bytes,
Ken Zhu37040ea2021-09-09 21:11:15 -07002067 dest_mark,
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08002068#ifdef CONFIG_NF_FLOW_COOKIE
2069 src_flow_cookie, dst_flow_cookie,
2070#endif
Ken Zhu37040ea2021-09-09 21:11:15 -07002071 last_sync_jiffies);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002072
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05302073 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 +05302074 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 +05302075 pppoe_session_id, pppoe_remote_mac);
2076 }
2077
2078 bytes_read += snprintf(msg + bytes_read, CHAR_DEV_MSG_SIZE, "/>\n");
2079
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002080 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2081 return false;
2082 }
2083
2084 *length -= bytes_read;
2085 *total_read += bytes_read;
2086
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002087 return true;
2088}
2089
2090/*
2091 * sfe_ipv4_debug_dev_read_connections_end()
2092 * Generate part of the XML output.
2093 */
2094static bool sfe_ipv4_debug_dev_read_connections_end(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2095 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2096{
2097 int bytes_read;
2098
2099 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t</connections>\n");
2100 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2101 return false;
2102 }
2103
2104 *length -= bytes_read;
2105 *total_read += bytes_read;
2106
2107 ws->state++;
2108 return true;
2109}
2110
2111/*
2112 * sfe_ipv4_debug_dev_read_exceptions_start()
2113 * Generate part of the XML output.
2114 */
2115static bool sfe_ipv4_debug_dev_read_exceptions_start(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2116 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2117{
2118 int bytes_read;
2119
2120 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<exceptions>\n");
2121 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2122 return false;
2123 }
2124
2125 *length -= bytes_read;
2126 *total_read += bytes_read;
2127
2128 ws->state++;
2129 return true;
2130}
2131
2132/*
2133 * sfe_ipv4_debug_dev_read_exceptions_exception()
2134 * Generate part of the XML output.
2135 */
2136static bool sfe_ipv4_debug_dev_read_exceptions_exception(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2137 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2138{
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302139 int i;
2140 u64 val = 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002141
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302142 for_each_possible_cpu(i) {
2143 const struct sfe_ipv4_stats *s = per_cpu_ptr(si->stats_pcpu, i);
2144 val += s->exception_events64[ws->iter_exception];
2145 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002146
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302147 if (val) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002148 int bytes_read;
2149
2150 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE,
2151 "\t\t<exception name=\"%s\" count=\"%llu\" />\n",
2152 sfe_ipv4_exception_events_string[ws->iter_exception],
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302153 val);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002154 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2155 return false;
2156 }
2157
2158 *length -= bytes_read;
2159 *total_read += bytes_read;
2160 }
2161
2162 ws->iter_exception++;
2163 if (ws->iter_exception >= SFE_IPV4_EXCEPTION_EVENT_LAST) {
2164 ws->iter_exception = 0;
2165 ws->state++;
2166 }
2167
2168 return true;
2169}
2170
2171/*
2172 * sfe_ipv4_debug_dev_read_exceptions_end()
2173 * Generate part of the XML output.
2174 */
2175static bool sfe_ipv4_debug_dev_read_exceptions_end(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2176 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2177{
2178 int bytes_read;
2179
2180 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t</exceptions>\n");
2181 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2182 return false;
2183 }
2184
2185 *length -= bytes_read;
2186 *total_read += bytes_read;
2187
2188 ws->state++;
2189 return true;
2190}
2191
2192/*
2193 * sfe_ipv4_debug_dev_read_stats()
2194 * Generate part of the XML output.
2195 */
2196static bool sfe_ipv4_debug_dev_read_stats(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2197 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2198{
2199 int bytes_read;
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302200 struct sfe_ipv4_stats stats;
2201 unsigned int num_conn;
2202
2203 sfe_ipv4_update_summary_stats(si, &stats);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002204
2205 spin_lock_bh(&si->lock);
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302206 num_conn = si->num_connections;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002207 spin_unlock_bh(&si->lock);
2208
2209 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<stats "
2210 "num_connections=\"%u\" "
Amitesh Anand63be37d2021-12-24 20:51:48 +05302211 "pkts_dropped=\"%llu\" "
Xiaoping Fan59176422015-05-22 15:58:10 -07002212 "pkts_forwarded=\"%llu\" pkts_not_forwarded=\"%llu\" "
2213 "create_requests=\"%llu\" create_collisions=\"%llu\" "
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05302214 "create_failures=\"%llu\" "
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002215 "destroy_requests=\"%llu\" destroy_misses=\"%llu\" "
2216 "flushes=\"%llu\" "
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +05302217 "hash_hits=\"%llu\" hash_reorders=\"%llu\" "
2218 "pppoe_encap_pkts_fwded=\"%llu\" "
Guduri Prathyusha034d6352022-01-12 16:49:04 +05302219 "pppoe_decap_pkts_fwded=\"%llu\" "
2220 "pppoe_bridge_pkts_fwded=\"%llu\" />\n",
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302221 num_conn,
Amitesh Anand63be37d2021-12-24 20:51:48 +05302222 stats.packets_dropped64,
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302223 stats.packets_forwarded64,
2224 stats.packets_not_forwarded64,
2225 stats.connection_create_requests64,
2226 stats.connection_create_collisions64,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05302227 stats.connection_create_failures64,
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302228 stats.connection_destroy_requests64,
2229 stats.connection_destroy_misses64,
2230 stats.connection_flushes64,
2231 stats.connection_match_hash_hits64,
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +05302232 stats.connection_match_hash_reorders64,
2233 stats.pppoe_encap_packets_forwarded64,
Guduri Prathyusha034d6352022-01-12 16:49:04 +05302234 stats.pppoe_decap_packets_forwarded64,
2235 stats.pppoe_bridge_packets_forwarded64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002236 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2237 return false;
2238 }
2239
2240 *length -= bytes_read;
2241 *total_read += bytes_read;
2242
2243 ws->state++;
2244 return true;
2245}
2246
2247/*
2248 * sfe_ipv4_debug_dev_read_end()
2249 * Generate part of the XML output.
2250 */
2251static bool sfe_ipv4_debug_dev_read_end(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2252 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2253{
2254 int bytes_read;
2255
2256 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "</sfe_ipv4>\n");
2257 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2258 return false;
2259 }
2260
2261 *length -= bytes_read;
2262 *total_read += bytes_read;
2263
2264 ws->state++;
2265 return true;
2266}
2267
2268/*
2269 * Array of write functions that write various XML elements that correspond to
2270 * our XML output state machine.
2271 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07002272static 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 +01002273 sfe_ipv4_debug_dev_read_start,
2274 sfe_ipv4_debug_dev_read_connections_start,
2275 sfe_ipv4_debug_dev_read_connections_connection,
2276 sfe_ipv4_debug_dev_read_connections_end,
2277 sfe_ipv4_debug_dev_read_exceptions_start,
2278 sfe_ipv4_debug_dev_read_exceptions_exception,
2279 sfe_ipv4_debug_dev_read_exceptions_end,
2280 sfe_ipv4_debug_dev_read_stats,
2281 sfe_ipv4_debug_dev_read_end,
2282};
2283
2284/*
2285 * sfe_ipv4_debug_dev_read()
2286 * Send info to userspace upon read request from user
2287 */
2288static ssize_t sfe_ipv4_debug_dev_read(struct file *filp, char *buffer, size_t length, loff_t *offset)
2289{
2290 char msg[CHAR_DEV_MSG_SIZE];
2291 int total_read = 0;
2292 struct sfe_ipv4_debug_xml_write_state *ws;
2293 struct sfe_ipv4 *si = &__si;
2294
2295 ws = (struct sfe_ipv4_debug_xml_write_state *)filp->private_data;
2296 while ((ws->state != SFE_IPV4_DEBUG_XML_STATE_DONE) && (length > CHAR_DEV_MSG_SIZE)) {
2297 if ((sfe_ipv4_debug_xml_write_methods[ws->state])(si, buffer, msg, &length, &total_read, ws)) {
2298 continue;
2299 }
2300 }
2301
2302 return total_read;
2303}
2304
2305/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002306 * sfe_ipv4_debug_dev_open()
2307 */
2308static int sfe_ipv4_debug_dev_open(struct inode *inode, struct file *file)
2309{
2310 struct sfe_ipv4_debug_xml_write_state *ws;
2311
2312 ws = (struct sfe_ipv4_debug_xml_write_state *)file->private_data;
2313 if (!ws) {
2314 ws = kzalloc(sizeof(struct sfe_ipv4_debug_xml_write_state), GFP_KERNEL);
2315 if (!ws) {
2316 return -ENOMEM;
2317 }
2318
2319 ws->state = SFE_IPV4_DEBUG_XML_STATE_START;
2320 file->private_data = ws;
2321 }
2322
2323 return 0;
2324}
2325
2326/*
2327 * sfe_ipv4_debug_dev_release()
2328 */
2329static int sfe_ipv4_debug_dev_release(struct inode *inode, struct file *file)
2330{
2331 struct sfe_ipv4_debug_xml_write_state *ws;
2332
2333 ws = (struct sfe_ipv4_debug_xml_write_state *)file->private_data;
2334 if (ws) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002335 /*
2336 * We've finished with our output so free the write state.
2337 */
2338 kfree(ws);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05302339 file->private_data = NULL;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002340 }
2341
2342 return 0;
2343}
2344
2345/*
2346 * File operations used in the debug char device
2347 */
2348static struct file_operations sfe_ipv4_debug_dev_fops = {
2349 .read = sfe_ipv4_debug_dev_read,
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002350 .open = sfe_ipv4_debug_dev_open,
2351 .release = sfe_ipv4_debug_dev_release
2352};
2353
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08002354#ifdef CONFIG_NF_FLOW_COOKIE
2355/*
2356 * sfe_register_flow_cookie_cb
2357 * register a function in SFE to let SFE use this function to configure flow cookie for a flow
2358 *
2359 * Hardware driver which support flow cookie should register a callback function in SFE. Then SFE
2360 * can use this function to configure flow cookie for a flow.
2361 * return: 0, success; !=0, fail
2362 */
2363int sfe_register_flow_cookie_cb(flow_cookie_set_func_t cb)
2364{
2365 struct sfe_ipv4 *si = &__si;
2366
2367 BUG_ON(!cb);
2368
2369 if (si->flow_cookie_set_func) {
2370 return -1;
2371 }
2372
2373 rcu_assign_pointer(si->flow_cookie_set_func, cb);
2374 return 0;
2375}
2376
2377/*
2378 * sfe_unregister_flow_cookie_cb
2379 * unregister function which is used to configure flow cookie for a flow
2380 *
2381 * return: 0, success; !=0, fail
2382 */
2383int sfe_unregister_flow_cookie_cb(flow_cookie_set_func_t cb)
2384{
2385 struct sfe_ipv4 *si = &__si;
2386
2387 RCU_INIT_POINTER(si->flow_cookie_set_func, NULL);
2388 return 0;
2389}
Xiaoping Fan640faf42015-08-28 15:50:55 -07002390
2391/*
2392 * sfe_ipv4_get_flow_cookie()
2393 */
2394static ssize_t sfe_ipv4_get_flow_cookie(struct device *dev,
2395 struct device_attribute *attr,
2396 char *buf)
2397{
2398 struct sfe_ipv4 *si = &__si;
Xiaoping Fan01c67cc2015-11-09 11:31:57 -08002399 return snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", si->flow_cookie_enable);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002400}
2401
2402/*
2403 * sfe_ipv4_set_flow_cookie()
2404 */
2405static ssize_t sfe_ipv4_set_flow_cookie(struct device *dev,
2406 struct device_attribute *attr,
2407 const char *buf, size_t size)
2408{
2409 struct sfe_ipv4 *si = &__si;
Ken Zhu137722d2021-09-23 17:57:36 -07002410 si->flow_cookie_enable = simple_strtol(buf, NULL, 0);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002411
2412 return size;
2413}
2414
2415/*
2416 * sysfs attributes.
2417 */
2418static const struct device_attribute sfe_ipv4_flow_cookie_attr =
Xiaoping Fane70da412016-02-26 16:47:57 -08002419 __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 -08002420#endif /*CONFIG_NF_FLOW_COOKIE*/
2421
Ken Zhu137722d2021-09-23 17:57:36 -07002422/*
2423 * sfe_ipv4_get_cpu()
2424 */
2425static ssize_t sfe_ipv4_get_cpu(struct device *dev,
2426 struct device_attribute *attr,
2427 char *buf)
2428{
2429 struct sfe_ipv4 *si = &__si;
2430 return snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", si->work_cpu);
2431}
2432
2433/*
2434 * sfe_ipv4_set_cpu()
2435 */
2436static ssize_t sfe_ipv4_set_cpu(struct device *dev,
2437 struct device_attribute *attr,
2438 const char *buf, size_t size)
2439{
2440 struct sfe_ipv4 *si = &__si;
2441 int work_cpu;
2442 work_cpu = simple_strtol(buf, NULL, 0);
2443 if ((work_cpu >= 0) && (work_cpu <= NR_CPUS)) {
2444 si->work_cpu = work_cpu;
2445 } else {
2446 dev_err(dev, "%s is not in valid range[0,%d]", buf, NR_CPUS);
2447 }
2448 return size;
2449}
2450/*
2451 * sysfs attributes.
2452 */
2453static const struct device_attribute sfe_ipv4_cpu_attr =
2454 __ATTR(stats_work_cpu, S_IWUSR | S_IRUGO, sfe_ipv4_get_cpu, sfe_ipv4_set_cpu);
2455
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05302456 /*
2457 * sfe_ipv4_conn_match_hash_init()
2458 * Initialize conn match hash lists
2459 */
2460static void sfe_ipv4_conn_match_hash_init(struct sfe_ipv4 *si, int len)
2461{
2462 struct hlist_head *hash_list = si->hlist_conn_match_hash_head;
2463 int i;
2464
2465 for (i = 0; i < len; i++) {
2466 INIT_HLIST_HEAD(&hash_list[i]);
2467 }
2468}
2469
Amitesh Anand63be37d2021-12-24 20:51:48 +05302470#ifdef SFE_PROCESS_LOCAL_OUT
2471/*
2472 * sfe_ipv4_local_out()
2473 * Called for packets from ip_local_out() - post encapsulation & other packets
2474 */
2475static unsigned int sfe_ipv4_local_out(void *priv, struct sk_buff *skb, const struct nf_hook_state *nhs)
2476{
Nitin Shettyc28f8172022-02-04 16:23:46 +05302477 struct sfe_l2_info l2_info = {0};
Guduri Prathyusha5f27e232022-01-06 14:39:04 +05302478
Amitesh Anand63be37d2021-12-24 20:51:48 +05302479 DEBUG_TRACE("%px: sfe: sfe_ipv4_local_out hook called.\n", skb);
2480
2481 if (likely(skb->skb_iif)) {
Guduri Prathyusha5f27e232022-01-06 14:39:04 +05302482 return sfe_ipv4_recv(skb->dev, skb, &l2_info, true) ? NF_STOLEN : NF_ACCEPT;
Amitesh Anand63be37d2021-12-24 20:51:48 +05302483 }
2484
2485 return NF_ACCEPT;
2486}
2487
2488/*
2489 * struct nf_hook_ops sfe_ipv4_ops_local_out[]
2490 * Hooks into netfilter local out packet monitoring points.
2491 */
2492static struct nf_hook_ops sfe_ipv4_ops_local_out[] __read_mostly = {
2493
2494 /*
2495 * Local out routing hook is used to monitor packets.
2496 */
2497 {
2498 .hook = sfe_ipv4_local_out,
2499 .pf = PF_INET,
2500 .hooknum = NF_INET_LOCAL_OUT,
2501 .priority = NF_IP_PRI_FIRST,
2502 },
2503};
2504#endif
2505
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002506/*
Dave Hudson87973cd2013-10-22 16:00:04 +01002507 * sfe_ipv4_init()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002508 */
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05302509int sfe_ipv4_init(void)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002510{
2511 struct sfe_ipv4 *si = &__si;
2512 int result = -1;
2513
Dave Hudsondcd08fb2013-11-22 09:25:16 -06002514 DEBUG_INFO("SFE IPv4 init\n");
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002515
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05302516 sfe_ipv4_conn_match_hash_init(si, ARRAY_SIZE(si->hlist_conn_match_hash_head));
2517
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302518 si->stats_pcpu = alloc_percpu_gfp(struct sfe_ipv4_stats, GFP_KERNEL | __GFP_ZERO);
2519 if (!si->stats_pcpu) {
2520 DEBUG_ERROR("failed to allocate stats memory for sfe_ipv4\n");
2521 goto exit0;
2522 }
2523
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002524 /*
2525 * Create sys/sfe_ipv4
2526 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302527 si->sys_ipv4 = kobject_create_and_add("sfe_ipv4", NULL);
2528 if (!si->sys_ipv4) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002529 DEBUG_ERROR("failed to register sfe_ipv4\n");
2530 goto exit1;
2531 }
2532
2533 /*
2534 * Create files, one for each parameter supported by this module.
2535 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302536 result = sysfs_create_file(si->sys_ipv4, &sfe_ipv4_debug_dev_attr.attr);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002537 if (result) {
2538 DEBUG_ERROR("failed to register debug dev file: %d\n", result);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002539 goto exit2;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002540 }
2541
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302542 result = sysfs_create_file(si->sys_ipv4, &sfe_ipv4_cpu_attr.attr);
Ken Zhu137722d2021-09-23 17:57:36 -07002543 if (result) {
2544 DEBUG_ERROR("failed to register debug dev file: %d\n", result);
2545 goto exit3;
2546 }
2547
Xiaoping Fan640faf42015-08-28 15:50:55 -07002548#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302549 result = sysfs_create_file(si->sys_ipv4, &sfe_ipv4_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002550 if (result) {
2551 DEBUG_ERROR("failed to register flow cookie enable file: %d\n", result);
Ken Zhu137722d2021-09-23 17:57:36 -07002552 goto exit4;
Xiaoping Fan640faf42015-08-28 15:50:55 -07002553 }
2554#endif /* CONFIG_NF_FLOW_COOKIE */
2555
Amitesh Anand63be37d2021-12-24 20:51:48 +05302556#ifdef SFE_PROCESS_LOCAL_OUT
2557#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
2558 result = nf_register_hooks(sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2559#else
2560 result = nf_register_net_hooks(&init_net, sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2561#endif
2562 if (result < 0) {
2563 DEBUG_ERROR("can't register nf local out hook: %d\n", result);
2564 goto exit5;
2565 }
2566 DEBUG_INFO("Register nf local out hook success: %d\n", result);
2567#endif
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002568 /*
2569 * Register our debug char device.
2570 */
2571 result = register_chrdev(0, "sfe_ipv4", &sfe_ipv4_debug_dev_fops);
2572 if (result < 0) {
2573 DEBUG_ERROR("Failed to register chrdev: %d\n", result);
Amitesh Anand63be37d2021-12-24 20:51:48 +05302574 goto exit6;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002575 }
2576
2577 si->debug_dev = result;
Ken Zhu137722d2021-09-23 17:57:36 -07002578 si->work_cpu = WORK_CPU_UNBOUND;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002579
2580 /*
Ken Zhu137722d2021-09-23 17:57:36 -07002581 * Create a work to handle periodic statistics.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002582 */
Ken Zhu137722d2021-09-23 17:57:36 -07002583 INIT_DELAYED_WORK(&(si->sync_dwork), sfe_ipv4_periodic_sync);
2584 schedule_delayed_work_on(si->work_cpu, &(si->sync_dwork), ((HZ + 99) / 100));
2585
Dave Hudson87973cd2013-10-22 16:00:04 +01002586 spin_lock_init(&si->lock);
Dave Hudson87973cd2013-10-22 16:00:04 +01002587 return 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002588
Amitesh Anand63be37d2021-12-24 20:51:48 +05302589exit6:
2590#ifdef SFE_PROCESS_LOCAL_OUT
2591 DEBUG_TRACE("sfe: Unregister local out hook\n");
2592#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
2593 nf_unregister_hooks(sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2594#else
2595 nf_unregister_net_hooks(&init_net, sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2596#endif
Ken Zhu137722d2021-09-23 17:57:36 -07002597exit5:
Amitesh Anand63be37d2021-12-24 20:51:48 +05302598#endif
Xiaoping Fan640faf42015-08-28 15:50:55 -07002599#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302600 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002601
Ken Zhu137722d2021-09-23 17:57:36 -07002602exit4:
Xiaoping Fan640faf42015-08-28 15:50:55 -07002603#endif /* CONFIG_NF_FLOW_COOKIE */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302604 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_cpu_attr.attr);
Ken Zhu137722d2021-09-23 17:57:36 -07002605exit3:
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302606 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_debug_dev_attr.attr);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002607
Xiaoping Fan640faf42015-08-28 15:50:55 -07002608exit2:
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302609 kobject_put(si->sys_ipv4);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002610
2611exit1:
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302612 free_percpu(si->stats_pcpu);
2613
2614exit0:
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002615 return result;
2616}
2617
2618/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002619 * sfe_ipv4_exit()
2620 */
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05302621void sfe_ipv4_exit(void)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002622{
Dave Hudson87973cd2013-10-22 16:00:04 +01002623 struct sfe_ipv4 *si = &__si;
2624
Dave Hudsondcd08fb2013-11-22 09:25:16 -06002625 DEBUG_INFO("SFE IPv4 exit\n");
Dave Hudson87973cd2013-10-22 16:00:04 +01002626 /*
2627 * Destroy all connections.
2628 */
Dave Hudsondcd08fb2013-11-22 09:25:16 -06002629 sfe_ipv4_destroy_all_rules_for_dev(NULL);
Dave Hudson87973cd2013-10-22 16:00:04 +01002630
Ken Zhu137722d2021-09-23 17:57:36 -07002631 cancel_delayed_work_sync(&si->sync_dwork);
Dave Hudson87973cd2013-10-22 16:00:04 +01002632
Dave Hudson87973cd2013-10-22 16:00:04 +01002633 unregister_chrdev(si->debug_dev, "sfe_ipv4");
2634
Amitesh Anand63be37d2021-12-24 20:51:48 +05302635#ifdef SFE_PROCESS_LOCAL_OUT
2636 DEBUG_TRACE("sfe: Unregister local out hook\n");
2637#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
2638 nf_unregister_hooks(sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2639#else
2640 nf_unregister_net_hooks(&init_net, sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2641#endif
2642#endif
2643
Xiaoping Fan640faf42015-08-28 15:50:55 -07002644#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302645 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002646#endif /* CONFIG_NF_FLOW_COOKIE */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302647 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_debug_dev_attr.attr);
2648 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_cpu_attr.attr);
Dave Hudson87973cd2013-10-22 16:00:04 +01002649
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302650 kobject_put(si->sys_ipv4);
Dave Hudson87973cd2013-10-22 16:00:04 +01002651
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302652 free_percpu(si->stats_pcpu);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002653}
2654
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08002655#ifdef CONFIG_NF_FLOW_COOKIE
2656EXPORT_SYMBOL(sfe_register_flow_cookie_cb);
2657EXPORT_SYMBOL(sfe_unregister_flow_cookie_cb);
2658#endif