blob: 104948dd26d85c94d3a21ced52b4bbf4b4c413b4 [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;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700924
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530925 src_dev = dev_get_by_index(&init_net, msg->conn_rule.flow_top_interface_num);
926 if (!src_dev) {
927 DEBUG_WARN("%px: Unable to find src_dev corresponding to %d\n", msg,
928 msg->conn_rule.flow_top_interface_num);
929 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
930 return -EINVAL;
931 }
932
933 dest_dev = dev_get_by_index(&init_net, msg->conn_rule.return_top_interface_num);
934 if (!dest_dev) {
935 DEBUG_WARN("%px: Unable to find dest_dev corresponding to %d\n", msg,
936 msg->conn_rule.return_top_interface_num);
937 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
938 dev_put(src_dev);
939 return -EINVAL;
940 }
Xiaoping Fan978b3772015-05-27 14:15:18 -0700941
942 if (unlikely((dest_dev->reg_state != NETREG_REGISTERED) ||
943 (src_dev->reg_state != NETREG_REGISTERED))) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530944 DEBUG_WARN("%px: src_dev=%s and dest_dev=%s are unregistered\n", msg,
945 src_dev->name, dest_dev->name);
946 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
947 dev_put(src_dev);
948 dev_put(dest_dev);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700949 return -EINVAL;
950 }
951
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530952 /*
953 * Allocate the various connection tracking objects.
954 */
955 c = (struct sfe_ipv6_connection *)kmalloc(sizeof(struct sfe_ipv6_connection), GFP_ATOMIC);
956 if (unlikely(!c)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530957 DEBUG_WARN("%px: memory allocation of connection entry failed\n", msg);
958 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
959 dev_put(src_dev);
960 dev_put(dest_dev);
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530961 return -ENOMEM;
962 }
963
964 original_cm = (struct sfe_ipv6_connection_match *)kmalloc(sizeof(struct sfe_ipv6_connection_match), GFP_ATOMIC);
965 if (unlikely(!original_cm)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530966 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
967 DEBUG_WARN("%px: memory allocation of connection match entry failed\n", msg);
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530968 kfree(c);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530969 dev_put(src_dev);
970 dev_put(dest_dev);
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530971 return -ENOMEM;
972 }
973
974 reply_cm = (struct sfe_ipv6_connection_match *)kmalloc(sizeof(struct sfe_ipv6_connection_match), GFP_ATOMIC);
975 if (unlikely(!reply_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(original_cm);
979 kfree(c);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530980 dev_put(src_dev);
981 dev_put(dest_dev);
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530982 return -ENOMEM;
983 }
984
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530985 this_cpu_inc(si->stats_pcpu->connection_create_requests64);
986
Xiaoping Fan978b3772015-05-27 14:15:18 -0700987 spin_lock_bh(&si->lock);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700988
989 /*
990 * Check to see if there is already a flow that matches the rule we're
991 * trying to create. If there is then we can't create a new one.
992 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530993 old_c = sfe_ipv6_find_connection(si, tuple->protocol, (struct sfe_ipv6_addr *)tuple->flow_ip, tuple->flow_ident,
994 (struct sfe_ipv6_addr *)tuple->return_ip, tuple->return_ident);
995
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530996 if (old_c != NULL) {
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530997 this_cpu_inc(si->stats_pcpu->connection_create_collisions64);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700998
999 /*
1000 * If we already have the flow then it's likely that this
1001 * request to create the connection rule contains more
1002 * up-to-date information. Check and update accordingly.
1003 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301004 sfe_ipv6_update_protocol_state(old_c, msg);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001005 spin_unlock_bh(&si->lock);
1006
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301007 kfree(reply_cm);
1008 kfree(original_cm);
1009 kfree(c);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301010 dev_put(src_dev);
1011 dev_put(dest_dev);
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301012
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301013 DEBUG_TRACE("connection already exists - p: %d\n"
Tian Yang45f39c82020-10-06 14:07:47 -07001014 " s: %s:%pxM:%pI6:%u, d: %s:%pxM:%pI6:%u\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301015 tuple->protocol,
1016 src_dev->name, msg->conn_rule.flow_mac, tuple->flow_ip, ntohs(tuple->flow_ident),
1017 dest_dev->name, msg->conn_rule.return_mac, tuple->return_ip, ntohs(tuple->return_ident));
Xiaoping Fan978b3772015-05-27 14:15:18 -07001018 return -EADDRINUSE;
1019 }
1020
1021 /*
Xiaoping Fan978b3772015-05-27 14:15:18 -07001022 * Fill in the "original" direction connection matching object.
1023 * Note that the transmit MAC address is "dest_mac_xlate" because
1024 * we always know both ends of a connection by their translated
1025 * addresses and not their public addresses.
1026 */
1027 original_cm->match_dev = src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301028 original_cm->match_protocol = tuple->protocol;
1029 original_cm->match_src_ip[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip;
1030 original_cm->match_src_port = tuple->flow_ident;
1031 original_cm->match_dest_ip[0] = *(struct sfe_ipv6_addr *)tuple->return_ip;
1032 original_cm->match_dest_port = tuple->return_ident;
1033
1034 original_cm->xlate_src_ip[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip;
1035 original_cm->xlate_src_port = tuple->flow_ident;
1036 original_cm->xlate_dest_ip[0] = *(struct sfe_ipv6_addr *)tuple->return_ip;
1037 original_cm->xlate_dest_port = tuple->return_ident;
1038
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301039 atomic_set(&original_cm->rx_packet_count, 0);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001040 original_cm->rx_packet_count64 = 0;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301041 atomic_set(&original_cm->rx_byte_count, 0);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001042 original_cm->rx_byte_count64 = 0;
1043 original_cm->xmit_dev = dest_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301044
1045 original_cm->xmit_dev_mtu = msg->conn_rule.return_mtu;
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301046
Xiaoping Fan978b3772015-05-27 14:15:18 -07001047 original_cm->connection = c;
1048 original_cm->counter_match = reply_cm;
1049 original_cm->flags = 0;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301050 if (msg->valid_flags & SFE_RULE_CREATE_QOS_VALID) {
1051 original_cm->priority = msg->qos_rule.flow_qos_tag;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001052 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_PRIORITY_REMARK;
1053 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301054 if (msg->valid_flags & SFE_RULE_CREATE_DSCP_MARKING_VALID) {
1055 original_cm->dscp = msg->dscp_rule.flow_dscp << SFE_IPV6_DSCP_SHIFT;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001056 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_DSCP_REMARK;
1057 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07001058#ifdef CONFIG_NF_FLOW_COOKIE
1059 original_cm->flow_cookie = 0;
1060#endif
Zhi Chen8748eb32015-06-18 12:58:48 -07001061#ifdef CONFIG_XFRM
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301062 if (msg->valid_flags & SFE_RULE_CREATE_DIRECTION_VALID) {
1063 original_cm->flow_accel = msg->direction_rule.flow_accel;
1064 } else {
1065 original_cm->flow_accel = 1;
1066 }
Zhi Chen8748eb32015-06-18 12:58:48 -07001067#endif
Xiaoping Fan978b3772015-05-27 14:15:18 -07001068
1069 /*
Ken Zhubbf49652021-09-12 15:33:09 -07001070 * For the non-arp interface, we don't write L2 HDR.
Xiaoping Fan978b3772015-05-27 14:15:18 -07001071 */
Ken Zhubbf49652021-09-12 15:33:09 -07001072 if (!(dest_dev->flags & IFF_NOARP)) {
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301073
1074 /*
1075 * Check whether the rule has configured a specific source MAC address to use.
1076 * This is needed when virtual L3 interfaces such as br-lan, macvlan, vlan are used during egress
1077 */
1078 if ((msg->valid_flags & SFE_RULE_CREATE_SRC_MAC_VALID) &&
1079 (msg->src_mac_rule.mac_valid_flags & SFE_SRC_MAC_RETURN_VALID)) {
1080 ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)msg->src_mac_rule.return_src_mac);
1081 } else {
1082 ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)dest_dev->dev_addr);
1083 }
1084 ether_addr_copy((u8 *)original_cm->xmit_dest_mac, (u8 *)msg->conn_rule.return_mac);
1085
Xiaoping Fan978b3772015-05-27 14:15:18 -07001086 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_WRITE_L2_HDR;
1087
1088 /*
1089 * If our dev writes Ethernet headers then we can write a really fast
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301090 * version
Xiaoping Fan978b3772015-05-27 14:15:18 -07001091 */
1092 if (dest_dev->header_ops) {
1093 if (dest_dev->header_ops->create == eth_header) {
1094 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR;
1095 }
1096 }
1097 }
1098
1099 /*
1100 * Fill in the "reply" direction connection matching object.
1101 */
1102 reply_cm->match_dev = dest_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301103 reply_cm->match_protocol = tuple->protocol;
1104 reply_cm->match_src_ip[0] = *(struct sfe_ipv6_addr *)tuple->return_ip;
1105 reply_cm->match_src_port = tuple->return_ident;
1106 reply_cm->match_dest_ip[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip;
1107 reply_cm->match_dest_port = tuple->flow_ident;
1108 reply_cm->xlate_src_ip[0] = *(struct sfe_ipv6_addr *)tuple->return_ip;
1109 reply_cm->xlate_src_port = tuple->return_ident;
1110 reply_cm->xlate_dest_ip[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip;
1111 reply_cm->xlate_dest_port = tuple->flow_ident;
1112
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301113 atomic_set(&original_cm->rx_byte_count, 0);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001114 reply_cm->rx_packet_count64 = 0;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301115 atomic_set(&reply_cm->rx_byte_count, 0);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001116 reply_cm->rx_byte_count64 = 0;
1117 reply_cm->xmit_dev = src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301118 reply_cm->xmit_dev_mtu = msg->conn_rule.flow_mtu;
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301119
Xiaoping Fan978b3772015-05-27 14:15:18 -07001120 reply_cm->connection = c;
1121 reply_cm->counter_match = original_cm;
1122 reply_cm->flags = 0;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301123 if (msg->valid_flags & SFE_RULE_CREATE_QOS_VALID) {
1124 reply_cm->priority = msg->qos_rule.return_qos_tag;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001125 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_PRIORITY_REMARK;
1126 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301127 if (msg->valid_flags & SFE_RULE_CREATE_DSCP_MARKING_VALID) {
1128 reply_cm->dscp = msg->dscp_rule.return_dscp << SFE_IPV6_DSCP_SHIFT;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001129 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_DSCP_REMARK;
1130 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07001131#ifdef CONFIG_NF_FLOW_COOKIE
1132 reply_cm->flow_cookie = 0;
1133#endif
Zhi Chen8748eb32015-06-18 12:58:48 -07001134#ifdef CONFIG_XFRM
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301135 if (msg->valid_flags & SFE_RULE_CREATE_DIRECTION_VALID) {
1136 reply_cm->flow_accel = msg->direction_rule.return_accel;
1137 } else {
1138 reply_cm->flow_accel = 1;
1139 }
Zhi Chen8748eb32015-06-18 12:58:48 -07001140#endif
Xiaoping Fan978b3772015-05-27 14:15:18 -07001141 /*
Ken Zhubbf49652021-09-12 15:33:09 -07001142 * For the non-arp interface, we don't write L2 HDR.
Xiaoping Fan978b3772015-05-27 14:15:18 -07001143 */
Ken Zhubbf49652021-09-12 15:33:09 -07001144 if (!(src_dev->flags & IFF_NOARP)) {
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301145
1146 /*
1147 * Check whether the rule has configured a specific source MAC address to use.
1148 * This is needed when virtual L3 interfaces such as br-lan, macvlan, vlan are used during egress
1149 */
1150 if ((msg->valid_flags & SFE_RULE_CREATE_SRC_MAC_VALID) &&
1151 (msg->src_mac_rule.mac_valid_flags & SFE_SRC_MAC_FLOW_VALID)) {
1152 ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)msg->src_mac_rule.flow_src_mac);
1153 } else {
1154 ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)src_dev->dev_addr);
1155 }
1156
1157 ether_addr_copy((u8 *)reply_cm->xmit_dest_mac, (u8 *)msg->conn_rule.flow_mac);
1158
Xiaoping Fan978b3772015-05-27 14:15:18 -07001159 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_WRITE_L2_HDR;
1160
1161 /*
1162 * If our dev writes Ethernet headers then we can write a really fast
1163 * version.
1164 */
1165 if (src_dev->header_ops) {
1166 if (src_dev->header_ops->create == eth_header) {
1167 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR;
1168 }
1169 }
1170 }
1171
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301172 /*
1173 * No support for NAT in ipv6
1174 */
Xiaoping Fan978b3772015-05-27 14:15:18 -07001175
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301176 c->protocol = tuple->protocol;
1177 c->src_ip[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip;
1178 c->src_ip_xlate[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip;
1179 c->src_port = tuple->flow_ident;
1180 c->src_port_xlate = tuple->flow_ident;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001181 c->original_dev = src_dev;
1182 c->original_match = original_cm;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301183
1184 c->dest_ip[0] = *(struct sfe_ipv6_addr *)tuple->return_ip;
1185 c->dest_ip_xlate[0] = *(struct sfe_ipv6_addr *)tuple->return_ip;
1186 c->dest_port = tuple->return_ident;
1187 c->dest_port_xlate = tuple->return_ident;
1188
Xiaoping Fan978b3772015-05-27 14:15:18 -07001189 c->reply_dev = dest_dev;
1190 c->reply_match = reply_cm;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301191 c->mark = 0; /* No mark support */
Xiaoping Fan34586472015-07-03 02:20:35 -07001192 c->debug_read_seq = 0;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001193 c->last_sync_jiffies = get_jiffies_64();
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301194 c->removed = false;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001195
1196 /*
Xiaoping Fan978b3772015-05-27 14:15:18 -07001197 * Initialize the protocol-specific information that we track.
1198 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301199 switch (tuple->protocol) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07001200 case IPPROTO_TCP:
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301201 original_cm->protocol_state.tcp.win_scale = msg->tcp_rule.flow_window_scale;
1202 original_cm->protocol_state.tcp.max_win = msg->tcp_rule.flow_max_window ? msg->tcp_rule.flow_max_window : 1;
1203 original_cm->protocol_state.tcp.end = msg->tcp_rule.flow_end;
1204 original_cm->protocol_state.tcp.max_end = msg->tcp_rule.flow_max_end;
1205 reply_cm->protocol_state.tcp.win_scale = msg->tcp_rule.return_window_scale;
1206 reply_cm->protocol_state.tcp.max_win = msg->tcp_rule.return_max_window ? msg->tcp_rule.return_max_window : 1;
1207 reply_cm->protocol_state.tcp.end = msg->tcp_rule.return_end;
1208 reply_cm->protocol_state.tcp.max_end = msg->tcp_rule.return_max_end;
1209 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_NO_SEQ_CHECK) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07001210 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
1211 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
1212 }
1213 break;
1214 }
1215
1216 sfe_ipv6_connection_match_compute_translations(original_cm);
1217 sfe_ipv6_connection_match_compute_translations(reply_cm);
1218 sfe_ipv6_insert_connection(si, c);
1219
1220 spin_unlock_bh(&si->lock);
1221
1222 /*
1223 * We have everything we need!
1224 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301225 DEBUG_INFO("new connection - p: %d\n"
Tian Yang45f39c82020-10-06 14:07:47 -07001226 " s: %s:%pxM(%pxM):%pI6(%pI6):%u(%u)\n"
1227 " d: %s:%pxM(%pxM):%pI6(%pI6):%u(%u)\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301228 tuple->protocol,
1229 src_dev->name, msg->conn_rule.flow_mac, NULL,
1230 (void *)tuple->flow_ip, (void *)tuple->flow_ip, ntohs(tuple->flow_ident), ntohs(tuple->flow_ident),
1231 dest_dev->name, NULL, msg->conn_rule.return_mac,
1232 (void *)tuple->return_ip, (void *)tuple->return_ip, ntohs(tuple->return_ident), ntohs(tuple->return_ident));
Xiaoping Fan978b3772015-05-27 14:15:18 -07001233
1234 return 0;
1235}
1236
1237/*
1238 * sfe_ipv6_destroy_rule()
1239 * Destroy a forwarding rule.
1240 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301241void sfe_ipv6_destroy_rule(struct sfe_ipv6_rule_destroy_msg *msg)
Xiaoping Fan978b3772015-05-27 14:15:18 -07001242{
1243 struct sfe_ipv6 *si = &__si6;
1244 struct sfe_ipv6_connection *c;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301245 bool ret;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301246 struct sfe_ipv6_5tuple *tuple = &msg->tuple;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001247
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301248 this_cpu_inc(si->stats_pcpu->connection_destroy_requests64);
1249
Xiaoping Fan978b3772015-05-27 14:15:18 -07001250 spin_lock_bh(&si->lock);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001251
1252 /*
1253 * Check to see if we have a flow that matches the rule we're trying
1254 * to destroy. If there isn't then we can't destroy it.
1255 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301256 c = sfe_ipv6_find_connection(si, tuple->protocol, (struct sfe_ipv6_addr *)tuple->flow_ip, tuple->flow_ident,
1257 (struct sfe_ipv6_addr *)tuple->return_ip, tuple->return_ident);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001258 if (!c) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07001259 spin_unlock_bh(&si->lock);
1260
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301261 this_cpu_inc(si->stats_pcpu->connection_destroy_misses64);
1262
Xiaoping Fan978b3772015-05-27 14:15:18 -07001263 DEBUG_TRACE("connection does not exist - p: %d, s: %pI6:%u, d: %pI6:%u\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301264 tuple->protocol, tuple->flow_ip, ntohs(tuple->flow_ident),
1265 tuple->return_ip, ntohs(tuple->return_ident));
Xiaoping Fan978b3772015-05-27 14:15:18 -07001266 return;
1267 }
1268
1269 /*
1270 * Remove our connection details from the hash tables.
1271 */
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301272 ret = sfe_ipv6_remove_connection(si, c);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001273 spin_unlock_bh(&si->lock);
1274
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301275 if (ret) {
1276 sfe_ipv6_flush_connection(si, c, SFE_SYNC_REASON_DESTROY);
1277 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07001278
1279 DEBUG_INFO("connection destroyed - p: %d, s: %pI6:%u, d: %pI6:%u\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301280 tuple->protocol, tuple->flow_ip, ntohs(tuple->flow_ident),
1281 tuple->return_ip, ntohs(tuple->return_ident));
Xiaoping Fan978b3772015-05-27 14:15:18 -07001282}
1283
1284/*
1285 * sfe_ipv6_register_sync_rule_callback()
1286 * Register a callback for rule synchronization.
1287 */
1288void sfe_ipv6_register_sync_rule_callback(sfe_sync_rule_callback_t sync_rule_callback)
1289{
1290 struct sfe_ipv6 *si = &__si6;
1291
1292 spin_lock_bh(&si->lock);
1293 rcu_assign_pointer(si->sync_rule_callback, sync_rule_callback);
1294 spin_unlock_bh(&si->lock);
1295}
1296
1297/*
1298 * sfe_ipv6_get_debug_dev()
1299 */
1300static ssize_t sfe_ipv6_get_debug_dev(struct device *dev,
1301 struct device_attribute *attr,
1302 char *buf)
1303{
1304 struct sfe_ipv6 *si = &__si6;
1305 ssize_t count;
1306 int num;
1307
1308 spin_lock_bh(&si->lock);
1309 num = si->debug_dev;
1310 spin_unlock_bh(&si->lock);
1311
1312 count = snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", num);
1313 return count;
1314}
1315
1316/*
1317 * sfe_ipv6_destroy_all_rules_for_dev()
1318 * Destroy all connections that match a particular device.
1319 *
1320 * If we pass dev as NULL then this destroys all connections.
1321 */
1322void sfe_ipv6_destroy_all_rules_for_dev(struct net_device *dev)
1323{
1324 struct sfe_ipv6 *si = &__si6;
1325 struct sfe_ipv6_connection *c;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301326 bool ret;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001327
Xiaoping Fan34586472015-07-03 02:20:35 -07001328another_round:
Xiaoping Fan978b3772015-05-27 14:15:18 -07001329 spin_lock_bh(&si->lock);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001330
Xiaoping Fan34586472015-07-03 02:20:35 -07001331 for (c = si->all_connections_head; c; c = c->all_connections_next) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07001332 /*
Xiaoping Fan34586472015-07-03 02:20:35 -07001333 * Does this connection relate to the device we are destroying?
Xiaoping Fan978b3772015-05-27 14:15:18 -07001334 */
1335 if (!dev
1336 || (dev == c->original_dev)
1337 || (dev == c->reply_dev)) {
Xiaoping Fan34586472015-07-03 02:20:35 -07001338 break;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001339 }
Xiaoping Fan34586472015-07-03 02:20:35 -07001340 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07001341
Xiaoping Fan34586472015-07-03 02:20:35 -07001342 if (c) {
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301343 ret = sfe_ipv6_remove_connection(si, c);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001344 }
1345
1346 spin_unlock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07001347
1348 if (c) {
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301349 if (ret) {
1350 sfe_ipv6_flush_connection(si, c, SFE_SYNC_REASON_DESTROY);
1351 }
Xiaoping Fan34586472015-07-03 02:20:35 -07001352 goto another_round;
1353 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07001354}
1355
1356/*
1357 * sfe_ipv6_periodic_sync()
1358 */
Ken Zhu137722d2021-09-23 17:57:36 -07001359static void sfe_ipv6_periodic_sync(struct work_struct *work)
Xiaoping Fan978b3772015-05-27 14:15:18 -07001360{
Ken Zhu137722d2021-09-23 17:57:36 -07001361 struct sfe_ipv6 *si = container_of((struct delayed_work *)work, struct sfe_ipv6, sync_dwork);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07001362 u64 now_jiffies;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001363 int quota;
1364 sfe_sync_rule_callback_t sync_rule_callback;
Ken Zhu32b95392021-09-03 13:52:04 -07001365 struct sfe_ipv6_connection *c;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001366
1367 now_jiffies = get_jiffies_64();
1368
1369 rcu_read_lock();
1370 sync_rule_callback = rcu_dereference(si->sync_rule_callback);
1371 if (!sync_rule_callback) {
1372 rcu_read_unlock();
1373 goto done;
1374 }
1375
1376 spin_lock_bh(&si->lock);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001377
1378 /*
Ken Zhu32b95392021-09-03 13:52:04 -07001379 * If we have reached the end of the connection list, walk from
1380 * the connection head.
1381 */
1382 c = si->wc_next;
1383 if (unlikely(!c)) {
1384 c = si->all_connections_head;
1385 }
1386 /*
Xiaoping Fan978b3772015-05-27 14:15:18 -07001387 * Get an estimate of the number of connections to parse in this sync.
1388 */
1389 quota = (si->num_connections + 63) / 64;
1390
1391 /*
Ken Zhu32b95392021-09-03 13:52:04 -07001392 * Walk the "all connection" list and sync the connection state.
Xiaoping Fan978b3772015-05-27 14:15:18 -07001393 */
Ken Zhu32b95392021-09-03 13:52:04 -07001394 while (likely(c && quota)) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07001395 struct sfe_ipv6_connection_match *cm;
1396 struct sfe_ipv6_connection_match *counter_cm;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001397 struct sfe_connection_sync sis;
1398
Ken Zhu32b95392021-09-03 13:52:04 -07001399 cm = c->original_match;
1400 counter_cm = c->reply_match;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001401
1402 /*
Ken Zhu32b95392021-09-03 13:52:04 -07001403 * Didn't receive packets in the origial direction or reply
1404 * direction, move to the next connection.
Xiaoping Fan978b3772015-05-27 14:15:18 -07001405 */
Ken Zhu32b95392021-09-03 13:52:04 -07001406 if (!atomic_read(&cm->rx_packet_count) && !atomic_read(&counter_cm->rx_packet_count)) {
1407 c = c->all_connections_next;
1408 continue;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001409 }
1410
Ken Zhu32b95392021-09-03 13:52:04 -07001411 quota--;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001412
1413 /*
1414 * Sync the connection state.
1415 */
Xiaoping Fan99cb4c12015-08-21 19:07:32 -07001416 sfe_ipv6_gen_sync_connection(si, c, &sis, SFE_SYNC_REASON_STATS, now_jiffies);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001417
Ken Zhu32b95392021-09-03 13:52:04 -07001418 si->wc_next = c->all_connections_next;
1419
Xiaoping Fan978b3772015-05-27 14:15:18 -07001420 spin_unlock_bh(&si->lock);
1421 sync_rule_callback(&sis);
1422 spin_lock_bh(&si->lock);
Ken Zhu32b95392021-09-03 13:52:04 -07001423
1424 /*
1425 * c must be set and used in the same lock/unlock window;
1426 * because c could be removed when we don't hold the lock,
1427 * so delay grabbing until after the callback and relock.
1428 */
1429 c = si->wc_next;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001430 }
1431
Ken Zhu32b95392021-09-03 13:52:04 -07001432 /*
1433 * At the end of loop, put wc_next to the connection we left
1434 */
1435 si->wc_next = c;
1436
Xiaoping Fan978b3772015-05-27 14:15:18 -07001437 spin_unlock_bh(&si->lock);
1438 rcu_read_unlock();
1439
1440done:
Ken Zhu137722d2021-09-23 17:57:36 -07001441 schedule_delayed_work_on(si->work_cpu, (struct delayed_work *)work, ((HZ + 99) / 100));
Xiaoping Fan978b3772015-05-27 14:15:18 -07001442}
1443
1444/*
1445 * sfe_ipv6_debug_dev_read_start()
1446 * Generate part of the XML output.
1447 */
1448static bool sfe_ipv6_debug_dev_read_start(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
1449 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
1450{
1451 int bytes_read;
1452
Xiaoping Fan34586472015-07-03 02:20:35 -07001453 si->debug_read_seq++;
1454
Xiaoping Fan978b3772015-05-27 14:15:18 -07001455 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "<sfe_ipv6>\n");
1456 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1457 return false;
1458 }
1459
1460 *length -= bytes_read;
1461 *total_read += bytes_read;
1462
1463 ws->state++;
1464 return true;
1465}
1466
1467/*
1468 * sfe_ipv6_debug_dev_read_connections_start()
1469 * Generate part of the XML output.
1470 */
1471static bool sfe_ipv6_debug_dev_read_connections_start(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
1472 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
1473{
1474 int bytes_read;
1475
1476 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<connections>\n");
1477 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1478 return false;
1479 }
1480
1481 *length -= bytes_read;
1482 *total_read += bytes_read;
1483
1484 ws->state++;
1485 return true;
1486}
1487
1488/*
1489 * sfe_ipv6_debug_dev_read_connections_connection()
1490 * Generate part of the XML output.
1491 */
1492static bool sfe_ipv6_debug_dev_read_connections_connection(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
1493 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
1494{
1495 struct sfe_ipv6_connection *c;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001496 struct sfe_ipv6_connection_match *original_cm;
1497 struct sfe_ipv6_connection_match *reply_cm;
1498 int bytes_read;
1499 int protocol;
1500 struct net_device *src_dev;
1501 struct sfe_ipv6_addr src_ip;
1502 struct sfe_ipv6_addr src_ip_xlate;
1503 __be16 src_port;
1504 __be16 src_port_xlate;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07001505 u64 src_rx_packets;
1506 u64 src_rx_bytes;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001507 struct net_device *dest_dev;
1508 struct sfe_ipv6_addr dest_ip;
1509 struct sfe_ipv6_addr dest_ip_xlate;
1510 __be16 dest_port;
1511 __be16 dest_port_xlate;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07001512 u64 dest_rx_packets;
1513 u64 dest_rx_bytes;
1514 u64 last_sync_jiffies;
1515 u32 mark, src_priority, dest_priority, src_dscp, dest_dscp;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301516 u32 packet, byte;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001517#ifdef CONFIG_NF_FLOW_COOKIE
1518 int src_flow_cookie, dst_flow_cookie;
1519#endif
1520
1521 spin_lock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07001522
1523 for (c = si->all_connections_head; c; c = c->all_connections_next) {
1524 if (c->debug_read_seq < si->debug_read_seq) {
1525 c->debug_read_seq = si->debug_read_seq;
1526 break;
1527 }
1528 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07001529
1530 /*
Xiaoping Fan34586472015-07-03 02:20:35 -07001531 * If there were no connections then move to the next state.
Xiaoping Fan978b3772015-05-27 14:15:18 -07001532 */
1533 if (!c) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07001534 spin_unlock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07001535 ws->state++;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001536 return true;
1537 }
1538
1539 original_cm = c->original_match;
1540 reply_cm = c->reply_match;
1541
1542 protocol = c->protocol;
1543 src_dev = c->original_dev;
1544 src_ip = c->src_ip[0];
1545 src_ip_xlate = c->src_ip_xlate[0];
1546 src_port = c->src_port;
1547 src_port_xlate = c->src_port_xlate;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001548 src_priority = original_cm->priority;
1549 src_dscp = original_cm->dscp >> SFE_IPV6_DSCP_SHIFT;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001550
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301551 sfe_ipv6_connection_match_update_summary_stats(original_cm, &packet, &byte);
1552 sfe_ipv6_connection_match_update_summary_stats(reply_cm, &packet, &byte);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001553
1554 src_rx_packets = original_cm->rx_packet_count64;
1555 src_rx_bytes = original_cm->rx_byte_count64;
1556 dest_dev = c->reply_dev;
1557 dest_ip = c->dest_ip[0];
1558 dest_ip_xlate = c->dest_ip_xlate[0];
1559 dest_port = c->dest_port;
1560 dest_port_xlate = c->dest_port_xlate;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001561 dest_priority = reply_cm->priority;
1562 dest_dscp = reply_cm->dscp >> SFE_IPV6_DSCP_SHIFT;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001563 dest_rx_packets = reply_cm->rx_packet_count64;
1564 dest_rx_bytes = reply_cm->rx_byte_count64;
1565 last_sync_jiffies = get_jiffies_64() - c->last_sync_jiffies;
1566 mark = c->mark;
1567#ifdef CONFIG_NF_FLOW_COOKIE
1568 src_flow_cookie = original_cm->flow_cookie;
1569 dst_flow_cookie = reply_cm->flow_cookie;
1570#endif
1571 spin_unlock_bh(&si->lock);
1572
1573 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t\t<connection "
1574 "protocol=\"%u\" "
1575 "src_dev=\"%s\" "
1576 "src_ip=\"%pI6\" src_ip_xlate=\"%pI6\" "
1577 "src_port=\"%u\" src_port_xlate=\"%u\" "
Xiaoping Fane1963d42015-08-25 17:06:19 -07001578 "src_priority=\"%u\" src_dscp=\"%u\" "
Xiaoping Fan978b3772015-05-27 14:15:18 -07001579 "src_rx_pkts=\"%llu\" src_rx_bytes=\"%llu\" "
1580 "dest_dev=\"%s\" "
1581 "dest_ip=\"%pI6\" dest_ip_xlate=\"%pI6\" "
1582 "dest_port=\"%u\" dest_port_xlate=\"%u\" "
Xiaoping Fane1963d42015-08-25 17:06:19 -07001583 "dest_priority=\"%u\" dest_dscp=\"%u\" "
Xiaoping Fan978b3772015-05-27 14:15:18 -07001584 "dest_rx_pkts=\"%llu\" dest_rx_bytes=\"%llu\" "
1585#ifdef CONFIG_NF_FLOW_COOKIE
1586 "src_flow_cookie=\"%d\" dst_flow_cookie=\"%d\" "
1587#endif
1588 "last_sync=\"%llu\" "
1589 "mark=\"%08x\" />\n",
1590 protocol,
1591 src_dev->name,
1592 &src_ip, &src_ip_xlate,
1593 ntohs(src_port), ntohs(src_port_xlate),
Xiaoping Fane1963d42015-08-25 17:06:19 -07001594 src_priority, src_dscp,
Xiaoping Fan978b3772015-05-27 14:15:18 -07001595 src_rx_packets, src_rx_bytes,
1596 dest_dev->name,
1597 &dest_ip, &dest_ip_xlate,
1598 ntohs(dest_port), ntohs(dest_port_xlate),
Xiaoping Fane1963d42015-08-25 17:06:19 -07001599 dest_priority, dest_dscp,
Xiaoping Fan978b3772015-05-27 14:15:18 -07001600 dest_rx_packets, dest_rx_bytes,
1601#ifdef CONFIG_NF_FLOW_COOKIE
1602 src_flow_cookie, dst_flow_cookie,
1603#endif
1604 last_sync_jiffies, mark);
1605
1606 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1607 return false;
1608 }
1609
1610 *length -= bytes_read;
1611 *total_read += bytes_read;
1612
Xiaoping Fan978b3772015-05-27 14:15:18 -07001613 return true;
1614}
1615
1616/*
1617 * sfe_ipv6_debug_dev_read_connections_end()
1618 * Generate part of the XML output.
1619 */
1620static bool sfe_ipv6_debug_dev_read_connections_end(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
1621 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
1622{
1623 int bytes_read;
1624
1625 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t</connections>\n");
1626 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1627 return false;
1628 }
1629
1630 *length -= bytes_read;
1631 *total_read += bytes_read;
1632
1633 ws->state++;
1634 return true;
1635}
1636
1637/*
1638 * sfe_ipv6_debug_dev_read_exceptions_start()
1639 * Generate part of the XML output.
1640 */
1641static bool sfe_ipv6_debug_dev_read_exceptions_start(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
1642 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
1643{
1644 int bytes_read;
1645
1646 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<exceptions>\n");
1647 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1648 return false;
1649 }
1650
1651 *length -= bytes_read;
1652 *total_read += bytes_read;
1653
1654 ws->state++;
1655 return true;
1656}
1657
1658/*
1659 * sfe_ipv6_debug_dev_read_exceptions_exception()
1660 * Generate part of the XML output.
1661 */
1662static bool sfe_ipv6_debug_dev_read_exceptions_exception(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
1663 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
1664{
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301665 int i;
1666 u64 val = 0;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001667
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301668 for_each_possible_cpu(i) {
1669 const struct sfe_ipv6_stats *s = per_cpu_ptr(si->stats_pcpu, i);
1670 val += s->exception_events64[ws->iter_exception];
1671 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07001672
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301673 if (val) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07001674 int bytes_read;
1675
1676 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE,
1677 "\t\t<exception name=\"%s\" count=\"%llu\" />\n",
1678 sfe_ipv6_exception_events_string[ws->iter_exception],
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301679 val);
1680
Xiaoping Fan978b3772015-05-27 14:15:18 -07001681 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1682 return false;
1683 }
1684
1685 *length -= bytes_read;
1686 *total_read += bytes_read;
1687 }
1688
1689 ws->iter_exception++;
1690 if (ws->iter_exception >= SFE_IPV6_EXCEPTION_EVENT_LAST) {
1691 ws->iter_exception = 0;
1692 ws->state++;
1693 }
1694
1695 return true;
1696}
1697
1698/*
1699 * sfe_ipv6_debug_dev_read_exceptions_end()
1700 * Generate part of the XML output.
1701 */
1702static bool sfe_ipv6_debug_dev_read_exceptions_end(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
1703 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
1704{
1705 int bytes_read;
1706
1707 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t</exceptions>\n");
1708 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1709 return false;
1710 }
1711
1712 *length -= bytes_read;
1713 *total_read += bytes_read;
1714
1715 ws->state++;
1716 return true;
1717}
1718
1719/*
1720 * sfe_ipv6_debug_dev_read_stats()
1721 * Generate part of the XML output.
1722 */
1723static bool sfe_ipv6_debug_dev_read_stats(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
1724 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
1725{
1726 int bytes_read;
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301727 struct sfe_ipv6_stats stats;
1728 unsigned int num_conn;
1729
1730 sfe_ipv6_update_summary_stats(si, &stats);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001731
1732 spin_lock_bh(&si->lock);
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301733 num_conn = si->num_connections;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001734 spin_unlock_bh(&si->lock);
1735
1736 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<stats "
1737 "num_connections=\"%u\" "
1738 "pkts_forwarded=\"%llu\" pkts_not_forwarded=\"%llu\" "
1739 "create_requests=\"%llu\" create_collisions=\"%llu\" "
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301740 "create_failures=\"%llu\" "
Xiaoping Fan978b3772015-05-27 14:15:18 -07001741 "destroy_requests=\"%llu\" destroy_misses=\"%llu\" "
1742 "flushes=\"%llu\" "
1743 "hash_hits=\"%llu\" hash_reorders=\"%llu\" />\n",
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301744
1745 num_conn,
1746 stats.packets_forwarded64,
1747 stats.packets_not_forwarded64,
1748 stats.connection_create_requests64,
1749 stats.connection_create_collisions64,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301750 stats.connection_create_failures64,
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301751 stats.connection_destroy_requests64,
1752 stats.connection_destroy_misses64,
1753 stats.connection_flushes64,
1754 stats.connection_match_hash_hits64,
1755 stats.connection_match_hash_reorders64);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001756 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1757 return false;
1758 }
1759
1760 *length -= bytes_read;
1761 *total_read += bytes_read;
1762
1763 ws->state++;
1764 return true;
1765}
1766
1767/*
1768 * sfe_ipv6_debug_dev_read_end()
1769 * Generate part of the XML output.
1770 */
1771static bool sfe_ipv6_debug_dev_read_end(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
1772 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
1773{
1774 int bytes_read;
1775
1776 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "</sfe_ipv6>\n");
1777 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1778 return false;
1779 }
1780
1781 *length -= bytes_read;
1782 *total_read += bytes_read;
1783
1784 ws->state++;
1785 return true;
1786}
1787
1788/*
1789 * Array of write functions that write various XML elements that correspond to
1790 * our XML output state machine.
1791 */
1792static sfe_ipv6_debug_xml_write_method_t sfe_ipv6_debug_xml_write_methods[SFE_IPV6_DEBUG_XML_STATE_DONE] = {
1793 sfe_ipv6_debug_dev_read_start,
1794 sfe_ipv6_debug_dev_read_connections_start,
1795 sfe_ipv6_debug_dev_read_connections_connection,
1796 sfe_ipv6_debug_dev_read_connections_end,
1797 sfe_ipv6_debug_dev_read_exceptions_start,
1798 sfe_ipv6_debug_dev_read_exceptions_exception,
1799 sfe_ipv6_debug_dev_read_exceptions_end,
1800 sfe_ipv6_debug_dev_read_stats,
1801 sfe_ipv6_debug_dev_read_end,
1802};
1803
1804/*
1805 * sfe_ipv6_debug_dev_read()
1806 * Send info to userspace upon read request from user
1807 */
1808static ssize_t sfe_ipv6_debug_dev_read(struct file *filp, char *buffer, size_t length, loff_t *offset)
1809{
1810 char msg[CHAR_DEV_MSG_SIZE];
1811 int total_read = 0;
1812 struct sfe_ipv6_debug_xml_write_state *ws;
1813 struct sfe_ipv6 *si = &__si6;
1814
1815 ws = (struct sfe_ipv6_debug_xml_write_state *)filp->private_data;
1816 while ((ws->state != SFE_IPV6_DEBUG_XML_STATE_DONE) && (length > CHAR_DEV_MSG_SIZE)) {
1817 if ((sfe_ipv6_debug_xml_write_methods[ws->state])(si, buffer, msg, &length, &total_read, ws)) {
1818 continue;
1819 }
1820 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07001821 return total_read;
1822}
1823
1824/*
Xiaoping Fan978b3772015-05-27 14:15:18 -07001825 * sfe_ipv6_debug_dev_open()
1826 */
1827static int sfe_ipv6_debug_dev_open(struct inode *inode, struct file *file)
1828{
1829 struct sfe_ipv6_debug_xml_write_state *ws;
1830
1831 ws = (struct sfe_ipv6_debug_xml_write_state *)file->private_data;
1832 if (ws) {
1833 return 0;
1834 }
1835
1836 ws = kzalloc(sizeof(struct sfe_ipv6_debug_xml_write_state), GFP_KERNEL);
1837 if (!ws) {
1838 return -ENOMEM;
1839 }
1840
1841 ws->state = SFE_IPV6_DEBUG_XML_STATE_START;
1842 file->private_data = ws;
1843
1844 return 0;
1845}
1846
1847/*
1848 * sfe_ipv6_debug_dev_release()
1849 */
1850static int sfe_ipv6_debug_dev_release(struct inode *inode, struct file *file)
1851{
1852 struct sfe_ipv6_debug_xml_write_state *ws;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001853
1854 ws = (struct sfe_ipv6_debug_xml_write_state *)file->private_data;
Xiaoping Fan34586472015-07-03 02:20:35 -07001855 if (ws) {
1856 /*
1857 * We've finished with our output so free the write state.
1858 */
1859 kfree(ws);
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301860 file->private_data = NULL;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001861 }
1862
Xiaoping Fan978b3772015-05-27 14:15:18 -07001863 return 0;
1864}
1865
1866/*
1867 * File operations used in the debug char device
1868 */
1869static struct file_operations sfe_ipv6_debug_dev_fops = {
1870 .read = sfe_ipv6_debug_dev_read,
Xiaoping Fan978b3772015-05-27 14:15:18 -07001871 .open = sfe_ipv6_debug_dev_open,
1872 .release = sfe_ipv6_debug_dev_release
1873};
1874
1875#ifdef CONFIG_NF_FLOW_COOKIE
1876/*
1877 * sfe_ipv6_register_flow_cookie_cb
1878 * register a function in SFE to let SFE use this function to configure flow cookie for a flow
1879 *
1880 * Hardware driver which support flow cookie should register a callback function in SFE. Then SFE
1881 * can use this function to configure flow cookie for a flow.
1882 * return: 0, success; !=0, fail
1883 */
1884int sfe_ipv6_register_flow_cookie_cb(sfe_ipv6_flow_cookie_set_func_t cb)
1885{
1886 struct sfe_ipv6 *si = &__si6;
1887
1888 BUG_ON(!cb);
1889
1890 if (si->flow_cookie_set_func) {
1891 return -1;
1892 }
1893
1894 rcu_assign_pointer(si->flow_cookie_set_func, cb);
1895 return 0;
1896}
1897
1898/*
1899 * sfe_ipv6_unregister_flow_cookie_cb
1900 * unregister function which is used to configure flow cookie for a flow
1901 *
1902 * return: 0, success; !=0, fail
1903 */
1904int sfe_ipv6_unregister_flow_cookie_cb(sfe_ipv6_flow_cookie_set_func_t cb)
1905{
1906 struct sfe_ipv6 *si = &__si6;
1907
1908 RCU_INIT_POINTER(si->flow_cookie_set_func, NULL);
1909 return 0;
1910}
Xiaoping Fan640faf42015-08-28 15:50:55 -07001911
1912/*
1913 * sfe_ipv6_get_flow_cookie()
1914 */
1915static ssize_t sfe_ipv6_get_flow_cookie(struct device *dev,
1916 struct device_attribute *attr,
1917 char *buf)
1918{
1919 struct sfe_ipv6 *si = &__si6;
Xiaoping Fan01c67cc2015-11-09 11:31:57 -08001920 return snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", si->flow_cookie_enable);
Xiaoping Fan640faf42015-08-28 15:50:55 -07001921}
1922
1923/*
1924 * sfe_ipv6_set_flow_cookie()
1925 */
1926static ssize_t sfe_ipv6_set_flow_cookie(struct device *dev,
1927 struct device_attribute *attr,
1928 const char *buf, size_t size)
1929{
1930 struct sfe_ipv6 *si = &__si6;
Ken Zhu137722d2021-09-23 17:57:36 -07001931 si->flow_cookie_enable = strict_strtol(buf, NULL, 0);
Xiaoping Fan640faf42015-08-28 15:50:55 -07001932
1933 return size;
1934}
1935
1936/*
1937 * sysfs attributes.
1938 */
1939static const struct device_attribute sfe_ipv6_flow_cookie_attr =
Xiaoping Fane70da412016-02-26 16:47:57 -08001940 __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 -07001941#endif /*CONFIG_NF_FLOW_COOKIE*/
1942
Ken Zhu137722d2021-09-23 17:57:36 -07001943/*
1944 * sfe_ipv6_get_cpu()
1945 */
1946static ssize_t sfe_ipv6_get_cpu(struct device *dev,
1947 struct device_attribute *attr,
1948 char *buf)
1949{
1950 struct sfe_ipv6 *si = &__si6;
1951 return snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", si->work_cpu);
1952}
1953
1954/*
1955 * sfe_ipv4_set_cpu()
1956 */
1957static ssize_t sfe_ipv6_set_cpu(struct device *dev,
1958 struct device_attribute *attr,
1959 const char *buf, size_t size)
1960{
1961 struct sfe_ipv6 *si = &__si6;
1962 int work_cpu;
1963
1964 work_cpu = simple_strtol(buf, NULL, 0);
1965 if ((work_cpu >= 0) && (work_cpu <= NR_CPUS)) {
1966 si->work_cpu = work_cpu;
1967 } else {
1968 dev_err(dev, "%s is not in valid range[0,%d]", buf, NR_CPUS);
1969 }
1970
1971 return size;
1972}
1973/*
1974 * sysfs attributes.
1975 */
1976static const struct device_attribute sfe_ipv6_cpu_attr =
1977 __ATTR(stat_work_cpu, S_IWUSR | S_IRUGO, sfe_ipv6_get_cpu, sfe_ipv6_set_cpu);
1978
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301979 /*
1980 * sfe_ipv6_hash_init()
1981 * Initialize conn match hash lists
1982 */
1983static void sfe_ipv6_conn_match_hash_init(struct sfe_ipv6 *si, int len)
1984{
1985 struct hlist_head *hash_list = si->hlist_conn_match_hash_head;
1986 int i;
1987
1988 for (i = 0; i < len; i++) {
1989 INIT_HLIST_HEAD(&hash_list[i]);
1990 }
1991}
1992
Xiaoping Fan978b3772015-05-27 14:15:18 -07001993/*
1994 * sfe_ipv6_init()
1995 */
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05301996int sfe_ipv6_init(void)
Xiaoping Fan978b3772015-05-27 14:15:18 -07001997{
1998 struct sfe_ipv6 *si = &__si6;
1999 int result = -1;
2000
2001 DEBUG_INFO("SFE IPv6 init\n");
2002
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05302003 sfe_ipv6_conn_match_hash_init(si, ARRAY_SIZE(si->hlist_conn_match_hash_head));
2004
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302005 si->stats_pcpu = alloc_percpu_gfp(struct sfe_ipv6_stats, GFP_KERNEL | __GFP_ZERO);
2006 if (!si->stats_pcpu) {
2007 DEBUG_ERROR("failed to allocate stats memory for sfe_ipv6\n");
2008 goto exit0;
2009 }
2010
Xiaoping Fan978b3772015-05-27 14:15:18 -07002011 /*
2012 * Create sys/sfe_ipv6
2013 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302014 si->sys_ipv6 = kobject_create_and_add("sfe_ipv6", NULL);
2015 if (!si->sys_ipv6) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07002016 DEBUG_ERROR("failed to register sfe_ipv6\n");
2017 goto exit1;
2018 }
2019
2020 /*
2021 * Create files, one for each parameter supported by this module.
2022 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302023 result = sysfs_create_file(si->sys_ipv6, &sfe_ipv6_debug_dev_attr.attr);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002024 if (result) {
2025 DEBUG_ERROR("failed to register debug dev file: %d\n", result);
2026 goto exit2;
2027 }
2028
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302029 result = sysfs_create_file(si->sys_ipv6, &sfe_ipv6_cpu_attr.attr);
Ken Zhu137722d2021-09-23 17:57:36 -07002030 if (result) {
2031 DEBUG_ERROR("failed to register debug dev file: %d\n", result);
2032 goto exit3;
2033 }
2034
Xiaoping Fan640faf42015-08-28 15:50:55 -07002035#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302036 result = sysfs_create_file(si->sys_ipv6, &sfe_ipv6_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002037 if (result) {
2038 DEBUG_ERROR("failed to register flow cookie enable file: %d\n", result);
Ken Zhu137722d2021-09-23 17:57:36 -07002039 goto exit4;
Xiaoping Fan640faf42015-08-28 15:50:55 -07002040 }
2041#endif /* CONFIG_NF_FLOW_COOKIE */
2042
Xiaoping Fan978b3772015-05-27 14:15:18 -07002043 /*
2044 * Register our debug char device.
2045 */
2046 result = register_chrdev(0, "sfe_ipv6", &sfe_ipv6_debug_dev_fops);
2047 if (result < 0) {
2048 DEBUG_ERROR("Failed to register chrdev: %d\n", result);
Ken Zhu137722d2021-09-23 17:57:36 -07002049 goto exit5;
Xiaoping Fan978b3772015-05-27 14:15:18 -07002050 }
2051
2052 si->debug_dev = result;
Ken Zhu137722d2021-09-23 17:57:36 -07002053 si->work_cpu = WORK_CPU_UNBOUND;
Xiaoping Fan978b3772015-05-27 14:15:18 -07002054
2055 /*
Ken Zhu137722d2021-09-23 17:57:36 -07002056 * Create work to handle periodic statistics.
Xiaoping Fan978b3772015-05-27 14:15:18 -07002057 */
Ken Zhu137722d2021-09-23 17:57:36 -07002058 INIT_DELAYED_WORK(&(si->sync_dwork), sfe_ipv6_periodic_sync);
2059 schedule_delayed_work_on(si->work_cpu, &(si->sync_dwork), ((HZ + 99) / 100));
Xiaoping Fan978b3772015-05-27 14:15:18 -07002060 spin_lock_init(&si->lock);
2061
2062 return 0;
2063
Ken Zhu137722d2021-09-23 17:57:36 -07002064exit5:
Xiaoping Fan640faf42015-08-28 15:50:55 -07002065#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302066 sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002067
Ken Zhu137722d2021-09-23 17:57:36 -07002068exit4:
Xiaoping Fan640faf42015-08-28 15:50:55 -07002069#endif /* CONFIG_NF_FLOW_COOKIE */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302070 sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_cpu_attr.attr);
Ken Zhu137722d2021-09-23 17:57:36 -07002071exit3:
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302072 sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_debug_dev_attr.attr);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002073
2074exit2:
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302075 kobject_put(si->sys_ipv6);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002076
2077exit1:
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302078 free_percpu(si->stats_pcpu);
2079
2080exit0:
Xiaoping Fan978b3772015-05-27 14:15:18 -07002081 return result;
2082}
2083
2084/*
2085 * sfe_ipv6_exit()
2086 */
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05302087void sfe_ipv6_exit(void)
Xiaoping Fan978b3772015-05-27 14:15:18 -07002088{
2089 struct sfe_ipv6 *si = &__si6;
2090
2091 DEBUG_INFO("SFE IPv6 exit\n");
2092
2093 /*
2094 * Destroy all connections.
2095 */
2096 sfe_ipv6_destroy_all_rules_for_dev(NULL);
2097
Ken Zhu137722d2021-09-23 17:57:36 -07002098 cancel_delayed_work(&si->sync_dwork);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002099
2100 unregister_chrdev(si->debug_dev, "sfe_ipv6");
2101
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302102 free_percpu(si->stats_pcpu);
2103
Xiaoping Fan640faf42015-08-28 15:50:55 -07002104#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302105 sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002106#endif /* CONFIG_NF_FLOW_COOKIE */
Ken Zhu137722d2021-09-23 17:57:36 -07002107
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302108 sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_cpu_attr.attr);
Ken Zhu137722d2021-09-23 17:57:36 -07002109
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302110 sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_debug_dev_attr.attr);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002111
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302112 kobject_put(si->sys_ipv6);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002113}
2114
Xiaoping Fan978b3772015-05-27 14:15:18 -07002115#ifdef CONFIG_NF_FLOW_COOKIE
2116EXPORT_SYMBOL(sfe_ipv6_register_flow_cookie_cb);
2117EXPORT_SYMBOL(sfe_ipv6_unregister_flow_cookie_cb);
2118#endif