blob: cf04e1264f832febe1053b36c226d1c96e3ac8c6 [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",
Ratheesh Kannoth43d64f82021-10-20 08:23:29 +053070 "HEADER_CSUM_BAD",
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010071 "BAD_TOTAL_LENGTH",
72 "NON_V4",
73 "NON_INITIAL_FRAGMENT",
74 "DATAGRAM_INCOMPLETE",
75 "IP_OPTIONS_INCOMPLETE",
76 "UNHANDLED_PROTOCOL"
77};
78
Xiaoping Fan6a1672f2016-08-17 19:58:12 -070079static struct sfe_ipv4 __si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010080
81/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010082 * sfe_ipv4_gen_ip_csum()
83 * Generate the IP checksum for an IPv4 header.
84 *
85 * Note that this function assumes that we have only 20 bytes of IP header.
86 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +053087u16 sfe_ipv4_gen_ip_csum(struct iphdr *iph)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010088{
Xiaoping Fan6a1672f2016-08-17 19:58:12 -070089 u32 sum;
90 u16 *i = (u16 *)iph;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010091
92 iph->check = 0;
93
94 /*
95 * Generate the sum.
96 */
97 sum = i[0] + i[1] + i[2] + i[3] + i[4] + i[5] + i[6] + i[7] + i[8] + i[9];
98
99 /*
100 * Fold it to ones-complement form.
101 */
102 sum = (sum & 0xffff) + (sum >> 16);
103 sum = (sum & 0xffff) + (sum >> 16);
104
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700105 return (u16)sum ^ 0xffff;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100106}
107
108/*
109 * sfe_ipv4_get_connection_match_hash()
110 * Generate the hash used in connection match lookups.
111 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700112static inline unsigned int sfe_ipv4_get_connection_match_hash(struct net_device *dev, u8 protocol,
Dave Hudson87973cd2013-10-22 16:00:04 +0100113 __be32 src_ip, __be16 src_port,
114 __be32 dest_ip, __be16 dest_port)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100115{
116 size_t dev_addr = (size_t)dev;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700117 u32 hash = ((u32)dev_addr) ^ ntohl(src_ip ^ dest_ip) ^ protocol ^ ntohs(src_port ^ dest_port);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100118 return ((hash >> SFE_IPV4_CONNECTION_HASH_SHIFT) ^ hash) & SFE_IPV4_CONNECTION_HASH_MASK;
119}
120
121/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530122 * sfe_ipv4_find_connection_match_rcu()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100123 * Get the IPv4 flow match info that corresponds to a particular 5-tuple.
124 *
125 * On entry we must be holding the lock that protects the hash table.
126 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530127struct sfe_ipv4_connection_match *
128sfe_ipv4_find_connection_match_rcu(struct sfe_ipv4 *si, struct net_device *dev, u8 protocol,
Dave Hudson87973cd2013-10-22 16:00:04 +0100129 __be32 src_ip, __be16 src_port,
130 __be32 dest_ip, __be16 dest_port)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100131{
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530132 struct sfe_ipv4_connection_match *cm = NULL;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100133 unsigned int conn_match_idx;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530134 struct hlist_head *lhead;
135
136 WARN_ON_ONCE(!rcu_read_lock_held());
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100137
138 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 +0100139
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530140 lhead = &si->hlist_conn_match_hash_head[conn_match_idx];
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100141
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530142 hlist_for_each_entry_rcu(cm, lhead, hnode) {
143 if (cm->match_src_port != src_port
144 || cm->match_dest_port != dest_port
145 || cm->match_src_ip != src_ip
146 || cm->match_dest_ip != dest_ip
147 || cm->match_protocol != protocol
148 || cm->match_dev != dev) {
149 continue;
150 }
151
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530152 this_cpu_inc(si->stats_pcpu->connection_match_hash_hits64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100153
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530154 break;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100155 }
156
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100157 return cm;
158}
159
160/*
161 * sfe_ipv4_connection_match_update_summary_stats()
162 * Update the summary stats for a connection match entry.
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530163 *
164 * Stats are incremented atomically. So use atomic substraction to update summary
165 * stats.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100166 */
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530167static inline void sfe_ipv4_connection_match_update_summary_stats(struct sfe_ipv4_connection_match *cm,
168 u32 *packets, u32 *bytes)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100169{
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530170 u32 packet_count, byte_count;
171
172 packet_count = atomic_read(&cm->rx_packet_count);
173 cm->rx_packet_count64 += packet_count;
174 atomic_sub(packet_count, &cm->rx_packet_count);
175
176 byte_count = atomic_read(&cm->rx_byte_count);
177 cm->rx_byte_count64 += byte_count;
178 atomic_sub(byte_count, &cm->rx_byte_count);
179
180 *packets = packet_count;
181 *bytes = byte_count;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100182}
183
184/*
185 * sfe_ipv4_connection_match_compute_translations()
186 * Compute port and address translations for a connection match entry.
187 */
188static void sfe_ipv4_connection_match_compute_translations(struct sfe_ipv4_connection_match *cm)
189{
190 /*
191 * Before we insert the entry look to see if this is tagged as doing address
192 * translations. If it is then work out the adjustment that we need to apply
193 * to the transport checksum.
194 */
195 if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC) {
196 /*
197 * Precompute an incremental checksum adjustment so we can
198 * edit packets in this stream very quickly. The algorithm is from RFC1624.
199 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700200 u16 src_ip_hi = cm->match_src_ip >> 16;
201 u16 src_ip_lo = cm->match_src_ip & 0xffff;
202 u32 xlate_src_ip = ~cm->xlate_src_ip;
203 u16 xlate_src_ip_hi = xlate_src_ip >> 16;
204 u16 xlate_src_ip_lo = xlate_src_ip & 0xffff;
205 u16 xlate_src_port = ~cm->xlate_src_port;
206 u32 adj;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100207
208 /*
209 * When we compute this fold it down to a 16-bit offset
210 * as that way we can avoid having to do a double
211 * folding of the twos-complement result because the
212 * addition of 2 16-bit values cannot cause a double
213 * wrap-around!
214 */
215 adj = src_ip_hi + src_ip_lo + cm->match_src_port
216 + xlate_src_ip_hi + xlate_src_ip_lo + xlate_src_port;
217 adj = (adj & 0xffff) + (adj >> 16);
218 adj = (adj & 0xffff) + (adj >> 16);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700219 cm->xlate_src_csum_adjustment = (u16)adj;
Nicolas Costaac2979c2014-01-14 10:35:24 -0600220
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100221 }
222
223 if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST) {
224 /*
225 * Precompute an incremental checksum adjustment so we can
226 * edit packets in this stream very quickly. The algorithm is from RFC1624.
227 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700228 u16 dest_ip_hi = cm->match_dest_ip >> 16;
229 u16 dest_ip_lo = cm->match_dest_ip & 0xffff;
230 u32 xlate_dest_ip = ~cm->xlate_dest_ip;
231 u16 xlate_dest_ip_hi = xlate_dest_ip >> 16;
232 u16 xlate_dest_ip_lo = xlate_dest_ip & 0xffff;
233 u16 xlate_dest_port = ~cm->xlate_dest_port;
234 u32 adj;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100235
236 /*
237 * When we compute this fold it down to a 16-bit offset
238 * as that way we can avoid having to do a double
239 * folding of the twos-complement result because the
240 * addition of 2 16-bit values cannot cause a double
241 * wrap-around!
242 */
243 adj = dest_ip_hi + dest_ip_lo + cm->match_dest_port
244 + xlate_dest_ip_hi + xlate_dest_ip_lo + xlate_dest_port;
245 adj = (adj & 0xffff) + (adj >> 16);
246 adj = (adj & 0xffff) + (adj >> 16);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700247 cm->xlate_dest_csum_adjustment = (u16)adj;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100248 }
Xiaoping Fanad755af2015-04-01 16:58:46 -0700249
250 if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC) {
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700251 u32 adj = ~cm->match_src_ip + cm->xlate_src_ip;
Xiaoping Fanad755af2015-04-01 16:58:46 -0700252 if (adj < cm->xlate_src_ip) {
253 adj++;
254 }
255
256 adj = (adj & 0xffff) + (adj >> 16);
257 adj = (adj & 0xffff) + (adj >> 16);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700258 cm->xlate_src_partial_csum_adjustment = (u16)adj;
Xiaoping Fanad755af2015-04-01 16:58:46 -0700259 }
260
261 if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST) {
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700262 u32 adj = ~cm->match_dest_ip + cm->xlate_dest_ip;
Xiaoping Fanad755af2015-04-01 16:58:46 -0700263 if (adj < cm->xlate_dest_ip) {
264 adj++;
265 }
266
267 adj = (adj & 0xffff) + (adj >> 16);
268 adj = (adj & 0xffff) + (adj >> 16);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700269 cm->xlate_dest_partial_csum_adjustment = (u16)adj;
Xiaoping Fanad755af2015-04-01 16:58:46 -0700270 }
271
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100272}
273
274/*
275 * sfe_ipv4_update_summary_stats()
276 * Update the summary stats.
277 */
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530278static void sfe_ipv4_update_summary_stats(struct sfe_ipv4 *si, struct sfe_ipv4_stats *stats)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100279{
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530280 int i = 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100281
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530282 memset(stats, 0, sizeof(*stats));
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100283
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530284 for_each_possible_cpu(i) {
285 const struct sfe_ipv4_stats *s = per_cpu_ptr(si->stats_pcpu, i);
286
287 stats->connection_create_requests64 += s->connection_create_requests64;
288 stats->connection_create_collisions64 += s->connection_create_collisions64;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530289 stats->connection_create_failures64 += s->connection_create_failures64;
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530290 stats->connection_destroy_requests64 += s->connection_destroy_requests64;
291 stats->connection_destroy_misses64 += s->connection_destroy_misses64;
292 stats->connection_match_hash_hits64 += s->connection_match_hash_hits64;
293 stats->connection_match_hash_reorders64 += s->connection_match_hash_reorders64;
294 stats->connection_flushes64 += s->connection_flushes64;
295 stats->packets_forwarded64 += s->packets_forwarded64;
296 stats->packets_not_forwarded64 += s->packets_not_forwarded64;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100297 }
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530298
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100299}
300
301/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530302 * sfe_ipv4_insert_connection_match()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100303 * Insert a connection match into the hash.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100304 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530305static inline void sfe_ipv4_insert_connection_match(struct sfe_ipv4 *si,
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700306 struct sfe_ipv4_connection_match *cm)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100307{
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100308 unsigned int conn_match_idx
309 = sfe_ipv4_get_connection_match_hash(cm->match_dev, cm->match_protocol,
310 cm->match_src_ip, cm->match_src_port,
311 cm->match_dest_ip, cm->match_dest_port);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700312
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530313 lockdep_assert_held(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100314
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530315 hlist_add_head_rcu(&cm->hnode, &si->hlist_conn_match_hash_head[conn_match_idx]);
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800316#ifdef CONFIG_NF_FLOW_COOKIE
Xiaoping Fan640faf42015-08-28 15:50:55 -0700317 if (!si->flow_cookie_enable)
318 return;
319
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800320 /*
321 * Configure hardware to put a flow cookie in packet of this flow,
322 * then we can accelerate the lookup process when we received this packet.
323 */
324 for (conn_match_idx = 1; conn_match_idx < SFE_FLOW_COOKIE_SIZE; conn_match_idx++) {
325 struct sfe_flow_cookie_entry *entry = &si->sfe_flow_cookie_table[conn_match_idx];
326
327 if ((NULL == entry->match) && time_is_before_jiffies(entry->last_clean_time + HZ)) {
328 flow_cookie_set_func_t func;
329
330 rcu_read_lock();
331 func = rcu_dereference(si->flow_cookie_set_func);
332 if (func) {
Xiaoping Fan59176422015-05-22 15:58:10 -0700333 if (!func(cm->match_protocol, cm->match_src_ip, cm->match_src_port,
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800334 cm->match_dest_ip, cm->match_dest_port, conn_match_idx)) {
335 entry->match = cm;
336 cm->flow_cookie = conn_match_idx;
337 }
338 }
339 rcu_read_unlock();
340
341 break;
342 }
343 }
344#endif
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100345}
346
347/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530348 * sfe_ipv4_remove_connection_match()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100349 * Remove a connection match object from the hash.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100350 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530351static inline void sfe_ipv4_remove_connection_match(struct sfe_ipv4 *si, struct sfe_ipv4_connection_match *cm)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100352{
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530353
354 lockdep_assert_held(&si->lock);
355
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800356#ifdef CONFIG_NF_FLOW_COOKIE
Xiaoping Fan640faf42015-08-28 15:50:55 -0700357 if (si->flow_cookie_enable) {
358 /*
359 * Tell hardware that we no longer need a flow cookie in packet of this flow
360 */
361 unsigned int conn_match_idx;
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800362
Xiaoping Fan640faf42015-08-28 15:50:55 -0700363 for (conn_match_idx = 1; conn_match_idx < SFE_FLOW_COOKIE_SIZE; conn_match_idx++) {
364 struct sfe_flow_cookie_entry *entry = &si->sfe_flow_cookie_table[conn_match_idx];
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800365
Xiaoping Fan640faf42015-08-28 15:50:55 -0700366 if (cm == entry->match) {
367 flow_cookie_set_func_t func;
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800368
Xiaoping Fan640faf42015-08-28 15:50:55 -0700369 rcu_read_lock();
370 func = rcu_dereference(si->flow_cookie_set_func);
371 if (func) {
372 func(cm->match_protocol, cm->match_src_ip, cm->match_src_port,
373 cm->match_dest_ip, cm->match_dest_port, 0);
374 }
375 rcu_read_unlock();
376
377 cm->flow_cookie = 0;
378 entry->match = NULL;
379 entry->last_clean_time = jiffies;
380 break;
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800381 }
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800382 }
383 }
384#endif
385
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530386 hlist_del_init_rcu(&cm->hnode);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100387
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100388}
389
390/*
391 * sfe_ipv4_get_connection_hash()
392 * Generate the hash used in connection lookups.
393 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700394static inline unsigned int sfe_ipv4_get_connection_hash(u8 protocol, __be32 src_ip, __be16 src_port,
Dave Hudson87973cd2013-10-22 16:00:04 +0100395 __be32 dest_ip, __be16 dest_port)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100396{
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700397 u32 hash = ntohl(src_ip ^ dest_ip) ^ protocol ^ ntohs(src_port ^ dest_port);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100398 return ((hash >> SFE_IPV4_CONNECTION_HASH_SHIFT) ^ hash) & SFE_IPV4_CONNECTION_HASH_MASK;
399}
400
401/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530402 * sfe_ipv4_find_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100403 * Get the IPv4 connection info that corresponds to a particular 5-tuple.
404 *
405 * On entry we must be holding the lock that protects the hash table.
406 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530407static inline struct sfe_ipv4_connection *sfe_ipv4_find_connection(struct sfe_ipv4 *si, u32 protocol,
Dave Hudson87973cd2013-10-22 16:00:04 +0100408 __be32 src_ip, __be16 src_port,
409 __be32 dest_ip, __be16 dest_port)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100410{
411 struct sfe_ipv4_connection *c;
412 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 +0530413
414 lockdep_assert_held(&si->lock);
415
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100416 c = si->conn_hash[conn_idx];
417
418 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100419 * Will need connection entry for next create/destroy metadata,
420 * So no need to re-order entry for these requests
421 */
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530422 while (c) {
423 if ((c->src_port == src_port)
424 && (c->dest_port == dest_port)
425 && (c->src_ip == src_ip)
426 && (c->dest_ip == dest_ip)
427 && (c->protocol == protocol)) {
428 return c;
429 }
430
431 c = c->next;
432 }
433
434 return NULL;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100435}
436
437/*
Matthew McClintockbe7b47d2013-11-27 13:26:23 -0600438 * sfe_ipv4_mark_rule()
439 * Updates the mark for a current offloaded connection
440 *
441 * Will take hash lock upon entry
442 */
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700443void sfe_ipv4_mark_rule(struct sfe_connection_mark *mark)
Matthew McClintockbe7b47d2013-11-27 13:26:23 -0600444{
445 struct sfe_ipv4 *si = &__si;
446 struct sfe_ipv4_connection *c;
Matthew McClintockdb5ac512014-01-16 17:01:40 -0600447
Xiaoping Fan3c423e32015-07-03 03:09:29 -0700448 spin_lock_bh(&si->lock);
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530449 c = sfe_ipv4_find_connection(si, mark->protocol,
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700450 mark->src_ip.ip, mark->src_port,
451 mark->dest_ip.ip, mark->dest_port);
Matthew McClintockbe7b47d2013-11-27 13:26:23 -0600452 if (c) {
Nicolas Costaf53d6fe2014-01-13 16:03:46 -0600453 WARN_ON((0 != c->mark) && (0 == mark->mark));
Matthew McClintockbe7b47d2013-11-27 13:26:23 -0600454 c->mark = mark->mark;
455 }
Xiaoping Fan3c423e32015-07-03 03:09:29 -0700456 spin_unlock_bh(&si->lock);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700457
458 if (c) {
459 DEBUG_TRACE("Matching connection found for mark, "
460 "setting from %08x to %08x\n",
461 c->mark, mark->mark);
462 }
Matthew McClintockbe7b47d2013-11-27 13:26:23 -0600463}
464
465/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530466 * sfe_ipv4_insert_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100467 * Insert a connection into the hash.
468 *
469 * On entry we must be holding the lock that protects the hash table.
470 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530471static void sfe_ipv4_insert_connection(struct sfe_ipv4 *si, struct sfe_ipv4_connection *c)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100472{
473 struct sfe_ipv4_connection **hash_head;
474 struct sfe_ipv4_connection *prev_head;
475 unsigned int conn_idx;
476
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530477 lockdep_assert_held(&si->lock);
478
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100479 /*
480 * Insert entry into the connection hash.
481 */
482 conn_idx = sfe_ipv4_get_connection_hash(c->protocol, c->src_ip, c->src_port,
483 c->dest_ip, c->dest_port);
484 hash_head = &si->conn_hash[conn_idx];
485 prev_head = *hash_head;
486 c->prev = NULL;
487 if (prev_head) {
488 prev_head->prev = c;
489 }
490
491 c->next = prev_head;
492 *hash_head = c;
493
494 /*
495 * Insert entry into the "all connections" list.
496 */
497 if (si->all_connections_tail) {
498 c->all_connections_prev = si->all_connections_tail;
499 si->all_connections_tail->all_connections_next = c;
500 } else {
501 c->all_connections_prev = NULL;
502 si->all_connections_head = c;
503 }
504
505 si->all_connections_tail = c;
506 c->all_connections_next = NULL;
507 si->num_connections++;
508
509 /*
510 * Insert the connection match objects too.
511 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530512 sfe_ipv4_insert_connection_match(si, c->original_match);
513 sfe_ipv4_insert_connection_match(si, c->reply_match);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100514}
515
516/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530517 * sfe_ipv4_remove_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100518 * Remove a sfe_ipv4_connection object from the hash.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100519 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530520bool sfe_ipv4_remove_connection(struct sfe_ipv4 *si, struct sfe_ipv4_connection *c)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100521{
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530522 lockdep_assert_held(&si->lock);
523
524 if (c->removed) {
525 DEBUG_ERROR("%px: Connection has been removed already\n", c);
526 return false;
527 }
528
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100529 /*
530 * Remove the connection match objects.
531 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530532 sfe_ipv4_remove_connection_match(si, c->reply_match);
533 sfe_ipv4_remove_connection_match(si, c->original_match);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100534
535 /*
536 * Unlink the connection.
537 */
538 if (c->prev) {
539 c->prev->next = c->next;
540 } else {
541 unsigned int conn_idx = sfe_ipv4_get_connection_hash(c->protocol, c->src_ip, c->src_port,
542 c->dest_ip, c->dest_port);
543 si->conn_hash[conn_idx] = c->next;
544 }
545
546 if (c->next) {
547 c->next->prev = c->prev;
548 }
Xiaoping Fan34586472015-07-03 02:20:35 -0700549
550 /*
551 * Unlink connection from all_connections list
552 */
553 if (c->all_connections_prev) {
554 c->all_connections_prev->all_connections_next = c->all_connections_next;
555 } else {
556 si->all_connections_head = c->all_connections_next;
557 }
558
559 if (c->all_connections_next) {
560 c->all_connections_next->all_connections_prev = c->all_connections_prev;
561 } else {
562 si->all_connections_tail = c->all_connections_prev;
563 }
564
Ken Zhudc423672021-09-02 18:27:01 -0700565 /*
566 * If I am the next sync connection, move the sync to my next or head.
567 */
568 if (unlikely(si->wc_next == c)) {
569 si->wc_next = c->all_connections_next;
570 }
571
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530572 c->removed = true;
Xiaoping Fan34586472015-07-03 02:20:35 -0700573 si->num_connections--;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530574 return true;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100575}
576
577/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530578 * sfe_ipv4_gen_sync_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100579 * Sync a connection.
580 *
581 * On entry to this function we expect that the lock for the connection is either
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530582 * already held (while called from sfe_ipv4_periodic_sync() or isn't required
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530583 * (while called from sfe_ipv4_flush_connection())
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100584 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530585static void sfe_ipv4_gen_sync_connection(struct sfe_ipv4 *si, struct sfe_ipv4_connection *c,
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700586 struct sfe_connection_sync *sis, sfe_sync_reason_t reason,
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700587 u64 now_jiffies)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100588{
589 struct sfe_ipv4_connection_match *original_cm;
590 struct sfe_ipv4_connection_match *reply_cm;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530591 u32 packet_count, byte_count;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100592
593 /*
594 * Fill in the update message.
595 */
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700596 sis->is_v6 = 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100597 sis->protocol = c->protocol;
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700598 sis->src_ip.ip = c->src_ip;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700599 sis->src_ip_xlate.ip = c->src_ip_xlate;
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700600 sis->dest_ip.ip = c->dest_ip;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700601 sis->dest_ip_xlate.ip = c->dest_ip_xlate;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100602 sis->src_port = c->src_port;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700603 sis->src_port_xlate = c->src_port_xlate;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100604 sis->dest_port = c->dest_port;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700605 sis->dest_port_xlate = c->dest_port_xlate;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100606
607 original_cm = c->original_match;
608 reply_cm = c->reply_match;
609 sis->src_td_max_window = original_cm->protocol_state.tcp.max_win;
610 sis->src_td_end = original_cm->protocol_state.tcp.end;
611 sis->src_td_max_end = original_cm->protocol_state.tcp.max_end;
612 sis->dest_td_max_window = reply_cm->protocol_state.tcp.max_win;
613 sis->dest_td_end = reply_cm->protocol_state.tcp.end;
614 sis->dest_td_max_end = reply_cm->protocol_state.tcp.max_end;
615
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530616 sfe_ipv4_connection_match_update_summary_stats(original_cm, &packet_count, &byte_count);
617 sis->src_new_packet_count = packet_count;
618 sis->src_new_byte_count = byte_count;
Matthew McClintockd0cdb802014-02-24 16:30:35 -0600619
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530620 sfe_ipv4_connection_match_update_summary_stats(reply_cm, &packet_count, &byte_count);
621 sis->dest_new_packet_count = packet_count;
622 sis->dest_new_byte_count = byte_count;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100623
Matthew McClintockd0cdb802014-02-24 16:30:35 -0600624 sis->src_dev = original_cm->match_dev;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100625 sis->src_packet_count = original_cm->rx_packet_count64;
626 sis->src_byte_count = original_cm->rx_byte_count64;
Matthew McClintockd0cdb802014-02-24 16:30:35 -0600627
628 sis->dest_dev = reply_cm->match_dev;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100629 sis->dest_packet_count = reply_cm->rx_packet_count64;
630 sis->dest_byte_count = reply_cm->rx_byte_count64;
631
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700632 sis->reason = reason;
633
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100634 /*
635 * Get the time increment since our last sync.
636 */
637 sis->delta_jiffies = now_jiffies - c->last_sync_jiffies;
638 c->last_sync_jiffies = now_jiffies;
639}
640
641/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530642 * sfe_ipv4_free_connection_rcu()
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530643 * Called at RCU qs state to free the connection object.
644 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530645static void sfe_ipv4_free_connection_rcu(struct rcu_head *head)
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530646{
647 struct sfe_ipv4_connection *c;
648
649 /*
650 * We dont need spin lock as the connection is already removed from link list
651 */
652 c = container_of(head, struct sfe_ipv4_connection, rcu);
653
654 BUG_ON(!c->removed);
655
656 DEBUG_TRACE("%px: connecton has been deleted\n", c);
657
658 /*
659 * Release our hold of the source and dest devices and free the memory
660 * for our connection objects.
661 */
662 dev_put(c->original_dev);
663 dev_put(c->reply_dev);
664 kfree(c->original_match);
665 kfree(c->reply_match);
666 kfree(c);
667}
668
669/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530670 * sfe_ipv4_flush_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100671 * Flush a connection and free all associated resources.
672 *
673 * We need to be called with bottom halves disabled locally as we need to acquire
674 * the connection hash lock and release it again. In general we're actually called
675 * from within a BH and so we're fine, but we're also called when connections are
676 * torn down.
677 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530678void sfe_ipv4_flush_connection(struct sfe_ipv4 *si,
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700679 struct sfe_ipv4_connection *c,
680 sfe_sync_reason_t reason)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100681{
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700682 u64 now_jiffies;
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700683 sfe_sync_rule_callback_t sync_rule_callback;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100684
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530685 BUG_ON(!c->removed);
686
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530687 this_cpu_inc(si->stats_pcpu->connection_flushes64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100688
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530689 rcu_read_lock();
690 sync_rule_callback = rcu_dereference(si->sync_rule_callback);
691
692 /*
693 * Generate a sync message and then sync.
694 */
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600695 if (sync_rule_callback) {
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530696 struct sfe_connection_sync sis;
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600697 now_jiffies = get_jiffies_64();
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530698 sfe_ipv4_gen_sync_connection(si, c, &sis, reason, now_jiffies);
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600699 sync_rule_callback(&sis);
700 }
701
702 rcu_read_unlock();
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100703
704 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100705 * Release our hold of the source and dest devices and free the memory
706 * for our connection objects.
707 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530708 call_rcu(&c->rcu, sfe_ipv4_free_connection_rcu);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100709}
710
711/*
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530712 * sfe_ipv4_exception_stats_inc()
713 * Increment exception stats.
714 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530715void sfe_ipv4_exception_stats_inc(struct sfe_ipv4 *si, enum sfe_ipv4_exception_events reason)
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530716{
717 struct sfe_ipv4_stats *stats = this_cpu_ptr(si->stats_pcpu);
718 stats->exception_events64[reason]++;
719 stats->packets_not_forwarded64++;
720}
721
722/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100723 * sfe_ipv4_recv()
Matthew McClintocka8ad7962014-01-16 16:49:30 -0600724 * Handle packet receives and forwaring.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100725 *
726 * Returns 1 if the packet is forwarded or 0 if it isn't.
727 */
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600728int sfe_ipv4_recv(struct net_device *dev, struct sk_buff *skb)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100729{
730 struct sfe_ipv4 *si = &__si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100731 unsigned int len;
732 unsigned int tot_len;
733 unsigned int frag_off;
734 unsigned int ihl;
735 bool flush_on_find;
736 bool ip_options;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530737 struct iphdr *iph;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700738 u32 protocol;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100739
740 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100741 * Check that we have space for an IP header here.
742 */
743 len = skb->len;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530744 if (unlikely(!pskb_may_pull(skb, sizeof(struct iphdr)))) {
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530745 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_HEADER_INCOMPLETE);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100746 DEBUG_TRACE("len: %u is too short\n", len);
747 return 0;
748 }
749
750 /*
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +0530751 * Validate ip csum if necessary. If ip_summed is set to CHECKSUM_UNNECESSARY, it is assumed
752 * that the L3 checksum is validated by the Rx interface or the tunnel interface that has
753 * generated the packet.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100754 */
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530755 iph = (struct iphdr *)skb->data;
Ratheesh Kannoth43d64f82021-10-20 08:23:29 +0530756 if (unlikely(skb->ip_summed != CHECKSUM_UNNECESSARY) && (ip_fast_csum((u8 *)iph, iph->ihl))) {
757 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_HEADER_CSUM_BAD);
758
759 DEBUG_TRACE("Bad IPv4 header csum: 0x%x\n", iph->check);
760 return 0;
761 }
762
763 /*
764 * Check that our "total length" is large enough for an IP header.
765 */
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100766 tot_len = ntohs(iph->tot_len);
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530767 if (unlikely(tot_len < sizeof(struct iphdr))) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100768
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530769 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_BAD_TOTAL_LENGTH);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100770 DEBUG_TRACE("tot_len: %u is too short\n", tot_len);
771 return 0;
772 }
773
774 /*
775 * Is our IP version wrong?
776 */
777 if (unlikely(iph->version != 4)) {
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530778 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_NON_V4);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100779 DEBUG_TRACE("IP version: %u\n", iph->version);
780 return 0;
781 }
782
783 /*
784 * Does our datagram fit inside the skb?
785 */
786 if (unlikely(tot_len > len)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100787
788 DEBUG_TRACE("tot_len: %u, exceeds len: %u\n", tot_len, len);
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530789 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_DATAGRAM_INCOMPLETE);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100790 return 0;
791 }
792
793 /*
794 * Do we have a non-initial fragment?
Nicolas Costaac2979c2014-01-14 10:35:24 -0600795 */
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100796 frag_off = ntohs(iph->frag_off);
797 if (unlikely(frag_off & IP_OFFSET)) {
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530798 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_NON_INITIAL_FRAGMENT);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100799 DEBUG_TRACE("non-initial fragment\n");
800 return 0;
801 }
802
803 /*
804 * If we have a (first) fragment then mark it to cause any connection to flush.
805 */
806 flush_on_find = unlikely(frag_off & IP_MF) ? true : false;
807
808 /*
809 * Do we have any IP options? That's definite a slow path! If we do have IP
810 * options we need to recheck our header size.
811 */
812 ihl = iph->ihl << 2;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530813 ip_options = unlikely(ihl != sizeof(struct iphdr)) ? true : false;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100814 if (unlikely(ip_options)) {
815 if (unlikely(len < ihl)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100816
817 DEBUG_TRACE("len: %u is too short for header of size: %u\n", len, ihl);
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530818 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_IP_OPTIONS_INCOMPLETE);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100819 return 0;
820 }
821
822 flush_on_find = true;
823 }
824
825 protocol = iph->protocol;
826 if (IPPROTO_UDP == protocol) {
827 return sfe_ipv4_recv_udp(si, skb, dev, len, iph, ihl, flush_on_find);
828 }
829
830 if (IPPROTO_TCP == protocol) {
831 return sfe_ipv4_recv_tcp(si, skb, dev, len, iph, ihl, flush_on_find);
832 }
833
834 if (IPPROTO_ICMP == protocol) {
835 return sfe_ipv4_recv_icmp(si, skb, dev, len, iph, ihl);
836 }
837
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530838 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_UNHANDLED_PROTOCOL);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100839
840 DEBUG_TRACE("not UDP, TCP or ICMP: %u\n", protocol);
841 return 0;
842}
843
Nicolas Costa436926b2014-01-14 10:36:22 -0600844static void
845sfe_ipv4_update_tcp_state(struct sfe_ipv4_connection *c,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530846 struct sfe_ipv4_rule_create_msg *msg)
Nicolas Costa436926b2014-01-14 10:36:22 -0600847{
848 struct sfe_ipv4_connection_match *orig_cm;
849 struct sfe_ipv4_connection_match *repl_cm;
850 struct sfe_ipv4_tcp_connection_match *orig_tcp;
851 struct sfe_ipv4_tcp_connection_match *repl_tcp;
852
853 orig_cm = c->original_match;
854 repl_cm = c->reply_match;
855 orig_tcp = &orig_cm->protocol_state.tcp;
856 repl_tcp = &repl_cm->protocol_state.tcp;
857
858 /* update orig */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530859 if (orig_tcp->max_win < msg->tcp_rule.flow_max_window) {
860 orig_tcp->max_win = msg->tcp_rule.flow_max_window;
Nicolas Costa436926b2014-01-14 10:36:22 -0600861 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530862 if ((s32)(orig_tcp->end - msg->tcp_rule.flow_end) < 0) {
863 orig_tcp->end = msg->tcp_rule.flow_end;
Nicolas Costa436926b2014-01-14 10:36:22 -0600864 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530865 if ((s32)(orig_tcp->max_end - msg->tcp_rule.flow_max_end) < 0) {
866 orig_tcp->max_end = msg->tcp_rule.flow_max_end;
Nicolas Costa436926b2014-01-14 10:36:22 -0600867 }
868
869 /* update reply */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530870 if (repl_tcp->max_win < msg->tcp_rule.return_max_window) {
871 repl_tcp->max_win = msg->tcp_rule.return_max_window;
Nicolas Costa436926b2014-01-14 10:36:22 -0600872 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530873 if ((s32)(repl_tcp->end - msg->tcp_rule.return_end) < 0) {
874 repl_tcp->end = msg->tcp_rule.return_end;
Nicolas Costa436926b2014-01-14 10:36:22 -0600875 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530876 if ((s32)(repl_tcp->max_end - msg->tcp_rule.return_max_end) < 0) {
877 repl_tcp->max_end = msg->tcp_rule.return_max_end;
Nicolas Costa436926b2014-01-14 10:36:22 -0600878 }
879
880 /* update match flags */
881 orig_cm->flags &= ~SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
882 repl_cm->flags &= ~SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530883 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_NO_SEQ_CHECK) {
884
Nicolas Costa436926b2014-01-14 10:36:22 -0600885 orig_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
886 repl_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
887 }
888}
889
890static void
891sfe_ipv4_update_protocol_state(struct sfe_ipv4_connection *c,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530892 struct sfe_ipv4_rule_create_msg *msg)
Nicolas Costa436926b2014-01-14 10:36:22 -0600893{
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530894 switch (msg->tuple.protocol) {
Nicolas Costa436926b2014-01-14 10:36:22 -0600895 case IPPROTO_TCP:
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530896 sfe_ipv4_update_tcp_state(c, msg);
Nicolas Costa436926b2014-01-14 10:36:22 -0600897 break;
898 }
899}
900
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530901void sfe_ipv4_update_rule(struct sfe_ipv4_rule_create_msg *msg)
Nicolas Costa436926b2014-01-14 10:36:22 -0600902{
903 struct sfe_ipv4_connection *c;
904 struct sfe_ipv4 *si = &__si;
905
906 spin_lock_bh(&si->lock);
907
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530908 c = sfe_ipv4_find_connection(si,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530909 msg->tuple.protocol,
910 msg->tuple.flow_ip,
911 msg->tuple.flow_ident,
912 msg->tuple.return_ip,
913 msg->tuple.return_ident);
Nicolas Costa436926b2014-01-14 10:36:22 -0600914 if (c != NULL) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530915 sfe_ipv4_update_protocol_state(c, msg);
Nicolas Costa436926b2014-01-14 10:36:22 -0600916 }
917
918 spin_unlock_bh(&si->lock);
919}
920
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100921/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100922 * sfe_ipv4_create_rule()
923 * Create a forwarding rule.
924 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530925int sfe_ipv4_create_rule(struct sfe_ipv4_rule_create_msg *msg)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100926{
Dave Hudsondcd08fb2013-11-22 09:25:16 -0600927 struct sfe_ipv4 *si = &__si;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530928 struct sfe_ipv4_connection *c, *c_old;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100929 struct sfe_ipv4_connection_match *original_cm;
930 struct sfe_ipv4_connection_match *reply_cm;
Matthew McClintockdb5ac512014-01-16 17:01:40 -0600931 struct net_device *dest_dev;
932 struct net_device *src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530933 struct sfe_ipv4_5tuple *tuple = &msg->tuple;
Suruchi Sumanc1a4a612021-10-21 14:50:23 +0530934 s32 flow_interface_num = msg->conn_rule.flow_top_interface_num;
935 s32 return_interface_num = msg->conn_rule.return_top_interface_num;
Matthew McClintockdb5ac512014-01-16 17:01:40 -0600936
Suruchi Sumanc1a4a612021-10-21 14:50:23 +0530937 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_FLOW_BOTTOM_INTERFACE) {
938 flow_interface_num = msg->conn_rule.flow_interface_num;
939 }
940
941 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_RETURN_BOTTOM_INTERFACE) {
942 return_interface_num = msg->conn_rule.return_interface_num;
943 }
944
945 src_dev = dev_get_by_index(&init_net, flow_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530946 if (!src_dev) {
947 DEBUG_WARN("%px: Unable to find src_dev corresponding to %d\n", msg,
Suruchi Sumanc1a4a612021-10-21 14:50:23 +0530948 flow_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530949 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
950 return -EINVAL;
951 }
952
Suruchi Sumanc1a4a612021-10-21 14:50:23 +0530953 dest_dev = dev_get_by_index(&init_net, return_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530954 if (!dest_dev) {
955 DEBUG_WARN("%px: Unable to find dest_dev corresponding to %d\n", msg,
Suruchi Sumanc1a4a612021-10-21 14:50:23 +0530956 return_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530957 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
958 dev_put(src_dev);
959 return -EINVAL;
960 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100961
Matthew McClintock389b42a2014-09-24 14:05:51 -0500962 if (unlikely((dest_dev->reg_state != NETREG_REGISTERED) ||
963 (src_dev->reg_state != NETREG_REGISTERED))) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530964 dev_put(src_dev);
965 dev_put(dest_dev);
966 DEBUG_WARN("%px: src_dev=%s and dest_dev=%s are unregistered\n", msg,
967 src_dev->name, dest_dev->name);
968 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
Matthew McClintock389b42a2014-09-24 14:05:51 -0500969 return -EINVAL;
970 }
971
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530972 /*
973 * Allocate the various connection tracking objects.
974 */
975 c = (struct sfe_ipv4_connection *)kmalloc(sizeof(struct sfe_ipv4_connection), GFP_ATOMIC);
976 if (unlikely(!c)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530977 DEBUG_WARN("%px: memory allocation of connection entry failed\n", msg);
978 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
979 dev_put(src_dev);
980 dev_put(dest_dev);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530981 return -ENOMEM;
982 }
983
984 original_cm = (struct sfe_ipv4_connection_match *)kmalloc(sizeof(struct sfe_ipv4_connection_match), GFP_ATOMIC);
985 if (unlikely(!original_cm)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530986 DEBUG_WARN("%px: memory allocation of connection match entry failed\n", msg);
987 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530988 kfree(c);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530989 dev_put(src_dev);
990 dev_put(dest_dev);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530991 return -ENOMEM;
992 }
993
994 reply_cm = (struct sfe_ipv4_connection_match *)kmalloc(sizeof(struct sfe_ipv4_connection_match), GFP_ATOMIC);
995 if (unlikely(!reply_cm)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530996 DEBUG_WARN("%px: memory allocation of connection match entry failed\n", msg);
997 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530998 kfree(original_cm);
999 kfree(c);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301000 dev_put(src_dev);
1001 dev_put(dest_dev);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301002 return -ENOMEM;
1003 }
1004
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301005 this_cpu_inc(si->stats_pcpu->connection_create_requests64);
1006
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001007 spin_lock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001008
1009 /*
Nicolas Costa436926b2014-01-14 10:36:22 -06001010 * Check to see if there is already a flow that matches the rule we're
1011 * trying to create. If there is then we can't create a new one.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001012 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301013 c_old = sfe_ipv4_find_connection(si,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301014 msg->tuple.protocol,
1015 msg->tuple.flow_ip,
1016 msg->tuple.flow_ident,
1017 msg->tuple.return_ip,
1018 msg->tuple.return_ident);
1019
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301020 if (c_old != NULL) {
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301021 this_cpu_inc(si->stats_pcpu->connection_create_collisions64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001022
1023 /*
Nicolas Costa436926b2014-01-14 10:36:22 -06001024 * If we already have the flow then it's likely that this
1025 * request to create the connection rule contains more
1026 * up-to-date information. Check and update accordingly.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001027 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301028 sfe_ipv4_update_protocol_state(c, msg);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001029 spin_unlock_bh(&si->lock);
1030
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301031 kfree(reply_cm);
1032 kfree(original_cm);
1033 kfree(c);
1034
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301035 dev_put(src_dev);
1036 dev_put(dest_dev);
1037
1038 DEBUG_TRACE("connection already exists - p:%d\n"
1039 " s: %s:%pM:%pI4:%u, d: %s:%pM:%pI4:%u\n",
1040 tuple->protocol,
1041 src_dev->name, msg->conn_rule.flow_mac, &tuple->flow_ip, ntohs(tuple->flow_ident),
1042 dest_dev->name, msg->conn_rule.return_mac, &tuple->return_ip, ntohs(tuple->return_ident));
1043
Nicolas Costa514fde02014-01-13 15:50:29 -06001044 return -EADDRINUSE;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001045 }
1046
1047 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001048 * Fill in the "original" direction connection matching object.
1049 * Note that the transmit MAC address is "dest_mac_xlate" because
1050 * we always know both ends of a connection by their translated
1051 * addresses and not their public addresses.
1052 */
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001053 original_cm->match_dev = src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301054 original_cm->match_protocol = tuple->protocol;
1055 original_cm->match_src_ip = tuple->flow_ip;
1056 original_cm->match_src_port = tuple->flow_ident;
1057 original_cm->match_dest_ip = tuple->return_ip;
1058 original_cm->match_dest_port = tuple->return_ident;
1059
1060 original_cm->xlate_src_ip = msg->conn_rule.flow_ip_xlate;
1061 original_cm->xlate_src_port = msg->conn_rule.flow_ident_xlate;
1062 original_cm->xlate_dest_ip = msg->conn_rule.return_ip_xlate;
1063 original_cm->xlate_dest_port =msg->conn_rule.return_ident_xlate;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301064 atomic_set(&original_cm->rx_packet_count, 0);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001065 original_cm->rx_packet_count64 = 0;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301066 atomic_set(&original_cm->rx_byte_count, 0);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001067 original_cm->rx_byte_count64 = 0;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301068
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001069 original_cm->xmit_dev = dest_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301070 original_cm->xmit_dev_mtu = msg->conn_rule.return_mtu;
1071
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001072 original_cm->connection = c;
1073 original_cm->counter_match = reply_cm;
1074 original_cm->flags = 0;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301075
1076 if (msg->valid_flags & SFE_RULE_CREATE_QOS_VALID) {
1077 original_cm->priority = msg->qos_rule.flow_qos_tag;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001078 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PRIORITY_REMARK;
1079 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301080
1081 if (msg->valid_flags & SFE_RULE_CREATE_DSCP_MARKING_VALID) {
1082 original_cm->dscp = msg->dscp_rule.flow_dscp << SFE_IPV4_DSCP_SHIFT;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001083 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_DSCP_REMARK;
1084 }
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301085
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001086#ifdef CONFIG_NF_FLOW_COOKIE
1087 original_cm->flow_cookie = 0;
1088#endif
Zhi Chen8748eb32015-06-18 12:58:48 -07001089#ifdef CONFIG_XFRM
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301090 if (msg->valid_flags & SFE_RULE_CREATE_DIRECTION_VALID) {
1091 original_cm->flow_accel = msg->direction_rule.flow_accel;
1092 } else {
1093 original_cm->flow_accel = 1;
1094 }
Zhi Chen8748eb32015-06-18 12:58:48 -07001095#endif
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301096 /*
1097 * If l2_features are disabled and flow uses l2 features such as macvlan/bridge/pppoe/vlan,
1098 * bottom interfaces are expected to be disabled in the flow rule and always top interfaces
1099 * are used. In such cases, do not use HW csum offload. csum offload is used only when we
1100 * are sending directly to the destination interface that supports it.
1101 */
1102 if (likely(dest_dev->features & NETIF_F_HW_CSUM)) {
1103 if ((msg->conn_rule.return_top_interface_num == msg->conn_rule.return_interface_num) ||
1104 (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_RETURN_BOTTOM_INTERFACE)) {
1105 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_CSUM_OFFLOAD;
1106 }
1107 }
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001108
1109 /*
Ken Zhubbf49652021-09-12 15:33:09 -07001110 * For the non-arp interface, we don't write L2 HDR.
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001111 */
Ken Zhubbf49652021-09-12 15:33:09 -07001112 if (!(dest_dev->flags & IFF_NOARP)) {
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301113
1114 /*
1115 * Check whether the rule has configured a specific source MAC address to use.
1116 * This is needed when virtual L3 interfaces such as br-lan, macvlan, vlan are used during egress
1117 */
1118 if ((msg->valid_flags & SFE_RULE_CREATE_SRC_MAC_VALID) &&
1119 (msg->src_mac_rule.mac_valid_flags & SFE_SRC_MAC_RETURN_VALID)) {
1120 ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)msg->src_mac_rule.return_src_mac);
1121 } else {
1122 ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)dest_dev->dev_addr);
1123 }
1124
1125 ether_addr_copy((u8 *)original_cm->xmit_dest_mac, (u8 *)msg->conn_rule.return_mac);
1126
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001127 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_L2_HDR;
1128
1129 /*
1130 * If our dev writes Ethernet headers then we can write a really fast
1131 * version.
1132 */
1133 if (dest_dev->header_ops) {
1134 if (dest_dev->header_ops->create == eth_header) {
1135 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR;
1136 }
1137 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001138 }
1139
1140 /*
1141 * Fill in the "reply" direction connection matching object.
1142 */
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001143 reply_cm->match_dev = dest_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301144 reply_cm->match_protocol = tuple->protocol;
1145 reply_cm->match_src_ip = msg->conn_rule.return_ip_xlate;
1146 reply_cm->match_src_port = msg->conn_rule.return_ident_xlate;
1147 reply_cm->match_dest_ip = msg->conn_rule.flow_ip_xlate;
1148 reply_cm->match_dest_port = msg->conn_rule.flow_ident_xlate;
1149
1150 reply_cm->xlate_src_ip = tuple->return_ip;
1151 reply_cm->xlate_src_port = tuple->return_ident;
1152 reply_cm->xlate_dest_ip = tuple->flow_ip;
1153 reply_cm->xlate_dest_port = tuple->flow_ident;;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301154
1155 atomic_set(&reply_cm->rx_packet_count, 0);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001156 reply_cm->rx_packet_count64 = 0;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301157 atomic_set(&reply_cm->rx_byte_count, 0);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001158 reply_cm->rx_byte_count64 = 0;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301159
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001160 reply_cm->xmit_dev = src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301161 reply_cm->xmit_dev_mtu = msg->conn_rule.flow_mtu;
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301162
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001163 reply_cm->connection = c;
1164 reply_cm->counter_match = original_cm;
1165 reply_cm->flags = 0;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301166 if (msg->valid_flags & SFE_RULE_CREATE_QOS_VALID) {
1167 reply_cm->priority = msg->qos_rule.return_qos_tag;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001168 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PRIORITY_REMARK;
1169 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301170 if (msg->valid_flags & SFE_RULE_CREATE_DSCP_MARKING_VALID) {
1171 reply_cm->dscp = msg->dscp_rule.return_dscp << SFE_IPV4_DSCP_SHIFT;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001172 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_DSCP_REMARK;
1173 }
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001174#ifdef CONFIG_NF_FLOW_COOKIE
1175 reply_cm->flow_cookie = 0;
1176#endif
Zhi Chen8748eb32015-06-18 12:58:48 -07001177#ifdef CONFIG_XFRM
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301178 if (msg->valid_flags & SFE_RULE_CREATE_DIRECTION_VALID) {
1179 reply_cm->flow_accel = msg->direction_rule.return_accel;
1180 } else {
1181 reply_cm->flow_accel = 1;
1182 }
1183
Zhi Chen8748eb32015-06-18 12:58:48 -07001184#endif
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301185 /*
1186 * If l2_features are disabled and flow uses l2 features such as macvlan/bridge/pppoe/vlan,
1187 * bottom interfaces are expected to be disabled in the flow rule and always top interfaces
1188 * are used. In such cases, do not use HW csum offload. csum offload is used only when we
1189 * are sending directly to the destination interface that supports it.
1190 */
1191 if (likely(src_dev->features & NETIF_F_HW_CSUM)) {
1192 if ((msg->conn_rule.flow_top_interface_num == msg->conn_rule.flow_interface_num) ||
1193 (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_FLOW_BOTTOM_INTERFACE)) {
1194 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_CSUM_OFFLOAD;
1195 }
1196 }
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001197
1198 /*
Ken Zhubbf49652021-09-12 15:33:09 -07001199 * For the non-arp interface, we don't write L2 HDR.
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001200 */
Ken Zhubbf49652021-09-12 15:33:09 -07001201 if (!(src_dev->flags & IFF_NOARP)) {
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301202
1203 /*
1204 * Check whether the rule has configured a specific source MAC address to use.
1205 * This is needed when virtual L3 interfaces such as br-lan, macvlan, vlan are used during egress
1206 */
1207 if ((msg->valid_flags & SFE_RULE_CREATE_SRC_MAC_VALID) &&
1208 (msg->src_mac_rule.mac_valid_flags & SFE_SRC_MAC_FLOW_VALID)) {
1209 ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)msg->src_mac_rule.flow_src_mac);
1210 } else {
1211 ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)src_dev->dev_addr);
1212 }
1213 ether_addr_copy((u8 *)reply_cm->xmit_dest_mac, (u8 *)msg->conn_rule.flow_mac);
1214
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001215 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_L2_HDR;
1216
1217 /*
1218 * If our dev writes Ethernet headers then we can write a really fast
1219 * version.
1220 */
1221 if (src_dev->header_ops) {
1222 if (src_dev->header_ops->create == eth_header) {
1223 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR;
1224 }
1225 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001226 }
1227
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301228 if ((tuple->return_ip != msg->conn_rule.return_ip_xlate) ||
1229 (tuple->return_ident != msg->conn_rule.return_ident_xlate)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001230 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST;
1231 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC;
1232 }
1233
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301234 if ((tuple->flow_ip != msg->conn_rule.flow_ip_xlate) ||
1235 (tuple->flow_ident != msg->conn_rule.flow_ident_xlate)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001236 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC;
1237 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST;
1238 }
1239
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301240 c->protocol = tuple->protocol;
1241 c->src_ip = tuple->flow_ip;
1242 c->src_ip_xlate = msg->conn_rule.flow_ip_xlate;
1243 c->src_port = tuple->flow_ident;
1244 c->src_port_xlate = msg->conn_rule.flow_ident_xlate;
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001245 c->original_dev = src_dev;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001246 c->original_match = original_cm;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301247 c->dest_ip = tuple->return_ip;
1248 c->dest_ip_xlate = msg->conn_rule.return_ip_xlate;
1249 c->dest_port = tuple->return_ident;
1250 c->dest_port_xlate = msg->conn_rule.return_ident_xlate;
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001251 c->reply_dev = dest_dev;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001252 c->reply_match = reply_cm;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301253 c->mark = 0; /* TODO : no mark setting for create rule */
Xiaoping Fan34586472015-07-03 02:20:35 -07001254 c->debug_read_seq = 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001255 c->last_sync_jiffies = get_jiffies_64();
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301256 c->removed = false;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001257
1258 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001259 * Initialize the protocol-specific information that we track.
1260 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301261 switch (tuple->protocol) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001262 case IPPROTO_TCP:
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301263 original_cm->protocol_state.tcp.win_scale = msg->tcp_rule.flow_window_scale;
1264 original_cm->protocol_state.tcp.max_win = msg->tcp_rule.flow_max_window ? msg->tcp_rule.flow_max_window : 1;
1265 original_cm->protocol_state.tcp.end = msg->tcp_rule.flow_end;
1266 original_cm->protocol_state.tcp.max_end = msg->tcp_rule.flow_max_end;
1267
1268 reply_cm->protocol_state.tcp.win_scale = msg->tcp_rule.return_window_scale;
1269 reply_cm->protocol_state.tcp.max_win = msg->tcp_rule.return_max_window ? msg->tcp_rule.return_max_window : 1;
1270 reply_cm->protocol_state.tcp.end = msg->tcp_rule.return_end;
1271 reply_cm->protocol_state.tcp.max_end = msg->tcp_rule.return_max_end;
1272
1273 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_NO_SEQ_CHECK) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001274 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
1275 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
1276 }
1277 break;
1278 }
1279
1280 sfe_ipv4_connection_match_compute_translations(original_cm);
1281 sfe_ipv4_connection_match_compute_translations(reply_cm);
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301282 sfe_ipv4_insert_connection(si, c);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001283
1284 spin_unlock_bh(&si->lock);
1285
1286 /*
1287 * We have everything we need!
1288 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301289 DEBUG_INFO("new connection - p: %d\n"
Tian Yang45f39c82020-10-06 14:07:47 -07001290 " s: %s:%pxM(%pxM):%pI4(%pI4):%u(%u)\n"
1291 " d: %s:%pxM(%pxM):%pI4(%pI4):%u(%u)\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301292 tuple->protocol,
1293 src_dev->name, msg->conn_rule.flow_mac, NULL,
1294 &tuple->flow_ip, &msg->conn_rule.flow_ip_xlate, ntohs(tuple->flow_ident), ntohs(msg->conn_rule.flow_ident_xlate),
1295 dest_dev->name, NULL, msg->conn_rule.return_mac,
1296 &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 -06001297
1298 return 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001299}
1300
1301/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001302 * sfe_ipv4_destroy_rule()
1303 * Destroy a forwarding rule.
1304 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301305void sfe_ipv4_destroy_rule(struct sfe_ipv4_rule_destroy_msg *msg)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001306{
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001307 struct sfe_ipv4 *si = &__si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001308 struct sfe_ipv4_connection *c;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301309 bool ret;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301310 struct sfe_ipv4_5tuple *tuple = &msg->tuple;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001311
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301312 this_cpu_inc(si->stats_pcpu->connection_destroy_requests64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001313 spin_lock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001314
1315 /*
1316 * Check to see if we have a flow that matches the rule we're trying
1317 * to destroy. If there isn't then we can't destroy it.
1318 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301319 c = sfe_ipv4_find_connection(si, tuple->protocol, tuple->flow_ip, tuple->flow_ident,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301320 tuple->return_ip, tuple->return_ident);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001321 if (!c) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001322 spin_unlock_bh(&si->lock);
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301323 this_cpu_inc(si->stats_pcpu->connection_destroy_misses64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001324
1325 DEBUG_TRACE("connection does not exist - p: %d, s: %pI4:%u, d: %pI4:%u\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301326 tuple->protocol, &tuple->flow_ip, ntohs(tuple->flow_ident),
1327 &tuple->return_ip, ntohs(tuple->return_ident));
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001328 return;
1329 }
1330
1331 /*
1332 * Remove our connection details from the hash tables.
1333 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301334 ret = sfe_ipv4_remove_connection(si, c);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001335 spin_unlock_bh(&si->lock);
1336
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301337 if (ret) {
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301338 sfe_ipv4_flush_connection(si, c, SFE_SYNC_REASON_DESTROY);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301339 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001340
1341 DEBUG_INFO("connection destroyed - p: %d, s: %pI4:%u, d: %pI4:%u\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301342 tuple->protocol, &tuple->flow_ip, ntohs(tuple->flow_ident),
1343 &tuple->return_ip, ntohs(tuple->return_ident));
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001344}
1345
1346/*
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001347 * sfe_ipv4_register_sync_rule_callback()
1348 * Register a callback for rule synchronization.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001349 */
Xiaoping Fand44a5b42015-05-26 17:37:37 -07001350void sfe_ipv4_register_sync_rule_callback(sfe_sync_rule_callback_t sync_rule_callback)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001351{
1352 struct sfe_ipv4 *si = &__si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001353
1354 spin_lock_bh(&si->lock);
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001355 rcu_assign_pointer(si->sync_rule_callback, sync_rule_callback);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001356 spin_unlock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001357}
1358
1359/*
1360 * sfe_ipv4_get_debug_dev()
1361 */
1362static ssize_t sfe_ipv4_get_debug_dev(struct device *dev,
1363 struct device_attribute *attr,
1364 char *buf)
1365{
1366 struct sfe_ipv4 *si = &__si;
1367 ssize_t count;
1368 int num;
1369
1370 spin_lock_bh(&si->lock);
1371 num = si->debug_dev;
1372 spin_unlock_bh(&si->lock);
1373
1374 count = snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", num);
1375 return count;
1376}
1377
1378/*
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001379 * sysfs attributes.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001380 */
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001381static const struct device_attribute sfe_ipv4_debug_dev_attr =
Xiaoping Fane70da412016-02-26 16:47:57 -08001382 __ATTR(debug_dev, S_IWUSR | S_IRUGO, sfe_ipv4_get_debug_dev, NULL);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001383
1384/*
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001385 * sfe_ipv4_destroy_all_rules_for_dev()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001386 * Destroy all connections that match a particular device.
1387 *
1388 * If we pass dev as NULL then this destroys all connections.
1389 */
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001390void sfe_ipv4_destroy_all_rules_for_dev(struct net_device *dev)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001391{
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001392 struct sfe_ipv4 *si = &__si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001393 struct sfe_ipv4_connection *c;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301394 bool ret;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001395
Xiaoping Fan34586472015-07-03 02:20:35 -07001396another_round:
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001397 spin_lock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001398
Xiaoping Fan34586472015-07-03 02:20:35 -07001399 for (c = si->all_connections_head; c; c = c->all_connections_next) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001400 /*
Xiaoping Fan34586472015-07-03 02:20:35 -07001401 * Does this connection relate to the device we are destroying?
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001402 */
1403 if (!dev
1404 || (dev == c->original_dev)
1405 || (dev == c->reply_dev)) {
Xiaoping Fan34586472015-07-03 02:20:35 -07001406 break;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001407 }
Xiaoping Fan34586472015-07-03 02:20:35 -07001408 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001409
Xiaoping Fan34586472015-07-03 02:20:35 -07001410 if (c) {
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301411 ret = sfe_ipv4_remove_connection(si, c);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001412 }
1413
1414 spin_unlock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07001415
1416 if (c) {
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301417 if (ret) {
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301418 sfe_ipv4_flush_connection(si, c, SFE_SYNC_REASON_DESTROY);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301419 }
Xiaoping Fan34586472015-07-03 02:20:35 -07001420 goto another_round;
1421 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001422}
1423
1424/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001425 * sfe_ipv4_periodic_sync()
1426 */
Ken Zhu137722d2021-09-23 17:57:36 -07001427static void sfe_ipv4_periodic_sync(struct work_struct *work)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001428{
Ken Zhu137722d2021-09-23 17:57:36 -07001429 struct sfe_ipv4 *si = container_of((struct delayed_work *)work, struct sfe_ipv4, sync_dwork);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07001430 u64 now_jiffies;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001431 int quota;
Xiaoping Fand44a5b42015-05-26 17:37:37 -07001432 sfe_sync_rule_callback_t sync_rule_callback;
Ken Zhudc423672021-09-02 18:27:01 -07001433 struct sfe_ipv4_connection *c;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001434
1435 now_jiffies = get_jiffies_64();
1436
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001437 rcu_read_lock();
1438 sync_rule_callback = rcu_dereference(si->sync_rule_callback);
1439 if (!sync_rule_callback) {
1440 rcu_read_unlock();
1441 goto done;
1442 }
1443
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001444 spin_lock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001445
1446 /*
Ken Zhudc423672021-09-02 18:27:01 -07001447 * If we have reached the end of the connection list, walk from
1448 * the connection head.
1449 */
1450 c = si->wc_next;
1451 if (unlikely(!c)) {
1452 c = si->all_connections_head;
1453 }
1454
1455 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001456 * Get an estimate of the number of connections to parse in this sync.
1457 */
1458 quota = (si->num_connections + 63) / 64;
1459
1460 /*
Ken Zhudc423672021-09-02 18:27:01 -07001461 * Walk the "all connection" list and sync the connection state.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001462 */
Ken Zhudc423672021-09-02 18:27:01 -07001463 while (likely(c && quota)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001464 struct sfe_ipv4_connection_match *cm;
1465 struct sfe_ipv4_connection_match *counter_cm;
Xiaoping Fand44a5b42015-05-26 17:37:37 -07001466 struct sfe_connection_sync sis;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001467
Ken Zhudc423672021-09-02 18:27:01 -07001468 cm = c->original_match;
1469 counter_cm = c->reply_match;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001470
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001471 /*
Ken Zhudc423672021-09-02 18:27:01 -07001472 * Didn't receive packets in the original direction or reply
1473 * direction, move to the next connection.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001474 */
Ken Zhudc423672021-09-02 18:27:01 -07001475 if ((!atomic_read(&cm->rx_packet_count)) && !(atomic_read(&counter_cm->rx_packet_count))) {
1476 c = c->all_connections_next;
1477 continue;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001478 }
1479
Ken Zhudc423672021-09-02 18:27:01 -07001480 quota--;
Matthew McClintockaf48f1e2014-01-23 15:29:19 -06001481
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301482 sfe_ipv4_gen_sync_connection(si, c, &sis, SFE_SYNC_REASON_STATS, now_jiffies);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001483
Ken Zhudc423672021-09-02 18:27:01 -07001484 si->wc_next = c->all_connections_next;
1485
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001486 /*
1487 * We don't want to be holding the lock when we sync!
1488 */
1489 spin_unlock_bh(&si->lock);
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001490 sync_rule_callback(&sis);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001491 spin_lock_bh(&si->lock);
Ken Zhudc423672021-09-02 18:27:01 -07001492
1493 /*
1494 * c must be set and used in the same lock/unlock window;
1495 * because c could be removed when we don't hold the lock,
1496 * so delay grabbing until after the callback and relock.
1497 */
1498 c = si->wc_next;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001499 }
1500
Ken Zhudc423672021-09-02 18:27:01 -07001501 /*
1502 * At the end of the sync, put the wc_next to the connection we left.
1503 */
1504 si->wc_next = c;
1505
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001506 spin_unlock_bh(&si->lock);
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001507 rcu_read_unlock();
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001508
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001509done:
Ken Zhu137722d2021-09-23 17:57:36 -07001510 schedule_delayed_work_on(si->work_cpu, (struct delayed_work *)work, ((HZ + 99) / 100));
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001511}
1512
1513#define CHAR_DEV_MSG_SIZE 768
1514
1515/*
1516 * sfe_ipv4_debug_dev_read_start()
1517 * Generate part of the XML output.
1518 */
1519static bool sfe_ipv4_debug_dev_read_start(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
1520 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
1521{
1522 int bytes_read;
1523
Xiaoping Fan34586472015-07-03 02:20:35 -07001524 si->debug_read_seq++;
1525
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001526 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "<sfe_ipv4>\n");
1527 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1528 return false;
1529 }
1530
1531 *length -= bytes_read;
1532 *total_read += bytes_read;
1533
1534 ws->state++;
1535 return true;
1536}
1537
1538/*
1539 * sfe_ipv4_debug_dev_read_connections_start()
1540 * Generate part of the XML output.
1541 */
1542static bool sfe_ipv4_debug_dev_read_connections_start(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
1543 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
1544{
1545 int bytes_read;
1546
1547 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<connections>\n");
1548 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1549 return false;
1550 }
1551
1552 *length -= bytes_read;
1553 *total_read += bytes_read;
1554
1555 ws->state++;
1556 return true;
1557}
1558
1559/*
1560 * sfe_ipv4_debug_dev_read_connections_connection()
1561 * Generate part of the XML output.
1562 */
1563static bool sfe_ipv4_debug_dev_read_connections_connection(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
1564 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
1565{
1566 struct sfe_ipv4_connection *c;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001567 struct sfe_ipv4_connection_match *original_cm;
1568 struct sfe_ipv4_connection_match *reply_cm;
1569 int bytes_read;
1570 int protocol;
1571 struct net_device *src_dev;
Dave Hudson87973cd2013-10-22 16:00:04 +01001572 __be32 src_ip;
1573 __be32 src_ip_xlate;
1574 __be16 src_port;
1575 __be16 src_port_xlate;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07001576 u64 src_rx_packets;
1577 u64 src_rx_bytes;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001578 struct net_device *dest_dev;
Dave Hudson87973cd2013-10-22 16:00:04 +01001579 __be32 dest_ip;
1580 __be32 dest_ip_xlate;
1581 __be16 dest_port;
1582 __be16 dest_port_xlate;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07001583 u64 dest_rx_packets;
1584 u64 dest_rx_bytes;
1585 u64 last_sync_jiffies;
1586 u32 mark, src_priority, dest_priority, src_dscp, dest_dscp;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301587 u32 packet, byte;
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001588#ifdef CONFIG_NF_FLOW_COOKIE
1589 int src_flow_cookie, dst_flow_cookie;
1590#endif
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001591
1592 spin_lock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07001593
1594 for (c = si->all_connections_head; c; c = c->all_connections_next) {
1595 if (c->debug_read_seq < si->debug_read_seq) {
1596 c->debug_read_seq = si->debug_read_seq;
1597 break;
1598 }
1599 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001600
1601 /*
Xiaoping Fan34586472015-07-03 02:20:35 -07001602 * If there were no connections then move to the next state.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001603 */
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301604 if (!c || c->removed) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001605 spin_unlock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07001606 ws->state++;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001607 return true;
1608 }
1609
1610 original_cm = c->original_match;
1611 reply_cm = c->reply_match;
1612
1613 protocol = c->protocol;
1614 src_dev = c->original_dev;
1615 src_ip = c->src_ip;
1616 src_ip_xlate = c->src_ip_xlate;
1617 src_port = c->src_port;
1618 src_port_xlate = c->src_port_xlate;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001619 src_priority = original_cm->priority;
1620 src_dscp = original_cm->dscp >> SFE_IPV4_DSCP_SHIFT;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001621
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301622 sfe_ipv4_connection_match_update_summary_stats(original_cm, &packet, &byte);
1623 sfe_ipv4_connection_match_update_summary_stats(reply_cm, &packet, &byte);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001624
1625 src_rx_packets = original_cm->rx_packet_count64;
1626 src_rx_bytes = original_cm->rx_byte_count64;
1627 dest_dev = c->reply_dev;
1628 dest_ip = c->dest_ip;
1629 dest_ip_xlate = c->dest_ip_xlate;
1630 dest_port = c->dest_port;
1631 dest_port_xlate = c->dest_port_xlate;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001632 dest_priority = reply_cm->priority;
1633 dest_dscp = reply_cm->dscp >> SFE_IPV4_DSCP_SHIFT;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001634 dest_rx_packets = reply_cm->rx_packet_count64;
1635 dest_rx_bytes = reply_cm->rx_byte_count64;
1636 last_sync_jiffies = get_jiffies_64() - c->last_sync_jiffies;
Cristian Prundeanu592265e2013-12-26 11:01:22 -06001637 mark = c->mark;
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001638#ifdef CONFIG_NF_FLOW_COOKIE
1639 src_flow_cookie = original_cm->flow_cookie;
1640 dst_flow_cookie = reply_cm->flow_cookie;
1641#endif
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001642 spin_unlock_bh(&si->lock);
1643
1644 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t\t<connection "
1645 "protocol=\"%u\" "
1646 "src_dev=\"%s\" "
1647 "src_ip=\"%pI4\" src_ip_xlate=\"%pI4\" "
1648 "src_port=\"%u\" src_port_xlate=\"%u\" "
Xiaoping Fane1963d42015-08-25 17:06:19 -07001649 "src_priority=\"%u\" src_dscp=\"%u\" "
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001650 "src_rx_pkts=\"%llu\" src_rx_bytes=\"%llu\" "
1651 "dest_dev=\"%s\" "
1652 "dest_ip=\"%pI4\" dest_ip_xlate=\"%pI4\" "
1653 "dest_port=\"%u\" dest_port_xlate=\"%u\" "
Xiaoping Fane1963d42015-08-25 17:06:19 -07001654 "dest_priority=\"%u\" dest_dscp=\"%u\" "
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001655 "dest_rx_pkts=\"%llu\" dest_rx_bytes=\"%llu\" "
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001656#ifdef CONFIG_NF_FLOW_COOKIE
1657 "src_flow_cookie=\"%d\" dst_flow_cookie=\"%d\" "
1658#endif
Cristian Prundeanu592265e2013-12-26 11:01:22 -06001659 "last_sync=\"%llu\" "
Nicolas Costabb85a2e2014-01-13 16:26:33 -06001660 "mark=\"%08x\" />\n",
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001661 protocol,
1662 src_dev->name,
1663 &src_ip, &src_ip_xlate,
Dave Hudson87973cd2013-10-22 16:00:04 +01001664 ntohs(src_port), ntohs(src_port_xlate),
Xiaoping Fane1963d42015-08-25 17:06:19 -07001665 src_priority, src_dscp,
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001666 src_rx_packets, src_rx_bytes,
1667 dest_dev->name,
1668 &dest_ip, &dest_ip_xlate,
Dave Hudson87973cd2013-10-22 16:00:04 +01001669 ntohs(dest_port), ntohs(dest_port_xlate),
Xiaoping Fane1963d42015-08-25 17:06:19 -07001670 dest_priority, dest_dscp,
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001671 dest_rx_packets, dest_rx_bytes,
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001672#ifdef CONFIG_NF_FLOW_COOKIE
1673 src_flow_cookie, dst_flow_cookie,
1674#endif
Cristian Prundeanu592265e2013-12-26 11:01:22 -06001675 last_sync_jiffies, mark);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001676
1677 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1678 return false;
1679 }
1680
1681 *length -= bytes_read;
1682 *total_read += bytes_read;
1683
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001684 return true;
1685}
1686
1687/*
1688 * sfe_ipv4_debug_dev_read_connections_end()
1689 * Generate part of the XML output.
1690 */
1691static bool sfe_ipv4_debug_dev_read_connections_end(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
1692 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
1693{
1694 int bytes_read;
1695
1696 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t</connections>\n");
1697 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1698 return false;
1699 }
1700
1701 *length -= bytes_read;
1702 *total_read += bytes_read;
1703
1704 ws->state++;
1705 return true;
1706}
1707
1708/*
1709 * sfe_ipv4_debug_dev_read_exceptions_start()
1710 * Generate part of the XML output.
1711 */
1712static bool sfe_ipv4_debug_dev_read_exceptions_start(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
1713 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
1714{
1715 int bytes_read;
1716
1717 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<exceptions>\n");
1718 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1719 return false;
1720 }
1721
1722 *length -= bytes_read;
1723 *total_read += bytes_read;
1724
1725 ws->state++;
1726 return true;
1727}
1728
1729/*
1730 * sfe_ipv4_debug_dev_read_exceptions_exception()
1731 * Generate part of the XML output.
1732 */
1733static bool sfe_ipv4_debug_dev_read_exceptions_exception(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
1734 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
1735{
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301736 int i;
1737 u64 val = 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001738
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301739 for_each_possible_cpu(i) {
1740 const struct sfe_ipv4_stats *s = per_cpu_ptr(si->stats_pcpu, i);
1741 val += s->exception_events64[ws->iter_exception];
1742 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001743
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301744 if (val) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001745 int bytes_read;
1746
1747 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE,
1748 "\t\t<exception name=\"%s\" count=\"%llu\" />\n",
1749 sfe_ipv4_exception_events_string[ws->iter_exception],
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301750 val);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001751 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1752 return false;
1753 }
1754
1755 *length -= bytes_read;
1756 *total_read += bytes_read;
1757 }
1758
1759 ws->iter_exception++;
1760 if (ws->iter_exception >= SFE_IPV4_EXCEPTION_EVENT_LAST) {
1761 ws->iter_exception = 0;
1762 ws->state++;
1763 }
1764
1765 return true;
1766}
1767
1768/*
1769 * sfe_ipv4_debug_dev_read_exceptions_end()
1770 * Generate part of the XML output.
1771 */
1772static bool sfe_ipv4_debug_dev_read_exceptions_end(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
1773 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
1774{
1775 int bytes_read;
1776
1777 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t</exceptions>\n");
1778 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1779 return false;
1780 }
1781
1782 *length -= bytes_read;
1783 *total_read += bytes_read;
1784
1785 ws->state++;
1786 return true;
1787}
1788
1789/*
1790 * sfe_ipv4_debug_dev_read_stats()
1791 * Generate part of the XML output.
1792 */
1793static bool sfe_ipv4_debug_dev_read_stats(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
1794 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
1795{
1796 int bytes_read;
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301797 struct sfe_ipv4_stats stats;
1798 unsigned int num_conn;
1799
1800 sfe_ipv4_update_summary_stats(si, &stats);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001801
1802 spin_lock_bh(&si->lock);
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301803 num_conn = si->num_connections;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001804 spin_unlock_bh(&si->lock);
1805
1806 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<stats "
1807 "num_connections=\"%u\" "
Xiaoping Fan59176422015-05-22 15:58:10 -07001808 "pkts_forwarded=\"%llu\" pkts_not_forwarded=\"%llu\" "
1809 "create_requests=\"%llu\" create_collisions=\"%llu\" "
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301810 "create_failures=\"%llu\" "
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001811 "destroy_requests=\"%llu\" destroy_misses=\"%llu\" "
1812 "flushes=\"%llu\" "
1813 "hash_hits=\"%llu\" hash_reorders=\"%llu\" />\n",
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301814 num_conn,
1815 stats.packets_forwarded64,
1816 stats.packets_not_forwarded64,
1817 stats.connection_create_requests64,
1818 stats.connection_create_collisions64,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301819 stats.connection_create_failures64,
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301820 stats.connection_destroy_requests64,
1821 stats.connection_destroy_misses64,
1822 stats.connection_flushes64,
1823 stats.connection_match_hash_hits64,
1824 stats.connection_match_hash_reorders64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001825 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1826 return false;
1827 }
1828
1829 *length -= bytes_read;
1830 *total_read += bytes_read;
1831
1832 ws->state++;
1833 return true;
1834}
1835
1836/*
1837 * sfe_ipv4_debug_dev_read_end()
1838 * Generate part of the XML output.
1839 */
1840static bool sfe_ipv4_debug_dev_read_end(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
1841 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
1842{
1843 int bytes_read;
1844
1845 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "</sfe_ipv4>\n");
1846 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1847 return false;
1848 }
1849
1850 *length -= bytes_read;
1851 *total_read += bytes_read;
1852
1853 ws->state++;
1854 return true;
1855}
1856
1857/*
1858 * Array of write functions that write various XML elements that correspond to
1859 * our XML output state machine.
1860 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07001861static 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 +01001862 sfe_ipv4_debug_dev_read_start,
1863 sfe_ipv4_debug_dev_read_connections_start,
1864 sfe_ipv4_debug_dev_read_connections_connection,
1865 sfe_ipv4_debug_dev_read_connections_end,
1866 sfe_ipv4_debug_dev_read_exceptions_start,
1867 sfe_ipv4_debug_dev_read_exceptions_exception,
1868 sfe_ipv4_debug_dev_read_exceptions_end,
1869 sfe_ipv4_debug_dev_read_stats,
1870 sfe_ipv4_debug_dev_read_end,
1871};
1872
1873/*
1874 * sfe_ipv4_debug_dev_read()
1875 * Send info to userspace upon read request from user
1876 */
1877static ssize_t sfe_ipv4_debug_dev_read(struct file *filp, char *buffer, size_t length, loff_t *offset)
1878{
1879 char msg[CHAR_DEV_MSG_SIZE];
1880 int total_read = 0;
1881 struct sfe_ipv4_debug_xml_write_state *ws;
1882 struct sfe_ipv4 *si = &__si;
1883
1884 ws = (struct sfe_ipv4_debug_xml_write_state *)filp->private_data;
1885 while ((ws->state != SFE_IPV4_DEBUG_XML_STATE_DONE) && (length > CHAR_DEV_MSG_SIZE)) {
1886 if ((sfe_ipv4_debug_xml_write_methods[ws->state])(si, buffer, msg, &length, &total_read, ws)) {
1887 continue;
1888 }
1889 }
1890
1891 return total_read;
1892}
1893
1894/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001895 * sfe_ipv4_debug_dev_open()
1896 */
1897static int sfe_ipv4_debug_dev_open(struct inode *inode, struct file *file)
1898{
1899 struct sfe_ipv4_debug_xml_write_state *ws;
1900
1901 ws = (struct sfe_ipv4_debug_xml_write_state *)file->private_data;
1902 if (!ws) {
1903 ws = kzalloc(sizeof(struct sfe_ipv4_debug_xml_write_state), GFP_KERNEL);
1904 if (!ws) {
1905 return -ENOMEM;
1906 }
1907
1908 ws->state = SFE_IPV4_DEBUG_XML_STATE_START;
1909 file->private_data = ws;
1910 }
1911
1912 return 0;
1913}
1914
1915/*
1916 * sfe_ipv4_debug_dev_release()
1917 */
1918static int sfe_ipv4_debug_dev_release(struct inode *inode, struct file *file)
1919{
1920 struct sfe_ipv4_debug_xml_write_state *ws;
1921
1922 ws = (struct sfe_ipv4_debug_xml_write_state *)file->private_data;
1923 if (ws) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001924 /*
1925 * We've finished with our output so free the write state.
1926 */
1927 kfree(ws);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301928 file->private_data = NULL;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001929 }
1930
1931 return 0;
1932}
1933
1934/*
1935 * File operations used in the debug char device
1936 */
1937static struct file_operations sfe_ipv4_debug_dev_fops = {
1938 .read = sfe_ipv4_debug_dev_read,
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001939 .open = sfe_ipv4_debug_dev_open,
1940 .release = sfe_ipv4_debug_dev_release
1941};
1942
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001943#ifdef CONFIG_NF_FLOW_COOKIE
1944/*
1945 * sfe_register_flow_cookie_cb
1946 * register a function in SFE to let SFE use this function to configure flow cookie for a flow
1947 *
1948 * Hardware driver which support flow cookie should register a callback function in SFE. Then SFE
1949 * can use this function to configure flow cookie for a flow.
1950 * return: 0, success; !=0, fail
1951 */
1952int sfe_register_flow_cookie_cb(flow_cookie_set_func_t cb)
1953{
1954 struct sfe_ipv4 *si = &__si;
1955
1956 BUG_ON(!cb);
1957
1958 if (si->flow_cookie_set_func) {
1959 return -1;
1960 }
1961
1962 rcu_assign_pointer(si->flow_cookie_set_func, cb);
1963 return 0;
1964}
1965
1966/*
1967 * sfe_unregister_flow_cookie_cb
1968 * unregister function which is used to configure flow cookie for a flow
1969 *
1970 * return: 0, success; !=0, fail
1971 */
1972int sfe_unregister_flow_cookie_cb(flow_cookie_set_func_t cb)
1973{
1974 struct sfe_ipv4 *si = &__si;
1975
1976 RCU_INIT_POINTER(si->flow_cookie_set_func, NULL);
1977 return 0;
1978}
Xiaoping Fan640faf42015-08-28 15:50:55 -07001979
1980/*
1981 * sfe_ipv4_get_flow_cookie()
1982 */
1983static ssize_t sfe_ipv4_get_flow_cookie(struct device *dev,
1984 struct device_attribute *attr,
1985 char *buf)
1986{
1987 struct sfe_ipv4 *si = &__si;
Xiaoping Fan01c67cc2015-11-09 11:31:57 -08001988 return snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", si->flow_cookie_enable);
Xiaoping Fan640faf42015-08-28 15:50:55 -07001989}
1990
1991/*
1992 * sfe_ipv4_set_flow_cookie()
1993 */
1994static ssize_t sfe_ipv4_set_flow_cookie(struct device *dev,
1995 struct device_attribute *attr,
1996 const char *buf, size_t size)
1997{
1998 struct sfe_ipv4 *si = &__si;
Ken Zhu137722d2021-09-23 17:57:36 -07001999 si->flow_cookie_enable = simple_strtol(buf, NULL, 0);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002000
2001 return size;
2002}
2003
2004/*
2005 * sysfs attributes.
2006 */
2007static const struct device_attribute sfe_ipv4_flow_cookie_attr =
Xiaoping Fane70da412016-02-26 16:47:57 -08002008 __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 -08002009#endif /*CONFIG_NF_FLOW_COOKIE*/
2010
Ken Zhu137722d2021-09-23 17:57:36 -07002011/*
2012 * sfe_ipv4_get_cpu()
2013 */
2014static ssize_t sfe_ipv4_get_cpu(struct device *dev,
2015 struct device_attribute *attr,
2016 char *buf)
2017{
2018 struct sfe_ipv4 *si = &__si;
2019 return snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", si->work_cpu);
2020}
2021
2022/*
2023 * sfe_ipv4_set_cpu()
2024 */
2025static ssize_t sfe_ipv4_set_cpu(struct device *dev,
2026 struct device_attribute *attr,
2027 const char *buf, size_t size)
2028{
2029 struct sfe_ipv4 *si = &__si;
2030 int work_cpu;
2031 work_cpu = simple_strtol(buf, NULL, 0);
2032 if ((work_cpu >= 0) && (work_cpu <= NR_CPUS)) {
2033 si->work_cpu = work_cpu;
2034 } else {
2035 dev_err(dev, "%s is not in valid range[0,%d]", buf, NR_CPUS);
2036 }
2037 return size;
2038}
2039/*
2040 * sysfs attributes.
2041 */
2042static const struct device_attribute sfe_ipv4_cpu_attr =
2043 __ATTR(stats_work_cpu, S_IWUSR | S_IRUGO, sfe_ipv4_get_cpu, sfe_ipv4_set_cpu);
2044
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05302045 /*
2046 * sfe_ipv4_conn_match_hash_init()
2047 * Initialize conn match hash lists
2048 */
2049static void sfe_ipv4_conn_match_hash_init(struct sfe_ipv4 *si, int len)
2050{
2051 struct hlist_head *hash_list = si->hlist_conn_match_hash_head;
2052 int i;
2053
2054 for (i = 0; i < len; i++) {
2055 INIT_HLIST_HEAD(&hash_list[i]);
2056 }
2057}
2058
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002059/*
Dave Hudson87973cd2013-10-22 16:00:04 +01002060 * sfe_ipv4_init()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002061 */
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05302062int sfe_ipv4_init(void)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002063{
2064 struct sfe_ipv4 *si = &__si;
2065 int result = -1;
2066
Dave Hudsondcd08fb2013-11-22 09:25:16 -06002067 DEBUG_INFO("SFE IPv4 init\n");
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002068
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05302069 sfe_ipv4_conn_match_hash_init(si, ARRAY_SIZE(si->hlist_conn_match_hash_head));
2070
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302071 si->stats_pcpu = alloc_percpu_gfp(struct sfe_ipv4_stats, GFP_KERNEL | __GFP_ZERO);
2072 if (!si->stats_pcpu) {
2073 DEBUG_ERROR("failed to allocate stats memory for sfe_ipv4\n");
2074 goto exit0;
2075 }
2076
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002077 /*
2078 * Create sys/sfe_ipv4
2079 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302080 si->sys_ipv4 = kobject_create_and_add("sfe_ipv4", NULL);
2081 if (!si->sys_ipv4) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002082 DEBUG_ERROR("failed to register sfe_ipv4\n");
2083 goto exit1;
2084 }
2085
2086 /*
2087 * Create files, one for each parameter supported by this module.
2088 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302089 result = sysfs_create_file(si->sys_ipv4, &sfe_ipv4_debug_dev_attr.attr);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002090 if (result) {
2091 DEBUG_ERROR("failed to register debug dev file: %d\n", result);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002092 goto exit2;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002093 }
2094
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302095 result = sysfs_create_file(si->sys_ipv4, &sfe_ipv4_cpu_attr.attr);
Ken Zhu137722d2021-09-23 17:57:36 -07002096 if (result) {
2097 DEBUG_ERROR("failed to register debug dev file: %d\n", result);
2098 goto exit3;
2099 }
2100
Xiaoping Fan640faf42015-08-28 15:50:55 -07002101#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302102 result = sysfs_create_file(si->sys_ipv4, &sfe_ipv4_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002103 if (result) {
2104 DEBUG_ERROR("failed to register flow cookie enable file: %d\n", result);
Ken Zhu137722d2021-09-23 17:57:36 -07002105 goto exit4;
Xiaoping Fan640faf42015-08-28 15:50:55 -07002106 }
2107#endif /* CONFIG_NF_FLOW_COOKIE */
2108
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002109 /*
2110 * Register our debug char device.
2111 */
2112 result = register_chrdev(0, "sfe_ipv4", &sfe_ipv4_debug_dev_fops);
2113 if (result < 0) {
2114 DEBUG_ERROR("Failed to register chrdev: %d\n", result);
Ken Zhu137722d2021-09-23 17:57:36 -07002115 goto exit5;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002116 }
2117
2118 si->debug_dev = result;
Ken Zhu137722d2021-09-23 17:57:36 -07002119 si->work_cpu = WORK_CPU_UNBOUND;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002120
2121 /*
Ken Zhu137722d2021-09-23 17:57:36 -07002122 * Create a work to handle periodic statistics.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002123 */
Ken Zhu137722d2021-09-23 17:57:36 -07002124 INIT_DELAYED_WORK(&(si->sync_dwork), sfe_ipv4_periodic_sync);
2125 schedule_delayed_work_on(si->work_cpu, &(si->sync_dwork), ((HZ + 99) / 100));
2126
Dave Hudson87973cd2013-10-22 16:00:04 +01002127 spin_lock_init(&si->lock);
Dave Hudson87973cd2013-10-22 16:00:04 +01002128 return 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002129
Ken Zhu137722d2021-09-23 17:57:36 -07002130exit5:
Xiaoping Fan640faf42015-08-28 15:50:55 -07002131#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302132 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002133
Ken Zhu137722d2021-09-23 17:57:36 -07002134exit4:
Xiaoping Fan640faf42015-08-28 15:50:55 -07002135#endif /* CONFIG_NF_FLOW_COOKIE */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302136 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_cpu_attr.attr);
Ken Zhu137722d2021-09-23 17:57:36 -07002137exit3:
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302138 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_debug_dev_attr.attr);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002139
Xiaoping Fan640faf42015-08-28 15:50:55 -07002140exit2:
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302141 kobject_put(si->sys_ipv4);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002142
2143exit1:
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302144 free_percpu(si->stats_pcpu);
2145
2146exit0:
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002147 return result;
2148}
2149
2150/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002151 * sfe_ipv4_exit()
2152 */
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05302153void sfe_ipv4_exit(void)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002154{
Dave Hudson87973cd2013-10-22 16:00:04 +01002155 struct sfe_ipv4 *si = &__si;
2156
Dave Hudsondcd08fb2013-11-22 09:25:16 -06002157 DEBUG_INFO("SFE IPv4 exit\n");
Dave Hudson87973cd2013-10-22 16:00:04 +01002158 /*
2159 * Destroy all connections.
2160 */
Dave Hudsondcd08fb2013-11-22 09:25:16 -06002161 sfe_ipv4_destroy_all_rules_for_dev(NULL);
Dave Hudson87973cd2013-10-22 16:00:04 +01002162
Ken Zhu137722d2021-09-23 17:57:36 -07002163 cancel_delayed_work_sync(&si->sync_dwork);
Dave Hudson87973cd2013-10-22 16:00:04 +01002164
Dave Hudson87973cd2013-10-22 16:00:04 +01002165 unregister_chrdev(si->debug_dev, "sfe_ipv4");
2166
Xiaoping Fan640faf42015-08-28 15:50:55 -07002167#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302168 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002169#endif /* CONFIG_NF_FLOW_COOKIE */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302170 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_debug_dev_attr.attr);
2171 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_cpu_attr.attr);
Dave Hudson87973cd2013-10-22 16:00:04 +01002172
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302173 kobject_put(si->sys_ipv4);
Dave Hudson87973cd2013-10-22 16:00:04 +01002174
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302175 free_percpu(si->stats_pcpu);
2176
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002177}
2178
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08002179#ifdef CONFIG_NF_FLOW_COOKIE
2180EXPORT_SYMBOL(sfe_register_flow_cookie_cb);
2181EXPORT_SYMBOL(sfe_unregister_flow_cookie_cb);
2182#endif