blob: e53cef534639b4fceb4b07d11d17d87181600975 [file] [log] [blame]
Xiaoping Fan978b3772015-05-27 14:15:18 -07001/*
2 * sfe_ipv6.c
3 * Shortcut forwarding engine - IPv6 support.
4 *
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05305 * Copyright (c) 2015-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.
Xiaoping Fan978b3772015-05-27 14:15:18 -070019 */
20
21#include <linux/module.h>
22#include <linux/sysfs.h>
23#include <linux/skbuff.h>
24#include <linux/icmp.h>
25#include <net/tcp.h>
26#include <linux/etherdevice.h>
Tian Yang45f39c82020-10-06 14:07:47 -070027#include <linux/version.h>
Xiaoping Fan978b3772015-05-27 14:15:18 -070028
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +053029#include "sfe_debug.h"
Ratheesh Kannoth89302a72021-10-20 08:10:37 +053030#include "sfe_api.h"
Xiaoping Fan978b3772015-05-27 14:15:18 -070031#include "sfe.h"
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +053032#include "sfe_flow_cookie.h"
33#include "sfe_ipv6.h"
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +053034#include "sfe_ipv6_udp.h"
35#include "sfe_ipv6_tcp.h"
36#include "sfe_ipv6_icmp.h"
Xiaoping Fan978b3772015-05-27 14:15:18 -070037
Ratheesh Kannoth89302a72021-10-20 08:10:37 +053038#define sfe_ipv6_addr_copy(src, dest) memcpy((void *)(dest), (void *)(src), 16)
39
Xiaoping Fan978b3772015-05-27 14:15:18 -070040static char *sfe_ipv6_exception_events_string[SFE_IPV6_EXCEPTION_EVENT_LAST] = {
41 "UDP_HEADER_INCOMPLETE",
42 "UDP_NO_CONNECTION",
43 "UDP_IP_OPTIONS_OR_INITIAL_FRAGMENT",
44 "UDP_SMALL_TTL",
45 "UDP_NEEDS_FRAGMENTATION",
46 "TCP_HEADER_INCOMPLETE",
47 "TCP_NO_CONNECTION_SLOW_FLAGS",
48 "TCP_NO_CONNECTION_FAST_FLAGS",
49 "TCP_IP_OPTIONS_OR_INITIAL_FRAGMENT",
50 "TCP_SMALL_TTL",
51 "TCP_NEEDS_FRAGMENTATION",
52 "TCP_FLAGS",
53 "TCP_SEQ_EXCEEDS_RIGHT_EDGE",
54 "TCP_SMALL_DATA_OFFS",
55 "TCP_BAD_SACK",
56 "TCP_BIG_DATA_OFFS",
57 "TCP_SEQ_BEFORE_LEFT_EDGE",
58 "TCP_ACK_EXCEEDS_RIGHT_EDGE",
59 "TCP_ACK_BEFORE_LEFT_EDGE",
60 "ICMP_HEADER_INCOMPLETE",
61 "ICMP_UNHANDLED_TYPE",
62 "ICMP_IPV6_HEADER_INCOMPLETE",
63 "ICMP_IPV6_NON_V6",
64 "ICMP_IPV6_IP_OPTIONS_INCOMPLETE",
65 "ICMP_IPV6_UDP_HEADER_INCOMPLETE",
66 "ICMP_IPV6_TCP_HEADER_INCOMPLETE",
67 "ICMP_IPV6_UNHANDLED_PROTOCOL",
68 "ICMP_NO_CONNECTION",
69 "ICMP_FLUSHED_CONNECTION",
70 "HEADER_INCOMPLETE",
71 "BAD_TOTAL_LENGTH",
72 "NON_V6",
73 "NON_INITIAL_FRAGMENT",
74 "DATAGRAM_INCOMPLETE",
75 "IP_OPTIONS_INCOMPLETE",
76 "UNHANDLED_PROTOCOL",
77 "FLOW_COOKIE_ADD_FAIL"
78};
79
Xiaoping Fan6a1672f2016-08-17 19:58:12 -070080static struct sfe_ipv6 __si6;
Xiaoping Fan978b3772015-05-27 14:15:18 -070081
82/*
83 * sfe_ipv6_get_debug_dev()
84 */
85static ssize_t sfe_ipv6_get_debug_dev(struct device *dev, struct device_attribute *attr, char *buf);
86
87/*
88 * sysfs attributes.
89 */
90static const struct device_attribute sfe_ipv6_debug_dev_attr =
Xiaoping Fane70da412016-02-26 16:47:57 -080091 __ATTR(debug_dev, S_IWUSR | S_IRUGO, sfe_ipv6_get_debug_dev, NULL);
Xiaoping Fan978b3772015-05-27 14:15:18 -070092
93/*
Xiaoping Fan978b3772015-05-27 14:15:18 -070094 * sfe_ipv6_get_connection_match_hash()
95 * Generate the hash used in connection match lookups.
96 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -070097static inline unsigned int sfe_ipv6_get_connection_match_hash(struct net_device *dev, u8 protocol,
Xiaoping Fan978b3772015-05-27 14:15:18 -070098 struct sfe_ipv6_addr *src_ip, __be16 src_port,
99 struct sfe_ipv6_addr *dest_ip, __be16 dest_port)
100{
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700101 u32 idx, hash = 0;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700102 size_t dev_addr = (size_t)dev;
103
104 for (idx = 0; idx < 4; idx++) {
105 hash ^= src_ip->addr[idx] ^ dest_ip->addr[idx];
106 }
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700107 hash = ((u32)dev_addr) ^ hash ^ protocol ^ ntohs(src_port ^ dest_port);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700108 return ((hash >> SFE_IPV6_CONNECTION_HASH_SHIFT) ^ hash) & SFE_IPV6_CONNECTION_HASH_MASK;
109}
110
111/*
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530112 * sfe_ipv6_find_connection_match_rcu()
Xiaoping Fan978b3772015-05-27 14:15:18 -0700113 * Get the IPv6 flow match info that corresponds to a particular 5-tuple.
Xiaoping Fan978b3772015-05-27 14:15:18 -0700114 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530115struct sfe_ipv6_connection_match *
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530116sfe_ipv6_find_connection_match_rcu(struct sfe_ipv6 *si, struct net_device *dev, u8 protocol,
Xiaoping Fan978b3772015-05-27 14:15:18 -0700117 struct sfe_ipv6_addr *src_ip, __be16 src_port,
118 struct sfe_ipv6_addr *dest_ip, __be16 dest_port)
119{
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530120 struct sfe_ipv6_connection_match *cm = NULL;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700121 unsigned int conn_match_idx;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530122 struct hlist_head *lhead;
123 WARN_ON_ONCE(!rcu_read_lock_held());
Xiaoping Fan978b3772015-05-27 14:15:18 -0700124
125 conn_match_idx = sfe_ipv6_get_connection_match_hash(dev, protocol, src_ip, src_port, dest_ip, dest_port);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700126
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530127 lhead = &si->hlist_conn_match_hash_head[conn_match_idx];
Xiaoping Fan978b3772015-05-27 14:15:18 -0700128
129 /*
130 * Hopefully the first entry is the one we want.
131 */
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530132 hlist_for_each_entry_rcu(cm, lhead, hnode) {
133 if ((cm->match_dest_port != dest_port) ||
134 (!sfe_ipv6_addr_equal(cm->match_src_ip, src_ip)) ||
135 (!sfe_ipv6_addr_equal(cm->match_dest_ip, dest_ip)) ||
136 (cm->match_protocol != protocol) ||
137 (cm->match_dev != dev)) {
138 continue;
139 }
140
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530141 this_cpu_inc(si->stats_pcpu->connection_match_hash_hits64);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700142
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530143 break;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700144
Xiaoping Fan978b3772015-05-27 14:15:18 -0700145 }
146
Xiaoping Fan978b3772015-05-27 14:15:18 -0700147 return cm;
148}
149
150/*
151 * sfe_ipv6_connection_match_update_summary_stats()
152 * Update the summary stats for a connection match entry.
153 */
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530154static inline void sfe_ipv6_connection_match_update_summary_stats(struct sfe_ipv6_connection_match *cm,
155 u32 *packets, u32 *bytes)
156
Xiaoping Fan978b3772015-05-27 14:15:18 -0700157{
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530158 u32 packet_count, byte_count;
159
160 packet_count = atomic_read(&cm->rx_packet_count);
161 cm->rx_packet_count64 += packet_count;
162 atomic_sub(packet_count, &cm->rx_packet_count);
163
164 byte_count = atomic_read(&cm->rx_byte_count);
165 cm->rx_byte_count64 += byte_count;
166 atomic_sub(byte_count, &cm->rx_byte_count);
167
168 *packets = packet_count;
169 *bytes = byte_count;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700170}
171
172/*
173 * sfe_ipv6_connection_match_compute_translations()
174 * Compute port and address translations for a connection match entry.
175 */
176static void sfe_ipv6_connection_match_compute_translations(struct sfe_ipv6_connection_match *cm)
177{
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700178 u32 diff[9];
179 u32 *idx_32;
180 u16 *idx_16;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700181
182 /*
183 * Before we insert the entry look to see if this is tagged as doing address
184 * translations. If it is then work out the adjustment that we need to apply
185 * to the transport checksum.
186 */
187 if (cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_XLATE_SRC) {
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700188 u32 adj = 0;
189 u32 carry = 0;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700190
191 /*
192 * Precompute an incremental checksum adjustment so we can
193 * edit packets in this stream very quickly. The algorithm is from RFC1624.
194 */
195 idx_32 = diff;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530196 *(idx_32++) = cm->match_src_ip[0].addr[0];
197 *(idx_32++) = cm->match_src_ip[0].addr[1];
198 *(idx_32++) = cm->match_src_ip[0].addr[2];
199 *(idx_32++) = cm->match_src_ip[0].addr[3];
Xiaoping Fan978b3772015-05-27 14:15:18 -0700200
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700201 idx_16 = (u16 *)idx_32;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700202 *(idx_16++) = cm->match_src_port;
203 *(idx_16++) = ~cm->xlate_src_port;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700204 idx_32 = (u32 *)idx_16;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700205
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530206 *(idx_32++) = ~cm->xlate_src_ip[0].addr[0];
207 *(idx_32++) = ~cm->xlate_src_ip[0].addr[1];
208 *(idx_32++) = ~cm->xlate_src_ip[0].addr[2];
209 *(idx_32++) = ~cm->xlate_src_ip[0].addr[3];
Xiaoping Fan978b3772015-05-27 14:15:18 -0700210
211 /*
212 * When we compute this fold it down to a 16-bit offset
213 * as that way we can avoid having to do a double
214 * folding of the twos-complement result because the
215 * addition of 2 16-bit values cannot cause a double
216 * wrap-around!
217 */
218 for (idx_32 = diff; idx_32 < diff + 9; idx_32++) {
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700219 u32 w = *idx_32;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700220 adj += carry;
221 adj += w;
222 carry = (w > adj);
223 }
224 adj += carry;
225 adj = (adj & 0xffff) + (adj >> 16);
226 adj = (adj & 0xffff) + (adj >> 16);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700227 cm->xlate_src_csum_adjustment = (u16)adj;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700228 }
229
230 if (cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_XLATE_DEST) {
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700231 u32 adj = 0;
232 u32 carry = 0;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700233
234 /*
235 * Precompute an incremental checksum adjustment so we can
236 * edit packets in this stream very quickly. The algorithm is from RFC1624.
237 */
238 idx_32 = diff;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530239 *(idx_32++) = cm->match_dest_ip[0].addr[0];
240 *(idx_32++) = cm->match_dest_ip[0].addr[1];
241 *(idx_32++) = cm->match_dest_ip[0].addr[2];
242 *(idx_32++) = cm->match_dest_ip[0].addr[3];
Xiaoping Fan978b3772015-05-27 14:15:18 -0700243
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700244 idx_16 = (u16 *)idx_32;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700245 *(idx_16++) = cm->match_dest_port;
246 *(idx_16++) = ~cm->xlate_dest_port;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700247 idx_32 = (u32 *)idx_16;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700248
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530249 *(idx_32++) = ~cm->xlate_dest_ip[0].addr[0];
250 *(idx_32++) = ~cm->xlate_dest_ip[0].addr[1];
251 *(idx_32++) = ~cm->xlate_dest_ip[0].addr[2];
252 *(idx_32++) = ~cm->xlate_dest_ip[0].addr[3];
Xiaoping Fan978b3772015-05-27 14:15:18 -0700253
254 /*
255 * When we compute this fold it down to a 16-bit offset
256 * as that way we can avoid having to do a double
257 * folding of the twos-complement result because the
258 * addition of 2 16-bit values cannot cause a double
259 * wrap-around!
260 */
261 for (idx_32 = diff; idx_32 < diff + 9; idx_32++) {
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700262 u32 w = *idx_32;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700263 adj += carry;
264 adj += w;
265 carry = (w > adj);
266 }
267 adj += carry;
268 adj = (adj & 0xffff) + (adj >> 16);
269 adj = (adj & 0xffff) + (adj >> 16);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700270 cm->xlate_dest_csum_adjustment = (u16)adj;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700271 }
272}
273
274/*
275 * sfe_ipv6_update_summary_stats()
276 * Update the summary stats.
277 */
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530278static void sfe_ipv6_update_summary_stats(struct sfe_ipv6 *si, struct sfe_ipv6_stats *stats)
Xiaoping Fan978b3772015-05-27 14:15:18 -0700279{
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530280 int i = 0;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700281
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530282 memset(stats, 0, sizeof(*stats));
Xiaoping Fan978b3772015-05-27 14:15:18 -0700283
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530284 for_each_possible_cpu(i) {
285 const struct sfe_ipv6_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 Kannoth1ed95462021-10-20 07:57:45 +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;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700297 }
298}
299
300/*
301 * sfe_ipv6_insert_connection_match()
302 * Insert a connection match into the hash.
303 *
304 * On entry we must be holding the lock that protects the hash table.
305 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700306static inline void sfe_ipv6_insert_connection_match(struct sfe_ipv6 *si,
307 struct sfe_ipv6_connection_match *cm)
Xiaoping Fan978b3772015-05-27 14:15:18 -0700308{
Xiaoping Fan978b3772015-05-27 14:15:18 -0700309 unsigned int conn_match_idx
310 = sfe_ipv6_get_connection_match_hash(cm->match_dev, cm->match_protocol,
311 cm->match_src_ip, cm->match_src_port,
312 cm->match_dest_ip, cm->match_dest_port);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700313
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530314 lockdep_assert_held(&si->lock);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700315
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530316 hlist_add_head_rcu(&cm->hnode, &si->hlist_conn_match_hash_head[conn_match_idx]);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700317#ifdef CONFIG_NF_FLOW_COOKIE
Xiaoping Fan640faf42015-08-28 15:50:55 -0700318 if (!si->flow_cookie_enable || !(cm->flags & (SFE_IPV6_CONNECTION_MATCH_FLAG_XLATE_SRC | SFE_IPV6_CONNECTION_MATCH_FLAG_XLATE_DEST)))
Xiaoping Fan978b3772015-05-27 14:15:18 -0700319 return;
320
321 /*
322 * Configure hardware to put a flow cookie in packet of this flow,
323 * then we can accelerate the lookup process when we received this packet.
324 */
325 for (conn_match_idx = 1; conn_match_idx < SFE_FLOW_COOKIE_SIZE; conn_match_idx++) {
326 struct sfe_ipv6_flow_cookie_entry *entry = &si->sfe_flow_cookie_table[conn_match_idx];
327
328 if ((NULL == entry->match) && time_is_before_jiffies(entry->last_clean_time + HZ)) {
329 sfe_ipv6_flow_cookie_set_func_t func;
330
331 rcu_read_lock();
332 func = rcu_dereference(si->flow_cookie_set_func);
333 if (func) {
334 if (!func(cm->match_protocol, cm->match_src_ip->addr, cm->match_src_port,
335 cm->match_dest_ip->addr, cm->match_dest_port, conn_match_idx)) {
336 entry->match = cm;
337 cm->flow_cookie = conn_match_idx;
338 } else {
339 si->exception_events[SFE_IPV6_EXCEPTION_EVENT_FLOW_COOKIE_ADD_FAIL]++;
340 }
341 }
342 rcu_read_unlock();
343
344 break;
345 }
346 }
347#endif
Xiaoping Fan978b3772015-05-27 14:15:18 -0700348}
349
350/*
351 * sfe_ipv6_remove_connection_match()
352 * Remove a connection match object from the hash.
Xiaoping Fan978b3772015-05-27 14:15:18 -0700353 */
354static inline void sfe_ipv6_remove_connection_match(struct sfe_ipv6 *si, struct sfe_ipv6_connection_match *cm)
355{
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530356
357 lockdep_assert_held(&si->lock);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700358#ifdef CONFIG_NF_FLOW_COOKIE
Xiaoping Fan640faf42015-08-28 15:50:55 -0700359 if (si->flow_cookie_enable) {
360 /*
361 * Tell hardware that we no longer need a flow cookie in packet of this flow
362 */
363 unsigned int conn_match_idx;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700364
Xiaoping Fan640faf42015-08-28 15:50:55 -0700365 for (conn_match_idx = 1; conn_match_idx < SFE_FLOW_COOKIE_SIZE; conn_match_idx++) {
366 struct sfe_ipv6_flow_cookie_entry *entry = &si->sfe_flow_cookie_table[conn_match_idx];
Xiaoping Fan978b3772015-05-27 14:15:18 -0700367
Xiaoping Fan640faf42015-08-28 15:50:55 -0700368 if (cm == entry->match) {
369 sfe_ipv6_flow_cookie_set_func_t func;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700370
Xiaoping Fan640faf42015-08-28 15:50:55 -0700371 rcu_read_lock();
372 func = rcu_dereference(si->flow_cookie_set_func);
373 if (func) {
374 func(cm->match_protocol, cm->match_src_ip->addr, cm->match_src_port,
375 cm->match_dest_ip->addr, cm->match_dest_port, 0);
376 }
377 rcu_read_unlock();
378
379 cm->flow_cookie = 0;
380 entry->match = NULL;
381 entry->last_clean_time = jiffies;
382 break;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700383 }
Xiaoping Fan978b3772015-05-27 14:15:18 -0700384 }
385 }
386#endif
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530387 hlist_del_init_rcu(&cm->hnode);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700388
Xiaoping Fan978b3772015-05-27 14:15:18 -0700389}
390
391/*
392 * sfe_ipv6_get_connection_hash()
393 * Generate the hash used in connection lookups.
394 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700395static inline unsigned int sfe_ipv6_get_connection_hash(u8 protocol, struct sfe_ipv6_addr *src_ip, __be16 src_port,
Xiaoping Fan978b3772015-05-27 14:15:18 -0700396 struct sfe_ipv6_addr *dest_ip, __be16 dest_port)
397{
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700398 u32 idx, hash = 0;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700399
400 for (idx = 0; idx < 4; idx++) {
401 hash ^= src_ip->addr[idx] ^ dest_ip->addr[idx];
402 }
403 hash = hash ^ protocol ^ ntohs(src_port ^ dest_port);
404 return ((hash >> SFE_IPV6_CONNECTION_HASH_SHIFT) ^ hash) & SFE_IPV6_CONNECTION_HASH_MASK;
405}
406
407/*
408 * sfe_ipv6_find_connection()
409 * Get the IPv6 connection info that corresponds to a particular 5-tuple.
410 *
411 * On entry we must be holding the lock that protects the hash table.
412 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700413static inline struct sfe_ipv6_connection *sfe_ipv6_find_connection(struct sfe_ipv6 *si, u32 protocol,
Xiaoping Fan978b3772015-05-27 14:15:18 -0700414 struct sfe_ipv6_addr *src_ip, __be16 src_port,
415 struct sfe_ipv6_addr *dest_ip, __be16 dest_port)
416{
417 struct sfe_ipv6_connection *c;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530418
Xiaoping Fan978b3772015-05-27 14:15:18 -0700419 unsigned int conn_idx = sfe_ipv6_get_connection_hash(protocol, src_ip, src_port, dest_ip, dest_port);
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530420
421 lockdep_assert_held(&si->lock);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700422 c = si->conn_hash[conn_idx];
423
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530424 while (c) {
425 if ((c->src_port == src_port)
426 && (c->dest_port == dest_port)
427 && (sfe_ipv6_addr_equal(c->src_ip, src_ip))
428 && (sfe_ipv6_addr_equal(c->dest_ip, dest_ip))
429 && (c->protocol == protocol)) {
430 return c;
431 }
Xiaoping Fan978b3772015-05-27 14:15:18 -0700432 c = c->next;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530433 }
Xiaoping Fan978b3772015-05-27 14:15:18 -0700434
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530435 return NULL;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700436}
437
438/*
439 * sfe_ipv6_mark_rule()
440 * Updates the mark for a current offloaded connection
441 *
442 * Will take hash lock upon entry
443 */
444void sfe_ipv6_mark_rule(struct sfe_connection_mark *mark)
445{
446 struct sfe_ipv6 *si = &__si6;
447 struct sfe_ipv6_connection *c;
448
Xiaoping Fan3c423e32015-07-03 03:09:29 -0700449 spin_lock_bh(&si->lock);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700450 c = sfe_ipv6_find_connection(si, mark->protocol,
451 mark->src_ip.ip6, mark->src_port,
452 mark->dest_ip.ip6, mark->dest_port);
453 if (c) {
Xiaoping Fan978b3772015-05-27 14:15:18 -0700454 WARN_ON((0 != c->mark) && (0 == mark->mark));
455 c->mark = mark->mark;
456 }
Xiaoping Fan3c423e32015-07-03 03:09:29 -0700457 spin_unlock_bh(&si->lock);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700458
459 if (c) {
460 DEBUG_TRACE("Matching connection found for mark, "
461 "setting from %08x to %08x\n",
462 c->mark, mark->mark);
463 }
Xiaoping Fan978b3772015-05-27 14:15:18 -0700464}
465
466/*
467 * sfe_ipv6_insert_connection()
468 * Insert a connection into the hash.
469 *
470 * On entry we must be holding the lock that protects the hash table.
471 */
472static void sfe_ipv6_insert_connection(struct sfe_ipv6 *si, struct sfe_ipv6_connection *c)
473{
474 struct sfe_ipv6_connection **hash_head;
475 struct sfe_ipv6_connection *prev_head;
476 unsigned int conn_idx;
477
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530478 lockdep_assert_held(&si->lock);
479
Xiaoping Fan978b3772015-05-27 14:15:18 -0700480 /*
481 * Insert entry into the connection hash.
482 */
483 conn_idx = sfe_ipv6_get_connection_hash(c->protocol, c->src_ip, c->src_port,
484 c->dest_ip, c->dest_port);
485 hash_head = &si->conn_hash[conn_idx];
486 prev_head = *hash_head;
487 c->prev = NULL;
488 if (prev_head) {
489 prev_head->prev = c;
490 }
491
492 c->next = prev_head;
493 *hash_head = c;
494
495 /*
496 * Insert entry into the "all connections" list.
497 */
498 if (si->all_connections_tail) {
499 c->all_connections_prev = si->all_connections_tail;
500 si->all_connections_tail->all_connections_next = c;
501 } else {
502 c->all_connections_prev = NULL;
503 si->all_connections_head = c;
504 }
505
506 si->all_connections_tail = c;
507 c->all_connections_next = NULL;
508 si->num_connections++;
509
510 /*
511 * Insert the connection match objects too.
512 */
513 sfe_ipv6_insert_connection_match(si, c->original_match);
514 sfe_ipv6_insert_connection_match(si, c->reply_match);
515}
516
517/*
518 * sfe_ipv6_remove_connection()
519 * Remove a sfe_ipv6_connection object from the hash.
520 *
521 * On entry we must be holding the lock that protects the hash table.
522 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530523bool sfe_ipv6_remove_connection(struct sfe_ipv6 *si, struct sfe_ipv6_connection *c)
Xiaoping Fan978b3772015-05-27 14:15:18 -0700524{
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530525
526 lockdep_assert_held(&si->lock);
527 if (c->removed) {
528 DEBUG_ERROR("%px: Connection has been removed already\n", c);
529 return false;
530 }
531
Xiaoping Fan978b3772015-05-27 14:15:18 -0700532 /*
533 * Remove the connection match objects.
534 */
535 sfe_ipv6_remove_connection_match(si, c->reply_match);
536 sfe_ipv6_remove_connection_match(si, c->original_match);
537
538 /*
539 * Unlink the connection.
540 */
541 if (c->prev) {
542 c->prev->next = c->next;
543 } else {
544 unsigned int conn_idx = sfe_ipv6_get_connection_hash(c->protocol, c->src_ip, c->src_port,
545 c->dest_ip, c->dest_port);
546 si->conn_hash[conn_idx] = c->next;
547 }
548
549 if (c->next) {
550 c->next->prev = c->prev;
551 }
Xiaoping Fan34586472015-07-03 02:20:35 -0700552
553 /*
554 * Unlink connection from all_connections list
555 */
556 if (c->all_connections_prev) {
557 c->all_connections_prev->all_connections_next = c->all_connections_next;
558 } else {
559 si->all_connections_head = c->all_connections_next;
560 }
561
562 if (c->all_connections_next) {
563 c->all_connections_next->all_connections_prev = c->all_connections_prev;
564 } else {
565 si->all_connections_tail = c->all_connections_prev;
566 }
567
Ken Zhu32b95392021-09-03 13:52:04 -0700568 /*
569 * If I am the next sync connection, move the sync to my next or head.
570 */
571 if (unlikely(si->wc_next == c)) {
572 si->wc_next = c->all_connections_next;
573 }
574
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530575 c->removed = true;
Xiaoping Fan34586472015-07-03 02:20:35 -0700576 si->num_connections--;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530577 return true;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700578}
579
580/*
581 * sfe_ipv6_gen_sync_connection()
582 * Sync a connection.
583 *
584 * On entry to this function we expect that the lock for the connection is either
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530585 * already held (while called from sfe_ipv6_periodic_sync() or isn't required
586 * (while called from sfe_ipv6_flush_sfe_ipv6_connection())
Xiaoping Fan978b3772015-05-27 14:15:18 -0700587 */
588static void sfe_ipv6_gen_sync_connection(struct sfe_ipv6 *si, struct sfe_ipv6_connection *c,
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700589 struct sfe_connection_sync *sis, sfe_sync_reason_t reason,
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700590 u64 now_jiffies)
Xiaoping Fan978b3772015-05-27 14:15:18 -0700591{
592 struct sfe_ipv6_connection_match *original_cm;
593 struct sfe_ipv6_connection_match *reply_cm;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530594 u32 packet_count, byte_count;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700595
596 /*
597 * Fill in the update message.
598 */
Murat Sezgin53509a12016-12-27 16:57:34 -0800599 sis->is_v6 = 1;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700600 sis->protocol = c->protocol;
601 sis->src_ip.ip6[0] = c->src_ip[0];
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700602 sis->src_ip_xlate.ip6[0] = c->src_ip_xlate[0];
Xiaoping Fan978b3772015-05-27 14:15:18 -0700603 sis->dest_ip.ip6[0] = c->dest_ip[0];
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700604 sis->dest_ip_xlate.ip6[0] = c->dest_ip_xlate[0];
Xiaoping Fan978b3772015-05-27 14:15:18 -0700605 sis->src_port = c->src_port;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700606 sis->src_port_xlate = c->src_port_xlate;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700607 sis->dest_port = c->dest_port;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700608 sis->dest_port_xlate = c->dest_port_xlate;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700609
610 original_cm = c->original_match;
611 reply_cm = c->reply_match;
612 sis->src_td_max_window = original_cm->protocol_state.tcp.max_win;
613 sis->src_td_end = original_cm->protocol_state.tcp.end;
614 sis->src_td_max_end = original_cm->protocol_state.tcp.max_end;
615 sis->dest_td_max_window = reply_cm->protocol_state.tcp.max_win;
616 sis->dest_td_end = reply_cm->protocol_state.tcp.end;
617 sis->dest_td_max_end = reply_cm->protocol_state.tcp.max_end;
618
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530619 sfe_ipv6_connection_match_update_summary_stats(original_cm, &packet_count, &byte_count);
620 sis->src_new_packet_count = packet_count;
621 sis->src_new_byte_count = byte_count;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700622
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530623 sfe_ipv6_connection_match_update_summary_stats(reply_cm, &packet_count, &byte_count);
624 sis->dest_new_packet_count = packet_count;
625 sis->dest_new_byte_count = byte_count;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700626
627 sis->src_dev = original_cm->match_dev;
628 sis->src_packet_count = original_cm->rx_packet_count64;
629 sis->src_byte_count = original_cm->rx_byte_count64;
630
631 sis->dest_dev = reply_cm->match_dev;
632 sis->dest_packet_count = reply_cm->rx_packet_count64;
633 sis->dest_byte_count = reply_cm->rx_byte_count64;
634
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700635 sis->reason = reason;
636
Xiaoping Fan978b3772015-05-27 14:15:18 -0700637 /*
638 * Get the time increment since our last sync.
639 */
640 sis->delta_jiffies = now_jiffies - c->last_sync_jiffies;
641 c->last_sync_jiffies = now_jiffies;
642}
643
644/*
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530645 * sfe_ipv6_free_sfe_ipv6_connection_rcu()
646 * Called at RCU qs state to free the connection object.
647 */
648static void sfe_ipv6_free_sfe_ipv6_connection_rcu(struct rcu_head *head)
649{
650 struct sfe_ipv6_connection *c;
651
652 /*
653 * We dont need spin lock as the connection is already removed from link list
654 */
655 c = container_of(head, struct sfe_ipv6_connection, rcu);
656 BUG_ON(!c->removed);
657
658 DEBUG_TRACE("%px: connecton has been deleted\n", c);
659
660 /*
661 * Release our hold of the source and dest devices and free the memory
662 * for our connection objects.
663 */
664 dev_put(c->original_dev);
665 dev_put(c->reply_dev);
666 kfree(c->original_match);
667 kfree(c->reply_match);
668 kfree(c);
669}
670
671/*
Xiaoping Fan978b3772015-05-27 14:15:18 -0700672 * sfe_ipv6_flush_connection()
673 * Flush a connection and free all associated resources.
674 *
675 * We need to be called with bottom halves disabled locally as we need to acquire
676 * the connection hash lock and release it again. In general we're actually called
677 * from within a BH and so we're fine, but we're also called when connections are
678 * torn down.
679 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530680void sfe_ipv6_flush_connection(struct sfe_ipv6 *si,
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700681 struct sfe_ipv6_connection *c,
682 sfe_sync_reason_t reason)
Xiaoping Fan978b3772015-05-27 14:15:18 -0700683{
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700684 u64 now_jiffies;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700685 sfe_sync_rule_callback_t sync_rule_callback;
686
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530687 BUG_ON(!c->removed);
688
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530689 this_cpu_inc(si->stats_pcpu->connection_flushes64);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700690
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530691 rcu_read_lock();
692
693 sync_rule_callback = rcu_dereference(si->sync_rule_callback);
694
695 /*
696 * Generate a sync message and then sync.
697 */
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530698
Xiaoping Fan978b3772015-05-27 14:15:18 -0700699 if (sync_rule_callback) {
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530700 struct sfe_connection_sync sis;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700701 now_jiffies = get_jiffies_64();
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700702 sfe_ipv6_gen_sync_connection(si, c, &sis, reason, now_jiffies);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700703 sync_rule_callback(&sis);
704 }
705
706 rcu_read_unlock();
707
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530708 call_rcu(&c->rcu, sfe_ipv6_free_sfe_ipv6_connection_rcu);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700709}
710
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530711 /*
712 * sfe_ipv6_exception_stats_inc()
713 * Increment exception stats.
714 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530715void sfe_ipv6_exception_stats_inc(struct sfe_ipv6 *si, enum sfe_ipv6_exception_events reason)
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530716{
717 struct sfe_ipv6_stats *stats = this_cpu_ptr(si->stats_pcpu);
718
719 stats->exception_events64[reason]++;
720 stats->packets_not_forwarded64++;
721}
722
Xiaoping Fan978b3772015-05-27 14:15:18 -0700723/*
Xiaoping Fan978b3772015-05-27 14:15:18 -0700724 * sfe_ipv6_recv()
725 * Handle packet receives and forwaring.
726 *
727 * Returns 1 if the packet is forwarded or 0 if it isn't.
728 */
729int sfe_ipv6_recv(struct net_device *dev, struct sk_buff *skb)
730{
731 struct sfe_ipv6 *si = &__si6;
732 unsigned int len;
733 unsigned int payload_len;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530734 unsigned int ihl = sizeof(struct ipv6hdr);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700735 bool flush_on_find = false;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530736 struct ipv6hdr *iph;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700737 u8 next_hdr;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700738
739 /*
740 * Check that we have space for an IP header and an uplayer header here.
741 */
742 len = skb->len;
743 if (!pskb_may_pull(skb, ihl + sizeof(struct sfe_ipv6_ext_hdr))) {
Xiaoping Fan978b3772015-05-27 14:15:18 -0700744
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530745 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_HEADER_INCOMPLETE);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700746 DEBUG_TRACE("len: %u is too short\n", len);
747 return 0;
748 }
749
750 /*
751 * Is our IP version wrong?
752 */
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530753 iph = (struct ipv6hdr *)skb->data;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700754 if (unlikely(iph->version != 6)) {
Xiaoping Fan978b3772015-05-27 14:15:18 -0700755
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530756 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_NON_V6);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700757 DEBUG_TRACE("IP version: %u\n", iph->version);
758 return 0;
759 }
760
761 /*
762 * Does our datagram fit inside the skb?
763 */
764 payload_len = ntohs(iph->payload_len);
765 if (unlikely(payload_len > (len - ihl))) {
Xiaoping Fan978b3772015-05-27 14:15:18 -0700766
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530767 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_DATAGRAM_INCOMPLETE);
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530768 DEBUG_TRACE("payload_len: %u, exceeds len: %u\n", payload_len, (len - (unsigned int)sizeof(struct ipv6hdr)));
Xiaoping Fan978b3772015-05-27 14:15:18 -0700769 return 0;
770 }
771
772 next_hdr = iph->nexthdr;
773 while (unlikely(sfe_ipv6_is_ext_hdr(next_hdr))) {
774 struct sfe_ipv6_ext_hdr *ext_hdr;
775 unsigned int ext_hdr_len;
776
777 ext_hdr = (struct sfe_ipv6_ext_hdr *)(skb->data + ihl);
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530778 if (next_hdr == NEXTHDR_FRAGMENT) {
779 struct frag_hdr *frag_hdr = (struct frag_hdr *)ext_hdr;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700780 unsigned int frag_off = ntohs(frag_hdr->frag_off);
781
782 if (frag_off & SFE_IPV6_FRAG_OFFSET) {
Xiaoping Fan978b3772015-05-27 14:15:18 -0700783
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530784 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_NON_INITIAL_FRAGMENT);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700785 DEBUG_TRACE("non-initial fragment\n");
786 return 0;
787 }
788 }
789
790 ext_hdr_len = ext_hdr->hdr_len;
791 ext_hdr_len <<= 3;
792 ext_hdr_len += sizeof(struct sfe_ipv6_ext_hdr);
793 ihl += ext_hdr_len;
794 if (!pskb_may_pull(skb, ihl + sizeof(struct sfe_ipv6_ext_hdr))) {
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530795 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_HEADER_INCOMPLETE);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700796
797 DEBUG_TRACE("extension header %d not completed\n", next_hdr);
798 return 0;
799 }
800
801 flush_on_find = true;
802 next_hdr = ext_hdr->next_hdr;
803 }
804
805 if (IPPROTO_UDP == next_hdr) {
806 return sfe_ipv6_recv_udp(si, skb, dev, len, iph, ihl, flush_on_find);
807 }
808
809 if (IPPROTO_TCP == next_hdr) {
810 return sfe_ipv6_recv_tcp(si, skb, dev, len, iph, ihl, flush_on_find);
811 }
812
813 if (IPPROTO_ICMPV6 == next_hdr) {
814 return sfe_ipv6_recv_icmp(si, skb, dev, len, iph, ihl);
815 }
816
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530817 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_UNHANDLED_PROTOCOL);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700818 DEBUG_TRACE("not UDP, TCP or ICMP: %u\n", next_hdr);
819 return 0;
820}
821
822/*
823 * sfe_ipv6_update_tcp_state()
824 * update TCP window variables.
825 */
826static void
827sfe_ipv6_update_tcp_state(struct sfe_ipv6_connection *c,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530828 struct sfe_ipv6_rule_create_msg *msg)
Xiaoping Fan978b3772015-05-27 14:15:18 -0700829{
830 struct sfe_ipv6_connection_match *orig_cm;
831 struct sfe_ipv6_connection_match *repl_cm;
832 struct sfe_ipv6_tcp_connection_match *orig_tcp;
833 struct sfe_ipv6_tcp_connection_match *repl_tcp;
834
835 orig_cm = c->original_match;
836 repl_cm = c->reply_match;
837 orig_tcp = &orig_cm->protocol_state.tcp;
838 repl_tcp = &repl_cm->protocol_state.tcp;
839
840 /* update orig */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530841 if (orig_tcp->max_win < msg->tcp_rule.flow_max_window) {
842 orig_tcp->max_win = msg->tcp_rule.flow_max_window;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700843 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530844 if ((s32)(orig_tcp->end - msg->tcp_rule.flow_end) < 0) {
845 orig_tcp->end = msg->tcp_rule.flow_end;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700846 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530847 if ((s32)(orig_tcp->max_end - msg->tcp_rule.flow_max_end) < 0) {
848 orig_tcp->max_end = msg->tcp_rule.flow_max_end;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700849 }
850
851 /* update reply */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530852 if (repl_tcp->max_win < msg->tcp_rule.return_max_window) {
853 repl_tcp->max_win = msg->tcp_rule.return_max_window;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700854 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530855 if ((s32)(repl_tcp->end - msg->tcp_rule.return_end) < 0) {
856 repl_tcp->end = msg->tcp_rule.return_end;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700857 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530858 if ((s32)(repl_tcp->max_end - msg->tcp_rule.return_max_end) < 0) {
859 repl_tcp->max_end = msg->tcp_rule.return_max_end;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700860 }
861
862 /* update match flags */
863 orig_cm->flags &= ~SFE_IPV6_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
864 repl_cm->flags &= ~SFE_IPV6_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530865 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_NO_SEQ_CHECK) {
Xiaoping Fan978b3772015-05-27 14:15:18 -0700866 orig_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
867 repl_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
868 }
869}
870
871/*
872 * sfe_ipv6_update_protocol_state()
873 * update protocol specified state machine.
874 */
875static void
876sfe_ipv6_update_protocol_state(struct sfe_ipv6_connection *c,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530877 struct sfe_ipv6_rule_create_msg *msg)
Xiaoping Fan978b3772015-05-27 14:15:18 -0700878{
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530879 switch (msg->tuple.protocol) {
Xiaoping Fan978b3772015-05-27 14:15:18 -0700880 case IPPROTO_TCP:
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530881 sfe_ipv6_update_tcp_state(c, msg);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700882 break;
883 }
884}
885
886/*
887 * sfe_ipv6_update_rule()
888 * update forwarding rule after rule is created.
889 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530890void sfe_ipv6_update_rule(struct sfe_ipv6_rule_create_msg *msg)
891
Xiaoping Fan978b3772015-05-27 14:15:18 -0700892{
893 struct sfe_ipv6_connection *c;
894 struct sfe_ipv6 *si = &__si6;
895
896 spin_lock_bh(&si->lock);
897
898 c = sfe_ipv6_find_connection(si,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530899 msg->tuple.protocol,
900 (struct sfe_ipv6_addr *)msg->tuple.flow_ip,
901 msg->tuple.flow_ident,
902 (struct sfe_ipv6_addr *)msg->tuple.return_ip,
903 msg->tuple.return_ident);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700904 if (c != NULL) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530905 sfe_ipv6_update_protocol_state(c, msg);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700906 }
907
908 spin_unlock_bh(&si->lock);
909}
910
911/*
912 * sfe_ipv6_create_rule()
913 * Create a forwarding rule.
914 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530915int sfe_ipv6_create_rule(struct sfe_ipv6_rule_create_msg *msg)
Xiaoping Fan978b3772015-05-27 14:15:18 -0700916{
917 struct sfe_ipv6 *si = &__si6;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530918 struct sfe_ipv6_connection *c, *old_c;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700919 struct sfe_ipv6_connection_match *original_cm;
920 struct sfe_ipv6_connection_match *reply_cm;
921 struct net_device *dest_dev;
922 struct net_device *src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530923 struct sfe_ipv6_5tuple *tuple = &msg->tuple;
Suruchi Sumanc1a4a612021-10-21 14:50:23 +0530924 s32 flow_interface_num = msg->conn_rule.flow_top_interface_num;
925 s32 return_interface_num = msg->conn_rule.return_top_interface_num;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700926
Suruchi Sumanc1a4a612021-10-21 14:50:23 +0530927 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_FLOW_BOTTOM_INTERFACE) {
928 flow_interface_num = msg->conn_rule.flow_interface_num;
929 }
930
931 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_RETURN_BOTTOM_INTERFACE) {
932 return_interface_num = msg->conn_rule.return_interface_num;
933 }
934
935 src_dev = dev_get_by_index(&init_net, flow_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530936 if (!src_dev) {
937 DEBUG_WARN("%px: Unable to find src_dev corresponding to %d\n", msg,
Suruchi Sumanc1a4a612021-10-21 14:50:23 +0530938 flow_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530939 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
940 return -EINVAL;
941 }
942
Suruchi Sumanc1a4a612021-10-21 14:50:23 +0530943 dest_dev = dev_get_by_index(&init_net, return_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530944 if (!dest_dev) {
945 DEBUG_WARN("%px: Unable to find dest_dev corresponding to %d\n", msg,
Suruchi Sumanc1a4a612021-10-21 14:50:23 +0530946 return_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530947 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
948 dev_put(src_dev);
949 return -EINVAL;
950 }
Xiaoping Fan978b3772015-05-27 14:15:18 -0700951
952 if (unlikely((dest_dev->reg_state != NETREG_REGISTERED) ||
953 (src_dev->reg_state != NETREG_REGISTERED))) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530954 DEBUG_WARN("%px: src_dev=%s and dest_dev=%s are unregistered\n", msg,
955 src_dev->name, dest_dev->name);
956 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
957 dev_put(src_dev);
958 dev_put(dest_dev);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700959 return -EINVAL;
960 }
961
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530962 /*
963 * Allocate the various connection tracking objects.
964 */
965 c = (struct sfe_ipv6_connection *)kmalloc(sizeof(struct sfe_ipv6_connection), GFP_ATOMIC);
966 if (unlikely(!c)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530967 DEBUG_WARN("%px: memory allocation of connection entry failed\n", msg);
968 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
969 dev_put(src_dev);
970 dev_put(dest_dev);
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530971 return -ENOMEM;
972 }
973
974 original_cm = (struct sfe_ipv6_connection_match *)kmalloc(sizeof(struct sfe_ipv6_connection_match), GFP_ATOMIC);
975 if (unlikely(!original_cm)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530976 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
977 DEBUG_WARN("%px: memory allocation of connection match entry failed\n", msg);
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530978 kfree(c);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530979 dev_put(src_dev);
980 dev_put(dest_dev);
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530981 return -ENOMEM;
982 }
983
984 reply_cm = (struct sfe_ipv6_connection_match *)kmalloc(sizeof(struct sfe_ipv6_connection_match), GFP_ATOMIC);
985 if (unlikely(!reply_cm)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530986 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
987 DEBUG_WARN("%px: memory allocation of connection match entry failed\n", msg);
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530988 kfree(original_cm);
989 kfree(c);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530990 dev_put(src_dev);
991 dev_put(dest_dev);
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530992 return -ENOMEM;
993 }
994
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530995 this_cpu_inc(si->stats_pcpu->connection_create_requests64);
996
Xiaoping Fan978b3772015-05-27 14:15:18 -0700997 spin_lock_bh(&si->lock);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700998
999 /*
1000 * Check to see if there is already a flow that matches the rule we're
1001 * trying to create. If there is then we can't create a new one.
1002 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301003 old_c = sfe_ipv6_find_connection(si, tuple->protocol, (struct sfe_ipv6_addr *)tuple->flow_ip, tuple->flow_ident,
1004 (struct sfe_ipv6_addr *)tuple->return_ip, tuple->return_ident);
1005
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301006 if (old_c != NULL) {
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301007 this_cpu_inc(si->stats_pcpu->connection_create_collisions64);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001008
1009 /*
1010 * If we already have the flow then it's likely that this
1011 * request to create the connection rule contains more
1012 * up-to-date information. Check and update accordingly.
1013 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301014 sfe_ipv6_update_protocol_state(old_c, msg);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001015 spin_unlock_bh(&si->lock);
1016
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301017 kfree(reply_cm);
1018 kfree(original_cm);
1019 kfree(c);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301020 dev_put(src_dev);
1021 dev_put(dest_dev);
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301022
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301023 DEBUG_TRACE("connection already exists - p: %d\n"
Tian Yang45f39c82020-10-06 14:07:47 -07001024 " s: %s:%pxM:%pI6:%u, d: %s:%pxM:%pI6:%u\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301025 tuple->protocol,
1026 src_dev->name, msg->conn_rule.flow_mac, tuple->flow_ip, ntohs(tuple->flow_ident),
1027 dest_dev->name, msg->conn_rule.return_mac, tuple->return_ip, ntohs(tuple->return_ident));
Xiaoping Fan978b3772015-05-27 14:15:18 -07001028 return -EADDRINUSE;
1029 }
1030
1031 /*
Xiaoping Fan978b3772015-05-27 14:15:18 -07001032 * Fill in the "original" direction connection matching object.
1033 * Note that the transmit MAC address is "dest_mac_xlate" because
1034 * we always know both ends of a connection by their translated
1035 * addresses and not their public addresses.
1036 */
1037 original_cm->match_dev = src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301038 original_cm->match_protocol = tuple->protocol;
1039 original_cm->match_src_ip[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip;
1040 original_cm->match_src_port = tuple->flow_ident;
1041 original_cm->match_dest_ip[0] = *(struct sfe_ipv6_addr *)tuple->return_ip;
1042 original_cm->match_dest_port = tuple->return_ident;
1043
1044 original_cm->xlate_src_ip[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip;
1045 original_cm->xlate_src_port = tuple->flow_ident;
1046 original_cm->xlate_dest_ip[0] = *(struct sfe_ipv6_addr *)tuple->return_ip;
1047 original_cm->xlate_dest_port = tuple->return_ident;
1048
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301049 atomic_set(&original_cm->rx_packet_count, 0);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001050 original_cm->rx_packet_count64 = 0;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301051 atomic_set(&original_cm->rx_byte_count, 0);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001052 original_cm->rx_byte_count64 = 0;
1053 original_cm->xmit_dev = dest_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301054
1055 original_cm->xmit_dev_mtu = msg->conn_rule.return_mtu;
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301056
Xiaoping Fan978b3772015-05-27 14:15:18 -07001057 original_cm->connection = c;
1058 original_cm->counter_match = reply_cm;
1059 original_cm->flags = 0;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301060 if (msg->valid_flags & SFE_RULE_CREATE_QOS_VALID) {
1061 original_cm->priority = msg->qos_rule.flow_qos_tag;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001062 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_PRIORITY_REMARK;
1063 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301064 if (msg->valid_flags & SFE_RULE_CREATE_DSCP_MARKING_VALID) {
1065 original_cm->dscp = msg->dscp_rule.flow_dscp << SFE_IPV6_DSCP_SHIFT;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001066 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_DSCP_REMARK;
1067 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07001068#ifdef CONFIG_NF_FLOW_COOKIE
1069 original_cm->flow_cookie = 0;
1070#endif
Zhi Chen8748eb32015-06-18 12:58:48 -07001071#ifdef CONFIG_XFRM
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301072 if (msg->valid_flags & SFE_RULE_CREATE_DIRECTION_VALID) {
1073 original_cm->flow_accel = msg->direction_rule.flow_accel;
1074 } else {
1075 original_cm->flow_accel = 1;
1076 }
Zhi Chen8748eb32015-06-18 12:58:48 -07001077#endif
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301078 /*
1079 * If l2_features are disabled and flow uses l2 features such as macvlan/bridge/pppoe/vlan,
1080 * bottom interfaces are expected to be disabled in the flow rule and always top interfaces
1081 * are used. In such cases, do not use HW csum offload. csum offload is used only when we
1082 * are sending directly to the destination interface that supports it.
1083 */
1084 if (likely(dest_dev->features & NETIF_F_HW_CSUM)) {
1085 if ((msg->conn_rule.return_top_interface_num == msg->conn_rule.return_interface_num) ||
1086 (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_RETURN_BOTTOM_INTERFACE)) {
1087 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_CSUM_OFFLOAD;
1088 }
1089 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07001090
1091 /*
Ken Zhubbf49652021-09-12 15:33:09 -07001092 * For the non-arp interface, we don't write L2 HDR.
Xiaoping Fan978b3772015-05-27 14:15:18 -07001093 */
Ken Zhubbf49652021-09-12 15:33:09 -07001094 if (!(dest_dev->flags & IFF_NOARP)) {
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301095
1096 /*
1097 * Check whether the rule has configured a specific source MAC address to use.
1098 * This is needed when virtual L3 interfaces such as br-lan, macvlan, vlan are used during egress
1099 */
1100 if ((msg->valid_flags & SFE_RULE_CREATE_SRC_MAC_VALID) &&
1101 (msg->src_mac_rule.mac_valid_flags & SFE_SRC_MAC_RETURN_VALID)) {
1102 ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)msg->src_mac_rule.return_src_mac);
1103 } else {
1104 ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)dest_dev->dev_addr);
1105 }
1106 ether_addr_copy((u8 *)original_cm->xmit_dest_mac, (u8 *)msg->conn_rule.return_mac);
1107
Xiaoping Fan978b3772015-05-27 14:15:18 -07001108 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_WRITE_L2_HDR;
1109
1110 /*
1111 * If our dev writes Ethernet headers then we can write a really fast
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301112 * version
Xiaoping Fan978b3772015-05-27 14:15:18 -07001113 */
1114 if (dest_dev->header_ops) {
1115 if (dest_dev->header_ops->create == eth_header) {
1116 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR;
1117 }
1118 }
1119 }
1120
1121 /*
1122 * Fill in the "reply" direction connection matching object.
1123 */
1124 reply_cm->match_dev = dest_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301125 reply_cm->match_protocol = tuple->protocol;
1126 reply_cm->match_src_ip[0] = *(struct sfe_ipv6_addr *)tuple->return_ip;
1127 reply_cm->match_src_port = tuple->return_ident;
1128 reply_cm->match_dest_ip[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip;
1129 reply_cm->match_dest_port = tuple->flow_ident;
1130 reply_cm->xlate_src_ip[0] = *(struct sfe_ipv6_addr *)tuple->return_ip;
1131 reply_cm->xlate_src_port = tuple->return_ident;
1132 reply_cm->xlate_dest_ip[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip;
1133 reply_cm->xlate_dest_port = tuple->flow_ident;
1134
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301135 atomic_set(&original_cm->rx_byte_count, 0);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001136 reply_cm->rx_packet_count64 = 0;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301137 atomic_set(&reply_cm->rx_byte_count, 0);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001138 reply_cm->rx_byte_count64 = 0;
1139 reply_cm->xmit_dev = src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301140 reply_cm->xmit_dev_mtu = msg->conn_rule.flow_mtu;
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301141
Xiaoping Fan978b3772015-05-27 14:15:18 -07001142 reply_cm->connection = c;
1143 reply_cm->counter_match = original_cm;
1144 reply_cm->flags = 0;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301145 if (msg->valid_flags & SFE_RULE_CREATE_QOS_VALID) {
1146 reply_cm->priority = msg->qos_rule.return_qos_tag;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001147 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_PRIORITY_REMARK;
1148 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301149 if (msg->valid_flags & SFE_RULE_CREATE_DSCP_MARKING_VALID) {
1150 reply_cm->dscp = msg->dscp_rule.return_dscp << SFE_IPV6_DSCP_SHIFT;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001151 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_DSCP_REMARK;
1152 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07001153#ifdef CONFIG_NF_FLOW_COOKIE
1154 reply_cm->flow_cookie = 0;
1155#endif
Zhi Chen8748eb32015-06-18 12:58:48 -07001156#ifdef CONFIG_XFRM
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301157 if (msg->valid_flags & SFE_RULE_CREATE_DIRECTION_VALID) {
1158 reply_cm->flow_accel = msg->direction_rule.return_accel;
1159 } else {
1160 reply_cm->flow_accel = 1;
1161 }
Zhi Chen8748eb32015-06-18 12:58:48 -07001162#endif
Xiaoping Fan978b3772015-05-27 14:15:18 -07001163 /*
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301164 * If l2_features are disabled and flow uses l2 features such as macvlan/bridge/pppoe/vlan,
1165 * bottom interfaces are expected to be disabled in the flow rule and always top interfaces
1166 * are used. In such cases, do not use HW csum offload. csum offload is used only when we
1167 * are sending directly to the destination interface that supports it.
1168 */
1169 if (likely(src_dev->features & NETIF_F_HW_CSUM)) {
1170 if ((msg->conn_rule.flow_top_interface_num == msg->conn_rule.flow_interface_num) ||
1171 (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_FLOW_BOTTOM_INTERFACE)) {
1172 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_CSUM_OFFLOAD;
1173 }
1174 }
1175
1176 /*
Ken Zhubbf49652021-09-12 15:33:09 -07001177 * For the non-arp interface, we don't write L2 HDR.
Xiaoping Fan978b3772015-05-27 14:15:18 -07001178 */
Ken Zhubbf49652021-09-12 15:33:09 -07001179 if (!(src_dev->flags & IFF_NOARP)) {
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301180
1181 /*
1182 * Check whether the rule has configured a specific source MAC address to use.
1183 * This is needed when virtual L3 interfaces such as br-lan, macvlan, vlan are used during egress
1184 */
1185 if ((msg->valid_flags & SFE_RULE_CREATE_SRC_MAC_VALID) &&
1186 (msg->src_mac_rule.mac_valid_flags & SFE_SRC_MAC_FLOW_VALID)) {
1187 ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)msg->src_mac_rule.flow_src_mac);
1188 } else {
1189 ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)src_dev->dev_addr);
1190 }
1191
1192 ether_addr_copy((u8 *)reply_cm->xmit_dest_mac, (u8 *)msg->conn_rule.flow_mac);
1193
Xiaoping Fan978b3772015-05-27 14:15:18 -07001194 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_WRITE_L2_HDR;
1195
1196 /*
1197 * If our dev writes Ethernet headers then we can write a really fast
1198 * version.
1199 */
1200 if (src_dev->header_ops) {
1201 if (src_dev->header_ops->create == eth_header) {
1202 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR;
1203 }
1204 }
1205 }
1206
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301207 /*
1208 * No support for NAT in ipv6
1209 */
Xiaoping Fan978b3772015-05-27 14:15:18 -07001210
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301211 c->protocol = tuple->protocol;
1212 c->src_ip[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip;
1213 c->src_ip_xlate[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip;
1214 c->src_port = tuple->flow_ident;
1215 c->src_port_xlate = tuple->flow_ident;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001216 c->original_dev = src_dev;
1217 c->original_match = original_cm;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301218
1219 c->dest_ip[0] = *(struct sfe_ipv6_addr *)tuple->return_ip;
1220 c->dest_ip_xlate[0] = *(struct sfe_ipv6_addr *)tuple->return_ip;
1221 c->dest_port = tuple->return_ident;
1222 c->dest_port_xlate = tuple->return_ident;
1223
Xiaoping Fan978b3772015-05-27 14:15:18 -07001224 c->reply_dev = dest_dev;
1225 c->reply_match = reply_cm;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301226 c->mark = 0; /* No mark support */
Xiaoping Fan34586472015-07-03 02:20:35 -07001227 c->debug_read_seq = 0;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001228 c->last_sync_jiffies = get_jiffies_64();
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301229 c->removed = false;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001230
1231 /*
Xiaoping Fan978b3772015-05-27 14:15:18 -07001232 * Initialize the protocol-specific information that we track.
1233 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301234 switch (tuple->protocol) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07001235 case IPPROTO_TCP:
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301236 original_cm->protocol_state.tcp.win_scale = msg->tcp_rule.flow_window_scale;
1237 original_cm->protocol_state.tcp.max_win = msg->tcp_rule.flow_max_window ? msg->tcp_rule.flow_max_window : 1;
1238 original_cm->protocol_state.tcp.end = msg->tcp_rule.flow_end;
1239 original_cm->protocol_state.tcp.max_end = msg->tcp_rule.flow_max_end;
1240 reply_cm->protocol_state.tcp.win_scale = msg->tcp_rule.return_window_scale;
1241 reply_cm->protocol_state.tcp.max_win = msg->tcp_rule.return_max_window ? msg->tcp_rule.return_max_window : 1;
1242 reply_cm->protocol_state.tcp.end = msg->tcp_rule.return_end;
1243 reply_cm->protocol_state.tcp.max_end = msg->tcp_rule.return_max_end;
1244 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_NO_SEQ_CHECK) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07001245 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
1246 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
1247 }
1248 break;
1249 }
1250
1251 sfe_ipv6_connection_match_compute_translations(original_cm);
1252 sfe_ipv6_connection_match_compute_translations(reply_cm);
1253 sfe_ipv6_insert_connection(si, c);
1254
1255 spin_unlock_bh(&si->lock);
1256
1257 /*
1258 * We have everything we need!
1259 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301260 DEBUG_INFO("new connection - p: %d\n"
Tian Yang45f39c82020-10-06 14:07:47 -07001261 " s: %s:%pxM(%pxM):%pI6(%pI6):%u(%u)\n"
1262 " d: %s:%pxM(%pxM):%pI6(%pI6):%u(%u)\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301263 tuple->protocol,
1264 src_dev->name, msg->conn_rule.flow_mac, NULL,
1265 (void *)tuple->flow_ip, (void *)tuple->flow_ip, ntohs(tuple->flow_ident), ntohs(tuple->flow_ident),
1266 dest_dev->name, NULL, msg->conn_rule.return_mac,
1267 (void *)tuple->return_ip, (void *)tuple->return_ip, ntohs(tuple->return_ident), ntohs(tuple->return_ident));
Xiaoping Fan978b3772015-05-27 14:15:18 -07001268
1269 return 0;
1270}
1271
1272/*
1273 * sfe_ipv6_destroy_rule()
1274 * Destroy a forwarding rule.
1275 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301276void sfe_ipv6_destroy_rule(struct sfe_ipv6_rule_destroy_msg *msg)
Xiaoping Fan978b3772015-05-27 14:15:18 -07001277{
1278 struct sfe_ipv6 *si = &__si6;
1279 struct sfe_ipv6_connection *c;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301280 bool ret;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301281 struct sfe_ipv6_5tuple *tuple = &msg->tuple;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001282
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301283 this_cpu_inc(si->stats_pcpu->connection_destroy_requests64);
1284
Xiaoping Fan978b3772015-05-27 14:15:18 -07001285 spin_lock_bh(&si->lock);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001286
1287 /*
1288 * Check to see if we have a flow that matches the rule we're trying
1289 * to destroy. If there isn't then we can't destroy it.
1290 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301291 c = sfe_ipv6_find_connection(si, tuple->protocol, (struct sfe_ipv6_addr *)tuple->flow_ip, tuple->flow_ident,
1292 (struct sfe_ipv6_addr *)tuple->return_ip, tuple->return_ident);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001293 if (!c) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07001294 spin_unlock_bh(&si->lock);
1295
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301296 this_cpu_inc(si->stats_pcpu->connection_destroy_misses64);
1297
Xiaoping Fan978b3772015-05-27 14:15:18 -07001298 DEBUG_TRACE("connection does not exist - p: %d, s: %pI6:%u, d: %pI6:%u\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301299 tuple->protocol, tuple->flow_ip, ntohs(tuple->flow_ident),
1300 tuple->return_ip, ntohs(tuple->return_ident));
Xiaoping Fan978b3772015-05-27 14:15:18 -07001301 return;
1302 }
1303
1304 /*
1305 * Remove our connection details from the hash tables.
1306 */
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301307 ret = sfe_ipv6_remove_connection(si, c);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001308 spin_unlock_bh(&si->lock);
1309
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301310 if (ret) {
1311 sfe_ipv6_flush_connection(si, c, SFE_SYNC_REASON_DESTROY);
1312 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07001313
1314 DEBUG_INFO("connection destroyed - p: %d, s: %pI6:%u, d: %pI6:%u\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301315 tuple->protocol, tuple->flow_ip, ntohs(tuple->flow_ident),
1316 tuple->return_ip, ntohs(tuple->return_ident));
Xiaoping Fan978b3772015-05-27 14:15:18 -07001317}
1318
1319/*
1320 * sfe_ipv6_register_sync_rule_callback()
1321 * Register a callback for rule synchronization.
1322 */
1323void sfe_ipv6_register_sync_rule_callback(sfe_sync_rule_callback_t sync_rule_callback)
1324{
1325 struct sfe_ipv6 *si = &__si6;
1326
1327 spin_lock_bh(&si->lock);
1328 rcu_assign_pointer(si->sync_rule_callback, sync_rule_callback);
1329 spin_unlock_bh(&si->lock);
1330}
1331
1332/*
1333 * sfe_ipv6_get_debug_dev()
1334 */
1335static ssize_t sfe_ipv6_get_debug_dev(struct device *dev,
1336 struct device_attribute *attr,
1337 char *buf)
1338{
1339 struct sfe_ipv6 *si = &__si6;
1340 ssize_t count;
1341 int num;
1342
1343 spin_lock_bh(&si->lock);
1344 num = si->debug_dev;
1345 spin_unlock_bh(&si->lock);
1346
1347 count = snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", num);
1348 return count;
1349}
1350
1351/*
1352 * sfe_ipv6_destroy_all_rules_for_dev()
1353 * Destroy all connections that match a particular device.
1354 *
1355 * If we pass dev as NULL then this destroys all connections.
1356 */
1357void sfe_ipv6_destroy_all_rules_for_dev(struct net_device *dev)
1358{
1359 struct sfe_ipv6 *si = &__si6;
1360 struct sfe_ipv6_connection *c;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301361 bool ret;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001362
Xiaoping Fan34586472015-07-03 02:20:35 -07001363another_round:
Xiaoping Fan978b3772015-05-27 14:15:18 -07001364 spin_lock_bh(&si->lock);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001365
Xiaoping Fan34586472015-07-03 02:20:35 -07001366 for (c = si->all_connections_head; c; c = c->all_connections_next) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07001367 /*
Xiaoping Fan34586472015-07-03 02:20:35 -07001368 * Does this connection relate to the device we are destroying?
Xiaoping Fan978b3772015-05-27 14:15:18 -07001369 */
1370 if (!dev
1371 || (dev == c->original_dev)
1372 || (dev == c->reply_dev)) {
Xiaoping Fan34586472015-07-03 02:20:35 -07001373 break;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001374 }
Xiaoping Fan34586472015-07-03 02:20:35 -07001375 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07001376
Xiaoping Fan34586472015-07-03 02:20:35 -07001377 if (c) {
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301378 ret = sfe_ipv6_remove_connection(si, c);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001379 }
1380
1381 spin_unlock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07001382
1383 if (c) {
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301384 if (ret) {
1385 sfe_ipv6_flush_connection(si, c, SFE_SYNC_REASON_DESTROY);
1386 }
Xiaoping Fan34586472015-07-03 02:20:35 -07001387 goto another_round;
1388 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07001389}
1390
1391/*
1392 * sfe_ipv6_periodic_sync()
1393 */
Ken Zhu137722d2021-09-23 17:57:36 -07001394static void sfe_ipv6_periodic_sync(struct work_struct *work)
Xiaoping Fan978b3772015-05-27 14:15:18 -07001395{
Ken Zhu137722d2021-09-23 17:57:36 -07001396 struct sfe_ipv6 *si = container_of((struct delayed_work *)work, struct sfe_ipv6, sync_dwork);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07001397 u64 now_jiffies;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001398 int quota;
1399 sfe_sync_rule_callback_t sync_rule_callback;
Ken Zhu32b95392021-09-03 13:52:04 -07001400 struct sfe_ipv6_connection *c;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001401
1402 now_jiffies = get_jiffies_64();
1403
1404 rcu_read_lock();
1405 sync_rule_callback = rcu_dereference(si->sync_rule_callback);
1406 if (!sync_rule_callback) {
1407 rcu_read_unlock();
1408 goto done;
1409 }
1410
1411 spin_lock_bh(&si->lock);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001412
1413 /*
Ken Zhu32b95392021-09-03 13:52:04 -07001414 * If we have reached the end of the connection list, walk from
1415 * the connection head.
1416 */
1417 c = si->wc_next;
1418 if (unlikely(!c)) {
1419 c = si->all_connections_head;
1420 }
1421 /*
Xiaoping Fan978b3772015-05-27 14:15:18 -07001422 * Get an estimate of the number of connections to parse in this sync.
1423 */
1424 quota = (si->num_connections + 63) / 64;
1425
1426 /*
Ken Zhu32b95392021-09-03 13:52:04 -07001427 * Walk the "all connection" list and sync the connection state.
Xiaoping Fan978b3772015-05-27 14:15:18 -07001428 */
Ken Zhu32b95392021-09-03 13:52:04 -07001429 while (likely(c && quota)) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07001430 struct sfe_ipv6_connection_match *cm;
1431 struct sfe_ipv6_connection_match *counter_cm;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001432 struct sfe_connection_sync sis;
1433
Ken Zhu32b95392021-09-03 13:52:04 -07001434 cm = c->original_match;
1435 counter_cm = c->reply_match;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001436
1437 /*
Ken Zhu32b95392021-09-03 13:52:04 -07001438 * Didn't receive packets in the origial direction or reply
1439 * direction, move to the next connection.
Xiaoping Fan978b3772015-05-27 14:15:18 -07001440 */
Ken Zhu32b95392021-09-03 13:52:04 -07001441 if (!atomic_read(&cm->rx_packet_count) && !atomic_read(&counter_cm->rx_packet_count)) {
1442 c = c->all_connections_next;
1443 continue;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001444 }
1445
Ken Zhu32b95392021-09-03 13:52:04 -07001446 quota--;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001447
1448 /*
1449 * Sync the connection state.
1450 */
Xiaoping Fan99cb4c12015-08-21 19:07:32 -07001451 sfe_ipv6_gen_sync_connection(si, c, &sis, SFE_SYNC_REASON_STATS, now_jiffies);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001452
Ken Zhu32b95392021-09-03 13:52:04 -07001453 si->wc_next = c->all_connections_next;
1454
Xiaoping Fan978b3772015-05-27 14:15:18 -07001455 spin_unlock_bh(&si->lock);
1456 sync_rule_callback(&sis);
1457 spin_lock_bh(&si->lock);
Ken Zhu32b95392021-09-03 13:52:04 -07001458
1459 /*
1460 * c must be set and used in the same lock/unlock window;
1461 * because c could be removed when we don't hold the lock,
1462 * so delay grabbing until after the callback and relock.
1463 */
1464 c = si->wc_next;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001465 }
1466
Ken Zhu32b95392021-09-03 13:52:04 -07001467 /*
1468 * At the end of loop, put wc_next to the connection we left
1469 */
1470 si->wc_next = c;
1471
Xiaoping Fan978b3772015-05-27 14:15:18 -07001472 spin_unlock_bh(&si->lock);
1473 rcu_read_unlock();
1474
1475done:
Ken Zhu137722d2021-09-23 17:57:36 -07001476 schedule_delayed_work_on(si->work_cpu, (struct delayed_work *)work, ((HZ + 99) / 100));
Xiaoping Fan978b3772015-05-27 14:15:18 -07001477}
1478
1479/*
1480 * sfe_ipv6_debug_dev_read_start()
1481 * Generate part of the XML output.
1482 */
1483static bool sfe_ipv6_debug_dev_read_start(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
1484 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
1485{
1486 int bytes_read;
1487
Xiaoping Fan34586472015-07-03 02:20:35 -07001488 si->debug_read_seq++;
1489
Xiaoping Fan978b3772015-05-27 14:15:18 -07001490 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "<sfe_ipv6>\n");
1491 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1492 return false;
1493 }
1494
1495 *length -= bytes_read;
1496 *total_read += bytes_read;
1497
1498 ws->state++;
1499 return true;
1500}
1501
1502/*
1503 * sfe_ipv6_debug_dev_read_connections_start()
1504 * Generate part of the XML output.
1505 */
1506static bool sfe_ipv6_debug_dev_read_connections_start(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
1507 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
1508{
1509 int bytes_read;
1510
1511 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<connections>\n");
1512 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1513 return false;
1514 }
1515
1516 *length -= bytes_read;
1517 *total_read += bytes_read;
1518
1519 ws->state++;
1520 return true;
1521}
1522
1523/*
1524 * sfe_ipv6_debug_dev_read_connections_connection()
1525 * Generate part of the XML output.
1526 */
1527static bool sfe_ipv6_debug_dev_read_connections_connection(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
1528 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
1529{
1530 struct sfe_ipv6_connection *c;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001531 struct sfe_ipv6_connection_match *original_cm;
1532 struct sfe_ipv6_connection_match *reply_cm;
1533 int bytes_read;
1534 int protocol;
1535 struct net_device *src_dev;
1536 struct sfe_ipv6_addr src_ip;
1537 struct sfe_ipv6_addr src_ip_xlate;
1538 __be16 src_port;
1539 __be16 src_port_xlate;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07001540 u64 src_rx_packets;
1541 u64 src_rx_bytes;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001542 struct net_device *dest_dev;
1543 struct sfe_ipv6_addr dest_ip;
1544 struct sfe_ipv6_addr dest_ip_xlate;
1545 __be16 dest_port;
1546 __be16 dest_port_xlate;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07001547 u64 dest_rx_packets;
1548 u64 dest_rx_bytes;
1549 u64 last_sync_jiffies;
1550 u32 mark, src_priority, dest_priority, src_dscp, dest_dscp;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301551 u32 packet, byte;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001552#ifdef CONFIG_NF_FLOW_COOKIE
1553 int src_flow_cookie, dst_flow_cookie;
1554#endif
1555
1556 spin_lock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07001557
1558 for (c = si->all_connections_head; c; c = c->all_connections_next) {
1559 if (c->debug_read_seq < si->debug_read_seq) {
1560 c->debug_read_seq = si->debug_read_seq;
1561 break;
1562 }
1563 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07001564
1565 /*
Xiaoping Fan34586472015-07-03 02:20:35 -07001566 * If there were no connections then move to the next state.
Xiaoping Fan978b3772015-05-27 14:15:18 -07001567 */
1568 if (!c) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07001569 spin_unlock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07001570 ws->state++;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001571 return true;
1572 }
1573
1574 original_cm = c->original_match;
1575 reply_cm = c->reply_match;
1576
1577 protocol = c->protocol;
1578 src_dev = c->original_dev;
1579 src_ip = c->src_ip[0];
1580 src_ip_xlate = c->src_ip_xlate[0];
1581 src_port = c->src_port;
1582 src_port_xlate = c->src_port_xlate;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001583 src_priority = original_cm->priority;
1584 src_dscp = original_cm->dscp >> SFE_IPV6_DSCP_SHIFT;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001585
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301586 sfe_ipv6_connection_match_update_summary_stats(original_cm, &packet, &byte);
1587 sfe_ipv6_connection_match_update_summary_stats(reply_cm, &packet, &byte);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001588
1589 src_rx_packets = original_cm->rx_packet_count64;
1590 src_rx_bytes = original_cm->rx_byte_count64;
1591 dest_dev = c->reply_dev;
1592 dest_ip = c->dest_ip[0];
1593 dest_ip_xlate = c->dest_ip_xlate[0];
1594 dest_port = c->dest_port;
1595 dest_port_xlate = c->dest_port_xlate;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001596 dest_priority = reply_cm->priority;
1597 dest_dscp = reply_cm->dscp >> SFE_IPV6_DSCP_SHIFT;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001598 dest_rx_packets = reply_cm->rx_packet_count64;
1599 dest_rx_bytes = reply_cm->rx_byte_count64;
1600 last_sync_jiffies = get_jiffies_64() - c->last_sync_jiffies;
1601 mark = c->mark;
1602#ifdef CONFIG_NF_FLOW_COOKIE
1603 src_flow_cookie = original_cm->flow_cookie;
1604 dst_flow_cookie = reply_cm->flow_cookie;
1605#endif
1606 spin_unlock_bh(&si->lock);
1607
1608 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t\t<connection "
1609 "protocol=\"%u\" "
1610 "src_dev=\"%s\" "
1611 "src_ip=\"%pI6\" src_ip_xlate=\"%pI6\" "
1612 "src_port=\"%u\" src_port_xlate=\"%u\" "
Xiaoping Fane1963d42015-08-25 17:06:19 -07001613 "src_priority=\"%u\" src_dscp=\"%u\" "
Xiaoping Fan978b3772015-05-27 14:15:18 -07001614 "src_rx_pkts=\"%llu\" src_rx_bytes=\"%llu\" "
1615 "dest_dev=\"%s\" "
1616 "dest_ip=\"%pI6\" dest_ip_xlate=\"%pI6\" "
1617 "dest_port=\"%u\" dest_port_xlate=\"%u\" "
Xiaoping Fane1963d42015-08-25 17:06:19 -07001618 "dest_priority=\"%u\" dest_dscp=\"%u\" "
Xiaoping Fan978b3772015-05-27 14:15:18 -07001619 "dest_rx_pkts=\"%llu\" dest_rx_bytes=\"%llu\" "
1620#ifdef CONFIG_NF_FLOW_COOKIE
1621 "src_flow_cookie=\"%d\" dst_flow_cookie=\"%d\" "
1622#endif
1623 "last_sync=\"%llu\" "
1624 "mark=\"%08x\" />\n",
1625 protocol,
1626 src_dev->name,
1627 &src_ip, &src_ip_xlate,
1628 ntohs(src_port), ntohs(src_port_xlate),
Xiaoping Fane1963d42015-08-25 17:06:19 -07001629 src_priority, src_dscp,
Xiaoping Fan978b3772015-05-27 14:15:18 -07001630 src_rx_packets, src_rx_bytes,
1631 dest_dev->name,
1632 &dest_ip, &dest_ip_xlate,
1633 ntohs(dest_port), ntohs(dest_port_xlate),
Xiaoping Fane1963d42015-08-25 17:06:19 -07001634 dest_priority, dest_dscp,
Xiaoping Fan978b3772015-05-27 14:15:18 -07001635 dest_rx_packets, dest_rx_bytes,
1636#ifdef CONFIG_NF_FLOW_COOKIE
1637 src_flow_cookie, dst_flow_cookie,
1638#endif
1639 last_sync_jiffies, mark);
1640
1641 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1642 return false;
1643 }
1644
1645 *length -= bytes_read;
1646 *total_read += bytes_read;
1647
Xiaoping Fan978b3772015-05-27 14:15:18 -07001648 return true;
1649}
1650
1651/*
1652 * sfe_ipv6_debug_dev_read_connections_end()
1653 * Generate part of the XML output.
1654 */
1655static bool sfe_ipv6_debug_dev_read_connections_end(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
1656 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
1657{
1658 int bytes_read;
1659
1660 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t</connections>\n");
1661 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1662 return false;
1663 }
1664
1665 *length -= bytes_read;
1666 *total_read += bytes_read;
1667
1668 ws->state++;
1669 return true;
1670}
1671
1672/*
1673 * sfe_ipv6_debug_dev_read_exceptions_start()
1674 * Generate part of the XML output.
1675 */
1676static bool sfe_ipv6_debug_dev_read_exceptions_start(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
1677 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
1678{
1679 int bytes_read;
1680
1681 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<exceptions>\n");
1682 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1683 return false;
1684 }
1685
1686 *length -= bytes_read;
1687 *total_read += bytes_read;
1688
1689 ws->state++;
1690 return true;
1691}
1692
1693/*
1694 * sfe_ipv6_debug_dev_read_exceptions_exception()
1695 * Generate part of the XML output.
1696 */
1697static bool sfe_ipv6_debug_dev_read_exceptions_exception(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
1698 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
1699{
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301700 int i;
1701 u64 val = 0;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001702
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301703 for_each_possible_cpu(i) {
1704 const struct sfe_ipv6_stats *s = per_cpu_ptr(si->stats_pcpu, i);
1705 val += s->exception_events64[ws->iter_exception];
1706 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07001707
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301708 if (val) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07001709 int bytes_read;
1710
1711 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE,
1712 "\t\t<exception name=\"%s\" count=\"%llu\" />\n",
1713 sfe_ipv6_exception_events_string[ws->iter_exception],
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301714 val);
1715
Xiaoping Fan978b3772015-05-27 14:15:18 -07001716 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1717 return false;
1718 }
1719
1720 *length -= bytes_read;
1721 *total_read += bytes_read;
1722 }
1723
1724 ws->iter_exception++;
1725 if (ws->iter_exception >= SFE_IPV6_EXCEPTION_EVENT_LAST) {
1726 ws->iter_exception = 0;
1727 ws->state++;
1728 }
1729
1730 return true;
1731}
1732
1733/*
1734 * sfe_ipv6_debug_dev_read_exceptions_end()
1735 * Generate part of the XML output.
1736 */
1737static bool sfe_ipv6_debug_dev_read_exceptions_end(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
1738 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
1739{
1740 int bytes_read;
1741
1742 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t</exceptions>\n");
1743 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1744 return false;
1745 }
1746
1747 *length -= bytes_read;
1748 *total_read += bytes_read;
1749
1750 ws->state++;
1751 return true;
1752}
1753
1754/*
1755 * sfe_ipv6_debug_dev_read_stats()
1756 * Generate part of the XML output.
1757 */
1758static bool sfe_ipv6_debug_dev_read_stats(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
1759 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
1760{
1761 int bytes_read;
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301762 struct sfe_ipv6_stats stats;
1763 unsigned int num_conn;
1764
1765 sfe_ipv6_update_summary_stats(si, &stats);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001766
1767 spin_lock_bh(&si->lock);
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301768 num_conn = si->num_connections;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001769 spin_unlock_bh(&si->lock);
1770
1771 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<stats "
1772 "num_connections=\"%u\" "
1773 "pkts_forwarded=\"%llu\" pkts_not_forwarded=\"%llu\" "
1774 "create_requests=\"%llu\" create_collisions=\"%llu\" "
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301775 "create_failures=\"%llu\" "
Xiaoping Fan978b3772015-05-27 14:15:18 -07001776 "destroy_requests=\"%llu\" destroy_misses=\"%llu\" "
1777 "flushes=\"%llu\" "
1778 "hash_hits=\"%llu\" hash_reorders=\"%llu\" />\n",
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301779
1780 num_conn,
1781 stats.packets_forwarded64,
1782 stats.packets_not_forwarded64,
1783 stats.connection_create_requests64,
1784 stats.connection_create_collisions64,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301785 stats.connection_create_failures64,
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301786 stats.connection_destroy_requests64,
1787 stats.connection_destroy_misses64,
1788 stats.connection_flushes64,
1789 stats.connection_match_hash_hits64,
1790 stats.connection_match_hash_reorders64);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001791 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1792 return false;
1793 }
1794
1795 *length -= bytes_read;
1796 *total_read += bytes_read;
1797
1798 ws->state++;
1799 return true;
1800}
1801
1802/*
1803 * sfe_ipv6_debug_dev_read_end()
1804 * Generate part of the XML output.
1805 */
1806static bool sfe_ipv6_debug_dev_read_end(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
1807 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
1808{
1809 int bytes_read;
1810
1811 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "</sfe_ipv6>\n");
1812 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1813 return false;
1814 }
1815
1816 *length -= bytes_read;
1817 *total_read += bytes_read;
1818
1819 ws->state++;
1820 return true;
1821}
1822
1823/*
1824 * Array of write functions that write various XML elements that correspond to
1825 * our XML output state machine.
1826 */
1827static sfe_ipv6_debug_xml_write_method_t sfe_ipv6_debug_xml_write_methods[SFE_IPV6_DEBUG_XML_STATE_DONE] = {
1828 sfe_ipv6_debug_dev_read_start,
1829 sfe_ipv6_debug_dev_read_connections_start,
1830 sfe_ipv6_debug_dev_read_connections_connection,
1831 sfe_ipv6_debug_dev_read_connections_end,
1832 sfe_ipv6_debug_dev_read_exceptions_start,
1833 sfe_ipv6_debug_dev_read_exceptions_exception,
1834 sfe_ipv6_debug_dev_read_exceptions_end,
1835 sfe_ipv6_debug_dev_read_stats,
1836 sfe_ipv6_debug_dev_read_end,
1837};
1838
1839/*
1840 * sfe_ipv6_debug_dev_read()
1841 * Send info to userspace upon read request from user
1842 */
1843static ssize_t sfe_ipv6_debug_dev_read(struct file *filp, char *buffer, size_t length, loff_t *offset)
1844{
1845 char msg[CHAR_DEV_MSG_SIZE];
1846 int total_read = 0;
1847 struct sfe_ipv6_debug_xml_write_state *ws;
1848 struct sfe_ipv6 *si = &__si6;
1849
1850 ws = (struct sfe_ipv6_debug_xml_write_state *)filp->private_data;
1851 while ((ws->state != SFE_IPV6_DEBUG_XML_STATE_DONE) && (length > CHAR_DEV_MSG_SIZE)) {
1852 if ((sfe_ipv6_debug_xml_write_methods[ws->state])(si, buffer, msg, &length, &total_read, ws)) {
1853 continue;
1854 }
1855 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07001856 return total_read;
1857}
1858
1859/*
Xiaoping Fan978b3772015-05-27 14:15:18 -07001860 * sfe_ipv6_debug_dev_open()
1861 */
1862static int sfe_ipv6_debug_dev_open(struct inode *inode, struct file *file)
1863{
1864 struct sfe_ipv6_debug_xml_write_state *ws;
1865
1866 ws = (struct sfe_ipv6_debug_xml_write_state *)file->private_data;
1867 if (ws) {
1868 return 0;
1869 }
1870
1871 ws = kzalloc(sizeof(struct sfe_ipv6_debug_xml_write_state), GFP_KERNEL);
1872 if (!ws) {
1873 return -ENOMEM;
1874 }
1875
1876 ws->state = SFE_IPV6_DEBUG_XML_STATE_START;
1877 file->private_data = ws;
1878
1879 return 0;
1880}
1881
1882/*
1883 * sfe_ipv6_debug_dev_release()
1884 */
1885static int sfe_ipv6_debug_dev_release(struct inode *inode, struct file *file)
1886{
1887 struct sfe_ipv6_debug_xml_write_state *ws;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001888
1889 ws = (struct sfe_ipv6_debug_xml_write_state *)file->private_data;
Xiaoping Fan34586472015-07-03 02:20:35 -07001890 if (ws) {
1891 /*
1892 * We've finished with our output so free the write state.
1893 */
1894 kfree(ws);
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301895 file->private_data = NULL;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001896 }
1897
Xiaoping Fan978b3772015-05-27 14:15:18 -07001898 return 0;
1899}
1900
1901/*
1902 * File operations used in the debug char device
1903 */
1904static struct file_operations sfe_ipv6_debug_dev_fops = {
1905 .read = sfe_ipv6_debug_dev_read,
Xiaoping Fan978b3772015-05-27 14:15:18 -07001906 .open = sfe_ipv6_debug_dev_open,
1907 .release = sfe_ipv6_debug_dev_release
1908};
1909
1910#ifdef CONFIG_NF_FLOW_COOKIE
1911/*
1912 * sfe_ipv6_register_flow_cookie_cb
1913 * register a function in SFE to let SFE use this function to configure flow cookie for a flow
1914 *
1915 * Hardware driver which support flow cookie should register a callback function in SFE. Then SFE
1916 * can use this function to configure flow cookie for a flow.
1917 * return: 0, success; !=0, fail
1918 */
1919int sfe_ipv6_register_flow_cookie_cb(sfe_ipv6_flow_cookie_set_func_t cb)
1920{
1921 struct sfe_ipv6 *si = &__si6;
1922
1923 BUG_ON(!cb);
1924
1925 if (si->flow_cookie_set_func) {
1926 return -1;
1927 }
1928
1929 rcu_assign_pointer(si->flow_cookie_set_func, cb);
1930 return 0;
1931}
1932
1933/*
1934 * sfe_ipv6_unregister_flow_cookie_cb
1935 * unregister function which is used to configure flow cookie for a flow
1936 *
1937 * return: 0, success; !=0, fail
1938 */
1939int sfe_ipv6_unregister_flow_cookie_cb(sfe_ipv6_flow_cookie_set_func_t cb)
1940{
1941 struct sfe_ipv6 *si = &__si6;
1942
1943 RCU_INIT_POINTER(si->flow_cookie_set_func, NULL);
1944 return 0;
1945}
Xiaoping Fan640faf42015-08-28 15:50:55 -07001946
1947/*
1948 * sfe_ipv6_get_flow_cookie()
1949 */
1950static ssize_t sfe_ipv6_get_flow_cookie(struct device *dev,
1951 struct device_attribute *attr,
1952 char *buf)
1953{
1954 struct sfe_ipv6 *si = &__si6;
Xiaoping Fan01c67cc2015-11-09 11:31:57 -08001955 return snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", si->flow_cookie_enable);
Xiaoping Fan640faf42015-08-28 15:50:55 -07001956}
1957
1958/*
1959 * sfe_ipv6_set_flow_cookie()
1960 */
1961static ssize_t sfe_ipv6_set_flow_cookie(struct device *dev,
1962 struct device_attribute *attr,
1963 const char *buf, size_t size)
1964{
1965 struct sfe_ipv6 *si = &__si6;
Ken Zhu137722d2021-09-23 17:57:36 -07001966 si->flow_cookie_enable = strict_strtol(buf, NULL, 0);
Xiaoping Fan640faf42015-08-28 15:50:55 -07001967
1968 return size;
1969}
1970
1971/*
1972 * sysfs attributes.
1973 */
1974static const struct device_attribute sfe_ipv6_flow_cookie_attr =
Xiaoping Fane70da412016-02-26 16:47:57 -08001975 __ATTR(flow_cookie_enable, S_IWUSR | S_IRUGO, sfe_ipv6_get_flow_cookie, sfe_ipv6_set_flow_cookie);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001976#endif /*CONFIG_NF_FLOW_COOKIE*/
1977
Ken Zhu137722d2021-09-23 17:57:36 -07001978/*
1979 * sfe_ipv6_get_cpu()
1980 */
1981static ssize_t sfe_ipv6_get_cpu(struct device *dev,
1982 struct device_attribute *attr,
1983 char *buf)
1984{
1985 struct sfe_ipv6 *si = &__si6;
1986 return snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", si->work_cpu);
1987}
1988
1989/*
1990 * sfe_ipv4_set_cpu()
1991 */
1992static ssize_t sfe_ipv6_set_cpu(struct device *dev,
1993 struct device_attribute *attr,
1994 const char *buf, size_t size)
1995{
1996 struct sfe_ipv6 *si = &__si6;
1997 int work_cpu;
1998
1999 work_cpu = simple_strtol(buf, NULL, 0);
2000 if ((work_cpu >= 0) && (work_cpu <= NR_CPUS)) {
2001 si->work_cpu = work_cpu;
2002 } else {
2003 dev_err(dev, "%s is not in valid range[0,%d]", buf, NR_CPUS);
2004 }
2005
2006 return size;
2007}
2008/*
2009 * sysfs attributes.
2010 */
2011static const struct device_attribute sfe_ipv6_cpu_attr =
2012 __ATTR(stat_work_cpu, S_IWUSR | S_IRUGO, sfe_ipv6_get_cpu, sfe_ipv6_set_cpu);
2013
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05302014 /*
2015 * sfe_ipv6_hash_init()
2016 * Initialize conn match hash lists
2017 */
2018static void sfe_ipv6_conn_match_hash_init(struct sfe_ipv6 *si, int len)
2019{
2020 struct hlist_head *hash_list = si->hlist_conn_match_hash_head;
2021 int i;
2022
2023 for (i = 0; i < len; i++) {
2024 INIT_HLIST_HEAD(&hash_list[i]);
2025 }
2026}
2027
Xiaoping Fan978b3772015-05-27 14:15:18 -07002028/*
2029 * sfe_ipv6_init()
2030 */
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05302031int sfe_ipv6_init(void)
Xiaoping Fan978b3772015-05-27 14:15:18 -07002032{
2033 struct sfe_ipv6 *si = &__si6;
2034 int result = -1;
2035
2036 DEBUG_INFO("SFE IPv6 init\n");
2037
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05302038 sfe_ipv6_conn_match_hash_init(si, ARRAY_SIZE(si->hlist_conn_match_hash_head));
2039
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302040 si->stats_pcpu = alloc_percpu_gfp(struct sfe_ipv6_stats, GFP_KERNEL | __GFP_ZERO);
2041 if (!si->stats_pcpu) {
2042 DEBUG_ERROR("failed to allocate stats memory for sfe_ipv6\n");
2043 goto exit0;
2044 }
2045
Xiaoping Fan978b3772015-05-27 14:15:18 -07002046 /*
2047 * Create sys/sfe_ipv6
2048 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302049 si->sys_ipv6 = kobject_create_and_add("sfe_ipv6", NULL);
2050 if (!si->sys_ipv6) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07002051 DEBUG_ERROR("failed to register sfe_ipv6\n");
2052 goto exit1;
2053 }
2054
2055 /*
2056 * Create files, one for each parameter supported by this module.
2057 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302058 result = sysfs_create_file(si->sys_ipv6, &sfe_ipv6_debug_dev_attr.attr);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002059 if (result) {
2060 DEBUG_ERROR("failed to register debug dev file: %d\n", result);
2061 goto exit2;
2062 }
2063
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302064 result = sysfs_create_file(si->sys_ipv6, &sfe_ipv6_cpu_attr.attr);
Ken Zhu137722d2021-09-23 17:57:36 -07002065 if (result) {
2066 DEBUG_ERROR("failed to register debug dev file: %d\n", result);
2067 goto exit3;
2068 }
2069
Xiaoping Fan640faf42015-08-28 15:50:55 -07002070#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302071 result = sysfs_create_file(si->sys_ipv6, &sfe_ipv6_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002072 if (result) {
2073 DEBUG_ERROR("failed to register flow cookie enable file: %d\n", result);
Ken Zhu137722d2021-09-23 17:57:36 -07002074 goto exit4;
Xiaoping Fan640faf42015-08-28 15:50:55 -07002075 }
2076#endif /* CONFIG_NF_FLOW_COOKIE */
2077
Xiaoping Fan978b3772015-05-27 14:15:18 -07002078 /*
2079 * Register our debug char device.
2080 */
2081 result = register_chrdev(0, "sfe_ipv6", &sfe_ipv6_debug_dev_fops);
2082 if (result < 0) {
2083 DEBUG_ERROR("Failed to register chrdev: %d\n", result);
Ken Zhu137722d2021-09-23 17:57:36 -07002084 goto exit5;
Xiaoping Fan978b3772015-05-27 14:15:18 -07002085 }
2086
2087 si->debug_dev = result;
Ken Zhu137722d2021-09-23 17:57:36 -07002088 si->work_cpu = WORK_CPU_UNBOUND;
Xiaoping Fan978b3772015-05-27 14:15:18 -07002089
2090 /*
Ken Zhu137722d2021-09-23 17:57:36 -07002091 * Create work to handle periodic statistics.
Xiaoping Fan978b3772015-05-27 14:15:18 -07002092 */
Ken Zhu137722d2021-09-23 17:57:36 -07002093 INIT_DELAYED_WORK(&(si->sync_dwork), sfe_ipv6_periodic_sync);
2094 schedule_delayed_work_on(si->work_cpu, &(si->sync_dwork), ((HZ + 99) / 100));
Xiaoping Fan978b3772015-05-27 14:15:18 -07002095 spin_lock_init(&si->lock);
2096
2097 return 0;
2098
Ken Zhu137722d2021-09-23 17:57:36 -07002099exit5:
Xiaoping Fan640faf42015-08-28 15:50:55 -07002100#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302101 sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002102
Ken Zhu137722d2021-09-23 17:57:36 -07002103exit4:
Xiaoping Fan640faf42015-08-28 15:50:55 -07002104#endif /* CONFIG_NF_FLOW_COOKIE */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302105 sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_cpu_attr.attr);
Ken Zhu137722d2021-09-23 17:57:36 -07002106exit3:
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302107 sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_debug_dev_attr.attr);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002108
2109exit2:
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302110 kobject_put(si->sys_ipv6);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002111
2112exit1:
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302113 free_percpu(si->stats_pcpu);
2114
2115exit0:
Xiaoping Fan978b3772015-05-27 14:15:18 -07002116 return result;
2117}
2118
2119/*
2120 * sfe_ipv6_exit()
2121 */
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05302122void sfe_ipv6_exit(void)
Xiaoping Fan978b3772015-05-27 14:15:18 -07002123{
2124 struct sfe_ipv6 *si = &__si6;
2125
2126 DEBUG_INFO("SFE IPv6 exit\n");
2127
2128 /*
2129 * Destroy all connections.
2130 */
2131 sfe_ipv6_destroy_all_rules_for_dev(NULL);
2132
Ken Zhu137722d2021-09-23 17:57:36 -07002133 cancel_delayed_work(&si->sync_dwork);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002134
2135 unregister_chrdev(si->debug_dev, "sfe_ipv6");
2136
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302137 free_percpu(si->stats_pcpu);
2138
Xiaoping Fan640faf42015-08-28 15:50:55 -07002139#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302140 sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002141#endif /* CONFIG_NF_FLOW_COOKIE */
Ken Zhu137722d2021-09-23 17:57:36 -07002142
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302143 sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_cpu_attr.attr);
Ken Zhu137722d2021-09-23 17:57:36 -07002144
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302145 sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_debug_dev_attr.attr);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002146
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302147 kobject_put(si->sys_ipv6);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002148}
2149
Xiaoping Fan978b3772015-05-27 14:15:18 -07002150#ifdef CONFIG_NF_FLOW_COOKIE
2151EXPORT_SYMBOL(sfe_ipv6_register_flow_cookie_cb);
2152EXPORT_SYMBOL(sfe_ipv6_unregister_flow_cookie_cb);
2153#endif