blob: 5f2d7913dc5bbe28b74ec1d192c4e6ca4c32ccd3 [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.
Guduri Prathyusha5f27e232022-01-06 14:39:04 +05306 * Copyright (c) 2021-2022 Qualcomm Innovation Center, Inc. All rights reserved.
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05307 *
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>
Suruchi Suman23a279d2021-11-16 15:13:09 +053028#include <net/udp.h>
29#include <net/vxlan.h>
30#include <linux/refcount.h>
31#include <linux/netfilter.h>
32#include <linux/inetdevice.h>
33#include <linux/netfilter_ipv6.h>
Parikshit Guned31a8202022-01-05 22:15:04 +053034#include <linux/seqlock.h>
Tian Yangafb03452022-01-13 18:53:13 -080035#include <net/protocol.h>
Nitin Shettye6ed5b52021-12-27 14:50:11 +053036#include <net/addrconf.h>
37#include <net/gre.h>
38
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +053039#include "sfe_debug.h"
Ratheesh Kannoth89302a72021-10-20 08:10:37 +053040#include "sfe_api.h"
Xiaoping Fan978b3772015-05-27 14:15:18 -070041#include "sfe.h"
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +053042#include "sfe_flow_cookie.h"
43#include "sfe_ipv6.h"
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +053044#include "sfe_ipv6_udp.h"
45#include "sfe_ipv6_tcp.h"
46#include "sfe_ipv6_icmp.h"
Wayne Tanbb7f1782021-12-13 11:16:04 -080047#include "sfe_pppoe.h"
Tian Yangafb03452022-01-13 18:53:13 -080048#include "sfe_ipv6_tunipip6.h"
Nitin Shettye6ed5b52021-12-27 14:50:11 +053049#include "sfe_ipv6_gre.h"
Xiaoping Fan978b3772015-05-27 14:15:18 -070050
Ratheesh Kannoth89302a72021-10-20 08:10:37 +053051#define sfe_ipv6_addr_copy(src, dest) memcpy((void *)(dest), (void *)(src), 16)
52
Xiaoping Fan978b3772015-05-27 14:15:18 -070053static char *sfe_ipv6_exception_events_string[SFE_IPV6_EXCEPTION_EVENT_LAST] = {
54 "UDP_HEADER_INCOMPLETE",
55 "UDP_NO_CONNECTION",
56 "UDP_IP_OPTIONS_OR_INITIAL_FRAGMENT",
57 "UDP_SMALL_TTL",
58 "UDP_NEEDS_FRAGMENTATION",
59 "TCP_HEADER_INCOMPLETE",
60 "TCP_NO_CONNECTION_SLOW_FLAGS",
61 "TCP_NO_CONNECTION_FAST_FLAGS",
62 "TCP_IP_OPTIONS_OR_INITIAL_FRAGMENT",
63 "TCP_SMALL_TTL",
64 "TCP_NEEDS_FRAGMENTATION",
65 "TCP_FLAGS",
66 "TCP_SEQ_EXCEEDS_RIGHT_EDGE",
67 "TCP_SMALL_DATA_OFFS",
68 "TCP_BAD_SACK",
69 "TCP_BIG_DATA_OFFS",
70 "TCP_SEQ_BEFORE_LEFT_EDGE",
71 "TCP_ACK_EXCEEDS_RIGHT_EDGE",
72 "TCP_ACK_BEFORE_LEFT_EDGE",
73 "ICMP_HEADER_INCOMPLETE",
74 "ICMP_UNHANDLED_TYPE",
75 "ICMP_IPV6_HEADER_INCOMPLETE",
76 "ICMP_IPV6_NON_V6",
77 "ICMP_IPV6_IP_OPTIONS_INCOMPLETE",
78 "ICMP_IPV6_UDP_HEADER_INCOMPLETE",
79 "ICMP_IPV6_TCP_HEADER_INCOMPLETE",
80 "ICMP_IPV6_UNHANDLED_PROTOCOL",
81 "ICMP_NO_CONNECTION",
82 "ICMP_FLUSHED_CONNECTION",
83 "HEADER_INCOMPLETE",
84 "BAD_TOTAL_LENGTH",
85 "NON_V6",
86 "NON_INITIAL_FRAGMENT",
87 "DATAGRAM_INCOMPLETE",
88 "IP_OPTIONS_INCOMPLETE",
89 "UNHANDLED_PROTOCOL",
Ratheesh Kannoth5dee3772022-01-18 11:27:14 +053090 "FLOW_COOKIE_ADD_FAIL",
Nitin Shetty16ab38d2022-02-09 01:26:19 +053091 "NO_HEADROOM",
92 "INVALID_PPPOE_SESSION",
93 "INCORRECT_PPPOE_PARSING",
94 "PPPOE_NOT_SET_IN_CME",
95 "INGRESS_VLAN_TAG_MISMATCH",
Ratheesh Kannoth5dee3772022-01-18 11:27:14 +053096 "INVALID_SOURCE_INTERFACE",
Tian Yangafb03452022-01-13 18:53:13 -080097 "TUNIPIP6_HEADER_INCOMPLETE",
98 "TUNIPIP6_NO_CONNECTION",
99 "TUNIPIP6_IP_OPTIONS_OR_INITIAL_FRAGMENT",
100 "TUNIPIP6_SMALL_TTL",
101 "TUNIPIP6_NEEDS_FRAGMENTATION",
Nitin Shetty16ab38d2022-02-09 01:26:19 +0530102 "TUNIPIP6_SYNC_ON_FIND",
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530103 "GRE_HEADER_INCOMPLETE",
104 "GRE_NO_CONNECTION",
105 "GRE_IP_OPTIONS_OR_INITIAL_FRAGMENT",
106 "GRE_SMALL_TTL",
107 "GRE_NEEDS_FRAGMENTATION"
Xiaoping Fan978b3772015-05-27 14:15:18 -0700108};
109
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700110static struct sfe_ipv6 __si6;
Ken Zhu7a43d882022-01-04 10:51:44 -0800111struct sfe_ipv6_msg *sfe_ipv6_sync_many_msg;
112uint32_t sfe_ipv6_sync_max_number;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700113
114/*
115 * sfe_ipv6_get_debug_dev()
116 */
117static ssize_t sfe_ipv6_get_debug_dev(struct device *dev, struct device_attribute *attr, char *buf);
118
119/*
120 * sysfs attributes.
121 */
122static const struct device_attribute sfe_ipv6_debug_dev_attr =
Xiaoping Fane70da412016-02-26 16:47:57 -0800123 __ATTR(debug_dev, S_IWUSR | S_IRUGO, sfe_ipv6_get_debug_dev, NULL);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700124
125/*
Xiaoping Fan978b3772015-05-27 14:15:18 -0700126 * sfe_ipv6_get_connection_match_hash()
127 * Generate the hash used in connection match lookups.
128 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700129static inline unsigned int sfe_ipv6_get_connection_match_hash(struct net_device *dev, u8 protocol,
Xiaoping Fan978b3772015-05-27 14:15:18 -0700130 struct sfe_ipv6_addr *src_ip, __be16 src_port,
131 struct sfe_ipv6_addr *dest_ip, __be16 dest_port)
132{
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700133 u32 idx, hash = 0;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700134
135 for (idx = 0; idx < 4; idx++) {
136 hash ^= src_ip->addr[idx] ^ dest_ip->addr[idx];
137 }
Ratheesh Kannoth5dee3772022-01-18 11:27:14 +0530138 hash = hash ^ protocol ^ ntohs(src_port ^ dest_port);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700139 return ((hash >> SFE_IPV6_CONNECTION_HASH_SHIFT) ^ hash) & SFE_IPV6_CONNECTION_HASH_MASK;
140}
141
142/*
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530143 * sfe_ipv6_find_connection_match_rcu()
Xiaoping Fan978b3772015-05-27 14:15:18 -0700144 * Get the IPv6 flow match info that corresponds to a particular 5-tuple.
Xiaoping Fan978b3772015-05-27 14:15:18 -0700145 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530146struct sfe_ipv6_connection_match *
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530147sfe_ipv6_find_connection_match_rcu(struct sfe_ipv6 *si, struct net_device *dev, u8 protocol,
Xiaoping Fan978b3772015-05-27 14:15:18 -0700148 struct sfe_ipv6_addr *src_ip, __be16 src_port,
149 struct sfe_ipv6_addr *dest_ip, __be16 dest_port)
150{
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530151 struct sfe_ipv6_connection_match *cm = NULL;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700152 unsigned int conn_match_idx;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530153 struct hlist_head *lhead;
154 WARN_ON_ONCE(!rcu_read_lock_held());
Xiaoping Fan978b3772015-05-27 14:15:18 -0700155
156 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 -0700157
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530158 lhead = &si->hlist_conn_match_hash_head[conn_match_idx];
Xiaoping Fan978b3772015-05-27 14:15:18 -0700159
160 /*
161 * Hopefully the first entry is the one we want.
162 */
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530163 hlist_for_each_entry_rcu(cm, lhead, hnode) {
164 if ((cm->match_dest_port != dest_port) ||
Wayne Tan5e0abb52022-06-14 12:49:38 -0700165 (cm->match_src_port != src_port) ||
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530166 (!sfe_ipv6_addr_equal(cm->match_src_ip, src_ip)) ||
167 (!sfe_ipv6_addr_equal(cm->match_dest_ip, dest_ip)) ||
Wayne Tanfcbcfee2022-06-21 22:40:48 -0700168 (cm->match_protocol != protocol)) {
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530169 continue;
170 }
171
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530172 this_cpu_inc(si->stats_pcpu->connection_match_hash_hits64);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700173
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530174 break;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700175
Xiaoping Fan978b3772015-05-27 14:15:18 -0700176 }
177
Xiaoping Fan978b3772015-05-27 14:15:18 -0700178 return cm;
179}
180
181/*
182 * sfe_ipv6_connection_match_update_summary_stats()
183 * Update the summary stats for a connection match entry.
184 */
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530185static inline void sfe_ipv6_connection_match_update_summary_stats(struct sfe_ipv6_connection_match *cm,
186 u32 *packets, u32 *bytes)
187
Xiaoping Fan978b3772015-05-27 14:15:18 -0700188{
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530189 u32 packet_count, byte_count;
190
191 packet_count = atomic_read(&cm->rx_packet_count);
192 cm->rx_packet_count64 += packet_count;
193 atomic_sub(packet_count, &cm->rx_packet_count);
194
195 byte_count = atomic_read(&cm->rx_byte_count);
196 cm->rx_byte_count64 += byte_count;
197 atomic_sub(byte_count, &cm->rx_byte_count);
198
199 *packets = packet_count;
200 *bytes = byte_count;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700201}
202
203/*
204 * sfe_ipv6_connection_match_compute_translations()
205 * Compute port and address translations for a connection match entry.
206 */
207static void sfe_ipv6_connection_match_compute_translations(struct sfe_ipv6_connection_match *cm)
208{
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700209 u32 diff[9];
210 u32 *idx_32;
211 u16 *idx_16;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700212
213 /*
214 * Before we insert the entry look to see if this is tagged as doing address
215 * translations. If it is then work out the adjustment that we need to apply
216 * to the transport checksum.
217 */
218 if (cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_XLATE_SRC) {
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700219 u32 adj = 0;
220 u32 carry = 0;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700221
222 /*
223 * Precompute an incremental checksum adjustment so we can
224 * edit packets in this stream very quickly. The algorithm is from RFC1624.
225 */
226 idx_32 = diff;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530227 *(idx_32++) = cm->match_src_ip[0].addr[0];
228 *(idx_32++) = cm->match_src_ip[0].addr[1];
229 *(idx_32++) = cm->match_src_ip[0].addr[2];
230 *(idx_32++) = cm->match_src_ip[0].addr[3];
Xiaoping Fan978b3772015-05-27 14:15:18 -0700231
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700232 idx_16 = (u16 *)idx_32;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700233 *(idx_16++) = cm->match_src_port;
234 *(idx_16++) = ~cm->xlate_src_port;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700235 idx_32 = (u32 *)idx_16;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700236
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530237 *(idx_32++) = ~cm->xlate_src_ip[0].addr[0];
238 *(idx_32++) = ~cm->xlate_src_ip[0].addr[1];
239 *(idx_32++) = ~cm->xlate_src_ip[0].addr[2];
240 *(idx_32++) = ~cm->xlate_src_ip[0].addr[3];
Xiaoping Fan978b3772015-05-27 14:15:18 -0700241
242 /*
243 * When we compute this fold it down to a 16-bit offset
244 * as that way we can avoid having to do a double
245 * folding of the twos-complement result because the
246 * addition of 2 16-bit values cannot cause a double
247 * wrap-around!
248 */
249 for (idx_32 = diff; idx_32 < diff + 9; idx_32++) {
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700250 u32 w = *idx_32;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700251 adj += carry;
252 adj += w;
253 carry = (w > adj);
254 }
255 adj += carry;
256 adj = (adj & 0xffff) + (adj >> 16);
257 adj = (adj & 0xffff) + (adj >> 16);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700258 cm->xlate_src_csum_adjustment = (u16)adj;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700259 }
260
261 if (cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_XLATE_DEST) {
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700262 u32 adj = 0;
263 u32 carry = 0;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700264
265 /*
266 * Precompute an incremental checksum adjustment so we can
267 * edit packets in this stream very quickly. The algorithm is from RFC1624.
268 */
269 idx_32 = diff;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530270 *(idx_32++) = cm->match_dest_ip[0].addr[0];
271 *(idx_32++) = cm->match_dest_ip[0].addr[1];
272 *(idx_32++) = cm->match_dest_ip[0].addr[2];
273 *(idx_32++) = cm->match_dest_ip[0].addr[3];
Xiaoping Fan978b3772015-05-27 14:15:18 -0700274
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700275 idx_16 = (u16 *)idx_32;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700276 *(idx_16++) = cm->match_dest_port;
277 *(idx_16++) = ~cm->xlate_dest_port;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700278 idx_32 = (u32 *)idx_16;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700279
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530280 *(idx_32++) = ~cm->xlate_dest_ip[0].addr[0];
281 *(idx_32++) = ~cm->xlate_dest_ip[0].addr[1];
282 *(idx_32++) = ~cm->xlate_dest_ip[0].addr[2];
283 *(idx_32++) = ~cm->xlate_dest_ip[0].addr[3];
Xiaoping Fan978b3772015-05-27 14:15:18 -0700284
285 /*
286 * When we compute this fold it down to a 16-bit offset
287 * as that way we can avoid having to do a double
288 * folding of the twos-complement result because the
289 * addition of 2 16-bit values cannot cause a double
290 * wrap-around!
291 */
292 for (idx_32 = diff; idx_32 < diff + 9; idx_32++) {
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700293 u32 w = *idx_32;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700294 adj += carry;
295 adj += w;
296 carry = (w > adj);
297 }
298 adj += carry;
299 adj = (adj & 0xffff) + (adj >> 16);
300 adj = (adj & 0xffff) + (adj >> 16);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700301 cm->xlate_dest_csum_adjustment = (u16)adj;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700302 }
303}
304
305/*
306 * sfe_ipv6_update_summary_stats()
307 * Update the summary stats.
308 */
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530309static void sfe_ipv6_update_summary_stats(struct sfe_ipv6 *si, struct sfe_ipv6_stats *stats)
Xiaoping Fan978b3772015-05-27 14:15:18 -0700310{
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530311 int i = 0;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700312
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530313 memset(stats, 0, sizeof(*stats));
Xiaoping Fan978b3772015-05-27 14:15:18 -0700314
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530315 for_each_possible_cpu(i) {
316 const struct sfe_ipv6_stats *s = per_cpu_ptr(si->stats_pcpu, i);
317
318 stats->connection_create_requests64 += s->connection_create_requests64;
319 stats->connection_create_collisions64 += s->connection_create_collisions64;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530320 stats->connection_create_failures64 += s->connection_create_failures64;
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530321 stats->connection_destroy_requests64 += s->connection_destroy_requests64;
322 stats->connection_destroy_misses64 += s->connection_destroy_misses64;
323 stats->connection_match_hash_hits64 += s->connection_match_hash_hits64;
324 stats->connection_match_hash_reorders64 += s->connection_match_hash_reorders64;
325 stats->connection_flushes64 += s->connection_flushes64;
Suruchi Suman23a279d2021-11-16 15:13:09 +0530326 stats->packets_dropped64 += s->packets_dropped64;
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530327 stats->packets_forwarded64 += s->packets_forwarded64;
Ken Zhu7e38d1a2021-11-30 17:31:46 -0800328 stats->packets_fast_xmited64 += s->packets_fast_xmited64;
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530329 stats->packets_not_forwarded64 += s->packets_not_forwarded64;
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +0530330 stats->pppoe_encap_packets_forwarded64 += s->pppoe_encap_packets_forwarded64;
331 stats->pppoe_decap_packets_forwarded64 += s->pppoe_decap_packets_forwarded64;
Guduri Prathyusha034d6352022-01-12 16:49:04 +0530332 stats->pppoe_bridge_packets_forwarded64 += s->pppoe_bridge_packets_forwarded64;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700333 }
334}
335
336/*
337 * sfe_ipv6_insert_connection_match()
338 * Insert a connection match into the hash.
339 *
340 * On entry we must be holding the lock that protects the hash table.
341 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700342static inline void sfe_ipv6_insert_connection_match(struct sfe_ipv6 *si,
343 struct sfe_ipv6_connection_match *cm)
Xiaoping Fan978b3772015-05-27 14:15:18 -0700344{
Xiaoping Fan978b3772015-05-27 14:15:18 -0700345 unsigned int conn_match_idx
346 = sfe_ipv6_get_connection_match_hash(cm->match_dev, cm->match_protocol,
347 cm->match_src_ip, cm->match_src_port,
348 cm->match_dest_ip, cm->match_dest_port);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700349
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530350 lockdep_assert_held(&si->lock);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700351
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530352 hlist_add_head_rcu(&cm->hnode, &si->hlist_conn_match_hash_head[conn_match_idx]);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700353#ifdef CONFIG_NF_FLOW_COOKIE
Xiaoping Fan640faf42015-08-28 15:50:55 -0700354 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 -0700355 return;
356
357 /*
358 * Configure hardware to put a flow cookie in packet of this flow,
359 * then we can accelerate the lookup process when we received this packet.
360 */
361 for (conn_match_idx = 1; conn_match_idx < SFE_FLOW_COOKIE_SIZE; conn_match_idx++) {
362 struct sfe_ipv6_flow_cookie_entry *entry = &si->sfe_flow_cookie_table[conn_match_idx];
363
364 if ((NULL == entry->match) && time_is_before_jiffies(entry->last_clean_time + HZ)) {
365 sfe_ipv6_flow_cookie_set_func_t func;
366
367 rcu_read_lock();
368 func = rcu_dereference(si->flow_cookie_set_func);
369 if (func) {
370 if (!func(cm->match_protocol, cm->match_src_ip->addr, cm->match_src_port,
371 cm->match_dest_ip->addr, cm->match_dest_port, conn_match_idx)) {
372 entry->match = cm;
373 cm->flow_cookie = conn_match_idx;
374 } else {
375 si->exception_events[SFE_IPV6_EXCEPTION_EVENT_FLOW_COOKIE_ADD_FAIL]++;
376 }
377 }
378 rcu_read_unlock();
379
380 break;
381 }
382 }
383#endif
Xiaoping Fan978b3772015-05-27 14:15:18 -0700384}
385
386/*
387 * sfe_ipv6_remove_connection_match()
388 * Remove a connection match object from the hash.
Xiaoping Fan978b3772015-05-27 14:15:18 -0700389 */
390static inline void sfe_ipv6_remove_connection_match(struct sfe_ipv6 *si, struct sfe_ipv6_connection_match *cm)
391{
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530392
393 lockdep_assert_held(&si->lock);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700394#ifdef CONFIG_NF_FLOW_COOKIE
Xiaoping Fan640faf42015-08-28 15:50:55 -0700395 if (si->flow_cookie_enable) {
396 /*
397 * Tell hardware that we no longer need a flow cookie in packet of this flow
398 */
399 unsigned int conn_match_idx;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700400
Xiaoping Fan640faf42015-08-28 15:50:55 -0700401 for (conn_match_idx = 1; conn_match_idx < SFE_FLOW_COOKIE_SIZE; conn_match_idx++) {
402 struct sfe_ipv6_flow_cookie_entry *entry = &si->sfe_flow_cookie_table[conn_match_idx];
Xiaoping Fan978b3772015-05-27 14:15:18 -0700403
Xiaoping Fan640faf42015-08-28 15:50:55 -0700404 if (cm == entry->match) {
405 sfe_ipv6_flow_cookie_set_func_t func;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700406
Xiaoping Fan640faf42015-08-28 15:50:55 -0700407 rcu_read_lock();
408 func = rcu_dereference(si->flow_cookie_set_func);
409 if (func) {
410 func(cm->match_protocol, cm->match_src_ip->addr, cm->match_src_port,
411 cm->match_dest_ip->addr, cm->match_dest_port, 0);
412 }
413 rcu_read_unlock();
414
415 cm->flow_cookie = 0;
416 entry->match = NULL;
417 entry->last_clean_time = jiffies;
418 break;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700419 }
Xiaoping Fan978b3772015-05-27 14:15:18 -0700420 }
421 }
422#endif
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530423 hlist_del_init_rcu(&cm->hnode);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700424
Xiaoping Fan978b3772015-05-27 14:15:18 -0700425}
426
427/*
428 * sfe_ipv6_get_connection_hash()
429 * Generate the hash used in connection lookups.
430 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700431static 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 -0700432 struct sfe_ipv6_addr *dest_ip, __be16 dest_port)
433{
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700434 u32 idx, hash = 0;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700435
436 for (idx = 0; idx < 4; idx++) {
437 hash ^= src_ip->addr[idx] ^ dest_ip->addr[idx];
438 }
439 hash = hash ^ protocol ^ ntohs(src_port ^ dest_port);
440 return ((hash >> SFE_IPV6_CONNECTION_HASH_SHIFT) ^ hash) & SFE_IPV6_CONNECTION_HASH_MASK;
441}
442
443/*
444 * sfe_ipv6_find_connection()
445 * Get the IPv6 connection info that corresponds to a particular 5-tuple.
446 *
447 * On entry we must be holding the lock that protects the hash table.
448 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700449static inline struct sfe_ipv6_connection *sfe_ipv6_find_connection(struct sfe_ipv6 *si, u32 protocol,
Xiaoping Fan978b3772015-05-27 14:15:18 -0700450 struct sfe_ipv6_addr *src_ip, __be16 src_port,
451 struct sfe_ipv6_addr *dest_ip, __be16 dest_port)
452{
453 struct sfe_ipv6_connection *c;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530454
Xiaoping Fan978b3772015-05-27 14:15:18 -0700455 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 +0530456
457 lockdep_assert_held(&si->lock);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700458 c = si->conn_hash[conn_idx];
459
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530460 while (c) {
461 if ((c->src_port == src_port)
462 && (c->dest_port == dest_port)
463 && (sfe_ipv6_addr_equal(c->src_ip, src_ip))
464 && (sfe_ipv6_addr_equal(c->dest_ip, dest_ip))
465 && (c->protocol == protocol)) {
466 return c;
467 }
Xiaoping Fan978b3772015-05-27 14:15:18 -0700468 c = c->next;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530469 }
Xiaoping Fan978b3772015-05-27 14:15:18 -0700470
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530471 return NULL;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700472}
473
474/*
Xiaoping Fan978b3772015-05-27 14:15:18 -0700475 * sfe_ipv6_insert_connection()
476 * Insert a connection into the hash.
477 *
478 * On entry we must be holding the lock that protects the hash table.
479 */
480static void sfe_ipv6_insert_connection(struct sfe_ipv6 *si, struct sfe_ipv6_connection *c)
481{
482 struct sfe_ipv6_connection **hash_head;
483 struct sfe_ipv6_connection *prev_head;
484 unsigned int conn_idx;
485
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530486 lockdep_assert_held(&si->lock);
487
Xiaoping Fan978b3772015-05-27 14:15:18 -0700488 /*
489 * Insert entry into the connection hash.
490 */
491 conn_idx = sfe_ipv6_get_connection_hash(c->protocol, c->src_ip, c->src_port,
492 c->dest_ip, c->dest_port);
493 hash_head = &si->conn_hash[conn_idx];
494 prev_head = *hash_head;
495 c->prev = NULL;
496 if (prev_head) {
497 prev_head->prev = c;
498 }
499
500 c->next = prev_head;
501 *hash_head = c;
502
503 /*
504 * Insert entry into the "all connections" list.
505 */
506 if (si->all_connections_tail) {
507 c->all_connections_prev = si->all_connections_tail;
508 si->all_connections_tail->all_connections_next = c;
509 } else {
510 c->all_connections_prev = NULL;
511 si->all_connections_head = c;
512 }
513
514 si->all_connections_tail = c;
515 c->all_connections_next = NULL;
516 si->num_connections++;
517
518 /*
519 * Insert the connection match objects too.
520 */
521 sfe_ipv6_insert_connection_match(si, c->original_match);
522 sfe_ipv6_insert_connection_match(si, c->reply_match);
523}
524
525/*
526 * sfe_ipv6_remove_connection()
527 * Remove a sfe_ipv6_connection object from the hash.
528 *
529 * On entry we must be holding the lock that protects the hash table.
530 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530531bool sfe_ipv6_remove_connection(struct sfe_ipv6 *si, struct sfe_ipv6_connection *c)
Xiaoping Fan978b3772015-05-27 14:15:18 -0700532{
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530533
534 lockdep_assert_held(&si->lock);
535 if (c->removed) {
536 DEBUG_ERROR("%px: Connection has been removed already\n", c);
537 return false;
538 }
539
Xiaoping Fan978b3772015-05-27 14:15:18 -0700540 /*
Tian Yang435afc42022-02-02 12:47:32 -0800541 * dereference the decap direction top_interface_dev
542 */
543 if (c->reply_match->top_interface_dev) {
544 dev_put(c->reply_match->top_interface_dev);
545 }
546 /*
Xiaoping Fan978b3772015-05-27 14:15:18 -0700547 * Remove the connection match objects.
548 */
549 sfe_ipv6_remove_connection_match(si, c->reply_match);
550 sfe_ipv6_remove_connection_match(si, c->original_match);
551
552 /*
553 * Unlink the connection.
554 */
555 if (c->prev) {
556 c->prev->next = c->next;
557 } else {
558 unsigned int conn_idx = sfe_ipv6_get_connection_hash(c->protocol, c->src_ip, c->src_port,
559 c->dest_ip, c->dest_port);
560 si->conn_hash[conn_idx] = c->next;
561 }
562
563 if (c->next) {
564 c->next->prev = c->prev;
565 }
Xiaoping Fan34586472015-07-03 02:20:35 -0700566
567 /*
568 * Unlink connection from all_connections list
569 */
570 if (c->all_connections_prev) {
571 c->all_connections_prev->all_connections_next = c->all_connections_next;
572 } else {
573 si->all_connections_head = c->all_connections_next;
574 }
575
576 if (c->all_connections_next) {
577 c->all_connections_next->all_connections_prev = c->all_connections_prev;
578 } else {
579 si->all_connections_tail = c->all_connections_prev;
580 }
581
Ken Zhu32b95392021-09-03 13:52:04 -0700582 /*
583 * If I am the next sync connection, move the sync to my next or head.
584 */
585 if (unlikely(si->wc_next == c)) {
586 si->wc_next = c->all_connections_next;
587 }
588
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530589 c->removed = true;
Xiaoping Fan34586472015-07-03 02:20:35 -0700590 si->num_connections--;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530591 return true;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700592}
593
594/*
595 * sfe_ipv6_gen_sync_connection()
596 * Sync a connection.
597 *
598 * On entry to this function we expect that the lock for the connection is either
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530599 * already held (while called from sfe_ipv6_periodic_sync() or isn't required
600 * (while called from sfe_ipv6_flush_sfe_ipv6_connection())
Xiaoping Fan978b3772015-05-27 14:15:18 -0700601 */
602static void sfe_ipv6_gen_sync_connection(struct sfe_ipv6 *si, struct sfe_ipv6_connection *c,
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700603 struct sfe_connection_sync *sis, sfe_sync_reason_t reason,
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700604 u64 now_jiffies)
Xiaoping Fan978b3772015-05-27 14:15:18 -0700605{
606 struct sfe_ipv6_connection_match *original_cm;
607 struct sfe_ipv6_connection_match *reply_cm;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530608 u32 packet_count, byte_count;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700609
610 /*
611 * Fill in the update message.
612 */
Murat Sezgin53509a12016-12-27 16:57:34 -0800613 sis->is_v6 = 1;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700614 sis->protocol = c->protocol;
615 sis->src_ip.ip6[0] = c->src_ip[0];
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700616 sis->src_ip_xlate.ip6[0] = c->src_ip_xlate[0];
Xiaoping Fan978b3772015-05-27 14:15:18 -0700617 sis->dest_ip.ip6[0] = c->dest_ip[0];
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700618 sis->dest_ip_xlate.ip6[0] = c->dest_ip_xlate[0];
Xiaoping Fan978b3772015-05-27 14:15:18 -0700619 sis->src_port = c->src_port;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700620 sis->src_port_xlate = c->src_port_xlate;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700621 sis->dest_port = c->dest_port;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700622 sis->dest_port_xlate = c->dest_port_xlate;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700623
624 original_cm = c->original_match;
625 reply_cm = c->reply_match;
626 sis->src_td_max_window = original_cm->protocol_state.tcp.max_win;
627 sis->src_td_end = original_cm->protocol_state.tcp.end;
628 sis->src_td_max_end = original_cm->protocol_state.tcp.max_end;
629 sis->dest_td_max_window = reply_cm->protocol_state.tcp.max_win;
630 sis->dest_td_end = reply_cm->protocol_state.tcp.end;
631 sis->dest_td_max_end = reply_cm->protocol_state.tcp.max_end;
632
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530633 sfe_ipv6_connection_match_update_summary_stats(original_cm, &packet_count, &byte_count);
634 sis->src_new_packet_count = packet_count;
635 sis->src_new_byte_count = byte_count;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700636
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530637 sfe_ipv6_connection_match_update_summary_stats(reply_cm, &packet_count, &byte_count);
638 sis->dest_new_packet_count = packet_count;
639 sis->dest_new_byte_count = byte_count;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700640
641 sis->src_dev = original_cm->match_dev;
642 sis->src_packet_count = original_cm->rx_packet_count64;
643 sis->src_byte_count = original_cm->rx_byte_count64;
644
645 sis->dest_dev = reply_cm->match_dev;
646 sis->dest_packet_count = reply_cm->rx_packet_count64;
647 sis->dest_byte_count = reply_cm->rx_byte_count64;
648
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700649 sis->reason = reason;
650
Xiaoping Fan978b3772015-05-27 14:15:18 -0700651 /*
652 * Get the time increment since our last sync.
653 */
654 sis->delta_jiffies = now_jiffies - c->last_sync_jiffies;
655 c->last_sync_jiffies = now_jiffies;
656}
657
658/*
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530659 * sfe_ipv6_free_sfe_ipv6_connection_rcu()
660 * Called at RCU qs state to free the connection object.
661 */
662static void sfe_ipv6_free_sfe_ipv6_connection_rcu(struct rcu_head *head)
663{
664 struct sfe_ipv6_connection *c;
Suruchi Suman23a279d2021-11-16 15:13:09 +0530665 struct udp_sock *up;
666 struct sock *sk;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530667
668 /*
669 * We dont need spin lock as the connection is already removed from link list
670 */
671 c = container_of(head, struct sfe_ipv6_connection, rcu);
672 BUG_ON(!c->removed);
673
674 DEBUG_TRACE("%px: connecton has been deleted\n", c);
675
676 /*
Suruchi Suman23a279d2021-11-16 15:13:09 +0530677 * Decrease the refcount taken in function sfe_ipv6_create_rule()
678 * during call of __udp6_lib_lookup()
679 */
680 up = c->reply_match->up;
681 if (up) {
682 sk = (struct sock *)up;
683 sock_put(sk);
684 }
685
686 /*
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530687 * Release our hold of the source and dest devices and free the memory
688 * for our connection objects.
689 */
690 dev_put(c->original_dev);
691 dev_put(c->reply_dev);
692 kfree(c->original_match);
693 kfree(c->reply_match);
694 kfree(c);
695}
696
697/*
Ken Zhu88c58152021-12-09 15:12:06 -0800698 * sfe_ipv6_sync_status()
699 * update a connection status to its connection manager.
700 *
701 * si: the ipv6 context
702 * c: which connection to be notified
703 * reason: what kind of reason: flush, or destroy
704 */
705void sfe_ipv6_sync_status(struct sfe_ipv6 *si,
706 struct sfe_ipv6_connection *c,
707 sfe_sync_reason_t reason)
708{
709 struct sfe_connection_sync sis;
710 u64 now_jiffies;
711 sfe_sync_rule_callback_t sync_rule_callback;
712
713 rcu_read_lock();
714 sync_rule_callback = rcu_dereference(si->sync_rule_callback);
Ken Zhu7a43d882022-01-04 10:51:44 -0800715 rcu_read_unlock();
Ken Zhu88c58152021-12-09 15:12:06 -0800716 if (unlikely(!sync_rule_callback)) {
Ken Zhu88c58152021-12-09 15:12:06 -0800717 return;
718 }
719
720 /*
721 * Generate a sync message and then sync.
722 */
723 now_jiffies = get_jiffies_64();
724 sfe_ipv6_gen_sync_connection(si, c, &sis, reason, now_jiffies);
725 sync_rule_callback(&sis);
Ken Zhu88c58152021-12-09 15:12:06 -0800726}
727
728/*
Xiaoping Fan978b3772015-05-27 14:15:18 -0700729 * sfe_ipv6_flush_connection()
730 * Flush a connection and free all associated resources.
731 *
732 * We need to be called with bottom halves disabled locally as we need to acquire
733 * the connection hash lock and release it again. In general we're actually called
734 * from within a BH and so we're fine, but we're also called when connections are
735 * torn down.
736 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530737void sfe_ipv6_flush_connection(struct sfe_ipv6 *si,
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700738 struct sfe_ipv6_connection *c,
739 sfe_sync_reason_t reason)
Xiaoping Fan978b3772015-05-27 14:15:18 -0700740{
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530741 BUG_ON(!c->removed);
742
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530743 this_cpu_inc(si->stats_pcpu->connection_flushes64);
Ken Zhu88c58152021-12-09 15:12:06 -0800744 sfe_ipv6_sync_status(si, c, reason);
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530745
746 /*
Ken Zhu88c58152021-12-09 15:12:06 -0800747 * Release our hold of the source and dest devices and free the memory
748 * for our connection objects.
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530749 */
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530750 call_rcu(&c->rcu, sfe_ipv6_free_sfe_ipv6_connection_rcu);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700751}
752
Parikshit Guned31a8202022-01-05 22:15:04 +0530753/*
Jackson Bockus3fafbf32022-02-13 17:15:26 -0800754 * sfe_ipv4_service_class_stats_pcpu_get()
755 * Gets one CPU's service class statistics.
756 */
757static inline bool sfe_ipv6_service_class_stats_pcpu_get(struct sfe_ipv6_per_service_class_stats *sc_stats, uint64_t *bytes, uint64_t *packets)
758{
759 uint32_t retries = 0;
760 uint32_t seq;
761 uint64_t bytes_tmp, packets_tmp;
762
763 do {
764 seq = read_seqcount_begin(&sc_stats->seq);
765 bytes_tmp = sc_stats->tx_bytes;
766 packets_tmp = sc_stats->tx_packets;
767 } while (read_seqcount_retry(&sc_stats->seq, seq) && ++retries < SFE_SERVICE_CLASS_STATS_MAX_RETRY);
768
769 *bytes += bytes_tmp;
770 *packets += packets_tmp;
771
772 return retries < SFE_SERVICE_CLASS_STATS_MAX_RETRY;
773}
774
775/*
776 * sfe_ipv4_service_class_stats_get()
777 * Copy the ipv4 statistics for the given service class.
778 */
779bool sfe_ipv6_service_class_stats_get(uint8_t sid, uint64_t *bytes, uint64_t *packets)
780{
781 struct sfe_ipv6 *si = &__si6;
782 uint32_t cpu = 0;
783
784 for_each_possible_cpu(cpu) {
785 struct sfe_ipv6_service_class_stats_db *stats_db = per_cpu_ptr(si->stats_pcpu_psc, cpu);
786 struct sfe_ipv6_per_service_class_stats *sc_stats = &stats_db->psc_stats[sid];
787
788 if (!sfe_ipv6_service_class_stats_pcpu_get(sc_stats, bytes, packets)) {
789 return false;
790 }
791 }
792
793 return true;
794}
795
796/*
Parikshit Guned31a8202022-01-05 22:15:04 +0530797 * sfe_ipv6_service_class_stats_inc()
798 * Increment per cpu per service class stats.
799 */
800void sfe_ipv6_service_class_stats_inc(struct sfe_ipv6 *si, uint8_t sid, uint64_t bytes)
801{
802 struct sfe_ipv6_service_class_stats_db *sc_stats_db = this_cpu_ptr(si->stats_pcpu_psc);
803 struct sfe_ipv6_per_service_class_stats *sc_stats = &sc_stats_db->psc_stats[sid];
804
805 write_seqcount_begin(&sc_stats->seq);
806 sc_stats->tx_bytes += bytes;
807 sc_stats->tx_packets++;
808 write_seqcount_end(&sc_stats->seq);
809}
810
811/*
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530812 * sfe_ipv6_exception_stats_inc()
813 * Increment exception stats.
814 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530815void sfe_ipv6_exception_stats_inc(struct sfe_ipv6 *si, enum sfe_ipv6_exception_events reason)
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530816{
817 struct sfe_ipv6_stats *stats = this_cpu_ptr(si->stats_pcpu);
818
819 stats->exception_events64[reason]++;
820 stats->packets_not_forwarded64++;
821}
822
Xiaoping Fan978b3772015-05-27 14:15:18 -0700823/*
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530824 * sfe_ipv6_is_local_ip()
825 * return true if it is local ip otherwise return false
826 */
827static bool sfe_ipv6_is_local_ip(struct sfe_ipv6 *si, uint8_t *addr)
828{
829 struct net_device *dev;
830 struct in6_addr ip_addr;
831 memcpy(ip_addr.s6_addr, addr, 16);
832
833 dev = ipv6_dev_find(&init_net, &ip_addr, 1);
834 if (dev) {
835 dev_put(dev);
836 return true;
837 }
838
839 return false;
840}
841
842/*
Xiaoping Fan978b3772015-05-27 14:15:18 -0700843 * sfe_ipv6_recv()
844 * Handle packet receives and forwaring.
845 *
846 * Returns 1 if the packet is forwarded or 0 if it isn't.
847 */
Suruchi Suman23a279d2021-11-16 15:13:09 +0530848int sfe_ipv6_recv(struct net_device *dev, struct sk_buff *skb, struct sfe_l2_info *l2_info, bool tun_outer)
Xiaoping Fan978b3772015-05-27 14:15:18 -0700849{
850 struct sfe_ipv6 *si = &__si6;
851 unsigned int len;
852 unsigned int payload_len;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530853 unsigned int ihl = sizeof(struct ipv6hdr);
Ken Zhu88c58152021-12-09 15:12:06 -0800854 bool sync_on_find = false;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530855 struct ipv6hdr *iph;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700856 u8 next_hdr;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700857
858 /*
859 * Check that we have space for an IP header and an uplayer header here.
860 */
861 len = skb->len;
862 if (!pskb_may_pull(skb, ihl + sizeof(struct sfe_ipv6_ext_hdr))) {
Xiaoping Fan978b3772015-05-27 14:15:18 -0700863
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530864 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_HEADER_INCOMPLETE);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700865 DEBUG_TRACE("len: %u is too short\n", len);
866 return 0;
867 }
868
869 /*
870 * Is our IP version wrong?
871 */
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530872 iph = (struct ipv6hdr *)skb->data;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700873 if (unlikely(iph->version != 6)) {
Xiaoping Fan978b3772015-05-27 14:15:18 -0700874
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530875 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_NON_V6);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700876 DEBUG_TRACE("IP version: %u\n", iph->version);
877 return 0;
878 }
879
880 /*
881 * Does our datagram fit inside the skb?
882 */
883 payload_len = ntohs(iph->payload_len);
884 if (unlikely(payload_len > (len - ihl))) {
Xiaoping Fan978b3772015-05-27 14:15:18 -0700885
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530886 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_DATAGRAM_INCOMPLETE);
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530887 DEBUG_TRACE("payload_len: %u, exceeds len: %u\n", payload_len, (len - (unsigned int)sizeof(struct ipv6hdr)));
Xiaoping Fan978b3772015-05-27 14:15:18 -0700888 return 0;
889 }
890
891 next_hdr = iph->nexthdr;
892 while (unlikely(sfe_ipv6_is_ext_hdr(next_hdr))) {
893 struct sfe_ipv6_ext_hdr *ext_hdr;
894 unsigned int ext_hdr_len;
895
896 ext_hdr = (struct sfe_ipv6_ext_hdr *)(skb->data + ihl);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700897
898 ext_hdr_len = ext_hdr->hdr_len;
899 ext_hdr_len <<= 3;
900 ext_hdr_len += sizeof(struct sfe_ipv6_ext_hdr);
901 ihl += ext_hdr_len;
902 if (!pskb_may_pull(skb, ihl + sizeof(struct sfe_ipv6_ext_hdr))) {
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530903 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_HEADER_INCOMPLETE);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700904
905 DEBUG_TRACE("extension header %d not completed\n", next_hdr);
906 return 0;
907 }
Ken Zhu88c58152021-12-09 15:12:06 -0800908 /*
909 * Any packets have extend hdr, won't be handled in the fast
910 * path,sync its status and exception to the kernel.
911 */
912 sync_on_find = true;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700913 next_hdr = ext_hdr->next_hdr;
914 }
915
916 if (IPPROTO_UDP == next_hdr) {
Ken Zhu88c58152021-12-09 15:12:06 -0800917 return sfe_ipv6_recv_udp(si, skb, dev, len, iph, ihl, sync_on_find, l2_info, tun_outer);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700918 }
919
920 if (IPPROTO_TCP == next_hdr) {
Ken Zhu88c58152021-12-09 15:12:06 -0800921 return sfe_ipv6_recv_tcp(si, skb, dev, len, iph, ihl, sync_on_find, l2_info);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700922 }
923
924 if (IPPROTO_ICMPV6 == next_hdr) {
925 return sfe_ipv6_recv_icmp(si, skb, dev, len, iph, ihl);
926 }
927
Tian Yangafb03452022-01-13 18:53:13 -0800928 if (IPPROTO_IPIP == next_hdr) {
929 return sfe_ipv6_recv_tunipip6(si, skb, dev, len, iph, ihl, sync_on_find, l2_info, true);
930 }
931
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530932#ifdef SFE_GRE_TUN_ENABLE
933 if (IPPROTO_GRE == next_hdr) {
Nitin Shetty2114a892022-01-28 20:03:56 +0530934 return sfe_ipv6_recv_gre(si, skb, dev, len, iph, ihl, sync_on_find, l2_info, tun_outer);
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530935 }
936#endif
937
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530938 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_UNHANDLED_PROTOCOL);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700939 DEBUG_TRACE("not UDP, TCP or ICMP: %u\n", next_hdr);
940 return 0;
941}
942
943/*
944 * sfe_ipv6_update_tcp_state()
945 * update TCP window variables.
946 */
947static void
948sfe_ipv6_update_tcp_state(struct sfe_ipv6_connection *c,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530949 struct sfe_ipv6_rule_create_msg *msg)
Xiaoping Fan978b3772015-05-27 14:15:18 -0700950{
951 struct sfe_ipv6_connection_match *orig_cm;
952 struct sfe_ipv6_connection_match *repl_cm;
953 struct sfe_ipv6_tcp_connection_match *orig_tcp;
954 struct sfe_ipv6_tcp_connection_match *repl_tcp;
955
956 orig_cm = c->original_match;
957 repl_cm = c->reply_match;
958 orig_tcp = &orig_cm->protocol_state.tcp;
959 repl_tcp = &repl_cm->protocol_state.tcp;
960
961 /* update orig */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530962 if (orig_tcp->max_win < msg->tcp_rule.flow_max_window) {
963 orig_tcp->max_win = msg->tcp_rule.flow_max_window;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700964 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530965 if ((s32)(orig_tcp->end - msg->tcp_rule.flow_end) < 0) {
966 orig_tcp->end = msg->tcp_rule.flow_end;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700967 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530968 if ((s32)(orig_tcp->max_end - msg->tcp_rule.flow_max_end) < 0) {
969 orig_tcp->max_end = msg->tcp_rule.flow_max_end;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700970 }
971
972 /* update reply */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530973 if (repl_tcp->max_win < msg->tcp_rule.return_max_window) {
974 repl_tcp->max_win = msg->tcp_rule.return_max_window;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700975 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530976 if ((s32)(repl_tcp->end - msg->tcp_rule.return_end) < 0) {
977 repl_tcp->end = msg->tcp_rule.return_end;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700978 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530979 if ((s32)(repl_tcp->max_end - msg->tcp_rule.return_max_end) < 0) {
980 repl_tcp->max_end = msg->tcp_rule.return_max_end;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700981 }
982
983 /* update match flags */
984 orig_cm->flags &= ~SFE_IPV6_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
985 repl_cm->flags &= ~SFE_IPV6_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530986 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_NO_SEQ_CHECK) {
Xiaoping Fan978b3772015-05-27 14:15:18 -0700987 orig_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
988 repl_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
989 }
990}
991
992/*
993 * sfe_ipv6_update_protocol_state()
994 * update protocol specified state machine.
995 */
996static void
997sfe_ipv6_update_protocol_state(struct sfe_ipv6_connection *c,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530998 struct sfe_ipv6_rule_create_msg *msg)
Xiaoping Fan978b3772015-05-27 14:15:18 -0700999{
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301000 switch (msg->tuple.protocol) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07001001 case IPPROTO_TCP:
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301002 sfe_ipv6_update_tcp_state(c, msg);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001003 break;
1004 }
1005}
1006
1007/*
Wayne Tanbb7f1782021-12-13 11:16:04 -08001008 * sfe_ipv6_match_entry_set_vlan()
1009 */
1010static void sfe_ipv6_match_entry_set_vlan(
1011 struct sfe_ipv6_connection_match *cm,
1012 u32 primary_ingress_vlan_tag,
1013 u32 primary_egress_vlan_tag,
1014 u32 secondary_ingress_vlan_tag,
1015 u32 secondary_egress_vlan_tag)
1016{
1017 u16 tpid;
1018 /*
1019 * Prevent stacking header counts when updating.
1020 */
1021 cm->ingress_vlan_hdr_cnt = 0;
1022 cm->egress_vlan_hdr_cnt = 0;
1023 memset(cm->ingress_vlan_hdr, 0, sizeof(cm->ingress_vlan_hdr));
1024 memset(cm->egress_vlan_hdr, 0, sizeof(cm->egress_vlan_hdr));
1025
1026 /*
1027 * vlan_hdr[0] corresponds to outer tag
1028 * vlan_hdr[1] corresponds to inner tag
1029 * Extract the vlan information (tpid and tci) from rule message
1030 */
1031 if ((primary_ingress_vlan_tag & VLAN_VID_MASK) != SFE_VLAN_ID_NOT_CONFIGURED) {
1032 tpid = (u16)(primary_ingress_vlan_tag >> 16);
1033 cm->ingress_vlan_hdr[0].tpid = ntohs(tpid);
1034 cm->ingress_vlan_hdr[0].tci = (u16)primary_ingress_vlan_tag;
1035 cm->ingress_vlan_hdr_cnt++;
1036 }
1037
1038 if ((secondary_ingress_vlan_tag & VLAN_VID_MASK) != SFE_VLAN_ID_NOT_CONFIGURED) {
1039 tpid = (u16)(secondary_ingress_vlan_tag >> 16);
1040 cm->ingress_vlan_hdr[1].tpid = ntohs(tpid);
1041 cm->ingress_vlan_hdr[1].tci = (u16)secondary_ingress_vlan_tag;
1042 cm->ingress_vlan_hdr_cnt++;
1043 }
1044
1045 if ((primary_egress_vlan_tag & VLAN_VID_MASK) != SFE_VLAN_ID_NOT_CONFIGURED) {
1046 tpid = (u16)(primary_egress_vlan_tag >> 16);
1047 cm->egress_vlan_hdr[0].tpid = ntohs(tpid);
1048 cm->egress_vlan_hdr[0].tci = (u16)primary_egress_vlan_tag;
1049 cm->egress_vlan_hdr_cnt++;
1050 }
1051
1052 if ((secondary_egress_vlan_tag & VLAN_VID_MASK) != SFE_VLAN_ID_NOT_CONFIGURED) {
1053 tpid = (u16)(secondary_egress_vlan_tag >> 16);
1054 cm->egress_vlan_hdr[1].tpid = ntohs(tpid);
1055 cm->egress_vlan_hdr[1].tci = (u16)secondary_egress_vlan_tag;
1056 cm->egress_vlan_hdr_cnt++;
1057 }
1058}
1059
1060/*
Xiaoping Fan978b3772015-05-27 14:15:18 -07001061 * sfe_ipv6_update_rule()
1062 * update forwarding rule after rule is created.
1063 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301064void sfe_ipv6_update_rule(struct sfe_ipv6_rule_create_msg *msg)
1065
Xiaoping Fan978b3772015-05-27 14:15:18 -07001066{
1067 struct sfe_ipv6_connection *c;
1068 struct sfe_ipv6 *si = &__si6;
1069
1070 spin_lock_bh(&si->lock);
1071
1072 c = sfe_ipv6_find_connection(si,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301073 msg->tuple.protocol,
1074 (struct sfe_ipv6_addr *)msg->tuple.flow_ip,
1075 msg->tuple.flow_ident,
1076 (struct sfe_ipv6_addr *)msg->tuple.return_ip,
1077 msg->tuple.return_ident);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001078 if (c != NULL) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301079 sfe_ipv6_update_protocol_state(c, msg);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001080 }
1081
1082 spin_unlock_bh(&si->lock);
1083}
1084
1085/*
Murat Sezginef190392022-04-04 17:56:31 -07001086 * sfe_ipv6_mark_rule_update()
1087 * Updates the mark values of match entries.
1088 */
1089void sfe_ipv6_mark_rule_update(struct sfe_connection_mark *mark)
1090{
1091 struct sfe_ipv6_connection *c;
1092 struct sfe_ipv6 *si = &__si6;
1093
1094 spin_lock_bh(&si->lock);
1095 c = sfe_ipv6_find_connection(si, mark->protocol,
1096 (struct sfe_ipv6_addr *)mark->src_ip,
1097 mark->src_port,
1098 (struct sfe_ipv6_addr *)mark->dest_ip,
1099 mark->dest_port);
1100 if (!c) {
1101 spin_unlock_bh(&si->lock);
1102 DEBUG_WARN("%px: connection not found for mark update\n", mark);
1103 return;
1104 }
1105 c->original_match ->mark = mark->mark;
1106 c->reply_match->mark = mark->mark;
1107 spin_unlock_bh(&si->lock);
1108 DEBUG_TRACE("%px: connection mark updated with %d\n", mark, mark->mark);
1109}
1110EXPORT_SYMBOL(sfe_ipv6_mark_rule_update);
1111
1112/*
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +05301113 * sfe_ipv6_xmit_eth_type_check
1114 * Checking if MAC header has to be written.
1115 */
1116static inline bool sfe_ipv6_xmit_eth_type_check(struct net_device *dev, u32 cm_flags)
1117{
1118 if (!(dev->flags & IFF_NOARP)) {
1119 return true;
1120 }
1121
1122 /*
1123 * For PPPoE, since we are now supporting PPPoE encapsulation, we are writing L2 header.
1124 */
1125 if (cm_flags & SFE_IPV6_CONNECTION_MATCH_FLAG_PPPOE_ENCAP) {
1126 return true;
1127 }
1128
1129 return false;
1130}
1131
1132/*
Xiaoping Fan978b3772015-05-27 14:15:18 -07001133 * sfe_ipv6_create_rule()
1134 * Create a forwarding rule.
1135 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301136int sfe_ipv6_create_rule(struct sfe_ipv6_rule_create_msg *msg)
Xiaoping Fan978b3772015-05-27 14:15:18 -07001137{
1138 struct sfe_ipv6 *si = &__si6;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301139 struct sfe_ipv6_connection *c, *old_c;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001140 struct sfe_ipv6_connection_match *original_cm;
1141 struct sfe_ipv6_connection_match *reply_cm;
1142 struct net_device *dest_dev;
1143 struct net_device *src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301144 struct sfe_ipv6_5tuple *tuple = &msg->tuple;
Suruchi Suman23a279d2021-11-16 15:13:09 +05301145 struct sock *sk;
1146 struct net *net;
1147 unsigned int src_if_idx;
1148
Suruchi Sumanc1a4a612021-10-21 14:50:23 +05301149 s32 flow_interface_num = msg->conn_rule.flow_top_interface_num;
1150 s32 return_interface_num = msg->conn_rule.return_top_interface_num;
Parikshit Guned31a8202022-01-05 22:15:04 +05301151 u32 flow_sawf_tag;
1152 u32 return_sawf_tag;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001153
Suruchi Sumanc1a4a612021-10-21 14:50:23 +05301154 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_FLOW_BOTTOM_INTERFACE) {
1155 flow_interface_num = msg->conn_rule.flow_interface_num;
1156 }
1157
1158 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_RETURN_BOTTOM_INTERFACE) {
1159 return_interface_num = msg->conn_rule.return_interface_num;
1160 }
1161
1162 src_dev = dev_get_by_index(&init_net, flow_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301163 if (!src_dev) {
1164 DEBUG_WARN("%px: Unable to find src_dev corresponding to %d\n", msg,
Suruchi Sumanc1a4a612021-10-21 14:50:23 +05301165 flow_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301166 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
1167 return -EINVAL;
1168 }
1169
Suruchi Sumanc1a4a612021-10-21 14:50:23 +05301170 dest_dev = dev_get_by_index(&init_net, return_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301171 if (!dest_dev) {
1172 DEBUG_WARN("%px: Unable to find dest_dev corresponding to %d\n", msg,
Suruchi Sumanc1a4a612021-10-21 14:50:23 +05301173 return_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301174 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
1175 dev_put(src_dev);
1176 return -EINVAL;
1177 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07001178
1179 if (unlikely((dest_dev->reg_state != NETREG_REGISTERED) ||
1180 (src_dev->reg_state != NETREG_REGISTERED))) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301181 DEBUG_WARN("%px: src_dev=%s and dest_dev=%s are unregistered\n", msg,
1182 src_dev->name, dest_dev->name);
1183 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
1184 dev_put(src_dev);
1185 dev_put(dest_dev);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001186 return -EINVAL;
1187 }
1188
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301189 /*
1190 * Allocate the various connection tracking objects.
1191 */
Parikshit Guneef1664c2022-03-24 14:15:42 +05301192 c = (struct sfe_ipv6_connection *)kzalloc(sizeof(struct sfe_ipv6_connection), GFP_ATOMIC);
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301193 if (unlikely(!c)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301194 DEBUG_WARN("%px: memory allocation of connection entry failed\n", msg);
1195 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
1196 dev_put(src_dev);
1197 dev_put(dest_dev);
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301198 return -ENOMEM;
1199 }
1200
Parikshit Guneef1664c2022-03-24 14:15:42 +05301201 original_cm = (struct sfe_ipv6_connection_match *)kzalloc(sizeof(struct sfe_ipv6_connection_match), GFP_ATOMIC);
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301202 if (unlikely(!original_cm)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301203 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
1204 DEBUG_WARN("%px: memory allocation of connection match entry failed\n", msg);
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301205 kfree(c);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301206 dev_put(src_dev);
1207 dev_put(dest_dev);
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301208 return -ENOMEM;
1209 }
1210
Parikshit Guneef1664c2022-03-24 14:15:42 +05301211 reply_cm = (struct sfe_ipv6_connection_match *)kzalloc(sizeof(struct sfe_ipv6_connection_match), GFP_ATOMIC);
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301212 if (unlikely(!reply_cm)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301213 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
1214 DEBUG_WARN("%px: memory allocation of connection match entry failed\n", msg);
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301215 kfree(original_cm);
1216 kfree(c);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301217 dev_put(src_dev);
1218 dev_put(dest_dev);
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301219 return -ENOMEM;
1220 }
1221
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301222 this_cpu_inc(si->stats_pcpu->connection_create_requests64);
1223
Xiaoping Fan978b3772015-05-27 14:15:18 -07001224 spin_lock_bh(&si->lock);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001225
1226 /*
1227 * Check to see if there is already a flow that matches the rule we're
1228 * trying to create. If there is then we can't create a new one.
1229 */
Wayne Tanbb7f1782021-12-13 11:16:04 -08001230 old_c = sfe_ipv6_find_connection(si,
1231 tuple->protocol,
1232 (struct sfe_ipv6_addr *)tuple->flow_ip,
1233 tuple->flow_ident,
1234 (struct sfe_ipv6_addr *)tuple->return_ip,
1235 tuple->return_ident);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301236
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301237 if (old_c != NULL) {
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301238 this_cpu_inc(si->stats_pcpu->connection_create_collisions64);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001239
1240 /*
1241 * If we already have the flow then it's likely that this
1242 * request to create the connection rule contains more
1243 * up-to-date information. Check and update accordingly.
1244 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301245 sfe_ipv6_update_protocol_state(old_c, msg);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001246 spin_unlock_bh(&si->lock);
1247
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301248 kfree(reply_cm);
1249 kfree(original_cm);
1250 kfree(c);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301251 dev_put(src_dev);
1252 dev_put(dest_dev);
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301253
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301254 DEBUG_TRACE("connection already exists - p: %d\n"
Tian Yang45f39c82020-10-06 14:07:47 -07001255 " s: %s:%pxM:%pI6:%u, d: %s:%pxM:%pI6:%u\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301256 tuple->protocol,
1257 src_dev->name, msg->conn_rule.flow_mac, tuple->flow_ip, ntohs(tuple->flow_ident),
1258 dest_dev->name, msg->conn_rule.return_mac, tuple->return_ip, ntohs(tuple->return_ident));
Xiaoping Fan978b3772015-05-27 14:15:18 -07001259 return -EADDRINUSE;
1260 }
1261
1262 /*
Xiaoping Fan978b3772015-05-27 14:15:18 -07001263 * Fill in the "original" direction connection matching object.
1264 * Note that the transmit MAC address is "dest_mac_xlate" because
1265 * we always know both ends of a connection by their translated
1266 * addresses and not their public addresses.
1267 */
1268 original_cm->match_dev = src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301269 original_cm->match_protocol = tuple->protocol;
1270 original_cm->match_src_ip[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip;
Suruchi Suman66609a72022-01-20 02:34:25 +05301271 original_cm->match_src_port = netif_is_vxlan(src_dev) ? 0 : tuple->flow_ident;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301272 original_cm->match_dest_ip[0] = *(struct sfe_ipv6_addr *)tuple->return_ip;
1273 original_cm->match_dest_port = tuple->return_ident;
1274
1275 original_cm->xlate_src_ip[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip;
1276 original_cm->xlate_src_port = tuple->flow_ident;
1277 original_cm->xlate_dest_ip[0] = *(struct sfe_ipv6_addr *)tuple->return_ip;
1278 original_cm->xlate_dest_port = tuple->return_ident;
1279
Xiaoping Fan978b3772015-05-27 14:15:18 -07001280 original_cm->xmit_dev = dest_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301281
1282 original_cm->xmit_dev_mtu = msg->conn_rule.return_mtu;
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301283
Xiaoping Fan978b3772015-05-27 14:15:18 -07001284 original_cm->connection = c;
1285 original_cm->counter_match = reply_cm;
Suruchi Suman23a279d2021-11-16 15:13:09 +05301286
1287 /*
1288 * Valid in decap direction only
1289 */
1290 RCU_INIT_POINTER(original_cm->up, NULL);
1291
Ken Zhu37040ea2021-09-09 21:11:15 -07001292 if (msg->valid_flags & SFE_RULE_CREATE_MARK_VALID) {
1293 original_cm->mark = msg->mark_rule.flow_mark;
1294 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_MARK;
1295 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301296 if (msg->valid_flags & SFE_RULE_CREATE_QOS_VALID) {
1297 original_cm->priority = msg->qos_rule.flow_qos_tag;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001298 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_PRIORITY_REMARK;
1299 }
Wayne Tanbb7f1782021-12-13 11:16:04 -08001300
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301301 if (msg->valid_flags & SFE_RULE_CREATE_DSCP_MARKING_VALID) {
1302 original_cm->dscp = msg->dscp_rule.flow_dscp << SFE_IPV6_DSCP_SHIFT;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001303 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_DSCP_REMARK;
1304 }
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301305 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1306 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_BRIDGE_FLOW;
1307 }
Ken Zhu7e38d1a2021-11-30 17:31:46 -08001308 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_FLOW_TRANSMIT_FAST) {
1309 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_FAST_XMIT_DEV_ADMISSION;
1310 }
1311
Parikshit Guned31a8202022-01-05 22:15:04 +05301312 /*
1313 * Mark SAWF metadata if the sawf tag is valid.
1314 */
1315 original_cm->sawf_valid = false;
1316 flow_sawf_tag = SFE_GET_SAWF_TAG(msg->sawf_rule.flow_mark);
1317 if (likely(SFE_SAWF_TAG_IS_VALID(flow_sawf_tag))) {
1318 original_cm->mark = msg->sawf_rule.flow_mark;
1319 original_cm->sawf_valid = true;
1320 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_MARK;
1321 }
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301322
Wayne Tanbb7f1782021-12-13 11:16:04 -08001323 /*
1324 * Add VLAN rule to original_cm
1325 */
1326 if (msg->valid_flags & SFE_RULE_CREATE_VLAN_VALID) {
1327 struct sfe_vlan_rule *vlan_primary_rule = &msg->vlan_primary_rule;
1328 struct sfe_vlan_rule *vlan_secondary_rule = &msg->vlan_secondary_rule;
1329 sfe_ipv6_match_entry_set_vlan(original_cm,
1330 vlan_primary_rule->ingress_vlan_tag,
1331 vlan_primary_rule->egress_vlan_tag,
1332 vlan_secondary_rule->ingress_vlan_tag,
1333 vlan_secondary_rule->egress_vlan_tag);
1334
1335 if ((msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_RETURN_BOTTOM_INTERFACE) &&
1336 original_cm->egress_vlan_hdr_cnt > 0) {
1337 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_INSERT_EGRESS_VLAN_TAG;
1338 original_cm->l2_hdr_size += original_cm->egress_vlan_hdr_cnt * VLAN_HLEN;
1339 }
1340 }
1341
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301342 if ((IPPROTO_GRE == tuple->protocol) && !sfe_ipv6_is_local_ip(si, (uint8_t *)original_cm->match_dest_ip)) {
1343 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_PASSTHROUGH;
1344 }
1345
Xiaoping Fan978b3772015-05-27 14:15:18 -07001346#ifdef CONFIG_NF_FLOW_COOKIE
1347 original_cm->flow_cookie = 0;
1348#endif
Zhi Chen8748eb32015-06-18 12:58:48 -07001349#ifdef CONFIG_XFRM
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301350 if (msg->valid_flags & SFE_RULE_CREATE_DIRECTION_VALID) {
1351 original_cm->flow_accel = msg->direction_rule.flow_accel;
1352 } else {
1353 original_cm->flow_accel = 1;
1354 }
Zhi Chen8748eb32015-06-18 12:58:48 -07001355#endif
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301356 /*
1357 * If l2_features are disabled and flow uses l2 features such as macvlan/bridge/pppoe/vlan,
1358 * bottom interfaces are expected to be disabled in the flow rule and always top interfaces
1359 * are used. In such cases, do not use HW csum offload. csum offload is used only when we
1360 * are sending directly to the destination interface that supports it.
1361 */
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301362 if (likely(dest_dev->features & NETIF_F_HW_CSUM) && sfe_dev_has_hw_csum(dest_dev)) {
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301363 if ((msg->conn_rule.return_top_interface_num == msg->conn_rule.return_interface_num) ||
1364 (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_RETURN_BOTTOM_INTERFACE)) {
Ratheesh Kannoth48445532022-02-07 16:19:00 +05301365 /*
1366 * Dont enable CSUM offload
1367 */
1368#if 0
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301369 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_CSUM_OFFLOAD;
Ratheesh Kannoth48445532022-02-07 16:19:00 +05301370#endif
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301371 }
1372 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07001373
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +05301374 /*
1375 * Adding PPPoE parameters to original and reply entries based on the direction where
1376 * PPPoE header is valid in ECM rule.
1377 *
1378 * If PPPoE is valid in flow direction (from interface is PPPoE), then
1379 * original cm will have PPPoE at ingress (strip PPPoE header)
1380 * reply cm will have PPPoE at egress (add PPPoE header)
1381 *
1382 * If PPPoE is valid in return direction (to interface is PPPoE), then
1383 * original cm will have PPPoE at egress (add PPPoE header)
1384 * reply cm will have PPPoE at ingress (strip PPPoE header)
1385 */
1386 if (msg->valid_flags & SFE_RULE_CREATE_PPPOE_DECAP_VALID) {
1387 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_PPPOE_DECAP;
1388 original_cm->pppoe_session_id = msg->pppoe_rule.flow_pppoe_session_id;
1389 ether_addr_copy(original_cm->pppoe_remote_mac, msg->pppoe_rule.flow_pppoe_remote_mac);
1390
1391 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_PPPOE_ENCAP;
Wayne Tanbb7f1782021-12-13 11:16:04 -08001392 reply_cm->l2_hdr_size += SFE_PPPOE_SESSION_HEADER_SIZE;
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +05301393 reply_cm->pppoe_session_id = msg->pppoe_rule.flow_pppoe_session_id;
1394 ether_addr_copy(reply_cm->pppoe_remote_mac, msg->pppoe_rule.flow_pppoe_remote_mac);
1395 }
1396
1397 if (msg->valid_flags & SFE_RULE_CREATE_PPPOE_ENCAP_VALID) {
1398 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_PPPOE_ENCAP;
Wayne Tanbb7f1782021-12-13 11:16:04 -08001399 original_cm->l2_hdr_size += SFE_PPPOE_SESSION_HEADER_SIZE;
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +05301400 original_cm->pppoe_session_id = msg->pppoe_rule.return_pppoe_session_id;
1401 ether_addr_copy(original_cm->pppoe_remote_mac, msg->pppoe_rule.return_pppoe_remote_mac);
1402
1403 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_PPPOE_DECAP;
1404 reply_cm->pppoe_session_id = msg->pppoe_rule.return_pppoe_session_id;
1405 ether_addr_copy(reply_cm->pppoe_remote_mac, msg->pppoe_rule.return_pppoe_remote_mac);
1406 }
1407
Murat Sezgin9c538972022-05-17 13:33:17 -07001408 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_FLOW_SRC_INTERFACE_CHECK) {
Ratheesh Kannoth5dee3772022-01-18 11:27:14 +05301409 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_SRC_INTERFACE_CHECK;
1410 }
1411
Murat Sezgin9c538972022-05-17 13:33:17 -07001412 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_FLOW_SRC_INTERFACE_CHECK_NO_FLUSH) {
1413 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_SRC_INTERFACE_CHECK_NO_FLUSH;
1414 }
1415
Xiaoping Fan978b3772015-05-27 14:15:18 -07001416 /*
Ken Zhubbf49652021-09-12 15:33:09 -07001417 * For the non-arp interface, we don't write L2 HDR.
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +05301418 * Excluding PPPoE from this, since we are now supporting PPPoE encap/decap.
Xiaoping Fan978b3772015-05-27 14:15:18 -07001419 */
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +05301420 if (sfe_ipv6_xmit_eth_type_check(dest_dev, original_cm->flags)) {
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301421
1422 /*
1423 * Check whether the rule has configured a specific source MAC address to use.
1424 * This is needed when virtual L3 interfaces such as br-lan, macvlan, vlan are used during egress
1425 */
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301426 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1427 ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)msg->conn_rule.flow_mac);
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301428 } else {
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301429 if ((msg->valid_flags & SFE_RULE_CREATE_SRC_MAC_VALID) &&
1430 (msg->src_mac_rule.mac_valid_flags & SFE_SRC_MAC_RETURN_VALID)) {
1431 ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)msg->src_mac_rule.return_src_mac);
1432 } else {
1433 ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)dest_dev->dev_addr);
1434 }
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301435 }
1436 ether_addr_copy((u8 *)original_cm->xmit_dest_mac, (u8 *)msg->conn_rule.return_mac);
1437
Xiaoping Fan978b3772015-05-27 14:15:18 -07001438 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_WRITE_L2_HDR;
Wayne Tanbb7f1782021-12-13 11:16:04 -08001439 original_cm->l2_hdr_size += ETH_HLEN;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001440
1441 /*
1442 * If our dev writes Ethernet headers then we can write a really fast
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301443 * version
Xiaoping Fan978b3772015-05-27 14:15:18 -07001444 */
1445 if (dest_dev->header_ops) {
1446 if (dest_dev->header_ops->create == eth_header) {
1447 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR;
1448 }
1449 }
1450 }
1451
1452 /*
1453 * Fill in the "reply" direction connection matching object.
1454 */
1455 reply_cm->match_dev = dest_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301456 reply_cm->match_protocol = tuple->protocol;
1457 reply_cm->match_src_ip[0] = *(struct sfe_ipv6_addr *)tuple->return_ip;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301458 reply_cm->match_dest_ip[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip;
1459 reply_cm->match_dest_port = tuple->flow_ident;
1460 reply_cm->xlate_src_ip[0] = *(struct sfe_ipv6_addr *)tuple->return_ip;
1461 reply_cm->xlate_src_port = tuple->return_ident;
1462 reply_cm->xlate_dest_ip[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip;
1463 reply_cm->xlate_dest_port = tuple->flow_ident;
1464
Suruchi Suman23a279d2021-11-16 15:13:09 +05301465 /*
1466 * Keep source port as 0 for VxLAN tunnels.
1467 */
1468 if (netif_is_vxlan(src_dev) || netif_is_vxlan(dest_dev)) {
1469 reply_cm->match_src_port = 0;
1470 } else {
1471 reply_cm->match_src_port = tuple->return_ident;
1472 }
1473
Xiaoping Fan978b3772015-05-27 14:15:18 -07001474 reply_cm->xmit_dev = src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301475 reply_cm->xmit_dev_mtu = msg->conn_rule.flow_mtu;
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301476
Xiaoping Fan978b3772015-05-27 14:15:18 -07001477 reply_cm->connection = c;
1478 reply_cm->counter_match = original_cm;
Suruchi Suman23a279d2021-11-16 15:13:09 +05301479
Ken Zhu37040ea2021-09-09 21:11:15 -07001480 if (msg->valid_flags & SFE_RULE_CREATE_MARK_VALID) {
1481 reply_cm->mark = msg->mark_rule.return_mark;
1482 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_MARK;
1483 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301484 if (msg->valid_flags & SFE_RULE_CREATE_QOS_VALID) {
1485 reply_cm->priority = msg->qos_rule.return_qos_tag;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001486 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_PRIORITY_REMARK;
1487 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301488 if (msg->valid_flags & SFE_RULE_CREATE_DSCP_MARKING_VALID) {
1489 reply_cm->dscp = msg->dscp_rule.return_dscp << SFE_IPV6_DSCP_SHIFT;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001490 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_DSCP_REMARK;
1491 }
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301492 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1493 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_BRIDGE_FLOW;
1494 }
Ken Zhu7e38d1a2021-11-30 17:31:46 -08001495 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_RETURN_TRANSMIT_FAST) {
1496 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_FAST_XMIT_DEV_ADMISSION;
1497 }
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301498
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301499 if ((IPPROTO_GRE == tuple->protocol) && !sfe_ipv6_is_local_ip(si, (uint8_t *)reply_cm->match_dest_ip)) {
1500 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_PASSTHROUGH;
1501 }
1502
Suruchi Suman23a279d2021-11-16 15:13:09 +05301503 /*
Parikshit Guned31a8202022-01-05 22:15:04 +05301504 * Mark return SAWF metadata if the sawf tag is valid.
1505 */
1506 reply_cm->sawf_valid = false;
1507 return_sawf_tag = SFE_GET_SAWF_TAG(msg->sawf_rule.return_mark);
1508 if (likely(SFE_SAWF_TAG_IS_VALID(return_sawf_tag))) {
1509 reply_cm->mark = msg->sawf_rule.return_mark;
1510 reply_cm->sawf_valid = true;
1511 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_MARK;
1512 }
1513
1514 /*
Suruchi Suman23a279d2021-11-16 15:13:09 +05301515 * Setup UDP Socket if found to be valid for decap.
1516 */
1517 RCU_INIT_POINTER(reply_cm->up, NULL);
1518 net = dev_net(reply_cm->match_dev);
1519 src_if_idx = src_dev->ifindex;
1520
1521 rcu_read_lock();
1522
1523 /*
1524 * Look for the associated sock object.
1525 * __udp6_lib_lookup() holds a reference for this sock object,
1526 * which will be released in sfe_ipv6_flush_connection()
1527 */
1528#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
1529 sk = __udp6_lib_lookup(net, (const struct in6_addr *)reply_cm->match_dest_ip,
1530 reply_cm->match_dest_port, (const struct in6_addr *)reply_cm->xlate_src_ip,
1531 reply_cm->xlate_src_port, src_if_idx, &udp_table);
1532#else
1533 sk = __udp6_lib_lookup(net, (const struct in6_addr *)reply_cm->match_dest_ip,
1534 reply_cm->match_dest_port, (const struct in6_addr *)reply_cm->xlate_src_ip,
1535 reply_cm->xlate_src_port, src_if_idx, 0, &udp_table, NULL);
1536#endif
1537 rcu_read_unlock();
1538
1539 /*
1540 * We set the UDP sock pointer as valid only for decap direction.
1541 */
1542 if (sk && udp_sk(sk)->encap_type) {
1543#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
1544 if (!atomic_add_unless(&sk->sk_refcnt, 1, 0)) {
1545#else
1546 if (!refcount_inc_not_zero(&sk->sk_refcnt)) {
1547#endif
Tian Yang435afc42022-02-02 12:47:32 -08001548 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
Wayne Tanbb7f1782021-12-13 11:16:04 -08001549 spin_unlock_bh(&si->lock);
Suruchi Suman23a279d2021-11-16 15:13:09 +05301550 kfree(reply_cm);
1551 kfree(original_cm);
1552 kfree(c);
1553
1554 DEBUG_INFO("sfe: unable to take reference for socket p:%d\n", tuple->protocol);
1555 DEBUG_INFO("SK: connection - \n"
1556 " s: %s:%pI6(%pI6):%u(%u)\n"
1557 " d: %s:%pI6(%pI6):%u(%u)\n",
1558 reply_cm->match_dev->name, &reply_cm->match_src_ip, &reply_cm->xlate_src_ip,
1559 ntohs(reply_cm->match_src_port), ntohs(reply_cm->xlate_src_port),
1560 reply_cm->xmit_dev->name, &reply_cm->match_dest_ip, &reply_cm->xlate_dest_ip,
1561 ntohs(reply_cm->match_dest_port), ntohs(reply_cm->xlate_dest_port));
1562
1563 dev_put(src_dev);
1564 dev_put(dest_dev);
1565
1566 return -ESHUTDOWN;
1567 }
1568
1569 rcu_assign_pointer(reply_cm->up, udp_sk(sk));
1570 DEBUG_INFO("Sock lookup success with reply_cm direction(%p)\n", sk);
1571 DEBUG_INFO("SK: connection - \n"
1572 " s: %s:%pI6(%pI6):%u(%u)\n"
1573 " d: %s:%pI6(%pI6):%u(%u)\n",
1574 reply_cm->match_dev->name, &reply_cm->match_src_ip, &reply_cm->xlate_src_ip,
1575 ntohs(reply_cm->match_src_port), ntohs(reply_cm->xlate_src_port),
1576 reply_cm->xmit_dev->name, &reply_cm->match_dest_ip, &reply_cm->xlate_dest_ip,
1577 ntohs(reply_cm->match_dest_port), ntohs(reply_cm->xlate_dest_port));
1578 }
1579
Wayne Tanbb7f1782021-12-13 11:16:04 -08001580 /*
1581 * Add VLAN rule to reply_cm
1582 */
1583 if (msg->valid_flags & SFE_RULE_CREATE_VLAN_VALID) {
1584 struct sfe_vlan_rule *vlan_primary_rule = &msg->vlan_primary_rule;
1585 struct sfe_vlan_rule *vlan_secondary_rule = &msg->vlan_secondary_rule;
1586 sfe_ipv6_match_entry_set_vlan(reply_cm,
1587 vlan_primary_rule->egress_vlan_tag,
1588 vlan_primary_rule->ingress_vlan_tag,
1589 vlan_secondary_rule->egress_vlan_tag,
1590 vlan_secondary_rule->ingress_vlan_tag);
1591
1592 if ((msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_FLOW_BOTTOM_INTERFACE) &&
1593 reply_cm->egress_vlan_hdr_cnt > 0) {
1594 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_INSERT_EGRESS_VLAN_TAG;
1595 reply_cm->l2_hdr_size += reply_cm->egress_vlan_hdr_cnt * VLAN_HLEN;
1596 }
1597 }
1598
Xiaoping Fan978b3772015-05-27 14:15:18 -07001599#ifdef CONFIG_NF_FLOW_COOKIE
1600 reply_cm->flow_cookie = 0;
1601#endif
Zhi Chen8748eb32015-06-18 12:58:48 -07001602#ifdef CONFIG_XFRM
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301603 if (msg->valid_flags & SFE_RULE_CREATE_DIRECTION_VALID) {
1604 reply_cm->flow_accel = msg->direction_rule.return_accel;
1605 } else {
1606 reply_cm->flow_accel = 1;
1607 }
Zhi Chen8748eb32015-06-18 12:58:48 -07001608#endif
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301609
1610 /*
1611 * the inet6_protocol handler will be used only in decap path
1612 * for non passthrough case.
1613 */
1614 original_cm->proto = NULL;
1615 reply_cm->proto = NULL;
Tian Yang435afc42022-02-02 12:47:32 -08001616 original_cm->top_interface_dev = NULL;
1617 reply_cm->top_interface_dev = NULL;
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301618
1619#ifdef SFE_GRE_TUN_ENABLE
1620 if (!(reply_cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_PASSTHROUGH)) {
1621 rcu_read_lock();
1622 reply_cm->proto = rcu_dereference(inet6_protos[tuple->protocol]);
1623 rcu_read_unlock();
1624
1625 if (unlikely(!reply_cm->proto)) {
Tian Yang435afc42022-02-02 12:47:32 -08001626 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
1627 spin_unlock_bh(&si->lock);
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301628 kfree(reply_cm);
1629 kfree(original_cm);
1630 kfree(c);
1631 dev_put(src_dev);
1632 dev_put(dest_dev);
1633 DEBUG_WARN("sfe: GRE proto handler is not registered\n");
1634 return -EPERM;
1635 }
1636 }
1637#endif
1638
Xiaoping Fan978b3772015-05-27 14:15:18 -07001639 /*
Tian Yangafb03452022-01-13 18:53:13 -08001640 * Decapsulation path have proto set.
1641 * This is used to differentiate de/encap, and call protocol specific handler.
1642 */
1643 if (IPPROTO_IPIP == tuple->protocol) {
1644 original_cm->proto = NULL;
1645 rcu_read_lock();
1646 reply_cm->proto = rcu_dereference(inet6_protos[tuple->protocol]);
1647 rcu_read_unlock();
Tian Yang435afc42022-02-02 12:47:32 -08001648 reply_cm->top_interface_dev = dev_get_by_index(&init_net, msg->conn_rule.return_top_interface_num);
1649
1650 if (unlikely(!reply_cm->top_interface_dev)) {
1651 DEBUG_WARN("%px: Unable to find top_interface_dev corresponding to %d\n", msg,
1652 msg->conn_rule.return_top_interface_num);
1653 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
1654 spin_unlock_bh(&si->lock);
1655 kfree(reply_cm);
1656 kfree(original_cm);
1657 kfree(c);
1658 dev_put(src_dev);
1659 dev_put(dest_dev);
1660 return -EINVAL;
1661 }
Tian Yangafb03452022-01-13 18:53:13 -08001662 }
1663 /*
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301664 * If l2_features are disabled and flow uses l2 features such as macvlan/bridge/pppoe/vlan,
1665 * bottom interfaces are expected to be disabled in the flow rule and always top interfaces
1666 * are used. In such cases, do not use HW csum offload. csum offload is used only when we
1667 * are sending directly to the destination interface that supports it.
1668 */
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301669 if (likely(src_dev->features & NETIF_F_HW_CSUM) && sfe_dev_has_hw_csum(src_dev)) {
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301670 if ((msg->conn_rule.flow_top_interface_num == msg->conn_rule.flow_interface_num) ||
1671 (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_FLOW_BOTTOM_INTERFACE)) {
Ratheesh Kannoth48445532022-02-07 16:19:00 +05301672 /*
1673 * Dont enable CSUM offload
1674 */
1675#if 0
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301676 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_CSUM_OFFLOAD;
Ratheesh Kannoth48445532022-02-07 16:19:00 +05301677#endif
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301678 }
1679 }
1680
Murat Sezgin9c538972022-05-17 13:33:17 -07001681 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_RETURN_SRC_INTERFACE_CHECK) {
Ratheesh Kannoth5dee3772022-01-18 11:27:14 +05301682 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_SRC_INTERFACE_CHECK;
1683 }
1684
Murat Sezgin9c538972022-05-17 13:33:17 -07001685 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_RETURN_SRC_INTERFACE_CHECK_NO_FLUSH) {
1686 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_SRC_INTERFACE_CHECK_NO_FLUSH;
1687 }
1688
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301689 /*
Ken Zhubbf49652021-09-12 15:33:09 -07001690 * For the non-arp interface, we don't write L2 HDR.
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +05301691 * Excluding PPPoE from this, since we are now supporting PPPoE encap/decap.
Xiaoping Fan978b3772015-05-27 14:15:18 -07001692 */
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +05301693 if (sfe_ipv6_xmit_eth_type_check(src_dev, reply_cm->flags)) {
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301694
1695 /*
1696 * Check whether the rule has configured a specific source MAC address to use.
1697 * This is needed when virtual L3 interfaces such as br-lan, macvlan, vlan are used during egress
1698 */
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301699 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1700 ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)msg->conn_rule.return_mac);
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301701 } else {
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301702 if ((msg->valid_flags & SFE_RULE_CREATE_SRC_MAC_VALID) &&
1703 (msg->src_mac_rule.mac_valid_flags & SFE_SRC_MAC_FLOW_VALID)) {
1704 ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)msg->src_mac_rule.flow_src_mac);
1705 } else {
1706 ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)src_dev->dev_addr);
1707 }
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301708 }
1709
1710 ether_addr_copy((u8 *)reply_cm->xmit_dest_mac, (u8 *)msg->conn_rule.flow_mac);
1711
Xiaoping Fan978b3772015-05-27 14:15:18 -07001712 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_WRITE_L2_HDR;
Wayne Tanbb7f1782021-12-13 11:16:04 -08001713 reply_cm->l2_hdr_size += ETH_HLEN;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001714
1715 /*
1716 * If our dev writes Ethernet headers then we can write a really fast
1717 * version.
1718 */
1719 if (src_dev->header_ops) {
1720 if (src_dev->header_ops->create == eth_header) {
1721 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR;
1722 }
1723 }
1724 }
1725
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301726 /*
1727 * No support for NAT in ipv6
1728 */
Xiaoping Fan978b3772015-05-27 14:15:18 -07001729
Xiaoping Fan978b3772015-05-27 14:15:18 -07001730 /*
Xiaoping Fan978b3772015-05-27 14:15:18 -07001731 * Initialize the protocol-specific information that we track.
1732 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301733 switch (tuple->protocol) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07001734 case IPPROTO_TCP:
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301735 original_cm->protocol_state.tcp.win_scale = msg->tcp_rule.flow_window_scale;
1736 original_cm->protocol_state.tcp.max_win = msg->tcp_rule.flow_max_window ? msg->tcp_rule.flow_max_window : 1;
1737 original_cm->protocol_state.tcp.end = msg->tcp_rule.flow_end;
1738 original_cm->protocol_state.tcp.max_end = msg->tcp_rule.flow_max_end;
1739 reply_cm->protocol_state.tcp.win_scale = msg->tcp_rule.return_window_scale;
1740 reply_cm->protocol_state.tcp.max_win = msg->tcp_rule.return_max_window ? msg->tcp_rule.return_max_window : 1;
1741 reply_cm->protocol_state.tcp.end = msg->tcp_rule.return_end;
1742 reply_cm->protocol_state.tcp.max_end = msg->tcp_rule.return_max_end;
1743 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_NO_SEQ_CHECK) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07001744 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
1745 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
1746 }
1747 break;
1748 }
1749
Wayne Tanbb7f1782021-12-13 11:16:04 -08001750 /*
1751 * Fill in the ipv6_connection object.
1752 */
1753 c->protocol = tuple->protocol;
1754 c->src_ip[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip;
1755 c->src_ip_xlate[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip;
1756 c->src_port = tuple->flow_ident;
1757 c->src_port_xlate = tuple->flow_ident;
1758 c->original_dev = src_dev;
1759 c->original_match = original_cm;
1760
1761 c->dest_ip[0] = *(struct sfe_ipv6_addr *)tuple->return_ip;
1762 c->dest_ip_xlate[0] = *(struct sfe_ipv6_addr *)tuple->return_ip;
1763 c->dest_port = tuple->return_ident;
1764 c->dest_port_xlate = tuple->return_ident;
1765
1766 c->reply_dev = dest_dev;
1767 c->reply_match = reply_cm;
1768 c->debug_read_seq = 0;
1769 c->last_sync_jiffies = get_jiffies_64();
1770 c->removed = false;
1771
Xiaoping Fan978b3772015-05-27 14:15:18 -07001772 sfe_ipv6_connection_match_compute_translations(original_cm);
1773 sfe_ipv6_connection_match_compute_translations(reply_cm);
1774 sfe_ipv6_insert_connection(si, c);
1775
1776 spin_unlock_bh(&si->lock);
1777
1778 /*
1779 * We have everything we need!
1780 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301781 DEBUG_INFO("new connection - p: %d\n"
Tian Yang45f39c82020-10-06 14:07:47 -07001782 " s: %s:%pxM(%pxM):%pI6(%pI6):%u(%u)\n"
1783 " d: %s:%pxM(%pxM):%pI6(%pI6):%u(%u)\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301784 tuple->protocol,
1785 src_dev->name, msg->conn_rule.flow_mac, NULL,
1786 (void *)tuple->flow_ip, (void *)tuple->flow_ip, ntohs(tuple->flow_ident), ntohs(tuple->flow_ident),
1787 dest_dev->name, NULL, msg->conn_rule.return_mac,
1788 (void *)tuple->return_ip, (void *)tuple->return_ip, ntohs(tuple->return_ident), ntohs(tuple->return_ident));
Xiaoping Fan978b3772015-05-27 14:15:18 -07001789
1790 return 0;
1791}
1792
1793/*
1794 * sfe_ipv6_destroy_rule()
1795 * Destroy a forwarding rule.
1796 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301797void sfe_ipv6_destroy_rule(struct sfe_ipv6_rule_destroy_msg *msg)
Xiaoping Fan978b3772015-05-27 14:15:18 -07001798{
1799 struct sfe_ipv6 *si = &__si6;
1800 struct sfe_ipv6_connection *c;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301801 bool ret;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301802 struct sfe_ipv6_5tuple *tuple = &msg->tuple;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001803
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301804 this_cpu_inc(si->stats_pcpu->connection_destroy_requests64);
1805
Xiaoping Fan978b3772015-05-27 14:15:18 -07001806 spin_lock_bh(&si->lock);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001807
1808 /*
1809 * Check to see if we have a flow that matches the rule we're trying
1810 * to destroy. If there isn't then we can't destroy it.
1811 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301812 c = sfe_ipv6_find_connection(si, tuple->protocol, (struct sfe_ipv6_addr *)tuple->flow_ip, tuple->flow_ident,
1813 (struct sfe_ipv6_addr *)tuple->return_ip, tuple->return_ident);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001814 if (!c) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07001815 spin_unlock_bh(&si->lock);
1816
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301817 this_cpu_inc(si->stats_pcpu->connection_destroy_misses64);
1818
Xiaoping Fan978b3772015-05-27 14:15:18 -07001819 DEBUG_TRACE("connection does not exist - p: %d, s: %pI6:%u, d: %pI6:%u\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301820 tuple->protocol, tuple->flow_ip, ntohs(tuple->flow_ident),
1821 tuple->return_ip, ntohs(tuple->return_ident));
Xiaoping Fan978b3772015-05-27 14:15:18 -07001822 return;
1823 }
1824
1825 /*
1826 * Remove our connection details from the hash tables.
1827 */
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301828 ret = sfe_ipv6_remove_connection(si, c);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001829 spin_unlock_bh(&si->lock);
1830
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301831 if (ret) {
1832 sfe_ipv6_flush_connection(si, c, SFE_SYNC_REASON_DESTROY);
1833 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07001834
1835 DEBUG_INFO("connection destroyed - p: %d, s: %pI6:%u, d: %pI6:%u\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301836 tuple->protocol, tuple->flow_ip, ntohs(tuple->flow_ident),
1837 tuple->return_ip, ntohs(tuple->return_ident));
Xiaoping Fan978b3772015-05-27 14:15:18 -07001838}
1839
1840/*
Ken Zhu7a43d882022-01-04 10:51:44 -08001841 * sfe_ipv6_sync_invoke()
1842 * Schedule many sync stats.
1843 */
1844bool sfe_ipv6_sync_invoke(uint16_t index)
1845{
1846 struct sfe_ipv6 *si = &__si6;
1847 return schedule_delayed_work_on(si->work_cpu, &(si->sync_dwork), 0);
1848}
1849
1850/*
Xiaoping Fan978b3772015-05-27 14:15:18 -07001851 * sfe_ipv6_register_sync_rule_callback()
1852 * Register a callback for rule synchronization.
1853 */
1854void sfe_ipv6_register_sync_rule_callback(sfe_sync_rule_callback_t sync_rule_callback)
1855{
1856 struct sfe_ipv6 *si = &__si6;
1857
1858 spin_lock_bh(&si->lock);
1859 rcu_assign_pointer(si->sync_rule_callback, sync_rule_callback);
1860 spin_unlock_bh(&si->lock);
1861}
1862
1863/*
Ken Zhu7a43d882022-01-04 10:51:44 -08001864 * sfe_ipv6_register_sync_rule_callback()
1865 * Register a callback for rule synchronization.
1866 */
1867void sfe_ipv6_register_many_sync_callback(sfe_ipv6_many_sync_callback_t cb)
1868{
1869 struct sfe_ipv6 *si = &__si6;
1870
1871 spin_lock_bh(&si->lock);
1872 rcu_assign_pointer(si->many_sync_callback, cb);
1873 spin_unlock_bh(&si->lock);
1874}
1875
1876/*
Xiaoping Fan978b3772015-05-27 14:15:18 -07001877 * sfe_ipv6_get_debug_dev()
1878 */
1879static ssize_t sfe_ipv6_get_debug_dev(struct device *dev,
1880 struct device_attribute *attr,
1881 char *buf)
1882{
1883 struct sfe_ipv6 *si = &__si6;
1884 ssize_t count;
1885 int num;
1886
1887 spin_lock_bh(&si->lock);
1888 num = si->debug_dev;
1889 spin_unlock_bh(&si->lock);
1890
1891 count = snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", num);
1892 return count;
1893}
1894
1895/*
1896 * sfe_ipv6_destroy_all_rules_for_dev()
1897 * Destroy all connections that match a particular device.
1898 *
1899 * If we pass dev as NULL then this destroys all connections.
1900 */
1901void sfe_ipv6_destroy_all_rules_for_dev(struct net_device *dev)
1902{
1903 struct sfe_ipv6 *si = &__si6;
1904 struct sfe_ipv6_connection *c;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301905 bool ret;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001906
Xiaoping Fan34586472015-07-03 02:20:35 -07001907another_round:
Xiaoping Fan978b3772015-05-27 14:15:18 -07001908 spin_lock_bh(&si->lock);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001909
Xiaoping Fan34586472015-07-03 02:20:35 -07001910 for (c = si->all_connections_head; c; c = c->all_connections_next) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07001911 /*
Xiaoping Fan34586472015-07-03 02:20:35 -07001912 * Does this connection relate to the device we are destroying?
Xiaoping Fan978b3772015-05-27 14:15:18 -07001913 */
1914 if (!dev
1915 || (dev == c->original_dev)
1916 || (dev == c->reply_dev)) {
Xiaoping Fan34586472015-07-03 02:20:35 -07001917 break;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001918 }
Xiaoping Fan34586472015-07-03 02:20:35 -07001919 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07001920
Xiaoping Fan34586472015-07-03 02:20:35 -07001921 if (c) {
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301922 ret = sfe_ipv6_remove_connection(si, c);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001923 }
1924
1925 spin_unlock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07001926
1927 if (c) {
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301928 if (ret) {
1929 sfe_ipv6_flush_connection(si, c, SFE_SYNC_REASON_DESTROY);
1930 }
Xiaoping Fan34586472015-07-03 02:20:35 -07001931 goto another_round;
1932 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07001933}
1934
1935/*
1936 * sfe_ipv6_periodic_sync()
1937 */
Ken Zhu137722d2021-09-23 17:57:36 -07001938static void sfe_ipv6_periodic_sync(struct work_struct *work)
Xiaoping Fan978b3772015-05-27 14:15:18 -07001939{
Ken Zhu137722d2021-09-23 17:57:36 -07001940 struct sfe_ipv6 *si = container_of((struct delayed_work *)work, struct sfe_ipv6, sync_dwork);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07001941 u64 now_jiffies;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001942 int quota;
Ken Zhu7a43d882022-01-04 10:51:44 -08001943 sfe_ipv6_many_sync_callback_t sync_rule_callback;
Ken Zhu32b95392021-09-03 13:52:04 -07001944 struct sfe_ipv6_connection *c;
Ken Zhu7a43d882022-01-04 10:51:44 -08001945 struct sfe_ipv6_conn_sync *conn_sync;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001946
1947 now_jiffies = get_jiffies_64();
1948
1949 rcu_read_lock();
Ken Zhu7a43d882022-01-04 10:51:44 -08001950 sync_rule_callback = rcu_dereference(si->many_sync_callback);
1951 rcu_read_unlock();
Xiaoping Fan978b3772015-05-27 14:15:18 -07001952 if (!sync_rule_callback) {
Ken Zhu7a43d882022-01-04 10:51:44 -08001953 return;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001954 }
1955
1956 spin_lock_bh(&si->lock);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001957
1958 /*
Ken Zhu32b95392021-09-03 13:52:04 -07001959 * If we have reached the end of the connection list, walk from
1960 * the connection head.
1961 */
1962 c = si->wc_next;
1963 if (unlikely(!c)) {
1964 c = si->all_connections_head;
1965 }
Ken Zhu7a43d882022-01-04 10:51:44 -08001966
Ken Zhu32b95392021-09-03 13:52:04 -07001967 /*
Ken Zhu7a43d882022-01-04 10:51:44 -08001968 * Get the max number of connections to be put in this sync msg.
Xiaoping Fan978b3772015-05-27 14:15:18 -07001969 */
Ken Zhu7a43d882022-01-04 10:51:44 -08001970 quota = sfe_ipv6_sync_max_number;
1971 conn_sync = sfe_ipv6_sync_many_msg->msg.conn_stats_many.conn_sync;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001972
1973 /*
Ken Zhu32b95392021-09-03 13:52:04 -07001974 * Walk the "all connection" list and sync the connection state.
Xiaoping Fan978b3772015-05-27 14:15:18 -07001975 */
Ken Zhu32b95392021-09-03 13:52:04 -07001976 while (likely(c && quota)) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07001977 struct sfe_ipv6_connection_match *cm;
1978 struct sfe_ipv6_connection_match *counter_cm;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001979 struct sfe_connection_sync sis;
1980
Ken Zhu32b95392021-09-03 13:52:04 -07001981 cm = c->original_match;
1982 counter_cm = c->reply_match;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001983
1984 /*
Ken Zhu32b95392021-09-03 13:52:04 -07001985 * Didn't receive packets in the origial direction or reply
1986 * direction, move to the next connection.
Xiaoping Fan978b3772015-05-27 14:15:18 -07001987 */
Ken Zhu32b95392021-09-03 13:52:04 -07001988 if (!atomic_read(&cm->rx_packet_count) && !atomic_read(&counter_cm->rx_packet_count)) {
1989 c = c->all_connections_next;
1990 continue;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001991 }
1992
Xiaoping Fan978b3772015-05-27 14:15:18 -07001993 /*
1994 * Sync the connection state.
1995 */
Xiaoping Fan99cb4c12015-08-21 19:07:32 -07001996 sfe_ipv6_gen_sync_connection(si, c, &sis, SFE_SYNC_REASON_STATS, now_jiffies);
Ken Zhu7a43d882022-01-04 10:51:44 -08001997 sfe_ipv6_stats_convert(conn_sync, &sis);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001998
Ken Zhu7a43d882022-01-04 10:51:44 -08001999 quota--;
2000 conn_sync++;
2001 c = c->all_connections_next;
Xiaoping Fan978b3772015-05-27 14:15:18 -07002002 }
2003
Ken Zhu32b95392021-09-03 13:52:04 -07002004 /*
2005 * At the end of loop, put wc_next to the connection we left
2006 */
2007 si->wc_next = c;
Xiaoping Fan978b3772015-05-27 14:15:18 -07002008 spin_unlock_bh(&si->lock);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002009
Ken Zhu7a43d882022-01-04 10:51:44 -08002010 if (c == NULL) {
2011 DEBUG_INFO("Synced all connections\n");
2012 sfe_ipv6_sync_many_msg->msg.conn_stats_many.next = 0;
2013 } else {
2014 DEBUG_INFO("Some connections left\n");
2015 sfe_ipv6_sync_many_msg->msg.conn_stats_many.next = sfe_ipv6_sync_max_number - quota;
2016 }
2017 DEBUG_INFO("Synced [%d] connections\n", (sfe_ipv6_sync_max_number - quota));
2018
2019 sfe_ipv6_sync_many_msg->msg.conn_stats_many.count = sfe_ipv6_sync_max_number - quota;
2020 sfe_ipv6_sync_many_msg->cm.response = SFE_CMN_RESPONSE_ACK;
2021
2022 sync_rule_callback(sfe_ipv6_sync_many_msg);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002023}
2024
2025/*
2026 * sfe_ipv6_debug_dev_read_start()
2027 * Generate part of the XML output.
2028 */
2029static bool sfe_ipv6_debug_dev_read_start(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
2030 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
2031{
2032 int bytes_read;
2033
Xiaoping Fan34586472015-07-03 02:20:35 -07002034 si->debug_read_seq++;
2035
Xiaoping Fan978b3772015-05-27 14:15:18 -07002036 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "<sfe_ipv6>\n");
2037 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2038 return false;
2039 }
2040
2041 *length -= bytes_read;
2042 *total_read += bytes_read;
2043
2044 ws->state++;
2045 return true;
2046}
2047
2048/*
2049 * sfe_ipv6_debug_dev_read_connections_start()
2050 * Generate part of the XML output.
2051 */
2052static bool sfe_ipv6_debug_dev_read_connections_start(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
2053 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
2054{
2055 int bytes_read;
2056
2057 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<connections>\n");
2058 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2059 return false;
2060 }
2061
2062 *length -= bytes_read;
2063 *total_read += bytes_read;
2064
2065 ws->state++;
2066 return true;
2067}
2068
2069/*
2070 * sfe_ipv6_debug_dev_read_connections_connection()
2071 * Generate part of the XML output.
2072 */
2073static bool sfe_ipv6_debug_dev_read_connections_connection(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
2074 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
2075{
2076 struct sfe_ipv6_connection *c;
Xiaoping Fan978b3772015-05-27 14:15:18 -07002077 struct sfe_ipv6_connection_match *original_cm;
2078 struct sfe_ipv6_connection_match *reply_cm;
2079 int bytes_read;
2080 int protocol;
2081 struct net_device *src_dev;
2082 struct sfe_ipv6_addr src_ip;
2083 struct sfe_ipv6_addr src_ip_xlate;
2084 __be16 src_port;
2085 __be16 src_port_xlate;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07002086 u64 src_rx_packets;
2087 u64 src_rx_bytes;
Xiaoping Fan978b3772015-05-27 14:15:18 -07002088 struct net_device *dest_dev;
2089 struct sfe_ipv6_addr dest_ip;
2090 struct sfe_ipv6_addr dest_ip_xlate;
2091 __be16 dest_port;
2092 __be16 dest_port_xlate;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07002093 u64 dest_rx_packets;
2094 u64 dest_rx_bytes;
2095 u64 last_sync_jiffies;
Ken Zhu37040ea2021-09-09 21:11:15 -07002096 u32 src_mark, dest_mark, src_priority, dest_priority, src_dscp, dest_dscp;
Parikshit Guned31a8202022-01-05 22:15:04 +05302097 bool original_cm_sawf_valid, reply_cm_sawf_valid;
2098 u32 flow_service_class, return_service_class;
2099 u32 flow_msduq, return_msduq;
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05302100 u32 packet, byte, original_cm_flags;
2101 u16 pppoe_session_id;
2102 u8 pppoe_remote_mac[ETH_ALEN];
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002103 u32 original_fast_xmit, reply_fast_xmit;
Xiaoping Fan978b3772015-05-27 14:15:18 -07002104#ifdef CONFIG_NF_FLOW_COOKIE
2105 int src_flow_cookie, dst_flow_cookie;
2106#endif
2107
2108 spin_lock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07002109
2110 for (c = si->all_connections_head; c; c = c->all_connections_next) {
2111 if (c->debug_read_seq < si->debug_read_seq) {
2112 c->debug_read_seq = si->debug_read_seq;
2113 break;
2114 }
2115 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07002116
2117 /*
Xiaoping Fan34586472015-07-03 02:20:35 -07002118 * If there were no connections then move to the next state.
Xiaoping Fan978b3772015-05-27 14:15:18 -07002119 */
2120 if (!c) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07002121 spin_unlock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07002122 ws->state++;
Xiaoping Fan978b3772015-05-27 14:15:18 -07002123 return true;
2124 }
2125
2126 original_cm = c->original_match;
2127 reply_cm = c->reply_match;
2128
2129 protocol = c->protocol;
2130 src_dev = c->original_dev;
2131 src_ip = c->src_ip[0];
2132 src_ip_xlate = c->src_ip_xlate[0];
2133 src_port = c->src_port;
2134 src_port_xlate = c->src_port_xlate;
Xiaoping Fane1963d42015-08-25 17:06:19 -07002135 src_priority = original_cm->priority;
2136 src_dscp = original_cm->dscp >> SFE_IPV6_DSCP_SHIFT;
Xiaoping Fan978b3772015-05-27 14:15:18 -07002137
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05302138 sfe_ipv6_connection_match_update_summary_stats(original_cm, &packet, &byte);
2139 sfe_ipv6_connection_match_update_summary_stats(reply_cm, &packet, &byte);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002140
2141 src_rx_packets = original_cm->rx_packet_count64;
2142 src_rx_bytes = original_cm->rx_byte_count64;
Ken Zhu37040ea2021-09-09 21:11:15 -07002143 src_mark = original_cm->mark;
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002144 original_fast_xmit = original_cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_FAST_XMIT;
Xiaoping Fan978b3772015-05-27 14:15:18 -07002145 dest_dev = c->reply_dev;
2146 dest_ip = c->dest_ip[0];
2147 dest_ip_xlate = c->dest_ip_xlate[0];
2148 dest_port = c->dest_port;
2149 dest_port_xlate = c->dest_port_xlate;
Xiaoping Fane1963d42015-08-25 17:06:19 -07002150 dest_priority = reply_cm->priority;
2151 dest_dscp = reply_cm->dscp >> SFE_IPV6_DSCP_SHIFT;
Xiaoping Fan978b3772015-05-27 14:15:18 -07002152 dest_rx_packets = reply_cm->rx_packet_count64;
2153 dest_rx_bytes = reply_cm->rx_byte_count64;
2154 last_sync_jiffies = get_jiffies_64() - c->last_sync_jiffies;
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05302155 original_cm_flags = original_cm->flags;
2156 pppoe_session_id = original_cm->pppoe_session_id;
2157 ether_addr_copy(pppoe_remote_mac, original_cm->pppoe_remote_mac);
Ken Zhu37040ea2021-09-09 21:11:15 -07002158 dest_mark = reply_cm->mark;
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002159 reply_fast_xmit = reply_cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_FAST_XMIT;
Parikshit Guned31a8202022-01-05 22:15:04 +05302160 original_cm_sawf_valid = original_cm->sawf_valid;
2161 reply_cm_sawf_valid = reply_cm->sawf_valid;
2162 flow_service_class = SFE_GET_SAWF_SERVICE_CLASS(original_cm->mark);
2163 flow_msduq = SFE_GET_SAWF_MSDUQ(original_cm->mark);
2164 return_service_class = SFE_GET_SAWF_SERVICE_CLASS(reply_cm->mark);
2165 return_msduq = SFE_GET_SAWF_MSDUQ(reply_cm->mark);
2166
Xiaoping Fan978b3772015-05-27 14:15:18 -07002167#ifdef CONFIG_NF_FLOW_COOKIE
2168 src_flow_cookie = original_cm->flow_cookie;
2169 dst_flow_cookie = reply_cm->flow_cookie;
2170#endif
2171 spin_unlock_bh(&si->lock);
2172
2173 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t\t<connection "
2174 "protocol=\"%u\" "
2175 "src_dev=\"%s\" "
2176 "src_ip=\"%pI6\" src_ip_xlate=\"%pI6\" "
2177 "src_port=\"%u\" src_port_xlate=\"%u\" "
Xiaoping Fane1963d42015-08-25 17:06:19 -07002178 "src_priority=\"%u\" src_dscp=\"%u\" "
Xiaoping Fan978b3772015-05-27 14:15:18 -07002179 "src_rx_pkts=\"%llu\" src_rx_bytes=\"%llu\" "
Ken Zhu37040ea2021-09-09 21:11:15 -07002180 "src_mark=\"%08x\" "
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002181 "src_fast_xmit=\"%s\" "
Xiaoping Fan978b3772015-05-27 14:15:18 -07002182 "dest_dev=\"%s\" "
2183 "dest_ip=\"%pI6\" dest_ip_xlate=\"%pI6\" "
2184 "dest_port=\"%u\" dest_port_xlate=\"%u\" "
Xiaoping Fane1963d42015-08-25 17:06:19 -07002185 "dest_priority=\"%u\" dest_dscp=\"%u\" "
Xiaoping Fan978b3772015-05-27 14:15:18 -07002186 "dest_rx_pkts=\"%llu\" dest_rx_bytes=\"%llu\" "
Ken Zhu37040ea2021-09-09 21:11:15 -07002187 "dest_mark=\"%08x\" "
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002188 "reply_fast_xmit=\"%s\" "
Xiaoping Fan978b3772015-05-27 14:15:18 -07002189#ifdef CONFIG_NF_FLOW_COOKIE
2190 "src_flow_cookie=\"%d\" dst_flow_cookie=\"%d\" "
2191#endif
Ken Zhu37040ea2021-09-09 21:11:15 -07002192 "last_sync=\"%llu\" ",
Xiaoping Fan978b3772015-05-27 14:15:18 -07002193 protocol,
2194 src_dev->name,
2195 &src_ip, &src_ip_xlate,
2196 ntohs(src_port), ntohs(src_port_xlate),
Xiaoping Fane1963d42015-08-25 17:06:19 -07002197 src_priority, src_dscp,
Xiaoping Fan978b3772015-05-27 14:15:18 -07002198 src_rx_packets, src_rx_bytes,
Ken Zhu37040ea2021-09-09 21:11:15 -07002199 src_mark,
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002200 original_fast_xmit ? "Yes" : "No",
Xiaoping Fan978b3772015-05-27 14:15:18 -07002201 dest_dev->name,
2202 &dest_ip, &dest_ip_xlate,
2203 ntohs(dest_port), ntohs(dest_port_xlate),
Xiaoping Fane1963d42015-08-25 17:06:19 -07002204 dest_priority, dest_dscp,
Xiaoping Fan978b3772015-05-27 14:15:18 -07002205 dest_rx_packets, dest_rx_bytes,
Ken Zhu37040ea2021-09-09 21:11:15 -07002206 dest_mark,
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002207 reply_fast_xmit ? "Yes" : "No",
Xiaoping Fan978b3772015-05-27 14:15:18 -07002208#ifdef CONFIG_NF_FLOW_COOKIE
2209 src_flow_cookie, dst_flow_cookie,
2210#endif
Ken Zhu37040ea2021-09-09 21:11:15 -07002211 last_sync_jiffies);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002212
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05302213 if (original_cm_flags &= (SFE_IPV6_CONNECTION_MATCH_FLAG_PPPOE_DECAP | SFE_IPV6_CONNECTION_MATCH_FLAG_PPPOE_ENCAP)) {
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +05302214 bytes_read += snprintf(msg + bytes_read, CHAR_DEV_MSG_SIZE, "pppoe_session_id=\"%u\" pppoe_server_MAC=\"%pM\" ",
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05302215 pppoe_session_id, pppoe_remote_mac);
2216 }
2217
Parikshit Guned31a8202022-01-05 22:15:04 +05302218 if (original_cm_sawf_valid) {
Parikshit Gunefdd98652022-03-14 17:33:01 +05302219 bytes_read += snprintf(msg + bytes_read, CHAR_DEV_MSG_SIZE, "flow_service_class=\"%d\" flow_msduq= \"0x%x\" ",
Parikshit Guned31a8202022-01-05 22:15:04 +05302220 flow_service_class, flow_msduq);
2221 }
2222
2223 if (reply_cm_sawf_valid) {
Parikshit Gunefdd98652022-03-14 17:33:01 +05302224 bytes_read += snprintf(msg + bytes_read, CHAR_DEV_MSG_SIZE, "return_service_class=\"%d\" return_msduq= \"0x%x\" ",
Parikshit Guned31a8202022-01-05 22:15:04 +05302225 return_service_class, return_msduq);
2226 }
2227
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05302228 bytes_read += snprintf(msg + bytes_read, CHAR_DEV_MSG_SIZE, ")/>\n");
2229
Xiaoping Fan978b3772015-05-27 14:15:18 -07002230 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2231 return false;
2232 }
2233
2234 *length -= bytes_read;
2235 *total_read += bytes_read;
2236
Xiaoping Fan978b3772015-05-27 14:15:18 -07002237 return true;
2238}
2239
2240/*
2241 * sfe_ipv6_debug_dev_read_connections_end()
2242 * Generate part of the XML output.
2243 */
2244static bool sfe_ipv6_debug_dev_read_connections_end(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
2245 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
2246{
2247 int bytes_read;
2248
2249 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t</connections>\n");
2250 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2251 return false;
2252 }
2253
2254 *length -= bytes_read;
2255 *total_read += bytes_read;
2256
2257 ws->state++;
2258 return true;
2259}
2260
2261/*
2262 * sfe_ipv6_debug_dev_read_exceptions_start()
2263 * Generate part of the XML output.
2264 */
2265static bool sfe_ipv6_debug_dev_read_exceptions_start(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
2266 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
2267{
2268 int bytes_read;
2269
2270 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<exceptions>\n");
2271 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2272 return false;
2273 }
2274
2275 *length -= bytes_read;
2276 *total_read += bytes_read;
2277
2278 ws->state++;
2279 return true;
2280}
2281
2282/*
2283 * sfe_ipv6_debug_dev_read_exceptions_exception()
2284 * Generate part of the XML output.
2285 */
2286static bool sfe_ipv6_debug_dev_read_exceptions_exception(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
2287 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
2288{
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302289 int i;
2290 u64 val = 0;
Xiaoping Fan978b3772015-05-27 14:15:18 -07002291
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302292 for_each_possible_cpu(i) {
2293 const struct sfe_ipv6_stats *s = per_cpu_ptr(si->stats_pcpu, i);
2294 val += s->exception_events64[ws->iter_exception];
2295 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07002296
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302297 if (val) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07002298 int bytes_read;
2299
2300 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE,
2301 "\t\t<exception name=\"%s\" count=\"%llu\" />\n",
2302 sfe_ipv6_exception_events_string[ws->iter_exception],
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302303 val);
2304
Xiaoping Fan978b3772015-05-27 14:15:18 -07002305 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2306 return false;
2307 }
2308
2309 *length -= bytes_read;
2310 *total_read += bytes_read;
2311 }
2312
2313 ws->iter_exception++;
2314 if (ws->iter_exception >= SFE_IPV6_EXCEPTION_EVENT_LAST) {
2315 ws->iter_exception = 0;
2316 ws->state++;
2317 }
2318
2319 return true;
2320}
2321
2322/*
2323 * sfe_ipv6_debug_dev_read_exceptions_end()
2324 * Generate part of the XML output.
2325 */
2326static bool sfe_ipv6_debug_dev_read_exceptions_end(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
2327 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
2328{
2329 int bytes_read;
2330
2331 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t</exceptions>\n");
2332 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2333 return false;
2334 }
2335
2336 *length -= bytes_read;
2337 *total_read += bytes_read;
2338
2339 ws->state++;
2340 return true;
2341}
2342
2343/*
2344 * sfe_ipv6_debug_dev_read_stats()
2345 * Generate part of the XML output.
2346 */
2347static bool sfe_ipv6_debug_dev_read_stats(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
2348 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
2349{
2350 int bytes_read;
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302351 struct sfe_ipv6_stats stats;
2352 unsigned int num_conn;
2353
2354 sfe_ipv6_update_summary_stats(si, &stats);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002355
2356 spin_lock_bh(&si->lock);
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302357 num_conn = si->num_connections;
Xiaoping Fan978b3772015-05-27 14:15:18 -07002358 spin_unlock_bh(&si->lock);
2359
2360 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<stats "
2361 "num_connections=\"%u\" "
Suruchi Suman23a279d2021-11-16 15:13:09 +05302362 "pkts_dropped=\"%llu\" "
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002363 "pkts_fast_xmited=\"%llu\" "
Xiaoping Fan978b3772015-05-27 14:15:18 -07002364 "pkts_forwarded=\"%llu\" pkts_not_forwarded=\"%llu\" "
2365 "create_requests=\"%llu\" create_collisions=\"%llu\" "
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05302366 "create_failures=\"%llu\" "
Xiaoping Fan978b3772015-05-27 14:15:18 -07002367 "destroy_requests=\"%llu\" destroy_misses=\"%llu\" "
2368 "flushes=\"%llu\" "
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +05302369 "hash_hits=\"%llu\" hash_reorders=\"%llu\" "
2370 "pppoe_encap_pkts_fwded=\"%llu\" "
Guduri Prathyusha034d6352022-01-12 16:49:04 +05302371 "pppoe_decap_pkts_fwded=\"%llu\" "
2372 "pppoe_bridge_pkts_fwded=\"%llu\" />\n",
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302373
2374 num_conn,
Suruchi Suman23a279d2021-11-16 15:13:09 +05302375 stats.packets_dropped64,
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002376 stats.packets_fast_xmited64,
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302377 stats.packets_forwarded64,
2378 stats.packets_not_forwarded64,
2379 stats.connection_create_requests64,
2380 stats.connection_create_collisions64,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05302381 stats.connection_create_failures64,
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302382 stats.connection_destroy_requests64,
2383 stats.connection_destroy_misses64,
2384 stats.connection_flushes64,
2385 stats.connection_match_hash_hits64,
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +05302386 stats.connection_match_hash_reorders64,
2387 stats.pppoe_encap_packets_forwarded64,
Guduri Prathyusha034d6352022-01-12 16:49:04 +05302388 stats.pppoe_decap_packets_forwarded64,
2389 stats.pppoe_bridge_packets_forwarded64);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002390 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2391 return false;
2392 }
2393
2394 *length -= bytes_read;
2395 *total_read += bytes_read;
2396
2397 ws->state++;
2398 return true;
2399}
2400
2401/*
2402 * sfe_ipv6_debug_dev_read_end()
2403 * Generate part of the XML output.
2404 */
2405static bool sfe_ipv6_debug_dev_read_end(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
2406 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
2407{
2408 int bytes_read;
2409
2410 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "</sfe_ipv6>\n");
2411 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2412 return false;
2413 }
2414
2415 *length -= bytes_read;
2416 *total_read += bytes_read;
2417
2418 ws->state++;
2419 return true;
2420}
2421
2422/*
2423 * Array of write functions that write various XML elements that correspond to
2424 * our XML output state machine.
2425 */
2426static sfe_ipv6_debug_xml_write_method_t sfe_ipv6_debug_xml_write_methods[SFE_IPV6_DEBUG_XML_STATE_DONE] = {
2427 sfe_ipv6_debug_dev_read_start,
2428 sfe_ipv6_debug_dev_read_connections_start,
2429 sfe_ipv6_debug_dev_read_connections_connection,
2430 sfe_ipv6_debug_dev_read_connections_end,
2431 sfe_ipv6_debug_dev_read_exceptions_start,
2432 sfe_ipv6_debug_dev_read_exceptions_exception,
2433 sfe_ipv6_debug_dev_read_exceptions_end,
2434 sfe_ipv6_debug_dev_read_stats,
2435 sfe_ipv6_debug_dev_read_end,
2436};
2437
2438/*
2439 * sfe_ipv6_debug_dev_read()
2440 * Send info to userspace upon read request from user
2441 */
2442static ssize_t sfe_ipv6_debug_dev_read(struct file *filp, char *buffer, size_t length, loff_t *offset)
2443{
2444 char msg[CHAR_DEV_MSG_SIZE];
2445 int total_read = 0;
2446 struct sfe_ipv6_debug_xml_write_state *ws;
2447 struct sfe_ipv6 *si = &__si6;
2448
2449 ws = (struct sfe_ipv6_debug_xml_write_state *)filp->private_data;
2450 while ((ws->state != SFE_IPV6_DEBUG_XML_STATE_DONE) && (length > CHAR_DEV_MSG_SIZE)) {
2451 if ((sfe_ipv6_debug_xml_write_methods[ws->state])(si, buffer, msg, &length, &total_read, ws)) {
2452 continue;
2453 }
2454 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07002455 return total_read;
2456}
2457
2458/*
Xiaoping Fan978b3772015-05-27 14:15:18 -07002459 * sfe_ipv6_debug_dev_open()
2460 */
2461static int sfe_ipv6_debug_dev_open(struct inode *inode, struct file *file)
2462{
2463 struct sfe_ipv6_debug_xml_write_state *ws;
2464
2465 ws = (struct sfe_ipv6_debug_xml_write_state *)file->private_data;
2466 if (ws) {
2467 return 0;
2468 }
2469
2470 ws = kzalloc(sizeof(struct sfe_ipv6_debug_xml_write_state), GFP_KERNEL);
2471 if (!ws) {
2472 return -ENOMEM;
2473 }
2474
2475 ws->state = SFE_IPV6_DEBUG_XML_STATE_START;
2476 file->private_data = ws;
2477
2478 return 0;
2479}
2480
2481/*
2482 * sfe_ipv6_debug_dev_release()
2483 */
2484static int sfe_ipv6_debug_dev_release(struct inode *inode, struct file *file)
2485{
2486 struct sfe_ipv6_debug_xml_write_state *ws;
Xiaoping Fan978b3772015-05-27 14:15:18 -07002487
2488 ws = (struct sfe_ipv6_debug_xml_write_state *)file->private_data;
Xiaoping Fan34586472015-07-03 02:20:35 -07002489 if (ws) {
2490 /*
2491 * We've finished with our output so free the write state.
2492 */
2493 kfree(ws);
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05302494 file->private_data = NULL;
Xiaoping Fan978b3772015-05-27 14:15:18 -07002495 }
2496
Xiaoping Fan978b3772015-05-27 14:15:18 -07002497 return 0;
2498}
2499
2500/*
2501 * File operations used in the debug char device
2502 */
2503static struct file_operations sfe_ipv6_debug_dev_fops = {
2504 .read = sfe_ipv6_debug_dev_read,
Xiaoping Fan978b3772015-05-27 14:15:18 -07002505 .open = sfe_ipv6_debug_dev_open,
2506 .release = sfe_ipv6_debug_dev_release
2507};
2508
2509#ifdef CONFIG_NF_FLOW_COOKIE
2510/*
2511 * sfe_ipv6_register_flow_cookie_cb
2512 * register a function in SFE to let SFE use this function to configure flow cookie for a flow
2513 *
2514 * Hardware driver which support flow cookie should register a callback function in SFE. Then SFE
2515 * can use this function to configure flow cookie for a flow.
2516 * return: 0, success; !=0, fail
2517 */
2518int sfe_ipv6_register_flow_cookie_cb(sfe_ipv6_flow_cookie_set_func_t cb)
2519{
2520 struct sfe_ipv6 *si = &__si6;
2521
2522 BUG_ON(!cb);
2523
2524 if (si->flow_cookie_set_func) {
2525 return -1;
2526 }
2527
2528 rcu_assign_pointer(si->flow_cookie_set_func, cb);
2529 return 0;
2530}
2531
2532/*
2533 * sfe_ipv6_unregister_flow_cookie_cb
2534 * unregister function which is used to configure flow cookie for a flow
2535 *
2536 * return: 0, success; !=0, fail
2537 */
2538int sfe_ipv6_unregister_flow_cookie_cb(sfe_ipv6_flow_cookie_set_func_t cb)
2539{
2540 struct sfe_ipv6 *si = &__si6;
2541
2542 RCU_INIT_POINTER(si->flow_cookie_set_func, NULL);
2543 return 0;
2544}
Xiaoping Fan640faf42015-08-28 15:50:55 -07002545
2546/*
2547 * sfe_ipv6_get_flow_cookie()
2548 */
2549static ssize_t sfe_ipv6_get_flow_cookie(struct device *dev,
2550 struct device_attribute *attr,
2551 char *buf)
2552{
2553 struct sfe_ipv6 *si = &__si6;
Xiaoping Fan01c67cc2015-11-09 11:31:57 -08002554 return snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", si->flow_cookie_enable);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002555}
2556
2557/*
2558 * sfe_ipv6_set_flow_cookie()
2559 */
2560static ssize_t sfe_ipv6_set_flow_cookie(struct device *dev,
2561 struct device_attribute *attr,
2562 const char *buf, size_t size)
2563{
2564 struct sfe_ipv6 *si = &__si6;
Ken Zhu137722d2021-09-23 17:57:36 -07002565 si->flow_cookie_enable = strict_strtol(buf, NULL, 0);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002566
2567 return size;
2568}
2569
2570/*
2571 * sysfs attributes.
2572 */
2573static const struct device_attribute sfe_ipv6_flow_cookie_attr =
Xiaoping Fane70da412016-02-26 16:47:57 -08002574 __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 -07002575#endif /*CONFIG_NF_FLOW_COOKIE*/
2576
Ken Zhu137722d2021-09-23 17:57:36 -07002577/*
2578 * sfe_ipv6_get_cpu()
2579 */
2580static ssize_t sfe_ipv6_get_cpu(struct device *dev,
2581 struct device_attribute *attr,
2582 char *buf)
2583{
2584 struct sfe_ipv6 *si = &__si6;
2585 return snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", si->work_cpu);
2586}
2587
2588/*
Wayne Tanbb7f1782021-12-13 11:16:04 -08002589 * sfe_ipv6_set_cpu()
Ken Zhu137722d2021-09-23 17:57:36 -07002590 */
2591static ssize_t sfe_ipv6_set_cpu(struct device *dev,
2592 struct device_attribute *attr,
2593 const char *buf, size_t size)
2594{
2595 struct sfe_ipv6 *si = &__si6;
2596 int work_cpu;
2597
2598 work_cpu = simple_strtol(buf, NULL, 0);
2599 if ((work_cpu >= 0) && (work_cpu <= NR_CPUS)) {
2600 si->work_cpu = work_cpu;
2601 } else {
2602 dev_err(dev, "%s is not in valid range[0,%d]", buf, NR_CPUS);
2603 }
2604
2605 return size;
2606}
2607/*
2608 * sysfs attributes.
2609 */
2610static const struct device_attribute sfe_ipv6_cpu_attr =
2611 __ATTR(stat_work_cpu, S_IWUSR | S_IRUGO, sfe_ipv6_get_cpu, sfe_ipv6_set_cpu);
2612
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05302613 /*
2614 * sfe_ipv6_hash_init()
2615 * Initialize conn match hash lists
2616 */
2617static void sfe_ipv6_conn_match_hash_init(struct sfe_ipv6 *si, int len)
2618{
2619 struct hlist_head *hash_list = si->hlist_conn_match_hash_head;
2620 int i;
2621
2622 for (i = 0; i < len; i++) {
2623 INIT_HLIST_HEAD(&hash_list[i]);
2624 }
2625}
2626
Suruchi Suman23a279d2021-11-16 15:13:09 +05302627#ifdef SFE_PROCESS_LOCAL_OUT
2628/*
2629 * sfe_ipv6_local_out()
2630 * Called for packets from ip_local_out() - post encapsulation & other packets
2631 */
2632static unsigned int sfe_ipv6_local_out(void *priv,
2633 struct sk_buff *skb,
2634 const struct nf_hook_state *nhs)
2635{
Nitin Shettyc28f8172022-02-04 16:23:46 +05302636 struct sfe_l2_info l2_info = {0};
2637
Suruchi Suman23a279d2021-11-16 15:13:09 +05302638 DEBUG_TRACE("sfe: sfe_ipv6_local_out hook called.\n");
2639
2640 if (likely(skb->skb_iif)) {
Nitin Shettyc28f8172022-02-04 16:23:46 +05302641 return sfe_ipv6_recv(skb->dev, skb, &l2_info, true) ? NF_STOLEN : NF_ACCEPT;
Suruchi Suman23a279d2021-11-16 15:13:09 +05302642 }
2643
2644 return NF_ACCEPT;
2645}
2646
2647/*
2648 * struct nf_hook_ops sfe_ipv6_ops_local_out[]
2649 * Hooks into netfilter local out packet monitoring points.
2650 */
2651static struct nf_hook_ops sfe_ipv6_ops_local_out[] __read_mostly = {
2652
2653 /*
2654 * Local out routing hook is used to monitor packets.
2655 */
2656 {
2657 .hook = sfe_ipv6_local_out,
2658 .pf = PF_INET6,
2659 .hooknum = NF_INET_LOCAL_OUT,
2660 .priority = NF_IP6_PRI_FIRST,
2661 },
2662};
2663#endif
2664
Xiaoping Fan978b3772015-05-27 14:15:18 -07002665/*
2666 * sfe_ipv6_init()
2667 */
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05302668int sfe_ipv6_init(void)
Xiaoping Fan978b3772015-05-27 14:15:18 -07002669{
2670 struct sfe_ipv6 *si = &__si6;
2671 int result = -1;
2672
2673 DEBUG_INFO("SFE IPv6 init\n");
2674
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05302675 sfe_ipv6_conn_match_hash_init(si, ARRAY_SIZE(si->hlist_conn_match_hash_head));
2676
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302677 si->stats_pcpu = alloc_percpu_gfp(struct sfe_ipv6_stats, GFP_KERNEL | __GFP_ZERO);
2678 if (!si->stats_pcpu) {
2679 DEBUG_ERROR("failed to allocate stats memory for sfe_ipv6\n");
2680 goto exit0;
2681 }
2682
Xiaoping Fan978b3772015-05-27 14:15:18 -07002683 /*
Parikshit Guned31a8202022-01-05 22:15:04 +05302684 * Allocate per cpu per service class memory.
2685 */
2686 si->stats_pcpu_psc = alloc_percpu_gfp(struct sfe_ipv6_service_class_stats_db,
2687 GFP_KERNEL | __GFP_ZERO);
2688 if (!si->stats_pcpu_psc) {
2689 DEBUG_ERROR("failed to allocate per cpu per service clas stats memory\n");
2690 goto exit1;
2691 }
2692
2693 /*
Xiaoping Fan978b3772015-05-27 14:15:18 -07002694 * Create sys/sfe_ipv6
2695 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302696 si->sys_ipv6 = kobject_create_and_add("sfe_ipv6", NULL);
2697 if (!si->sys_ipv6) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07002698 DEBUG_ERROR("failed to register sfe_ipv6\n");
Parikshit Guned31a8202022-01-05 22:15:04 +05302699 goto exit2;
Xiaoping Fan978b3772015-05-27 14:15:18 -07002700 }
2701
2702 /*
2703 * Create files, one for each parameter supported by this module.
2704 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302705 result = sysfs_create_file(si->sys_ipv6, &sfe_ipv6_debug_dev_attr.attr);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002706 if (result) {
2707 DEBUG_ERROR("failed to register debug dev file: %d\n", result);
Parikshit Guned31a8202022-01-05 22:15:04 +05302708 goto exit3;
Xiaoping Fan978b3772015-05-27 14:15:18 -07002709 }
2710
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302711 result = sysfs_create_file(si->sys_ipv6, &sfe_ipv6_cpu_attr.attr);
Ken Zhu137722d2021-09-23 17:57:36 -07002712 if (result) {
2713 DEBUG_ERROR("failed to register debug dev file: %d\n", result);
Parikshit Guned31a8202022-01-05 22:15:04 +05302714 goto exit4;
Ken Zhu137722d2021-09-23 17:57:36 -07002715 }
2716
Xiaoping Fan640faf42015-08-28 15:50:55 -07002717#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302718 result = sysfs_create_file(si->sys_ipv6, &sfe_ipv6_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002719 if (result) {
2720 DEBUG_ERROR("failed to register flow cookie enable file: %d\n", result);
Parikshit Guned31a8202022-01-05 22:15:04 +05302721 goto exit5;
Xiaoping Fan640faf42015-08-28 15:50:55 -07002722 }
2723#endif /* CONFIG_NF_FLOW_COOKIE */
2724
Suruchi Suman23a279d2021-11-16 15:13:09 +05302725#ifdef SFE_PROCESS_LOCAL_OUT
2726#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
2727 result = nf_register_hooks(sfe_ipv6_ops_local_out, ARRAY_SIZE(sfe_ipv6_ops_local_out));
2728#else
2729 result = nf_register_net_hooks(&init_net, sfe_ipv6_ops_local_out, ARRAY_SIZE(sfe_ipv6_ops_local_out));
2730#endif
Suruchi Suman23a279d2021-11-16 15:13:09 +05302731 if (result < 0) {
2732 DEBUG_ERROR("can't register nf local out hook: %d\n", result);
Parikshit Guned31a8202022-01-05 22:15:04 +05302733 goto exit6;
Suruchi Suman23a279d2021-11-16 15:13:09 +05302734 }
Murat Sezginc09b1322022-03-16 10:15:38 -07002735 DEBUG_INFO("Register nf local out hook success: %d\n", result);
2736#endif
Suruchi Suman23a279d2021-11-16 15:13:09 +05302737
Xiaoping Fan978b3772015-05-27 14:15:18 -07002738 /*
2739 * Register our debug char device.
2740 */
2741 result = register_chrdev(0, "sfe_ipv6", &sfe_ipv6_debug_dev_fops);
2742 if (result < 0) {
2743 DEBUG_ERROR("Failed to register chrdev: %d\n", result);
Parikshit Guned31a8202022-01-05 22:15:04 +05302744 goto exit7;
Xiaoping Fan978b3772015-05-27 14:15:18 -07002745 }
2746
2747 si->debug_dev = result;
Ken Zhu137722d2021-09-23 17:57:36 -07002748 si->work_cpu = WORK_CPU_UNBOUND;
Xiaoping Fan978b3772015-05-27 14:15:18 -07002749
2750 /*
Ken Zhu137722d2021-09-23 17:57:36 -07002751 * Create work to handle periodic statistics.
Xiaoping Fan978b3772015-05-27 14:15:18 -07002752 */
Ken Zhu137722d2021-09-23 17:57:36 -07002753 INIT_DELAYED_WORK(&(si->sync_dwork), sfe_ipv6_periodic_sync);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002754
Ken Zhu7a43d882022-01-04 10:51:44 -08002755 sfe_ipv6_sync_many_msg = kzalloc(PAGE_SIZE, GFP_KERNEL);
2756 if(!sfe_ipv6_sync_many_msg) {
2757 goto exit8;
2758 }
2759
2760 sfe_ipv6_msg_init(sfe_ipv6_sync_many_msg, SFE_SPECIAL_INTERFACE_IPV6,
2761 SFE_TX_CONN_STATS_SYNC_MANY_MSG,
2762 sizeof(struct sfe_ipv4_conn_sync_many_msg),
2763 NULL,
2764 NULL);
2765 sfe_ipv6_sync_max_number = (PAGE_SIZE - sizeof(struct sfe_ipv6_msg)) / sizeof(struct sfe_ipv6_conn_sync);
2766
2767 spin_lock_init(&si->lock);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002768 return 0;
2769
Ken Zhu7a43d882022-01-04 10:51:44 -08002770exit8:
2771 unregister_chrdev(si->debug_dev, "sfe_ipv6");
2772
Parikshit Guned31a8202022-01-05 22:15:04 +05302773exit7:
Suruchi Suman23a279d2021-11-16 15:13:09 +05302774#ifdef SFE_PROCESS_LOCAL_OUT
2775#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
2776 DEBUG_TRACE("sfe: Unregister local out hook\n");
2777 nf_unregister_hooks(sfe_ipv6_ops_local_out, ARRAY_SIZE(sfe_ipv6_ops_local_out));
2778#else
2779 DEBUG_TRACE("sfe: Unregister local out hook\n");
2780 nf_unregister_net_hooks(&init_net, sfe_ipv6_ops_local_out, ARRAY_SIZE(sfe_ipv6_ops_local_out));
2781#endif
Parikshit Guned31a8202022-01-05 22:15:04 +05302782exit6:
Murat Sezginc09b1322022-03-16 10:15:38 -07002783#endif
Xiaoping Fan640faf42015-08-28 15:50:55 -07002784#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302785 sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002786
Parikshit Guned31a8202022-01-05 22:15:04 +05302787exit5:
Xiaoping Fan640faf42015-08-28 15:50:55 -07002788#endif /* CONFIG_NF_FLOW_COOKIE */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302789 sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_cpu_attr.attr);
Suruchi Suman23a279d2021-11-16 15:13:09 +05302790
Parikshit Guned31a8202022-01-05 22:15:04 +05302791exit4:
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302792 sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_debug_dev_attr.attr);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002793
Parikshit Guned31a8202022-01-05 22:15:04 +05302794exit3:
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302795 kobject_put(si->sys_ipv6);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002796
Parikshit Guned31a8202022-01-05 22:15:04 +05302797exit2:
2798 free_percpu(si->stats_pcpu_psc);
2799
Xiaoping Fan978b3772015-05-27 14:15:18 -07002800exit1:
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302801 free_percpu(si->stats_pcpu);
2802
2803exit0:
Xiaoping Fan978b3772015-05-27 14:15:18 -07002804 return result;
2805}
2806
2807/*
2808 * sfe_ipv6_exit()
2809 */
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05302810void sfe_ipv6_exit(void)
Xiaoping Fan978b3772015-05-27 14:15:18 -07002811{
2812 struct sfe_ipv6 *si = &__si6;
2813
2814 DEBUG_INFO("SFE IPv6 exit\n");
2815
2816 /*
2817 * Destroy all connections.
2818 */
2819 sfe_ipv6_destroy_all_rules_for_dev(NULL);
2820
Ken Zhu137722d2021-09-23 17:57:36 -07002821 cancel_delayed_work(&si->sync_dwork);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002822
2823 unregister_chrdev(si->debug_dev, "sfe_ipv6");
2824
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302825 free_percpu(si->stats_pcpu);
Parikshit Guned31a8202022-01-05 22:15:04 +05302826 free_percpu(si->stats_pcpu_psc);
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302827
Suruchi Suman23a279d2021-11-16 15:13:09 +05302828#ifdef SFE_PROCESS_LOCAL_OUT
2829#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
2830 DEBUG_TRACE("sfe: Unregister local out hook\n");
2831 nf_unregister_hooks(sfe_ipv6_ops_local_out, ARRAY_SIZE(sfe_ipv6_ops_local_out));
2832#else
2833 DEBUG_TRACE("sfe: Unregister local out hook\n");
2834 nf_unregister_net_hooks(&init_net, sfe_ipv6_ops_local_out, ARRAY_SIZE(sfe_ipv6_ops_local_out));
2835#endif
2836#endif
2837
Xiaoping Fan640faf42015-08-28 15:50:55 -07002838#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302839 sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002840#endif /* CONFIG_NF_FLOW_COOKIE */
Ken Zhu137722d2021-09-23 17:57:36 -07002841
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302842 sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_cpu_attr.attr);
Ken Zhu137722d2021-09-23 17:57:36 -07002843
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302844 sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_debug_dev_attr.attr);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002845
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302846 kobject_put(si->sys_ipv6);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002847}
2848
Xiaoping Fan978b3772015-05-27 14:15:18 -07002849#ifdef CONFIG_NF_FLOW_COOKIE
2850EXPORT_SYMBOL(sfe_ipv6_register_flow_cookie_cb);
2851EXPORT_SYMBOL(sfe_ipv6_unregister_flow_cookie_cb);
2852#endif