blob: 7d0e88afe8df2a37a2675f54c2474681c542de4c [file] [log] [blame]
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001/*
2 * sfe_ipv4.c
3 * Shortcut forwarding engine - IPv4 edition.
4 *
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05305 * Copyright (c) 2013-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.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010019 */
Matthew McClintocka3221942014-01-16 11:44:26 -060020
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010021#include <linux/module.h>
Dave Hudsondcd08fb2013-11-22 09:25:16 -060022#include <linux/sysfs.h>
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010023#include <linux/skbuff.h>
24#include <linux/icmp.h>
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010025#include <net/tcp.h>
Amitesh Anand63be37d2021-12-24 20:51:48 +053026#include <net/udp.h>
27#include <net/vxlan.h>
Dave Hudsondcd08fb2013-11-22 09:25:16 -060028#include <linux/etherdevice.h>
Tian Yang45f39c82020-10-06 14:07:47 -070029#include <linux/version.h>
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +053030#include <linux/lockdep.h>
Amitesh Anand63be37d2021-12-24 20:51:48 +053031#include <linux/refcount.h>
32#include <linux/netfilter.h>
33#include <linux/inetdevice.h>
34#include <linux/netfilter_ipv4.h>
Parikshit Guned31a8202022-01-05 22:15:04 +053035#include <linux/seqlock.h>
Nitin Shettye6ed5b52021-12-27 14:50:11 +053036#include <net/protocol.h>
37#include <net/gre.h>
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010038
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +053039#include "sfe_debug.h"
Ratheesh Kannoth89302a72021-10-20 08:10:37 +053040#include "sfe_api.h"
Dave Hudsondcd08fb2013-11-22 09:25:16 -060041#include "sfe.h"
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +053042#include "sfe_flow_cookie.h"
43#include "sfe_ipv4.h"
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +053044#include "sfe_ipv4_udp.h"
45#include "sfe_ipv4_tcp.h"
46#include "sfe_ipv4_icmp.h"
Wayne Tanbb7f1782021-12-13 11:16:04 -080047#include "sfe_pppoe.h"
Nitin Shettye6ed5b52021-12-27 14:50:11 +053048#include "sfe_ipv4_gre.h"
Tian Yangd98d91b2022-03-09 14:50:12 -080049#include "sfe_ipv4_tun6rd.h"
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010050
51static char *sfe_ipv4_exception_events_string[SFE_IPV4_EXCEPTION_EVENT_LAST] = {
52 "UDP_HEADER_INCOMPLETE",
53 "UDP_NO_CONNECTION",
54 "UDP_IP_OPTIONS_OR_INITIAL_FRAGMENT",
55 "UDP_SMALL_TTL",
56 "UDP_NEEDS_FRAGMENTATION",
57 "TCP_HEADER_INCOMPLETE",
58 "TCP_NO_CONNECTION_SLOW_FLAGS",
59 "TCP_NO_CONNECTION_FAST_FLAGS",
60 "TCP_IP_OPTIONS_OR_INITIAL_FRAGMENT",
61 "TCP_SMALL_TTL",
62 "TCP_NEEDS_FRAGMENTATION",
63 "TCP_FLAGS",
64 "TCP_SEQ_EXCEEDS_RIGHT_EDGE",
65 "TCP_SMALL_DATA_OFFS",
66 "TCP_BAD_SACK",
67 "TCP_BIG_DATA_OFFS",
68 "TCP_SEQ_BEFORE_LEFT_EDGE",
69 "TCP_ACK_EXCEEDS_RIGHT_EDGE",
70 "TCP_ACK_BEFORE_LEFT_EDGE",
71 "ICMP_HEADER_INCOMPLETE",
72 "ICMP_UNHANDLED_TYPE",
73 "ICMP_IPV4_HEADER_INCOMPLETE",
74 "ICMP_IPV4_NON_V4",
75 "ICMP_IPV4_IP_OPTIONS_INCOMPLETE",
76 "ICMP_IPV4_UDP_HEADER_INCOMPLETE",
77 "ICMP_IPV4_TCP_HEADER_INCOMPLETE",
78 "ICMP_IPV4_UNHANDLED_PROTOCOL",
79 "ICMP_NO_CONNECTION",
80 "ICMP_FLUSHED_CONNECTION",
81 "HEADER_INCOMPLETE",
Ratheesh Kannoth43d64f82021-10-20 08:23:29 +053082 "HEADER_CSUM_BAD",
Dave Hudsonaaf97ca2013-06-13 17:52:29 +010083 "BAD_TOTAL_LENGTH",
84 "NON_V4",
85 "NON_INITIAL_FRAGMENT",
86 "DATAGRAM_INCOMPLETE",
87 "IP_OPTIONS_INCOMPLETE",
Ratheesh Kannoth5dee3772022-01-18 11:27:14 +053088 "UNHANDLED_PROTOCOL",
Nitin Shetty16ab38d2022-02-09 01:26:19 +053089 "NO_HEADROOM",
90 "INVALID_PPPOE_SESSION",
91 "INCORRECT_PPPOE_PARSING",
92 "PPPOE_NOT_SET_IN_CME",
93 "INGRESS_VLAN_TAG_MISMATCH",
Ratheesh Kannoth5dee3772022-01-18 11:27:14 +053094 "INVALID_SOURCE_INTERFACE",
Tian Yangd98d91b2022-03-09 14:50:12 -080095 "TUN6RD_NO_CONNECTION",
96 "TUN6RD_NEEDS_FRAGMENTATION",
97 "TUN6RD_SYNC_ON_FIND",
Nitin Shettye6ed5b52021-12-27 14:50:11 +053098 "GRE_HEADER_INCOMPLETE",
99 "GRE_NO_CONNECTION",
100 "GRE_IP_OPTIONS_OR_INITIAL_FRAGMENT",
101 "GRE_SMALL_TTL",
102 "GRE_NEEDS_FRAGMENTATION"
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100103};
104
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700105static struct sfe_ipv4 __si;
Ken Zhu7a43d882022-01-04 10:51:44 -0800106struct sfe_ipv4_msg *sfe_ipv4_sync_many_msg;
107uint32_t sfe_ipv4_sync_max_number;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100108
109/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100110 * sfe_ipv4_gen_ip_csum()
111 * Generate the IP checksum for an IPv4 header.
112 *
113 * Note that this function assumes that we have only 20 bytes of IP header.
114 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530115u16 sfe_ipv4_gen_ip_csum(struct iphdr *iph)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100116{
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700117 u32 sum;
118 u16 *i = (u16 *)iph;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100119
120 iph->check = 0;
121
122 /*
123 * Generate the sum.
124 */
125 sum = i[0] + i[1] + i[2] + i[3] + i[4] + i[5] + i[6] + i[7] + i[8] + i[9];
126
127 /*
128 * Fold it to ones-complement form.
129 */
130 sum = (sum & 0xffff) + (sum >> 16);
131 sum = (sum & 0xffff) + (sum >> 16);
132
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700133 return (u16)sum ^ 0xffff;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100134}
135
136/*
137 * sfe_ipv4_get_connection_match_hash()
138 * Generate the hash used in connection match lookups.
139 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700140static inline unsigned int sfe_ipv4_get_connection_match_hash(struct net_device *dev, u8 protocol,
Dave Hudson87973cd2013-10-22 16:00:04 +0100141 __be32 src_ip, __be16 src_port,
142 __be32 dest_ip, __be16 dest_port)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100143{
Ratheesh Kannoth5dee3772022-01-18 11:27:14 +0530144 u32 hash = ntohl(src_ip ^ dest_ip) ^ protocol ^ ntohs(src_port ^ dest_port);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100145 return ((hash >> SFE_IPV4_CONNECTION_HASH_SHIFT) ^ hash) & SFE_IPV4_CONNECTION_HASH_MASK;
146}
147
148/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530149 * sfe_ipv4_find_connection_match_rcu()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100150 * Get the IPv4 flow match info that corresponds to a particular 5-tuple.
151 *
152 * On entry we must be holding the lock that protects the hash table.
153 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530154struct sfe_ipv4_connection_match *
155sfe_ipv4_find_connection_match_rcu(struct sfe_ipv4 *si, struct net_device *dev, u8 protocol,
Dave Hudson87973cd2013-10-22 16:00:04 +0100156 __be32 src_ip, __be16 src_port,
157 __be32 dest_ip, __be16 dest_port)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100158{
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530159 struct sfe_ipv4_connection_match *cm = NULL;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100160 unsigned int conn_match_idx;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530161 struct hlist_head *lhead;
162
163 WARN_ON_ONCE(!rcu_read_lock_held());
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100164
165 conn_match_idx = sfe_ipv4_get_connection_match_hash(dev, protocol, src_ip, src_port, dest_ip, dest_port);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100166
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530167 lhead = &si->hlist_conn_match_hash_head[conn_match_idx];
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100168
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530169 hlist_for_each_entry_rcu(cm, lhead, hnode) {
170 if (cm->match_src_port != src_port
171 || cm->match_dest_port != dest_port
172 || cm->match_src_ip != src_ip
173 || cm->match_dest_ip != dest_ip
Ratheesh Kannoth5dee3772022-01-18 11:27:14 +0530174 || cm->match_protocol != protocol) {
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530175 continue;
176 }
177
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530178 this_cpu_inc(si->stats_pcpu->connection_match_hash_hits64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100179
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530180 break;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100181 }
182
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100183 return cm;
184}
185
186/*
187 * sfe_ipv4_connection_match_update_summary_stats()
188 * Update the summary stats for a connection match entry.
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530189 *
190 * Stats are incremented atomically. So use atomic substraction to update summary
191 * stats.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100192 */
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530193static inline void sfe_ipv4_connection_match_update_summary_stats(struct sfe_ipv4_connection_match *cm,
194 u32 *packets, u32 *bytes)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100195{
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530196 u32 packet_count, byte_count;
197
198 packet_count = atomic_read(&cm->rx_packet_count);
199 cm->rx_packet_count64 += packet_count;
200 atomic_sub(packet_count, &cm->rx_packet_count);
201
202 byte_count = atomic_read(&cm->rx_byte_count);
203 cm->rx_byte_count64 += byte_count;
204 atomic_sub(byte_count, &cm->rx_byte_count);
205
206 *packets = packet_count;
207 *bytes = byte_count;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100208}
209
210/*
211 * sfe_ipv4_connection_match_compute_translations()
212 * Compute port and address translations for a connection match entry.
213 */
214static void sfe_ipv4_connection_match_compute_translations(struct sfe_ipv4_connection_match *cm)
215{
216 /*
217 * Before we insert the entry look to see if this is tagged as doing address
218 * translations. If it is then work out the adjustment that we need to apply
219 * to the transport checksum.
220 */
221 if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC) {
222 /*
223 * Precompute an incremental checksum adjustment so we can
224 * edit packets in this stream very quickly. The algorithm is from RFC1624.
225 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700226 u16 src_ip_hi = cm->match_src_ip >> 16;
227 u16 src_ip_lo = cm->match_src_ip & 0xffff;
228 u32 xlate_src_ip = ~cm->xlate_src_ip;
229 u16 xlate_src_ip_hi = xlate_src_ip >> 16;
230 u16 xlate_src_ip_lo = xlate_src_ip & 0xffff;
231 u16 xlate_src_port = ~cm->xlate_src_port;
232 u32 adj;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100233
234 /*
235 * When we compute this fold it down to a 16-bit offset
236 * as that way we can avoid having to do a double
237 * folding of the twos-complement result because the
238 * addition of 2 16-bit values cannot cause a double
239 * wrap-around!
240 */
241 adj = src_ip_hi + src_ip_lo + cm->match_src_port
242 + xlate_src_ip_hi + xlate_src_ip_lo + xlate_src_port;
243 adj = (adj & 0xffff) + (adj >> 16);
244 adj = (adj & 0xffff) + (adj >> 16);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700245 cm->xlate_src_csum_adjustment = (u16)adj;
Nicolas Costaac2979c2014-01-14 10:35:24 -0600246
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100247 }
248
249 if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST) {
250 /*
251 * Precompute an incremental checksum adjustment so we can
252 * edit packets in this stream very quickly. The algorithm is from RFC1624.
253 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700254 u16 dest_ip_hi = cm->match_dest_ip >> 16;
255 u16 dest_ip_lo = cm->match_dest_ip & 0xffff;
256 u32 xlate_dest_ip = ~cm->xlate_dest_ip;
257 u16 xlate_dest_ip_hi = xlate_dest_ip >> 16;
258 u16 xlate_dest_ip_lo = xlate_dest_ip & 0xffff;
259 u16 xlate_dest_port = ~cm->xlate_dest_port;
260 u32 adj;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100261
262 /*
263 * When we compute this fold it down to a 16-bit offset
264 * as that way we can avoid having to do a double
265 * folding of the twos-complement result because the
266 * addition of 2 16-bit values cannot cause a double
267 * wrap-around!
268 */
269 adj = dest_ip_hi + dest_ip_lo + cm->match_dest_port
270 + xlate_dest_ip_hi + xlate_dest_ip_lo + xlate_dest_port;
271 adj = (adj & 0xffff) + (adj >> 16);
272 adj = (adj & 0xffff) + (adj >> 16);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700273 cm->xlate_dest_csum_adjustment = (u16)adj;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100274 }
Xiaoping Fanad755af2015-04-01 16:58:46 -0700275
276 if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC) {
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700277 u32 adj = ~cm->match_src_ip + cm->xlate_src_ip;
Xiaoping Fanad755af2015-04-01 16:58:46 -0700278 if (adj < cm->xlate_src_ip) {
279 adj++;
280 }
281
282 adj = (adj & 0xffff) + (adj >> 16);
283 adj = (adj & 0xffff) + (adj >> 16);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700284 cm->xlate_src_partial_csum_adjustment = (u16)adj;
Xiaoping Fanad755af2015-04-01 16:58:46 -0700285 }
286
287 if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST) {
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700288 u32 adj = ~cm->match_dest_ip + cm->xlate_dest_ip;
Xiaoping Fanad755af2015-04-01 16:58:46 -0700289 if (adj < cm->xlate_dest_ip) {
290 adj++;
291 }
292
293 adj = (adj & 0xffff) + (adj >> 16);
294 adj = (adj & 0xffff) + (adj >> 16);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700295 cm->xlate_dest_partial_csum_adjustment = (u16)adj;
Xiaoping Fanad755af2015-04-01 16:58:46 -0700296 }
297
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100298}
299
300/*
301 * sfe_ipv4_update_summary_stats()
302 * Update the summary stats.
303 */
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530304static void sfe_ipv4_update_summary_stats(struct sfe_ipv4 *si, struct sfe_ipv4_stats *stats)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100305{
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530306 int i = 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100307
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530308 memset(stats, 0, sizeof(*stats));
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100309
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530310 for_each_possible_cpu(i) {
311 const struct sfe_ipv4_stats *s = per_cpu_ptr(si->stats_pcpu, i);
312
313 stats->connection_create_requests64 += s->connection_create_requests64;
314 stats->connection_create_collisions64 += s->connection_create_collisions64;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530315 stats->connection_create_failures64 += s->connection_create_failures64;
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530316 stats->connection_destroy_requests64 += s->connection_destroy_requests64;
317 stats->connection_destroy_misses64 += s->connection_destroy_misses64;
318 stats->connection_match_hash_hits64 += s->connection_match_hash_hits64;
319 stats->connection_match_hash_reorders64 += s->connection_match_hash_reorders64;
320 stats->connection_flushes64 += s->connection_flushes64;
Amitesh Anand63be37d2021-12-24 20:51:48 +0530321 stats->packets_dropped64 += s->packets_dropped64;
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530322 stats->packets_forwarded64 += s->packets_forwarded64;
Ken Zhu7e38d1a2021-11-30 17:31:46 -0800323 stats->packets_fast_xmited64 += s->packets_fast_xmited64;
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530324 stats->packets_not_forwarded64 += s->packets_not_forwarded64;
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +0530325 stats->pppoe_encap_packets_forwarded64 += s->pppoe_encap_packets_forwarded64;
326 stats->pppoe_decap_packets_forwarded64 += s->pppoe_decap_packets_forwarded64;
Guduri Prathyusha034d6352022-01-12 16:49:04 +0530327 stats->pppoe_bridge_packets_forwarded64 += s->pppoe_bridge_packets_forwarded64;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100328 }
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530329
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100330}
331
332/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530333 * sfe_ipv4_insert_connection_match()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100334 * Insert a connection match into the hash.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100335 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530336static inline void sfe_ipv4_insert_connection_match(struct sfe_ipv4 *si,
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700337 struct sfe_ipv4_connection_match *cm)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100338{
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100339 unsigned int conn_match_idx
340 = sfe_ipv4_get_connection_match_hash(cm->match_dev, cm->match_protocol,
341 cm->match_src_ip, cm->match_src_port,
342 cm->match_dest_ip, cm->match_dest_port);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700343
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530344 lockdep_assert_held(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100345
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530346 hlist_add_head_rcu(&cm->hnode, &si->hlist_conn_match_hash_head[conn_match_idx]);
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800347#ifdef CONFIG_NF_FLOW_COOKIE
Xiaoping Fan640faf42015-08-28 15:50:55 -0700348 if (!si->flow_cookie_enable)
349 return;
350
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800351 /*
352 * Configure hardware to put a flow cookie in packet of this flow,
353 * then we can accelerate the lookup process when we received this packet.
354 */
355 for (conn_match_idx = 1; conn_match_idx < SFE_FLOW_COOKIE_SIZE; conn_match_idx++) {
356 struct sfe_flow_cookie_entry *entry = &si->sfe_flow_cookie_table[conn_match_idx];
357
358 if ((NULL == entry->match) && time_is_before_jiffies(entry->last_clean_time + HZ)) {
359 flow_cookie_set_func_t func;
360
361 rcu_read_lock();
362 func = rcu_dereference(si->flow_cookie_set_func);
363 if (func) {
Xiaoping Fan59176422015-05-22 15:58:10 -0700364 if (!func(cm->match_protocol, cm->match_src_ip, cm->match_src_port,
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800365 cm->match_dest_ip, cm->match_dest_port, conn_match_idx)) {
366 entry->match = cm;
367 cm->flow_cookie = conn_match_idx;
368 }
369 }
370 rcu_read_unlock();
371
372 break;
373 }
374 }
375#endif
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100376}
377
378/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530379 * sfe_ipv4_remove_connection_match()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100380 * Remove a connection match object from the hash.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100381 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530382static inline void sfe_ipv4_remove_connection_match(struct sfe_ipv4 *si, struct sfe_ipv4_connection_match *cm)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100383{
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530384
385 lockdep_assert_held(&si->lock);
386
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800387#ifdef CONFIG_NF_FLOW_COOKIE
Xiaoping Fan640faf42015-08-28 15:50:55 -0700388 if (si->flow_cookie_enable) {
389 /*
390 * Tell hardware that we no longer need a flow cookie in packet of this flow
391 */
392 unsigned int conn_match_idx;
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800393
Xiaoping Fan640faf42015-08-28 15:50:55 -0700394 for (conn_match_idx = 1; conn_match_idx < SFE_FLOW_COOKIE_SIZE; conn_match_idx++) {
395 struct sfe_flow_cookie_entry *entry = &si->sfe_flow_cookie_table[conn_match_idx];
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800396
Xiaoping Fan640faf42015-08-28 15:50:55 -0700397 if (cm == entry->match) {
398 flow_cookie_set_func_t func;
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800399
Xiaoping Fan640faf42015-08-28 15:50:55 -0700400 rcu_read_lock();
401 func = rcu_dereference(si->flow_cookie_set_func);
402 if (func) {
403 func(cm->match_protocol, cm->match_src_ip, cm->match_src_port,
404 cm->match_dest_ip, cm->match_dest_port, 0);
405 }
406 rcu_read_unlock();
407
408 cm->flow_cookie = 0;
409 entry->match = NULL;
410 entry->last_clean_time = jiffies;
411 break;
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800412 }
Xiaoping Fand1dc7b22015-01-23 00:43:56 -0800413 }
414 }
415#endif
416
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530417 hlist_del_init_rcu(&cm->hnode);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100418
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100419}
420
421/*
422 * sfe_ipv4_get_connection_hash()
423 * Generate the hash used in connection lookups.
424 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700425static inline unsigned int sfe_ipv4_get_connection_hash(u8 protocol, __be32 src_ip, __be16 src_port,
Dave Hudson87973cd2013-10-22 16:00:04 +0100426 __be32 dest_ip, __be16 dest_port)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100427{
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700428 u32 hash = ntohl(src_ip ^ dest_ip) ^ protocol ^ ntohs(src_port ^ dest_port);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100429 return ((hash >> SFE_IPV4_CONNECTION_HASH_SHIFT) ^ hash) & SFE_IPV4_CONNECTION_HASH_MASK;
430}
431
432/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530433 * sfe_ipv4_find_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100434 * Get the IPv4 connection info that corresponds to a particular 5-tuple.
435 *
436 * On entry we must be holding the lock that protects the hash table.
437 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530438static inline struct sfe_ipv4_connection *sfe_ipv4_find_connection(struct sfe_ipv4 *si, u32 protocol,
Dave Hudson87973cd2013-10-22 16:00:04 +0100439 __be32 src_ip, __be16 src_port,
440 __be32 dest_ip, __be16 dest_port)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100441{
442 struct sfe_ipv4_connection *c;
443 unsigned int conn_idx = sfe_ipv4_get_connection_hash(protocol, src_ip, src_port, dest_ip, dest_port);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530444
445 lockdep_assert_held(&si->lock);
446
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100447 c = si->conn_hash[conn_idx];
448
449 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100450 * Will need connection entry for next create/destroy metadata,
451 * So no need to re-order entry for these requests
452 */
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530453 while (c) {
454 if ((c->src_port == src_port)
455 && (c->dest_port == dest_port)
456 && (c->src_ip == src_ip)
457 && (c->dest_ip == dest_ip)
458 && (c->protocol == protocol)) {
459 return c;
460 }
461
462 c = c->next;
463 }
464
465 return NULL;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100466}
467
468/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530469 * sfe_ipv4_insert_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100470 * Insert a connection into the hash.
471 *
472 * On entry we must be holding the lock that protects the hash table.
473 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530474static void sfe_ipv4_insert_connection(struct sfe_ipv4 *si, struct sfe_ipv4_connection *c)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100475{
476 struct sfe_ipv4_connection **hash_head;
477 struct sfe_ipv4_connection *prev_head;
478 unsigned int conn_idx;
479
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530480 lockdep_assert_held(&si->lock);
481
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100482 /*
483 * Insert entry into the connection hash.
484 */
485 conn_idx = sfe_ipv4_get_connection_hash(c->protocol, c->src_ip, c->src_port,
486 c->dest_ip, c->dest_port);
487 hash_head = &si->conn_hash[conn_idx];
488 prev_head = *hash_head;
489 c->prev = NULL;
490 if (prev_head) {
491 prev_head->prev = c;
492 }
493
494 c->next = prev_head;
495 *hash_head = c;
496
497 /*
498 * Insert entry into the "all connections" list.
499 */
500 if (si->all_connections_tail) {
501 c->all_connections_prev = si->all_connections_tail;
502 si->all_connections_tail->all_connections_next = c;
503 } else {
504 c->all_connections_prev = NULL;
505 si->all_connections_head = c;
506 }
507
508 si->all_connections_tail = c;
509 c->all_connections_next = NULL;
510 si->num_connections++;
511
512 /*
513 * Insert the connection match objects too.
514 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530515 sfe_ipv4_insert_connection_match(si, c->original_match);
516 sfe_ipv4_insert_connection_match(si, c->reply_match);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100517}
518
519/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530520 * sfe_ipv4_remove_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100521 * Remove a sfe_ipv4_connection object from the hash.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100522 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530523bool sfe_ipv4_remove_connection(struct sfe_ipv4 *si, struct sfe_ipv4_connection *c)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100524{
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530525 lockdep_assert_held(&si->lock);
526
527 if (c->removed) {
528 DEBUG_ERROR("%px: Connection has been removed already\n", c);
529 return false;
530 }
531
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100532 /*
533 * Remove the connection match objects.
534 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530535 sfe_ipv4_remove_connection_match(si, c->reply_match);
536 sfe_ipv4_remove_connection_match(si, c->original_match);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100537
538 /*
539 * Unlink the connection.
540 */
541 if (c->prev) {
542 c->prev->next = c->next;
543 } else {
544 unsigned int conn_idx = sfe_ipv4_get_connection_hash(c->protocol, c->src_ip, c->src_port,
545 c->dest_ip, c->dest_port);
546 si->conn_hash[conn_idx] = c->next;
547 }
548
549 if (c->next) {
550 c->next->prev = c->prev;
551 }
Xiaoping Fan34586472015-07-03 02:20:35 -0700552
553 /*
554 * Unlink connection from all_connections list
555 */
556 if (c->all_connections_prev) {
557 c->all_connections_prev->all_connections_next = c->all_connections_next;
558 } else {
559 si->all_connections_head = c->all_connections_next;
560 }
561
562 if (c->all_connections_next) {
563 c->all_connections_next->all_connections_prev = c->all_connections_prev;
564 } else {
565 si->all_connections_tail = c->all_connections_prev;
566 }
567
Ken Zhudc423672021-09-02 18:27:01 -0700568 /*
569 * If I am the next sync connection, move the sync to my next or head.
570 */
571 if (unlikely(si->wc_next == c)) {
572 si->wc_next = c->all_connections_next;
573 }
574
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530575 c->removed = true;
Xiaoping Fan34586472015-07-03 02:20:35 -0700576 si->num_connections--;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530577 return true;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100578}
579
580/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530581 * sfe_ipv4_gen_sync_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100582 * Sync a connection.
583 *
584 * On entry to this function we expect that the lock for the connection is either
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530585 * already held (while called from sfe_ipv4_periodic_sync() or isn't required
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530586 * (while called from sfe_ipv4_flush_connection())
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100587 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530588static void sfe_ipv4_gen_sync_connection(struct sfe_ipv4 *si, struct sfe_ipv4_connection *c,
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700589 struct sfe_connection_sync *sis, sfe_sync_reason_t reason,
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700590 u64 now_jiffies)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100591{
592 struct sfe_ipv4_connection_match *original_cm;
593 struct sfe_ipv4_connection_match *reply_cm;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530594 u32 packet_count, byte_count;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100595
596 /*
597 * Fill in the update message.
598 */
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700599 sis->is_v6 = 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100600 sis->protocol = c->protocol;
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700601 sis->src_ip.ip = c->src_ip;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700602 sis->src_ip_xlate.ip = c->src_ip_xlate;
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700603 sis->dest_ip.ip = c->dest_ip;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700604 sis->dest_ip_xlate.ip = c->dest_ip_xlate;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100605 sis->src_port = c->src_port;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700606 sis->src_port_xlate = c->src_port_xlate;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100607 sis->dest_port = c->dest_port;
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700608 sis->dest_port_xlate = c->dest_port_xlate;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100609
610 original_cm = c->original_match;
611 reply_cm = c->reply_match;
612 sis->src_td_max_window = original_cm->protocol_state.tcp.max_win;
613 sis->src_td_end = original_cm->protocol_state.tcp.end;
614 sis->src_td_max_end = original_cm->protocol_state.tcp.max_end;
615 sis->dest_td_max_window = reply_cm->protocol_state.tcp.max_win;
616 sis->dest_td_end = reply_cm->protocol_state.tcp.end;
617 sis->dest_td_max_end = reply_cm->protocol_state.tcp.max_end;
618
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530619 sfe_ipv4_connection_match_update_summary_stats(original_cm, &packet_count, &byte_count);
620 sis->src_new_packet_count = packet_count;
621 sis->src_new_byte_count = byte_count;
Matthew McClintockd0cdb802014-02-24 16:30:35 -0600622
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530623 sfe_ipv4_connection_match_update_summary_stats(reply_cm, &packet_count, &byte_count);
624 sis->dest_new_packet_count = packet_count;
625 sis->dest_new_byte_count = byte_count;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100626
Matthew McClintockd0cdb802014-02-24 16:30:35 -0600627 sis->src_dev = original_cm->match_dev;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100628 sis->src_packet_count = original_cm->rx_packet_count64;
629 sis->src_byte_count = original_cm->rx_byte_count64;
Matthew McClintockd0cdb802014-02-24 16:30:35 -0600630
631 sis->dest_dev = reply_cm->match_dev;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100632 sis->dest_packet_count = reply_cm->rx_packet_count64;
633 sis->dest_byte_count = reply_cm->rx_byte_count64;
634
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700635 sis->reason = reason;
636
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100637 /*
638 * Get the time increment since our last sync.
639 */
640 sis->delta_jiffies = now_jiffies - c->last_sync_jiffies;
641 c->last_sync_jiffies = now_jiffies;
642}
643
644/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530645 * sfe_ipv4_free_connection_rcu()
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530646 * Called at RCU qs state to free the connection object.
647 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530648static void sfe_ipv4_free_connection_rcu(struct rcu_head *head)
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530649{
650 struct sfe_ipv4_connection *c;
Amitesh Anand63be37d2021-12-24 20:51:48 +0530651 struct udp_sock *up;
652 struct sock *sk;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530653
654 /*
655 * We dont need spin lock as the connection is already removed from link list
656 */
657 c = container_of(head, struct sfe_ipv4_connection, rcu);
658
659 BUG_ON(!c->removed);
660
661 DEBUG_TRACE("%px: connecton has been deleted\n", c);
662
663 /*
Amitesh Anand63be37d2021-12-24 20:51:48 +0530664 * Decrease the refcount taken in function sfe_ipv4_create_rule(),
665 * during call of __udp4_lib_lookup()
666 */
667 up = c->reply_match->up;
668 if (up) {
669 sk = (struct sock *)up;
670 sock_put(sk);
671 }
672
673 /*
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530674 * Release our hold of the source and dest devices and free the memory
675 * for our connection objects.
676 */
677 dev_put(c->original_dev);
678 dev_put(c->reply_dev);
679 kfree(c->original_match);
680 kfree(c->reply_match);
681 kfree(c);
682}
683
684/*
Ken Zhu88c58152021-12-09 15:12:06 -0800685 * sfe_ipv4_sync_status()
686 * update a connection status to its connection manager.
687 *
688 * si: the ipv4 context
689 * c: which connection to be notified
690 * reason: what kind of notification: flush, stats or destroy
691 */
692void sfe_ipv4_sync_status(struct sfe_ipv4 *si,
693 struct sfe_ipv4_connection *c,
694 sfe_sync_reason_t reason)
695{
696 struct sfe_connection_sync sis;
697 u64 now_jiffies;
698 sfe_sync_rule_callback_t sync_rule_callback;
699
700 rcu_read_lock();
701 sync_rule_callback = rcu_dereference(si->sync_rule_callback);
Ken Zhu7a43d882022-01-04 10:51:44 -0800702 rcu_read_unlock();
Ken Zhu88c58152021-12-09 15:12:06 -0800703 if (!sync_rule_callback) {
Ken Zhu88c58152021-12-09 15:12:06 -0800704 return;
705 }
706
707 /*
708 * Generate a sync message and then sync.
709 */
710 now_jiffies = get_jiffies_64();
711 sfe_ipv4_gen_sync_connection(si, c, &sis, reason, now_jiffies);
712 sync_rule_callback(&sis);
Ken Zhu88c58152021-12-09 15:12:06 -0800713}
714
715/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530716 * sfe_ipv4_flush_connection()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100717 * Flush a connection and free all associated resources.
718 *
719 * We need to be called with bottom halves disabled locally as we need to acquire
720 * the connection hash lock and release it again. In general we're actually called
721 * from within a BH and so we're fine, but we're also called when connections are
722 * torn down.
723 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530724void sfe_ipv4_flush_connection(struct sfe_ipv4 *si,
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700725 struct sfe_ipv4_connection *c,
726 sfe_sync_reason_t reason)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100727{
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530728 BUG_ON(!c->removed);
729
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530730 this_cpu_inc(si->stats_pcpu->connection_flushes64);
Ken Zhu88c58152021-12-09 15:12:06 -0800731 sfe_ipv4_sync_status(si, c, reason);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100732
733 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100734 * Release our hold of the source and dest devices and free the memory
735 * for our connection objects.
736 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530737 call_rcu(&c->rcu, sfe_ipv4_free_connection_rcu);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100738}
739
740/*
Parikshit Guned31a8202022-01-05 22:15:04 +0530741 * sfe_ipv4_service_class_stats_inc()
742 * Increment per cpu per service class stats.
743 */
744void sfe_ipv4_service_class_stats_inc(struct sfe_ipv4 *si, uint8_t sid, uint64_t bytes)
745{
746 struct sfe_ipv4_service_class_stats_db *sc_stats_db = this_cpu_ptr(si->stats_pcpu_psc);
747 struct sfe_ipv4_per_service_class_stats *sc_stats = &sc_stats_db->psc_stats[sid];
748
749 write_seqcount_begin(&sc_stats->seq);
750 sc_stats->tx_bytes += bytes;
751 sc_stats->tx_packets++;
752 write_seqcount_end(&sc_stats->seq);
753}
754
755/*
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530756 * sfe_ipv4_exception_stats_inc()
757 * Increment exception stats.
758 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530759void sfe_ipv4_exception_stats_inc(struct sfe_ipv4 *si, enum sfe_ipv4_exception_events reason)
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530760{
761 struct sfe_ipv4_stats *stats = this_cpu_ptr(si->stats_pcpu);
762 stats->exception_events64[reason]++;
763 stats->packets_not_forwarded64++;
764}
765
766/*
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530767 * sfe_ipv4_is_loal_ip()
768 * Returns true if IP is local; returns false otherwise.
769 */
770static bool sfe_ipv4_is_local_ip(struct sfe_ipv4 *si, __be32 ip_addr)
771{
772 struct net_device *dev;
773
774 dev = ip_dev_find(&init_net, ip_addr);
775 if (dev) {
776 dev_put(dev);
777 return true;
778 }
779
780 return false;
781}
782
783/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100784 * sfe_ipv4_recv()
Matthew McClintocka8ad7962014-01-16 16:49:30 -0600785 * Handle packet receives and forwaring.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100786 *
787 * Returns 1 if the packet is forwarded or 0 if it isn't.
788 */
Amitesh Anand63be37d2021-12-24 20:51:48 +0530789int sfe_ipv4_recv(struct net_device *dev, struct sk_buff *skb, struct sfe_l2_info *l2_info, bool tun_outer)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100790{
791 struct sfe_ipv4 *si = &__si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100792 unsigned int len;
793 unsigned int tot_len;
794 unsigned int frag_off;
795 unsigned int ihl;
Ken Zhu88c58152021-12-09 15:12:06 -0800796 bool sync_on_find;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100797 bool ip_options;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530798 struct iphdr *iph;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700799 u32 protocol;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100800
801 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100802 * Check that we have space for an IP header here.
803 */
804 len = skb->len;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530805 if (unlikely(!pskb_may_pull(skb, sizeof(struct iphdr)))) {
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530806 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_HEADER_INCOMPLETE);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100807 DEBUG_TRACE("len: %u is too short\n", len);
808 return 0;
809 }
810
811 /*
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +0530812 * Validate ip csum if necessary. If ip_summed is set to CHECKSUM_UNNECESSARY, it is assumed
813 * that the L3 checksum is validated by the Rx interface or the tunnel interface that has
814 * generated the packet.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100815 */
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530816 iph = (struct iphdr *)skb->data;
Ratheesh Kannoth43d64f82021-10-20 08:23:29 +0530817 if (unlikely(skb->ip_summed != CHECKSUM_UNNECESSARY) && (ip_fast_csum((u8 *)iph, iph->ihl))) {
818 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_HEADER_CSUM_BAD);
819
820 DEBUG_TRACE("Bad IPv4 header csum: 0x%x\n", iph->check);
821 return 0;
822 }
823
824 /*
825 * Check that our "total length" is large enough for an IP header.
826 */
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100827 tot_len = ntohs(iph->tot_len);
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530828 if (unlikely(tot_len < sizeof(struct iphdr))) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100829
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530830 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_BAD_TOTAL_LENGTH);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100831 DEBUG_TRACE("tot_len: %u is too short\n", tot_len);
832 return 0;
833 }
834
835 /*
836 * Is our IP version wrong?
837 */
838 if (unlikely(iph->version != 4)) {
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530839 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_NON_V4);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100840 DEBUG_TRACE("IP version: %u\n", iph->version);
841 return 0;
842 }
843
844 /*
845 * Does our datagram fit inside the skb?
846 */
847 if (unlikely(tot_len > len)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100848 DEBUG_TRACE("tot_len: %u, exceeds len: %u\n", tot_len, len);
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530849 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_DATAGRAM_INCOMPLETE);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100850 return 0;
851 }
852
853 /*
854 * Do we have a non-initial fragment?
Nicolas Costaac2979c2014-01-14 10:35:24 -0600855 */
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100856 frag_off = ntohs(iph->frag_off);
857 if (unlikely(frag_off & IP_OFFSET)) {
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530858 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_NON_INITIAL_FRAGMENT);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100859 DEBUG_TRACE("non-initial fragment\n");
860 return 0;
861 }
862
863 /*
864 * If we have a (first) fragment then mark it to cause any connection to flush.
865 */
Ken Zhu88c58152021-12-09 15:12:06 -0800866 sync_on_find = unlikely(frag_off & IP_MF) ? true : false;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100867
868 /*
869 * Do we have any IP options? That's definite a slow path! If we do have IP
870 * options we need to recheck our header size.
871 */
872 ihl = iph->ihl << 2;
Ratheesh Kannoth741f7992021-10-20 07:39:52 +0530873 ip_options = unlikely(ihl != sizeof(struct iphdr)) ? true : false;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100874 if (unlikely(ip_options)) {
875 if (unlikely(len < ihl)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100876
877 DEBUG_TRACE("len: %u is too short for header of size: %u\n", len, ihl);
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530878 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_IP_OPTIONS_INCOMPLETE);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100879 return 0;
880 }
881
Ken Zhu88c58152021-12-09 15:12:06 -0800882 sync_on_find = true;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100883 }
884
885 protocol = iph->protocol;
886 if (IPPROTO_UDP == protocol) {
Ken Zhu88c58152021-12-09 15:12:06 -0800887 return sfe_ipv4_recv_udp(si, skb, dev, len, iph, ihl, sync_on_find, l2_info, tun_outer);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100888 }
889
890 if (IPPROTO_TCP == protocol) {
Ken Zhu88c58152021-12-09 15:12:06 -0800891 return sfe_ipv4_recv_tcp(si, skb, dev, len, iph, ihl, sync_on_find, l2_info);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100892 }
893
894 if (IPPROTO_ICMP == protocol) {
895 return sfe_ipv4_recv_icmp(si, skb, dev, len, iph, ihl);
896 }
897
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530898#ifdef SFE_GRE_TUN_ENABLE
899 if (IPPROTO_GRE == protocol) {
Nitin Shetty2114a892022-01-28 20:03:56 +0530900 return sfe_ipv4_recv_gre(si, skb, dev, len, iph, ihl, sync_on_find, l2_info, tun_outer);
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530901 }
902#endif
Tian Yangd98d91b2022-03-09 14:50:12 -0800903 if (IPPROTO_IPV6 == protocol) {
904 return sfe_ipv4_recv_tun6rd(si, skb, dev, len, iph, ihl, sync_on_find, l2_info, true);
905 }
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530906
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530907 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_UNHANDLED_PROTOCOL);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +0100908
909 DEBUG_TRACE("not UDP, TCP or ICMP: %u\n", protocol);
910 return 0;
911}
912
Nicolas Costa436926b2014-01-14 10:36:22 -0600913static void
914sfe_ipv4_update_tcp_state(struct sfe_ipv4_connection *c,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530915 struct sfe_ipv4_rule_create_msg *msg)
Nicolas Costa436926b2014-01-14 10:36:22 -0600916{
917 struct sfe_ipv4_connection_match *orig_cm;
918 struct sfe_ipv4_connection_match *repl_cm;
919 struct sfe_ipv4_tcp_connection_match *orig_tcp;
920 struct sfe_ipv4_tcp_connection_match *repl_tcp;
921
922 orig_cm = c->original_match;
923 repl_cm = c->reply_match;
924 orig_tcp = &orig_cm->protocol_state.tcp;
925 repl_tcp = &repl_cm->protocol_state.tcp;
926
927 /* update orig */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530928 if (orig_tcp->max_win < msg->tcp_rule.flow_max_window) {
929 orig_tcp->max_win = msg->tcp_rule.flow_max_window;
Nicolas Costa436926b2014-01-14 10:36:22 -0600930 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530931 if ((s32)(orig_tcp->end - msg->tcp_rule.flow_end) < 0) {
932 orig_tcp->end = msg->tcp_rule.flow_end;
Nicolas Costa436926b2014-01-14 10:36:22 -0600933 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530934 if ((s32)(orig_tcp->max_end - msg->tcp_rule.flow_max_end) < 0) {
935 orig_tcp->max_end = msg->tcp_rule.flow_max_end;
Nicolas Costa436926b2014-01-14 10:36:22 -0600936 }
937
938 /* update reply */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530939 if (repl_tcp->max_win < msg->tcp_rule.return_max_window) {
940 repl_tcp->max_win = msg->tcp_rule.return_max_window;
Nicolas Costa436926b2014-01-14 10:36:22 -0600941 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530942 if ((s32)(repl_tcp->end - msg->tcp_rule.return_end) < 0) {
943 repl_tcp->end = msg->tcp_rule.return_end;
Nicolas Costa436926b2014-01-14 10:36:22 -0600944 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530945 if ((s32)(repl_tcp->max_end - msg->tcp_rule.return_max_end) < 0) {
946 repl_tcp->max_end = msg->tcp_rule.return_max_end;
Nicolas Costa436926b2014-01-14 10:36:22 -0600947 }
948
949 /* update match flags */
950 orig_cm->flags &= ~SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
951 repl_cm->flags &= ~SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530952 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_NO_SEQ_CHECK) {
953
Nicolas Costa436926b2014-01-14 10:36:22 -0600954 orig_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
955 repl_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
956 }
957}
958
959static void
960sfe_ipv4_update_protocol_state(struct sfe_ipv4_connection *c,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530961 struct sfe_ipv4_rule_create_msg *msg)
Nicolas Costa436926b2014-01-14 10:36:22 -0600962{
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530963 switch (msg->tuple.protocol) {
Nicolas Costa436926b2014-01-14 10:36:22 -0600964 case IPPROTO_TCP:
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530965 sfe_ipv4_update_tcp_state(c, msg);
Nicolas Costa436926b2014-01-14 10:36:22 -0600966 break;
967 }
968}
969
Wayne Tanbb7f1782021-12-13 11:16:04 -0800970/*
971 * sfe_ipv4_match_entry_set_vlan()
972 */
973static void sfe_ipv4_match_entry_set_vlan(
974 struct sfe_ipv4_connection_match *cm,
975 u32 primary_ingress_vlan_tag,
976 u32 primary_egress_vlan_tag,
977 u32 secondary_ingress_vlan_tag,
978 u32 secondary_egress_vlan_tag)
979{
980 u16 tpid;
981 /*
982 * Prevent stacking header counts when updating.
983 */
984 cm->ingress_vlan_hdr_cnt = 0;
985 cm->egress_vlan_hdr_cnt = 0;
986 memset(cm->ingress_vlan_hdr, 0, sizeof(cm->ingress_vlan_hdr));
987 memset(cm->egress_vlan_hdr, 0, sizeof(cm->egress_vlan_hdr));
988
989 /*
990 * vlan_hdr[0] corresponds to outer tag
991 * vlan_hdr[1] corresponds to inner tag
992 * Extract the vlan information (tpid and tci) from rule message
993 */
994 if ((primary_ingress_vlan_tag & VLAN_VID_MASK) != SFE_VLAN_ID_NOT_CONFIGURED) {
995 tpid = (u16)(primary_ingress_vlan_tag >> 16);
996 cm->ingress_vlan_hdr[0].tpid = ntohs(tpid);
997 cm->ingress_vlan_hdr[0].tci = (u16)primary_ingress_vlan_tag;
998 cm->ingress_vlan_hdr_cnt++;
999 }
1000
1001 if ((secondary_ingress_vlan_tag & VLAN_VID_MASK) != SFE_VLAN_ID_NOT_CONFIGURED) {
1002 tpid = (u16)(secondary_ingress_vlan_tag >> 16);
1003 cm->ingress_vlan_hdr[1].tpid = ntohs(tpid);
1004 cm->ingress_vlan_hdr[1].tci = (u16)secondary_ingress_vlan_tag;
1005 cm->ingress_vlan_hdr_cnt++;
1006 }
1007
1008 if ((primary_egress_vlan_tag & VLAN_VID_MASK) != SFE_VLAN_ID_NOT_CONFIGURED) {
1009 tpid = (u16)(primary_egress_vlan_tag >> 16);
1010 cm->egress_vlan_hdr[0].tpid = ntohs(tpid);
1011 cm->egress_vlan_hdr[0].tci = (u16)primary_egress_vlan_tag;
1012 cm->egress_vlan_hdr_cnt++;
1013 }
1014
1015 if ((secondary_egress_vlan_tag & VLAN_VID_MASK) != SFE_VLAN_ID_NOT_CONFIGURED) {
1016 tpid = (u16)(secondary_egress_vlan_tag >> 16);
1017 cm->egress_vlan_hdr[1].tpid = ntohs(tpid);
1018 cm->egress_vlan_hdr[1].tci = (u16)secondary_egress_vlan_tag;
1019 cm->egress_vlan_hdr_cnt++;
1020 }
1021}
1022
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301023void sfe_ipv4_update_rule(struct sfe_ipv4_rule_create_msg *msg)
Nicolas Costa436926b2014-01-14 10:36:22 -06001024{
1025 struct sfe_ipv4_connection *c;
1026 struct sfe_ipv4 *si = &__si;
1027
1028 spin_lock_bh(&si->lock);
1029
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301030 c = sfe_ipv4_find_connection(si,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301031 msg->tuple.protocol,
1032 msg->tuple.flow_ip,
1033 msg->tuple.flow_ident,
1034 msg->tuple.return_ip,
1035 msg->tuple.return_ident);
Nicolas Costa436926b2014-01-14 10:36:22 -06001036 if (c != NULL) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301037 sfe_ipv4_update_protocol_state(c, msg);
Nicolas Costa436926b2014-01-14 10:36:22 -06001038 }
1039
1040 spin_unlock_bh(&si->lock);
1041}
1042
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001043/*
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +05301044 * sfe_ipv4_xmit_eth_type_check()
1045 * Checking if MAC header has to be written.
1046 */
1047static inline bool sfe_ipv4_xmit_eth_type_check(struct net_device *dev, u32 cm_flags)
1048{
1049 if (!(dev->flags & IFF_NOARP)) {
1050 return true;
1051 }
1052
1053 /*
1054 * For PPPoE, since we are now supporting PPPoE encapsulation, we are writing L2 header.
1055 */
1056 if (unlikely(cm_flags & SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_ENCAP)) {
1057 return true;
1058 }
1059
1060 return false;
1061}
1062
1063/*
Jackson Bockus3fafbf32022-02-13 17:15:26 -08001064 * sfe_ipv4_service_class_stats_pcpu_get()
1065 * Gets one CPU's service class statistics.
1066 */
1067static inline bool sfe_ipv4_service_class_stats_pcpu_get(struct sfe_ipv4_per_service_class_stats *sc_stats, uint64_t *bytes, uint64_t *packets)
1068{
1069 uint32_t retries = 0;
1070 uint32_t seq;
1071 uint64_t bytes_tmp, packets_tmp;
1072
1073 do {
1074 seq = read_seqcount_begin(&sc_stats->seq);
1075 bytes_tmp = sc_stats->tx_bytes;
1076 packets_tmp = sc_stats->tx_packets;
1077 } while (read_seqcount_retry(&sc_stats->seq, seq) && ++retries < SFE_SERVICE_CLASS_STATS_MAX_RETRY);
1078
1079 *bytes += bytes_tmp;
1080 *packets += packets_tmp;
1081
1082 return retries < SFE_SERVICE_CLASS_STATS_MAX_RETRY;
1083}
1084
1085/*
1086 * sfe_ipv4_service_class_stats_get()
1087 * Copy the ipv4 statistics for the given service class.
1088 */
1089bool sfe_ipv4_service_class_stats_get(uint8_t sid, uint64_t *bytes, uint64_t *packets)
1090{
1091 struct sfe_ipv4 *si = &__si;
1092 uint32_t cpu = 0;
1093
1094 for_each_possible_cpu(cpu) {
1095 struct sfe_ipv4_service_class_stats_db *stats_db = per_cpu_ptr(si->stats_pcpu_psc, cpu);
1096 struct sfe_ipv4_per_service_class_stats *sc_stats = &stats_db->psc_stats[sid];
1097
1098 if (!sfe_ipv4_service_class_stats_pcpu_get(sc_stats, bytes, packets)) {
1099 return false;
1100 }
1101 }
1102
1103 return true;
1104}
1105
1106/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001107 * sfe_ipv4_create_rule()
1108 * Create a forwarding rule.
1109 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301110int sfe_ipv4_create_rule(struct sfe_ipv4_rule_create_msg *msg)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001111{
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001112 struct sfe_ipv4 *si = &__si;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301113 struct sfe_ipv4_connection *c, *c_old;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001114 struct sfe_ipv4_connection_match *original_cm;
1115 struct sfe_ipv4_connection_match *reply_cm;
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001116 struct net_device *dest_dev;
1117 struct net_device *src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301118 struct sfe_ipv4_5tuple *tuple = &msg->tuple;
Suruchi Sumanc1a4a612021-10-21 14:50:23 +05301119 s32 flow_interface_num = msg->conn_rule.flow_top_interface_num;
1120 s32 return_interface_num = msg->conn_rule.return_top_interface_num;
Amitesh Anand63be37d2021-12-24 20:51:48 +05301121 struct net *net;
1122 struct sock *sk;
1123 unsigned int src_if_idx;
Parikshit Guned31a8202022-01-05 22:15:04 +05301124 u32 flow_sawf_tag;
1125 u32 return_sawf_tag;
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001126
Suruchi Sumanc1a4a612021-10-21 14:50:23 +05301127 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_FLOW_BOTTOM_INTERFACE) {
1128 flow_interface_num = msg->conn_rule.flow_interface_num;
1129 }
1130
1131 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_RETURN_BOTTOM_INTERFACE) {
1132 return_interface_num = msg->conn_rule.return_interface_num;
1133 }
1134
1135 src_dev = dev_get_by_index(&init_net, flow_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301136 if (!src_dev) {
1137 DEBUG_WARN("%px: Unable to find src_dev corresponding to %d\n", msg,
Suruchi Sumanc1a4a612021-10-21 14:50:23 +05301138 flow_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301139 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
1140 return -EINVAL;
1141 }
1142
Suruchi Sumanc1a4a612021-10-21 14:50:23 +05301143 dest_dev = dev_get_by_index(&init_net, return_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301144 if (!dest_dev) {
1145 DEBUG_WARN("%px: Unable to find dest_dev corresponding to %d\n", msg,
Suruchi Sumanc1a4a612021-10-21 14:50:23 +05301146 return_interface_num);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301147 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
1148 dev_put(src_dev);
1149 return -EINVAL;
1150 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001151
Matthew McClintock389b42a2014-09-24 14:05:51 -05001152 if (unlikely((dest_dev->reg_state != NETREG_REGISTERED) ||
1153 (src_dev->reg_state != NETREG_REGISTERED))) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301154 dev_put(src_dev);
1155 dev_put(dest_dev);
1156 DEBUG_WARN("%px: src_dev=%s and dest_dev=%s are unregistered\n", msg,
1157 src_dev->name, dest_dev->name);
1158 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
Matthew McClintock389b42a2014-09-24 14:05:51 -05001159 return -EINVAL;
1160 }
1161
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301162 /*
1163 * Allocate the various connection tracking objects.
1164 */
Parikshit Guneef1664c2022-03-24 14:15:42 +05301165 c = (struct sfe_ipv4_connection *)kzalloc(sizeof(struct sfe_ipv4_connection), GFP_ATOMIC);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301166 if (unlikely(!c)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301167 DEBUG_WARN("%px: memory allocation of connection entry failed\n", msg);
1168 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
1169 dev_put(src_dev);
1170 dev_put(dest_dev);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301171 return -ENOMEM;
1172 }
1173
Parikshit Guneef1664c2022-03-24 14:15:42 +05301174 original_cm = (struct sfe_ipv4_connection_match *)kzalloc(sizeof(struct sfe_ipv4_connection_match), GFP_ATOMIC);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301175 if (unlikely(!original_cm)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301176 DEBUG_WARN("%px: memory allocation of connection match entry failed\n", msg);
1177 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301178 kfree(c);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301179 dev_put(src_dev);
1180 dev_put(dest_dev);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301181 return -ENOMEM;
1182 }
1183
Parikshit Guneef1664c2022-03-24 14:15:42 +05301184 reply_cm = (struct sfe_ipv4_connection_match *)kzalloc(sizeof(struct sfe_ipv4_connection_match), GFP_ATOMIC);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301185 if (unlikely(!reply_cm)) {
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301186 DEBUG_WARN("%px: memory allocation of connection match entry failed\n", msg);
1187 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301188 kfree(original_cm);
1189 kfree(c);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301190 dev_put(src_dev);
1191 dev_put(dest_dev);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301192 return -ENOMEM;
1193 }
1194
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301195 this_cpu_inc(si->stats_pcpu->connection_create_requests64);
1196
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001197 spin_lock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001198
1199 /*
Nicolas Costa436926b2014-01-14 10:36:22 -06001200 * Check to see if there is already a flow that matches the rule we're
1201 * trying to create. If there is then we can't create a new one.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001202 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301203 c_old = sfe_ipv4_find_connection(si,
Wayne Tanbb7f1782021-12-13 11:16:04 -08001204 msg->tuple.protocol,
1205 msg->tuple.flow_ip,
1206 msg->tuple.flow_ident,
1207 msg->tuple.return_ip,
1208 msg->tuple.return_ident);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301209
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301210 if (c_old != NULL) {
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301211 this_cpu_inc(si->stats_pcpu->connection_create_collisions64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001212
1213 /*
Nicolas Costa436926b2014-01-14 10:36:22 -06001214 * If we already have the flow then it's likely that this
1215 * request to create the connection rule contains more
1216 * up-to-date information. Check and update accordingly.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001217 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301218 sfe_ipv4_update_protocol_state(c, msg);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001219 spin_unlock_bh(&si->lock);
1220
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301221 kfree(reply_cm);
1222 kfree(original_cm);
1223 kfree(c);
1224
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301225 dev_put(src_dev);
1226 dev_put(dest_dev);
1227
Amitesh Anand63be37d2021-12-24 20:51:48 +05301228 DEBUG_TRACE("%px: connection already exists - p:%d\n"
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301229 " s: %s:%pM:%pI4:%u, d: %s:%pM:%pI4:%u\n",
Amitesh Anand63be37d2021-12-24 20:51:48 +05301230 msg, tuple->protocol,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301231 src_dev->name, msg->conn_rule.flow_mac, &tuple->flow_ip, ntohs(tuple->flow_ident),
1232 dest_dev->name, msg->conn_rule.return_mac, &tuple->return_ip, ntohs(tuple->return_ident));
1233
Nicolas Costa514fde02014-01-13 15:50:29 -06001234 return -EADDRINUSE;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001235 }
1236
1237 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001238 * Fill in the "original" direction connection matching object.
1239 * Note that the transmit MAC address is "dest_mac_xlate" because
1240 * we always know both ends of a connection by their translated
1241 * addresses and not their public addresses.
1242 */
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001243 original_cm->match_dev = src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301244 original_cm->match_protocol = tuple->protocol;
1245 original_cm->match_src_ip = tuple->flow_ip;
Suruchi Suman66609a72022-01-20 02:34:25 +05301246 original_cm->match_src_port = netif_is_vxlan(src_dev) ? 0 : tuple->flow_ident;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301247 original_cm->match_dest_ip = tuple->return_ip;
1248 original_cm->match_dest_port = tuple->return_ident;
1249
1250 original_cm->xlate_src_ip = msg->conn_rule.flow_ip_xlate;
1251 original_cm->xlate_src_port = msg->conn_rule.flow_ident_xlate;
1252 original_cm->xlate_dest_ip = msg->conn_rule.return_ip_xlate;
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301253 original_cm->xlate_dest_port = msg->conn_rule.return_ident_xlate;
1254
1255 if (tuple->protocol == IPPROTO_GRE) {
1256 /*
1257 * the PPTP is 4 tuple lookup.
1258 * During th rule lookup destination call id from packet
1259 * is matched against destination port in cm.
1260 */
1261 original_cm->match_src_port = 0;
1262 original_cm->xlate_src_port = 0;
1263 }
Wayne Tanbb7f1782021-12-13 11:16:04 -08001264
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001265 original_cm->xmit_dev = dest_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301266 original_cm->xmit_dev_mtu = msg->conn_rule.return_mtu;
1267
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001268 original_cm->connection = c;
1269 original_cm->counter_match = reply_cm;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301270
Amitesh Anand63be37d2021-12-24 20:51:48 +05301271 /*
1272 * UDP Socket is valid only in decap direction.
1273 */
1274 RCU_INIT_POINTER(original_cm->up, NULL);
1275
Ken Zhu37040ea2021-09-09 21:11:15 -07001276 if (msg->valid_flags & SFE_RULE_CREATE_MARK_VALID) {
1277 original_cm->mark = msg->mark_rule.flow_mark;
1278 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_MARK;
1279 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301280 if (msg->valid_flags & SFE_RULE_CREATE_QOS_VALID) {
1281 original_cm->priority = msg->qos_rule.flow_qos_tag;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001282 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PRIORITY_REMARK;
1283 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301284 if (msg->valid_flags & SFE_RULE_CREATE_DSCP_MARKING_VALID) {
1285 original_cm->dscp = msg->dscp_rule.flow_dscp << SFE_IPV4_DSCP_SHIFT;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001286 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_DSCP_REMARK;
1287 }
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301288 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1289 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_BRIDGE_FLOW;
1290 }
Ken Zhu7e38d1a2021-11-30 17:31:46 -08001291 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_FLOW_TRANSMIT_FAST) {
1292 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_FAST_XMIT_DEV_ADMISSION;
1293 }
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301294
Wayne Tanbb7f1782021-12-13 11:16:04 -08001295 /*
Parikshit Guned31a8202022-01-05 22:15:04 +05301296 * Mark SAWF metadata if the sawf tag is valid and set.
1297 */
1298 original_cm->sawf_valid = false;
1299 flow_sawf_tag = SFE_GET_SAWF_TAG(msg->sawf_rule.flow_mark);
1300 if (likely(SFE_SAWF_TAG_IS_VALID(flow_sawf_tag))) {
1301 original_cm->mark = msg->sawf_rule.flow_mark;
1302 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_MARK;
1303 original_cm->sawf_valid = true;
1304 }
1305
1306 /*
Wayne Tanbb7f1782021-12-13 11:16:04 -08001307 * Add VLAN rule to original_cm
1308 */
1309 if (msg->valid_flags & SFE_RULE_CREATE_VLAN_VALID) {
1310 struct sfe_vlan_rule *vlan_primary_rule = &msg->vlan_primary_rule;
1311 struct sfe_vlan_rule *vlan_secondary_rule = &msg->vlan_secondary_rule;
1312 sfe_ipv4_match_entry_set_vlan(original_cm,
1313 vlan_primary_rule->ingress_vlan_tag,
1314 vlan_primary_rule->egress_vlan_tag,
1315 vlan_secondary_rule->ingress_vlan_tag,
1316 vlan_secondary_rule->egress_vlan_tag);
1317
1318 if ((msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_RETURN_BOTTOM_INTERFACE) &&
1319 original_cm->egress_vlan_hdr_cnt > 0) {
1320 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_INSERT_EGRESS_VLAN_TAG;
1321 original_cm->l2_hdr_size += original_cm->egress_vlan_hdr_cnt * VLAN_HLEN;
1322 }
1323 }
1324
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301325 if ((IPPROTO_GRE == tuple->protocol) && !sfe_ipv4_is_local_ip(si, original_cm->match_dest_ip)) {
1326 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PASSTHROUGH;
1327 }
1328
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001329#ifdef CONFIG_NF_FLOW_COOKIE
1330 original_cm->flow_cookie = 0;
1331#endif
Zhi Chen8748eb32015-06-18 12:58:48 -07001332#ifdef CONFIG_XFRM
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301333 if (msg->valid_flags & SFE_RULE_CREATE_DIRECTION_VALID) {
1334 original_cm->flow_accel = msg->direction_rule.flow_accel;
1335 } else {
1336 original_cm->flow_accel = 1;
1337 }
Zhi Chen8748eb32015-06-18 12:58:48 -07001338#endif
Tian Yangd98d91b2022-03-09 14:50:12 -08001339
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301340 /*
1341 * If l2_features are disabled and flow uses l2 features such as macvlan/bridge/pppoe/vlan,
1342 * bottom interfaces are expected to be disabled in the flow rule and always top interfaces
1343 * are used. In such cases, do not use HW csum offload. csum offload is used only when we
1344 * are sending directly to the destination interface that supports it.
1345 */
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301346 if (likely(dest_dev->features & NETIF_F_HW_CSUM) && sfe_dev_has_hw_csum(dest_dev)) {
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301347 if ((msg->conn_rule.return_top_interface_num == msg->conn_rule.return_interface_num) ||
1348 (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_RETURN_BOTTOM_INTERFACE)) {
Ratheesh Kannoth48445532022-02-07 16:19:00 +05301349
1350 /*
1351 * Dont enable CSUM offload
1352 */
1353#if 0
Suruchi Sumanf2077182022-01-13 21:35:23 +05301354 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_CSUM_OFFLOAD;
Ratheesh Kannoth48445532022-02-07 16:19:00 +05301355#endif
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301356 }
1357 }
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001358
Ratheesh Kannoth5dee3772022-01-18 11:27:14 +05301359 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_SRC_INTERFACE_CHECK) {
1360 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_SRC_INTERFACE_CHECK;
1361 }
1362
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001363 /*
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301364 * Adding PPPoE parameters to original and reply entries based on the direction where
1365 * PPPoE header is valid in ECM rule.
1366 *
1367 * If PPPoE is valid in flow direction (from interface is PPPoE), then
1368 * original cm will have PPPoE at ingress (strip PPPoE header)
1369 * reply cm will have PPPoE at egress (add PPPoE header)
1370 *
1371 * If PPPoE is valid in return direction (to interface is PPPoE), then
1372 * original cm will have PPPoE at egress (add PPPoE header)
1373 * reply cm will have PPPoE at ingress (strip PPPoE header)
1374 */
1375 if (msg->valid_flags & SFE_RULE_CREATE_PPPOE_DECAP_VALID) {
1376 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_DECAP;
1377 original_cm->pppoe_session_id = msg->pppoe_rule.flow_pppoe_session_id;
1378 ether_addr_copy(original_cm->pppoe_remote_mac, msg->pppoe_rule.flow_pppoe_remote_mac);
1379
1380 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_ENCAP;
Wayne Tanbb7f1782021-12-13 11:16:04 -08001381 reply_cm->l2_hdr_size += SFE_PPPOE_SESSION_HEADER_SIZE;
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301382 reply_cm->pppoe_session_id = msg->pppoe_rule.flow_pppoe_session_id;
1383 ether_addr_copy(reply_cm->pppoe_remote_mac, msg->pppoe_rule.flow_pppoe_remote_mac);
1384 }
1385
1386 if (msg->valid_flags & SFE_RULE_CREATE_PPPOE_ENCAP_VALID) {
1387 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_ENCAP;
Wayne Tanbb7f1782021-12-13 11:16:04 -08001388 original_cm->l2_hdr_size += SFE_PPPOE_SESSION_HEADER_SIZE;
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301389 original_cm->pppoe_session_id = msg->pppoe_rule.return_pppoe_session_id;
1390 ether_addr_copy(original_cm->pppoe_remote_mac, msg->pppoe_rule.return_pppoe_remote_mac);
1391
1392 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_DECAP;
1393 reply_cm->pppoe_session_id = msg->pppoe_rule.return_pppoe_session_id;
1394 ether_addr_copy(reply_cm->pppoe_remote_mac, msg->pppoe_rule.return_pppoe_remote_mac);
1395 }
1396
Ratheesh Kannoth5dee3772022-01-18 11:27:14 +05301397 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_SRC_INTERFACE_CHECK) {
1398 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_SRC_INTERFACE_CHECK;
1399 }
1400
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05301401 /*
Ken Zhubbf49652021-09-12 15:33:09 -07001402 * For the non-arp interface, we don't write L2 HDR.
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001403 */
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +05301404 if (sfe_ipv4_xmit_eth_type_check(dest_dev, original_cm->flags)) {
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301405
1406 /*
1407 * Check whether the rule has configured a specific source MAC address to use.
1408 * This is needed when virtual L3 interfaces such as br-lan, macvlan, vlan are used during egress
1409 */
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301410
1411 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1412 ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)msg->conn_rule.flow_mac);
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301413 } else {
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301414 if ((msg->valid_flags & SFE_RULE_CREATE_SRC_MAC_VALID) &&
1415 (msg->src_mac_rule.mac_valid_flags & SFE_SRC_MAC_RETURN_VALID)) {
1416 ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)msg->src_mac_rule.return_src_mac);
1417 } else {
1418 ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)dest_dev->dev_addr);
1419 }
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301420 }
1421
1422 ether_addr_copy((u8 *)original_cm->xmit_dest_mac, (u8 *)msg->conn_rule.return_mac);
1423
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001424 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_L2_HDR;
Wayne Tanbb7f1782021-12-13 11:16:04 -08001425 original_cm->l2_hdr_size += ETH_HLEN;
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001426
1427 /*
1428 * If our dev writes Ethernet headers then we can write a really fast
1429 * version.
1430 */
1431 if (dest_dev->header_ops) {
1432 if (dest_dev->header_ops->create == eth_header) {
1433 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR;
1434 }
1435 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001436 }
1437
1438 /*
1439 * Fill in the "reply" direction connection matching object.
1440 */
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001441 reply_cm->match_dev = dest_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301442 reply_cm->match_protocol = tuple->protocol;
1443 reply_cm->match_src_ip = msg->conn_rule.return_ip_xlate;
Amitesh Anand63be37d2021-12-24 20:51:48 +05301444
1445 /*
1446 * Keep source port as 0 for VxLAN tunnels.
1447 */
1448 if (netif_is_vxlan(src_dev) || netif_is_vxlan(dest_dev)) {
1449 reply_cm->match_src_port = 0;
1450 } else {
1451 reply_cm->match_src_port = msg->conn_rule.return_ident_xlate;
1452 }
1453
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301454 reply_cm->match_dest_ip = msg->conn_rule.flow_ip_xlate;
1455 reply_cm->match_dest_port = msg->conn_rule.flow_ident_xlate;
1456
1457 reply_cm->xlate_src_ip = tuple->return_ip;
1458 reply_cm->xlate_src_port = tuple->return_ident;
1459 reply_cm->xlate_dest_ip = tuple->flow_ip;
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301460 reply_cm->xlate_dest_port = tuple->flow_ident;
1461
1462 if (tuple->protocol == IPPROTO_GRE) {
1463 /*
1464 * the PPTP is 4 tuple lookup.
1465 * During th rule lookup destination call id from packet
1466 * is matched against destination port in cm.
1467 */
1468 reply_cm->match_src_port = 0;
1469 reply_cm->xlate_src_port = 0;
1470 }
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301471
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001472 reply_cm->xmit_dev = src_dev;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301473 reply_cm->xmit_dev_mtu = msg->conn_rule.flow_mtu;
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301474
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001475 reply_cm->connection = c;
1476 reply_cm->counter_match = original_cm;
Ken Zhu37040ea2021-09-09 21:11:15 -07001477
Ken Zhu37040ea2021-09-09 21:11:15 -07001478 if (msg->valid_flags & SFE_RULE_CREATE_MARK_VALID) {
1479 reply_cm->mark = msg->mark_rule.return_mark;
1480 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_MARK;
1481 }
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301482 if (msg->valid_flags & SFE_RULE_CREATE_QOS_VALID) {
1483 reply_cm->priority = msg->qos_rule.return_qos_tag;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001484 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PRIORITY_REMARK;
1485 }
Wayne Tanbb7f1782021-12-13 11:16:04 -08001486
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301487 if (msg->valid_flags & SFE_RULE_CREATE_DSCP_MARKING_VALID) {
1488 reply_cm->dscp = msg->dscp_rule.return_dscp << SFE_IPV4_DSCP_SHIFT;
Xiaoping Fane1963d42015-08-25 17:06:19 -07001489 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_DSCP_REMARK;
1490 }
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301491 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1492 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_BRIDGE_FLOW;
1493 }
Ken Zhu7e38d1a2021-11-30 17:31:46 -08001494 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_RETURN_TRANSMIT_FAST) {
1495 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_FAST_XMIT_DEV_ADMISSION;
1496 }
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301497
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301498 if ((IPPROTO_GRE == tuple->protocol) && !sfe_ipv4_is_local_ip(si, reply_cm->match_dest_ip)) {
1499 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PASSTHROUGH;
1500 }
1501
Amitesh Anand63be37d2021-12-24 20:51:48 +05301502 /*
Parikshit Guned31a8202022-01-05 22:15:04 +05301503 * Mark SAWF metadata in reply match if the sawf tag is valid.
1504 */
1505 reply_cm->sawf_valid = false;
1506 return_sawf_tag = SFE_GET_SAWF_TAG(msg->sawf_rule.return_mark);
1507 if (likely(SFE_SAWF_TAG_IS_VALID(return_sawf_tag))) {
1508 reply_cm->mark = msg->sawf_rule.return_mark;
1509 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_MARK;
1510 reply_cm->sawf_valid = true;
1511 }
1512
1513 /*
Amitesh Anand63be37d2021-12-24 20:51:48 +05301514 * Setup UDP Socket if found to be valid for decap.
1515 */
1516 RCU_INIT_POINTER(reply_cm->up, NULL);
1517 net = dev_net(reply_cm->match_dev);
1518 src_if_idx = src_dev->ifindex;
1519
1520 rcu_read_lock();
1521
1522 /*
1523 * Look for the associated sock object.
1524 * __udp4_lib_lookup() holds a reference for this sock object,
1525 * which will be released in sfe_ipv4_free_connection_rcu()
1526 */
1527#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
1528 sk = __udp4_lib_lookup(net, reply_cm->match_dest_ip, reply_cm->match_dest_port,
1529 reply_cm->xlate_src_ip, reply_cm->xlate_src_port, src_if_idx, &udp_table);
1530#else
1531 sk = __udp4_lib_lookup(net, reply_cm->match_dest_ip, reply_cm->match_dest_port,
1532 reply_cm->xlate_src_ip, reply_cm->xlate_src_port, src_if_idx, 0, &udp_table, NULL);
1533#endif
1534
1535 rcu_read_unlock();
1536
1537 /*
1538 * We set the UDP sock pointer as valid only for decap direction.
1539 */
1540 if (sk && udp_sk(sk)->encap_type) {
1541#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
1542 if (!atomic_add_unless(&sk->sk_refcnt, 1, 0)) {
1543#else
1544 if (!refcount_inc_not_zero(&sk->sk_refcnt)) {
1545#endif
Nitin Shetty9ab15622022-04-11 08:04:06 +05301546 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
Wayne Tanbb7f1782021-12-13 11:16:04 -08001547 spin_unlock_bh(&si->lock);
Amitesh Anand63be37d2021-12-24 20:51:48 +05301548 kfree(reply_cm);
1549 kfree(original_cm);
1550 kfree(c);
1551
1552 DEBUG_TRACE("%px: sfe: unable to take reference for socket(%px) p:%d\n"
1553 " s: %s:%pM:%pI4:%u, d: %s:%pM:%pI4:%u\n",
1554 msg, sk, tuple->protocol,
1555 src_dev->name, msg->conn_rule.flow_mac, &tuple->flow_ip, ntohs(tuple->flow_ident),
1556 dest_dev->name, msg->conn_rule.return_mac, &tuple->return_ip, ntohs(tuple->return_ident));
1557
1558 dev_put(src_dev);
1559 dev_put(dest_dev);
1560
1561 return -ESHUTDOWN;
1562 }
1563
1564 rcu_assign_pointer(reply_cm->up, udp_sk(sk));
1565
1566 DEBUG_INFO("%px: Sock(%px) lookup success with reply_cm direction\n", msg, sk);
1567 DEBUG_INFO("%px: SFE connection -\n"
1568 " s: %s:%pI4(%pI4):%u(%u)\n"
1569 " d: %s:%pI4(%pI4):%u(%u)\n",
1570 msg, reply_cm->match_dev->name, &reply_cm->match_src_ip, &reply_cm->xlate_src_ip,
1571 ntohs(reply_cm->match_src_port), ntohs(reply_cm->xlate_src_port),
1572 reply_cm->xmit_dev->name, &reply_cm->match_dest_ip, &reply_cm->xlate_dest_ip,
1573 ntohs(reply_cm->match_dest_port), ntohs(reply_cm->xlate_dest_port));
1574 }
1575
Wayne Tanbb7f1782021-12-13 11:16:04 -08001576 /*
1577 * Add VLAN rule to reply_cm
1578 */
1579 if (msg->valid_flags & SFE_RULE_CREATE_VLAN_VALID) {
1580 struct sfe_vlan_rule *vlan_primary_rule = &msg->vlan_primary_rule;
1581 struct sfe_vlan_rule *vlan_secondary_rule = &msg->vlan_secondary_rule;
1582 sfe_ipv4_match_entry_set_vlan(reply_cm,
1583 vlan_primary_rule->egress_vlan_tag,
1584 vlan_primary_rule->ingress_vlan_tag,
1585 vlan_secondary_rule->egress_vlan_tag,
1586 vlan_secondary_rule->ingress_vlan_tag);
1587
1588 if ((msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_FLOW_BOTTOM_INTERFACE) &&
1589 reply_cm->egress_vlan_hdr_cnt > 0) {
1590 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_INSERT_EGRESS_VLAN_TAG;
1591 reply_cm->l2_hdr_size += reply_cm->egress_vlan_hdr_cnt * VLAN_HLEN;
1592 }
1593 }
1594
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301595 /*
1596 * the net_protocol handler will be used only in decap path
1597 * for non passthrough case.
1598 */
1599 original_cm->proto = NULL;
1600 reply_cm->proto = NULL;
1601
1602#ifdef SFE_GRE_TUN_ENABLE
1603 if ((IPPROTO_GRE == tuple->protocol) && !(reply_cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_PASSTHROUGH)) {
1604 rcu_read_lock();
1605 reply_cm->proto = rcu_dereference(inet_protos[IPPROTO_GRE]);
1606 rcu_read_unlock();
1607
1608 if (unlikely(!reply_cm->proto)) {
Nitin Shetty9ab15622022-04-11 08:04:06 +05301609 this_cpu_inc(si->stats_pcpu->connection_create_failures64);
1610 spin_unlock_bh(&si->lock);
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301611 kfree(reply_cm);
1612 kfree(original_cm);
1613 kfree(c);
1614 dev_put(src_dev);
1615 dev_put(dest_dev);
1616 DEBUG_WARN("sfe: GRE proto handler is not registered\n");
1617 return -EPERM;
1618 }
1619 }
1620#endif
1621
Tian Yangd98d91b2022-03-09 14:50:12 -08001622 if (IPPROTO_IPV6 == tuple->protocol) {
1623 original_cm->proto = NULL;
1624 rcu_read_lock();
1625 reply_cm->proto = rcu_dereference(inet_protos[IPPROTO_IPV6]);
1626 rcu_read_unlock();
1627 }
1628
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08001629#ifdef CONFIG_NF_FLOW_COOKIE
1630 reply_cm->flow_cookie = 0;
1631#endif
Zhi Chen8748eb32015-06-18 12:58:48 -07001632#ifdef CONFIG_XFRM
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301633 if (msg->valid_flags & SFE_RULE_CREATE_DIRECTION_VALID) {
1634 reply_cm->flow_accel = msg->direction_rule.return_accel;
1635 } else {
1636 reply_cm->flow_accel = 1;
1637 }
1638
Zhi Chen8748eb32015-06-18 12:58:48 -07001639#endif
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301640 /*
1641 * If l2_features are disabled and flow uses l2 features such as macvlan/bridge/pppoe/vlan,
1642 * bottom interfaces are expected to be disabled in the flow rule and always top interfaces
1643 * are used. In such cases, do not use HW csum offload. csum offload is used only when we
1644 * are sending directly to the destination interface that supports it.
1645 */
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301646 if (likely(src_dev->features & NETIF_F_HW_CSUM) && sfe_dev_has_hw_csum(src_dev)) {
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301647 if ((msg->conn_rule.flow_top_interface_num == msg->conn_rule.flow_interface_num) ||
1648 (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_FLOW_BOTTOM_INTERFACE)) {
Ratheesh Kannoth48445532022-02-07 16:19:00 +05301649 /*
1650 * Dont enable CSUM offload
1651 */
1652#if 0
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301653 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_CSUM_OFFLOAD;
Ratheesh Kannoth48445532022-02-07 16:19:00 +05301654#endif
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +05301655 }
1656 }
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001657
1658 /*
Ken Zhubbf49652021-09-12 15:33:09 -07001659 * For the non-arp interface, we don't write L2 HDR.
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001660 */
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +05301661 if (sfe_ipv4_xmit_eth_type_check(src_dev, reply_cm->flags)) {
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301662
1663 /*
1664 * Check whether the rule has configured a specific source MAC address to use.
1665 * This is needed when virtual L3 interfaces such as br-lan, macvlan, vlan are used during egress
1666 */
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301667
1668 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) {
1669 ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)msg->conn_rule.return_mac);
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301670 } else {
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301671 if ((msg->valid_flags & SFE_RULE_CREATE_SRC_MAC_VALID) &&
1672 (msg->src_mac_rule.mac_valid_flags & SFE_SRC_MAC_FLOW_VALID)) {
1673 ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)msg->src_mac_rule.flow_src_mac);
1674 } else {
1675 ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)src_dev->dev_addr);
1676 }
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301677 }
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +05301678
Ratheesh Kannoth29140aa2021-10-20 08:25:02 +05301679 ether_addr_copy((u8 *)reply_cm->xmit_dest_mac, (u8 *)msg->conn_rule.flow_mac);
1680
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001681 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_L2_HDR;
Wayne Tanbb7f1782021-12-13 11:16:04 -08001682 reply_cm->l2_hdr_size += ETH_HLEN;
Matthew McClintockdb5ac512014-01-16 17:01:40 -06001683
1684 /*
1685 * If our dev writes Ethernet headers then we can write a really fast
1686 * version.
1687 */
1688 if (src_dev->header_ops) {
1689 if (src_dev->header_ops->create == eth_header) {
1690 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR;
1691 }
1692 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001693 }
1694
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301695 if ((tuple->return_ip != msg->conn_rule.return_ip_xlate) ||
1696 (tuple->return_ident != msg->conn_rule.return_ident_xlate)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001697 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST;
1698 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC;
1699 }
1700
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301701 if ((tuple->flow_ip != msg->conn_rule.flow_ip_xlate) ||
1702 (tuple->flow_ident != msg->conn_rule.flow_ident_xlate)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001703 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC;
1704 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST;
1705 }
1706
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001707 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001708 * Initialize the protocol-specific information that we track.
1709 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301710 switch (tuple->protocol) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001711 case IPPROTO_TCP:
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301712 original_cm->protocol_state.tcp.win_scale = msg->tcp_rule.flow_window_scale;
1713 original_cm->protocol_state.tcp.max_win = msg->tcp_rule.flow_max_window ? msg->tcp_rule.flow_max_window : 1;
1714 original_cm->protocol_state.tcp.end = msg->tcp_rule.flow_end;
1715 original_cm->protocol_state.tcp.max_end = msg->tcp_rule.flow_max_end;
1716
1717 reply_cm->protocol_state.tcp.win_scale = msg->tcp_rule.return_window_scale;
1718 reply_cm->protocol_state.tcp.max_win = msg->tcp_rule.return_max_window ? msg->tcp_rule.return_max_window : 1;
1719 reply_cm->protocol_state.tcp.end = msg->tcp_rule.return_end;
1720 reply_cm->protocol_state.tcp.max_end = msg->tcp_rule.return_max_end;
1721
1722 if (msg->rule_flags & SFE_RULE_CREATE_FLAG_NO_SEQ_CHECK) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001723 original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
1724 reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK;
1725 }
1726 break;
1727 }
1728
Wayne Tanbb7f1782021-12-13 11:16:04 -08001729 /*
1730 * Fill in the ipv4_connection object.
1731 */
1732 c->protocol = tuple->protocol;
1733 c->src_ip = tuple->flow_ip;
1734 c->src_ip_xlate = msg->conn_rule.flow_ip_xlate;
1735 c->src_port = tuple->flow_ident;
1736 c->src_port_xlate = msg->conn_rule.flow_ident_xlate;
1737 c->original_dev = src_dev;
1738 c->original_match = original_cm;
1739 c->dest_ip = tuple->return_ip;
1740 c->dest_ip_xlate = msg->conn_rule.return_ip_xlate;
1741 c->dest_port = tuple->return_ident;
1742 c->dest_port_xlate = msg->conn_rule.return_ident_xlate;
1743 c->reply_dev = dest_dev;
1744 c->reply_match = reply_cm;
1745 c->debug_read_seq = 0;
1746 c->last_sync_jiffies = get_jiffies_64();
1747 c->removed = false;
1748
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001749 sfe_ipv4_connection_match_compute_translations(original_cm);
1750 sfe_ipv4_connection_match_compute_translations(reply_cm);
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301751 sfe_ipv4_insert_connection(si, c);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001752
1753 spin_unlock_bh(&si->lock);
1754
1755 /*
1756 * We have everything we need!
1757 */
Wayne Tanbb7f1782021-12-13 11:16:04 -08001758 DEBUG_INFO("NEW connection - p: %d\n"
1759 "original_cm: match_dev=src_dev: %s %d %pM\n"
1760 " xmit_dev=dest_dev: %s %d %pM\n"
1761 " xmit_src_mac: %pM\n"
1762 " xmit_dest_mac: %pM\n"
1763 " flags: %x l2_hdr: %u\n"
1764 "flow_ip: %pI4:%u\n"
1765 "flow_ip_xlate: %pI4:%u\n"
1766 "flow_mac: %pM\n"
1767 "reply_cm: match_dev=dest_dev: %s %d %pM\n"
1768 " xmit_dev=src_dev: %s %d %pM\n"
1769 " xmit_src_mac: %pM\n"
1770 " xmit_dest_mac: %pM\n"
1771 " flags: %x l2_hdr: %u\n"
1772 "return_ip: %pI4:%u\n"
1773 "return_ip_xlate: %pI4:%u\n"
1774 "return_mac: %pM\n"
1775 "flags: valid=%x src_mac_valid=%x\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301776 tuple->protocol,
Wayne Tanbb7f1782021-12-13 11:16:04 -08001777 original_cm->match_dev->name, original_cm->match_dev->ifindex, original_cm->match_dev->dev_addr,
1778 original_cm->xmit_dev->name, original_cm->xmit_dev->ifindex, original_cm->xmit_dev->dev_addr,
1779 original_cm->xmit_src_mac, original_cm->xmit_dest_mac, original_cm->flags, original_cm->l2_hdr_size,
1780 &tuple->flow_ip, ntohs(tuple->flow_ident),
1781 &msg->conn_rule.flow_ip_xlate, ntohs(msg->conn_rule.flow_ident_xlate),
1782 msg->conn_rule.flow_mac,
1783 reply_cm->match_dev->name, reply_cm->match_dev->ifindex, reply_cm->match_dev->dev_addr,
1784 reply_cm->xmit_dev->name, reply_cm->xmit_dev->ifindex, reply_cm->xmit_dev->dev_addr,
1785 reply_cm->xmit_src_mac, reply_cm->xmit_dest_mac, reply_cm->flags, reply_cm->l2_hdr_size,
1786 &tuple->return_ip, ntohs(tuple->return_ident),
1787 &msg->conn_rule.return_ip_xlate, ntohs(msg->conn_rule.return_ident_xlate),
1788 msg->conn_rule.return_mac,
1789 msg->valid_flags, msg->src_mac_rule.mac_valid_flags);
Nicolas Costa514fde02014-01-13 15:50:29 -06001790
1791 return 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001792}
1793
1794/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001795 * sfe_ipv4_destroy_rule()
1796 * Destroy a forwarding rule.
1797 */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301798void sfe_ipv4_destroy_rule(struct sfe_ipv4_rule_destroy_msg *msg)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001799{
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001800 struct sfe_ipv4 *si = &__si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001801 struct sfe_ipv4_connection *c;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301802 bool ret;
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301803 struct sfe_ipv4_5tuple *tuple = &msg->tuple;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001804
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301805 this_cpu_inc(si->stats_pcpu->connection_destroy_requests64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001806 spin_lock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001807
1808 /*
1809 * Check to see if we have a flow that matches the rule we're trying
1810 * to destroy. If there isn't then we can't destroy it.
1811 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301812 c = sfe_ipv4_find_connection(si, tuple->protocol, tuple->flow_ip, tuple->flow_ident,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301813 tuple->return_ip, tuple->return_ident);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001814 if (!c) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001815 spin_unlock_bh(&si->lock);
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05301816 this_cpu_inc(si->stats_pcpu->connection_destroy_misses64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001817
1818 DEBUG_TRACE("connection does not exist - p: %d, s: %pI4:%u, d: %pI4:%u\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301819 tuple->protocol, &tuple->flow_ip, ntohs(tuple->flow_ident),
1820 &tuple->return_ip, ntohs(tuple->return_ident));
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001821 return;
1822 }
1823
1824 /*
1825 * Remove our connection details from the hash tables.
1826 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301827 ret = sfe_ipv4_remove_connection(si, c);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001828 spin_unlock_bh(&si->lock);
1829
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301830 if (ret) {
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301831 sfe_ipv4_flush_connection(si, c, SFE_SYNC_REASON_DESTROY);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301832 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001833
1834 DEBUG_INFO("connection destroyed - p: %d, s: %pI4:%u, d: %pI4:%u\n",
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05301835 tuple->protocol, &tuple->flow_ip, ntohs(tuple->flow_ident),
1836 &tuple->return_ip, ntohs(tuple->return_ident));
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001837}
1838
1839/*
Ken Zhu7a43d882022-01-04 10:51:44 -08001840 * sfe_ipv4_sync_invoke()
1841 * Schedule many sync stats.
1842 */
1843bool sfe_ipv4_sync_invoke(uint16_t index)
1844{
1845 struct sfe_ipv4 *si = &__si;
1846 DEBUG_INFO("Request for a sync with index[%d]\n", index);
1847 return schedule_delayed_work_on(si->work_cpu, &(si->sync_dwork), 0);
1848}
1849
1850/*
1851 * sfe_ipv4_register_sync_rule_callback()
1852 * Register a callback for many rule synchronization.
1853 */
1854void sfe_ipv4_register_many_sync_callback(sfe_ipv4_many_sync_callback_t cb)
1855{
1856 struct sfe_ipv4 *si = &__si;
1857
1858 spin_lock_bh(&si->lock);
1859 rcu_assign_pointer(si->many_sync_callback, cb);
1860 spin_unlock_bh(&si->lock);
1861}
1862
1863/*
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001864 * sfe_ipv4_register_sync_rule_callback()
1865 * Register a callback for rule synchronization.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001866 */
Xiaoping Fand44a5b42015-05-26 17:37:37 -07001867void sfe_ipv4_register_sync_rule_callback(sfe_sync_rule_callback_t sync_rule_callback)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001868{
1869 struct sfe_ipv4 *si = &__si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001870
1871 spin_lock_bh(&si->lock);
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001872 rcu_assign_pointer(si->sync_rule_callback, sync_rule_callback);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001873 spin_unlock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001874}
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001875/*
1876 * sfe_ipv4_get_debug_dev()
1877 */
1878static ssize_t sfe_ipv4_get_debug_dev(struct device *dev,
1879 struct device_attribute *attr,
1880 char *buf)
1881{
1882 struct sfe_ipv4 *si = &__si;
1883 ssize_t count;
1884 int num;
1885
1886 spin_lock_bh(&si->lock);
1887 num = si->debug_dev;
1888 spin_unlock_bh(&si->lock);
1889
1890 count = snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", num);
1891 return count;
1892}
1893
1894/*
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001895 * sysfs attributes.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001896 */
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001897static const struct device_attribute sfe_ipv4_debug_dev_attr =
Xiaoping Fane70da412016-02-26 16:47:57 -08001898 __ATTR(debug_dev, S_IWUSR | S_IRUGO, sfe_ipv4_get_debug_dev, NULL);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001899
1900/*
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001901 * sfe_ipv4_destroy_all_rules_for_dev()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001902 * Destroy all connections that match a particular device.
1903 *
1904 * If we pass dev as NULL then this destroys all connections.
1905 */
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001906void sfe_ipv4_destroy_all_rules_for_dev(struct net_device *dev)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001907{
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001908 struct sfe_ipv4 *si = &__si;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001909 struct sfe_ipv4_connection *c;
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301910 bool ret;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001911
Xiaoping Fan34586472015-07-03 02:20:35 -07001912another_round:
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001913 spin_lock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001914
Xiaoping Fan34586472015-07-03 02:20:35 -07001915 for (c = si->all_connections_head; c; c = c->all_connections_next) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001916 /*
Xiaoping Fan34586472015-07-03 02:20:35 -07001917 * Does this connection relate to the device we are destroying?
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001918 */
1919 if (!dev
1920 || (dev == c->original_dev)
1921 || (dev == c->reply_dev)) {
Xiaoping Fan34586472015-07-03 02:20:35 -07001922 break;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001923 }
Xiaoping Fan34586472015-07-03 02:20:35 -07001924 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001925
Xiaoping Fan34586472015-07-03 02:20:35 -07001926 if (c) {
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301927 ret = sfe_ipv4_remove_connection(si, c);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001928 }
1929
1930 spin_unlock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07001931
1932 if (c) {
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301933 if (ret) {
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301934 sfe_ipv4_flush_connection(si, c, SFE_SYNC_REASON_DESTROY);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05301935 }
Xiaoping Fan34586472015-07-03 02:20:35 -07001936 goto another_round;
1937 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001938}
1939
1940/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001941 * sfe_ipv4_periodic_sync()
1942 */
Ken Zhu137722d2021-09-23 17:57:36 -07001943static void sfe_ipv4_periodic_sync(struct work_struct *work)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001944{
Ken Zhu137722d2021-09-23 17:57:36 -07001945 struct sfe_ipv4 *si = container_of((struct delayed_work *)work, struct sfe_ipv4, sync_dwork);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07001946 u64 now_jiffies;
Ken Zhu7a43d882022-01-04 10:51:44 -08001947 int quota,count;
1948 sfe_ipv4_many_sync_callback_t sync_rule_callback;
Ken Zhudc423672021-09-02 18:27:01 -07001949 struct sfe_ipv4_connection *c;
Ken Zhu7a43d882022-01-04 10:51:44 -08001950 struct sfe_ipv4_conn_sync *conn_sync;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001951
1952 now_jiffies = get_jiffies_64();
1953
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001954 rcu_read_lock();
Ken Zhu7a43d882022-01-04 10:51:44 -08001955 sync_rule_callback = rcu_dereference(si->many_sync_callback);
1956 rcu_read_unlock();
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001957 if (!sync_rule_callback) {
Ken Zhu7a43d882022-01-04 10:51:44 -08001958 return;
Dave Hudsondcd08fb2013-11-22 09:25:16 -06001959 }
1960
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001961 spin_lock_bh(&si->lock);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001962
1963 /*
Ken Zhudc423672021-09-02 18:27:01 -07001964 * If we have reached the end of the connection list, walk from
1965 * the connection head.
1966 */
1967 c = si->wc_next;
1968 if (unlikely(!c)) {
1969 c = si->all_connections_head;
1970 }
1971
1972 /*
Ken Zhu7a43d882022-01-04 10:51:44 -08001973 * Get the max number of connections to be put in this sync msg.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001974 */
Ken Zhu7a43d882022-01-04 10:51:44 -08001975 quota = sfe_ipv4_sync_max_number;
1976 conn_sync = sfe_ipv4_sync_many_msg->msg.conn_stats_many.conn_sync;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001977
1978 /*
Ken Zhudc423672021-09-02 18:27:01 -07001979 * Walk the "all connection" list and sync the connection state.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001980 */
Ken Zhudc423672021-09-02 18:27:01 -07001981 while (likely(c && quota)) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001982 struct sfe_ipv4_connection_match *cm;
1983 struct sfe_ipv4_connection_match *counter_cm;
Xiaoping Fand44a5b42015-05-26 17:37:37 -07001984 struct sfe_connection_sync sis;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001985
Ken Zhudc423672021-09-02 18:27:01 -07001986 cm = c->original_match;
1987 counter_cm = c->reply_match;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001988
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001989 /*
Ken Zhudc423672021-09-02 18:27:01 -07001990 * Didn't receive packets in the original direction or reply
1991 * direction, move to the next connection.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001992 */
Ken Zhudc423672021-09-02 18:27:01 -07001993 if ((!atomic_read(&cm->rx_packet_count)) && !(atomic_read(&counter_cm->rx_packet_count))) {
1994 c = c->all_connections_next;
1995 continue;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01001996 }
1997
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301998 sfe_ipv4_gen_sync_connection(si, c, &sis, SFE_SYNC_REASON_STATS, now_jiffies);
Ken Zhu7a43d882022-01-04 10:51:44 -08001999 sfe_ipv4_stats_convert(conn_sync, &sis);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002000
Ken Zhu7a43d882022-01-04 10:51:44 -08002001 quota--;
2002 conn_sync++;
2003 c = c->all_connections_next;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002004 }
2005
Ken Zhudc423672021-09-02 18:27:01 -07002006 /*
2007 * At the end of the sync, put the wc_next to the connection we left.
2008 */
2009 si->wc_next = c;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002010 spin_unlock_bh(&si->lock);
2011
Ken Zhu7a43d882022-01-04 10:51:44 -08002012 count = sfe_ipv4_sync_max_number - quota;
2013 /*
2014 * Tell ecm sync round done if at the end of all connection
2015 * otherwise tell the number in the msg.
2016 */
2017 if (c == NULL) {
2018 DEBUG_INFO("Synced all connections.\n");
2019 sfe_ipv4_sync_many_msg->msg.conn_stats_many.next = 0;
2020 } else {
2021 DEBUG_INFO("Some connections left.\n");
2022 sfe_ipv4_sync_many_msg->msg.conn_stats_many.next = count;
2023 }
2024 DEBUG_INFO("Sync %d connections\n", count);
2025 sfe_ipv4_sync_many_msg->msg.conn_stats_many.count = count;
2026 sfe_ipv4_sync_many_msg->cm.response = SFE_CMN_RESPONSE_ACK;
2027
2028 sync_rule_callback(sfe_ipv4_sync_many_msg);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002029}
2030
2031#define CHAR_DEV_MSG_SIZE 768
2032
2033/*
2034 * sfe_ipv4_debug_dev_read_start()
2035 * Generate part of the XML output.
2036 */
2037static bool sfe_ipv4_debug_dev_read_start(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
Ken Zhu7a43d882022-01-04 10:51:44 -08002038 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002039{
2040 int bytes_read;
2041
Xiaoping Fan34586472015-07-03 02:20:35 -07002042 si->debug_read_seq++;
2043
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002044 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "<sfe_ipv4>\n");
2045 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2046 return false;
2047 }
2048
2049 *length -= bytes_read;
2050 *total_read += bytes_read;
2051
2052 ws->state++;
2053 return true;
2054}
2055
2056/*
2057 * sfe_ipv4_debug_dev_read_connections_start()
2058 * Generate part of the XML output.
2059 */
2060static bool sfe_ipv4_debug_dev_read_connections_start(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2061 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2062{
2063 int bytes_read;
2064
2065 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<connections>\n");
2066 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2067 return false;
2068 }
2069
2070 *length -= bytes_read;
2071 *total_read += bytes_read;
2072
2073 ws->state++;
2074 return true;
2075}
2076
2077/*
2078 * sfe_ipv4_debug_dev_read_connections_connection()
2079 * Generate part of the XML output.
2080 */
2081static bool sfe_ipv4_debug_dev_read_connections_connection(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2082 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2083{
2084 struct sfe_ipv4_connection *c;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002085 struct sfe_ipv4_connection_match *original_cm;
2086 struct sfe_ipv4_connection_match *reply_cm;
2087 int bytes_read;
2088 int protocol;
2089 struct net_device *src_dev;
Dave Hudson87973cd2013-10-22 16:00:04 +01002090 __be32 src_ip;
2091 __be32 src_ip_xlate;
2092 __be16 src_port;
2093 __be16 src_port_xlate;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07002094 u64 src_rx_packets;
2095 u64 src_rx_bytes;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002096 struct net_device *dest_dev;
Dave Hudson87973cd2013-10-22 16:00:04 +01002097 __be32 dest_ip;
2098 __be32 dest_ip_xlate;
2099 __be16 dest_port;
2100 __be16 dest_port_xlate;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07002101 u64 dest_rx_packets;
2102 u64 dest_rx_bytes;
2103 u64 last_sync_jiffies;
Ken Zhu37040ea2021-09-09 21:11:15 -07002104 u32 src_mark, dest_mark, src_priority, dest_priority, src_dscp, dest_dscp;
Parikshit Guned31a8202022-01-05 22:15:04 +05302105 bool original_cm_sawf_valid, reply_cm_sawf_valid;
2106 u32 flow_service_class, return_service_class;
2107 u32 flow_msduq, return_msduq;
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05302108 u32 packet, byte, original_cm_flags;
2109 u16 pppoe_session_id;
2110 u8 pppoe_remote_mac[ETH_ALEN];
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002111 u32 original_fast_xmit, reply_fast_xmit;
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08002112#ifdef CONFIG_NF_FLOW_COOKIE
2113 int src_flow_cookie, dst_flow_cookie;
2114#endif
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002115
2116 spin_lock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07002117
2118 for (c = si->all_connections_head; c; c = c->all_connections_next) {
2119 if (c->debug_read_seq < si->debug_read_seq) {
2120 c->debug_read_seq = si->debug_read_seq;
2121 break;
2122 }
2123 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002124
2125 /*
Xiaoping Fan34586472015-07-03 02:20:35 -07002126 * If there were no connections then move to the next state.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002127 */
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05302128 if (!c || c->removed) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002129 spin_unlock_bh(&si->lock);
Xiaoping Fan34586472015-07-03 02:20:35 -07002130 ws->state++;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002131 return true;
2132 }
2133
2134 original_cm = c->original_match;
2135 reply_cm = c->reply_match;
2136
2137 protocol = c->protocol;
2138 src_dev = c->original_dev;
2139 src_ip = c->src_ip;
2140 src_ip_xlate = c->src_ip_xlate;
2141 src_port = c->src_port;
2142 src_port_xlate = c->src_port_xlate;
Xiaoping Fane1963d42015-08-25 17:06:19 -07002143 src_priority = original_cm->priority;
2144 src_dscp = original_cm->dscp >> SFE_IPV4_DSCP_SHIFT;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002145
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05302146 sfe_ipv4_connection_match_update_summary_stats(original_cm, &packet, &byte);
2147 sfe_ipv4_connection_match_update_summary_stats(reply_cm, &packet, &byte);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002148
2149 src_rx_packets = original_cm->rx_packet_count64;
2150 src_rx_bytes = original_cm->rx_byte_count64;
Ken Zhu37040ea2021-09-09 21:11:15 -07002151 src_mark = original_cm->mark;
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002152 original_fast_xmit = (original_cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_FAST_XMIT);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002153 dest_dev = c->reply_dev;
2154 dest_ip = c->dest_ip;
2155 dest_ip_xlate = c->dest_ip_xlate;
2156 dest_port = c->dest_port;
2157 dest_port_xlate = c->dest_port_xlate;
Xiaoping Fane1963d42015-08-25 17:06:19 -07002158 dest_priority = reply_cm->priority;
2159 dest_dscp = reply_cm->dscp >> SFE_IPV4_DSCP_SHIFT;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002160 dest_rx_packets = reply_cm->rx_packet_count64;
2161 dest_rx_bytes = reply_cm->rx_byte_count64;
Ken Zhu37040ea2021-09-09 21:11:15 -07002162 dest_mark = reply_cm->mark;
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002163 reply_fast_xmit = (reply_cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_FAST_XMIT);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002164 last_sync_jiffies = get_jiffies_64() - c->last_sync_jiffies;
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05302165 original_cm_flags = original_cm->flags;
2166 pppoe_session_id = original_cm->pppoe_session_id;
2167 ether_addr_copy(pppoe_remote_mac, original_cm->pppoe_remote_mac);
Parikshit Guned31a8202022-01-05 22:15:04 +05302168 original_cm_sawf_valid = original_cm->sawf_valid;
2169 reply_cm_sawf_valid = reply_cm->sawf_valid;
2170 flow_service_class = SFE_GET_SAWF_SERVICE_CLASS(original_cm->mark);
2171 flow_msduq = SFE_GET_SAWF_MSDUQ(original_cm->mark);
2172 return_service_class = SFE_GET_SAWF_SERVICE_CLASS(reply_cm->mark);
2173 return_msduq = SFE_GET_SAWF_MSDUQ(reply_cm->mark);
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08002174#ifdef CONFIG_NF_FLOW_COOKIE
2175 src_flow_cookie = original_cm->flow_cookie;
2176 dst_flow_cookie = reply_cm->flow_cookie;
2177#endif
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002178 spin_unlock_bh(&si->lock);
2179
2180 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t\t<connection "
2181 "protocol=\"%u\" "
2182 "src_dev=\"%s\" "
2183 "src_ip=\"%pI4\" src_ip_xlate=\"%pI4\" "
2184 "src_port=\"%u\" src_port_xlate=\"%u\" "
Xiaoping Fane1963d42015-08-25 17:06:19 -07002185 "src_priority=\"%u\" src_dscp=\"%u\" "
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002186 "src_rx_pkts=\"%llu\" src_rx_bytes=\"%llu\" "
Ken Zhu37040ea2021-09-09 21:11:15 -07002187 "src_mark=\"%08x\" "
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002188 "src_fast_xmit=\"%s\" "
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002189 "dest_dev=\"%s\" "
2190 "dest_ip=\"%pI4\" dest_ip_xlate=\"%pI4\" "
2191 "dest_port=\"%u\" dest_port_xlate=\"%u\" "
Xiaoping Fane1963d42015-08-25 17:06:19 -07002192 "dest_priority=\"%u\" dest_dscp=\"%u\" "
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002193 "dest_rx_pkts=\"%llu\" dest_rx_bytes=\"%llu\" "
Ken Zhu37040ea2021-09-09 21:11:15 -07002194 "dest_mark=\"%08x\" "
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002195 "reply_fast_xmit=\"%s\" "
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08002196#ifdef CONFIG_NF_FLOW_COOKIE
2197 "src_flow_cookie=\"%d\" dst_flow_cookie=\"%d\" "
2198#endif
Ken Zhu37040ea2021-09-09 21:11:15 -07002199 "last_sync=\"%llu\" ",
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002200 protocol,
2201 src_dev->name,
2202 &src_ip, &src_ip_xlate,
Dave Hudson87973cd2013-10-22 16:00:04 +01002203 ntohs(src_port), ntohs(src_port_xlate),
Xiaoping Fane1963d42015-08-25 17:06:19 -07002204 src_priority, src_dscp,
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002205 src_rx_packets, src_rx_bytes,
Ken Zhu37040ea2021-09-09 21:11:15 -07002206 src_mark,
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002207 original_fast_xmit ? "Yes" : "No",
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002208 dest_dev->name,
2209 &dest_ip, &dest_ip_xlate,
Dave Hudson87973cd2013-10-22 16:00:04 +01002210 ntohs(dest_port), ntohs(dest_port_xlate),
Xiaoping Fane1963d42015-08-25 17:06:19 -07002211 dest_priority, dest_dscp,
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002212 dest_rx_packets, dest_rx_bytes,
Ken Zhu37040ea2021-09-09 21:11:15 -07002213 dest_mark,
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002214 reply_fast_xmit ? "Yes" : "No",
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08002215#ifdef CONFIG_NF_FLOW_COOKIE
2216 src_flow_cookie, dst_flow_cookie,
2217#endif
Ken Zhu37040ea2021-09-09 21:11:15 -07002218 last_sync_jiffies);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002219
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05302220 if (original_cm_flags &= (SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_DECAP | SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_ENCAP)) {
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +05302221 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 +05302222 pppoe_session_id, pppoe_remote_mac);
2223 }
2224
Parikshit Guned31a8202022-01-05 22:15:04 +05302225 if (original_cm_sawf_valid) {
Parikshit Gunefdd98652022-03-14 17:33:01 +05302226 bytes_read += snprintf(msg + bytes_read, CHAR_DEV_MSG_SIZE, "flow_service_class=\"%d\" flow_msduq = \"0x%x\" ",
Parikshit Guned31a8202022-01-05 22:15:04 +05302227 flow_service_class, flow_msduq);
2228 }
2229
2230 if (reply_cm_sawf_valid) {
Parikshit Gunefdd98652022-03-14 17:33:01 +05302231 bytes_read += snprintf(msg + bytes_read, CHAR_DEV_MSG_SIZE, "return_service_class=\"%d\" return_msduq = \"0x%x\" ",
Parikshit Guned31a8202022-01-05 22:15:04 +05302232 return_service_class, return_msduq);
2233 }
2234
Guduri Prathyushaeb31c902021-11-10 20:18:50 +05302235 bytes_read += snprintf(msg + bytes_read, CHAR_DEV_MSG_SIZE, "/>\n");
2236
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002237 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2238 return false;
2239 }
2240
2241 *length -= bytes_read;
2242 *total_read += bytes_read;
2243
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002244 return true;
2245}
2246
2247/*
2248 * sfe_ipv4_debug_dev_read_connections_end()
2249 * Generate part of the XML output.
2250 */
2251static bool sfe_ipv4_debug_dev_read_connections_end(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2252 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2253{
2254 int bytes_read;
2255
2256 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t</connections>\n");
2257 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2258 return false;
2259 }
2260
2261 *length -= bytes_read;
2262 *total_read += bytes_read;
2263
2264 ws->state++;
2265 return true;
2266}
2267
2268/*
2269 * sfe_ipv4_debug_dev_read_exceptions_start()
2270 * Generate part of the XML output.
2271 */
2272static bool sfe_ipv4_debug_dev_read_exceptions_start(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2273 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2274{
2275 int bytes_read;
2276
2277 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<exceptions>\n");
2278 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2279 return false;
2280 }
2281
2282 *length -= bytes_read;
2283 *total_read += bytes_read;
2284
2285 ws->state++;
2286 return true;
2287}
2288
2289/*
2290 * sfe_ipv4_debug_dev_read_exceptions_exception()
2291 * Generate part of the XML output.
2292 */
2293static bool sfe_ipv4_debug_dev_read_exceptions_exception(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2294 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2295{
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302296 int i;
2297 u64 val = 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002298
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302299 for_each_possible_cpu(i) {
2300 const struct sfe_ipv4_stats *s = per_cpu_ptr(si->stats_pcpu, i);
2301 val += s->exception_events64[ws->iter_exception];
2302 }
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002303
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302304 if (val) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002305 int bytes_read;
2306
2307 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE,
2308 "\t\t<exception name=\"%s\" count=\"%llu\" />\n",
2309 sfe_ipv4_exception_events_string[ws->iter_exception],
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302310 val);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002311 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2312 return false;
2313 }
2314
2315 *length -= bytes_read;
2316 *total_read += bytes_read;
2317 }
2318
2319 ws->iter_exception++;
2320 if (ws->iter_exception >= SFE_IPV4_EXCEPTION_EVENT_LAST) {
2321 ws->iter_exception = 0;
2322 ws->state++;
2323 }
2324
2325 return true;
2326}
2327
2328/*
2329 * sfe_ipv4_debug_dev_read_exceptions_end()
2330 * Generate part of the XML output.
2331 */
2332static bool sfe_ipv4_debug_dev_read_exceptions_end(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2333 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2334{
2335 int bytes_read;
2336
2337 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t</exceptions>\n");
2338 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2339 return false;
2340 }
2341
2342 *length -= bytes_read;
2343 *total_read += bytes_read;
2344
2345 ws->state++;
2346 return true;
2347}
2348
2349/*
2350 * sfe_ipv4_debug_dev_read_stats()
2351 * Generate part of the XML output.
2352 */
2353static bool sfe_ipv4_debug_dev_read_stats(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2354 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2355{
2356 int bytes_read;
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302357 struct sfe_ipv4_stats stats;
2358 unsigned int num_conn;
2359
2360 sfe_ipv4_update_summary_stats(si, &stats);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002361
2362 spin_lock_bh(&si->lock);
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302363 num_conn = si->num_connections;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002364 spin_unlock_bh(&si->lock);
2365
2366 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<stats "
2367 "num_connections=\"%u\" "
Amitesh Anand63be37d2021-12-24 20:51:48 +05302368 "pkts_dropped=\"%llu\" "
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002369 "pkts_fast_xmited=\"%llu\" "
Xiaoping Fan59176422015-05-22 15:58:10 -07002370 "pkts_forwarded=\"%llu\" pkts_not_forwarded=\"%llu\" "
2371 "create_requests=\"%llu\" create_collisions=\"%llu\" "
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05302372 "create_failures=\"%llu\" "
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002373 "destroy_requests=\"%llu\" destroy_misses=\"%llu\" "
2374 "flushes=\"%llu\" "
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +05302375 "hash_hits=\"%llu\" hash_reorders=\"%llu\" "
2376 "pppoe_encap_pkts_fwded=\"%llu\" "
Guduri Prathyusha034d6352022-01-12 16:49:04 +05302377 "pppoe_decap_pkts_fwded=\"%llu\" "
2378 "pppoe_bridge_pkts_fwded=\"%llu\" />\n",
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302379 num_conn,
Amitesh Anand63be37d2021-12-24 20:51:48 +05302380 stats.packets_dropped64,
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002381 stats.packets_fast_xmited64,
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302382 stats.packets_forwarded64,
2383 stats.packets_not_forwarded64,
2384 stats.connection_create_requests64,
2385 stats.connection_create_collisions64,
Ratheesh Kannoth89302a72021-10-20 08:10:37 +05302386 stats.connection_create_failures64,
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302387 stats.connection_destroy_requests64,
2388 stats.connection_destroy_misses64,
2389 stats.connection_flushes64,
2390 stats.connection_match_hash_hits64,
Guduri Prathyusha647fe3e2021-11-22 19:17:51 +05302391 stats.connection_match_hash_reorders64,
2392 stats.pppoe_encap_packets_forwarded64,
Guduri Prathyusha034d6352022-01-12 16:49:04 +05302393 stats.pppoe_decap_packets_forwarded64,
2394 stats.pppoe_bridge_packets_forwarded64);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002395 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2396 return false;
2397 }
2398
2399 *length -= bytes_read;
2400 *total_read += bytes_read;
2401
2402 ws->state++;
2403 return true;
2404}
2405
2406/*
2407 * sfe_ipv4_debug_dev_read_end()
2408 * Generate part of the XML output.
2409 */
2410static bool sfe_ipv4_debug_dev_read_end(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
2411 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws)
2412{
2413 int bytes_read;
2414
2415 bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "</sfe_ipv4>\n");
2416 if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) {
2417 return false;
2418 }
2419
2420 *length -= bytes_read;
2421 *total_read += bytes_read;
2422
2423 ws->state++;
2424 return true;
2425}
2426
2427/*
2428 * Array of write functions that write various XML elements that correspond to
2429 * our XML output state machine.
2430 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -07002431static sfe_ipv4_debug_xml_write_method_t sfe_ipv4_debug_xml_write_methods[SFE_IPV4_DEBUG_XML_STATE_DONE] = {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002432 sfe_ipv4_debug_dev_read_start,
2433 sfe_ipv4_debug_dev_read_connections_start,
2434 sfe_ipv4_debug_dev_read_connections_connection,
2435 sfe_ipv4_debug_dev_read_connections_end,
2436 sfe_ipv4_debug_dev_read_exceptions_start,
2437 sfe_ipv4_debug_dev_read_exceptions_exception,
2438 sfe_ipv4_debug_dev_read_exceptions_end,
2439 sfe_ipv4_debug_dev_read_stats,
2440 sfe_ipv4_debug_dev_read_end,
2441};
2442
2443/*
2444 * sfe_ipv4_debug_dev_read()
2445 * Send info to userspace upon read request from user
2446 */
2447static ssize_t sfe_ipv4_debug_dev_read(struct file *filp, char *buffer, size_t length, loff_t *offset)
2448{
2449 char msg[CHAR_DEV_MSG_SIZE];
2450 int total_read = 0;
2451 struct sfe_ipv4_debug_xml_write_state *ws;
2452 struct sfe_ipv4 *si = &__si;
2453
2454 ws = (struct sfe_ipv4_debug_xml_write_state *)filp->private_data;
2455 while ((ws->state != SFE_IPV4_DEBUG_XML_STATE_DONE) && (length > CHAR_DEV_MSG_SIZE)) {
2456 if ((sfe_ipv4_debug_xml_write_methods[ws->state])(si, buffer, msg, &length, &total_read, ws)) {
2457 continue;
2458 }
2459 }
2460
2461 return total_read;
2462}
2463
2464/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002465 * sfe_ipv4_debug_dev_open()
2466 */
2467static int sfe_ipv4_debug_dev_open(struct inode *inode, struct file *file)
2468{
2469 struct sfe_ipv4_debug_xml_write_state *ws;
2470
2471 ws = (struct sfe_ipv4_debug_xml_write_state *)file->private_data;
2472 if (!ws) {
2473 ws = kzalloc(sizeof(struct sfe_ipv4_debug_xml_write_state), GFP_KERNEL);
2474 if (!ws) {
2475 return -ENOMEM;
2476 }
2477
2478 ws->state = SFE_IPV4_DEBUG_XML_STATE_START;
2479 file->private_data = ws;
2480 }
2481
2482 return 0;
2483}
2484
2485/*
2486 * sfe_ipv4_debug_dev_release()
2487 */
2488static int sfe_ipv4_debug_dev_release(struct inode *inode, struct file *file)
2489{
2490 struct sfe_ipv4_debug_xml_write_state *ws;
2491
2492 ws = (struct sfe_ipv4_debug_xml_write_state *)file->private_data;
2493 if (ws) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002494 /*
2495 * We've finished with our output so free the write state.
2496 */
2497 kfree(ws);
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05302498 file->private_data = NULL;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002499 }
2500
2501 return 0;
2502}
2503
2504/*
2505 * File operations used in the debug char device
2506 */
2507static struct file_operations sfe_ipv4_debug_dev_fops = {
2508 .read = sfe_ipv4_debug_dev_read,
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002509 .open = sfe_ipv4_debug_dev_open,
2510 .release = sfe_ipv4_debug_dev_release
2511};
2512
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08002513#ifdef CONFIG_NF_FLOW_COOKIE
2514/*
2515 * sfe_register_flow_cookie_cb
2516 * register a function in SFE to let SFE use this function to configure flow cookie for a flow
2517 *
2518 * Hardware driver which support flow cookie should register a callback function in SFE. Then SFE
2519 * can use this function to configure flow cookie for a flow.
2520 * return: 0, success; !=0, fail
2521 */
2522int sfe_register_flow_cookie_cb(flow_cookie_set_func_t cb)
2523{
2524 struct sfe_ipv4 *si = &__si;
2525
2526 BUG_ON(!cb);
2527
2528 if (si->flow_cookie_set_func) {
2529 return -1;
2530 }
2531
2532 rcu_assign_pointer(si->flow_cookie_set_func, cb);
2533 return 0;
2534}
2535
2536/*
2537 * sfe_unregister_flow_cookie_cb
2538 * unregister function which is used to configure flow cookie for a flow
2539 *
2540 * return: 0, success; !=0, fail
2541 */
2542int sfe_unregister_flow_cookie_cb(flow_cookie_set_func_t cb)
2543{
2544 struct sfe_ipv4 *si = &__si;
2545
2546 RCU_INIT_POINTER(si->flow_cookie_set_func, NULL);
2547 return 0;
2548}
Xiaoping Fan640faf42015-08-28 15:50:55 -07002549
2550/*
2551 * sfe_ipv4_get_flow_cookie()
2552 */
2553static ssize_t sfe_ipv4_get_flow_cookie(struct device *dev,
2554 struct device_attribute *attr,
2555 char *buf)
2556{
2557 struct sfe_ipv4 *si = &__si;
Xiaoping Fan01c67cc2015-11-09 11:31:57 -08002558 return snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", si->flow_cookie_enable);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002559}
2560
2561/*
2562 * sfe_ipv4_set_flow_cookie()
2563 */
2564static ssize_t sfe_ipv4_set_flow_cookie(struct device *dev,
2565 struct device_attribute *attr,
2566 const char *buf, size_t size)
2567{
2568 struct sfe_ipv4 *si = &__si;
Ken Zhu137722d2021-09-23 17:57:36 -07002569 si->flow_cookie_enable = simple_strtol(buf, NULL, 0);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002570
2571 return size;
2572}
2573
2574/*
2575 * sysfs attributes.
2576 */
2577static const struct device_attribute sfe_ipv4_flow_cookie_attr =
Xiaoping Fane70da412016-02-26 16:47:57 -08002578 __ATTR(flow_cookie_enable, S_IWUSR | S_IRUGO, sfe_ipv4_get_flow_cookie, sfe_ipv4_set_flow_cookie);
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08002579#endif /*CONFIG_NF_FLOW_COOKIE*/
2580
Ken Zhu137722d2021-09-23 17:57:36 -07002581/*
2582 * sfe_ipv4_get_cpu()
2583 */
2584static ssize_t sfe_ipv4_get_cpu(struct device *dev,
2585 struct device_attribute *attr,
2586 char *buf)
2587{
2588 struct sfe_ipv4 *si = &__si;
2589 return snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", si->work_cpu);
2590}
2591
2592/*
2593 * sfe_ipv4_set_cpu()
2594 */
2595static ssize_t sfe_ipv4_set_cpu(struct device *dev,
2596 struct device_attribute *attr,
2597 const char *buf, size_t size)
2598{
2599 struct sfe_ipv4 *si = &__si;
2600 int work_cpu;
2601 work_cpu = simple_strtol(buf, NULL, 0);
2602 if ((work_cpu >= 0) && (work_cpu <= NR_CPUS)) {
2603 si->work_cpu = work_cpu;
2604 } else {
2605 dev_err(dev, "%s is not in valid range[0,%d]", buf, NR_CPUS);
2606 }
2607 return size;
2608}
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002609
Ken Zhu137722d2021-09-23 17:57:36 -07002610/*
2611 * sysfs attributes.
2612 */
2613static const struct device_attribute sfe_ipv4_cpu_attr =
2614 __ATTR(stats_work_cpu, S_IWUSR | S_IRUGO, sfe_ipv4_get_cpu, sfe_ipv4_set_cpu);
2615
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002616/*
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05302617 * sfe_ipv4_conn_match_hash_init()
2618 * Initialize conn match hash lists
2619 */
2620static void sfe_ipv4_conn_match_hash_init(struct sfe_ipv4 *si, int len)
2621{
2622 struct hlist_head *hash_list = si->hlist_conn_match_hash_head;
2623 int i;
2624
2625 for (i = 0; i < len; i++) {
2626 INIT_HLIST_HEAD(&hash_list[i]);
2627 }
2628}
2629
Amitesh Anand63be37d2021-12-24 20:51:48 +05302630#ifdef SFE_PROCESS_LOCAL_OUT
2631/*
2632 * sfe_ipv4_local_out()
2633 * Called for packets from ip_local_out() - post encapsulation & other packets
2634 */
2635static unsigned int sfe_ipv4_local_out(void *priv, struct sk_buff *skb, const struct nf_hook_state *nhs)
2636{
Nitin Shettyc28f8172022-02-04 16:23:46 +05302637 struct sfe_l2_info l2_info = {0};
Guduri Prathyusha5f27e232022-01-06 14:39:04 +05302638
Amitesh Anand63be37d2021-12-24 20:51:48 +05302639 DEBUG_TRACE("%px: sfe: sfe_ipv4_local_out hook called.\n", skb);
2640
2641 if (likely(skb->skb_iif)) {
Guduri Prathyusha5f27e232022-01-06 14:39:04 +05302642 return sfe_ipv4_recv(skb->dev, skb, &l2_info, true) ? NF_STOLEN : NF_ACCEPT;
Amitesh Anand63be37d2021-12-24 20:51:48 +05302643 }
2644
2645 return NF_ACCEPT;
2646}
2647
2648/*
2649 * struct nf_hook_ops sfe_ipv4_ops_local_out[]
2650 * Hooks into netfilter local out packet monitoring points.
2651 */
2652static struct nf_hook_ops sfe_ipv4_ops_local_out[] __read_mostly = {
2653
2654 /*
2655 * Local out routing hook is used to monitor packets.
2656 */
2657 {
2658 .hook = sfe_ipv4_local_out,
2659 .pf = PF_INET,
2660 .hooknum = NF_INET_LOCAL_OUT,
2661 .priority = NF_IP_PRI_FIRST,
2662 },
2663};
2664#endif
2665
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002666/*
Dave Hudson87973cd2013-10-22 16:00:04 +01002667 * sfe_ipv4_init()
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002668 */
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05302669int sfe_ipv4_init(void)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002670{
2671 struct sfe_ipv4 *si = &__si;
2672 int result = -1;
2673
Dave Hudsondcd08fb2013-11-22 09:25:16 -06002674 DEBUG_INFO("SFE IPv4 init\n");
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002675
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +05302676 sfe_ipv4_conn_match_hash_init(si, ARRAY_SIZE(si->hlist_conn_match_hash_head));
2677
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302678 si->stats_pcpu = alloc_percpu_gfp(struct sfe_ipv4_stats, GFP_KERNEL | __GFP_ZERO);
2679 if (!si->stats_pcpu) {
2680 DEBUG_ERROR("failed to allocate stats memory for sfe_ipv4\n");
2681 goto exit0;
2682 }
2683
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002684 /*
Parikshit Guned31a8202022-01-05 22:15:04 +05302685 * Allocate per cpu per service class memory.
2686 */
2687 si->stats_pcpu_psc = alloc_percpu_gfp(struct sfe_ipv4_service_class_stats_db,
2688 GFP_KERNEL | __GFP_ZERO);
2689 if (!si->stats_pcpu_psc) {
2690 DEBUG_ERROR("failed to allocate per cpu per service clas stats memory\n");
2691 goto exit1;
2692 }
2693
2694 /*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002695 * Create sys/sfe_ipv4
2696 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302697 si->sys_ipv4 = kobject_create_and_add("sfe_ipv4", NULL);
2698 if (!si->sys_ipv4) {
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002699 DEBUG_ERROR("failed to register sfe_ipv4\n");
Parikshit Guned31a8202022-01-05 22:15:04 +05302700 goto exit2;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002701 }
2702
2703 /*
2704 * Create files, one for each parameter supported by this module.
2705 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302706 result = sysfs_create_file(si->sys_ipv4, &sfe_ipv4_debug_dev_attr.attr);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002707 if (result) {
2708 DEBUG_ERROR("failed to register debug dev file: %d\n", result);
Parikshit Guned31a8202022-01-05 22:15:04 +05302709 goto exit3;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002710 }
2711
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302712 result = sysfs_create_file(si->sys_ipv4, &sfe_ipv4_cpu_attr.attr);
Ken Zhu137722d2021-09-23 17:57:36 -07002713 if (result) {
2714 DEBUG_ERROR("failed to register debug dev file: %d\n", result);
Parikshit Guned31a8202022-01-05 22:15:04 +05302715 goto exit4;
Ken Zhu137722d2021-09-23 17:57:36 -07002716 }
2717
Xiaoping Fan640faf42015-08-28 15:50:55 -07002718#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302719 result = sysfs_create_file(si->sys_ipv4, &sfe_ipv4_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002720 if (result) {
2721 DEBUG_ERROR("failed to register flow cookie enable file: %d\n", result);
Parikshit Guned31a8202022-01-05 22:15:04 +05302722 goto exit5;
Xiaoping Fan640faf42015-08-28 15:50:55 -07002723 }
2724#endif /* CONFIG_NF_FLOW_COOKIE */
2725
Amitesh Anand63be37d2021-12-24 20:51:48 +05302726#ifdef SFE_PROCESS_LOCAL_OUT
2727#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
2728 result = nf_register_hooks(sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2729#else
2730 result = nf_register_net_hooks(&init_net, sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2731#endif
2732 if (result < 0) {
2733 DEBUG_ERROR("can't register nf local out hook: %d\n", result);
Parikshit Guned31a8202022-01-05 22:15:04 +05302734 goto exit6;
Amitesh Anand63be37d2021-12-24 20:51:48 +05302735 }
2736 DEBUG_INFO("Register nf local out hook success: %d\n", result);
2737#endif
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002738 /*
2739 * Register our debug char device.
2740 */
2741 result = register_chrdev(0, "sfe_ipv4", &sfe_ipv4_debug_dev_fops);
2742 if (result < 0) {
2743 DEBUG_ERROR("Failed to register chrdev: %d\n", result);
Parikshit Guned31a8202022-01-05 22:15:04 +05302744 goto exit7;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002745 }
2746
2747 si->debug_dev = result;
Ken Zhu137722d2021-09-23 17:57:36 -07002748 si->work_cpu = WORK_CPU_UNBOUND;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002749
2750 /*
Ken Zhu7a43d882022-01-04 10:51:44 -08002751 * Create a work to handle pull message from ecm.
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002752 */
Ken Zhu137722d2021-09-23 17:57:36 -07002753 INIT_DELAYED_WORK(&(si->sync_dwork), sfe_ipv4_periodic_sync);
Ken Zhu7a43d882022-01-04 10:51:44 -08002754 /*
2755 * Allocate a message for stats sync many
2756 */
2757 sfe_ipv4_sync_many_msg = kzalloc(PAGE_SIZE, GFP_KERNEL);
2758 if(!sfe_ipv4_sync_many_msg) {
2759 goto exit8;
2760 }
2761
2762 sfe_ipv4_msg_init(sfe_ipv4_sync_many_msg, SFE_SPECIAL_INTERFACE_IPV4,
2763 SFE_TX_CONN_STATS_SYNC_MANY_MSG,
2764 sizeof(struct sfe_ipv4_conn_sync_many_msg),
2765 NULL,
2766 NULL);
2767 sfe_ipv4_sync_max_number = (PAGE_SIZE - sizeof(struct sfe_ipv4_msg)) / sizeof(struct sfe_ipv4_conn_sync);
Ken Zhu137722d2021-09-23 17:57:36 -07002768
Dave Hudson87973cd2013-10-22 16:00:04 +01002769 spin_lock_init(&si->lock);
Dave Hudson87973cd2013-10-22 16:00:04 +01002770 return 0;
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002771
Ken Zhu7a43d882022-01-04 10:51:44 -08002772exit8:
2773 unregister_chrdev(si->debug_dev, "sfe_ipv4");
2774
Parikshit Guned31a8202022-01-05 22:15:04 +05302775exit7:
Amitesh Anand63be37d2021-12-24 20:51:48 +05302776#ifdef SFE_PROCESS_LOCAL_OUT
2777 DEBUG_TRACE("sfe: Unregister local out hook\n");
2778#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
2779 nf_unregister_hooks(sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2780#else
2781 nf_unregister_net_hooks(&init_net, sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2782#endif
Parikshit Guned31a8202022-01-05 22:15:04 +05302783exit6:
Amitesh Anand63be37d2021-12-24 20:51:48 +05302784#endif
Xiaoping Fan640faf42015-08-28 15:50:55 -07002785#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302786 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002787
Parikshit Guned31a8202022-01-05 22:15:04 +05302788exit5:
Xiaoping Fan640faf42015-08-28 15:50:55 -07002789#endif /* CONFIG_NF_FLOW_COOKIE */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302790 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_cpu_attr.attr);
Parikshit Guned31a8202022-01-05 22:15:04 +05302791exit4:
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302792 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_debug_dev_attr.attr);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002793
Parikshit Guned31a8202022-01-05 22:15:04 +05302794exit3:
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302795 kobject_put(si->sys_ipv4);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002796
Parikshit Guned31a8202022-01-05 22:15:04 +05302797exit2:
2798 free_percpu(si->stats_pcpu_psc);
2799
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002800exit1:
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302801 free_percpu(si->stats_pcpu);
2802
2803exit0:
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002804 return result;
2805}
2806
2807/*
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002808 * sfe_ipv4_exit()
2809 */
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05302810void sfe_ipv4_exit(void)
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002811{
Dave Hudson87973cd2013-10-22 16:00:04 +01002812 struct sfe_ipv4 *si = &__si;
2813
Dave Hudsondcd08fb2013-11-22 09:25:16 -06002814 DEBUG_INFO("SFE IPv4 exit\n");
Dave Hudson87973cd2013-10-22 16:00:04 +01002815 /*
2816 * Destroy all connections.
2817 */
Dave Hudsondcd08fb2013-11-22 09:25:16 -06002818 sfe_ipv4_destroy_all_rules_for_dev(NULL);
Dave Hudson87973cd2013-10-22 16:00:04 +01002819
Ken Zhu137722d2021-09-23 17:57:36 -07002820 cancel_delayed_work_sync(&si->sync_dwork);
Dave Hudson87973cd2013-10-22 16:00:04 +01002821
Dave Hudson87973cd2013-10-22 16:00:04 +01002822 unregister_chrdev(si->debug_dev, "sfe_ipv4");
2823
Amitesh Anand63be37d2021-12-24 20:51:48 +05302824#ifdef SFE_PROCESS_LOCAL_OUT
2825 DEBUG_TRACE("sfe: Unregister local out hook\n");
2826#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
2827 nf_unregister_hooks(sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2828#else
2829 nf_unregister_net_hooks(&init_net, sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out));
2830#endif
2831#endif
2832
Xiaoping Fan640faf42015-08-28 15:50:55 -07002833#ifdef CONFIG_NF_FLOW_COOKIE
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302834 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_flow_cookie_attr.attr);
Xiaoping Fan640faf42015-08-28 15:50:55 -07002835#endif /* CONFIG_NF_FLOW_COOKIE */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302836 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_debug_dev_attr.attr);
Ken Zhu7e38d1a2021-11-30 17:31:46 -08002837
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302838 sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_cpu_attr.attr);
Dave Hudson87973cd2013-10-22 16:00:04 +01002839
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05302840 kobject_put(si->sys_ipv4);
Dave Hudson87973cd2013-10-22 16:00:04 +01002841
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +05302842 free_percpu(si->stats_pcpu);
Parikshit Guned31a8202022-01-05 22:15:04 +05302843 free_percpu(si->stats_pcpu_psc);
Dave Hudsonaaf97ca2013-06-13 17:52:29 +01002844}
2845
Xiaoping Fand1dc7b22015-01-23 00:43:56 -08002846#ifdef CONFIG_NF_FLOW_COOKIE
2847EXPORT_SYMBOL(sfe_register_flow_cookie_cb);
2848EXPORT_SYMBOL(sfe_unregister_flow_cookie_cb);
2849#endif