blob: 5a7087a4d7c3dee2db87f2ba54b5dd5b3e901786 [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)) ||
168 (cm->match_protocol != protocol) ||
169 (cm->match_dev != dev)) {
170 continue;
171 }
172
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530173 this_cpu_inc(si->stats_pcpu->connection_match_hash_hits64);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700174
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530175 break;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700176
Xiaoping Fan978b3772015-05-27 14:15:18 -0700177 }
178
Xiaoping Fan978b3772015-05-27 14:15:18 -0700179 return cm;
180}
181
182/*
183 * sfe_ipv6_connection_match_update_summary_stats()
184 * Update the summary stats for a connection match entry.
185 */
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530186static inline void sfe_ipv6_connection_match_update_summary_stats(struct sfe_ipv6_connection_match *cm,
187 u32 *packets, u32 *bytes)
188
Xiaoping Fan978b3772015-05-27 14:15:18 -0700189{
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530190 u32 packet_count, byte_count;
191
192 packet_count = atomic_read(&cm->rx_packet_count);
193 cm->rx_packet_count64 += packet_count;
194 atomic_sub(packet_count, &cm->rx_packet_count);
195
196 byte_count = atomic_read(&cm->rx_byte_count);
197 cm->rx_byte_count64 += byte_count;
198 atomic_sub(byte_count, &cm->rx_byte_count);
199
200 *packets = packet_count;
201 *bytes = byte_count;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700202}
203
204/*
205 * sfe_ipv6_connection_match_compute_translations()
206 * Compute port and address translations for a connection match entry.
207 */
208static void sfe_ipv6_connection_match_compute_translations(struct sfe_ipv6_connection_match *cm)
209{
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700210 u32 diff[9];
211 u32 *idx_32;
212 u16 *idx_16;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700213
214 /*
215 * Before we insert the entry look to see if this is tagged as doing address
216 * translations. If it is then work out the adjustment that we need to apply
217 * to the transport checksum.
218 */
219 if (cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_XLATE_SRC) {
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700220 u32 adj = 0;
221 u32 carry = 0;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700222
223 /*
224 * Precompute an incremental checksum adjustment so we can
225 * edit packets in this stream very quickly. The algorithm is from RFC1624.
226 */
227 idx_32 = diff;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530228 *(idx_32++) = cm->match_src_ip[0].addr[0];
229 *(idx_32++) = cm->match_src_ip[0].addr[1];
230 *(idx_32++) = cm->match_src_ip[0].addr[2];
231 *(idx_32++) = cm->match_src_ip[0].addr[3];
Xiaoping Fan978b3772015-05-27 14:15:18 -0700232
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700233 idx_16 = (u16 *)idx_32;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700234 *(idx_16++) = cm->match_src_port;
235 *(idx_16++) = ~cm->xlate_src_port;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700236 idx_32 = (u32 *)idx_16;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700237
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530238 *(idx_32++) = ~cm->xlate_src_ip[0].addr[0];
239 *(idx_32++) = ~cm->xlate_src_ip[0].addr[1];
240 *(idx_32++) = ~cm->xlate_src_ip[0].addr[2];
241 *(idx_32++) = ~cm->xlate_src_ip[0].addr[3];
Xiaoping Fan978b3772015-05-27 14:15:18 -0700242
243 /*
244 * When we compute this fold it down to a 16-bit offset
245 * as that way we can avoid having to do a double
246 * folding of the twos-complement result because the
247 * addition of 2 16-bit values cannot cause a double
248 * wrap-around!
249 */
250 for (idx_32 = diff; idx_32 < diff + 9; idx_32++) {
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700251 u32 w = *idx_32;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700252 adj += carry;
253 adj += w;
254 carry = (w > adj);
255 }
256 adj += carry;
257 adj = (adj & 0xffff) + (adj >> 16);
258 adj = (adj & 0xffff) + (adj >> 16);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700259 cm->xlate_src_csum_adjustment = (u16)adj;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700260 }
261
262 if (cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_XLATE_DEST) {
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700263 u32 adj = 0;
264 u32 carry = 0;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700265
266 /*
267 * Precompute an incremental checksum adjustment so we can
268 * edit packets in this stream very quickly. The algorithm is from RFC1624.
269 */
270 idx_32 = diff;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530271 *(idx_32++) = cm->match_dest_ip[0].addr[0];
272 *(idx_32++) = cm->match_dest_ip[0].addr[1];
273 *(idx_32++) = cm->match_dest_ip[0].addr[2];
274 *(idx_32++) = cm->match_dest_ip[0].addr[3];
Xiaoping Fan978b3772015-05-27 14:15:18 -0700275
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700276 idx_16 = (u16 *)idx_32;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700277 *(idx_16++) = cm->match_dest_port;
278 *(idx_16++) = ~cm->xlate_dest_port;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700279 idx_32 = (u32 *)idx_16;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700280
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530281 *(idx_32++) = ~cm->xlate_dest_ip[0].addr[0];
282 *(idx_32++) = ~cm->xlate_dest_ip[0].addr[1];
283 *(idx_32++) = ~cm->xlate_dest_ip[0].addr[2];
284 *(idx_32++) = ~cm->xlate_dest_ip[0].addr[3];
Xiaoping Fan978b3772015-05-27 14:15:18 -0700285
286 /*
287 * When we compute this fold it down to a 16-bit offset
288 * as that way we can avoid having to do a double
289 * folding of the twos-complement result because the
290 * addition of 2 16-bit values cannot cause a double
291 * wrap-around!
292 */
293 for (idx_32 = diff; idx_32 < diff + 9; idx_32++) {
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700294 u32 w = *idx_32;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700295 adj += carry;
296 adj += w;
297 carry = (w > adj);
298 }
299 adj += carry;
300 adj = (adj & 0xffff) + (adj >> 16);
301 adj = (adj & 0xffff) + (adj >> 16);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700302 cm->xlate_dest_csum_adjustment = (u16)adj;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700303 }
304}
305
306/*
307 * sfe_ipv6_update_summary_stats()
308 * Update the summary stats.
309 */
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530310static void sfe_ipv6_update_summary_stats(struct sfe_ipv6 *si, struct sfe_ipv6_stats *stats)
Xiaoping Fan978b3772015-05-27 14:15:18 -0700311{
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530312 int i = 0;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700313
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530314 memset(stats, 0, sizeof(*stats));
Xiaoping Fan978b3772015-05-27 14:15:18 -0700315
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530316 for_each_possible_cpu(i) {
317 const struct sfe_ipv6_stats *s = per_cpu_ptr(si->stats_pcpu, i);
318
319 stats->connection_create_requests64 += s->connection_create_requests64;
320 stats->connection_create_collisions64 += s->connection_create_collisions64;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530321 stats->connection_create_failures64 += s->connection_create_failures64;
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530322 stats->connection_destroy_requests64 += s->connection_destroy_requests64;
323 stats->connection_destroy_misses64 += s->connection_destroy_misses64;
324 stats->connection_match_hash_hits64 += s->connection_match_hash_hits64;
325 stats->connection_match_hash_reorders64 += s->connection_match_hash_reorders64;
326 stats->connection_flushes64 += s->connection_flushes64;
Suruchi Suman23a279d2021-11-16 15:13:09 +0530327 stats->packets_dropped64 += s->packets_dropped64;
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530328 stats->packets_forwarded64 += s->packets_forwarded64;
Ken Zhu7e38d1a2021-11-30 17:31:46 -0800329 stats->packets_fast_xmited64 += s->packets_fast_xmited64;
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530330 stats->packets_not_forwarded64 += s->packets_not_forwarded64;
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +0530331 stats->pppoe_encap_packets_forwarded64 += s->pppoe_encap_packets_forwarded64;
332 stats->pppoe_decap_packets_forwarded64 += s->pppoe_decap_packets_forwarded64;
Guduri Prathyusha034d6352022-01-12 16:49:04 +0530333 stats->pppoe_bridge_packets_forwarded64 += s->pppoe_bridge_packets_forwarded64;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700334 }
335}
336
337/*
338 * sfe_ipv6_insert_connection_match()
339 * Insert a connection match into the hash.
340 *
341 * On entry we must be holding the lock that protects the hash table.
342 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700343static inline void sfe_ipv6_insert_connection_match(struct sfe_ipv6 *si,
344 struct sfe_ipv6_connection_match *cm)
Xiaoping Fan978b3772015-05-27 14:15:18 -0700345{
Xiaoping Fan978b3772015-05-27 14:15:18 -0700346 unsigned int conn_match_idx
347 = sfe_ipv6_get_connection_match_hash(cm->match_dev, cm->match_protocol,
348 cm->match_src_ip, cm->match_src_port,
349 cm->match_dest_ip, cm->match_dest_port);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700350
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530351 lockdep_assert_held(&si->lock);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700352
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530353 hlist_add_head_rcu(&cm->hnode, &si->hlist_conn_match_hash_head[conn_match_idx]);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700354#ifdef CONFIG_NF_FLOW_COOKIE
Xiaoping Fan640faf42015-08-28 15:50:55 -0700355 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 -0700356 return;
357
358 /*
359 * Configure hardware to put a flow cookie in packet of this flow,
360 * then we can accelerate the lookup process when we received this packet.
361 */
362 for (conn_match_idx = 1; conn_match_idx < SFE_FLOW_COOKIE_SIZE; conn_match_idx++) {
363 struct sfe_ipv6_flow_cookie_entry *entry = &si->sfe_flow_cookie_table[conn_match_idx];
364
365 if ((NULL == entry->match) && time_is_before_jiffies(entry->last_clean_time + HZ)) {
366 sfe_ipv6_flow_cookie_set_func_t func;
367
368 rcu_read_lock();
369 func = rcu_dereference(si->flow_cookie_set_func);
370 if (func) {
371 if (!func(cm->match_protocol, cm->match_src_ip->addr, cm->match_src_port,
372 cm->match_dest_ip->addr, cm->match_dest_port, conn_match_idx)) {
373 entry->match = cm;
374 cm->flow_cookie = conn_match_idx;
375 } else {
376 si->exception_events[SFE_IPV6_EXCEPTION_EVENT_FLOW_COOKIE_ADD_FAIL]++;
377 }
378 }
379 rcu_read_unlock();
380
381 break;
382 }
383 }
384#endif
Xiaoping Fan978b3772015-05-27 14:15:18 -0700385}
386
387/*
388 * sfe_ipv6_remove_connection_match()
389 * Remove a connection match object from the hash.
Xiaoping Fan978b3772015-05-27 14:15:18 -0700390 */
391static inline void sfe_ipv6_remove_connection_match(struct sfe_ipv6 *si, struct sfe_ipv6_connection_match *cm)
392{
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530393
394 lockdep_assert_held(&si->lock);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700395#ifdef CONFIG_NF_FLOW_COOKIE
Xiaoping Fan640faf42015-08-28 15:50:55 -0700396 if (si->flow_cookie_enable) {
397 /*
398 * Tell hardware that we no longer need a flow cookie in packet of this flow
399 */
400 unsigned int conn_match_idx;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700401
Xiaoping Fan640faf42015-08-28 15:50:55 -0700402 for (conn_match_idx = 1; conn_match_idx < SFE_FLOW_COOKIE_SIZE; conn_match_idx++) {
403 struct sfe_ipv6_flow_cookie_entry *entry = &si->sfe_flow_cookie_table[conn_match_idx];
Xiaoping Fan978b3772015-05-27 14:15:18 -0700404
Xiaoping Fan640faf42015-08-28 15:50:55 -0700405 if (cm == entry->match) {
406 sfe_ipv6_flow_cookie_set_func_t func;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700407
Xiaoping Fan640faf42015-08-28 15:50:55 -0700408 rcu_read_lock();
409 func = rcu_dereference(si->flow_cookie_set_func);
410 if (func) {
411 func(cm->match_protocol, cm->match_src_ip->addr, cm->match_src_port,
412 cm->match_dest_ip->addr, cm->match_dest_port, 0);
413 }
414 rcu_read_unlock();
415
416 cm->flow_cookie = 0;
417 entry->match = NULL;
418 entry->last_clean_time = jiffies;
419 break;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700420 }
Xiaoping Fan978b3772015-05-27 14:15:18 -0700421 }
422 }
423#endif
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530424 hlist_del_init_rcu(&cm->hnode);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700425
Xiaoping Fan978b3772015-05-27 14:15:18 -0700426}
427
428/*
429 * sfe_ipv6_get_connection_hash()
430 * Generate the hash used in connection lookups.
431 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700432static 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 -0700433 struct sfe_ipv6_addr *dest_ip, __be16 dest_port)
434{
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700435 u32 idx, hash = 0;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700436
437 for (idx = 0; idx < 4; idx++) {
438 hash ^= src_ip->addr[idx] ^ dest_ip->addr[idx];
439 }
440 hash = hash ^ protocol ^ ntohs(src_port ^ dest_port);
441 return ((hash >> SFE_IPV6_CONNECTION_HASH_SHIFT) ^ hash) & SFE_IPV6_CONNECTION_HASH_MASK;
442}
443
444/*
445 * sfe_ipv6_find_connection()
446 * Get the IPv6 connection info that corresponds to a particular 5-tuple.
447 *
448 * On entry we must be holding the lock that protects the hash table.
449 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700450static inline struct sfe_ipv6_connection *sfe_ipv6_find_connection(struct sfe_ipv6 *si, u32 protocol,
Xiaoping Fan978b3772015-05-27 14:15:18 -0700451 struct sfe_ipv6_addr *src_ip, __be16 src_port,
452 struct sfe_ipv6_addr *dest_ip, __be16 dest_port)
453{
454 struct sfe_ipv6_connection *c;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530455
Xiaoping Fan978b3772015-05-27 14:15:18 -0700456 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 +0530457
458 lockdep_assert_held(&si->lock);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700459 c = si->conn_hash[conn_idx];
460
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530461 while (c) {
462 if ((c->src_port == src_port)
463 && (c->dest_port == dest_port)
464 && (sfe_ipv6_addr_equal(c->src_ip, src_ip))
465 && (sfe_ipv6_addr_equal(c->dest_ip, dest_ip))
466 && (c->protocol == protocol)) {
467 return c;
468 }
Xiaoping Fan978b3772015-05-27 14:15:18 -0700469 c = c->next;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530470 }
Xiaoping Fan978b3772015-05-27 14:15:18 -0700471
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530472 return NULL;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700473}
474
475/*
Xiaoping Fan978b3772015-05-27 14:15:18 -0700476 * sfe_ipv6_insert_connection()
477 * Insert a connection into the hash.
478 *
479 * On entry we must be holding the lock that protects the hash table.
480 */
481static void sfe_ipv6_insert_connection(struct sfe_ipv6 *si, struct sfe_ipv6_connection *c)
482{
483 struct sfe_ipv6_connection **hash_head;
484 struct sfe_ipv6_connection *prev_head;
485 unsigned int conn_idx;
486
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530487 lockdep_assert_held(&si->lock);
488
Xiaoping Fan978b3772015-05-27 14:15:18 -0700489 /*
490 * Insert entry into the connection hash.
491 */
492 conn_idx = sfe_ipv6_get_connection_hash(c->protocol, c->src_ip, c->src_port,
493 c->dest_ip, c->dest_port);
494 hash_head = &si->conn_hash[conn_idx];
495 prev_head = *hash_head;
496 c->prev = NULL;
497 if (prev_head) {
498 prev_head->prev = c;
499 }
500
501 c->next = prev_head;
502 *hash_head = c;
503
504 /*
505 * Insert entry into the "all connections" list.
506 */
507 if (si->all_connections_tail) {
508 c->all_connections_prev = si->all_connections_tail;
509 si->all_connections_tail->all_connections_next = c;
510 } else {
511 c->all_connections_prev = NULL;
512 si->all_connections_head = c;
513 }
514
515 si->all_connections_tail = c;
516 c->all_connections_next = NULL;
517 si->num_connections++;
518
519 /*
520 * Insert the connection match objects too.
521 */
522 sfe_ipv6_insert_connection_match(si, c->original_match);
523 sfe_ipv6_insert_connection_match(si, c->reply_match);
524}
525
526/*
527 * sfe_ipv6_remove_connection()
528 * Remove a sfe_ipv6_connection object from the hash.
529 *
530 * On entry we must be holding the lock that protects the hash table.
531 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530532bool sfe_ipv6_remove_connection(struct sfe_ipv6 *si, struct sfe_ipv6_connection *c)
Xiaoping Fan978b3772015-05-27 14:15:18 -0700533{
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530534
535 lockdep_assert_held(&si->lock);
536 if (c->removed) {
537 DEBUG_ERROR("%px: Connection has been removed already\n", c);
538 return false;
539 }
540
Xiaoping Fan978b3772015-05-27 14:15:18 -0700541 /*
Tian Yang435afc42022-02-02 12:47:32 -0800542 * dereference the decap direction top_interface_dev
543 */
544 if (c->reply_match->top_interface_dev) {
545 dev_put(c->reply_match->top_interface_dev);
546 }
547 /*
Xiaoping Fan978b3772015-05-27 14:15:18 -0700548 * Remove the connection match objects.
549 */
550 sfe_ipv6_remove_connection_match(si, c->reply_match);
551 sfe_ipv6_remove_connection_match(si, c->original_match);
552
553 /*
554 * Unlink the connection.
555 */
556 if (c->prev) {
557 c->prev->next = c->next;
558 } else {
559 unsigned int conn_idx = sfe_ipv6_get_connection_hash(c->protocol, c->src_ip, c->src_port,
560 c->dest_ip, c->dest_port);
561 si->conn_hash[conn_idx] = c->next;
562 }
563
564 if (c->next) {
565 c->next->prev = c->prev;
566 }
Xiaoping Fan34586472015-07-03 02:20:35 -0700567
568 /*
569 * Unlink connection from all_connections list
570 */
571 if (c->all_connections_prev) {
572 c->all_connections_prev->all_connections_next = c->all_connections_next;
573 } else {
574 si->all_connections_head = c->all_connections_next;
575 }
576
577 if (c->all_connections_next) {
578 c->all_connections_next->all_connections_prev = c->all_connections_prev;
579 } else {
580 si->all_connections_tail = c->all_connections_prev;
581 }
582
Ken Zhu32b95392021-09-03 13:52:04 -0700583 /*
584 * If I am the next sync connection, move the sync to my next or head.
585 */
586 if (unlikely(si->wc_next == c)) {
587 si->wc_next = c->all_connections_next;
588 }
589
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530590 c->removed = true;
Xiaoping Fan34586472015-07-03 02:20:35 -0700591 si->num_connections--;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530592 return true;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700593}
594
595/*
596 * sfe_ipv6_gen_sync_connection()
597 * Sync a connection.
598 *
599 * On entry to this function we expect that the lock for the connection is either
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530600 * already held (while called from sfe_ipv6_periodic_sync() or isn't required
601 * (while called from sfe_ipv6_flush_sfe_ipv6_connection())
Xiaoping Fan978b3772015-05-27 14:15:18 -0700602 */
603static void sfe_ipv6_gen_sync_connection(struct sfe_ipv6 *si, struct sfe_ipv6_connection *c,
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700604 struct sfe_connection_sync *sis, sfe_sync_reason_t reason,
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700605 u64 now_jiffies)
Xiaoping Fan978b3772015-05-27 14:15:18 -0700606{
607 struct sfe_ipv6_connection_match *original_cm;
608 struct sfe_ipv6_connection_match *reply_cm;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530609 u32 packet_count, byte_count;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700610
611 /*
612 * Fill in the update message.
613 */
Murat Sezgin53509a12016-12-27 16:57:34 -0800614 sis->is_v6 = 1;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700615 sis->protocol = c->protocol;
616 sis->src_ip.ip6[0] = c->src_ip[0];
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700617 sis->src_ip_xlate.ip6[0] = c->src_ip_xlate[0];
Xiaoping Fan978b3772015-05-27 14:15:18 -0700618 sis->dest_ip.ip6[0] = c->dest_ip[0];
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700619 sis->dest_ip_xlate.ip6[0] = c->dest_ip_xlate[0];
Xiaoping Fan978b3772015-05-27 14:15:18 -0700620 sis->src_port = c->src_port;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700621 sis->src_port_xlate = c->src_port_xlate;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700622 sis->dest_port = c->dest_port;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700623 sis->dest_port_xlate = c->dest_port_xlate;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700624
625 original_cm = c->original_match;
626 reply_cm = c->reply_match;
627 sis->src_td_max_window = original_cm->protocol_state.tcp.max_win;
628 sis->src_td_end = original_cm->protocol_state.tcp.end;
629 sis->src_td_max_end = original_cm->protocol_state.tcp.max_end;
630 sis->dest_td_max_window = reply_cm->protocol_state.tcp.max_win;
631 sis->dest_td_end = reply_cm->protocol_state.tcp.end;
632 sis->dest_td_max_end = reply_cm->protocol_state.tcp.max_end;
633
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530634 sfe_ipv6_connection_match_update_summary_stats(original_cm, &packet_count, &byte_count);
635 sis->src_new_packet_count = packet_count;
636 sis->src_new_byte_count = byte_count;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700637
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530638 sfe_ipv6_connection_match_update_summary_stats(reply_cm, &packet_count, &byte_count);
639 sis->dest_new_packet_count = packet_count;
640 sis->dest_new_byte_count = byte_count;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700641
642 sis->src_dev = original_cm->match_dev;
643 sis->src_packet_count = original_cm->rx_packet_count64;
644 sis->src_byte_count = original_cm->rx_byte_count64;
645
646 sis->dest_dev = reply_cm->match_dev;
647 sis->dest_packet_count = reply_cm->rx_packet_count64;
648 sis->dest_byte_count = reply_cm->rx_byte_count64;
649
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700650 sis->reason = reason;
651
Xiaoping Fan978b3772015-05-27 14:15:18 -0700652 /*
653 * Get the time increment since our last sync.
654 */
655 sis->delta_jiffies = now_jiffies - c->last_sync_jiffies;
656 c->last_sync_jiffies = now_jiffies;
657}
658
659/*
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530660 * sfe_ipv6_free_sfe_ipv6_connection_rcu()
661 * Called at RCU qs state to free the connection object.
662 */
663static void sfe_ipv6_free_sfe_ipv6_connection_rcu(struct rcu_head *head)
664{
665 struct sfe_ipv6_connection *c;
Suruchi Suman23a279d2021-11-16 15:13:09 +0530666 struct udp_sock *up;
667 struct sock *sk;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530668
669 /*
670 * We dont need spin lock as the connection is already removed from link list
671 */
672 c = container_of(head, struct sfe_ipv6_connection, rcu);
673 BUG_ON(!c->removed);
674
675 DEBUG_TRACE("%px: connecton has been deleted\n", c);
676
677 /*
Suruchi Suman23a279d2021-11-16 15:13:09 +0530678 * Decrease the refcount taken in function sfe_ipv6_create_rule()
679 * during call of __udp6_lib_lookup()
680 */
681 up = c->reply_match->up;
682 if (up) {
683 sk = (struct sock *)up;
684 sock_put(sk);
685 }
686
687 /*
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530688 * Release our hold of the source and dest devices and free the memory
689 * for our connection objects.
690 */
691 dev_put(c->original_dev);
692 dev_put(c->reply_dev);
693 kfree(c->original_match);
694 kfree(c->reply_match);
695 kfree(c);
696}
697
698/*
Ken Zhu88c58152021-12-09 15:12:06 -0800699 * sfe_ipv6_sync_status()
700 * update a connection status to its connection manager.
701 *
702 * si: the ipv6 context
703 * c: which connection to be notified
704 * reason: what kind of reason: flush, or destroy
705 */
706void sfe_ipv6_sync_status(struct sfe_ipv6 *si,
707 struct sfe_ipv6_connection *c,
708 sfe_sync_reason_t reason)
709{
710 struct sfe_connection_sync sis;
711 u64 now_jiffies;
712 sfe_sync_rule_callback_t sync_rule_callback;
713
714 rcu_read_lock();
715 sync_rule_callback = rcu_dereference(si->sync_rule_callback);
Ken Zhu7a43d882022-01-04 10:51:44 -0800716 rcu_read_unlock();
Ken Zhu88c58152021-12-09 15:12:06 -0800717 if (unlikely(!sync_rule_callback)) {
Ken Zhu88c58152021-12-09 15:12:06 -0800718 return;
719 }
720
721 /*
722 * Generate a sync message and then sync.
723 */
724 now_jiffies = get_jiffies_64();
725 sfe_ipv6_gen_sync_connection(si, c, &sis, reason, now_jiffies);
726 sync_rule_callback(&sis);
Ken Zhu88c58152021-12-09 15:12:06 -0800727}
728
729/*
Xiaoping Fan978b3772015-05-27 14:15:18 -0700730 * sfe_ipv6_flush_connection()
731 * Flush a connection and free all associated resources.
732 *
733 * We need to be called with bottom halves disabled locally as we need to acquire
734 * the connection hash lock and release it again. In general we're actually called
735 * from within a BH and so we're fine, but we're also called when connections are
736 * torn down.
737 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530738void sfe_ipv6_flush_connection(struct sfe_ipv6 *si,
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700739 struct sfe_ipv6_connection *c,
740 sfe_sync_reason_t reason)
Xiaoping Fan978b3772015-05-27 14:15:18 -0700741{
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530742 BUG_ON(!c->removed);
743
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530744 this_cpu_inc(si->stats_pcpu->connection_flushes64);
Ken Zhu88c58152021-12-09 15:12:06 -0800745 sfe_ipv6_sync_status(si, c, reason);
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530746
747 /*
Ken Zhu88c58152021-12-09 15:12:06 -0800748 * Release our hold of the source and dest devices and free the memory
749 * for our connection objects.
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530750 */
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530751 call_rcu(&c->rcu, sfe_ipv6_free_sfe_ipv6_connection_rcu);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700752}
753
Parikshit Guned31a8202022-01-05 22:15:04 +0530754/*
Jackson Bockus3fafbf32022-02-13 17:15:26 -0800755 * sfe_ipv4_service_class_stats_pcpu_get()
756 * Gets one CPU's service class statistics.
757 */
758static inline bool sfe_ipv6_service_class_stats_pcpu_get(struct sfe_ipv6_per_service_class_stats *sc_stats, uint64_t *bytes, uint64_t *packets)
759{
760 uint32_t retries = 0;
761 uint32_t seq;
762 uint64_t bytes_tmp, packets_tmp;
763
764 do {
765 seq = read_seqcount_begin(&sc_stats->seq);
766 bytes_tmp = sc_stats->tx_bytes;
767 packets_tmp = sc_stats->tx_packets;
768 } while (read_seqcount_retry(&sc_stats->seq, seq) && ++retries < SFE_SERVICE_CLASS_STATS_MAX_RETRY);
769
770 *bytes += bytes_tmp;
771 *packets += packets_tmp;
772
773 return retries < SFE_SERVICE_CLASS_STATS_MAX_RETRY;
774}
775
776/*
777 * sfe_ipv4_service_class_stats_get()
778 * Copy the ipv4 statistics for the given service class.
779 */
780bool sfe_ipv6_service_class_stats_get(uint8_t sid, uint64_t *bytes, uint64_t *packets)
781{
782 struct sfe_ipv6 *si = &__si6;
783 uint32_t cpu = 0;
784
785 for_each_possible_cpu(cpu) {
786 struct sfe_ipv6_service_class_stats_db *stats_db = per_cpu_ptr(si->stats_pcpu_psc, cpu);
787 struct sfe_ipv6_per_service_class_stats *sc_stats = &stats_db->psc_stats[sid];
788
789 if (!sfe_ipv6_service_class_stats_pcpu_get(sc_stats, bytes, packets)) {
790 return false;
791 }
792 }
793
794 return true;
795}
796
797/*
Parikshit Guned31a8202022-01-05 22:15:04 +0530798 * sfe_ipv6_service_class_stats_inc()
799 * Increment per cpu per service class stats.
800 */
801void sfe_ipv6_service_class_stats_inc(struct sfe_ipv6 *si, uint8_t sid, uint64_t bytes)
802{
803 struct sfe_ipv6_service_class_stats_db *sc_stats_db = this_cpu_ptr(si->stats_pcpu_psc);
804 struct sfe_ipv6_per_service_class_stats *sc_stats = &sc_stats_db->psc_stats[sid];
805
806 write_seqcount_begin(&sc_stats->seq);
807 sc_stats->tx_bytes += bytes;
808 sc_stats->tx_packets++;
809 write_seqcount_end(&sc_stats->seq);
810}
811
812/*
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530813 * sfe_ipv6_exception_stats_inc()
814 * Increment exception stats.
815 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530816void sfe_ipv6_exception_stats_inc(struct sfe_ipv6 *si, enum sfe_ipv6_exception_events reason)
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530817{
818 struct sfe_ipv6_stats *stats = this_cpu_ptr(si->stats_pcpu);
819
820 stats->exception_events64[reason]++;
821 stats->packets_not_forwarded64++;
822}
823
Xiaoping Fan978b3772015-05-27 14:15:18 -0700824/*
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530825 * sfe_ipv6_is_local_ip()
826 * return true if it is local ip otherwise return false
827 */
828static bool sfe_ipv6_is_local_ip(struct sfe_ipv6 *si, uint8_t *addr)
829{
830 struct net_device *dev;
831 struct in6_addr ip_addr;
832 memcpy(ip_addr.s6_addr, addr, 16);
833
834 dev = ipv6_dev_find(&init_net, &ip_addr, 1);
835 if (dev) {
836 dev_put(dev);
837 return true;
838 }
839
840 return false;
841}
842
843/*
Xiaoping Fan978b3772015-05-27 14:15:18 -0700844 * sfe_ipv6_recv()
845 * Handle packet receives and forwaring.
846 *
847 * Returns 1 if the packet is forwarded or 0 if it isn't.
848 */
Suruchi Suman23a279d2021-11-16 15:13:09 +0530849int 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 -0700850{
851 struct sfe_ipv6 *si = &__si6;
852 unsigned int len;
853 unsigned int payload_len;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530854 unsigned int ihl = sizeof(struct ipv6hdr);
Ken Zhu88c58152021-12-09 15:12:06 -0800855 bool sync_on_find = false;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530856 struct ipv6hdr *iph;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700857 u8 next_hdr;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700858
859 /*
860 * Check that we have space for an IP header and an uplayer header here.
861 */
862 len = skb->len;
863 if (!pskb_may_pull(skb, ihl + sizeof(struct sfe_ipv6_ext_hdr))) {
Xiaoping Fan978b3772015-05-27 14:15:18 -0700864
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530865 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_HEADER_INCOMPLETE);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700866 DEBUG_TRACE("len: %u is too short\n", len);
867 return 0;
868 }
869
870 /*
871 * Is our IP version wrong?
872 */
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530873 iph = (struct ipv6hdr *)skb->data;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700874 if (unlikely(iph->version != 6)) {
Xiaoping Fan978b3772015-05-27 14:15:18 -0700875
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530876 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_NON_V6);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700877 DEBUG_TRACE("IP version: %u\n", iph->version);
878 return 0;
879 }
880
881 /*
882 * Does our datagram fit inside the skb?
883 */
884 payload_len = ntohs(iph->payload_len);
885 if (unlikely(payload_len > (len - ihl))) {
Xiaoping Fan978b3772015-05-27 14:15:18 -0700886
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530887 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_DATAGRAM_INCOMPLETE);
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530888 DEBUG_TRACE("payload_len: %u, exceeds len: %u\n", payload_len, (len - (unsigned int)sizeof(struct ipv6hdr)));
Xiaoping Fan978b3772015-05-27 14:15:18 -0700889 return 0;
890 }
891
892 next_hdr = iph->nexthdr;
893 while (unlikely(sfe_ipv6_is_ext_hdr(next_hdr))) {
894 struct sfe_ipv6_ext_hdr *ext_hdr;
895 unsigned int ext_hdr_len;
896
897 ext_hdr = (struct sfe_ipv6_ext_hdr *)(skb->data + ihl);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700898
899 ext_hdr_len = ext_hdr->hdr_len;
900 ext_hdr_len <<= 3;
901 ext_hdr_len += sizeof(struct sfe_ipv6_ext_hdr);
902 ihl += ext_hdr_len;
903 if (!pskb_may_pull(skb, ihl + sizeof(struct sfe_ipv6_ext_hdr))) {
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530904 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_HEADER_INCOMPLETE);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700905
906 DEBUG_TRACE("extension header %d not completed\n", next_hdr);
907 return 0;
908 }
Ken Zhu88c58152021-12-09 15:12:06 -0800909 /*
910 * Any packets have extend hdr, won't be handled in the fast
911 * path,sync its status and exception to the kernel.
912 */
913 sync_on_find = true;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700914 next_hdr = ext_hdr->next_hdr;
915 }
916
917 if (IPPROTO_UDP == next_hdr) {
Ken Zhu88c58152021-12-09 15:12:06 -0800918 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 -0700919 }
920
921 if (IPPROTO_TCP == next_hdr) {
Ken Zhu88c58152021-12-09 15:12:06 -0800922 return sfe_ipv6_recv_tcp(si, skb, dev, len, iph, ihl, sync_on_find, l2_info);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700923 }
924
925 if (IPPROTO_ICMPV6 == next_hdr) {
926 return sfe_ipv6_recv_icmp(si, skb, dev, len, iph, ihl);
927 }
928
Tian Yangafb03452022-01-13 18:53:13 -0800929 if (IPPROTO_IPIP == next_hdr) {
930 return sfe_ipv6_recv_tunipip6(si, skb, dev, len, iph, ihl, sync_on_find, l2_info, true);
931 }
932
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530933#ifdef SFE_GRE_TUN_ENABLE
934 if (IPPROTO_GRE == next_hdr) {
Nitin Shetty2114a892022-01-28 20:03:56 +0530935 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 +0530936 }
937#endif
938
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530939 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_UNHANDLED_PROTOCOL);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700940 DEBUG_TRACE("not UDP, TCP or ICMP: %u\n", next_hdr);
941 return 0;
942}
943
944/*
945 * sfe_ipv6_update_tcp_state()
946 * update TCP window variables.
947 */
948static void
949sfe_ipv6_update_tcp_state(struct sfe_ipv6_connection *c,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530950 struct sfe_ipv6_rule_create_msg *msg)
Xiaoping Fan978b3772015-05-27 14:15:18 -0700951{
952 struct sfe_ipv6_connection_match *orig_cm;
953 struct sfe_ipv6_connection_match *repl_cm;
954 struct sfe_ipv6_tcp_connection_match *orig_tcp;
955 struct sfe_ipv6_tcp_connection_match *repl_tcp;
956
957 orig_cm = c->original_match;
958 repl_cm = c->reply_match;
959 orig_tcp = &orig_cm->protocol_state.tcp;
960 repl_tcp = &repl_cm->protocol_state.tcp;
961
962 /* update orig */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530963 if (orig_tcp->max_win < msg->tcp_rule.flow_max_window) {
964 orig_tcp->max_win = msg->tcp_rule.flow_max_window;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700965 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530966 if ((s32)(orig_tcp->end - msg->tcp_rule.flow_end) < 0) {
967 orig_tcp->end = msg->tcp_rule.flow_end;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700968 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530969 if ((s32)(orig_tcp->max_end - msg->tcp_rule.flow_max_end) < 0) {
970 orig_tcp->max_end = msg->tcp_rule.flow_max_end;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700971 }
972
973 /* update reply */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530974 if (repl_tcp->max_win < msg->tcp_rule.return_max_window) {
975 repl_tcp->max_win = msg->tcp_rule.return_max_window;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700976 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530977 if ((s32)(repl_tcp->end - msg->tcp_rule.return_end) < 0) {
978 repl_tcp->end = msg->tcp_rule.return_end;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700979 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530980 if ((s32)(repl_tcp->max_end - msg->tcp_rule.return_max_end) < 0) {
981 repl_tcp->max_end = msg->tcp_rule.return_max_end;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700982 }
983
984 /* update match flags */
985 orig_cm->flags &= ~SFE_IPV6_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
986 repl_cm->flags &= ~SFE_IPV6_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530987 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_NO_SEQ_CHECK) {
Xiaoping Fan978b3772015-05-27 14:15:18 -0700988 orig_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
989 repl_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
990 }
991}
992
993/*
994 * sfe_ipv6_update_protocol_state()
995 * update protocol specified state machine.
996 */
997static void
998sfe_ipv6_update_protocol_state(struct sfe_ipv6_connection *c,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530999 struct sfe_ipv6_rule_create_msg *msg)
Xiaoping Fan978b3772015-05-27 14:15:18 -07001000{
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301001 switch (msg->tuple.protocol) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07001002 case IPPROTO_TCP:
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301003 sfe_ipv6_update_tcp_state(c, msg);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001004 break;
1005 }
1006}
1007
1008/*
Wayne Tanbb7f1782021-12-13 11:16:04 -08001009 * sfe_ipv6_match_entry_set_vlan()
1010 */
1011static void sfe_ipv6_match_entry_set_vlan(
1012 struct sfe_ipv6_connection_match *cm,
1013 u32 primary_ingress_vlan_tag,
1014 u32 primary_egress_vlan_tag,
1015 u32 secondary_ingress_vlan_tag,
1016 u32 secondary_egress_vlan_tag)
1017{
1018 u16 tpid;
1019 /*
1020 * Prevent stacking header counts when updating.
1021 */
1022 cm->ingress_vlan_hdr_cnt = 0;
1023 cm->egress_vlan_hdr_cnt = 0;
1024 memset(cm->ingress_vlan_hdr, 0, sizeof(cm->ingress_vlan_hdr));
1025 memset(cm->egress_vlan_hdr, 0, sizeof(cm->egress_vlan_hdr));
1026
1027 /*
1028 * vlan_hdr[0] corresponds to outer tag
1029 * vlan_hdr[1] corresponds to inner tag
1030 * Extract the vlan information (tpid and tci) from rule message
1031 */
1032 if ((primary_ingress_vlan_tag & VLAN_VID_MASK) != SFE_VLAN_ID_NOT_CONFIGURED) {
1033 tpid = (u16)(primary_ingress_vlan_tag >> 16);
1034 cm->ingress_vlan_hdr[0].tpid = ntohs(tpid);
1035 cm->ingress_vlan_hdr[0].tci = (u16)primary_ingress_vlan_tag;
1036 cm->ingress_vlan_hdr_cnt++;
1037 }
1038
1039 if ((secondary_ingress_vlan_tag & VLAN_VID_MASK) != SFE_VLAN_ID_NOT_CONFIGURED) {
1040 tpid = (u16)(secondary_ingress_vlan_tag >> 16);
1041 cm->ingress_vlan_hdr[1].tpid = ntohs(tpid);
1042 cm->ingress_vlan_hdr[1].tci = (u16)secondary_ingress_vlan_tag;
1043 cm->ingress_vlan_hdr_cnt++;
1044 }
1045
1046 if ((primary_egress_vlan_tag & VLAN_VID_MASK) != SFE_VLAN_ID_NOT_CONFIGURED) {
1047 tpid = (u16)(primary_egress_vlan_tag >> 16);
1048 cm->egress_vlan_hdr[0].tpid = ntohs(tpid);
1049 cm->egress_vlan_hdr[0].tci = (u16)primary_egress_vlan_tag;
1050 cm->egress_vlan_hdr_cnt++;
1051 }
1052
1053 if ((secondary_egress_vlan_tag & VLAN_VID_MASK) != SFE_VLAN_ID_NOT_CONFIGURED) {
1054 tpid = (u16)(secondary_egress_vlan_tag >> 16);
1055 cm->egress_vlan_hdr[1].tpid = ntohs(tpid);
1056 cm->egress_vlan_hdr[1].tci = (u16)secondary_egress_vlan_tag;
1057 cm->egress_vlan_hdr_cnt++;
1058 }
1059}
1060
1061/*
Xiaoping Fan978b3772015-05-27 14:15:18 -07001062 * sfe_ipv6_update_rule()
1063 * update forwarding rule after rule is created.
1064 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301065void sfe_ipv6_update_rule(struct sfe_ipv6_rule_create_msg *msg)
1066
Xiaoping Fan978b3772015-05-27 14:15:18 -07001067{
1068 struct sfe_ipv6_connection *c;
1069 struct sfe_ipv6 *si = &__si6;
1070
1071 spin_lock_bh(&si->lock);
1072
1073 c = sfe_ipv6_find_connection(si,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301074 msg->tuple.protocol,
1075 (struct sfe_ipv6_addr *)msg->tuple.flow_ip,
1076 msg->tuple.flow_ident,
1077 (struct sfe_ipv6_addr *)msg->tuple.return_ip,
1078 msg->tuple.return_ident);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001079 if (c != NULL) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301080 sfe_ipv6_update_protocol_state(c, msg);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001081 }
1082
1083 spin_unlock_bh(&si->lock);
1084}
1085
1086/*
Murat Sezginef190392022-04-04 17:56:31 -07001087 * sfe_ipv6_mark_rule_update()
1088 * Updates the mark values of match entries.
1089 */
1090void sfe_ipv6_mark_rule_update(struct sfe_connection_mark *mark)
1091{
1092 struct sfe_ipv6_connection *c;
1093 struct sfe_ipv6 *si = &__si6;
1094
1095 spin_lock_bh(&si->lock);
1096 c = sfe_ipv6_find_connection(si, mark->protocol,
1097 (struct sfe_ipv6_addr *)mark->src_ip,
1098 mark->src_port,
1099 (struct sfe_ipv6_addr *)mark->dest_ip,
1100 mark->dest_port);
1101 if (!c) {
1102 spin_unlock_bh(&si->lock);
1103 DEBUG_WARN("%px: connection not found for mark update\n", mark);
1104 return;
1105 }
1106 c->original_match ->mark = mark->mark;
1107 c->reply_match->mark = mark->mark;
1108 spin_unlock_bh(&si->lock);
1109 DEBUG_TRACE("%px: connection mark updated with %d\n", mark, mark->mark);
1110}
1111EXPORT_SYMBOL(sfe_ipv6_mark_rule_update);
1112
1113/*
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +05301114 * sfe_ipv6_xmit_eth_type_check
1115 * Checking if MAC header has to be written.
1116 */
1117static inline bool sfe_ipv6_xmit_eth_type_check(struct net_device *dev, u32 cm_flags)
1118{
1119 if (!(dev->flags & IFF_NOARP)) {
1120 return true;
1121 }
1122
1123 /*
1124 * For PPPoE, since we are now supporting PPPoE encapsulation, we are writing L2 header.
1125 */
1126 if (cm_flags & SFE_IPV6_CONNECTION_MATCH_FLAG_PPPOE_ENCAP) {
1127 return true;
1128 }
1129
1130 return false;
1131}
1132
1133/*
Xiaoping Fan978b3772015-05-27 14:15:18 -07001134 * sfe_ipv6_create_rule()
1135 * Create a forwarding rule.
1136 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301137int sfe_ipv6_create_rule(struct sfe_ipv6_rule_create_msg *msg)
Xiaoping Fan978b3772015-05-27 14:15:18 -07001138{
1139 struct sfe_ipv6 *si = &__si6;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301140 struct sfe_ipv6_connection *c, *old_c;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001141 struct sfe_ipv6_connection_match *original_cm;
1142 struct sfe_ipv6_connection_match *reply_cm;
1143 struct net_device *dest_dev;
1144 struct net_device *src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301145 struct sfe_ipv6_5tuple *tuple = &msg->tuple;
Suruchi Suman23a279d2021-11-16 15:13:09 +05301146 struct sock *sk;
1147 struct net *net;
1148 unsigned int src_if_idx;
1149
Suruchi Sumanc1a4a612021-10-21 14:50:23 +05301150 s32 flow_interface_num = msg->conn_rule.flow_top_interface_num;
1151 s32 return_interface_num = msg->conn_rule.return_top_interface_num;
Parikshit Guned31a8202022-01-05 22:15:04 +05301152 u32 flow_sawf_tag;
1153 u32 return_sawf_tag;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001154
Suruchi Sumanc1a4a612021-10-21 14:50:23 +05301155 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_FLOW_BOTTOM_INTERFACE) {
1156 flow_interface_num = msg->conn_rule.flow_interface_num;
1157 }
1158
1159 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_RETURN_BOTTOM_INTERFACE) {
1160 return_interface_num = msg->conn_rule.return_interface_num;
1161 }
1162
1163 src_dev = dev_get_by_index(&init_net, flow_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301164 if (!src_dev) {
1165 DEBUG_WARN("%px: Unable to find src_dev corresponding to %d\n", msg,
Suruchi Sumanc1a4a612021-10-21 14:50:23 +05301166 flow_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301167 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
1168 return -EINVAL;
1169 }
1170
Suruchi Sumanc1a4a612021-10-21 14:50:23 +05301171 dest_dev = dev_get_by_index(&init_net, return_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301172 if (!dest_dev) {
1173 DEBUG_WARN("%px: Unable to find dest_dev corresponding to %d\n", msg,
Suruchi Sumanc1a4a612021-10-21 14:50:23 +05301174 return_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301175 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
1176 dev_put(src_dev);
1177 return -EINVAL;
1178 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07001179
1180 if (unlikely((dest_dev->reg_state != NETREG_REGISTERED) ||
1181 (src_dev->reg_state != NETREG_REGISTERED))) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301182 DEBUG_WARN("%px: src_dev=%s and dest_dev=%s are unregistered\n", msg,
1183 src_dev->name, dest_dev->name);
1184 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
1185 dev_put(src_dev);
1186 dev_put(dest_dev);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001187 return -EINVAL;
1188 }
1189
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301190 /*
1191 * Allocate the various connection tracking objects.
1192 */
Parikshit Guneef1664c2022-03-24 14:15:42 +05301193 c = (struct sfe_ipv6_connection *)kzalloc(sizeof(struct sfe_ipv6_connection), GFP_ATOMIC);
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301194 if (unlikely(!c)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301195 DEBUG_WARN("%px: memory allocation of connection entry failed\n", msg);
1196 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
1197 dev_put(src_dev);
1198 dev_put(dest_dev);
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301199 return -ENOMEM;
1200 }
1201
Parikshit Guneef1664c2022-03-24 14:15:42 +05301202 original_cm = (struct sfe_ipv6_connection_match *)kzalloc(sizeof(struct sfe_ipv6_connection_match), GFP_ATOMIC);
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301203 if (unlikely(!original_cm)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301204 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
1205 DEBUG_WARN("%px: memory allocation of connection match entry failed\n", msg);
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301206 kfree(c);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301207 dev_put(src_dev);
1208 dev_put(dest_dev);
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301209 return -ENOMEM;
1210 }
1211
Parikshit Guneef1664c2022-03-24 14:15:42 +05301212 reply_cm = (struct sfe_ipv6_connection_match *)kzalloc(sizeof(struct sfe_ipv6_connection_match), GFP_ATOMIC);
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301213 if (unlikely(!reply_cm)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301214 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
1215 DEBUG_WARN("%px: memory allocation of connection match entry failed\n", msg);
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301216 kfree(original_cm);
1217 kfree(c);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301218 dev_put(src_dev);
1219 dev_put(dest_dev);
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301220 return -ENOMEM;
1221 }
1222
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301223 this_cpu_inc(si->stats_pcpu->connection_create_requests64);
1224
Xiaoping Fan978b3772015-05-27 14:15:18 -07001225 spin_lock_bh(&si->lock);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001226
1227 /*
1228 * Check to see if there is already a flow that matches the rule we're
1229 * trying to create. If there is then we can't create a new one.
1230 */
Wayne Tanbb7f1782021-12-13 11:16:04 -08001231 old_c = sfe_ipv6_find_connection(si,
1232 tuple->protocol,
1233 (struct sfe_ipv6_addr *)tuple->flow_ip,
1234 tuple->flow_ident,
1235 (struct sfe_ipv6_addr *)tuple->return_ip,
1236 tuple->return_ident);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301237
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301238 if (old_c != NULL) {
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301239 this_cpu_inc(si->stats_pcpu->connection_create_collisions64);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001240
1241 /*
1242 * If we already have the flow then it's likely that this
1243 * request to create the connection rule contains more
1244 * up-to-date information. Check and update accordingly.
1245 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301246 sfe_ipv6_update_protocol_state(old_c, msg);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001247 spin_unlock_bh(&si->lock);
1248
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301249 kfree(reply_cm);
1250 kfree(original_cm);
1251 kfree(c);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301252 dev_put(src_dev);
1253 dev_put(dest_dev);
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301254
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301255 DEBUG_TRACE("connection already exists - p: %d\n"
Tian Yang45f39c82020-10-06 14:07:47 -07001256 " s: %s:%pxM:%pI6:%u, d: %s:%pxM:%pI6:%u\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301257 tuple->protocol,
1258 src_dev->name, msg->conn_rule.flow_mac, tuple->flow_ip, ntohs(tuple->flow_ident),
1259 dest_dev->name, msg->conn_rule.return_mac, tuple->return_ip, ntohs(tuple->return_ident));
Xiaoping Fan978b3772015-05-27 14:15:18 -07001260 return -EADDRINUSE;
1261 }
1262
1263 /*
Xiaoping Fan978b3772015-05-27 14:15:18 -07001264 * Fill in the "original" direction connection matching object.
1265 * Note that the transmit MAC address is "dest_mac_xlate" because
1266 * we always know both ends of a connection by their translated
1267 * addresses and not their public addresses.
1268 */
1269 original_cm->match_dev = src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301270 original_cm->match_protocol = tuple->protocol;
1271 original_cm->match_src_ip[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip;
Suruchi Suman66609a72022-01-20 02:34:25 +05301272 original_cm->match_src_port = netif_is_vxlan(src_dev) ? 0 : tuple->flow_ident;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301273 original_cm->match_dest_ip[0] = *(struct sfe_ipv6_addr *)tuple->return_ip;
1274 original_cm->match_dest_port = tuple->return_ident;
1275
1276 original_cm->xlate_src_ip[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip;
1277 original_cm->xlate_src_port = tuple->flow_ident;
1278 original_cm->xlate_dest_ip[0] = *(struct sfe_ipv6_addr *)tuple->return_ip;
1279 original_cm->xlate_dest_port = tuple->return_ident;
1280
Xiaoping Fan978b3772015-05-27 14:15:18 -07001281 original_cm->xmit_dev = dest_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301282
1283 original_cm->xmit_dev_mtu = msg->conn_rule.return_mtu;
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301284
Xiaoping Fan978b3772015-05-27 14:15:18 -07001285 original_cm->connection = c;
1286 original_cm->counter_match = reply_cm;
Suruchi Suman23a279d2021-11-16 15:13:09 +05301287
1288 /*
1289 * Valid in decap direction only
1290 */
1291 RCU_INIT_POINTER(original_cm->up, NULL);
1292
Ken Zhu37040ea2021-09-09 21:11:15 -07001293 if (msg->valid_flags & SFE_RULE_CREATE_MARK_VALID) {
1294 original_cm->mark = msg->mark_rule.flow_mark;
1295 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_MARK;
1296 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301297 if (msg->valid_flags & SFE_RULE_CREATE_QOS_VALID) {
1298 original_cm->priority = msg->qos_rule.flow_qos_tag;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001299 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_PRIORITY_REMARK;
1300 }
Wayne Tanbb7f1782021-12-13 11:16:04 -08001301
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301302 if (msg->valid_flags & SFE_RULE_CREATE_DSCP_MARKING_VALID) {
1303 original_cm->dscp = msg->dscp_rule.flow_dscp << SFE_IPV6_DSCP_SHIFT;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001304 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_DSCP_REMARK;
1305 }
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301306 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1307 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_BRIDGE_FLOW;
1308 }
Ken Zhu7e38d1a2021-11-30 17:31:46 -08001309 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_FLOW_TRANSMIT_FAST) {
1310 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_FAST_XMIT_DEV_ADMISSION;
1311 }
1312
Parikshit Guned31a8202022-01-05 22:15:04 +05301313 /*
1314 * Mark SAWF metadata if the sawf tag is valid.
1315 */
1316 original_cm->sawf_valid = false;
1317 flow_sawf_tag = SFE_GET_SAWF_TAG(msg->sawf_rule.flow_mark);
1318 if (likely(SFE_SAWF_TAG_IS_VALID(flow_sawf_tag))) {
1319 original_cm->mark = msg->sawf_rule.flow_mark;
1320 original_cm->sawf_valid = true;
1321 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_MARK;
1322 }
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301323
Wayne Tanbb7f1782021-12-13 11:16:04 -08001324 /*
1325 * Add VLAN rule to original_cm
1326 */
1327 if (msg->valid_flags & SFE_RULE_CREATE_VLAN_VALID) {
1328 struct sfe_vlan_rule *vlan_primary_rule = &msg->vlan_primary_rule;
1329 struct sfe_vlan_rule *vlan_secondary_rule = &msg->vlan_secondary_rule;
1330 sfe_ipv6_match_entry_set_vlan(original_cm,
1331 vlan_primary_rule->ingress_vlan_tag,
1332 vlan_primary_rule->egress_vlan_tag,
1333 vlan_secondary_rule->ingress_vlan_tag,
1334 vlan_secondary_rule->egress_vlan_tag);
1335
1336 if ((msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_RETURN_BOTTOM_INTERFACE) &&
1337 original_cm->egress_vlan_hdr_cnt > 0) {
1338 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_INSERT_EGRESS_VLAN_TAG;
1339 original_cm->l2_hdr_size += original_cm->egress_vlan_hdr_cnt * VLAN_HLEN;
1340 }
1341 }
1342
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301343 if ((IPPROTO_GRE == tuple->protocol) && !sfe_ipv6_is_local_ip(si, (uint8_t *)original_cm->match_dest_ip)) {
1344 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_PASSTHROUGH;
1345 }
1346
Xiaoping Fan978b3772015-05-27 14:15:18 -07001347#ifdef CONFIG_NF_FLOW_COOKIE
1348 original_cm->flow_cookie = 0;
1349#endif
Zhi Chen8748eb32015-06-18 12:58:48 -07001350#ifdef CONFIG_XFRM
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301351 if (msg->valid_flags & SFE_RULE_CREATE_DIRECTION_VALID) {
1352 original_cm->flow_accel = msg->direction_rule.flow_accel;
1353 } else {
1354 original_cm->flow_accel = 1;
1355 }
Zhi Chen8748eb32015-06-18 12:58:48 -07001356#endif
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301357 /*
1358 * If l2_features are disabled and flow uses l2 features such as macvlan/bridge/pppoe/vlan,
1359 * bottom interfaces are expected to be disabled in the flow rule and always top interfaces
1360 * are used. In such cases, do not use HW csum offload. csum offload is used only when we
1361 * are sending directly to the destination interface that supports it.
1362 */
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301363 if (likely(dest_dev->features & NETIF_F_HW_CSUM) && sfe_dev_has_hw_csum(dest_dev)) {
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301364 if ((msg->conn_rule.return_top_interface_num == msg->conn_rule.return_interface_num) ||
1365 (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_RETURN_BOTTOM_INTERFACE)) {
Ratheesh Kannoth48445532022-02-07 16:19:00 +05301366 /*
1367 * Dont enable CSUM offload
1368 */
1369#if 0
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301370 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_CSUM_OFFLOAD;
Ratheesh Kannoth48445532022-02-07 16:19:00 +05301371#endif
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301372 }
1373 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07001374
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +05301375 /*
1376 * Adding PPPoE parameters to original and reply entries based on the direction where
1377 * PPPoE header is valid in ECM rule.
1378 *
1379 * If PPPoE is valid in flow direction (from interface is PPPoE), then
1380 * original cm will have PPPoE at ingress (strip PPPoE header)
1381 * reply cm will have PPPoE at egress (add PPPoE header)
1382 *
1383 * If PPPoE is valid in return direction (to interface is PPPoE), then
1384 * original cm will have PPPoE at egress (add PPPoE header)
1385 * reply cm will have PPPoE at ingress (strip PPPoE header)
1386 */
1387 if (msg->valid_flags & SFE_RULE_CREATE_PPPOE_DECAP_VALID) {
1388 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_PPPOE_DECAP;
1389 original_cm->pppoe_session_id = msg->pppoe_rule.flow_pppoe_session_id;
1390 ether_addr_copy(original_cm->pppoe_remote_mac, msg->pppoe_rule.flow_pppoe_remote_mac);
1391
1392 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_PPPOE_ENCAP;
Wayne Tanbb7f1782021-12-13 11:16:04 -08001393 reply_cm->l2_hdr_size += SFE_PPPOE_SESSION_HEADER_SIZE;
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +05301394 reply_cm->pppoe_session_id = msg->pppoe_rule.flow_pppoe_session_id;
1395 ether_addr_copy(reply_cm->pppoe_remote_mac, msg->pppoe_rule.flow_pppoe_remote_mac);
1396 }
1397
1398 if (msg->valid_flags & SFE_RULE_CREATE_PPPOE_ENCAP_VALID) {
1399 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_PPPOE_ENCAP;
Wayne Tanbb7f1782021-12-13 11:16:04 -08001400 original_cm->l2_hdr_size += SFE_PPPOE_SESSION_HEADER_SIZE;
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +05301401 original_cm->pppoe_session_id = msg->pppoe_rule.return_pppoe_session_id;
1402 ether_addr_copy(original_cm->pppoe_remote_mac, msg->pppoe_rule.return_pppoe_remote_mac);
1403
1404 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_PPPOE_DECAP;
1405 reply_cm->pppoe_session_id = msg->pppoe_rule.return_pppoe_session_id;
1406 ether_addr_copy(reply_cm->pppoe_remote_mac, msg->pppoe_rule.return_pppoe_remote_mac);
1407 }
1408
Murat Sezgin9c538972022-05-17 13:33:17 -07001409 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_FLOW_SRC_INTERFACE_CHECK) {
Ratheesh Kannoth5dee3772022-01-18 11:27:14 +05301410 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_SRC_INTERFACE_CHECK;
1411 }
1412
Murat Sezgin9c538972022-05-17 13:33:17 -07001413 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_FLOW_SRC_INTERFACE_CHECK_NO_FLUSH) {
1414 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_SRC_INTERFACE_CHECK_NO_FLUSH;
1415 }
1416
Xiaoping Fan978b3772015-05-27 14:15:18 -07001417 /*
Ken Zhubbf49652021-09-12 15:33:09 -07001418 * For the non-arp interface, we don't write L2 HDR.
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +05301419 * Excluding PPPoE from this, since we are now supporting PPPoE encap/decap.
Xiaoping Fan978b3772015-05-27 14:15:18 -07001420 */
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +05301421 if (sfe_ipv6_xmit_eth_type_check(dest_dev, original_cm->flags)) {
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301422
1423 /*
1424 * Check whether the rule has configured a specific source MAC address to use.
1425 * This is needed when virtual L3 interfaces such as br-lan, macvlan, vlan are used during egress
1426 */
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301427 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1428 ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)msg->conn_rule.flow_mac);
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301429 } else {
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301430 if ((msg->valid_flags & SFE_RULE_CREATE_SRC_MAC_VALID) &&
1431 (msg->src_mac_rule.mac_valid_flags & SFE_SRC_MAC_RETURN_VALID)) {
1432 ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)msg->src_mac_rule.return_src_mac);
1433 } else {
1434 ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)dest_dev->dev_addr);
1435 }
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301436 }
1437 ether_addr_copy((u8 *)original_cm->xmit_dest_mac, (u8 *)msg->conn_rule.return_mac);
1438
Xiaoping Fan978b3772015-05-27 14:15:18 -07001439 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_WRITE_L2_HDR;
Wayne Tanbb7f1782021-12-13 11:16:04 -08001440 original_cm->l2_hdr_size += ETH_HLEN;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001441
1442 /*
1443 * If our dev writes Ethernet headers then we can write a really fast
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301444 * version
Xiaoping Fan978b3772015-05-27 14:15:18 -07001445 */
1446 if (dest_dev->header_ops) {
1447 if (dest_dev->header_ops->create == eth_header) {
1448 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR;
1449 }
1450 }
1451 }
1452
1453 /*
1454 * Fill in the "reply" direction connection matching object.
1455 */
1456 reply_cm->match_dev = dest_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301457 reply_cm->match_protocol = tuple->protocol;
1458 reply_cm->match_src_ip[0] = *(struct sfe_ipv6_addr *)tuple->return_ip;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301459 reply_cm->match_dest_ip[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip;
1460 reply_cm->match_dest_port = tuple->flow_ident;
1461 reply_cm->xlate_src_ip[0] = *(struct sfe_ipv6_addr *)tuple->return_ip;
1462 reply_cm->xlate_src_port = tuple->return_ident;
1463 reply_cm->xlate_dest_ip[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip;
1464 reply_cm->xlate_dest_port = tuple->flow_ident;
1465
Suruchi Suman23a279d2021-11-16 15:13:09 +05301466 /*
1467 * Keep source port as 0 for VxLAN tunnels.
1468 */
1469 if (netif_is_vxlan(src_dev) || netif_is_vxlan(dest_dev)) {
1470 reply_cm->match_src_port = 0;
1471 } else {
1472 reply_cm->match_src_port = tuple->return_ident;
1473 }
1474
Xiaoping Fan978b3772015-05-27 14:15:18 -07001475 reply_cm->xmit_dev = src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301476 reply_cm->xmit_dev_mtu = msg->conn_rule.flow_mtu;
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301477
Xiaoping Fan978b3772015-05-27 14:15:18 -07001478 reply_cm->connection = c;
1479 reply_cm->counter_match = original_cm;
Suruchi Suman23a279d2021-11-16 15:13:09 +05301480
Ken Zhu37040ea2021-09-09 21:11:15 -07001481 if (msg->valid_flags & SFE_RULE_CREATE_MARK_VALID) {
1482 reply_cm->mark = msg->mark_rule.return_mark;
1483 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_MARK;
1484 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301485 if (msg->valid_flags & SFE_RULE_CREATE_QOS_VALID) {
1486 reply_cm->priority = msg->qos_rule.return_qos_tag;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001487 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_PRIORITY_REMARK;
1488 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301489 if (msg->valid_flags & SFE_RULE_CREATE_DSCP_MARKING_VALID) {
1490 reply_cm->dscp = msg->dscp_rule.return_dscp << SFE_IPV6_DSCP_SHIFT;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001491 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_DSCP_REMARK;
1492 }
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301493 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1494 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_BRIDGE_FLOW;
1495 }
Ken Zhu7e38d1a2021-11-30 17:31:46 -08001496 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_RETURN_TRANSMIT_FAST) {
1497 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_FAST_XMIT_DEV_ADMISSION;
1498 }
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301499
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301500 if ((IPPROTO_GRE == tuple->protocol) && !sfe_ipv6_is_local_ip(si, (uint8_t *)reply_cm->match_dest_ip)) {
1501 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_PASSTHROUGH;
1502 }
1503
Suruchi Suman23a279d2021-11-16 15:13:09 +05301504 /*
Parikshit Guned31a8202022-01-05 22:15:04 +05301505 * Mark return SAWF metadata if the sawf tag is valid.
1506 */
1507 reply_cm->sawf_valid = false;
1508 return_sawf_tag = SFE_GET_SAWF_TAG(msg->sawf_rule.return_mark);
1509 if (likely(SFE_SAWF_TAG_IS_VALID(return_sawf_tag))) {
1510 reply_cm->mark = msg->sawf_rule.return_mark;
1511 reply_cm->sawf_valid = true;
1512 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_MARK;
1513 }
1514
1515 /*
Suruchi Suman23a279d2021-11-16 15:13:09 +05301516 * Setup UDP Socket if found to be valid for decap.
1517 */
1518 RCU_INIT_POINTER(reply_cm->up, NULL);
1519 net = dev_net(reply_cm->match_dev);
1520 src_if_idx = src_dev->ifindex;
1521
1522 rcu_read_lock();
1523
1524 /*
1525 * Look for the associated sock object.
1526 * __udp6_lib_lookup() holds a reference for this sock object,
1527 * which will be released in sfe_ipv6_flush_connection()
1528 */
1529#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
1530 sk = __udp6_lib_lookup(net, (const struct in6_addr *)reply_cm->match_dest_ip,
1531 reply_cm->match_dest_port, (const struct in6_addr *)reply_cm->xlate_src_ip,
1532 reply_cm->xlate_src_port, src_if_idx, &udp_table);
1533#else
1534 sk = __udp6_lib_lookup(net, (const struct in6_addr *)reply_cm->match_dest_ip,
1535 reply_cm->match_dest_port, (const struct in6_addr *)reply_cm->xlate_src_ip,
1536 reply_cm->xlate_src_port, src_if_idx, 0, &udp_table, NULL);
1537#endif
1538 rcu_read_unlock();
1539
1540 /*
1541 * We set the UDP sock pointer as valid only for decap direction.
1542 */
1543 if (sk && udp_sk(sk)->encap_type) {
1544#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
1545 if (!atomic_add_unless(&sk->sk_refcnt, 1, 0)) {
1546#else
1547 if (!refcount_inc_not_zero(&sk->sk_refcnt)) {
1548#endif
Tian Yang435afc42022-02-02 12:47:32 -08001549 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
Wayne Tanbb7f1782021-12-13 11:16:04 -08001550 spin_unlock_bh(&si->lock);
Suruchi Suman23a279d2021-11-16 15:13:09 +05301551 kfree(reply_cm);
1552 kfree(original_cm);
1553 kfree(c);
1554
1555 DEBUG_INFO("sfe: unable to take reference for socket p:%d\n", tuple->protocol);
1556 DEBUG_INFO("SK: connection - \n"
1557 " s: %s:%pI6(%pI6):%u(%u)\n"
1558 " d: %s:%pI6(%pI6):%u(%u)\n",
1559 reply_cm->match_dev->name, &reply_cm->match_src_ip, &reply_cm->xlate_src_ip,
1560 ntohs(reply_cm->match_src_port), ntohs(reply_cm->xlate_src_port),
1561 reply_cm->xmit_dev->name, &reply_cm->match_dest_ip, &reply_cm->xlate_dest_ip,
1562 ntohs(reply_cm->match_dest_port), ntohs(reply_cm->xlate_dest_port));
1563
1564 dev_put(src_dev);
1565 dev_put(dest_dev);
1566
1567 return -ESHUTDOWN;
1568 }
1569
1570 rcu_assign_pointer(reply_cm->up, udp_sk(sk));
1571 DEBUG_INFO("Sock lookup success with reply_cm direction(%p)\n", sk);
1572 DEBUG_INFO("SK: connection - \n"
1573 " s: %s:%pI6(%pI6):%u(%u)\n"
1574 " d: %s:%pI6(%pI6):%u(%u)\n",
1575 reply_cm->match_dev->name, &reply_cm->match_src_ip, &reply_cm->xlate_src_ip,
1576 ntohs(reply_cm->match_src_port), ntohs(reply_cm->xlate_src_port),
1577 reply_cm->xmit_dev->name, &reply_cm->match_dest_ip, &reply_cm->xlate_dest_ip,
1578 ntohs(reply_cm->match_dest_port), ntohs(reply_cm->xlate_dest_port));
1579 }
1580
Wayne Tanbb7f1782021-12-13 11:16:04 -08001581 /*
1582 * Add VLAN rule to reply_cm
1583 */
1584 if (msg->valid_flags & SFE_RULE_CREATE_VLAN_VALID) {
1585 struct sfe_vlan_rule *vlan_primary_rule = &msg->vlan_primary_rule;
1586 struct sfe_vlan_rule *vlan_secondary_rule = &msg->vlan_secondary_rule;
1587 sfe_ipv6_match_entry_set_vlan(reply_cm,
1588 vlan_primary_rule->egress_vlan_tag,
1589 vlan_primary_rule->ingress_vlan_tag,
1590 vlan_secondary_rule->egress_vlan_tag,
1591 vlan_secondary_rule->ingress_vlan_tag);
1592
1593 if ((msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_FLOW_BOTTOM_INTERFACE) &&
1594 reply_cm->egress_vlan_hdr_cnt > 0) {
1595 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_INSERT_EGRESS_VLAN_TAG;
1596 reply_cm->l2_hdr_size += reply_cm->egress_vlan_hdr_cnt * VLAN_HLEN;
1597 }
1598 }
1599
Xiaoping Fan978b3772015-05-27 14:15:18 -07001600#ifdef CONFIG_NF_FLOW_COOKIE
1601 reply_cm->flow_cookie = 0;
1602#endif
Zhi Chen8748eb32015-06-18 12:58:48 -07001603#ifdef CONFIG_XFRM
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301604 if (msg->valid_flags & SFE_RULE_CREATE_DIRECTION_VALID) {
1605 reply_cm->flow_accel = msg->direction_rule.return_accel;
1606 } else {
1607 reply_cm->flow_accel = 1;
1608 }
Zhi Chen8748eb32015-06-18 12:58:48 -07001609#endif
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301610
1611 /*
1612 * the inet6_protocol handler will be used only in decap path
1613 * for non passthrough case.
1614 */
1615 original_cm->proto = NULL;
1616 reply_cm->proto = NULL;
Tian Yang435afc42022-02-02 12:47:32 -08001617 original_cm->top_interface_dev = NULL;
1618 reply_cm->top_interface_dev = NULL;
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301619
1620#ifdef SFE_GRE_TUN_ENABLE
1621 if (!(reply_cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_PASSTHROUGH)) {
1622 rcu_read_lock();
1623 reply_cm->proto = rcu_dereference(inet6_protos[tuple->protocol]);
1624 rcu_read_unlock();
1625
1626 if (unlikely(!reply_cm->proto)) {
Tian Yang435afc42022-02-02 12:47:32 -08001627 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
1628 spin_unlock_bh(&si->lock);
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301629 kfree(reply_cm);
1630 kfree(original_cm);
1631 kfree(c);
1632 dev_put(src_dev);
1633 dev_put(dest_dev);
1634 DEBUG_WARN("sfe: GRE proto handler is not registered\n");
1635 return -EPERM;
1636 }
1637 }
1638#endif
1639
Xiaoping Fan978b3772015-05-27 14:15:18 -07001640 /*
Tian Yangafb03452022-01-13 18:53:13 -08001641 * Decapsulation path have proto set.
1642 * This is used to differentiate de/encap, and call protocol specific handler.
1643 */
1644 if (IPPROTO_IPIP == tuple->protocol) {
1645 original_cm->proto = NULL;
1646 rcu_read_lock();
1647 reply_cm->proto = rcu_dereference(inet6_protos[tuple->protocol]);
1648 rcu_read_unlock();
Tian Yang435afc42022-02-02 12:47:32 -08001649 reply_cm->top_interface_dev = dev_get_by_index(&init_net, msg->conn_rule.return_top_interface_num);
1650
1651 if (unlikely(!reply_cm->top_interface_dev)) {
1652 DEBUG_WARN("%px: Unable to find top_interface_dev corresponding to %d\n", msg,
1653 msg->conn_rule.return_top_interface_num);
1654 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
1655 spin_unlock_bh(&si->lock);
1656 kfree(reply_cm);
1657 kfree(original_cm);
1658 kfree(c);
1659 dev_put(src_dev);
1660 dev_put(dest_dev);
1661 return -EINVAL;
1662 }
Tian Yangafb03452022-01-13 18:53:13 -08001663 }
1664 /*
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301665 * If l2_features are disabled and flow uses l2 features such as macvlan/bridge/pppoe/vlan,
1666 * bottom interfaces are expected to be disabled in the flow rule and always top interfaces
1667 * are used. In such cases, do not use HW csum offload. csum offload is used only when we
1668 * are sending directly to the destination interface that supports it.
1669 */
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301670 if (likely(src_dev->features & NETIF_F_HW_CSUM) && sfe_dev_has_hw_csum(src_dev)) {
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301671 if ((msg->conn_rule.flow_top_interface_num == msg->conn_rule.flow_interface_num) ||
1672 (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_FLOW_BOTTOM_INTERFACE)) {
Ratheesh Kannoth48445532022-02-07 16:19:00 +05301673 /*
1674 * Dont enable CSUM offload
1675 */
1676#if 0
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301677 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_CSUM_OFFLOAD;
Ratheesh Kannoth48445532022-02-07 16:19:00 +05301678#endif
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301679 }
1680 }
1681
Murat Sezgin9c538972022-05-17 13:33:17 -07001682 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_RETURN_SRC_INTERFACE_CHECK) {
Ratheesh Kannoth5dee3772022-01-18 11:27:14 +05301683 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_SRC_INTERFACE_CHECK;
1684 }
1685
Murat Sezgin9c538972022-05-17 13:33:17 -07001686 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_RETURN_SRC_INTERFACE_CHECK_NO_FLUSH) {
1687 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_SRC_INTERFACE_CHECK_NO_FLUSH;
1688 }
1689
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301690 /*
Ken Zhubbf49652021-09-12 15:33:09 -07001691 * For the non-arp interface, we don't write L2 HDR.
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +05301692 * Excluding PPPoE from this, since we are now supporting PPPoE encap/decap.
Xiaoping Fan978b3772015-05-27 14:15:18 -07001693 */
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +05301694 if (sfe_ipv6_xmit_eth_type_check(src_dev, reply_cm->flags)) {
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301695
1696 /*
1697 * Check whether the rule has configured a specific source MAC address to use.
1698 * This is needed when virtual L3 interfaces such as br-lan, macvlan, vlan are used during egress
1699 */
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301700 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1701 ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)msg->conn_rule.return_mac);
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301702 } else {
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301703 if ((msg->valid_flags & SFE_RULE_CREATE_SRC_MAC_VALID) &&
1704 (msg->src_mac_rule.mac_valid_flags & SFE_SRC_MAC_FLOW_VALID)) {
1705 ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)msg->src_mac_rule.flow_src_mac);
1706 } else {
1707 ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)src_dev->dev_addr);
1708 }
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301709 }
1710
1711 ether_addr_copy((u8 *)reply_cm->xmit_dest_mac, (u8 *)msg->conn_rule.flow_mac);
1712
Xiaoping Fan978b3772015-05-27 14:15:18 -07001713 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_WRITE_L2_HDR;
Wayne Tanbb7f1782021-12-13 11:16:04 -08001714 reply_cm->l2_hdr_size += ETH_HLEN;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001715
1716 /*
1717 * If our dev writes Ethernet headers then we can write a really fast
1718 * version.
1719 */
1720 if (src_dev->header_ops) {
1721 if (src_dev->header_ops->create == eth_header) {
1722 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR;
1723 }
1724 }
1725 }
1726
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301727 /*
1728 * No support for NAT in ipv6
1729 */
Xiaoping Fan978b3772015-05-27 14:15:18 -07001730
Xiaoping Fan978b3772015-05-27 14:15:18 -07001731 /*
Xiaoping Fan978b3772015-05-27 14:15:18 -07001732 * Initialize the protocol-specific information that we track.
1733 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301734 switch (tuple->protocol) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07001735 case IPPROTO_TCP:
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301736 original_cm->protocol_state.tcp.win_scale = msg->tcp_rule.flow_window_scale;
1737 original_cm->protocol_state.tcp.max_win = msg->tcp_rule.flow_max_window ? msg->tcp_rule.flow_max_window : 1;
1738 original_cm->protocol_state.tcp.end = msg->tcp_rule.flow_end;
1739 original_cm->protocol_state.tcp.max_end = msg->tcp_rule.flow_max_end;
1740 reply_cm->protocol_state.tcp.win_scale = msg->tcp_rule.return_window_scale;
1741 reply_cm->protocol_state.tcp.max_win = msg->tcp_rule.return_max_window ? msg->tcp_rule.return_max_window : 1;
1742 reply_cm->protocol_state.tcp.end = msg->tcp_rule.return_end;
1743 reply_cm->protocol_state.tcp.max_end = msg->tcp_rule.return_max_end;
1744 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_NO_SEQ_CHECK) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07001745 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
1746 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
1747 }
1748 break;
1749 }
1750
Wayne Tanbb7f1782021-12-13 11:16:04 -08001751 /*
1752 * Fill in the ipv6_connection object.
1753 */
1754 c->protocol = tuple->protocol;
1755 c->src_ip[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip;
1756 c->src_ip_xlate[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip;
1757 c->src_port = tuple->flow_ident;
1758 c->src_port_xlate = tuple->flow_ident;
1759 c->original_dev = src_dev;
1760 c->original_match = original_cm;
1761
1762 c->dest_ip[0] = *(struct sfe_ipv6_addr *)tuple->return_ip;
1763 c->dest_ip_xlate[0] = *(struct sfe_ipv6_addr *)tuple->return_ip;
1764 c->dest_port = tuple->return_ident;
1765 c->dest_port_xlate = tuple->return_ident;
1766
1767 c->reply_dev = dest_dev;
1768 c->reply_match = reply_cm;
1769 c->debug_read_seq = 0;
1770 c->last_sync_jiffies = get_jiffies_64();
1771 c->removed = false;
1772
Xiaoping Fan978b3772015-05-27 14:15:18 -07001773 sfe_ipv6_connection_match_compute_translations(original_cm);
1774 sfe_ipv6_connection_match_compute_translations(reply_cm);
1775 sfe_ipv6_insert_connection(si, c);
1776
1777 spin_unlock_bh(&si->lock);
1778
1779 /*
1780 * We have everything we need!
1781 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301782 DEBUG_INFO("new connection - p: %d\n"
Tian Yang45f39c82020-10-06 14:07:47 -07001783 " s: %s:%pxM(%pxM):%pI6(%pI6):%u(%u)\n"
1784 " d: %s:%pxM(%pxM):%pI6(%pI6):%u(%u)\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301785 tuple->protocol,
1786 src_dev->name, msg->conn_rule.flow_mac, NULL,
1787 (void *)tuple->flow_ip, (void *)tuple->flow_ip, ntohs(tuple->flow_ident), ntohs(tuple->flow_ident),
1788 dest_dev->name, NULL, msg->conn_rule.return_mac,
1789 (void *)tuple->return_ip, (void *)tuple->return_ip, ntohs(tuple->return_ident), ntohs(tuple->return_ident));
Xiaoping Fan978b3772015-05-27 14:15:18 -07001790
1791 return 0;
1792}
1793
1794/*
1795 * sfe_ipv6_destroy_rule()
1796 * Destroy a forwarding rule.
1797 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301798void sfe_ipv6_destroy_rule(struct sfe_ipv6_rule_destroy_msg *msg)
Xiaoping Fan978b3772015-05-27 14:15:18 -07001799{
1800 struct sfe_ipv6 *si = &__si6;
1801 struct sfe_ipv6_connection *c;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301802 bool ret;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301803 struct sfe_ipv6_5tuple *tuple = &msg->tuple;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001804
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301805 this_cpu_inc(si->stats_pcpu->connection_destroy_requests64);
1806
Xiaoping Fan978b3772015-05-27 14:15:18 -07001807 spin_lock_bh(&si->lock);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001808
1809 /*
1810 * Check to see if we have a flow that matches the rule we're trying
1811 * to destroy. If there isn't then we can't destroy it.
1812 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301813 c = sfe_ipv6_find_connection(si, tuple->protocol, (struct sfe_ipv6_addr *)tuple->flow_ip, tuple->flow_ident,
1814 (struct sfe_ipv6_addr *)tuple->return_ip, tuple->return_ident);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001815 if (!c) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07001816 spin_unlock_bh(&si->lock);
1817
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301818 this_cpu_inc(si->stats_pcpu->connection_destroy_misses64);
1819
Xiaoping Fan978b3772015-05-27 14:15:18 -07001820 DEBUG_TRACE("connection does not exist - p: %d, s: %pI6:%u, d: %pI6:%u\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301821 tuple->protocol, tuple->flow_ip, ntohs(tuple->flow_ident),
1822 tuple->return_ip, ntohs(tuple->return_ident));
Xiaoping Fan978b3772015-05-27 14:15:18 -07001823 return;
1824 }
1825
1826 /*
1827 * Remove our connection details from the hash tables.
1828 */
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301829 ret = sfe_ipv6_remove_connection(si, c);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001830 spin_unlock_bh(&si->lock);
1831
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301832 if (ret) {
1833 sfe_ipv6_flush_connection(si, c, SFE_SYNC_REASON_DESTROY);
1834 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07001835
1836 DEBUG_INFO("connection destroyed - p: %d, s: %pI6:%u, d: %pI6:%u\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301837 tuple->protocol, tuple->flow_ip, ntohs(tuple->flow_ident),
1838 tuple->return_ip, ntohs(tuple->return_ident));
Xiaoping Fan978b3772015-05-27 14:15:18 -07001839}
1840
1841/*
Ken Zhu7a43d882022-01-04 10:51:44 -08001842 * sfe_ipv6_sync_invoke()
1843 * Schedule many sync stats.
1844 */
1845bool sfe_ipv6_sync_invoke(uint16_t index)
1846{
1847 struct sfe_ipv6 *si = &__si6;
1848 return schedule_delayed_work_on(si->work_cpu, &(si->sync_dwork), 0);
1849}
1850
1851/*
Xiaoping Fan978b3772015-05-27 14:15:18 -07001852 * sfe_ipv6_register_sync_rule_callback()
1853 * Register a callback for rule synchronization.
1854 */
1855void sfe_ipv6_register_sync_rule_callback(sfe_sync_rule_callback_t sync_rule_callback)
1856{
1857 struct sfe_ipv6 *si = &__si6;
1858
1859 spin_lock_bh(&si->lock);
1860 rcu_assign_pointer(si->sync_rule_callback, sync_rule_callback);
1861 spin_unlock_bh(&si->lock);
1862}
1863
1864/*
Ken Zhu7a43d882022-01-04 10:51:44 -08001865 * sfe_ipv6_register_sync_rule_callback()
1866 * Register a callback for rule synchronization.
1867 */
1868void sfe_ipv6_register_many_sync_callback(sfe_ipv6_many_sync_callback_t cb)
1869{
1870 struct sfe_ipv6 *si = &__si6;
1871
1872 spin_lock_bh(&si->lock);
1873 rcu_assign_pointer(si->many_sync_callback, cb);
1874 spin_unlock_bh(&si->lock);
1875}
1876
1877/*
Xiaoping Fan978b3772015-05-27 14:15:18 -07001878 * sfe_ipv6_get_debug_dev()
1879 */
1880static ssize_t sfe_ipv6_get_debug_dev(struct device *dev,
1881 struct device_attribute *attr,
1882 char *buf)
1883{
1884 struct sfe_ipv6 *si = &__si6;
1885 ssize_t count;
1886 int num;
1887
1888 spin_lock_bh(&si->lock);
1889 num = si->debug_dev;
1890 spin_unlock_bh(&si->lock);
1891
1892 count = snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", num);
1893 return count;
1894}
1895
1896/*
1897 * sfe_ipv6_destroy_all_rules_for_dev()
1898 * Destroy all connections that match a particular device.
1899 *
1900 * If we pass dev as NULL then this destroys all connections.
1901 */
1902void sfe_ipv6_destroy_all_rules_for_dev(struct net_device *dev)
1903{
1904 struct sfe_ipv6 *si = &__si6;
1905 struct sfe_ipv6_connection *c;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301906 bool ret;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001907
Xiaoping Fan34586472015-07-03 02:20:35 -07001908another_round:
Xiaoping Fan978b3772015-05-27 14:15:18 -07001909 spin_lock_bh(&si->lock);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001910
Xiaoping Fan34586472015-07-03 02:20:35 -07001911 for (c = si->all_connections_head; c; c = c->all_connections_next) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07001912 /*
Xiaoping Fan34586472015-07-03 02:20:35 -07001913 * Does this connection relate to the device we are destroying?
Xiaoping Fan978b3772015-05-27 14:15:18 -07001914 */
1915 if (!dev
1916 || (dev == c->original_dev)
1917 || (dev == c->reply_dev)) {
Xiaoping Fan34586472015-07-03 02:20:35 -07001918 break;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001919 }
Xiaoping Fan34586472015-07-03 02:20:35 -07001920 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07001921
Xiaoping Fan34586472015-07-03 02:20:35 -07001922 if (c) {
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301923 ret = sfe_ipv6_remove_connection(si, c);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001924 }
1925
1926 spin_unlock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07001927
1928 if (c) {
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301929 if (ret) {
1930 sfe_ipv6_flush_connection(si, c, SFE_SYNC_REASON_DESTROY);
1931 }
Xiaoping Fan34586472015-07-03 02:20:35 -07001932 goto another_round;
1933 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07001934}
1935
1936/*
1937 * sfe_ipv6_periodic_sync()
1938 */
Ken Zhu137722d2021-09-23 17:57:36 -07001939static void sfe_ipv6_periodic_sync(struct work_struct *work)
Xiaoping Fan978b3772015-05-27 14:15:18 -07001940{
Ken Zhu137722d2021-09-23 17:57:36 -07001941 struct sfe_ipv6 *si = container_of((struct delayed_work *)work, struct sfe_ipv6, sync_dwork);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07001942 u64 now_jiffies;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001943 int quota;
Ken Zhu7a43d882022-01-04 10:51:44 -08001944 sfe_ipv6_many_sync_callback_t sync_rule_callback;
Ken Zhu32b95392021-09-03 13:52:04 -07001945 struct sfe_ipv6_connection *c;
Ken Zhu7a43d882022-01-04 10:51:44 -08001946 struct sfe_ipv6_conn_sync *conn_sync;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001947
1948 now_jiffies = get_jiffies_64();
1949
1950 rcu_read_lock();
Ken Zhu7a43d882022-01-04 10:51:44 -08001951 sync_rule_callback = rcu_dereference(si->many_sync_callback);
1952 rcu_read_unlock();
Xiaoping Fan978b3772015-05-27 14:15:18 -07001953 if (!sync_rule_callback) {
Ken Zhu7a43d882022-01-04 10:51:44 -08001954 return;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001955 }
1956
1957 spin_lock_bh(&si->lock);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001958
1959 /*
Ken Zhu32b95392021-09-03 13:52:04 -07001960 * If we have reached the end of the connection list, walk from
1961 * the connection head.
1962 */
1963 c = si->wc_next;
1964 if (unlikely(!c)) {
1965 c = si->all_connections_head;
1966 }
Ken Zhu7a43d882022-01-04 10:51:44 -08001967
Ken Zhu32b95392021-09-03 13:52:04 -07001968 /*
Ken Zhu7a43d882022-01-04 10:51:44 -08001969 * Get the max number of connections to be put in this sync msg.
Xiaoping Fan978b3772015-05-27 14:15:18 -07001970 */
Ken Zhu7a43d882022-01-04 10:51:44 -08001971 quota = sfe_ipv6_sync_max_number;
1972 conn_sync = sfe_ipv6_sync_many_msg->msg.conn_stats_many.conn_sync;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001973
1974 /*
Ken Zhu32b95392021-09-03 13:52:04 -07001975 * Walk the "all connection" list and sync the connection state.
Xiaoping Fan978b3772015-05-27 14:15:18 -07001976 */
Ken Zhu32b95392021-09-03 13:52:04 -07001977 while (likely(c && quota)) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07001978 struct sfe_ipv6_connection_match *cm;
1979 struct sfe_ipv6_connection_match *counter_cm;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001980 struct sfe_connection_sync sis;
1981
Ken Zhu32b95392021-09-03 13:52:04 -07001982 cm = c->original_match;
1983 counter_cm = c->reply_match;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001984
1985 /*
Ken Zhu32b95392021-09-03 13:52:04 -07001986 * Didn't receive packets in the origial direction or reply
1987 * direction, move to the next connection.
Xiaoping Fan978b3772015-05-27 14:15:18 -07001988 */
Ken Zhu32b95392021-09-03 13:52:04 -07001989 if (!atomic_read(&cm->rx_packet_count) && !atomic_read(&counter_cm->rx_packet_count)) {
1990 c = c->all_connections_next;
1991 continue;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001992 }
1993
Xiaoping Fan978b3772015-05-27 14:15:18 -07001994 /*
1995 * Sync the connection state.
1996 */
Xiaoping Fan99cb4c12015-08-21 19:07:32 -07001997 sfe_ipv6_gen_sync_connection(si, c, &sis, SFE_SYNC_REASON_STATS, now_jiffies);
Ken Zhu7a43d882022-01-04 10:51:44 -08001998 sfe_ipv6_stats_convert(conn_sync, &sis);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001999
Ken Zhu7a43d882022-01-04 10:51:44 -08002000 quota--;
2001 conn_sync++;
2002 c = c->all_connections_next;
Xiaoping Fan978b3772015-05-27 14:15:18 -07002003 }
2004
Ken Zhu32b95392021-09-03 13:52:04 -07002005 /*
2006 * At the end of loop, put wc_next to the connection we left
2007 */
2008 si->wc_next = c;
Xiaoping Fan978b3772015-05-27 14:15:18 -07002009 spin_unlock_bh(&si->lock);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002010
Ken Zhu7a43d882022-01-04 10:51:44 -08002011 if (c == NULL) {
2012 DEBUG_INFO("Synced all connections\n");
2013 sfe_ipv6_sync_many_msg->msg.conn_stats_many.next = 0;
2014 } else {
2015 DEBUG_INFO("Some connections left\n");
2016 sfe_ipv6_sync_many_msg->msg.conn_stats_many.next = sfe_ipv6_sync_max_number - quota;
2017 }
2018 DEBUG_INFO("Synced [%d] connections\n", (sfe_ipv6_sync_max_number - quota));
2019
2020 sfe_ipv6_sync_many_msg->msg.conn_stats_many.count = sfe_ipv6_sync_max_number - quota;
2021 sfe_ipv6_sync_many_msg->cm.response = SFE_CMN_RESPONSE_ACK;
2022
2023 sync_rule_callback(sfe_ipv6_sync_many_msg);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002024}
2025
2026/*
2027 * sfe_ipv6_debug_dev_read_start()
2028 * Generate part of the XML output.
2029 */
2030static bool sfe_ipv6_debug_dev_read_start(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
2031 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
2032{
2033 int bytes_read;
2034
Xiaoping Fan34586472015-07-03 02:20:35 -07002035 si->debug_read_seq++;
2036
Xiaoping Fan978b3772015-05-27 14:15:18 -07002037 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "<sfe_ipv6>\n");
2038 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2039 return false;
2040 }
2041
2042 *length -= bytes_read;
2043 *total_read += bytes_read;
2044
2045 ws->state++;
2046 return true;
2047}
2048
2049/*
2050 * sfe_ipv6_debug_dev_read_connections_start()
2051 * Generate part of the XML output.
2052 */
2053static bool sfe_ipv6_debug_dev_read_connections_start(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
2054 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
2055{
2056 int bytes_read;
2057
2058 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<connections>\n");
2059 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2060 return false;
2061 }
2062
2063 *length -= bytes_read;
2064 *total_read += bytes_read;
2065
2066 ws->state++;
2067 return true;
2068}
2069
2070/*
2071 * sfe_ipv6_debug_dev_read_connections_connection()
2072 * Generate part of the XML output.
2073 */
2074static bool sfe_ipv6_debug_dev_read_connections_connection(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
2075 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
2076{
2077 struct sfe_ipv6_connection *c;
Xiaoping Fan978b3772015-05-27 14:15:18 -07002078 struct sfe_ipv6_connection_match *original_cm;
2079 struct sfe_ipv6_connection_match *reply_cm;
2080 int bytes_read;
2081 int protocol;
2082 struct net_device *src_dev;
2083 struct sfe_ipv6_addr src_ip;
2084 struct sfe_ipv6_addr src_ip_xlate;
2085 __be16 src_port;
2086 __be16 src_port_xlate;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07002087 u64 src_rx_packets;
2088 u64 src_rx_bytes;
Xiaoping Fan978b3772015-05-27 14:15:18 -07002089 struct net_device *dest_dev;
2090 struct sfe_ipv6_addr dest_ip;
2091 struct sfe_ipv6_addr dest_ip_xlate;
2092 __be16 dest_port;
2093 __be16 dest_port_xlate;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07002094 u64 dest_rx_packets;
2095 u64 dest_rx_bytes;
2096 u64 last_sync_jiffies;
Ken Zhu37040ea2021-09-09 21:11:15 -07002097 u32 src_mark, dest_mark, src_priority, dest_priority, src_dscp, dest_dscp;
Parikshit Guned31a8202022-01-05 22:15:04 +05302098 bool original_cm_sawf_valid, reply_cm_sawf_valid;
2099 u32 flow_service_class, return_service_class;
2100 u32 flow_msduq, return_msduq;
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05302101 u32 packet, byte, original_cm_flags;
2102 u16 pppoe_session_id;
2103 u8 pppoe_remote_mac[ETH_ALEN];
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002104 u32 original_fast_xmit, reply_fast_xmit;
Xiaoping Fan978b3772015-05-27 14:15:18 -07002105#ifdef CONFIG_NF_FLOW_COOKIE
2106 int src_flow_cookie, dst_flow_cookie;
2107#endif
2108
2109 spin_lock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07002110
2111 for (c = si->all_connections_head; c; c = c->all_connections_next) {
2112 if (c->debug_read_seq < si->debug_read_seq) {
2113 c->debug_read_seq = si->debug_read_seq;
2114 break;
2115 }
2116 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07002117
2118 /*
Xiaoping Fan34586472015-07-03 02:20:35 -07002119 * If there were no connections then move to the next state.
Xiaoping Fan978b3772015-05-27 14:15:18 -07002120 */
2121 if (!c) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07002122 spin_unlock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07002123 ws->state++;
Xiaoping Fan978b3772015-05-27 14:15:18 -07002124 return true;
2125 }
2126
2127 original_cm = c->original_match;
2128 reply_cm = c->reply_match;
2129
2130 protocol = c->protocol;
2131 src_dev = c->original_dev;
2132 src_ip = c->src_ip[0];
2133 src_ip_xlate = c->src_ip_xlate[0];
2134 src_port = c->src_port;
2135 src_port_xlate = c->src_port_xlate;
Xiaoping Fane1963d42015-08-25 17:06:19 -07002136 src_priority = original_cm->priority;
2137 src_dscp = original_cm->dscp >> SFE_IPV6_DSCP_SHIFT;
Xiaoping Fan978b3772015-05-27 14:15:18 -07002138
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05302139 sfe_ipv6_connection_match_update_summary_stats(original_cm, &packet, &byte);
2140 sfe_ipv6_connection_match_update_summary_stats(reply_cm, &packet, &byte);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002141
2142 src_rx_packets = original_cm->rx_packet_count64;
2143 src_rx_bytes = original_cm->rx_byte_count64;
Ken Zhu37040ea2021-09-09 21:11:15 -07002144 src_mark = original_cm->mark;
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002145 original_fast_xmit = original_cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_FAST_XMIT;
Xiaoping Fan978b3772015-05-27 14:15:18 -07002146 dest_dev = c->reply_dev;
2147 dest_ip = c->dest_ip[0];
2148 dest_ip_xlate = c->dest_ip_xlate[0];
2149 dest_port = c->dest_port;
2150 dest_port_xlate = c->dest_port_xlate;
Xiaoping Fane1963d42015-08-25 17:06:19 -07002151 dest_priority = reply_cm->priority;
2152 dest_dscp = reply_cm->dscp >> SFE_IPV6_DSCP_SHIFT;
Xiaoping Fan978b3772015-05-27 14:15:18 -07002153 dest_rx_packets = reply_cm->rx_packet_count64;
2154 dest_rx_bytes = reply_cm->rx_byte_count64;
2155 last_sync_jiffies = get_jiffies_64() - c->last_sync_jiffies;
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05302156 original_cm_flags = original_cm->flags;
2157 pppoe_session_id = original_cm->pppoe_session_id;
2158 ether_addr_copy(pppoe_remote_mac, original_cm->pppoe_remote_mac);
Ken Zhu37040ea2021-09-09 21:11:15 -07002159 dest_mark = reply_cm->mark;
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002160 reply_fast_xmit = reply_cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_FAST_XMIT;
Parikshit Guned31a8202022-01-05 22:15:04 +05302161 original_cm_sawf_valid = original_cm->sawf_valid;
2162 reply_cm_sawf_valid = reply_cm->sawf_valid;
2163 flow_service_class = SFE_GET_SAWF_SERVICE_CLASS(original_cm->mark);
2164 flow_msduq = SFE_GET_SAWF_MSDUQ(original_cm->mark);
2165 return_service_class = SFE_GET_SAWF_SERVICE_CLASS(reply_cm->mark);
2166 return_msduq = SFE_GET_SAWF_MSDUQ(reply_cm->mark);
2167
Xiaoping Fan978b3772015-05-27 14:15:18 -07002168#ifdef CONFIG_NF_FLOW_COOKIE
2169 src_flow_cookie = original_cm->flow_cookie;
2170 dst_flow_cookie = reply_cm->flow_cookie;
2171#endif
2172 spin_unlock_bh(&si->lock);
2173
2174 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t\t<connection "
2175 "protocol=\"%u\" "
2176 "src_dev=\"%s\" "
2177 "src_ip=\"%pI6\" src_ip_xlate=\"%pI6\" "
2178 "src_port=\"%u\" src_port_xlate=\"%u\" "
Xiaoping Fane1963d42015-08-25 17:06:19 -07002179 "src_priority=\"%u\" src_dscp=\"%u\" "
Xiaoping Fan978b3772015-05-27 14:15:18 -07002180 "src_rx_pkts=\"%llu\" src_rx_bytes=\"%llu\" "
Ken Zhu37040ea2021-09-09 21:11:15 -07002181 "src_mark=\"%08x\" "
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002182 "src_fast_xmit=\"%s\" "
Xiaoping Fan978b3772015-05-27 14:15:18 -07002183 "dest_dev=\"%s\" "
2184 "dest_ip=\"%pI6\" dest_ip_xlate=\"%pI6\" "
2185 "dest_port=\"%u\" dest_port_xlate=\"%u\" "
Xiaoping Fane1963d42015-08-25 17:06:19 -07002186 "dest_priority=\"%u\" dest_dscp=\"%u\" "
Xiaoping Fan978b3772015-05-27 14:15:18 -07002187 "dest_rx_pkts=\"%llu\" dest_rx_bytes=\"%llu\" "
Ken Zhu37040ea2021-09-09 21:11:15 -07002188 "dest_mark=\"%08x\" "
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002189 "reply_fast_xmit=\"%s\" "
Xiaoping Fan978b3772015-05-27 14:15:18 -07002190#ifdef CONFIG_NF_FLOW_COOKIE
2191 "src_flow_cookie=\"%d\" dst_flow_cookie=\"%d\" "
2192#endif
Ken Zhu37040ea2021-09-09 21:11:15 -07002193 "last_sync=\"%llu\" ",
Xiaoping Fan978b3772015-05-27 14:15:18 -07002194 protocol,
2195 src_dev->name,
2196 &src_ip, &src_ip_xlate,
2197 ntohs(src_port), ntohs(src_port_xlate),
Xiaoping Fane1963d42015-08-25 17:06:19 -07002198 src_priority, src_dscp,
Xiaoping Fan978b3772015-05-27 14:15:18 -07002199 src_rx_packets, src_rx_bytes,
Ken Zhu37040ea2021-09-09 21:11:15 -07002200 src_mark,
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002201 original_fast_xmit ? "Yes" : "No",
Xiaoping Fan978b3772015-05-27 14:15:18 -07002202 dest_dev->name,
2203 &dest_ip, &dest_ip_xlate,
2204 ntohs(dest_port), ntohs(dest_port_xlate),
Xiaoping Fane1963d42015-08-25 17:06:19 -07002205 dest_priority, dest_dscp,
Xiaoping Fan978b3772015-05-27 14:15:18 -07002206 dest_rx_packets, dest_rx_bytes,
Ken Zhu37040ea2021-09-09 21:11:15 -07002207 dest_mark,
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002208 reply_fast_xmit ? "Yes" : "No",
Xiaoping Fan978b3772015-05-27 14:15:18 -07002209#ifdef CONFIG_NF_FLOW_COOKIE
2210 src_flow_cookie, dst_flow_cookie,
2211#endif
Ken Zhu37040ea2021-09-09 21:11:15 -07002212 last_sync_jiffies);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002213
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05302214 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 +05302215 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 +05302216 pppoe_session_id, pppoe_remote_mac);
2217 }
2218
Parikshit Guned31a8202022-01-05 22:15:04 +05302219 if (original_cm_sawf_valid) {
Parikshit Gunefdd98652022-03-14 17:33:01 +05302220 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 +05302221 flow_service_class, flow_msduq);
2222 }
2223
2224 if (reply_cm_sawf_valid) {
Parikshit Gunefdd98652022-03-14 17:33:01 +05302225 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 +05302226 return_service_class, return_msduq);
2227 }
2228
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05302229 bytes_read += snprintf(msg + bytes_read, CHAR_DEV_MSG_SIZE, ")/>\n");
2230
Xiaoping Fan978b3772015-05-27 14:15:18 -07002231 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2232 return false;
2233 }
2234
2235 *length -= bytes_read;
2236 *total_read += bytes_read;
2237
Xiaoping Fan978b3772015-05-27 14:15:18 -07002238 return true;
2239}
2240
2241/*
2242 * sfe_ipv6_debug_dev_read_connections_end()
2243 * Generate part of the XML output.
2244 */
2245static bool sfe_ipv6_debug_dev_read_connections_end(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
2246 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
2247{
2248 int bytes_read;
2249
2250 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t</connections>\n");
2251 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2252 return false;
2253 }
2254
2255 *length -= bytes_read;
2256 *total_read += bytes_read;
2257
2258 ws->state++;
2259 return true;
2260}
2261
2262/*
2263 * sfe_ipv6_debug_dev_read_exceptions_start()
2264 * Generate part of the XML output.
2265 */
2266static bool sfe_ipv6_debug_dev_read_exceptions_start(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
2267 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
2268{
2269 int bytes_read;
2270
2271 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<exceptions>\n");
2272 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2273 return false;
2274 }
2275
2276 *length -= bytes_read;
2277 *total_read += bytes_read;
2278
2279 ws->state++;
2280 return true;
2281}
2282
2283/*
2284 * sfe_ipv6_debug_dev_read_exceptions_exception()
2285 * Generate part of the XML output.
2286 */
2287static bool sfe_ipv6_debug_dev_read_exceptions_exception(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
2288 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
2289{
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302290 int i;
2291 u64 val = 0;
Xiaoping Fan978b3772015-05-27 14:15:18 -07002292
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302293 for_each_possible_cpu(i) {
2294 const struct sfe_ipv6_stats *s = per_cpu_ptr(si->stats_pcpu, i);
2295 val += s->exception_events64[ws->iter_exception];
2296 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07002297
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302298 if (val) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07002299 int bytes_read;
2300
2301 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE,
2302 "\t\t<exception name=\"%s\" count=\"%llu\" />\n",
2303 sfe_ipv6_exception_events_string[ws->iter_exception],
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302304 val);
2305
Xiaoping Fan978b3772015-05-27 14:15:18 -07002306 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2307 return false;
2308 }
2309
2310 *length -= bytes_read;
2311 *total_read += bytes_read;
2312 }
2313
2314 ws->iter_exception++;
2315 if (ws->iter_exception >= SFE_IPV6_EXCEPTION_EVENT_LAST) {
2316 ws->iter_exception = 0;
2317 ws->state++;
2318 }
2319
2320 return true;
2321}
2322
2323/*
2324 * sfe_ipv6_debug_dev_read_exceptions_end()
2325 * Generate part of the XML output.
2326 */
2327static bool sfe_ipv6_debug_dev_read_exceptions_end(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
2328 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
2329{
2330 int bytes_read;
2331
2332 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t</exceptions>\n");
2333 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2334 return false;
2335 }
2336
2337 *length -= bytes_read;
2338 *total_read += bytes_read;
2339
2340 ws->state++;
2341 return true;
2342}
2343
2344/*
2345 * sfe_ipv6_debug_dev_read_stats()
2346 * Generate part of the XML output.
2347 */
2348static bool sfe_ipv6_debug_dev_read_stats(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
2349 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
2350{
2351 int bytes_read;
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302352 struct sfe_ipv6_stats stats;
2353 unsigned int num_conn;
2354
2355 sfe_ipv6_update_summary_stats(si, &stats);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002356
2357 spin_lock_bh(&si->lock);
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302358 num_conn = si->num_connections;
Xiaoping Fan978b3772015-05-27 14:15:18 -07002359 spin_unlock_bh(&si->lock);
2360
2361 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<stats "
2362 "num_connections=\"%u\" "
Suruchi Suman23a279d2021-11-16 15:13:09 +05302363 "pkts_dropped=\"%llu\" "
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002364 "pkts_fast_xmited=\"%llu\" "
Xiaoping Fan978b3772015-05-27 14:15:18 -07002365 "pkts_forwarded=\"%llu\" pkts_not_forwarded=\"%llu\" "
2366 "create_requests=\"%llu\" create_collisions=\"%llu\" "
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05302367 "create_failures=\"%llu\" "
Xiaoping Fan978b3772015-05-27 14:15:18 -07002368 "destroy_requests=\"%llu\" destroy_misses=\"%llu\" "
2369 "flushes=\"%llu\" "
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +05302370 "hash_hits=\"%llu\" hash_reorders=\"%llu\" "
2371 "pppoe_encap_pkts_fwded=\"%llu\" "
Guduri Prathyusha034d6352022-01-12 16:49:04 +05302372 "pppoe_decap_pkts_fwded=\"%llu\" "
2373 "pppoe_bridge_pkts_fwded=\"%llu\" />\n",
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302374
2375 num_conn,
Suruchi Suman23a279d2021-11-16 15:13:09 +05302376 stats.packets_dropped64,
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002377 stats.packets_fast_xmited64,
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302378 stats.packets_forwarded64,
2379 stats.packets_not_forwarded64,
2380 stats.connection_create_requests64,
2381 stats.connection_create_collisions64,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05302382 stats.connection_create_failures64,
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302383 stats.connection_destroy_requests64,
2384 stats.connection_destroy_misses64,
2385 stats.connection_flushes64,
2386 stats.connection_match_hash_hits64,
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +05302387 stats.connection_match_hash_reorders64,
2388 stats.pppoe_encap_packets_forwarded64,
Guduri Prathyusha034d6352022-01-12 16:49:04 +05302389 stats.pppoe_decap_packets_forwarded64,
2390 stats.pppoe_bridge_packets_forwarded64);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002391 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2392 return false;
2393 }
2394
2395 *length -= bytes_read;
2396 *total_read += bytes_read;
2397
2398 ws->state++;
2399 return true;
2400}
2401
2402/*
2403 * sfe_ipv6_debug_dev_read_end()
2404 * Generate part of the XML output.
2405 */
2406static bool sfe_ipv6_debug_dev_read_end(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
2407 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
2408{
2409 int bytes_read;
2410
2411 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "</sfe_ipv6>\n");
2412 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2413 return false;
2414 }
2415
2416 *length -= bytes_read;
2417 *total_read += bytes_read;
2418
2419 ws->state++;
2420 return true;
2421}
2422
2423/*
2424 * Array of write functions that write various XML elements that correspond to
2425 * our XML output state machine.
2426 */
2427static sfe_ipv6_debug_xml_write_method_t sfe_ipv6_debug_xml_write_methods[SFE_IPV6_DEBUG_XML_STATE_DONE] = {
2428 sfe_ipv6_debug_dev_read_start,
2429 sfe_ipv6_debug_dev_read_connections_start,
2430 sfe_ipv6_debug_dev_read_connections_connection,
2431 sfe_ipv6_debug_dev_read_connections_end,
2432 sfe_ipv6_debug_dev_read_exceptions_start,
2433 sfe_ipv6_debug_dev_read_exceptions_exception,
2434 sfe_ipv6_debug_dev_read_exceptions_end,
2435 sfe_ipv6_debug_dev_read_stats,
2436 sfe_ipv6_debug_dev_read_end,
2437};
2438
2439/*
2440 * sfe_ipv6_debug_dev_read()
2441 * Send info to userspace upon read request from user
2442 */
2443static ssize_t sfe_ipv6_debug_dev_read(struct file *filp, char *buffer, size_t length, loff_t *offset)
2444{
2445 char msg[CHAR_DEV_MSG_SIZE];
2446 int total_read = 0;
2447 struct sfe_ipv6_debug_xml_write_state *ws;
2448 struct sfe_ipv6 *si = &__si6;
2449
2450 ws = (struct sfe_ipv6_debug_xml_write_state *)filp->private_data;
2451 while ((ws->state != SFE_IPV6_DEBUG_XML_STATE_DONE) && (length > CHAR_DEV_MSG_SIZE)) {
2452 if ((sfe_ipv6_debug_xml_write_methods[ws->state])(si, buffer, msg, &length, &total_read, ws)) {
2453 continue;
2454 }
2455 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07002456 return total_read;
2457}
2458
2459/*
Xiaoping Fan978b3772015-05-27 14:15:18 -07002460 * sfe_ipv6_debug_dev_open()
2461 */
2462static int sfe_ipv6_debug_dev_open(struct inode *inode, struct file *file)
2463{
2464 struct sfe_ipv6_debug_xml_write_state *ws;
2465
2466 ws = (struct sfe_ipv6_debug_xml_write_state *)file->private_data;
2467 if (ws) {
2468 return 0;
2469 }
2470
2471 ws = kzalloc(sizeof(struct sfe_ipv6_debug_xml_write_state), GFP_KERNEL);
2472 if (!ws) {
2473 return -ENOMEM;
2474 }
2475
2476 ws->state = SFE_IPV6_DEBUG_XML_STATE_START;
2477 file->private_data = ws;
2478
2479 return 0;
2480}
2481
2482/*
2483 * sfe_ipv6_debug_dev_release()
2484 */
2485static int sfe_ipv6_debug_dev_release(struct inode *inode, struct file *file)
2486{
2487 struct sfe_ipv6_debug_xml_write_state *ws;
Xiaoping Fan978b3772015-05-27 14:15:18 -07002488
2489 ws = (struct sfe_ipv6_debug_xml_write_state *)file->private_data;
Xiaoping Fan34586472015-07-03 02:20:35 -07002490 if (ws) {
2491 /*
2492 * We've finished with our output so free the write state.
2493 */
2494 kfree(ws);
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05302495 file->private_data = NULL;
Xiaoping Fan978b3772015-05-27 14:15:18 -07002496 }
2497
Xiaoping Fan978b3772015-05-27 14:15:18 -07002498 return 0;
2499}
2500
2501/*
2502 * File operations used in the debug char device
2503 */
2504static struct file_operations sfe_ipv6_debug_dev_fops = {
2505 .read = sfe_ipv6_debug_dev_read,
Xiaoping Fan978b3772015-05-27 14:15:18 -07002506 .open = sfe_ipv6_debug_dev_open,
2507 .release = sfe_ipv6_debug_dev_release
2508};
2509
2510#ifdef CONFIG_NF_FLOW_COOKIE
2511/*
2512 * sfe_ipv6_register_flow_cookie_cb
2513 * register a function in SFE to let SFE use this function to configure flow cookie for a flow
2514 *
2515 * Hardware driver which support flow cookie should register a callback function in SFE. Then SFE
2516 * can use this function to configure flow cookie for a flow.
2517 * return: 0, success; !=0, fail
2518 */
2519int sfe_ipv6_register_flow_cookie_cb(sfe_ipv6_flow_cookie_set_func_t cb)
2520{
2521 struct sfe_ipv6 *si = &__si6;
2522
2523 BUG_ON(!cb);
2524
2525 if (si->flow_cookie_set_func) {
2526 return -1;
2527 }
2528
2529 rcu_assign_pointer(si->flow_cookie_set_func, cb);
2530 return 0;
2531}
2532
2533/*
2534 * sfe_ipv6_unregister_flow_cookie_cb
2535 * unregister function which is used to configure flow cookie for a flow
2536 *
2537 * return: 0, success; !=0, fail
2538 */
2539int sfe_ipv6_unregister_flow_cookie_cb(sfe_ipv6_flow_cookie_set_func_t cb)
2540{
2541 struct sfe_ipv6 *si = &__si6;
2542
2543 RCU_INIT_POINTER(si->flow_cookie_set_func, NULL);
2544 return 0;
2545}
Xiaoping Fan640faf42015-08-28 15:50:55 -07002546
2547/*
2548 * sfe_ipv6_get_flow_cookie()
2549 */
2550static ssize_t sfe_ipv6_get_flow_cookie(struct device *dev,
2551 struct device_attribute *attr,
2552 char *buf)
2553{
2554 struct sfe_ipv6 *si = &__si6;
Xiaoping Fan01c67cc2015-11-09 11:31:57 -08002555 return snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", si->flow_cookie_enable);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002556}
2557
2558/*
2559 * sfe_ipv6_set_flow_cookie()
2560 */
2561static ssize_t sfe_ipv6_set_flow_cookie(struct device *dev,
2562 struct device_attribute *attr,
2563 const char *buf, size_t size)
2564{
2565 struct sfe_ipv6 *si = &__si6;
Ken Zhu137722d2021-09-23 17:57:36 -07002566 si->flow_cookie_enable = strict_strtol(buf, NULL, 0);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002567
2568 return size;
2569}
2570
2571/*
2572 * sysfs attributes.
2573 */
2574static const struct device_attribute sfe_ipv6_flow_cookie_attr =
Xiaoping Fane70da412016-02-26 16:47:57 -08002575 __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 -07002576#endif /*CONFIG_NF_FLOW_COOKIE*/
2577
Ken Zhu137722d2021-09-23 17:57:36 -07002578/*
2579 * sfe_ipv6_get_cpu()
2580 */
2581static ssize_t sfe_ipv6_get_cpu(struct device *dev,
2582 struct device_attribute *attr,
2583 char *buf)
2584{
2585 struct sfe_ipv6 *si = &__si6;
2586 return snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", si->work_cpu);
2587}
2588
2589/*
Wayne Tanbb7f1782021-12-13 11:16:04 -08002590 * sfe_ipv6_set_cpu()
Ken Zhu137722d2021-09-23 17:57:36 -07002591 */
2592static ssize_t sfe_ipv6_set_cpu(struct device *dev,
2593 struct device_attribute *attr,
2594 const char *buf, size_t size)
2595{
2596 struct sfe_ipv6 *si = &__si6;
2597 int work_cpu;
2598
2599 work_cpu = simple_strtol(buf, NULL, 0);
2600 if ((work_cpu >= 0) && (work_cpu <= NR_CPUS)) {
2601 si->work_cpu = work_cpu;
2602 } else {
2603 dev_err(dev, "%s is not in valid range[0,%d]", buf, NR_CPUS);
2604 }
2605
2606 return size;
2607}
2608/*
2609 * sysfs attributes.
2610 */
2611static const struct device_attribute sfe_ipv6_cpu_attr =
2612 __ATTR(stat_work_cpu, S_IWUSR | S_IRUGO, sfe_ipv6_get_cpu, sfe_ipv6_set_cpu);
2613
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05302614 /*
2615 * sfe_ipv6_hash_init()
2616 * Initialize conn match hash lists
2617 */
2618static void sfe_ipv6_conn_match_hash_init(struct sfe_ipv6 *si, int len)
2619{
2620 struct hlist_head *hash_list = si->hlist_conn_match_hash_head;
2621 int i;
2622
2623 for (i = 0; i < len; i++) {
2624 INIT_HLIST_HEAD(&hash_list[i]);
2625 }
2626}
2627
Suruchi Suman23a279d2021-11-16 15:13:09 +05302628#ifdef SFE_PROCESS_LOCAL_OUT
2629/*
2630 * sfe_ipv6_local_out()
2631 * Called for packets from ip_local_out() - post encapsulation & other packets
2632 */
2633static unsigned int sfe_ipv6_local_out(void *priv,
2634 struct sk_buff *skb,
2635 const struct nf_hook_state *nhs)
2636{
Nitin Shettyc28f8172022-02-04 16:23:46 +05302637 struct sfe_l2_info l2_info = {0};
2638
Suruchi Suman23a279d2021-11-16 15:13:09 +05302639 DEBUG_TRACE("sfe: sfe_ipv6_local_out hook called.\n");
2640
2641 if (likely(skb->skb_iif)) {
Nitin Shettyc28f8172022-02-04 16:23:46 +05302642 return sfe_ipv6_recv(skb->dev, skb, &l2_info, true) ? NF_STOLEN : NF_ACCEPT;
Suruchi Suman23a279d2021-11-16 15:13:09 +05302643 }
2644
2645 return NF_ACCEPT;
2646}
2647
2648/*
2649 * struct nf_hook_ops sfe_ipv6_ops_local_out[]
2650 * Hooks into netfilter local out packet monitoring points.
2651 */
2652static struct nf_hook_ops sfe_ipv6_ops_local_out[] __read_mostly = {
2653
2654 /*
2655 * Local out routing hook is used to monitor packets.
2656 */
2657 {
2658 .hook = sfe_ipv6_local_out,
2659 .pf = PF_INET6,
2660 .hooknum = NF_INET_LOCAL_OUT,
2661 .priority = NF_IP6_PRI_FIRST,
2662 },
2663};
2664#endif
2665
Xiaoping Fan978b3772015-05-27 14:15:18 -07002666/*
2667 * sfe_ipv6_init()
2668 */
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05302669int sfe_ipv6_init(void)
Xiaoping Fan978b3772015-05-27 14:15:18 -07002670{
2671 struct sfe_ipv6 *si = &__si6;
2672 int result = -1;
2673
2674 DEBUG_INFO("SFE IPv6 init\n");
2675
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05302676 sfe_ipv6_conn_match_hash_init(si, ARRAY_SIZE(si->hlist_conn_match_hash_head));
2677
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302678 si->stats_pcpu = alloc_percpu_gfp(struct sfe_ipv6_stats, GFP_KERNEL | __GFP_ZERO);
2679 if (!si->stats_pcpu) {
2680 DEBUG_ERROR("failed to allocate stats memory for sfe_ipv6\n");
2681 goto exit0;
2682 }
2683
Xiaoping Fan978b3772015-05-27 14:15:18 -07002684 /*
Parikshit Guned31a8202022-01-05 22:15:04 +05302685 * Allocate per cpu per service class memory.
2686 */
2687 si->stats_pcpu_psc = alloc_percpu_gfp(struct sfe_ipv6_service_class_stats_db,
2688 GFP_KERNEL | __GFP_ZERO);
2689 if (!si->stats_pcpu_psc) {
2690 DEBUG_ERROR("failed to allocate per cpu per service clas stats memory\n");
2691 goto exit1;
2692 }
2693
2694 /*
Xiaoping Fan978b3772015-05-27 14:15:18 -07002695 * Create sys/sfe_ipv6
2696 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302697 si->sys_ipv6 = kobject_create_and_add("sfe_ipv6", NULL);
2698 if (!si->sys_ipv6) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07002699 DEBUG_ERROR("failed to register sfe_ipv6\n");
Parikshit Guned31a8202022-01-05 22:15:04 +05302700 goto exit2;
Xiaoping Fan978b3772015-05-27 14:15:18 -07002701 }
2702
2703 /*
2704 * Create files, one for each parameter supported by this module.
2705 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302706 result = sysfs_create_file(si->sys_ipv6, &sfe_ipv6_debug_dev_attr.attr);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002707 if (result) {
2708 DEBUG_ERROR("failed to register debug dev file: %d\n", result);
Parikshit Guned31a8202022-01-05 22:15:04 +05302709 goto exit3;
Xiaoping Fan978b3772015-05-27 14:15:18 -07002710 }
2711
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302712 result = sysfs_create_file(si->sys_ipv6, &sfe_ipv6_cpu_attr.attr);
Ken Zhu137722d2021-09-23 17:57:36 -07002713 if (result) {
2714 DEBUG_ERROR("failed to register debug dev file: %d\n", result);
Parikshit Guned31a8202022-01-05 22:15:04 +05302715 goto exit4;
Ken Zhu137722d2021-09-23 17:57:36 -07002716 }
2717
Xiaoping Fan640faf42015-08-28 15:50:55 -07002718#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302719 result = sysfs_create_file(si->sys_ipv6, &sfe_ipv6_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002720 if (result) {
2721 DEBUG_ERROR("failed to register flow cookie enable file: %d\n", result);
Parikshit Guned31a8202022-01-05 22:15:04 +05302722 goto exit5;
Xiaoping Fan640faf42015-08-28 15:50:55 -07002723 }
2724#endif /* CONFIG_NF_FLOW_COOKIE */
2725
Suruchi Suman23a279d2021-11-16 15:13:09 +05302726#ifdef SFE_PROCESS_LOCAL_OUT
2727#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
2728 result = nf_register_hooks(sfe_ipv6_ops_local_out, ARRAY_SIZE(sfe_ipv6_ops_local_out));
2729#else
2730 result = nf_register_net_hooks(&init_net, sfe_ipv6_ops_local_out, ARRAY_SIZE(sfe_ipv6_ops_local_out));
2731#endif
Suruchi Suman23a279d2021-11-16 15:13:09 +05302732 if (result < 0) {
2733 DEBUG_ERROR("can't register nf local out hook: %d\n", result);
Parikshit Guned31a8202022-01-05 22:15:04 +05302734 goto exit6;
Suruchi Suman23a279d2021-11-16 15:13:09 +05302735 }
Murat Sezginc09b1322022-03-16 10:15:38 -07002736 DEBUG_INFO("Register nf local out hook success: %d\n", result);
2737#endif
Suruchi Suman23a279d2021-11-16 15:13:09 +05302738
Xiaoping Fan978b3772015-05-27 14:15:18 -07002739 /*
2740 * Register our debug char device.
2741 */
2742 result = register_chrdev(0, "sfe_ipv6", &sfe_ipv6_debug_dev_fops);
2743 if (result < 0) {
2744 DEBUG_ERROR("Failed to register chrdev: %d\n", result);
Parikshit Guned31a8202022-01-05 22:15:04 +05302745 goto exit7;
Xiaoping Fan978b3772015-05-27 14:15:18 -07002746 }
2747
2748 si->debug_dev = result;
Ken Zhu137722d2021-09-23 17:57:36 -07002749 si->work_cpu = WORK_CPU_UNBOUND;
Xiaoping Fan978b3772015-05-27 14:15:18 -07002750
2751 /*
Ken Zhu137722d2021-09-23 17:57:36 -07002752 * Create work to handle periodic statistics.
Xiaoping Fan978b3772015-05-27 14:15:18 -07002753 */
Ken Zhu137722d2021-09-23 17:57:36 -07002754 INIT_DELAYED_WORK(&(si->sync_dwork), sfe_ipv6_periodic_sync);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002755
Ken Zhu7a43d882022-01-04 10:51:44 -08002756 sfe_ipv6_sync_many_msg = kzalloc(PAGE_SIZE, GFP_KERNEL);
2757 if(!sfe_ipv6_sync_many_msg) {
2758 goto exit8;
2759 }
2760
2761 sfe_ipv6_msg_init(sfe_ipv6_sync_many_msg, SFE_SPECIAL_INTERFACE_IPV6,
2762 SFE_TX_CONN_STATS_SYNC_MANY_MSG,
2763 sizeof(struct sfe_ipv4_conn_sync_many_msg),
2764 NULL,
2765 NULL);
2766 sfe_ipv6_sync_max_number = (PAGE_SIZE - sizeof(struct sfe_ipv6_msg)) / sizeof(struct sfe_ipv6_conn_sync);
2767
2768 spin_lock_init(&si->lock);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002769 return 0;
2770
Ken Zhu7a43d882022-01-04 10:51:44 -08002771exit8:
2772 unregister_chrdev(si->debug_dev, "sfe_ipv6");
2773
Parikshit Guned31a8202022-01-05 22:15:04 +05302774exit7:
Suruchi Suman23a279d2021-11-16 15:13:09 +05302775#ifdef SFE_PROCESS_LOCAL_OUT
2776#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
2777 DEBUG_TRACE("sfe: Unregister local out hook\n");
2778 nf_unregister_hooks(sfe_ipv6_ops_local_out, ARRAY_SIZE(sfe_ipv6_ops_local_out));
2779#else
2780 DEBUG_TRACE("sfe: Unregister local out hook\n");
2781 nf_unregister_net_hooks(&init_net, sfe_ipv6_ops_local_out, ARRAY_SIZE(sfe_ipv6_ops_local_out));
2782#endif
Parikshit Guned31a8202022-01-05 22:15:04 +05302783exit6:
Murat Sezginc09b1322022-03-16 10:15:38 -07002784#endif
Xiaoping Fan640faf42015-08-28 15:50:55 -07002785#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302786 sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002787
Parikshit Guned31a8202022-01-05 22:15:04 +05302788exit5:
Xiaoping Fan640faf42015-08-28 15:50:55 -07002789#endif /* CONFIG_NF_FLOW_COOKIE */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302790 sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_cpu_attr.attr);
Suruchi Suman23a279d2021-11-16 15:13:09 +05302791
Parikshit Guned31a8202022-01-05 22:15:04 +05302792exit4:
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302793 sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_debug_dev_attr.attr);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002794
Parikshit Guned31a8202022-01-05 22:15:04 +05302795exit3:
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302796 kobject_put(si->sys_ipv6);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002797
Parikshit Guned31a8202022-01-05 22:15:04 +05302798exit2:
2799 free_percpu(si->stats_pcpu_psc);
2800
Xiaoping Fan978b3772015-05-27 14:15:18 -07002801exit1:
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302802 free_percpu(si->stats_pcpu);
2803
2804exit0:
Xiaoping Fan978b3772015-05-27 14:15:18 -07002805 return result;
2806}
2807
2808/*
2809 * sfe_ipv6_exit()
2810 */
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05302811void sfe_ipv6_exit(void)
Xiaoping Fan978b3772015-05-27 14:15:18 -07002812{
2813 struct sfe_ipv6 *si = &__si6;
2814
2815 DEBUG_INFO("SFE IPv6 exit\n");
2816
2817 /*
2818 * Destroy all connections.
2819 */
2820 sfe_ipv6_destroy_all_rules_for_dev(NULL);
2821
Ken Zhu137722d2021-09-23 17:57:36 -07002822 cancel_delayed_work(&si->sync_dwork);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002823
2824 unregister_chrdev(si->debug_dev, "sfe_ipv6");
2825
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302826 free_percpu(si->stats_pcpu);
Parikshit Guned31a8202022-01-05 22:15:04 +05302827 free_percpu(si->stats_pcpu_psc);
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302828
Suruchi Suman23a279d2021-11-16 15:13:09 +05302829#ifdef SFE_PROCESS_LOCAL_OUT
2830#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
2831 DEBUG_TRACE("sfe: Unregister local out hook\n");
2832 nf_unregister_hooks(sfe_ipv6_ops_local_out, ARRAY_SIZE(sfe_ipv6_ops_local_out));
2833#else
2834 DEBUG_TRACE("sfe: Unregister local out hook\n");
2835 nf_unregister_net_hooks(&init_net, sfe_ipv6_ops_local_out, ARRAY_SIZE(sfe_ipv6_ops_local_out));
2836#endif
2837#endif
2838
Xiaoping Fan640faf42015-08-28 15:50:55 -07002839#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302840 sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002841#endif /* CONFIG_NF_FLOW_COOKIE */
Ken Zhu137722d2021-09-23 17:57:36 -07002842
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302843 sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_cpu_attr.attr);
Ken Zhu137722d2021-09-23 17:57:36 -07002844
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302845 sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_debug_dev_attr.attr);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002846
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302847 kobject_put(si->sys_ipv6);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002848}
2849
Xiaoping Fan978b3772015-05-27 14:15:18 -07002850#ifdef CONFIG_NF_FLOW_COOKIE
2851EXPORT_SYMBOL(sfe_ipv6_register_flow_cookie_cb);
2852EXPORT_SYMBOL(sfe_ipv6_unregister_flow_cookie_cb);
2853#endif