blob: 8af9a44cb7634a393bd1e6ddf8b17cd3d862993f [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",
84 "FLOW_COOKIE_ADD_FAIL"
85};
86
Xiaoping Fan6a1672f2016-08-17 19:58:12 -070087static struct sfe_ipv6 __si6;
Xiaoping Fan978b3772015-05-27 14:15:18 -070088
89/*
90 * sfe_ipv6_get_debug_dev()
91 */
92static ssize_t sfe_ipv6_get_debug_dev(struct device *dev, struct device_attribute *attr, char *buf);
93
94/*
95 * sysfs attributes.
96 */
97static const struct device_attribute sfe_ipv6_debug_dev_attr =
Xiaoping Fane70da412016-02-26 16:47:57 -080098 __ATTR(debug_dev, S_IWUSR | S_IRUGO, sfe_ipv6_get_debug_dev, NULL);
Xiaoping Fan978b3772015-05-27 14:15:18 -070099
100/*
Xiaoping Fan978b3772015-05-27 14:15:18 -0700101 * sfe_ipv6_get_connection_match_hash()
102 * Generate the hash used in connection match lookups.
103 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700104static inline unsigned int sfe_ipv6_get_connection_match_hash(struct net_device *dev, u8 protocol,
Xiaoping Fan978b3772015-05-27 14:15:18 -0700105 struct sfe_ipv6_addr *src_ip, __be16 src_port,
106 struct sfe_ipv6_addr *dest_ip, __be16 dest_port)
107{
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700108 u32 idx, hash = 0;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700109 size_t dev_addr = (size_t)dev;
110
111 for (idx = 0; idx < 4; idx++) {
112 hash ^= src_ip->addr[idx] ^ dest_ip->addr[idx];
113 }
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700114 hash = ((u32)dev_addr) ^ 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
Xiaoping Fan978b3772015-05-27 14:15:18 -07001250 /*
Ken Zhubbf49652021-09-12 15:33:09 -07001251 * For the non-arp interface, we don't write L2 HDR.
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +05301252 * Excluding PPPoE from this, since we are now supporting PPPoE encap/decap.
Xiaoping Fan978b3772015-05-27 14:15:18 -07001253 */
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +05301254 if (sfe_ipv6_xmit_eth_type_check(dest_dev, original_cm->flags)) {
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301255
1256 /*
1257 * Check whether the rule has configured a specific source MAC address to use.
1258 * This is needed when virtual L3 interfaces such as br-lan, macvlan, vlan are used during egress
1259 */
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301260 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1261 ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)msg->conn_rule.flow_mac);
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301262 } else {
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301263 if ((msg->valid_flags & SFE_RULE_CREATE_SRC_MAC_VALID) &&
1264 (msg->src_mac_rule.mac_valid_flags & SFE_SRC_MAC_RETURN_VALID)) {
1265 ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)msg->src_mac_rule.return_src_mac);
1266 } else {
1267 ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)dest_dev->dev_addr);
1268 }
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301269 }
1270 ether_addr_copy((u8 *)original_cm->xmit_dest_mac, (u8 *)msg->conn_rule.return_mac);
1271
Xiaoping Fan978b3772015-05-27 14:15:18 -07001272 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_WRITE_L2_HDR;
Wayne Tanbb7f1782021-12-13 11:16:04 -08001273 original_cm->l2_hdr_size += ETH_HLEN;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001274
1275 /*
1276 * If our dev writes Ethernet headers then we can write a really fast
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301277 * version
Xiaoping Fan978b3772015-05-27 14:15:18 -07001278 */
1279 if (dest_dev->header_ops) {
1280 if (dest_dev->header_ops->create == eth_header) {
1281 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR;
1282 }
1283 }
1284 }
1285
1286 /*
1287 * Fill in the "reply" direction connection matching object.
1288 */
1289 reply_cm->match_dev = dest_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301290 reply_cm->match_protocol = tuple->protocol;
1291 reply_cm->match_src_ip[0] = *(struct sfe_ipv6_addr *)tuple->return_ip;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301292 reply_cm->match_dest_ip[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip;
1293 reply_cm->match_dest_port = tuple->flow_ident;
1294 reply_cm->xlate_src_ip[0] = *(struct sfe_ipv6_addr *)tuple->return_ip;
1295 reply_cm->xlate_src_port = tuple->return_ident;
1296 reply_cm->xlate_dest_ip[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip;
1297 reply_cm->xlate_dest_port = tuple->flow_ident;
1298
Suruchi Suman23a279d2021-11-16 15:13:09 +05301299 /*
1300 * Keep source port as 0 for VxLAN tunnels.
1301 */
1302 if (netif_is_vxlan(src_dev) || netif_is_vxlan(dest_dev)) {
1303 reply_cm->match_src_port = 0;
1304 } else {
1305 reply_cm->match_src_port = tuple->return_ident;
1306 }
1307
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301308 atomic_set(&original_cm->rx_byte_count, 0);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001309 reply_cm->rx_packet_count64 = 0;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301310 atomic_set(&reply_cm->rx_byte_count, 0);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001311 reply_cm->rx_byte_count64 = 0;
1312 reply_cm->xmit_dev = src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301313 reply_cm->xmit_dev_mtu = msg->conn_rule.flow_mtu;
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301314
Xiaoping Fan978b3772015-05-27 14:15:18 -07001315 reply_cm->connection = c;
1316 reply_cm->counter_match = original_cm;
Suruchi Suman23a279d2021-11-16 15:13:09 +05301317
Ken Zhu37040ea2021-09-09 21:11:15 -07001318 if (msg->valid_flags & SFE_RULE_CREATE_MARK_VALID) {
1319 reply_cm->mark = msg->mark_rule.return_mark;
1320 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_MARK;
1321 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301322 if (msg->valid_flags & SFE_RULE_CREATE_QOS_VALID) {
1323 reply_cm->priority = msg->qos_rule.return_qos_tag;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001324 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_PRIORITY_REMARK;
1325 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301326 if (msg->valid_flags & SFE_RULE_CREATE_DSCP_MARKING_VALID) {
1327 reply_cm->dscp = msg->dscp_rule.return_dscp << SFE_IPV6_DSCP_SHIFT;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001328 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_DSCP_REMARK;
1329 }
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301330
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301331 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1332 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_BRIDGE_FLOW;
1333 }
1334
Suruchi Suman23a279d2021-11-16 15:13:09 +05301335 /*
1336 * Setup UDP Socket if found to be valid for decap.
1337 */
1338 RCU_INIT_POINTER(reply_cm->up, NULL);
1339 net = dev_net(reply_cm->match_dev);
1340 src_if_idx = src_dev->ifindex;
1341
1342 rcu_read_lock();
1343
1344 /*
1345 * Look for the associated sock object.
1346 * __udp6_lib_lookup() holds a reference for this sock object,
1347 * which will be released in sfe_ipv6_flush_connection()
1348 */
1349#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
1350 sk = __udp6_lib_lookup(net, (const struct in6_addr *)reply_cm->match_dest_ip,
1351 reply_cm->match_dest_port, (const struct in6_addr *)reply_cm->xlate_src_ip,
1352 reply_cm->xlate_src_port, src_if_idx, &udp_table);
1353#else
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, 0, &udp_table, NULL);
1357#endif
1358 rcu_read_unlock();
1359
1360 /*
1361 * We set the UDP sock pointer as valid only for decap direction.
1362 */
1363 if (sk && udp_sk(sk)->encap_type) {
1364#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
1365 if (!atomic_add_unless(&sk->sk_refcnt, 1, 0)) {
1366#else
1367 if (!refcount_inc_not_zero(&sk->sk_refcnt)) {
1368#endif
Wayne Tanbb7f1782021-12-13 11:16:04 -08001369 spin_unlock_bh(&si->lock);
Suruchi Suman23a279d2021-11-16 15:13:09 +05301370 kfree(reply_cm);
1371 kfree(original_cm);
1372 kfree(c);
1373
1374 DEBUG_INFO("sfe: unable to take reference for socket p:%d\n", tuple->protocol);
1375 DEBUG_INFO("SK: connection - \n"
1376 " s: %s:%pI6(%pI6):%u(%u)\n"
1377 " d: %s:%pI6(%pI6):%u(%u)\n",
1378 reply_cm->match_dev->name, &reply_cm->match_src_ip, &reply_cm->xlate_src_ip,
1379 ntohs(reply_cm->match_src_port), ntohs(reply_cm->xlate_src_port),
1380 reply_cm->xmit_dev->name, &reply_cm->match_dest_ip, &reply_cm->xlate_dest_ip,
1381 ntohs(reply_cm->match_dest_port), ntohs(reply_cm->xlate_dest_port));
1382
1383 dev_put(src_dev);
1384 dev_put(dest_dev);
1385
1386 return -ESHUTDOWN;
1387 }
1388
1389 rcu_assign_pointer(reply_cm->up, udp_sk(sk));
1390 DEBUG_INFO("Sock lookup success with reply_cm direction(%p)\n", sk);
1391 DEBUG_INFO("SK: connection - \n"
1392 " s: %s:%pI6(%pI6):%u(%u)\n"
1393 " d: %s:%pI6(%pI6):%u(%u)\n",
1394 reply_cm->match_dev->name, &reply_cm->match_src_ip, &reply_cm->xlate_src_ip,
1395 ntohs(reply_cm->match_src_port), ntohs(reply_cm->xlate_src_port),
1396 reply_cm->xmit_dev->name, &reply_cm->match_dest_ip, &reply_cm->xlate_dest_ip,
1397 ntohs(reply_cm->match_dest_port), ntohs(reply_cm->xlate_dest_port));
1398 }
1399
Wayne Tanbb7f1782021-12-13 11:16:04 -08001400 /*
1401 * Add VLAN rule to reply_cm
1402 */
1403 if (msg->valid_flags & SFE_RULE_CREATE_VLAN_VALID) {
1404 struct sfe_vlan_rule *vlan_primary_rule = &msg->vlan_primary_rule;
1405 struct sfe_vlan_rule *vlan_secondary_rule = &msg->vlan_secondary_rule;
1406 sfe_ipv6_match_entry_set_vlan(reply_cm,
1407 vlan_primary_rule->egress_vlan_tag,
1408 vlan_primary_rule->ingress_vlan_tag,
1409 vlan_secondary_rule->egress_vlan_tag,
1410 vlan_secondary_rule->ingress_vlan_tag);
1411
1412 if ((msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_FLOW_BOTTOM_INTERFACE) &&
1413 reply_cm->egress_vlan_hdr_cnt > 0) {
1414 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_INSERT_EGRESS_VLAN_TAG;
1415 reply_cm->l2_hdr_size += reply_cm->egress_vlan_hdr_cnt * VLAN_HLEN;
1416 }
1417 }
1418
Xiaoping Fan978b3772015-05-27 14:15:18 -07001419#ifdef CONFIG_NF_FLOW_COOKIE
1420 reply_cm->flow_cookie = 0;
1421#endif
Zhi Chen8748eb32015-06-18 12:58:48 -07001422#ifdef CONFIG_XFRM
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301423 if (msg->valid_flags & SFE_RULE_CREATE_DIRECTION_VALID) {
1424 reply_cm->flow_accel = msg->direction_rule.return_accel;
1425 } else {
1426 reply_cm->flow_accel = 1;
1427 }
Zhi Chen8748eb32015-06-18 12:58:48 -07001428#endif
Xiaoping Fan978b3772015-05-27 14:15:18 -07001429 /*
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301430 * If l2_features are disabled and flow uses l2 features such as macvlan/bridge/pppoe/vlan,
1431 * bottom interfaces are expected to be disabled in the flow rule and always top interfaces
1432 * are used. In such cases, do not use HW csum offload. csum offload is used only when we
1433 * are sending directly to the destination interface that supports it.
1434 */
Suruchi Sumanf2077182022-01-13 21:35:23 +05301435 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 +05301436 if ((msg->conn_rule.flow_top_interface_num == msg->conn_rule.flow_interface_num) ||
1437 (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_FLOW_BOTTOM_INTERFACE)) {
1438 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_CSUM_OFFLOAD;
1439 }
1440 }
1441
1442 /*
Ken Zhubbf49652021-09-12 15:33:09 -07001443 * For the non-arp interface, we don't write L2 HDR.
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +05301444 * Excluding PPPoE from this, since we are now supporting PPPoE encap/decap.
Xiaoping Fan978b3772015-05-27 14:15:18 -07001445 */
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +05301446 if (sfe_ipv6_xmit_eth_type_check(src_dev, reply_cm->flags)) {
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301447
1448 /*
1449 * Check whether the rule has configured a specific source MAC address to use.
1450 * This is needed when virtual L3 interfaces such as br-lan, macvlan, vlan are used during egress
1451 */
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301452 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1453 ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)msg->conn_rule.return_mac);
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301454 } else {
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301455 if ((msg->valid_flags & SFE_RULE_CREATE_SRC_MAC_VALID) &&
1456 (msg->src_mac_rule.mac_valid_flags & SFE_SRC_MAC_FLOW_VALID)) {
1457 ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)msg->src_mac_rule.flow_src_mac);
1458 } else {
1459 ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)src_dev->dev_addr);
1460 }
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301461 }
1462
1463 ether_addr_copy((u8 *)reply_cm->xmit_dest_mac, (u8 *)msg->conn_rule.flow_mac);
1464
Xiaoping Fan978b3772015-05-27 14:15:18 -07001465 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_WRITE_L2_HDR;
Wayne Tanbb7f1782021-12-13 11:16:04 -08001466 reply_cm->l2_hdr_size += ETH_HLEN;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001467
1468 /*
1469 * If our dev writes Ethernet headers then we can write a really fast
1470 * version.
1471 */
1472 if (src_dev->header_ops) {
1473 if (src_dev->header_ops->create == eth_header) {
1474 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR;
1475 }
1476 }
1477 }
1478
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301479 /*
1480 * No support for NAT in ipv6
1481 */
Xiaoping Fan978b3772015-05-27 14:15:18 -07001482
Xiaoping Fan978b3772015-05-27 14:15:18 -07001483 /*
Xiaoping Fan978b3772015-05-27 14:15:18 -07001484 * Initialize the protocol-specific information that we track.
1485 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301486 switch (tuple->protocol) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07001487 case IPPROTO_TCP:
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301488 original_cm->protocol_state.tcp.win_scale = msg->tcp_rule.flow_window_scale;
1489 original_cm->protocol_state.tcp.max_win = msg->tcp_rule.flow_max_window ? msg->tcp_rule.flow_max_window : 1;
1490 original_cm->protocol_state.tcp.end = msg->tcp_rule.flow_end;
1491 original_cm->protocol_state.tcp.max_end = msg->tcp_rule.flow_max_end;
1492 reply_cm->protocol_state.tcp.win_scale = msg->tcp_rule.return_window_scale;
1493 reply_cm->protocol_state.tcp.max_win = msg->tcp_rule.return_max_window ? msg->tcp_rule.return_max_window : 1;
1494 reply_cm->protocol_state.tcp.end = msg->tcp_rule.return_end;
1495 reply_cm->protocol_state.tcp.max_end = msg->tcp_rule.return_max_end;
1496 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_NO_SEQ_CHECK) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07001497 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
1498 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
1499 }
1500 break;
1501 }
1502
Wayne Tanbb7f1782021-12-13 11:16:04 -08001503 /*
1504 * Fill in the ipv6_connection object.
1505 */
1506 c->protocol = tuple->protocol;
1507 c->src_ip[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip;
1508 c->src_ip_xlate[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip;
1509 c->src_port = tuple->flow_ident;
1510 c->src_port_xlate = tuple->flow_ident;
1511 c->original_dev = src_dev;
1512 c->original_match = original_cm;
1513
1514 c->dest_ip[0] = *(struct sfe_ipv6_addr *)tuple->return_ip;
1515 c->dest_ip_xlate[0] = *(struct sfe_ipv6_addr *)tuple->return_ip;
1516 c->dest_port = tuple->return_ident;
1517 c->dest_port_xlate = tuple->return_ident;
1518
1519 c->reply_dev = dest_dev;
1520 c->reply_match = reply_cm;
1521 c->debug_read_seq = 0;
1522 c->last_sync_jiffies = get_jiffies_64();
1523 c->removed = false;
1524
Xiaoping Fan978b3772015-05-27 14:15:18 -07001525 sfe_ipv6_connection_match_compute_translations(original_cm);
1526 sfe_ipv6_connection_match_compute_translations(reply_cm);
1527 sfe_ipv6_insert_connection(si, c);
1528
1529 spin_unlock_bh(&si->lock);
1530
1531 /*
1532 * We have everything we need!
1533 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301534 DEBUG_INFO("new connection - p: %d\n"
Tian Yang45f39c82020-10-06 14:07:47 -07001535 " s: %s:%pxM(%pxM):%pI6(%pI6):%u(%u)\n"
1536 " d: %s:%pxM(%pxM):%pI6(%pI6):%u(%u)\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301537 tuple->protocol,
1538 src_dev->name, msg->conn_rule.flow_mac, NULL,
1539 (void *)tuple->flow_ip, (void *)tuple->flow_ip, ntohs(tuple->flow_ident), ntohs(tuple->flow_ident),
1540 dest_dev->name, NULL, msg->conn_rule.return_mac,
1541 (void *)tuple->return_ip, (void *)tuple->return_ip, ntohs(tuple->return_ident), ntohs(tuple->return_ident));
Xiaoping Fan978b3772015-05-27 14:15:18 -07001542
1543 return 0;
1544}
1545
1546/*
1547 * sfe_ipv6_destroy_rule()
1548 * Destroy a forwarding rule.
1549 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301550void sfe_ipv6_destroy_rule(struct sfe_ipv6_rule_destroy_msg *msg)
Xiaoping Fan978b3772015-05-27 14:15:18 -07001551{
1552 struct sfe_ipv6 *si = &__si6;
1553 struct sfe_ipv6_connection *c;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301554 bool ret;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301555 struct sfe_ipv6_5tuple *tuple = &msg->tuple;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001556
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301557 this_cpu_inc(si->stats_pcpu->connection_destroy_requests64);
1558
Xiaoping Fan978b3772015-05-27 14:15:18 -07001559 spin_lock_bh(&si->lock);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001560
1561 /*
1562 * Check to see if we have a flow that matches the rule we're trying
1563 * to destroy. If there isn't then we can't destroy it.
1564 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301565 c = sfe_ipv6_find_connection(si, tuple->protocol, (struct sfe_ipv6_addr *)tuple->flow_ip, tuple->flow_ident,
1566 (struct sfe_ipv6_addr *)tuple->return_ip, tuple->return_ident);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001567 if (!c) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07001568 spin_unlock_bh(&si->lock);
1569
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301570 this_cpu_inc(si->stats_pcpu->connection_destroy_misses64);
1571
Xiaoping Fan978b3772015-05-27 14:15:18 -07001572 DEBUG_TRACE("connection does not exist - p: %d, s: %pI6:%u, d: %pI6:%u\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301573 tuple->protocol, tuple->flow_ip, ntohs(tuple->flow_ident),
1574 tuple->return_ip, ntohs(tuple->return_ident));
Xiaoping Fan978b3772015-05-27 14:15:18 -07001575 return;
1576 }
1577
1578 /*
1579 * Remove our connection details from the hash tables.
1580 */
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301581 ret = sfe_ipv6_remove_connection(si, c);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001582 spin_unlock_bh(&si->lock);
1583
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301584 if (ret) {
1585 sfe_ipv6_flush_connection(si, c, SFE_SYNC_REASON_DESTROY);
1586 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07001587
1588 DEBUG_INFO("connection destroyed - p: %d, s: %pI6:%u, d: %pI6:%u\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301589 tuple->protocol, tuple->flow_ip, ntohs(tuple->flow_ident),
1590 tuple->return_ip, ntohs(tuple->return_ident));
Xiaoping Fan978b3772015-05-27 14:15:18 -07001591}
1592
1593/*
1594 * sfe_ipv6_register_sync_rule_callback()
1595 * Register a callback for rule synchronization.
1596 */
1597void sfe_ipv6_register_sync_rule_callback(sfe_sync_rule_callback_t sync_rule_callback)
1598{
1599 struct sfe_ipv6 *si = &__si6;
1600
1601 spin_lock_bh(&si->lock);
1602 rcu_assign_pointer(si->sync_rule_callback, sync_rule_callback);
1603 spin_unlock_bh(&si->lock);
1604}
1605
1606/*
1607 * sfe_ipv6_get_debug_dev()
1608 */
1609static ssize_t sfe_ipv6_get_debug_dev(struct device *dev,
1610 struct device_attribute *attr,
1611 char *buf)
1612{
1613 struct sfe_ipv6 *si = &__si6;
1614 ssize_t count;
1615 int num;
1616
1617 spin_lock_bh(&si->lock);
1618 num = si->debug_dev;
1619 spin_unlock_bh(&si->lock);
1620
1621 count = snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", num);
1622 return count;
1623}
1624
1625/*
1626 * sfe_ipv6_destroy_all_rules_for_dev()
1627 * Destroy all connections that match a particular device.
1628 *
1629 * If we pass dev as NULL then this destroys all connections.
1630 */
1631void sfe_ipv6_destroy_all_rules_for_dev(struct net_device *dev)
1632{
1633 struct sfe_ipv6 *si = &__si6;
1634 struct sfe_ipv6_connection *c;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301635 bool ret;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001636
Xiaoping Fan34586472015-07-03 02:20:35 -07001637another_round:
Xiaoping Fan978b3772015-05-27 14:15:18 -07001638 spin_lock_bh(&si->lock);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001639
Xiaoping Fan34586472015-07-03 02:20:35 -07001640 for (c = si->all_connections_head; c; c = c->all_connections_next) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07001641 /*
Xiaoping Fan34586472015-07-03 02:20:35 -07001642 * Does this connection relate to the device we are destroying?
Xiaoping Fan978b3772015-05-27 14:15:18 -07001643 */
1644 if (!dev
1645 || (dev == c->original_dev)
1646 || (dev == c->reply_dev)) {
Xiaoping Fan34586472015-07-03 02:20:35 -07001647 break;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001648 }
Xiaoping Fan34586472015-07-03 02:20:35 -07001649 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07001650
Xiaoping Fan34586472015-07-03 02:20:35 -07001651 if (c) {
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301652 ret = sfe_ipv6_remove_connection(si, c);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001653 }
1654
1655 spin_unlock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07001656
1657 if (c) {
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301658 if (ret) {
1659 sfe_ipv6_flush_connection(si, c, SFE_SYNC_REASON_DESTROY);
1660 }
Xiaoping Fan34586472015-07-03 02:20:35 -07001661 goto another_round;
1662 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07001663}
1664
1665/*
1666 * sfe_ipv6_periodic_sync()
1667 */
Ken Zhu137722d2021-09-23 17:57:36 -07001668static void sfe_ipv6_periodic_sync(struct work_struct *work)
Xiaoping Fan978b3772015-05-27 14:15:18 -07001669{
Ken Zhu137722d2021-09-23 17:57:36 -07001670 struct sfe_ipv6 *si = container_of((struct delayed_work *)work, struct sfe_ipv6, sync_dwork);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07001671 u64 now_jiffies;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001672 int quota;
1673 sfe_sync_rule_callback_t sync_rule_callback;
Ken Zhu32b95392021-09-03 13:52:04 -07001674 struct sfe_ipv6_connection *c;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001675
1676 now_jiffies = get_jiffies_64();
1677
1678 rcu_read_lock();
1679 sync_rule_callback = rcu_dereference(si->sync_rule_callback);
1680 if (!sync_rule_callback) {
1681 rcu_read_unlock();
1682 goto done;
1683 }
1684
1685 spin_lock_bh(&si->lock);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001686
1687 /*
Ken Zhu32b95392021-09-03 13:52:04 -07001688 * If we have reached the end of the connection list, walk from
1689 * the connection head.
1690 */
1691 c = si->wc_next;
1692 if (unlikely(!c)) {
1693 c = si->all_connections_head;
1694 }
1695 /*
Xiaoping Fan978b3772015-05-27 14:15:18 -07001696 * Get an estimate of the number of connections to parse in this sync.
1697 */
1698 quota = (si->num_connections + 63) / 64;
1699
1700 /*
Ken Zhu32b95392021-09-03 13:52:04 -07001701 * Walk the "all connection" list and sync the connection state.
Xiaoping Fan978b3772015-05-27 14:15:18 -07001702 */
Ken Zhu32b95392021-09-03 13:52:04 -07001703 while (likely(c && quota)) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07001704 struct sfe_ipv6_connection_match *cm;
1705 struct sfe_ipv6_connection_match *counter_cm;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001706 struct sfe_connection_sync sis;
1707
Ken Zhu32b95392021-09-03 13:52:04 -07001708 cm = c->original_match;
1709 counter_cm = c->reply_match;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001710
1711 /*
Ken Zhu32b95392021-09-03 13:52:04 -07001712 * Didn't receive packets in the origial direction or reply
1713 * direction, move to the next connection.
Xiaoping Fan978b3772015-05-27 14:15:18 -07001714 */
Ken Zhu32b95392021-09-03 13:52:04 -07001715 if (!atomic_read(&cm->rx_packet_count) && !atomic_read(&counter_cm->rx_packet_count)) {
1716 c = c->all_connections_next;
1717 continue;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001718 }
1719
Ken Zhu32b95392021-09-03 13:52:04 -07001720 quota--;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001721
1722 /*
1723 * Sync the connection state.
1724 */
Xiaoping Fan99cb4c12015-08-21 19:07:32 -07001725 sfe_ipv6_gen_sync_connection(si, c, &sis, SFE_SYNC_REASON_STATS, now_jiffies);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001726
Ken Zhu32b95392021-09-03 13:52:04 -07001727 si->wc_next = c->all_connections_next;
1728
Xiaoping Fan978b3772015-05-27 14:15:18 -07001729 spin_unlock_bh(&si->lock);
1730 sync_rule_callback(&sis);
1731 spin_lock_bh(&si->lock);
Ken Zhu32b95392021-09-03 13:52:04 -07001732
1733 /*
1734 * c must be set and used in the same lock/unlock window;
1735 * because c could be removed when we don't hold the lock,
1736 * so delay grabbing until after the callback and relock.
1737 */
1738 c = si->wc_next;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001739 }
1740
Ken Zhu32b95392021-09-03 13:52:04 -07001741 /*
1742 * At the end of loop, put wc_next to the connection we left
1743 */
1744 si->wc_next = c;
1745
Xiaoping Fan978b3772015-05-27 14:15:18 -07001746 spin_unlock_bh(&si->lock);
1747 rcu_read_unlock();
1748
1749done:
Ken Zhu137722d2021-09-23 17:57:36 -07001750 schedule_delayed_work_on(si->work_cpu, (struct delayed_work *)work, ((HZ + 99) / 100));
Xiaoping Fan978b3772015-05-27 14:15:18 -07001751}
1752
1753/*
1754 * sfe_ipv6_debug_dev_read_start()
1755 * Generate part of the XML output.
1756 */
1757static bool sfe_ipv6_debug_dev_read_start(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
1758 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
1759{
1760 int bytes_read;
1761
Xiaoping Fan34586472015-07-03 02:20:35 -07001762 si->debug_read_seq++;
1763
Xiaoping Fan978b3772015-05-27 14:15:18 -07001764 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "<sfe_ipv6>\n");
1765 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1766 return false;
1767 }
1768
1769 *length -= bytes_read;
1770 *total_read += bytes_read;
1771
1772 ws->state++;
1773 return true;
1774}
1775
1776/*
1777 * sfe_ipv6_debug_dev_read_connections_start()
1778 * Generate part of the XML output.
1779 */
1780static bool sfe_ipv6_debug_dev_read_connections_start(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
1781 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
1782{
1783 int bytes_read;
1784
1785 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<connections>\n");
1786 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1787 return false;
1788 }
1789
1790 *length -= bytes_read;
1791 *total_read += bytes_read;
1792
1793 ws->state++;
1794 return true;
1795}
1796
1797/*
1798 * sfe_ipv6_debug_dev_read_connections_connection()
1799 * Generate part of the XML output.
1800 */
1801static bool sfe_ipv6_debug_dev_read_connections_connection(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
1802 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
1803{
1804 struct sfe_ipv6_connection *c;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001805 struct sfe_ipv6_connection_match *original_cm;
1806 struct sfe_ipv6_connection_match *reply_cm;
1807 int bytes_read;
1808 int protocol;
1809 struct net_device *src_dev;
1810 struct sfe_ipv6_addr src_ip;
1811 struct sfe_ipv6_addr src_ip_xlate;
1812 __be16 src_port;
1813 __be16 src_port_xlate;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07001814 u64 src_rx_packets;
1815 u64 src_rx_bytes;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001816 struct net_device *dest_dev;
1817 struct sfe_ipv6_addr dest_ip;
1818 struct sfe_ipv6_addr dest_ip_xlate;
1819 __be16 dest_port;
1820 __be16 dest_port_xlate;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07001821 u64 dest_rx_packets;
1822 u64 dest_rx_bytes;
1823 u64 last_sync_jiffies;
Ken Zhu37040ea2021-09-09 21:11:15 -07001824 u32 src_mark, dest_mark, src_priority, dest_priority, src_dscp, dest_dscp;
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301825 u32 packet, byte, original_cm_flags;
1826 u16 pppoe_session_id;
1827 u8 pppoe_remote_mac[ETH_ALEN];
Xiaoping Fan978b3772015-05-27 14:15:18 -07001828#ifdef CONFIG_NF_FLOW_COOKIE
1829 int src_flow_cookie, dst_flow_cookie;
1830#endif
1831
1832 spin_lock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07001833
1834 for (c = si->all_connections_head; c; c = c->all_connections_next) {
1835 if (c->debug_read_seq < si->debug_read_seq) {
1836 c->debug_read_seq = si->debug_read_seq;
1837 break;
1838 }
1839 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07001840
1841 /*
Xiaoping Fan34586472015-07-03 02:20:35 -07001842 * If there were no connections then move to the next state.
Xiaoping Fan978b3772015-05-27 14:15:18 -07001843 */
1844 if (!c) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07001845 spin_unlock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07001846 ws->state++;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001847 return true;
1848 }
1849
1850 original_cm = c->original_match;
1851 reply_cm = c->reply_match;
1852
1853 protocol = c->protocol;
1854 src_dev = c->original_dev;
1855 src_ip = c->src_ip[0];
1856 src_ip_xlate = c->src_ip_xlate[0];
1857 src_port = c->src_port;
1858 src_port_xlate = c->src_port_xlate;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001859 src_priority = original_cm->priority;
1860 src_dscp = original_cm->dscp >> SFE_IPV6_DSCP_SHIFT;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001861
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301862 sfe_ipv6_connection_match_update_summary_stats(original_cm, &packet, &byte);
1863 sfe_ipv6_connection_match_update_summary_stats(reply_cm, &packet, &byte);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001864
1865 src_rx_packets = original_cm->rx_packet_count64;
1866 src_rx_bytes = original_cm->rx_byte_count64;
Ken Zhu37040ea2021-09-09 21:11:15 -07001867 src_mark = original_cm->mark;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001868 dest_dev = c->reply_dev;
1869 dest_ip = c->dest_ip[0];
1870 dest_ip_xlate = c->dest_ip_xlate[0];
1871 dest_port = c->dest_port;
1872 dest_port_xlate = c->dest_port_xlate;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001873 dest_priority = reply_cm->priority;
1874 dest_dscp = reply_cm->dscp >> SFE_IPV6_DSCP_SHIFT;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001875 dest_rx_packets = reply_cm->rx_packet_count64;
1876 dest_rx_bytes = reply_cm->rx_byte_count64;
1877 last_sync_jiffies = get_jiffies_64() - c->last_sync_jiffies;
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301878 original_cm_flags = original_cm->flags;
1879 pppoe_session_id = original_cm->pppoe_session_id;
1880 ether_addr_copy(pppoe_remote_mac, original_cm->pppoe_remote_mac);
Ken Zhu37040ea2021-09-09 21:11:15 -07001881 dest_mark = reply_cm->mark;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001882#ifdef CONFIG_NF_FLOW_COOKIE
1883 src_flow_cookie = original_cm->flow_cookie;
1884 dst_flow_cookie = reply_cm->flow_cookie;
1885#endif
1886 spin_unlock_bh(&si->lock);
1887
1888 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t\t<connection "
1889 "protocol=\"%u\" "
1890 "src_dev=\"%s\" "
1891 "src_ip=\"%pI6\" src_ip_xlate=\"%pI6\" "
1892 "src_port=\"%u\" src_port_xlate=\"%u\" "
Xiaoping Fane1963d42015-08-25 17:06:19 -07001893 "src_priority=\"%u\" src_dscp=\"%u\" "
Xiaoping Fan978b3772015-05-27 14:15:18 -07001894 "src_rx_pkts=\"%llu\" src_rx_bytes=\"%llu\" "
Ken Zhu37040ea2021-09-09 21:11:15 -07001895 "src_mark=\"%08x\" "
Xiaoping Fan978b3772015-05-27 14:15:18 -07001896 "dest_dev=\"%s\" "
1897 "dest_ip=\"%pI6\" dest_ip_xlate=\"%pI6\" "
1898 "dest_port=\"%u\" dest_port_xlate=\"%u\" "
Xiaoping Fane1963d42015-08-25 17:06:19 -07001899 "dest_priority=\"%u\" dest_dscp=\"%u\" "
Xiaoping Fan978b3772015-05-27 14:15:18 -07001900 "dest_rx_pkts=\"%llu\" dest_rx_bytes=\"%llu\" "
Ken Zhu37040ea2021-09-09 21:11:15 -07001901 "dest_mark=\"%08x\" "
Xiaoping Fan978b3772015-05-27 14:15:18 -07001902#ifdef CONFIG_NF_FLOW_COOKIE
1903 "src_flow_cookie=\"%d\" dst_flow_cookie=\"%d\" "
1904#endif
Ken Zhu37040ea2021-09-09 21:11:15 -07001905 "last_sync=\"%llu\" ",
Xiaoping Fan978b3772015-05-27 14:15:18 -07001906 protocol,
1907 src_dev->name,
1908 &src_ip, &src_ip_xlate,
1909 ntohs(src_port), ntohs(src_port_xlate),
Xiaoping Fane1963d42015-08-25 17:06:19 -07001910 src_priority, src_dscp,
Xiaoping Fan978b3772015-05-27 14:15:18 -07001911 src_rx_packets, src_rx_bytes,
Ken Zhu37040ea2021-09-09 21:11:15 -07001912 src_mark,
Xiaoping Fan978b3772015-05-27 14:15:18 -07001913 dest_dev->name,
1914 &dest_ip, &dest_ip_xlate,
1915 ntohs(dest_port), ntohs(dest_port_xlate),
Xiaoping Fane1963d42015-08-25 17:06:19 -07001916 dest_priority, dest_dscp,
Xiaoping Fan978b3772015-05-27 14:15:18 -07001917 dest_rx_packets, dest_rx_bytes,
Ken Zhu37040ea2021-09-09 21:11:15 -07001918 dest_mark,
Xiaoping Fan978b3772015-05-27 14:15:18 -07001919#ifdef CONFIG_NF_FLOW_COOKIE
1920 src_flow_cookie, dst_flow_cookie,
1921#endif
Ken Zhu37040ea2021-09-09 21:11:15 -07001922 last_sync_jiffies);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001923
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301924 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 +05301925 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 +05301926 pppoe_session_id, pppoe_remote_mac);
1927 }
1928
1929 bytes_read += snprintf(msg + bytes_read, CHAR_DEV_MSG_SIZE, ")/>\n");
1930
Xiaoping Fan978b3772015-05-27 14:15:18 -07001931 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1932 return false;
1933 }
1934
1935 *length -= bytes_read;
1936 *total_read += bytes_read;
1937
Xiaoping Fan978b3772015-05-27 14:15:18 -07001938 return true;
1939}
1940
1941/*
1942 * sfe_ipv6_debug_dev_read_connections_end()
1943 * Generate part of the XML output.
1944 */
1945static bool sfe_ipv6_debug_dev_read_connections_end(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
1946 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
1947{
1948 int bytes_read;
1949
1950 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t</connections>\n");
1951 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1952 return false;
1953 }
1954
1955 *length -= bytes_read;
1956 *total_read += bytes_read;
1957
1958 ws->state++;
1959 return true;
1960}
1961
1962/*
1963 * sfe_ipv6_debug_dev_read_exceptions_start()
1964 * Generate part of the XML output.
1965 */
1966static bool sfe_ipv6_debug_dev_read_exceptions_start(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
1967 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
1968{
1969 int bytes_read;
1970
1971 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<exceptions>\n");
1972 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1973 return false;
1974 }
1975
1976 *length -= bytes_read;
1977 *total_read += bytes_read;
1978
1979 ws->state++;
1980 return true;
1981}
1982
1983/*
1984 * sfe_ipv6_debug_dev_read_exceptions_exception()
1985 * Generate part of the XML output.
1986 */
1987static bool sfe_ipv6_debug_dev_read_exceptions_exception(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
1988 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
1989{
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301990 int i;
1991 u64 val = 0;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001992
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301993 for_each_possible_cpu(i) {
1994 const struct sfe_ipv6_stats *s = per_cpu_ptr(si->stats_pcpu, i);
1995 val += s->exception_events64[ws->iter_exception];
1996 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07001997
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301998 if (val) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07001999 int bytes_read;
2000
2001 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE,
2002 "\t\t<exception name=\"%s\" count=\"%llu\" />\n",
2003 sfe_ipv6_exception_events_string[ws->iter_exception],
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302004 val);
2005
Xiaoping Fan978b3772015-05-27 14:15:18 -07002006 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2007 return false;
2008 }
2009
2010 *length -= bytes_read;
2011 *total_read += bytes_read;
2012 }
2013
2014 ws->iter_exception++;
2015 if (ws->iter_exception >= SFE_IPV6_EXCEPTION_EVENT_LAST) {
2016 ws->iter_exception = 0;
2017 ws->state++;
2018 }
2019
2020 return true;
2021}
2022
2023/*
2024 * sfe_ipv6_debug_dev_read_exceptions_end()
2025 * Generate part of the XML output.
2026 */
2027static bool sfe_ipv6_debug_dev_read_exceptions_end(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
2028 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
2029{
2030 int bytes_read;
2031
2032 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t</exceptions>\n");
2033 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2034 return false;
2035 }
2036
2037 *length -= bytes_read;
2038 *total_read += bytes_read;
2039
2040 ws->state++;
2041 return true;
2042}
2043
2044/*
2045 * sfe_ipv6_debug_dev_read_stats()
2046 * Generate part of the XML output.
2047 */
2048static bool sfe_ipv6_debug_dev_read_stats(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
2049 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
2050{
2051 int bytes_read;
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302052 struct sfe_ipv6_stats stats;
2053 unsigned int num_conn;
2054
2055 sfe_ipv6_update_summary_stats(si, &stats);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002056
2057 spin_lock_bh(&si->lock);
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302058 num_conn = si->num_connections;
Xiaoping Fan978b3772015-05-27 14:15:18 -07002059 spin_unlock_bh(&si->lock);
2060
2061 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<stats "
2062 "num_connections=\"%u\" "
Suruchi Suman23a279d2021-11-16 15:13:09 +05302063 "pkts_dropped=\"%llu\" "
Xiaoping Fan978b3772015-05-27 14:15:18 -07002064 "pkts_forwarded=\"%llu\" pkts_not_forwarded=\"%llu\" "
2065 "create_requests=\"%llu\" create_collisions=\"%llu\" "
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05302066 "create_failures=\"%llu\" "
Xiaoping Fan978b3772015-05-27 14:15:18 -07002067 "destroy_requests=\"%llu\" destroy_misses=\"%llu\" "
2068 "flushes=\"%llu\" "
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +05302069 "hash_hits=\"%llu\" hash_reorders=\"%llu\" "
2070 "pppoe_encap_pkts_fwded=\"%llu\" "
Guduri Prathyusha034d6352022-01-12 16:49:04 +05302071 "pppoe_decap_pkts_fwded=\"%llu\" "
2072 "pppoe_bridge_pkts_fwded=\"%llu\" />\n",
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302073
2074 num_conn,
Suruchi Suman23a279d2021-11-16 15:13:09 +05302075 stats.packets_dropped64,
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302076 stats.packets_forwarded64,
2077 stats.packets_not_forwarded64,
2078 stats.connection_create_requests64,
2079 stats.connection_create_collisions64,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05302080 stats.connection_create_failures64,
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302081 stats.connection_destroy_requests64,
2082 stats.connection_destroy_misses64,
2083 stats.connection_flushes64,
2084 stats.connection_match_hash_hits64,
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +05302085 stats.connection_match_hash_reorders64,
2086 stats.pppoe_encap_packets_forwarded64,
Guduri Prathyusha034d6352022-01-12 16:49:04 +05302087 stats.pppoe_decap_packets_forwarded64,
2088 stats.pppoe_bridge_packets_forwarded64);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002089 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2090 return false;
2091 }
2092
2093 *length -= bytes_read;
2094 *total_read += bytes_read;
2095
2096 ws->state++;
2097 return true;
2098}
2099
2100/*
2101 * sfe_ipv6_debug_dev_read_end()
2102 * Generate part of the XML output.
2103 */
2104static bool sfe_ipv6_debug_dev_read_end(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
2105 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
2106{
2107 int bytes_read;
2108
2109 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "</sfe_ipv6>\n");
2110 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2111 return false;
2112 }
2113
2114 *length -= bytes_read;
2115 *total_read += bytes_read;
2116
2117 ws->state++;
2118 return true;
2119}
2120
2121/*
2122 * Array of write functions that write various XML elements that correspond to
2123 * our XML output state machine.
2124 */
2125static sfe_ipv6_debug_xml_write_method_t sfe_ipv6_debug_xml_write_methods[SFE_IPV6_DEBUG_XML_STATE_DONE] = {
2126 sfe_ipv6_debug_dev_read_start,
2127 sfe_ipv6_debug_dev_read_connections_start,
2128 sfe_ipv6_debug_dev_read_connections_connection,
2129 sfe_ipv6_debug_dev_read_connections_end,
2130 sfe_ipv6_debug_dev_read_exceptions_start,
2131 sfe_ipv6_debug_dev_read_exceptions_exception,
2132 sfe_ipv6_debug_dev_read_exceptions_end,
2133 sfe_ipv6_debug_dev_read_stats,
2134 sfe_ipv6_debug_dev_read_end,
2135};
2136
2137/*
2138 * sfe_ipv6_debug_dev_read()
2139 * Send info to userspace upon read request from user
2140 */
2141static ssize_t sfe_ipv6_debug_dev_read(struct file *filp, char *buffer, size_t length, loff_t *offset)
2142{
2143 char msg[CHAR_DEV_MSG_SIZE];
2144 int total_read = 0;
2145 struct sfe_ipv6_debug_xml_write_state *ws;
2146 struct sfe_ipv6 *si = &__si6;
2147
2148 ws = (struct sfe_ipv6_debug_xml_write_state *)filp->private_data;
2149 while ((ws->state != SFE_IPV6_DEBUG_XML_STATE_DONE) && (length > CHAR_DEV_MSG_SIZE)) {
2150 if ((sfe_ipv6_debug_xml_write_methods[ws->state])(si, buffer, msg, &length, &total_read, ws)) {
2151 continue;
2152 }
2153 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07002154 return total_read;
2155}
2156
2157/*
Xiaoping Fan978b3772015-05-27 14:15:18 -07002158 * sfe_ipv6_debug_dev_open()
2159 */
2160static int sfe_ipv6_debug_dev_open(struct inode *inode, struct file *file)
2161{
2162 struct sfe_ipv6_debug_xml_write_state *ws;
2163
2164 ws = (struct sfe_ipv6_debug_xml_write_state *)file->private_data;
2165 if (ws) {
2166 return 0;
2167 }
2168
2169 ws = kzalloc(sizeof(struct sfe_ipv6_debug_xml_write_state), GFP_KERNEL);
2170 if (!ws) {
2171 return -ENOMEM;
2172 }
2173
2174 ws->state = SFE_IPV6_DEBUG_XML_STATE_START;
2175 file->private_data = ws;
2176
2177 return 0;
2178}
2179
2180/*
2181 * sfe_ipv6_debug_dev_release()
2182 */
2183static int sfe_ipv6_debug_dev_release(struct inode *inode, struct file *file)
2184{
2185 struct sfe_ipv6_debug_xml_write_state *ws;
Xiaoping Fan978b3772015-05-27 14:15:18 -07002186
2187 ws = (struct sfe_ipv6_debug_xml_write_state *)file->private_data;
Xiaoping Fan34586472015-07-03 02:20:35 -07002188 if (ws) {
2189 /*
2190 * We've finished with our output so free the write state.
2191 */
2192 kfree(ws);
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05302193 file->private_data = NULL;
Xiaoping Fan978b3772015-05-27 14:15:18 -07002194 }
2195
Xiaoping Fan978b3772015-05-27 14:15:18 -07002196 return 0;
2197}
2198
2199/*
2200 * File operations used in the debug char device
2201 */
2202static struct file_operations sfe_ipv6_debug_dev_fops = {
2203 .read = sfe_ipv6_debug_dev_read,
Xiaoping Fan978b3772015-05-27 14:15:18 -07002204 .open = sfe_ipv6_debug_dev_open,
2205 .release = sfe_ipv6_debug_dev_release
2206};
2207
2208#ifdef CONFIG_NF_FLOW_COOKIE
2209/*
2210 * sfe_ipv6_register_flow_cookie_cb
2211 * register a function in SFE to let SFE use this function to configure flow cookie for a flow
2212 *
2213 * Hardware driver which support flow cookie should register a callback function in SFE. Then SFE
2214 * can use this function to configure flow cookie for a flow.
2215 * return: 0, success; !=0, fail
2216 */
2217int sfe_ipv6_register_flow_cookie_cb(sfe_ipv6_flow_cookie_set_func_t cb)
2218{
2219 struct sfe_ipv6 *si = &__si6;
2220
2221 BUG_ON(!cb);
2222
2223 if (si->flow_cookie_set_func) {
2224 return -1;
2225 }
2226
2227 rcu_assign_pointer(si->flow_cookie_set_func, cb);
2228 return 0;
2229}
2230
2231/*
2232 * sfe_ipv6_unregister_flow_cookie_cb
2233 * unregister function which is used to configure flow cookie for a flow
2234 *
2235 * return: 0, success; !=0, fail
2236 */
2237int sfe_ipv6_unregister_flow_cookie_cb(sfe_ipv6_flow_cookie_set_func_t cb)
2238{
2239 struct sfe_ipv6 *si = &__si6;
2240
2241 RCU_INIT_POINTER(si->flow_cookie_set_func, NULL);
2242 return 0;
2243}
Xiaoping Fan640faf42015-08-28 15:50:55 -07002244
2245/*
2246 * sfe_ipv6_get_flow_cookie()
2247 */
2248static ssize_t sfe_ipv6_get_flow_cookie(struct device *dev,
2249 struct device_attribute *attr,
2250 char *buf)
2251{
2252 struct sfe_ipv6 *si = &__si6;
Xiaoping Fan01c67cc2015-11-09 11:31:57 -08002253 return snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", si->flow_cookie_enable);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002254}
2255
2256/*
2257 * sfe_ipv6_set_flow_cookie()
2258 */
2259static ssize_t sfe_ipv6_set_flow_cookie(struct device *dev,
2260 struct device_attribute *attr,
2261 const char *buf, size_t size)
2262{
2263 struct sfe_ipv6 *si = &__si6;
Ken Zhu137722d2021-09-23 17:57:36 -07002264 si->flow_cookie_enable = strict_strtol(buf, NULL, 0);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002265
2266 return size;
2267}
2268
2269/*
2270 * sysfs attributes.
2271 */
2272static const struct device_attribute sfe_ipv6_flow_cookie_attr =
Xiaoping Fane70da412016-02-26 16:47:57 -08002273 __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 -07002274#endif /*CONFIG_NF_FLOW_COOKIE*/
2275
Ken Zhu137722d2021-09-23 17:57:36 -07002276/*
2277 * sfe_ipv6_get_cpu()
2278 */
2279static ssize_t sfe_ipv6_get_cpu(struct device *dev,
2280 struct device_attribute *attr,
2281 char *buf)
2282{
2283 struct sfe_ipv6 *si = &__si6;
2284 return snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", si->work_cpu);
2285}
2286
2287/*
Wayne Tanbb7f1782021-12-13 11:16:04 -08002288 * sfe_ipv6_set_cpu()
Ken Zhu137722d2021-09-23 17:57:36 -07002289 */
2290static ssize_t sfe_ipv6_set_cpu(struct device *dev,
2291 struct device_attribute *attr,
2292 const char *buf, size_t size)
2293{
2294 struct sfe_ipv6 *si = &__si6;
2295 int work_cpu;
2296
2297 work_cpu = simple_strtol(buf, NULL, 0);
2298 if ((work_cpu >= 0) && (work_cpu <= NR_CPUS)) {
2299 si->work_cpu = work_cpu;
2300 } else {
2301 dev_err(dev, "%s is not in valid range[0,%d]", buf, NR_CPUS);
2302 }
2303
2304 return size;
2305}
2306/*
2307 * sysfs attributes.
2308 */
2309static const struct device_attribute sfe_ipv6_cpu_attr =
2310 __ATTR(stat_work_cpu, S_IWUSR | S_IRUGO, sfe_ipv6_get_cpu, sfe_ipv6_set_cpu);
2311
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05302312 /*
2313 * sfe_ipv6_hash_init()
2314 * Initialize conn match hash lists
2315 */
2316static void sfe_ipv6_conn_match_hash_init(struct sfe_ipv6 *si, int len)
2317{
2318 struct hlist_head *hash_list = si->hlist_conn_match_hash_head;
2319 int i;
2320
2321 for (i = 0; i < len; i++) {
2322 INIT_HLIST_HEAD(&hash_list[i]);
2323 }
2324}
2325
Suruchi Suman23a279d2021-11-16 15:13:09 +05302326#ifdef SFE_PROCESS_LOCAL_OUT
2327/*
2328 * sfe_ipv6_local_out()
2329 * Called for packets from ip_local_out() - post encapsulation & other packets
2330 */
2331static unsigned int sfe_ipv6_local_out(void *priv,
2332 struct sk_buff *skb,
2333 const struct nf_hook_state *nhs)
2334{
2335 DEBUG_TRACE("sfe: sfe_ipv6_local_out hook called.\n");
2336
2337 if (likely(skb->skb_iif)) {
2338 return sfe_ipv6_recv(skb->dev, skb, NULL, true) ? NF_STOLEN : NF_ACCEPT;
2339 }
2340
2341 return NF_ACCEPT;
2342}
2343
2344/*
2345 * struct nf_hook_ops sfe_ipv6_ops_local_out[]
2346 * Hooks into netfilter local out packet monitoring points.
2347 */
2348static struct nf_hook_ops sfe_ipv6_ops_local_out[] __read_mostly = {
2349
2350 /*
2351 * Local out routing hook is used to monitor packets.
2352 */
2353 {
2354 .hook = sfe_ipv6_local_out,
2355 .pf = PF_INET6,
2356 .hooknum = NF_INET_LOCAL_OUT,
2357 .priority = NF_IP6_PRI_FIRST,
2358 },
2359};
2360#endif
2361
Xiaoping Fan978b3772015-05-27 14:15:18 -07002362/*
2363 * sfe_ipv6_init()
2364 */
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05302365int sfe_ipv6_init(void)
Xiaoping Fan978b3772015-05-27 14:15:18 -07002366{
2367 struct sfe_ipv6 *si = &__si6;
2368 int result = -1;
2369
2370 DEBUG_INFO("SFE IPv6 init\n");
2371
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05302372 sfe_ipv6_conn_match_hash_init(si, ARRAY_SIZE(si->hlist_conn_match_hash_head));
2373
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302374 si->stats_pcpu = alloc_percpu_gfp(struct sfe_ipv6_stats, GFP_KERNEL | __GFP_ZERO);
2375 if (!si->stats_pcpu) {
2376 DEBUG_ERROR("failed to allocate stats memory for sfe_ipv6\n");
2377 goto exit0;
2378 }
2379
Xiaoping Fan978b3772015-05-27 14:15:18 -07002380 /*
2381 * Create sys/sfe_ipv6
2382 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302383 si->sys_ipv6 = kobject_create_and_add("sfe_ipv6", NULL);
2384 if (!si->sys_ipv6) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07002385 DEBUG_ERROR("failed to register sfe_ipv6\n");
2386 goto exit1;
2387 }
2388
2389 /*
2390 * Create files, one for each parameter supported by this module.
2391 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302392 result = sysfs_create_file(si->sys_ipv6, &sfe_ipv6_debug_dev_attr.attr);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002393 if (result) {
2394 DEBUG_ERROR("failed to register debug dev file: %d\n", result);
2395 goto exit2;
2396 }
2397
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302398 result = sysfs_create_file(si->sys_ipv6, &sfe_ipv6_cpu_attr.attr);
Ken Zhu137722d2021-09-23 17:57:36 -07002399 if (result) {
2400 DEBUG_ERROR("failed to register debug dev file: %d\n", result);
2401 goto exit3;
2402 }
2403
Xiaoping Fan640faf42015-08-28 15:50:55 -07002404#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302405 result = sysfs_create_file(si->sys_ipv6, &sfe_ipv6_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002406 if (result) {
2407 DEBUG_ERROR("failed to register flow cookie enable file: %d\n", result);
Ken Zhu137722d2021-09-23 17:57:36 -07002408 goto exit4;
Xiaoping Fan640faf42015-08-28 15:50:55 -07002409 }
2410#endif /* CONFIG_NF_FLOW_COOKIE */
2411
Suruchi Suman23a279d2021-11-16 15:13:09 +05302412#ifdef SFE_PROCESS_LOCAL_OUT
2413#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
2414 result = nf_register_hooks(sfe_ipv6_ops_local_out, ARRAY_SIZE(sfe_ipv6_ops_local_out));
2415#else
2416 result = nf_register_net_hooks(&init_net, sfe_ipv6_ops_local_out, ARRAY_SIZE(sfe_ipv6_ops_local_out));
2417#endif
2418#endif
2419 if (result < 0) {
2420 DEBUG_ERROR("can't register nf local out hook: %d\n", result);
2421 goto exit5;
2422 } else {
2423 DEBUG_ERROR("Register nf local out hook success: %d\n", result);
2424 }
2425
Xiaoping Fan978b3772015-05-27 14:15:18 -07002426 /*
2427 * Register our debug char device.
2428 */
2429 result = register_chrdev(0, "sfe_ipv6", &sfe_ipv6_debug_dev_fops);
2430 if (result < 0) {
2431 DEBUG_ERROR("Failed to register chrdev: %d\n", result);
Suruchi Suman23a279d2021-11-16 15:13:09 +05302432 goto exit6;
Xiaoping Fan978b3772015-05-27 14:15:18 -07002433 }
2434
2435 si->debug_dev = result;
Ken Zhu137722d2021-09-23 17:57:36 -07002436 si->work_cpu = WORK_CPU_UNBOUND;
Xiaoping Fan978b3772015-05-27 14:15:18 -07002437
2438 /*
Ken Zhu137722d2021-09-23 17:57:36 -07002439 * Create work to handle periodic statistics.
Xiaoping Fan978b3772015-05-27 14:15:18 -07002440 */
Ken Zhu137722d2021-09-23 17:57:36 -07002441 INIT_DELAYED_WORK(&(si->sync_dwork), sfe_ipv6_periodic_sync);
2442 schedule_delayed_work_on(si->work_cpu, &(si->sync_dwork), ((HZ + 99) / 100));
Xiaoping Fan978b3772015-05-27 14:15:18 -07002443 spin_lock_init(&si->lock);
2444
2445 return 0;
2446
Suruchi Suman23a279d2021-11-16 15:13:09 +05302447exit6:
2448#ifdef SFE_PROCESS_LOCAL_OUT
2449#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
2450 DEBUG_TRACE("sfe: Unregister local out hook\n");
2451 nf_unregister_hooks(sfe_ipv6_ops_local_out, ARRAY_SIZE(sfe_ipv6_ops_local_out));
2452#else
2453 DEBUG_TRACE("sfe: Unregister local out hook\n");
2454 nf_unregister_net_hooks(&init_net, sfe_ipv6_ops_local_out, ARRAY_SIZE(sfe_ipv6_ops_local_out));
2455#endif
2456#endif
2457
Ken Zhu137722d2021-09-23 17:57:36 -07002458exit5:
Xiaoping Fan640faf42015-08-28 15:50:55 -07002459#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302460 sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002461
Ken Zhu137722d2021-09-23 17:57:36 -07002462exit4:
Xiaoping Fan640faf42015-08-28 15:50:55 -07002463#endif /* CONFIG_NF_FLOW_COOKIE */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302464 sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_cpu_attr.attr);
Suruchi Suman23a279d2021-11-16 15:13:09 +05302465
Ken Zhu137722d2021-09-23 17:57:36 -07002466exit3:
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302467 sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_debug_dev_attr.attr);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002468
2469exit2:
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302470 kobject_put(si->sys_ipv6);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002471
2472exit1:
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302473 free_percpu(si->stats_pcpu);
2474
2475exit0:
Xiaoping Fan978b3772015-05-27 14:15:18 -07002476 return result;
2477}
2478
2479/*
2480 * sfe_ipv6_exit()
2481 */
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05302482void sfe_ipv6_exit(void)
Xiaoping Fan978b3772015-05-27 14:15:18 -07002483{
2484 struct sfe_ipv6 *si = &__si6;
2485
2486 DEBUG_INFO("SFE IPv6 exit\n");
2487
2488 /*
2489 * Destroy all connections.
2490 */
2491 sfe_ipv6_destroy_all_rules_for_dev(NULL);
2492
Ken Zhu137722d2021-09-23 17:57:36 -07002493 cancel_delayed_work(&si->sync_dwork);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002494
2495 unregister_chrdev(si->debug_dev, "sfe_ipv6");
2496
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302497 free_percpu(si->stats_pcpu);
2498
Suruchi Suman23a279d2021-11-16 15:13:09 +05302499#ifdef SFE_PROCESS_LOCAL_OUT
2500#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
2501 DEBUG_TRACE("sfe: Unregister local out hook\n");
2502 nf_unregister_hooks(sfe_ipv6_ops_local_out, ARRAY_SIZE(sfe_ipv6_ops_local_out));
2503#else
2504 DEBUG_TRACE("sfe: Unregister local out hook\n");
2505 nf_unregister_net_hooks(&init_net, sfe_ipv6_ops_local_out, ARRAY_SIZE(sfe_ipv6_ops_local_out));
2506#endif
2507#endif
2508
Xiaoping Fan640faf42015-08-28 15:50:55 -07002509#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302510 sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002511#endif /* CONFIG_NF_FLOW_COOKIE */
Ken Zhu137722d2021-09-23 17:57:36 -07002512
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302513 sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_cpu_attr.attr);
Ken Zhu137722d2021-09-23 17:57:36 -07002514
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302515 sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_debug_dev_attr.attr);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002516
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302517 kobject_put(si->sys_ipv6);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002518}
2519
Xiaoping Fan978b3772015-05-27 14:15:18 -07002520#ifdef CONFIG_NF_FLOW_COOKIE
2521EXPORT_SYMBOL(sfe_ipv6_register_flow_cookie_cb);
2522EXPORT_SYMBOL(sfe_ipv6_unregister_flow_cookie_cb);
2523#endif