blob: 54a3229a5bc0b2684b6ea1ecb2e86d569a85f541 [file] [log] [blame]
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001/*
2 * sfe_ipv4.c
3 * Shortcut forwarding engine - IPv4 edition.
4 *
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05305 * Copyright (c) 2013-2016, 2019-2020, The Linux Foundation. All rights reserved.
Guduri Prathyusha5f27e232022-01-06 14:39:04 +05306 * Copyright (c) 2021-2022 Qualcomm Innovation Center, Inc. All rights reserved.
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05307 *
8 * Permission to use, copy, modify, and/or distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
11 *
Xiaoping Fana42c68b2015-08-07 18:00:39 -070012 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +053017 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010019 */
Matthew McClintocka3221942014-01-16 11:44:26 -060020
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010021#include <linux/module.h>
Dave Hudsondcd08fb2013-11-22 09:25:16 -060022#include <linux/sysfs.h>
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010023#include <linux/skbuff.h>
24#include <linux/icmp.h>
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010025#include <net/tcp.h>
Amitesh Anand63be37d2021-12-24 20:51:48 +053026#include <net/udp.h>
27#include <net/vxlan.h>
Dave Hudsondcd08fb2013-11-22 09:25:16 -060028#include <linux/etherdevice.h>
Tian Yang45f39c82020-10-06 14:07:47 -070029#include <linux/version.h>
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +053030#include <linux/lockdep.h>
Amitesh Anand63be37d2021-12-24 20:51:48 +053031#include <linux/refcount.h>
32#include <linux/netfilter.h>
33#include <linux/inetdevice.h>
34#include <linux/netfilter_ipv4.h>
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010035
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +053036#include "sfe_debug.h"
Ratheesh Kannoth89302a72021-10-20 08:10:37 +053037#include "sfe_api.h"
Dave Hudsondcd08fb2013-11-22 09:25:16 -060038#include "sfe.h"
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +053039#include "sfe_flow_cookie.h"
40#include "sfe_ipv4.h"
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +053041#include "sfe_ipv4_udp.h"
42#include "sfe_ipv4_tcp.h"
43#include "sfe_ipv4_icmp.h"
Wayne Tanbb7f1782021-12-13 11:16:04 -080044#include "sfe_pppoe.h"
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010045
46static char *sfe_ipv4_exception_events_string[SFE_IPV4_EXCEPTION_EVENT_LAST] = {
47 "UDP_HEADER_INCOMPLETE",
48 "UDP_NO_CONNECTION",
49 "UDP_IP_OPTIONS_OR_INITIAL_FRAGMENT",
50 "UDP_SMALL_TTL",
51 "UDP_NEEDS_FRAGMENTATION",
52 "TCP_HEADER_INCOMPLETE",
53 "TCP_NO_CONNECTION_SLOW_FLAGS",
54 "TCP_NO_CONNECTION_FAST_FLAGS",
55 "TCP_IP_OPTIONS_OR_INITIAL_FRAGMENT",
56 "TCP_SMALL_TTL",
57 "TCP_NEEDS_FRAGMENTATION",
58 "TCP_FLAGS",
59 "TCP_SEQ_EXCEEDS_RIGHT_EDGE",
60 "TCP_SMALL_DATA_OFFS",
61 "TCP_BAD_SACK",
62 "TCP_BIG_DATA_OFFS",
63 "TCP_SEQ_BEFORE_LEFT_EDGE",
64 "TCP_ACK_EXCEEDS_RIGHT_EDGE",
65 "TCP_ACK_BEFORE_LEFT_EDGE",
66 "ICMP_HEADER_INCOMPLETE",
67 "ICMP_UNHANDLED_TYPE",
68 "ICMP_IPV4_HEADER_INCOMPLETE",
69 "ICMP_IPV4_NON_V4",
70 "ICMP_IPV4_IP_OPTIONS_INCOMPLETE",
71 "ICMP_IPV4_UDP_HEADER_INCOMPLETE",
72 "ICMP_IPV4_TCP_HEADER_INCOMPLETE",
73 "ICMP_IPV4_UNHANDLED_PROTOCOL",
74 "ICMP_NO_CONNECTION",
75 "ICMP_FLUSHED_CONNECTION",
76 "HEADER_INCOMPLETE",
Ratheesh Kannoth43d64f82021-10-20 08:23:29 +053077 "HEADER_CSUM_BAD",
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010078 "BAD_TOTAL_LENGTH",
79 "NON_V4",
80 "NON_INITIAL_FRAGMENT",
81 "DATAGRAM_INCOMPLETE",
82 "IP_OPTIONS_INCOMPLETE",
Ratheesh Kannoth5dee3772022-01-18 11:27:14 +053083 "UNHANDLED_PROTOCOL",
84 "INVALID_SOURCE_INTERFACE",
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010085};
86
Xiaoping Fan6a1672f2016-08-17 19:58:12 -070087static struct sfe_ipv4 __si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010088
89/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010090 * sfe_ipv4_gen_ip_csum()
91 * Generate the IP checksum for an IPv4 header.
92 *
93 * Note that this function assumes that we have only 20 bytes of IP header.
94 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +053095u16 sfe_ipv4_gen_ip_csum(struct iphdr *iph)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010096{
Xiaoping Fan6a1672f2016-08-17 19:58:12 -070097 u32 sum;
98 u16 *i = (u16 *)iph;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010099
100 iph->check = 0;
101
102 /*
103 * Generate the sum.
104 */
105 sum = i[0] + i[1] + i[2] + i[3] + i[4] + i[5] + i[6] + i[7] + i[8] + i[9];
106
107 /*
108 * Fold it to ones-complement form.
109 */
110 sum = (sum & 0xffff) + (sum >> 16);
111 sum = (sum & 0xffff) + (sum >> 16);
112
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700113 return (u16)sum ^ 0xffff;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100114}
115
116/*
117 * sfe_ipv4_get_connection_match_hash()
118 * Generate the hash used in connection match lookups.
119 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700120static inline unsigned int sfe_ipv4_get_connection_match_hash(struct net_device *dev, u8 protocol,
Dave Hudson87973cd2013-10-22 16:00:04 +0100121 __be32 src_ip, __be16 src_port,
122 __be32 dest_ip, __be16 dest_port)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100123{
Ratheesh Kannoth5dee3772022-01-18 11:27:14 +0530124 u32 hash = ntohl(src_ip ^ dest_ip) ^ protocol ^ ntohs(src_port ^ dest_port);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100125 return ((hash >> SFE_IPV4_CONNECTION_HASH_SHIFT) ^ hash) & SFE_IPV4_CONNECTION_HASH_MASK;
126}
127
128/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530129 * sfe_ipv4_find_connection_match_rcu()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100130 * Get the IPv4 flow match info that corresponds to a particular 5-tuple.
131 *
132 * On entry we must be holding the lock that protects the hash table.
133 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530134struct sfe_ipv4_connection_match *
135sfe_ipv4_find_connection_match_rcu(struct sfe_ipv4 *si, struct net_device *dev, u8 protocol,
Dave Hudson87973cd2013-10-22 16:00:04 +0100136 __be32 src_ip, __be16 src_port,
137 __be32 dest_ip, __be16 dest_port)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100138{
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530139 struct sfe_ipv4_connection_match *cm = NULL;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100140 unsigned int conn_match_idx;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530141 struct hlist_head *lhead;
142
143 WARN_ON_ONCE(!rcu_read_lock_held());
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100144
145 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 +0100146
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530147 lhead = &si->hlist_conn_match_hash_head[conn_match_idx];
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100148
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530149 hlist_for_each_entry_rcu(cm, lhead, hnode) {
150 if (cm->match_src_port != src_port
151 || cm->match_dest_port != dest_port
152 || cm->match_src_ip != src_ip
153 || cm->match_dest_ip != dest_ip
Ratheesh Kannoth5dee3772022-01-18 11:27:14 +0530154 || cm->match_protocol != protocol) {
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530155 continue;
156 }
157
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530158 this_cpu_inc(si->stats_pcpu->connection_match_hash_hits64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100159
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530160 break;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100161 }
162
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100163 return cm;
164}
165
166/*
167 * sfe_ipv4_connection_match_update_summary_stats()
168 * Update the summary stats for a connection match entry.
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530169 *
170 * Stats are incremented atomically. So use atomic substraction to update summary
171 * stats.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100172 */
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530173static inline void sfe_ipv4_connection_match_update_summary_stats(struct sfe_ipv4_connection_match *cm,
174 u32 *packets, u32 *bytes)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100175{
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530176 u32 packet_count, byte_count;
177
178 packet_count = atomic_read(&cm->rx_packet_count);
179 cm->rx_packet_count64 += packet_count;
180 atomic_sub(packet_count, &cm->rx_packet_count);
181
182 byte_count = atomic_read(&cm->rx_byte_count);
183 cm->rx_byte_count64 += byte_count;
184 atomic_sub(byte_count, &cm->rx_byte_count);
185
186 *packets = packet_count;
187 *bytes = byte_count;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100188}
189
190/*
191 * sfe_ipv4_connection_match_compute_translations()
192 * Compute port and address translations for a connection match entry.
193 */
194static void sfe_ipv4_connection_match_compute_translations(struct sfe_ipv4_connection_match *cm)
195{
196 /*
197 * Before we insert the entry look to see if this is tagged as doing address
198 * translations. If it is then work out the adjustment that we need to apply
199 * to the transport checksum.
200 */
201 if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC) {
202 /*
203 * Precompute an incremental checksum adjustment so we can
204 * edit packets in this stream very quickly. The algorithm is from RFC1624.
205 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700206 u16 src_ip_hi = cm->match_src_ip >> 16;
207 u16 src_ip_lo = cm->match_src_ip & 0xffff;
208 u32 xlate_src_ip = ~cm->xlate_src_ip;
209 u16 xlate_src_ip_hi = xlate_src_ip >> 16;
210 u16 xlate_src_ip_lo = xlate_src_ip & 0xffff;
211 u16 xlate_src_port = ~cm->xlate_src_port;
212 u32 adj;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100213
214 /*
215 * When we compute this fold it down to a 16-bit offset
216 * as that way we can avoid having to do a double
217 * folding of the twos-complement result because the
218 * addition of 2 16-bit values cannot cause a double
219 * wrap-around!
220 */
221 adj = src_ip_hi + src_ip_lo + cm->match_src_port
222 + xlate_src_ip_hi + xlate_src_ip_lo + xlate_src_port;
223 adj = (adj & 0xffff) + (adj >> 16);
224 adj = (adj & 0xffff) + (adj >> 16);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700225 cm->xlate_src_csum_adjustment = (u16)adj;
Nicolas Costaac2979c2014-01-14 10:35:24 -0600226
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100227 }
228
229 if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST) {
230 /*
231 * Precompute an incremental checksum adjustment so we can
232 * edit packets in this stream very quickly. The algorithm is from RFC1624.
233 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700234 u16 dest_ip_hi = cm->match_dest_ip >> 16;
235 u16 dest_ip_lo = cm->match_dest_ip & 0xffff;
236 u32 xlate_dest_ip = ~cm->xlate_dest_ip;
237 u16 xlate_dest_ip_hi = xlate_dest_ip >> 16;
238 u16 xlate_dest_ip_lo = xlate_dest_ip & 0xffff;
239 u16 xlate_dest_port = ~cm->xlate_dest_port;
240 u32 adj;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100241
242 /*
243 * When we compute this fold it down to a 16-bit offset
244 * as that way we can avoid having to do a double
245 * folding of the twos-complement result because the
246 * addition of 2 16-bit values cannot cause a double
247 * wrap-around!
248 */
249 adj = dest_ip_hi + dest_ip_lo + cm->match_dest_port
250 + xlate_dest_ip_hi + xlate_dest_ip_lo + xlate_dest_port;
251 adj = (adj & 0xffff) + (adj >> 16);
252 adj = (adj & 0xffff) + (adj >> 16);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700253 cm->xlate_dest_csum_adjustment = (u16)adj;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100254 }
Xiaoping Fanad755af2015-04-01 16:58:46 -0700255
256 if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC) {
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700257 u32 adj = ~cm->match_src_ip + cm->xlate_src_ip;
Xiaoping Fanad755af2015-04-01 16:58:46 -0700258 if (adj < cm->xlate_src_ip) {
259 adj++;
260 }
261
262 adj = (adj & 0xffff) + (adj >> 16);
263 adj = (adj & 0xffff) + (adj >> 16);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700264 cm->xlate_src_partial_csum_adjustment = (u16)adj;
Xiaoping Fanad755af2015-04-01 16:58:46 -0700265 }
266
267 if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST) {
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700268 u32 adj = ~cm->match_dest_ip + cm->xlate_dest_ip;
Xiaoping Fanad755af2015-04-01 16:58:46 -0700269 if (adj < cm->xlate_dest_ip) {
270 adj++;
271 }
272
273 adj = (adj & 0xffff) + (adj >> 16);
274 adj = (adj & 0xffff) + (adj >> 16);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700275 cm->xlate_dest_partial_csum_adjustment = (u16)adj;
Xiaoping Fanad755af2015-04-01 16:58:46 -0700276 }
277
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100278}
279
280/*
281 * sfe_ipv4_update_summary_stats()
282 * Update the summary stats.
283 */
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530284static void sfe_ipv4_update_summary_stats(struct sfe_ipv4 *si, struct sfe_ipv4_stats *stats)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100285{
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530286 int i = 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100287
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530288 memset(stats, 0, sizeof(*stats));
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100289
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530290 for_each_possible_cpu(i) {
291 const struct sfe_ipv4_stats *s = per_cpu_ptr(si->stats_pcpu, i);
292
293 stats->connection_create_requests64 += s->connection_create_requests64;
294 stats->connection_create_collisions64 += s->connection_create_collisions64;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530295 stats->connection_create_failures64 += s->connection_create_failures64;
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530296 stats->connection_destroy_requests64 += s->connection_destroy_requests64;
297 stats->connection_destroy_misses64 += s->connection_destroy_misses64;
298 stats->connection_match_hash_hits64 += s->connection_match_hash_hits64;
299 stats->connection_match_hash_reorders64 += s->connection_match_hash_reorders64;
300 stats->connection_flushes64 += s->connection_flushes64;
Amitesh Anand63be37d2021-12-24 20:51:48 +0530301 stats->packets_dropped64 += s->packets_dropped64;
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530302 stats->packets_forwarded64 += s->packets_forwarded64;
303 stats->packets_not_forwarded64 += s->packets_not_forwarded64;
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +0530304 stats->pppoe_encap_packets_forwarded64 += s->pppoe_encap_packets_forwarded64;
305 stats->pppoe_decap_packets_forwarded64 += s->pppoe_decap_packets_forwarded64;
Guduri Prathyusha034d6352022-01-12 16:49:04 +0530306 stats->pppoe_bridge_packets_forwarded64 += s->pppoe_bridge_packets_forwarded64;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100307 }
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530308
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100309}
310
311/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530312 * sfe_ipv4_insert_connection_match()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100313 * Insert a connection match into the hash.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100314 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530315static inline void sfe_ipv4_insert_connection_match(struct sfe_ipv4 *si,
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700316 struct sfe_ipv4_connection_match *cm)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100317{
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100318 unsigned int conn_match_idx
319 = sfe_ipv4_get_connection_match_hash(cm->match_dev, cm->match_protocol,
320 cm->match_src_ip, cm->match_src_port,
321 cm->match_dest_ip, cm->match_dest_port);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700322
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530323 lockdep_assert_held(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100324
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530325 hlist_add_head_rcu(&cm->hnode, &si->hlist_conn_match_hash_head[conn_match_idx]);
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800326#ifdef CONFIG_NF_FLOW_COOKIE
Xiaoping Fan640faf42015-08-28 15:50:55 -0700327 if (!si->flow_cookie_enable)
328 return;
329
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800330 /*
331 * Configure hardware to put a flow cookie in packet of this flow,
332 * then we can accelerate the lookup process when we received this packet.
333 */
334 for (conn_match_idx = 1; conn_match_idx < SFE_FLOW_COOKIE_SIZE; conn_match_idx++) {
335 struct sfe_flow_cookie_entry *entry = &si->sfe_flow_cookie_table[conn_match_idx];
336
337 if ((NULL == entry->match) && time_is_before_jiffies(entry->last_clean_time + HZ)) {
338 flow_cookie_set_func_t func;
339
340 rcu_read_lock();
341 func = rcu_dereference(si->flow_cookie_set_func);
342 if (func) {
Xiaoping Fan59176422015-05-22 15:58:10 -0700343 if (!func(cm->match_protocol, cm->match_src_ip, cm->match_src_port,
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800344 cm->match_dest_ip, cm->match_dest_port, conn_match_idx)) {
345 entry->match = cm;
346 cm->flow_cookie = conn_match_idx;
347 }
348 }
349 rcu_read_unlock();
350
351 break;
352 }
353 }
354#endif
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100355}
356
357/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530358 * sfe_ipv4_remove_connection_match()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100359 * Remove a connection match object from the hash.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100360 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530361static inline void sfe_ipv4_remove_connection_match(struct sfe_ipv4 *si, struct sfe_ipv4_connection_match *cm)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100362{
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530363
364 lockdep_assert_held(&si->lock);
365
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800366#ifdef CONFIG_NF_FLOW_COOKIE
Xiaoping Fan640faf42015-08-28 15:50:55 -0700367 if (si->flow_cookie_enable) {
368 /*
369 * Tell hardware that we no longer need a flow cookie in packet of this flow
370 */
371 unsigned int conn_match_idx;
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800372
Xiaoping Fan640faf42015-08-28 15:50:55 -0700373 for (conn_match_idx = 1; conn_match_idx < SFE_FLOW_COOKIE_SIZE; conn_match_idx++) {
374 struct sfe_flow_cookie_entry *entry = &si->sfe_flow_cookie_table[conn_match_idx];
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800375
Xiaoping Fan640faf42015-08-28 15:50:55 -0700376 if (cm == entry->match) {
377 flow_cookie_set_func_t func;
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800378
Xiaoping Fan640faf42015-08-28 15:50:55 -0700379 rcu_read_lock();
380 func = rcu_dereference(si->flow_cookie_set_func);
381 if (func) {
382 func(cm->match_protocol, cm->match_src_ip, cm->match_src_port,
383 cm->match_dest_ip, cm->match_dest_port, 0);
384 }
385 rcu_read_unlock();
386
387 cm->flow_cookie = 0;
388 entry->match = NULL;
389 entry->last_clean_time = jiffies;
390 break;
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800391 }
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800392 }
393 }
394#endif
395
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530396 hlist_del_init_rcu(&cm->hnode);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100397
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100398}
399
400/*
401 * sfe_ipv4_get_connection_hash()
402 * Generate the hash used in connection lookups.
403 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700404static inline unsigned int sfe_ipv4_get_connection_hash(u8 protocol, __be32 src_ip, __be16 src_port,
Dave Hudson87973cd2013-10-22 16:00:04 +0100405 __be32 dest_ip, __be16 dest_port)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100406{
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700407 u32 hash = ntohl(src_ip ^ dest_ip) ^ protocol ^ ntohs(src_port ^ dest_port);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100408 return ((hash >> SFE_IPV4_CONNECTION_HASH_SHIFT) ^ hash) & SFE_IPV4_CONNECTION_HASH_MASK;
409}
410
411/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530412 * sfe_ipv4_find_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100413 * Get the IPv4 connection info that corresponds to a particular 5-tuple.
414 *
415 * On entry we must be holding the lock that protects the hash table.
416 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530417static inline struct sfe_ipv4_connection *sfe_ipv4_find_connection(struct sfe_ipv4 *si, u32 protocol,
Dave Hudson87973cd2013-10-22 16:00:04 +0100418 __be32 src_ip, __be16 src_port,
419 __be32 dest_ip, __be16 dest_port)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100420{
421 struct sfe_ipv4_connection *c;
422 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 +0530423
424 lockdep_assert_held(&si->lock);
425
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100426 c = si->conn_hash[conn_idx];
427
428 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100429 * Will need connection entry for next create/destroy metadata,
430 * So no need to re-order entry for these requests
431 */
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530432 while (c) {
433 if ((c->src_port == src_port)
434 && (c->dest_port == dest_port)
435 && (c->src_ip == src_ip)
436 && (c->dest_ip == dest_ip)
437 && (c->protocol == protocol)) {
438 return c;
439 }
440
441 c = c->next;
442 }
443
444 return NULL;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100445}
446
447/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530448 * sfe_ipv4_insert_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100449 * Insert a connection into the hash.
450 *
451 * On entry we must be holding the lock that protects the hash table.
452 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530453static void sfe_ipv4_insert_connection(struct sfe_ipv4 *si, struct sfe_ipv4_connection *c)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100454{
455 struct sfe_ipv4_connection **hash_head;
456 struct sfe_ipv4_connection *prev_head;
457 unsigned int conn_idx;
458
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530459 lockdep_assert_held(&si->lock);
460
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100461 /*
462 * Insert entry into the connection hash.
463 */
464 conn_idx = sfe_ipv4_get_connection_hash(c->protocol, c->src_ip, c->src_port,
465 c->dest_ip, c->dest_port);
466 hash_head = &si->conn_hash[conn_idx];
467 prev_head = *hash_head;
468 c->prev = NULL;
469 if (prev_head) {
470 prev_head->prev = c;
471 }
472
473 c->next = prev_head;
474 *hash_head = c;
475
476 /*
477 * Insert entry into the "all connections" list.
478 */
479 if (si->all_connections_tail) {
480 c->all_connections_prev = si->all_connections_tail;
481 si->all_connections_tail->all_connections_next = c;
482 } else {
483 c->all_connections_prev = NULL;
484 si->all_connections_head = c;
485 }
486
487 si->all_connections_tail = c;
488 c->all_connections_next = NULL;
489 si->num_connections++;
490
491 /*
492 * Insert the connection match objects too.
493 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530494 sfe_ipv4_insert_connection_match(si, c->original_match);
495 sfe_ipv4_insert_connection_match(si, c->reply_match);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100496}
497
498/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530499 * sfe_ipv4_remove_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100500 * Remove a sfe_ipv4_connection object from the hash.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100501 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530502bool sfe_ipv4_remove_connection(struct sfe_ipv4 *si, struct sfe_ipv4_connection *c)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100503{
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530504 lockdep_assert_held(&si->lock);
505
506 if (c->removed) {
507 DEBUG_ERROR("%px: Connection has been removed already\n", c);
508 return false;
509 }
510
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100511 /*
512 * Remove the connection match objects.
513 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530514 sfe_ipv4_remove_connection_match(si, c->reply_match);
515 sfe_ipv4_remove_connection_match(si, c->original_match);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100516
517 /*
518 * Unlink the connection.
519 */
520 if (c->prev) {
521 c->prev->next = c->next;
522 } else {
523 unsigned int conn_idx = sfe_ipv4_get_connection_hash(c->protocol, c->src_ip, c->src_port,
524 c->dest_ip, c->dest_port);
525 si->conn_hash[conn_idx] = c->next;
526 }
527
528 if (c->next) {
529 c->next->prev = c->prev;
530 }
Xiaoping Fan34586472015-07-03 02:20:35 -0700531
532 /*
533 * Unlink connection from all_connections list
534 */
535 if (c->all_connections_prev) {
536 c->all_connections_prev->all_connections_next = c->all_connections_next;
537 } else {
538 si->all_connections_head = c->all_connections_next;
539 }
540
541 if (c->all_connections_next) {
542 c->all_connections_next->all_connections_prev = c->all_connections_prev;
543 } else {
544 si->all_connections_tail = c->all_connections_prev;
545 }
546
Ken Zhudc423672021-09-02 18:27:01 -0700547 /*
548 * If I am the next sync connection, move the sync to my next or head.
549 */
550 if (unlikely(si->wc_next == c)) {
551 si->wc_next = c->all_connections_next;
552 }
553
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530554 c->removed = true;
Xiaoping Fan34586472015-07-03 02:20:35 -0700555 si->num_connections--;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530556 return true;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100557}
558
559/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530560 * sfe_ipv4_gen_sync_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100561 * Sync a connection.
562 *
563 * On entry to this function we expect that the lock for the connection is either
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530564 * already held (while called from sfe_ipv4_periodic_sync() or isn't required
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530565 * (while called from sfe_ipv4_flush_connection())
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100566 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530567static void sfe_ipv4_gen_sync_connection(struct sfe_ipv4 *si, struct sfe_ipv4_connection *c,
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700568 struct sfe_connection_sync *sis, sfe_sync_reason_t reason,
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700569 u64 now_jiffies)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100570{
571 struct sfe_ipv4_connection_match *original_cm;
572 struct sfe_ipv4_connection_match *reply_cm;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530573 u32 packet_count, byte_count;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100574
575 /*
576 * Fill in the update message.
577 */
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700578 sis->is_v6 = 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100579 sis->protocol = c->protocol;
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700580 sis->src_ip.ip = c->src_ip;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700581 sis->src_ip_xlate.ip = c->src_ip_xlate;
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700582 sis->dest_ip.ip = c->dest_ip;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700583 sis->dest_ip_xlate.ip = c->dest_ip_xlate;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100584 sis->src_port = c->src_port;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700585 sis->src_port_xlate = c->src_port_xlate;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100586 sis->dest_port = c->dest_port;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700587 sis->dest_port_xlate = c->dest_port_xlate;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100588
589 original_cm = c->original_match;
590 reply_cm = c->reply_match;
591 sis->src_td_max_window = original_cm->protocol_state.tcp.max_win;
592 sis->src_td_end = original_cm->protocol_state.tcp.end;
593 sis->src_td_max_end = original_cm->protocol_state.tcp.max_end;
594 sis->dest_td_max_window = reply_cm->protocol_state.tcp.max_win;
595 sis->dest_td_end = reply_cm->protocol_state.tcp.end;
596 sis->dest_td_max_end = reply_cm->protocol_state.tcp.max_end;
597
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530598 sfe_ipv4_connection_match_update_summary_stats(original_cm, &packet_count, &byte_count);
599 sis->src_new_packet_count = packet_count;
600 sis->src_new_byte_count = byte_count;
Matthew McClintockd0cdb802014-02-24 16:30:35 -0600601
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530602 sfe_ipv4_connection_match_update_summary_stats(reply_cm, &packet_count, &byte_count);
603 sis->dest_new_packet_count = packet_count;
604 sis->dest_new_byte_count = byte_count;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100605
Matthew McClintockd0cdb802014-02-24 16:30:35 -0600606 sis->src_dev = original_cm->match_dev;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100607 sis->src_packet_count = original_cm->rx_packet_count64;
608 sis->src_byte_count = original_cm->rx_byte_count64;
Matthew McClintockd0cdb802014-02-24 16:30:35 -0600609
610 sis->dest_dev = reply_cm->match_dev;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100611 sis->dest_packet_count = reply_cm->rx_packet_count64;
612 sis->dest_byte_count = reply_cm->rx_byte_count64;
613
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700614 sis->reason = reason;
615
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100616 /*
617 * Get the time increment since our last sync.
618 */
619 sis->delta_jiffies = now_jiffies - c->last_sync_jiffies;
620 c->last_sync_jiffies = now_jiffies;
621}
622
623/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530624 * sfe_ipv4_free_connection_rcu()
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530625 * Called at RCU qs state to free the connection object.
626 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530627static void sfe_ipv4_free_connection_rcu(struct rcu_head *head)
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530628{
629 struct sfe_ipv4_connection *c;
Amitesh Anand63be37d2021-12-24 20:51:48 +0530630 struct udp_sock *up;
631 struct sock *sk;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530632
633 /*
634 * We dont need spin lock as the connection is already removed from link list
635 */
636 c = container_of(head, struct sfe_ipv4_connection, rcu);
637
638 BUG_ON(!c->removed);
639
640 DEBUG_TRACE("%px: connecton has been deleted\n", c);
641
642 /*
Amitesh Anand63be37d2021-12-24 20:51:48 +0530643 * Decrease the refcount taken in function sfe_ipv4_create_rule(),
644 * during call of __udp4_lib_lookup()
645 */
646 up = c->reply_match->up;
647 if (up) {
648 sk = (struct sock *)up;
649 sock_put(sk);
650 }
651
652 /*
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530653 * Release our hold of the source and dest devices and free the memory
654 * for our connection objects.
655 */
656 dev_put(c->original_dev);
657 dev_put(c->reply_dev);
658 kfree(c->original_match);
659 kfree(c->reply_match);
660 kfree(c);
661}
662
663/*
Ken Zhu88c58152021-12-09 15:12:06 -0800664 * sfe_ipv4_sync_status()
665 * update a connection status to its connection manager.
666 *
667 * si: the ipv4 context
668 * c: which connection to be notified
669 * reason: what kind of notification: flush, stats or destroy
670 */
671void sfe_ipv4_sync_status(struct sfe_ipv4 *si,
672 struct sfe_ipv4_connection *c,
673 sfe_sync_reason_t reason)
674{
675 struct sfe_connection_sync sis;
676 u64 now_jiffies;
677 sfe_sync_rule_callback_t sync_rule_callback;
678
679 rcu_read_lock();
680 sync_rule_callback = rcu_dereference(si->sync_rule_callback);
681 if (!sync_rule_callback) {
682 rcu_read_unlock();
683 return;
684 }
685
686 /*
687 * Generate a sync message and then sync.
688 */
689 now_jiffies = get_jiffies_64();
690 sfe_ipv4_gen_sync_connection(si, c, &sis, reason, now_jiffies);
691 sync_rule_callback(&sis);
692
693 rcu_read_unlock();
694}
695
696/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530697 * sfe_ipv4_flush_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100698 * Flush a connection and free all associated resources.
699 *
700 * We need to be called with bottom halves disabled locally as we need to acquire
701 * the connection hash lock and release it again. In general we're actually called
702 * from within a BH and so we're fine, but we're also called when connections are
703 * torn down.
704 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530705void sfe_ipv4_flush_connection(struct sfe_ipv4 *si,
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700706 struct sfe_ipv4_connection *c,
707 sfe_sync_reason_t reason)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100708{
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530709 BUG_ON(!c->removed);
710
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530711 this_cpu_inc(si->stats_pcpu->connection_flushes64);
Ken Zhu88c58152021-12-09 15:12:06 -0800712 sfe_ipv4_sync_status(si, c, reason);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100713
714 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100715 * Release our hold of the source and dest devices and free the memory
716 * for our connection objects.
717 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530718 call_rcu(&c->rcu, sfe_ipv4_free_connection_rcu);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100719}
720
721/*
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530722 * sfe_ipv4_exception_stats_inc()
723 * Increment exception stats.
724 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530725void sfe_ipv4_exception_stats_inc(struct sfe_ipv4 *si, enum sfe_ipv4_exception_events reason)
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530726{
727 struct sfe_ipv4_stats *stats = this_cpu_ptr(si->stats_pcpu);
728 stats->exception_events64[reason]++;
729 stats->packets_not_forwarded64++;
730}
731
732/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100733 * sfe_ipv4_recv()
Matthew McClintocka8ad7962014-01-16 16:49:30 -0600734 * Handle packet receives and forwaring.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100735 *
736 * Returns 1 if the packet is forwarded or 0 if it isn't.
737 */
Amitesh Anand63be37d2021-12-24 20:51:48 +0530738int 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 +0100739{
740 struct sfe_ipv4 *si = &__si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100741 unsigned int len;
742 unsigned int tot_len;
743 unsigned int frag_off;
744 unsigned int ihl;
Ken Zhu88c58152021-12-09 15:12:06 -0800745 bool sync_on_find;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100746 bool ip_options;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530747 struct iphdr *iph;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700748 u32 protocol;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100749
750 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100751 * Check that we have space for an IP header here.
752 */
753 len = skb->len;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530754 if (unlikely(!pskb_may_pull(skb, sizeof(struct iphdr)))) {
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530755 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_HEADER_INCOMPLETE);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100756 DEBUG_TRACE("len: %u is too short\n", len);
757 return 0;
758 }
759
760 /*
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +0530761 * Validate ip csum if necessary. If ip_summed is set to CHECKSUM_UNNECESSARY, it is assumed
762 * that the L3 checksum is validated by the Rx interface or the tunnel interface that has
763 * generated the packet.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100764 */
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530765 iph = (struct iphdr *)skb->data;
Ratheesh Kannoth43d64f82021-10-20 08:23:29 +0530766 if (unlikely(skb->ip_summed != CHECKSUM_UNNECESSARY) && (ip_fast_csum((u8 *)iph, iph->ihl))) {
767 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_HEADER_CSUM_BAD);
768
769 DEBUG_TRACE("Bad IPv4 header csum: 0x%x\n", iph->check);
770 return 0;
771 }
772
773 /*
774 * Check that our "total length" is large enough for an IP header.
775 */
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100776 tot_len = ntohs(iph->tot_len);
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530777 if (unlikely(tot_len < sizeof(struct iphdr))) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100778
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530779 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_BAD_TOTAL_LENGTH);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100780 DEBUG_TRACE("tot_len: %u is too short\n", tot_len);
781 return 0;
782 }
783
784 /*
785 * Is our IP version wrong?
786 */
787 if (unlikely(iph->version != 4)) {
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530788 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_NON_V4);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100789 DEBUG_TRACE("IP version: %u\n", iph->version);
790 return 0;
791 }
792
793 /*
794 * Does our datagram fit inside the skb?
795 */
796 if (unlikely(tot_len > len)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100797 DEBUG_TRACE("tot_len: %u, exceeds len: %u\n", tot_len, len);
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530798 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_DATAGRAM_INCOMPLETE);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100799 return 0;
800 }
801
802 /*
803 * Do we have a non-initial fragment?
Nicolas Costaac2979c2014-01-14 10:35:24 -0600804 */
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100805 frag_off = ntohs(iph->frag_off);
806 if (unlikely(frag_off & IP_OFFSET)) {
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530807 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_NON_INITIAL_FRAGMENT);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100808 DEBUG_TRACE("non-initial fragment\n");
809 return 0;
810 }
811
812 /*
813 * If we have a (first) fragment then mark it to cause any connection to flush.
814 */
Ken Zhu88c58152021-12-09 15:12:06 -0800815 sync_on_find = unlikely(frag_off & IP_MF) ? true : false;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100816
817 /*
818 * Do we have any IP options? That's definite a slow path! If we do have IP
819 * options we need to recheck our header size.
820 */
821 ihl = iph->ihl << 2;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530822 ip_options = unlikely(ihl != sizeof(struct iphdr)) ? true : false;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100823 if (unlikely(ip_options)) {
824 if (unlikely(len < ihl)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100825
826 DEBUG_TRACE("len: %u is too short for header of size: %u\n", len, ihl);
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530827 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_IP_OPTIONS_INCOMPLETE);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100828 return 0;
829 }
830
Ken Zhu88c58152021-12-09 15:12:06 -0800831 sync_on_find = true;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100832 }
833
834 protocol = iph->protocol;
835 if (IPPROTO_UDP == protocol) {
Ken Zhu88c58152021-12-09 15:12:06 -0800836 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 +0100837 }
838
839 if (IPPROTO_TCP == protocol) {
Ken Zhu88c58152021-12-09 15:12:06 -0800840 return sfe_ipv4_recv_tcp(si, skb, dev, len, iph, ihl, sync_on_find, l2_info);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100841 }
842
843 if (IPPROTO_ICMP == protocol) {
844 return sfe_ipv4_recv_icmp(si, skb, dev, len, iph, ihl);
845 }
846
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530847 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_UNHANDLED_PROTOCOL);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100848
849 DEBUG_TRACE("not UDP, TCP or ICMP: %u\n", protocol);
850 return 0;
851}
852
Nicolas Costa436926b2014-01-14 10:36:22 -0600853static void
854sfe_ipv4_update_tcp_state(struct sfe_ipv4_connection *c,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530855 struct sfe_ipv4_rule_create_msg *msg)
Nicolas Costa436926b2014-01-14 10:36:22 -0600856{
857 struct sfe_ipv4_connection_match *orig_cm;
858 struct sfe_ipv4_connection_match *repl_cm;
859 struct sfe_ipv4_tcp_connection_match *orig_tcp;
860 struct sfe_ipv4_tcp_connection_match *repl_tcp;
861
862 orig_cm = c->original_match;
863 repl_cm = c->reply_match;
864 orig_tcp = &orig_cm->protocol_state.tcp;
865 repl_tcp = &repl_cm->protocol_state.tcp;
866
867 /* update orig */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530868 if (orig_tcp->max_win < msg->tcp_rule.flow_max_window) {
869 orig_tcp->max_win = msg->tcp_rule.flow_max_window;
Nicolas Costa436926b2014-01-14 10:36:22 -0600870 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530871 if ((s32)(orig_tcp->end - msg->tcp_rule.flow_end) < 0) {
872 orig_tcp->end = msg->tcp_rule.flow_end;
Nicolas Costa436926b2014-01-14 10:36:22 -0600873 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530874 if ((s32)(orig_tcp->max_end - msg->tcp_rule.flow_max_end) < 0) {
875 orig_tcp->max_end = msg->tcp_rule.flow_max_end;
Nicolas Costa436926b2014-01-14 10:36:22 -0600876 }
877
878 /* update reply */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530879 if (repl_tcp->max_win < msg->tcp_rule.return_max_window) {
880 repl_tcp->max_win = msg->tcp_rule.return_max_window;
Nicolas Costa436926b2014-01-14 10:36:22 -0600881 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530882 if ((s32)(repl_tcp->end - msg->tcp_rule.return_end) < 0) {
883 repl_tcp->end = msg->tcp_rule.return_end;
Nicolas Costa436926b2014-01-14 10:36:22 -0600884 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530885 if ((s32)(repl_tcp->max_end - msg->tcp_rule.return_max_end) < 0) {
886 repl_tcp->max_end = msg->tcp_rule.return_max_end;
Nicolas Costa436926b2014-01-14 10:36:22 -0600887 }
888
889 /* update match flags */
890 orig_cm->flags &= ~SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
891 repl_cm->flags &= ~SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530892 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_NO_SEQ_CHECK) {
893
Nicolas Costa436926b2014-01-14 10:36:22 -0600894 orig_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
895 repl_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
896 }
897}
898
899static void
900sfe_ipv4_update_protocol_state(struct sfe_ipv4_connection *c,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530901 struct sfe_ipv4_rule_create_msg *msg)
Nicolas Costa436926b2014-01-14 10:36:22 -0600902{
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530903 switch (msg->tuple.protocol) {
Nicolas Costa436926b2014-01-14 10:36:22 -0600904 case IPPROTO_TCP:
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530905 sfe_ipv4_update_tcp_state(c, msg);
Nicolas Costa436926b2014-01-14 10:36:22 -0600906 break;
907 }
908}
909
Wayne Tanbb7f1782021-12-13 11:16:04 -0800910/*
911 * sfe_ipv4_match_entry_set_vlan()
912 */
913static void sfe_ipv4_match_entry_set_vlan(
914 struct sfe_ipv4_connection_match *cm,
915 u32 primary_ingress_vlan_tag,
916 u32 primary_egress_vlan_tag,
917 u32 secondary_ingress_vlan_tag,
918 u32 secondary_egress_vlan_tag)
919{
920 u16 tpid;
921 /*
922 * Prevent stacking header counts when updating.
923 */
924 cm->ingress_vlan_hdr_cnt = 0;
925 cm->egress_vlan_hdr_cnt = 0;
926 memset(cm->ingress_vlan_hdr, 0, sizeof(cm->ingress_vlan_hdr));
927 memset(cm->egress_vlan_hdr, 0, sizeof(cm->egress_vlan_hdr));
928
929 /*
930 * vlan_hdr[0] corresponds to outer tag
931 * vlan_hdr[1] corresponds to inner tag
932 * Extract the vlan information (tpid and tci) from rule message
933 */
934 if ((primary_ingress_vlan_tag & VLAN_VID_MASK) != SFE_VLAN_ID_NOT_CONFIGURED) {
935 tpid = (u16)(primary_ingress_vlan_tag >> 16);
936 cm->ingress_vlan_hdr[0].tpid = ntohs(tpid);
937 cm->ingress_vlan_hdr[0].tci = (u16)primary_ingress_vlan_tag;
938 cm->ingress_vlan_hdr_cnt++;
939 }
940
941 if ((secondary_ingress_vlan_tag & VLAN_VID_MASK) != SFE_VLAN_ID_NOT_CONFIGURED) {
942 tpid = (u16)(secondary_ingress_vlan_tag >> 16);
943 cm->ingress_vlan_hdr[1].tpid = ntohs(tpid);
944 cm->ingress_vlan_hdr[1].tci = (u16)secondary_ingress_vlan_tag;
945 cm->ingress_vlan_hdr_cnt++;
946 }
947
948 if ((primary_egress_vlan_tag & VLAN_VID_MASK) != SFE_VLAN_ID_NOT_CONFIGURED) {
949 tpid = (u16)(primary_egress_vlan_tag >> 16);
950 cm->egress_vlan_hdr[0].tpid = ntohs(tpid);
951 cm->egress_vlan_hdr[0].tci = (u16)primary_egress_vlan_tag;
952 cm->egress_vlan_hdr_cnt++;
953 }
954
955 if ((secondary_egress_vlan_tag & VLAN_VID_MASK) != SFE_VLAN_ID_NOT_CONFIGURED) {
956 tpid = (u16)(secondary_egress_vlan_tag >> 16);
957 cm->egress_vlan_hdr[1].tpid = ntohs(tpid);
958 cm->egress_vlan_hdr[1].tci = (u16)secondary_egress_vlan_tag;
959 cm->egress_vlan_hdr_cnt++;
960 }
961}
962
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530963void sfe_ipv4_update_rule(struct sfe_ipv4_rule_create_msg *msg)
Nicolas Costa436926b2014-01-14 10:36:22 -0600964{
965 struct sfe_ipv4_connection *c;
966 struct sfe_ipv4 *si = &__si;
967
968 spin_lock_bh(&si->lock);
969
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530970 c = sfe_ipv4_find_connection(si,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530971 msg->tuple.protocol,
972 msg->tuple.flow_ip,
973 msg->tuple.flow_ident,
974 msg->tuple.return_ip,
975 msg->tuple.return_ident);
Nicolas Costa436926b2014-01-14 10:36:22 -0600976 if (c != NULL) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530977 sfe_ipv4_update_protocol_state(c, msg);
Nicolas Costa436926b2014-01-14 10:36:22 -0600978 }
979
980 spin_unlock_bh(&si->lock);
981}
982
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100983/*
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +0530984 * sfe_ipv4_xmit_eth_type_check()
985 * Checking if MAC header has to be written.
986 */
987static inline bool sfe_ipv4_xmit_eth_type_check(struct net_device *dev, u32 cm_flags)
988{
989 if (!(dev->flags & IFF_NOARP)) {
990 return true;
991 }
992
993 /*
994 * For PPPoE, since we are now supporting PPPoE encapsulation, we are writing L2 header.
995 */
996 if (unlikely(cm_flags & SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_ENCAP)) {
997 return true;
998 }
999
1000 return false;
1001}
1002
1003/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001004 * sfe_ipv4_create_rule()
1005 * Create a forwarding rule.
1006 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301007int sfe_ipv4_create_rule(struct sfe_ipv4_rule_create_msg *msg)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001008{
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001009 struct sfe_ipv4 *si = &__si;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301010 struct sfe_ipv4_connection *c, *c_old;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001011 struct sfe_ipv4_connection_match *original_cm;
1012 struct sfe_ipv4_connection_match *reply_cm;
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001013 struct net_device *dest_dev;
1014 struct net_device *src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301015 struct sfe_ipv4_5tuple *tuple = &msg->tuple;
Suruchi Sumanc1a4a612021-10-21 14:50:23 +05301016 s32 flow_interface_num = msg->conn_rule.flow_top_interface_num;
1017 s32 return_interface_num = msg->conn_rule.return_top_interface_num;
Amitesh Anand63be37d2021-12-24 20:51:48 +05301018 struct net *net;
1019 struct sock *sk;
1020 unsigned int src_if_idx;
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001021
Suruchi Sumanc1a4a612021-10-21 14:50:23 +05301022 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_FLOW_BOTTOM_INTERFACE) {
1023 flow_interface_num = msg->conn_rule.flow_interface_num;
1024 }
1025
1026 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_RETURN_BOTTOM_INTERFACE) {
1027 return_interface_num = msg->conn_rule.return_interface_num;
1028 }
1029
1030 src_dev = dev_get_by_index(&init_net, flow_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301031 if (!src_dev) {
1032 DEBUG_WARN("%px: Unable to find src_dev corresponding to %d\n", msg,
Suruchi Sumanc1a4a612021-10-21 14:50:23 +05301033 flow_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301034 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
1035 return -EINVAL;
1036 }
1037
Suruchi Sumanc1a4a612021-10-21 14:50:23 +05301038 dest_dev = dev_get_by_index(&init_net, return_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301039 if (!dest_dev) {
1040 DEBUG_WARN("%px: Unable to find dest_dev corresponding to %d\n", msg,
Suruchi Sumanc1a4a612021-10-21 14:50:23 +05301041 return_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301042 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
1043 dev_put(src_dev);
1044 return -EINVAL;
1045 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001046
Matthew McClintock389b42a2014-09-24 14:05:51 -05001047 if (unlikely((dest_dev->reg_state != NETREG_REGISTERED) ||
1048 (src_dev->reg_state != NETREG_REGISTERED))) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301049 dev_put(src_dev);
1050 dev_put(dest_dev);
1051 DEBUG_WARN("%px: src_dev=%s and dest_dev=%s are unregistered\n", msg,
1052 src_dev->name, dest_dev->name);
1053 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
Matthew McClintock389b42a2014-09-24 14:05:51 -05001054 return -EINVAL;
1055 }
1056
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301057 /*
1058 * Allocate the various connection tracking objects.
1059 */
1060 c = (struct sfe_ipv4_connection *)kmalloc(sizeof(struct sfe_ipv4_connection), GFP_ATOMIC);
1061 if (unlikely(!c)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301062 DEBUG_WARN("%px: memory allocation of connection entry failed\n", msg);
1063 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
1064 dev_put(src_dev);
1065 dev_put(dest_dev);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301066 return -ENOMEM;
1067 }
1068
1069 original_cm = (struct sfe_ipv4_connection_match *)kmalloc(sizeof(struct sfe_ipv4_connection_match), GFP_ATOMIC);
1070 if (unlikely(!original_cm)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301071 DEBUG_WARN("%px: memory allocation of connection match entry failed\n", msg);
1072 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301073 kfree(c);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301074 dev_put(src_dev);
1075 dev_put(dest_dev);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301076 return -ENOMEM;
1077 }
1078
1079 reply_cm = (struct sfe_ipv4_connection_match *)kmalloc(sizeof(struct sfe_ipv4_connection_match), GFP_ATOMIC);
1080 if (unlikely(!reply_cm)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301081 DEBUG_WARN("%px: memory allocation of connection match entry failed\n", msg);
1082 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301083 kfree(original_cm);
1084 kfree(c);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301085 dev_put(src_dev);
1086 dev_put(dest_dev);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301087 return -ENOMEM;
1088 }
1089
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301090 this_cpu_inc(si->stats_pcpu->connection_create_requests64);
1091
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001092 spin_lock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001093
1094 /*
Nicolas Costa436926b2014-01-14 10:36:22 -06001095 * Check to see if there is already a flow that matches the rule we're
1096 * trying to create. If there is then we can't create a new one.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001097 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301098 c_old = sfe_ipv4_find_connection(si,
Wayne Tanbb7f1782021-12-13 11:16:04 -08001099 msg->tuple.protocol,
1100 msg->tuple.flow_ip,
1101 msg->tuple.flow_ident,
1102 msg->tuple.return_ip,
1103 msg->tuple.return_ident);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301104
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301105 if (c_old != NULL) {
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301106 this_cpu_inc(si->stats_pcpu->connection_create_collisions64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001107
1108 /*
Nicolas Costa436926b2014-01-14 10:36:22 -06001109 * If we already have the flow then it's likely that this
1110 * request to create the connection rule contains more
1111 * up-to-date information. Check and update accordingly.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001112 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301113 sfe_ipv4_update_protocol_state(c, msg);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001114 spin_unlock_bh(&si->lock);
1115
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301116 kfree(reply_cm);
1117 kfree(original_cm);
1118 kfree(c);
1119
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301120 dev_put(src_dev);
1121 dev_put(dest_dev);
1122
Amitesh Anand63be37d2021-12-24 20:51:48 +05301123 DEBUG_TRACE("%px: connection already exists - p:%d\n"
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301124 " s: %s:%pM:%pI4:%u, d: %s:%pM:%pI4:%u\n",
Amitesh Anand63be37d2021-12-24 20:51:48 +05301125 msg, tuple->protocol,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301126 src_dev->name, msg->conn_rule.flow_mac, &tuple->flow_ip, ntohs(tuple->flow_ident),
1127 dest_dev->name, msg->conn_rule.return_mac, &tuple->return_ip, ntohs(tuple->return_ident));
1128
Nicolas Costa514fde02014-01-13 15:50:29 -06001129 return -EADDRINUSE;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001130 }
1131
1132 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001133 * Fill in the "original" direction connection matching object.
1134 * Note that the transmit MAC address is "dest_mac_xlate" because
1135 * we always know both ends of a connection by their translated
1136 * addresses and not their public addresses.
1137 */
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001138 original_cm->match_dev = src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301139 original_cm->match_protocol = tuple->protocol;
1140 original_cm->match_src_ip = tuple->flow_ip;
Suruchi Suman66609a72022-01-20 02:34:25 +05301141 original_cm->match_src_port = netif_is_vxlan(src_dev) ? 0 : tuple->flow_ident;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301142 original_cm->match_dest_ip = tuple->return_ip;
1143 original_cm->match_dest_port = tuple->return_ident;
1144
1145 original_cm->xlate_src_ip = msg->conn_rule.flow_ip_xlate;
1146 original_cm->xlate_src_port = msg->conn_rule.flow_ident_xlate;
1147 original_cm->xlate_dest_ip = msg->conn_rule.return_ip_xlate;
1148 original_cm->xlate_dest_port =msg->conn_rule.return_ident_xlate;
Wayne Tanbb7f1782021-12-13 11:16:04 -08001149
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301150 atomic_set(&original_cm->rx_packet_count, 0);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001151 original_cm->rx_packet_count64 = 0;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301152 atomic_set(&original_cm->rx_byte_count, 0);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001153 original_cm->rx_byte_count64 = 0;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301154
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001155 original_cm->xmit_dev = dest_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301156 original_cm->xmit_dev_mtu = msg->conn_rule.return_mtu;
1157
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001158 original_cm->connection = c;
1159 original_cm->counter_match = reply_cm;
Wayne Tanbb7f1782021-12-13 11:16:04 -08001160 original_cm->l2_hdr_size = 0;
1161 original_cm->flags = 0;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301162
Amitesh Anand63be37d2021-12-24 20:51:48 +05301163 /*
1164 * UDP Socket is valid only in decap direction.
1165 */
1166 RCU_INIT_POINTER(original_cm->up, NULL);
1167
Ken Zhu37040ea2021-09-09 21:11:15 -07001168 if (msg->valid_flags & SFE_RULE_CREATE_MARK_VALID) {
1169 original_cm->mark = msg->mark_rule.flow_mark;
1170 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_MARK;
1171 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301172 if (msg->valid_flags & SFE_RULE_CREATE_QOS_VALID) {
1173 original_cm->priority = msg->qos_rule.flow_qos_tag;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001174 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PRIORITY_REMARK;
1175 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301176 if (msg->valid_flags & SFE_RULE_CREATE_DSCP_MARKING_VALID) {
1177 original_cm->dscp = msg->dscp_rule.flow_dscp << SFE_IPV4_DSCP_SHIFT;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001178 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_DSCP_REMARK;
1179 }
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301180
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301181 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1182 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_BRIDGE_FLOW;
1183 }
1184
Wayne Tanbb7f1782021-12-13 11:16:04 -08001185 /*
1186 * Add VLAN rule to original_cm
1187 */
1188 if (msg->valid_flags & SFE_RULE_CREATE_VLAN_VALID) {
1189 struct sfe_vlan_rule *vlan_primary_rule = &msg->vlan_primary_rule;
1190 struct sfe_vlan_rule *vlan_secondary_rule = &msg->vlan_secondary_rule;
1191 sfe_ipv4_match_entry_set_vlan(original_cm,
1192 vlan_primary_rule->ingress_vlan_tag,
1193 vlan_primary_rule->egress_vlan_tag,
1194 vlan_secondary_rule->ingress_vlan_tag,
1195 vlan_secondary_rule->egress_vlan_tag);
1196
1197 if ((msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_RETURN_BOTTOM_INTERFACE) &&
1198 original_cm->egress_vlan_hdr_cnt > 0) {
1199 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_INSERT_EGRESS_VLAN_TAG;
1200 original_cm->l2_hdr_size += original_cm->egress_vlan_hdr_cnt * VLAN_HLEN;
1201 }
1202 }
1203
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001204#ifdef CONFIG_NF_FLOW_COOKIE
1205 original_cm->flow_cookie = 0;
1206#endif
Zhi Chen8748eb32015-06-18 12:58:48 -07001207#ifdef CONFIG_XFRM
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301208 if (msg->valid_flags & SFE_RULE_CREATE_DIRECTION_VALID) {
1209 original_cm->flow_accel = msg->direction_rule.flow_accel;
1210 } else {
1211 original_cm->flow_accel = 1;
1212 }
Zhi Chen8748eb32015-06-18 12:58:48 -07001213#endif
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301214 /*
1215 * If l2_features are disabled and flow uses l2 features such as macvlan/bridge/pppoe/vlan,
1216 * bottom interfaces are expected to be disabled in the flow rule and always top interfaces
1217 * are used. In such cases, do not use HW csum offload. csum offload is used only when we
1218 * are sending directly to the destination interface that supports it.
1219 */
Suruchi Sumanf2077182022-01-13 21:35:23 +05301220 if (likely(dest_dev->features & NETIF_F_HW_CSUM) && !netif_is_vxlan(dest_dev)) {
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301221 if ((msg->conn_rule.return_top_interface_num == msg->conn_rule.return_interface_num) ||
1222 (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_RETURN_BOTTOM_INTERFACE)) {
Suruchi Sumanf2077182022-01-13 21:35:23 +05301223 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_CSUM_OFFLOAD;
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301224 }
1225 }
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001226
Wayne Tanbb7f1782021-12-13 11:16:04 -08001227 reply_cm->l2_hdr_size = 0;
Ratheesh Kannoth5dee3772022-01-18 11:27:14 +05301228 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_SRC_INTERFACE_CHECK) {
1229 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_SRC_INTERFACE_CHECK;
1230 }
1231
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +05301232 reply_cm->flags = 0;
1233
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001234 /*
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301235 * Adding PPPoE parameters to original and reply entries based on the direction where
1236 * PPPoE header is valid in ECM rule.
1237 *
1238 * If PPPoE is valid in flow direction (from interface is PPPoE), then
1239 * original cm will have PPPoE at ingress (strip PPPoE header)
1240 * reply cm will have PPPoE at egress (add PPPoE header)
1241 *
1242 * If PPPoE is valid in return direction (to interface is PPPoE), then
1243 * original cm will have PPPoE at egress (add PPPoE header)
1244 * reply cm will have PPPoE at ingress (strip PPPoE header)
1245 */
1246 if (msg->valid_flags & SFE_RULE_CREATE_PPPOE_DECAP_VALID) {
1247 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_DECAP;
1248 original_cm->pppoe_session_id = msg->pppoe_rule.flow_pppoe_session_id;
1249 ether_addr_copy(original_cm->pppoe_remote_mac, msg->pppoe_rule.flow_pppoe_remote_mac);
1250
1251 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_ENCAP;
Wayne Tanbb7f1782021-12-13 11:16:04 -08001252 reply_cm->l2_hdr_size += SFE_PPPOE_SESSION_HEADER_SIZE;
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301253 reply_cm->pppoe_session_id = msg->pppoe_rule.flow_pppoe_session_id;
1254 ether_addr_copy(reply_cm->pppoe_remote_mac, msg->pppoe_rule.flow_pppoe_remote_mac);
1255 }
1256
1257 if (msg->valid_flags & SFE_RULE_CREATE_PPPOE_ENCAP_VALID) {
1258 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_ENCAP;
Wayne Tanbb7f1782021-12-13 11:16:04 -08001259 original_cm->l2_hdr_size += SFE_PPPOE_SESSION_HEADER_SIZE;
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301260 original_cm->pppoe_session_id = msg->pppoe_rule.return_pppoe_session_id;
1261 ether_addr_copy(original_cm->pppoe_remote_mac, msg->pppoe_rule.return_pppoe_remote_mac);
1262
1263 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_DECAP;
1264 reply_cm->pppoe_session_id = msg->pppoe_rule.return_pppoe_session_id;
1265 ether_addr_copy(reply_cm->pppoe_remote_mac, msg->pppoe_rule.return_pppoe_remote_mac);
1266 }
1267
Ratheesh Kannoth5dee3772022-01-18 11:27:14 +05301268 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_SRC_INTERFACE_CHECK) {
1269 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_SRC_INTERFACE_CHECK;
1270 }
1271
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301272 /*
Ken Zhubbf49652021-09-12 15:33:09 -07001273 * For the non-arp interface, we don't write L2 HDR.
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001274 */
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +05301275 if (sfe_ipv4_xmit_eth_type_check(dest_dev, original_cm->flags)) {
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301276
1277 /*
1278 * Check whether the rule has configured a specific source MAC address to use.
1279 * This is needed when virtual L3 interfaces such as br-lan, macvlan, vlan are used during egress
1280 */
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301281
1282 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1283 ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)msg->conn_rule.flow_mac);
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301284 } else {
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301285 if ((msg->valid_flags & SFE_RULE_CREATE_SRC_MAC_VALID) &&
1286 (msg->src_mac_rule.mac_valid_flags & SFE_SRC_MAC_RETURN_VALID)) {
1287 ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)msg->src_mac_rule.return_src_mac);
1288 } else {
1289 ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)dest_dev->dev_addr);
1290 }
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301291 }
1292
1293 ether_addr_copy((u8 *)original_cm->xmit_dest_mac, (u8 *)msg->conn_rule.return_mac);
1294
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001295 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_L2_HDR;
Wayne Tanbb7f1782021-12-13 11:16:04 -08001296 original_cm->l2_hdr_size += ETH_HLEN;
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001297
1298 /*
1299 * If our dev writes Ethernet headers then we can write a really fast
1300 * version.
1301 */
1302 if (dest_dev->header_ops) {
1303 if (dest_dev->header_ops->create == eth_header) {
1304 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR;
1305 }
1306 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001307 }
1308
1309 /*
1310 * Fill in the "reply" direction connection matching object.
1311 */
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001312 reply_cm->match_dev = dest_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301313 reply_cm->match_protocol = tuple->protocol;
1314 reply_cm->match_src_ip = msg->conn_rule.return_ip_xlate;
Amitesh Anand63be37d2021-12-24 20:51:48 +05301315
1316 /*
1317 * Keep source port as 0 for VxLAN tunnels.
1318 */
1319 if (netif_is_vxlan(src_dev) || netif_is_vxlan(dest_dev)) {
1320 reply_cm->match_src_port = 0;
1321 } else {
1322 reply_cm->match_src_port = msg->conn_rule.return_ident_xlate;
1323 }
1324
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301325 reply_cm->match_dest_ip = msg->conn_rule.flow_ip_xlate;
1326 reply_cm->match_dest_port = msg->conn_rule.flow_ident_xlate;
1327
1328 reply_cm->xlate_src_ip = tuple->return_ip;
1329 reply_cm->xlate_src_port = tuple->return_ident;
1330 reply_cm->xlate_dest_ip = tuple->flow_ip;
1331 reply_cm->xlate_dest_port = tuple->flow_ident;;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301332
1333 atomic_set(&reply_cm->rx_packet_count, 0);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001334 reply_cm->rx_packet_count64 = 0;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301335 atomic_set(&reply_cm->rx_byte_count, 0);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001336 reply_cm->rx_byte_count64 = 0;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301337
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001338 reply_cm->xmit_dev = src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301339 reply_cm->xmit_dev_mtu = msg->conn_rule.flow_mtu;
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301340
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001341 reply_cm->connection = c;
1342 reply_cm->counter_match = original_cm;
Ken Zhu37040ea2021-09-09 21:11:15 -07001343
Ken Zhu37040ea2021-09-09 21:11:15 -07001344 if (msg->valid_flags & SFE_RULE_CREATE_MARK_VALID) {
1345 reply_cm->mark = msg->mark_rule.return_mark;
1346 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_MARK;
1347 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301348 if (msg->valid_flags & SFE_RULE_CREATE_QOS_VALID) {
1349 reply_cm->priority = msg->qos_rule.return_qos_tag;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001350 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PRIORITY_REMARK;
1351 }
Wayne Tanbb7f1782021-12-13 11:16:04 -08001352
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301353 if (msg->valid_flags & SFE_RULE_CREATE_DSCP_MARKING_VALID) {
1354 reply_cm->dscp = msg->dscp_rule.return_dscp << SFE_IPV4_DSCP_SHIFT;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001355 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_DSCP_REMARK;
1356 }
Amitesh Anand63be37d2021-12-24 20:51:48 +05301357
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301358 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1359 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_BRIDGE_FLOW;
1360 }
1361
Amitesh Anand63be37d2021-12-24 20:51:48 +05301362 /*
1363 * Setup UDP Socket if found to be valid for decap.
1364 */
1365 RCU_INIT_POINTER(reply_cm->up, NULL);
1366 net = dev_net(reply_cm->match_dev);
1367 src_if_idx = src_dev->ifindex;
1368
1369 rcu_read_lock();
1370
1371 /*
1372 * Look for the associated sock object.
1373 * __udp4_lib_lookup() holds a reference for this sock object,
1374 * which will be released in sfe_ipv4_free_connection_rcu()
1375 */
1376#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
1377 sk = __udp4_lib_lookup(net, reply_cm->match_dest_ip, reply_cm->match_dest_port,
1378 reply_cm->xlate_src_ip, reply_cm->xlate_src_port, src_if_idx, &udp_table);
1379#else
1380 sk = __udp4_lib_lookup(net, reply_cm->match_dest_ip, reply_cm->match_dest_port,
1381 reply_cm->xlate_src_ip, reply_cm->xlate_src_port, src_if_idx, 0, &udp_table, NULL);
1382#endif
1383
1384 rcu_read_unlock();
1385
1386 /*
1387 * We set the UDP sock pointer as valid only for decap direction.
1388 */
1389 if (sk && udp_sk(sk)->encap_type) {
1390#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
1391 if (!atomic_add_unless(&sk->sk_refcnt, 1, 0)) {
1392#else
1393 if (!refcount_inc_not_zero(&sk->sk_refcnt)) {
1394#endif
Wayne Tanbb7f1782021-12-13 11:16:04 -08001395 spin_unlock_bh(&si->lock);
Amitesh Anand63be37d2021-12-24 20:51:48 +05301396 kfree(reply_cm);
1397 kfree(original_cm);
1398 kfree(c);
1399
1400 DEBUG_TRACE("%px: sfe: unable to take reference for socket(%px) p:%d\n"
1401 " s: %s:%pM:%pI4:%u, d: %s:%pM:%pI4:%u\n",
1402 msg, sk, tuple->protocol,
1403 src_dev->name, msg->conn_rule.flow_mac, &tuple->flow_ip, ntohs(tuple->flow_ident),
1404 dest_dev->name, msg->conn_rule.return_mac, &tuple->return_ip, ntohs(tuple->return_ident));
1405
1406 dev_put(src_dev);
1407 dev_put(dest_dev);
1408
1409 return -ESHUTDOWN;
1410 }
1411
1412 rcu_assign_pointer(reply_cm->up, udp_sk(sk));
1413
1414 DEBUG_INFO("%px: Sock(%px) lookup success with reply_cm direction\n", msg, sk);
1415 DEBUG_INFO("%px: SFE connection -\n"
1416 " s: %s:%pI4(%pI4):%u(%u)\n"
1417 " d: %s:%pI4(%pI4):%u(%u)\n",
1418 msg, reply_cm->match_dev->name, &reply_cm->match_src_ip, &reply_cm->xlate_src_ip,
1419 ntohs(reply_cm->match_src_port), ntohs(reply_cm->xlate_src_port),
1420 reply_cm->xmit_dev->name, &reply_cm->match_dest_ip, &reply_cm->xlate_dest_ip,
1421 ntohs(reply_cm->match_dest_port), ntohs(reply_cm->xlate_dest_port));
1422 }
1423
Wayne Tanbb7f1782021-12-13 11:16:04 -08001424 /*
1425 * Add VLAN rule to reply_cm
1426 */
1427 if (msg->valid_flags & SFE_RULE_CREATE_VLAN_VALID) {
1428 struct sfe_vlan_rule *vlan_primary_rule = &msg->vlan_primary_rule;
1429 struct sfe_vlan_rule *vlan_secondary_rule = &msg->vlan_secondary_rule;
1430 sfe_ipv4_match_entry_set_vlan(reply_cm,
1431 vlan_primary_rule->egress_vlan_tag,
1432 vlan_primary_rule->ingress_vlan_tag,
1433 vlan_secondary_rule->egress_vlan_tag,
1434 vlan_secondary_rule->ingress_vlan_tag);
1435
1436 if ((msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_FLOW_BOTTOM_INTERFACE) &&
1437 reply_cm->egress_vlan_hdr_cnt > 0) {
1438 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_INSERT_EGRESS_VLAN_TAG;
1439 reply_cm->l2_hdr_size += reply_cm->egress_vlan_hdr_cnt * VLAN_HLEN;
1440 }
1441 }
1442
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001443#ifdef CONFIG_NF_FLOW_COOKIE
1444 reply_cm->flow_cookie = 0;
1445#endif
Zhi Chen8748eb32015-06-18 12:58:48 -07001446#ifdef CONFIG_XFRM
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301447 if (msg->valid_flags & SFE_RULE_CREATE_DIRECTION_VALID) {
1448 reply_cm->flow_accel = msg->direction_rule.return_accel;
1449 } else {
1450 reply_cm->flow_accel = 1;
1451 }
1452
Zhi Chen8748eb32015-06-18 12:58:48 -07001453#endif
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301454 /*
1455 * If l2_features are disabled and flow uses l2 features such as macvlan/bridge/pppoe/vlan,
1456 * bottom interfaces are expected to be disabled in the flow rule and always top interfaces
1457 * are used. In such cases, do not use HW csum offload. csum offload is used only when we
1458 * are sending directly to the destination interface that supports it.
1459 */
Suruchi Sumanf2077182022-01-13 21:35:23 +05301460 if (likely(src_dev->features & NETIF_F_HW_CSUM) && !(netif_is_vxlan(src_dev) || netif_is_vxlan(dest_dev))) {
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301461 if ((msg->conn_rule.flow_top_interface_num == msg->conn_rule.flow_interface_num) ||
1462 (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_FLOW_BOTTOM_INTERFACE)) {
1463 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_CSUM_OFFLOAD;
1464 }
1465 }
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001466
1467 /*
Ken Zhubbf49652021-09-12 15:33:09 -07001468 * For the non-arp interface, we don't write L2 HDR.
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001469 */
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +05301470 if (sfe_ipv4_xmit_eth_type_check(src_dev, reply_cm->flags)) {
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301471
1472 /*
1473 * Check whether the rule has configured a specific source MAC address to use.
1474 * This is needed when virtual L3 interfaces such as br-lan, macvlan, vlan are used during egress
1475 */
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301476
1477 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1478 ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)msg->conn_rule.return_mac);
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301479 } else {
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301480 if ((msg->valid_flags & SFE_RULE_CREATE_SRC_MAC_VALID) &&
1481 (msg->src_mac_rule.mac_valid_flags & SFE_SRC_MAC_FLOW_VALID)) {
1482 ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)msg->src_mac_rule.flow_src_mac);
1483 } else {
1484 ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)src_dev->dev_addr);
1485 }
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301486 }
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301487
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301488 ether_addr_copy((u8 *)reply_cm->xmit_dest_mac, (u8 *)msg->conn_rule.flow_mac);
1489
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001490 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_L2_HDR;
Wayne Tanbb7f1782021-12-13 11:16:04 -08001491 reply_cm->l2_hdr_size += ETH_HLEN;
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001492
1493 /*
1494 * If our dev writes Ethernet headers then we can write a really fast
1495 * version.
1496 */
1497 if (src_dev->header_ops) {
1498 if (src_dev->header_ops->create == eth_header) {
1499 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR;
1500 }
1501 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001502 }
1503
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301504 if ((tuple->return_ip != msg->conn_rule.return_ip_xlate) ||
1505 (tuple->return_ident != msg->conn_rule.return_ident_xlate)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001506 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST;
1507 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC;
1508 }
1509
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301510 if ((tuple->flow_ip != msg->conn_rule.flow_ip_xlate) ||
1511 (tuple->flow_ident != msg->conn_rule.flow_ident_xlate)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001512 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC;
1513 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST;
1514 }
1515
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001516 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001517 * Initialize the protocol-specific information that we track.
1518 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301519 switch (tuple->protocol) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001520 case IPPROTO_TCP:
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301521 original_cm->protocol_state.tcp.win_scale = msg->tcp_rule.flow_window_scale;
1522 original_cm->protocol_state.tcp.max_win = msg->tcp_rule.flow_max_window ? msg->tcp_rule.flow_max_window : 1;
1523 original_cm->protocol_state.tcp.end = msg->tcp_rule.flow_end;
1524 original_cm->protocol_state.tcp.max_end = msg->tcp_rule.flow_max_end;
1525
1526 reply_cm->protocol_state.tcp.win_scale = msg->tcp_rule.return_window_scale;
1527 reply_cm->protocol_state.tcp.max_win = msg->tcp_rule.return_max_window ? msg->tcp_rule.return_max_window : 1;
1528 reply_cm->protocol_state.tcp.end = msg->tcp_rule.return_end;
1529 reply_cm->protocol_state.tcp.max_end = msg->tcp_rule.return_max_end;
1530
1531 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_NO_SEQ_CHECK) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001532 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
1533 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
1534 }
1535 break;
1536 }
1537
Wayne Tanbb7f1782021-12-13 11:16:04 -08001538 /*
1539 * Fill in the ipv4_connection object.
1540 */
1541 c->protocol = tuple->protocol;
1542 c->src_ip = tuple->flow_ip;
1543 c->src_ip_xlate = msg->conn_rule.flow_ip_xlate;
1544 c->src_port = tuple->flow_ident;
1545 c->src_port_xlate = msg->conn_rule.flow_ident_xlate;
1546 c->original_dev = src_dev;
1547 c->original_match = original_cm;
1548 c->dest_ip = tuple->return_ip;
1549 c->dest_ip_xlate = msg->conn_rule.return_ip_xlate;
1550 c->dest_port = tuple->return_ident;
1551 c->dest_port_xlate = msg->conn_rule.return_ident_xlate;
1552 c->reply_dev = dest_dev;
1553 c->reply_match = reply_cm;
1554 c->debug_read_seq = 0;
1555 c->last_sync_jiffies = get_jiffies_64();
1556 c->removed = false;
1557
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001558 sfe_ipv4_connection_match_compute_translations(original_cm);
1559 sfe_ipv4_connection_match_compute_translations(reply_cm);
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301560 sfe_ipv4_insert_connection(si, c);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001561
1562 spin_unlock_bh(&si->lock);
1563
1564 /*
1565 * We have everything we need!
1566 */
Wayne Tanbb7f1782021-12-13 11:16:04 -08001567 DEBUG_INFO("NEW connection - p: %d\n"
1568 "original_cm: match_dev=src_dev: %s %d %pM\n"
1569 " xmit_dev=dest_dev: %s %d %pM\n"
1570 " xmit_src_mac: %pM\n"
1571 " xmit_dest_mac: %pM\n"
1572 " flags: %x l2_hdr: %u\n"
1573 "flow_ip: %pI4:%u\n"
1574 "flow_ip_xlate: %pI4:%u\n"
1575 "flow_mac: %pM\n"
1576 "reply_cm: match_dev=dest_dev: %s %d %pM\n"
1577 " xmit_dev=src_dev: %s %d %pM\n"
1578 " xmit_src_mac: %pM\n"
1579 " xmit_dest_mac: %pM\n"
1580 " flags: %x l2_hdr: %u\n"
1581 "return_ip: %pI4:%u\n"
1582 "return_ip_xlate: %pI4:%u\n"
1583 "return_mac: %pM\n"
1584 "flags: valid=%x src_mac_valid=%x\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301585 tuple->protocol,
Wayne Tanbb7f1782021-12-13 11:16:04 -08001586 original_cm->match_dev->name, original_cm->match_dev->ifindex, original_cm->match_dev->dev_addr,
1587 original_cm->xmit_dev->name, original_cm->xmit_dev->ifindex, original_cm->xmit_dev->dev_addr,
1588 original_cm->xmit_src_mac, original_cm->xmit_dest_mac, original_cm->flags, original_cm->l2_hdr_size,
1589 &tuple->flow_ip, ntohs(tuple->flow_ident),
1590 &msg->conn_rule.flow_ip_xlate, ntohs(msg->conn_rule.flow_ident_xlate),
1591 msg->conn_rule.flow_mac,
1592 reply_cm->match_dev->name, reply_cm->match_dev->ifindex, reply_cm->match_dev->dev_addr,
1593 reply_cm->xmit_dev->name, reply_cm->xmit_dev->ifindex, reply_cm->xmit_dev->dev_addr,
1594 reply_cm->xmit_src_mac, reply_cm->xmit_dest_mac, reply_cm->flags, reply_cm->l2_hdr_size,
1595 &tuple->return_ip, ntohs(tuple->return_ident),
1596 &msg->conn_rule.return_ip_xlate, ntohs(msg->conn_rule.return_ident_xlate),
1597 msg->conn_rule.return_mac,
1598 msg->valid_flags, msg->src_mac_rule.mac_valid_flags);
Nicolas Costa514fde02014-01-13 15:50:29 -06001599
1600 return 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001601}
1602
1603/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001604 * sfe_ipv4_destroy_rule()
1605 * Destroy a forwarding rule.
1606 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301607void sfe_ipv4_destroy_rule(struct sfe_ipv4_rule_destroy_msg *msg)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001608{
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001609 struct sfe_ipv4 *si = &__si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001610 struct sfe_ipv4_connection *c;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301611 bool ret;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301612 struct sfe_ipv4_5tuple *tuple = &msg->tuple;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001613
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301614 this_cpu_inc(si->stats_pcpu->connection_destroy_requests64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001615 spin_lock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001616
1617 /*
1618 * Check to see if we have a flow that matches the rule we're trying
1619 * to destroy. If there isn't then we can't destroy it.
1620 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301621 c = sfe_ipv4_find_connection(si, tuple->protocol, tuple->flow_ip, tuple->flow_ident,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301622 tuple->return_ip, tuple->return_ident);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001623 if (!c) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001624 spin_unlock_bh(&si->lock);
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301625 this_cpu_inc(si->stats_pcpu->connection_destroy_misses64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001626
1627 DEBUG_TRACE("connection does not exist - p: %d, s: %pI4:%u, d: %pI4:%u\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301628 tuple->protocol, &tuple->flow_ip, ntohs(tuple->flow_ident),
1629 &tuple->return_ip, ntohs(tuple->return_ident));
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001630 return;
1631 }
1632
1633 /*
1634 * Remove our connection details from the hash tables.
1635 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301636 ret = sfe_ipv4_remove_connection(si, c);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001637 spin_unlock_bh(&si->lock);
1638
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301639 if (ret) {
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301640 sfe_ipv4_flush_connection(si, c, SFE_SYNC_REASON_DESTROY);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301641 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001642
1643 DEBUG_INFO("connection destroyed - p: %d, s: %pI4:%u, d: %pI4:%u\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301644 tuple->protocol, &tuple->flow_ip, ntohs(tuple->flow_ident),
1645 &tuple->return_ip, ntohs(tuple->return_ident));
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001646}
1647
1648/*
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001649 * sfe_ipv4_register_sync_rule_callback()
1650 * Register a callback for rule synchronization.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001651 */
Xiaoping Fand44a5b42015-05-26 17:37:37 -07001652void sfe_ipv4_register_sync_rule_callback(sfe_sync_rule_callback_t sync_rule_callback)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001653{
1654 struct sfe_ipv4 *si = &__si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001655
1656 spin_lock_bh(&si->lock);
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001657 rcu_assign_pointer(si->sync_rule_callback, sync_rule_callback);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001658 spin_unlock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001659}
1660
1661/*
1662 * sfe_ipv4_get_debug_dev()
1663 */
1664static ssize_t sfe_ipv4_get_debug_dev(struct device *dev,
1665 struct device_attribute *attr,
1666 char *buf)
1667{
1668 struct sfe_ipv4 *si = &__si;
1669 ssize_t count;
1670 int num;
1671
1672 spin_lock_bh(&si->lock);
1673 num = si->debug_dev;
1674 spin_unlock_bh(&si->lock);
1675
1676 count = snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", num);
1677 return count;
1678}
1679
1680/*
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001681 * sysfs attributes.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001682 */
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001683static const struct device_attribute sfe_ipv4_debug_dev_attr =
Xiaoping Fane70da412016-02-26 16:47:57 -08001684 __ATTR(debug_dev, S_IWUSR | S_IRUGO, sfe_ipv4_get_debug_dev, NULL);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001685
1686/*
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001687 * sfe_ipv4_destroy_all_rules_for_dev()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001688 * Destroy all connections that match a particular device.
1689 *
1690 * If we pass dev as NULL then this destroys all connections.
1691 */
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001692void sfe_ipv4_destroy_all_rules_for_dev(struct net_device *dev)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001693{
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001694 struct sfe_ipv4 *si = &__si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001695 struct sfe_ipv4_connection *c;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301696 bool ret;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001697
Xiaoping Fan34586472015-07-03 02:20:35 -07001698another_round:
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001699 spin_lock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001700
Xiaoping Fan34586472015-07-03 02:20:35 -07001701 for (c = si->all_connections_head; c; c = c->all_connections_next) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001702 /*
Xiaoping Fan34586472015-07-03 02:20:35 -07001703 * Does this connection relate to the device we are destroying?
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001704 */
1705 if (!dev
1706 || (dev == c->original_dev)
1707 || (dev == c->reply_dev)) {
Xiaoping Fan34586472015-07-03 02:20:35 -07001708 break;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001709 }
Xiaoping Fan34586472015-07-03 02:20:35 -07001710 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001711
Xiaoping Fan34586472015-07-03 02:20:35 -07001712 if (c) {
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301713 ret = sfe_ipv4_remove_connection(si, c);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001714 }
1715
1716 spin_unlock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07001717
1718 if (c) {
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301719 if (ret) {
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301720 sfe_ipv4_flush_connection(si, c, SFE_SYNC_REASON_DESTROY);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301721 }
Xiaoping Fan34586472015-07-03 02:20:35 -07001722 goto another_round;
1723 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001724}
1725
1726/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001727 * sfe_ipv4_periodic_sync()
1728 */
Ken Zhu137722d2021-09-23 17:57:36 -07001729static void sfe_ipv4_periodic_sync(struct work_struct *work)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001730{
Ken Zhu137722d2021-09-23 17:57:36 -07001731 struct sfe_ipv4 *si = container_of((struct delayed_work *)work, struct sfe_ipv4, sync_dwork);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07001732 u64 now_jiffies;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001733 int quota;
Xiaoping Fand44a5b42015-05-26 17:37:37 -07001734 sfe_sync_rule_callback_t sync_rule_callback;
Ken Zhudc423672021-09-02 18:27:01 -07001735 struct sfe_ipv4_connection *c;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001736
1737 now_jiffies = get_jiffies_64();
1738
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001739 rcu_read_lock();
1740 sync_rule_callback = rcu_dereference(si->sync_rule_callback);
1741 if (!sync_rule_callback) {
1742 rcu_read_unlock();
1743 goto done;
1744 }
1745
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001746 spin_lock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001747
1748 /*
Ken Zhudc423672021-09-02 18:27:01 -07001749 * If we have reached the end of the connection list, walk from
1750 * the connection head.
1751 */
1752 c = si->wc_next;
1753 if (unlikely(!c)) {
1754 c = si->all_connections_head;
1755 }
1756
1757 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001758 * Get an estimate of the number of connections to parse in this sync.
1759 */
1760 quota = (si->num_connections + 63) / 64;
1761
1762 /*
Ken Zhudc423672021-09-02 18:27:01 -07001763 * Walk the "all connection" list and sync the connection state.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001764 */
Ken Zhudc423672021-09-02 18:27:01 -07001765 while (likely(c && quota)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001766 struct sfe_ipv4_connection_match *cm;
1767 struct sfe_ipv4_connection_match *counter_cm;
Xiaoping Fand44a5b42015-05-26 17:37:37 -07001768 struct sfe_connection_sync sis;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001769
Ken Zhudc423672021-09-02 18:27:01 -07001770 cm = c->original_match;
1771 counter_cm = c->reply_match;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001772
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001773 /*
Ken Zhudc423672021-09-02 18:27:01 -07001774 * Didn't receive packets in the original direction or reply
1775 * direction, move to the next connection.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001776 */
Ken Zhudc423672021-09-02 18:27:01 -07001777 if ((!atomic_read(&cm->rx_packet_count)) && !(atomic_read(&counter_cm->rx_packet_count))) {
1778 c = c->all_connections_next;
1779 continue;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001780 }
1781
Ken Zhudc423672021-09-02 18:27:01 -07001782 quota--;
Matthew McClintockaf48f1e2014-01-23 15:29:19 -06001783
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301784 sfe_ipv4_gen_sync_connection(si, c, &sis, SFE_SYNC_REASON_STATS, now_jiffies);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001785
Ken Zhudc423672021-09-02 18:27:01 -07001786 si->wc_next = c->all_connections_next;
1787
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001788 /*
1789 * We don't want to be holding the lock when we sync!
1790 */
1791 spin_unlock_bh(&si->lock);
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001792 sync_rule_callback(&sis);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001793 spin_lock_bh(&si->lock);
Ken Zhudc423672021-09-02 18:27:01 -07001794
1795 /*
1796 * c must be set and used in the same lock/unlock window;
1797 * because c could be removed when we don't hold the lock,
1798 * so delay grabbing until after the callback and relock.
1799 */
1800 c = si->wc_next;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001801 }
1802
Ken Zhudc423672021-09-02 18:27:01 -07001803 /*
1804 * At the end of the sync, put the wc_next to the connection we left.
1805 */
1806 si->wc_next = c;
1807
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001808 spin_unlock_bh(&si->lock);
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001809 rcu_read_unlock();
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001810
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001811done:
Ken Zhu137722d2021-09-23 17:57:36 -07001812 schedule_delayed_work_on(si->work_cpu, (struct delayed_work *)work, ((HZ + 99) / 100));
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001813}
1814
1815#define CHAR_DEV_MSG_SIZE 768
1816
1817/*
1818 * sfe_ipv4_debug_dev_read_start()
1819 * Generate part of the XML output.
1820 */
1821static bool sfe_ipv4_debug_dev_read_start(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
1822 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
1823{
1824 int bytes_read;
1825
Xiaoping Fan34586472015-07-03 02:20:35 -07001826 si->debug_read_seq++;
1827
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001828 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "<sfe_ipv4>\n");
1829 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1830 return false;
1831 }
1832
1833 *length -= bytes_read;
1834 *total_read += bytes_read;
1835
1836 ws->state++;
1837 return true;
1838}
1839
1840/*
1841 * sfe_ipv4_debug_dev_read_connections_start()
1842 * Generate part of the XML output.
1843 */
1844static bool sfe_ipv4_debug_dev_read_connections_start(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
1845 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
1846{
1847 int bytes_read;
1848
1849 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<connections>\n");
1850 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1851 return false;
1852 }
1853
1854 *length -= bytes_read;
1855 *total_read += bytes_read;
1856
1857 ws->state++;
1858 return true;
1859}
1860
1861/*
1862 * sfe_ipv4_debug_dev_read_connections_connection()
1863 * Generate part of the XML output.
1864 */
1865static bool sfe_ipv4_debug_dev_read_connections_connection(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
1866 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
1867{
1868 struct sfe_ipv4_connection *c;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001869 struct sfe_ipv4_connection_match *original_cm;
1870 struct sfe_ipv4_connection_match *reply_cm;
1871 int bytes_read;
1872 int protocol;
1873 struct net_device *src_dev;
Dave Hudson87973cd2013-10-22 16:00:04 +01001874 __be32 src_ip;
1875 __be32 src_ip_xlate;
1876 __be16 src_port;
1877 __be16 src_port_xlate;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07001878 u64 src_rx_packets;
1879 u64 src_rx_bytes;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001880 struct net_device *dest_dev;
Dave Hudson87973cd2013-10-22 16:00:04 +01001881 __be32 dest_ip;
1882 __be32 dest_ip_xlate;
1883 __be16 dest_port;
1884 __be16 dest_port_xlate;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07001885 u64 dest_rx_packets;
1886 u64 dest_rx_bytes;
1887 u64 last_sync_jiffies;
Ken Zhu37040ea2021-09-09 21:11:15 -07001888 u32 src_mark, dest_mark, src_priority, dest_priority, src_dscp, dest_dscp;
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301889 u32 packet, byte, original_cm_flags;
1890 u16 pppoe_session_id;
1891 u8 pppoe_remote_mac[ETH_ALEN];
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001892#ifdef CONFIG_NF_FLOW_COOKIE
1893 int src_flow_cookie, dst_flow_cookie;
1894#endif
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001895
1896 spin_lock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07001897
1898 for (c = si->all_connections_head; c; c = c->all_connections_next) {
1899 if (c->debug_read_seq < si->debug_read_seq) {
1900 c->debug_read_seq = si->debug_read_seq;
1901 break;
1902 }
1903 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001904
1905 /*
Xiaoping Fan34586472015-07-03 02:20:35 -07001906 * If there were no connections then move to the next state.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001907 */
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301908 if (!c || c->removed) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001909 spin_unlock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07001910 ws->state++;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001911 return true;
1912 }
1913
1914 original_cm = c->original_match;
1915 reply_cm = c->reply_match;
1916
1917 protocol = c->protocol;
1918 src_dev = c->original_dev;
1919 src_ip = c->src_ip;
1920 src_ip_xlate = c->src_ip_xlate;
1921 src_port = c->src_port;
1922 src_port_xlate = c->src_port_xlate;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001923 src_priority = original_cm->priority;
1924 src_dscp = original_cm->dscp >> SFE_IPV4_DSCP_SHIFT;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001925
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301926 sfe_ipv4_connection_match_update_summary_stats(original_cm, &packet, &byte);
1927 sfe_ipv4_connection_match_update_summary_stats(reply_cm, &packet, &byte);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001928
1929 src_rx_packets = original_cm->rx_packet_count64;
1930 src_rx_bytes = original_cm->rx_byte_count64;
Ken Zhu37040ea2021-09-09 21:11:15 -07001931 src_mark = original_cm->mark;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001932 dest_dev = c->reply_dev;
1933 dest_ip = c->dest_ip;
1934 dest_ip_xlate = c->dest_ip_xlate;
1935 dest_port = c->dest_port;
1936 dest_port_xlate = c->dest_port_xlate;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001937 dest_priority = reply_cm->priority;
1938 dest_dscp = reply_cm->dscp >> SFE_IPV4_DSCP_SHIFT;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001939 dest_rx_packets = reply_cm->rx_packet_count64;
1940 dest_rx_bytes = reply_cm->rx_byte_count64;
Ken Zhu37040ea2021-09-09 21:11:15 -07001941 dest_mark = reply_cm->mark;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001942 last_sync_jiffies = get_jiffies_64() - c->last_sync_jiffies;
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301943 original_cm_flags = original_cm->flags;
1944 pppoe_session_id = original_cm->pppoe_session_id;
1945 ether_addr_copy(pppoe_remote_mac, original_cm->pppoe_remote_mac);
1946
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001947#ifdef CONFIG_NF_FLOW_COOKIE
1948 src_flow_cookie = original_cm->flow_cookie;
1949 dst_flow_cookie = reply_cm->flow_cookie;
1950#endif
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001951 spin_unlock_bh(&si->lock);
1952
1953 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t\t<connection "
1954 "protocol=\"%u\" "
1955 "src_dev=\"%s\" "
1956 "src_ip=\"%pI4\" src_ip_xlate=\"%pI4\" "
1957 "src_port=\"%u\" src_port_xlate=\"%u\" "
Xiaoping Fane1963d42015-08-25 17:06:19 -07001958 "src_priority=\"%u\" src_dscp=\"%u\" "
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001959 "src_rx_pkts=\"%llu\" src_rx_bytes=\"%llu\" "
Ken Zhu37040ea2021-09-09 21:11:15 -07001960 "src_mark=\"%08x\" "
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001961 "dest_dev=\"%s\" "
1962 "dest_ip=\"%pI4\" dest_ip_xlate=\"%pI4\" "
1963 "dest_port=\"%u\" dest_port_xlate=\"%u\" "
Xiaoping Fane1963d42015-08-25 17:06:19 -07001964 "dest_priority=\"%u\" dest_dscp=\"%u\" "
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001965 "dest_rx_pkts=\"%llu\" dest_rx_bytes=\"%llu\" "
Ken Zhu37040ea2021-09-09 21:11:15 -07001966 "dest_mark=\"%08x\" "
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001967#ifdef CONFIG_NF_FLOW_COOKIE
1968 "src_flow_cookie=\"%d\" dst_flow_cookie=\"%d\" "
1969#endif
Ken Zhu37040ea2021-09-09 21:11:15 -07001970 "last_sync=\"%llu\" ",
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001971 protocol,
1972 src_dev->name,
1973 &src_ip, &src_ip_xlate,
Dave Hudson87973cd2013-10-22 16:00:04 +01001974 ntohs(src_port), ntohs(src_port_xlate),
Xiaoping Fane1963d42015-08-25 17:06:19 -07001975 src_priority, src_dscp,
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001976 src_rx_packets, src_rx_bytes,
Ken Zhu37040ea2021-09-09 21:11:15 -07001977 src_mark,
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001978 dest_dev->name,
1979 &dest_ip, &dest_ip_xlate,
Dave Hudson87973cd2013-10-22 16:00:04 +01001980 ntohs(dest_port), ntohs(dest_port_xlate),
Xiaoping Fane1963d42015-08-25 17:06:19 -07001981 dest_priority, dest_dscp,
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001982 dest_rx_packets, dest_rx_bytes,
Ken Zhu37040ea2021-09-09 21:11:15 -07001983 dest_mark,
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001984#ifdef CONFIG_NF_FLOW_COOKIE
1985 src_flow_cookie, dst_flow_cookie,
1986#endif
Ken Zhu37040ea2021-09-09 21:11:15 -07001987 last_sync_jiffies);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001988
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301989 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 +05301990 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 +05301991 pppoe_session_id, pppoe_remote_mac);
1992 }
1993
1994 bytes_read += snprintf(msg + bytes_read, CHAR_DEV_MSG_SIZE, "/>\n");
1995
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001996 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1997 return false;
1998 }
1999
2000 *length -= bytes_read;
2001 *total_read += bytes_read;
2002
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002003 return true;
2004}
2005
2006/*
2007 * sfe_ipv4_debug_dev_read_connections_end()
2008 * Generate part of the XML output.
2009 */
2010static bool sfe_ipv4_debug_dev_read_connections_end(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2011 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2012{
2013 int bytes_read;
2014
2015 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t</connections>\n");
2016 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2017 return false;
2018 }
2019
2020 *length -= bytes_read;
2021 *total_read += bytes_read;
2022
2023 ws->state++;
2024 return true;
2025}
2026
2027/*
2028 * sfe_ipv4_debug_dev_read_exceptions_start()
2029 * Generate part of the XML output.
2030 */
2031static bool sfe_ipv4_debug_dev_read_exceptions_start(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2032 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2033{
2034 int bytes_read;
2035
2036 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<exceptions>\n");
2037 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2038 return false;
2039 }
2040
2041 *length -= bytes_read;
2042 *total_read += bytes_read;
2043
2044 ws->state++;
2045 return true;
2046}
2047
2048/*
2049 * sfe_ipv4_debug_dev_read_exceptions_exception()
2050 * Generate part of the XML output.
2051 */
2052static bool sfe_ipv4_debug_dev_read_exceptions_exception(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2053 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2054{
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302055 int i;
2056 u64 val = 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002057
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302058 for_each_possible_cpu(i) {
2059 const struct sfe_ipv4_stats *s = per_cpu_ptr(si->stats_pcpu, i);
2060 val += s->exception_events64[ws->iter_exception];
2061 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002062
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302063 if (val) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002064 int bytes_read;
2065
2066 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE,
2067 "\t\t<exception name=\"%s\" count=\"%llu\" />\n",
2068 sfe_ipv4_exception_events_string[ws->iter_exception],
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302069 val);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002070 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2071 return false;
2072 }
2073
2074 *length -= bytes_read;
2075 *total_read += bytes_read;
2076 }
2077
2078 ws->iter_exception++;
2079 if (ws->iter_exception >= SFE_IPV4_EXCEPTION_EVENT_LAST) {
2080 ws->iter_exception = 0;
2081 ws->state++;
2082 }
2083
2084 return true;
2085}
2086
2087/*
2088 * sfe_ipv4_debug_dev_read_exceptions_end()
2089 * Generate part of the XML output.
2090 */
2091static bool sfe_ipv4_debug_dev_read_exceptions_end(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2092 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2093{
2094 int bytes_read;
2095
2096 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t</exceptions>\n");
2097 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2098 return false;
2099 }
2100
2101 *length -= bytes_read;
2102 *total_read += bytes_read;
2103
2104 ws->state++;
2105 return true;
2106}
2107
2108/*
2109 * sfe_ipv4_debug_dev_read_stats()
2110 * Generate part of the XML output.
2111 */
2112static bool sfe_ipv4_debug_dev_read_stats(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2113 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2114{
2115 int bytes_read;
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302116 struct sfe_ipv4_stats stats;
2117 unsigned int num_conn;
2118
2119 sfe_ipv4_update_summary_stats(si, &stats);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002120
2121 spin_lock_bh(&si->lock);
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302122 num_conn = si->num_connections;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002123 spin_unlock_bh(&si->lock);
2124
2125 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<stats "
2126 "num_connections=\"%u\" "
Amitesh Anand63be37d2021-12-24 20:51:48 +05302127 "pkts_dropped=\"%llu\" "
Xiaoping Fan59176422015-05-22 15:58:10 -07002128 "pkts_forwarded=\"%llu\" pkts_not_forwarded=\"%llu\" "
2129 "create_requests=\"%llu\" create_collisions=\"%llu\" "
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05302130 "create_failures=\"%llu\" "
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002131 "destroy_requests=\"%llu\" destroy_misses=\"%llu\" "
2132 "flushes=\"%llu\" "
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +05302133 "hash_hits=\"%llu\" hash_reorders=\"%llu\" "
2134 "pppoe_encap_pkts_fwded=\"%llu\" "
Guduri Prathyusha034d6352022-01-12 16:49:04 +05302135 "pppoe_decap_pkts_fwded=\"%llu\" "
2136 "pppoe_bridge_pkts_fwded=\"%llu\" />\n",
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302137 num_conn,
Amitesh Anand63be37d2021-12-24 20:51:48 +05302138 stats.packets_dropped64,
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302139 stats.packets_forwarded64,
2140 stats.packets_not_forwarded64,
2141 stats.connection_create_requests64,
2142 stats.connection_create_collisions64,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05302143 stats.connection_create_failures64,
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302144 stats.connection_destroy_requests64,
2145 stats.connection_destroy_misses64,
2146 stats.connection_flushes64,
2147 stats.connection_match_hash_hits64,
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +05302148 stats.connection_match_hash_reorders64,
2149 stats.pppoe_encap_packets_forwarded64,
Guduri Prathyusha034d6352022-01-12 16:49:04 +05302150 stats.pppoe_decap_packets_forwarded64,
2151 stats.pppoe_bridge_packets_forwarded64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002152 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2153 return false;
2154 }
2155
2156 *length -= bytes_read;
2157 *total_read += bytes_read;
2158
2159 ws->state++;
2160 return true;
2161}
2162
2163/*
2164 * sfe_ipv4_debug_dev_read_end()
2165 * Generate part of the XML output.
2166 */
2167static bool sfe_ipv4_debug_dev_read_end(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2168 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2169{
2170 int bytes_read;
2171
2172 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "</sfe_ipv4>\n");
2173 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2174 return false;
2175 }
2176
2177 *length -= bytes_read;
2178 *total_read += bytes_read;
2179
2180 ws->state++;
2181 return true;
2182}
2183
2184/*
2185 * Array of write functions that write various XML elements that correspond to
2186 * our XML output state machine.
2187 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07002188static 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 +01002189 sfe_ipv4_debug_dev_read_start,
2190 sfe_ipv4_debug_dev_read_connections_start,
2191 sfe_ipv4_debug_dev_read_connections_connection,
2192 sfe_ipv4_debug_dev_read_connections_end,
2193 sfe_ipv4_debug_dev_read_exceptions_start,
2194 sfe_ipv4_debug_dev_read_exceptions_exception,
2195 sfe_ipv4_debug_dev_read_exceptions_end,
2196 sfe_ipv4_debug_dev_read_stats,
2197 sfe_ipv4_debug_dev_read_end,
2198};
2199
2200/*
2201 * sfe_ipv4_debug_dev_read()
2202 * Send info to userspace upon read request from user
2203 */
2204static ssize_t sfe_ipv4_debug_dev_read(struct file *filp, char *buffer, size_t length, loff_t *offset)
2205{
2206 char msg[CHAR_DEV_MSG_SIZE];
2207 int total_read = 0;
2208 struct sfe_ipv4_debug_xml_write_state *ws;
2209 struct sfe_ipv4 *si = &__si;
2210
2211 ws = (struct sfe_ipv4_debug_xml_write_state *)filp->private_data;
2212 while ((ws->state != SFE_IPV4_DEBUG_XML_STATE_DONE) && (length > CHAR_DEV_MSG_SIZE)) {
2213 if ((sfe_ipv4_debug_xml_write_methods[ws->state])(si, buffer, msg, &length, &total_read, ws)) {
2214 continue;
2215 }
2216 }
2217
2218 return total_read;
2219}
2220
2221/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002222 * sfe_ipv4_debug_dev_open()
2223 */
2224static int sfe_ipv4_debug_dev_open(struct inode *inode, struct file *file)
2225{
2226 struct sfe_ipv4_debug_xml_write_state *ws;
2227
2228 ws = (struct sfe_ipv4_debug_xml_write_state *)file->private_data;
2229 if (!ws) {
2230 ws = kzalloc(sizeof(struct sfe_ipv4_debug_xml_write_state), GFP_KERNEL);
2231 if (!ws) {
2232 return -ENOMEM;
2233 }
2234
2235 ws->state = SFE_IPV4_DEBUG_XML_STATE_START;
2236 file->private_data = ws;
2237 }
2238
2239 return 0;
2240}
2241
2242/*
2243 * sfe_ipv4_debug_dev_release()
2244 */
2245static int sfe_ipv4_debug_dev_release(struct inode *inode, struct file *file)
2246{
2247 struct sfe_ipv4_debug_xml_write_state *ws;
2248
2249 ws = (struct sfe_ipv4_debug_xml_write_state *)file->private_data;
2250 if (ws) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002251 /*
2252 * We've finished with our output so free the write state.
2253 */
2254 kfree(ws);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05302255 file->private_data = NULL;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002256 }
2257
2258 return 0;
2259}
2260
2261/*
2262 * File operations used in the debug char device
2263 */
2264static struct file_operations sfe_ipv4_debug_dev_fops = {
2265 .read = sfe_ipv4_debug_dev_read,
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002266 .open = sfe_ipv4_debug_dev_open,
2267 .release = sfe_ipv4_debug_dev_release
2268};
2269
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08002270#ifdef CONFIG_NF_FLOW_COOKIE
2271/*
2272 * sfe_register_flow_cookie_cb
2273 * register a function in SFE to let SFE use this function to configure flow cookie for a flow
2274 *
2275 * Hardware driver which support flow cookie should register a callback function in SFE. Then SFE
2276 * can use this function to configure flow cookie for a flow.
2277 * return: 0, success; !=0, fail
2278 */
2279int sfe_register_flow_cookie_cb(flow_cookie_set_func_t cb)
2280{
2281 struct sfe_ipv4 *si = &__si;
2282
2283 BUG_ON(!cb);
2284
2285 if (si->flow_cookie_set_func) {
2286 return -1;
2287 }
2288
2289 rcu_assign_pointer(si->flow_cookie_set_func, cb);
2290 return 0;
2291}
2292
2293/*
2294 * sfe_unregister_flow_cookie_cb
2295 * unregister function which is used to configure flow cookie for a flow
2296 *
2297 * return: 0, success; !=0, fail
2298 */
2299int sfe_unregister_flow_cookie_cb(flow_cookie_set_func_t cb)
2300{
2301 struct sfe_ipv4 *si = &__si;
2302
2303 RCU_INIT_POINTER(si->flow_cookie_set_func, NULL);
2304 return 0;
2305}
Xiaoping Fan640faf42015-08-28 15:50:55 -07002306
2307/*
2308 * sfe_ipv4_get_flow_cookie()
2309 */
2310static ssize_t sfe_ipv4_get_flow_cookie(struct device *dev,
2311 struct device_attribute *attr,
2312 char *buf)
2313{
2314 struct sfe_ipv4 *si = &__si;
Xiaoping Fan01c67cc2015-11-09 11:31:57 -08002315 return snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", si->flow_cookie_enable);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002316}
2317
2318/*
2319 * sfe_ipv4_set_flow_cookie()
2320 */
2321static ssize_t sfe_ipv4_set_flow_cookie(struct device *dev,
2322 struct device_attribute *attr,
2323 const char *buf, size_t size)
2324{
2325 struct sfe_ipv4 *si = &__si;
Ken Zhu137722d2021-09-23 17:57:36 -07002326 si->flow_cookie_enable = simple_strtol(buf, NULL, 0);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002327
2328 return size;
2329}
2330
2331/*
2332 * sysfs attributes.
2333 */
2334static const struct device_attribute sfe_ipv4_flow_cookie_attr =
Xiaoping Fane70da412016-02-26 16:47:57 -08002335 __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 -08002336#endif /*CONFIG_NF_FLOW_COOKIE*/
2337
Ken Zhu137722d2021-09-23 17:57:36 -07002338/*
2339 * sfe_ipv4_get_cpu()
2340 */
2341static ssize_t sfe_ipv4_get_cpu(struct device *dev,
2342 struct device_attribute *attr,
2343 char *buf)
2344{
2345 struct sfe_ipv4 *si = &__si;
2346 return snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", si->work_cpu);
2347}
2348
2349/*
2350 * sfe_ipv4_set_cpu()
2351 */
2352static ssize_t sfe_ipv4_set_cpu(struct device *dev,
2353 struct device_attribute *attr,
2354 const char *buf, size_t size)
2355{
2356 struct sfe_ipv4 *si = &__si;
2357 int work_cpu;
2358 work_cpu = simple_strtol(buf, NULL, 0);
2359 if ((work_cpu >= 0) && (work_cpu <= NR_CPUS)) {
2360 si->work_cpu = work_cpu;
2361 } else {
2362 dev_err(dev, "%s is not in valid range[0,%d]", buf, NR_CPUS);
2363 }
2364 return size;
2365}
2366/*
2367 * sysfs attributes.
2368 */
2369static const struct device_attribute sfe_ipv4_cpu_attr =
2370 __ATTR(stats_work_cpu, S_IWUSR | S_IRUGO, sfe_ipv4_get_cpu, sfe_ipv4_set_cpu);
2371
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05302372 /*
2373 * sfe_ipv4_conn_match_hash_init()
2374 * Initialize conn match hash lists
2375 */
2376static void sfe_ipv4_conn_match_hash_init(struct sfe_ipv4 *si, int len)
2377{
2378 struct hlist_head *hash_list = si->hlist_conn_match_hash_head;
2379 int i;
2380
2381 for (i = 0; i < len; i++) {
2382 INIT_HLIST_HEAD(&hash_list[i]);
2383 }
2384}
2385
Amitesh Anand63be37d2021-12-24 20:51:48 +05302386#ifdef SFE_PROCESS_LOCAL_OUT
2387/*
2388 * sfe_ipv4_local_out()
2389 * Called for packets from ip_local_out() - post encapsulation & other packets
2390 */
2391static unsigned int sfe_ipv4_local_out(void *priv, struct sk_buff *skb, const struct nf_hook_state *nhs)
2392{
Guduri Prathyusha5f27e232022-01-06 14:39:04 +05302393 struct sfe_l2_info l2_info;
2394 l2_info.parse_flags = 0;
2395
Amitesh Anand63be37d2021-12-24 20:51:48 +05302396 DEBUG_TRACE("%px: sfe: sfe_ipv4_local_out hook called.\n", skb);
2397
2398 if (likely(skb->skb_iif)) {
Guduri Prathyusha5f27e232022-01-06 14:39:04 +05302399 return sfe_ipv4_recv(skb->dev, skb, &l2_info, true) ? NF_STOLEN : NF_ACCEPT;
Amitesh Anand63be37d2021-12-24 20:51:48 +05302400 }
2401
2402 return NF_ACCEPT;
2403}
2404
2405/*
2406 * struct nf_hook_ops sfe_ipv4_ops_local_out[]
2407 * Hooks into netfilter local out packet monitoring points.
2408 */
2409static struct nf_hook_ops sfe_ipv4_ops_local_out[] __read_mostly = {
2410
2411 /*
2412 * Local out routing hook is used to monitor packets.
2413 */
2414 {
2415 .hook = sfe_ipv4_local_out,
2416 .pf = PF_INET,
2417 .hooknum = NF_INET_LOCAL_OUT,
2418 .priority = NF_IP_PRI_FIRST,
2419 },
2420};
2421#endif
2422
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002423/*
Dave Hudson87973cd2013-10-22 16:00:04 +01002424 * sfe_ipv4_init()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002425 */
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05302426int sfe_ipv4_init(void)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002427{
2428 struct sfe_ipv4 *si = &__si;
2429 int result = -1;
2430
Dave Hudsondcd08fb2013-11-22 09:25:16 -06002431 DEBUG_INFO("SFE IPv4 init\n");
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002432
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05302433 sfe_ipv4_conn_match_hash_init(si, ARRAY_SIZE(si->hlist_conn_match_hash_head));
2434
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302435 si->stats_pcpu = alloc_percpu_gfp(struct sfe_ipv4_stats, GFP_KERNEL | __GFP_ZERO);
2436 if (!si->stats_pcpu) {
2437 DEBUG_ERROR("failed to allocate stats memory for sfe_ipv4\n");
2438 goto exit0;
2439 }
2440
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002441 /*
2442 * Create sys/sfe_ipv4
2443 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302444 si->sys_ipv4 = kobject_create_and_add("sfe_ipv4", NULL);
2445 if (!si->sys_ipv4) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002446 DEBUG_ERROR("failed to register sfe_ipv4\n");
2447 goto exit1;
2448 }
2449
2450 /*
2451 * Create files, one for each parameter supported by this module.
2452 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302453 result = sysfs_create_file(si->sys_ipv4, &sfe_ipv4_debug_dev_attr.attr);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002454 if (result) {
2455 DEBUG_ERROR("failed to register debug dev file: %d\n", result);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002456 goto exit2;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002457 }
2458
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302459 result = sysfs_create_file(si->sys_ipv4, &sfe_ipv4_cpu_attr.attr);
Ken Zhu137722d2021-09-23 17:57:36 -07002460 if (result) {
2461 DEBUG_ERROR("failed to register debug dev file: %d\n", result);
2462 goto exit3;
2463 }
2464
Xiaoping Fan640faf42015-08-28 15:50:55 -07002465#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302466 result = sysfs_create_file(si->sys_ipv4, &sfe_ipv4_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002467 if (result) {
2468 DEBUG_ERROR("failed to register flow cookie enable file: %d\n", result);
Ken Zhu137722d2021-09-23 17:57:36 -07002469 goto exit4;
Xiaoping Fan640faf42015-08-28 15:50:55 -07002470 }
2471#endif /* CONFIG_NF_FLOW_COOKIE */
2472
Amitesh Anand63be37d2021-12-24 20:51:48 +05302473#ifdef SFE_PROCESS_LOCAL_OUT
2474#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
2475 result = nf_register_hooks(sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2476#else
2477 result = nf_register_net_hooks(&init_net, sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2478#endif
2479 if (result < 0) {
2480 DEBUG_ERROR("can't register nf local out hook: %d\n", result);
2481 goto exit5;
2482 }
2483 DEBUG_INFO("Register nf local out hook success: %d\n", result);
2484#endif
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002485 /*
2486 * Register our debug char device.
2487 */
2488 result = register_chrdev(0, "sfe_ipv4", &sfe_ipv4_debug_dev_fops);
2489 if (result < 0) {
2490 DEBUG_ERROR("Failed to register chrdev: %d\n", result);
Amitesh Anand63be37d2021-12-24 20:51:48 +05302491 goto exit6;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002492 }
2493
2494 si->debug_dev = result;
Ken Zhu137722d2021-09-23 17:57:36 -07002495 si->work_cpu = WORK_CPU_UNBOUND;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002496
2497 /*
Ken Zhu137722d2021-09-23 17:57:36 -07002498 * Create a work to handle periodic statistics.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002499 */
Ken Zhu137722d2021-09-23 17:57:36 -07002500 INIT_DELAYED_WORK(&(si->sync_dwork), sfe_ipv4_periodic_sync);
2501 schedule_delayed_work_on(si->work_cpu, &(si->sync_dwork), ((HZ + 99) / 100));
2502
Dave Hudson87973cd2013-10-22 16:00:04 +01002503 spin_lock_init(&si->lock);
Dave Hudson87973cd2013-10-22 16:00:04 +01002504 return 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002505
Amitesh Anand63be37d2021-12-24 20:51:48 +05302506exit6:
2507#ifdef SFE_PROCESS_LOCAL_OUT
2508 DEBUG_TRACE("sfe: Unregister local out hook\n");
2509#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
2510 nf_unregister_hooks(sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2511#else
2512 nf_unregister_net_hooks(&init_net, sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2513#endif
Ken Zhu137722d2021-09-23 17:57:36 -07002514exit5:
Amitesh Anand63be37d2021-12-24 20:51:48 +05302515#endif
Xiaoping Fan640faf42015-08-28 15:50:55 -07002516#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302517 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002518
Ken Zhu137722d2021-09-23 17:57:36 -07002519exit4:
Xiaoping Fan640faf42015-08-28 15:50:55 -07002520#endif /* CONFIG_NF_FLOW_COOKIE */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302521 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_cpu_attr.attr);
Ken Zhu137722d2021-09-23 17:57:36 -07002522exit3:
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302523 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_debug_dev_attr.attr);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002524
Xiaoping Fan640faf42015-08-28 15:50:55 -07002525exit2:
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302526 kobject_put(si->sys_ipv4);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002527
2528exit1:
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302529 free_percpu(si->stats_pcpu);
2530
2531exit0:
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002532 return result;
2533}
2534
2535/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002536 * sfe_ipv4_exit()
2537 */
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05302538void sfe_ipv4_exit(void)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002539{
Dave Hudson87973cd2013-10-22 16:00:04 +01002540 struct sfe_ipv4 *si = &__si;
2541
Dave Hudsondcd08fb2013-11-22 09:25:16 -06002542 DEBUG_INFO("SFE IPv4 exit\n");
Dave Hudson87973cd2013-10-22 16:00:04 +01002543 /*
2544 * Destroy all connections.
2545 */
Dave Hudsondcd08fb2013-11-22 09:25:16 -06002546 sfe_ipv4_destroy_all_rules_for_dev(NULL);
Dave Hudson87973cd2013-10-22 16:00:04 +01002547
Ken Zhu137722d2021-09-23 17:57:36 -07002548 cancel_delayed_work_sync(&si->sync_dwork);
Dave Hudson87973cd2013-10-22 16:00:04 +01002549
Dave Hudson87973cd2013-10-22 16:00:04 +01002550 unregister_chrdev(si->debug_dev, "sfe_ipv4");
2551
Amitesh Anand63be37d2021-12-24 20:51:48 +05302552#ifdef SFE_PROCESS_LOCAL_OUT
2553 DEBUG_TRACE("sfe: Unregister local out hook\n");
2554#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
2555 nf_unregister_hooks(sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2556#else
2557 nf_unregister_net_hooks(&init_net, sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2558#endif
2559#endif
2560
Xiaoping Fan640faf42015-08-28 15:50:55 -07002561#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302562 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002563#endif /* CONFIG_NF_FLOW_COOKIE */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302564 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_debug_dev_attr.attr);
2565 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_cpu_attr.attr);
Dave Hudson87973cd2013-10-22 16:00:04 +01002566
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302567 kobject_put(si->sys_ipv4);
Dave Hudson87973cd2013-10-22 16:00:04 +01002568
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302569 free_percpu(si->stats_pcpu);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002570}
2571
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08002572#ifdef CONFIG_NF_FLOW_COOKIE
2573EXPORT_SYMBOL(sfe_register_flow_cookie_cb);
2574EXPORT_SYMBOL(sfe_unregister_flow_cookie_cb);
2575#endif