blob: 5da6615449e3a6f49043e2333e12094162b4948e [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.
6 * Copyright (c) 2021 Qualcomm Innovation Center, Inc. All rights reserved.
7 *
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>
Dave Hudsondcd08fb2013-11-22 09:25:16 -060026#include <linux/etherdevice.h>
Tian Yang45f39c82020-10-06 14:07:47 -070027#include <linux/version.h>
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +053028#include <linux/lockdep.h>
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010029
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +053030#include "sfe_debug.h"
Ratheesh Kannoth89302a72021-10-20 08:10:37 +053031#include "sfe_api.h"
Dave Hudsondcd08fb2013-11-22 09:25:16 -060032#include "sfe.h"
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +053033#include "sfe_flow_cookie.h"
34#include "sfe_ipv4.h"
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +053035#include "sfe_ipv4_udp.h"
36#include "sfe_ipv4_tcp.h"
37#include "sfe_ipv4_icmp.h"
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010038
39static char *sfe_ipv4_exception_events_string[SFE_IPV4_EXCEPTION_EVENT_LAST] = {
40 "UDP_HEADER_INCOMPLETE",
41 "UDP_NO_CONNECTION",
42 "UDP_IP_OPTIONS_OR_INITIAL_FRAGMENT",
43 "UDP_SMALL_TTL",
44 "UDP_NEEDS_FRAGMENTATION",
45 "TCP_HEADER_INCOMPLETE",
46 "TCP_NO_CONNECTION_SLOW_FLAGS",
47 "TCP_NO_CONNECTION_FAST_FLAGS",
48 "TCP_IP_OPTIONS_OR_INITIAL_FRAGMENT",
49 "TCP_SMALL_TTL",
50 "TCP_NEEDS_FRAGMENTATION",
51 "TCP_FLAGS",
52 "TCP_SEQ_EXCEEDS_RIGHT_EDGE",
53 "TCP_SMALL_DATA_OFFS",
54 "TCP_BAD_SACK",
55 "TCP_BIG_DATA_OFFS",
56 "TCP_SEQ_BEFORE_LEFT_EDGE",
57 "TCP_ACK_EXCEEDS_RIGHT_EDGE",
58 "TCP_ACK_BEFORE_LEFT_EDGE",
59 "ICMP_HEADER_INCOMPLETE",
60 "ICMP_UNHANDLED_TYPE",
61 "ICMP_IPV4_HEADER_INCOMPLETE",
62 "ICMP_IPV4_NON_V4",
63 "ICMP_IPV4_IP_OPTIONS_INCOMPLETE",
64 "ICMP_IPV4_UDP_HEADER_INCOMPLETE",
65 "ICMP_IPV4_TCP_HEADER_INCOMPLETE",
66 "ICMP_IPV4_UNHANDLED_PROTOCOL",
67 "ICMP_NO_CONNECTION",
68 "ICMP_FLUSHED_CONNECTION",
69 "HEADER_INCOMPLETE",
70 "BAD_TOTAL_LENGTH",
71 "NON_V4",
72 "NON_INITIAL_FRAGMENT",
73 "DATAGRAM_INCOMPLETE",
74 "IP_OPTIONS_INCOMPLETE",
75 "UNHANDLED_PROTOCOL"
76};
77
Xiaoping Fan6a1672f2016-08-17 19:58:12 -070078static struct sfe_ipv4 __si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010079
80/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010081 * sfe_ipv4_gen_ip_csum()
82 * Generate the IP checksum for an IPv4 header.
83 *
84 * Note that this function assumes that we have only 20 bytes of IP header.
85 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +053086u16 sfe_ipv4_gen_ip_csum(struct iphdr *iph)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010087{
Xiaoping Fan6a1672f2016-08-17 19:58:12 -070088 u32 sum;
89 u16 *i = (u16 *)iph;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010090
91 iph->check = 0;
92
93 /*
94 * Generate the sum.
95 */
96 sum = i[0] + i[1] + i[2] + i[3] + i[4] + i[5] + i[6] + i[7] + i[8] + i[9];
97
98 /*
99 * Fold it to ones-complement form.
100 */
101 sum = (sum & 0xffff) + (sum >> 16);
102 sum = (sum & 0xffff) + (sum >> 16);
103
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700104 return (u16)sum ^ 0xffff;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100105}
106
107/*
108 * sfe_ipv4_get_connection_match_hash()
109 * Generate the hash used in connection match lookups.
110 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700111static inline unsigned int sfe_ipv4_get_connection_match_hash(struct net_device *dev, u8 protocol,
Dave Hudson87973cd2013-10-22 16:00:04 +0100112 __be32 src_ip, __be16 src_port,
113 __be32 dest_ip, __be16 dest_port)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100114{
115 size_t dev_addr = (size_t)dev;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700116 u32 hash = ((u32)dev_addr) ^ ntohl(src_ip ^ dest_ip) ^ protocol ^ ntohs(src_port ^ dest_port);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100117 return ((hash >> SFE_IPV4_CONNECTION_HASH_SHIFT) ^ hash) & SFE_IPV4_CONNECTION_HASH_MASK;
118}
119
120/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530121 * sfe_ipv4_find_connection_match_rcu()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100122 * Get the IPv4 flow match info that corresponds to a particular 5-tuple.
123 *
124 * On entry we must be holding the lock that protects the hash table.
125 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530126struct sfe_ipv4_connection_match *
127sfe_ipv4_find_connection_match_rcu(struct sfe_ipv4 *si, struct net_device *dev, u8 protocol,
Dave Hudson87973cd2013-10-22 16:00:04 +0100128 __be32 src_ip, __be16 src_port,
129 __be32 dest_ip, __be16 dest_port)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100130{
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530131 struct sfe_ipv4_connection_match *cm = NULL;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100132 unsigned int conn_match_idx;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530133 struct hlist_head *lhead;
134
135 WARN_ON_ONCE(!rcu_read_lock_held());
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100136
137 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 +0100138
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530139 lhead = &si->hlist_conn_match_hash_head[conn_match_idx];
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100140
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530141 hlist_for_each_entry_rcu(cm, lhead, hnode) {
142 if (cm->match_src_port != src_port
143 || cm->match_dest_port != dest_port
144 || cm->match_src_ip != src_ip
145 || cm->match_dest_ip != dest_ip
146 || cm->match_protocol != protocol
147 || cm->match_dev != dev) {
148 continue;
149 }
150
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530151 this_cpu_inc(si->stats_pcpu->connection_match_hash_hits64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100152
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530153 break;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100154 }
155
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100156 return cm;
157}
158
159/*
160 * sfe_ipv4_connection_match_update_summary_stats()
161 * Update the summary stats for a connection match entry.
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530162 *
163 * Stats are incremented atomically. So use atomic substraction to update summary
164 * stats.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100165 */
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530166static inline void sfe_ipv4_connection_match_update_summary_stats(struct sfe_ipv4_connection_match *cm,
167 u32 *packets, u32 *bytes)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100168{
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530169 u32 packet_count, byte_count;
170
171 packet_count = atomic_read(&cm->rx_packet_count);
172 cm->rx_packet_count64 += packet_count;
173 atomic_sub(packet_count, &cm->rx_packet_count);
174
175 byte_count = atomic_read(&cm->rx_byte_count);
176 cm->rx_byte_count64 += byte_count;
177 atomic_sub(byte_count, &cm->rx_byte_count);
178
179 *packets = packet_count;
180 *bytes = byte_count;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100181}
182
183/*
184 * sfe_ipv4_connection_match_compute_translations()
185 * Compute port and address translations for a connection match entry.
186 */
187static void sfe_ipv4_connection_match_compute_translations(struct sfe_ipv4_connection_match *cm)
188{
189 /*
190 * Before we insert the entry look to see if this is tagged as doing address
191 * translations. If it is then work out the adjustment that we need to apply
192 * to the transport checksum.
193 */
194 if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC) {
195 /*
196 * Precompute an incremental checksum adjustment so we can
197 * edit packets in this stream very quickly. The algorithm is from RFC1624.
198 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700199 u16 src_ip_hi = cm->match_src_ip >> 16;
200 u16 src_ip_lo = cm->match_src_ip & 0xffff;
201 u32 xlate_src_ip = ~cm->xlate_src_ip;
202 u16 xlate_src_ip_hi = xlate_src_ip >> 16;
203 u16 xlate_src_ip_lo = xlate_src_ip & 0xffff;
204 u16 xlate_src_port = ~cm->xlate_src_port;
205 u32 adj;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100206
207 /*
208 * When we compute this fold it down to a 16-bit offset
209 * as that way we can avoid having to do a double
210 * folding of the twos-complement result because the
211 * addition of 2 16-bit values cannot cause a double
212 * wrap-around!
213 */
214 adj = src_ip_hi + src_ip_lo + cm->match_src_port
215 + xlate_src_ip_hi + xlate_src_ip_lo + xlate_src_port;
216 adj = (adj & 0xffff) + (adj >> 16);
217 adj = (adj & 0xffff) + (adj >> 16);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700218 cm->xlate_src_csum_adjustment = (u16)adj;
Nicolas Costaac2979c2014-01-14 10:35:24 -0600219
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100220 }
221
222 if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST) {
223 /*
224 * Precompute an incremental checksum adjustment so we can
225 * edit packets in this stream very quickly. The algorithm is from RFC1624.
226 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700227 u16 dest_ip_hi = cm->match_dest_ip >> 16;
228 u16 dest_ip_lo = cm->match_dest_ip & 0xffff;
229 u32 xlate_dest_ip = ~cm->xlate_dest_ip;
230 u16 xlate_dest_ip_hi = xlate_dest_ip >> 16;
231 u16 xlate_dest_ip_lo = xlate_dest_ip & 0xffff;
232 u16 xlate_dest_port = ~cm->xlate_dest_port;
233 u32 adj;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100234
235 /*
236 * When we compute this fold it down to a 16-bit offset
237 * as that way we can avoid having to do a double
238 * folding of the twos-complement result because the
239 * addition of 2 16-bit values cannot cause a double
240 * wrap-around!
241 */
242 adj = dest_ip_hi + dest_ip_lo + cm->match_dest_port
243 + xlate_dest_ip_hi + xlate_dest_ip_lo + xlate_dest_port;
244 adj = (adj & 0xffff) + (adj >> 16);
245 adj = (adj & 0xffff) + (adj >> 16);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700246 cm->xlate_dest_csum_adjustment = (u16)adj;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100247 }
Xiaoping Fanad755af2015-04-01 16:58:46 -0700248
249 if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC) {
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700250 u32 adj = ~cm->match_src_ip + cm->xlate_src_ip;
Xiaoping Fanad755af2015-04-01 16:58:46 -0700251 if (adj < cm->xlate_src_ip) {
252 adj++;
253 }
254
255 adj = (adj & 0xffff) + (adj >> 16);
256 adj = (adj & 0xffff) + (adj >> 16);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700257 cm->xlate_src_partial_csum_adjustment = (u16)adj;
Xiaoping Fanad755af2015-04-01 16:58:46 -0700258 }
259
260 if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST) {
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700261 u32 adj = ~cm->match_dest_ip + cm->xlate_dest_ip;
Xiaoping Fanad755af2015-04-01 16:58:46 -0700262 if (adj < cm->xlate_dest_ip) {
263 adj++;
264 }
265
266 adj = (adj & 0xffff) + (adj >> 16);
267 adj = (adj & 0xffff) + (adj >> 16);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700268 cm->xlate_dest_partial_csum_adjustment = (u16)adj;
Xiaoping Fanad755af2015-04-01 16:58:46 -0700269 }
270
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100271}
272
273/*
274 * sfe_ipv4_update_summary_stats()
275 * Update the summary stats.
276 */
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530277static void sfe_ipv4_update_summary_stats(struct sfe_ipv4 *si, struct sfe_ipv4_stats *stats)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100278{
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530279 int i = 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100280
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530281 memset(stats, 0, sizeof(*stats));
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100282
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530283 for_each_possible_cpu(i) {
284 const struct sfe_ipv4_stats *s = per_cpu_ptr(si->stats_pcpu, i);
285
286 stats->connection_create_requests64 += s->connection_create_requests64;
287 stats->connection_create_collisions64 += s->connection_create_collisions64;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530288 stats->connection_create_failures64 += s->connection_create_failures64;
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530289 stats->connection_destroy_requests64 += s->connection_destroy_requests64;
290 stats->connection_destroy_misses64 += s->connection_destroy_misses64;
291 stats->connection_match_hash_hits64 += s->connection_match_hash_hits64;
292 stats->connection_match_hash_reorders64 += s->connection_match_hash_reorders64;
293 stats->connection_flushes64 += s->connection_flushes64;
294 stats->packets_forwarded64 += s->packets_forwarded64;
295 stats->packets_not_forwarded64 += s->packets_not_forwarded64;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100296 }
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530297
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100298}
299
300/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530301 * sfe_ipv4_insert_connection_match()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100302 * Insert a connection match into the hash.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100303 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530304static inline void sfe_ipv4_insert_connection_match(struct sfe_ipv4 *si,
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700305 struct sfe_ipv4_connection_match *cm)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100306{
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100307 unsigned int conn_match_idx
308 = sfe_ipv4_get_connection_match_hash(cm->match_dev, cm->match_protocol,
309 cm->match_src_ip, cm->match_src_port,
310 cm->match_dest_ip, cm->match_dest_port);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700311
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530312 lockdep_assert_held(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100313
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530314 hlist_add_head_rcu(&cm->hnode, &si->hlist_conn_match_hash_head[conn_match_idx]);
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800315#ifdef CONFIG_NF_FLOW_COOKIE
Xiaoping Fan640faf42015-08-28 15:50:55 -0700316 if (!si->flow_cookie_enable)
317 return;
318
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800319 /*
320 * Configure hardware to put a flow cookie in packet of this flow,
321 * then we can accelerate the lookup process when we received this packet.
322 */
323 for (conn_match_idx = 1; conn_match_idx < SFE_FLOW_COOKIE_SIZE; conn_match_idx++) {
324 struct sfe_flow_cookie_entry *entry = &si->sfe_flow_cookie_table[conn_match_idx];
325
326 if ((NULL == entry->match) && time_is_before_jiffies(entry->last_clean_time + HZ)) {
327 flow_cookie_set_func_t func;
328
329 rcu_read_lock();
330 func = rcu_dereference(si->flow_cookie_set_func);
331 if (func) {
Xiaoping Fan59176422015-05-22 15:58:10 -0700332 if (!func(cm->match_protocol, cm->match_src_ip, cm->match_src_port,
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800333 cm->match_dest_ip, cm->match_dest_port, conn_match_idx)) {
334 entry->match = cm;
335 cm->flow_cookie = conn_match_idx;
336 }
337 }
338 rcu_read_unlock();
339
340 break;
341 }
342 }
343#endif
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100344}
345
346/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530347 * sfe_ipv4_remove_connection_match()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100348 * Remove a connection match object from the hash.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100349 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530350static inline void sfe_ipv4_remove_connection_match(struct sfe_ipv4 *si, struct sfe_ipv4_connection_match *cm)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100351{
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530352
353 lockdep_assert_held(&si->lock);
354
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800355#ifdef CONFIG_NF_FLOW_COOKIE
Xiaoping Fan640faf42015-08-28 15:50:55 -0700356 if (si->flow_cookie_enable) {
357 /*
358 * Tell hardware that we no longer need a flow cookie in packet of this flow
359 */
360 unsigned int conn_match_idx;
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800361
Xiaoping Fan640faf42015-08-28 15:50:55 -0700362 for (conn_match_idx = 1; conn_match_idx < SFE_FLOW_COOKIE_SIZE; conn_match_idx++) {
363 struct sfe_flow_cookie_entry *entry = &si->sfe_flow_cookie_table[conn_match_idx];
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800364
Xiaoping Fan640faf42015-08-28 15:50:55 -0700365 if (cm == entry->match) {
366 flow_cookie_set_func_t func;
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800367
Xiaoping Fan640faf42015-08-28 15:50:55 -0700368 rcu_read_lock();
369 func = rcu_dereference(si->flow_cookie_set_func);
370 if (func) {
371 func(cm->match_protocol, cm->match_src_ip, cm->match_src_port,
372 cm->match_dest_ip, cm->match_dest_port, 0);
373 }
374 rcu_read_unlock();
375
376 cm->flow_cookie = 0;
377 entry->match = NULL;
378 entry->last_clean_time = jiffies;
379 break;
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800380 }
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800381 }
382 }
383#endif
384
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530385 hlist_del_init_rcu(&cm->hnode);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100386
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100387}
388
389/*
390 * sfe_ipv4_get_connection_hash()
391 * Generate the hash used in connection lookups.
392 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700393static inline unsigned int sfe_ipv4_get_connection_hash(u8 protocol, __be32 src_ip, __be16 src_port,
Dave Hudson87973cd2013-10-22 16:00:04 +0100394 __be32 dest_ip, __be16 dest_port)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100395{
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700396 u32 hash = ntohl(src_ip ^ dest_ip) ^ protocol ^ ntohs(src_port ^ dest_port);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100397 return ((hash >> SFE_IPV4_CONNECTION_HASH_SHIFT) ^ hash) & SFE_IPV4_CONNECTION_HASH_MASK;
398}
399
400/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530401 * sfe_ipv4_find_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100402 * Get the IPv4 connection info that corresponds to a particular 5-tuple.
403 *
404 * On entry we must be holding the lock that protects the hash table.
405 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530406static inline struct sfe_ipv4_connection *sfe_ipv4_find_connection(struct sfe_ipv4 *si, u32 protocol,
Dave Hudson87973cd2013-10-22 16:00:04 +0100407 __be32 src_ip, __be16 src_port,
408 __be32 dest_ip, __be16 dest_port)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100409{
410 struct sfe_ipv4_connection *c;
411 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 +0530412
413 lockdep_assert_held(&si->lock);
414
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100415 c = si->conn_hash[conn_idx];
416
417 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100418 * Will need connection entry for next create/destroy metadata,
419 * So no need to re-order entry for these requests
420 */
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530421 while (c) {
422 if ((c->src_port == src_port)
423 && (c->dest_port == dest_port)
424 && (c->src_ip == src_ip)
425 && (c->dest_ip == dest_ip)
426 && (c->protocol == protocol)) {
427 return c;
428 }
429
430 c = c->next;
431 }
432
433 return NULL;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100434}
435
436/*
Matthew McClintockbe7b47d2013-11-27 13:26:23 -0600437 * sfe_ipv4_mark_rule()
438 * Updates the mark for a current offloaded connection
439 *
440 * Will take hash lock upon entry
441 */
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700442void sfe_ipv4_mark_rule(struct sfe_connection_mark *mark)
Matthew McClintockbe7b47d2013-11-27 13:26:23 -0600443{
444 struct sfe_ipv4 *si = &__si;
445 struct sfe_ipv4_connection *c;
Matthew McClintockdb5ac512014-01-16 17:01:40 -0600446
Xiaoping Fan3c423e32015-07-03 03:09:29 -0700447 spin_lock_bh(&si->lock);
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530448 c = sfe_ipv4_find_connection(si, mark->protocol,
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700449 mark->src_ip.ip, mark->src_port,
450 mark->dest_ip.ip, mark->dest_port);
Matthew McClintockbe7b47d2013-11-27 13:26:23 -0600451 if (c) {
Nicolas Costaf53d6fe2014-01-13 16:03:46 -0600452 WARN_ON((0 != c->mark) && (0 == mark->mark));
Matthew McClintockbe7b47d2013-11-27 13:26:23 -0600453 c->mark = mark->mark;
454 }
Xiaoping Fan3c423e32015-07-03 03:09:29 -0700455 spin_unlock_bh(&si->lock);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700456
457 if (c) {
458 DEBUG_TRACE("Matching connection found for mark, "
459 "setting from %08x to %08x\n",
460 c->mark, mark->mark);
461 }
Matthew McClintockbe7b47d2013-11-27 13:26:23 -0600462}
463
464/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530465 * sfe_ipv4_insert_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100466 * Insert a connection into the hash.
467 *
468 * On entry we must be holding the lock that protects the hash table.
469 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530470static void sfe_ipv4_insert_connection(struct sfe_ipv4 *si, struct sfe_ipv4_connection *c)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100471{
472 struct sfe_ipv4_connection **hash_head;
473 struct sfe_ipv4_connection *prev_head;
474 unsigned int conn_idx;
475
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530476 lockdep_assert_held(&si->lock);
477
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100478 /*
479 * Insert entry into the connection hash.
480 */
481 conn_idx = sfe_ipv4_get_connection_hash(c->protocol, c->src_ip, c->src_port,
482 c->dest_ip, c->dest_port);
483 hash_head = &si->conn_hash[conn_idx];
484 prev_head = *hash_head;
485 c->prev = NULL;
486 if (prev_head) {
487 prev_head->prev = c;
488 }
489
490 c->next = prev_head;
491 *hash_head = c;
492
493 /*
494 * Insert entry into the "all connections" list.
495 */
496 if (si->all_connections_tail) {
497 c->all_connections_prev = si->all_connections_tail;
498 si->all_connections_tail->all_connections_next = c;
499 } else {
500 c->all_connections_prev = NULL;
501 si->all_connections_head = c;
502 }
503
504 si->all_connections_tail = c;
505 c->all_connections_next = NULL;
506 si->num_connections++;
507
508 /*
509 * Insert the connection match objects too.
510 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530511 sfe_ipv4_insert_connection_match(si, c->original_match);
512 sfe_ipv4_insert_connection_match(si, c->reply_match);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100513}
514
515/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530516 * sfe_ipv4_remove_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100517 * Remove a sfe_ipv4_connection object from the hash.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100518 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530519bool sfe_ipv4_remove_connection(struct sfe_ipv4 *si, struct sfe_ipv4_connection *c)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100520{
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530521 lockdep_assert_held(&si->lock);
522
523 if (c->removed) {
524 DEBUG_ERROR("%px: Connection has been removed already\n", c);
525 return false;
526 }
527
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100528 /*
529 * Remove the connection match objects.
530 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530531 sfe_ipv4_remove_connection_match(si, c->reply_match);
532 sfe_ipv4_remove_connection_match(si, c->original_match);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100533
534 /*
535 * Unlink the connection.
536 */
537 if (c->prev) {
538 c->prev->next = c->next;
539 } else {
540 unsigned int conn_idx = sfe_ipv4_get_connection_hash(c->protocol, c->src_ip, c->src_port,
541 c->dest_ip, c->dest_port);
542 si->conn_hash[conn_idx] = c->next;
543 }
544
545 if (c->next) {
546 c->next->prev = c->prev;
547 }
Xiaoping Fan34586472015-07-03 02:20:35 -0700548
549 /*
550 * Unlink connection from all_connections list
551 */
552 if (c->all_connections_prev) {
553 c->all_connections_prev->all_connections_next = c->all_connections_next;
554 } else {
555 si->all_connections_head = c->all_connections_next;
556 }
557
558 if (c->all_connections_next) {
559 c->all_connections_next->all_connections_prev = c->all_connections_prev;
560 } else {
561 si->all_connections_tail = c->all_connections_prev;
562 }
563
Ken Zhudc423672021-09-02 18:27:01 -0700564 /*
565 * If I am the next sync connection, move the sync to my next or head.
566 */
567 if (unlikely(si->wc_next == c)) {
568 si->wc_next = c->all_connections_next;
569 }
570
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530571 c->removed = true;
Xiaoping Fan34586472015-07-03 02:20:35 -0700572 si->num_connections--;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530573 return true;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100574}
575
576/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530577 * sfe_ipv4_gen_sync_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100578 * Sync a connection.
579 *
580 * On entry to this function we expect that the lock for the connection is either
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530581 * already held (while called from sfe_ipv4_periodic_sync() or isn't required
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530582 * (while called from sfe_ipv4_flush_connection())
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100583 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530584static void sfe_ipv4_gen_sync_connection(struct sfe_ipv4 *si, struct sfe_ipv4_connection *c,
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700585 struct sfe_connection_sync *sis, sfe_sync_reason_t reason,
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700586 u64 now_jiffies)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100587{
588 struct sfe_ipv4_connection_match *original_cm;
589 struct sfe_ipv4_connection_match *reply_cm;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530590 u32 packet_count, byte_count;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100591
592 /*
593 * Fill in the update message.
594 */
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700595 sis->is_v6 = 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100596 sis->protocol = c->protocol;
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700597 sis->src_ip.ip = c->src_ip;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700598 sis->src_ip_xlate.ip = c->src_ip_xlate;
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700599 sis->dest_ip.ip = c->dest_ip;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700600 sis->dest_ip_xlate.ip = c->dest_ip_xlate;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100601 sis->src_port = c->src_port;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700602 sis->src_port_xlate = c->src_port_xlate;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100603 sis->dest_port = c->dest_port;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700604 sis->dest_port_xlate = c->dest_port_xlate;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100605
606 original_cm = c->original_match;
607 reply_cm = c->reply_match;
608 sis->src_td_max_window = original_cm->protocol_state.tcp.max_win;
609 sis->src_td_end = original_cm->protocol_state.tcp.end;
610 sis->src_td_max_end = original_cm->protocol_state.tcp.max_end;
611 sis->dest_td_max_window = reply_cm->protocol_state.tcp.max_win;
612 sis->dest_td_end = reply_cm->protocol_state.tcp.end;
613 sis->dest_td_max_end = reply_cm->protocol_state.tcp.max_end;
614
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530615 sfe_ipv4_connection_match_update_summary_stats(original_cm, &packet_count, &byte_count);
616 sis->src_new_packet_count = packet_count;
617 sis->src_new_byte_count = byte_count;
Matthew McClintockd0cdb802014-02-24 16:30:35 -0600618
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530619 sfe_ipv4_connection_match_update_summary_stats(reply_cm, &packet_count, &byte_count);
620 sis->dest_new_packet_count = packet_count;
621 sis->dest_new_byte_count = byte_count;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100622
Matthew McClintockd0cdb802014-02-24 16:30:35 -0600623 sis->src_dev = original_cm->match_dev;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100624 sis->src_packet_count = original_cm->rx_packet_count64;
625 sis->src_byte_count = original_cm->rx_byte_count64;
Matthew McClintockd0cdb802014-02-24 16:30:35 -0600626
627 sis->dest_dev = reply_cm->match_dev;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100628 sis->dest_packet_count = reply_cm->rx_packet_count64;
629 sis->dest_byte_count = reply_cm->rx_byte_count64;
630
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700631 sis->reason = reason;
632
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100633 /*
634 * Get the time increment since our last sync.
635 */
636 sis->delta_jiffies = now_jiffies - c->last_sync_jiffies;
637 c->last_sync_jiffies = now_jiffies;
638}
639
640/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530641 * sfe_ipv4_free_connection_rcu()
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530642 * Called at RCU qs state to free the connection object.
643 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530644static void sfe_ipv4_free_connection_rcu(struct rcu_head *head)
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530645{
646 struct sfe_ipv4_connection *c;
647
648 /*
649 * We dont need spin lock as the connection is already removed from link list
650 */
651 c = container_of(head, struct sfe_ipv4_connection, rcu);
652
653 BUG_ON(!c->removed);
654
655 DEBUG_TRACE("%px: connecton has been deleted\n", c);
656
657 /*
658 * Release our hold of the source and dest devices and free the memory
659 * for our connection objects.
660 */
661 dev_put(c->original_dev);
662 dev_put(c->reply_dev);
663 kfree(c->original_match);
664 kfree(c->reply_match);
665 kfree(c);
666}
667
668/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530669 * sfe_ipv4_flush_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100670 * Flush a connection and free all associated resources.
671 *
672 * We need to be called with bottom halves disabled locally as we need to acquire
673 * the connection hash lock and release it again. In general we're actually called
674 * from within a BH and so we're fine, but we're also called when connections are
675 * torn down.
676 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530677void sfe_ipv4_flush_connection(struct sfe_ipv4 *si,
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700678 struct sfe_ipv4_connection *c,
679 sfe_sync_reason_t reason)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100680{
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700681 u64 now_jiffies;
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700682 sfe_sync_rule_callback_t sync_rule_callback;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100683
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530684 BUG_ON(!c->removed);
685
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530686 this_cpu_inc(si->stats_pcpu->connection_flushes64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100687
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530688 rcu_read_lock();
689 sync_rule_callback = rcu_dereference(si->sync_rule_callback);
690
691 /*
692 * Generate a sync message and then sync.
693 */
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600694 if (sync_rule_callback) {
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530695 struct sfe_connection_sync sis;
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600696 now_jiffies = get_jiffies_64();
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530697 sfe_ipv4_gen_sync_connection(si, c, &sis, reason, now_jiffies);
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600698 sync_rule_callback(&sis);
699 }
700
701 rcu_read_unlock();
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100702
703 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100704 * Release our hold of the source and dest devices and free the memory
705 * for our connection objects.
706 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530707 call_rcu(&c->rcu, sfe_ipv4_free_connection_rcu);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100708}
709
710/*
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530711 * sfe_ipv4_exception_stats_inc()
712 * Increment exception stats.
713 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530714void sfe_ipv4_exception_stats_inc(struct sfe_ipv4 *si, enum sfe_ipv4_exception_events reason)
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530715{
716 struct sfe_ipv4_stats *stats = this_cpu_ptr(si->stats_pcpu);
717 stats->exception_events64[reason]++;
718 stats->packets_not_forwarded64++;
719}
720
721/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100722 * sfe_ipv4_recv()
Matthew McClintocka8ad7962014-01-16 16:49:30 -0600723 * Handle packet receives and forwaring.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100724 *
725 * Returns 1 if the packet is forwarded or 0 if it isn't.
726 */
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600727int sfe_ipv4_recv(struct net_device *dev, struct sk_buff *skb)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100728{
729 struct sfe_ipv4 *si = &__si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100730 unsigned int len;
731 unsigned int tot_len;
732 unsigned int frag_off;
733 unsigned int ihl;
734 bool flush_on_find;
735 bool ip_options;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530736 struct iphdr *iph;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700737 u32 protocol;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100738
739 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100740 * Check that we have space for an IP header here.
741 */
742 len = skb->len;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530743 if (unlikely(!pskb_may_pull(skb, sizeof(struct iphdr)))) {
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530744 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_HEADER_INCOMPLETE);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100745 DEBUG_TRACE("len: %u is too short\n", len);
746 return 0;
747 }
748
749 /*
750 * Check that our "total length" is large enough for an IP header.
751 */
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530752 iph = (struct iphdr *)skb->data;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100753 tot_len = ntohs(iph->tot_len);
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530754 if (unlikely(tot_len < sizeof(struct iphdr))) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100755
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530756 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_BAD_TOTAL_LENGTH);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100757 DEBUG_TRACE("tot_len: %u is too short\n", tot_len);
758 return 0;
759 }
760
761 /*
762 * Is our IP version wrong?
763 */
764 if (unlikely(iph->version != 4)) {
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530765 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_NON_V4);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100766 DEBUG_TRACE("IP version: %u\n", iph->version);
767 return 0;
768 }
769
770 /*
771 * Does our datagram fit inside the skb?
772 */
773 if (unlikely(tot_len > len)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100774
775 DEBUG_TRACE("tot_len: %u, exceeds len: %u\n", tot_len, len);
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530776 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_DATAGRAM_INCOMPLETE);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100777 return 0;
778 }
779
780 /*
781 * Do we have a non-initial fragment?
Nicolas Costaac2979c2014-01-14 10:35:24 -0600782 */
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100783 frag_off = ntohs(iph->frag_off);
784 if (unlikely(frag_off & IP_OFFSET)) {
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530785 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_NON_INITIAL_FRAGMENT);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100786 DEBUG_TRACE("non-initial fragment\n");
787 return 0;
788 }
789
790 /*
791 * If we have a (first) fragment then mark it to cause any connection to flush.
792 */
793 flush_on_find = unlikely(frag_off & IP_MF) ? true : false;
794
795 /*
796 * Do we have any IP options? That's definite a slow path! If we do have IP
797 * options we need to recheck our header size.
798 */
799 ihl = iph->ihl << 2;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530800 ip_options = unlikely(ihl != sizeof(struct iphdr)) ? true : false;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100801 if (unlikely(ip_options)) {
802 if (unlikely(len < ihl)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100803
804 DEBUG_TRACE("len: %u is too short for header of size: %u\n", len, ihl);
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530805 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_IP_OPTIONS_INCOMPLETE);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100806 return 0;
807 }
808
809 flush_on_find = true;
810 }
811
812 protocol = iph->protocol;
813 if (IPPROTO_UDP == protocol) {
814 return sfe_ipv4_recv_udp(si, skb, dev, len, iph, ihl, flush_on_find);
815 }
816
817 if (IPPROTO_TCP == protocol) {
818 return sfe_ipv4_recv_tcp(si, skb, dev, len, iph, ihl, flush_on_find);
819 }
820
821 if (IPPROTO_ICMP == protocol) {
822 return sfe_ipv4_recv_icmp(si, skb, dev, len, iph, ihl);
823 }
824
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530825 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_UNHANDLED_PROTOCOL);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100826
827 DEBUG_TRACE("not UDP, TCP or ICMP: %u\n", protocol);
828 return 0;
829}
830
Nicolas Costa436926b2014-01-14 10:36:22 -0600831static void
832sfe_ipv4_update_tcp_state(struct sfe_ipv4_connection *c,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530833 struct sfe_ipv4_rule_create_msg *msg)
Nicolas Costa436926b2014-01-14 10:36:22 -0600834{
835 struct sfe_ipv4_connection_match *orig_cm;
836 struct sfe_ipv4_connection_match *repl_cm;
837 struct sfe_ipv4_tcp_connection_match *orig_tcp;
838 struct sfe_ipv4_tcp_connection_match *repl_tcp;
839
840 orig_cm = c->original_match;
841 repl_cm = c->reply_match;
842 orig_tcp = &orig_cm->protocol_state.tcp;
843 repl_tcp = &repl_cm->protocol_state.tcp;
844
845 /* update orig */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530846 if (orig_tcp->max_win < msg->tcp_rule.flow_max_window) {
847 orig_tcp->max_win = msg->tcp_rule.flow_max_window;
Nicolas Costa436926b2014-01-14 10:36:22 -0600848 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530849 if ((s32)(orig_tcp->end - msg->tcp_rule.flow_end) < 0) {
850 orig_tcp->end = msg->tcp_rule.flow_end;
Nicolas Costa436926b2014-01-14 10:36:22 -0600851 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530852 if ((s32)(orig_tcp->max_end - msg->tcp_rule.flow_max_end) < 0) {
853 orig_tcp->max_end = msg->tcp_rule.flow_max_end;
Nicolas Costa436926b2014-01-14 10:36:22 -0600854 }
855
856 /* update reply */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530857 if (repl_tcp->max_win < msg->tcp_rule.return_max_window) {
858 repl_tcp->max_win = msg->tcp_rule.return_max_window;
Nicolas Costa436926b2014-01-14 10:36:22 -0600859 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530860 if ((s32)(repl_tcp->end - msg->tcp_rule.return_end) < 0) {
861 repl_tcp->end = msg->tcp_rule.return_end;
Nicolas Costa436926b2014-01-14 10:36:22 -0600862 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530863 if ((s32)(repl_tcp->max_end - msg->tcp_rule.return_max_end) < 0) {
864 repl_tcp->max_end = msg->tcp_rule.return_max_end;
Nicolas Costa436926b2014-01-14 10:36:22 -0600865 }
866
867 /* update match flags */
868 orig_cm->flags &= ~SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
869 repl_cm->flags &= ~SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530870 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_NO_SEQ_CHECK) {
871
Nicolas Costa436926b2014-01-14 10:36:22 -0600872 orig_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
873 repl_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
874 }
875}
876
877static void
878sfe_ipv4_update_protocol_state(struct sfe_ipv4_connection *c,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530879 struct sfe_ipv4_rule_create_msg *msg)
Nicolas Costa436926b2014-01-14 10:36:22 -0600880{
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530881 switch (msg->tuple.protocol) {
Nicolas Costa436926b2014-01-14 10:36:22 -0600882 case IPPROTO_TCP:
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530883 sfe_ipv4_update_tcp_state(c, msg);
Nicolas Costa436926b2014-01-14 10:36:22 -0600884 break;
885 }
886}
887
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530888void sfe_ipv4_update_rule(struct sfe_ipv4_rule_create_msg *msg)
Nicolas Costa436926b2014-01-14 10:36:22 -0600889{
890 struct sfe_ipv4_connection *c;
891 struct sfe_ipv4 *si = &__si;
892
893 spin_lock_bh(&si->lock);
894
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530895 c = sfe_ipv4_find_connection(si,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530896 msg->tuple.protocol,
897 msg->tuple.flow_ip,
898 msg->tuple.flow_ident,
899 msg->tuple.return_ip,
900 msg->tuple.return_ident);
Nicolas Costa436926b2014-01-14 10:36:22 -0600901 if (c != NULL) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530902 sfe_ipv4_update_protocol_state(c, msg);
Nicolas Costa436926b2014-01-14 10:36:22 -0600903 }
904
905 spin_unlock_bh(&si->lock);
906}
907
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100908/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100909 * sfe_ipv4_create_rule()
910 * Create a forwarding rule.
911 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530912int sfe_ipv4_create_rule(struct sfe_ipv4_rule_create_msg *msg)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100913{
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600914 struct sfe_ipv4 *si = &__si;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530915 struct sfe_ipv4_connection *c, *c_old;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100916 struct sfe_ipv4_connection_match *original_cm;
917 struct sfe_ipv4_connection_match *reply_cm;
Matthew McClintockdb5ac512014-01-16 17:01:40 -0600918 struct net_device *dest_dev;
919 struct net_device *src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530920 struct sfe_ipv4_5tuple *tuple = &msg->tuple;
Matthew McClintockdb5ac512014-01-16 17:01:40 -0600921
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530922 src_dev = dev_get_by_index(&init_net, msg->conn_rule.flow_top_interface_num);
923 if (!src_dev) {
924 DEBUG_WARN("%px: Unable to find src_dev corresponding to %d\n", msg,
925 msg->conn_rule.flow_top_interface_num);
926 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
927 return -EINVAL;
928 }
929
930 dest_dev = dev_get_by_index(&init_net, msg->conn_rule.return_top_interface_num);
931 if (!dest_dev) {
932 DEBUG_WARN("%px: Unable to find dest_dev corresponding to %d\n", msg,
933 msg->conn_rule.return_top_interface_num);
934 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
935 dev_put(src_dev);
936 return -EINVAL;
937 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100938
Matthew McClintock389b42a2014-09-24 14:05:51 -0500939 if (unlikely((dest_dev->reg_state != NETREG_REGISTERED) ||
940 (src_dev->reg_state != NETREG_REGISTERED))) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530941 dev_put(src_dev);
942 dev_put(dest_dev);
943 DEBUG_WARN("%px: src_dev=%s and dest_dev=%s are unregistered\n", msg,
944 src_dev->name, dest_dev->name);
945 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
Matthew McClintock389b42a2014-09-24 14:05:51 -0500946 return -EINVAL;
947 }
948
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530949 /*
950 * Allocate the various connection tracking objects.
951 */
952 c = (struct sfe_ipv4_connection *)kmalloc(sizeof(struct sfe_ipv4_connection), GFP_ATOMIC);
953 if (unlikely(!c)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530954 DEBUG_WARN("%px: memory allocation of connection entry failed\n", msg);
955 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
956 dev_put(src_dev);
957 dev_put(dest_dev);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530958 return -ENOMEM;
959 }
960
961 original_cm = (struct sfe_ipv4_connection_match *)kmalloc(sizeof(struct sfe_ipv4_connection_match), GFP_ATOMIC);
962 if (unlikely(!original_cm)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530963 DEBUG_WARN("%px: memory allocation of connection match entry failed\n", msg);
964 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530965 kfree(c);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530966 dev_put(src_dev);
967 dev_put(dest_dev);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530968 return -ENOMEM;
969 }
970
971 reply_cm = (struct sfe_ipv4_connection_match *)kmalloc(sizeof(struct sfe_ipv4_connection_match), GFP_ATOMIC);
972 if (unlikely(!reply_cm)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530973 DEBUG_WARN("%px: memory allocation of connection match entry failed\n", msg);
974 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530975 kfree(original_cm);
976 kfree(c);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530977 dev_put(src_dev);
978 dev_put(dest_dev);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530979 return -ENOMEM;
980 }
981
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530982 this_cpu_inc(si->stats_pcpu->connection_create_requests64);
983
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100984 spin_lock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100985
986 /*
Nicolas Costa436926b2014-01-14 10:36:22 -0600987 * Check to see if there is already a flow that matches the rule we're
988 * trying to create. If there is then we can't create a new one.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100989 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530990 c_old = sfe_ipv4_find_connection(si,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530991 msg->tuple.protocol,
992 msg->tuple.flow_ip,
993 msg->tuple.flow_ident,
994 msg->tuple.return_ip,
995 msg->tuple.return_ident);
996
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530997 if (c_old != NULL) {
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530998 this_cpu_inc(si->stats_pcpu->connection_create_collisions64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100999
1000 /*
Nicolas Costa436926b2014-01-14 10:36:22 -06001001 * If we already have the flow then it's likely that this
1002 * request to create the connection rule contains more
1003 * up-to-date information. Check and update accordingly.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001004 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301005 sfe_ipv4_update_protocol_state(c, msg);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001006 spin_unlock_bh(&si->lock);
1007
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301008 kfree(reply_cm);
1009 kfree(original_cm);
1010 kfree(c);
1011
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301012 dev_put(src_dev);
1013 dev_put(dest_dev);
1014
1015 DEBUG_TRACE("connection already exists - p:%d\n"
1016 " s: %s:%pM:%pI4:%u, d: %s:%pM:%pI4:%u\n",
1017 tuple->protocol,
1018 src_dev->name, msg->conn_rule.flow_mac, &tuple->flow_ip, ntohs(tuple->flow_ident),
1019 dest_dev->name, msg->conn_rule.return_mac, &tuple->return_ip, ntohs(tuple->return_ident));
1020
Nicolas Costa514fde02014-01-13 15:50:29 -06001021 return -EADDRINUSE;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001022 }
1023
1024 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001025 * Fill in the "original" direction connection matching object.
1026 * Note that the transmit MAC address is "dest_mac_xlate" because
1027 * we always know both ends of a connection by their translated
1028 * addresses and not their public addresses.
1029 */
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001030 original_cm->match_dev = src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301031 original_cm->match_protocol = tuple->protocol;
1032 original_cm->match_src_ip = tuple->flow_ip;
1033 original_cm->match_src_port = tuple->flow_ident;
1034 original_cm->match_dest_ip = tuple->return_ip;
1035 original_cm->match_dest_port = tuple->return_ident;
1036
1037 original_cm->xlate_src_ip = msg->conn_rule.flow_ip_xlate;
1038 original_cm->xlate_src_port = msg->conn_rule.flow_ident_xlate;
1039 original_cm->xlate_dest_ip = msg->conn_rule.return_ip_xlate;
1040 original_cm->xlate_dest_port =msg->conn_rule.return_ident_xlate;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301041 atomic_set(&original_cm->rx_packet_count, 0);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001042 original_cm->rx_packet_count64 = 0;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301043 atomic_set(&original_cm->rx_byte_count, 0);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001044 original_cm->rx_byte_count64 = 0;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301045
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001046 original_cm->xmit_dev = dest_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301047 original_cm->xmit_dev_mtu = msg->conn_rule.return_mtu;
1048
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001049 memcpy(original_cm->xmit_src_mac, dest_dev->dev_addr, ETH_ALEN);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301050 memcpy(original_cm->xmit_dest_mac, msg->conn_rule.return_mac, ETH_ALEN);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001051 original_cm->connection = c;
1052 original_cm->counter_match = reply_cm;
1053 original_cm->flags = 0;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301054
1055 if (msg->valid_flags & SFE_RULE_CREATE_QOS_VALID) {
1056 original_cm->priority = msg->qos_rule.flow_qos_tag;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001057 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PRIORITY_REMARK;
1058 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301059
1060 if (msg->valid_flags & SFE_RULE_CREATE_DSCP_MARKING_VALID) {
1061 original_cm->dscp = msg->dscp_rule.flow_dscp << SFE_IPV4_DSCP_SHIFT;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001062 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_DSCP_REMARK;
1063 }
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001064#ifdef CONFIG_NF_FLOW_COOKIE
1065 original_cm->flow_cookie = 0;
1066#endif
Zhi Chen8748eb32015-06-18 12:58:48 -07001067#ifdef CONFIG_XFRM
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301068 if (msg->valid_flags & SFE_RULE_CREATE_DIRECTION_VALID) {
1069 original_cm->flow_accel = msg->direction_rule.flow_accel;
1070 } else {
1071 original_cm->flow_accel = 1;
1072 }
1073
Zhi Chen8748eb32015-06-18 12:58:48 -07001074#endif
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001075
1076 /*
Ken Zhubbf49652021-09-12 15:33:09 -07001077 * For the non-arp interface, we don't write L2 HDR.
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001078 */
Ken Zhubbf49652021-09-12 15:33:09 -07001079 if (!(dest_dev->flags & IFF_NOARP)) {
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001080 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_L2_HDR;
1081
1082 /*
1083 * If our dev writes Ethernet headers then we can write a really fast
1084 * version.
1085 */
1086 if (dest_dev->header_ops) {
1087 if (dest_dev->header_ops->create == eth_header) {
1088 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR;
1089 }
1090 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001091 }
1092
1093 /*
1094 * Fill in the "reply" direction connection matching object.
1095 */
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001096 reply_cm->match_dev = dest_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301097 reply_cm->match_protocol = tuple->protocol;
1098 reply_cm->match_src_ip = msg->conn_rule.return_ip_xlate;
1099 reply_cm->match_src_port = msg->conn_rule.return_ident_xlate;
1100 reply_cm->match_dest_ip = msg->conn_rule.flow_ip_xlate;
1101 reply_cm->match_dest_port = msg->conn_rule.flow_ident_xlate;
1102
1103 reply_cm->xlate_src_ip = tuple->return_ip;
1104 reply_cm->xlate_src_port = tuple->return_ident;
1105 reply_cm->xlate_dest_ip = tuple->flow_ip;
1106 reply_cm->xlate_dest_port = tuple->flow_ident;;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301107
1108 atomic_set(&reply_cm->rx_packet_count, 0);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001109 reply_cm->rx_packet_count64 = 0;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301110 atomic_set(&reply_cm->rx_byte_count, 0);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001111 reply_cm->rx_byte_count64 = 0;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301112
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001113 reply_cm->xmit_dev = src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301114 reply_cm->xmit_dev_mtu = msg->conn_rule.flow_mtu;
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001115 memcpy(reply_cm->xmit_src_mac, src_dev->dev_addr, ETH_ALEN);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301116 memcpy(reply_cm->xmit_dest_mac, msg->conn_rule.flow_mac, ETH_ALEN);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001117 reply_cm->connection = c;
1118 reply_cm->counter_match = original_cm;
1119 reply_cm->flags = 0;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301120 if (msg->valid_flags & SFE_RULE_CREATE_QOS_VALID) {
1121 reply_cm->priority = msg->qos_rule.return_qos_tag;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001122 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PRIORITY_REMARK;
1123 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301124 if (msg->valid_flags & SFE_RULE_CREATE_DSCP_MARKING_VALID) {
1125 reply_cm->dscp = msg->dscp_rule.return_dscp << SFE_IPV4_DSCP_SHIFT;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001126 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_DSCP_REMARK;
1127 }
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001128#ifdef CONFIG_NF_FLOW_COOKIE
1129 reply_cm->flow_cookie = 0;
1130#endif
Zhi Chen8748eb32015-06-18 12:58:48 -07001131#ifdef CONFIG_XFRM
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301132 if (msg->valid_flags & SFE_RULE_CREATE_DIRECTION_VALID) {
1133 reply_cm->flow_accel = msg->direction_rule.return_accel;
1134 } else {
1135 reply_cm->flow_accel = 1;
1136 }
1137
Zhi Chen8748eb32015-06-18 12:58:48 -07001138#endif
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001139
1140 /*
Ken Zhubbf49652021-09-12 15:33:09 -07001141 * For the non-arp interface, we don't write L2 HDR.
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001142 */
Ken Zhubbf49652021-09-12 15:33:09 -07001143 if (!(src_dev->flags & IFF_NOARP)) {
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001144 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_L2_HDR;
1145
1146 /*
1147 * If our dev writes Ethernet headers then we can write a really fast
1148 * version.
1149 */
1150 if (src_dev->header_ops) {
1151 if (src_dev->header_ops->create == eth_header) {
1152 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR;
1153 }
1154 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001155 }
1156
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301157 if ((tuple->return_ip != msg->conn_rule.return_ip_xlate) ||
1158 (tuple->return_ident != msg->conn_rule.return_ident_xlate)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001159 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST;
1160 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC;
1161 }
1162
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301163 if ((tuple->flow_ip != msg->conn_rule.flow_ip_xlate) ||
1164 (tuple->flow_ident != msg->conn_rule.flow_ident_xlate)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001165 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC;
1166 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST;
1167 }
1168
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301169 c->protocol = tuple->protocol;
1170 c->src_ip = tuple->flow_ip;
1171 c->src_ip_xlate = msg->conn_rule.flow_ip_xlate;
1172 c->src_port = tuple->flow_ident;
1173 c->src_port_xlate = msg->conn_rule.flow_ident_xlate;
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001174 c->original_dev = src_dev;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001175 c->original_match = original_cm;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301176 c->dest_ip = tuple->return_ip;
1177 c->dest_ip_xlate = msg->conn_rule.return_ip_xlate;
1178 c->dest_port = tuple->return_ident;
1179 c->dest_port_xlate = msg->conn_rule.return_ident_xlate;
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001180 c->reply_dev = dest_dev;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001181 c->reply_match = reply_cm;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301182 c->mark = 0; /* TODO : no mark setting for create rule */
Xiaoping Fan34586472015-07-03 02:20:35 -07001183 c->debug_read_seq = 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001184 c->last_sync_jiffies = get_jiffies_64();
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301185 c->removed = false;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001186
1187 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001188 * Initialize the protocol-specific information that we track.
1189 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301190 switch (tuple->protocol) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001191 case IPPROTO_TCP:
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301192 original_cm->protocol_state.tcp.win_scale = msg->tcp_rule.flow_window_scale;
1193 original_cm->protocol_state.tcp.max_win = msg->tcp_rule.flow_max_window ? msg->tcp_rule.flow_max_window : 1;
1194 original_cm->protocol_state.tcp.end = msg->tcp_rule.flow_end;
1195 original_cm->protocol_state.tcp.max_end = msg->tcp_rule.flow_max_end;
1196
1197 reply_cm->protocol_state.tcp.win_scale = msg->tcp_rule.return_window_scale;
1198 reply_cm->protocol_state.tcp.max_win = msg->tcp_rule.return_max_window ? msg->tcp_rule.return_max_window : 1;
1199 reply_cm->protocol_state.tcp.end = msg->tcp_rule.return_end;
1200 reply_cm->protocol_state.tcp.max_end = msg->tcp_rule.return_max_end;
1201
1202 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_NO_SEQ_CHECK) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001203 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
1204 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
1205 }
1206 break;
1207 }
1208
1209 sfe_ipv4_connection_match_compute_translations(original_cm);
1210 sfe_ipv4_connection_match_compute_translations(reply_cm);
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301211 sfe_ipv4_insert_connection(si, c);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001212
1213 spin_unlock_bh(&si->lock);
1214
1215 /*
1216 * We have everything we need!
1217 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301218 DEBUG_INFO("new connection - p: %d\n"
Tian Yang45f39c82020-10-06 14:07:47 -07001219 " s: %s:%pxM(%pxM):%pI4(%pI4):%u(%u)\n"
1220 " d: %s:%pxM(%pxM):%pI4(%pI4):%u(%u)\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301221 tuple->protocol,
1222 src_dev->name, msg->conn_rule.flow_mac, NULL,
1223 &tuple->flow_ip, &msg->conn_rule.flow_ip_xlate, ntohs(tuple->flow_ident), ntohs(msg->conn_rule.flow_ident_xlate),
1224 dest_dev->name, NULL, msg->conn_rule.return_mac,
1225 &tuple->return_ip, &msg->conn_rule.return_ip_xlate, ntohs(tuple->return_ident), ntohs(msg->conn_rule.return_ident_xlate));
Nicolas Costa514fde02014-01-13 15:50:29 -06001226
1227 return 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001228}
1229
1230/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001231 * sfe_ipv4_destroy_rule()
1232 * Destroy a forwarding rule.
1233 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301234void sfe_ipv4_destroy_rule(struct sfe_ipv4_rule_destroy_msg *msg)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001235{
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001236 struct sfe_ipv4 *si = &__si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001237 struct sfe_ipv4_connection *c;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301238 bool ret;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301239 struct sfe_ipv4_5tuple *tuple = &msg->tuple;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001240
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301241 this_cpu_inc(si->stats_pcpu->connection_destroy_requests64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001242 spin_lock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001243
1244 /*
1245 * Check to see if we have a flow that matches the rule we're trying
1246 * to destroy. If there isn't then we can't destroy it.
1247 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301248 c = sfe_ipv4_find_connection(si, tuple->protocol, tuple->flow_ip, tuple->flow_ident,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301249 tuple->return_ip, tuple->return_ident);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001250 if (!c) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001251 spin_unlock_bh(&si->lock);
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301252 this_cpu_inc(si->stats_pcpu->connection_destroy_misses64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001253
1254 DEBUG_TRACE("connection does not exist - p: %d, s: %pI4:%u, d: %pI4:%u\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301255 tuple->protocol, &tuple->flow_ip, ntohs(tuple->flow_ident),
1256 &tuple->return_ip, ntohs(tuple->return_ident));
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001257 return;
1258 }
1259
1260 /*
1261 * Remove our connection details from the hash tables.
1262 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301263 ret = sfe_ipv4_remove_connection(si, c);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001264 spin_unlock_bh(&si->lock);
1265
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301266 if (ret) {
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301267 sfe_ipv4_flush_connection(si, c, SFE_SYNC_REASON_DESTROY);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301268 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001269
1270 DEBUG_INFO("connection destroyed - p: %d, s: %pI4:%u, d: %pI4:%u\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301271 tuple->protocol, &tuple->flow_ip, ntohs(tuple->flow_ident),
1272 &tuple->return_ip, ntohs(tuple->return_ident));
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001273}
1274
1275/*
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001276 * sfe_ipv4_register_sync_rule_callback()
1277 * Register a callback for rule synchronization.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001278 */
Xiaoping Fand44a5b42015-05-26 17:37:37 -07001279void sfe_ipv4_register_sync_rule_callback(sfe_sync_rule_callback_t sync_rule_callback)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001280{
1281 struct sfe_ipv4 *si = &__si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001282
1283 spin_lock_bh(&si->lock);
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001284 rcu_assign_pointer(si->sync_rule_callback, sync_rule_callback);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001285 spin_unlock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001286}
1287
1288/*
1289 * sfe_ipv4_get_debug_dev()
1290 */
1291static ssize_t sfe_ipv4_get_debug_dev(struct device *dev,
1292 struct device_attribute *attr,
1293 char *buf)
1294{
1295 struct sfe_ipv4 *si = &__si;
1296 ssize_t count;
1297 int num;
1298
1299 spin_lock_bh(&si->lock);
1300 num = si->debug_dev;
1301 spin_unlock_bh(&si->lock);
1302
1303 count = snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", num);
1304 return count;
1305}
1306
1307/*
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001308 * sysfs attributes.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001309 */
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001310static const struct device_attribute sfe_ipv4_debug_dev_attr =
Xiaoping Fane70da412016-02-26 16:47:57 -08001311 __ATTR(debug_dev, S_IWUSR | S_IRUGO, sfe_ipv4_get_debug_dev, NULL);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001312
1313/*
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001314 * sfe_ipv4_destroy_all_rules_for_dev()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001315 * Destroy all connections that match a particular device.
1316 *
1317 * If we pass dev as NULL then this destroys all connections.
1318 */
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001319void sfe_ipv4_destroy_all_rules_for_dev(struct net_device *dev)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001320{
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001321 struct sfe_ipv4 *si = &__si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001322 struct sfe_ipv4_connection *c;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301323 bool ret;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001324
Xiaoping Fan34586472015-07-03 02:20:35 -07001325another_round:
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001326 spin_lock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001327
Xiaoping Fan34586472015-07-03 02:20:35 -07001328 for (c = si->all_connections_head; c; c = c->all_connections_next) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001329 /*
Xiaoping Fan34586472015-07-03 02:20:35 -07001330 * Does this connection relate to the device we are destroying?
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001331 */
1332 if (!dev
1333 || (dev == c->original_dev)
1334 || (dev == c->reply_dev)) {
Xiaoping Fan34586472015-07-03 02:20:35 -07001335 break;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001336 }
Xiaoping Fan34586472015-07-03 02:20:35 -07001337 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001338
Xiaoping Fan34586472015-07-03 02:20:35 -07001339 if (c) {
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301340 ret = sfe_ipv4_remove_connection(si, c);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001341 }
1342
1343 spin_unlock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07001344
1345 if (c) {
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301346 if (ret) {
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301347 sfe_ipv4_flush_connection(si, c, SFE_SYNC_REASON_DESTROY);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301348 }
Xiaoping Fan34586472015-07-03 02:20:35 -07001349 goto another_round;
1350 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001351}
1352
1353/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001354 * sfe_ipv4_periodic_sync()
1355 */
Ken Zhu137722d2021-09-23 17:57:36 -07001356static void sfe_ipv4_periodic_sync(struct work_struct *work)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001357{
Ken Zhu137722d2021-09-23 17:57:36 -07001358 struct sfe_ipv4 *si = container_of((struct delayed_work *)work, struct sfe_ipv4, sync_dwork);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07001359 u64 now_jiffies;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001360 int quota;
Xiaoping Fand44a5b42015-05-26 17:37:37 -07001361 sfe_sync_rule_callback_t sync_rule_callback;
Ken Zhudc423672021-09-02 18:27:01 -07001362 struct sfe_ipv4_connection *c;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001363
1364 now_jiffies = get_jiffies_64();
1365
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001366 rcu_read_lock();
1367 sync_rule_callback = rcu_dereference(si->sync_rule_callback);
1368 if (!sync_rule_callback) {
1369 rcu_read_unlock();
1370 goto done;
1371 }
1372
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001373 spin_lock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001374
1375 /*
Ken Zhudc423672021-09-02 18:27:01 -07001376 * If we have reached the end of the connection list, walk from
1377 * the connection head.
1378 */
1379 c = si->wc_next;
1380 if (unlikely(!c)) {
1381 c = si->all_connections_head;
1382 }
1383
1384 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001385 * Get an estimate of the number of connections to parse in this sync.
1386 */
1387 quota = (si->num_connections + 63) / 64;
1388
1389 /*
Ken Zhudc423672021-09-02 18:27:01 -07001390 * Walk the "all connection" list and sync the connection state.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001391 */
Ken Zhudc423672021-09-02 18:27:01 -07001392 while (likely(c && quota)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001393 struct sfe_ipv4_connection_match *cm;
1394 struct sfe_ipv4_connection_match *counter_cm;
Xiaoping Fand44a5b42015-05-26 17:37:37 -07001395 struct sfe_connection_sync sis;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001396
Ken Zhudc423672021-09-02 18:27:01 -07001397 cm = c->original_match;
1398 counter_cm = c->reply_match;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001399
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001400 /*
Ken Zhudc423672021-09-02 18:27:01 -07001401 * Didn't receive packets in the original direction or reply
1402 * direction, move to the next connection.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001403 */
Ken Zhudc423672021-09-02 18:27:01 -07001404 if ((!atomic_read(&cm->rx_packet_count)) && !(atomic_read(&counter_cm->rx_packet_count))) {
1405 c = c->all_connections_next;
1406 continue;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001407 }
1408
Ken Zhudc423672021-09-02 18:27:01 -07001409 quota--;
Matthew McClintockaf48f1e2014-01-23 15:29:19 -06001410
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301411 sfe_ipv4_gen_sync_connection(si, c, &sis, SFE_SYNC_REASON_STATS, now_jiffies);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001412
Ken Zhudc423672021-09-02 18:27:01 -07001413 si->wc_next = c->all_connections_next;
1414
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001415 /*
1416 * We don't want to be holding the lock when we sync!
1417 */
1418 spin_unlock_bh(&si->lock);
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001419 sync_rule_callback(&sis);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001420 spin_lock_bh(&si->lock);
Ken Zhudc423672021-09-02 18:27:01 -07001421
1422 /*
1423 * c must be set and used in the same lock/unlock window;
1424 * because c could be removed when we don't hold the lock,
1425 * so delay grabbing until after the callback and relock.
1426 */
1427 c = si->wc_next;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001428 }
1429
Ken Zhudc423672021-09-02 18:27:01 -07001430 /*
1431 * At the end of the sync, put the wc_next to the connection we left.
1432 */
1433 si->wc_next = c;
1434
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001435 spin_unlock_bh(&si->lock);
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001436 rcu_read_unlock();
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001437
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001438done:
Ken Zhu137722d2021-09-23 17:57:36 -07001439 schedule_delayed_work_on(si->work_cpu, (struct delayed_work *)work, ((HZ + 99) / 100));
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001440}
1441
1442#define CHAR_DEV_MSG_SIZE 768
1443
1444/*
1445 * sfe_ipv4_debug_dev_read_start()
1446 * Generate part of the XML output.
1447 */
1448static bool sfe_ipv4_debug_dev_read_start(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
1449 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
1450{
1451 int bytes_read;
1452
Xiaoping Fan34586472015-07-03 02:20:35 -07001453 si->debug_read_seq++;
1454
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001455 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "<sfe_ipv4>\n");
1456 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1457 return false;
1458 }
1459
1460 *length -= bytes_read;
1461 *total_read += bytes_read;
1462
1463 ws->state++;
1464 return true;
1465}
1466
1467/*
1468 * sfe_ipv4_debug_dev_read_connections_start()
1469 * Generate part of the XML output.
1470 */
1471static bool sfe_ipv4_debug_dev_read_connections_start(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
1472 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
1473{
1474 int bytes_read;
1475
1476 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<connections>\n");
1477 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1478 return false;
1479 }
1480
1481 *length -= bytes_read;
1482 *total_read += bytes_read;
1483
1484 ws->state++;
1485 return true;
1486}
1487
1488/*
1489 * sfe_ipv4_debug_dev_read_connections_connection()
1490 * Generate part of the XML output.
1491 */
1492static bool sfe_ipv4_debug_dev_read_connections_connection(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
1493 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
1494{
1495 struct sfe_ipv4_connection *c;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001496 struct sfe_ipv4_connection_match *original_cm;
1497 struct sfe_ipv4_connection_match *reply_cm;
1498 int bytes_read;
1499 int protocol;
1500 struct net_device *src_dev;
Dave Hudson87973cd2013-10-22 16:00:04 +01001501 __be32 src_ip;
1502 __be32 src_ip_xlate;
1503 __be16 src_port;
1504 __be16 src_port_xlate;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07001505 u64 src_rx_packets;
1506 u64 src_rx_bytes;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001507 struct net_device *dest_dev;
Dave Hudson87973cd2013-10-22 16:00:04 +01001508 __be32 dest_ip;
1509 __be32 dest_ip_xlate;
1510 __be16 dest_port;
1511 __be16 dest_port_xlate;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07001512 u64 dest_rx_packets;
1513 u64 dest_rx_bytes;
1514 u64 last_sync_jiffies;
1515 u32 mark, src_priority, dest_priority, src_dscp, dest_dscp;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301516 u32 packet, byte;
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001517#ifdef CONFIG_NF_FLOW_COOKIE
1518 int src_flow_cookie, dst_flow_cookie;
1519#endif
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001520
1521 spin_lock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07001522
1523 for (c = si->all_connections_head; c; c = c->all_connections_next) {
1524 if (c->debug_read_seq < si->debug_read_seq) {
1525 c->debug_read_seq = si->debug_read_seq;
1526 break;
1527 }
1528 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001529
1530 /*
Xiaoping Fan34586472015-07-03 02:20:35 -07001531 * If there were no connections then move to the next state.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001532 */
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301533 if (!c || c->removed) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001534 spin_unlock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07001535 ws->state++;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001536 return true;
1537 }
1538
1539 original_cm = c->original_match;
1540 reply_cm = c->reply_match;
1541
1542 protocol = c->protocol;
1543 src_dev = c->original_dev;
1544 src_ip = c->src_ip;
1545 src_ip_xlate = c->src_ip_xlate;
1546 src_port = c->src_port;
1547 src_port_xlate = c->src_port_xlate;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001548 src_priority = original_cm->priority;
1549 src_dscp = original_cm->dscp >> SFE_IPV4_DSCP_SHIFT;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001550
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301551 sfe_ipv4_connection_match_update_summary_stats(original_cm, &packet, &byte);
1552 sfe_ipv4_connection_match_update_summary_stats(reply_cm, &packet, &byte);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001553
1554 src_rx_packets = original_cm->rx_packet_count64;
1555 src_rx_bytes = original_cm->rx_byte_count64;
1556 dest_dev = c->reply_dev;
1557 dest_ip = c->dest_ip;
1558 dest_ip_xlate = c->dest_ip_xlate;
1559 dest_port = c->dest_port;
1560 dest_port_xlate = c->dest_port_xlate;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001561 dest_priority = reply_cm->priority;
1562 dest_dscp = reply_cm->dscp >> SFE_IPV4_DSCP_SHIFT;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001563 dest_rx_packets = reply_cm->rx_packet_count64;
1564 dest_rx_bytes = reply_cm->rx_byte_count64;
1565 last_sync_jiffies = get_jiffies_64() - c->last_sync_jiffies;
Cristian Prundeanu592265e2013-12-26 11:01:22 -06001566 mark = c->mark;
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001567#ifdef CONFIG_NF_FLOW_COOKIE
1568 src_flow_cookie = original_cm->flow_cookie;
1569 dst_flow_cookie = reply_cm->flow_cookie;
1570#endif
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001571 spin_unlock_bh(&si->lock);
1572
1573 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t\t<connection "
1574 "protocol=\"%u\" "
1575 "src_dev=\"%s\" "
1576 "src_ip=\"%pI4\" src_ip_xlate=\"%pI4\" "
1577 "src_port=\"%u\" src_port_xlate=\"%u\" "
Xiaoping Fane1963d42015-08-25 17:06:19 -07001578 "src_priority=\"%u\" src_dscp=\"%u\" "
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001579 "src_rx_pkts=\"%llu\" src_rx_bytes=\"%llu\" "
1580 "dest_dev=\"%s\" "
1581 "dest_ip=\"%pI4\" dest_ip_xlate=\"%pI4\" "
1582 "dest_port=\"%u\" dest_port_xlate=\"%u\" "
Xiaoping Fane1963d42015-08-25 17:06:19 -07001583 "dest_priority=\"%u\" dest_dscp=\"%u\" "
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001584 "dest_rx_pkts=\"%llu\" dest_rx_bytes=\"%llu\" "
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001585#ifdef CONFIG_NF_FLOW_COOKIE
1586 "src_flow_cookie=\"%d\" dst_flow_cookie=\"%d\" "
1587#endif
Cristian Prundeanu592265e2013-12-26 11:01:22 -06001588 "last_sync=\"%llu\" "
Nicolas Costabb85a2e2014-01-13 16:26:33 -06001589 "mark=\"%08x\" />\n",
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001590 protocol,
1591 src_dev->name,
1592 &src_ip, &src_ip_xlate,
Dave Hudson87973cd2013-10-22 16:00:04 +01001593 ntohs(src_port), ntohs(src_port_xlate),
Xiaoping Fane1963d42015-08-25 17:06:19 -07001594 src_priority, src_dscp,
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001595 src_rx_packets, src_rx_bytes,
1596 dest_dev->name,
1597 &dest_ip, &dest_ip_xlate,
Dave Hudson87973cd2013-10-22 16:00:04 +01001598 ntohs(dest_port), ntohs(dest_port_xlate),
Xiaoping Fane1963d42015-08-25 17:06:19 -07001599 dest_priority, dest_dscp,
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001600 dest_rx_packets, dest_rx_bytes,
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001601#ifdef CONFIG_NF_FLOW_COOKIE
1602 src_flow_cookie, dst_flow_cookie,
1603#endif
Cristian Prundeanu592265e2013-12-26 11:01:22 -06001604 last_sync_jiffies, mark);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001605
1606 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1607 return false;
1608 }
1609
1610 *length -= bytes_read;
1611 *total_read += bytes_read;
1612
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001613 return true;
1614}
1615
1616/*
1617 * sfe_ipv4_debug_dev_read_connections_end()
1618 * Generate part of the XML output.
1619 */
1620static bool sfe_ipv4_debug_dev_read_connections_end(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
1621 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
1622{
1623 int bytes_read;
1624
1625 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t</connections>\n");
1626 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1627 return false;
1628 }
1629
1630 *length -= bytes_read;
1631 *total_read += bytes_read;
1632
1633 ws->state++;
1634 return true;
1635}
1636
1637/*
1638 * sfe_ipv4_debug_dev_read_exceptions_start()
1639 * Generate part of the XML output.
1640 */
1641static bool sfe_ipv4_debug_dev_read_exceptions_start(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
1642 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
1643{
1644 int bytes_read;
1645
1646 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<exceptions>\n");
1647 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1648 return false;
1649 }
1650
1651 *length -= bytes_read;
1652 *total_read += bytes_read;
1653
1654 ws->state++;
1655 return true;
1656}
1657
1658/*
1659 * sfe_ipv4_debug_dev_read_exceptions_exception()
1660 * Generate part of the XML output.
1661 */
1662static bool sfe_ipv4_debug_dev_read_exceptions_exception(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
1663 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
1664{
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301665 int i;
1666 u64 val = 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001667
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301668 for_each_possible_cpu(i) {
1669 const struct sfe_ipv4_stats *s = per_cpu_ptr(si->stats_pcpu, i);
1670 val += s->exception_events64[ws->iter_exception];
1671 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001672
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301673 if (val) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001674 int bytes_read;
1675
1676 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE,
1677 "\t\t<exception name=\"%s\" count=\"%llu\" />\n",
1678 sfe_ipv4_exception_events_string[ws->iter_exception],
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301679 val);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001680 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1681 return false;
1682 }
1683
1684 *length -= bytes_read;
1685 *total_read += bytes_read;
1686 }
1687
1688 ws->iter_exception++;
1689 if (ws->iter_exception >= SFE_IPV4_EXCEPTION_EVENT_LAST) {
1690 ws->iter_exception = 0;
1691 ws->state++;
1692 }
1693
1694 return true;
1695}
1696
1697/*
1698 * sfe_ipv4_debug_dev_read_exceptions_end()
1699 * Generate part of the XML output.
1700 */
1701static bool sfe_ipv4_debug_dev_read_exceptions_end(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
1702 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
1703{
1704 int bytes_read;
1705
1706 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t</exceptions>\n");
1707 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1708 return false;
1709 }
1710
1711 *length -= bytes_read;
1712 *total_read += bytes_read;
1713
1714 ws->state++;
1715 return true;
1716}
1717
1718/*
1719 * sfe_ipv4_debug_dev_read_stats()
1720 * Generate part of the XML output.
1721 */
1722static bool sfe_ipv4_debug_dev_read_stats(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
1723 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
1724{
1725 int bytes_read;
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301726 struct sfe_ipv4_stats stats;
1727 unsigned int num_conn;
1728
1729 sfe_ipv4_update_summary_stats(si, &stats);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001730
1731 spin_lock_bh(&si->lock);
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301732 num_conn = si->num_connections;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001733 spin_unlock_bh(&si->lock);
1734
1735 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<stats "
1736 "num_connections=\"%u\" "
Xiaoping Fan59176422015-05-22 15:58:10 -07001737 "pkts_forwarded=\"%llu\" pkts_not_forwarded=\"%llu\" "
1738 "create_requests=\"%llu\" create_collisions=\"%llu\" "
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301739 "create_failures=\"%llu\" "
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001740 "destroy_requests=\"%llu\" destroy_misses=\"%llu\" "
1741 "flushes=\"%llu\" "
1742 "hash_hits=\"%llu\" hash_reorders=\"%llu\" />\n",
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301743 num_conn,
1744 stats.packets_forwarded64,
1745 stats.packets_not_forwarded64,
1746 stats.connection_create_requests64,
1747 stats.connection_create_collisions64,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301748 stats.connection_create_failures64,
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301749 stats.connection_destroy_requests64,
1750 stats.connection_destroy_misses64,
1751 stats.connection_flushes64,
1752 stats.connection_match_hash_hits64,
1753 stats.connection_match_hash_reorders64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001754 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1755 return false;
1756 }
1757
1758 *length -= bytes_read;
1759 *total_read += bytes_read;
1760
1761 ws->state++;
1762 return true;
1763}
1764
1765/*
1766 * sfe_ipv4_debug_dev_read_end()
1767 * Generate part of the XML output.
1768 */
1769static bool sfe_ipv4_debug_dev_read_end(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
1770 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
1771{
1772 int bytes_read;
1773
1774 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "</sfe_ipv4>\n");
1775 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1776 return false;
1777 }
1778
1779 *length -= bytes_read;
1780 *total_read += bytes_read;
1781
1782 ws->state++;
1783 return true;
1784}
1785
1786/*
1787 * Array of write functions that write various XML elements that correspond to
1788 * our XML output state machine.
1789 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07001790static 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 +01001791 sfe_ipv4_debug_dev_read_start,
1792 sfe_ipv4_debug_dev_read_connections_start,
1793 sfe_ipv4_debug_dev_read_connections_connection,
1794 sfe_ipv4_debug_dev_read_connections_end,
1795 sfe_ipv4_debug_dev_read_exceptions_start,
1796 sfe_ipv4_debug_dev_read_exceptions_exception,
1797 sfe_ipv4_debug_dev_read_exceptions_end,
1798 sfe_ipv4_debug_dev_read_stats,
1799 sfe_ipv4_debug_dev_read_end,
1800};
1801
1802/*
1803 * sfe_ipv4_debug_dev_read()
1804 * Send info to userspace upon read request from user
1805 */
1806static ssize_t sfe_ipv4_debug_dev_read(struct file *filp, char *buffer, size_t length, loff_t *offset)
1807{
1808 char msg[CHAR_DEV_MSG_SIZE];
1809 int total_read = 0;
1810 struct sfe_ipv4_debug_xml_write_state *ws;
1811 struct sfe_ipv4 *si = &__si;
1812
1813 ws = (struct sfe_ipv4_debug_xml_write_state *)filp->private_data;
1814 while ((ws->state != SFE_IPV4_DEBUG_XML_STATE_DONE) && (length > CHAR_DEV_MSG_SIZE)) {
1815 if ((sfe_ipv4_debug_xml_write_methods[ws->state])(si, buffer, msg, &length, &total_read, ws)) {
1816 continue;
1817 }
1818 }
1819
1820 return total_read;
1821}
1822
1823/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001824 * sfe_ipv4_debug_dev_open()
1825 */
1826static int sfe_ipv4_debug_dev_open(struct inode *inode, struct file *file)
1827{
1828 struct sfe_ipv4_debug_xml_write_state *ws;
1829
1830 ws = (struct sfe_ipv4_debug_xml_write_state *)file->private_data;
1831 if (!ws) {
1832 ws = kzalloc(sizeof(struct sfe_ipv4_debug_xml_write_state), GFP_KERNEL);
1833 if (!ws) {
1834 return -ENOMEM;
1835 }
1836
1837 ws->state = SFE_IPV4_DEBUG_XML_STATE_START;
1838 file->private_data = ws;
1839 }
1840
1841 return 0;
1842}
1843
1844/*
1845 * sfe_ipv4_debug_dev_release()
1846 */
1847static int sfe_ipv4_debug_dev_release(struct inode *inode, struct file *file)
1848{
1849 struct sfe_ipv4_debug_xml_write_state *ws;
1850
1851 ws = (struct sfe_ipv4_debug_xml_write_state *)file->private_data;
1852 if (ws) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001853 /*
1854 * We've finished with our output so free the write state.
1855 */
1856 kfree(ws);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301857 file->private_data = NULL;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001858 }
1859
1860 return 0;
1861}
1862
1863/*
1864 * File operations used in the debug char device
1865 */
1866static struct file_operations sfe_ipv4_debug_dev_fops = {
1867 .read = sfe_ipv4_debug_dev_read,
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001868 .open = sfe_ipv4_debug_dev_open,
1869 .release = sfe_ipv4_debug_dev_release
1870};
1871
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001872#ifdef CONFIG_NF_FLOW_COOKIE
1873/*
1874 * sfe_register_flow_cookie_cb
1875 * register a function in SFE to let SFE use this function to configure flow cookie for a flow
1876 *
1877 * Hardware driver which support flow cookie should register a callback function in SFE. Then SFE
1878 * can use this function to configure flow cookie for a flow.
1879 * return: 0, success; !=0, fail
1880 */
1881int sfe_register_flow_cookie_cb(flow_cookie_set_func_t cb)
1882{
1883 struct sfe_ipv4 *si = &__si;
1884
1885 BUG_ON(!cb);
1886
1887 if (si->flow_cookie_set_func) {
1888 return -1;
1889 }
1890
1891 rcu_assign_pointer(si->flow_cookie_set_func, cb);
1892 return 0;
1893}
1894
1895/*
1896 * sfe_unregister_flow_cookie_cb
1897 * unregister function which is used to configure flow cookie for a flow
1898 *
1899 * return: 0, success; !=0, fail
1900 */
1901int sfe_unregister_flow_cookie_cb(flow_cookie_set_func_t cb)
1902{
1903 struct sfe_ipv4 *si = &__si;
1904
1905 RCU_INIT_POINTER(si->flow_cookie_set_func, NULL);
1906 return 0;
1907}
Xiaoping Fan640faf42015-08-28 15:50:55 -07001908
1909/*
1910 * sfe_ipv4_get_flow_cookie()
1911 */
1912static ssize_t sfe_ipv4_get_flow_cookie(struct device *dev,
1913 struct device_attribute *attr,
1914 char *buf)
1915{
1916 struct sfe_ipv4 *si = &__si;
Xiaoping Fan01c67cc2015-11-09 11:31:57 -08001917 return snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", si->flow_cookie_enable);
Xiaoping Fan640faf42015-08-28 15:50:55 -07001918}
1919
1920/*
1921 * sfe_ipv4_set_flow_cookie()
1922 */
1923static ssize_t sfe_ipv4_set_flow_cookie(struct device *dev,
1924 struct device_attribute *attr,
1925 const char *buf, size_t size)
1926{
1927 struct sfe_ipv4 *si = &__si;
Ken Zhu137722d2021-09-23 17:57:36 -07001928 si->flow_cookie_enable = simple_strtol(buf, NULL, 0);
Xiaoping Fan640faf42015-08-28 15:50:55 -07001929
1930 return size;
1931}
1932
1933/*
1934 * sysfs attributes.
1935 */
1936static const struct device_attribute sfe_ipv4_flow_cookie_attr =
Xiaoping Fane70da412016-02-26 16:47:57 -08001937 __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 -08001938#endif /*CONFIG_NF_FLOW_COOKIE*/
1939
Ken Zhu137722d2021-09-23 17:57:36 -07001940/*
1941 * sfe_ipv4_get_cpu()
1942 */
1943static ssize_t sfe_ipv4_get_cpu(struct device *dev,
1944 struct device_attribute *attr,
1945 char *buf)
1946{
1947 struct sfe_ipv4 *si = &__si;
1948 return snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", si->work_cpu);
1949}
1950
1951/*
1952 * sfe_ipv4_set_cpu()
1953 */
1954static ssize_t sfe_ipv4_set_cpu(struct device *dev,
1955 struct device_attribute *attr,
1956 const char *buf, size_t size)
1957{
1958 struct sfe_ipv4 *si = &__si;
1959 int work_cpu;
1960 work_cpu = simple_strtol(buf, NULL, 0);
1961 if ((work_cpu >= 0) && (work_cpu <= NR_CPUS)) {
1962 si->work_cpu = work_cpu;
1963 } else {
1964 dev_err(dev, "%s is not in valid range[0,%d]", buf, NR_CPUS);
1965 }
1966 return size;
1967}
1968/*
1969 * sysfs attributes.
1970 */
1971static const struct device_attribute sfe_ipv4_cpu_attr =
1972 __ATTR(stats_work_cpu, S_IWUSR | S_IRUGO, sfe_ipv4_get_cpu, sfe_ipv4_set_cpu);
1973
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301974 /*
1975 * sfe_ipv4_conn_match_hash_init()
1976 * Initialize conn match hash lists
1977 */
1978static void sfe_ipv4_conn_match_hash_init(struct sfe_ipv4 *si, int len)
1979{
1980 struct hlist_head *hash_list = si->hlist_conn_match_hash_head;
1981 int i;
1982
1983 for (i = 0; i < len; i++) {
1984 INIT_HLIST_HEAD(&hash_list[i]);
1985 }
1986}
1987
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001988/*
Dave Hudson87973cd2013-10-22 16:00:04 +01001989 * sfe_ipv4_init()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001990 */
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05301991int sfe_ipv4_init(void)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001992{
1993 struct sfe_ipv4 *si = &__si;
1994 int result = -1;
1995
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001996 DEBUG_INFO("SFE IPv4 init\n");
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001997
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301998 sfe_ipv4_conn_match_hash_init(si, ARRAY_SIZE(si->hlist_conn_match_hash_head));
1999
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302000 si->stats_pcpu = alloc_percpu_gfp(struct sfe_ipv4_stats, GFP_KERNEL | __GFP_ZERO);
2001 if (!si->stats_pcpu) {
2002 DEBUG_ERROR("failed to allocate stats memory for sfe_ipv4\n");
2003 goto exit0;
2004 }
2005
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002006 /*
2007 * Create sys/sfe_ipv4
2008 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302009 si->sys_ipv4 = kobject_create_and_add("sfe_ipv4", NULL);
2010 if (!si->sys_ipv4) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002011 DEBUG_ERROR("failed to register sfe_ipv4\n");
2012 goto exit1;
2013 }
2014
2015 /*
2016 * Create files, one for each parameter supported by this module.
2017 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302018 result = sysfs_create_file(si->sys_ipv4, &sfe_ipv4_debug_dev_attr.attr);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002019 if (result) {
2020 DEBUG_ERROR("failed to register debug dev file: %d\n", result);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002021 goto exit2;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002022 }
2023
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302024 result = sysfs_create_file(si->sys_ipv4, &sfe_ipv4_cpu_attr.attr);
Ken Zhu137722d2021-09-23 17:57:36 -07002025 if (result) {
2026 DEBUG_ERROR("failed to register debug dev file: %d\n", result);
2027 goto exit3;
2028 }
2029
Xiaoping Fan640faf42015-08-28 15:50:55 -07002030#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302031 result = sysfs_create_file(si->sys_ipv4, &sfe_ipv4_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002032 if (result) {
2033 DEBUG_ERROR("failed to register flow cookie enable file: %d\n", result);
Ken Zhu137722d2021-09-23 17:57:36 -07002034 goto exit4;
Xiaoping Fan640faf42015-08-28 15:50:55 -07002035 }
2036#endif /* CONFIG_NF_FLOW_COOKIE */
2037
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002038 /*
2039 * Register our debug char device.
2040 */
2041 result = register_chrdev(0, "sfe_ipv4", &sfe_ipv4_debug_dev_fops);
2042 if (result < 0) {
2043 DEBUG_ERROR("Failed to register chrdev: %d\n", result);
Ken Zhu137722d2021-09-23 17:57:36 -07002044 goto exit5;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002045 }
2046
2047 si->debug_dev = result;
Ken Zhu137722d2021-09-23 17:57:36 -07002048 si->work_cpu = WORK_CPU_UNBOUND;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002049
2050 /*
Ken Zhu137722d2021-09-23 17:57:36 -07002051 * Create a work to handle periodic statistics.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002052 */
Ken Zhu137722d2021-09-23 17:57:36 -07002053 INIT_DELAYED_WORK(&(si->sync_dwork), sfe_ipv4_periodic_sync);
2054 schedule_delayed_work_on(si->work_cpu, &(si->sync_dwork), ((HZ + 99) / 100));
2055
Dave Hudson87973cd2013-10-22 16:00:04 +01002056 spin_lock_init(&si->lock);
Dave Hudson87973cd2013-10-22 16:00:04 +01002057 return 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002058
Ken Zhu137722d2021-09-23 17:57:36 -07002059exit5:
Xiaoping Fan640faf42015-08-28 15:50:55 -07002060#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302061 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002062
Ken Zhu137722d2021-09-23 17:57:36 -07002063exit4:
Xiaoping Fan640faf42015-08-28 15:50:55 -07002064#endif /* CONFIG_NF_FLOW_COOKIE */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302065 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_cpu_attr.attr);
Ken Zhu137722d2021-09-23 17:57:36 -07002066exit3:
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302067 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_debug_dev_attr.attr);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002068
Xiaoping Fan640faf42015-08-28 15:50:55 -07002069exit2:
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302070 kobject_put(si->sys_ipv4);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002071
2072exit1:
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302073 free_percpu(si->stats_pcpu);
2074
2075exit0:
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002076 return result;
2077}
2078
2079/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002080 * sfe_ipv4_exit()
2081 */
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05302082void sfe_ipv4_exit(void)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002083{
Dave Hudson87973cd2013-10-22 16:00:04 +01002084 struct sfe_ipv4 *si = &__si;
2085
Dave Hudsondcd08fb2013-11-22 09:25:16 -06002086 DEBUG_INFO("SFE IPv4 exit\n");
Dave Hudson87973cd2013-10-22 16:00:04 +01002087 /*
2088 * Destroy all connections.
2089 */
Dave Hudsondcd08fb2013-11-22 09:25:16 -06002090 sfe_ipv4_destroy_all_rules_for_dev(NULL);
Dave Hudson87973cd2013-10-22 16:00:04 +01002091
Ken Zhu137722d2021-09-23 17:57:36 -07002092 cancel_delayed_work_sync(&si->sync_dwork);
Dave Hudson87973cd2013-10-22 16:00:04 +01002093
Dave Hudson87973cd2013-10-22 16:00:04 +01002094 unregister_chrdev(si->debug_dev, "sfe_ipv4");
2095
Xiaoping Fan640faf42015-08-28 15:50:55 -07002096#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302097 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002098#endif /* CONFIG_NF_FLOW_COOKIE */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302099 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_debug_dev_attr.attr);
2100 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_cpu_attr.attr);
Dave Hudson87973cd2013-10-22 16:00:04 +01002101
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302102 kobject_put(si->sys_ipv4);
Dave Hudson87973cd2013-10-22 16:00:04 +01002103
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302104 free_percpu(si->stats_pcpu);
2105
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002106}
2107
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08002108#ifdef CONFIG_NF_FLOW_COOKIE
2109EXPORT_SYMBOL(sfe_register_flow_cookie_cb);
2110EXPORT_SYMBOL(sfe_unregister_flow_cookie_cb);
2111#endif