blob: c4237bfc1a7298a764803d7f76e9135cef3e8fe0 [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",
83 "UNHANDLED_PROTOCOL"
84};
85
Xiaoping Fan6a1672f2016-08-17 19:58:12 -070086static struct sfe_ipv4 __si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010087
88/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010089 * sfe_ipv4_gen_ip_csum()
90 * Generate the IP checksum for an IPv4 header.
91 *
92 * Note that this function assumes that we have only 20 bytes of IP header.
93 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +053094u16 sfe_ipv4_gen_ip_csum(struct iphdr *iph)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010095{
Xiaoping Fan6a1672f2016-08-17 19:58:12 -070096 u32 sum;
97 u16 *i = (u16 *)iph;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010098
99 iph->check = 0;
100
101 /*
102 * Generate the sum.
103 */
104 sum = i[0] + i[1] + i[2] + i[3] + i[4] + i[5] + i[6] + i[7] + i[8] + i[9];
105
106 /*
107 * Fold it to ones-complement form.
108 */
109 sum = (sum & 0xffff) + (sum >> 16);
110 sum = (sum & 0xffff) + (sum >> 16);
111
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700112 return (u16)sum ^ 0xffff;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100113}
114
115/*
116 * sfe_ipv4_get_connection_match_hash()
117 * Generate the hash used in connection match lookups.
118 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700119static inline unsigned int sfe_ipv4_get_connection_match_hash(struct net_device *dev, u8 protocol,
Dave Hudson87973cd2013-10-22 16:00:04 +0100120 __be32 src_ip, __be16 src_port,
121 __be32 dest_ip, __be16 dest_port)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100122{
123 size_t dev_addr = (size_t)dev;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700124 u32 hash = ((u32)dev_addr) ^ 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
154 || cm->match_protocol != protocol
155 || cm->match_dev != dev) {
156 continue;
157 }
158
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530159 this_cpu_inc(si->stats_pcpu->connection_match_hash_hits64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100160
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530161 break;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100162 }
163
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100164 return cm;
165}
166
167/*
168 * sfe_ipv4_connection_match_update_summary_stats()
169 * Update the summary stats for a connection match entry.
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530170 *
171 * Stats are incremented atomically. So use atomic substraction to update summary
172 * stats.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100173 */
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530174static inline void sfe_ipv4_connection_match_update_summary_stats(struct sfe_ipv4_connection_match *cm,
175 u32 *packets, u32 *bytes)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100176{
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530177 u32 packet_count, byte_count;
178
179 packet_count = atomic_read(&cm->rx_packet_count);
180 cm->rx_packet_count64 += packet_count;
181 atomic_sub(packet_count, &cm->rx_packet_count);
182
183 byte_count = atomic_read(&cm->rx_byte_count);
184 cm->rx_byte_count64 += byte_count;
185 atomic_sub(byte_count, &cm->rx_byte_count);
186
187 *packets = packet_count;
188 *bytes = byte_count;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100189}
190
191/*
192 * sfe_ipv4_connection_match_compute_translations()
193 * Compute port and address translations for a connection match entry.
194 */
195static void sfe_ipv4_connection_match_compute_translations(struct sfe_ipv4_connection_match *cm)
196{
197 /*
198 * Before we insert the entry look to see if this is tagged as doing address
199 * translations. If it is then work out the adjustment that we need to apply
200 * to the transport checksum.
201 */
202 if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC) {
203 /*
204 * Precompute an incremental checksum adjustment so we can
205 * edit packets in this stream very quickly. The algorithm is from RFC1624.
206 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700207 u16 src_ip_hi = cm->match_src_ip >> 16;
208 u16 src_ip_lo = cm->match_src_ip & 0xffff;
209 u32 xlate_src_ip = ~cm->xlate_src_ip;
210 u16 xlate_src_ip_hi = xlate_src_ip >> 16;
211 u16 xlate_src_ip_lo = xlate_src_ip & 0xffff;
212 u16 xlate_src_port = ~cm->xlate_src_port;
213 u32 adj;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100214
215 /*
216 * When we compute this fold it down to a 16-bit offset
217 * as that way we can avoid having to do a double
218 * folding of the twos-complement result because the
219 * addition of 2 16-bit values cannot cause a double
220 * wrap-around!
221 */
222 adj = src_ip_hi + src_ip_lo + cm->match_src_port
223 + xlate_src_ip_hi + xlate_src_ip_lo + xlate_src_port;
224 adj = (adj & 0xffff) + (adj >> 16);
225 adj = (adj & 0xffff) + (adj >> 16);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700226 cm->xlate_src_csum_adjustment = (u16)adj;
Nicolas Costaac2979c2014-01-14 10:35:24 -0600227
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100228 }
229
230 if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST) {
231 /*
232 * Precompute an incremental checksum adjustment so we can
233 * edit packets in this stream very quickly. The algorithm is from RFC1624.
234 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700235 u16 dest_ip_hi = cm->match_dest_ip >> 16;
236 u16 dest_ip_lo = cm->match_dest_ip & 0xffff;
237 u32 xlate_dest_ip = ~cm->xlate_dest_ip;
238 u16 xlate_dest_ip_hi = xlate_dest_ip >> 16;
239 u16 xlate_dest_ip_lo = xlate_dest_ip & 0xffff;
240 u16 xlate_dest_port = ~cm->xlate_dest_port;
241 u32 adj;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100242
243 /*
244 * When we compute this fold it down to a 16-bit offset
245 * as that way we can avoid having to do a double
246 * folding of the twos-complement result because the
247 * addition of 2 16-bit values cannot cause a double
248 * wrap-around!
249 */
250 adj = dest_ip_hi + dest_ip_lo + cm->match_dest_port
251 + xlate_dest_ip_hi + xlate_dest_ip_lo + xlate_dest_port;
252 adj = (adj & 0xffff) + (adj >> 16);
253 adj = (adj & 0xffff) + (adj >> 16);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700254 cm->xlate_dest_csum_adjustment = (u16)adj;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100255 }
Xiaoping Fanad755af2015-04-01 16:58:46 -0700256
257 if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC) {
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700258 u32 adj = ~cm->match_src_ip + cm->xlate_src_ip;
Xiaoping Fanad755af2015-04-01 16:58:46 -0700259 if (adj < cm->xlate_src_ip) {
260 adj++;
261 }
262
263 adj = (adj & 0xffff) + (adj >> 16);
264 adj = (adj & 0xffff) + (adj >> 16);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700265 cm->xlate_src_partial_csum_adjustment = (u16)adj;
Xiaoping Fanad755af2015-04-01 16:58:46 -0700266 }
267
268 if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST) {
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700269 u32 adj = ~cm->match_dest_ip + cm->xlate_dest_ip;
Xiaoping Fanad755af2015-04-01 16:58:46 -0700270 if (adj < cm->xlate_dest_ip) {
271 adj++;
272 }
273
274 adj = (adj & 0xffff) + (adj >> 16);
275 adj = (adj & 0xffff) + (adj >> 16);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700276 cm->xlate_dest_partial_csum_adjustment = (u16)adj;
Xiaoping Fanad755af2015-04-01 16:58:46 -0700277 }
278
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100279}
280
281/*
282 * sfe_ipv4_update_summary_stats()
283 * Update the summary stats.
284 */
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530285static void sfe_ipv4_update_summary_stats(struct sfe_ipv4 *si, struct sfe_ipv4_stats *stats)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100286{
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530287 int i = 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100288
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530289 memset(stats, 0, sizeof(*stats));
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100290
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530291 for_each_possible_cpu(i) {
292 const struct sfe_ipv4_stats *s = per_cpu_ptr(si->stats_pcpu, i);
293
294 stats->connection_create_requests64 += s->connection_create_requests64;
295 stats->connection_create_collisions64 += s->connection_create_collisions64;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530296 stats->connection_create_failures64 += s->connection_create_failures64;
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530297 stats->connection_destroy_requests64 += s->connection_destroy_requests64;
298 stats->connection_destroy_misses64 += s->connection_destroy_misses64;
299 stats->connection_match_hash_hits64 += s->connection_match_hash_hits64;
300 stats->connection_match_hash_reorders64 += s->connection_match_hash_reorders64;
301 stats->connection_flushes64 += s->connection_flushes64;
Amitesh Anand63be37d2021-12-24 20:51:48 +0530302 stats->packets_dropped64 += s->packets_dropped64;
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530303 stats->packets_forwarded64 += s->packets_forwarded64;
304 stats->packets_not_forwarded64 += s->packets_not_forwarded64;
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +0530305 stats->pppoe_encap_packets_forwarded64 += s->pppoe_encap_packets_forwarded64;
306 stats->pppoe_decap_packets_forwarded64 += s->pppoe_decap_packets_forwarded64;
Guduri Prathyusha034d6352022-01-12 16:49:04 +0530307 stats->pppoe_bridge_packets_forwarded64 += s->pppoe_bridge_packets_forwarded64;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100308 }
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530309
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100310}
311
312/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530313 * sfe_ipv4_insert_connection_match()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100314 * Insert a connection match into the hash.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100315 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530316static inline void sfe_ipv4_insert_connection_match(struct sfe_ipv4 *si,
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700317 struct sfe_ipv4_connection_match *cm)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100318{
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100319 unsigned int conn_match_idx
320 = sfe_ipv4_get_connection_match_hash(cm->match_dev, cm->match_protocol,
321 cm->match_src_ip, cm->match_src_port,
322 cm->match_dest_ip, cm->match_dest_port);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700323
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530324 lockdep_assert_held(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100325
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530326 hlist_add_head_rcu(&cm->hnode, &si->hlist_conn_match_hash_head[conn_match_idx]);
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800327#ifdef CONFIG_NF_FLOW_COOKIE
Xiaoping Fan640faf42015-08-28 15:50:55 -0700328 if (!si->flow_cookie_enable)
329 return;
330
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800331 /*
332 * Configure hardware to put a flow cookie in packet of this flow,
333 * then we can accelerate the lookup process when we received this packet.
334 */
335 for (conn_match_idx = 1; conn_match_idx < SFE_FLOW_COOKIE_SIZE; conn_match_idx++) {
336 struct sfe_flow_cookie_entry *entry = &si->sfe_flow_cookie_table[conn_match_idx];
337
338 if ((NULL == entry->match) && time_is_before_jiffies(entry->last_clean_time + HZ)) {
339 flow_cookie_set_func_t func;
340
341 rcu_read_lock();
342 func = rcu_dereference(si->flow_cookie_set_func);
343 if (func) {
Xiaoping Fan59176422015-05-22 15:58:10 -0700344 if (!func(cm->match_protocol, cm->match_src_ip, cm->match_src_port,
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800345 cm->match_dest_ip, cm->match_dest_port, conn_match_idx)) {
346 entry->match = cm;
347 cm->flow_cookie = conn_match_idx;
348 }
349 }
350 rcu_read_unlock();
351
352 break;
353 }
354 }
355#endif
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100356}
357
358/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530359 * sfe_ipv4_remove_connection_match()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100360 * Remove a connection match object from the hash.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100361 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530362static inline void sfe_ipv4_remove_connection_match(struct sfe_ipv4 *si, struct sfe_ipv4_connection_match *cm)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100363{
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530364
365 lockdep_assert_held(&si->lock);
366
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800367#ifdef CONFIG_NF_FLOW_COOKIE
Xiaoping Fan640faf42015-08-28 15:50:55 -0700368 if (si->flow_cookie_enable) {
369 /*
370 * Tell hardware that we no longer need a flow cookie in packet of this flow
371 */
372 unsigned int conn_match_idx;
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800373
Xiaoping Fan640faf42015-08-28 15:50:55 -0700374 for (conn_match_idx = 1; conn_match_idx < SFE_FLOW_COOKIE_SIZE; conn_match_idx++) {
375 struct sfe_flow_cookie_entry *entry = &si->sfe_flow_cookie_table[conn_match_idx];
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800376
Xiaoping Fan640faf42015-08-28 15:50:55 -0700377 if (cm == entry->match) {
378 flow_cookie_set_func_t func;
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800379
Xiaoping Fan640faf42015-08-28 15:50:55 -0700380 rcu_read_lock();
381 func = rcu_dereference(si->flow_cookie_set_func);
382 if (func) {
383 func(cm->match_protocol, cm->match_src_ip, cm->match_src_port,
384 cm->match_dest_ip, cm->match_dest_port, 0);
385 }
386 rcu_read_unlock();
387
388 cm->flow_cookie = 0;
389 entry->match = NULL;
390 entry->last_clean_time = jiffies;
391 break;
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800392 }
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800393 }
394 }
395#endif
396
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530397 hlist_del_init_rcu(&cm->hnode);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100398
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100399}
400
401/*
402 * sfe_ipv4_get_connection_hash()
403 * Generate the hash used in connection lookups.
404 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700405static inline unsigned int sfe_ipv4_get_connection_hash(u8 protocol, __be32 src_ip, __be16 src_port,
Dave Hudson87973cd2013-10-22 16:00:04 +0100406 __be32 dest_ip, __be16 dest_port)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100407{
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700408 u32 hash = ntohl(src_ip ^ dest_ip) ^ protocol ^ ntohs(src_port ^ dest_port);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100409 return ((hash >> SFE_IPV4_CONNECTION_HASH_SHIFT) ^ hash) & SFE_IPV4_CONNECTION_HASH_MASK;
410}
411
412/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530413 * sfe_ipv4_find_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100414 * Get the IPv4 connection info that corresponds to a particular 5-tuple.
415 *
416 * On entry we must be holding the lock that protects the hash table.
417 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530418static inline struct sfe_ipv4_connection *sfe_ipv4_find_connection(struct sfe_ipv4 *si, u32 protocol,
Dave Hudson87973cd2013-10-22 16:00:04 +0100419 __be32 src_ip, __be16 src_port,
420 __be32 dest_ip, __be16 dest_port)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100421{
422 struct sfe_ipv4_connection *c;
423 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 +0530424
425 lockdep_assert_held(&si->lock);
426
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100427 c = si->conn_hash[conn_idx];
428
429 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100430 * Will need connection entry for next create/destroy metadata,
431 * So no need to re-order entry for these requests
432 */
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530433 while (c) {
434 if ((c->src_port == src_port)
435 && (c->dest_port == dest_port)
436 && (c->src_ip == src_ip)
437 && (c->dest_ip == dest_ip)
438 && (c->protocol == protocol)) {
439 return c;
440 }
441
442 c = c->next;
443 }
444
445 return NULL;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100446}
447
448/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530449 * sfe_ipv4_insert_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100450 * Insert a connection into the hash.
451 *
452 * On entry we must be holding the lock that protects the hash table.
453 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530454static void sfe_ipv4_insert_connection(struct sfe_ipv4 *si, struct sfe_ipv4_connection *c)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100455{
456 struct sfe_ipv4_connection **hash_head;
457 struct sfe_ipv4_connection *prev_head;
458 unsigned int conn_idx;
459
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530460 lockdep_assert_held(&si->lock);
461
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100462 /*
463 * Insert entry into the connection hash.
464 */
465 conn_idx = sfe_ipv4_get_connection_hash(c->protocol, c->src_ip, c->src_port,
466 c->dest_ip, c->dest_port);
467 hash_head = &si->conn_hash[conn_idx];
468 prev_head = *hash_head;
469 c->prev = NULL;
470 if (prev_head) {
471 prev_head->prev = c;
472 }
473
474 c->next = prev_head;
475 *hash_head = c;
476
477 /*
478 * Insert entry into the "all connections" list.
479 */
480 if (si->all_connections_tail) {
481 c->all_connections_prev = si->all_connections_tail;
482 si->all_connections_tail->all_connections_next = c;
483 } else {
484 c->all_connections_prev = NULL;
485 si->all_connections_head = c;
486 }
487
488 si->all_connections_tail = c;
489 c->all_connections_next = NULL;
490 si->num_connections++;
491
492 /*
493 * Insert the connection match objects too.
494 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530495 sfe_ipv4_insert_connection_match(si, c->original_match);
496 sfe_ipv4_insert_connection_match(si, c->reply_match);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100497}
498
499/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530500 * sfe_ipv4_remove_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100501 * Remove a sfe_ipv4_connection object from the hash.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100502 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530503bool sfe_ipv4_remove_connection(struct sfe_ipv4 *si, struct sfe_ipv4_connection *c)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100504{
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530505 lockdep_assert_held(&si->lock);
506
507 if (c->removed) {
508 DEBUG_ERROR("%px: Connection has been removed already\n", c);
509 return false;
510 }
511
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100512 /*
513 * Remove the connection match objects.
514 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530515 sfe_ipv4_remove_connection_match(si, c->reply_match);
516 sfe_ipv4_remove_connection_match(si, c->original_match);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100517
518 /*
519 * Unlink the connection.
520 */
521 if (c->prev) {
522 c->prev->next = c->next;
523 } else {
524 unsigned int conn_idx = sfe_ipv4_get_connection_hash(c->protocol, c->src_ip, c->src_port,
525 c->dest_ip, c->dest_port);
526 si->conn_hash[conn_idx] = c->next;
527 }
528
529 if (c->next) {
530 c->next->prev = c->prev;
531 }
Xiaoping Fan34586472015-07-03 02:20:35 -0700532
533 /*
534 * Unlink connection from all_connections list
535 */
536 if (c->all_connections_prev) {
537 c->all_connections_prev->all_connections_next = c->all_connections_next;
538 } else {
539 si->all_connections_head = c->all_connections_next;
540 }
541
542 if (c->all_connections_next) {
543 c->all_connections_next->all_connections_prev = c->all_connections_prev;
544 } else {
545 si->all_connections_tail = c->all_connections_prev;
546 }
547
Ken Zhudc423672021-09-02 18:27:01 -0700548 /*
549 * If I am the next sync connection, move the sync to my next or head.
550 */
551 if (unlikely(si->wc_next == c)) {
552 si->wc_next = c->all_connections_next;
553 }
554
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530555 c->removed = true;
Xiaoping Fan34586472015-07-03 02:20:35 -0700556 si->num_connections--;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530557 return true;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100558}
559
560/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530561 * sfe_ipv4_gen_sync_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100562 * Sync a connection.
563 *
564 * On entry to this function we expect that the lock for the connection is either
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530565 * already held (while called from sfe_ipv4_periodic_sync() or isn't required
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530566 * (while called from sfe_ipv4_flush_connection())
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100567 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530568static void sfe_ipv4_gen_sync_connection(struct sfe_ipv4 *si, struct sfe_ipv4_connection *c,
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700569 struct sfe_connection_sync *sis, sfe_sync_reason_t reason,
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700570 u64 now_jiffies)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100571{
572 struct sfe_ipv4_connection_match *original_cm;
573 struct sfe_ipv4_connection_match *reply_cm;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530574 u32 packet_count, byte_count;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100575
576 /*
577 * Fill in the update message.
578 */
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700579 sis->is_v6 = 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100580 sis->protocol = c->protocol;
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700581 sis->src_ip.ip = c->src_ip;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700582 sis->src_ip_xlate.ip = c->src_ip_xlate;
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700583 sis->dest_ip.ip = c->dest_ip;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700584 sis->dest_ip_xlate.ip = c->dest_ip_xlate;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100585 sis->src_port = c->src_port;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700586 sis->src_port_xlate = c->src_port_xlate;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100587 sis->dest_port = c->dest_port;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700588 sis->dest_port_xlate = c->dest_port_xlate;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100589
590 original_cm = c->original_match;
591 reply_cm = c->reply_match;
592 sis->src_td_max_window = original_cm->protocol_state.tcp.max_win;
593 sis->src_td_end = original_cm->protocol_state.tcp.end;
594 sis->src_td_max_end = original_cm->protocol_state.tcp.max_end;
595 sis->dest_td_max_window = reply_cm->protocol_state.tcp.max_win;
596 sis->dest_td_end = reply_cm->protocol_state.tcp.end;
597 sis->dest_td_max_end = reply_cm->protocol_state.tcp.max_end;
598
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530599 sfe_ipv4_connection_match_update_summary_stats(original_cm, &packet_count, &byte_count);
600 sis->src_new_packet_count = packet_count;
601 sis->src_new_byte_count = byte_count;
Matthew McClintockd0cdb802014-02-24 16:30:35 -0600602
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530603 sfe_ipv4_connection_match_update_summary_stats(reply_cm, &packet_count, &byte_count);
604 sis->dest_new_packet_count = packet_count;
605 sis->dest_new_byte_count = byte_count;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100606
Matthew McClintockd0cdb802014-02-24 16:30:35 -0600607 sis->src_dev = original_cm->match_dev;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100608 sis->src_packet_count = original_cm->rx_packet_count64;
609 sis->src_byte_count = original_cm->rx_byte_count64;
Matthew McClintockd0cdb802014-02-24 16:30:35 -0600610
611 sis->dest_dev = reply_cm->match_dev;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100612 sis->dest_packet_count = reply_cm->rx_packet_count64;
613 sis->dest_byte_count = reply_cm->rx_byte_count64;
614
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700615 sis->reason = reason;
616
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100617 /*
618 * Get the time increment since our last sync.
619 */
620 sis->delta_jiffies = now_jiffies - c->last_sync_jiffies;
621 c->last_sync_jiffies = now_jiffies;
622}
623
624/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530625 * sfe_ipv4_free_connection_rcu()
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530626 * Called at RCU qs state to free the connection object.
627 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530628static void sfe_ipv4_free_connection_rcu(struct rcu_head *head)
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530629{
630 struct sfe_ipv4_connection *c;
Amitesh Anand63be37d2021-12-24 20:51:48 +0530631 struct udp_sock *up;
632 struct sock *sk;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530633
634 /*
635 * We dont need spin lock as the connection is already removed from link list
636 */
637 c = container_of(head, struct sfe_ipv4_connection, rcu);
638
639 BUG_ON(!c->removed);
640
641 DEBUG_TRACE("%px: connecton has been deleted\n", c);
642
643 /*
Amitesh Anand63be37d2021-12-24 20:51:48 +0530644 * Decrease the refcount taken in function sfe_ipv4_create_rule(),
645 * during call of __udp4_lib_lookup()
646 */
647 up = c->reply_match->up;
648 if (up) {
649 sk = (struct sock *)up;
650 sock_put(sk);
651 }
652
653 /*
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530654 * Release our hold of the source and dest devices and free the memory
655 * for our connection objects.
656 */
657 dev_put(c->original_dev);
658 dev_put(c->reply_dev);
659 kfree(c->original_match);
660 kfree(c->reply_match);
661 kfree(c);
662}
663
664/*
Ken Zhu88c58152021-12-09 15:12:06 -0800665 * sfe_ipv4_sync_status()
666 * update a connection status to its connection manager.
667 *
668 * si: the ipv4 context
669 * c: which connection to be notified
670 * reason: what kind of notification: flush, stats or destroy
671 */
672void sfe_ipv4_sync_status(struct sfe_ipv4 *si,
673 struct sfe_ipv4_connection *c,
674 sfe_sync_reason_t reason)
675{
676 struct sfe_connection_sync sis;
677 u64 now_jiffies;
678 sfe_sync_rule_callback_t sync_rule_callback;
679
680 rcu_read_lock();
681 sync_rule_callback = rcu_dereference(si->sync_rule_callback);
682 if (!sync_rule_callback) {
683 rcu_read_unlock();
684 return;
685 }
686
687 /*
688 * Generate a sync message and then sync.
689 */
690 now_jiffies = get_jiffies_64();
691 sfe_ipv4_gen_sync_connection(si, c, &sis, reason, now_jiffies);
692 sync_rule_callback(&sis);
693
694 rcu_read_unlock();
695}
696
697/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530698 * sfe_ipv4_flush_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100699 * Flush a connection and free all associated resources.
700 *
701 * We need to be called with bottom halves disabled locally as we need to acquire
702 * the connection hash lock and release it again. In general we're actually called
703 * from within a BH and so we're fine, but we're also called when connections are
704 * torn down.
705 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530706void sfe_ipv4_flush_connection(struct sfe_ipv4 *si,
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700707 struct sfe_ipv4_connection *c,
708 sfe_sync_reason_t reason)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100709{
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530710 BUG_ON(!c->removed);
711
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530712 this_cpu_inc(si->stats_pcpu->connection_flushes64);
Ken Zhu88c58152021-12-09 15:12:06 -0800713 sfe_ipv4_sync_status(si, c, reason);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100714
715 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100716 * Release our hold of the source and dest devices and free the memory
717 * for our connection objects.
718 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530719 call_rcu(&c->rcu, sfe_ipv4_free_connection_rcu);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100720}
721
722/*
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530723 * sfe_ipv4_exception_stats_inc()
724 * Increment exception stats.
725 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530726void sfe_ipv4_exception_stats_inc(struct sfe_ipv4 *si, enum sfe_ipv4_exception_events reason)
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530727{
728 struct sfe_ipv4_stats *stats = this_cpu_ptr(si->stats_pcpu);
729 stats->exception_events64[reason]++;
730 stats->packets_not_forwarded64++;
731}
732
733/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100734 * sfe_ipv4_recv()
Matthew McClintocka8ad7962014-01-16 16:49:30 -0600735 * Handle packet receives and forwaring.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100736 *
737 * Returns 1 if the packet is forwarded or 0 if it isn't.
738 */
Amitesh Anand63be37d2021-12-24 20:51:48 +0530739int 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 +0100740{
741 struct sfe_ipv4 *si = &__si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100742 unsigned int len;
743 unsigned int tot_len;
744 unsigned int frag_off;
745 unsigned int ihl;
Ken Zhu88c58152021-12-09 15:12:06 -0800746 bool sync_on_find;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100747 bool ip_options;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530748 struct iphdr *iph;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700749 u32 protocol;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100750
751 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100752 * Check that we have space for an IP header here.
753 */
754 len = skb->len;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530755 if (unlikely(!pskb_may_pull(skb, sizeof(struct iphdr)))) {
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530756 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_HEADER_INCOMPLETE);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100757 DEBUG_TRACE("len: %u is too short\n", len);
758 return 0;
759 }
760
761 /*
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +0530762 * Validate ip csum if necessary. If ip_summed is set to CHECKSUM_UNNECESSARY, it is assumed
763 * that the L3 checksum is validated by the Rx interface or the tunnel interface that has
764 * generated the packet.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100765 */
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530766 iph = (struct iphdr *)skb->data;
Ratheesh Kannoth43d64f82021-10-20 08:23:29 +0530767 if (unlikely(skb->ip_summed != CHECKSUM_UNNECESSARY) && (ip_fast_csum((u8 *)iph, iph->ihl))) {
768 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_HEADER_CSUM_BAD);
769
770 DEBUG_TRACE("Bad IPv4 header csum: 0x%x\n", iph->check);
771 return 0;
772 }
773
774 /*
775 * Check that our "total length" is large enough for an IP header.
776 */
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100777 tot_len = ntohs(iph->tot_len);
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530778 if (unlikely(tot_len < sizeof(struct iphdr))) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100779
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530780 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_BAD_TOTAL_LENGTH);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100781 DEBUG_TRACE("tot_len: %u is too short\n", tot_len);
782 return 0;
783 }
784
785 /*
786 * Is our IP version wrong?
787 */
788 if (unlikely(iph->version != 4)) {
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530789 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_NON_V4);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100790 DEBUG_TRACE("IP version: %u\n", iph->version);
791 return 0;
792 }
793
794 /*
795 * Does our datagram fit inside the skb?
796 */
797 if (unlikely(tot_len > len)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100798 DEBUG_TRACE("tot_len: %u, exceeds len: %u\n", tot_len, len);
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530799 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_DATAGRAM_INCOMPLETE);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100800 return 0;
801 }
802
803 /*
804 * Do we have a non-initial fragment?
Nicolas Costaac2979c2014-01-14 10:35:24 -0600805 */
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100806 frag_off = ntohs(iph->frag_off);
807 if (unlikely(frag_off & IP_OFFSET)) {
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530808 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_NON_INITIAL_FRAGMENT);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100809 DEBUG_TRACE("non-initial fragment\n");
810 return 0;
811 }
812
813 /*
814 * If we have a (first) fragment then mark it to cause any connection to flush.
815 */
Ken Zhu88c58152021-12-09 15:12:06 -0800816 sync_on_find = unlikely(frag_off & IP_MF) ? true : false;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100817
818 /*
819 * Do we have any IP options? That's definite a slow path! If we do have IP
820 * options we need to recheck our header size.
821 */
822 ihl = iph->ihl << 2;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530823 ip_options = unlikely(ihl != sizeof(struct iphdr)) ? true : false;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100824 if (unlikely(ip_options)) {
825 if (unlikely(len < ihl)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100826
827 DEBUG_TRACE("len: %u is too short for header of size: %u\n", len, ihl);
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530828 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_IP_OPTIONS_INCOMPLETE);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100829 return 0;
830 }
831
Ken Zhu88c58152021-12-09 15:12:06 -0800832 sync_on_find = true;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100833 }
834
835 protocol = iph->protocol;
836 if (IPPROTO_UDP == protocol) {
Ken Zhu88c58152021-12-09 15:12:06 -0800837 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 +0100838 }
839
840 if (IPPROTO_TCP == protocol) {
Ken Zhu88c58152021-12-09 15:12:06 -0800841 return sfe_ipv4_recv_tcp(si, skb, dev, len, iph, ihl, sync_on_find, l2_info);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100842 }
843
844 if (IPPROTO_ICMP == protocol) {
845 return sfe_ipv4_recv_icmp(si, skb, dev, len, iph, ihl);
846 }
847
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530848 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_UNHANDLED_PROTOCOL);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100849
850 DEBUG_TRACE("not UDP, TCP or ICMP: %u\n", protocol);
851 return 0;
852}
853
Nicolas Costa436926b2014-01-14 10:36:22 -0600854static void
855sfe_ipv4_update_tcp_state(struct sfe_ipv4_connection *c,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530856 struct sfe_ipv4_rule_create_msg *msg)
Nicolas Costa436926b2014-01-14 10:36:22 -0600857{
858 struct sfe_ipv4_connection_match *orig_cm;
859 struct sfe_ipv4_connection_match *repl_cm;
860 struct sfe_ipv4_tcp_connection_match *orig_tcp;
861 struct sfe_ipv4_tcp_connection_match *repl_tcp;
862
863 orig_cm = c->original_match;
864 repl_cm = c->reply_match;
865 orig_tcp = &orig_cm->protocol_state.tcp;
866 repl_tcp = &repl_cm->protocol_state.tcp;
867
868 /* update orig */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530869 if (orig_tcp->max_win < msg->tcp_rule.flow_max_window) {
870 orig_tcp->max_win = msg->tcp_rule.flow_max_window;
Nicolas Costa436926b2014-01-14 10:36:22 -0600871 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530872 if ((s32)(orig_tcp->end - msg->tcp_rule.flow_end) < 0) {
873 orig_tcp->end = msg->tcp_rule.flow_end;
Nicolas Costa436926b2014-01-14 10:36:22 -0600874 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530875 if ((s32)(orig_tcp->max_end - msg->tcp_rule.flow_max_end) < 0) {
876 orig_tcp->max_end = msg->tcp_rule.flow_max_end;
Nicolas Costa436926b2014-01-14 10:36:22 -0600877 }
878
879 /* update reply */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530880 if (repl_tcp->max_win < msg->tcp_rule.return_max_window) {
881 repl_tcp->max_win = msg->tcp_rule.return_max_window;
Nicolas Costa436926b2014-01-14 10:36:22 -0600882 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530883 if ((s32)(repl_tcp->end - msg->tcp_rule.return_end) < 0) {
884 repl_tcp->end = msg->tcp_rule.return_end;
Nicolas Costa436926b2014-01-14 10:36:22 -0600885 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530886 if ((s32)(repl_tcp->max_end - msg->tcp_rule.return_max_end) < 0) {
887 repl_tcp->max_end = msg->tcp_rule.return_max_end;
Nicolas Costa436926b2014-01-14 10:36:22 -0600888 }
889
890 /* update match flags */
891 orig_cm->flags &= ~SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
892 repl_cm->flags &= ~SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530893 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_NO_SEQ_CHECK) {
894
Nicolas Costa436926b2014-01-14 10:36:22 -0600895 orig_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
896 repl_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
897 }
898}
899
900static void
901sfe_ipv4_update_protocol_state(struct sfe_ipv4_connection *c,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530902 struct sfe_ipv4_rule_create_msg *msg)
Nicolas Costa436926b2014-01-14 10:36:22 -0600903{
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530904 switch (msg->tuple.protocol) {
Nicolas Costa436926b2014-01-14 10:36:22 -0600905 case IPPROTO_TCP:
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530906 sfe_ipv4_update_tcp_state(c, msg);
Nicolas Costa436926b2014-01-14 10:36:22 -0600907 break;
908 }
909}
910
Wayne Tanbb7f1782021-12-13 11:16:04 -0800911/*
912 * sfe_ipv4_match_entry_set_vlan()
913 */
914static void sfe_ipv4_match_entry_set_vlan(
915 struct sfe_ipv4_connection_match *cm,
916 u32 primary_ingress_vlan_tag,
917 u32 primary_egress_vlan_tag,
918 u32 secondary_ingress_vlan_tag,
919 u32 secondary_egress_vlan_tag)
920{
921 u16 tpid;
922 /*
923 * Prevent stacking header counts when updating.
924 */
925 cm->ingress_vlan_hdr_cnt = 0;
926 cm->egress_vlan_hdr_cnt = 0;
927 memset(cm->ingress_vlan_hdr, 0, sizeof(cm->ingress_vlan_hdr));
928 memset(cm->egress_vlan_hdr, 0, sizeof(cm->egress_vlan_hdr));
929
930 /*
931 * vlan_hdr[0] corresponds to outer tag
932 * vlan_hdr[1] corresponds to inner tag
933 * Extract the vlan information (tpid and tci) from rule message
934 */
935 if ((primary_ingress_vlan_tag & VLAN_VID_MASK) != SFE_VLAN_ID_NOT_CONFIGURED) {
936 tpid = (u16)(primary_ingress_vlan_tag >> 16);
937 cm->ingress_vlan_hdr[0].tpid = ntohs(tpid);
938 cm->ingress_vlan_hdr[0].tci = (u16)primary_ingress_vlan_tag;
939 cm->ingress_vlan_hdr_cnt++;
940 }
941
942 if ((secondary_ingress_vlan_tag & VLAN_VID_MASK) != SFE_VLAN_ID_NOT_CONFIGURED) {
943 tpid = (u16)(secondary_ingress_vlan_tag >> 16);
944 cm->ingress_vlan_hdr[1].tpid = ntohs(tpid);
945 cm->ingress_vlan_hdr[1].tci = (u16)secondary_ingress_vlan_tag;
946 cm->ingress_vlan_hdr_cnt++;
947 }
948
949 if ((primary_egress_vlan_tag & VLAN_VID_MASK) != SFE_VLAN_ID_NOT_CONFIGURED) {
950 tpid = (u16)(primary_egress_vlan_tag >> 16);
951 cm->egress_vlan_hdr[0].tpid = ntohs(tpid);
952 cm->egress_vlan_hdr[0].tci = (u16)primary_egress_vlan_tag;
953 cm->egress_vlan_hdr_cnt++;
954 }
955
956 if ((secondary_egress_vlan_tag & VLAN_VID_MASK) != SFE_VLAN_ID_NOT_CONFIGURED) {
957 tpid = (u16)(secondary_egress_vlan_tag >> 16);
958 cm->egress_vlan_hdr[1].tpid = ntohs(tpid);
959 cm->egress_vlan_hdr[1].tci = (u16)secondary_egress_vlan_tag;
960 cm->egress_vlan_hdr_cnt++;
961 }
962}
963
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530964void sfe_ipv4_update_rule(struct sfe_ipv4_rule_create_msg *msg)
Nicolas Costa436926b2014-01-14 10:36:22 -0600965{
966 struct sfe_ipv4_connection *c;
967 struct sfe_ipv4 *si = &__si;
968
969 spin_lock_bh(&si->lock);
970
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530971 c = sfe_ipv4_find_connection(si,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530972 msg->tuple.protocol,
973 msg->tuple.flow_ip,
974 msg->tuple.flow_ident,
975 msg->tuple.return_ip,
976 msg->tuple.return_ident);
Nicolas Costa436926b2014-01-14 10:36:22 -0600977 if (c != NULL) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530978 sfe_ipv4_update_protocol_state(c, msg);
Nicolas Costa436926b2014-01-14 10:36:22 -0600979 }
980
981 spin_unlock_bh(&si->lock);
982}
983
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100984/*
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +0530985 * sfe_ipv4_xmit_eth_type_check()
986 * Checking if MAC header has to be written.
987 */
988static inline bool sfe_ipv4_xmit_eth_type_check(struct net_device *dev, u32 cm_flags)
989{
990 if (!(dev->flags & IFF_NOARP)) {
991 return true;
992 }
993
994 /*
995 * For PPPoE, since we are now supporting PPPoE encapsulation, we are writing L2 header.
996 */
997 if (unlikely(cm_flags & SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_ENCAP)) {
998 return true;
999 }
1000
1001 return false;
1002}
1003
1004/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001005 * sfe_ipv4_create_rule()
1006 * Create a forwarding rule.
1007 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301008int sfe_ipv4_create_rule(struct sfe_ipv4_rule_create_msg *msg)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001009{
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001010 struct sfe_ipv4 *si = &__si;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301011 struct sfe_ipv4_connection *c, *c_old;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001012 struct sfe_ipv4_connection_match *original_cm;
1013 struct sfe_ipv4_connection_match *reply_cm;
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001014 struct net_device *dest_dev;
1015 struct net_device *src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301016 struct sfe_ipv4_5tuple *tuple = &msg->tuple;
Suruchi Sumanc1a4a612021-10-21 14:50:23 +05301017 s32 flow_interface_num = msg->conn_rule.flow_top_interface_num;
1018 s32 return_interface_num = msg->conn_rule.return_top_interface_num;
Amitesh Anand63be37d2021-12-24 20:51:48 +05301019 struct net *net;
1020 struct sock *sk;
1021 unsigned int src_if_idx;
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001022
Suruchi Sumanc1a4a612021-10-21 14:50:23 +05301023 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_FLOW_BOTTOM_INTERFACE) {
1024 flow_interface_num = msg->conn_rule.flow_interface_num;
1025 }
1026
1027 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_RETURN_BOTTOM_INTERFACE) {
1028 return_interface_num = msg->conn_rule.return_interface_num;
1029 }
1030
1031 src_dev = dev_get_by_index(&init_net, flow_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301032 if (!src_dev) {
1033 DEBUG_WARN("%px: Unable to find src_dev corresponding to %d\n", msg,
Suruchi Sumanc1a4a612021-10-21 14:50:23 +05301034 flow_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301035 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
1036 return -EINVAL;
1037 }
1038
Suruchi Sumanc1a4a612021-10-21 14:50:23 +05301039 dest_dev = dev_get_by_index(&init_net, return_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301040 if (!dest_dev) {
1041 DEBUG_WARN("%px: Unable to find dest_dev corresponding to %d\n", msg,
Suruchi Sumanc1a4a612021-10-21 14:50:23 +05301042 return_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301043 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
1044 dev_put(src_dev);
1045 return -EINVAL;
1046 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001047
Matthew McClintock389b42a2014-09-24 14:05:51 -05001048 if (unlikely((dest_dev->reg_state != NETREG_REGISTERED) ||
1049 (src_dev->reg_state != NETREG_REGISTERED))) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301050 dev_put(src_dev);
1051 dev_put(dest_dev);
1052 DEBUG_WARN("%px: src_dev=%s and dest_dev=%s are unregistered\n", msg,
1053 src_dev->name, dest_dev->name);
1054 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
Matthew McClintock389b42a2014-09-24 14:05:51 -05001055 return -EINVAL;
1056 }
1057
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301058 /*
1059 * Allocate the various connection tracking objects.
1060 */
1061 c = (struct sfe_ipv4_connection *)kmalloc(sizeof(struct sfe_ipv4_connection), GFP_ATOMIC);
1062 if (unlikely(!c)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301063 DEBUG_WARN("%px: memory allocation of connection entry failed\n", msg);
1064 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
1065 dev_put(src_dev);
1066 dev_put(dest_dev);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301067 return -ENOMEM;
1068 }
1069
1070 original_cm = (struct sfe_ipv4_connection_match *)kmalloc(sizeof(struct sfe_ipv4_connection_match), GFP_ATOMIC);
1071 if (unlikely(!original_cm)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301072 DEBUG_WARN("%px: memory allocation of connection match entry failed\n", msg);
1073 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301074 kfree(c);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301075 dev_put(src_dev);
1076 dev_put(dest_dev);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301077 return -ENOMEM;
1078 }
1079
1080 reply_cm = (struct sfe_ipv4_connection_match *)kmalloc(sizeof(struct sfe_ipv4_connection_match), GFP_ATOMIC);
1081 if (unlikely(!reply_cm)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301082 DEBUG_WARN("%px: memory allocation of connection match entry failed\n", msg);
1083 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301084 kfree(original_cm);
1085 kfree(c);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301086 dev_put(src_dev);
1087 dev_put(dest_dev);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301088 return -ENOMEM;
1089 }
1090
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301091 this_cpu_inc(si->stats_pcpu->connection_create_requests64);
1092
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001093 spin_lock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001094
1095 /*
Nicolas Costa436926b2014-01-14 10:36:22 -06001096 * Check to see if there is already a flow that matches the rule we're
1097 * trying to create. If there is then we can't create a new one.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001098 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301099 c_old = sfe_ipv4_find_connection(si,
Wayne Tanbb7f1782021-12-13 11:16:04 -08001100 msg->tuple.protocol,
1101 msg->tuple.flow_ip,
1102 msg->tuple.flow_ident,
1103 msg->tuple.return_ip,
1104 msg->tuple.return_ident);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301105
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301106 if (c_old != NULL) {
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301107 this_cpu_inc(si->stats_pcpu->connection_create_collisions64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001108
1109 /*
Nicolas Costa436926b2014-01-14 10:36:22 -06001110 * If we already have the flow then it's likely that this
1111 * request to create the connection rule contains more
1112 * up-to-date information. Check and update accordingly.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001113 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301114 sfe_ipv4_update_protocol_state(c, msg);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001115 spin_unlock_bh(&si->lock);
1116
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301117 kfree(reply_cm);
1118 kfree(original_cm);
1119 kfree(c);
1120
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301121 dev_put(src_dev);
1122 dev_put(dest_dev);
1123
Amitesh Anand63be37d2021-12-24 20:51:48 +05301124 DEBUG_TRACE("%px: connection already exists - p:%d\n"
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301125 " s: %s:%pM:%pI4:%u, d: %s:%pM:%pI4:%u\n",
Amitesh Anand63be37d2021-12-24 20:51:48 +05301126 msg, tuple->protocol,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301127 src_dev->name, msg->conn_rule.flow_mac, &tuple->flow_ip, ntohs(tuple->flow_ident),
1128 dest_dev->name, msg->conn_rule.return_mac, &tuple->return_ip, ntohs(tuple->return_ident));
1129
Nicolas Costa514fde02014-01-13 15:50:29 -06001130 return -EADDRINUSE;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001131 }
1132
1133 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001134 * Fill in the "original" direction connection matching object.
1135 * Note that the transmit MAC address is "dest_mac_xlate" because
1136 * we always know both ends of a connection by their translated
1137 * addresses and not their public addresses.
1138 */
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001139 original_cm->match_dev = src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301140 original_cm->match_protocol = tuple->protocol;
1141 original_cm->match_src_ip = tuple->flow_ip;
Suruchi Suman66609a72022-01-20 02:34:25 +05301142 original_cm->match_src_port = netif_is_vxlan(src_dev) ? 0 : tuple->flow_ident;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301143 original_cm->match_dest_ip = tuple->return_ip;
1144 original_cm->match_dest_port = tuple->return_ident;
1145
1146 original_cm->xlate_src_ip = msg->conn_rule.flow_ip_xlate;
1147 original_cm->xlate_src_port = msg->conn_rule.flow_ident_xlate;
1148 original_cm->xlate_dest_ip = msg->conn_rule.return_ip_xlate;
1149 original_cm->xlate_dest_port =msg->conn_rule.return_ident_xlate;
Wayne Tanbb7f1782021-12-13 11:16:04 -08001150
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301151 atomic_set(&original_cm->rx_packet_count, 0);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001152 original_cm->rx_packet_count64 = 0;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301153 atomic_set(&original_cm->rx_byte_count, 0);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001154 original_cm->rx_byte_count64 = 0;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301155
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001156 original_cm->xmit_dev = dest_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301157 original_cm->xmit_dev_mtu = msg->conn_rule.return_mtu;
1158
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001159 original_cm->connection = c;
1160 original_cm->counter_match = reply_cm;
Wayne Tanbb7f1782021-12-13 11:16:04 -08001161 original_cm->l2_hdr_size = 0;
1162 original_cm->flags = 0;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301163
Amitesh Anand63be37d2021-12-24 20:51:48 +05301164 /*
1165 * UDP Socket is valid only in decap direction.
1166 */
1167 RCU_INIT_POINTER(original_cm->up, NULL);
1168
Ken Zhu37040ea2021-09-09 21:11:15 -07001169 if (msg->valid_flags & SFE_RULE_CREATE_MARK_VALID) {
1170 original_cm->mark = msg->mark_rule.flow_mark;
1171 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_MARK;
1172 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301173 if (msg->valid_flags & SFE_RULE_CREATE_QOS_VALID) {
1174 original_cm->priority = msg->qos_rule.flow_qos_tag;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001175 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PRIORITY_REMARK;
1176 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301177 if (msg->valid_flags & SFE_RULE_CREATE_DSCP_MARKING_VALID) {
1178 original_cm->dscp = msg->dscp_rule.flow_dscp << SFE_IPV4_DSCP_SHIFT;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001179 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_DSCP_REMARK;
1180 }
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301181
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301182 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1183 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_BRIDGE_FLOW;
1184 }
1185
Wayne Tanbb7f1782021-12-13 11:16:04 -08001186 /*
1187 * Add VLAN rule to original_cm
1188 */
1189 if (msg->valid_flags & SFE_RULE_CREATE_VLAN_VALID) {
1190 struct sfe_vlan_rule *vlan_primary_rule = &msg->vlan_primary_rule;
1191 struct sfe_vlan_rule *vlan_secondary_rule = &msg->vlan_secondary_rule;
1192 sfe_ipv4_match_entry_set_vlan(original_cm,
1193 vlan_primary_rule->ingress_vlan_tag,
1194 vlan_primary_rule->egress_vlan_tag,
1195 vlan_secondary_rule->ingress_vlan_tag,
1196 vlan_secondary_rule->egress_vlan_tag);
1197
1198 if ((msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_RETURN_BOTTOM_INTERFACE) &&
1199 original_cm->egress_vlan_hdr_cnt > 0) {
1200 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_INSERT_EGRESS_VLAN_TAG;
1201 original_cm->l2_hdr_size += original_cm->egress_vlan_hdr_cnt * VLAN_HLEN;
1202 }
1203 }
1204
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001205#ifdef CONFIG_NF_FLOW_COOKIE
1206 original_cm->flow_cookie = 0;
1207#endif
Zhi Chen8748eb32015-06-18 12:58:48 -07001208#ifdef CONFIG_XFRM
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301209 if (msg->valid_flags & SFE_RULE_CREATE_DIRECTION_VALID) {
1210 original_cm->flow_accel = msg->direction_rule.flow_accel;
1211 } else {
1212 original_cm->flow_accel = 1;
1213 }
Zhi Chen8748eb32015-06-18 12:58:48 -07001214#endif
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301215 /*
1216 * If l2_features are disabled and flow uses l2 features such as macvlan/bridge/pppoe/vlan,
1217 * bottom interfaces are expected to be disabled in the flow rule and always top interfaces
1218 * are used. In such cases, do not use HW csum offload. csum offload is used only when we
1219 * are sending directly to the destination interface that supports it.
1220 */
Suruchi Sumanf2077182022-01-13 21:35:23 +05301221 if (likely(dest_dev->features & NETIF_F_HW_CSUM) && !netif_is_vxlan(dest_dev)) {
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301222 if ((msg->conn_rule.return_top_interface_num == msg->conn_rule.return_interface_num) ||
1223 (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_RETURN_BOTTOM_INTERFACE)) {
Suruchi Sumanf2077182022-01-13 21:35:23 +05301224 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_CSUM_OFFLOAD;
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301225 }
1226 }
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001227
Wayne Tanbb7f1782021-12-13 11:16:04 -08001228 reply_cm->l2_hdr_size = 0;
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +05301229 reply_cm->flags = 0;
1230
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001231 /*
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301232 * Adding PPPoE parameters to original and reply entries based on the direction where
1233 * PPPoE header is valid in ECM rule.
1234 *
1235 * If PPPoE is valid in flow direction (from interface is PPPoE), then
1236 * original cm will have PPPoE at ingress (strip PPPoE header)
1237 * reply cm will have PPPoE at egress (add PPPoE header)
1238 *
1239 * If PPPoE is valid in return direction (to interface is PPPoE), then
1240 * original cm will have PPPoE at egress (add PPPoE header)
1241 * reply cm will have PPPoE at ingress (strip PPPoE header)
1242 */
1243 if (msg->valid_flags & SFE_RULE_CREATE_PPPOE_DECAP_VALID) {
1244 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_DECAP;
1245 original_cm->pppoe_session_id = msg->pppoe_rule.flow_pppoe_session_id;
1246 ether_addr_copy(original_cm->pppoe_remote_mac, msg->pppoe_rule.flow_pppoe_remote_mac);
1247
1248 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_ENCAP;
Wayne Tanbb7f1782021-12-13 11:16:04 -08001249 reply_cm->l2_hdr_size += SFE_PPPOE_SESSION_HEADER_SIZE;
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301250 reply_cm->pppoe_session_id = msg->pppoe_rule.flow_pppoe_session_id;
1251 ether_addr_copy(reply_cm->pppoe_remote_mac, msg->pppoe_rule.flow_pppoe_remote_mac);
1252 }
1253
1254 if (msg->valid_flags & SFE_RULE_CREATE_PPPOE_ENCAP_VALID) {
1255 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_ENCAP;
Wayne Tanbb7f1782021-12-13 11:16:04 -08001256 original_cm->l2_hdr_size += SFE_PPPOE_SESSION_HEADER_SIZE;
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301257 original_cm->pppoe_session_id = msg->pppoe_rule.return_pppoe_session_id;
1258 ether_addr_copy(original_cm->pppoe_remote_mac, msg->pppoe_rule.return_pppoe_remote_mac);
1259
1260 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_DECAP;
1261 reply_cm->pppoe_session_id = msg->pppoe_rule.return_pppoe_session_id;
1262 ether_addr_copy(reply_cm->pppoe_remote_mac, msg->pppoe_rule.return_pppoe_remote_mac);
1263 }
1264
1265 /*
Ken Zhubbf49652021-09-12 15:33:09 -07001266 * For the non-arp interface, we don't write L2 HDR.
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001267 */
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +05301268 if (sfe_ipv4_xmit_eth_type_check(dest_dev, original_cm->flags)) {
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301269
1270 /*
1271 * Check whether the rule has configured a specific source MAC address to use.
1272 * This is needed when virtual L3 interfaces such as br-lan, macvlan, vlan are used during egress
1273 */
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301274
1275 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1276 ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)msg->conn_rule.flow_mac);
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301277 } else {
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301278 if ((msg->valid_flags & SFE_RULE_CREATE_SRC_MAC_VALID) &&
1279 (msg->src_mac_rule.mac_valid_flags & SFE_SRC_MAC_RETURN_VALID)) {
1280 ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)msg->src_mac_rule.return_src_mac);
1281 } else {
1282 ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)dest_dev->dev_addr);
1283 }
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301284 }
1285
1286 ether_addr_copy((u8 *)original_cm->xmit_dest_mac, (u8 *)msg->conn_rule.return_mac);
1287
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001288 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_L2_HDR;
Wayne Tanbb7f1782021-12-13 11:16:04 -08001289 original_cm->l2_hdr_size += ETH_HLEN;
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001290
1291 /*
1292 * If our dev writes Ethernet headers then we can write a really fast
1293 * version.
1294 */
1295 if (dest_dev->header_ops) {
1296 if (dest_dev->header_ops->create == eth_header) {
1297 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR;
1298 }
1299 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001300 }
1301
1302 /*
1303 * Fill in the "reply" direction connection matching object.
1304 */
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001305 reply_cm->match_dev = dest_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301306 reply_cm->match_protocol = tuple->protocol;
1307 reply_cm->match_src_ip = msg->conn_rule.return_ip_xlate;
Amitesh Anand63be37d2021-12-24 20:51:48 +05301308
1309 /*
1310 * Keep source port as 0 for VxLAN tunnels.
1311 */
1312 if (netif_is_vxlan(src_dev) || netif_is_vxlan(dest_dev)) {
1313 reply_cm->match_src_port = 0;
1314 } else {
1315 reply_cm->match_src_port = msg->conn_rule.return_ident_xlate;
1316 }
1317
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301318 reply_cm->match_dest_ip = msg->conn_rule.flow_ip_xlate;
1319 reply_cm->match_dest_port = msg->conn_rule.flow_ident_xlate;
1320
1321 reply_cm->xlate_src_ip = tuple->return_ip;
1322 reply_cm->xlate_src_port = tuple->return_ident;
1323 reply_cm->xlate_dest_ip = tuple->flow_ip;
1324 reply_cm->xlate_dest_port = tuple->flow_ident;;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301325
1326 atomic_set(&reply_cm->rx_packet_count, 0);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001327 reply_cm->rx_packet_count64 = 0;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301328 atomic_set(&reply_cm->rx_byte_count, 0);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001329 reply_cm->rx_byte_count64 = 0;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301330
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001331 reply_cm->xmit_dev = src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301332 reply_cm->xmit_dev_mtu = msg->conn_rule.flow_mtu;
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301333
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001334 reply_cm->connection = c;
1335 reply_cm->counter_match = original_cm;
Ken Zhu37040ea2021-09-09 21:11:15 -07001336
Ken Zhu37040ea2021-09-09 21:11:15 -07001337 if (msg->valid_flags & SFE_RULE_CREATE_MARK_VALID) {
1338 reply_cm->mark = msg->mark_rule.return_mark;
1339 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_MARK;
1340 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301341 if (msg->valid_flags & SFE_RULE_CREATE_QOS_VALID) {
1342 reply_cm->priority = msg->qos_rule.return_qos_tag;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001343 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PRIORITY_REMARK;
1344 }
Wayne Tanbb7f1782021-12-13 11:16:04 -08001345
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301346 if (msg->valid_flags & SFE_RULE_CREATE_DSCP_MARKING_VALID) {
1347 reply_cm->dscp = msg->dscp_rule.return_dscp << SFE_IPV4_DSCP_SHIFT;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001348 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_DSCP_REMARK;
1349 }
Amitesh Anand63be37d2021-12-24 20:51:48 +05301350
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301351 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1352 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_BRIDGE_FLOW;
1353 }
1354
Amitesh Anand63be37d2021-12-24 20:51:48 +05301355 /*
1356 * Setup UDP Socket if found to be valid for decap.
1357 */
1358 RCU_INIT_POINTER(reply_cm->up, NULL);
1359 net = dev_net(reply_cm->match_dev);
1360 src_if_idx = src_dev->ifindex;
1361
1362 rcu_read_lock();
1363
1364 /*
1365 * Look for the associated sock object.
1366 * __udp4_lib_lookup() holds a reference for this sock object,
1367 * which will be released in sfe_ipv4_free_connection_rcu()
1368 */
1369#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
1370 sk = __udp4_lib_lookup(net, reply_cm->match_dest_ip, reply_cm->match_dest_port,
1371 reply_cm->xlate_src_ip, reply_cm->xlate_src_port, src_if_idx, &udp_table);
1372#else
1373 sk = __udp4_lib_lookup(net, reply_cm->match_dest_ip, reply_cm->match_dest_port,
1374 reply_cm->xlate_src_ip, reply_cm->xlate_src_port, src_if_idx, 0, &udp_table, NULL);
1375#endif
1376
1377 rcu_read_unlock();
1378
1379 /*
1380 * We set the UDP sock pointer as valid only for decap direction.
1381 */
1382 if (sk && udp_sk(sk)->encap_type) {
1383#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
1384 if (!atomic_add_unless(&sk->sk_refcnt, 1, 0)) {
1385#else
1386 if (!refcount_inc_not_zero(&sk->sk_refcnt)) {
1387#endif
Wayne Tanbb7f1782021-12-13 11:16:04 -08001388 spin_unlock_bh(&si->lock);
Amitesh Anand63be37d2021-12-24 20:51:48 +05301389 kfree(reply_cm);
1390 kfree(original_cm);
1391 kfree(c);
1392
1393 DEBUG_TRACE("%px: sfe: unable to take reference for socket(%px) p:%d\n"
1394 " s: %s:%pM:%pI4:%u, d: %s:%pM:%pI4:%u\n",
1395 msg, sk, tuple->protocol,
1396 src_dev->name, msg->conn_rule.flow_mac, &tuple->flow_ip, ntohs(tuple->flow_ident),
1397 dest_dev->name, msg->conn_rule.return_mac, &tuple->return_ip, ntohs(tuple->return_ident));
1398
1399 dev_put(src_dev);
1400 dev_put(dest_dev);
1401
1402 return -ESHUTDOWN;
1403 }
1404
1405 rcu_assign_pointer(reply_cm->up, udp_sk(sk));
1406
1407 DEBUG_INFO("%px: Sock(%px) lookup success with reply_cm direction\n", msg, sk);
1408 DEBUG_INFO("%px: SFE connection -\n"
1409 " s: %s:%pI4(%pI4):%u(%u)\n"
1410 " d: %s:%pI4(%pI4):%u(%u)\n",
1411 msg, reply_cm->match_dev->name, &reply_cm->match_src_ip, &reply_cm->xlate_src_ip,
1412 ntohs(reply_cm->match_src_port), ntohs(reply_cm->xlate_src_port),
1413 reply_cm->xmit_dev->name, &reply_cm->match_dest_ip, &reply_cm->xlate_dest_ip,
1414 ntohs(reply_cm->match_dest_port), ntohs(reply_cm->xlate_dest_port));
1415 }
1416
Wayne Tanbb7f1782021-12-13 11:16:04 -08001417 /*
1418 * Add VLAN rule to reply_cm
1419 */
1420 if (msg->valid_flags & SFE_RULE_CREATE_VLAN_VALID) {
1421 struct sfe_vlan_rule *vlan_primary_rule = &msg->vlan_primary_rule;
1422 struct sfe_vlan_rule *vlan_secondary_rule = &msg->vlan_secondary_rule;
1423 sfe_ipv4_match_entry_set_vlan(reply_cm,
1424 vlan_primary_rule->egress_vlan_tag,
1425 vlan_primary_rule->ingress_vlan_tag,
1426 vlan_secondary_rule->egress_vlan_tag,
1427 vlan_secondary_rule->ingress_vlan_tag);
1428
1429 if ((msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_FLOW_BOTTOM_INTERFACE) &&
1430 reply_cm->egress_vlan_hdr_cnt > 0) {
1431 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_INSERT_EGRESS_VLAN_TAG;
1432 reply_cm->l2_hdr_size += reply_cm->egress_vlan_hdr_cnt * VLAN_HLEN;
1433 }
1434 }
1435
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001436#ifdef CONFIG_NF_FLOW_COOKIE
1437 reply_cm->flow_cookie = 0;
1438#endif
Zhi Chen8748eb32015-06-18 12:58:48 -07001439#ifdef CONFIG_XFRM
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301440 if (msg->valid_flags & SFE_RULE_CREATE_DIRECTION_VALID) {
1441 reply_cm->flow_accel = msg->direction_rule.return_accel;
1442 } else {
1443 reply_cm->flow_accel = 1;
1444 }
1445
Zhi Chen8748eb32015-06-18 12:58:48 -07001446#endif
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301447 /*
1448 * If l2_features are disabled and flow uses l2 features such as macvlan/bridge/pppoe/vlan,
1449 * bottom interfaces are expected to be disabled in the flow rule and always top interfaces
1450 * are used. In such cases, do not use HW csum offload. csum offload is used only when we
1451 * are sending directly to the destination interface that supports it.
1452 */
Suruchi Sumanf2077182022-01-13 21:35:23 +05301453 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 +05301454 if ((msg->conn_rule.flow_top_interface_num == msg->conn_rule.flow_interface_num) ||
1455 (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_FLOW_BOTTOM_INTERFACE)) {
1456 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_CSUM_OFFLOAD;
1457 }
1458 }
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001459
1460 /*
Ken Zhubbf49652021-09-12 15:33:09 -07001461 * For the non-arp interface, we don't write L2 HDR.
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001462 */
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +05301463 if (sfe_ipv4_xmit_eth_type_check(src_dev, reply_cm->flags)) {
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301464
1465 /*
1466 * Check whether the rule has configured a specific source MAC address to use.
1467 * This is needed when virtual L3 interfaces such as br-lan, macvlan, vlan are used during egress
1468 */
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301469
1470 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1471 ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)msg->conn_rule.return_mac);
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301472 } else {
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301473 if ((msg->valid_flags & SFE_RULE_CREATE_SRC_MAC_VALID) &&
1474 (msg->src_mac_rule.mac_valid_flags & SFE_SRC_MAC_FLOW_VALID)) {
1475 ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)msg->src_mac_rule.flow_src_mac);
1476 } else {
1477 ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)src_dev->dev_addr);
1478 }
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301479 }
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301480
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301481 ether_addr_copy((u8 *)reply_cm->xmit_dest_mac, (u8 *)msg->conn_rule.flow_mac);
1482
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001483 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_L2_HDR;
Wayne Tanbb7f1782021-12-13 11:16:04 -08001484 reply_cm->l2_hdr_size += ETH_HLEN;
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001485
1486 /*
1487 * If our dev writes Ethernet headers then we can write a really fast
1488 * version.
1489 */
1490 if (src_dev->header_ops) {
1491 if (src_dev->header_ops->create == eth_header) {
1492 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR;
1493 }
1494 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001495 }
1496
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301497 if ((tuple->return_ip != msg->conn_rule.return_ip_xlate) ||
1498 (tuple->return_ident != msg->conn_rule.return_ident_xlate)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001499 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST;
1500 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC;
1501 }
1502
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301503 if ((tuple->flow_ip != msg->conn_rule.flow_ip_xlate) ||
1504 (tuple->flow_ident != msg->conn_rule.flow_ident_xlate)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001505 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC;
1506 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST;
1507 }
1508
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001509 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001510 * Initialize the protocol-specific information that we track.
1511 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301512 switch (tuple->protocol) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001513 case IPPROTO_TCP:
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301514 original_cm->protocol_state.tcp.win_scale = msg->tcp_rule.flow_window_scale;
1515 original_cm->protocol_state.tcp.max_win = msg->tcp_rule.flow_max_window ? msg->tcp_rule.flow_max_window : 1;
1516 original_cm->protocol_state.tcp.end = msg->tcp_rule.flow_end;
1517 original_cm->protocol_state.tcp.max_end = msg->tcp_rule.flow_max_end;
1518
1519 reply_cm->protocol_state.tcp.win_scale = msg->tcp_rule.return_window_scale;
1520 reply_cm->protocol_state.tcp.max_win = msg->tcp_rule.return_max_window ? msg->tcp_rule.return_max_window : 1;
1521 reply_cm->protocol_state.tcp.end = msg->tcp_rule.return_end;
1522 reply_cm->protocol_state.tcp.max_end = msg->tcp_rule.return_max_end;
1523
1524 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_NO_SEQ_CHECK) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001525 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
1526 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
1527 }
1528 break;
1529 }
1530
Wayne Tanbb7f1782021-12-13 11:16:04 -08001531 /*
1532 * Fill in the ipv4_connection object.
1533 */
1534 c->protocol = tuple->protocol;
1535 c->src_ip = tuple->flow_ip;
1536 c->src_ip_xlate = msg->conn_rule.flow_ip_xlate;
1537 c->src_port = tuple->flow_ident;
1538 c->src_port_xlate = msg->conn_rule.flow_ident_xlate;
1539 c->original_dev = src_dev;
1540 c->original_match = original_cm;
1541 c->dest_ip = tuple->return_ip;
1542 c->dest_ip_xlate = msg->conn_rule.return_ip_xlate;
1543 c->dest_port = tuple->return_ident;
1544 c->dest_port_xlate = msg->conn_rule.return_ident_xlate;
1545 c->reply_dev = dest_dev;
1546 c->reply_match = reply_cm;
1547 c->debug_read_seq = 0;
1548 c->last_sync_jiffies = get_jiffies_64();
1549 c->removed = false;
1550
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001551 sfe_ipv4_connection_match_compute_translations(original_cm);
1552 sfe_ipv4_connection_match_compute_translations(reply_cm);
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301553 sfe_ipv4_insert_connection(si, c);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001554
1555 spin_unlock_bh(&si->lock);
1556
1557 /*
1558 * We have everything we need!
1559 */
Wayne Tanbb7f1782021-12-13 11:16:04 -08001560 DEBUG_INFO("NEW connection - p: %d\n"
1561 "original_cm: match_dev=src_dev: %s %d %pM\n"
1562 " xmit_dev=dest_dev: %s %d %pM\n"
1563 " xmit_src_mac: %pM\n"
1564 " xmit_dest_mac: %pM\n"
1565 " flags: %x l2_hdr: %u\n"
1566 "flow_ip: %pI4:%u\n"
1567 "flow_ip_xlate: %pI4:%u\n"
1568 "flow_mac: %pM\n"
1569 "reply_cm: match_dev=dest_dev: %s %d %pM\n"
1570 " xmit_dev=src_dev: %s %d %pM\n"
1571 " xmit_src_mac: %pM\n"
1572 " xmit_dest_mac: %pM\n"
1573 " flags: %x l2_hdr: %u\n"
1574 "return_ip: %pI4:%u\n"
1575 "return_ip_xlate: %pI4:%u\n"
1576 "return_mac: %pM\n"
1577 "flags: valid=%x src_mac_valid=%x\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301578 tuple->protocol,
Wayne Tanbb7f1782021-12-13 11:16:04 -08001579 original_cm->match_dev->name, original_cm->match_dev->ifindex, original_cm->match_dev->dev_addr,
1580 original_cm->xmit_dev->name, original_cm->xmit_dev->ifindex, original_cm->xmit_dev->dev_addr,
1581 original_cm->xmit_src_mac, original_cm->xmit_dest_mac, original_cm->flags, original_cm->l2_hdr_size,
1582 &tuple->flow_ip, ntohs(tuple->flow_ident),
1583 &msg->conn_rule.flow_ip_xlate, ntohs(msg->conn_rule.flow_ident_xlate),
1584 msg->conn_rule.flow_mac,
1585 reply_cm->match_dev->name, reply_cm->match_dev->ifindex, reply_cm->match_dev->dev_addr,
1586 reply_cm->xmit_dev->name, reply_cm->xmit_dev->ifindex, reply_cm->xmit_dev->dev_addr,
1587 reply_cm->xmit_src_mac, reply_cm->xmit_dest_mac, reply_cm->flags, reply_cm->l2_hdr_size,
1588 &tuple->return_ip, ntohs(tuple->return_ident),
1589 &msg->conn_rule.return_ip_xlate, ntohs(msg->conn_rule.return_ident_xlate),
1590 msg->conn_rule.return_mac,
1591 msg->valid_flags, msg->src_mac_rule.mac_valid_flags);
Nicolas Costa514fde02014-01-13 15:50:29 -06001592
1593 return 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001594}
1595
1596/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001597 * sfe_ipv4_destroy_rule()
1598 * Destroy a forwarding rule.
1599 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301600void sfe_ipv4_destroy_rule(struct sfe_ipv4_rule_destroy_msg *msg)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001601{
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001602 struct sfe_ipv4 *si = &__si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001603 struct sfe_ipv4_connection *c;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301604 bool ret;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301605 struct sfe_ipv4_5tuple *tuple = &msg->tuple;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001606
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301607 this_cpu_inc(si->stats_pcpu->connection_destroy_requests64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001608 spin_lock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001609
1610 /*
1611 * Check to see if we have a flow that matches the rule we're trying
1612 * to destroy. If there isn't then we can't destroy it.
1613 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301614 c = sfe_ipv4_find_connection(si, tuple->protocol, tuple->flow_ip, tuple->flow_ident,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301615 tuple->return_ip, tuple->return_ident);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001616 if (!c) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001617 spin_unlock_bh(&si->lock);
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301618 this_cpu_inc(si->stats_pcpu->connection_destroy_misses64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001619
1620 DEBUG_TRACE("connection does not exist - p: %d, s: %pI4:%u, d: %pI4:%u\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301621 tuple->protocol, &tuple->flow_ip, ntohs(tuple->flow_ident),
1622 &tuple->return_ip, ntohs(tuple->return_ident));
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001623 return;
1624 }
1625
1626 /*
1627 * Remove our connection details from the hash tables.
1628 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301629 ret = sfe_ipv4_remove_connection(si, c);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001630 spin_unlock_bh(&si->lock);
1631
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301632 if (ret) {
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301633 sfe_ipv4_flush_connection(si, c, SFE_SYNC_REASON_DESTROY);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301634 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001635
1636 DEBUG_INFO("connection destroyed - p: %d, s: %pI4:%u, d: %pI4:%u\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301637 tuple->protocol, &tuple->flow_ip, ntohs(tuple->flow_ident),
1638 &tuple->return_ip, ntohs(tuple->return_ident));
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001639}
1640
1641/*
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001642 * sfe_ipv4_register_sync_rule_callback()
1643 * Register a callback for rule synchronization.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001644 */
Xiaoping Fand44a5b42015-05-26 17:37:37 -07001645void sfe_ipv4_register_sync_rule_callback(sfe_sync_rule_callback_t sync_rule_callback)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001646{
1647 struct sfe_ipv4 *si = &__si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001648
1649 spin_lock_bh(&si->lock);
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001650 rcu_assign_pointer(si->sync_rule_callback, sync_rule_callback);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001651 spin_unlock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001652}
1653
1654/*
1655 * sfe_ipv4_get_debug_dev()
1656 */
1657static ssize_t sfe_ipv4_get_debug_dev(struct device *dev,
1658 struct device_attribute *attr,
1659 char *buf)
1660{
1661 struct sfe_ipv4 *si = &__si;
1662 ssize_t count;
1663 int num;
1664
1665 spin_lock_bh(&si->lock);
1666 num = si->debug_dev;
1667 spin_unlock_bh(&si->lock);
1668
1669 count = snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", num);
1670 return count;
1671}
1672
1673/*
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001674 * sysfs attributes.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001675 */
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001676static const struct device_attribute sfe_ipv4_debug_dev_attr =
Xiaoping Fane70da412016-02-26 16:47:57 -08001677 __ATTR(debug_dev, S_IWUSR | S_IRUGO, sfe_ipv4_get_debug_dev, NULL);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001678
1679/*
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001680 * sfe_ipv4_destroy_all_rules_for_dev()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001681 * Destroy all connections that match a particular device.
1682 *
1683 * If we pass dev as NULL then this destroys all connections.
1684 */
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001685void sfe_ipv4_destroy_all_rules_for_dev(struct net_device *dev)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001686{
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001687 struct sfe_ipv4 *si = &__si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001688 struct sfe_ipv4_connection *c;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301689 bool ret;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001690
Xiaoping Fan34586472015-07-03 02:20:35 -07001691another_round:
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001692 spin_lock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001693
Xiaoping Fan34586472015-07-03 02:20:35 -07001694 for (c = si->all_connections_head; c; c = c->all_connections_next) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001695 /*
Xiaoping Fan34586472015-07-03 02:20:35 -07001696 * Does this connection relate to the device we are destroying?
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001697 */
1698 if (!dev
1699 || (dev == c->original_dev)
1700 || (dev == c->reply_dev)) {
Xiaoping Fan34586472015-07-03 02:20:35 -07001701 break;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001702 }
Xiaoping Fan34586472015-07-03 02:20:35 -07001703 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001704
Xiaoping Fan34586472015-07-03 02:20:35 -07001705 if (c) {
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301706 ret = sfe_ipv4_remove_connection(si, c);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001707 }
1708
1709 spin_unlock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07001710
1711 if (c) {
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301712 if (ret) {
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301713 sfe_ipv4_flush_connection(si, c, SFE_SYNC_REASON_DESTROY);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301714 }
Xiaoping Fan34586472015-07-03 02:20:35 -07001715 goto another_round;
1716 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001717}
1718
1719/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001720 * sfe_ipv4_periodic_sync()
1721 */
Ken Zhu137722d2021-09-23 17:57:36 -07001722static void sfe_ipv4_periodic_sync(struct work_struct *work)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001723{
Ken Zhu137722d2021-09-23 17:57:36 -07001724 struct sfe_ipv4 *si = container_of((struct delayed_work *)work, struct sfe_ipv4, sync_dwork);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07001725 u64 now_jiffies;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001726 int quota;
Xiaoping Fand44a5b42015-05-26 17:37:37 -07001727 sfe_sync_rule_callback_t sync_rule_callback;
Ken Zhudc423672021-09-02 18:27:01 -07001728 struct sfe_ipv4_connection *c;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001729
1730 now_jiffies = get_jiffies_64();
1731
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001732 rcu_read_lock();
1733 sync_rule_callback = rcu_dereference(si->sync_rule_callback);
1734 if (!sync_rule_callback) {
1735 rcu_read_unlock();
1736 goto done;
1737 }
1738
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001739 spin_lock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001740
1741 /*
Ken Zhudc423672021-09-02 18:27:01 -07001742 * If we have reached the end of the connection list, walk from
1743 * the connection head.
1744 */
1745 c = si->wc_next;
1746 if (unlikely(!c)) {
1747 c = si->all_connections_head;
1748 }
1749
1750 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001751 * Get an estimate of the number of connections to parse in this sync.
1752 */
1753 quota = (si->num_connections + 63) / 64;
1754
1755 /*
Ken Zhudc423672021-09-02 18:27:01 -07001756 * Walk the "all connection" list and sync the connection state.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001757 */
Ken Zhudc423672021-09-02 18:27:01 -07001758 while (likely(c && quota)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001759 struct sfe_ipv4_connection_match *cm;
1760 struct sfe_ipv4_connection_match *counter_cm;
Xiaoping Fand44a5b42015-05-26 17:37:37 -07001761 struct sfe_connection_sync sis;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001762
Ken Zhudc423672021-09-02 18:27:01 -07001763 cm = c->original_match;
1764 counter_cm = c->reply_match;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001765
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001766 /*
Ken Zhudc423672021-09-02 18:27:01 -07001767 * Didn't receive packets in the original direction or reply
1768 * direction, move to the next connection.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001769 */
Ken Zhudc423672021-09-02 18:27:01 -07001770 if ((!atomic_read(&cm->rx_packet_count)) && !(atomic_read(&counter_cm->rx_packet_count))) {
1771 c = c->all_connections_next;
1772 continue;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001773 }
1774
Ken Zhudc423672021-09-02 18:27:01 -07001775 quota--;
Matthew McClintockaf48f1e2014-01-23 15:29:19 -06001776
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301777 sfe_ipv4_gen_sync_connection(si, c, &sis, SFE_SYNC_REASON_STATS, now_jiffies);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001778
Ken Zhudc423672021-09-02 18:27:01 -07001779 si->wc_next = c->all_connections_next;
1780
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001781 /*
1782 * We don't want to be holding the lock when we sync!
1783 */
1784 spin_unlock_bh(&si->lock);
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001785 sync_rule_callback(&sis);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001786 spin_lock_bh(&si->lock);
Ken Zhudc423672021-09-02 18:27:01 -07001787
1788 /*
1789 * c must be set and used in the same lock/unlock window;
1790 * because c could be removed when we don't hold the lock,
1791 * so delay grabbing until after the callback and relock.
1792 */
1793 c = si->wc_next;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001794 }
1795
Ken Zhudc423672021-09-02 18:27:01 -07001796 /*
1797 * At the end of the sync, put the wc_next to the connection we left.
1798 */
1799 si->wc_next = c;
1800
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001801 spin_unlock_bh(&si->lock);
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001802 rcu_read_unlock();
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001803
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001804done:
Ken Zhu137722d2021-09-23 17:57:36 -07001805 schedule_delayed_work_on(si->work_cpu, (struct delayed_work *)work, ((HZ + 99) / 100));
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001806}
1807
1808#define CHAR_DEV_MSG_SIZE 768
1809
1810/*
1811 * sfe_ipv4_debug_dev_read_start()
1812 * Generate part of the XML output.
1813 */
1814static bool sfe_ipv4_debug_dev_read_start(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
1815 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
1816{
1817 int bytes_read;
1818
Xiaoping Fan34586472015-07-03 02:20:35 -07001819 si->debug_read_seq++;
1820
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001821 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "<sfe_ipv4>\n");
1822 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1823 return false;
1824 }
1825
1826 *length -= bytes_read;
1827 *total_read += bytes_read;
1828
1829 ws->state++;
1830 return true;
1831}
1832
1833/*
1834 * sfe_ipv4_debug_dev_read_connections_start()
1835 * Generate part of the XML output.
1836 */
1837static bool sfe_ipv4_debug_dev_read_connections_start(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
1838 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
1839{
1840 int bytes_read;
1841
1842 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<connections>\n");
1843 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1844 return false;
1845 }
1846
1847 *length -= bytes_read;
1848 *total_read += bytes_read;
1849
1850 ws->state++;
1851 return true;
1852}
1853
1854/*
1855 * sfe_ipv4_debug_dev_read_connections_connection()
1856 * Generate part of the XML output.
1857 */
1858static bool sfe_ipv4_debug_dev_read_connections_connection(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
1859 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
1860{
1861 struct sfe_ipv4_connection *c;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001862 struct sfe_ipv4_connection_match *original_cm;
1863 struct sfe_ipv4_connection_match *reply_cm;
1864 int bytes_read;
1865 int protocol;
1866 struct net_device *src_dev;
Dave Hudson87973cd2013-10-22 16:00:04 +01001867 __be32 src_ip;
1868 __be32 src_ip_xlate;
1869 __be16 src_port;
1870 __be16 src_port_xlate;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07001871 u64 src_rx_packets;
1872 u64 src_rx_bytes;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001873 struct net_device *dest_dev;
Dave Hudson87973cd2013-10-22 16:00:04 +01001874 __be32 dest_ip;
1875 __be32 dest_ip_xlate;
1876 __be16 dest_port;
1877 __be16 dest_port_xlate;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07001878 u64 dest_rx_packets;
1879 u64 dest_rx_bytes;
1880 u64 last_sync_jiffies;
Ken Zhu37040ea2021-09-09 21:11:15 -07001881 u32 src_mark, dest_mark, src_priority, dest_priority, src_dscp, dest_dscp;
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301882 u32 packet, byte, original_cm_flags;
1883 u16 pppoe_session_id;
1884 u8 pppoe_remote_mac[ETH_ALEN];
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001885#ifdef CONFIG_NF_FLOW_COOKIE
1886 int src_flow_cookie, dst_flow_cookie;
1887#endif
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001888
1889 spin_lock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07001890
1891 for (c = si->all_connections_head; c; c = c->all_connections_next) {
1892 if (c->debug_read_seq < si->debug_read_seq) {
1893 c->debug_read_seq = si->debug_read_seq;
1894 break;
1895 }
1896 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001897
1898 /*
Xiaoping Fan34586472015-07-03 02:20:35 -07001899 * If there were no connections then move to the next state.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001900 */
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301901 if (!c || c->removed) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001902 spin_unlock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07001903 ws->state++;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001904 return true;
1905 }
1906
1907 original_cm = c->original_match;
1908 reply_cm = c->reply_match;
1909
1910 protocol = c->protocol;
1911 src_dev = c->original_dev;
1912 src_ip = c->src_ip;
1913 src_ip_xlate = c->src_ip_xlate;
1914 src_port = c->src_port;
1915 src_port_xlate = c->src_port_xlate;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001916 src_priority = original_cm->priority;
1917 src_dscp = original_cm->dscp >> SFE_IPV4_DSCP_SHIFT;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001918
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301919 sfe_ipv4_connection_match_update_summary_stats(original_cm, &packet, &byte);
1920 sfe_ipv4_connection_match_update_summary_stats(reply_cm, &packet, &byte);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001921
1922 src_rx_packets = original_cm->rx_packet_count64;
1923 src_rx_bytes = original_cm->rx_byte_count64;
Ken Zhu37040ea2021-09-09 21:11:15 -07001924 src_mark = original_cm->mark;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001925 dest_dev = c->reply_dev;
1926 dest_ip = c->dest_ip;
1927 dest_ip_xlate = c->dest_ip_xlate;
1928 dest_port = c->dest_port;
1929 dest_port_xlate = c->dest_port_xlate;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001930 dest_priority = reply_cm->priority;
1931 dest_dscp = reply_cm->dscp >> SFE_IPV4_DSCP_SHIFT;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001932 dest_rx_packets = reply_cm->rx_packet_count64;
1933 dest_rx_bytes = reply_cm->rx_byte_count64;
Ken Zhu37040ea2021-09-09 21:11:15 -07001934 dest_mark = reply_cm->mark;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001935 last_sync_jiffies = get_jiffies_64() - c->last_sync_jiffies;
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301936 original_cm_flags = original_cm->flags;
1937 pppoe_session_id = original_cm->pppoe_session_id;
1938 ether_addr_copy(pppoe_remote_mac, original_cm->pppoe_remote_mac);
1939
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001940#ifdef CONFIG_NF_FLOW_COOKIE
1941 src_flow_cookie = original_cm->flow_cookie;
1942 dst_flow_cookie = reply_cm->flow_cookie;
1943#endif
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001944 spin_unlock_bh(&si->lock);
1945
1946 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t\t<connection "
1947 "protocol=\"%u\" "
1948 "src_dev=\"%s\" "
1949 "src_ip=\"%pI4\" src_ip_xlate=\"%pI4\" "
1950 "src_port=\"%u\" src_port_xlate=\"%u\" "
Xiaoping Fane1963d42015-08-25 17:06:19 -07001951 "src_priority=\"%u\" src_dscp=\"%u\" "
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001952 "src_rx_pkts=\"%llu\" src_rx_bytes=\"%llu\" "
Ken Zhu37040ea2021-09-09 21:11:15 -07001953 "src_mark=\"%08x\" "
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001954 "dest_dev=\"%s\" "
1955 "dest_ip=\"%pI4\" dest_ip_xlate=\"%pI4\" "
1956 "dest_port=\"%u\" dest_port_xlate=\"%u\" "
Xiaoping Fane1963d42015-08-25 17:06:19 -07001957 "dest_priority=\"%u\" dest_dscp=\"%u\" "
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001958 "dest_rx_pkts=\"%llu\" dest_rx_bytes=\"%llu\" "
Ken Zhu37040ea2021-09-09 21:11:15 -07001959 "dest_mark=\"%08x\" "
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001960#ifdef CONFIG_NF_FLOW_COOKIE
1961 "src_flow_cookie=\"%d\" dst_flow_cookie=\"%d\" "
1962#endif
Ken Zhu37040ea2021-09-09 21:11:15 -07001963 "last_sync=\"%llu\" ",
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001964 protocol,
1965 src_dev->name,
1966 &src_ip, &src_ip_xlate,
Dave Hudson87973cd2013-10-22 16:00:04 +01001967 ntohs(src_port), ntohs(src_port_xlate),
Xiaoping Fane1963d42015-08-25 17:06:19 -07001968 src_priority, src_dscp,
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001969 src_rx_packets, src_rx_bytes,
Ken Zhu37040ea2021-09-09 21:11:15 -07001970 src_mark,
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001971 dest_dev->name,
1972 &dest_ip, &dest_ip_xlate,
Dave Hudson87973cd2013-10-22 16:00:04 +01001973 ntohs(dest_port), ntohs(dest_port_xlate),
Xiaoping Fane1963d42015-08-25 17:06:19 -07001974 dest_priority, dest_dscp,
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001975 dest_rx_packets, dest_rx_bytes,
Ken Zhu37040ea2021-09-09 21:11:15 -07001976 dest_mark,
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001977#ifdef CONFIG_NF_FLOW_COOKIE
1978 src_flow_cookie, dst_flow_cookie,
1979#endif
Ken Zhu37040ea2021-09-09 21:11:15 -07001980 last_sync_jiffies);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001981
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301982 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 +05301983 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 +05301984 pppoe_session_id, pppoe_remote_mac);
1985 }
1986
1987 bytes_read += snprintf(msg + bytes_read, CHAR_DEV_MSG_SIZE, "/>\n");
1988
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001989 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1990 return false;
1991 }
1992
1993 *length -= bytes_read;
1994 *total_read += bytes_read;
1995
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001996 return true;
1997}
1998
1999/*
2000 * sfe_ipv4_debug_dev_read_connections_end()
2001 * Generate part of the XML output.
2002 */
2003static bool sfe_ipv4_debug_dev_read_connections_end(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2004 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2005{
2006 int bytes_read;
2007
2008 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t</connections>\n");
2009 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2010 return false;
2011 }
2012
2013 *length -= bytes_read;
2014 *total_read += bytes_read;
2015
2016 ws->state++;
2017 return true;
2018}
2019
2020/*
2021 * sfe_ipv4_debug_dev_read_exceptions_start()
2022 * Generate part of the XML output.
2023 */
2024static bool sfe_ipv4_debug_dev_read_exceptions_start(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2025 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2026{
2027 int bytes_read;
2028
2029 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<exceptions>\n");
2030 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2031 return false;
2032 }
2033
2034 *length -= bytes_read;
2035 *total_read += bytes_read;
2036
2037 ws->state++;
2038 return true;
2039}
2040
2041/*
2042 * sfe_ipv4_debug_dev_read_exceptions_exception()
2043 * Generate part of the XML output.
2044 */
2045static bool sfe_ipv4_debug_dev_read_exceptions_exception(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2046 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2047{
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302048 int i;
2049 u64 val = 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002050
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302051 for_each_possible_cpu(i) {
2052 const struct sfe_ipv4_stats *s = per_cpu_ptr(si->stats_pcpu, i);
2053 val += s->exception_events64[ws->iter_exception];
2054 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002055
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302056 if (val) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002057 int bytes_read;
2058
2059 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE,
2060 "\t\t<exception name=\"%s\" count=\"%llu\" />\n",
2061 sfe_ipv4_exception_events_string[ws->iter_exception],
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302062 val);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002063 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2064 return false;
2065 }
2066
2067 *length -= bytes_read;
2068 *total_read += bytes_read;
2069 }
2070
2071 ws->iter_exception++;
2072 if (ws->iter_exception >= SFE_IPV4_EXCEPTION_EVENT_LAST) {
2073 ws->iter_exception = 0;
2074 ws->state++;
2075 }
2076
2077 return true;
2078}
2079
2080/*
2081 * sfe_ipv4_debug_dev_read_exceptions_end()
2082 * Generate part of the XML output.
2083 */
2084static bool sfe_ipv4_debug_dev_read_exceptions_end(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2085 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2086{
2087 int bytes_read;
2088
2089 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t</exceptions>\n");
2090 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2091 return false;
2092 }
2093
2094 *length -= bytes_read;
2095 *total_read += bytes_read;
2096
2097 ws->state++;
2098 return true;
2099}
2100
2101/*
2102 * sfe_ipv4_debug_dev_read_stats()
2103 * Generate part of the XML output.
2104 */
2105static bool sfe_ipv4_debug_dev_read_stats(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2106 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2107{
2108 int bytes_read;
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302109 struct sfe_ipv4_stats stats;
2110 unsigned int num_conn;
2111
2112 sfe_ipv4_update_summary_stats(si, &stats);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002113
2114 spin_lock_bh(&si->lock);
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302115 num_conn = si->num_connections;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002116 spin_unlock_bh(&si->lock);
2117
2118 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<stats "
2119 "num_connections=\"%u\" "
Amitesh Anand63be37d2021-12-24 20:51:48 +05302120 "pkts_dropped=\"%llu\" "
Xiaoping Fan59176422015-05-22 15:58:10 -07002121 "pkts_forwarded=\"%llu\" pkts_not_forwarded=\"%llu\" "
2122 "create_requests=\"%llu\" create_collisions=\"%llu\" "
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05302123 "create_failures=\"%llu\" "
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002124 "destroy_requests=\"%llu\" destroy_misses=\"%llu\" "
2125 "flushes=\"%llu\" "
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +05302126 "hash_hits=\"%llu\" hash_reorders=\"%llu\" "
2127 "pppoe_encap_pkts_fwded=\"%llu\" "
Guduri Prathyusha034d6352022-01-12 16:49:04 +05302128 "pppoe_decap_pkts_fwded=\"%llu\" "
2129 "pppoe_bridge_pkts_fwded=\"%llu\" />\n",
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302130 num_conn,
Amitesh Anand63be37d2021-12-24 20:51:48 +05302131 stats.packets_dropped64,
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302132 stats.packets_forwarded64,
2133 stats.packets_not_forwarded64,
2134 stats.connection_create_requests64,
2135 stats.connection_create_collisions64,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05302136 stats.connection_create_failures64,
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302137 stats.connection_destroy_requests64,
2138 stats.connection_destroy_misses64,
2139 stats.connection_flushes64,
2140 stats.connection_match_hash_hits64,
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +05302141 stats.connection_match_hash_reorders64,
2142 stats.pppoe_encap_packets_forwarded64,
Guduri Prathyusha034d6352022-01-12 16:49:04 +05302143 stats.pppoe_decap_packets_forwarded64,
2144 stats.pppoe_bridge_packets_forwarded64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002145 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2146 return false;
2147 }
2148
2149 *length -= bytes_read;
2150 *total_read += bytes_read;
2151
2152 ws->state++;
2153 return true;
2154}
2155
2156/*
2157 * sfe_ipv4_debug_dev_read_end()
2158 * Generate part of the XML output.
2159 */
2160static bool sfe_ipv4_debug_dev_read_end(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2161 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2162{
2163 int bytes_read;
2164
2165 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "</sfe_ipv4>\n");
2166 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2167 return false;
2168 }
2169
2170 *length -= bytes_read;
2171 *total_read += bytes_read;
2172
2173 ws->state++;
2174 return true;
2175}
2176
2177/*
2178 * Array of write functions that write various XML elements that correspond to
2179 * our XML output state machine.
2180 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07002181static 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 +01002182 sfe_ipv4_debug_dev_read_start,
2183 sfe_ipv4_debug_dev_read_connections_start,
2184 sfe_ipv4_debug_dev_read_connections_connection,
2185 sfe_ipv4_debug_dev_read_connections_end,
2186 sfe_ipv4_debug_dev_read_exceptions_start,
2187 sfe_ipv4_debug_dev_read_exceptions_exception,
2188 sfe_ipv4_debug_dev_read_exceptions_end,
2189 sfe_ipv4_debug_dev_read_stats,
2190 sfe_ipv4_debug_dev_read_end,
2191};
2192
2193/*
2194 * sfe_ipv4_debug_dev_read()
2195 * Send info to userspace upon read request from user
2196 */
2197static ssize_t sfe_ipv4_debug_dev_read(struct file *filp, char *buffer, size_t length, loff_t *offset)
2198{
2199 char msg[CHAR_DEV_MSG_SIZE];
2200 int total_read = 0;
2201 struct sfe_ipv4_debug_xml_write_state *ws;
2202 struct sfe_ipv4 *si = &__si;
2203
2204 ws = (struct sfe_ipv4_debug_xml_write_state *)filp->private_data;
2205 while ((ws->state != SFE_IPV4_DEBUG_XML_STATE_DONE) && (length > CHAR_DEV_MSG_SIZE)) {
2206 if ((sfe_ipv4_debug_xml_write_methods[ws->state])(si, buffer, msg, &length, &total_read, ws)) {
2207 continue;
2208 }
2209 }
2210
2211 return total_read;
2212}
2213
2214/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002215 * sfe_ipv4_debug_dev_open()
2216 */
2217static int sfe_ipv4_debug_dev_open(struct inode *inode, struct file *file)
2218{
2219 struct sfe_ipv4_debug_xml_write_state *ws;
2220
2221 ws = (struct sfe_ipv4_debug_xml_write_state *)file->private_data;
2222 if (!ws) {
2223 ws = kzalloc(sizeof(struct sfe_ipv4_debug_xml_write_state), GFP_KERNEL);
2224 if (!ws) {
2225 return -ENOMEM;
2226 }
2227
2228 ws->state = SFE_IPV4_DEBUG_XML_STATE_START;
2229 file->private_data = ws;
2230 }
2231
2232 return 0;
2233}
2234
2235/*
2236 * sfe_ipv4_debug_dev_release()
2237 */
2238static int sfe_ipv4_debug_dev_release(struct inode *inode, struct file *file)
2239{
2240 struct sfe_ipv4_debug_xml_write_state *ws;
2241
2242 ws = (struct sfe_ipv4_debug_xml_write_state *)file->private_data;
2243 if (ws) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002244 /*
2245 * We've finished with our output so free the write state.
2246 */
2247 kfree(ws);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05302248 file->private_data = NULL;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002249 }
2250
2251 return 0;
2252}
2253
2254/*
2255 * File operations used in the debug char device
2256 */
2257static struct file_operations sfe_ipv4_debug_dev_fops = {
2258 .read = sfe_ipv4_debug_dev_read,
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002259 .open = sfe_ipv4_debug_dev_open,
2260 .release = sfe_ipv4_debug_dev_release
2261};
2262
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08002263#ifdef CONFIG_NF_FLOW_COOKIE
2264/*
2265 * sfe_register_flow_cookie_cb
2266 * register a function in SFE to let SFE use this function to configure flow cookie for a flow
2267 *
2268 * Hardware driver which support flow cookie should register a callback function in SFE. Then SFE
2269 * can use this function to configure flow cookie for a flow.
2270 * return: 0, success; !=0, fail
2271 */
2272int sfe_register_flow_cookie_cb(flow_cookie_set_func_t cb)
2273{
2274 struct sfe_ipv4 *si = &__si;
2275
2276 BUG_ON(!cb);
2277
2278 if (si->flow_cookie_set_func) {
2279 return -1;
2280 }
2281
2282 rcu_assign_pointer(si->flow_cookie_set_func, cb);
2283 return 0;
2284}
2285
2286/*
2287 * sfe_unregister_flow_cookie_cb
2288 * unregister function which is used to configure flow cookie for a flow
2289 *
2290 * return: 0, success; !=0, fail
2291 */
2292int sfe_unregister_flow_cookie_cb(flow_cookie_set_func_t cb)
2293{
2294 struct sfe_ipv4 *si = &__si;
2295
2296 RCU_INIT_POINTER(si->flow_cookie_set_func, NULL);
2297 return 0;
2298}
Xiaoping Fan640faf42015-08-28 15:50:55 -07002299
2300/*
2301 * sfe_ipv4_get_flow_cookie()
2302 */
2303static ssize_t sfe_ipv4_get_flow_cookie(struct device *dev,
2304 struct device_attribute *attr,
2305 char *buf)
2306{
2307 struct sfe_ipv4 *si = &__si;
Xiaoping Fan01c67cc2015-11-09 11:31:57 -08002308 return snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", si->flow_cookie_enable);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002309}
2310
2311/*
2312 * sfe_ipv4_set_flow_cookie()
2313 */
2314static ssize_t sfe_ipv4_set_flow_cookie(struct device *dev,
2315 struct device_attribute *attr,
2316 const char *buf, size_t size)
2317{
2318 struct sfe_ipv4 *si = &__si;
Ken Zhu137722d2021-09-23 17:57:36 -07002319 si->flow_cookie_enable = simple_strtol(buf, NULL, 0);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002320
2321 return size;
2322}
2323
2324/*
2325 * sysfs attributes.
2326 */
2327static const struct device_attribute sfe_ipv4_flow_cookie_attr =
Xiaoping Fane70da412016-02-26 16:47:57 -08002328 __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 -08002329#endif /*CONFIG_NF_FLOW_COOKIE*/
2330
Ken Zhu137722d2021-09-23 17:57:36 -07002331/*
2332 * sfe_ipv4_get_cpu()
2333 */
2334static ssize_t sfe_ipv4_get_cpu(struct device *dev,
2335 struct device_attribute *attr,
2336 char *buf)
2337{
2338 struct sfe_ipv4 *si = &__si;
2339 return snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", si->work_cpu);
2340}
2341
2342/*
2343 * sfe_ipv4_set_cpu()
2344 */
2345static ssize_t sfe_ipv4_set_cpu(struct device *dev,
2346 struct device_attribute *attr,
2347 const char *buf, size_t size)
2348{
2349 struct sfe_ipv4 *si = &__si;
2350 int work_cpu;
2351 work_cpu = simple_strtol(buf, NULL, 0);
2352 if ((work_cpu >= 0) && (work_cpu <= NR_CPUS)) {
2353 si->work_cpu = work_cpu;
2354 } else {
2355 dev_err(dev, "%s is not in valid range[0,%d]", buf, NR_CPUS);
2356 }
2357 return size;
2358}
2359/*
2360 * sysfs attributes.
2361 */
2362static const struct device_attribute sfe_ipv4_cpu_attr =
2363 __ATTR(stats_work_cpu, S_IWUSR | S_IRUGO, sfe_ipv4_get_cpu, sfe_ipv4_set_cpu);
2364
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05302365 /*
2366 * sfe_ipv4_conn_match_hash_init()
2367 * Initialize conn match hash lists
2368 */
2369static void sfe_ipv4_conn_match_hash_init(struct sfe_ipv4 *si, int len)
2370{
2371 struct hlist_head *hash_list = si->hlist_conn_match_hash_head;
2372 int i;
2373
2374 for (i = 0; i < len; i++) {
2375 INIT_HLIST_HEAD(&hash_list[i]);
2376 }
2377}
2378
Amitesh Anand63be37d2021-12-24 20:51:48 +05302379#ifdef SFE_PROCESS_LOCAL_OUT
2380/*
2381 * sfe_ipv4_local_out()
2382 * Called for packets from ip_local_out() - post encapsulation & other packets
2383 */
2384static unsigned int sfe_ipv4_local_out(void *priv, struct sk_buff *skb, const struct nf_hook_state *nhs)
2385{
Guduri Prathyusha5f27e232022-01-06 14:39:04 +05302386 struct sfe_l2_info l2_info;
2387 l2_info.parse_flags = 0;
2388
Amitesh Anand63be37d2021-12-24 20:51:48 +05302389 DEBUG_TRACE("%px: sfe: sfe_ipv4_local_out hook called.\n", skb);
2390
2391 if (likely(skb->skb_iif)) {
Guduri Prathyusha5f27e232022-01-06 14:39:04 +05302392 return sfe_ipv4_recv(skb->dev, skb, &l2_info, true) ? NF_STOLEN : NF_ACCEPT;
Amitesh Anand63be37d2021-12-24 20:51:48 +05302393 }
2394
2395 return NF_ACCEPT;
2396}
2397
2398/*
2399 * struct nf_hook_ops sfe_ipv4_ops_local_out[]
2400 * Hooks into netfilter local out packet monitoring points.
2401 */
2402static struct nf_hook_ops sfe_ipv4_ops_local_out[] __read_mostly = {
2403
2404 /*
2405 * Local out routing hook is used to monitor packets.
2406 */
2407 {
2408 .hook = sfe_ipv4_local_out,
2409 .pf = PF_INET,
2410 .hooknum = NF_INET_LOCAL_OUT,
2411 .priority = NF_IP_PRI_FIRST,
2412 },
2413};
2414#endif
2415
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002416/*
Dave Hudson87973cd2013-10-22 16:00:04 +01002417 * sfe_ipv4_init()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002418 */
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05302419int sfe_ipv4_init(void)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002420{
2421 struct sfe_ipv4 *si = &__si;
2422 int result = -1;
2423
Dave Hudsondcd08fb2013-11-22 09:25:16 -06002424 DEBUG_INFO("SFE IPv4 init\n");
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002425
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05302426 sfe_ipv4_conn_match_hash_init(si, ARRAY_SIZE(si->hlist_conn_match_hash_head));
2427
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302428 si->stats_pcpu = alloc_percpu_gfp(struct sfe_ipv4_stats, GFP_KERNEL | __GFP_ZERO);
2429 if (!si->stats_pcpu) {
2430 DEBUG_ERROR("failed to allocate stats memory for sfe_ipv4\n");
2431 goto exit0;
2432 }
2433
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002434 /*
2435 * Create sys/sfe_ipv4
2436 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302437 si->sys_ipv4 = kobject_create_and_add("sfe_ipv4", NULL);
2438 if (!si->sys_ipv4) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002439 DEBUG_ERROR("failed to register sfe_ipv4\n");
2440 goto exit1;
2441 }
2442
2443 /*
2444 * Create files, one for each parameter supported by this module.
2445 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302446 result = sysfs_create_file(si->sys_ipv4, &sfe_ipv4_debug_dev_attr.attr);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002447 if (result) {
2448 DEBUG_ERROR("failed to register debug dev file: %d\n", result);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002449 goto exit2;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002450 }
2451
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302452 result = sysfs_create_file(si->sys_ipv4, &sfe_ipv4_cpu_attr.attr);
Ken Zhu137722d2021-09-23 17:57:36 -07002453 if (result) {
2454 DEBUG_ERROR("failed to register debug dev file: %d\n", result);
2455 goto exit3;
2456 }
2457
Xiaoping Fan640faf42015-08-28 15:50:55 -07002458#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302459 result = sysfs_create_file(si->sys_ipv4, &sfe_ipv4_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002460 if (result) {
2461 DEBUG_ERROR("failed to register flow cookie enable file: %d\n", result);
Ken Zhu137722d2021-09-23 17:57:36 -07002462 goto exit4;
Xiaoping Fan640faf42015-08-28 15:50:55 -07002463 }
2464#endif /* CONFIG_NF_FLOW_COOKIE */
2465
Amitesh Anand63be37d2021-12-24 20:51:48 +05302466#ifdef SFE_PROCESS_LOCAL_OUT
2467#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
2468 result = nf_register_hooks(sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2469#else
2470 result = nf_register_net_hooks(&init_net, sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2471#endif
2472 if (result < 0) {
2473 DEBUG_ERROR("can't register nf local out hook: %d\n", result);
2474 goto exit5;
2475 }
2476 DEBUG_INFO("Register nf local out hook success: %d\n", result);
2477#endif
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002478 /*
2479 * Register our debug char device.
2480 */
2481 result = register_chrdev(0, "sfe_ipv4", &sfe_ipv4_debug_dev_fops);
2482 if (result < 0) {
2483 DEBUG_ERROR("Failed to register chrdev: %d\n", result);
Amitesh Anand63be37d2021-12-24 20:51:48 +05302484 goto exit6;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002485 }
2486
2487 si->debug_dev = result;
Ken Zhu137722d2021-09-23 17:57:36 -07002488 si->work_cpu = WORK_CPU_UNBOUND;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002489
2490 /*
Ken Zhu137722d2021-09-23 17:57:36 -07002491 * Create a work to handle periodic statistics.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002492 */
Ken Zhu137722d2021-09-23 17:57:36 -07002493 INIT_DELAYED_WORK(&(si->sync_dwork), sfe_ipv4_periodic_sync);
2494 schedule_delayed_work_on(si->work_cpu, &(si->sync_dwork), ((HZ + 99) / 100));
2495
Dave Hudson87973cd2013-10-22 16:00:04 +01002496 spin_lock_init(&si->lock);
Dave Hudson87973cd2013-10-22 16:00:04 +01002497 return 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002498
Amitesh Anand63be37d2021-12-24 20:51:48 +05302499exit6:
2500#ifdef SFE_PROCESS_LOCAL_OUT
2501 DEBUG_TRACE("sfe: Unregister local out hook\n");
2502#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
2503 nf_unregister_hooks(sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2504#else
2505 nf_unregister_net_hooks(&init_net, sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2506#endif
Ken Zhu137722d2021-09-23 17:57:36 -07002507exit5:
Amitesh Anand63be37d2021-12-24 20:51:48 +05302508#endif
Xiaoping Fan640faf42015-08-28 15:50:55 -07002509#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302510 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002511
Ken Zhu137722d2021-09-23 17:57:36 -07002512exit4:
Xiaoping Fan640faf42015-08-28 15:50:55 -07002513#endif /* CONFIG_NF_FLOW_COOKIE */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302514 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_cpu_attr.attr);
Ken Zhu137722d2021-09-23 17:57:36 -07002515exit3:
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302516 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_debug_dev_attr.attr);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002517
Xiaoping Fan640faf42015-08-28 15:50:55 -07002518exit2:
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302519 kobject_put(si->sys_ipv4);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002520
2521exit1:
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302522 free_percpu(si->stats_pcpu);
2523
2524exit0:
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002525 return result;
2526}
2527
2528/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002529 * sfe_ipv4_exit()
2530 */
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05302531void sfe_ipv4_exit(void)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002532{
Dave Hudson87973cd2013-10-22 16:00:04 +01002533 struct sfe_ipv4 *si = &__si;
2534
Dave Hudsondcd08fb2013-11-22 09:25:16 -06002535 DEBUG_INFO("SFE IPv4 exit\n");
Dave Hudson87973cd2013-10-22 16:00:04 +01002536 /*
2537 * Destroy all connections.
2538 */
Dave Hudsondcd08fb2013-11-22 09:25:16 -06002539 sfe_ipv4_destroy_all_rules_for_dev(NULL);
Dave Hudson87973cd2013-10-22 16:00:04 +01002540
Ken Zhu137722d2021-09-23 17:57:36 -07002541 cancel_delayed_work_sync(&si->sync_dwork);
Dave Hudson87973cd2013-10-22 16:00:04 +01002542
Dave Hudson87973cd2013-10-22 16:00:04 +01002543 unregister_chrdev(si->debug_dev, "sfe_ipv4");
2544
Amitesh Anand63be37d2021-12-24 20:51:48 +05302545#ifdef SFE_PROCESS_LOCAL_OUT
2546 DEBUG_TRACE("sfe: Unregister local out hook\n");
2547#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
2548 nf_unregister_hooks(sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2549#else
2550 nf_unregister_net_hooks(&init_net, sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2551#endif
2552#endif
2553
Xiaoping Fan640faf42015-08-28 15:50:55 -07002554#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302555 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002556#endif /* CONFIG_NF_FLOW_COOKIE */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302557 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_debug_dev_attr.attr);
2558 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_cpu_attr.attr);
Dave Hudson87973cd2013-10-22 16:00:04 +01002559
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302560 kobject_put(si->sys_ipv4);
Dave Hudson87973cd2013-10-22 16:00:04 +01002561
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302562 free_percpu(si->stats_pcpu);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002563}
2564
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08002565#ifdef CONFIG_NF_FLOW_COOKIE
2566EXPORT_SYMBOL(sfe_register_flow_cookie_cb);
2567EXPORT_SYMBOL(sfe_unregister_flow_cookie_cb);
2568#endif