blob: eab271734ab18abd331a63579acc0ddd86b20aa3 [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>
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +053034#include "sfe_debug.h"
Ratheesh Kannoth89302a72021-10-20 08:10:37 +053035#include "sfe_api.h"
Xiaoping Fan978b3772015-05-27 14:15:18 -070036#include "sfe.h"
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +053037#include "sfe_flow_cookie.h"
38#include "sfe_ipv6.h"
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +053039#include "sfe_ipv6_udp.h"
40#include "sfe_ipv6_tcp.h"
41#include "sfe_ipv6_icmp.h"
Xiaoping Fan978b3772015-05-27 14:15:18 -070042
Ratheesh Kannoth89302a72021-10-20 08:10:37 +053043#define sfe_ipv6_addr_copy(src, dest) memcpy((void *)(dest), (void *)(src), 16)
44
Xiaoping Fan978b3772015-05-27 14:15:18 -070045static char *sfe_ipv6_exception_events_string[SFE_IPV6_EXCEPTION_EVENT_LAST] = {
46 "UDP_HEADER_INCOMPLETE",
47 "UDP_NO_CONNECTION",
48 "UDP_IP_OPTIONS_OR_INITIAL_FRAGMENT",
49 "UDP_SMALL_TTL",
50 "UDP_NEEDS_FRAGMENTATION",
51 "TCP_HEADER_INCOMPLETE",
52 "TCP_NO_CONNECTION_SLOW_FLAGS",
53 "TCP_NO_CONNECTION_FAST_FLAGS",
54 "TCP_IP_OPTIONS_OR_INITIAL_FRAGMENT",
55 "TCP_SMALL_TTL",
56 "TCP_NEEDS_FRAGMENTATION",
57 "TCP_FLAGS",
58 "TCP_SEQ_EXCEEDS_RIGHT_EDGE",
59 "TCP_SMALL_DATA_OFFS",
60 "TCP_BAD_SACK",
61 "TCP_BIG_DATA_OFFS",
62 "TCP_SEQ_BEFORE_LEFT_EDGE",
63 "TCP_ACK_EXCEEDS_RIGHT_EDGE",
64 "TCP_ACK_BEFORE_LEFT_EDGE",
65 "ICMP_HEADER_INCOMPLETE",
66 "ICMP_UNHANDLED_TYPE",
67 "ICMP_IPV6_HEADER_INCOMPLETE",
68 "ICMP_IPV6_NON_V6",
69 "ICMP_IPV6_IP_OPTIONS_INCOMPLETE",
70 "ICMP_IPV6_UDP_HEADER_INCOMPLETE",
71 "ICMP_IPV6_TCP_HEADER_INCOMPLETE",
72 "ICMP_IPV6_UNHANDLED_PROTOCOL",
73 "ICMP_NO_CONNECTION",
74 "ICMP_FLUSHED_CONNECTION",
75 "HEADER_INCOMPLETE",
76 "BAD_TOTAL_LENGTH",
77 "NON_V6",
78 "NON_INITIAL_FRAGMENT",
79 "DATAGRAM_INCOMPLETE",
80 "IP_OPTIONS_INCOMPLETE",
81 "UNHANDLED_PROTOCOL",
82 "FLOW_COOKIE_ADD_FAIL"
83};
84
Xiaoping Fan6a1672f2016-08-17 19:58:12 -070085static struct sfe_ipv6 __si6;
Xiaoping Fan978b3772015-05-27 14:15:18 -070086
87/*
88 * sfe_ipv6_get_debug_dev()
89 */
90static ssize_t sfe_ipv6_get_debug_dev(struct device *dev, struct device_attribute *attr, char *buf);
91
92/*
93 * sysfs attributes.
94 */
95static const struct device_attribute sfe_ipv6_debug_dev_attr =
Xiaoping Fane70da412016-02-26 16:47:57 -080096 __ATTR(debug_dev, S_IWUSR | S_IRUGO, sfe_ipv6_get_debug_dev, NULL);
Xiaoping Fan978b3772015-05-27 14:15:18 -070097
98/*
Xiaoping Fan978b3772015-05-27 14:15:18 -070099 * sfe_ipv6_get_connection_match_hash()
100 * Generate the hash used in connection match lookups.
101 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700102static inline unsigned int sfe_ipv6_get_connection_match_hash(struct net_device *dev, u8 protocol,
Xiaoping Fan978b3772015-05-27 14:15:18 -0700103 struct sfe_ipv6_addr *src_ip, __be16 src_port,
104 struct sfe_ipv6_addr *dest_ip, __be16 dest_port)
105{
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700106 u32 idx, hash = 0;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700107 size_t dev_addr = (size_t)dev;
108
109 for (idx = 0; idx < 4; idx++) {
110 hash ^= src_ip->addr[idx] ^ dest_ip->addr[idx];
111 }
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700112 hash = ((u32)dev_addr) ^ hash ^ protocol ^ ntohs(src_port ^ dest_port);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700113 return ((hash >> SFE_IPV6_CONNECTION_HASH_SHIFT) ^ hash) & SFE_IPV6_CONNECTION_HASH_MASK;
114}
115
116/*
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530117 * sfe_ipv6_find_connection_match_rcu()
Xiaoping Fan978b3772015-05-27 14:15:18 -0700118 * Get the IPv6 flow match info that corresponds to a particular 5-tuple.
Xiaoping Fan978b3772015-05-27 14:15:18 -0700119 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530120struct sfe_ipv6_connection_match *
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530121sfe_ipv6_find_connection_match_rcu(struct sfe_ipv6 *si, struct net_device *dev, u8 protocol,
Xiaoping Fan978b3772015-05-27 14:15:18 -0700122 struct sfe_ipv6_addr *src_ip, __be16 src_port,
123 struct sfe_ipv6_addr *dest_ip, __be16 dest_port)
124{
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530125 struct sfe_ipv6_connection_match *cm = NULL;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700126 unsigned int conn_match_idx;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530127 struct hlist_head *lhead;
128 WARN_ON_ONCE(!rcu_read_lock_held());
Xiaoping Fan978b3772015-05-27 14:15:18 -0700129
130 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 -0700131
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530132 lhead = &si->hlist_conn_match_hash_head[conn_match_idx];
Xiaoping Fan978b3772015-05-27 14:15:18 -0700133
134 /*
135 * Hopefully the first entry is the one we want.
136 */
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530137 hlist_for_each_entry_rcu(cm, lhead, hnode) {
138 if ((cm->match_dest_port != dest_port) ||
139 (!sfe_ipv6_addr_equal(cm->match_src_ip, src_ip)) ||
140 (!sfe_ipv6_addr_equal(cm->match_dest_ip, dest_ip)) ||
141 (cm->match_protocol != protocol) ||
142 (cm->match_dev != dev)) {
143 continue;
144 }
145
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530146 this_cpu_inc(si->stats_pcpu->connection_match_hash_hits64);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700147
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530148 break;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700149
Xiaoping Fan978b3772015-05-27 14:15:18 -0700150 }
151
Xiaoping Fan978b3772015-05-27 14:15:18 -0700152 return cm;
153}
154
155/*
156 * sfe_ipv6_connection_match_update_summary_stats()
157 * Update the summary stats for a connection match entry.
158 */
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530159static inline void sfe_ipv6_connection_match_update_summary_stats(struct sfe_ipv6_connection_match *cm,
160 u32 *packets, u32 *bytes)
161
Xiaoping Fan978b3772015-05-27 14:15:18 -0700162{
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530163 u32 packet_count, byte_count;
164
165 packet_count = atomic_read(&cm->rx_packet_count);
166 cm->rx_packet_count64 += packet_count;
167 atomic_sub(packet_count, &cm->rx_packet_count);
168
169 byte_count = atomic_read(&cm->rx_byte_count);
170 cm->rx_byte_count64 += byte_count;
171 atomic_sub(byte_count, &cm->rx_byte_count);
172
173 *packets = packet_count;
174 *bytes = byte_count;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700175}
176
177/*
178 * sfe_ipv6_connection_match_compute_translations()
179 * Compute port and address translations for a connection match entry.
180 */
181static void sfe_ipv6_connection_match_compute_translations(struct sfe_ipv6_connection_match *cm)
182{
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700183 u32 diff[9];
184 u32 *idx_32;
185 u16 *idx_16;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700186
187 /*
188 * Before we insert the entry look to see if this is tagged as doing address
189 * translations. If it is then work out the adjustment that we need to apply
190 * to the transport checksum.
191 */
192 if (cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_XLATE_SRC) {
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700193 u32 adj = 0;
194 u32 carry = 0;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700195
196 /*
197 * Precompute an incremental checksum adjustment so we can
198 * edit packets in this stream very quickly. The algorithm is from RFC1624.
199 */
200 idx_32 = diff;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530201 *(idx_32++) = cm->match_src_ip[0].addr[0];
202 *(idx_32++) = cm->match_src_ip[0].addr[1];
203 *(idx_32++) = cm->match_src_ip[0].addr[2];
204 *(idx_32++) = cm->match_src_ip[0].addr[3];
Xiaoping Fan978b3772015-05-27 14:15:18 -0700205
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700206 idx_16 = (u16 *)idx_32;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700207 *(idx_16++) = cm->match_src_port;
208 *(idx_16++) = ~cm->xlate_src_port;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700209 idx_32 = (u32 *)idx_16;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700210
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530211 *(idx_32++) = ~cm->xlate_src_ip[0].addr[0];
212 *(idx_32++) = ~cm->xlate_src_ip[0].addr[1];
213 *(idx_32++) = ~cm->xlate_src_ip[0].addr[2];
214 *(idx_32++) = ~cm->xlate_src_ip[0].addr[3];
Xiaoping Fan978b3772015-05-27 14:15:18 -0700215
216 /*
217 * When we compute this fold it down to a 16-bit offset
218 * as that way we can avoid having to do a double
219 * folding of the twos-complement result because the
220 * addition of 2 16-bit values cannot cause a double
221 * wrap-around!
222 */
223 for (idx_32 = diff; idx_32 < diff + 9; idx_32++) {
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700224 u32 w = *idx_32;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700225 adj += carry;
226 adj += w;
227 carry = (w > adj);
228 }
229 adj += carry;
230 adj = (adj & 0xffff) + (adj >> 16);
231 adj = (adj & 0xffff) + (adj >> 16);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700232 cm->xlate_src_csum_adjustment = (u16)adj;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700233 }
234
235 if (cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_XLATE_DEST) {
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700236 u32 adj = 0;
237 u32 carry = 0;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700238
239 /*
240 * Precompute an incremental checksum adjustment so we can
241 * edit packets in this stream very quickly. The algorithm is from RFC1624.
242 */
243 idx_32 = diff;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530244 *(idx_32++) = cm->match_dest_ip[0].addr[0];
245 *(idx_32++) = cm->match_dest_ip[0].addr[1];
246 *(idx_32++) = cm->match_dest_ip[0].addr[2];
247 *(idx_32++) = cm->match_dest_ip[0].addr[3];
Xiaoping Fan978b3772015-05-27 14:15:18 -0700248
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700249 idx_16 = (u16 *)idx_32;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700250 *(idx_16++) = cm->match_dest_port;
251 *(idx_16++) = ~cm->xlate_dest_port;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700252 idx_32 = (u32 *)idx_16;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700253
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530254 *(idx_32++) = ~cm->xlate_dest_ip[0].addr[0];
255 *(idx_32++) = ~cm->xlate_dest_ip[0].addr[1];
256 *(idx_32++) = ~cm->xlate_dest_ip[0].addr[2];
257 *(idx_32++) = ~cm->xlate_dest_ip[0].addr[3];
Xiaoping Fan978b3772015-05-27 14:15:18 -0700258
259 /*
260 * When we compute this fold it down to a 16-bit offset
261 * as that way we can avoid having to do a double
262 * folding of the twos-complement result because the
263 * addition of 2 16-bit values cannot cause a double
264 * wrap-around!
265 */
266 for (idx_32 = diff; idx_32 < diff + 9; idx_32++) {
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700267 u32 w = *idx_32;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700268 adj += carry;
269 adj += w;
270 carry = (w > adj);
271 }
272 adj += carry;
273 adj = (adj & 0xffff) + (adj >> 16);
274 adj = (adj & 0xffff) + (adj >> 16);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700275 cm->xlate_dest_csum_adjustment = (u16)adj;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700276 }
277}
278
279/*
280 * sfe_ipv6_update_summary_stats()
281 * Update the summary stats.
282 */
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530283static void sfe_ipv6_update_summary_stats(struct sfe_ipv6 *si, struct sfe_ipv6_stats *stats)
Xiaoping Fan978b3772015-05-27 14:15:18 -0700284{
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530285 int i = 0;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700286
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530287 memset(stats, 0, sizeof(*stats));
Xiaoping Fan978b3772015-05-27 14:15:18 -0700288
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530289 for_each_possible_cpu(i) {
290 const struct sfe_ipv6_stats *s = per_cpu_ptr(si->stats_pcpu, i);
291
292 stats->connection_create_requests64 += s->connection_create_requests64;
293 stats->connection_create_collisions64 += s->connection_create_collisions64;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530294 stats->connection_create_failures64 += s->connection_create_failures64;
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530295 stats->connection_destroy_requests64 += s->connection_destroy_requests64;
296 stats->connection_destroy_misses64 += s->connection_destroy_misses64;
297 stats->connection_match_hash_hits64 += s->connection_match_hash_hits64;
298 stats->connection_match_hash_reorders64 += s->connection_match_hash_reorders64;
299 stats->connection_flushes64 += s->connection_flushes64;
Suruchi Suman23a279d2021-11-16 15:13:09 +0530300 stats->packets_dropped64 += s->packets_dropped64;
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530301 stats->packets_forwarded64 += s->packets_forwarded64;
302 stats->packets_not_forwarded64 += s->packets_not_forwarded64;
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +0530303 stats->pppoe_encap_packets_forwarded64 += s->pppoe_encap_packets_forwarded64;
304 stats->pppoe_decap_packets_forwarded64 += s->pppoe_decap_packets_forwarded64;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700305 }
306}
307
308/*
309 * sfe_ipv6_insert_connection_match()
310 * Insert a connection match into the hash.
311 *
312 * On entry we must be holding the lock that protects the hash table.
313 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700314static inline void sfe_ipv6_insert_connection_match(struct sfe_ipv6 *si,
315 struct sfe_ipv6_connection_match *cm)
Xiaoping Fan978b3772015-05-27 14:15:18 -0700316{
Xiaoping Fan978b3772015-05-27 14:15:18 -0700317 unsigned int conn_match_idx
318 = sfe_ipv6_get_connection_match_hash(cm->match_dev, cm->match_protocol,
319 cm->match_src_ip, cm->match_src_port,
320 cm->match_dest_ip, cm->match_dest_port);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700321
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530322 lockdep_assert_held(&si->lock);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700323
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530324 hlist_add_head_rcu(&cm->hnode, &si->hlist_conn_match_hash_head[conn_match_idx]);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700325#ifdef CONFIG_NF_FLOW_COOKIE
Xiaoping Fan640faf42015-08-28 15:50:55 -0700326 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 -0700327 return;
328
329 /*
330 * Configure hardware to put a flow cookie in packet of this flow,
331 * then we can accelerate the lookup process when we received this packet.
332 */
333 for (conn_match_idx = 1; conn_match_idx < SFE_FLOW_COOKIE_SIZE; conn_match_idx++) {
334 struct sfe_ipv6_flow_cookie_entry *entry = &si->sfe_flow_cookie_table[conn_match_idx];
335
336 if ((NULL == entry->match) && time_is_before_jiffies(entry->last_clean_time + HZ)) {
337 sfe_ipv6_flow_cookie_set_func_t func;
338
339 rcu_read_lock();
340 func = rcu_dereference(si->flow_cookie_set_func);
341 if (func) {
342 if (!func(cm->match_protocol, cm->match_src_ip->addr, cm->match_src_port,
343 cm->match_dest_ip->addr, cm->match_dest_port, conn_match_idx)) {
344 entry->match = cm;
345 cm->flow_cookie = conn_match_idx;
346 } else {
347 si->exception_events[SFE_IPV6_EXCEPTION_EVENT_FLOW_COOKIE_ADD_FAIL]++;
348 }
349 }
350 rcu_read_unlock();
351
352 break;
353 }
354 }
355#endif
Xiaoping Fan978b3772015-05-27 14:15:18 -0700356}
357
358/*
359 * sfe_ipv6_remove_connection_match()
360 * Remove a connection match object from the hash.
Xiaoping Fan978b3772015-05-27 14:15:18 -0700361 */
362static inline void sfe_ipv6_remove_connection_match(struct sfe_ipv6 *si, struct sfe_ipv6_connection_match *cm)
363{
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530364
365 lockdep_assert_held(&si->lock);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700366#ifdef CONFIG_NF_FLOW_COOKIE
Xiaoping Fan640faf42015-08-28 15:50:55 -0700367 if (si->flow_cookie_enable) {
368 /*
369 * Tell hardware that we no longer need a flow cookie in packet of this flow
370 */
371 unsigned int conn_match_idx;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700372
Xiaoping Fan640faf42015-08-28 15:50:55 -0700373 for (conn_match_idx = 1; conn_match_idx < SFE_FLOW_COOKIE_SIZE; conn_match_idx++) {
374 struct sfe_ipv6_flow_cookie_entry *entry = &si->sfe_flow_cookie_table[conn_match_idx];
Xiaoping Fan978b3772015-05-27 14:15:18 -0700375
Xiaoping Fan640faf42015-08-28 15:50:55 -0700376 if (cm == entry->match) {
377 sfe_ipv6_flow_cookie_set_func_t func;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700378
Xiaoping Fan640faf42015-08-28 15:50:55 -0700379 rcu_read_lock();
380 func = rcu_dereference(si->flow_cookie_set_func);
381 if (func) {
382 func(cm->match_protocol, cm->match_src_ip->addr, cm->match_src_port,
383 cm->match_dest_ip->addr, cm->match_dest_port, 0);
384 }
385 rcu_read_unlock();
386
387 cm->flow_cookie = 0;
388 entry->match = NULL;
389 entry->last_clean_time = jiffies;
390 break;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700391 }
Xiaoping Fan978b3772015-05-27 14:15:18 -0700392 }
393 }
394#endif
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530395 hlist_del_init_rcu(&cm->hnode);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700396
Xiaoping Fan978b3772015-05-27 14:15:18 -0700397}
398
399/*
400 * sfe_ipv6_get_connection_hash()
401 * Generate the hash used in connection lookups.
402 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700403static 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 -0700404 struct sfe_ipv6_addr *dest_ip, __be16 dest_port)
405{
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700406 u32 idx, hash = 0;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700407
408 for (idx = 0; idx < 4; idx++) {
409 hash ^= src_ip->addr[idx] ^ dest_ip->addr[idx];
410 }
411 hash = hash ^ protocol ^ ntohs(src_port ^ dest_port);
412 return ((hash >> SFE_IPV6_CONNECTION_HASH_SHIFT) ^ hash) & SFE_IPV6_CONNECTION_HASH_MASK;
413}
414
415/*
416 * sfe_ipv6_find_connection()
417 * Get the IPv6 connection info that corresponds to a particular 5-tuple.
418 *
419 * On entry we must be holding the lock that protects the hash table.
420 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700421static inline struct sfe_ipv6_connection *sfe_ipv6_find_connection(struct sfe_ipv6 *si, u32 protocol,
Xiaoping Fan978b3772015-05-27 14:15:18 -0700422 struct sfe_ipv6_addr *src_ip, __be16 src_port,
423 struct sfe_ipv6_addr *dest_ip, __be16 dest_port)
424{
425 struct sfe_ipv6_connection *c;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530426
Xiaoping Fan978b3772015-05-27 14:15:18 -0700427 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 +0530428
429 lockdep_assert_held(&si->lock);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700430 c = si->conn_hash[conn_idx];
431
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530432 while (c) {
433 if ((c->src_port == src_port)
434 && (c->dest_port == dest_port)
435 && (sfe_ipv6_addr_equal(c->src_ip, src_ip))
436 && (sfe_ipv6_addr_equal(c->dest_ip, dest_ip))
437 && (c->protocol == protocol)) {
438 return c;
439 }
Xiaoping Fan978b3772015-05-27 14:15:18 -0700440 c = c->next;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530441 }
Xiaoping Fan978b3772015-05-27 14:15:18 -0700442
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530443 return NULL;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700444}
445
446/*
Xiaoping Fan978b3772015-05-27 14:15:18 -0700447 * sfe_ipv6_insert_connection()
448 * Insert a connection into the hash.
449 *
450 * On entry we must be holding the lock that protects the hash table.
451 */
452static void sfe_ipv6_insert_connection(struct sfe_ipv6 *si, struct sfe_ipv6_connection *c)
453{
454 struct sfe_ipv6_connection **hash_head;
455 struct sfe_ipv6_connection *prev_head;
456 unsigned int conn_idx;
457
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530458 lockdep_assert_held(&si->lock);
459
Xiaoping Fan978b3772015-05-27 14:15:18 -0700460 /*
461 * Insert entry into the connection hash.
462 */
463 conn_idx = sfe_ipv6_get_connection_hash(c->protocol, c->src_ip, c->src_port,
464 c->dest_ip, c->dest_port);
465 hash_head = &si->conn_hash[conn_idx];
466 prev_head = *hash_head;
467 c->prev = NULL;
468 if (prev_head) {
469 prev_head->prev = c;
470 }
471
472 c->next = prev_head;
473 *hash_head = c;
474
475 /*
476 * Insert entry into the "all connections" list.
477 */
478 if (si->all_connections_tail) {
479 c->all_connections_prev = si->all_connections_tail;
480 si->all_connections_tail->all_connections_next = c;
481 } else {
482 c->all_connections_prev = NULL;
483 si->all_connections_head = c;
484 }
485
486 si->all_connections_tail = c;
487 c->all_connections_next = NULL;
488 si->num_connections++;
489
490 /*
491 * Insert the connection match objects too.
492 */
493 sfe_ipv6_insert_connection_match(si, c->original_match);
494 sfe_ipv6_insert_connection_match(si, c->reply_match);
495}
496
497/*
498 * sfe_ipv6_remove_connection()
499 * Remove a sfe_ipv6_connection object from the hash.
500 *
501 * On entry we must be holding the lock that protects the hash table.
502 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530503bool sfe_ipv6_remove_connection(struct sfe_ipv6 *si, struct sfe_ipv6_connection *c)
Xiaoping Fan978b3772015-05-27 14:15:18 -0700504{
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530505
506 lockdep_assert_held(&si->lock);
507 if (c->removed) {
508 DEBUG_ERROR("%px: Connection has been removed already\n", c);
509 return false;
510 }
511
Xiaoping Fan978b3772015-05-27 14:15:18 -0700512 /*
513 * Remove the connection match objects.
514 */
515 sfe_ipv6_remove_connection_match(si, c->reply_match);
516 sfe_ipv6_remove_connection_match(si, c->original_match);
517
518 /*
519 * Unlink the connection.
520 */
521 if (c->prev) {
522 c->prev->next = c->next;
523 } else {
524 unsigned int conn_idx = sfe_ipv6_get_connection_hash(c->protocol, c->src_ip, c->src_port,
525 c->dest_ip, c->dest_port);
526 si->conn_hash[conn_idx] = c->next;
527 }
528
529 if (c->next) {
530 c->next->prev = c->prev;
531 }
Xiaoping Fan34586472015-07-03 02:20:35 -0700532
533 /*
534 * Unlink connection from all_connections list
535 */
536 if (c->all_connections_prev) {
537 c->all_connections_prev->all_connections_next = c->all_connections_next;
538 } else {
539 si->all_connections_head = c->all_connections_next;
540 }
541
542 if (c->all_connections_next) {
543 c->all_connections_next->all_connections_prev = c->all_connections_prev;
544 } else {
545 si->all_connections_tail = c->all_connections_prev;
546 }
547
Ken Zhu32b95392021-09-03 13:52:04 -0700548 /*
549 * If I am the next sync connection, move the sync to my next or head.
550 */
551 if (unlikely(si->wc_next == c)) {
552 si->wc_next = c->all_connections_next;
553 }
554
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530555 c->removed = true;
Xiaoping Fan34586472015-07-03 02:20:35 -0700556 si->num_connections--;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530557 return true;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700558}
559
560/*
561 * sfe_ipv6_gen_sync_connection()
562 * Sync a connection.
563 *
564 * On entry to this function we expect that the lock for the connection is either
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530565 * already held (while called from sfe_ipv6_periodic_sync() or isn't required
566 * (while called from sfe_ipv6_flush_sfe_ipv6_connection())
Xiaoping Fan978b3772015-05-27 14:15:18 -0700567 */
568static void sfe_ipv6_gen_sync_connection(struct sfe_ipv6 *si, struct sfe_ipv6_connection *c,
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700569 struct sfe_connection_sync *sis, sfe_sync_reason_t reason,
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700570 u64 now_jiffies)
Xiaoping Fan978b3772015-05-27 14:15:18 -0700571{
572 struct sfe_ipv6_connection_match *original_cm;
573 struct sfe_ipv6_connection_match *reply_cm;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530574 u32 packet_count, byte_count;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700575
576 /*
577 * Fill in the update message.
578 */
Murat Sezgin53509a12016-12-27 16:57:34 -0800579 sis->is_v6 = 1;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700580 sis->protocol = c->protocol;
581 sis->src_ip.ip6[0] = c->src_ip[0];
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700582 sis->src_ip_xlate.ip6[0] = c->src_ip_xlate[0];
Xiaoping Fan978b3772015-05-27 14:15:18 -0700583 sis->dest_ip.ip6[0] = c->dest_ip[0];
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700584 sis->dest_ip_xlate.ip6[0] = c->dest_ip_xlate[0];
Xiaoping Fan978b3772015-05-27 14:15:18 -0700585 sis->src_port = c->src_port;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700586 sis->src_port_xlate = c->src_port_xlate;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700587 sis->dest_port = c->dest_port;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700588 sis->dest_port_xlate = c->dest_port_xlate;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700589
590 original_cm = c->original_match;
591 reply_cm = c->reply_match;
592 sis->src_td_max_window = original_cm->protocol_state.tcp.max_win;
593 sis->src_td_end = original_cm->protocol_state.tcp.end;
594 sis->src_td_max_end = original_cm->protocol_state.tcp.max_end;
595 sis->dest_td_max_window = reply_cm->protocol_state.tcp.max_win;
596 sis->dest_td_end = reply_cm->protocol_state.tcp.end;
597 sis->dest_td_max_end = reply_cm->protocol_state.tcp.max_end;
598
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530599 sfe_ipv6_connection_match_update_summary_stats(original_cm, &packet_count, &byte_count);
600 sis->src_new_packet_count = packet_count;
601 sis->src_new_byte_count = byte_count;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700602
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530603 sfe_ipv6_connection_match_update_summary_stats(reply_cm, &packet_count, &byte_count);
604 sis->dest_new_packet_count = packet_count;
605 sis->dest_new_byte_count = byte_count;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700606
607 sis->src_dev = original_cm->match_dev;
608 sis->src_packet_count = original_cm->rx_packet_count64;
609 sis->src_byte_count = original_cm->rx_byte_count64;
610
611 sis->dest_dev = reply_cm->match_dev;
612 sis->dest_packet_count = reply_cm->rx_packet_count64;
613 sis->dest_byte_count = reply_cm->rx_byte_count64;
614
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700615 sis->reason = reason;
616
Xiaoping Fan978b3772015-05-27 14:15:18 -0700617 /*
618 * Get the time increment since our last sync.
619 */
620 sis->delta_jiffies = now_jiffies - c->last_sync_jiffies;
621 c->last_sync_jiffies = now_jiffies;
622}
623
624/*
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530625 * sfe_ipv6_free_sfe_ipv6_connection_rcu()
626 * Called at RCU qs state to free the connection object.
627 */
628static void sfe_ipv6_free_sfe_ipv6_connection_rcu(struct rcu_head *head)
629{
630 struct sfe_ipv6_connection *c;
Suruchi Suman23a279d2021-11-16 15:13:09 +0530631 struct udp_sock *up;
632 struct sock *sk;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530633
634 /*
635 * We dont need spin lock as the connection is already removed from link list
636 */
637 c = container_of(head, struct sfe_ipv6_connection, rcu);
638 BUG_ON(!c->removed);
639
640 DEBUG_TRACE("%px: connecton has been deleted\n", c);
641
642 /*
Suruchi Suman23a279d2021-11-16 15:13:09 +0530643 * Decrease the refcount taken in function sfe_ipv6_create_rule()
644 * during call of __udp6_lib_lookup()
645 */
646 up = c->reply_match->up;
647 if (up) {
648 sk = (struct sock *)up;
649 sock_put(sk);
650 }
651
652 /*
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530653 * Release our hold of the source and dest devices and free the memory
654 * for our connection objects.
655 */
656 dev_put(c->original_dev);
657 dev_put(c->reply_dev);
658 kfree(c->original_match);
659 kfree(c->reply_match);
660 kfree(c);
661}
662
663/*
Ken Zhu88c58152021-12-09 15:12:06 -0800664 * sfe_ipv6_sync_status()
665 * update a connection status to its connection manager.
666 *
667 * si: the ipv6 context
668 * c: which connection to be notified
669 * reason: what kind of reason: flush, or destroy
670 */
671void sfe_ipv6_sync_status(struct sfe_ipv6 *si,
672 struct sfe_ipv6_connection *c,
673 sfe_sync_reason_t reason)
674{
675 struct sfe_connection_sync sis;
676 u64 now_jiffies;
677 sfe_sync_rule_callback_t sync_rule_callback;
678
679 rcu_read_lock();
680 sync_rule_callback = rcu_dereference(si->sync_rule_callback);
681
682 if (unlikely(!sync_rule_callback)) {
683 rcu_read_unlock();
684 return;
685 }
686
687 /*
688 * Generate a sync message and then sync.
689 */
690 now_jiffies = get_jiffies_64();
691 sfe_ipv6_gen_sync_connection(si, c, &sis, reason, now_jiffies);
692 sync_rule_callback(&sis);
693
694 rcu_read_unlock();
695}
696
697/*
Xiaoping Fan978b3772015-05-27 14:15:18 -0700698 * sfe_ipv6_flush_connection()
699 * Flush a connection and free all associated resources.
700 *
701 * We need to be called with bottom halves disabled locally as we need to acquire
702 * the connection hash lock and release it again. In general we're actually called
703 * from within a BH and so we're fine, but we're also called when connections are
704 * torn down.
705 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530706void sfe_ipv6_flush_connection(struct sfe_ipv6 *si,
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700707 struct sfe_ipv6_connection *c,
708 sfe_sync_reason_t reason)
Xiaoping Fan978b3772015-05-27 14:15:18 -0700709{
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530710 BUG_ON(!c->removed);
711
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530712 this_cpu_inc(si->stats_pcpu->connection_flushes64);
Ken Zhu88c58152021-12-09 15:12:06 -0800713 sfe_ipv6_sync_status(si, c, reason);
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530714
715 /*
Ken Zhu88c58152021-12-09 15:12:06 -0800716 * Release our hold of the source and dest devices and free the memory
717 * for our connection objects.
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530718 */
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530719 call_rcu(&c->rcu, sfe_ipv6_free_sfe_ipv6_connection_rcu);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700720}
721
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530722 /*
723 * sfe_ipv6_exception_stats_inc()
724 * Increment exception stats.
725 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530726void sfe_ipv6_exception_stats_inc(struct sfe_ipv6 *si, enum sfe_ipv6_exception_events reason)
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530727{
728 struct sfe_ipv6_stats *stats = this_cpu_ptr(si->stats_pcpu);
729
730 stats->exception_events64[reason]++;
731 stats->packets_not_forwarded64++;
732}
733
Xiaoping Fan978b3772015-05-27 14:15:18 -0700734/*
Xiaoping Fan978b3772015-05-27 14:15:18 -0700735 * sfe_ipv6_recv()
736 * Handle packet receives and forwaring.
737 *
738 * Returns 1 if the packet is forwarded or 0 if it isn't.
739 */
Suruchi Suman23a279d2021-11-16 15:13:09 +0530740int 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 -0700741{
742 struct sfe_ipv6 *si = &__si6;
743 unsigned int len;
744 unsigned int payload_len;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530745 unsigned int ihl = sizeof(struct ipv6hdr);
Ken Zhu88c58152021-12-09 15:12:06 -0800746 bool sync_on_find = false;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530747 struct ipv6hdr *iph;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700748 u8 next_hdr;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700749
750 /*
751 * Check that we have space for an IP header and an uplayer header here.
752 */
753 len = skb->len;
754 if (!pskb_may_pull(skb, ihl + sizeof(struct sfe_ipv6_ext_hdr))) {
Xiaoping Fan978b3772015-05-27 14:15:18 -0700755
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530756 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_HEADER_INCOMPLETE);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700757 DEBUG_TRACE("len: %u is too short\n", len);
758 return 0;
759 }
760
761 /*
762 * Is our IP version wrong?
763 */
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530764 iph = (struct ipv6hdr *)skb->data;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700765 if (unlikely(iph->version != 6)) {
Xiaoping Fan978b3772015-05-27 14:15:18 -0700766
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530767 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_NON_V6);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700768 DEBUG_TRACE("IP version: %u\n", iph->version);
769 return 0;
770 }
771
772 /*
773 * Does our datagram fit inside the skb?
774 */
775 payload_len = ntohs(iph->payload_len);
776 if (unlikely(payload_len > (len - ihl))) {
Xiaoping Fan978b3772015-05-27 14:15:18 -0700777
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530778 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_DATAGRAM_INCOMPLETE);
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530779 DEBUG_TRACE("payload_len: %u, exceeds len: %u\n", payload_len, (len - (unsigned int)sizeof(struct ipv6hdr)));
Xiaoping Fan978b3772015-05-27 14:15:18 -0700780 return 0;
781 }
782
783 next_hdr = iph->nexthdr;
784 while (unlikely(sfe_ipv6_is_ext_hdr(next_hdr))) {
785 struct sfe_ipv6_ext_hdr *ext_hdr;
786 unsigned int ext_hdr_len;
787
788 ext_hdr = (struct sfe_ipv6_ext_hdr *)(skb->data + ihl);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700789
790 ext_hdr_len = ext_hdr->hdr_len;
791 ext_hdr_len <<= 3;
792 ext_hdr_len += sizeof(struct sfe_ipv6_ext_hdr);
793 ihl += ext_hdr_len;
794 if (!pskb_may_pull(skb, ihl + sizeof(struct sfe_ipv6_ext_hdr))) {
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530795 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_HEADER_INCOMPLETE);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700796
797 DEBUG_TRACE("extension header %d not completed\n", next_hdr);
798 return 0;
799 }
Ken Zhu88c58152021-12-09 15:12:06 -0800800 /*
801 * Any packets have extend hdr, won't be handled in the fast
802 * path,sync its status and exception to the kernel.
803 */
804 sync_on_find = true;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700805 next_hdr = ext_hdr->next_hdr;
806 }
807
808 if (IPPROTO_UDP == next_hdr) {
Ken Zhu88c58152021-12-09 15:12:06 -0800809 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 -0700810 }
811
812 if (IPPROTO_TCP == next_hdr) {
Ken Zhu88c58152021-12-09 15:12:06 -0800813 return sfe_ipv6_recv_tcp(si, skb, dev, len, iph, ihl, sync_on_find, l2_info);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700814 }
815
816 if (IPPROTO_ICMPV6 == next_hdr) {
817 return sfe_ipv6_recv_icmp(si, skb, dev, len, iph, ihl);
818 }
819
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +0530820 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_UNHANDLED_PROTOCOL);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700821 DEBUG_TRACE("not UDP, TCP or ICMP: %u\n", next_hdr);
822 return 0;
823}
824
825/*
826 * sfe_ipv6_update_tcp_state()
827 * update TCP window variables.
828 */
829static void
830sfe_ipv6_update_tcp_state(struct sfe_ipv6_connection *c,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530831 struct sfe_ipv6_rule_create_msg *msg)
Xiaoping Fan978b3772015-05-27 14:15:18 -0700832{
833 struct sfe_ipv6_connection_match *orig_cm;
834 struct sfe_ipv6_connection_match *repl_cm;
835 struct sfe_ipv6_tcp_connection_match *orig_tcp;
836 struct sfe_ipv6_tcp_connection_match *repl_tcp;
837
838 orig_cm = c->original_match;
839 repl_cm = c->reply_match;
840 orig_tcp = &orig_cm->protocol_state.tcp;
841 repl_tcp = &repl_cm->protocol_state.tcp;
842
843 /* update orig */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530844 if (orig_tcp->max_win < msg->tcp_rule.flow_max_window) {
845 orig_tcp->max_win = msg->tcp_rule.flow_max_window;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700846 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530847 if ((s32)(orig_tcp->end - msg->tcp_rule.flow_end) < 0) {
848 orig_tcp->end = msg->tcp_rule.flow_end;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700849 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530850 if ((s32)(orig_tcp->max_end - msg->tcp_rule.flow_max_end) < 0) {
851 orig_tcp->max_end = msg->tcp_rule.flow_max_end;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700852 }
853
854 /* update reply */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530855 if (repl_tcp->max_win < msg->tcp_rule.return_max_window) {
856 repl_tcp->max_win = msg->tcp_rule.return_max_window;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700857 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530858 if ((s32)(repl_tcp->end - msg->tcp_rule.return_end) < 0) {
859 repl_tcp->end = msg->tcp_rule.return_end;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700860 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530861 if ((s32)(repl_tcp->max_end - msg->tcp_rule.return_max_end) < 0) {
862 repl_tcp->max_end = msg->tcp_rule.return_max_end;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700863 }
864
865 /* update match flags */
866 orig_cm->flags &= ~SFE_IPV6_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
867 repl_cm->flags &= ~SFE_IPV6_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530868 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_NO_SEQ_CHECK) {
Xiaoping Fan978b3772015-05-27 14:15:18 -0700869 orig_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
870 repl_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
871 }
872}
873
874/*
875 * sfe_ipv6_update_protocol_state()
876 * update protocol specified state machine.
877 */
878static void
879sfe_ipv6_update_protocol_state(struct sfe_ipv6_connection *c,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530880 struct sfe_ipv6_rule_create_msg *msg)
Xiaoping Fan978b3772015-05-27 14:15:18 -0700881{
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530882 switch (msg->tuple.protocol) {
Xiaoping Fan978b3772015-05-27 14:15:18 -0700883 case IPPROTO_TCP:
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530884 sfe_ipv6_update_tcp_state(c, msg);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700885 break;
886 }
887}
888
889/*
890 * sfe_ipv6_update_rule()
891 * update forwarding rule after rule is created.
892 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530893void sfe_ipv6_update_rule(struct sfe_ipv6_rule_create_msg *msg)
894
Xiaoping Fan978b3772015-05-27 14:15:18 -0700895{
896 struct sfe_ipv6_connection *c;
897 struct sfe_ipv6 *si = &__si6;
898
899 spin_lock_bh(&si->lock);
900
901 c = sfe_ipv6_find_connection(si,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530902 msg->tuple.protocol,
903 (struct sfe_ipv6_addr *)msg->tuple.flow_ip,
904 msg->tuple.flow_ident,
905 (struct sfe_ipv6_addr *)msg->tuple.return_ip,
906 msg->tuple.return_ident);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700907 if (c != NULL) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530908 sfe_ipv6_update_protocol_state(c, msg);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700909 }
910
911 spin_unlock_bh(&si->lock);
912}
913
914/*
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +0530915 * sfe_ipv6_xmit_eth_type_check
916 * Checking if MAC header has to be written.
917 */
918static inline bool sfe_ipv6_xmit_eth_type_check(struct net_device *dev, u32 cm_flags)
919{
920 if (!(dev->flags & IFF_NOARP)) {
921 return true;
922 }
923
924 /*
925 * For PPPoE, since we are now supporting PPPoE encapsulation, we are writing L2 header.
926 */
927 if (cm_flags & SFE_IPV6_CONNECTION_MATCH_FLAG_PPPOE_ENCAP) {
928 return true;
929 }
930
931 return false;
932}
933
934/*
Xiaoping Fan978b3772015-05-27 14:15:18 -0700935 * sfe_ipv6_create_rule()
936 * Create a forwarding rule.
937 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530938int sfe_ipv6_create_rule(struct sfe_ipv6_rule_create_msg *msg)
Xiaoping Fan978b3772015-05-27 14:15:18 -0700939{
940 struct sfe_ipv6 *si = &__si6;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530941 struct sfe_ipv6_connection *c, *old_c;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700942 struct sfe_ipv6_connection_match *original_cm;
943 struct sfe_ipv6_connection_match *reply_cm;
944 struct net_device *dest_dev;
945 struct net_device *src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530946 struct sfe_ipv6_5tuple *tuple = &msg->tuple;
Suruchi Suman23a279d2021-11-16 15:13:09 +0530947 struct sock *sk;
948 struct net *net;
949 unsigned int src_if_idx;
950
Suruchi Sumanc1a4a612021-10-21 14:50:23 +0530951 s32 flow_interface_num = msg->conn_rule.flow_top_interface_num;
952 s32 return_interface_num = msg->conn_rule.return_top_interface_num;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700953
Suruchi Sumanc1a4a612021-10-21 14:50:23 +0530954 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_FLOW_BOTTOM_INTERFACE) {
955 flow_interface_num = msg->conn_rule.flow_interface_num;
956 }
957
958 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_RETURN_BOTTOM_INTERFACE) {
959 return_interface_num = msg->conn_rule.return_interface_num;
960 }
961
962 src_dev = dev_get_by_index(&init_net, flow_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530963 if (!src_dev) {
964 DEBUG_WARN("%px: Unable to find src_dev corresponding to %d\n", msg,
Suruchi Sumanc1a4a612021-10-21 14:50:23 +0530965 flow_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530966 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
967 return -EINVAL;
968 }
969
Suruchi Sumanc1a4a612021-10-21 14:50:23 +0530970 dest_dev = dev_get_by_index(&init_net, return_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530971 if (!dest_dev) {
972 DEBUG_WARN("%px: Unable to find dest_dev corresponding to %d\n", msg,
Suruchi Sumanc1a4a612021-10-21 14:50:23 +0530973 return_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530974 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
975 dev_put(src_dev);
976 return -EINVAL;
977 }
Xiaoping Fan978b3772015-05-27 14:15:18 -0700978
979 if (unlikely((dest_dev->reg_state != NETREG_REGISTERED) ||
980 (src_dev->reg_state != NETREG_REGISTERED))) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530981 DEBUG_WARN("%px: src_dev=%s and dest_dev=%s are unregistered\n", msg,
982 src_dev->name, dest_dev->name);
983 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
984 dev_put(src_dev);
985 dev_put(dest_dev);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700986 return -EINVAL;
987 }
988
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530989 /*
990 * Allocate the various connection tracking objects.
991 */
992 c = (struct sfe_ipv6_connection *)kmalloc(sizeof(struct sfe_ipv6_connection), GFP_ATOMIC);
993 if (unlikely(!c)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530994 DEBUG_WARN("%px: memory allocation of connection entry failed\n", msg);
995 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
996 dev_put(src_dev);
997 dev_put(dest_dev);
Ratheesh Kannotha212fc52021-10-20 07:50:32 +0530998 return -ENOMEM;
999 }
1000
1001 original_cm = (struct sfe_ipv6_connection_match *)kmalloc(sizeof(struct sfe_ipv6_connection_match), GFP_ATOMIC);
1002 if (unlikely(!original_cm)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301003 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
1004 DEBUG_WARN("%px: memory allocation of connection match entry failed\n", msg);
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301005 kfree(c);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301006 dev_put(src_dev);
1007 dev_put(dest_dev);
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301008 return -ENOMEM;
1009 }
1010
1011 reply_cm = (struct sfe_ipv6_connection_match *)kmalloc(sizeof(struct sfe_ipv6_connection_match), GFP_ATOMIC);
1012 if (unlikely(!reply_cm)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301013 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
1014 DEBUG_WARN("%px: memory allocation of connection match entry failed\n", msg);
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301015 kfree(original_cm);
1016 kfree(c);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301017 dev_put(src_dev);
1018 dev_put(dest_dev);
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301019 return -ENOMEM;
1020 }
1021
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301022 this_cpu_inc(si->stats_pcpu->connection_create_requests64);
1023
Xiaoping Fan978b3772015-05-27 14:15:18 -07001024 spin_lock_bh(&si->lock);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001025
1026 /*
1027 * Check to see if there is already a flow that matches the rule we're
1028 * trying to create. If there is then we can't create a new one.
1029 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301030 old_c = sfe_ipv6_find_connection(si, tuple->protocol, (struct sfe_ipv6_addr *)tuple->flow_ip, tuple->flow_ident,
1031 (struct sfe_ipv6_addr *)tuple->return_ip, tuple->return_ident);
1032
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301033 if (old_c != NULL) {
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301034 this_cpu_inc(si->stats_pcpu->connection_create_collisions64);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001035
1036 /*
1037 * If we already have the flow then it's likely that this
1038 * request to create the connection rule contains more
1039 * up-to-date information. Check and update accordingly.
1040 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301041 sfe_ipv6_update_protocol_state(old_c, msg);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001042 spin_unlock_bh(&si->lock);
1043
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301044 kfree(reply_cm);
1045 kfree(original_cm);
1046 kfree(c);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301047 dev_put(src_dev);
1048 dev_put(dest_dev);
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301049
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301050 DEBUG_TRACE("connection already exists - p: %d\n"
Tian Yang45f39c82020-10-06 14:07:47 -07001051 " s: %s:%pxM:%pI6:%u, d: %s:%pxM:%pI6:%u\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301052 tuple->protocol,
1053 src_dev->name, msg->conn_rule.flow_mac, tuple->flow_ip, ntohs(tuple->flow_ident),
1054 dest_dev->name, msg->conn_rule.return_mac, tuple->return_ip, ntohs(tuple->return_ident));
Xiaoping Fan978b3772015-05-27 14:15:18 -07001055 return -EADDRINUSE;
1056 }
1057
1058 /*
Xiaoping Fan978b3772015-05-27 14:15:18 -07001059 * Fill in the "original" direction connection matching object.
1060 * Note that the transmit MAC address is "dest_mac_xlate" because
1061 * we always know both ends of a connection by their translated
1062 * addresses and not their public addresses.
1063 */
1064 original_cm->match_dev = src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301065 original_cm->match_protocol = tuple->protocol;
1066 original_cm->match_src_ip[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip;
Suruchi Suman66609a72022-01-20 02:34:25 +05301067 original_cm->match_src_port = netif_is_vxlan(src_dev) ? 0 : tuple->flow_ident;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301068 original_cm->match_dest_ip[0] = *(struct sfe_ipv6_addr *)tuple->return_ip;
1069 original_cm->match_dest_port = tuple->return_ident;
1070
1071 original_cm->xlate_src_ip[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip;
1072 original_cm->xlate_src_port = tuple->flow_ident;
1073 original_cm->xlate_dest_ip[0] = *(struct sfe_ipv6_addr *)tuple->return_ip;
1074 original_cm->xlate_dest_port = tuple->return_ident;
1075
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301076 atomic_set(&original_cm->rx_packet_count, 0);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001077 original_cm->rx_packet_count64 = 0;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301078 atomic_set(&original_cm->rx_byte_count, 0);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001079 original_cm->rx_byte_count64 = 0;
1080 original_cm->xmit_dev = dest_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301081
1082 original_cm->xmit_dev_mtu = msg->conn_rule.return_mtu;
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301083
Xiaoping Fan978b3772015-05-27 14:15:18 -07001084 original_cm->connection = c;
1085 original_cm->counter_match = reply_cm;
Suruchi Suman23a279d2021-11-16 15:13:09 +05301086
1087 /*
1088 * Valid in decap direction only
1089 */
1090 RCU_INIT_POINTER(original_cm->up, NULL);
1091
Ken Zhu37040ea2021-09-09 21:11:15 -07001092 original_cm->flags = 0;
1093 if (msg->valid_flags & SFE_RULE_CREATE_MARK_VALID) {
1094 original_cm->mark = msg->mark_rule.flow_mark;
1095 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_MARK;
1096 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301097 if (msg->valid_flags & SFE_RULE_CREATE_QOS_VALID) {
1098 original_cm->priority = msg->qos_rule.flow_qos_tag;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001099 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_PRIORITY_REMARK;
1100 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301101 if (msg->valid_flags & SFE_RULE_CREATE_DSCP_MARKING_VALID) {
1102 original_cm->dscp = msg->dscp_rule.flow_dscp << SFE_IPV6_DSCP_SHIFT;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001103 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_DSCP_REMARK;
1104 }
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301105 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1106 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_BRIDGE_FLOW;
1107 }
1108
Xiaoping Fan978b3772015-05-27 14:15:18 -07001109#ifdef CONFIG_NF_FLOW_COOKIE
1110 original_cm->flow_cookie = 0;
1111#endif
Zhi Chen8748eb32015-06-18 12:58:48 -07001112#ifdef CONFIG_XFRM
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301113 if (msg->valid_flags & SFE_RULE_CREATE_DIRECTION_VALID) {
1114 original_cm->flow_accel = msg->direction_rule.flow_accel;
1115 } else {
1116 original_cm->flow_accel = 1;
1117 }
Zhi Chen8748eb32015-06-18 12:58:48 -07001118#endif
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301119 /*
1120 * If l2_features are disabled and flow uses l2 features such as macvlan/bridge/pppoe/vlan,
1121 * bottom interfaces are expected to be disabled in the flow rule and always top interfaces
1122 * are used. In such cases, do not use HW csum offload. csum offload is used only when we
1123 * are sending directly to the destination interface that supports it.
1124 */
Suruchi Sumanf2077182022-01-13 21:35:23 +05301125 if (likely(dest_dev->features & NETIF_F_HW_CSUM) && !netif_is_vxlan(dest_dev)) {
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301126 if ((msg->conn_rule.return_top_interface_num == msg->conn_rule.return_interface_num) ||
1127 (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_RETURN_BOTTOM_INTERFACE)) {
1128 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_CSUM_OFFLOAD;
1129 }
1130 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07001131
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +05301132 reply_cm->flags = 0;
1133
1134 /*
1135 * Adding PPPoE parameters to original and reply entries based on the direction where
1136 * PPPoE header is valid in ECM rule.
1137 *
1138 * If PPPoE is valid in flow direction (from interface is PPPoE), then
1139 * original cm will have PPPoE at ingress (strip PPPoE header)
1140 * reply cm will have PPPoE at egress (add PPPoE header)
1141 *
1142 * If PPPoE is valid in return direction (to interface is PPPoE), then
1143 * original cm will have PPPoE at egress (add PPPoE header)
1144 * reply cm will have PPPoE at ingress (strip PPPoE header)
1145 */
1146 if (msg->valid_flags & SFE_RULE_CREATE_PPPOE_DECAP_VALID) {
1147 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_PPPOE_DECAP;
1148 original_cm->pppoe_session_id = msg->pppoe_rule.flow_pppoe_session_id;
1149 ether_addr_copy(original_cm->pppoe_remote_mac, msg->pppoe_rule.flow_pppoe_remote_mac);
1150
1151 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_PPPOE_ENCAP;
1152 reply_cm->pppoe_session_id = msg->pppoe_rule.flow_pppoe_session_id;
1153 ether_addr_copy(reply_cm->pppoe_remote_mac, msg->pppoe_rule.flow_pppoe_remote_mac);
1154 }
1155
1156 if (msg->valid_flags & SFE_RULE_CREATE_PPPOE_ENCAP_VALID) {
1157 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_PPPOE_ENCAP;
1158 original_cm->pppoe_session_id = msg->pppoe_rule.return_pppoe_session_id;
1159 ether_addr_copy(original_cm->pppoe_remote_mac, msg->pppoe_rule.return_pppoe_remote_mac);
1160
1161 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_PPPOE_DECAP;
1162 reply_cm->pppoe_session_id = msg->pppoe_rule.return_pppoe_session_id;
1163 ether_addr_copy(reply_cm->pppoe_remote_mac, msg->pppoe_rule.return_pppoe_remote_mac);
1164 }
1165
Xiaoping Fan978b3772015-05-27 14:15:18 -07001166 /*
Ken Zhubbf49652021-09-12 15:33:09 -07001167 * For the non-arp interface, we don't write L2 HDR.
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +05301168 * Excluding PPPoE from this, since we are now supporting PPPoE encap/decap.
Xiaoping Fan978b3772015-05-27 14:15:18 -07001169 */
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +05301170 if (sfe_ipv6_xmit_eth_type_check(dest_dev, original_cm->flags)) {
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301171
1172 /*
1173 * Check whether the rule has configured a specific source MAC address to use.
1174 * This is needed when virtual L3 interfaces such as br-lan, macvlan, vlan are used during egress
1175 */
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301176 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1177 ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)msg->conn_rule.flow_mac);
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301178 } else {
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301179 if ((msg->valid_flags & SFE_RULE_CREATE_SRC_MAC_VALID) &&
1180 (msg->src_mac_rule.mac_valid_flags & SFE_SRC_MAC_RETURN_VALID)) {
1181 ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)msg->src_mac_rule.return_src_mac);
1182 } else {
1183 ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)dest_dev->dev_addr);
1184 }
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301185 }
1186 ether_addr_copy((u8 *)original_cm->xmit_dest_mac, (u8 *)msg->conn_rule.return_mac);
1187
Xiaoping Fan978b3772015-05-27 14:15:18 -07001188 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_WRITE_L2_HDR;
1189
1190 /*
1191 * If our dev writes Ethernet headers then we can write a really fast
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301192 * version
Xiaoping Fan978b3772015-05-27 14:15:18 -07001193 */
1194 if (dest_dev->header_ops) {
1195 if (dest_dev->header_ops->create == eth_header) {
1196 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR;
1197 }
1198 }
1199 }
1200
1201 /*
1202 * Fill in the "reply" direction connection matching object.
1203 */
1204 reply_cm->match_dev = dest_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301205 reply_cm->match_protocol = tuple->protocol;
1206 reply_cm->match_src_ip[0] = *(struct sfe_ipv6_addr *)tuple->return_ip;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301207 reply_cm->match_dest_ip[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip;
1208 reply_cm->match_dest_port = tuple->flow_ident;
1209 reply_cm->xlate_src_ip[0] = *(struct sfe_ipv6_addr *)tuple->return_ip;
1210 reply_cm->xlate_src_port = tuple->return_ident;
1211 reply_cm->xlate_dest_ip[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip;
1212 reply_cm->xlate_dest_port = tuple->flow_ident;
1213
Suruchi Suman23a279d2021-11-16 15:13:09 +05301214 /*
1215 * Keep source port as 0 for VxLAN tunnels.
1216 */
1217 if (netif_is_vxlan(src_dev) || netif_is_vxlan(dest_dev)) {
1218 reply_cm->match_src_port = 0;
1219 } else {
1220 reply_cm->match_src_port = tuple->return_ident;
1221 }
1222
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301223 atomic_set(&original_cm->rx_byte_count, 0);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001224 reply_cm->rx_packet_count64 = 0;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301225 atomic_set(&reply_cm->rx_byte_count, 0);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001226 reply_cm->rx_byte_count64 = 0;
1227 reply_cm->xmit_dev = src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301228 reply_cm->xmit_dev_mtu = msg->conn_rule.flow_mtu;
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301229
Xiaoping Fan978b3772015-05-27 14:15:18 -07001230 reply_cm->connection = c;
1231 reply_cm->counter_match = original_cm;
Suruchi Suman23a279d2021-11-16 15:13:09 +05301232
Ken Zhu37040ea2021-09-09 21:11:15 -07001233 reply_cm->flags = 0;
1234 if (msg->valid_flags & SFE_RULE_CREATE_MARK_VALID) {
1235 reply_cm->mark = msg->mark_rule.return_mark;
1236 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_MARK;
1237 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301238 if (msg->valid_flags & SFE_RULE_CREATE_QOS_VALID) {
1239 reply_cm->priority = msg->qos_rule.return_qos_tag;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001240 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_PRIORITY_REMARK;
1241 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301242 if (msg->valid_flags & SFE_RULE_CREATE_DSCP_MARKING_VALID) {
1243 reply_cm->dscp = msg->dscp_rule.return_dscp << SFE_IPV6_DSCP_SHIFT;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001244 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_DSCP_REMARK;
1245 }
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301246
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301247 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1248 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_BRIDGE_FLOW;
1249 }
1250
Suruchi Suman23a279d2021-11-16 15:13:09 +05301251 /*
1252 * Setup UDP Socket if found to be valid for decap.
1253 */
1254 RCU_INIT_POINTER(reply_cm->up, NULL);
1255 net = dev_net(reply_cm->match_dev);
1256 src_if_idx = src_dev->ifindex;
1257
1258 rcu_read_lock();
1259
1260 /*
1261 * Look for the associated sock object.
1262 * __udp6_lib_lookup() holds a reference for this sock object,
1263 * which will be released in sfe_ipv6_flush_connection()
1264 */
1265#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
1266 sk = __udp6_lib_lookup(net, (const struct in6_addr *)reply_cm->match_dest_ip,
1267 reply_cm->match_dest_port, (const struct in6_addr *)reply_cm->xlate_src_ip,
1268 reply_cm->xlate_src_port, src_if_idx, &udp_table);
1269#else
1270 sk = __udp6_lib_lookup(net, (const struct in6_addr *)reply_cm->match_dest_ip,
1271 reply_cm->match_dest_port, (const struct in6_addr *)reply_cm->xlate_src_ip,
1272 reply_cm->xlate_src_port, src_if_idx, 0, &udp_table, NULL);
1273#endif
1274 rcu_read_unlock();
1275
1276 /*
1277 * We set the UDP sock pointer as valid only for decap direction.
1278 */
1279 if (sk && udp_sk(sk)->encap_type) {
1280#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
1281 if (!atomic_add_unless(&sk->sk_refcnt, 1, 0)) {
1282#else
1283 if (!refcount_inc_not_zero(&sk->sk_refcnt)) {
1284#endif
1285 kfree(reply_cm);
1286 kfree(original_cm);
1287 kfree(c);
1288
1289 DEBUG_INFO("sfe: unable to take reference for socket p:%d\n", tuple->protocol);
1290 DEBUG_INFO("SK: connection - \n"
1291 " s: %s:%pI6(%pI6):%u(%u)\n"
1292 " d: %s:%pI6(%pI6):%u(%u)\n",
1293 reply_cm->match_dev->name, &reply_cm->match_src_ip, &reply_cm->xlate_src_ip,
1294 ntohs(reply_cm->match_src_port), ntohs(reply_cm->xlate_src_port),
1295 reply_cm->xmit_dev->name, &reply_cm->match_dest_ip, &reply_cm->xlate_dest_ip,
1296 ntohs(reply_cm->match_dest_port), ntohs(reply_cm->xlate_dest_port));
1297
1298 dev_put(src_dev);
1299 dev_put(dest_dev);
1300
1301 return -ESHUTDOWN;
1302 }
1303
1304 rcu_assign_pointer(reply_cm->up, udp_sk(sk));
1305 DEBUG_INFO("Sock lookup success with reply_cm direction(%p)\n", sk);
1306 DEBUG_INFO("SK: connection - \n"
1307 " s: %s:%pI6(%pI6):%u(%u)\n"
1308 " d: %s:%pI6(%pI6):%u(%u)\n",
1309 reply_cm->match_dev->name, &reply_cm->match_src_ip, &reply_cm->xlate_src_ip,
1310 ntohs(reply_cm->match_src_port), ntohs(reply_cm->xlate_src_port),
1311 reply_cm->xmit_dev->name, &reply_cm->match_dest_ip, &reply_cm->xlate_dest_ip,
1312 ntohs(reply_cm->match_dest_port), ntohs(reply_cm->xlate_dest_port));
1313 }
1314
Xiaoping Fan978b3772015-05-27 14:15:18 -07001315#ifdef CONFIG_NF_FLOW_COOKIE
1316 reply_cm->flow_cookie = 0;
1317#endif
Zhi Chen8748eb32015-06-18 12:58:48 -07001318#ifdef CONFIG_XFRM
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301319 if (msg->valid_flags & SFE_RULE_CREATE_DIRECTION_VALID) {
1320 reply_cm->flow_accel = msg->direction_rule.return_accel;
1321 } else {
1322 reply_cm->flow_accel = 1;
1323 }
Zhi Chen8748eb32015-06-18 12:58:48 -07001324#endif
Xiaoping Fan978b3772015-05-27 14:15:18 -07001325 /*
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301326 * If l2_features are disabled and flow uses l2 features such as macvlan/bridge/pppoe/vlan,
1327 * bottom interfaces are expected to be disabled in the flow rule and always top interfaces
1328 * are used. In such cases, do not use HW csum offload. csum offload is used only when we
1329 * are sending directly to the destination interface that supports it.
1330 */
Suruchi Sumanf2077182022-01-13 21:35:23 +05301331 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 +05301332 if ((msg->conn_rule.flow_top_interface_num == msg->conn_rule.flow_interface_num) ||
1333 (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_FLOW_BOTTOM_INTERFACE)) {
1334 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_CSUM_OFFLOAD;
1335 }
1336 }
1337
1338 /*
Ken Zhubbf49652021-09-12 15:33:09 -07001339 * For the non-arp interface, we don't write L2 HDR.
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +05301340 * Excluding PPPoE from this, since we are now supporting PPPoE encap/decap.
Xiaoping Fan978b3772015-05-27 14:15:18 -07001341 */
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +05301342 if (sfe_ipv6_xmit_eth_type_check(src_dev, reply_cm->flags)) {
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301343
1344 /*
1345 * Check whether the rule has configured a specific source MAC address to use.
1346 * This is needed when virtual L3 interfaces such as br-lan, macvlan, vlan are used during egress
1347 */
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301348 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1349 ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)msg->conn_rule.return_mac);
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301350 } else {
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301351 if ((msg->valid_flags & SFE_RULE_CREATE_SRC_MAC_VALID) &&
1352 (msg->src_mac_rule.mac_valid_flags & SFE_SRC_MAC_FLOW_VALID)) {
1353 ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)msg->src_mac_rule.flow_src_mac);
1354 } else {
1355 ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)src_dev->dev_addr);
1356 }
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301357 }
1358
1359 ether_addr_copy((u8 *)reply_cm->xmit_dest_mac, (u8 *)msg->conn_rule.flow_mac);
1360
Xiaoping Fan978b3772015-05-27 14:15:18 -07001361 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_WRITE_L2_HDR;
1362
1363 /*
1364 * If our dev writes Ethernet headers then we can write a really fast
1365 * version.
1366 */
1367 if (src_dev->header_ops) {
1368 if (src_dev->header_ops->create == eth_header) {
1369 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR;
1370 }
1371 }
1372 }
1373
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301374 /*
1375 * No support for NAT in ipv6
1376 */
Xiaoping Fan978b3772015-05-27 14:15:18 -07001377
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301378 c->protocol = tuple->protocol;
1379 c->src_ip[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip;
1380 c->src_ip_xlate[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip;
1381 c->src_port = tuple->flow_ident;
1382 c->src_port_xlate = tuple->flow_ident;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001383 c->original_dev = src_dev;
1384 c->original_match = original_cm;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301385
1386 c->dest_ip[0] = *(struct sfe_ipv6_addr *)tuple->return_ip;
1387 c->dest_ip_xlate[0] = *(struct sfe_ipv6_addr *)tuple->return_ip;
1388 c->dest_port = tuple->return_ident;
1389 c->dest_port_xlate = tuple->return_ident;
1390
Xiaoping Fan978b3772015-05-27 14:15:18 -07001391 c->reply_dev = dest_dev;
1392 c->reply_match = reply_cm;
Xiaoping Fan34586472015-07-03 02:20:35 -07001393 c->debug_read_seq = 0;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001394 c->last_sync_jiffies = get_jiffies_64();
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301395 c->removed = false;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001396
1397 /*
Xiaoping Fan978b3772015-05-27 14:15:18 -07001398 * Initialize the protocol-specific information that we track.
1399 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301400 switch (tuple->protocol) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07001401 case IPPROTO_TCP:
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301402 original_cm->protocol_state.tcp.win_scale = msg->tcp_rule.flow_window_scale;
1403 original_cm->protocol_state.tcp.max_win = msg->tcp_rule.flow_max_window ? msg->tcp_rule.flow_max_window : 1;
1404 original_cm->protocol_state.tcp.end = msg->tcp_rule.flow_end;
1405 original_cm->protocol_state.tcp.max_end = msg->tcp_rule.flow_max_end;
1406 reply_cm->protocol_state.tcp.win_scale = msg->tcp_rule.return_window_scale;
1407 reply_cm->protocol_state.tcp.max_win = msg->tcp_rule.return_max_window ? msg->tcp_rule.return_max_window : 1;
1408 reply_cm->protocol_state.tcp.end = msg->tcp_rule.return_end;
1409 reply_cm->protocol_state.tcp.max_end = msg->tcp_rule.return_max_end;
1410 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_NO_SEQ_CHECK) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07001411 original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
1412 reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
1413 }
1414 break;
1415 }
1416
1417 sfe_ipv6_connection_match_compute_translations(original_cm);
1418 sfe_ipv6_connection_match_compute_translations(reply_cm);
1419 sfe_ipv6_insert_connection(si, c);
1420
1421 spin_unlock_bh(&si->lock);
1422
1423 /*
1424 * We have everything we need!
1425 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301426 DEBUG_INFO("new connection - p: %d\n"
Tian Yang45f39c82020-10-06 14:07:47 -07001427 " s: %s:%pxM(%pxM):%pI6(%pI6):%u(%u)\n"
1428 " d: %s:%pxM(%pxM):%pI6(%pI6):%u(%u)\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301429 tuple->protocol,
1430 src_dev->name, msg->conn_rule.flow_mac, NULL,
1431 (void *)tuple->flow_ip, (void *)tuple->flow_ip, ntohs(tuple->flow_ident), ntohs(tuple->flow_ident),
1432 dest_dev->name, NULL, msg->conn_rule.return_mac,
1433 (void *)tuple->return_ip, (void *)tuple->return_ip, ntohs(tuple->return_ident), ntohs(tuple->return_ident));
Xiaoping Fan978b3772015-05-27 14:15:18 -07001434
1435 return 0;
1436}
1437
1438/*
1439 * sfe_ipv6_destroy_rule()
1440 * Destroy a forwarding rule.
1441 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301442void sfe_ipv6_destroy_rule(struct sfe_ipv6_rule_destroy_msg *msg)
Xiaoping Fan978b3772015-05-27 14:15:18 -07001443{
1444 struct sfe_ipv6 *si = &__si6;
1445 struct sfe_ipv6_connection *c;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301446 bool ret;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301447 struct sfe_ipv6_5tuple *tuple = &msg->tuple;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001448
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301449 this_cpu_inc(si->stats_pcpu->connection_destroy_requests64);
1450
Xiaoping Fan978b3772015-05-27 14:15:18 -07001451 spin_lock_bh(&si->lock);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001452
1453 /*
1454 * Check to see if we have a flow that matches the rule we're trying
1455 * to destroy. If there isn't then we can't destroy it.
1456 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301457 c = sfe_ipv6_find_connection(si, tuple->protocol, (struct sfe_ipv6_addr *)tuple->flow_ip, tuple->flow_ident,
1458 (struct sfe_ipv6_addr *)tuple->return_ip, tuple->return_ident);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001459 if (!c) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07001460 spin_unlock_bh(&si->lock);
1461
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301462 this_cpu_inc(si->stats_pcpu->connection_destroy_misses64);
1463
Xiaoping Fan978b3772015-05-27 14:15:18 -07001464 DEBUG_TRACE("connection does not exist - p: %d, s: %pI6:%u, d: %pI6:%u\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301465 tuple->protocol, tuple->flow_ip, ntohs(tuple->flow_ident),
1466 tuple->return_ip, ntohs(tuple->return_ident));
Xiaoping Fan978b3772015-05-27 14:15:18 -07001467 return;
1468 }
1469
1470 /*
1471 * Remove our connection details from the hash tables.
1472 */
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301473 ret = sfe_ipv6_remove_connection(si, c);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001474 spin_unlock_bh(&si->lock);
1475
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301476 if (ret) {
1477 sfe_ipv6_flush_connection(si, c, SFE_SYNC_REASON_DESTROY);
1478 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07001479
1480 DEBUG_INFO("connection destroyed - p: %d, s: %pI6:%u, d: %pI6:%u\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301481 tuple->protocol, tuple->flow_ip, ntohs(tuple->flow_ident),
1482 tuple->return_ip, ntohs(tuple->return_ident));
Xiaoping Fan978b3772015-05-27 14:15:18 -07001483}
1484
1485/*
1486 * sfe_ipv6_register_sync_rule_callback()
1487 * Register a callback for rule synchronization.
1488 */
1489void sfe_ipv6_register_sync_rule_callback(sfe_sync_rule_callback_t sync_rule_callback)
1490{
1491 struct sfe_ipv6 *si = &__si6;
1492
1493 spin_lock_bh(&si->lock);
1494 rcu_assign_pointer(si->sync_rule_callback, sync_rule_callback);
1495 spin_unlock_bh(&si->lock);
1496}
1497
1498/*
1499 * sfe_ipv6_get_debug_dev()
1500 */
1501static ssize_t sfe_ipv6_get_debug_dev(struct device *dev,
1502 struct device_attribute *attr,
1503 char *buf)
1504{
1505 struct sfe_ipv6 *si = &__si6;
1506 ssize_t count;
1507 int num;
1508
1509 spin_lock_bh(&si->lock);
1510 num = si->debug_dev;
1511 spin_unlock_bh(&si->lock);
1512
1513 count = snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", num);
1514 return count;
1515}
1516
1517/*
1518 * sfe_ipv6_destroy_all_rules_for_dev()
1519 * Destroy all connections that match a particular device.
1520 *
1521 * If we pass dev as NULL then this destroys all connections.
1522 */
1523void sfe_ipv6_destroy_all_rules_for_dev(struct net_device *dev)
1524{
1525 struct sfe_ipv6 *si = &__si6;
1526 struct sfe_ipv6_connection *c;
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301527 bool ret;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001528
Xiaoping Fan34586472015-07-03 02:20:35 -07001529another_round:
Xiaoping Fan978b3772015-05-27 14:15:18 -07001530 spin_lock_bh(&si->lock);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001531
Xiaoping Fan34586472015-07-03 02:20:35 -07001532 for (c = si->all_connections_head; c; c = c->all_connections_next) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07001533 /*
Xiaoping Fan34586472015-07-03 02:20:35 -07001534 * Does this connection relate to the device we are destroying?
Xiaoping Fan978b3772015-05-27 14:15:18 -07001535 */
1536 if (!dev
1537 || (dev == c->original_dev)
1538 || (dev == c->reply_dev)) {
Xiaoping Fan34586472015-07-03 02:20:35 -07001539 break;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001540 }
Xiaoping Fan34586472015-07-03 02:20:35 -07001541 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07001542
Xiaoping Fan34586472015-07-03 02:20:35 -07001543 if (c) {
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301544 ret = sfe_ipv6_remove_connection(si, c);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001545 }
1546
1547 spin_unlock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07001548
1549 if (c) {
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301550 if (ret) {
1551 sfe_ipv6_flush_connection(si, c, SFE_SYNC_REASON_DESTROY);
1552 }
Xiaoping Fan34586472015-07-03 02:20:35 -07001553 goto another_round;
1554 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07001555}
1556
1557/*
1558 * sfe_ipv6_periodic_sync()
1559 */
Ken Zhu137722d2021-09-23 17:57:36 -07001560static void sfe_ipv6_periodic_sync(struct work_struct *work)
Xiaoping Fan978b3772015-05-27 14:15:18 -07001561{
Ken Zhu137722d2021-09-23 17:57:36 -07001562 struct sfe_ipv6 *si = container_of((struct delayed_work *)work, struct sfe_ipv6, sync_dwork);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07001563 u64 now_jiffies;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001564 int quota;
1565 sfe_sync_rule_callback_t sync_rule_callback;
Ken Zhu32b95392021-09-03 13:52:04 -07001566 struct sfe_ipv6_connection *c;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001567
1568 now_jiffies = get_jiffies_64();
1569
1570 rcu_read_lock();
1571 sync_rule_callback = rcu_dereference(si->sync_rule_callback);
1572 if (!sync_rule_callback) {
1573 rcu_read_unlock();
1574 goto done;
1575 }
1576
1577 spin_lock_bh(&si->lock);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001578
1579 /*
Ken Zhu32b95392021-09-03 13:52:04 -07001580 * If we have reached the end of the connection list, walk from
1581 * the connection head.
1582 */
1583 c = si->wc_next;
1584 if (unlikely(!c)) {
1585 c = si->all_connections_head;
1586 }
1587 /*
Xiaoping Fan978b3772015-05-27 14:15:18 -07001588 * Get an estimate of the number of connections to parse in this sync.
1589 */
1590 quota = (si->num_connections + 63) / 64;
1591
1592 /*
Ken Zhu32b95392021-09-03 13:52:04 -07001593 * Walk the "all connection" list and sync the connection state.
Xiaoping Fan978b3772015-05-27 14:15:18 -07001594 */
Ken Zhu32b95392021-09-03 13:52:04 -07001595 while (likely(c && quota)) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07001596 struct sfe_ipv6_connection_match *cm;
1597 struct sfe_ipv6_connection_match *counter_cm;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001598 struct sfe_connection_sync sis;
1599
Ken Zhu32b95392021-09-03 13:52:04 -07001600 cm = c->original_match;
1601 counter_cm = c->reply_match;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001602
1603 /*
Ken Zhu32b95392021-09-03 13:52:04 -07001604 * Didn't receive packets in the origial direction or reply
1605 * direction, move to the next connection.
Xiaoping Fan978b3772015-05-27 14:15:18 -07001606 */
Ken Zhu32b95392021-09-03 13:52:04 -07001607 if (!atomic_read(&cm->rx_packet_count) && !atomic_read(&counter_cm->rx_packet_count)) {
1608 c = c->all_connections_next;
1609 continue;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001610 }
1611
Ken Zhu32b95392021-09-03 13:52:04 -07001612 quota--;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001613
1614 /*
1615 * Sync the connection state.
1616 */
Xiaoping Fan99cb4c12015-08-21 19:07:32 -07001617 sfe_ipv6_gen_sync_connection(si, c, &sis, SFE_SYNC_REASON_STATS, now_jiffies);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001618
Ken Zhu32b95392021-09-03 13:52:04 -07001619 si->wc_next = c->all_connections_next;
1620
Xiaoping Fan978b3772015-05-27 14:15:18 -07001621 spin_unlock_bh(&si->lock);
1622 sync_rule_callback(&sis);
1623 spin_lock_bh(&si->lock);
Ken Zhu32b95392021-09-03 13:52:04 -07001624
1625 /*
1626 * c must be set and used in the same lock/unlock window;
1627 * because c could be removed when we don't hold the lock,
1628 * so delay grabbing until after the callback and relock.
1629 */
1630 c = si->wc_next;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001631 }
1632
Ken Zhu32b95392021-09-03 13:52:04 -07001633 /*
1634 * At the end of loop, put wc_next to the connection we left
1635 */
1636 si->wc_next = c;
1637
Xiaoping Fan978b3772015-05-27 14:15:18 -07001638 spin_unlock_bh(&si->lock);
1639 rcu_read_unlock();
1640
1641done:
Ken Zhu137722d2021-09-23 17:57:36 -07001642 schedule_delayed_work_on(si->work_cpu, (struct delayed_work *)work, ((HZ + 99) / 100));
Xiaoping Fan978b3772015-05-27 14:15:18 -07001643}
1644
1645/*
1646 * sfe_ipv6_debug_dev_read_start()
1647 * Generate part of the XML output.
1648 */
1649static bool sfe_ipv6_debug_dev_read_start(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
1650 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
1651{
1652 int bytes_read;
1653
Xiaoping Fan34586472015-07-03 02:20:35 -07001654 si->debug_read_seq++;
1655
Xiaoping Fan978b3772015-05-27 14:15:18 -07001656 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "<sfe_ipv6>\n");
1657 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1658 return false;
1659 }
1660
1661 *length -= bytes_read;
1662 *total_read += bytes_read;
1663
1664 ws->state++;
1665 return true;
1666}
1667
1668/*
1669 * sfe_ipv6_debug_dev_read_connections_start()
1670 * Generate part of the XML output.
1671 */
1672static bool sfe_ipv6_debug_dev_read_connections_start(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
1673 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
1674{
1675 int bytes_read;
1676
1677 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<connections>\n");
1678 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1679 return false;
1680 }
1681
1682 *length -= bytes_read;
1683 *total_read += bytes_read;
1684
1685 ws->state++;
1686 return true;
1687}
1688
1689/*
1690 * sfe_ipv6_debug_dev_read_connections_connection()
1691 * Generate part of the XML output.
1692 */
1693static bool sfe_ipv6_debug_dev_read_connections_connection(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
1694 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
1695{
1696 struct sfe_ipv6_connection *c;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001697 struct sfe_ipv6_connection_match *original_cm;
1698 struct sfe_ipv6_connection_match *reply_cm;
1699 int bytes_read;
1700 int protocol;
1701 struct net_device *src_dev;
1702 struct sfe_ipv6_addr src_ip;
1703 struct sfe_ipv6_addr src_ip_xlate;
1704 __be16 src_port;
1705 __be16 src_port_xlate;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07001706 u64 src_rx_packets;
1707 u64 src_rx_bytes;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001708 struct net_device *dest_dev;
1709 struct sfe_ipv6_addr dest_ip;
1710 struct sfe_ipv6_addr dest_ip_xlate;
1711 __be16 dest_port;
1712 __be16 dest_port_xlate;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07001713 u64 dest_rx_packets;
1714 u64 dest_rx_bytes;
1715 u64 last_sync_jiffies;
Ken Zhu37040ea2021-09-09 21:11:15 -07001716 u32 src_mark, dest_mark, src_priority, dest_priority, src_dscp, dest_dscp;
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301717 u32 packet, byte, original_cm_flags;
1718 u16 pppoe_session_id;
1719 u8 pppoe_remote_mac[ETH_ALEN];
Xiaoping Fan978b3772015-05-27 14:15:18 -07001720#ifdef CONFIG_NF_FLOW_COOKIE
1721 int src_flow_cookie, dst_flow_cookie;
1722#endif
1723
1724 spin_lock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07001725
1726 for (c = si->all_connections_head; c; c = c->all_connections_next) {
1727 if (c->debug_read_seq < si->debug_read_seq) {
1728 c->debug_read_seq = si->debug_read_seq;
1729 break;
1730 }
1731 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07001732
1733 /*
Xiaoping Fan34586472015-07-03 02:20:35 -07001734 * If there were no connections then move to the next state.
Xiaoping Fan978b3772015-05-27 14:15:18 -07001735 */
1736 if (!c) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07001737 spin_unlock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07001738 ws->state++;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001739 return true;
1740 }
1741
1742 original_cm = c->original_match;
1743 reply_cm = c->reply_match;
1744
1745 protocol = c->protocol;
1746 src_dev = c->original_dev;
1747 src_ip = c->src_ip[0];
1748 src_ip_xlate = c->src_ip_xlate[0];
1749 src_port = c->src_port;
1750 src_port_xlate = c->src_port_xlate;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001751 src_priority = original_cm->priority;
1752 src_dscp = original_cm->dscp >> SFE_IPV6_DSCP_SHIFT;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001753
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05301754 sfe_ipv6_connection_match_update_summary_stats(original_cm, &packet, &byte);
1755 sfe_ipv6_connection_match_update_summary_stats(reply_cm, &packet, &byte);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001756
1757 src_rx_packets = original_cm->rx_packet_count64;
1758 src_rx_bytes = original_cm->rx_byte_count64;
Ken Zhu37040ea2021-09-09 21:11:15 -07001759 src_mark = original_cm->mark;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001760 dest_dev = c->reply_dev;
1761 dest_ip = c->dest_ip[0];
1762 dest_ip_xlate = c->dest_ip_xlate[0];
1763 dest_port = c->dest_port;
1764 dest_port_xlate = c->dest_port_xlate;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001765 dest_priority = reply_cm->priority;
1766 dest_dscp = reply_cm->dscp >> SFE_IPV6_DSCP_SHIFT;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001767 dest_rx_packets = reply_cm->rx_packet_count64;
1768 dest_rx_bytes = reply_cm->rx_byte_count64;
1769 last_sync_jiffies = get_jiffies_64() - c->last_sync_jiffies;
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301770 original_cm_flags = original_cm->flags;
1771 pppoe_session_id = original_cm->pppoe_session_id;
1772 ether_addr_copy(pppoe_remote_mac, original_cm->pppoe_remote_mac);
Ken Zhu37040ea2021-09-09 21:11:15 -07001773 dest_mark = reply_cm->mark;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001774#ifdef CONFIG_NF_FLOW_COOKIE
1775 src_flow_cookie = original_cm->flow_cookie;
1776 dst_flow_cookie = reply_cm->flow_cookie;
1777#endif
1778 spin_unlock_bh(&si->lock);
1779
1780 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t\t<connection "
1781 "protocol=\"%u\" "
1782 "src_dev=\"%s\" "
1783 "src_ip=\"%pI6\" src_ip_xlate=\"%pI6\" "
1784 "src_port=\"%u\" src_port_xlate=\"%u\" "
Xiaoping Fane1963d42015-08-25 17:06:19 -07001785 "src_priority=\"%u\" src_dscp=\"%u\" "
Xiaoping Fan978b3772015-05-27 14:15:18 -07001786 "src_rx_pkts=\"%llu\" src_rx_bytes=\"%llu\" "
Ken Zhu37040ea2021-09-09 21:11:15 -07001787 "src_mark=\"%08x\" "
Xiaoping Fan978b3772015-05-27 14:15:18 -07001788 "dest_dev=\"%s\" "
1789 "dest_ip=\"%pI6\" dest_ip_xlate=\"%pI6\" "
1790 "dest_port=\"%u\" dest_port_xlate=\"%u\" "
Xiaoping Fane1963d42015-08-25 17:06:19 -07001791 "dest_priority=\"%u\" dest_dscp=\"%u\" "
Xiaoping Fan978b3772015-05-27 14:15:18 -07001792 "dest_rx_pkts=\"%llu\" dest_rx_bytes=\"%llu\" "
Ken Zhu37040ea2021-09-09 21:11:15 -07001793 "dest_mark=\"%08x\" "
Xiaoping Fan978b3772015-05-27 14:15:18 -07001794#ifdef CONFIG_NF_FLOW_COOKIE
1795 "src_flow_cookie=\"%d\" dst_flow_cookie=\"%d\" "
1796#endif
Ken Zhu37040ea2021-09-09 21:11:15 -07001797 "last_sync=\"%llu\" ",
Xiaoping Fan978b3772015-05-27 14:15:18 -07001798 protocol,
1799 src_dev->name,
1800 &src_ip, &src_ip_xlate,
1801 ntohs(src_port), ntohs(src_port_xlate),
Xiaoping Fane1963d42015-08-25 17:06:19 -07001802 src_priority, src_dscp,
Xiaoping Fan978b3772015-05-27 14:15:18 -07001803 src_rx_packets, src_rx_bytes,
Ken Zhu37040ea2021-09-09 21:11:15 -07001804 src_mark,
Xiaoping Fan978b3772015-05-27 14:15:18 -07001805 dest_dev->name,
1806 &dest_ip, &dest_ip_xlate,
1807 ntohs(dest_port), ntohs(dest_port_xlate),
Xiaoping Fane1963d42015-08-25 17:06:19 -07001808 dest_priority, dest_dscp,
Xiaoping Fan978b3772015-05-27 14:15:18 -07001809 dest_rx_packets, dest_rx_bytes,
Ken Zhu37040ea2021-09-09 21:11:15 -07001810 dest_mark,
Xiaoping Fan978b3772015-05-27 14:15:18 -07001811#ifdef CONFIG_NF_FLOW_COOKIE
1812 src_flow_cookie, dst_flow_cookie,
1813#endif
Ken Zhu37040ea2021-09-09 21:11:15 -07001814 last_sync_jiffies);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001815
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301816 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 +05301817 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 +05301818 pppoe_session_id, pppoe_remote_mac);
1819 }
1820
1821 bytes_read += snprintf(msg + bytes_read, CHAR_DEV_MSG_SIZE, ")/>\n");
1822
Xiaoping Fan978b3772015-05-27 14:15:18 -07001823 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1824 return false;
1825 }
1826
1827 *length -= bytes_read;
1828 *total_read += bytes_read;
1829
Xiaoping Fan978b3772015-05-27 14:15:18 -07001830 return true;
1831}
1832
1833/*
1834 * sfe_ipv6_debug_dev_read_connections_end()
1835 * Generate part of the XML output.
1836 */
1837static bool sfe_ipv6_debug_dev_read_connections_end(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
1838 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
1839{
1840 int bytes_read;
1841
1842 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t</connections>\n");
1843 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1844 return false;
1845 }
1846
1847 *length -= bytes_read;
1848 *total_read += bytes_read;
1849
1850 ws->state++;
1851 return true;
1852}
1853
1854/*
1855 * sfe_ipv6_debug_dev_read_exceptions_start()
1856 * Generate part of the XML output.
1857 */
1858static bool sfe_ipv6_debug_dev_read_exceptions_start(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
1859 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
1860{
1861 int bytes_read;
1862
1863 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<exceptions>\n");
1864 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1865 return false;
1866 }
1867
1868 *length -= bytes_read;
1869 *total_read += bytes_read;
1870
1871 ws->state++;
1872 return true;
1873}
1874
1875/*
1876 * sfe_ipv6_debug_dev_read_exceptions_exception()
1877 * Generate part of the XML output.
1878 */
1879static bool sfe_ipv6_debug_dev_read_exceptions_exception(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
1880 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
1881{
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301882 int i;
1883 u64 val = 0;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001884
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301885 for_each_possible_cpu(i) {
1886 const struct sfe_ipv6_stats *s = per_cpu_ptr(si->stats_pcpu, i);
1887 val += s->exception_events64[ws->iter_exception];
1888 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07001889
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301890 if (val) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07001891 int bytes_read;
1892
1893 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE,
1894 "\t\t<exception name=\"%s\" count=\"%llu\" />\n",
1895 sfe_ipv6_exception_events_string[ws->iter_exception],
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301896 val);
1897
Xiaoping Fan978b3772015-05-27 14:15:18 -07001898 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1899 return false;
1900 }
1901
1902 *length -= bytes_read;
1903 *total_read += bytes_read;
1904 }
1905
1906 ws->iter_exception++;
1907 if (ws->iter_exception >= SFE_IPV6_EXCEPTION_EVENT_LAST) {
1908 ws->iter_exception = 0;
1909 ws->state++;
1910 }
1911
1912 return true;
1913}
1914
1915/*
1916 * sfe_ipv6_debug_dev_read_exceptions_end()
1917 * Generate part of the XML output.
1918 */
1919static bool sfe_ipv6_debug_dev_read_exceptions_end(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
1920 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
1921{
1922 int bytes_read;
1923
1924 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t</exceptions>\n");
1925 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1926 return false;
1927 }
1928
1929 *length -= bytes_read;
1930 *total_read += bytes_read;
1931
1932 ws->state++;
1933 return true;
1934}
1935
1936/*
1937 * sfe_ipv6_debug_dev_read_stats()
1938 * Generate part of the XML output.
1939 */
1940static bool sfe_ipv6_debug_dev_read_stats(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
1941 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
1942{
1943 int bytes_read;
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301944 struct sfe_ipv6_stats stats;
1945 unsigned int num_conn;
1946
1947 sfe_ipv6_update_summary_stats(si, &stats);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001948
1949 spin_lock_bh(&si->lock);
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301950 num_conn = si->num_connections;
Xiaoping Fan978b3772015-05-27 14:15:18 -07001951 spin_unlock_bh(&si->lock);
1952
1953 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<stats "
1954 "num_connections=\"%u\" "
Suruchi Suman23a279d2021-11-16 15:13:09 +05301955 "pkts_dropped=\"%llu\" "
Xiaoping Fan978b3772015-05-27 14:15:18 -07001956 "pkts_forwarded=\"%llu\" pkts_not_forwarded=\"%llu\" "
1957 "create_requests=\"%llu\" create_collisions=\"%llu\" "
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301958 "create_failures=\"%llu\" "
Xiaoping Fan978b3772015-05-27 14:15:18 -07001959 "destroy_requests=\"%llu\" destroy_misses=\"%llu\" "
1960 "flushes=\"%llu\" "
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +05301961 "hash_hits=\"%llu\" hash_reorders=\"%llu\" "
1962 "pppoe_encap_pkts_fwded=\"%llu\" "
1963 "pppoe_decap_pkts_fwded=\"%llu\" />\n",
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301964
1965 num_conn,
Suruchi Suman23a279d2021-11-16 15:13:09 +05301966 stats.packets_dropped64,
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301967 stats.packets_forwarded64,
1968 stats.packets_not_forwarded64,
1969 stats.connection_create_requests64,
1970 stats.connection_create_collisions64,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301971 stats.connection_create_failures64,
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05301972 stats.connection_destroy_requests64,
1973 stats.connection_destroy_misses64,
1974 stats.connection_flushes64,
1975 stats.connection_match_hash_hits64,
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +05301976 stats.connection_match_hash_reorders64,
1977 stats.pppoe_encap_packets_forwarded64,
1978 stats.pppoe_decap_packets_forwarded64);
Xiaoping Fan978b3772015-05-27 14:15:18 -07001979 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
1980 return false;
1981 }
1982
1983 *length -= bytes_read;
1984 *total_read += bytes_read;
1985
1986 ws->state++;
1987 return true;
1988}
1989
1990/*
1991 * sfe_ipv6_debug_dev_read_end()
1992 * Generate part of the XML output.
1993 */
1994static bool sfe_ipv6_debug_dev_read_end(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
1995 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws)
1996{
1997 int bytes_read;
1998
1999 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "</sfe_ipv6>\n");
2000 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2001 return false;
2002 }
2003
2004 *length -= bytes_read;
2005 *total_read += bytes_read;
2006
2007 ws->state++;
2008 return true;
2009}
2010
2011/*
2012 * Array of write functions that write various XML elements that correspond to
2013 * our XML output state machine.
2014 */
2015static sfe_ipv6_debug_xml_write_method_t sfe_ipv6_debug_xml_write_methods[SFE_IPV6_DEBUG_XML_STATE_DONE] = {
2016 sfe_ipv6_debug_dev_read_start,
2017 sfe_ipv6_debug_dev_read_connections_start,
2018 sfe_ipv6_debug_dev_read_connections_connection,
2019 sfe_ipv6_debug_dev_read_connections_end,
2020 sfe_ipv6_debug_dev_read_exceptions_start,
2021 sfe_ipv6_debug_dev_read_exceptions_exception,
2022 sfe_ipv6_debug_dev_read_exceptions_end,
2023 sfe_ipv6_debug_dev_read_stats,
2024 sfe_ipv6_debug_dev_read_end,
2025};
2026
2027/*
2028 * sfe_ipv6_debug_dev_read()
2029 * Send info to userspace upon read request from user
2030 */
2031static ssize_t sfe_ipv6_debug_dev_read(struct file *filp, char *buffer, size_t length, loff_t *offset)
2032{
2033 char msg[CHAR_DEV_MSG_SIZE];
2034 int total_read = 0;
2035 struct sfe_ipv6_debug_xml_write_state *ws;
2036 struct sfe_ipv6 *si = &__si6;
2037
2038 ws = (struct sfe_ipv6_debug_xml_write_state *)filp->private_data;
2039 while ((ws->state != SFE_IPV6_DEBUG_XML_STATE_DONE) && (length > CHAR_DEV_MSG_SIZE)) {
2040 if ((sfe_ipv6_debug_xml_write_methods[ws->state])(si, buffer, msg, &length, &total_read, ws)) {
2041 continue;
2042 }
2043 }
Xiaoping Fan978b3772015-05-27 14:15:18 -07002044 return total_read;
2045}
2046
2047/*
Xiaoping Fan978b3772015-05-27 14:15:18 -07002048 * sfe_ipv6_debug_dev_open()
2049 */
2050static int sfe_ipv6_debug_dev_open(struct inode *inode, struct file *file)
2051{
2052 struct sfe_ipv6_debug_xml_write_state *ws;
2053
2054 ws = (struct sfe_ipv6_debug_xml_write_state *)file->private_data;
2055 if (ws) {
2056 return 0;
2057 }
2058
2059 ws = kzalloc(sizeof(struct sfe_ipv6_debug_xml_write_state), GFP_KERNEL);
2060 if (!ws) {
2061 return -ENOMEM;
2062 }
2063
2064 ws->state = SFE_IPV6_DEBUG_XML_STATE_START;
2065 file->private_data = ws;
2066
2067 return 0;
2068}
2069
2070/*
2071 * sfe_ipv6_debug_dev_release()
2072 */
2073static int sfe_ipv6_debug_dev_release(struct inode *inode, struct file *file)
2074{
2075 struct sfe_ipv6_debug_xml_write_state *ws;
Xiaoping Fan978b3772015-05-27 14:15:18 -07002076
2077 ws = (struct sfe_ipv6_debug_xml_write_state *)file->private_data;
Xiaoping Fan34586472015-07-03 02:20:35 -07002078 if (ws) {
2079 /*
2080 * We've finished with our output so free the write state.
2081 */
2082 kfree(ws);
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05302083 file->private_data = NULL;
Xiaoping Fan978b3772015-05-27 14:15:18 -07002084 }
2085
Xiaoping Fan978b3772015-05-27 14:15:18 -07002086 return 0;
2087}
2088
2089/*
2090 * File operations used in the debug char device
2091 */
2092static struct file_operations sfe_ipv6_debug_dev_fops = {
2093 .read = sfe_ipv6_debug_dev_read,
Xiaoping Fan978b3772015-05-27 14:15:18 -07002094 .open = sfe_ipv6_debug_dev_open,
2095 .release = sfe_ipv6_debug_dev_release
2096};
2097
2098#ifdef CONFIG_NF_FLOW_COOKIE
2099/*
2100 * sfe_ipv6_register_flow_cookie_cb
2101 * register a function in SFE to let SFE use this function to configure flow cookie for a flow
2102 *
2103 * Hardware driver which support flow cookie should register a callback function in SFE. Then SFE
2104 * can use this function to configure flow cookie for a flow.
2105 * return: 0, success; !=0, fail
2106 */
2107int sfe_ipv6_register_flow_cookie_cb(sfe_ipv6_flow_cookie_set_func_t cb)
2108{
2109 struct sfe_ipv6 *si = &__si6;
2110
2111 BUG_ON(!cb);
2112
2113 if (si->flow_cookie_set_func) {
2114 return -1;
2115 }
2116
2117 rcu_assign_pointer(si->flow_cookie_set_func, cb);
2118 return 0;
2119}
2120
2121/*
2122 * sfe_ipv6_unregister_flow_cookie_cb
2123 * unregister function which is used to configure flow cookie for a flow
2124 *
2125 * return: 0, success; !=0, fail
2126 */
2127int sfe_ipv6_unregister_flow_cookie_cb(sfe_ipv6_flow_cookie_set_func_t cb)
2128{
2129 struct sfe_ipv6 *si = &__si6;
2130
2131 RCU_INIT_POINTER(si->flow_cookie_set_func, NULL);
2132 return 0;
2133}
Xiaoping Fan640faf42015-08-28 15:50:55 -07002134
2135/*
2136 * sfe_ipv6_get_flow_cookie()
2137 */
2138static ssize_t sfe_ipv6_get_flow_cookie(struct device *dev,
2139 struct device_attribute *attr,
2140 char *buf)
2141{
2142 struct sfe_ipv6 *si = &__si6;
Xiaoping Fan01c67cc2015-11-09 11:31:57 -08002143 return snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", si->flow_cookie_enable);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002144}
2145
2146/*
2147 * sfe_ipv6_set_flow_cookie()
2148 */
2149static ssize_t sfe_ipv6_set_flow_cookie(struct device *dev,
2150 struct device_attribute *attr,
2151 const char *buf, size_t size)
2152{
2153 struct sfe_ipv6 *si = &__si6;
Ken Zhu137722d2021-09-23 17:57:36 -07002154 si->flow_cookie_enable = strict_strtol(buf, NULL, 0);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002155
2156 return size;
2157}
2158
2159/*
2160 * sysfs attributes.
2161 */
2162static const struct device_attribute sfe_ipv6_flow_cookie_attr =
Xiaoping Fane70da412016-02-26 16:47:57 -08002163 __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 -07002164#endif /*CONFIG_NF_FLOW_COOKIE*/
2165
Ken Zhu137722d2021-09-23 17:57:36 -07002166/*
2167 * sfe_ipv6_get_cpu()
2168 */
2169static ssize_t sfe_ipv6_get_cpu(struct device *dev,
2170 struct device_attribute *attr,
2171 char *buf)
2172{
2173 struct sfe_ipv6 *si = &__si6;
2174 return snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", si->work_cpu);
2175}
2176
2177/*
2178 * sfe_ipv4_set_cpu()
2179 */
2180static ssize_t sfe_ipv6_set_cpu(struct device *dev,
2181 struct device_attribute *attr,
2182 const char *buf, size_t size)
2183{
2184 struct sfe_ipv6 *si = &__si6;
2185 int work_cpu;
2186
2187 work_cpu = simple_strtol(buf, NULL, 0);
2188 if ((work_cpu >= 0) && (work_cpu <= NR_CPUS)) {
2189 si->work_cpu = work_cpu;
2190 } else {
2191 dev_err(dev, "%s is not in valid range[0,%d]", buf, NR_CPUS);
2192 }
2193
2194 return size;
2195}
2196/*
2197 * sysfs attributes.
2198 */
2199static const struct device_attribute sfe_ipv6_cpu_attr =
2200 __ATTR(stat_work_cpu, S_IWUSR | S_IRUGO, sfe_ipv6_get_cpu, sfe_ipv6_set_cpu);
2201
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05302202 /*
2203 * sfe_ipv6_hash_init()
2204 * Initialize conn match hash lists
2205 */
2206static void sfe_ipv6_conn_match_hash_init(struct sfe_ipv6 *si, int len)
2207{
2208 struct hlist_head *hash_list = si->hlist_conn_match_hash_head;
2209 int i;
2210
2211 for (i = 0; i < len; i++) {
2212 INIT_HLIST_HEAD(&hash_list[i]);
2213 }
2214}
2215
Suruchi Suman23a279d2021-11-16 15:13:09 +05302216#ifdef SFE_PROCESS_LOCAL_OUT
2217/*
2218 * sfe_ipv6_local_out()
2219 * Called for packets from ip_local_out() - post encapsulation & other packets
2220 */
2221static unsigned int sfe_ipv6_local_out(void *priv,
2222 struct sk_buff *skb,
2223 const struct nf_hook_state *nhs)
2224{
2225 DEBUG_TRACE("sfe: sfe_ipv6_local_out hook called.\n");
2226
2227 if (likely(skb->skb_iif)) {
2228 return sfe_ipv6_recv(skb->dev, skb, NULL, true) ? NF_STOLEN : NF_ACCEPT;
2229 }
2230
2231 return NF_ACCEPT;
2232}
2233
2234/*
2235 * struct nf_hook_ops sfe_ipv6_ops_local_out[]
2236 * Hooks into netfilter local out packet monitoring points.
2237 */
2238static struct nf_hook_ops sfe_ipv6_ops_local_out[] __read_mostly = {
2239
2240 /*
2241 * Local out routing hook is used to monitor packets.
2242 */
2243 {
2244 .hook = sfe_ipv6_local_out,
2245 .pf = PF_INET6,
2246 .hooknum = NF_INET_LOCAL_OUT,
2247 .priority = NF_IP6_PRI_FIRST,
2248 },
2249};
2250#endif
2251
Xiaoping Fan978b3772015-05-27 14:15:18 -07002252/*
2253 * sfe_ipv6_init()
2254 */
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05302255int sfe_ipv6_init(void)
Xiaoping Fan978b3772015-05-27 14:15:18 -07002256{
2257 struct sfe_ipv6 *si = &__si6;
2258 int result = -1;
2259
2260 DEBUG_INFO("SFE IPv6 init\n");
2261
Ratheesh Kannotha212fc52021-10-20 07:50:32 +05302262 sfe_ipv6_conn_match_hash_init(si, ARRAY_SIZE(si->hlist_conn_match_hash_head));
2263
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302264 si->stats_pcpu = alloc_percpu_gfp(struct sfe_ipv6_stats, GFP_KERNEL | __GFP_ZERO);
2265 if (!si->stats_pcpu) {
2266 DEBUG_ERROR("failed to allocate stats memory for sfe_ipv6\n");
2267 goto exit0;
2268 }
2269
Xiaoping Fan978b3772015-05-27 14:15:18 -07002270 /*
2271 * Create sys/sfe_ipv6
2272 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302273 si->sys_ipv6 = kobject_create_and_add("sfe_ipv6", NULL);
2274 if (!si->sys_ipv6) {
Xiaoping Fan978b3772015-05-27 14:15:18 -07002275 DEBUG_ERROR("failed to register sfe_ipv6\n");
2276 goto exit1;
2277 }
2278
2279 /*
2280 * Create files, one for each parameter supported by this module.
2281 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302282 result = sysfs_create_file(si->sys_ipv6, &sfe_ipv6_debug_dev_attr.attr);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002283 if (result) {
2284 DEBUG_ERROR("failed to register debug dev file: %d\n", result);
2285 goto exit2;
2286 }
2287
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302288 result = sysfs_create_file(si->sys_ipv6, &sfe_ipv6_cpu_attr.attr);
Ken Zhu137722d2021-09-23 17:57:36 -07002289 if (result) {
2290 DEBUG_ERROR("failed to register debug dev file: %d\n", result);
2291 goto exit3;
2292 }
2293
Xiaoping Fan640faf42015-08-28 15:50:55 -07002294#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302295 result = sysfs_create_file(si->sys_ipv6, &sfe_ipv6_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002296 if (result) {
2297 DEBUG_ERROR("failed to register flow cookie enable file: %d\n", result);
Ken Zhu137722d2021-09-23 17:57:36 -07002298 goto exit4;
Xiaoping Fan640faf42015-08-28 15:50:55 -07002299 }
2300#endif /* CONFIG_NF_FLOW_COOKIE */
2301
Suruchi Suman23a279d2021-11-16 15:13:09 +05302302#ifdef SFE_PROCESS_LOCAL_OUT
2303#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
2304 result = nf_register_hooks(sfe_ipv6_ops_local_out, ARRAY_SIZE(sfe_ipv6_ops_local_out));
2305#else
2306 result = nf_register_net_hooks(&init_net, sfe_ipv6_ops_local_out, ARRAY_SIZE(sfe_ipv6_ops_local_out));
2307#endif
2308#endif
2309 if (result < 0) {
2310 DEBUG_ERROR("can't register nf local out hook: %d\n", result);
2311 goto exit5;
2312 } else {
2313 DEBUG_ERROR("Register nf local out hook success: %d\n", result);
2314 }
2315
Xiaoping Fan978b3772015-05-27 14:15:18 -07002316 /*
2317 * Register our debug char device.
2318 */
2319 result = register_chrdev(0, "sfe_ipv6", &sfe_ipv6_debug_dev_fops);
2320 if (result < 0) {
2321 DEBUG_ERROR("Failed to register chrdev: %d\n", result);
Suruchi Suman23a279d2021-11-16 15:13:09 +05302322 goto exit6;
Xiaoping Fan978b3772015-05-27 14:15:18 -07002323 }
2324
2325 si->debug_dev = result;
Ken Zhu137722d2021-09-23 17:57:36 -07002326 si->work_cpu = WORK_CPU_UNBOUND;
Xiaoping Fan978b3772015-05-27 14:15:18 -07002327
2328 /*
Ken Zhu137722d2021-09-23 17:57:36 -07002329 * Create work to handle periodic statistics.
Xiaoping Fan978b3772015-05-27 14:15:18 -07002330 */
Ken Zhu137722d2021-09-23 17:57:36 -07002331 INIT_DELAYED_WORK(&(si->sync_dwork), sfe_ipv6_periodic_sync);
2332 schedule_delayed_work_on(si->work_cpu, &(si->sync_dwork), ((HZ + 99) / 100));
Xiaoping Fan978b3772015-05-27 14:15:18 -07002333 spin_lock_init(&si->lock);
2334
2335 return 0;
2336
Suruchi Suman23a279d2021-11-16 15:13:09 +05302337exit6:
2338#ifdef SFE_PROCESS_LOCAL_OUT
2339#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
2340 DEBUG_TRACE("sfe: Unregister local out hook\n");
2341 nf_unregister_hooks(sfe_ipv6_ops_local_out, ARRAY_SIZE(sfe_ipv6_ops_local_out));
2342#else
2343 DEBUG_TRACE("sfe: Unregister local out hook\n");
2344 nf_unregister_net_hooks(&init_net, sfe_ipv6_ops_local_out, ARRAY_SIZE(sfe_ipv6_ops_local_out));
2345#endif
2346#endif
2347
Ken Zhu137722d2021-09-23 17:57:36 -07002348exit5:
Xiaoping Fan640faf42015-08-28 15:50:55 -07002349#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302350 sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002351
Ken Zhu137722d2021-09-23 17:57:36 -07002352exit4:
Xiaoping Fan640faf42015-08-28 15:50:55 -07002353#endif /* CONFIG_NF_FLOW_COOKIE */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302354 sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_cpu_attr.attr);
Suruchi Suman23a279d2021-11-16 15:13:09 +05302355
Ken Zhu137722d2021-09-23 17:57:36 -07002356exit3:
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302357 sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_debug_dev_attr.attr);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002358
2359exit2:
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302360 kobject_put(si->sys_ipv6);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002361
2362exit1:
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302363 free_percpu(si->stats_pcpu);
2364
2365exit0:
Xiaoping Fan978b3772015-05-27 14:15:18 -07002366 return result;
2367}
2368
2369/*
2370 * sfe_ipv6_exit()
2371 */
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05302372void sfe_ipv6_exit(void)
Xiaoping Fan978b3772015-05-27 14:15:18 -07002373{
2374 struct sfe_ipv6 *si = &__si6;
2375
2376 DEBUG_INFO("SFE IPv6 exit\n");
2377
2378 /*
2379 * Destroy all connections.
2380 */
2381 sfe_ipv6_destroy_all_rules_for_dev(NULL);
2382
Ken Zhu137722d2021-09-23 17:57:36 -07002383 cancel_delayed_work(&si->sync_dwork);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002384
2385 unregister_chrdev(si->debug_dev, "sfe_ipv6");
2386
Ratheesh Kannoth1ed95462021-10-20 07:57:45 +05302387 free_percpu(si->stats_pcpu);
2388
Suruchi Suman23a279d2021-11-16 15:13:09 +05302389#ifdef SFE_PROCESS_LOCAL_OUT
2390#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
2391 DEBUG_TRACE("sfe: Unregister local out hook\n");
2392 nf_unregister_hooks(sfe_ipv6_ops_local_out, ARRAY_SIZE(sfe_ipv6_ops_local_out));
2393#else
2394 DEBUG_TRACE("sfe: Unregister local out hook\n");
2395 nf_unregister_net_hooks(&init_net, sfe_ipv6_ops_local_out, ARRAY_SIZE(sfe_ipv6_ops_local_out));
2396#endif
2397#endif
2398
Xiaoping Fan640faf42015-08-28 15:50:55 -07002399#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302400 sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002401#endif /* CONFIG_NF_FLOW_COOKIE */
Ken Zhu137722d2021-09-23 17:57:36 -07002402
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302403 sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_cpu_attr.attr);
Ken Zhu137722d2021-09-23 17:57:36 -07002404
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302405 sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_debug_dev_attr.attr);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002406
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302407 kobject_put(si->sys_ipv6);
Xiaoping Fan978b3772015-05-27 14:15:18 -07002408}
2409
Xiaoping Fan978b3772015-05-27 14:15:18 -07002410#ifdef CONFIG_NF_FLOW_COOKIE
2411EXPORT_SYMBOL(sfe_ipv6_register_flow_cookie_cb);
2412EXPORT_SYMBOL(sfe_ipv6_unregister_flow_cookie_cb);
2413#endif