blob: 50c35b623c940b5c783fd107bae239815955aba4 [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
Xiaoping Fan978b3772015-05-27 14:15:18 -07001078
1079 /*
Ken Zhubbf49652021-09-12 15:33:09 -07001080 * For the non-arp interface, we don't write L2 HDR.
Xiaoping Fan978b3772015-05-27 14:15:18 -07001081 */
Ken Zhubbf49652021-09-12 15:33:09 -07001082 if (!(dest_dev->flags & IFF_NOARP)) {
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301083
1084 /*
1085 * Check whether the rule has configured a specific source MAC address to use.
1086 * This is needed when virtual L3 interfaces such as br-lan, macvlan, vlan are used during egress
1087 */
1088 if ((msg->valid_flags & SFE_RULE_CREATE_SRC_MAC_VALID) &&
1089 (msg->src_mac_rule.mac_valid_flags & SFE_SRC_MAC_RETURN_VALID)) {
1090 ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)msg->src_mac_rule.return_src_mac);
1091 } else {
1092 ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)dest_dev->dev_addr);
1093 }
1094 ether_addr_copy((u8 *)original_cm->xmit_dest_mac, (u8 *)msg->conn_rule.return_mac);
1095
Xiaoping Fan978b3772015-05-27 14:15:18 -07001096 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_WRITE_L2_HDR;
1097
1098 /*
1099 * If our dev writes Ethernet headers then we can write a really fast
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301100 * version
Xiaoping Fan978b3772015-05-27 14:15:18 -07001101 */
1102 if (dest_dev->header_ops) {
1103 if (dest_dev->header_ops->create == eth_header) {
1104 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR;
1105 }
1106 }
1107 }
1108
1109 /*
1110 * Fill in the "reply" direction connection matching object.
1111 */
1112 reply_cm->match_dev = dest_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301113 reply_cm->match_protocol = tuple->protocol;
1114 reply_cm->match_src_ip[0] = *(struct sfe_ipv6_addr *)tuple->return_ip;
1115 reply_cm->match_src_port = tuple->return_ident;
1116 reply_cm->match_dest_ip[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip;
1117 reply_cm->match_dest_port = tuple->flow_ident;
1118 reply_cm->xlate_src_ip[0] = *(struct sfe_ipv6_addr *)tuple->return_ip;
1119 reply_cm->xlate_src_port = tuple->return_ident;
1120 reply_cm->xlate_dest_ip[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip;
1121 reply_cm->xlate_dest_port = tuple->flow_ident;
1122
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301123 atomic_set(&original_cm->rx_byte_count, 0);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001124 reply_cm->rx_packet_count64 = 0;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301125 atomic_set(&reply_cm->rx_byte_count, 0);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001126 reply_cm->rx_byte_count64 = 0;
1127 reply_cm->xmit_dev = src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301128 reply_cm->xmit_dev_mtu = msg->conn_rule.flow_mtu;
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301129
Xiaoping Fan978b3772015-05-27 14:15:18 -07001130 reply_cm->connection = c;
1131 reply_cm->counter_match = original_cm;
1132 reply_cm->flags = 0;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301133 if (msg->valid_flags & SFE_RULE_CREATE_QOS_VALID) {
1134 reply_cm->priority = msg->qos_rule.return_qos_tag;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001135 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_PRIORITY_REMARK;
1136 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301137 if (msg->valid_flags & SFE_RULE_CREATE_DSCP_MARKING_VALID) {
1138 reply_cm->dscp = msg->dscp_rule.return_dscp << SFE_IPV6_DSCP_SHIFT;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001139 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_DSCP_REMARK;
1140 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07001141#ifdef CONFIG_NF_FLOW_COOKIE
1142 reply_cm->flow_cookie = 0;
1143#endif
Zhi Chen8748eb32015-06-18 12:58:48 -07001144#ifdef CONFIG_XFRM
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301145 if (msg->valid_flags & SFE_RULE_CREATE_DIRECTION_VALID) {
1146 reply_cm->flow_accel = msg->direction_rule.return_accel;
1147 } else {
1148 reply_cm->flow_accel = 1;
1149 }
Zhi Chen8748eb32015-06-18 12:58:48 -07001150#endif
Xiaoping Fan978b3772015-05-27 14:15:18 -07001151 /*
Ken Zhubbf49652021-09-12 15:33:09 -07001152 * For the non-arp interface, we don't write L2 HDR.
Xiaoping Fan978b3772015-05-27 14:15:18 -07001153 */
Ken Zhubbf49652021-09-12 15:33:09 -07001154 if (!(src_dev->flags & IFF_NOARP)) {
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301155
1156 /*
1157 * Check whether the rule has configured a specific source MAC address to use.
1158 * This is needed when virtual L3 interfaces such as br-lan, macvlan, vlan are used during egress
1159 */
1160 if ((msg->valid_flags & SFE_RULE_CREATE_SRC_MAC_VALID) &&
1161 (msg->src_mac_rule.mac_valid_flags & SFE_SRC_MAC_FLOW_VALID)) {
1162 ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)msg->src_mac_rule.flow_src_mac);
1163 } else {
1164 ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)src_dev->dev_addr);
1165 }
1166
1167 ether_addr_copy((u8 *)reply_cm->xmit_dest_mac, (u8 *)msg->conn_rule.flow_mac);
1168
Xiaoping Fan978b3772015-05-27 14:15:18 -07001169 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_WRITE_L2_HDR;
1170
1171 /*
1172 * If our dev writes Ethernet headers then we can write a really fast
1173 * version.
1174 */
1175 if (src_dev->header_ops) {
1176 if (src_dev->header_ops->create == eth_header) {
1177 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR;
1178 }
1179 }
1180 }
1181
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301182 /*
1183 * No support for NAT in ipv6
1184 */
Xiaoping Fan978b3772015-05-27 14:15:18 -07001185
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301186 c->protocol = tuple->protocol;
1187 c->src_ip[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip;
1188 c->src_ip_xlate[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip;
1189 c->src_port = tuple->flow_ident;
1190 c->src_port_xlate = tuple->flow_ident;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001191 c->original_dev = src_dev;
1192 c->original_match = original_cm;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301193
1194 c->dest_ip[0] = *(struct sfe_ipv6_addr *)tuple->return_ip;
1195 c->dest_ip_xlate[0] = *(struct sfe_ipv6_addr *)tuple->return_ip;
1196 c->dest_port = tuple->return_ident;
1197 c->dest_port_xlate = tuple->return_ident;
1198
Xiaoping Fan978b3772015-05-27 14:15:18 -07001199 c->reply_dev = dest_dev;
1200 c->reply_match = reply_cm;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301201 c->mark = 0; /* No mark support */
Xiaoping Fan34586472015-07-03 02:20:35 -07001202 c->debug_read_seq = 0;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001203 c->last_sync_jiffies = get_jiffies_64();
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301204 c->removed = false;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001205
1206 /*
Xiaoping Fan978b3772015-05-27 14:15:18 -07001207 * Initialize the protocol-specific information that we track.
1208 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301209 switch (tuple->protocol) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07001210 case IPPROTO_TCP:
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301211 original_cm->protocol_state.tcp.win_scale = msg->tcp_rule.flow_window_scale;
1212 original_cm->protocol_state.tcp.max_win = msg->tcp_rule.flow_max_window ? msg->tcp_rule.flow_max_window : 1;
1213 original_cm->protocol_state.tcp.end = msg->tcp_rule.flow_end;
1214 original_cm->protocol_state.tcp.max_end = msg->tcp_rule.flow_max_end;
1215 reply_cm->protocol_state.tcp.win_scale = msg->tcp_rule.return_window_scale;
1216 reply_cm->protocol_state.tcp.max_win = msg->tcp_rule.return_max_window ? msg->tcp_rule.return_max_window : 1;
1217 reply_cm->protocol_state.tcp.end = msg->tcp_rule.return_end;
1218 reply_cm->protocol_state.tcp.max_end = msg->tcp_rule.return_max_end;
1219 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_NO_SEQ_CHECK) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07001220 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
1221 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
1222 }
1223 break;
1224 }
1225
1226 sfe_ipv6_connection_match_compute_translations(original_cm);
1227 sfe_ipv6_connection_match_compute_translations(reply_cm);
1228 sfe_ipv6_insert_connection(si, c);
1229
1230 spin_unlock_bh(&si->lock);
1231
1232 /*
1233 * We have everything we need!
1234 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301235 DEBUG_INFO("new connection - p: %d\n"
Tian Yang45f39c82020-10-06 14:07:47 -07001236 " s: %s:%pxM(%pxM):%pI6(%pI6):%u(%u)\n"
1237 " d: %s:%pxM(%pxM):%pI6(%pI6):%u(%u)\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301238 tuple->protocol,
1239 src_dev->name, msg->conn_rule.flow_mac, NULL,
1240 (void *)tuple->flow_ip, (void *)tuple->flow_ip, ntohs(tuple->flow_ident), ntohs(tuple->flow_ident),
1241 dest_dev->name, NULL, msg->conn_rule.return_mac,
1242 (void *)tuple->return_ip, (void *)tuple->return_ip, ntohs(tuple->return_ident), ntohs(tuple->return_ident));
Xiaoping Fan978b3772015-05-27 14:15:18 -07001243
1244 return 0;
1245}
1246
1247/*
1248 * sfe_ipv6_destroy_rule()
1249 * Destroy a forwarding rule.
1250 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301251void sfe_ipv6_destroy_rule(struct sfe_ipv6_rule_destroy_msg *msg)
Xiaoping Fan978b3772015-05-27 14:15:18 -07001252{
1253 struct sfe_ipv6 *si = &__si6;
1254 struct sfe_ipv6_connection *c;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301255 bool ret;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301256 struct sfe_ipv6_5tuple *tuple = &msg->tuple;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001257
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301258 this_cpu_inc(si->stats_pcpu->connection_destroy_requests64);
1259
Xiaoping Fan978b3772015-05-27 14:15:18 -07001260 spin_lock_bh(&si->lock);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001261
1262 /*
1263 * Check to see if we have a flow that matches the rule we're trying
1264 * to destroy. If there isn't then we can't destroy it.
1265 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301266 c = sfe_ipv6_find_connection(si, tuple->protocol, (struct sfe_ipv6_addr *)tuple->flow_ip, tuple->flow_ident,
1267 (struct sfe_ipv6_addr *)tuple->return_ip, tuple->return_ident);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001268 if (!c) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07001269 spin_unlock_bh(&si->lock);
1270
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301271 this_cpu_inc(si->stats_pcpu->connection_destroy_misses64);
1272
Xiaoping Fan978b3772015-05-27 14:15:18 -07001273 DEBUG_TRACE("connection does not exist - p: %d, s: %pI6:%u, d: %pI6:%u\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301274 tuple->protocol, tuple->flow_ip, ntohs(tuple->flow_ident),
1275 tuple->return_ip, ntohs(tuple->return_ident));
Xiaoping Fan978b3772015-05-27 14:15:18 -07001276 return;
1277 }
1278
1279 /*
1280 * Remove our connection details from the hash tables.
1281 */
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301282 ret = sfe_ipv6_remove_connection(si, c);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001283 spin_unlock_bh(&si->lock);
1284
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301285 if (ret) {
1286 sfe_ipv6_flush_connection(si, c, SFE_SYNC_REASON_DESTROY);
1287 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07001288
1289 DEBUG_INFO("connection destroyed - p: %d, s: %pI6:%u, d: %pI6:%u\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301290 tuple->protocol, tuple->flow_ip, ntohs(tuple->flow_ident),
1291 tuple->return_ip, ntohs(tuple->return_ident));
Xiaoping Fan978b3772015-05-27 14:15:18 -07001292}
1293
1294/*
1295 * sfe_ipv6_register_sync_rule_callback()
1296 * Register a callback for rule synchronization.
1297 */
1298void sfe_ipv6_register_sync_rule_callback(sfe_sync_rule_callback_t sync_rule_callback)
1299{
1300 struct sfe_ipv6 *si = &__si6;
1301
1302 spin_lock_bh(&si->lock);
1303 rcu_assign_pointer(si->sync_rule_callback, sync_rule_callback);
1304 spin_unlock_bh(&si->lock);
1305}
1306
1307/*
1308 * sfe_ipv6_get_debug_dev()
1309 */
1310static ssize_t sfe_ipv6_get_debug_dev(struct device *dev,
1311 struct device_attribute *attr,
1312 char *buf)
1313{
1314 struct sfe_ipv6 *si = &__si6;
1315 ssize_t count;
1316 int num;
1317
1318 spin_lock_bh(&si->lock);
1319 num = si->debug_dev;
1320 spin_unlock_bh(&si->lock);
1321
1322 count = snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", num);
1323 return count;
1324}
1325
1326/*
1327 * sfe_ipv6_destroy_all_rules_for_dev()
1328 * Destroy all connections that match a particular device.
1329 *
1330 * If we pass dev as NULL then this destroys all connections.
1331 */
1332void sfe_ipv6_destroy_all_rules_for_dev(struct net_device *dev)
1333{
1334 struct sfe_ipv6 *si = &__si6;
1335 struct sfe_ipv6_connection *c;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301336 bool ret;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001337
Xiaoping Fan34586472015-07-03 02:20:35 -07001338another_round:
Xiaoping Fan978b3772015-05-27 14:15:18 -07001339 spin_lock_bh(&si->lock);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001340
Xiaoping Fan34586472015-07-03 02:20:35 -07001341 for (c = si->all_connections_head; c; c = c->all_connections_next) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07001342 /*
Xiaoping Fan34586472015-07-03 02:20:35 -07001343 * Does this connection relate to the device we are destroying?
Xiaoping Fan978b3772015-05-27 14:15:18 -07001344 */
1345 if (!dev
1346 || (dev == c->original_dev)
1347 || (dev == c->reply_dev)) {
Xiaoping Fan34586472015-07-03 02:20:35 -07001348 break;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001349 }
Xiaoping Fan34586472015-07-03 02:20:35 -07001350 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07001351
Xiaoping Fan34586472015-07-03 02:20:35 -07001352 if (c) {
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301353 ret = sfe_ipv6_remove_connection(si, c);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001354 }
1355
1356 spin_unlock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07001357
1358 if (c) {
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301359 if (ret) {
1360 sfe_ipv6_flush_connection(si, c, SFE_SYNC_REASON_DESTROY);
1361 }
Xiaoping Fan34586472015-07-03 02:20:35 -07001362 goto another_round;
1363 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07001364}
1365
1366/*
1367 * sfe_ipv6_periodic_sync()
1368 */
Ken Zhu137722d2021-09-23 17:57:36 -07001369static void sfe_ipv6_periodic_sync(struct work_struct *work)
Xiaoping Fan978b3772015-05-27 14:15:18 -07001370{
Ken Zhu137722d2021-09-23 17:57:36 -07001371 struct sfe_ipv6 *si = container_of((struct delayed_work *)work, struct sfe_ipv6, sync_dwork);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07001372 u64 now_jiffies;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001373 int quota;
1374 sfe_sync_rule_callback_t sync_rule_callback;
Ken Zhu32b95392021-09-03 13:52:04 -07001375 struct sfe_ipv6_connection *c;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001376
1377 now_jiffies = get_jiffies_64();
1378
1379 rcu_read_lock();
1380 sync_rule_callback = rcu_dereference(si->sync_rule_callback);
1381 if (!sync_rule_callback) {
1382 rcu_read_unlock();
1383 goto done;
1384 }
1385
1386 spin_lock_bh(&si->lock);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001387
1388 /*
Ken Zhu32b95392021-09-03 13:52:04 -07001389 * If we have reached the end of the connection list, walk from
1390 * the connection head.
1391 */
1392 c = si->wc_next;
1393 if (unlikely(!c)) {
1394 c = si->all_connections_head;
1395 }
1396 /*
Xiaoping Fan978b3772015-05-27 14:15:18 -07001397 * Get an estimate of the number of connections to parse in this sync.
1398 */
1399 quota = (si->num_connections + 63) / 64;
1400
1401 /*
Ken Zhu32b95392021-09-03 13:52:04 -07001402 * Walk the "all connection" list and sync the connection state.
Xiaoping Fan978b3772015-05-27 14:15:18 -07001403 */
Ken Zhu32b95392021-09-03 13:52:04 -07001404 while (likely(c && quota)) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07001405 struct sfe_ipv6_connection_match *cm;
1406 struct sfe_ipv6_connection_match *counter_cm;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001407 struct sfe_connection_sync sis;
1408
Ken Zhu32b95392021-09-03 13:52:04 -07001409 cm = c->original_match;
1410 counter_cm = c->reply_match;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001411
1412 /*
Ken Zhu32b95392021-09-03 13:52:04 -07001413 * Didn't receive packets in the origial direction or reply
1414 * direction, move to the next connection.
Xiaoping Fan978b3772015-05-27 14:15:18 -07001415 */
Ken Zhu32b95392021-09-03 13:52:04 -07001416 if (!atomic_read(&cm->rx_packet_count) && !atomic_read(&counter_cm->rx_packet_count)) {
1417 c = c->all_connections_next;
1418 continue;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001419 }
1420
Ken Zhu32b95392021-09-03 13:52:04 -07001421 quota--;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001422
1423 /*
1424 * Sync the connection state.
1425 */
Xiaoping Fan99cb4c12015-08-21 19:07:32 -07001426 sfe_ipv6_gen_sync_connection(si, c, &sis, SFE_SYNC_REASON_STATS, now_jiffies);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001427
Ken Zhu32b95392021-09-03 13:52:04 -07001428 si->wc_next = c->all_connections_next;
1429
Xiaoping Fan978b3772015-05-27 14:15:18 -07001430 spin_unlock_bh(&si->lock);
1431 sync_rule_callback(&sis);
1432 spin_lock_bh(&si->lock);
Ken Zhu32b95392021-09-03 13:52:04 -07001433
1434 /*
1435 * c must be set and used in the same lock/unlock window;
1436 * because c could be removed when we don't hold the lock,
1437 * so delay grabbing until after the callback and relock.
1438 */
1439 c = si->wc_next;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001440 }
1441
Ken Zhu32b95392021-09-03 13:52:04 -07001442 /*
1443 * At the end of loop, put wc_next to the connection we left
1444 */
1445 si->wc_next = c;
1446
Xiaoping Fan978b3772015-05-27 14:15:18 -07001447 spin_unlock_bh(&si->lock);
1448 rcu_read_unlock();
1449
1450done:
Ken Zhu137722d2021-09-23 17:57:36 -07001451 schedule_delayed_work_on(si->work_cpu, (struct delayed_work *)work, ((HZ + 99) / 100));
Xiaoping Fan978b3772015-05-27 14:15:18 -07001452}
1453
1454/*
1455 * sfe_ipv6_debug_dev_read_start()
1456 * Generate part of the XML output.
1457 */
1458static bool sfe_ipv6_debug_dev_read_start(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
1459 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
1460{
1461 int bytes_read;
1462
Xiaoping Fan34586472015-07-03 02:20:35 -07001463 si->debug_read_seq++;
1464
Xiaoping Fan978b3772015-05-27 14:15:18 -07001465 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "<sfe_ipv6>\n");
1466 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1467 return false;
1468 }
1469
1470 *length -= bytes_read;
1471 *total_read += bytes_read;
1472
1473 ws->state++;
1474 return true;
1475}
1476
1477/*
1478 * sfe_ipv6_debug_dev_read_connections_start()
1479 * Generate part of the XML output.
1480 */
1481static bool sfe_ipv6_debug_dev_read_connections_start(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
1482 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
1483{
1484 int bytes_read;
1485
1486 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<connections>\n");
1487 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1488 return false;
1489 }
1490
1491 *length -= bytes_read;
1492 *total_read += bytes_read;
1493
1494 ws->state++;
1495 return true;
1496}
1497
1498/*
1499 * sfe_ipv6_debug_dev_read_connections_connection()
1500 * Generate part of the XML output.
1501 */
1502static bool sfe_ipv6_debug_dev_read_connections_connection(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
1503 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
1504{
1505 struct sfe_ipv6_connection *c;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001506 struct sfe_ipv6_connection_match *original_cm;
1507 struct sfe_ipv6_connection_match *reply_cm;
1508 int bytes_read;
1509 int protocol;
1510 struct net_device *src_dev;
1511 struct sfe_ipv6_addr src_ip;
1512 struct sfe_ipv6_addr src_ip_xlate;
1513 __be16 src_port;
1514 __be16 src_port_xlate;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07001515 u64 src_rx_packets;
1516 u64 src_rx_bytes;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001517 struct net_device *dest_dev;
1518 struct sfe_ipv6_addr dest_ip;
1519 struct sfe_ipv6_addr dest_ip_xlate;
1520 __be16 dest_port;
1521 __be16 dest_port_xlate;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07001522 u64 dest_rx_packets;
1523 u64 dest_rx_bytes;
1524 u64 last_sync_jiffies;
1525 u32 mark, src_priority, dest_priority, src_dscp, dest_dscp;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301526 u32 packet, byte;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001527#ifdef CONFIG_NF_FLOW_COOKIE
1528 int src_flow_cookie, dst_flow_cookie;
1529#endif
1530
1531 spin_lock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07001532
1533 for (c = si->all_connections_head; c; c = c->all_connections_next) {
1534 if (c->debug_read_seq < si->debug_read_seq) {
1535 c->debug_read_seq = si->debug_read_seq;
1536 break;
1537 }
1538 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07001539
1540 /*
Xiaoping Fan34586472015-07-03 02:20:35 -07001541 * If there were no connections then move to the next state.
Xiaoping Fan978b3772015-05-27 14:15:18 -07001542 */
1543 if (!c) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07001544 spin_unlock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07001545 ws->state++;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001546 return true;
1547 }
1548
1549 original_cm = c->original_match;
1550 reply_cm = c->reply_match;
1551
1552 protocol = c->protocol;
1553 src_dev = c->original_dev;
1554 src_ip = c->src_ip[0];
1555 src_ip_xlate = c->src_ip_xlate[0];
1556 src_port = c->src_port;
1557 src_port_xlate = c->src_port_xlate;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001558 src_priority = original_cm->priority;
1559 src_dscp = original_cm->dscp >> SFE_IPV6_DSCP_SHIFT;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001560
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301561 sfe_ipv6_connection_match_update_summary_stats(original_cm, &packet, &byte);
1562 sfe_ipv6_connection_match_update_summary_stats(reply_cm, &packet, &byte);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001563
1564 src_rx_packets = original_cm->rx_packet_count64;
1565 src_rx_bytes = original_cm->rx_byte_count64;
1566 dest_dev = c->reply_dev;
1567 dest_ip = c->dest_ip[0];
1568 dest_ip_xlate = c->dest_ip_xlate[0];
1569 dest_port = c->dest_port;
1570 dest_port_xlate = c->dest_port_xlate;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001571 dest_priority = reply_cm->priority;
1572 dest_dscp = reply_cm->dscp >> SFE_IPV6_DSCP_SHIFT;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001573 dest_rx_packets = reply_cm->rx_packet_count64;
1574 dest_rx_bytes = reply_cm->rx_byte_count64;
1575 last_sync_jiffies = get_jiffies_64() - c->last_sync_jiffies;
1576 mark = c->mark;
1577#ifdef CONFIG_NF_FLOW_COOKIE
1578 src_flow_cookie = original_cm->flow_cookie;
1579 dst_flow_cookie = reply_cm->flow_cookie;
1580#endif
1581 spin_unlock_bh(&si->lock);
1582
1583 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t\t<connection "
1584 "protocol=\"%u\" "
1585 "src_dev=\"%s\" "
1586 "src_ip=\"%pI6\" src_ip_xlate=\"%pI6\" "
1587 "src_port=\"%u\" src_port_xlate=\"%u\" "
Xiaoping Fane1963d42015-08-25 17:06:19 -07001588 "src_priority=\"%u\" src_dscp=\"%u\" "
Xiaoping Fan978b3772015-05-27 14:15:18 -07001589 "src_rx_pkts=\"%llu\" src_rx_bytes=\"%llu\" "
1590 "dest_dev=\"%s\" "
1591 "dest_ip=\"%pI6\" dest_ip_xlate=\"%pI6\" "
1592 "dest_port=\"%u\" dest_port_xlate=\"%u\" "
Xiaoping Fane1963d42015-08-25 17:06:19 -07001593 "dest_priority=\"%u\" dest_dscp=\"%u\" "
Xiaoping Fan978b3772015-05-27 14:15:18 -07001594 "dest_rx_pkts=\"%llu\" dest_rx_bytes=\"%llu\" "
1595#ifdef CONFIG_NF_FLOW_COOKIE
1596 "src_flow_cookie=\"%d\" dst_flow_cookie=\"%d\" "
1597#endif
1598 "last_sync=\"%llu\" "
1599 "mark=\"%08x\" />\n",
1600 protocol,
1601 src_dev->name,
1602 &src_ip, &src_ip_xlate,
1603 ntohs(src_port), ntohs(src_port_xlate),
Xiaoping Fane1963d42015-08-25 17:06:19 -07001604 src_priority, src_dscp,
Xiaoping Fan978b3772015-05-27 14:15:18 -07001605 src_rx_packets, src_rx_bytes,
1606 dest_dev->name,
1607 &dest_ip, &dest_ip_xlate,
1608 ntohs(dest_port), ntohs(dest_port_xlate),
Xiaoping Fane1963d42015-08-25 17:06:19 -07001609 dest_priority, dest_dscp,
Xiaoping Fan978b3772015-05-27 14:15:18 -07001610 dest_rx_packets, dest_rx_bytes,
1611#ifdef CONFIG_NF_FLOW_COOKIE
1612 src_flow_cookie, dst_flow_cookie,
1613#endif
1614 last_sync_jiffies, mark);
1615
1616 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1617 return false;
1618 }
1619
1620 *length -= bytes_read;
1621 *total_read += bytes_read;
1622
Xiaoping Fan978b3772015-05-27 14:15:18 -07001623 return true;
1624}
1625
1626/*
1627 * sfe_ipv6_debug_dev_read_connections_end()
1628 * Generate part of the XML output.
1629 */
1630static bool sfe_ipv6_debug_dev_read_connections_end(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
1631 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
1632{
1633 int bytes_read;
1634
1635 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t</connections>\n");
1636 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1637 return false;
1638 }
1639
1640 *length -= bytes_read;
1641 *total_read += bytes_read;
1642
1643 ws->state++;
1644 return true;
1645}
1646
1647/*
1648 * sfe_ipv6_debug_dev_read_exceptions_start()
1649 * Generate part of the XML output.
1650 */
1651static bool sfe_ipv6_debug_dev_read_exceptions_start(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
1652 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
1653{
1654 int bytes_read;
1655
1656 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<exceptions>\n");
1657 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1658 return false;
1659 }
1660
1661 *length -= bytes_read;
1662 *total_read += bytes_read;
1663
1664 ws->state++;
1665 return true;
1666}
1667
1668/*
1669 * sfe_ipv6_debug_dev_read_exceptions_exception()
1670 * Generate part of the XML output.
1671 */
1672static bool sfe_ipv6_debug_dev_read_exceptions_exception(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
1673 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
1674{
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301675 int i;
1676 u64 val = 0;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001677
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301678 for_each_possible_cpu(i) {
1679 const struct sfe_ipv6_stats *s = per_cpu_ptr(si->stats_pcpu, i);
1680 val += s->exception_events64[ws->iter_exception];
1681 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07001682
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301683 if (val) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07001684 int bytes_read;
1685
1686 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE,
1687 "\t\t<exception name=\"%s\" count=\"%llu\" />\n",
1688 sfe_ipv6_exception_events_string[ws->iter_exception],
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301689 val);
1690
Xiaoping Fan978b3772015-05-27 14:15:18 -07001691 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1692 return false;
1693 }
1694
1695 *length -= bytes_read;
1696 *total_read += bytes_read;
1697 }
1698
1699 ws->iter_exception++;
1700 if (ws->iter_exception >= SFE_IPV6_EXCEPTION_EVENT_LAST) {
1701 ws->iter_exception = 0;
1702 ws->state++;
1703 }
1704
1705 return true;
1706}
1707
1708/*
1709 * sfe_ipv6_debug_dev_read_exceptions_end()
1710 * Generate part of the XML output.
1711 */
1712static bool sfe_ipv6_debug_dev_read_exceptions_end(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
1713 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
1714{
1715 int bytes_read;
1716
1717 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t</exceptions>\n");
1718 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1719 return false;
1720 }
1721
1722 *length -= bytes_read;
1723 *total_read += bytes_read;
1724
1725 ws->state++;
1726 return true;
1727}
1728
1729/*
1730 * sfe_ipv6_debug_dev_read_stats()
1731 * Generate part of the XML output.
1732 */
1733static bool sfe_ipv6_debug_dev_read_stats(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
1734 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
1735{
1736 int bytes_read;
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301737 struct sfe_ipv6_stats stats;
1738 unsigned int num_conn;
1739
1740 sfe_ipv6_update_summary_stats(si, &stats);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001741
1742 spin_lock_bh(&si->lock);
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301743 num_conn = si->num_connections;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001744 spin_unlock_bh(&si->lock);
1745
1746 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<stats "
1747 "num_connections=\"%u\" "
1748 "pkts_forwarded=\"%llu\" pkts_not_forwarded=\"%llu\" "
1749 "create_requests=\"%llu\" create_collisions=\"%llu\" "
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301750 "create_failures=\"%llu\" "
Xiaoping Fan978b3772015-05-27 14:15:18 -07001751 "destroy_requests=\"%llu\" destroy_misses=\"%llu\" "
1752 "flushes=\"%llu\" "
1753 "hash_hits=\"%llu\" hash_reorders=\"%llu\" />\n",
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301754
1755 num_conn,
1756 stats.packets_forwarded64,
1757 stats.packets_not_forwarded64,
1758 stats.connection_create_requests64,
1759 stats.connection_create_collisions64,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301760 stats.connection_create_failures64,
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301761 stats.connection_destroy_requests64,
1762 stats.connection_destroy_misses64,
1763 stats.connection_flushes64,
1764 stats.connection_match_hash_hits64,
1765 stats.connection_match_hash_reorders64);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001766 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1767 return false;
1768 }
1769
1770 *length -= bytes_read;
1771 *total_read += bytes_read;
1772
1773 ws->state++;
1774 return true;
1775}
1776
1777/*
1778 * sfe_ipv6_debug_dev_read_end()
1779 * Generate part of the XML output.
1780 */
1781static bool sfe_ipv6_debug_dev_read_end(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
1782 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
1783{
1784 int bytes_read;
1785
1786 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "</sfe_ipv6>\n");
1787 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1788 return false;
1789 }
1790
1791 *length -= bytes_read;
1792 *total_read += bytes_read;
1793
1794 ws->state++;
1795 return true;
1796}
1797
1798/*
1799 * Array of write functions that write various XML elements that correspond to
1800 * our XML output state machine.
1801 */
1802static sfe_ipv6_debug_xml_write_method_t sfe_ipv6_debug_xml_write_methods[SFE_IPV6_DEBUG_XML_STATE_DONE] = {
1803 sfe_ipv6_debug_dev_read_start,
1804 sfe_ipv6_debug_dev_read_connections_start,
1805 sfe_ipv6_debug_dev_read_connections_connection,
1806 sfe_ipv6_debug_dev_read_connections_end,
1807 sfe_ipv6_debug_dev_read_exceptions_start,
1808 sfe_ipv6_debug_dev_read_exceptions_exception,
1809 sfe_ipv6_debug_dev_read_exceptions_end,
1810 sfe_ipv6_debug_dev_read_stats,
1811 sfe_ipv6_debug_dev_read_end,
1812};
1813
1814/*
1815 * sfe_ipv6_debug_dev_read()
1816 * Send info to userspace upon read request from user
1817 */
1818static ssize_t sfe_ipv6_debug_dev_read(struct file *filp, char *buffer, size_t length, loff_t *offset)
1819{
1820 char msg[CHAR_DEV_MSG_SIZE];
1821 int total_read = 0;
1822 struct sfe_ipv6_debug_xml_write_state *ws;
1823 struct sfe_ipv6 *si = &__si6;
1824
1825 ws = (struct sfe_ipv6_debug_xml_write_state *)filp->private_data;
1826 while ((ws->state != SFE_IPV6_DEBUG_XML_STATE_DONE) && (length > CHAR_DEV_MSG_SIZE)) {
1827 if ((sfe_ipv6_debug_xml_write_methods[ws->state])(si, buffer, msg, &length, &total_read, ws)) {
1828 continue;
1829 }
1830 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07001831 return total_read;
1832}
1833
1834/*
Xiaoping Fan978b3772015-05-27 14:15:18 -07001835 * sfe_ipv6_debug_dev_open()
1836 */
1837static int sfe_ipv6_debug_dev_open(struct inode *inode, struct file *file)
1838{
1839 struct sfe_ipv6_debug_xml_write_state *ws;
1840
1841 ws = (struct sfe_ipv6_debug_xml_write_state *)file->private_data;
1842 if (ws) {
1843 return 0;
1844 }
1845
1846 ws = kzalloc(sizeof(struct sfe_ipv6_debug_xml_write_state), GFP_KERNEL);
1847 if (!ws) {
1848 return -ENOMEM;
1849 }
1850
1851 ws->state = SFE_IPV6_DEBUG_XML_STATE_START;
1852 file->private_data = ws;
1853
1854 return 0;
1855}
1856
1857/*
1858 * sfe_ipv6_debug_dev_release()
1859 */
1860static int sfe_ipv6_debug_dev_release(struct inode *inode, struct file *file)
1861{
1862 struct sfe_ipv6_debug_xml_write_state *ws;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001863
1864 ws = (struct sfe_ipv6_debug_xml_write_state *)file->private_data;
Xiaoping Fan34586472015-07-03 02:20:35 -07001865 if (ws) {
1866 /*
1867 * We've finished with our output so free the write state.
1868 */
1869 kfree(ws);
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301870 file->private_data = NULL;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001871 }
1872
Xiaoping Fan978b3772015-05-27 14:15:18 -07001873 return 0;
1874}
1875
1876/*
1877 * File operations used in the debug char device
1878 */
1879static struct file_operations sfe_ipv6_debug_dev_fops = {
1880 .read = sfe_ipv6_debug_dev_read,
Xiaoping Fan978b3772015-05-27 14:15:18 -07001881 .open = sfe_ipv6_debug_dev_open,
1882 .release = sfe_ipv6_debug_dev_release
1883};
1884
1885#ifdef CONFIG_NF_FLOW_COOKIE
1886/*
1887 * sfe_ipv6_register_flow_cookie_cb
1888 * register a function in SFE to let SFE use this function to configure flow cookie for a flow
1889 *
1890 * Hardware driver which support flow cookie should register a callback function in SFE. Then SFE
1891 * can use this function to configure flow cookie for a flow.
1892 * return: 0, success; !=0, fail
1893 */
1894int sfe_ipv6_register_flow_cookie_cb(sfe_ipv6_flow_cookie_set_func_t cb)
1895{
1896 struct sfe_ipv6 *si = &__si6;
1897
1898 BUG_ON(!cb);
1899
1900 if (si->flow_cookie_set_func) {
1901 return -1;
1902 }
1903
1904 rcu_assign_pointer(si->flow_cookie_set_func, cb);
1905 return 0;
1906}
1907
1908/*
1909 * sfe_ipv6_unregister_flow_cookie_cb
1910 * unregister function which is used to configure flow cookie for a flow
1911 *
1912 * return: 0, success; !=0, fail
1913 */
1914int sfe_ipv6_unregister_flow_cookie_cb(sfe_ipv6_flow_cookie_set_func_t cb)
1915{
1916 struct sfe_ipv6 *si = &__si6;
1917
1918 RCU_INIT_POINTER(si->flow_cookie_set_func, NULL);
1919 return 0;
1920}
Xiaoping Fan640faf42015-08-28 15:50:55 -07001921
1922/*
1923 * sfe_ipv6_get_flow_cookie()
1924 */
1925static ssize_t sfe_ipv6_get_flow_cookie(struct device *dev,
1926 struct device_attribute *attr,
1927 char *buf)
1928{
1929 struct sfe_ipv6 *si = &__si6;
Xiaoping Fan01c67cc2015-11-09 11:31:57 -08001930 return snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", si->flow_cookie_enable);
Xiaoping Fan640faf42015-08-28 15:50:55 -07001931}
1932
1933/*
1934 * sfe_ipv6_set_flow_cookie()
1935 */
1936static ssize_t sfe_ipv6_set_flow_cookie(struct device *dev,
1937 struct device_attribute *attr,
1938 const char *buf, size_t size)
1939{
1940 struct sfe_ipv6 *si = &__si6;
Ken Zhu137722d2021-09-23 17:57:36 -07001941 si->flow_cookie_enable = strict_strtol(buf, NULL, 0);
Xiaoping Fan640faf42015-08-28 15:50:55 -07001942
1943 return size;
1944}
1945
1946/*
1947 * sysfs attributes.
1948 */
1949static const struct device_attribute sfe_ipv6_flow_cookie_attr =
Xiaoping Fane70da412016-02-26 16:47:57 -08001950 __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 -07001951#endif /*CONFIG_NF_FLOW_COOKIE*/
1952
Ken Zhu137722d2021-09-23 17:57:36 -07001953/*
1954 * sfe_ipv6_get_cpu()
1955 */
1956static ssize_t sfe_ipv6_get_cpu(struct device *dev,
1957 struct device_attribute *attr,
1958 char *buf)
1959{
1960 struct sfe_ipv6 *si = &__si6;
1961 return snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", si->work_cpu);
1962}
1963
1964/*
1965 * sfe_ipv4_set_cpu()
1966 */
1967static ssize_t sfe_ipv6_set_cpu(struct device *dev,
1968 struct device_attribute *attr,
1969 const char *buf, size_t size)
1970{
1971 struct sfe_ipv6 *si = &__si6;
1972 int work_cpu;
1973
1974 work_cpu = simple_strtol(buf, NULL, 0);
1975 if ((work_cpu >= 0) && (work_cpu <= NR_CPUS)) {
1976 si->work_cpu = work_cpu;
1977 } else {
1978 dev_err(dev, "%s is not in valid range[0,%d]", buf, NR_CPUS);
1979 }
1980
1981 return size;
1982}
1983/*
1984 * sysfs attributes.
1985 */
1986static const struct device_attribute sfe_ipv6_cpu_attr =
1987 __ATTR(stat_work_cpu, S_IWUSR | S_IRUGO, sfe_ipv6_get_cpu, sfe_ipv6_set_cpu);
1988
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301989 /*
1990 * sfe_ipv6_hash_init()
1991 * Initialize conn match hash lists
1992 */
1993static void sfe_ipv6_conn_match_hash_init(struct sfe_ipv6 *si, int len)
1994{
1995 struct hlist_head *hash_list = si->hlist_conn_match_hash_head;
1996 int i;
1997
1998 for (i = 0; i < len; i++) {
1999 INIT_HLIST_HEAD(&hash_list[i]);
2000 }
2001}
2002
Xiaoping Fan978b3772015-05-27 14:15:18 -07002003/*
2004 * sfe_ipv6_init()
2005 */
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05302006int sfe_ipv6_init(void)
Xiaoping Fan978b3772015-05-27 14:15:18 -07002007{
2008 struct sfe_ipv6 *si = &__si6;
2009 int result = -1;
2010
2011 DEBUG_INFO("SFE IPv6 init\n");
2012
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05302013 sfe_ipv6_conn_match_hash_init(si, ARRAY_SIZE(si->hlist_conn_match_hash_head));
2014
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302015 si->stats_pcpu = alloc_percpu_gfp(struct sfe_ipv6_stats, GFP_KERNEL | __GFP_ZERO);
2016 if (!si->stats_pcpu) {
2017 DEBUG_ERROR("failed to allocate stats memory for sfe_ipv6\n");
2018 goto exit0;
2019 }
2020
Xiaoping Fan978b3772015-05-27 14:15:18 -07002021 /*
2022 * Create sys/sfe_ipv6
2023 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302024 si->sys_ipv6 = kobject_create_and_add("sfe_ipv6", NULL);
2025 if (!si->sys_ipv6) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07002026 DEBUG_ERROR("failed to register sfe_ipv6\n");
2027 goto exit1;
2028 }
2029
2030 /*
2031 * Create files, one for each parameter supported by this module.
2032 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302033 result = sysfs_create_file(si->sys_ipv6, &sfe_ipv6_debug_dev_attr.attr);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002034 if (result) {
2035 DEBUG_ERROR("failed to register debug dev file: %d\n", result);
2036 goto exit2;
2037 }
2038
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302039 result = sysfs_create_file(si->sys_ipv6, &sfe_ipv6_cpu_attr.attr);
Ken Zhu137722d2021-09-23 17:57:36 -07002040 if (result) {
2041 DEBUG_ERROR("failed to register debug dev file: %d\n", result);
2042 goto exit3;
2043 }
2044
Xiaoping Fan640faf42015-08-28 15:50:55 -07002045#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302046 result = sysfs_create_file(si->sys_ipv6, &sfe_ipv6_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002047 if (result) {
2048 DEBUG_ERROR("failed to register flow cookie enable file: %d\n", result);
Ken Zhu137722d2021-09-23 17:57:36 -07002049 goto exit4;
Xiaoping Fan640faf42015-08-28 15:50:55 -07002050 }
2051#endif /* CONFIG_NF_FLOW_COOKIE */
2052
Xiaoping Fan978b3772015-05-27 14:15:18 -07002053 /*
2054 * Register our debug char device.
2055 */
2056 result = register_chrdev(0, "sfe_ipv6", &sfe_ipv6_debug_dev_fops);
2057 if (result < 0) {
2058 DEBUG_ERROR("Failed to register chrdev: %d\n", result);
Ken Zhu137722d2021-09-23 17:57:36 -07002059 goto exit5;
Xiaoping Fan978b3772015-05-27 14:15:18 -07002060 }
2061
2062 si->debug_dev = result;
Ken Zhu137722d2021-09-23 17:57:36 -07002063 si->work_cpu = WORK_CPU_UNBOUND;
Xiaoping Fan978b3772015-05-27 14:15:18 -07002064
2065 /*
Ken Zhu137722d2021-09-23 17:57:36 -07002066 * Create work to handle periodic statistics.
Xiaoping Fan978b3772015-05-27 14:15:18 -07002067 */
Ken Zhu137722d2021-09-23 17:57:36 -07002068 INIT_DELAYED_WORK(&(si->sync_dwork), sfe_ipv6_periodic_sync);
2069 schedule_delayed_work_on(si->work_cpu, &(si->sync_dwork), ((HZ + 99) / 100));
Xiaoping Fan978b3772015-05-27 14:15:18 -07002070 spin_lock_init(&si->lock);
2071
2072 return 0;
2073
Ken Zhu137722d2021-09-23 17:57:36 -07002074exit5:
Xiaoping Fan640faf42015-08-28 15:50:55 -07002075#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302076 sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002077
Ken Zhu137722d2021-09-23 17:57:36 -07002078exit4:
Xiaoping Fan640faf42015-08-28 15:50:55 -07002079#endif /* CONFIG_NF_FLOW_COOKIE */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302080 sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_cpu_attr.attr);
Ken Zhu137722d2021-09-23 17:57:36 -07002081exit3:
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302082 sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_debug_dev_attr.attr);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002083
2084exit2:
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302085 kobject_put(si->sys_ipv6);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002086
2087exit1:
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302088 free_percpu(si->stats_pcpu);
2089
2090exit0:
Xiaoping Fan978b3772015-05-27 14:15:18 -07002091 return result;
2092}
2093
2094/*
2095 * sfe_ipv6_exit()
2096 */
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05302097void sfe_ipv6_exit(void)
Xiaoping Fan978b3772015-05-27 14:15:18 -07002098{
2099 struct sfe_ipv6 *si = &__si6;
2100
2101 DEBUG_INFO("SFE IPv6 exit\n");
2102
2103 /*
2104 * Destroy all connections.
2105 */
2106 sfe_ipv6_destroy_all_rules_for_dev(NULL);
2107
Ken Zhu137722d2021-09-23 17:57:36 -07002108 cancel_delayed_work(&si->sync_dwork);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002109
2110 unregister_chrdev(si->debug_dev, "sfe_ipv6");
2111
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302112 free_percpu(si->stats_pcpu);
2113
Xiaoping Fan640faf42015-08-28 15:50:55 -07002114#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302115 sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002116#endif /* CONFIG_NF_FLOW_COOKIE */
Ken Zhu137722d2021-09-23 17:57:36 -07002117
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302118 sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_cpu_attr.attr);
Ken Zhu137722d2021-09-23 17:57:36 -07002119
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302120 sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_debug_dev_attr.attr);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002121
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302122 kobject_put(si->sys_ipv6);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002123}
2124
Xiaoping Fan978b3772015-05-27 14:15:18 -07002125#ifdef CONFIG_NF_FLOW_COOKIE
2126EXPORT_SYMBOL(sfe_ipv6_register_flow_cookie_cb);
2127EXPORT_SYMBOL(sfe_ipv6_unregister_flow_cookie_cb);
2128#endif