blob: d49f06fbcca919bbf4fb4266d883eb329379cf6e [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>
Wayne Tanbb7f1782021-12-13 11:16:04 -080034
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +053035#include "sfe_debug.h"
Ratheesh Kannoth89302a72021-10-20 08:10:37 +053036#include "sfe_api.h"
Xiaoping Fan978b3772015-05-27 14:15:18 -070037#include "sfe.h"
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +053038#include "sfe_flow_cookie.h"
39#include "sfe_ipv6.h"
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +053040#include "sfe_ipv6_udp.h"
41#include "sfe_ipv6_tcp.h"
42#include "sfe_ipv6_icmp.h"
Wayne Tanbb7f1782021-12-13 11:16:04 -080043#include "sfe_pppoe.h"
Xiaoping Fan978b3772015-05-27 14:15:18 -070044
Ratheesh Kannoth89302a72021-10-20 08:10:37 +053045#define sfe_ipv6_addr_copy(src, dest) memcpy((void *)(dest), (void *)(src), 16)
46
Xiaoping Fan978b3772015-05-27 14:15:18 -070047static char *sfe_ipv6_exception_events_string[SFE_IPV6_EXCEPTION_EVENT_LAST] = {
48 "UDP_HEADER_INCOMPLETE",
49 "UDP_NO_CONNECTION",
50 "UDP_IP_OPTIONS_OR_INITIAL_FRAGMENT",
51 "UDP_SMALL_TTL",
52 "UDP_NEEDS_FRAGMENTATION",
53 "TCP_HEADER_INCOMPLETE",
54 "TCP_NO_CONNECTION_SLOW_FLAGS",
55 "TCP_NO_CONNECTION_FAST_FLAGS",
56 "TCP_IP_OPTIONS_OR_INITIAL_FRAGMENT",
57 "TCP_SMALL_TTL",
58 "TCP_NEEDS_FRAGMENTATION",
59 "TCP_FLAGS",
60 "TCP_SEQ_EXCEEDS_RIGHT_EDGE",
61 "TCP_SMALL_DATA_OFFS",
62 "TCP_BAD_SACK",
63 "TCP_BIG_DATA_OFFS",
64 "TCP_SEQ_BEFORE_LEFT_EDGE",
65 "TCP_ACK_EXCEEDS_RIGHT_EDGE",
66 "TCP_ACK_BEFORE_LEFT_EDGE",
67 "ICMP_HEADER_INCOMPLETE",
68 "ICMP_UNHANDLED_TYPE",
69 "ICMP_IPV6_HEADER_INCOMPLETE",
70 "ICMP_IPV6_NON_V6",
71 "ICMP_IPV6_IP_OPTIONS_INCOMPLETE",
72 "ICMP_IPV6_UDP_HEADER_INCOMPLETE",
73 "ICMP_IPV6_TCP_HEADER_INCOMPLETE",
74 "ICMP_IPV6_UNHANDLED_PROTOCOL",
75 "ICMP_NO_CONNECTION",
76 "ICMP_FLUSHED_CONNECTION",
77 "HEADER_INCOMPLETE",
78 "BAD_TOTAL_LENGTH",
79 "NON_V6",
80 "NON_INITIAL_FRAGMENT",
81 "DATAGRAM_INCOMPLETE",
82 "IP_OPTIONS_INCOMPLETE",
83 "UNHANDLED_PROTOCOL",
Ratheesh Kannoth5dee3772022-01-18 11:27:14 +053084 "FLOW_COOKIE_ADD_FAIL",
85 "INVALID_SOURCE_INTERFACE",
Xiaoping Fan978b3772015-05-27 14:15:18 -070086};
87
Xiaoping Fan6a1672f2016-08-17 19:58:12 -070088static struct sfe_ipv6 __si6;
Xiaoping Fan978b3772015-05-27 14:15:18 -070089
90/*
91 * sfe_ipv6_get_debug_dev()
92 */
93static ssize_t sfe_ipv6_get_debug_dev(struct device *dev, struct device_attribute *attr, char *buf);
94
95/*
96 * sysfs attributes.
97 */
98static const struct device_attribute sfe_ipv6_debug_dev_attr =
Xiaoping Fane70da412016-02-26 16:47:57 -080099 __ATTR(debug_dev, S_IWUSR | S_IRUGO, sfe_ipv6_get_debug_dev, NULL);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700100
101/*
Xiaoping Fan978b3772015-05-27 14:15:18 -0700102 * sfe_ipv6_get_connection_match_hash()
103 * Generate the hash used in connection match lookups.
104 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700105static inline unsigned int sfe_ipv6_get_connection_match_hash(struct net_device *dev, u8 protocol,
Xiaoping Fan978b3772015-05-27 14:15:18 -0700106 struct sfe_ipv6_addr *src_ip, __be16 src_port,
107 struct sfe_ipv6_addr *dest_ip, __be16 dest_port)
108{
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700109 u32 idx, hash = 0;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700110
111 for (idx = 0; idx < 4; idx++) {
112 hash ^= src_ip->addr[idx] ^ dest_ip->addr[idx];
113 }
Ratheesh Kannoth5dee3772022-01-18 11:27:14 +0530114 hash = hash ^ protocol ^ ntohs(src_port ^ dest_port);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700115 return ((hash >> SFE_IPV6_CONNECTION_HASH_SHIFT) ^ hash) & SFE_IPV6_CONNECTION_HASH_MASK;
116}
117
118/*
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530119 * sfe_ipv6_find_connection_match_rcu()
Xiaoping Fan978b3772015-05-27 14:15:18 -0700120 * Get the IPv6 flow match info that corresponds to a particular 5-tuple.
Xiaoping Fan978b3772015-05-27 14:15:18 -0700121 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530122struct sfe_ipv6_connection_match *
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530123sfe_ipv6_find_connection_match_rcu(struct sfe_ipv6 *si, struct net_device *dev, u8 protocol,
Xiaoping Fan978b3772015-05-27 14:15:18 -0700124 struct sfe_ipv6_addr *src_ip, __be16 src_port,
125 struct sfe_ipv6_addr *dest_ip, __be16 dest_port)
126{
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530127 struct sfe_ipv6_connection_match *cm = NULL;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700128 unsigned int conn_match_idx;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530129 struct hlist_head *lhead;
130 WARN_ON_ONCE(!rcu_read_lock_held());
Xiaoping Fan978b3772015-05-27 14:15:18 -0700131
132 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 -0700133
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530134 lhead = &si->hlist_conn_match_hash_head[conn_match_idx];
Xiaoping Fan978b3772015-05-27 14:15:18 -0700135
136 /*
137 * Hopefully the first entry is the one we want.
138 */
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530139 hlist_for_each_entry_rcu(cm, lhead, hnode) {
140 if ((cm->match_dest_port != dest_port) ||
141 (!sfe_ipv6_addr_equal(cm->match_src_ip, src_ip)) ||
142 (!sfe_ipv6_addr_equal(cm->match_dest_ip, dest_ip)) ||
143 (cm->match_protocol != protocol) ||
144 (cm->match_dev != dev)) {
145 continue;
146 }
147
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530148 this_cpu_inc(si->stats_pcpu->connection_match_hash_hits64);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700149
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530150 break;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700151
Xiaoping Fan978b3772015-05-27 14:15:18 -0700152 }
153
Xiaoping Fan978b3772015-05-27 14:15:18 -0700154 return cm;
155}
156
157/*
158 * sfe_ipv6_connection_match_update_summary_stats()
159 * Update the summary stats for a connection match entry.
160 */
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530161static inline void sfe_ipv6_connection_match_update_summary_stats(struct sfe_ipv6_connection_match *cm,
162 u32 *packets, u32 *bytes)
163
Xiaoping Fan978b3772015-05-27 14:15:18 -0700164{
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530165 u32 packet_count, byte_count;
166
167 packet_count = atomic_read(&cm->rx_packet_count);
168 cm->rx_packet_count64 += packet_count;
169 atomic_sub(packet_count, &cm->rx_packet_count);
170
171 byte_count = atomic_read(&cm->rx_byte_count);
172 cm->rx_byte_count64 += byte_count;
173 atomic_sub(byte_count, &cm->rx_byte_count);
174
175 *packets = packet_count;
176 *bytes = byte_count;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700177}
178
179/*
180 * sfe_ipv6_connection_match_compute_translations()
181 * Compute port and address translations for a connection match entry.
182 */
183static void sfe_ipv6_connection_match_compute_translations(struct sfe_ipv6_connection_match *cm)
184{
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700185 u32 diff[9];
186 u32 *idx_32;
187 u16 *idx_16;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700188
189 /*
190 * Before we insert the entry look to see if this is tagged as doing address
191 * translations. If it is then work out the adjustment that we need to apply
192 * to the transport checksum.
193 */
194 if (cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_XLATE_SRC) {
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700195 u32 adj = 0;
196 u32 carry = 0;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700197
198 /*
199 * Precompute an incremental checksum adjustment so we can
200 * edit packets in this stream very quickly. The algorithm is from RFC1624.
201 */
202 idx_32 = diff;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530203 *(idx_32++) = cm->match_src_ip[0].addr[0];
204 *(idx_32++) = cm->match_src_ip[0].addr[1];
205 *(idx_32++) = cm->match_src_ip[0].addr[2];
206 *(idx_32++) = cm->match_src_ip[0].addr[3];
Xiaoping Fan978b3772015-05-27 14:15:18 -0700207
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700208 idx_16 = (u16 *)idx_32;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700209 *(idx_16++) = cm->match_src_port;
210 *(idx_16++) = ~cm->xlate_src_port;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700211 idx_32 = (u32 *)idx_16;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700212
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530213 *(idx_32++) = ~cm->xlate_src_ip[0].addr[0];
214 *(idx_32++) = ~cm->xlate_src_ip[0].addr[1];
215 *(idx_32++) = ~cm->xlate_src_ip[0].addr[2];
216 *(idx_32++) = ~cm->xlate_src_ip[0].addr[3];
Xiaoping Fan978b3772015-05-27 14:15:18 -0700217
218 /*
219 * When we compute this fold it down to a 16-bit offset
220 * as that way we can avoid having to do a double
221 * folding of the twos-complement result because the
222 * addition of 2 16-bit values cannot cause a double
223 * wrap-around!
224 */
225 for (idx_32 = diff; idx_32 < diff + 9; idx_32++) {
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700226 u32 w = *idx_32;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700227 adj += carry;
228 adj += w;
229 carry = (w > adj);
230 }
231 adj += carry;
232 adj = (adj & 0xffff) + (adj >> 16);
233 adj = (adj & 0xffff) + (adj >> 16);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700234 cm->xlate_src_csum_adjustment = (u16)adj;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700235 }
236
237 if (cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_XLATE_DEST) {
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700238 u32 adj = 0;
239 u32 carry = 0;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700240
241 /*
242 * Precompute an incremental checksum adjustment so we can
243 * edit packets in this stream very quickly. The algorithm is from RFC1624.
244 */
245 idx_32 = diff;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530246 *(idx_32++) = cm->match_dest_ip[0].addr[0];
247 *(idx_32++) = cm->match_dest_ip[0].addr[1];
248 *(idx_32++) = cm->match_dest_ip[0].addr[2];
249 *(idx_32++) = cm->match_dest_ip[0].addr[3];
Xiaoping Fan978b3772015-05-27 14:15:18 -0700250
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700251 idx_16 = (u16 *)idx_32;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700252 *(idx_16++) = cm->match_dest_port;
253 *(idx_16++) = ~cm->xlate_dest_port;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700254 idx_32 = (u32 *)idx_16;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700255
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530256 *(idx_32++) = ~cm->xlate_dest_ip[0].addr[0];
257 *(idx_32++) = ~cm->xlate_dest_ip[0].addr[1];
258 *(idx_32++) = ~cm->xlate_dest_ip[0].addr[2];
259 *(idx_32++) = ~cm->xlate_dest_ip[0].addr[3];
Xiaoping Fan978b3772015-05-27 14:15:18 -0700260
261 /*
262 * When we compute this fold it down to a 16-bit offset
263 * as that way we can avoid having to do a double
264 * folding of the twos-complement result because the
265 * addition of 2 16-bit values cannot cause a double
266 * wrap-around!
267 */
268 for (idx_32 = diff; idx_32 < diff + 9; idx_32++) {
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700269 u32 w = *idx_32;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700270 adj += carry;
271 adj += w;
272 carry = (w > adj);
273 }
274 adj += carry;
275 adj = (adj & 0xffff) + (adj >> 16);
276 adj = (adj & 0xffff) + (adj >> 16);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700277 cm->xlate_dest_csum_adjustment = (u16)adj;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700278 }
279}
280
281/*
282 * sfe_ipv6_update_summary_stats()
283 * Update the summary stats.
284 */
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530285static void sfe_ipv6_update_summary_stats(struct sfe_ipv6 *si, struct sfe_ipv6_stats *stats)
Xiaoping Fan978b3772015-05-27 14:15:18 -0700286{
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530287 int i = 0;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700288
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530289 memset(stats, 0, sizeof(*stats));
Xiaoping Fan978b3772015-05-27 14:15:18 -0700290
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530291 for_each_possible_cpu(i) {
292 const struct sfe_ipv6_stats *s = per_cpu_ptr(si->stats_pcpu, i);
293
294 stats->connection_create_requests64 += s->connection_create_requests64;
295 stats->connection_create_collisions64 += s->connection_create_collisions64;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530296 stats->connection_create_failures64 += s->connection_create_failures64;
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530297 stats->connection_destroy_requests64 += s->connection_destroy_requests64;
298 stats->connection_destroy_misses64 += s->connection_destroy_misses64;
299 stats->connection_match_hash_hits64 += s->connection_match_hash_hits64;
300 stats->connection_match_hash_reorders64 += s->connection_match_hash_reorders64;
301 stats->connection_flushes64 += s->connection_flushes64;
Suruchi Suman23a279d2021-11-16 15:13:09 +0530302 stats->packets_dropped64 += s->packets_dropped64;
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530303 stats->packets_forwarded64 += s->packets_forwarded64;
304 stats->packets_not_forwarded64 += s->packets_not_forwarded64;
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +0530305 stats->pppoe_encap_packets_forwarded64 += s->pppoe_encap_packets_forwarded64;
306 stats->pppoe_decap_packets_forwarded64 += s->pppoe_decap_packets_forwarded64;
Guduri Prathyusha034d6352022-01-12 16:49:04 +0530307 stats->pppoe_bridge_packets_forwarded64 += s->pppoe_bridge_packets_forwarded64;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700308 }
309}
310
311/*
312 * sfe_ipv6_insert_connection_match()
313 * Insert a connection match into the hash.
314 *
315 * On entry we must be holding the lock that protects the hash table.
316 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700317static inline void sfe_ipv6_insert_connection_match(struct sfe_ipv6 *si,
318 struct sfe_ipv6_connection_match *cm)
Xiaoping Fan978b3772015-05-27 14:15:18 -0700319{
Xiaoping Fan978b3772015-05-27 14:15:18 -0700320 unsigned int conn_match_idx
321 = sfe_ipv6_get_connection_match_hash(cm->match_dev, cm->match_protocol,
322 cm->match_src_ip, cm->match_src_port,
323 cm->match_dest_ip, cm->match_dest_port);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700324
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530325 lockdep_assert_held(&si->lock);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700326
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530327 hlist_add_head_rcu(&cm->hnode, &si->hlist_conn_match_hash_head[conn_match_idx]);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700328#ifdef CONFIG_NF_FLOW_COOKIE
Xiaoping Fan640faf42015-08-28 15:50:55 -0700329 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 -0700330 return;
331
332 /*
333 * Configure hardware to put a flow cookie in packet of this flow,
334 * then we can accelerate the lookup process when we received this packet.
335 */
336 for (conn_match_idx = 1; conn_match_idx < SFE_FLOW_COOKIE_SIZE; conn_match_idx++) {
337 struct sfe_ipv6_flow_cookie_entry *entry = &si->sfe_flow_cookie_table[conn_match_idx];
338
339 if ((NULL == entry->match) && time_is_before_jiffies(entry->last_clean_time + HZ)) {
340 sfe_ipv6_flow_cookie_set_func_t func;
341
342 rcu_read_lock();
343 func = rcu_dereference(si->flow_cookie_set_func);
344 if (func) {
345 if (!func(cm->match_protocol, cm->match_src_ip->addr, cm->match_src_port,
346 cm->match_dest_ip->addr, cm->match_dest_port, conn_match_idx)) {
347 entry->match = cm;
348 cm->flow_cookie = conn_match_idx;
349 } else {
350 si->exception_events[SFE_IPV6_EXCEPTION_EVENT_FLOW_COOKIE_ADD_FAIL]++;
351 }
352 }
353 rcu_read_unlock();
354
355 break;
356 }
357 }
358#endif
Xiaoping Fan978b3772015-05-27 14:15:18 -0700359}
360
361/*
362 * sfe_ipv6_remove_connection_match()
363 * Remove a connection match object from the hash.
Xiaoping Fan978b3772015-05-27 14:15:18 -0700364 */
365static inline void sfe_ipv6_remove_connection_match(struct sfe_ipv6 *si, struct sfe_ipv6_connection_match *cm)
366{
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530367
368 lockdep_assert_held(&si->lock);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700369#ifdef CONFIG_NF_FLOW_COOKIE
Xiaoping Fan640faf42015-08-28 15:50:55 -0700370 if (si->flow_cookie_enable) {
371 /*
372 * Tell hardware that we no longer need a flow cookie in packet of this flow
373 */
374 unsigned int conn_match_idx;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700375
Xiaoping Fan640faf42015-08-28 15:50:55 -0700376 for (conn_match_idx = 1; conn_match_idx < SFE_FLOW_COOKIE_SIZE; conn_match_idx++) {
377 struct sfe_ipv6_flow_cookie_entry *entry = &si->sfe_flow_cookie_table[conn_match_idx];
Xiaoping Fan978b3772015-05-27 14:15:18 -0700378
Xiaoping Fan640faf42015-08-28 15:50:55 -0700379 if (cm == entry->match) {
380 sfe_ipv6_flow_cookie_set_func_t func;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700381
Xiaoping Fan640faf42015-08-28 15:50:55 -0700382 rcu_read_lock();
383 func = rcu_dereference(si->flow_cookie_set_func);
384 if (func) {
385 func(cm->match_protocol, cm->match_src_ip->addr, cm->match_src_port,
386 cm->match_dest_ip->addr, cm->match_dest_port, 0);
387 }
388 rcu_read_unlock();
389
390 cm->flow_cookie = 0;
391 entry->match = NULL;
392 entry->last_clean_time = jiffies;
393 break;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700394 }
Xiaoping Fan978b3772015-05-27 14:15:18 -0700395 }
396 }
397#endif
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530398 hlist_del_init_rcu(&cm->hnode);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700399
Xiaoping Fan978b3772015-05-27 14:15:18 -0700400}
401
402/*
403 * sfe_ipv6_get_connection_hash()
404 * Generate the hash used in connection lookups.
405 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700406static 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 -0700407 struct sfe_ipv6_addr *dest_ip, __be16 dest_port)
408{
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700409 u32 idx, hash = 0;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700410
411 for (idx = 0; idx < 4; idx++) {
412 hash ^= src_ip->addr[idx] ^ dest_ip->addr[idx];
413 }
414 hash = hash ^ protocol ^ ntohs(src_port ^ dest_port);
415 return ((hash >> SFE_IPV6_CONNECTION_HASH_SHIFT) ^ hash) & SFE_IPV6_CONNECTION_HASH_MASK;
416}
417
418/*
419 * sfe_ipv6_find_connection()
420 * Get the IPv6 connection info that corresponds to a particular 5-tuple.
421 *
422 * On entry we must be holding the lock that protects the hash table.
423 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700424static inline struct sfe_ipv6_connection *sfe_ipv6_find_connection(struct sfe_ipv6 *si, u32 protocol,
Xiaoping Fan978b3772015-05-27 14:15:18 -0700425 struct sfe_ipv6_addr *src_ip, __be16 src_port,
426 struct sfe_ipv6_addr *dest_ip, __be16 dest_port)
427{
428 struct sfe_ipv6_connection *c;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530429
Xiaoping Fan978b3772015-05-27 14:15:18 -0700430 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 +0530431
432 lockdep_assert_held(&si->lock);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700433 c = si->conn_hash[conn_idx];
434
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530435 while (c) {
436 if ((c->src_port == src_port)
437 && (c->dest_port == dest_port)
438 && (sfe_ipv6_addr_equal(c->src_ip, src_ip))
439 && (sfe_ipv6_addr_equal(c->dest_ip, dest_ip))
440 && (c->protocol == protocol)) {
441 return c;
442 }
Xiaoping Fan978b3772015-05-27 14:15:18 -0700443 c = c->next;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530444 }
Xiaoping Fan978b3772015-05-27 14:15:18 -0700445
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530446 return NULL;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700447}
448
449/*
Xiaoping Fan978b3772015-05-27 14:15:18 -0700450 * sfe_ipv6_insert_connection()
451 * Insert a connection into the hash.
452 *
453 * On entry we must be holding the lock that protects the hash table.
454 */
455static void sfe_ipv6_insert_connection(struct sfe_ipv6 *si, struct sfe_ipv6_connection *c)
456{
457 struct sfe_ipv6_connection **hash_head;
458 struct sfe_ipv6_connection *prev_head;
459 unsigned int conn_idx;
460
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530461 lockdep_assert_held(&si->lock);
462
Xiaoping Fan978b3772015-05-27 14:15:18 -0700463 /*
464 * Insert entry into the connection hash.
465 */
466 conn_idx = sfe_ipv6_get_connection_hash(c->protocol, c->src_ip, c->src_port,
467 c->dest_ip, c->dest_port);
468 hash_head = &si->conn_hash[conn_idx];
469 prev_head = *hash_head;
470 c->prev = NULL;
471 if (prev_head) {
472 prev_head->prev = c;
473 }
474
475 c->next = prev_head;
476 *hash_head = c;
477
478 /*
479 * Insert entry into the "all connections" list.
480 */
481 if (si->all_connections_tail) {
482 c->all_connections_prev = si->all_connections_tail;
483 si->all_connections_tail->all_connections_next = c;
484 } else {
485 c->all_connections_prev = NULL;
486 si->all_connections_head = c;
487 }
488
489 si->all_connections_tail = c;
490 c->all_connections_next = NULL;
491 si->num_connections++;
492
493 /*
494 * Insert the connection match objects too.
495 */
496 sfe_ipv6_insert_connection_match(si, c->original_match);
497 sfe_ipv6_insert_connection_match(si, c->reply_match);
498}
499
500/*
501 * sfe_ipv6_remove_connection()
502 * Remove a sfe_ipv6_connection object from the hash.
503 *
504 * On entry we must be holding the lock that protects the hash table.
505 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530506bool sfe_ipv6_remove_connection(struct sfe_ipv6 *si, struct sfe_ipv6_connection *c)
Xiaoping Fan978b3772015-05-27 14:15:18 -0700507{
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530508
509 lockdep_assert_held(&si->lock);
510 if (c->removed) {
511 DEBUG_ERROR("%px: Connection has been removed already\n", c);
512 return false;
513 }
514
Xiaoping Fan978b3772015-05-27 14:15:18 -0700515 /*
516 * Remove the connection match objects.
517 */
518 sfe_ipv6_remove_connection_match(si, c->reply_match);
519 sfe_ipv6_remove_connection_match(si, c->original_match);
520
521 /*
522 * Unlink the connection.
523 */
524 if (c->prev) {
525 c->prev->next = c->next;
526 } else {
527 unsigned int conn_idx = sfe_ipv6_get_connection_hash(c->protocol, c->src_ip, c->src_port,
528 c->dest_ip, c->dest_port);
529 si->conn_hash[conn_idx] = c->next;
530 }
531
532 if (c->next) {
533 c->next->prev = c->prev;
534 }
Xiaoping Fan34586472015-07-03 02:20:35 -0700535
536 /*
537 * Unlink connection from all_connections list
538 */
539 if (c->all_connections_prev) {
540 c->all_connections_prev->all_connections_next = c->all_connections_next;
541 } else {
542 si->all_connections_head = c->all_connections_next;
543 }
544
545 if (c->all_connections_next) {
546 c->all_connections_next->all_connections_prev = c->all_connections_prev;
547 } else {
548 si->all_connections_tail = c->all_connections_prev;
549 }
550
Ken Zhu32b95392021-09-03 13:52:04 -0700551 /*
552 * If I am the next sync connection, move the sync to my next or head.
553 */
554 if (unlikely(si->wc_next == c)) {
555 si->wc_next = c->all_connections_next;
556 }
557
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530558 c->removed = true;
Xiaoping Fan34586472015-07-03 02:20:35 -0700559 si->num_connections--;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530560 return true;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700561}
562
563/*
564 * sfe_ipv6_gen_sync_connection()
565 * Sync a connection.
566 *
567 * On entry to this function we expect that the lock for the connection is either
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530568 * already held (while called from sfe_ipv6_periodic_sync() or isn't required
569 * (while called from sfe_ipv6_flush_sfe_ipv6_connection())
Xiaoping Fan978b3772015-05-27 14:15:18 -0700570 */
571static void sfe_ipv6_gen_sync_connection(struct sfe_ipv6 *si, struct sfe_ipv6_connection *c,
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700572 struct sfe_connection_sync *sis, sfe_sync_reason_t reason,
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700573 u64 now_jiffies)
Xiaoping Fan978b3772015-05-27 14:15:18 -0700574{
575 struct sfe_ipv6_connection_match *original_cm;
576 struct sfe_ipv6_connection_match *reply_cm;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530577 u32 packet_count, byte_count;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700578
579 /*
580 * Fill in the update message.
581 */
Murat Sezgin53509a12016-12-27 16:57:34 -0800582 sis->is_v6 = 1;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700583 sis->protocol = c->protocol;
584 sis->src_ip.ip6[0] = c->src_ip[0];
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700585 sis->src_ip_xlate.ip6[0] = c->src_ip_xlate[0];
Xiaoping Fan978b3772015-05-27 14:15:18 -0700586 sis->dest_ip.ip6[0] = c->dest_ip[0];
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700587 sis->dest_ip_xlate.ip6[0] = c->dest_ip_xlate[0];
Xiaoping Fan978b3772015-05-27 14:15:18 -0700588 sis->src_port = c->src_port;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700589 sis->src_port_xlate = c->src_port_xlate;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700590 sis->dest_port = c->dest_port;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700591 sis->dest_port_xlate = c->dest_port_xlate;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700592
593 original_cm = c->original_match;
594 reply_cm = c->reply_match;
595 sis->src_td_max_window = original_cm->protocol_state.tcp.max_win;
596 sis->src_td_end = original_cm->protocol_state.tcp.end;
597 sis->src_td_max_end = original_cm->protocol_state.tcp.max_end;
598 sis->dest_td_max_window = reply_cm->protocol_state.tcp.max_win;
599 sis->dest_td_end = reply_cm->protocol_state.tcp.end;
600 sis->dest_td_max_end = reply_cm->protocol_state.tcp.max_end;
601
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530602 sfe_ipv6_connection_match_update_summary_stats(original_cm, &packet_count, &byte_count);
603 sis->src_new_packet_count = packet_count;
604 sis->src_new_byte_count = byte_count;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700605
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530606 sfe_ipv6_connection_match_update_summary_stats(reply_cm, &packet_count, &byte_count);
607 sis->dest_new_packet_count = packet_count;
608 sis->dest_new_byte_count = byte_count;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700609
610 sis->src_dev = original_cm->match_dev;
611 sis->src_packet_count = original_cm->rx_packet_count64;
612 sis->src_byte_count = original_cm->rx_byte_count64;
613
614 sis->dest_dev = reply_cm->match_dev;
615 sis->dest_packet_count = reply_cm->rx_packet_count64;
616 sis->dest_byte_count = reply_cm->rx_byte_count64;
617
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700618 sis->reason = reason;
619
Xiaoping Fan978b3772015-05-27 14:15:18 -0700620 /*
621 * Get the time increment since our last sync.
622 */
623 sis->delta_jiffies = now_jiffies - c->last_sync_jiffies;
624 c->last_sync_jiffies = now_jiffies;
625}
626
627/*
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530628 * sfe_ipv6_free_sfe_ipv6_connection_rcu()
629 * Called at RCU qs state to free the connection object.
630 */
631static void sfe_ipv6_free_sfe_ipv6_connection_rcu(struct rcu_head *head)
632{
633 struct sfe_ipv6_connection *c;
Suruchi Suman23a279d2021-11-16 15:13:09 +0530634 struct udp_sock *up;
635 struct sock *sk;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530636
637 /*
638 * We dont need spin lock as the connection is already removed from link list
639 */
640 c = container_of(head, struct sfe_ipv6_connection, rcu);
641 BUG_ON(!c->removed);
642
643 DEBUG_TRACE("%px: connecton has been deleted\n", c);
644
645 /*
Suruchi Suman23a279d2021-11-16 15:13:09 +0530646 * Decrease the refcount taken in function sfe_ipv6_create_rule()
647 * during call of __udp6_lib_lookup()
648 */
649 up = c->reply_match->up;
650 if (up) {
651 sk = (struct sock *)up;
652 sock_put(sk);
653 }
654
655 /*
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530656 * Release our hold of the source and dest devices and free the memory
657 * for our connection objects.
658 */
659 dev_put(c->original_dev);
660 dev_put(c->reply_dev);
661 kfree(c->original_match);
662 kfree(c->reply_match);
663 kfree(c);
664}
665
666/*
Ken Zhu88c58152021-12-09 15:12:06 -0800667 * sfe_ipv6_sync_status()
668 * update a connection status to its connection manager.
669 *
670 * si: the ipv6 context
671 * c: which connection to be notified
672 * reason: what kind of reason: flush, or destroy
673 */
674void sfe_ipv6_sync_status(struct sfe_ipv6 *si,
675 struct sfe_ipv6_connection *c,
676 sfe_sync_reason_t reason)
677{
678 struct sfe_connection_sync sis;
679 u64 now_jiffies;
680 sfe_sync_rule_callback_t sync_rule_callback;
681
682 rcu_read_lock();
683 sync_rule_callback = rcu_dereference(si->sync_rule_callback);
684
685 if (unlikely(!sync_rule_callback)) {
686 rcu_read_unlock();
687 return;
688 }
689
690 /*
691 * Generate a sync message and then sync.
692 */
693 now_jiffies = get_jiffies_64();
694 sfe_ipv6_gen_sync_connection(si, c, &sis, reason, now_jiffies);
695 sync_rule_callback(&sis);
696
697 rcu_read_unlock();
698}
699
700/*
Xiaoping Fan978b3772015-05-27 14:15:18 -0700701 * sfe_ipv6_flush_connection()
702 * Flush a connection and free all associated resources.
703 *
704 * We need to be called with bottom halves disabled locally as we need to acquire
705 * the connection hash lock and release it again. In general we're actually called
706 * from within a BH and so we're fine, but we're also called when connections are
707 * torn down.
708 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530709void sfe_ipv6_flush_connection(struct sfe_ipv6 *si,
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700710 struct sfe_ipv6_connection *c,
711 sfe_sync_reason_t reason)
Xiaoping Fan978b3772015-05-27 14:15:18 -0700712{
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530713 BUG_ON(!c->removed);
714
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530715 this_cpu_inc(si->stats_pcpu->connection_flushes64);
Ken Zhu88c58152021-12-09 15:12:06 -0800716 sfe_ipv6_sync_status(si, c, reason);
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530717
718 /*
Ken Zhu88c58152021-12-09 15:12:06 -0800719 * Release our hold of the source and dest devices and free the memory
720 * for our connection objects.
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530721 */
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530722 call_rcu(&c->rcu, sfe_ipv6_free_sfe_ipv6_connection_rcu);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700723}
724
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530725 /*
726 * sfe_ipv6_exception_stats_inc()
727 * Increment exception stats.
728 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530729void sfe_ipv6_exception_stats_inc(struct sfe_ipv6 *si, enum sfe_ipv6_exception_events reason)
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530730{
731 struct sfe_ipv6_stats *stats = this_cpu_ptr(si->stats_pcpu);
732
733 stats->exception_events64[reason]++;
734 stats->packets_not_forwarded64++;
735}
736
Xiaoping Fan978b3772015-05-27 14:15:18 -0700737/*
Xiaoping Fan978b3772015-05-27 14:15:18 -0700738 * sfe_ipv6_recv()
739 * Handle packet receives and forwaring.
740 *
741 * Returns 1 if the packet is forwarded or 0 if it isn't.
742 */
Suruchi Suman23a279d2021-11-16 15:13:09 +0530743int 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 -0700744{
745 struct sfe_ipv6 *si = &__si6;
746 unsigned int len;
747 unsigned int payload_len;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530748 unsigned int ihl = sizeof(struct ipv6hdr);
Ken Zhu88c58152021-12-09 15:12:06 -0800749 bool sync_on_find = false;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530750 struct ipv6hdr *iph;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700751 u8 next_hdr;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700752
753 /*
754 * Check that we have space for an IP header and an uplayer header here.
755 */
756 len = skb->len;
757 if (!pskb_may_pull(skb, ihl + sizeof(struct sfe_ipv6_ext_hdr))) {
Xiaoping Fan978b3772015-05-27 14:15:18 -0700758
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530759 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_HEADER_INCOMPLETE);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700760 DEBUG_TRACE("len: %u is too short\n", len);
761 return 0;
762 }
763
764 /*
765 * Is our IP version wrong?
766 */
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530767 iph = (struct ipv6hdr *)skb->data;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700768 if (unlikely(iph->version != 6)) {
Xiaoping Fan978b3772015-05-27 14:15:18 -0700769
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530770 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_NON_V6);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700771 DEBUG_TRACE("IP version: %u\n", iph->version);
772 return 0;
773 }
774
775 /*
776 * Does our datagram fit inside the skb?
777 */
778 payload_len = ntohs(iph->payload_len);
779 if (unlikely(payload_len > (len - ihl))) {
Xiaoping Fan978b3772015-05-27 14:15:18 -0700780
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530781 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_DATAGRAM_INCOMPLETE);
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530782 DEBUG_TRACE("payload_len: %u, exceeds len: %u\n", payload_len, (len - (unsigned int)sizeof(struct ipv6hdr)));
Xiaoping Fan978b3772015-05-27 14:15:18 -0700783 return 0;
784 }
785
786 next_hdr = iph->nexthdr;
787 while (unlikely(sfe_ipv6_is_ext_hdr(next_hdr))) {
788 struct sfe_ipv6_ext_hdr *ext_hdr;
789 unsigned int ext_hdr_len;
790
791 ext_hdr = (struct sfe_ipv6_ext_hdr *)(skb->data + ihl);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700792
793 ext_hdr_len = ext_hdr->hdr_len;
794 ext_hdr_len <<= 3;
795 ext_hdr_len += sizeof(struct sfe_ipv6_ext_hdr);
796 ihl += ext_hdr_len;
797 if (!pskb_may_pull(skb, ihl + sizeof(struct sfe_ipv6_ext_hdr))) {
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530798 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_HEADER_INCOMPLETE);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700799
800 DEBUG_TRACE("extension header %d not completed\n", next_hdr);
801 return 0;
802 }
Ken Zhu88c58152021-12-09 15:12:06 -0800803 /*
804 * Any packets have extend hdr, won't be handled in the fast
805 * path,sync its status and exception to the kernel.
806 */
807 sync_on_find = true;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700808 next_hdr = ext_hdr->next_hdr;
809 }
810
811 if (IPPROTO_UDP == next_hdr) {
Ken Zhu88c58152021-12-09 15:12:06 -0800812 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 -0700813 }
814
815 if (IPPROTO_TCP == next_hdr) {
Ken Zhu88c58152021-12-09 15:12:06 -0800816 return sfe_ipv6_recv_tcp(si, skb, dev, len, iph, ihl, sync_on_find, l2_info);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700817 }
818
819 if (IPPROTO_ICMPV6 == next_hdr) {
820 return sfe_ipv6_recv_icmp(si, skb, dev, len, iph, ihl);
821 }
822
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530823 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_UNHANDLED_PROTOCOL);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700824 DEBUG_TRACE("not UDP, TCP or ICMP: %u\n", next_hdr);
825 return 0;
826}
827
828/*
829 * sfe_ipv6_update_tcp_state()
830 * update TCP window variables.
831 */
832static void
833sfe_ipv6_update_tcp_state(struct sfe_ipv6_connection *c,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530834 struct sfe_ipv6_rule_create_msg *msg)
Xiaoping Fan978b3772015-05-27 14:15:18 -0700835{
836 struct sfe_ipv6_connection_match *orig_cm;
837 struct sfe_ipv6_connection_match *repl_cm;
838 struct sfe_ipv6_tcp_connection_match *orig_tcp;
839 struct sfe_ipv6_tcp_connection_match *repl_tcp;
840
841 orig_cm = c->original_match;
842 repl_cm = c->reply_match;
843 orig_tcp = &orig_cm->protocol_state.tcp;
844 repl_tcp = &repl_cm->protocol_state.tcp;
845
846 /* update orig */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530847 if (orig_tcp->max_win < msg->tcp_rule.flow_max_window) {
848 orig_tcp->max_win = msg->tcp_rule.flow_max_window;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700849 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530850 if ((s32)(orig_tcp->end - msg->tcp_rule.flow_end) < 0) {
851 orig_tcp->end = msg->tcp_rule.flow_end;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700852 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530853 if ((s32)(orig_tcp->max_end - msg->tcp_rule.flow_max_end) < 0) {
854 orig_tcp->max_end = msg->tcp_rule.flow_max_end;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700855 }
856
857 /* update reply */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530858 if (repl_tcp->max_win < msg->tcp_rule.return_max_window) {
859 repl_tcp->max_win = msg->tcp_rule.return_max_window;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700860 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530861 if ((s32)(repl_tcp->end - msg->tcp_rule.return_end) < 0) {
862 repl_tcp->end = msg->tcp_rule.return_end;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700863 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530864 if ((s32)(repl_tcp->max_end - msg->tcp_rule.return_max_end) < 0) {
865 repl_tcp->max_end = msg->tcp_rule.return_max_end;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700866 }
867
868 /* update match flags */
869 orig_cm->flags &= ~SFE_IPV6_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
870 repl_cm->flags &= ~SFE_IPV6_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530871 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_NO_SEQ_CHECK) {
Xiaoping Fan978b3772015-05-27 14:15:18 -0700872 orig_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
873 repl_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
874 }
875}
876
877/*
878 * sfe_ipv6_update_protocol_state()
879 * update protocol specified state machine.
880 */
881static void
882sfe_ipv6_update_protocol_state(struct sfe_ipv6_connection *c,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530883 struct sfe_ipv6_rule_create_msg *msg)
Xiaoping Fan978b3772015-05-27 14:15:18 -0700884{
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530885 switch (msg->tuple.protocol) {
Xiaoping Fan978b3772015-05-27 14:15:18 -0700886 case IPPROTO_TCP:
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530887 sfe_ipv6_update_tcp_state(c, msg);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700888 break;
889 }
890}
891
892/*
Wayne Tanbb7f1782021-12-13 11:16:04 -0800893 * sfe_ipv6_match_entry_set_vlan()
894 */
895static void sfe_ipv6_match_entry_set_vlan(
896 struct sfe_ipv6_connection_match *cm,
897 u32 primary_ingress_vlan_tag,
898 u32 primary_egress_vlan_tag,
899 u32 secondary_ingress_vlan_tag,
900 u32 secondary_egress_vlan_tag)
901{
902 u16 tpid;
903 /*
904 * Prevent stacking header counts when updating.
905 */
906 cm->ingress_vlan_hdr_cnt = 0;
907 cm->egress_vlan_hdr_cnt = 0;
908 memset(cm->ingress_vlan_hdr, 0, sizeof(cm->ingress_vlan_hdr));
909 memset(cm->egress_vlan_hdr, 0, sizeof(cm->egress_vlan_hdr));
910
911 /*
912 * vlan_hdr[0] corresponds to outer tag
913 * vlan_hdr[1] corresponds to inner tag
914 * Extract the vlan information (tpid and tci) from rule message
915 */
916 if ((primary_ingress_vlan_tag & VLAN_VID_MASK) != SFE_VLAN_ID_NOT_CONFIGURED) {
917 tpid = (u16)(primary_ingress_vlan_tag >> 16);
918 cm->ingress_vlan_hdr[0].tpid = ntohs(tpid);
919 cm->ingress_vlan_hdr[0].tci = (u16)primary_ingress_vlan_tag;
920 cm->ingress_vlan_hdr_cnt++;
921 }
922
923 if ((secondary_ingress_vlan_tag & VLAN_VID_MASK) != SFE_VLAN_ID_NOT_CONFIGURED) {
924 tpid = (u16)(secondary_ingress_vlan_tag >> 16);
925 cm->ingress_vlan_hdr[1].tpid = ntohs(tpid);
926 cm->ingress_vlan_hdr[1].tci = (u16)secondary_ingress_vlan_tag;
927 cm->ingress_vlan_hdr_cnt++;
928 }
929
930 if ((primary_egress_vlan_tag & VLAN_VID_MASK) != SFE_VLAN_ID_NOT_CONFIGURED) {
931 tpid = (u16)(primary_egress_vlan_tag >> 16);
932 cm->egress_vlan_hdr[0].tpid = ntohs(tpid);
933 cm->egress_vlan_hdr[0].tci = (u16)primary_egress_vlan_tag;
934 cm->egress_vlan_hdr_cnt++;
935 }
936
937 if ((secondary_egress_vlan_tag & VLAN_VID_MASK) != SFE_VLAN_ID_NOT_CONFIGURED) {
938 tpid = (u16)(secondary_egress_vlan_tag >> 16);
939 cm->egress_vlan_hdr[1].tpid = ntohs(tpid);
940 cm->egress_vlan_hdr[1].tci = (u16)secondary_egress_vlan_tag;
941 cm->egress_vlan_hdr_cnt++;
942 }
943}
944
945/*
Xiaoping Fan978b3772015-05-27 14:15:18 -0700946 * sfe_ipv6_update_rule()
947 * update forwarding rule after rule is created.
948 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530949void sfe_ipv6_update_rule(struct sfe_ipv6_rule_create_msg *msg)
950
Xiaoping Fan978b3772015-05-27 14:15:18 -0700951{
952 struct sfe_ipv6_connection *c;
953 struct sfe_ipv6 *si = &__si6;
954
955 spin_lock_bh(&si->lock);
956
957 c = sfe_ipv6_find_connection(si,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530958 msg->tuple.protocol,
959 (struct sfe_ipv6_addr *)msg->tuple.flow_ip,
960 msg->tuple.flow_ident,
961 (struct sfe_ipv6_addr *)msg->tuple.return_ip,
962 msg->tuple.return_ident);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700963 if (c != NULL) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530964 sfe_ipv6_update_protocol_state(c, msg);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700965 }
966
967 spin_unlock_bh(&si->lock);
968}
969
970/*
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +0530971 * sfe_ipv6_xmit_eth_type_check
972 * Checking if MAC header has to be written.
973 */
974static inline bool sfe_ipv6_xmit_eth_type_check(struct net_device *dev, u32 cm_flags)
975{
976 if (!(dev->flags & IFF_NOARP)) {
977 return true;
978 }
979
980 /*
981 * For PPPoE, since we are now supporting PPPoE encapsulation, we are writing L2 header.
982 */
983 if (cm_flags & SFE_IPV6_CONNECTION_MATCH_FLAG_PPPOE_ENCAP) {
984 return true;
985 }
986
987 return false;
988}
989
990/*
Xiaoping Fan978b3772015-05-27 14:15:18 -0700991 * sfe_ipv6_create_rule()
992 * Create a forwarding rule.
993 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530994int sfe_ipv6_create_rule(struct sfe_ipv6_rule_create_msg *msg)
Xiaoping Fan978b3772015-05-27 14:15:18 -0700995{
996 struct sfe_ipv6 *si = &__si6;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530997 struct sfe_ipv6_connection *c, *old_c;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700998 struct sfe_ipv6_connection_match *original_cm;
999 struct sfe_ipv6_connection_match *reply_cm;
1000 struct net_device *dest_dev;
1001 struct net_device *src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301002 struct sfe_ipv6_5tuple *tuple = &msg->tuple;
Suruchi Suman23a279d2021-11-16 15:13:09 +05301003 struct sock *sk;
1004 struct net *net;
1005 unsigned int src_if_idx;
1006
Suruchi Sumanc1a4a612021-10-21 14:50:23 +05301007 s32 flow_interface_num = msg->conn_rule.flow_top_interface_num;
1008 s32 return_interface_num = msg->conn_rule.return_top_interface_num;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001009
Suruchi Sumanc1a4a612021-10-21 14:50:23 +05301010 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_FLOW_BOTTOM_INTERFACE) {
1011 flow_interface_num = msg->conn_rule.flow_interface_num;
1012 }
1013
1014 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_RETURN_BOTTOM_INTERFACE) {
1015 return_interface_num = msg->conn_rule.return_interface_num;
1016 }
1017
1018 src_dev = dev_get_by_index(&init_net, flow_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301019 if (!src_dev) {
1020 DEBUG_WARN("%px: Unable to find src_dev corresponding to %d\n", msg,
Suruchi Sumanc1a4a612021-10-21 14:50:23 +05301021 flow_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301022 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
1023 return -EINVAL;
1024 }
1025
Suruchi Sumanc1a4a612021-10-21 14:50:23 +05301026 dest_dev = dev_get_by_index(&init_net, return_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301027 if (!dest_dev) {
1028 DEBUG_WARN("%px: Unable to find dest_dev corresponding to %d\n", msg,
Suruchi Sumanc1a4a612021-10-21 14:50:23 +05301029 return_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301030 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
1031 dev_put(src_dev);
1032 return -EINVAL;
1033 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07001034
1035 if (unlikely((dest_dev->reg_state != NETREG_REGISTERED) ||
1036 (src_dev->reg_state != NETREG_REGISTERED))) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301037 DEBUG_WARN("%px: src_dev=%s and dest_dev=%s are unregistered\n", msg,
1038 src_dev->name, dest_dev->name);
1039 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
1040 dev_put(src_dev);
1041 dev_put(dest_dev);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001042 return -EINVAL;
1043 }
1044
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301045 /*
1046 * Allocate the various connection tracking objects.
1047 */
1048 c = (struct sfe_ipv6_connection *)kmalloc(sizeof(struct sfe_ipv6_connection), GFP_ATOMIC);
1049 if (unlikely(!c)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301050 DEBUG_WARN("%px: memory allocation of connection entry failed\n", msg);
1051 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
1052 dev_put(src_dev);
1053 dev_put(dest_dev);
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301054 return -ENOMEM;
1055 }
1056
1057 original_cm = (struct sfe_ipv6_connection_match *)kmalloc(sizeof(struct sfe_ipv6_connection_match), GFP_ATOMIC);
1058 if (unlikely(!original_cm)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301059 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
1060 DEBUG_WARN("%px: memory allocation of connection match entry failed\n", msg);
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301061 kfree(c);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301062 dev_put(src_dev);
1063 dev_put(dest_dev);
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301064 return -ENOMEM;
1065 }
1066
1067 reply_cm = (struct sfe_ipv6_connection_match *)kmalloc(sizeof(struct sfe_ipv6_connection_match), GFP_ATOMIC);
1068 if (unlikely(!reply_cm)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301069 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
1070 DEBUG_WARN("%px: memory allocation of connection match entry failed\n", msg);
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301071 kfree(original_cm);
1072 kfree(c);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301073 dev_put(src_dev);
1074 dev_put(dest_dev);
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301075 return -ENOMEM;
1076 }
1077
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301078 this_cpu_inc(si->stats_pcpu->connection_create_requests64);
1079
Xiaoping Fan978b3772015-05-27 14:15:18 -07001080 spin_lock_bh(&si->lock);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001081
1082 /*
1083 * Check to see if there is already a flow that matches the rule we're
1084 * trying to create. If there is then we can't create a new one.
1085 */
Wayne Tanbb7f1782021-12-13 11:16:04 -08001086 old_c = sfe_ipv6_find_connection(si,
1087 tuple->protocol,
1088 (struct sfe_ipv6_addr *)tuple->flow_ip,
1089 tuple->flow_ident,
1090 (struct sfe_ipv6_addr *)tuple->return_ip,
1091 tuple->return_ident);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301092
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301093 if (old_c != NULL) {
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301094 this_cpu_inc(si->stats_pcpu->connection_create_collisions64);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001095
1096 /*
1097 * If we already have the flow then it's likely that this
1098 * request to create the connection rule contains more
1099 * up-to-date information. Check and update accordingly.
1100 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301101 sfe_ipv6_update_protocol_state(old_c, msg);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001102 spin_unlock_bh(&si->lock);
1103
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301104 kfree(reply_cm);
1105 kfree(original_cm);
1106 kfree(c);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301107 dev_put(src_dev);
1108 dev_put(dest_dev);
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301109
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301110 DEBUG_TRACE("connection already exists - p: %d\n"
Tian Yang45f39c82020-10-06 14:07:47 -07001111 " s: %s:%pxM:%pI6:%u, d: %s:%pxM:%pI6:%u\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301112 tuple->protocol,
1113 src_dev->name, msg->conn_rule.flow_mac, tuple->flow_ip, ntohs(tuple->flow_ident),
1114 dest_dev->name, msg->conn_rule.return_mac, tuple->return_ip, ntohs(tuple->return_ident));
Xiaoping Fan978b3772015-05-27 14:15:18 -07001115 return -EADDRINUSE;
1116 }
1117
1118 /*
Xiaoping Fan978b3772015-05-27 14:15:18 -07001119 * Fill in the "original" direction connection matching object.
1120 * Note that the transmit MAC address is "dest_mac_xlate" because
1121 * we always know both ends of a connection by their translated
1122 * addresses and not their public addresses.
1123 */
1124 original_cm->match_dev = src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301125 original_cm->match_protocol = tuple->protocol;
1126 original_cm->match_src_ip[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip;
Suruchi Suman66609a72022-01-20 02:34:25 +05301127 original_cm->match_src_port = netif_is_vxlan(src_dev) ? 0 : tuple->flow_ident;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301128 original_cm->match_dest_ip[0] = *(struct sfe_ipv6_addr *)tuple->return_ip;
1129 original_cm->match_dest_port = tuple->return_ident;
1130
1131 original_cm->xlate_src_ip[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip;
1132 original_cm->xlate_src_port = tuple->flow_ident;
1133 original_cm->xlate_dest_ip[0] = *(struct sfe_ipv6_addr *)tuple->return_ip;
1134 original_cm->xlate_dest_port = tuple->return_ident;
1135
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301136 atomic_set(&original_cm->rx_packet_count, 0);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001137 original_cm->rx_packet_count64 = 0;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301138 atomic_set(&original_cm->rx_byte_count, 0);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001139 original_cm->rx_byte_count64 = 0;
1140 original_cm->xmit_dev = dest_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301141
1142 original_cm->xmit_dev_mtu = msg->conn_rule.return_mtu;
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301143
Xiaoping Fan978b3772015-05-27 14:15:18 -07001144 original_cm->connection = c;
1145 original_cm->counter_match = reply_cm;
Wayne Tanbb7f1782021-12-13 11:16:04 -08001146 original_cm->l2_hdr_size = 0;
1147 original_cm->flags = 0;
Suruchi Suman23a279d2021-11-16 15:13:09 +05301148
1149 /*
1150 * Valid in decap direction only
1151 */
1152 RCU_INIT_POINTER(original_cm->up, NULL);
1153
Ken Zhu37040ea2021-09-09 21:11:15 -07001154 if (msg->valid_flags & SFE_RULE_CREATE_MARK_VALID) {
1155 original_cm->mark = msg->mark_rule.flow_mark;
1156 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_MARK;
1157 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301158 if (msg->valid_flags & SFE_RULE_CREATE_QOS_VALID) {
1159 original_cm->priority = msg->qos_rule.flow_qos_tag;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001160 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_PRIORITY_REMARK;
1161 }
Wayne Tanbb7f1782021-12-13 11:16:04 -08001162
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301163 if (msg->valid_flags & SFE_RULE_CREATE_DSCP_MARKING_VALID) {
1164 original_cm->dscp = msg->dscp_rule.flow_dscp << SFE_IPV6_DSCP_SHIFT;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001165 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_DSCP_REMARK;
1166 }
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301167 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1168 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_BRIDGE_FLOW;
1169 }
1170
Wayne Tanbb7f1782021-12-13 11:16:04 -08001171 /*
1172 * Add VLAN rule to original_cm
1173 */
1174 if (msg->valid_flags & SFE_RULE_CREATE_VLAN_VALID) {
1175 struct sfe_vlan_rule *vlan_primary_rule = &msg->vlan_primary_rule;
1176 struct sfe_vlan_rule *vlan_secondary_rule = &msg->vlan_secondary_rule;
1177 sfe_ipv6_match_entry_set_vlan(original_cm,
1178 vlan_primary_rule->ingress_vlan_tag,
1179 vlan_primary_rule->egress_vlan_tag,
1180 vlan_secondary_rule->ingress_vlan_tag,
1181 vlan_secondary_rule->egress_vlan_tag);
1182
1183 if ((msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_RETURN_BOTTOM_INTERFACE) &&
1184 original_cm->egress_vlan_hdr_cnt > 0) {
1185 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_INSERT_EGRESS_VLAN_TAG;
1186 original_cm->l2_hdr_size += original_cm->egress_vlan_hdr_cnt * VLAN_HLEN;
1187 }
1188 }
1189
Xiaoping Fan978b3772015-05-27 14:15:18 -07001190#ifdef CONFIG_NF_FLOW_COOKIE
1191 original_cm->flow_cookie = 0;
1192#endif
Zhi Chen8748eb32015-06-18 12:58:48 -07001193#ifdef CONFIG_XFRM
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301194 if (msg->valid_flags & SFE_RULE_CREATE_DIRECTION_VALID) {
1195 original_cm->flow_accel = msg->direction_rule.flow_accel;
1196 } else {
1197 original_cm->flow_accel = 1;
1198 }
Zhi Chen8748eb32015-06-18 12:58:48 -07001199#endif
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301200 /*
1201 * If l2_features are disabled and flow uses l2 features such as macvlan/bridge/pppoe/vlan,
1202 * bottom interfaces are expected to be disabled in the flow rule and always top interfaces
1203 * are used. In such cases, do not use HW csum offload. csum offload is used only when we
1204 * are sending directly to the destination interface that supports it.
1205 */
Suruchi Sumanf2077182022-01-13 21:35:23 +05301206 if (likely(dest_dev->features & NETIF_F_HW_CSUM) && !netif_is_vxlan(dest_dev)) {
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301207 if ((msg->conn_rule.return_top_interface_num == msg->conn_rule.return_interface_num) ||
1208 (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_RETURN_BOTTOM_INTERFACE)) {
1209 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_CSUM_OFFLOAD;
1210 }
1211 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07001212
Wayne Tanbb7f1782021-12-13 11:16:04 -08001213 reply_cm->l2_hdr_size = 0;
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +05301214 reply_cm->flags = 0;
1215
1216 /*
1217 * Adding PPPoE parameters to original and reply entries based on the direction where
1218 * PPPoE header is valid in ECM rule.
1219 *
1220 * If PPPoE is valid in flow direction (from interface is PPPoE), then
1221 * original cm will have PPPoE at ingress (strip PPPoE header)
1222 * reply cm will have PPPoE at egress (add PPPoE header)
1223 *
1224 * If PPPoE is valid in return direction (to interface is PPPoE), then
1225 * original cm will have PPPoE at egress (add PPPoE header)
1226 * reply cm will have PPPoE at ingress (strip PPPoE header)
1227 */
1228 if (msg->valid_flags & SFE_RULE_CREATE_PPPOE_DECAP_VALID) {
1229 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_PPPOE_DECAP;
1230 original_cm->pppoe_session_id = msg->pppoe_rule.flow_pppoe_session_id;
1231 ether_addr_copy(original_cm->pppoe_remote_mac, msg->pppoe_rule.flow_pppoe_remote_mac);
1232
1233 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_PPPOE_ENCAP;
Wayne Tanbb7f1782021-12-13 11:16:04 -08001234 reply_cm->l2_hdr_size += SFE_PPPOE_SESSION_HEADER_SIZE;
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +05301235 reply_cm->pppoe_session_id = msg->pppoe_rule.flow_pppoe_session_id;
1236 ether_addr_copy(reply_cm->pppoe_remote_mac, msg->pppoe_rule.flow_pppoe_remote_mac);
1237 }
1238
1239 if (msg->valid_flags & SFE_RULE_CREATE_PPPOE_ENCAP_VALID) {
1240 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_PPPOE_ENCAP;
Wayne Tanbb7f1782021-12-13 11:16:04 -08001241 original_cm->l2_hdr_size += SFE_PPPOE_SESSION_HEADER_SIZE;
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +05301242 original_cm->pppoe_session_id = msg->pppoe_rule.return_pppoe_session_id;
1243 ether_addr_copy(original_cm->pppoe_remote_mac, msg->pppoe_rule.return_pppoe_remote_mac);
1244
1245 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_PPPOE_DECAP;
1246 reply_cm->pppoe_session_id = msg->pppoe_rule.return_pppoe_session_id;
1247 ether_addr_copy(reply_cm->pppoe_remote_mac, msg->pppoe_rule.return_pppoe_remote_mac);
1248 }
1249
Ratheesh Kannoth5dee3772022-01-18 11:27:14 +05301250 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_SRC_INTERFACE_CHECK) {
1251 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_SRC_INTERFACE_CHECK;
1252 }
1253
Xiaoping Fan978b3772015-05-27 14:15:18 -07001254 /*
Ken Zhubbf49652021-09-12 15:33:09 -07001255 * For the non-arp interface, we don't write L2 HDR.
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +05301256 * Excluding PPPoE from this, since we are now supporting PPPoE encap/decap.
Xiaoping Fan978b3772015-05-27 14:15:18 -07001257 */
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +05301258 if (sfe_ipv6_xmit_eth_type_check(dest_dev, original_cm->flags)) {
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301259
1260 /*
1261 * Check whether the rule has configured a specific source MAC address to use.
1262 * This is needed when virtual L3 interfaces such as br-lan, macvlan, vlan are used during egress
1263 */
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301264 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1265 ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)msg->conn_rule.flow_mac);
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301266 } else {
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301267 if ((msg->valid_flags & SFE_RULE_CREATE_SRC_MAC_VALID) &&
1268 (msg->src_mac_rule.mac_valid_flags & SFE_SRC_MAC_RETURN_VALID)) {
1269 ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)msg->src_mac_rule.return_src_mac);
1270 } else {
1271 ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)dest_dev->dev_addr);
1272 }
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301273 }
1274 ether_addr_copy((u8 *)original_cm->xmit_dest_mac, (u8 *)msg->conn_rule.return_mac);
1275
Xiaoping Fan978b3772015-05-27 14:15:18 -07001276 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_WRITE_L2_HDR;
Wayne Tanbb7f1782021-12-13 11:16:04 -08001277 original_cm->l2_hdr_size += ETH_HLEN;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001278
1279 /*
1280 * If our dev writes Ethernet headers then we can write a really fast
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301281 * version
Xiaoping Fan978b3772015-05-27 14:15:18 -07001282 */
1283 if (dest_dev->header_ops) {
1284 if (dest_dev->header_ops->create == eth_header) {
1285 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR;
1286 }
1287 }
1288 }
1289
1290 /*
1291 * Fill in the "reply" direction connection matching object.
1292 */
1293 reply_cm->match_dev = dest_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301294 reply_cm->match_protocol = tuple->protocol;
1295 reply_cm->match_src_ip[0] = *(struct sfe_ipv6_addr *)tuple->return_ip;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301296 reply_cm->match_dest_ip[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip;
1297 reply_cm->match_dest_port = tuple->flow_ident;
1298 reply_cm->xlate_src_ip[0] = *(struct sfe_ipv6_addr *)tuple->return_ip;
1299 reply_cm->xlate_src_port = tuple->return_ident;
1300 reply_cm->xlate_dest_ip[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip;
1301 reply_cm->xlate_dest_port = tuple->flow_ident;
1302
Suruchi Suman23a279d2021-11-16 15:13:09 +05301303 /*
1304 * Keep source port as 0 for VxLAN tunnels.
1305 */
1306 if (netif_is_vxlan(src_dev) || netif_is_vxlan(dest_dev)) {
1307 reply_cm->match_src_port = 0;
1308 } else {
1309 reply_cm->match_src_port = tuple->return_ident;
1310 }
1311
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301312 atomic_set(&original_cm->rx_byte_count, 0);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001313 reply_cm->rx_packet_count64 = 0;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301314 atomic_set(&reply_cm->rx_byte_count, 0);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001315 reply_cm->rx_byte_count64 = 0;
1316 reply_cm->xmit_dev = src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301317 reply_cm->xmit_dev_mtu = msg->conn_rule.flow_mtu;
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301318
Xiaoping Fan978b3772015-05-27 14:15:18 -07001319 reply_cm->connection = c;
1320 reply_cm->counter_match = original_cm;
Suruchi Suman23a279d2021-11-16 15:13:09 +05301321
Ken Zhu37040ea2021-09-09 21:11:15 -07001322 if (msg->valid_flags & SFE_RULE_CREATE_MARK_VALID) {
1323 reply_cm->mark = msg->mark_rule.return_mark;
1324 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_MARK;
1325 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301326 if (msg->valid_flags & SFE_RULE_CREATE_QOS_VALID) {
1327 reply_cm->priority = msg->qos_rule.return_qos_tag;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001328 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_PRIORITY_REMARK;
1329 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301330 if (msg->valid_flags & SFE_RULE_CREATE_DSCP_MARKING_VALID) {
1331 reply_cm->dscp = msg->dscp_rule.return_dscp << SFE_IPV6_DSCP_SHIFT;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001332 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_DSCP_REMARK;
1333 }
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301334
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301335 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1336 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_BRIDGE_FLOW;
1337 }
1338
Suruchi Suman23a279d2021-11-16 15:13:09 +05301339 /*
1340 * Setup UDP Socket if found to be valid for decap.
1341 */
1342 RCU_INIT_POINTER(reply_cm->up, NULL);
1343 net = dev_net(reply_cm->match_dev);
1344 src_if_idx = src_dev->ifindex;
1345
1346 rcu_read_lock();
1347
1348 /*
1349 * Look for the associated sock object.
1350 * __udp6_lib_lookup() holds a reference for this sock object,
1351 * which will be released in sfe_ipv6_flush_connection()
1352 */
1353#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
1354 sk = __udp6_lib_lookup(net, (const struct in6_addr *)reply_cm->match_dest_ip,
1355 reply_cm->match_dest_port, (const struct in6_addr *)reply_cm->xlate_src_ip,
1356 reply_cm->xlate_src_port, src_if_idx, &udp_table);
1357#else
1358 sk = __udp6_lib_lookup(net, (const struct in6_addr *)reply_cm->match_dest_ip,
1359 reply_cm->match_dest_port, (const struct in6_addr *)reply_cm->xlate_src_ip,
1360 reply_cm->xlate_src_port, src_if_idx, 0, &udp_table, NULL);
1361#endif
1362 rcu_read_unlock();
1363
1364 /*
1365 * We set the UDP sock pointer as valid only for decap direction.
1366 */
1367 if (sk && udp_sk(sk)->encap_type) {
1368#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
1369 if (!atomic_add_unless(&sk->sk_refcnt, 1, 0)) {
1370#else
1371 if (!refcount_inc_not_zero(&sk->sk_refcnt)) {
1372#endif
Wayne Tanbb7f1782021-12-13 11:16:04 -08001373 spin_unlock_bh(&si->lock);
Suruchi Suman23a279d2021-11-16 15:13:09 +05301374 kfree(reply_cm);
1375 kfree(original_cm);
1376 kfree(c);
1377
1378 DEBUG_INFO("sfe: unable to take reference for socket p:%d\n", tuple->protocol);
1379 DEBUG_INFO("SK: connection - \n"
1380 " s: %s:%pI6(%pI6):%u(%u)\n"
1381 " d: %s:%pI6(%pI6):%u(%u)\n",
1382 reply_cm->match_dev->name, &reply_cm->match_src_ip, &reply_cm->xlate_src_ip,
1383 ntohs(reply_cm->match_src_port), ntohs(reply_cm->xlate_src_port),
1384 reply_cm->xmit_dev->name, &reply_cm->match_dest_ip, &reply_cm->xlate_dest_ip,
1385 ntohs(reply_cm->match_dest_port), ntohs(reply_cm->xlate_dest_port));
1386
1387 dev_put(src_dev);
1388 dev_put(dest_dev);
1389
1390 return -ESHUTDOWN;
1391 }
1392
1393 rcu_assign_pointer(reply_cm->up, udp_sk(sk));
1394 DEBUG_INFO("Sock lookup success with reply_cm direction(%p)\n", sk);
1395 DEBUG_INFO("SK: connection - \n"
1396 " s: %s:%pI6(%pI6):%u(%u)\n"
1397 " d: %s:%pI6(%pI6):%u(%u)\n",
1398 reply_cm->match_dev->name, &reply_cm->match_src_ip, &reply_cm->xlate_src_ip,
1399 ntohs(reply_cm->match_src_port), ntohs(reply_cm->xlate_src_port),
1400 reply_cm->xmit_dev->name, &reply_cm->match_dest_ip, &reply_cm->xlate_dest_ip,
1401 ntohs(reply_cm->match_dest_port), ntohs(reply_cm->xlate_dest_port));
1402 }
1403
Wayne Tanbb7f1782021-12-13 11:16:04 -08001404 /*
1405 * Add VLAN rule to reply_cm
1406 */
1407 if (msg->valid_flags & SFE_RULE_CREATE_VLAN_VALID) {
1408 struct sfe_vlan_rule *vlan_primary_rule = &msg->vlan_primary_rule;
1409 struct sfe_vlan_rule *vlan_secondary_rule = &msg->vlan_secondary_rule;
1410 sfe_ipv6_match_entry_set_vlan(reply_cm,
1411 vlan_primary_rule->egress_vlan_tag,
1412 vlan_primary_rule->ingress_vlan_tag,
1413 vlan_secondary_rule->egress_vlan_tag,
1414 vlan_secondary_rule->ingress_vlan_tag);
1415
1416 if ((msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_FLOW_BOTTOM_INTERFACE) &&
1417 reply_cm->egress_vlan_hdr_cnt > 0) {
1418 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_INSERT_EGRESS_VLAN_TAG;
1419 reply_cm->l2_hdr_size += reply_cm->egress_vlan_hdr_cnt * VLAN_HLEN;
1420 }
1421 }
1422
Xiaoping Fan978b3772015-05-27 14:15:18 -07001423#ifdef CONFIG_NF_FLOW_COOKIE
1424 reply_cm->flow_cookie = 0;
1425#endif
Zhi Chen8748eb32015-06-18 12:58:48 -07001426#ifdef CONFIG_XFRM
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301427 if (msg->valid_flags & SFE_RULE_CREATE_DIRECTION_VALID) {
1428 reply_cm->flow_accel = msg->direction_rule.return_accel;
1429 } else {
1430 reply_cm->flow_accel = 1;
1431 }
Zhi Chen8748eb32015-06-18 12:58:48 -07001432#endif
Xiaoping Fan978b3772015-05-27 14:15:18 -07001433 /*
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301434 * If l2_features are disabled and flow uses l2 features such as macvlan/bridge/pppoe/vlan,
1435 * bottom interfaces are expected to be disabled in the flow rule and always top interfaces
1436 * are used. In such cases, do not use HW csum offload. csum offload is used only when we
1437 * are sending directly to the destination interface that supports it.
1438 */
Suruchi Sumanf2077182022-01-13 21:35:23 +05301439 if (likely(src_dev->features & NETIF_F_HW_CSUM) && !(netif_is_vxlan(src_dev) || netif_is_vxlan(dest_dev))) {
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301440 if ((msg->conn_rule.flow_top_interface_num == msg->conn_rule.flow_interface_num) ||
1441 (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_FLOW_BOTTOM_INTERFACE)) {
1442 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_CSUM_OFFLOAD;
1443 }
1444 }
1445
Ratheesh Kannoth5dee3772022-01-18 11:27:14 +05301446 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_SRC_INTERFACE_CHECK) {
1447 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_SRC_INTERFACE_CHECK;
1448 }
1449
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301450 /*
Ken Zhubbf49652021-09-12 15:33:09 -07001451 * For the non-arp interface, we don't write L2 HDR.
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +05301452 * Excluding PPPoE from this, since we are now supporting PPPoE encap/decap.
Xiaoping Fan978b3772015-05-27 14:15:18 -07001453 */
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +05301454 if (sfe_ipv6_xmit_eth_type_check(src_dev, reply_cm->flags)) {
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301455
1456 /*
1457 * Check whether the rule has configured a specific source MAC address to use.
1458 * This is needed when virtual L3 interfaces such as br-lan, macvlan, vlan are used during egress
1459 */
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301460 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1461 ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)msg->conn_rule.return_mac);
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301462 } else {
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301463 if ((msg->valid_flags & SFE_RULE_CREATE_SRC_MAC_VALID) &&
1464 (msg->src_mac_rule.mac_valid_flags & SFE_SRC_MAC_FLOW_VALID)) {
1465 ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)msg->src_mac_rule.flow_src_mac);
1466 } else {
1467 ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)src_dev->dev_addr);
1468 }
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301469 }
1470
1471 ether_addr_copy((u8 *)reply_cm->xmit_dest_mac, (u8 *)msg->conn_rule.flow_mac);
1472
Xiaoping Fan978b3772015-05-27 14:15:18 -07001473 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_WRITE_L2_HDR;
Wayne Tanbb7f1782021-12-13 11:16:04 -08001474 reply_cm->l2_hdr_size += ETH_HLEN;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001475
1476 /*
1477 * If our dev writes Ethernet headers then we can write a really fast
1478 * version.
1479 */
1480 if (src_dev->header_ops) {
1481 if (src_dev->header_ops->create == eth_header) {
1482 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR;
1483 }
1484 }
1485 }
1486
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301487 /*
1488 * No support for NAT in ipv6
1489 */
Xiaoping Fan978b3772015-05-27 14:15:18 -07001490
Xiaoping Fan978b3772015-05-27 14:15:18 -07001491 /*
Xiaoping Fan978b3772015-05-27 14:15:18 -07001492 * Initialize the protocol-specific information that we track.
1493 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301494 switch (tuple->protocol) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07001495 case IPPROTO_TCP:
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301496 original_cm->protocol_state.tcp.win_scale = msg->tcp_rule.flow_window_scale;
1497 original_cm->protocol_state.tcp.max_win = msg->tcp_rule.flow_max_window ? msg->tcp_rule.flow_max_window : 1;
1498 original_cm->protocol_state.tcp.end = msg->tcp_rule.flow_end;
1499 original_cm->protocol_state.tcp.max_end = msg->tcp_rule.flow_max_end;
1500 reply_cm->protocol_state.tcp.win_scale = msg->tcp_rule.return_window_scale;
1501 reply_cm->protocol_state.tcp.max_win = msg->tcp_rule.return_max_window ? msg->tcp_rule.return_max_window : 1;
1502 reply_cm->protocol_state.tcp.end = msg->tcp_rule.return_end;
1503 reply_cm->protocol_state.tcp.max_end = msg->tcp_rule.return_max_end;
1504 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_NO_SEQ_CHECK) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07001505 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
1506 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
1507 }
1508 break;
1509 }
1510
Wayne Tanbb7f1782021-12-13 11:16:04 -08001511 /*
1512 * Fill in the ipv6_connection object.
1513 */
1514 c->protocol = tuple->protocol;
1515 c->src_ip[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip;
1516 c->src_ip_xlate[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip;
1517 c->src_port = tuple->flow_ident;
1518 c->src_port_xlate = tuple->flow_ident;
1519 c->original_dev = src_dev;
1520 c->original_match = original_cm;
1521
1522 c->dest_ip[0] = *(struct sfe_ipv6_addr *)tuple->return_ip;
1523 c->dest_ip_xlate[0] = *(struct sfe_ipv6_addr *)tuple->return_ip;
1524 c->dest_port = tuple->return_ident;
1525 c->dest_port_xlate = tuple->return_ident;
1526
1527 c->reply_dev = dest_dev;
1528 c->reply_match = reply_cm;
1529 c->debug_read_seq = 0;
1530 c->last_sync_jiffies = get_jiffies_64();
1531 c->removed = false;
1532
Xiaoping Fan978b3772015-05-27 14:15:18 -07001533 sfe_ipv6_connection_match_compute_translations(original_cm);
1534 sfe_ipv6_connection_match_compute_translations(reply_cm);
1535 sfe_ipv6_insert_connection(si, c);
1536
1537 spin_unlock_bh(&si->lock);
1538
1539 /*
1540 * We have everything we need!
1541 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301542 DEBUG_INFO("new connection - p: %d\n"
Tian Yang45f39c82020-10-06 14:07:47 -07001543 " s: %s:%pxM(%pxM):%pI6(%pI6):%u(%u)\n"
1544 " d: %s:%pxM(%pxM):%pI6(%pI6):%u(%u)\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301545 tuple->protocol,
1546 src_dev->name, msg->conn_rule.flow_mac, NULL,
1547 (void *)tuple->flow_ip, (void *)tuple->flow_ip, ntohs(tuple->flow_ident), ntohs(tuple->flow_ident),
1548 dest_dev->name, NULL, msg->conn_rule.return_mac,
1549 (void *)tuple->return_ip, (void *)tuple->return_ip, ntohs(tuple->return_ident), ntohs(tuple->return_ident));
Xiaoping Fan978b3772015-05-27 14:15:18 -07001550
1551 return 0;
1552}
1553
1554/*
1555 * sfe_ipv6_destroy_rule()
1556 * Destroy a forwarding rule.
1557 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301558void sfe_ipv6_destroy_rule(struct sfe_ipv6_rule_destroy_msg *msg)
Xiaoping Fan978b3772015-05-27 14:15:18 -07001559{
1560 struct sfe_ipv6 *si = &__si6;
1561 struct sfe_ipv6_connection *c;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301562 bool ret;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301563 struct sfe_ipv6_5tuple *tuple = &msg->tuple;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001564
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301565 this_cpu_inc(si->stats_pcpu->connection_destroy_requests64);
1566
Xiaoping Fan978b3772015-05-27 14:15:18 -07001567 spin_lock_bh(&si->lock);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001568
1569 /*
1570 * Check to see if we have a flow that matches the rule we're trying
1571 * to destroy. If there isn't then we can't destroy it.
1572 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301573 c = sfe_ipv6_find_connection(si, tuple->protocol, (struct sfe_ipv6_addr *)tuple->flow_ip, tuple->flow_ident,
1574 (struct sfe_ipv6_addr *)tuple->return_ip, tuple->return_ident);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001575 if (!c) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07001576 spin_unlock_bh(&si->lock);
1577
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301578 this_cpu_inc(si->stats_pcpu->connection_destroy_misses64);
1579
Xiaoping Fan978b3772015-05-27 14:15:18 -07001580 DEBUG_TRACE("connection does not exist - p: %d, s: %pI6:%u, d: %pI6:%u\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301581 tuple->protocol, tuple->flow_ip, ntohs(tuple->flow_ident),
1582 tuple->return_ip, ntohs(tuple->return_ident));
Xiaoping Fan978b3772015-05-27 14:15:18 -07001583 return;
1584 }
1585
1586 /*
1587 * Remove our connection details from the hash tables.
1588 */
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301589 ret = sfe_ipv6_remove_connection(si, c);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001590 spin_unlock_bh(&si->lock);
1591
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301592 if (ret) {
1593 sfe_ipv6_flush_connection(si, c, SFE_SYNC_REASON_DESTROY);
1594 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07001595
1596 DEBUG_INFO("connection destroyed - p: %d, s: %pI6:%u, d: %pI6:%u\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301597 tuple->protocol, tuple->flow_ip, ntohs(tuple->flow_ident),
1598 tuple->return_ip, ntohs(tuple->return_ident));
Xiaoping Fan978b3772015-05-27 14:15:18 -07001599}
1600
1601/*
1602 * sfe_ipv6_register_sync_rule_callback()
1603 * Register a callback for rule synchronization.
1604 */
1605void sfe_ipv6_register_sync_rule_callback(sfe_sync_rule_callback_t sync_rule_callback)
1606{
1607 struct sfe_ipv6 *si = &__si6;
1608
1609 spin_lock_bh(&si->lock);
1610 rcu_assign_pointer(si->sync_rule_callback, sync_rule_callback);
1611 spin_unlock_bh(&si->lock);
1612}
1613
1614/*
1615 * sfe_ipv6_get_debug_dev()
1616 */
1617static ssize_t sfe_ipv6_get_debug_dev(struct device *dev,
1618 struct device_attribute *attr,
1619 char *buf)
1620{
1621 struct sfe_ipv6 *si = &__si6;
1622 ssize_t count;
1623 int num;
1624
1625 spin_lock_bh(&si->lock);
1626 num = si->debug_dev;
1627 spin_unlock_bh(&si->lock);
1628
1629 count = snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", num);
1630 return count;
1631}
1632
1633/*
1634 * sfe_ipv6_destroy_all_rules_for_dev()
1635 * Destroy all connections that match a particular device.
1636 *
1637 * If we pass dev as NULL then this destroys all connections.
1638 */
1639void sfe_ipv6_destroy_all_rules_for_dev(struct net_device *dev)
1640{
1641 struct sfe_ipv6 *si = &__si6;
1642 struct sfe_ipv6_connection *c;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301643 bool ret;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001644
Xiaoping Fan34586472015-07-03 02:20:35 -07001645another_round:
Xiaoping Fan978b3772015-05-27 14:15:18 -07001646 spin_lock_bh(&si->lock);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001647
Xiaoping Fan34586472015-07-03 02:20:35 -07001648 for (c = si->all_connections_head; c; c = c->all_connections_next) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07001649 /*
Xiaoping Fan34586472015-07-03 02:20:35 -07001650 * Does this connection relate to the device we are destroying?
Xiaoping Fan978b3772015-05-27 14:15:18 -07001651 */
1652 if (!dev
1653 || (dev == c->original_dev)
1654 || (dev == c->reply_dev)) {
Xiaoping Fan34586472015-07-03 02:20:35 -07001655 break;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001656 }
Xiaoping Fan34586472015-07-03 02:20:35 -07001657 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07001658
Xiaoping Fan34586472015-07-03 02:20:35 -07001659 if (c) {
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301660 ret = sfe_ipv6_remove_connection(si, c);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001661 }
1662
1663 spin_unlock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07001664
1665 if (c) {
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301666 if (ret) {
1667 sfe_ipv6_flush_connection(si, c, SFE_SYNC_REASON_DESTROY);
1668 }
Xiaoping Fan34586472015-07-03 02:20:35 -07001669 goto another_round;
1670 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07001671}
1672
1673/*
1674 * sfe_ipv6_periodic_sync()
1675 */
Ken Zhu137722d2021-09-23 17:57:36 -07001676static void sfe_ipv6_periodic_sync(struct work_struct *work)
Xiaoping Fan978b3772015-05-27 14:15:18 -07001677{
Ken Zhu137722d2021-09-23 17:57:36 -07001678 struct sfe_ipv6 *si = container_of((struct delayed_work *)work, struct sfe_ipv6, sync_dwork);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07001679 u64 now_jiffies;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001680 int quota;
1681 sfe_sync_rule_callback_t sync_rule_callback;
Ken Zhu32b95392021-09-03 13:52:04 -07001682 struct sfe_ipv6_connection *c;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001683
1684 now_jiffies = get_jiffies_64();
1685
1686 rcu_read_lock();
1687 sync_rule_callback = rcu_dereference(si->sync_rule_callback);
1688 if (!sync_rule_callback) {
1689 rcu_read_unlock();
1690 goto done;
1691 }
1692
1693 spin_lock_bh(&si->lock);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001694
1695 /*
Ken Zhu32b95392021-09-03 13:52:04 -07001696 * If we have reached the end of the connection list, walk from
1697 * the connection head.
1698 */
1699 c = si->wc_next;
1700 if (unlikely(!c)) {
1701 c = si->all_connections_head;
1702 }
1703 /*
Xiaoping Fan978b3772015-05-27 14:15:18 -07001704 * Get an estimate of the number of connections to parse in this sync.
1705 */
1706 quota = (si->num_connections + 63) / 64;
1707
1708 /*
Ken Zhu32b95392021-09-03 13:52:04 -07001709 * Walk the "all connection" list and sync the connection state.
Xiaoping Fan978b3772015-05-27 14:15:18 -07001710 */
Ken Zhu32b95392021-09-03 13:52:04 -07001711 while (likely(c && quota)) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07001712 struct sfe_ipv6_connection_match *cm;
1713 struct sfe_ipv6_connection_match *counter_cm;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001714 struct sfe_connection_sync sis;
1715
Ken Zhu32b95392021-09-03 13:52:04 -07001716 cm = c->original_match;
1717 counter_cm = c->reply_match;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001718
1719 /*
Ken Zhu32b95392021-09-03 13:52:04 -07001720 * Didn't receive packets in the origial direction or reply
1721 * direction, move to the next connection.
Xiaoping Fan978b3772015-05-27 14:15:18 -07001722 */
Ken Zhu32b95392021-09-03 13:52:04 -07001723 if (!atomic_read(&cm->rx_packet_count) && !atomic_read(&counter_cm->rx_packet_count)) {
1724 c = c->all_connections_next;
1725 continue;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001726 }
1727
Ken Zhu32b95392021-09-03 13:52:04 -07001728 quota--;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001729
1730 /*
1731 * Sync the connection state.
1732 */
Xiaoping Fan99cb4c12015-08-21 19:07:32 -07001733 sfe_ipv6_gen_sync_connection(si, c, &sis, SFE_SYNC_REASON_STATS, now_jiffies);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001734
Ken Zhu32b95392021-09-03 13:52:04 -07001735 si->wc_next = c->all_connections_next;
1736
Xiaoping Fan978b3772015-05-27 14:15:18 -07001737 spin_unlock_bh(&si->lock);
1738 sync_rule_callback(&sis);
1739 spin_lock_bh(&si->lock);
Ken Zhu32b95392021-09-03 13:52:04 -07001740
1741 /*
1742 * c must be set and used in the same lock/unlock window;
1743 * because c could be removed when we don't hold the lock,
1744 * so delay grabbing until after the callback and relock.
1745 */
1746 c = si->wc_next;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001747 }
1748
Ken Zhu32b95392021-09-03 13:52:04 -07001749 /*
1750 * At the end of loop, put wc_next to the connection we left
1751 */
1752 si->wc_next = c;
1753
Xiaoping Fan978b3772015-05-27 14:15:18 -07001754 spin_unlock_bh(&si->lock);
1755 rcu_read_unlock();
1756
1757done:
Ken Zhu137722d2021-09-23 17:57:36 -07001758 schedule_delayed_work_on(si->work_cpu, (struct delayed_work *)work, ((HZ + 99) / 100));
Xiaoping Fan978b3772015-05-27 14:15:18 -07001759}
1760
1761/*
1762 * sfe_ipv6_debug_dev_read_start()
1763 * Generate part of the XML output.
1764 */
1765static bool sfe_ipv6_debug_dev_read_start(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
1766 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
1767{
1768 int bytes_read;
1769
Xiaoping Fan34586472015-07-03 02:20:35 -07001770 si->debug_read_seq++;
1771
Xiaoping Fan978b3772015-05-27 14:15:18 -07001772 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "<sfe_ipv6>\n");
1773 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1774 return false;
1775 }
1776
1777 *length -= bytes_read;
1778 *total_read += bytes_read;
1779
1780 ws->state++;
1781 return true;
1782}
1783
1784/*
1785 * sfe_ipv6_debug_dev_read_connections_start()
1786 * Generate part of the XML output.
1787 */
1788static bool sfe_ipv6_debug_dev_read_connections_start(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
1789 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
1790{
1791 int bytes_read;
1792
1793 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<connections>\n");
1794 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1795 return false;
1796 }
1797
1798 *length -= bytes_read;
1799 *total_read += bytes_read;
1800
1801 ws->state++;
1802 return true;
1803}
1804
1805/*
1806 * sfe_ipv6_debug_dev_read_connections_connection()
1807 * Generate part of the XML output.
1808 */
1809static bool sfe_ipv6_debug_dev_read_connections_connection(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
1810 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
1811{
1812 struct sfe_ipv6_connection *c;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001813 struct sfe_ipv6_connection_match *original_cm;
1814 struct sfe_ipv6_connection_match *reply_cm;
1815 int bytes_read;
1816 int protocol;
1817 struct net_device *src_dev;
1818 struct sfe_ipv6_addr src_ip;
1819 struct sfe_ipv6_addr src_ip_xlate;
1820 __be16 src_port;
1821 __be16 src_port_xlate;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07001822 u64 src_rx_packets;
1823 u64 src_rx_bytes;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001824 struct net_device *dest_dev;
1825 struct sfe_ipv6_addr dest_ip;
1826 struct sfe_ipv6_addr dest_ip_xlate;
1827 __be16 dest_port;
1828 __be16 dest_port_xlate;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07001829 u64 dest_rx_packets;
1830 u64 dest_rx_bytes;
1831 u64 last_sync_jiffies;
Ken Zhu37040ea2021-09-09 21:11:15 -07001832 u32 src_mark, dest_mark, src_priority, dest_priority, src_dscp, dest_dscp;
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301833 u32 packet, byte, original_cm_flags;
1834 u16 pppoe_session_id;
1835 u8 pppoe_remote_mac[ETH_ALEN];
Xiaoping Fan978b3772015-05-27 14:15:18 -07001836#ifdef CONFIG_NF_FLOW_COOKIE
1837 int src_flow_cookie, dst_flow_cookie;
1838#endif
1839
1840 spin_lock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07001841
1842 for (c = si->all_connections_head; c; c = c->all_connections_next) {
1843 if (c->debug_read_seq < si->debug_read_seq) {
1844 c->debug_read_seq = si->debug_read_seq;
1845 break;
1846 }
1847 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07001848
1849 /*
Xiaoping Fan34586472015-07-03 02:20:35 -07001850 * If there were no connections then move to the next state.
Xiaoping Fan978b3772015-05-27 14:15:18 -07001851 */
1852 if (!c) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07001853 spin_unlock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07001854 ws->state++;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001855 return true;
1856 }
1857
1858 original_cm = c->original_match;
1859 reply_cm = c->reply_match;
1860
1861 protocol = c->protocol;
1862 src_dev = c->original_dev;
1863 src_ip = c->src_ip[0];
1864 src_ip_xlate = c->src_ip_xlate[0];
1865 src_port = c->src_port;
1866 src_port_xlate = c->src_port_xlate;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001867 src_priority = original_cm->priority;
1868 src_dscp = original_cm->dscp >> SFE_IPV6_DSCP_SHIFT;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001869
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301870 sfe_ipv6_connection_match_update_summary_stats(original_cm, &packet, &byte);
1871 sfe_ipv6_connection_match_update_summary_stats(reply_cm, &packet, &byte);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001872
1873 src_rx_packets = original_cm->rx_packet_count64;
1874 src_rx_bytes = original_cm->rx_byte_count64;
Ken Zhu37040ea2021-09-09 21:11:15 -07001875 src_mark = original_cm->mark;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001876 dest_dev = c->reply_dev;
1877 dest_ip = c->dest_ip[0];
1878 dest_ip_xlate = c->dest_ip_xlate[0];
1879 dest_port = c->dest_port;
1880 dest_port_xlate = c->dest_port_xlate;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001881 dest_priority = reply_cm->priority;
1882 dest_dscp = reply_cm->dscp >> SFE_IPV6_DSCP_SHIFT;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001883 dest_rx_packets = reply_cm->rx_packet_count64;
1884 dest_rx_bytes = reply_cm->rx_byte_count64;
1885 last_sync_jiffies = get_jiffies_64() - c->last_sync_jiffies;
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301886 original_cm_flags = original_cm->flags;
1887 pppoe_session_id = original_cm->pppoe_session_id;
1888 ether_addr_copy(pppoe_remote_mac, original_cm->pppoe_remote_mac);
Ken Zhu37040ea2021-09-09 21:11:15 -07001889 dest_mark = reply_cm->mark;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001890#ifdef CONFIG_NF_FLOW_COOKIE
1891 src_flow_cookie = original_cm->flow_cookie;
1892 dst_flow_cookie = reply_cm->flow_cookie;
1893#endif
1894 spin_unlock_bh(&si->lock);
1895
1896 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t\t<connection "
1897 "protocol=\"%u\" "
1898 "src_dev=\"%s\" "
1899 "src_ip=\"%pI6\" src_ip_xlate=\"%pI6\" "
1900 "src_port=\"%u\" src_port_xlate=\"%u\" "
Xiaoping Fane1963d42015-08-25 17:06:19 -07001901 "src_priority=\"%u\" src_dscp=\"%u\" "
Xiaoping Fan978b3772015-05-27 14:15:18 -07001902 "src_rx_pkts=\"%llu\" src_rx_bytes=\"%llu\" "
Ken Zhu37040ea2021-09-09 21:11:15 -07001903 "src_mark=\"%08x\" "
Xiaoping Fan978b3772015-05-27 14:15:18 -07001904 "dest_dev=\"%s\" "
1905 "dest_ip=\"%pI6\" dest_ip_xlate=\"%pI6\" "
1906 "dest_port=\"%u\" dest_port_xlate=\"%u\" "
Xiaoping Fane1963d42015-08-25 17:06:19 -07001907 "dest_priority=\"%u\" dest_dscp=\"%u\" "
Xiaoping Fan978b3772015-05-27 14:15:18 -07001908 "dest_rx_pkts=\"%llu\" dest_rx_bytes=\"%llu\" "
Ken Zhu37040ea2021-09-09 21:11:15 -07001909 "dest_mark=\"%08x\" "
Xiaoping Fan978b3772015-05-27 14:15:18 -07001910#ifdef CONFIG_NF_FLOW_COOKIE
1911 "src_flow_cookie=\"%d\" dst_flow_cookie=\"%d\" "
1912#endif
Ken Zhu37040ea2021-09-09 21:11:15 -07001913 "last_sync=\"%llu\" ",
Xiaoping Fan978b3772015-05-27 14:15:18 -07001914 protocol,
1915 src_dev->name,
1916 &src_ip, &src_ip_xlate,
1917 ntohs(src_port), ntohs(src_port_xlate),
Xiaoping Fane1963d42015-08-25 17:06:19 -07001918 src_priority, src_dscp,
Xiaoping Fan978b3772015-05-27 14:15:18 -07001919 src_rx_packets, src_rx_bytes,
Ken Zhu37040ea2021-09-09 21:11:15 -07001920 src_mark,
Xiaoping Fan978b3772015-05-27 14:15:18 -07001921 dest_dev->name,
1922 &dest_ip, &dest_ip_xlate,
1923 ntohs(dest_port), ntohs(dest_port_xlate),
Xiaoping Fane1963d42015-08-25 17:06:19 -07001924 dest_priority, dest_dscp,
Xiaoping Fan978b3772015-05-27 14:15:18 -07001925 dest_rx_packets, dest_rx_bytes,
Ken Zhu37040ea2021-09-09 21:11:15 -07001926 dest_mark,
Xiaoping Fan978b3772015-05-27 14:15:18 -07001927#ifdef CONFIG_NF_FLOW_COOKIE
1928 src_flow_cookie, dst_flow_cookie,
1929#endif
Ken Zhu37040ea2021-09-09 21:11:15 -07001930 last_sync_jiffies);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001931
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301932 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 +05301933 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 +05301934 pppoe_session_id, pppoe_remote_mac);
1935 }
1936
1937 bytes_read += snprintf(msg + bytes_read, CHAR_DEV_MSG_SIZE, ")/>\n");
1938
Xiaoping Fan978b3772015-05-27 14:15:18 -07001939 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1940 return false;
1941 }
1942
1943 *length -= bytes_read;
1944 *total_read += bytes_read;
1945
Xiaoping Fan978b3772015-05-27 14:15:18 -07001946 return true;
1947}
1948
1949/*
1950 * sfe_ipv6_debug_dev_read_connections_end()
1951 * Generate part of the XML output.
1952 */
1953static bool sfe_ipv6_debug_dev_read_connections_end(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
1954 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
1955{
1956 int bytes_read;
1957
1958 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t</connections>\n");
1959 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1960 return false;
1961 }
1962
1963 *length -= bytes_read;
1964 *total_read += bytes_read;
1965
1966 ws->state++;
1967 return true;
1968}
1969
1970/*
1971 * sfe_ipv6_debug_dev_read_exceptions_start()
1972 * Generate part of the XML output.
1973 */
1974static bool sfe_ipv6_debug_dev_read_exceptions_start(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
1975 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
1976{
1977 int bytes_read;
1978
1979 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<exceptions>\n");
1980 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1981 return false;
1982 }
1983
1984 *length -= bytes_read;
1985 *total_read += bytes_read;
1986
1987 ws->state++;
1988 return true;
1989}
1990
1991/*
1992 * sfe_ipv6_debug_dev_read_exceptions_exception()
1993 * Generate part of the XML output.
1994 */
1995static bool sfe_ipv6_debug_dev_read_exceptions_exception(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
1996 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
1997{
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301998 int i;
1999 u64 val = 0;
Xiaoping Fan978b3772015-05-27 14:15:18 -07002000
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302001 for_each_possible_cpu(i) {
2002 const struct sfe_ipv6_stats *s = per_cpu_ptr(si->stats_pcpu, i);
2003 val += s->exception_events64[ws->iter_exception];
2004 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07002005
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302006 if (val) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07002007 int bytes_read;
2008
2009 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE,
2010 "\t\t<exception name=\"%s\" count=\"%llu\" />\n",
2011 sfe_ipv6_exception_events_string[ws->iter_exception],
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302012 val);
2013
Xiaoping Fan978b3772015-05-27 14:15:18 -07002014 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2015 return false;
2016 }
2017
2018 *length -= bytes_read;
2019 *total_read += bytes_read;
2020 }
2021
2022 ws->iter_exception++;
2023 if (ws->iter_exception >= SFE_IPV6_EXCEPTION_EVENT_LAST) {
2024 ws->iter_exception = 0;
2025 ws->state++;
2026 }
2027
2028 return true;
2029}
2030
2031/*
2032 * sfe_ipv6_debug_dev_read_exceptions_end()
2033 * Generate part of the XML output.
2034 */
2035static bool sfe_ipv6_debug_dev_read_exceptions_end(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
2036 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
2037{
2038 int bytes_read;
2039
2040 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t</exceptions>\n");
2041 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2042 return false;
2043 }
2044
2045 *length -= bytes_read;
2046 *total_read += bytes_read;
2047
2048 ws->state++;
2049 return true;
2050}
2051
2052/*
2053 * sfe_ipv6_debug_dev_read_stats()
2054 * Generate part of the XML output.
2055 */
2056static bool sfe_ipv6_debug_dev_read_stats(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
2057 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
2058{
2059 int bytes_read;
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302060 struct sfe_ipv6_stats stats;
2061 unsigned int num_conn;
2062
2063 sfe_ipv6_update_summary_stats(si, &stats);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002064
2065 spin_lock_bh(&si->lock);
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302066 num_conn = si->num_connections;
Xiaoping Fan978b3772015-05-27 14:15:18 -07002067 spin_unlock_bh(&si->lock);
2068
2069 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<stats "
2070 "num_connections=\"%u\" "
Suruchi Suman23a279d2021-11-16 15:13:09 +05302071 "pkts_dropped=\"%llu\" "
Xiaoping Fan978b3772015-05-27 14:15:18 -07002072 "pkts_forwarded=\"%llu\" pkts_not_forwarded=\"%llu\" "
2073 "create_requests=\"%llu\" create_collisions=\"%llu\" "
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05302074 "create_failures=\"%llu\" "
Xiaoping Fan978b3772015-05-27 14:15:18 -07002075 "destroy_requests=\"%llu\" destroy_misses=\"%llu\" "
2076 "flushes=\"%llu\" "
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +05302077 "hash_hits=\"%llu\" hash_reorders=\"%llu\" "
2078 "pppoe_encap_pkts_fwded=\"%llu\" "
Guduri Prathyusha034d6352022-01-12 16:49:04 +05302079 "pppoe_decap_pkts_fwded=\"%llu\" "
2080 "pppoe_bridge_pkts_fwded=\"%llu\" />\n",
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302081
2082 num_conn,
Suruchi Suman23a279d2021-11-16 15:13:09 +05302083 stats.packets_dropped64,
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302084 stats.packets_forwarded64,
2085 stats.packets_not_forwarded64,
2086 stats.connection_create_requests64,
2087 stats.connection_create_collisions64,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05302088 stats.connection_create_failures64,
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302089 stats.connection_destroy_requests64,
2090 stats.connection_destroy_misses64,
2091 stats.connection_flushes64,
2092 stats.connection_match_hash_hits64,
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +05302093 stats.connection_match_hash_reorders64,
2094 stats.pppoe_encap_packets_forwarded64,
Guduri Prathyusha034d6352022-01-12 16:49:04 +05302095 stats.pppoe_decap_packets_forwarded64,
2096 stats.pppoe_bridge_packets_forwarded64);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002097 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2098 return false;
2099 }
2100
2101 *length -= bytes_read;
2102 *total_read += bytes_read;
2103
2104 ws->state++;
2105 return true;
2106}
2107
2108/*
2109 * sfe_ipv6_debug_dev_read_end()
2110 * Generate part of the XML output.
2111 */
2112static bool sfe_ipv6_debug_dev_read_end(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
2113 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
2114{
2115 int bytes_read;
2116
2117 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "</sfe_ipv6>\n");
2118 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2119 return false;
2120 }
2121
2122 *length -= bytes_read;
2123 *total_read += bytes_read;
2124
2125 ws->state++;
2126 return true;
2127}
2128
2129/*
2130 * Array of write functions that write various XML elements that correspond to
2131 * our XML output state machine.
2132 */
2133static sfe_ipv6_debug_xml_write_method_t sfe_ipv6_debug_xml_write_methods[SFE_IPV6_DEBUG_XML_STATE_DONE] = {
2134 sfe_ipv6_debug_dev_read_start,
2135 sfe_ipv6_debug_dev_read_connections_start,
2136 sfe_ipv6_debug_dev_read_connections_connection,
2137 sfe_ipv6_debug_dev_read_connections_end,
2138 sfe_ipv6_debug_dev_read_exceptions_start,
2139 sfe_ipv6_debug_dev_read_exceptions_exception,
2140 sfe_ipv6_debug_dev_read_exceptions_end,
2141 sfe_ipv6_debug_dev_read_stats,
2142 sfe_ipv6_debug_dev_read_end,
2143};
2144
2145/*
2146 * sfe_ipv6_debug_dev_read()
2147 * Send info to userspace upon read request from user
2148 */
2149static ssize_t sfe_ipv6_debug_dev_read(struct file *filp, char *buffer, size_t length, loff_t *offset)
2150{
2151 char msg[CHAR_DEV_MSG_SIZE];
2152 int total_read = 0;
2153 struct sfe_ipv6_debug_xml_write_state *ws;
2154 struct sfe_ipv6 *si = &__si6;
2155
2156 ws = (struct sfe_ipv6_debug_xml_write_state *)filp->private_data;
2157 while ((ws->state != SFE_IPV6_DEBUG_XML_STATE_DONE) && (length > CHAR_DEV_MSG_SIZE)) {
2158 if ((sfe_ipv6_debug_xml_write_methods[ws->state])(si, buffer, msg, &length, &total_read, ws)) {
2159 continue;
2160 }
2161 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07002162 return total_read;
2163}
2164
2165/*
Xiaoping Fan978b3772015-05-27 14:15:18 -07002166 * sfe_ipv6_debug_dev_open()
2167 */
2168static int sfe_ipv6_debug_dev_open(struct inode *inode, struct file *file)
2169{
2170 struct sfe_ipv6_debug_xml_write_state *ws;
2171
2172 ws = (struct sfe_ipv6_debug_xml_write_state *)file->private_data;
2173 if (ws) {
2174 return 0;
2175 }
2176
2177 ws = kzalloc(sizeof(struct sfe_ipv6_debug_xml_write_state), GFP_KERNEL);
2178 if (!ws) {
2179 return -ENOMEM;
2180 }
2181
2182 ws->state = SFE_IPV6_DEBUG_XML_STATE_START;
2183 file->private_data = ws;
2184
2185 return 0;
2186}
2187
2188/*
2189 * sfe_ipv6_debug_dev_release()
2190 */
2191static int sfe_ipv6_debug_dev_release(struct inode *inode, struct file *file)
2192{
2193 struct sfe_ipv6_debug_xml_write_state *ws;
Xiaoping Fan978b3772015-05-27 14:15:18 -07002194
2195 ws = (struct sfe_ipv6_debug_xml_write_state *)file->private_data;
Xiaoping Fan34586472015-07-03 02:20:35 -07002196 if (ws) {
2197 /*
2198 * We've finished with our output so free the write state.
2199 */
2200 kfree(ws);
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05302201 file->private_data = NULL;
Xiaoping Fan978b3772015-05-27 14:15:18 -07002202 }
2203
Xiaoping Fan978b3772015-05-27 14:15:18 -07002204 return 0;
2205}
2206
2207/*
2208 * File operations used in the debug char device
2209 */
2210static struct file_operations sfe_ipv6_debug_dev_fops = {
2211 .read = sfe_ipv6_debug_dev_read,
Xiaoping Fan978b3772015-05-27 14:15:18 -07002212 .open = sfe_ipv6_debug_dev_open,
2213 .release = sfe_ipv6_debug_dev_release
2214};
2215
2216#ifdef CONFIG_NF_FLOW_COOKIE
2217/*
2218 * sfe_ipv6_register_flow_cookie_cb
2219 * register a function in SFE to let SFE use this function to configure flow cookie for a flow
2220 *
2221 * Hardware driver which support flow cookie should register a callback function in SFE. Then SFE
2222 * can use this function to configure flow cookie for a flow.
2223 * return: 0, success; !=0, fail
2224 */
2225int sfe_ipv6_register_flow_cookie_cb(sfe_ipv6_flow_cookie_set_func_t cb)
2226{
2227 struct sfe_ipv6 *si = &__si6;
2228
2229 BUG_ON(!cb);
2230
2231 if (si->flow_cookie_set_func) {
2232 return -1;
2233 }
2234
2235 rcu_assign_pointer(si->flow_cookie_set_func, cb);
2236 return 0;
2237}
2238
2239/*
2240 * sfe_ipv6_unregister_flow_cookie_cb
2241 * unregister function which is used to configure flow cookie for a flow
2242 *
2243 * return: 0, success; !=0, fail
2244 */
2245int sfe_ipv6_unregister_flow_cookie_cb(sfe_ipv6_flow_cookie_set_func_t cb)
2246{
2247 struct sfe_ipv6 *si = &__si6;
2248
2249 RCU_INIT_POINTER(si->flow_cookie_set_func, NULL);
2250 return 0;
2251}
Xiaoping Fan640faf42015-08-28 15:50:55 -07002252
2253/*
2254 * sfe_ipv6_get_flow_cookie()
2255 */
2256static ssize_t sfe_ipv6_get_flow_cookie(struct device *dev,
2257 struct device_attribute *attr,
2258 char *buf)
2259{
2260 struct sfe_ipv6 *si = &__si6;
Xiaoping Fan01c67cc2015-11-09 11:31:57 -08002261 return snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", si->flow_cookie_enable);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002262}
2263
2264/*
2265 * sfe_ipv6_set_flow_cookie()
2266 */
2267static ssize_t sfe_ipv6_set_flow_cookie(struct device *dev,
2268 struct device_attribute *attr,
2269 const char *buf, size_t size)
2270{
2271 struct sfe_ipv6 *si = &__si6;
Ken Zhu137722d2021-09-23 17:57:36 -07002272 si->flow_cookie_enable = strict_strtol(buf, NULL, 0);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002273
2274 return size;
2275}
2276
2277/*
2278 * sysfs attributes.
2279 */
2280static const struct device_attribute sfe_ipv6_flow_cookie_attr =
Xiaoping Fane70da412016-02-26 16:47:57 -08002281 __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 -07002282#endif /*CONFIG_NF_FLOW_COOKIE*/
2283
Ken Zhu137722d2021-09-23 17:57:36 -07002284/*
2285 * sfe_ipv6_get_cpu()
2286 */
2287static ssize_t sfe_ipv6_get_cpu(struct device *dev,
2288 struct device_attribute *attr,
2289 char *buf)
2290{
2291 struct sfe_ipv6 *si = &__si6;
2292 return snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", si->work_cpu);
2293}
2294
2295/*
Wayne Tanbb7f1782021-12-13 11:16:04 -08002296 * sfe_ipv6_set_cpu()
Ken Zhu137722d2021-09-23 17:57:36 -07002297 */
2298static ssize_t sfe_ipv6_set_cpu(struct device *dev,
2299 struct device_attribute *attr,
2300 const char *buf, size_t size)
2301{
2302 struct sfe_ipv6 *si = &__si6;
2303 int work_cpu;
2304
2305 work_cpu = simple_strtol(buf, NULL, 0);
2306 if ((work_cpu >= 0) && (work_cpu <= NR_CPUS)) {
2307 si->work_cpu = work_cpu;
2308 } else {
2309 dev_err(dev, "%s is not in valid range[0,%d]", buf, NR_CPUS);
2310 }
2311
2312 return size;
2313}
2314/*
2315 * sysfs attributes.
2316 */
2317static const struct device_attribute sfe_ipv6_cpu_attr =
2318 __ATTR(stat_work_cpu, S_IWUSR | S_IRUGO, sfe_ipv6_get_cpu, sfe_ipv6_set_cpu);
2319
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05302320 /*
2321 * sfe_ipv6_hash_init()
2322 * Initialize conn match hash lists
2323 */
2324static void sfe_ipv6_conn_match_hash_init(struct sfe_ipv6 *si, int len)
2325{
2326 struct hlist_head *hash_list = si->hlist_conn_match_hash_head;
2327 int i;
2328
2329 for (i = 0; i < len; i++) {
2330 INIT_HLIST_HEAD(&hash_list[i]);
2331 }
2332}
2333
Suruchi Suman23a279d2021-11-16 15:13:09 +05302334#ifdef SFE_PROCESS_LOCAL_OUT
2335/*
2336 * sfe_ipv6_local_out()
2337 * Called for packets from ip_local_out() - post encapsulation & other packets
2338 */
2339static unsigned int sfe_ipv6_local_out(void *priv,
2340 struct sk_buff *skb,
2341 const struct nf_hook_state *nhs)
2342{
2343 DEBUG_TRACE("sfe: sfe_ipv6_local_out hook called.\n");
2344
2345 if (likely(skb->skb_iif)) {
2346 return sfe_ipv6_recv(skb->dev, skb, NULL, true) ? NF_STOLEN : NF_ACCEPT;
2347 }
2348
2349 return NF_ACCEPT;
2350}
2351
2352/*
2353 * struct nf_hook_ops sfe_ipv6_ops_local_out[]
2354 * Hooks into netfilter local out packet monitoring points.
2355 */
2356static struct nf_hook_ops sfe_ipv6_ops_local_out[] __read_mostly = {
2357
2358 /*
2359 * Local out routing hook is used to monitor packets.
2360 */
2361 {
2362 .hook = sfe_ipv6_local_out,
2363 .pf = PF_INET6,
2364 .hooknum = NF_INET_LOCAL_OUT,
2365 .priority = NF_IP6_PRI_FIRST,
2366 },
2367};
2368#endif
2369
Xiaoping Fan978b3772015-05-27 14:15:18 -07002370/*
2371 * sfe_ipv6_init()
2372 */
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05302373int sfe_ipv6_init(void)
Xiaoping Fan978b3772015-05-27 14:15:18 -07002374{
2375 struct sfe_ipv6 *si = &__si6;
2376 int result = -1;
2377
2378 DEBUG_INFO("SFE IPv6 init\n");
2379
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05302380 sfe_ipv6_conn_match_hash_init(si, ARRAY_SIZE(si->hlist_conn_match_hash_head));
2381
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302382 si->stats_pcpu = alloc_percpu_gfp(struct sfe_ipv6_stats, GFP_KERNEL | __GFP_ZERO);
2383 if (!si->stats_pcpu) {
2384 DEBUG_ERROR("failed to allocate stats memory for sfe_ipv6\n");
2385 goto exit0;
2386 }
2387
Xiaoping Fan978b3772015-05-27 14:15:18 -07002388 /*
2389 * Create sys/sfe_ipv6
2390 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302391 si->sys_ipv6 = kobject_create_and_add("sfe_ipv6", NULL);
2392 if (!si->sys_ipv6) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07002393 DEBUG_ERROR("failed to register sfe_ipv6\n");
2394 goto exit1;
2395 }
2396
2397 /*
2398 * Create files, one for each parameter supported by this module.
2399 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302400 result = sysfs_create_file(si->sys_ipv6, &sfe_ipv6_debug_dev_attr.attr);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002401 if (result) {
2402 DEBUG_ERROR("failed to register debug dev file: %d\n", result);
2403 goto exit2;
2404 }
2405
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302406 result = sysfs_create_file(si->sys_ipv6, &sfe_ipv6_cpu_attr.attr);
Ken Zhu137722d2021-09-23 17:57:36 -07002407 if (result) {
2408 DEBUG_ERROR("failed to register debug dev file: %d\n", result);
2409 goto exit3;
2410 }
2411
Xiaoping Fan640faf42015-08-28 15:50:55 -07002412#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302413 result = sysfs_create_file(si->sys_ipv6, &sfe_ipv6_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002414 if (result) {
2415 DEBUG_ERROR("failed to register flow cookie enable file: %d\n", result);
Ken Zhu137722d2021-09-23 17:57:36 -07002416 goto exit4;
Xiaoping Fan640faf42015-08-28 15:50:55 -07002417 }
2418#endif /* CONFIG_NF_FLOW_COOKIE */
2419
Suruchi Suman23a279d2021-11-16 15:13:09 +05302420#ifdef SFE_PROCESS_LOCAL_OUT
2421#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
2422 result = nf_register_hooks(sfe_ipv6_ops_local_out, ARRAY_SIZE(sfe_ipv6_ops_local_out));
2423#else
2424 result = nf_register_net_hooks(&init_net, sfe_ipv6_ops_local_out, ARRAY_SIZE(sfe_ipv6_ops_local_out));
2425#endif
2426#endif
2427 if (result < 0) {
2428 DEBUG_ERROR("can't register nf local out hook: %d\n", result);
2429 goto exit5;
2430 } else {
2431 DEBUG_ERROR("Register nf local out hook success: %d\n", result);
2432 }
2433
Xiaoping Fan978b3772015-05-27 14:15:18 -07002434 /*
2435 * Register our debug char device.
2436 */
2437 result = register_chrdev(0, "sfe_ipv6", &sfe_ipv6_debug_dev_fops);
2438 if (result < 0) {
2439 DEBUG_ERROR("Failed to register chrdev: %d\n", result);
Suruchi Suman23a279d2021-11-16 15:13:09 +05302440 goto exit6;
Xiaoping Fan978b3772015-05-27 14:15:18 -07002441 }
2442
2443 si->debug_dev = result;
Ken Zhu137722d2021-09-23 17:57:36 -07002444 si->work_cpu = WORK_CPU_UNBOUND;
Xiaoping Fan978b3772015-05-27 14:15:18 -07002445
2446 /*
Ken Zhu137722d2021-09-23 17:57:36 -07002447 * Create work to handle periodic statistics.
Xiaoping Fan978b3772015-05-27 14:15:18 -07002448 */
Ken Zhu137722d2021-09-23 17:57:36 -07002449 INIT_DELAYED_WORK(&(si->sync_dwork), sfe_ipv6_periodic_sync);
2450 schedule_delayed_work_on(si->work_cpu, &(si->sync_dwork), ((HZ + 99) / 100));
Xiaoping Fan978b3772015-05-27 14:15:18 -07002451 spin_lock_init(&si->lock);
2452
2453 return 0;
2454
Suruchi Suman23a279d2021-11-16 15:13:09 +05302455exit6:
2456#ifdef SFE_PROCESS_LOCAL_OUT
2457#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
2458 DEBUG_TRACE("sfe: Unregister local out hook\n");
2459 nf_unregister_hooks(sfe_ipv6_ops_local_out, ARRAY_SIZE(sfe_ipv6_ops_local_out));
2460#else
2461 DEBUG_TRACE("sfe: Unregister local out hook\n");
2462 nf_unregister_net_hooks(&init_net, sfe_ipv6_ops_local_out, ARRAY_SIZE(sfe_ipv6_ops_local_out));
2463#endif
2464#endif
2465
Ken Zhu137722d2021-09-23 17:57:36 -07002466exit5:
Xiaoping Fan640faf42015-08-28 15:50:55 -07002467#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302468 sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002469
Ken Zhu137722d2021-09-23 17:57:36 -07002470exit4:
Xiaoping Fan640faf42015-08-28 15:50:55 -07002471#endif /* CONFIG_NF_FLOW_COOKIE */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302472 sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_cpu_attr.attr);
Suruchi Suman23a279d2021-11-16 15:13:09 +05302473
Ken Zhu137722d2021-09-23 17:57:36 -07002474exit3:
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302475 sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_debug_dev_attr.attr);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002476
2477exit2:
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302478 kobject_put(si->sys_ipv6);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002479
2480exit1:
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302481 free_percpu(si->stats_pcpu);
2482
2483exit0:
Xiaoping Fan978b3772015-05-27 14:15:18 -07002484 return result;
2485}
2486
2487/*
2488 * sfe_ipv6_exit()
2489 */
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05302490void sfe_ipv6_exit(void)
Xiaoping Fan978b3772015-05-27 14:15:18 -07002491{
2492 struct sfe_ipv6 *si = &__si6;
2493
2494 DEBUG_INFO("SFE IPv6 exit\n");
2495
2496 /*
2497 * Destroy all connections.
2498 */
2499 sfe_ipv6_destroy_all_rules_for_dev(NULL);
2500
Ken Zhu137722d2021-09-23 17:57:36 -07002501 cancel_delayed_work(&si->sync_dwork);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002502
2503 unregister_chrdev(si->debug_dev, "sfe_ipv6");
2504
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302505 free_percpu(si->stats_pcpu);
2506
Suruchi Suman23a279d2021-11-16 15:13:09 +05302507#ifdef SFE_PROCESS_LOCAL_OUT
2508#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
2509 DEBUG_TRACE("sfe: Unregister local out hook\n");
2510 nf_unregister_hooks(sfe_ipv6_ops_local_out, ARRAY_SIZE(sfe_ipv6_ops_local_out));
2511#else
2512 DEBUG_TRACE("sfe: Unregister local out hook\n");
2513 nf_unregister_net_hooks(&init_net, sfe_ipv6_ops_local_out, ARRAY_SIZE(sfe_ipv6_ops_local_out));
2514#endif
2515#endif
2516
Xiaoping Fan640faf42015-08-28 15:50:55 -07002517#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302518 sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002519#endif /* CONFIG_NF_FLOW_COOKIE */
Ken Zhu137722d2021-09-23 17:57:36 -07002520
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302521 sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_cpu_attr.attr);
Ken Zhu137722d2021-09-23 17:57:36 -07002522
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302523 sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_debug_dev_attr.attr);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002524
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302525 kobject_put(si->sys_ipv6);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002526}
2527
Xiaoping Fan978b3772015-05-27 14:15:18 -07002528#ifdef CONFIG_NF_FLOW_COOKIE
2529EXPORT_SYMBOL(sfe_ipv6_register_flow_cookie_cb);
2530EXPORT_SYMBOL(sfe_ipv6_unregister_flow_cookie_cb);
2531#endif