Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | * sfe_ipv6.c |
| 3 | * Shortcut forwarding engine - IPv6 support. |
| 4 | * |
Ratheesh Kannoth | 24fb1db | 2021-10-20 07:28:06 +0530 | [diff] [blame] | 5 | * Copyright (c) 2015-2016, 2019-2020, The Linux Foundation. All rights reserved. |
Guduri Prathyusha | 5f27e23 | 2022-01-06 14:39:04 +0530 | [diff] [blame] | 6 | * Copyright (c) 2021-2022 Qualcomm Innovation Center, Inc. All rights reserved. |
Ratheesh Kannoth | 24fb1db | 2021-10-20 07:28:06 +0530 | [diff] [blame] | 7 | * |
| 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 Fan | a42c68b | 2015-08-07 18:00:39 -0700 | [diff] [blame] | 12 | * 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 Kannoth | 24fb1db | 2021-10-20 07:28:06 +0530 | [diff] [blame] | 17 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 18 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/sysfs.h> |
| 23 | #include <linux/skbuff.h> |
| 24 | #include <linux/icmp.h> |
| 25 | #include <net/tcp.h> |
| 26 | #include <linux/etherdevice.h> |
Tian Yang | 45f39c8 | 2020-10-06 14:07:47 -0700 | [diff] [blame] | 27 | #include <linux/version.h> |
Suruchi Suman | 23a279d | 2021-11-16 15:13:09 +0530 | [diff] [blame] | 28 | #include <net/udp.h> |
| 29 | #include <net/vxlan.h> |
| 30 | #include <linux/refcount.h> |
| 31 | #include <linux/netfilter.h> |
| 32 | #include <linux/inetdevice.h> |
| 33 | #include <linux/netfilter_ipv6.h> |
Parikshit Gune | d31a820 | 2022-01-05 22:15:04 +0530 | [diff] [blame] | 34 | #include <linux/seqlock.h> |
Tian Yang | afb0345 | 2022-01-13 18:53:13 -0800 | [diff] [blame] | 35 | #include <net/protocol.h> |
Nitin Shetty | e6ed5b5 | 2021-12-27 14:50:11 +0530 | [diff] [blame] | 36 | #include <net/addrconf.h> |
| 37 | #include <net/gre.h> |
| 38 | |
Ratheesh Kannoth | 24fb1db | 2021-10-20 07:28:06 +0530 | [diff] [blame] | 39 | #include "sfe_debug.h" |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 40 | #include "sfe_api.h" |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 41 | #include "sfe.h" |
Ratheesh Kannoth | 24fb1db | 2021-10-20 07:28:06 +0530 | [diff] [blame] | 42 | #include "sfe_flow_cookie.h" |
| 43 | #include "sfe_ipv6.h" |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 44 | #include "sfe_ipv6_udp.h" |
| 45 | #include "sfe_ipv6_tcp.h" |
| 46 | #include "sfe_ipv6_icmp.h" |
Wayne Tan | bb7f178 | 2021-12-13 11:16:04 -0800 | [diff] [blame] | 47 | #include "sfe_pppoe.h" |
Tian Yang | afb0345 | 2022-01-13 18:53:13 -0800 | [diff] [blame] | 48 | #include "sfe_ipv6_tunipip6.h" |
Nitin Shetty | e6ed5b5 | 2021-12-27 14:50:11 +0530 | [diff] [blame] | 49 | #include "sfe_ipv6_gre.h" |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 50 | |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 51 | #define sfe_ipv6_addr_copy(src, dest) memcpy((void *)(dest), (void *)(src), 16) |
| 52 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 53 | static char *sfe_ipv6_exception_events_string[SFE_IPV6_EXCEPTION_EVENT_LAST] = { |
| 54 | "UDP_HEADER_INCOMPLETE", |
| 55 | "UDP_NO_CONNECTION", |
| 56 | "UDP_IP_OPTIONS_OR_INITIAL_FRAGMENT", |
| 57 | "UDP_SMALL_TTL", |
| 58 | "UDP_NEEDS_FRAGMENTATION", |
| 59 | "TCP_HEADER_INCOMPLETE", |
| 60 | "TCP_NO_CONNECTION_SLOW_FLAGS", |
| 61 | "TCP_NO_CONNECTION_FAST_FLAGS", |
| 62 | "TCP_IP_OPTIONS_OR_INITIAL_FRAGMENT", |
| 63 | "TCP_SMALL_TTL", |
| 64 | "TCP_NEEDS_FRAGMENTATION", |
| 65 | "TCP_FLAGS", |
| 66 | "TCP_SEQ_EXCEEDS_RIGHT_EDGE", |
| 67 | "TCP_SMALL_DATA_OFFS", |
| 68 | "TCP_BAD_SACK", |
| 69 | "TCP_BIG_DATA_OFFS", |
| 70 | "TCP_SEQ_BEFORE_LEFT_EDGE", |
| 71 | "TCP_ACK_EXCEEDS_RIGHT_EDGE", |
| 72 | "TCP_ACK_BEFORE_LEFT_EDGE", |
| 73 | "ICMP_HEADER_INCOMPLETE", |
| 74 | "ICMP_UNHANDLED_TYPE", |
| 75 | "ICMP_IPV6_HEADER_INCOMPLETE", |
| 76 | "ICMP_IPV6_NON_V6", |
| 77 | "ICMP_IPV6_IP_OPTIONS_INCOMPLETE", |
| 78 | "ICMP_IPV6_UDP_HEADER_INCOMPLETE", |
| 79 | "ICMP_IPV6_TCP_HEADER_INCOMPLETE", |
| 80 | "ICMP_IPV6_UNHANDLED_PROTOCOL", |
| 81 | "ICMP_NO_CONNECTION", |
| 82 | "ICMP_FLUSHED_CONNECTION", |
| 83 | "HEADER_INCOMPLETE", |
| 84 | "BAD_TOTAL_LENGTH", |
| 85 | "NON_V6", |
| 86 | "NON_INITIAL_FRAGMENT", |
| 87 | "DATAGRAM_INCOMPLETE", |
| 88 | "IP_OPTIONS_INCOMPLETE", |
| 89 | "UNHANDLED_PROTOCOL", |
Ratheesh Kannoth | 5dee377 | 2022-01-18 11:27:14 +0530 | [diff] [blame] | 90 | "FLOW_COOKIE_ADD_FAIL", |
Nitin Shetty | 16ab38d | 2022-02-09 01:26:19 +0530 | [diff] [blame] | 91 | "NO_HEADROOM", |
| 92 | "INVALID_PPPOE_SESSION", |
| 93 | "INCORRECT_PPPOE_PARSING", |
| 94 | "PPPOE_NOT_SET_IN_CME", |
| 95 | "INGRESS_VLAN_TAG_MISMATCH", |
Ratheesh Kannoth | 5dee377 | 2022-01-18 11:27:14 +0530 | [diff] [blame] | 96 | "INVALID_SOURCE_INTERFACE", |
Tian Yang | afb0345 | 2022-01-13 18:53:13 -0800 | [diff] [blame] | 97 | "TUNIPIP6_HEADER_INCOMPLETE", |
| 98 | "TUNIPIP6_NO_CONNECTION", |
| 99 | "TUNIPIP6_IP_OPTIONS_OR_INITIAL_FRAGMENT", |
| 100 | "TUNIPIP6_SMALL_TTL", |
| 101 | "TUNIPIP6_NEEDS_FRAGMENTATION", |
Nitin Shetty | 16ab38d | 2022-02-09 01:26:19 +0530 | [diff] [blame] | 102 | "TUNIPIP6_SYNC_ON_FIND", |
Nitin Shetty | e6ed5b5 | 2021-12-27 14:50:11 +0530 | [diff] [blame] | 103 | "GRE_HEADER_INCOMPLETE", |
| 104 | "GRE_NO_CONNECTION", |
| 105 | "GRE_IP_OPTIONS_OR_INITIAL_FRAGMENT", |
| 106 | "GRE_SMALL_TTL", |
| 107 | "GRE_NEEDS_FRAGMENTATION" |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 108 | }; |
| 109 | |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 110 | static struct sfe_ipv6 __si6; |
Ken Zhu | 7a43d88 | 2022-01-04 10:51:44 -0800 | [diff] [blame] | 111 | struct sfe_ipv6_msg *sfe_ipv6_sync_many_msg; |
| 112 | uint32_t sfe_ipv6_sync_max_number; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 113 | |
| 114 | /* |
| 115 | * sfe_ipv6_get_debug_dev() |
| 116 | */ |
| 117 | static ssize_t sfe_ipv6_get_debug_dev(struct device *dev, struct device_attribute *attr, char *buf); |
| 118 | |
| 119 | /* |
| 120 | * sysfs attributes. |
| 121 | */ |
| 122 | static const struct device_attribute sfe_ipv6_debug_dev_attr = |
Xiaoping Fan | e70da41 | 2016-02-26 16:47:57 -0800 | [diff] [blame] | 123 | __ATTR(debug_dev, S_IWUSR | S_IRUGO, sfe_ipv6_get_debug_dev, NULL); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 124 | |
| 125 | /* |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 126 | * sfe_ipv6_get_connection_match_hash() |
| 127 | * Generate the hash used in connection match lookups. |
| 128 | */ |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 129 | static inline unsigned int sfe_ipv6_get_connection_match_hash(struct net_device *dev, u8 protocol, |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 130 | struct sfe_ipv6_addr *src_ip, __be16 src_port, |
| 131 | struct sfe_ipv6_addr *dest_ip, __be16 dest_port) |
| 132 | { |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 133 | u32 idx, hash = 0; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 134 | |
| 135 | for (idx = 0; idx < 4; idx++) { |
| 136 | hash ^= src_ip->addr[idx] ^ dest_ip->addr[idx]; |
| 137 | } |
Ratheesh Kannoth | 5dee377 | 2022-01-18 11:27:14 +0530 | [diff] [blame] | 138 | hash = hash ^ protocol ^ ntohs(src_port ^ dest_port); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 139 | return ((hash >> SFE_IPV6_CONNECTION_HASH_SHIFT) ^ hash) & SFE_IPV6_CONNECTION_HASH_MASK; |
| 140 | } |
| 141 | |
| 142 | /* |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 143 | * sfe_ipv6_find_connection_match_rcu() |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 144 | * Get the IPv6 flow match info that corresponds to a particular 5-tuple. |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 145 | */ |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 146 | struct sfe_ipv6_connection_match * |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 147 | sfe_ipv6_find_connection_match_rcu(struct sfe_ipv6 *si, struct net_device *dev, u8 protocol, |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 148 | struct sfe_ipv6_addr *src_ip, __be16 src_port, |
| 149 | struct sfe_ipv6_addr *dest_ip, __be16 dest_port) |
| 150 | { |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 151 | struct sfe_ipv6_connection_match *cm = NULL; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 152 | unsigned int conn_match_idx; |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 153 | struct hlist_head *lhead; |
| 154 | WARN_ON_ONCE(!rcu_read_lock_held()); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 155 | |
| 156 | conn_match_idx = sfe_ipv6_get_connection_match_hash(dev, protocol, src_ip, src_port, dest_ip, dest_port); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 157 | |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 158 | lhead = &si->hlist_conn_match_hash_head[conn_match_idx]; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 159 | |
| 160 | /* |
| 161 | * Hopefully the first entry is the one we want. |
| 162 | */ |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 163 | hlist_for_each_entry_rcu(cm, lhead, hnode) { |
| 164 | if ((cm->match_dest_port != dest_port) || |
Wayne Tan | 5e0abb5 | 2022-06-14 12:49:38 -0700 | [diff] [blame] | 165 | (cm->match_src_port != src_port) || |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 166 | (!sfe_ipv6_addr_equal(cm->match_src_ip, src_ip)) || |
| 167 | (!sfe_ipv6_addr_equal(cm->match_dest_ip, dest_ip)) || |
Wayne Tan | fcbcfee | 2022-06-21 22:40:48 -0700 | [diff] [blame^] | 168 | (cm->match_protocol != protocol)) { |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 169 | continue; |
| 170 | } |
| 171 | |
Ratheesh Kannoth | 1ed9546 | 2021-10-20 07:57:45 +0530 | [diff] [blame] | 172 | this_cpu_inc(si->stats_pcpu->connection_match_hash_hits64); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 173 | |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 174 | break; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 175 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 178 | return cm; |
| 179 | } |
| 180 | |
| 181 | /* |
| 182 | * sfe_ipv6_connection_match_update_summary_stats() |
| 183 | * Update the summary stats for a connection match entry. |
| 184 | */ |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 185 | static inline void sfe_ipv6_connection_match_update_summary_stats(struct sfe_ipv6_connection_match *cm, |
| 186 | u32 *packets, u32 *bytes) |
| 187 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 188 | { |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 189 | u32 packet_count, byte_count; |
| 190 | |
| 191 | packet_count = atomic_read(&cm->rx_packet_count); |
| 192 | cm->rx_packet_count64 += packet_count; |
| 193 | atomic_sub(packet_count, &cm->rx_packet_count); |
| 194 | |
| 195 | byte_count = atomic_read(&cm->rx_byte_count); |
| 196 | cm->rx_byte_count64 += byte_count; |
| 197 | atomic_sub(byte_count, &cm->rx_byte_count); |
| 198 | |
| 199 | *packets = packet_count; |
| 200 | *bytes = byte_count; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | /* |
| 204 | * sfe_ipv6_connection_match_compute_translations() |
| 205 | * Compute port and address translations for a connection match entry. |
| 206 | */ |
| 207 | static void sfe_ipv6_connection_match_compute_translations(struct sfe_ipv6_connection_match *cm) |
| 208 | { |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 209 | u32 diff[9]; |
| 210 | u32 *idx_32; |
| 211 | u16 *idx_16; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 212 | |
| 213 | /* |
| 214 | * Before we insert the entry look to see if this is tagged as doing address |
| 215 | * translations. If it is then work out the adjustment that we need to apply |
| 216 | * to the transport checksum. |
| 217 | */ |
| 218 | if (cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_XLATE_SRC) { |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 219 | u32 adj = 0; |
| 220 | u32 carry = 0; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 221 | |
| 222 | /* |
| 223 | * Precompute an incremental checksum adjustment so we can |
| 224 | * edit packets in this stream very quickly. The algorithm is from RFC1624. |
| 225 | */ |
| 226 | idx_32 = diff; |
Ratheesh Kannoth | 741f799 | 2021-10-20 07:39:52 +0530 | [diff] [blame] | 227 | *(idx_32++) = cm->match_src_ip[0].addr[0]; |
| 228 | *(idx_32++) = cm->match_src_ip[0].addr[1]; |
| 229 | *(idx_32++) = cm->match_src_ip[0].addr[2]; |
| 230 | *(idx_32++) = cm->match_src_ip[0].addr[3]; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 231 | |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 232 | idx_16 = (u16 *)idx_32; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 233 | *(idx_16++) = cm->match_src_port; |
| 234 | *(idx_16++) = ~cm->xlate_src_port; |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 235 | idx_32 = (u32 *)idx_16; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 236 | |
Ratheesh Kannoth | 741f799 | 2021-10-20 07:39:52 +0530 | [diff] [blame] | 237 | *(idx_32++) = ~cm->xlate_src_ip[0].addr[0]; |
| 238 | *(idx_32++) = ~cm->xlate_src_ip[0].addr[1]; |
| 239 | *(idx_32++) = ~cm->xlate_src_ip[0].addr[2]; |
| 240 | *(idx_32++) = ~cm->xlate_src_ip[0].addr[3]; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 241 | |
| 242 | /* |
| 243 | * When we compute this fold it down to a 16-bit offset |
| 244 | * as that way we can avoid having to do a double |
| 245 | * folding of the twos-complement result because the |
| 246 | * addition of 2 16-bit values cannot cause a double |
| 247 | * wrap-around! |
| 248 | */ |
| 249 | for (idx_32 = diff; idx_32 < diff + 9; idx_32++) { |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 250 | u32 w = *idx_32; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 251 | adj += carry; |
| 252 | adj += w; |
| 253 | carry = (w > adj); |
| 254 | } |
| 255 | adj += carry; |
| 256 | adj = (adj & 0xffff) + (adj >> 16); |
| 257 | adj = (adj & 0xffff) + (adj >> 16); |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 258 | cm->xlate_src_csum_adjustment = (u16)adj; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | if (cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_XLATE_DEST) { |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 262 | u32 adj = 0; |
| 263 | u32 carry = 0; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 264 | |
| 265 | /* |
| 266 | * Precompute an incremental checksum adjustment so we can |
| 267 | * edit packets in this stream very quickly. The algorithm is from RFC1624. |
| 268 | */ |
| 269 | idx_32 = diff; |
Ratheesh Kannoth | 741f799 | 2021-10-20 07:39:52 +0530 | [diff] [blame] | 270 | *(idx_32++) = cm->match_dest_ip[0].addr[0]; |
| 271 | *(idx_32++) = cm->match_dest_ip[0].addr[1]; |
| 272 | *(idx_32++) = cm->match_dest_ip[0].addr[2]; |
| 273 | *(idx_32++) = cm->match_dest_ip[0].addr[3]; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 274 | |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 275 | idx_16 = (u16 *)idx_32; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 276 | *(idx_16++) = cm->match_dest_port; |
| 277 | *(idx_16++) = ~cm->xlate_dest_port; |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 278 | idx_32 = (u32 *)idx_16; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 279 | |
Ratheesh Kannoth | 741f799 | 2021-10-20 07:39:52 +0530 | [diff] [blame] | 280 | *(idx_32++) = ~cm->xlate_dest_ip[0].addr[0]; |
| 281 | *(idx_32++) = ~cm->xlate_dest_ip[0].addr[1]; |
| 282 | *(idx_32++) = ~cm->xlate_dest_ip[0].addr[2]; |
| 283 | *(idx_32++) = ~cm->xlate_dest_ip[0].addr[3]; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 284 | |
| 285 | /* |
| 286 | * When we compute this fold it down to a 16-bit offset |
| 287 | * as that way we can avoid having to do a double |
| 288 | * folding of the twos-complement result because the |
| 289 | * addition of 2 16-bit values cannot cause a double |
| 290 | * wrap-around! |
| 291 | */ |
| 292 | for (idx_32 = diff; idx_32 < diff + 9; idx_32++) { |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 293 | u32 w = *idx_32; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 294 | adj += carry; |
| 295 | adj += w; |
| 296 | carry = (w > adj); |
| 297 | } |
| 298 | adj += carry; |
| 299 | adj = (adj & 0xffff) + (adj >> 16); |
| 300 | adj = (adj & 0xffff) + (adj >> 16); |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 301 | cm->xlate_dest_csum_adjustment = (u16)adj; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | |
| 305 | /* |
| 306 | * sfe_ipv6_update_summary_stats() |
| 307 | * Update the summary stats. |
| 308 | */ |
Ratheesh Kannoth | 1ed9546 | 2021-10-20 07:57:45 +0530 | [diff] [blame] | 309 | static void sfe_ipv6_update_summary_stats(struct sfe_ipv6 *si, struct sfe_ipv6_stats *stats) |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 310 | { |
Ratheesh Kannoth | 1ed9546 | 2021-10-20 07:57:45 +0530 | [diff] [blame] | 311 | int i = 0; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 312 | |
Ratheesh Kannoth | 1ed9546 | 2021-10-20 07:57:45 +0530 | [diff] [blame] | 313 | memset(stats, 0, sizeof(*stats)); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 314 | |
Ratheesh Kannoth | 1ed9546 | 2021-10-20 07:57:45 +0530 | [diff] [blame] | 315 | for_each_possible_cpu(i) { |
| 316 | const struct sfe_ipv6_stats *s = per_cpu_ptr(si->stats_pcpu, i); |
| 317 | |
| 318 | stats->connection_create_requests64 += s->connection_create_requests64; |
| 319 | stats->connection_create_collisions64 += s->connection_create_collisions64; |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 320 | stats->connection_create_failures64 += s->connection_create_failures64; |
Ratheesh Kannoth | 1ed9546 | 2021-10-20 07:57:45 +0530 | [diff] [blame] | 321 | stats->connection_destroy_requests64 += s->connection_destroy_requests64; |
| 322 | stats->connection_destroy_misses64 += s->connection_destroy_misses64; |
| 323 | stats->connection_match_hash_hits64 += s->connection_match_hash_hits64; |
| 324 | stats->connection_match_hash_reorders64 += s->connection_match_hash_reorders64; |
| 325 | stats->connection_flushes64 += s->connection_flushes64; |
Suruchi Suman | 23a279d | 2021-11-16 15:13:09 +0530 | [diff] [blame] | 326 | stats->packets_dropped64 += s->packets_dropped64; |
Ratheesh Kannoth | 1ed9546 | 2021-10-20 07:57:45 +0530 | [diff] [blame] | 327 | stats->packets_forwarded64 += s->packets_forwarded64; |
Ken Zhu | 7e38d1a | 2021-11-30 17:31:46 -0800 | [diff] [blame] | 328 | stats->packets_fast_xmited64 += s->packets_fast_xmited64; |
Ratheesh Kannoth | 1ed9546 | 2021-10-20 07:57:45 +0530 | [diff] [blame] | 329 | stats->packets_not_forwarded64 += s->packets_not_forwarded64; |
Guduri Prathyusha | 647fe3e | 2021-11-22 19:17:51 +0530 | [diff] [blame] | 330 | stats->pppoe_encap_packets_forwarded64 += s->pppoe_encap_packets_forwarded64; |
| 331 | stats->pppoe_decap_packets_forwarded64 += s->pppoe_decap_packets_forwarded64; |
Guduri Prathyusha | 034d635 | 2022-01-12 16:49:04 +0530 | [diff] [blame] | 332 | stats->pppoe_bridge_packets_forwarded64 += s->pppoe_bridge_packets_forwarded64; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | |
| 336 | /* |
| 337 | * sfe_ipv6_insert_connection_match() |
| 338 | * Insert a connection match into the hash. |
| 339 | * |
| 340 | * On entry we must be holding the lock that protects the hash table. |
| 341 | */ |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 342 | static inline void sfe_ipv6_insert_connection_match(struct sfe_ipv6 *si, |
| 343 | struct sfe_ipv6_connection_match *cm) |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 344 | { |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 345 | unsigned int conn_match_idx |
| 346 | = sfe_ipv6_get_connection_match_hash(cm->match_dev, cm->match_protocol, |
| 347 | cm->match_src_ip, cm->match_src_port, |
| 348 | cm->match_dest_ip, cm->match_dest_port); |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 349 | |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 350 | lockdep_assert_held(&si->lock); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 351 | |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 352 | hlist_add_head_rcu(&cm->hnode, &si->hlist_conn_match_hash_head[conn_match_idx]); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 353 | #ifdef CONFIG_NF_FLOW_COOKIE |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 354 | if (!si->flow_cookie_enable || !(cm->flags & (SFE_IPV6_CONNECTION_MATCH_FLAG_XLATE_SRC | SFE_IPV6_CONNECTION_MATCH_FLAG_XLATE_DEST))) |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 355 | return; |
| 356 | |
| 357 | /* |
| 358 | * Configure hardware to put a flow cookie in packet of this flow, |
| 359 | * then we can accelerate the lookup process when we received this packet. |
| 360 | */ |
| 361 | for (conn_match_idx = 1; conn_match_idx < SFE_FLOW_COOKIE_SIZE; conn_match_idx++) { |
| 362 | struct sfe_ipv6_flow_cookie_entry *entry = &si->sfe_flow_cookie_table[conn_match_idx]; |
| 363 | |
| 364 | if ((NULL == entry->match) && time_is_before_jiffies(entry->last_clean_time + HZ)) { |
| 365 | sfe_ipv6_flow_cookie_set_func_t func; |
| 366 | |
| 367 | rcu_read_lock(); |
| 368 | func = rcu_dereference(si->flow_cookie_set_func); |
| 369 | if (func) { |
| 370 | if (!func(cm->match_protocol, cm->match_src_ip->addr, cm->match_src_port, |
| 371 | cm->match_dest_ip->addr, cm->match_dest_port, conn_match_idx)) { |
| 372 | entry->match = cm; |
| 373 | cm->flow_cookie = conn_match_idx; |
| 374 | } else { |
| 375 | si->exception_events[SFE_IPV6_EXCEPTION_EVENT_FLOW_COOKIE_ADD_FAIL]++; |
| 376 | } |
| 377 | } |
| 378 | rcu_read_unlock(); |
| 379 | |
| 380 | break; |
| 381 | } |
| 382 | } |
| 383 | #endif |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | /* |
| 387 | * sfe_ipv6_remove_connection_match() |
| 388 | * Remove a connection match object from the hash. |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 389 | */ |
| 390 | static inline void sfe_ipv6_remove_connection_match(struct sfe_ipv6 *si, struct sfe_ipv6_connection_match *cm) |
| 391 | { |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 392 | |
| 393 | lockdep_assert_held(&si->lock); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 394 | #ifdef CONFIG_NF_FLOW_COOKIE |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 395 | if (si->flow_cookie_enable) { |
| 396 | /* |
| 397 | * Tell hardware that we no longer need a flow cookie in packet of this flow |
| 398 | */ |
| 399 | unsigned int conn_match_idx; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 400 | |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 401 | for (conn_match_idx = 1; conn_match_idx < SFE_FLOW_COOKIE_SIZE; conn_match_idx++) { |
| 402 | struct sfe_ipv6_flow_cookie_entry *entry = &si->sfe_flow_cookie_table[conn_match_idx]; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 403 | |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 404 | if (cm == entry->match) { |
| 405 | sfe_ipv6_flow_cookie_set_func_t func; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 406 | |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 407 | rcu_read_lock(); |
| 408 | func = rcu_dereference(si->flow_cookie_set_func); |
| 409 | if (func) { |
| 410 | func(cm->match_protocol, cm->match_src_ip->addr, cm->match_src_port, |
| 411 | cm->match_dest_ip->addr, cm->match_dest_port, 0); |
| 412 | } |
| 413 | rcu_read_unlock(); |
| 414 | |
| 415 | cm->flow_cookie = 0; |
| 416 | entry->match = NULL; |
| 417 | entry->last_clean_time = jiffies; |
| 418 | break; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 419 | } |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 420 | } |
| 421 | } |
| 422 | #endif |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 423 | hlist_del_init_rcu(&cm->hnode); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 424 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | /* |
| 428 | * sfe_ipv6_get_connection_hash() |
| 429 | * Generate the hash used in connection lookups. |
| 430 | */ |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 431 | static inline unsigned int sfe_ipv6_get_connection_hash(u8 protocol, struct sfe_ipv6_addr *src_ip, __be16 src_port, |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 432 | struct sfe_ipv6_addr *dest_ip, __be16 dest_port) |
| 433 | { |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 434 | u32 idx, hash = 0; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 435 | |
| 436 | for (idx = 0; idx < 4; idx++) { |
| 437 | hash ^= src_ip->addr[idx] ^ dest_ip->addr[idx]; |
| 438 | } |
| 439 | hash = hash ^ protocol ^ ntohs(src_port ^ dest_port); |
| 440 | return ((hash >> SFE_IPV6_CONNECTION_HASH_SHIFT) ^ hash) & SFE_IPV6_CONNECTION_HASH_MASK; |
| 441 | } |
| 442 | |
| 443 | /* |
| 444 | * sfe_ipv6_find_connection() |
| 445 | * Get the IPv6 connection info that corresponds to a particular 5-tuple. |
| 446 | * |
| 447 | * On entry we must be holding the lock that protects the hash table. |
| 448 | */ |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 449 | static inline struct sfe_ipv6_connection *sfe_ipv6_find_connection(struct sfe_ipv6 *si, u32 protocol, |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 450 | struct sfe_ipv6_addr *src_ip, __be16 src_port, |
| 451 | struct sfe_ipv6_addr *dest_ip, __be16 dest_port) |
| 452 | { |
| 453 | struct sfe_ipv6_connection *c; |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 454 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 455 | unsigned int conn_idx = sfe_ipv6_get_connection_hash(protocol, src_ip, src_port, dest_ip, dest_port); |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 456 | |
| 457 | lockdep_assert_held(&si->lock); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 458 | c = si->conn_hash[conn_idx]; |
| 459 | |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 460 | while (c) { |
| 461 | if ((c->src_port == src_port) |
| 462 | && (c->dest_port == dest_port) |
| 463 | && (sfe_ipv6_addr_equal(c->src_ip, src_ip)) |
| 464 | && (sfe_ipv6_addr_equal(c->dest_ip, dest_ip)) |
| 465 | && (c->protocol == protocol)) { |
| 466 | return c; |
| 467 | } |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 468 | c = c->next; |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 469 | } |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 470 | |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 471 | return NULL; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | /* |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 475 | * sfe_ipv6_insert_connection() |
| 476 | * Insert a connection into the hash. |
| 477 | * |
| 478 | * On entry we must be holding the lock that protects the hash table. |
| 479 | */ |
| 480 | static void sfe_ipv6_insert_connection(struct sfe_ipv6 *si, struct sfe_ipv6_connection *c) |
| 481 | { |
| 482 | struct sfe_ipv6_connection **hash_head; |
| 483 | struct sfe_ipv6_connection *prev_head; |
| 484 | unsigned int conn_idx; |
| 485 | |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 486 | lockdep_assert_held(&si->lock); |
| 487 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 488 | /* |
| 489 | * Insert entry into the connection hash. |
| 490 | */ |
| 491 | conn_idx = sfe_ipv6_get_connection_hash(c->protocol, c->src_ip, c->src_port, |
| 492 | c->dest_ip, c->dest_port); |
| 493 | hash_head = &si->conn_hash[conn_idx]; |
| 494 | prev_head = *hash_head; |
| 495 | c->prev = NULL; |
| 496 | if (prev_head) { |
| 497 | prev_head->prev = c; |
| 498 | } |
| 499 | |
| 500 | c->next = prev_head; |
| 501 | *hash_head = c; |
| 502 | |
| 503 | /* |
| 504 | * Insert entry into the "all connections" list. |
| 505 | */ |
| 506 | if (si->all_connections_tail) { |
| 507 | c->all_connections_prev = si->all_connections_tail; |
| 508 | si->all_connections_tail->all_connections_next = c; |
| 509 | } else { |
| 510 | c->all_connections_prev = NULL; |
| 511 | si->all_connections_head = c; |
| 512 | } |
| 513 | |
| 514 | si->all_connections_tail = c; |
| 515 | c->all_connections_next = NULL; |
| 516 | si->num_connections++; |
| 517 | |
| 518 | /* |
| 519 | * Insert the connection match objects too. |
| 520 | */ |
| 521 | sfe_ipv6_insert_connection_match(si, c->original_match); |
| 522 | sfe_ipv6_insert_connection_match(si, c->reply_match); |
| 523 | } |
| 524 | |
| 525 | /* |
| 526 | * sfe_ipv6_remove_connection() |
| 527 | * Remove a sfe_ipv6_connection object from the hash. |
| 528 | * |
| 529 | * On entry we must be holding the lock that protects the hash table. |
| 530 | */ |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 531 | bool sfe_ipv6_remove_connection(struct sfe_ipv6 *si, struct sfe_ipv6_connection *c) |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 532 | { |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 533 | |
| 534 | lockdep_assert_held(&si->lock); |
| 535 | if (c->removed) { |
| 536 | DEBUG_ERROR("%px: Connection has been removed already\n", c); |
| 537 | return false; |
| 538 | } |
| 539 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 540 | /* |
Tian Yang | 435afc4 | 2022-02-02 12:47:32 -0800 | [diff] [blame] | 541 | * dereference the decap direction top_interface_dev |
| 542 | */ |
| 543 | if (c->reply_match->top_interface_dev) { |
| 544 | dev_put(c->reply_match->top_interface_dev); |
| 545 | } |
| 546 | /* |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 547 | * Remove the connection match objects. |
| 548 | */ |
| 549 | sfe_ipv6_remove_connection_match(si, c->reply_match); |
| 550 | sfe_ipv6_remove_connection_match(si, c->original_match); |
| 551 | |
| 552 | /* |
| 553 | * Unlink the connection. |
| 554 | */ |
| 555 | if (c->prev) { |
| 556 | c->prev->next = c->next; |
| 557 | } else { |
| 558 | unsigned int conn_idx = sfe_ipv6_get_connection_hash(c->protocol, c->src_ip, c->src_port, |
| 559 | c->dest_ip, c->dest_port); |
| 560 | si->conn_hash[conn_idx] = c->next; |
| 561 | } |
| 562 | |
| 563 | if (c->next) { |
| 564 | c->next->prev = c->prev; |
| 565 | } |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 566 | |
| 567 | /* |
| 568 | * Unlink connection from all_connections list |
| 569 | */ |
| 570 | if (c->all_connections_prev) { |
| 571 | c->all_connections_prev->all_connections_next = c->all_connections_next; |
| 572 | } else { |
| 573 | si->all_connections_head = c->all_connections_next; |
| 574 | } |
| 575 | |
| 576 | if (c->all_connections_next) { |
| 577 | c->all_connections_next->all_connections_prev = c->all_connections_prev; |
| 578 | } else { |
| 579 | si->all_connections_tail = c->all_connections_prev; |
| 580 | } |
| 581 | |
Ken Zhu | 32b9539 | 2021-09-03 13:52:04 -0700 | [diff] [blame] | 582 | /* |
| 583 | * If I am the next sync connection, move the sync to my next or head. |
| 584 | */ |
| 585 | if (unlikely(si->wc_next == c)) { |
| 586 | si->wc_next = c->all_connections_next; |
| 587 | } |
| 588 | |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 589 | c->removed = true; |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 590 | si->num_connections--; |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 591 | return true; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | /* |
| 595 | * sfe_ipv6_gen_sync_connection() |
| 596 | * Sync a connection. |
| 597 | * |
| 598 | * On entry to this function we expect that the lock for the connection is either |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 599 | * already held (while called from sfe_ipv6_periodic_sync() or isn't required |
| 600 | * (while called from sfe_ipv6_flush_sfe_ipv6_connection()) |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 601 | */ |
| 602 | static void sfe_ipv6_gen_sync_connection(struct sfe_ipv6 *si, struct sfe_ipv6_connection *c, |
Xiaoping Fan | 99cb4c1 | 2015-08-21 19:07:32 -0700 | [diff] [blame] | 603 | struct sfe_connection_sync *sis, sfe_sync_reason_t reason, |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 604 | u64 now_jiffies) |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 605 | { |
| 606 | struct sfe_ipv6_connection_match *original_cm; |
| 607 | struct sfe_ipv6_connection_match *reply_cm; |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 608 | u32 packet_count, byte_count; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 609 | |
| 610 | /* |
| 611 | * Fill in the update message. |
| 612 | */ |
Murat Sezgin | 53509a1 | 2016-12-27 16:57:34 -0800 | [diff] [blame] | 613 | sis->is_v6 = 1; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 614 | sis->protocol = c->protocol; |
| 615 | sis->src_ip.ip6[0] = c->src_ip[0]; |
Xiaoping Fan | 99cb4c1 | 2015-08-21 19:07:32 -0700 | [diff] [blame] | 616 | sis->src_ip_xlate.ip6[0] = c->src_ip_xlate[0]; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 617 | sis->dest_ip.ip6[0] = c->dest_ip[0]; |
Xiaoping Fan | 99cb4c1 | 2015-08-21 19:07:32 -0700 | [diff] [blame] | 618 | sis->dest_ip_xlate.ip6[0] = c->dest_ip_xlate[0]; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 619 | sis->src_port = c->src_port; |
Xiaoping Fan | 99cb4c1 | 2015-08-21 19:07:32 -0700 | [diff] [blame] | 620 | sis->src_port_xlate = c->src_port_xlate; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 621 | sis->dest_port = c->dest_port; |
Xiaoping Fan | 99cb4c1 | 2015-08-21 19:07:32 -0700 | [diff] [blame] | 622 | sis->dest_port_xlate = c->dest_port_xlate; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 623 | |
| 624 | original_cm = c->original_match; |
| 625 | reply_cm = c->reply_match; |
| 626 | sis->src_td_max_window = original_cm->protocol_state.tcp.max_win; |
| 627 | sis->src_td_end = original_cm->protocol_state.tcp.end; |
| 628 | sis->src_td_max_end = original_cm->protocol_state.tcp.max_end; |
| 629 | sis->dest_td_max_window = reply_cm->protocol_state.tcp.max_win; |
| 630 | sis->dest_td_end = reply_cm->protocol_state.tcp.end; |
| 631 | sis->dest_td_max_end = reply_cm->protocol_state.tcp.max_end; |
| 632 | |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 633 | sfe_ipv6_connection_match_update_summary_stats(original_cm, &packet_count, &byte_count); |
| 634 | sis->src_new_packet_count = packet_count; |
| 635 | sis->src_new_byte_count = byte_count; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 636 | |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 637 | sfe_ipv6_connection_match_update_summary_stats(reply_cm, &packet_count, &byte_count); |
| 638 | sis->dest_new_packet_count = packet_count; |
| 639 | sis->dest_new_byte_count = byte_count; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 640 | |
| 641 | sis->src_dev = original_cm->match_dev; |
| 642 | sis->src_packet_count = original_cm->rx_packet_count64; |
| 643 | sis->src_byte_count = original_cm->rx_byte_count64; |
| 644 | |
| 645 | sis->dest_dev = reply_cm->match_dev; |
| 646 | sis->dest_packet_count = reply_cm->rx_packet_count64; |
| 647 | sis->dest_byte_count = reply_cm->rx_byte_count64; |
| 648 | |
Xiaoping Fan | 99cb4c1 | 2015-08-21 19:07:32 -0700 | [diff] [blame] | 649 | sis->reason = reason; |
| 650 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 651 | /* |
| 652 | * Get the time increment since our last sync. |
| 653 | */ |
| 654 | sis->delta_jiffies = now_jiffies - c->last_sync_jiffies; |
| 655 | c->last_sync_jiffies = now_jiffies; |
| 656 | } |
| 657 | |
| 658 | /* |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 659 | * sfe_ipv6_free_sfe_ipv6_connection_rcu() |
| 660 | * Called at RCU qs state to free the connection object. |
| 661 | */ |
| 662 | static void sfe_ipv6_free_sfe_ipv6_connection_rcu(struct rcu_head *head) |
| 663 | { |
| 664 | struct sfe_ipv6_connection *c; |
Suruchi Suman | 23a279d | 2021-11-16 15:13:09 +0530 | [diff] [blame] | 665 | struct udp_sock *up; |
| 666 | struct sock *sk; |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 667 | |
| 668 | /* |
| 669 | * We dont need spin lock as the connection is already removed from link list |
| 670 | */ |
| 671 | c = container_of(head, struct sfe_ipv6_connection, rcu); |
| 672 | BUG_ON(!c->removed); |
| 673 | |
| 674 | DEBUG_TRACE("%px: connecton has been deleted\n", c); |
| 675 | |
| 676 | /* |
Suruchi Suman | 23a279d | 2021-11-16 15:13:09 +0530 | [diff] [blame] | 677 | * Decrease the refcount taken in function sfe_ipv6_create_rule() |
| 678 | * during call of __udp6_lib_lookup() |
| 679 | */ |
| 680 | up = c->reply_match->up; |
| 681 | if (up) { |
| 682 | sk = (struct sock *)up; |
| 683 | sock_put(sk); |
| 684 | } |
| 685 | |
| 686 | /* |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 687 | * Release our hold of the source and dest devices and free the memory |
| 688 | * for our connection objects. |
| 689 | */ |
| 690 | dev_put(c->original_dev); |
| 691 | dev_put(c->reply_dev); |
| 692 | kfree(c->original_match); |
| 693 | kfree(c->reply_match); |
| 694 | kfree(c); |
| 695 | } |
| 696 | |
| 697 | /* |
Ken Zhu | 88c5815 | 2021-12-09 15:12:06 -0800 | [diff] [blame] | 698 | * sfe_ipv6_sync_status() |
| 699 | * update a connection status to its connection manager. |
| 700 | * |
| 701 | * si: the ipv6 context |
| 702 | * c: which connection to be notified |
| 703 | * reason: what kind of reason: flush, or destroy |
| 704 | */ |
| 705 | void sfe_ipv6_sync_status(struct sfe_ipv6 *si, |
| 706 | struct sfe_ipv6_connection *c, |
| 707 | sfe_sync_reason_t reason) |
| 708 | { |
| 709 | struct sfe_connection_sync sis; |
| 710 | u64 now_jiffies; |
| 711 | sfe_sync_rule_callback_t sync_rule_callback; |
| 712 | |
| 713 | rcu_read_lock(); |
| 714 | sync_rule_callback = rcu_dereference(si->sync_rule_callback); |
Ken Zhu | 7a43d88 | 2022-01-04 10:51:44 -0800 | [diff] [blame] | 715 | rcu_read_unlock(); |
Ken Zhu | 88c5815 | 2021-12-09 15:12:06 -0800 | [diff] [blame] | 716 | if (unlikely(!sync_rule_callback)) { |
Ken Zhu | 88c5815 | 2021-12-09 15:12:06 -0800 | [diff] [blame] | 717 | return; |
| 718 | } |
| 719 | |
| 720 | /* |
| 721 | * Generate a sync message and then sync. |
| 722 | */ |
| 723 | now_jiffies = get_jiffies_64(); |
| 724 | sfe_ipv6_gen_sync_connection(si, c, &sis, reason, now_jiffies); |
| 725 | sync_rule_callback(&sis); |
Ken Zhu | 88c5815 | 2021-12-09 15:12:06 -0800 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | /* |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 729 | * sfe_ipv6_flush_connection() |
| 730 | * Flush a connection and free all associated resources. |
| 731 | * |
| 732 | * We need to be called with bottom halves disabled locally as we need to acquire |
| 733 | * the connection hash lock and release it again. In general we're actually called |
| 734 | * from within a BH and so we're fine, but we're also called when connections are |
| 735 | * torn down. |
| 736 | */ |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 737 | void sfe_ipv6_flush_connection(struct sfe_ipv6 *si, |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 738 | struct sfe_ipv6_connection *c, |
| 739 | sfe_sync_reason_t reason) |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 740 | { |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 741 | BUG_ON(!c->removed); |
| 742 | |
Ratheesh Kannoth | 1ed9546 | 2021-10-20 07:57:45 +0530 | [diff] [blame] | 743 | this_cpu_inc(si->stats_pcpu->connection_flushes64); |
Ken Zhu | 88c5815 | 2021-12-09 15:12:06 -0800 | [diff] [blame] | 744 | sfe_ipv6_sync_status(si, c, reason); |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 745 | |
| 746 | /* |
Ken Zhu | 88c5815 | 2021-12-09 15:12:06 -0800 | [diff] [blame] | 747 | * Release our hold of the source and dest devices and free the memory |
| 748 | * for our connection objects. |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 749 | */ |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 750 | call_rcu(&c->rcu, sfe_ipv6_free_sfe_ipv6_connection_rcu); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 751 | } |
| 752 | |
Parikshit Gune | d31a820 | 2022-01-05 22:15:04 +0530 | [diff] [blame] | 753 | /* |
Jackson Bockus | 3fafbf3 | 2022-02-13 17:15:26 -0800 | [diff] [blame] | 754 | * sfe_ipv4_service_class_stats_pcpu_get() |
| 755 | * Gets one CPU's service class statistics. |
| 756 | */ |
| 757 | static inline bool sfe_ipv6_service_class_stats_pcpu_get(struct sfe_ipv6_per_service_class_stats *sc_stats, uint64_t *bytes, uint64_t *packets) |
| 758 | { |
| 759 | uint32_t retries = 0; |
| 760 | uint32_t seq; |
| 761 | uint64_t bytes_tmp, packets_tmp; |
| 762 | |
| 763 | do { |
| 764 | seq = read_seqcount_begin(&sc_stats->seq); |
| 765 | bytes_tmp = sc_stats->tx_bytes; |
| 766 | packets_tmp = sc_stats->tx_packets; |
| 767 | } while (read_seqcount_retry(&sc_stats->seq, seq) && ++retries < SFE_SERVICE_CLASS_STATS_MAX_RETRY); |
| 768 | |
| 769 | *bytes += bytes_tmp; |
| 770 | *packets += packets_tmp; |
| 771 | |
| 772 | return retries < SFE_SERVICE_CLASS_STATS_MAX_RETRY; |
| 773 | } |
| 774 | |
| 775 | /* |
| 776 | * sfe_ipv4_service_class_stats_get() |
| 777 | * Copy the ipv4 statistics for the given service class. |
| 778 | */ |
| 779 | bool sfe_ipv6_service_class_stats_get(uint8_t sid, uint64_t *bytes, uint64_t *packets) |
| 780 | { |
| 781 | struct sfe_ipv6 *si = &__si6; |
| 782 | uint32_t cpu = 0; |
| 783 | |
| 784 | for_each_possible_cpu(cpu) { |
| 785 | struct sfe_ipv6_service_class_stats_db *stats_db = per_cpu_ptr(si->stats_pcpu_psc, cpu); |
| 786 | struct sfe_ipv6_per_service_class_stats *sc_stats = &stats_db->psc_stats[sid]; |
| 787 | |
| 788 | if (!sfe_ipv6_service_class_stats_pcpu_get(sc_stats, bytes, packets)) { |
| 789 | return false; |
| 790 | } |
| 791 | } |
| 792 | |
| 793 | return true; |
| 794 | } |
| 795 | |
| 796 | /* |
Parikshit Gune | d31a820 | 2022-01-05 22:15:04 +0530 | [diff] [blame] | 797 | * sfe_ipv6_service_class_stats_inc() |
| 798 | * Increment per cpu per service class stats. |
| 799 | */ |
| 800 | void sfe_ipv6_service_class_stats_inc(struct sfe_ipv6 *si, uint8_t sid, uint64_t bytes) |
| 801 | { |
| 802 | struct sfe_ipv6_service_class_stats_db *sc_stats_db = this_cpu_ptr(si->stats_pcpu_psc); |
| 803 | struct sfe_ipv6_per_service_class_stats *sc_stats = &sc_stats_db->psc_stats[sid]; |
| 804 | |
| 805 | write_seqcount_begin(&sc_stats->seq); |
| 806 | sc_stats->tx_bytes += bytes; |
| 807 | sc_stats->tx_packets++; |
| 808 | write_seqcount_end(&sc_stats->seq); |
| 809 | } |
| 810 | |
| 811 | /* |
Ratheesh Kannoth | 1ed9546 | 2021-10-20 07:57:45 +0530 | [diff] [blame] | 812 | * sfe_ipv6_exception_stats_inc() |
| 813 | * Increment exception stats. |
| 814 | */ |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 815 | void sfe_ipv6_exception_stats_inc(struct sfe_ipv6 *si, enum sfe_ipv6_exception_events reason) |
Ratheesh Kannoth | 1ed9546 | 2021-10-20 07:57:45 +0530 | [diff] [blame] | 816 | { |
| 817 | struct sfe_ipv6_stats *stats = this_cpu_ptr(si->stats_pcpu); |
| 818 | |
| 819 | stats->exception_events64[reason]++; |
| 820 | stats->packets_not_forwarded64++; |
| 821 | } |
| 822 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 823 | /* |
Nitin Shetty | e6ed5b5 | 2021-12-27 14:50:11 +0530 | [diff] [blame] | 824 | * sfe_ipv6_is_local_ip() |
| 825 | * return true if it is local ip otherwise return false |
| 826 | */ |
| 827 | static bool sfe_ipv6_is_local_ip(struct sfe_ipv6 *si, uint8_t *addr) |
| 828 | { |
| 829 | struct net_device *dev; |
| 830 | struct in6_addr ip_addr; |
| 831 | memcpy(ip_addr.s6_addr, addr, 16); |
| 832 | |
| 833 | dev = ipv6_dev_find(&init_net, &ip_addr, 1); |
| 834 | if (dev) { |
| 835 | dev_put(dev); |
| 836 | return true; |
| 837 | } |
| 838 | |
| 839 | return false; |
| 840 | } |
| 841 | |
| 842 | /* |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 843 | * sfe_ipv6_recv() |
| 844 | * Handle packet receives and forwaring. |
| 845 | * |
| 846 | * Returns 1 if the packet is forwarded or 0 if it isn't. |
| 847 | */ |
Suruchi Suman | 23a279d | 2021-11-16 15:13:09 +0530 | [diff] [blame] | 848 | int sfe_ipv6_recv(struct net_device *dev, struct sk_buff *skb, struct sfe_l2_info *l2_info, bool tun_outer) |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 849 | { |
| 850 | struct sfe_ipv6 *si = &__si6; |
| 851 | unsigned int len; |
| 852 | unsigned int payload_len; |
Ratheesh Kannoth | 741f799 | 2021-10-20 07:39:52 +0530 | [diff] [blame] | 853 | unsigned int ihl = sizeof(struct ipv6hdr); |
Ken Zhu | 88c5815 | 2021-12-09 15:12:06 -0800 | [diff] [blame] | 854 | bool sync_on_find = false; |
Ratheesh Kannoth | 741f799 | 2021-10-20 07:39:52 +0530 | [diff] [blame] | 855 | struct ipv6hdr *iph; |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 856 | u8 next_hdr; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 857 | |
| 858 | /* |
| 859 | * Check that we have space for an IP header and an uplayer header here. |
| 860 | */ |
| 861 | len = skb->len; |
| 862 | if (!pskb_may_pull(skb, ihl + sizeof(struct sfe_ipv6_ext_hdr))) { |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 863 | |
Ratheesh Kannoth | 1ed9546 | 2021-10-20 07:57:45 +0530 | [diff] [blame] | 864 | sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_HEADER_INCOMPLETE); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 865 | DEBUG_TRACE("len: %u is too short\n", len); |
| 866 | return 0; |
| 867 | } |
| 868 | |
| 869 | /* |
| 870 | * Is our IP version wrong? |
| 871 | */ |
Ratheesh Kannoth | 741f799 | 2021-10-20 07:39:52 +0530 | [diff] [blame] | 872 | iph = (struct ipv6hdr *)skb->data; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 873 | if (unlikely(iph->version != 6)) { |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 874 | |
Ratheesh Kannoth | 1ed9546 | 2021-10-20 07:57:45 +0530 | [diff] [blame] | 875 | sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_NON_V6); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 876 | DEBUG_TRACE("IP version: %u\n", iph->version); |
| 877 | return 0; |
| 878 | } |
| 879 | |
| 880 | /* |
| 881 | * Does our datagram fit inside the skb? |
| 882 | */ |
| 883 | payload_len = ntohs(iph->payload_len); |
| 884 | if (unlikely(payload_len > (len - ihl))) { |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 885 | |
Ratheesh Kannoth | 1ed9546 | 2021-10-20 07:57:45 +0530 | [diff] [blame] | 886 | sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_DATAGRAM_INCOMPLETE); |
Ratheesh Kannoth | 741f799 | 2021-10-20 07:39:52 +0530 | [diff] [blame] | 887 | DEBUG_TRACE("payload_len: %u, exceeds len: %u\n", payload_len, (len - (unsigned int)sizeof(struct ipv6hdr))); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 888 | return 0; |
| 889 | } |
| 890 | |
| 891 | next_hdr = iph->nexthdr; |
| 892 | while (unlikely(sfe_ipv6_is_ext_hdr(next_hdr))) { |
| 893 | struct sfe_ipv6_ext_hdr *ext_hdr; |
| 894 | unsigned int ext_hdr_len; |
| 895 | |
| 896 | ext_hdr = (struct sfe_ipv6_ext_hdr *)(skb->data + ihl); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 897 | |
| 898 | ext_hdr_len = ext_hdr->hdr_len; |
| 899 | ext_hdr_len <<= 3; |
| 900 | ext_hdr_len += sizeof(struct sfe_ipv6_ext_hdr); |
| 901 | ihl += ext_hdr_len; |
| 902 | if (!pskb_may_pull(skb, ihl + sizeof(struct sfe_ipv6_ext_hdr))) { |
Ratheesh Kannoth | 1ed9546 | 2021-10-20 07:57:45 +0530 | [diff] [blame] | 903 | sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_HEADER_INCOMPLETE); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 904 | |
| 905 | DEBUG_TRACE("extension header %d not completed\n", next_hdr); |
| 906 | return 0; |
| 907 | } |
Ken Zhu | 88c5815 | 2021-12-09 15:12:06 -0800 | [diff] [blame] | 908 | /* |
| 909 | * Any packets have extend hdr, won't be handled in the fast |
| 910 | * path,sync its status and exception to the kernel. |
| 911 | */ |
| 912 | sync_on_find = true; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 913 | next_hdr = ext_hdr->next_hdr; |
| 914 | } |
| 915 | |
| 916 | if (IPPROTO_UDP == next_hdr) { |
Ken Zhu | 88c5815 | 2021-12-09 15:12:06 -0800 | [diff] [blame] | 917 | return sfe_ipv6_recv_udp(si, skb, dev, len, iph, ihl, sync_on_find, l2_info, tun_outer); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | if (IPPROTO_TCP == next_hdr) { |
Ken Zhu | 88c5815 | 2021-12-09 15:12:06 -0800 | [diff] [blame] | 921 | return sfe_ipv6_recv_tcp(si, skb, dev, len, iph, ihl, sync_on_find, l2_info); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | if (IPPROTO_ICMPV6 == next_hdr) { |
| 925 | return sfe_ipv6_recv_icmp(si, skb, dev, len, iph, ihl); |
| 926 | } |
| 927 | |
Tian Yang | afb0345 | 2022-01-13 18:53:13 -0800 | [diff] [blame] | 928 | if (IPPROTO_IPIP == next_hdr) { |
| 929 | return sfe_ipv6_recv_tunipip6(si, skb, dev, len, iph, ihl, sync_on_find, l2_info, true); |
| 930 | } |
| 931 | |
Nitin Shetty | e6ed5b5 | 2021-12-27 14:50:11 +0530 | [diff] [blame] | 932 | #ifdef SFE_GRE_TUN_ENABLE |
| 933 | if (IPPROTO_GRE == next_hdr) { |
Nitin Shetty | 2114a89 | 2022-01-28 20:03:56 +0530 | [diff] [blame] | 934 | return sfe_ipv6_recv_gre(si, skb, dev, len, iph, ihl, sync_on_find, l2_info, tun_outer); |
Nitin Shetty | e6ed5b5 | 2021-12-27 14:50:11 +0530 | [diff] [blame] | 935 | } |
| 936 | #endif |
| 937 | |
Ratheesh Kannoth | 1ed9546 | 2021-10-20 07:57:45 +0530 | [diff] [blame] | 938 | sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_UNHANDLED_PROTOCOL); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 939 | DEBUG_TRACE("not UDP, TCP or ICMP: %u\n", next_hdr); |
| 940 | return 0; |
| 941 | } |
| 942 | |
| 943 | /* |
| 944 | * sfe_ipv6_update_tcp_state() |
| 945 | * update TCP window variables. |
| 946 | */ |
| 947 | static void |
| 948 | sfe_ipv6_update_tcp_state(struct sfe_ipv6_connection *c, |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 949 | struct sfe_ipv6_rule_create_msg *msg) |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 950 | { |
| 951 | struct sfe_ipv6_connection_match *orig_cm; |
| 952 | struct sfe_ipv6_connection_match *repl_cm; |
| 953 | struct sfe_ipv6_tcp_connection_match *orig_tcp; |
| 954 | struct sfe_ipv6_tcp_connection_match *repl_tcp; |
| 955 | |
| 956 | orig_cm = c->original_match; |
| 957 | repl_cm = c->reply_match; |
| 958 | orig_tcp = &orig_cm->protocol_state.tcp; |
| 959 | repl_tcp = &repl_cm->protocol_state.tcp; |
| 960 | |
| 961 | /* update orig */ |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 962 | if (orig_tcp->max_win < msg->tcp_rule.flow_max_window) { |
| 963 | orig_tcp->max_win = msg->tcp_rule.flow_max_window; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 964 | } |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 965 | if ((s32)(orig_tcp->end - msg->tcp_rule.flow_end) < 0) { |
| 966 | orig_tcp->end = msg->tcp_rule.flow_end; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 967 | } |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 968 | if ((s32)(orig_tcp->max_end - msg->tcp_rule.flow_max_end) < 0) { |
| 969 | orig_tcp->max_end = msg->tcp_rule.flow_max_end; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 970 | } |
| 971 | |
| 972 | /* update reply */ |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 973 | if (repl_tcp->max_win < msg->tcp_rule.return_max_window) { |
| 974 | repl_tcp->max_win = msg->tcp_rule.return_max_window; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 975 | } |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 976 | if ((s32)(repl_tcp->end - msg->tcp_rule.return_end) < 0) { |
| 977 | repl_tcp->end = msg->tcp_rule.return_end; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 978 | } |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 979 | if ((s32)(repl_tcp->max_end - msg->tcp_rule.return_max_end) < 0) { |
| 980 | repl_tcp->max_end = msg->tcp_rule.return_max_end; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | /* update match flags */ |
| 984 | orig_cm->flags &= ~SFE_IPV6_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK; |
| 985 | repl_cm->flags &= ~SFE_IPV6_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK; |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 986 | if (msg->rule_flags & SFE_RULE_CREATE_FLAG_NO_SEQ_CHECK) { |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 987 | orig_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK; |
| 988 | repl_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK; |
| 989 | } |
| 990 | } |
| 991 | |
| 992 | /* |
| 993 | * sfe_ipv6_update_protocol_state() |
| 994 | * update protocol specified state machine. |
| 995 | */ |
| 996 | static void |
| 997 | sfe_ipv6_update_protocol_state(struct sfe_ipv6_connection *c, |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 998 | struct sfe_ipv6_rule_create_msg *msg) |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 999 | { |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1000 | switch (msg->tuple.protocol) { |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1001 | case IPPROTO_TCP: |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1002 | sfe_ipv6_update_tcp_state(c, msg); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1003 | break; |
| 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | /* |
Wayne Tan | bb7f178 | 2021-12-13 11:16:04 -0800 | [diff] [blame] | 1008 | * sfe_ipv6_match_entry_set_vlan() |
| 1009 | */ |
| 1010 | static void sfe_ipv6_match_entry_set_vlan( |
| 1011 | struct sfe_ipv6_connection_match *cm, |
| 1012 | u32 primary_ingress_vlan_tag, |
| 1013 | u32 primary_egress_vlan_tag, |
| 1014 | u32 secondary_ingress_vlan_tag, |
| 1015 | u32 secondary_egress_vlan_tag) |
| 1016 | { |
| 1017 | u16 tpid; |
| 1018 | /* |
| 1019 | * Prevent stacking header counts when updating. |
| 1020 | */ |
| 1021 | cm->ingress_vlan_hdr_cnt = 0; |
| 1022 | cm->egress_vlan_hdr_cnt = 0; |
| 1023 | memset(cm->ingress_vlan_hdr, 0, sizeof(cm->ingress_vlan_hdr)); |
| 1024 | memset(cm->egress_vlan_hdr, 0, sizeof(cm->egress_vlan_hdr)); |
| 1025 | |
| 1026 | /* |
| 1027 | * vlan_hdr[0] corresponds to outer tag |
| 1028 | * vlan_hdr[1] corresponds to inner tag |
| 1029 | * Extract the vlan information (tpid and tci) from rule message |
| 1030 | */ |
| 1031 | if ((primary_ingress_vlan_tag & VLAN_VID_MASK) != SFE_VLAN_ID_NOT_CONFIGURED) { |
| 1032 | tpid = (u16)(primary_ingress_vlan_tag >> 16); |
| 1033 | cm->ingress_vlan_hdr[0].tpid = ntohs(tpid); |
| 1034 | cm->ingress_vlan_hdr[0].tci = (u16)primary_ingress_vlan_tag; |
| 1035 | cm->ingress_vlan_hdr_cnt++; |
| 1036 | } |
| 1037 | |
| 1038 | if ((secondary_ingress_vlan_tag & VLAN_VID_MASK) != SFE_VLAN_ID_NOT_CONFIGURED) { |
| 1039 | tpid = (u16)(secondary_ingress_vlan_tag >> 16); |
| 1040 | cm->ingress_vlan_hdr[1].tpid = ntohs(tpid); |
| 1041 | cm->ingress_vlan_hdr[1].tci = (u16)secondary_ingress_vlan_tag; |
| 1042 | cm->ingress_vlan_hdr_cnt++; |
| 1043 | } |
| 1044 | |
| 1045 | if ((primary_egress_vlan_tag & VLAN_VID_MASK) != SFE_VLAN_ID_NOT_CONFIGURED) { |
| 1046 | tpid = (u16)(primary_egress_vlan_tag >> 16); |
| 1047 | cm->egress_vlan_hdr[0].tpid = ntohs(tpid); |
| 1048 | cm->egress_vlan_hdr[0].tci = (u16)primary_egress_vlan_tag; |
| 1049 | cm->egress_vlan_hdr_cnt++; |
| 1050 | } |
| 1051 | |
| 1052 | if ((secondary_egress_vlan_tag & VLAN_VID_MASK) != SFE_VLAN_ID_NOT_CONFIGURED) { |
| 1053 | tpid = (u16)(secondary_egress_vlan_tag >> 16); |
| 1054 | cm->egress_vlan_hdr[1].tpid = ntohs(tpid); |
| 1055 | cm->egress_vlan_hdr[1].tci = (u16)secondary_egress_vlan_tag; |
| 1056 | cm->egress_vlan_hdr_cnt++; |
| 1057 | } |
| 1058 | } |
| 1059 | |
| 1060 | /* |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1061 | * sfe_ipv6_update_rule() |
| 1062 | * update forwarding rule after rule is created. |
| 1063 | */ |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1064 | void sfe_ipv6_update_rule(struct sfe_ipv6_rule_create_msg *msg) |
| 1065 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1066 | { |
| 1067 | struct sfe_ipv6_connection *c; |
| 1068 | struct sfe_ipv6 *si = &__si6; |
| 1069 | |
| 1070 | spin_lock_bh(&si->lock); |
| 1071 | |
| 1072 | c = sfe_ipv6_find_connection(si, |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1073 | msg->tuple.protocol, |
| 1074 | (struct sfe_ipv6_addr *)msg->tuple.flow_ip, |
| 1075 | msg->tuple.flow_ident, |
| 1076 | (struct sfe_ipv6_addr *)msg->tuple.return_ip, |
| 1077 | msg->tuple.return_ident); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1078 | if (c != NULL) { |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1079 | sfe_ipv6_update_protocol_state(c, msg); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1080 | } |
| 1081 | |
| 1082 | spin_unlock_bh(&si->lock); |
| 1083 | } |
| 1084 | |
| 1085 | /* |
Murat Sezgin | ef19039 | 2022-04-04 17:56:31 -0700 | [diff] [blame] | 1086 | * sfe_ipv6_mark_rule_update() |
| 1087 | * Updates the mark values of match entries. |
| 1088 | */ |
| 1089 | void sfe_ipv6_mark_rule_update(struct sfe_connection_mark *mark) |
| 1090 | { |
| 1091 | struct sfe_ipv6_connection *c; |
| 1092 | struct sfe_ipv6 *si = &__si6; |
| 1093 | |
| 1094 | spin_lock_bh(&si->lock); |
| 1095 | c = sfe_ipv6_find_connection(si, mark->protocol, |
| 1096 | (struct sfe_ipv6_addr *)mark->src_ip, |
| 1097 | mark->src_port, |
| 1098 | (struct sfe_ipv6_addr *)mark->dest_ip, |
| 1099 | mark->dest_port); |
| 1100 | if (!c) { |
| 1101 | spin_unlock_bh(&si->lock); |
| 1102 | DEBUG_WARN("%px: connection not found for mark update\n", mark); |
| 1103 | return; |
| 1104 | } |
| 1105 | c->original_match ->mark = mark->mark; |
| 1106 | c->reply_match->mark = mark->mark; |
| 1107 | spin_unlock_bh(&si->lock); |
| 1108 | DEBUG_TRACE("%px: connection mark updated with %d\n", mark, mark->mark); |
| 1109 | } |
| 1110 | EXPORT_SYMBOL(sfe_ipv6_mark_rule_update); |
| 1111 | |
| 1112 | /* |
Guduri Prathyusha | 79a5fee | 2021-11-11 17:59:10 +0530 | [diff] [blame] | 1113 | * sfe_ipv6_xmit_eth_type_check |
| 1114 | * Checking if MAC header has to be written. |
| 1115 | */ |
| 1116 | static inline bool sfe_ipv6_xmit_eth_type_check(struct net_device *dev, u32 cm_flags) |
| 1117 | { |
| 1118 | if (!(dev->flags & IFF_NOARP)) { |
| 1119 | return true; |
| 1120 | } |
| 1121 | |
| 1122 | /* |
| 1123 | * For PPPoE, since we are now supporting PPPoE encapsulation, we are writing L2 header. |
| 1124 | */ |
| 1125 | if (cm_flags & SFE_IPV6_CONNECTION_MATCH_FLAG_PPPOE_ENCAP) { |
| 1126 | return true; |
| 1127 | } |
| 1128 | |
| 1129 | return false; |
| 1130 | } |
| 1131 | |
| 1132 | /* |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1133 | * sfe_ipv6_create_rule() |
| 1134 | * Create a forwarding rule. |
| 1135 | */ |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1136 | int sfe_ipv6_create_rule(struct sfe_ipv6_rule_create_msg *msg) |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1137 | { |
| 1138 | struct sfe_ipv6 *si = &__si6; |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 1139 | struct sfe_ipv6_connection *c, *old_c; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1140 | struct sfe_ipv6_connection_match *original_cm; |
| 1141 | struct sfe_ipv6_connection_match *reply_cm; |
| 1142 | struct net_device *dest_dev; |
| 1143 | struct net_device *src_dev; |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1144 | struct sfe_ipv6_5tuple *tuple = &msg->tuple; |
Suruchi Suman | 23a279d | 2021-11-16 15:13:09 +0530 | [diff] [blame] | 1145 | struct sock *sk; |
| 1146 | struct net *net; |
| 1147 | unsigned int src_if_idx; |
| 1148 | |
Suruchi Suman | c1a4a61 | 2021-10-21 14:50:23 +0530 | [diff] [blame] | 1149 | s32 flow_interface_num = msg->conn_rule.flow_top_interface_num; |
| 1150 | s32 return_interface_num = msg->conn_rule.return_top_interface_num; |
Parikshit Gune | d31a820 | 2022-01-05 22:15:04 +0530 | [diff] [blame] | 1151 | u32 flow_sawf_tag; |
| 1152 | u32 return_sawf_tag; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1153 | |
Suruchi Suman | c1a4a61 | 2021-10-21 14:50:23 +0530 | [diff] [blame] | 1154 | if (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_FLOW_BOTTOM_INTERFACE) { |
| 1155 | flow_interface_num = msg->conn_rule.flow_interface_num; |
| 1156 | } |
| 1157 | |
| 1158 | if (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_RETURN_BOTTOM_INTERFACE) { |
| 1159 | return_interface_num = msg->conn_rule.return_interface_num; |
| 1160 | } |
| 1161 | |
| 1162 | src_dev = dev_get_by_index(&init_net, flow_interface_num); |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1163 | if (!src_dev) { |
| 1164 | DEBUG_WARN("%px: Unable to find src_dev corresponding to %d\n", msg, |
Suruchi Suman | c1a4a61 | 2021-10-21 14:50:23 +0530 | [diff] [blame] | 1165 | flow_interface_num); |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1166 | this_cpu_inc(si->stats_pcpu->connection_create_failures64); |
| 1167 | return -EINVAL; |
| 1168 | } |
| 1169 | |
Suruchi Suman | c1a4a61 | 2021-10-21 14:50:23 +0530 | [diff] [blame] | 1170 | dest_dev = dev_get_by_index(&init_net, return_interface_num); |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1171 | if (!dest_dev) { |
| 1172 | DEBUG_WARN("%px: Unable to find dest_dev corresponding to %d\n", msg, |
Suruchi Suman | c1a4a61 | 2021-10-21 14:50:23 +0530 | [diff] [blame] | 1173 | return_interface_num); |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1174 | this_cpu_inc(si->stats_pcpu->connection_create_failures64); |
| 1175 | dev_put(src_dev); |
| 1176 | return -EINVAL; |
| 1177 | } |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1178 | |
| 1179 | if (unlikely((dest_dev->reg_state != NETREG_REGISTERED) || |
| 1180 | (src_dev->reg_state != NETREG_REGISTERED))) { |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1181 | DEBUG_WARN("%px: src_dev=%s and dest_dev=%s are unregistered\n", msg, |
| 1182 | src_dev->name, dest_dev->name); |
| 1183 | this_cpu_inc(si->stats_pcpu->connection_create_failures64); |
| 1184 | dev_put(src_dev); |
| 1185 | dev_put(dest_dev); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1186 | return -EINVAL; |
| 1187 | } |
| 1188 | |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 1189 | /* |
| 1190 | * Allocate the various connection tracking objects. |
| 1191 | */ |
Parikshit Gune | ef1664c | 2022-03-24 14:15:42 +0530 | [diff] [blame] | 1192 | c = (struct sfe_ipv6_connection *)kzalloc(sizeof(struct sfe_ipv6_connection), GFP_ATOMIC); |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 1193 | if (unlikely(!c)) { |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1194 | DEBUG_WARN("%px: memory allocation of connection entry failed\n", msg); |
| 1195 | this_cpu_inc(si->stats_pcpu->connection_create_failures64); |
| 1196 | dev_put(src_dev); |
| 1197 | dev_put(dest_dev); |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 1198 | return -ENOMEM; |
| 1199 | } |
| 1200 | |
Parikshit Gune | ef1664c | 2022-03-24 14:15:42 +0530 | [diff] [blame] | 1201 | original_cm = (struct sfe_ipv6_connection_match *)kzalloc(sizeof(struct sfe_ipv6_connection_match), GFP_ATOMIC); |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 1202 | if (unlikely(!original_cm)) { |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1203 | this_cpu_inc(si->stats_pcpu->connection_create_failures64); |
| 1204 | DEBUG_WARN("%px: memory allocation of connection match entry failed\n", msg); |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 1205 | kfree(c); |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1206 | dev_put(src_dev); |
| 1207 | dev_put(dest_dev); |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 1208 | return -ENOMEM; |
| 1209 | } |
| 1210 | |
Parikshit Gune | ef1664c | 2022-03-24 14:15:42 +0530 | [diff] [blame] | 1211 | reply_cm = (struct sfe_ipv6_connection_match *)kzalloc(sizeof(struct sfe_ipv6_connection_match), GFP_ATOMIC); |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 1212 | if (unlikely(!reply_cm)) { |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1213 | this_cpu_inc(si->stats_pcpu->connection_create_failures64); |
| 1214 | DEBUG_WARN("%px: memory allocation of connection match entry failed\n", msg); |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 1215 | kfree(original_cm); |
| 1216 | kfree(c); |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1217 | dev_put(src_dev); |
| 1218 | dev_put(dest_dev); |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 1219 | return -ENOMEM; |
| 1220 | } |
| 1221 | |
Ratheesh Kannoth | 1ed9546 | 2021-10-20 07:57:45 +0530 | [diff] [blame] | 1222 | this_cpu_inc(si->stats_pcpu->connection_create_requests64); |
| 1223 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1224 | spin_lock_bh(&si->lock); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1225 | |
| 1226 | /* |
| 1227 | * Check to see if there is already a flow that matches the rule we're |
| 1228 | * trying to create. If there is then we can't create a new one. |
| 1229 | */ |
Wayne Tan | bb7f178 | 2021-12-13 11:16:04 -0800 | [diff] [blame] | 1230 | old_c = sfe_ipv6_find_connection(si, |
| 1231 | tuple->protocol, |
| 1232 | (struct sfe_ipv6_addr *)tuple->flow_ip, |
| 1233 | tuple->flow_ident, |
| 1234 | (struct sfe_ipv6_addr *)tuple->return_ip, |
| 1235 | tuple->return_ident); |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1236 | |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 1237 | if (old_c != NULL) { |
Ratheesh Kannoth | 1ed9546 | 2021-10-20 07:57:45 +0530 | [diff] [blame] | 1238 | this_cpu_inc(si->stats_pcpu->connection_create_collisions64); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1239 | |
| 1240 | /* |
| 1241 | * If we already have the flow then it's likely that this |
| 1242 | * request to create the connection rule contains more |
| 1243 | * up-to-date information. Check and update accordingly. |
| 1244 | */ |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1245 | sfe_ipv6_update_protocol_state(old_c, msg); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1246 | spin_unlock_bh(&si->lock); |
| 1247 | |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 1248 | kfree(reply_cm); |
| 1249 | kfree(original_cm); |
| 1250 | kfree(c); |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1251 | dev_put(src_dev); |
| 1252 | dev_put(dest_dev); |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 1253 | |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1254 | DEBUG_TRACE("connection already exists - p: %d\n" |
Tian Yang | 45f39c8 | 2020-10-06 14:07:47 -0700 | [diff] [blame] | 1255 | " s: %s:%pxM:%pI6:%u, d: %s:%pxM:%pI6:%u\n", |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1256 | tuple->protocol, |
| 1257 | src_dev->name, msg->conn_rule.flow_mac, tuple->flow_ip, ntohs(tuple->flow_ident), |
| 1258 | dest_dev->name, msg->conn_rule.return_mac, tuple->return_ip, ntohs(tuple->return_ident)); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1259 | return -EADDRINUSE; |
| 1260 | } |
| 1261 | |
| 1262 | /* |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1263 | * Fill in the "original" direction connection matching object. |
| 1264 | * Note that the transmit MAC address is "dest_mac_xlate" because |
| 1265 | * we always know both ends of a connection by their translated |
| 1266 | * addresses and not their public addresses. |
| 1267 | */ |
| 1268 | original_cm->match_dev = src_dev; |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1269 | original_cm->match_protocol = tuple->protocol; |
| 1270 | original_cm->match_src_ip[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip; |
Suruchi Suman | 66609a7 | 2022-01-20 02:34:25 +0530 | [diff] [blame] | 1271 | original_cm->match_src_port = netif_is_vxlan(src_dev) ? 0 : tuple->flow_ident; |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1272 | original_cm->match_dest_ip[0] = *(struct sfe_ipv6_addr *)tuple->return_ip; |
| 1273 | original_cm->match_dest_port = tuple->return_ident; |
| 1274 | |
| 1275 | original_cm->xlate_src_ip[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip; |
| 1276 | original_cm->xlate_src_port = tuple->flow_ident; |
| 1277 | original_cm->xlate_dest_ip[0] = *(struct sfe_ipv6_addr *)tuple->return_ip; |
| 1278 | original_cm->xlate_dest_port = tuple->return_ident; |
| 1279 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1280 | original_cm->xmit_dev = dest_dev; |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1281 | |
| 1282 | original_cm->xmit_dev_mtu = msg->conn_rule.return_mtu; |
Ratheesh Kannoth | 29140aa | 2021-10-20 08:25:02 +0530 | [diff] [blame] | 1283 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1284 | original_cm->connection = c; |
| 1285 | original_cm->counter_match = reply_cm; |
Suruchi Suman | 23a279d | 2021-11-16 15:13:09 +0530 | [diff] [blame] | 1286 | |
| 1287 | /* |
| 1288 | * Valid in decap direction only |
| 1289 | */ |
| 1290 | RCU_INIT_POINTER(original_cm->up, NULL); |
| 1291 | |
Ken Zhu | 37040ea | 2021-09-09 21:11:15 -0700 | [diff] [blame] | 1292 | if (msg->valid_flags & SFE_RULE_CREATE_MARK_VALID) { |
| 1293 | original_cm->mark = msg->mark_rule.flow_mark; |
| 1294 | original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_MARK; |
| 1295 | } |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1296 | if (msg->valid_flags & SFE_RULE_CREATE_QOS_VALID) { |
| 1297 | original_cm->priority = msg->qos_rule.flow_qos_tag; |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 1298 | original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_PRIORITY_REMARK; |
| 1299 | } |
Wayne Tan | bb7f178 | 2021-12-13 11:16:04 -0800 | [diff] [blame] | 1300 | |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1301 | if (msg->valid_flags & SFE_RULE_CREATE_DSCP_MARKING_VALID) { |
| 1302 | original_cm->dscp = msg->dscp_rule.flow_dscp << SFE_IPV6_DSCP_SHIFT; |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 1303 | original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_DSCP_REMARK; |
| 1304 | } |
Ratheesh Kannoth | 71fc51e | 2022-01-05 10:02:47 +0530 | [diff] [blame] | 1305 | if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) { |
| 1306 | original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_BRIDGE_FLOW; |
| 1307 | } |
Ken Zhu | 7e38d1a | 2021-11-30 17:31:46 -0800 | [diff] [blame] | 1308 | if (msg->rule_flags & SFE_RULE_CREATE_FLAG_FLOW_TRANSMIT_FAST) { |
| 1309 | original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_FAST_XMIT_DEV_ADMISSION; |
| 1310 | } |
| 1311 | |
Parikshit Gune | d31a820 | 2022-01-05 22:15:04 +0530 | [diff] [blame] | 1312 | /* |
| 1313 | * Mark SAWF metadata if the sawf tag is valid. |
| 1314 | */ |
| 1315 | original_cm->sawf_valid = false; |
| 1316 | flow_sawf_tag = SFE_GET_SAWF_TAG(msg->sawf_rule.flow_mark); |
| 1317 | if (likely(SFE_SAWF_TAG_IS_VALID(flow_sawf_tag))) { |
| 1318 | original_cm->mark = msg->sawf_rule.flow_mark; |
| 1319 | original_cm->sawf_valid = true; |
| 1320 | original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_MARK; |
| 1321 | } |
Ratheesh Kannoth | 71fc51e | 2022-01-05 10:02:47 +0530 | [diff] [blame] | 1322 | |
Wayne Tan | bb7f178 | 2021-12-13 11:16:04 -0800 | [diff] [blame] | 1323 | /* |
| 1324 | * Add VLAN rule to original_cm |
| 1325 | */ |
| 1326 | if (msg->valid_flags & SFE_RULE_CREATE_VLAN_VALID) { |
| 1327 | struct sfe_vlan_rule *vlan_primary_rule = &msg->vlan_primary_rule; |
| 1328 | struct sfe_vlan_rule *vlan_secondary_rule = &msg->vlan_secondary_rule; |
| 1329 | sfe_ipv6_match_entry_set_vlan(original_cm, |
| 1330 | vlan_primary_rule->ingress_vlan_tag, |
| 1331 | vlan_primary_rule->egress_vlan_tag, |
| 1332 | vlan_secondary_rule->ingress_vlan_tag, |
| 1333 | vlan_secondary_rule->egress_vlan_tag); |
| 1334 | |
| 1335 | if ((msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_RETURN_BOTTOM_INTERFACE) && |
| 1336 | original_cm->egress_vlan_hdr_cnt > 0) { |
| 1337 | original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_INSERT_EGRESS_VLAN_TAG; |
| 1338 | original_cm->l2_hdr_size += original_cm->egress_vlan_hdr_cnt * VLAN_HLEN; |
| 1339 | } |
| 1340 | } |
| 1341 | |
Nitin Shetty | e6ed5b5 | 2021-12-27 14:50:11 +0530 | [diff] [blame] | 1342 | if ((IPPROTO_GRE == tuple->protocol) && !sfe_ipv6_is_local_ip(si, (uint8_t *)original_cm->match_dest_ip)) { |
| 1343 | original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_PASSTHROUGH; |
| 1344 | } |
| 1345 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1346 | #ifdef CONFIG_NF_FLOW_COOKIE |
| 1347 | original_cm->flow_cookie = 0; |
| 1348 | #endif |
Zhi Chen | 8748eb3 | 2015-06-18 12:58:48 -0700 | [diff] [blame] | 1349 | #ifdef CONFIG_XFRM |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1350 | if (msg->valid_flags & SFE_RULE_CREATE_DIRECTION_VALID) { |
| 1351 | original_cm->flow_accel = msg->direction_rule.flow_accel; |
| 1352 | } else { |
| 1353 | original_cm->flow_accel = 1; |
| 1354 | } |
Zhi Chen | 8748eb3 | 2015-06-18 12:58:48 -0700 | [diff] [blame] | 1355 | #endif |
Ratheesh Kannoth | a3cf0e0 | 2021-12-09 09:44:10 +0530 | [diff] [blame] | 1356 | /* |
| 1357 | * If l2_features are disabled and flow uses l2 features such as macvlan/bridge/pppoe/vlan, |
| 1358 | * bottom interfaces are expected to be disabled in the flow rule and always top interfaces |
| 1359 | * are used. In such cases, do not use HW csum offload. csum offload is used only when we |
| 1360 | * are sending directly to the destination interface that supports it. |
| 1361 | */ |
Nitin Shetty | e6ed5b5 | 2021-12-27 14:50:11 +0530 | [diff] [blame] | 1362 | if (likely(dest_dev->features & NETIF_F_HW_CSUM) && sfe_dev_has_hw_csum(dest_dev)) { |
Ratheesh Kannoth | a3cf0e0 | 2021-12-09 09:44:10 +0530 | [diff] [blame] | 1363 | if ((msg->conn_rule.return_top_interface_num == msg->conn_rule.return_interface_num) || |
| 1364 | (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_RETURN_BOTTOM_INTERFACE)) { |
Ratheesh Kannoth | 4844553 | 2022-02-07 16:19:00 +0530 | [diff] [blame] | 1365 | /* |
| 1366 | * Dont enable CSUM offload |
| 1367 | */ |
| 1368 | #if 0 |
Ratheesh Kannoth | a3cf0e0 | 2021-12-09 09:44:10 +0530 | [diff] [blame] | 1369 | original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_CSUM_OFFLOAD; |
Ratheesh Kannoth | 4844553 | 2022-02-07 16:19:00 +0530 | [diff] [blame] | 1370 | #endif |
Ratheesh Kannoth | a3cf0e0 | 2021-12-09 09:44:10 +0530 | [diff] [blame] | 1371 | } |
| 1372 | } |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1373 | |
Guduri Prathyusha | 647fe3e | 2021-11-22 19:17:51 +0530 | [diff] [blame] | 1374 | /* |
| 1375 | * Adding PPPoE parameters to original and reply entries based on the direction where |
| 1376 | * PPPoE header is valid in ECM rule. |
| 1377 | * |
| 1378 | * If PPPoE is valid in flow direction (from interface is PPPoE), then |
| 1379 | * original cm will have PPPoE at ingress (strip PPPoE header) |
| 1380 | * reply cm will have PPPoE at egress (add PPPoE header) |
| 1381 | * |
| 1382 | * If PPPoE is valid in return direction (to interface is PPPoE), then |
| 1383 | * original cm will have PPPoE at egress (add PPPoE header) |
| 1384 | * reply cm will have PPPoE at ingress (strip PPPoE header) |
| 1385 | */ |
| 1386 | if (msg->valid_flags & SFE_RULE_CREATE_PPPOE_DECAP_VALID) { |
| 1387 | original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_PPPOE_DECAP; |
| 1388 | original_cm->pppoe_session_id = msg->pppoe_rule.flow_pppoe_session_id; |
| 1389 | ether_addr_copy(original_cm->pppoe_remote_mac, msg->pppoe_rule.flow_pppoe_remote_mac); |
| 1390 | |
| 1391 | reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_PPPOE_ENCAP; |
Wayne Tan | bb7f178 | 2021-12-13 11:16:04 -0800 | [diff] [blame] | 1392 | reply_cm->l2_hdr_size += SFE_PPPOE_SESSION_HEADER_SIZE; |
Guduri Prathyusha | 647fe3e | 2021-11-22 19:17:51 +0530 | [diff] [blame] | 1393 | reply_cm->pppoe_session_id = msg->pppoe_rule.flow_pppoe_session_id; |
| 1394 | ether_addr_copy(reply_cm->pppoe_remote_mac, msg->pppoe_rule.flow_pppoe_remote_mac); |
| 1395 | } |
| 1396 | |
| 1397 | if (msg->valid_flags & SFE_RULE_CREATE_PPPOE_ENCAP_VALID) { |
| 1398 | original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_PPPOE_ENCAP; |
Wayne Tan | bb7f178 | 2021-12-13 11:16:04 -0800 | [diff] [blame] | 1399 | original_cm->l2_hdr_size += SFE_PPPOE_SESSION_HEADER_SIZE; |
Guduri Prathyusha | 647fe3e | 2021-11-22 19:17:51 +0530 | [diff] [blame] | 1400 | original_cm->pppoe_session_id = msg->pppoe_rule.return_pppoe_session_id; |
| 1401 | ether_addr_copy(original_cm->pppoe_remote_mac, msg->pppoe_rule.return_pppoe_remote_mac); |
| 1402 | |
| 1403 | reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_PPPOE_DECAP; |
| 1404 | reply_cm->pppoe_session_id = msg->pppoe_rule.return_pppoe_session_id; |
| 1405 | ether_addr_copy(reply_cm->pppoe_remote_mac, msg->pppoe_rule.return_pppoe_remote_mac); |
| 1406 | } |
| 1407 | |
Murat Sezgin | 9c53897 | 2022-05-17 13:33:17 -0700 | [diff] [blame] | 1408 | if (msg->rule_flags & SFE_RULE_CREATE_FLAG_FLOW_SRC_INTERFACE_CHECK) { |
Ratheesh Kannoth | 5dee377 | 2022-01-18 11:27:14 +0530 | [diff] [blame] | 1409 | original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_SRC_INTERFACE_CHECK; |
| 1410 | } |
| 1411 | |
Murat Sezgin | 9c53897 | 2022-05-17 13:33:17 -0700 | [diff] [blame] | 1412 | if (msg->rule_flags & SFE_RULE_CREATE_FLAG_FLOW_SRC_INTERFACE_CHECK_NO_FLUSH) { |
| 1413 | original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_SRC_INTERFACE_CHECK_NO_FLUSH; |
| 1414 | } |
| 1415 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1416 | /* |
Ken Zhu | bbf4965 | 2021-09-12 15:33:09 -0700 | [diff] [blame] | 1417 | * For the non-arp interface, we don't write L2 HDR. |
Guduri Prathyusha | 79a5fee | 2021-11-11 17:59:10 +0530 | [diff] [blame] | 1418 | * Excluding PPPoE from this, since we are now supporting PPPoE encap/decap. |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1419 | */ |
Guduri Prathyusha | 79a5fee | 2021-11-11 17:59:10 +0530 | [diff] [blame] | 1420 | if (sfe_ipv6_xmit_eth_type_check(dest_dev, original_cm->flags)) { |
Ratheesh Kannoth | 29140aa | 2021-10-20 08:25:02 +0530 | [diff] [blame] | 1421 | |
| 1422 | /* |
| 1423 | * Check whether the rule has configured a specific source MAC address to use. |
| 1424 | * This is needed when virtual L3 interfaces such as br-lan, macvlan, vlan are used during egress |
| 1425 | */ |
Ratheesh Kannoth | 71fc51e | 2022-01-05 10:02:47 +0530 | [diff] [blame] | 1426 | if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) { |
| 1427 | ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)msg->conn_rule.flow_mac); |
Ratheesh Kannoth | 29140aa | 2021-10-20 08:25:02 +0530 | [diff] [blame] | 1428 | } else { |
Ratheesh Kannoth | 71fc51e | 2022-01-05 10:02:47 +0530 | [diff] [blame] | 1429 | if ((msg->valid_flags & SFE_RULE_CREATE_SRC_MAC_VALID) && |
| 1430 | (msg->src_mac_rule.mac_valid_flags & SFE_SRC_MAC_RETURN_VALID)) { |
| 1431 | ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)msg->src_mac_rule.return_src_mac); |
| 1432 | } else { |
| 1433 | ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)dest_dev->dev_addr); |
| 1434 | } |
Ratheesh Kannoth | 29140aa | 2021-10-20 08:25:02 +0530 | [diff] [blame] | 1435 | } |
| 1436 | ether_addr_copy((u8 *)original_cm->xmit_dest_mac, (u8 *)msg->conn_rule.return_mac); |
| 1437 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1438 | original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_WRITE_L2_HDR; |
Wayne Tan | bb7f178 | 2021-12-13 11:16:04 -0800 | [diff] [blame] | 1439 | original_cm->l2_hdr_size += ETH_HLEN; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1440 | |
| 1441 | /* |
| 1442 | * If our dev writes Ethernet headers then we can write a really fast |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1443 | * version |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1444 | */ |
| 1445 | if (dest_dev->header_ops) { |
| 1446 | if (dest_dev->header_ops->create == eth_header) { |
| 1447 | original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR; |
| 1448 | } |
| 1449 | } |
| 1450 | } |
| 1451 | |
| 1452 | /* |
| 1453 | * Fill in the "reply" direction connection matching object. |
| 1454 | */ |
| 1455 | reply_cm->match_dev = dest_dev; |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1456 | reply_cm->match_protocol = tuple->protocol; |
| 1457 | reply_cm->match_src_ip[0] = *(struct sfe_ipv6_addr *)tuple->return_ip; |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1458 | reply_cm->match_dest_ip[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip; |
| 1459 | reply_cm->match_dest_port = tuple->flow_ident; |
| 1460 | reply_cm->xlate_src_ip[0] = *(struct sfe_ipv6_addr *)tuple->return_ip; |
| 1461 | reply_cm->xlate_src_port = tuple->return_ident; |
| 1462 | reply_cm->xlate_dest_ip[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip; |
| 1463 | reply_cm->xlate_dest_port = tuple->flow_ident; |
| 1464 | |
Suruchi Suman | 23a279d | 2021-11-16 15:13:09 +0530 | [diff] [blame] | 1465 | /* |
| 1466 | * Keep source port as 0 for VxLAN tunnels. |
| 1467 | */ |
| 1468 | if (netif_is_vxlan(src_dev) || netif_is_vxlan(dest_dev)) { |
| 1469 | reply_cm->match_src_port = 0; |
| 1470 | } else { |
| 1471 | reply_cm->match_src_port = tuple->return_ident; |
| 1472 | } |
| 1473 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1474 | reply_cm->xmit_dev = src_dev; |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1475 | reply_cm->xmit_dev_mtu = msg->conn_rule.flow_mtu; |
Ratheesh Kannoth | 29140aa | 2021-10-20 08:25:02 +0530 | [diff] [blame] | 1476 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1477 | reply_cm->connection = c; |
| 1478 | reply_cm->counter_match = original_cm; |
Suruchi Suman | 23a279d | 2021-11-16 15:13:09 +0530 | [diff] [blame] | 1479 | |
Ken Zhu | 37040ea | 2021-09-09 21:11:15 -0700 | [diff] [blame] | 1480 | if (msg->valid_flags & SFE_RULE_CREATE_MARK_VALID) { |
| 1481 | reply_cm->mark = msg->mark_rule.return_mark; |
| 1482 | reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_MARK; |
| 1483 | } |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1484 | if (msg->valid_flags & SFE_RULE_CREATE_QOS_VALID) { |
| 1485 | reply_cm->priority = msg->qos_rule.return_qos_tag; |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 1486 | reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_PRIORITY_REMARK; |
| 1487 | } |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1488 | if (msg->valid_flags & SFE_RULE_CREATE_DSCP_MARKING_VALID) { |
| 1489 | reply_cm->dscp = msg->dscp_rule.return_dscp << SFE_IPV6_DSCP_SHIFT; |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 1490 | reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_DSCP_REMARK; |
| 1491 | } |
Ratheesh Kannoth | 71fc51e | 2022-01-05 10:02:47 +0530 | [diff] [blame] | 1492 | if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) { |
| 1493 | reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_BRIDGE_FLOW; |
| 1494 | } |
Ken Zhu | 7e38d1a | 2021-11-30 17:31:46 -0800 | [diff] [blame] | 1495 | if (msg->rule_flags & SFE_RULE_CREATE_FLAG_RETURN_TRANSMIT_FAST) { |
| 1496 | reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_FAST_XMIT_DEV_ADMISSION; |
| 1497 | } |
Ratheesh Kannoth | 71fc51e | 2022-01-05 10:02:47 +0530 | [diff] [blame] | 1498 | |
Nitin Shetty | e6ed5b5 | 2021-12-27 14:50:11 +0530 | [diff] [blame] | 1499 | if ((IPPROTO_GRE == tuple->protocol) && !sfe_ipv6_is_local_ip(si, (uint8_t *)reply_cm->match_dest_ip)) { |
| 1500 | reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_PASSTHROUGH; |
| 1501 | } |
| 1502 | |
Suruchi Suman | 23a279d | 2021-11-16 15:13:09 +0530 | [diff] [blame] | 1503 | /* |
Parikshit Gune | d31a820 | 2022-01-05 22:15:04 +0530 | [diff] [blame] | 1504 | * Mark return SAWF metadata if the sawf tag is valid. |
| 1505 | */ |
| 1506 | reply_cm->sawf_valid = false; |
| 1507 | return_sawf_tag = SFE_GET_SAWF_TAG(msg->sawf_rule.return_mark); |
| 1508 | if (likely(SFE_SAWF_TAG_IS_VALID(return_sawf_tag))) { |
| 1509 | reply_cm->mark = msg->sawf_rule.return_mark; |
| 1510 | reply_cm->sawf_valid = true; |
| 1511 | reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_MARK; |
| 1512 | } |
| 1513 | |
| 1514 | /* |
Suruchi Suman | 23a279d | 2021-11-16 15:13:09 +0530 | [diff] [blame] | 1515 | * Setup UDP Socket if found to be valid for decap. |
| 1516 | */ |
| 1517 | RCU_INIT_POINTER(reply_cm->up, NULL); |
| 1518 | net = dev_net(reply_cm->match_dev); |
| 1519 | src_if_idx = src_dev->ifindex; |
| 1520 | |
| 1521 | rcu_read_lock(); |
| 1522 | |
| 1523 | /* |
| 1524 | * Look for the associated sock object. |
| 1525 | * __udp6_lib_lookup() holds a reference for this sock object, |
| 1526 | * which will be released in sfe_ipv6_flush_connection() |
| 1527 | */ |
| 1528 | #if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0)) |
| 1529 | sk = __udp6_lib_lookup(net, (const struct in6_addr *)reply_cm->match_dest_ip, |
| 1530 | reply_cm->match_dest_port, (const struct in6_addr *)reply_cm->xlate_src_ip, |
| 1531 | reply_cm->xlate_src_port, src_if_idx, &udp_table); |
| 1532 | #else |
| 1533 | sk = __udp6_lib_lookup(net, (const struct in6_addr *)reply_cm->match_dest_ip, |
| 1534 | reply_cm->match_dest_port, (const struct in6_addr *)reply_cm->xlate_src_ip, |
| 1535 | reply_cm->xlate_src_port, src_if_idx, 0, &udp_table, NULL); |
| 1536 | #endif |
| 1537 | rcu_read_unlock(); |
| 1538 | |
| 1539 | /* |
| 1540 | * We set the UDP sock pointer as valid only for decap direction. |
| 1541 | */ |
| 1542 | if (sk && udp_sk(sk)->encap_type) { |
| 1543 | #if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0)) |
| 1544 | if (!atomic_add_unless(&sk->sk_refcnt, 1, 0)) { |
| 1545 | #else |
| 1546 | if (!refcount_inc_not_zero(&sk->sk_refcnt)) { |
| 1547 | #endif |
Tian Yang | 435afc4 | 2022-02-02 12:47:32 -0800 | [diff] [blame] | 1548 | this_cpu_inc(si->stats_pcpu->connection_create_failures64); |
Wayne Tan | bb7f178 | 2021-12-13 11:16:04 -0800 | [diff] [blame] | 1549 | spin_unlock_bh(&si->lock); |
Suruchi Suman | 23a279d | 2021-11-16 15:13:09 +0530 | [diff] [blame] | 1550 | kfree(reply_cm); |
| 1551 | kfree(original_cm); |
| 1552 | kfree(c); |
| 1553 | |
| 1554 | DEBUG_INFO("sfe: unable to take reference for socket p:%d\n", tuple->protocol); |
| 1555 | DEBUG_INFO("SK: connection - \n" |
| 1556 | " s: %s:%pI6(%pI6):%u(%u)\n" |
| 1557 | " d: %s:%pI6(%pI6):%u(%u)\n", |
| 1558 | reply_cm->match_dev->name, &reply_cm->match_src_ip, &reply_cm->xlate_src_ip, |
| 1559 | ntohs(reply_cm->match_src_port), ntohs(reply_cm->xlate_src_port), |
| 1560 | reply_cm->xmit_dev->name, &reply_cm->match_dest_ip, &reply_cm->xlate_dest_ip, |
| 1561 | ntohs(reply_cm->match_dest_port), ntohs(reply_cm->xlate_dest_port)); |
| 1562 | |
| 1563 | dev_put(src_dev); |
| 1564 | dev_put(dest_dev); |
| 1565 | |
| 1566 | return -ESHUTDOWN; |
| 1567 | } |
| 1568 | |
| 1569 | rcu_assign_pointer(reply_cm->up, udp_sk(sk)); |
| 1570 | DEBUG_INFO("Sock lookup success with reply_cm direction(%p)\n", sk); |
| 1571 | DEBUG_INFO("SK: connection - \n" |
| 1572 | " s: %s:%pI6(%pI6):%u(%u)\n" |
| 1573 | " d: %s:%pI6(%pI6):%u(%u)\n", |
| 1574 | reply_cm->match_dev->name, &reply_cm->match_src_ip, &reply_cm->xlate_src_ip, |
| 1575 | ntohs(reply_cm->match_src_port), ntohs(reply_cm->xlate_src_port), |
| 1576 | reply_cm->xmit_dev->name, &reply_cm->match_dest_ip, &reply_cm->xlate_dest_ip, |
| 1577 | ntohs(reply_cm->match_dest_port), ntohs(reply_cm->xlate_dest_port)); |
| 1578 | } |
| 1579 | |
Wayne Tan | bb7f178 | 2021-12-13 11:16:04 -0800 | [diff] [blame] | 1580 | /* |
| 1581 | * Add VLAN rule to reply_cm |
| 1582 | */ |
| 1583 | if (msg->valid_flags & SFE_RULE_CREATE_VLAN_VALID) { |
| 1584 | struct sfe_vlan_rule *vlan_primary_rule = &msg->vlan_primary_rule; |
| 1585 | struct sfe_vlan_rule *vlan_secondary_rule = &msg->vlan_secondary_rule; |
| 1586 | sfe_ipv6_match_entry_set_vlan(reply_cm, |
| 1587 | vlan_primary_rule->egress_vlan_tag, |
| 1588 | vlan_primary_rule->ingress_vlan_tag, |
| 1589 | vlan_secondary_rule->egress_vlan_tag, |
| 1590 | vlan_secondary_rule->ingress_vlan_tag); |
| 1591 | |
| 1592 | if ((msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_FLOW_BOTTOM_INTERFACE) && |
| 1593 | reply_cm->egress_vlan_hdr_cnt > 0) { |
| 1594 | reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_INSERT_EGRESS_VLAN_TAG; |
| 1595 | reply_cm->l2_hdr_size += reply_cm->egress_vlan_hdr_cnt * VLAN_HLEN; |
| 1596 | } |
| 1597 | } |
| 1598 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1599 | #ifdef CONFIG_NF_FLOW_COOKIE |
| 1600 | reply_cm->flow_cookie = 0; |
| 1601 | #endif |
Zhi Chen | 8748eb3 | 2015-06-18 12:58:48 -0700 | [diff] [blame] | 1602 | #ifdef CONFIG_XFRM |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1603 | if (msg->valid_flags & SFE_RULE_CREATE_DIRECTION_VALID) { |
| 1604 | reply_cm->flow_accel = msg->direction_rule.return_accel; |
| 1605 | } else { |
| 1606 | reply_cm->flow_accel = 1; |
| 1607 | } |
Zhi Chen | 8748eb3 | 2015-06-18 12:58:48 -0700 | [diff] [blame] | 1608 | #endif |
Nitin Shetty | e6ed5b5 | 2021-12-27 14:50:11 +0530 | [diff] [blame] | 1609 | |
| 1610 | /* |
| 1611 | * the inet6_protocol handler will be used only in decap path |
| 1612 | * for non passthrough case. |
| 1613 | */ |
| 1614 | original_cm->proto = NULL; |
| 1615 | reply_cm->proto = NULL; |
Tian Yang | 435afc4 | 2022-02-02 12:47:32 -0800 | [diff] [blame] | 1616 | original_cm->top_interface_dev = NULL; |
| 1617 | reply_cm->top_interface_dev = NULL; |
Nitin Shetty | e6ed5b5 | 2021-12-27 14:50:11 +0530 | [diff] [blame] | 1618 | |
| 1619 | #ifdef SFE_GRE_TUN_ENABLE |
| 1620 | if (!(reply_cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_PASSTHROUGH)) { |
| 1621 | rcu_read_lock(); |
| 1622 | reply_cm->proto = rcu_dereference(inet6_protos[tuple->protocol]); |
| 1623 | rcu_read_unlock(); |
| 1624 | |
| 1625 | if (unlikely(!reply_cm->proto)) { |
Tian Yang | 435afc4 | 2022-02-02 12:47:32 -0800 | [diff] [blame] | 1626 | this_cpu_inc(si->stats_pcpu->connection_create_failures64); |
| 1627 | spin_unlock_bh(&si->lock); |
Nitin Shetty | e6ed5b5 | 2021-12-27 14:50:11 +0530 | [diff] [blame] | 1628 | kfree(reply_cm); |
| 1629 | kfree(original_cm); |
| 1630 | kfree(c); |
| 1631 | dev_put(src_dev); |
| 1632 | dev_put(dest_dev); |
| 1633 | DEBUG_WARN("sfe: GRE proto handler is not registered\n"); |
| 1634 | return -EPERM; |
| 1635 | } |
| 1636 | } |
| 1637 | #endif |
| 1638 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1639 | /* |
Tian Yang | afb0345 | 2022-01-13 18:53:13 -0800 | [diff] [blame] | 1640 | * Decapsulation path have proto set. |
| 1641 | * This is used to differentiate de/encap, and call protocol specific handler. |
| 1642 | */ |
| 1643 | if (IPPROTO_IPIP == tuple->protocol) { |
| 1644 | original_cm->proto = NULL; |
| 1645 | rcu_read_lock(); |
| 1646 | reply_cm->proto = rcu_dereference(inet6_protos[tuple->protocol]); |
| 1647 | rcu_read_unlock(); |
Tian Yang | 435afc4 | 2022-02-02 12:47:32 -0800 | [diff] [blame] | 1648 | reply_cm->top_interface_dev = dev_get_by_index(&init_net, msg->conn_rule.return_top_interface_num); |
| 1649 | |
| 1650 | if (unlikely(!reply_cm->top_interface_dev)) { |
| 1651 | DEBUG_WARN("%px: Unable to find top_interface_dev corresponding to %d\n", msg, |
| 1652 | msg->conn_rule.return_top_interface_num); |
| 1653 | this_cpu_inc(si->stats_pcpu->connection_create_failures64); |
| 1654 | spin_unlock_bh(&si->lock); |
| 1655 | kfree(reply_cm); |
| 1656 | kfree(original_cm); |
| 1657 | kfree(c); |
| 1658 | dev_put(src_dev); |
| 1659 | dev_put(dest_dev); |
| 1660 | return -EINVAL; |
| 1661 | } |
Tian Yang | afb0345 | 2022-01-13 18:53:13 -0800 | [diff] [blame] | 1662 | } |
| 1663 | /* |
Ratheesh Kannoth | a3cf0e0 | 2021-12-09 09:44:10 +0530 | [diff] [blame] | 1664 | * If l2_features are disabled and flow uses l2 features such as macvlan/bridge/pppoe/vlan, |
| 1665 | * bottom interfaces are expected to be disabled in the flow rule and always top interfaces |
| 1666 | * are used. In such cases, do not use HW csum offload. csum offload is used only when we |
| 1667 | * are sending directly to the destination interface that supports it. |
| 1668 | */ |
Nitin Shetty | e6ed5b5 | 2021-12-27 14:50:11 +0530 | [diff] [blame] | 1669 | if (likely(src_dev->features & NETIF_F_HW_CSUM) && sfe_dev_has_hw_csum(src_dev)) { |
Ratheesh Kannoth | a3cf0e0 | 2021-12-09 09:44:10 +0530 | [diff] [blame] | 1670 | if ((msg->conn_rule.flow_top_interface_num == msg->conn_rule.flow_interface_num) || |
| 1671 | (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_FLOW_BOTTOM_INTERFACE)) { |
Ratheesh Kannoth | 4844553 | 2022-02-07 16:19:00 +0530 | [diff] [blame] | 1672 | /* |
| 1673 | * Dont enable CSUM offload |
| 1674 | */ |
| 1675 | #if 0 |
Ratheesh Kannoth | a3cf0e0 | 2021-12-09 09:44:10 +0530 | [diff] [blame] | 1676 | reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_CSUM_OFFLOAD; |
Ratheesh Kannoth | 4844553 | 2022-02-07 16:19:00 +0530 | [diff] [blame] | 1677 | #endif |
Ratheesh Kannoth | a3cf0e0 | 2021-12-09 09:44:10 +0530 | [diff] [blame] | 1678 | } |
| 1679 | } |
| 1680 | |
Murat Sezgin | 9c53897 | 2022-05-17 13:33:17 -0700 | [diff] [blame] | 1681 | if (msg->rule_flags & SFE_RULE_CREATE_FLAG_RETURN_SRC_INTERFACE_CHECK) { |
Ratheesh Kannoth | 5dee377 | 2022-01-18 11:27:14 +0530 | [diff] [blame] | 1682 | reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_SRC_INTERFACE_CHECK; |
| 1683 | } |
| 1684 | |
Murat Sezgin | 9c53897 | 2022-05-17 13:33:17 -0700 | [diff] [blame] | 1685 | if (msg->rule_flags & SFE_RULE_CREATE_FLAG_RETURN_SRC_INTERFACE_CHECK_NO_FLUSH) { |
| 1686 | reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_SRC_INTERFACE_CHECK_NO_FLUSH; |
| 1687 | } |
| 1688 | |
Ratheesh Kannoth | a3cf0e0 | 2021-12-09 09:44:10 +0530 | [diff] [blame] | 1689 | /* |
Ken Zhu | bbf4965 | 2021-09-12 15:33:09 -0700 | [diff] [blame] | 1690 | * For the non-arp interface, we don't write L2 HDR. |
Guduri Prathyusha | 79a5fee | 2021-11-11 17:59:10 +0530 | [diff] [blame] | 1691 | * Excluding PPPoE from this, since we are now supporting PPPoE encap/decap. |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1692 | */ |
Guduri Prathyusha | 79a5fee | 2021-11-11 17:59:10 +0530 | [diff] [blame] | 1693 | if (sfe_ipv6_xmit_eth_type_check(src_dev, reply_cm->flags)) { |
Ratheesh Kannoth | 29140aa | 2021-10-20 08:25:02 +0530 | [diff] [blame] | 1694 | |
| 1695 | /* |
| 1696 | * Check whether the rule has configured a specific source MAC address to use. |
| 1697 | * This is needed when virtual L3 interfaces such as br-lan, macvlan, vlan are used during egress |
| 1698 | */ |
Ratheesh Kannoth | 71fc51e | 2022-01-05 10:02:47 +0530 | [diff] [blame] | 1699 | if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) { |
| 1700 | ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)msg->conn_rule.return_mac); |
Ratheesh Kannoth | 29140aa | 2021-10-20 08:25:02 +0530 | [diff] [blame] | 1701 | } else { |
Ratheesh Kannoth | 71fc51e | 2022-01-05 10:02:47 +0530 | [diff] [blame] | 1702 | if ((msg->valid_flags & SFE_RULE_CREATE_SRC_MAC_VALID) && |
| 1703 | (msg->src_mac_rule.mac_valid_flags & SFE_SRC_MAC_FLOW_VALID)) { |
| 1704 | ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)msg->src_mac_rule.flow_src_mac); |
| 1705 | } else { |
| 1706 | ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)src_dev->dev_addr); |
| 1707 | } |
Ratheesh Kannoth | 29140aa | 2021-10-20 08:25:02 +0530 | [diff] [blame] | 1708 | } |
| 1709 | |
| 1710 | ether_addr_copy((u8 *)reply_cm->xmit_dest_mac, (u8 *)msg->conn_rule.flow_mac); |
| 1711 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1712 | reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_WRITE_L2_HDR; |
Wayne Tan | bb7f178 | 2021-12-13 11:16:04 -0800 | [diff] [blame] | 1713 | reply_cm->l2_hdr_size += ETH_HLEN; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1714 | |
| 1715 | /* |
| 1716 | * If our dev writes Ethernet headers then we can write a really fast |
| 1717 | * version. |
| 1718 | */ |
| 1719 | if (src_dev->header_ops) { |
| 1720 | if (src_dev->header_ops->create == eth_header) { |
| 1721 | reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR; |
| 1722 | } |
| 1723 | } |
| 1724 | } |
| 1725 | |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1726 | /* |
| 1727 | * No support for NAT in ipv6 |
| 1728 | */ |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1729 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1730 | /* |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1731 | * Initialize the protocol-specific information that we track. |
| 1732 | */ |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1733 | switch (tuple->protocol) { |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1734 | case IPPROTO_TCP: |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1735 | original_cm->protocol_state.tcp.win_scale = msg->tcp_rule.flow_window_scale; |
| 1736 | original_cm->protocol_state.tcp.max_win = msg->tcp_rule.flow_max_window ? msg->tcp_rule.flow_max_window : 1; |
| 1737 | original_cm->protocol_state.tcp.end = msg->tcp_rule.flow_end; |
| 1738 | original_cm->protocol_state.tcp.max_end = msg->tcp_rule.flow_max_end; |
| 1739 | reply_cm->protocol_state.tcp.win_scale = msg->tcp_rule.return_window_scale; |
| 1740 | reply_cm->protocol_state.tcp.max_win = msg->tcp_rule.return_max_window ? msg->tcp_rule.return_max_window : 1; |
| 1741 | reply_cm->protocol_state.tcp.end = msg->tcp_rule.return_end; |
| 1742 | reply_cm->protocol_state.tcp.max_end = msg->tcp_rule.return_max_end; |
| 1743 | if (msg->rule_flags & SFE_RULE_CREATE_FLAG_NO_SEQ_CHECK) { |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1744 | original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK; |
| 1745 | reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK; |
| 1746 | } |
| 1747 | break; |
| 1748 | } |
| 1749 | |
Wayne Tan | bb7f178 | 2021-12-13 11:16:04 -0800 | [diff] [blame] | 1750 | /* |
| 1751 | * Fill in the ipv6_connection object. |
| 1752 | */ |
| 1753 | c->protocol = tuple->protocol; |
| 1754 | c->src_ip[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip; |
| 1755 | c->src_ip_xlate[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip; |
| 1756 | c->src_port = tuple->flow_ident; |
| 1757 | c->src_port_xlate = tuple->flow_ident; |
| 1758 | c->original_dev = src_dev; |
| 1759 | c->original_match = original_cm; |
| 1760 | |
| 1761 | c->dest_ip[0] = *(struct sfe_ipv6_addr *)tuple->return_ip; |
| 1762 | c->dest_ip_xlate[0] = *(struct sfe_ipv6_addr *)tuple->return_ip; |
| 1763 | c->dest_port = tuple->return_ident; |
| 1764 | c->dest_port_xlate = tuple->return_ident; |
| 1765 | |
| 1766 | c->reply_dev = dest_dev; |
| 1767 | c->reply_match = reply_cm; |
| 1768 | c->debug_read_seq = 0; |
| 1769 | c->last_sync_jiffies = get_jiffies_64(); |
| 1770 | c->removed = false; |
| 1771 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1772 | sfe_ipv6_connection_match_compute_translations(original_cm); |
| 1773 | sfe_ipv6_connection_match_compute_translations(reply_cm); |
| 1774 | sfe_ipv6_insert_connection(si, c); |
| 1775 | |
| 1776 | spin_unlock_bh(&si->lock); |
| 1777 | |
| 1778 | /* |
| 1779 | * We have everything we need! |
| 1780 | */ |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1781 | DEBUG_INFO("new connection - p: %d\n" |
Tian Yang | 45f39c8 | 2020-10-06 14:07:47 -0700 | [diff] [blame] | 1782 | " s: %s:%pxM(%pxM):%pI6(%pI6):%u(%u)\n" |
| 1783 | " d: %s:%pxM(%pxM):%pI6(%pI6):%u(%u)\n", |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1784 | tuple->protocol, |
| 1785 | src_dev->name, msg->conn_rule.flow_mac, NULL, |
| 1786 | (void *)tuple->flow_ip, (void *)tuple->flow_ip, ntohs(tuple->flow_ident), ntohs(tuple->flow_ident), |
| 1787 | dest_dev->name, NULL, msg->conn_rule.return_mac, |
| 1788 | (void *)tuple->return_ip, (void *)tuple->return_ip, ntohs(tuple->return_ident), ntohs(tuple->return_ident)); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1789 | |
| 1790 | return 0; |
| 1791 | } |
| 1792 | |
| 1793 | /* |
| 1794 | * sfe_ipv6_destroy_rule() |
| 1795 | * Destroy a forwarding rule. |
| 1796 | */ |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1797 | void sfe_ipv6_destroy_rule(struct sfe_ipv6_rule_destroy_msg *msg) |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1798 | { |
| 1799 | struct sfe_ipv6 *si = &__si6; |
| 1800 | struct sfe_ipv6_connection *c; |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 1801 | bool ret; |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1802 | struct sfe_ipv6_5tuple *tuple = &msg->tuple; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1803 | |
Ratheesh Kannoth | 1ed9546 | 2021-10-20 07:57:45 +0530 | [diff] [blame] | 1804 | this_cpu_inc(si->stats_pcpu->connection_destroy_requests64); |
| 1805 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1806 | spin_lock_bh(&si->lock); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1807 | |
| 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 Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1812 | c = sfe_ipv6_find_connection(si, tuple->protocol, (struct sfe_ipv6_addr *)tuple->flow_ip, tuple->flow_ident, |
| 1813 | (struct sfe_ipv6_addr *)tuple->return_ip, tuple->return_ident); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1814 | if (!c) { |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1815 | spin_unlock_bh(&si->lock); |
| 1816 | |
Ratheesh Kannoth | 1ed9546 | 2021-10-20 07:57:45 +0530 | [diff] [blame] | 1817 | this_cpu_inc(si->stats_pcpu->connection_destroy_misses64); |
| 1818 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1819 | DEBUG_TRACE("connection does not exist - p: %d, s: %pI6:%u, d: %pI6:%u\n", |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1820 | tuple->protocol, tuple->flow_ip, ntohs(tuple->flow_ident), |
| 1821 | tuple->return_ip, ntohs(tuple->return_ident)); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1822 | return; |
| 1823 | } |
| 1824 | |
| 1825 | /* |
| 1826 | * Remove our connection details from the hash tables. |
| 1827 | */ |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 1828 | ret = sfe_ipv6_remove_connection(si, c); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1829 | spin_unlock_bh(&si->lock); |
| 1830 | |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 1831 | if (ret) { |
| 1832 | sfe_ipv6_flush_connection(si, c, SFE_SYNC_REASON_DESTROY); |
| 1833 | } |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1834 | |
| 1835 | DEBUG_INFO("connection destroyed - p: %d, s: %pI6:%u, d: %pI6:%u\n", |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1836 | tuple->protocol, tuple->flow_ip, ntohs(tuple->flow_ident), |
| 1837 | tuple->return_ip, ntohs(tuple->return_ident)); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1838 | } |
| 1839 | |
| 1840 | /* |
Ken Zhu | 7a43d88 | 2022-01-04 10:51:44 -0800 | [diff] [blame] | 1841 | * sfe_ipv6_sync_invoke() |
| 1842 | * Schedule many sync stats. |
| 1843 | */ |
| 1844 | bool sfe_ipv6_sync_invoke(uint16_t index) |
| 1845 | { |
| 1846 | struct sfe_ipv6 *si = &__si6; |
| 1847 | return schedule_delayed_work_on(si->work_cpu, &(si->sync_dwork), 0); |
| 1848 | } |
| 1849 | |
| 1850 | /* |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1851 | * sfe_ipv6_register_sync_rule_callback() |
| 1852 | * Register a callback for rule synchronization. |
| 1853 | */ |
| 1854 | void sfe_ipv6_register_sync_rule_callback(sfe_sync_rule_callback_t sync_rule_callback) |
| 1855 | { |
| 1856 | struct sfe_ipv6 *si = &__si6; |
| 1857 | |
| 1858 | spin_lock_bh(&si->lock); |
| 1859 | rcu_assign_pointer(si->sync_rule_callback, sync_rule_callback); |
| 1860 | spin_unlock_bh(&si->lock); |
| 1861 | } |
| 1862 | |
| 1863 | /* |
Ken Zhu | 7a43d88 | 2022-01-04 10:51:44 -0800 | [diff] [blame] | 1864 | * sfe_ipv6_register_sync_rule_callback() |
| 1865 | * Register a callback for rule synchronization. |
| 1866 | */ |
| 1867 | void sfe_ipv6_register_many_sync_callback(sfe_ipv6_many_sync_callback_t cb) |
| 1868 | { |
| 1869 | struct sfe_ipv6 *si = &__si6; |
| 1870 | |
| 1871 | spin_lock_bh(&si->lock); |
| 1872 | rcu_assign_pointer(si->many_sync_callback, cb); |
| 1873 | spin_unlock_bh(&si->lock); |
| 1874 | } |
| 1875 | |
| 1876 | /* |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1877 | * sfe_ipv6_get_debug_dev() |
| 1878 | */ |
| 1879 | static ssize_t sfe_ipv6_get_debug_dev(struct device *dev, |
| 1880 | struct device_attribute *attr, |
| 1881 | char *buf) |
| 1882 | { |
| 1883 | struct sfe_ipv6 *si = &__si6; |
| 1884 | ssize_t count; |
| 1885 | int num; |
| 1886 | |
| 1887 | spin_lock_bh(&si->lock); |
| 1888 | num = si->debug_dev; |
| 1889 | spin_unlock_bh(&si->lock); |
| 1890 | |
| 1891 | count = snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", num); |
| 1892 | return count; |
| 1893 | } |
| 1894 | |
| 1895 | /* |
| 1896 | * sfe_ipv6_destroy_all_rules_for_dev() |
| 1897 | * Destroy all connections that match a particular device. |
| 1898 | * |
| 1899 | * If we pass dev as NULL then this destroys all connections. |
| 1900 | */ |
| 1901 | void sfe_ipv6_destroy_all_rules_for_dev(struct net_device *dev) |
| 1902 | { |
| 1903 | struct sfe_ipv6 *si = &__si6; |
| 1904 | struct sfe_ipv6_connection *c; |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 1905 | bool ret; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1906 | |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 1907 | another_round: |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1908 | spin_lock_bh(&si->lock); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1909 | |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 1910 | for (c = si->all_connections_head; c; c = c->all_connections_next) { |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1911 | /* |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 1912 | * Does this connection relate to the device we are destroying? |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1913 | */ |
| 1914 | if (!dev |
| 1915 | || (dev == c->original_dev) |
| 1916 | || (dev == c->reply_dev)) { |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 1917 | break; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1918 | } |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 1919 | } |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1920 | |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 1921 | if (c) { |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 1922 | ret = sfe_ipv6_remove_connection(si, c); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1923 | } |
| 1924 | |
| 1925 | spin_unlock_bh(&si->lock); |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 1926 | |
| 1927 | if (c) { |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 1928 | if (ret) { |
| 1929 | sfe_ipv6_flush_connection(si, c, SFE_SYNC_REASON_DESTROY); |
| 1930 | } |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 1931 | goto another_round; |
| 1932 | } |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1933 | } |
| 1934 | |
| 1935 | /* |
| 1936 | * sfe_ipv6_periodic_sync() |
| 1937 | */ |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 1938 | static void sfe_ipv6_periodic_sync(struct work_struct *work) |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1939 | { |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 1940 | struct sfe_ipv6 *si = container_of((struct delayed_work *)work, struct sfe_ipv6, sync_dwork); |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 1941 | u64 now_jiffies; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1942 | int quota; |
Ken Zhu | 7a43d88 | 2022-01-04 10:51:44 -0800 | [diff] [blame] | 1943 | sfe_ipv6_many_sync_callback_t sync_rule_callback; |
Ken Zhu | 32b9539 | 2021-09-03 13:52:04 -0700 | [diff] [blame] | 1944 | struct sfe_ipv6_connection *c; |
Ken Zhu | 7a43d88 | 2022-01-04 10:51:44 -0800 | [diff] [blame] | 1945 | struct sfe_ipv6_conn_sync *conn_sync; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1946 | |
| 1947 | now_jiffies = get_jiffies_64(); |
| 1948 | |
| 1949 | rcu_read_lock(); |
Ken Zhu | 7a43d88 | 2022-01-04 10:51:44 -0800 | [diff] [blame] | 1950 | sync_rule_callback = rcu_dereference(si->many_sync_callback); |
| 1951 | rcu_read_unlock(); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1952 | if (!sync_rule_callback) { |
Ken Zhu | 7a43d88 | 2022-01-04 10:51:44 -0800 | [diff] [blame] | 1953 | return; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1954 | } |
| 1955 | |
| 1956 | spin_lock_bh(&si->lock); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1957 | |
| 1958 | /* |
Ken Zhu | 32b9539 | 2021-09-03 13:52:04 -0700 | [diff] [blame] | 1959 | * If we have reached the end of the connection list, walk from |
| 1960 | * the connection head. |
| 1961 | */ |
| 1962 | c = si->wc_next; |
| 1963 | if (unlikely(!c)) { |
| 1964 | c = si->all_connections_head; |
| 1965 | } |
Ken Zhu | 7a43d88 | 2022-01-04 10:51:44 -0800 | [diff] [blame] | 1966 | |
Ken Zhu | 32b9539 | 2021-09-03 13:52:04 -0700 | [diff] [blame] | 1967 | /* |
Ken Zhu | 7a43d88 | 2022-01-04 10:51:44 -0800 | [diff] [blame] | 1968 | * Get the max number of connections to be put in this sync msg. |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1969 | */ |
Ken Zhu | 7a43d88 | 2022-01-04 10:51:44 -0800 | [diff] [blame] | 1970 | quota = sfe_ipv6_sync_max_number; |
| 1971 | conn_sync = sfe_ipv6_sync_many_msg->msg.conn_stats_many.conn_sync; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1972 | |
| 1973 | /* |
Ken Zhu | 32b9539 | 2021-09-03 13:52:04 -0700 | [diff] [blame] | 1974 | * Walk the "all connection" list and sync the connection state. |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1975 | */ |
Ken Zhu | 32b9539 | 2021-09-03 13:52:04 -0700 | [diff] [blame] | 1976 | while (likely(c && quota)) { |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1977 | struct sfe_ipv6_connection_match *cm; |
| 1978 | struct sfe_ipv6_connection_match *counter_cm; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1979 | struct sfe_connection_sync sis; |
| 1980 | |
Ken Zhu | 32b9539 | 2021-09-03 13:52:04 -0700 | [diff] [blame] | 1981 | cm = c->original_match; |
| 1982 | counter_cm = c->reply_match; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1983 | |
| 1984 | /* |
Ken Zhu | 32b9539 | 2021-09-03 13:52:04 -0700 | [diff] [blame] | 1985 | * Didn't receive packets in the origial direction or reply |
| 1986 | * direction, move to the next connection. |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1987 | */ |
Ken Zhu | 32b9539 | 2021-09-03 13:52:04 -0700 | [diff] [blame] | 1988 | if (!atomic_read(&cm->rx_packet_count) && !atomic_read(&counter_cm->rx_packet_count)) { |
| 1989 | c = c->all_connections_next; |
| 1990 | continue; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1991 | } |
| 1992 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1993 | /* |
| 1994 | * Sync the connection state. |
| 1995 | */ |
Xiaoping Fan | 99cb4c1 | 2015-08-21 19:07:32 -0700 | [diff] [blame] | 1996 | sfe_ipv6_gen_sync_connection(si, c, &sis, SFE_SYNC_REASON_STATS, now_jiffies); |
Ken Zhu | 7a43d88 | 2022-01-04 10:51:44 -0800 | [diff] [blame] | 1997 | sfe_ipv6_stats_convert(conn_sync, &sis); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1998 | |
Ken Zhu | 7a43d88 | 2022-01-04 10:51:44 -0800 | [diff] [blame] | 1999 | quota--; |
| 2000 | conn_sync++; |
| 2001 | c = c->all_connections_next; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2002 | } |
| 2003 | |
Ken Zhu | 32b9539 | 2021-09-03 13:52:04 -0700 | [diff] [blame] | 2004 | /* |
| 2005 | * At the end of loop, put wc_next to the connection we left |
| 2006 | */ |
| 2007 | si->wc_next = c; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2008 | spin_unlock_bh(&si->lock); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2009 | |
Ken Zhu | 7a43d88 | 2022-01-04 10:51:44 -0800 | [diff] [blame] | 2010 | if (c == NULL) { |
| 2011 | DEBUG_INFO("Synced all connections\n"); |
| 2012 | sfe_ipv6_sync_many_msg->msg.conn_stats_many.next = 0; |
| 2013 | } else { |
| 2014 | DEBUG_INFO("Some connections left\n"); |
| 2015 | sfe_ipv6_sync_many_msg->msg.conn_stats_many.next = sfe_ipv6_sync_max_number - quota; |
| 2016 | } |
| 2017 | DEBUG_INFO("Synced [%d] connections\n", (sfe_ipv6_sync_max_number - quota)); |
| 2018 | |
| 2019 | sfe_ipv6_sync_many_msg->msg.conn_stats_many.count = sfe_ipv6_sync_max_number - quota; |
| 2020 | sfe_ipv6_sync_many_msg->cm.response = SFE_CMN_RESPONSE_ACK; |
| 2021 | |
| 2022 | sync_rule_callback(sfe_ipv6_sync_many_msg); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2023 | } |
| 2024 | |
| 2025 | /* |
| 2026 | * sfe_ipv6_debug_dev_read_start() |
| 2027 | * Generate part of the XML output. |
| 2028 | */ |
| 2029 | static bool sfe_ipv6_debug_dev_read_start(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length, |
| 2030 | int *total_read, struct sfe_ipv6_debug_xml_write_state *ws) |
| 2031 | { |
| 2032 | int bytes_read; |
| 2033 | |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 2034 | si->debug_read_seq++; |
| 2035 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2036 | bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "<sfe_ipv6>\n"); |
| 2037 | if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) { |
| 2038 | return false; |
| 2039 | } |
| 2040 | |
| 2041 | *length -= bytes_read; |
| 2042 | *total_read += bytes_read; |
| 2043 | |
| 2044 | ws->state++; |
| 2045 | return true; |
| 2046 | } |
| 2047 | |
| 2048 | /* |
| 2049 | * sfe_ipv6_debug_dev_read_connections_start() |
| 2050 | * Generate part of the XML output. |
| 2051 | */ |
| 2052 | static bool sfe_ipv6_debug_dev_read_connections_start(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length, |
| 2053 | int *total_read, struct sfe_ipv6_debug_xml_write_state *ws) |
| 2054 | { |
| 2055 | int bytes_read; |
| 2056 | |
| 2057 | bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<connections>\n"); |
| 2058 | if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) { |
| 2059 | return false; |
| 2060 | } |
| 2061 | |
| 2062 | *length -= bytes_read; |
| 2063 | *total_read += bytes_read; |
| 2064 | |
| 2065 | ws->state++; |
| 2066 | return true; |
| 2067 | } |
| 2068 | |
| 2069 | /* |
| 2070 | * sfe_ipv6_debug_dev_read_connections_connection() |
| 2071 | * Generate part of the XML output. |
| 2072 | */ |
| 2073 | static bool sfe_ipv6_debug_dev_read_connections_connection(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length, |
| 2074 | int *total_read, struct sfe_ipv6_debug_xml_write_state *ws) |
| 2075 | { |
| 2076 | struct sfe_ipv6_connection *c; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2077 | struct sfe_ipv6_connection_match *original_cm; |
| 2078 | struct sfe_ipv6_connection_match *reply_cm; |
| 2079 | int bytes_read; |
| 2080 | int protocol; |
| 2081 | struct net_device *src_dev; |
| 2082 | struct sfe_ipv6_addr src_ip; |
| 2083 | struct sfe_ipv6_addr src_ip_xlate; |
| 2084 | __be16 src_port; |
| 2085 | __be16 src_port_xlate; |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 2086 | u64 src_rx_packets; |
| 2087 | u64 src_rx_bytes; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2088 | struct net_device *dest_dev; |
| 2089 | struct sfe_ipv6_addr dest_ip; |
| 2090 | struct sfe_ipv6_addr dest_ip_xlate; |
| 2091 | __be16 dest_port; |
| 2092 | __be16 dest_port_xlate; |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 2093 | u64 dest_rx_packets; |
| 2094 | u64 dest_rx_bytes; |
| 2095 | u64 last_sync_jiffies; |
Ken Zhu | 37040ea | 2021-09-09 21:11:15 -0700 | [diff] [blame] | 2096 | u32 src_mark, dest_mark, src_priority, dest_priority, src_dscp, dest_dscp; |
Parikshit Gune | d31a820 | 2022-01-05 22:15:04 +0530 | [diff] [blame] | 2097 | bool original_cm_sawf_valid, reply_cm_sawf_valid; |
| 2098 | u32 flow_service_class, return_service_class; |
| 2099 | u32 flow_msduq, return_msduq; |
Guduri Prathyusha | eb31c90 | 2021-11-10 20:18:50 +0530 | [diff] [blame] | 2100 | u32 packet, byte, original_cm_flags; |
| 2101 | u16 pppoe_session_id; |
| 2102 | u8 pppoe_remote_mac[ETH_ALEN]; |
Ken Zhu | 7e38d1a | 2021-11-30 17:31:46 -0800 | [diff] [blame] | 2103 | u32 original_fast_xmit, reply_fast_xmit; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2104 | #ifdef CONFIG_NF_FLOW_COOKIE |
| 2105 | int src_flow_cookie, dst_flow_cookie; |
| 2106 | #endif |
| 2107 | |
| 2108 | spin_lock_bh(&si->lock); |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 2109 | |
| 2110 | for (c = si->all_connections_head; c; c = c->all_connections_next) { |
| 2111 | if (c->debug_read_seq < si->debug_read_seq) { |
| 2112 | c->debug_read_seq = si->debug_read_seq; |
| 2113 | break; |
| 2114 | } |
| 2115 | } |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2116 | |
| 2117 | /* |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 2118 | * If there were no connections then move to the next state. |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2119 | */ |
| 2120 | if (!c) { |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2121 | spin_unlock_bh(&si->lock); |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 2122 | ws->state++; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2123 | return true; |
| 2124 | } |
| 2125 | |
| 2126 | original_cm = c->original_match; |
| 2127 | reply_cm = c->reply_match; |
| 2128 | |
| 2129 | protocol = c->protocol; |
| 2130 | src_dev = c->original_dev; |
| 2131 | src_ip = c->src_ip[0]; |
| 2132 | src_ip_xlate = c->src_ip_xlate[0]; |
| 2133 | src_port = c->src_port; |
| 2134 | src_port_xlate = c->src_port_xlate; |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 2135 | src_priority = original_cm->priority; |
| 2136 | src_dscp = original_cm->dscp >> SFE_IPV6_DSCP_SHIFT; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2137 | |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 2138 | sfe_ipv6_connection_match_update_summary_stats(original_cm, &packet, &byte); |
| 2139 | sfe_ipv6_connection_match_update_summary_stats(reply_cm, &packet, &byte); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2140 | |
| 2141 | src_rx_packets = original_cm->rx_packet_count64; |
| 2142 | src_rx_bytes = original_cm->rx_byte_count64; |
Ken Zhu | 37040ea | 2021-09-09 21:11:15 -0700 | [diff] [blame] | 2143 | src_mark = original_cm->mark; |
Ken Zhu | 7e38d1a | 2021-11-30 17:31:46 -0800 | [diff] [blame] | 2144 | original_fast_xmit = original_cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_FAST_XMIT; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2145 | dest_dev = c->reply_dev; |
| 2146 | dest_ip = c->dest_ip[0]; |
| 2147 | dest_ip_xlate = c->dest_ip_xlate[0]; |
| 2148 | dest_port = c->dest_port; |
| 2149 | dest_port_xlate = c->dest_port_xlate; |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 2150 | dest_priority = reply_cm->priority; |
| 2151 | dest_dscp = reply_cm->dscp >> SFE_IPV6_DSCP_SHIFT; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2152 | dest_rx_packets = reply_cm->rx_packet_count64; |
| 2153 | dest_rx_bytes = reply_cm->rx_byte_count64; |
| 2154 | last_sync_jiffies = get_jiffies_64() - c->last_sync_jiffies; |
Guduri Prathyusha | eb31c90 | 2021-11-10 20:18:50 +0530 | [diff] [blame] | 2155 | original_cm_flags = original_cm->flags; |
| 2156 | pppoe_session_id = original_cm->pppoe_session_id; |
| 2157 | ether_addr_copy(pppoe_remote_mac, original_cm->pppoe_remote_mac); |
Ken Zhu | 37040ea | 2021-09-09 21:11:15 -0700 | [diff] [blame] | 2158 | dest_mark = reply_cm->mark; |
Ken Zhu | 7e38d1a | 2021-11-30 17:31:46 -0800 | [diff] [blame] | 2159 | reply_fast_xmit = reply_cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_FAST_XMIT; |
Parikshit Gune | d31a820 | 2022-01-05 22:15:04 +0530 | [diff] [blame] | 2160 | original_cm_sawf_valid = original_cm->sawf_valid; |
| 2161 | reply_cm_sawf_valid = reply_cm->sawf_valid; |
| 2162 | flow_service_class = SFE_GET_SAWF_SERVICE_CLASS(original_cm->mark); |
| 2163 | flow_msduq = SFE_GET_SAWF_MSDUQ(original_cm->mark); |
| 2164 | return_service_class = SFE_GET_SAWF_SERVICE_CLASS(reply_cm->mark); |
| 2165 | return_msduq = SFE_GET_SAWF_MSDUQ(reply_cm->mark); |
| 2166 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2167 | #ifdef CONFIG_NF_FLOW_COOKIE |
| 2168 | src_flow_cookie = original_cm->flow_cookie; |
| 2169 | dst_flow_cookie = reply_cm->flow_cookie; |
| 2170 | #endif |
| 2171 | spin_unlock_bh(&si->lock); |
| 2172 | |
| 2173 | bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t\t<connection " |
| 2174 | "protocol=\"%u\" " |
| 2175 | "src_dev=\"%s\" " |
| 2176 | "src_ip=\"%pI6\" src_ip_xlate=\"%pI6\" " |
| 2177 | "src_port=\"%u\" src_port_xlate=\"%u\" " |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 2178 | "src_priority=\"%u\" src_dscp=\"%u\" " |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2179 | "src_rx_pkts=\"%llu\" src_rx_bytes=\"%llu\" " |
Ken Zhu | 37040ea | 2021-09-09 21:11:15 -0700 | [diff] [blame] | 2180 | "src_mark=\"%08x\" " |
Ken Zhu | 7e38d1a | 2021-11-30 17:31:46 -0800 | [diff] [blame] | 2181 | "src_fast_xmit=\"%s\" " |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2182 | "dest_dev=\"%s\" " |
| 2183 | "dest_ip=\"%pI6\" dest_ip_xlate=\"%pI6\" " |
| 2184 | "dest_port=\"%u\" dest_port_xlate=\"%u\" " |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 2185 | "dest_priority=\"%u\" dest_dscp=\"%u\" " |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2186 | "dest_rx_pkts=\"%llu\" dest_rx_bytes=\"%llu\" " |
Ken Zhu | 37040ea | 2021-09-09 21:11:15 -0700 | [diff] [blame] | 2187 | "dest_mark=\"%08x\" " |
Ken Zhu | 7e38d1a | 2021-11-30 17:31:46 -0800 | [diff] [blame] | 2188 | "reply_fast_xmit=\"%s\" " |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2189 | #ifdef CONFIG_NF_FLOW_COOKIE |
| 2190 | "src_flow_cookie=\"%d\" dst_flow_cookie=\"%d\" " |
| 2191 | #endif |
Ken Zhu | 37040ea | 2021-09-09 21:11:15 -0700 | [diff] [blame] | 2192 | "last_sync=\"%llu\" ", |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2193 | protocol, |
| 2194 | src_dev->name, |
| 2195 | &src_ip, &src_ip_xlate, |
| 2196 | ntohs(src_port), ntohs(src_port_xlate), |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 2197 | src_priority, src_dscp, |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2198 | src_rx_packets, src_rx_bytes, |
Ken Zhu | 37040ea | 2021-09-09 21:11:15 -0700 | [diff] [blame] | 2199 | src_mark, |
Ken Zhu | 7e38d1a | 2021-11-30 17:31:46 -0800 | [diff] [blame] | 2200 | original_fast_xmit ? "Yes" : "No", |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2201 | dest_dev->name, |
| 2202 | &dest_ip, &dest_ip_xlate, |
| 2203 | ntohs(dest_port), ntohs(dest_port_xlate), |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 2204 | dest_priority, dest_dscp, |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2205 | dest_rx_packets, dest_rx_bytes, |
Ken Zhu | 37040ea | 2021-09-09 21:11:15 -0700 | [diff] [blame] | 2206 | dest_mark, |
Ken Zhu | 7e38d1a | 2021-11-30 17:31:46 -0800 | [diff] [blame] | 2207 | reply_fast_xmit ? "Yes" : "No", |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2208 | #ifdef CONFIG_NF_FLOW_COOKIE |
| 2209 | src_flow_cookie, dst_flow_cookie, |
| 2210 | #endif |
Ken Zhu | 37040ea | 2021-09-09 21:11:15 -0700 | [diff] [blame] | 2211 | last_sync_jiffies); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2212 | |
Guduri Prathyusha | eb31c90 | 2021-11-10 20:18:50 +0530 | [diff] [blame] | 2213 | if (original_cm_flags &= (SFE_IPV6_CONNECTION_MATCH_FLAG_PPPOE_DECAP | SFE_IPV6_CONNECTION_MATCH_FLAG_PPPOE_ENCAP)) { |
Guduri Prathyusha | 79a5fee | 2021-11-11 17:59:10 +0530 | [diff] [blame] | 2214 | bytes_read += snprintf(msg + bytes_read, CHAR_DEV_MSG_SIZE, "pppoe_session_id=\"%u\" pppoe_server_MAC=\"%pM\" ", |
Guduri Prathyusha | eb31c90 | 2021-11-10 20:18:50 +0530 | [diff] [blame] | 2215 | pppoe_session_id, pppoe_remote_mac); |
| 2216 | } |
| 2217 | |
Parikshit Gune | d31a820 | 2022-01-05 22:15:04 +0530 | [diff] [blame] | 2218 | if (original_cm_sawf_valid) { |
Parikshit Gune | fdd9865 | 2022-03-14 17:33:01 +0530 | [diff] [blame] | 2219 | bytes_read += snprintf(msg + bytes_read, CHAR_DEV_MSG_SIZE, "flow_service_class=\"%d\" flow_msduq= \"0x%x\" ", |
Parikshit Gune | d31a820 | 2022-01-05 22:15:04 +0530 | [diff] [blame] | 2220 | flow_service_class, flow_msduq); |
| 2221 | } |
| 2222 | |
| 2223 | if (reply_cm_sawf_valid) { |
Parikshit Gune | fdd9865 | 2022-03-14 17:33:01 +0530 | [diff] [blame] | 2224 | bytes_read += snprintf(msg + bytes_read, CHAR_DEV_MSG_SIZE, "return_service_class=\"%d\" return_msduq= \"0x%x\" ", |
Parikshit Gune | d31a820 | 2022-01-05 22:15:04 +0530 | [diff] [blame] | 2225 | return_service_class, return_msduq); |
| 2226 | } |
| 2227 | |
Guduri Prathyusha | eb31c90 | 2021-11-10 20:18:50 +0530 | [diff] [blame] | 2228 | bytes_read += snprintf(msg + bytes_read, CHAR_DEV_MSG_SIZE, ")/>\n"); |
| 2229 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2230 | if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) { |
| 2231 | return false; |
| 2232 | } |
| 2233 | |
| 2234 | *length -= bytes_read; |
| 2235 | *total_read += bytes_read; |
| 2236 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2237 | return true; |
| 2238 | } |
| 2239 | |
| 2240 | /* |
| 2241 | * sfe_ipv6_debug_dev_read_connections_end() |
| 2242 | * Generate part of the XML output. |
| 2243 | */ |
| 2244 | static bool sfe_ipv6_debug_dev_read_connections_end(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length, |
| 2245 | int *total_read, struct sfe_ipv6_debug_xml_write_state *ws) |
| 2246 | { |
| 2247 | int bytes_read; |
| 2248 | |
| 2249 | bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t</connections>\n"); |
| 2250 | if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) { |
| 2251 | return false; |
| 2252 | } |
| 2253 | |
| 2254 | *length -= bytes_read; |
| 2255 | *total_read += bytes_read; |
| 2256 | |
| 2257 | ws->state++; |
| 2258 | return true; |
| 2259 | } |
| 2260 | |
| 2261 | /* |
| 2262 | * sfe_ipv6_debug_dev_read_exceptions_start() |
| 2263 | * Generate part of the XML output. |
| 2264 | */ |
| 2265 | static bool sfe_ipv6_debug_dev_read_exceptions_start(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length, |
| 2266 | int *total_read, struct sfe_ipv6_debug_xml_write_state *ws) |
| 2267 | { |
| 2268 | int bytes_read; |
| 2269 | |
| 2270 | bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<exceptions>\n"); |
| 2271 | if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) { |
| 2272 | return false; |
| 2273 | } |
| 2274 | |
| 2275 | *length -= bytes_read; |
| 2276 | *total_read += bytes_read; |
| 2277 | |
| 2278 | ws->state++; |
| 2279 | return true; |
| 2280 | } |
| 2281 | |
| 2282 | /* |
| 2283 | * sfe_ipv6_debug_dev_read_exceptions_exception() |
| 2284 | * Generate part of the XML output. |
| 2285 | */ |
| 2286 | static bool sfe_ipv6_debug_dev_read_exceptions_exception(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length, |
| 2287 | int *total_read, struct sfe_ipv6_debug_xml_write_state *ws) |
| 2288 | { |
Ratheesh Kannoth | 1ed9546 | 2021-10-20 07:57:45 +0530 | [diff] [blame] | 2289 | int i; |
| 2290 | u64 val = 0; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2291 | |
Ratheesh Kannoth | 1ed9546 | 2021-10-20 07:57:45 +0530 | [diff] [blame] | 2292 | for_each_possible_cpu(i) { |
| 2293 | const struct sfe_ipv6_stats *s = per_cpu_ptr(si->stats_pcpu, i); |
| 2294 | val += s->exception_events64[ws->iter_exception]; |
| 2295 | } |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2296 | |
Ratheesh Kannoth | 1ed9546 | 2021-10-20 07:57:45 +0530 | [diff] [blame] | 2297 | if (val) { |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2298 | int bytes_read; |
| 2299 | |
| 2300 | bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, |
| 2301 | "\t\t<exception name=\"%s\" count=\"%llu\" />\n", |
| 2302 | sfe_ipv6_exception_events_string[ws->iter_exception], |
Ratheesh Kannoth | 1ed9546 | 2021-10-20 07:57:45 +0530 | [diff] [blame] | 2303 | val); |
| 2304 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2305 | if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) { |
| 2306 | return false; |
| 2307 | } |
| 2308 | |
| 2309 | *length -= bytes_read; |
| 2310 | *total_read += bytes_read; |
| 2311 | } |
| 2312 | |
| 2313 | ws->iter_exception++; |
| 2314 | if (ws->iter_exception >= SFE_IPV6_EXCEPTION_EVENT_LAST) { |
| 2315 | ws->iter_exception = 0; |
| 2316 | ws->state++; |
| 2317 | } |
| 2318 | |
| 2319 | return true; |
| 2320 | } |
| 2321 | |
| 2322 | /* |
| 2323 | * sfe_ipv6_debug_dev_read_exceptions_end() |
| 2324 | * Generate part of the XML output. |
| 2325 | */ |
| 2326 | static bool sfe_ipv6_debug_dev_read_exceptions_end(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length, |
| 2327 | int *total_read, struct sfe_ipv6_debug_xml_write_state *ws) |
| 2328 | { |
| 2329 | int bytes_read; |
| 2330 | |
| 2331 | bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t</exceptions>\n"); |
| 2332 | if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) { |
| 2333 | return false; |
| 2334 | } |
| 2335 | |
| 2336 | *length -= bytes_read; |
| 2337 | *total_read += bytes_read; |
| 2338 | |
| 2339 | ws->state++; |
| 2340 | return true; |
| 2341 | } |
| 2342 | |
| 2343 | /* |
| 2344 | * sfe_ipv6_debug_dev_read_stats() |
| 2345 | * Generate part of the XML output. |
| 2346 | */ |
| 2347 | static bool sfe_ipv6_debug_dev_read_stats(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length, |
| 2348 | int *total_read, struct sfe_ipv6_debug_xml_write_state *ws) |
| 2349 | { |
| 2350 | int bytes_read; |
Ratheesh Kannoth | 1ed9546 | 2021-10-20 07:57:45 +0530 | [diff] [blame] | 2351 | struct sfe_ipv6_stats stats; |
| 2352 | unsigned int num_conn; |
| 2353 | |
| 2354 | sfe_ipv6_update_summary_stats(si, &stats); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2355 | |
| 2356 | spin_lock_bh(&si->lock); |
Ratheesh Kannoth | 1ed9546 | 2021-10-20 07:57:45 +0530 | [diff] [blame] | 2357 | num_conn = si->num_connections; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2358 | spin_unlock_bh(&si->lock); |
| 2359 | |
| 2360 | bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<stats " |
| 2361 | "num_connections=\"%u\" " |
Suruchi Suman | 23a279d | 2021-11-16 15:13:09 +0530 | [diff] [blame] | 2362 | "pkts_dropped=\"%llu\" " |
Ken Zhu | 7e38d1a | 2021-11-30 17:31:46 -0800 | [diff] [blame] | 2363 | "pkts_fast_xmited=\"%llu\" " |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2364 | "pkts_forwarded=\"%llu\" pkts_not_forwarded=\"%llu\" " |
| 2365 | "create_requests=\"%llu\" create_collisions=\"%llu\" " |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 2366 | "create_failures=\"%llu\" " |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2367 | "destroy_requests=\"%llu\" destroy_misses=\"%llu\" " |
| 2368 | "flushes=\"%llu\" " |
Guduri Prathyusha | 647fe3e | 2021-11-22 19:17:51 +0530 | [diff] [blame] | 2369 | "hash_hits=\"%llu\" hash_reorders=\"%llu\" " |
| 2370 | "pppoe_encap_pkts_fwded=\"%llu\" " |
Guduri Prathyusha | 034d635 | 2022-01-12 16:49:04 +0530 | [diff] [blame] | 2371 | "pppoe_decap_pkts_fwded=\"%llu\" " |
| 2372 | "pppoe_bridge_pkts_fwded=\"%llu\" />\n", |
Ratheesh Kannoth | 1ed9546 | 2021-10-20 07:57:45 +0530 | [diff] [blame] | 2373 | |
| 2374 | num_conn, |
Suruchi Suman | 23a279d | 2021-11-16 15:13:09 +0530 | [diff] [blame] | 2375 | stats.packets_dropped64, |
Ken Zhu | 7e38d1a | 2021-11-30 17:31:46 -0800 | [diff] [blame] | 2376 | stats.packets_fast_xmited64, |
Ratheesh Kannoth | 1ed9546 | 2021-10-20 07:57:45 +0530 | [diff] [blame] | 2377 | stats.packets_forwarded64, |
| 2378 | stats.packets_not_forwarded64, |
| 2379 | stats.connection_create_requests64, |
| 2380 | stats.connection_create_collisions64, |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 2381 | stats.connection_create_failures64, |
Ratheesh Kannoth | 1ed9546 | 2021-10-20 07:57:45 +0530 | [diff] [blame] | 2382 | stats.connection_destroy_requests64, |
| 2383 | stats.connection_destroy_misses64, |
| 2384 | stats.connection_flushes64, |
| 2385 | stats.connection_match_hash_hits64, |
Guduri Prathyusha | 647fe3e | 2021-11-22 19:17:51 +0530 | [diff] [blame] | 2386 | stats.connection_match_hash_reorders64, |
| 2387 | stats.pppoe_encap_packets_forwarded64, |
Guduri Prathyusha | 034d635 | 2022-01-12 16:49:04 +0530 | [diff] [blame] | 2388 | stats.pppoe_decap_packets_forwarded64, |
| 2389 | stats.pppoe_bridge_packets_forwarded64); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2390 | if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) { |
| 2391 | return false; |
| 2392 | } |
| 2393 | |
| 2394 | *length -= bytes_read; |
| 2395 | *total_read += bytes_read; |
| 2396 | |
| 2397 | ws->state++; |
| 2398 | return true; |
| 2399 | } |
| 2400 | |
| 2401 | /* |
| 2402 | * sfe_ipv6_debug_dev_read_end() |
| 2403 | * Generate part of the XML output. |
| 2404 | */ |
| 2405 | static bool sfe_ipv6_debug_dev_read_end(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length, |
| 2406 | int *total_read, struct sfe_ipv6_debug_xml_write_state *ws) |
| 2407 | { |
| 2408 | int bytes_read; |
| 2409 | |
| 2410 | bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "</sfe_ipv6>\n"); |
| 2411 | if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) { |
| 2412 | return false; |
| 2413 | } |
| 2414 | |
| 2415 | *length -= bytes_read; |
| 2416 | *total_read += bytes_read; |
| 2417 | |
| 2418 | ws->state++; |
| 2419 | return true; |
| 2420 | } |
| 2421 | |
| 2422 | /* |
| 2423 | * Array of write functions that write various XML elements that correspond to |
| 2424 | * our XML output state machine. |
| 2425 | */ |
| 2426 | static sfe_ipv6_debug_xml_write_method_t sfe_ipv6_debug_xml_write_methods[SFE_IPV6_DEBUG_XML_STATE_DONE] = { |
| 2427 | sfe_ipv6_debug_dev_read_start, |
| 2428 | sfe_ipv6_debug_dev_read_connections_start, |
| 2429 | sfe_ipv6_debug_dev_read_connections_connection, |
| 2430 | sfe_ipv6_debug_dev_read_connections_end, |
| 2431 | sfe_ipv6_debug_dev_read_exceptions_start, |
| 2432 | sfe_ipv6_debug_dev_read_exceptions_exception, |
| 2433 | sfe_ipv6_debug_dev_read_exceptions_end, |
| 2434 | sfe_ipv6_debug_dev_read_stats, |
| 2435 | sfe_ipv6_debug_dev_read_end, |
| 2436 | }; |
| 2437 | |
| 2438 | /* |
| 2439 | * sfe_ipv6_debug_dev_read() |
| 2440 | * Send info to userspace upon read request from user |
| 2441 | */ |
| 2442 | static ssize_t sfe_ipv6_debug_dev_read(struct file *filp, char *buffer, size_t length, loff_t *offset) |
| 2443 | { |
| 2444 | char msg[CHAR_DEV_MSG_SIZE]; |
| 2445 | int total_read = 0; |
| 2446 | struct sfe_ipv6_debug_xml_write_state *ws; |
| 2447 | struct sfe_ipv6 *si = &__si6; |
| 2448 | |
| 2449 | ws = (struct sfe_ipv6_debug_xml_write_state *)filp->private_data; |
| 2450 | while ((ws->state != SFE_IPV6_DEBUG_XML_STATE_DONE) && (length > CHAR_DEV_MSG_SIZE)) { |
| 2451 | if ((sfe_ipv6_debug_xml_write_methods[ws->state])(si, buffer, msg, &length, &total_read, ws)) { |
| 2452 | continue; |
| 2453 | } |
| 2454 | } |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2455 | return total_read; |
| 2456 | } |
| 2457 | |
| 2458 | /* |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2459 | * sfe_ipv6_debug_dev_open() |
| 2460 | */ |
| 2461 | static int sfe_ipv6_debug_dev_open(struct inode *inode, struct file *file) |
| 2462 | { |
| 2463 | struct sfe_ipv6_debug_xml_write_state *ws; |
| 2464 | |
| 2465 | ws = (struct sfe_ipv6_debug_xml_write_state *)file->private_data; |
| 2466 | if (ws) { |
| 2467 | return 0; |
| 2468 | } |
| 2469 | |
| 2470 | ws = kzalloc(sizeof(struct sfe_ipv6_debug_xml_write_state), GFP_KERNEL); |
| 2471 | if (!ws) { |
| 2472 | return -ENOMEM; |
| 2473 | } |
| 2474 | |
| 2475 | ws->state = SFE_IPV6_DEBUG_XML_STATE_START; |
| 2476 | file->private_data = ws; |
| 2477 | |
| 2478 | return 0; |
| 2479 | } |
| 2480 | |
| 2481 | /* |
| 2482 | * sfe_ipv6_debug_dev_release() |
| 2483 | */ |
| 2484 | static int sfe_ipv6_debug_dev_release(struct inode *inode, struct file *file) |
| 2485 | { |
| 2486 | struct sfe_ipv6_debug_xml_write_state *ws; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2487 | |
| 2488 | ws = (struct sfe_ipv6_debug_xml_write_state *)file->private_data; |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 2489 | if (ws) { |
| 2490 | /* |
| 2491 | * We've finished with our output so free the write state. |
| 2492 | */ |
| 2493 | kfree(ws); |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 2494 | file->private_data = NULL; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2495 | } |
| 2496 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2497 | return 0; |
| 2498 | } |
| 2499 | |
| 2500 | /* |
| 2501 | * File operations used in the debug char device |
| 2502 | */ |
| 2503 | static struct file_operations sfe_ipv6_debug_dev_fops = { |
| 2504 | .read = sfe_ipv6_debug_dev_read, |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2505 | .open = sfe_ipv6_debug_dev_open, |
| 2506 | .release = sfe_ipv6_debug_dev_release |
| 2507 | }; |
| 2508 | |
| 2509 | #ifdef CONFIG_NF_FLOW_COOKIE |
| 2510 | /* |
| 2511 | * sfe_ipv6_register_flow_cookie_cb |
| 2512 | * register a function in SFE to let SFE use this function to configure flow cookie for a flow |
| 2513 | * |
| 2514 | * Hardware driver which support flow cookie should register a callback function in SFE. Then SFE |
| 2515 | * can use this function to configure flow cookie for a flow. |
| 2516 | * return: 0, success; !=0, fail |
| 2517 | */ |
| 2518 | int sfe_ipv6_register_flow_cookie_cb(sfe_ipv6_flow_cookie_set_func_t cb) |
| 2519 | { |
| 2520 | struct sfe_ipv6 *si = &__si6; |
| 2521 | |
| 2522 | BUG_ON(!cb); |
| 2523 | |
| 2524 | if (si->flow_cookie_set_func) { |
| 2525 | return -1; |
| 2526 | } |
| 2527 | |
| 2528 | rcu_assign_pointer(si->flow_cookie_set_func, cb); |
| 2529 | return 0; |
| 2530 | } |
| 2531 | |
| 2532 | /* |
| 2533 | * sfe_ipv6_unregister_flow_cookie_cb |
| 2534 | * unregister function which is used to configure flow cookie for a flow |
| 2535 | * |
| 2536 | * return: 0, success; !=0, fail |
| 2537 | */ |
| 2538 | int sfe_ipv6_unregister_flow_cookie_cb(sfe_ipv6_flow_cookie_set_func_t cb) |
| 2539 | { |
| 2540 | struct sfe_ipv6 *si = &__si6; |
| 2541 | |
| 2542 | RCU_INIT_POINTER(si->flow_cookie_set_func, NULL); |
| 2543 | return 0; |
| 2544 | } |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 2545 | |
| 2546 | /* |
| 2547 | * sfe_ipv6_get_flow_cookie() |
| 2548 | */ |
| 2549 | static ssize_t sfe_ipv6_get_flow_cookie(struct device *dev, |
| 2550 | struct device_attribute *attr, |
| 2551 | char *buf) |
| 2552 | { |
| 2553 | struct sfe_ipv6 *si = &__si6; |
Xiaoping Fan | 01c67cc | 2015-11-09 11:31:57 -0800 | [diff] [blame] | 2554 | return snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", si->flow_cookie_enable); |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 2555 | } |
| 2556 | |
| 2557 | /* |
| 2558 | * sfe_ipv6_set_flow_cookie() |
| 2559 | */ |
| 2560 | static ssize_t sfe_ipv6_set_flow_cookie(struct device *dev, |
| 2561 | struct device_attribute *attr, |
| 2562 | const char *buf, size_t size) |
| 2563 | { |
| 2564 | struct sfe_ipv6 *si = &__si6; |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 2565 | si->flow_cookie_enable = strict_strtol(buf, NULL, 0); |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 2566 | |
| 2567 | return size; |
| 2568 | } |
| 2569 | |
| 2570 | /* |
| 2571 | * sysfs attributes. |
| 2572 | */ |
| 2573 | static const struct device_attribute sfe_ipv6_flow_cookie_attr = |
Xiaoping Fan | e70da41 | 2016-02-26 16:47:57 -0800 | [diff] [blame] | 2574 | __ATTR(flow_cookie_enable, S_IWUSR | S_IRUGO, sfe_ipv6_get_flow_cookie, sfe_ipv6_set_flow_cookie); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2575 | #endif /*CONFIG_NF_FLOW_COOKIE*/ |
| 2576 | |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 2577 | /* |
| 2578 | * sfe_ipv6_get_cpu() |
| 2579 | */ |
| 2580 | static ssize_t sfe_ipv6_get_cpu(struct device *dev, |
| 2581 | struct device_attribute *attr, |
| 2582 | char *buf) |
| 2583 | { |
| 2584 | struct sfe_ipv6 *si = &__si6; |
| 2585 | return snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", si->work_cpu); |
| 2586 | } |
| 2587 | |
| 2588 | /* |
Wayne Tan | bb7f178 | 2021-12-13 11:16:04 -0800 | [diff] [blame] | 2589 | * sfe_ipv6_set_cpu() |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 2590 | */ |
| 2591 | static ssize_t sfe_ipv6_set_cpu(struct device *dev, |
| 2592 | struct device_attribute *attr, |
| 2593 | const char *buf, size_t size) |
| 2594 | { |
| 2595 | struct sfe_ipv6 *si = &__si6; |
| 2596 | int work_cpu; |
| 2597 | |
| 2598 | work_cpu = simple_strtol(buf, NULL, 0); |
| 2599 | if ((work_cpu >= 0) && (work_cpu <= NR_CPUS)) { |
| 2600 | si->work_cpu = work_cpu; |
| 2601 | } else { |
| 2602 | dev_err(dev, "%s is not in valid range[0,%d]", buf, NR_CPUS); |
| 2603 | } |
| 2604 | |
| 2605 | return size; |
| 2606 | } |
| 2607 | /* |
| 2608 | * sysfs attributes. |
| 2609 | */ |
| 2610 | static const struct device_attribute sfe_ipv6_cpu_attr = |
| 2611 | __ATTR(stat_work_cpu, S_IWUSR | S_IRUGO, sfe_ipv6_get_cpu, sfe_ipv6_set_cpu); |
| 2612 | |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 2613 | /* |
| 2614 | * sfe_ipv6_hash_init() |
| 2615 | * Initialize conn match hash lists |
| 2616 | */ |
| 2617 | static void sfe_ipv6_conn_match_hash_init(struct sfe_ipv6 *si, int len) |
| 2618 | { |
| 2619 | struct hlist_head *hash_list = si->hlist_conn_match_hash_head; |
| 2620 | int i; |
| 2621 | |
| 2622 | for (i = 0; i < len; i++) { |
| 2623 | INIT_HLIST_HEAD(&hash_list[i]); |
| 2624 | } |
| 2625 | } |
| 2626 | |
Suruchi Suman | 23a279d | 2021-11-16 15:13:09 +0530 | [diff] [blame] | 2627 | #ifdef SFE_PROCESS_LOCAL_OUT |
| 2628 | /* |
| 2629 | * sfe_ipv6_local_out() |
| 2630 | * Called for packets from ip_local_out() - post encapsulation & other packets |
| 2631 | */ |
| 2632 | static unsigned int sfe_ipv6_local_out(void *priv, |
| 2633 | struct sk_buff *skb, |
| 2634 | const struct nf_hook_state *nhs) |
| 2635 | { |
Nitin Shetty | c28f817 | 2022-02-04 16:23:46 +0530 | [diff] [blame] | 2636 | struct sfe_l2_info l2_info = {0}; |
| 2637 | |
Suruchi Suman | 23a279d | 2021-11-16 15:13:09 +0530 | [diff] [blame] | 2638 | DEBUG_TRACE("sfe: sfe_ipv6_local_out hook called.\n"); |
| 2639 | |
| 2640 | if (likely(skb->skb_iif)) { |
Nitin Shetty | c28f817 | 2022-02-04 16:23:46 +0530 | [diff] [blame] | 2641 | return sfe_ipv6_recv(skb->dev, skb, &l2_info, true) ? NF_STOLEN : NF_ACCEPT; |
Suruchi Suman | 23a279d | 2021-11-16 15:13:09 +0530 | [diff] [blame] | 2642 | } |
| 2643 | |
| 2644 | return NF_ACCEPT; |
| 2645 | } |
| 2646 | |
| 2647 | /* |
| 2648 | * struct nf_hook_ops sfe_ipv6_ops_local_out[] |
| 2649 | * Hooks into netfilter local out packet monitoring points. |
| 2650 | */ |
| 2651 | static struct nf_hook_ops sfe_ipv6_ops_local_out[] __read_mostly = { |
| 2652 | |
| 2653 | /* |
| 2654 | * Local out routing hook is used to monitor packets. |
| 2655 | */ |
| 2656 | { |
| 2657 | .hook = sfe_ipv6_local_out, |
| 2658 | .pf = PF_INET6, |
| 2659 | .hooknum = NF_INET_LOCAL_OUT, |
| 2660 | .priority = NF_IP6_PRI_FIRST, |
| 2661 | }, |
| 2662 | }; |
| 2663 | #endif |
| 2664 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2665 | /* |
| 2666 | * sfe_ipv6_init() |
| 2667 | */ |
Ratheesh Kannoth | 24fb1db | 2021-10-20 07:28:06 +0530 | [diff] [blame] | 2668 | int sfe_ipv6_init(void) |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2669 | { |
| 2670 | struct sfe_ipv6 *si = &__si6; |
| 2671 | int result = -1; |
| 2672 | |
| 2673 | DEBUG_INFO("SFE IPv6 init\n"); |
| 2674 | |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 2675 | sfe_ipv6_conn_match_hash_init(si, ARRAY_SIZE(si->hlist_conn_match_hash_head)); |
| 2676 | |
Ratheesh Kannoth | 1ed9546 | 2021-10-20 07:57:45 +0530 | [diff] [blame] | 2677 | si->stats_pcpu = alloc_percpu_gfp(struct sfe_ipv6_stats, GFP_KERNEL | __GFP_ZERO); |
| 2678 | if (!si->stats_pcpu) { |
| 2679 | DEBUG_ERROR("failed to allocate stats memory for sfe_ipv6\n"); |
| 2680 | goto exit0; |
| 2681 | } |
| 2682 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2683 | /* |
Parikshit Gune | d31a820 | 2022-01-05 22:15:04 +0530 | [diff] [blame] | 2684 | * Allocate per cpu per service class memory. |
| 2685 | */ |
| 2686 | si->stats_pcpu_psc = alloc_percpu_gfp(struct sfe_ipv6_service_class_stats_db, |
| 2687 | GFP_KERNEL | __GFP_ZERO); |
| 2688 | if (!si->stats_pcpu_psc) { |
| 2689 | DEBUG_ERROR("failed to allocate per cpu per service clas stats memory\n"); |
| 2690 | goto exit1; |
| 2691 | } |
| 2692 | |
| 2693 | /* |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2694 | * Create sys/sfe_ipv6 |
| 2695 | */ |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 2696 | si->sys_ipv6 = kobject_create_and_add("sfe_ipv6", NULL); |
| 2697 | if (!si->sys_ipv6) { |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2698 | DEBUG_ERROR("failed to register sfe_ipv6\n"); |
Parikshit Gune | d31a820 | 2022-01-05 22:15:04 +0530 | [diff] [blame] | 2699 | goto exit2; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2700 | } |
| 2701 | |
| 2702 | /* |
| 2703 | * Create files, one for each parameter supported by this module. |
| 2704 | */ |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 2705 | result = sysfs_create_file(si->sys_ipv6, &sfe_ipv6_debug_dev_attr.attr); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2706 | if (result) { |
| 2707 | DEBUG_ERROR("failed to register debug dev file: %d\n", result); |
Parikshit Gune | d31a820 | 2022-01-05 22:15:04 +0530 | [diff] [blame] | 2708 | goto exit3; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2709 | } |
| 2710 | |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 2711 | result = sysfs_create_file(si->sys_ipv6, &sfe_ipv6_cpu_attr.attr); |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 2712 | if (result) { |
| 2713 | DEBUG_ERROR("failed to register debug dev file: %d\n", result); |
Parikshit Gune | d31a820 | 2022-01-05 22:15:04 +0530 | [diff] [blame] | 2714 | goto exit4; |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 2715 | } |
| 2716 | |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 2717 | #ifdef CONFIG_NF_FLOW_COOKIE |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 2718 | result = sysfs_create_file(si->sys_ipv6, &sfe_ipv6_flow_cookie_attr.attr); |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 2719 | if (result) { |
| 2720 | DEBUG_ERROR("failed to register flow cookie enable file: %d\n", result); |
Parikshit Gune | d31a820 | 2022-01-05 22:15:04 +0530 | [diff] [blame] | 2721 | goto exit5; |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 2722 | } |
| 2723 | #endif /* CONFIG_NF_FLOW_COOKIE */ |
| 2724 | |
Suruchi Suman | 23a279d | 2021-11-16 15:13:09 +0530 | [diff] [blame] | 2725 | #ifdef SFE_PROCESS_LOCAL_OUT |
| 2726 | #if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0)) |
| 2727 | result = nf_register_hooks(sfe_ipv6_ops_local_out, ARRAY_SIZE(sfe_ipv6_ops_local_out)); |
| 2728 | #else |
| 2729 | result = nf_register_net_hooks(&init_net, sfe_ipv6_ops_local_out, ARRAY_SIZE(sfe_ipv6_ops_local_out)); |
| 2730 | #endif |
Suruchi Suman | 23a279d | 2021-11-16 15:13:09 +0530 | [diff] [blame] | 2731 | if (result < 0) { |
| 2732 | DEBUG_ERROR("can't register nf local out hook: %d\n", result); |
Parikshit Gune | d31a820 | 2022-01-05 22:15:04 +0530 | [diff] [blame] | 2733 | goto exit6; |
Suruchi Suman | 23a279d | 2021-11-16 15:13:09 +0530 | [diff] [blame] | 2734 | } |
Murat Sezgin | c09b132 | 2022-03-16 10:15:38 -0700 | [diff] [blame] | 2735 | DEBUG_INFO("Register nf local out hook success: %d\n", result); |
| 2736 | #endif |
Suruchi Suman | 23a279d | 2021-11-16 15:13:09 +0530 | [diff] [blame] | 2737 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2738 | /* |
| 2739 | * Register our debug char device. |
| 2740 | */ |
| 2741 | result = register_chrdev(0, "sfe_ipv6", &sfe_ipv6_debug_dev_fops); |
| 2742 | if (result < 0) { |
| 2743 | DEBUG_ERROR("Failed to register chrdev: %d\n", result); |
Parikshit Gune | d31a820 | 2022-01-05 22:15:04 +0530 | [diff] [blame] | 2744 | goto exit7; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2745 | } |
| 2746 | |
| 2747 | si->debug_dev = result; |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 2748 | si->work_cpu = WORK_CPU_UNBOUND; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2749 | |
| 2750 | /* |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 2751 | * Create work to handle periodic statistics. |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2752 | */ |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 2753 | INIT_DELAYED_WORK(&(si->sync_dwork), sfe_ipv6_periodic_sync); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2754 | |
Ken Zhu | 7a43d88 | 2022-01-04 10:51:44 -0800 | [diff] [blame] | 2755 | sfe_ipv6_sync_many_msg = kzalloc(PAGE_SIZE, GFP_KERNEL); |
| 2756 | if(!sfe_ipv6_sync_many_msg) { |
| 2757 | goto exit8; |
| 2758 | } |
| 2759 | |
| 2760 | sfe_ipv6_msg_init(sfe_ipv6_sync_many_msg, SFE_SPECIAL_INTERFACE_IPV6, |
| 2761 | SFE_TX_CONN_STATS_SYNC_MANY_MSG, |
| 2762 | sizeof(struct sfe_ipv4_conn_sync_many_msg), |
| 2763 | NULL, |
| 2764 | NULL); |
| 2765 | sfe_ipv6_sync_max_number = (PAGE_SIZE - sizeof(struct sfe_ipv6_msg)) / sizeof(struct sfe_ipv6_conn_sync); |
| 2766 | |
| 2767 | spin_lock_init(&si->lock); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2768 | return 0; |
| 2769 | |
Ken Zhu | 7a43d88 | 2022-01-04 10:51:44 -0800 | [diff] [blame] | 2770 | exit8: |
| 2771 | unregister_chrdev(si->debug_dev, "sfe_ipv6"); |
| 2772 | |
Parikshit Gune | d31a820 | 2022-01-05 22:15:04 +0530 | [diff] [blame] | 2773 | exit7: |
Suruchi Suman | 23a279d | 2021-11-16 15:13:09 +0530 | [diff] [blame] | 2774 | #ifdef SFE_PROCESS_LOCAL_OUT |
| 2775 | #if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0)) |
| 2776 | DEBUG_TRACE("sfe: Unregister local out hook\n"); |
| 2777 | nf_unregister_hooks(sfe_ipv6_ops_local_out, ARRAY_SIZE(sfe_ipv6_ops_local_out)); |
| 2778 | #else |
| 2779 | DEBUG_TRACE("sfe: Unregister local out hook\n"); |
| 2780 | nf_unregister_net_hooks(&init_net, sfe_ipv6_ops_local_out, ARRAY_SIZE(sfe_ipv6_ops_local_out)); |
| 2781 | #endif |
Parikshit Gune | d31a820 | 2022-01-05 22:15:04 +0530 | [diff] [blame] | 2782 | exit6: |
Murat Sezgin | c09b132 | 2022-03-16 10:15:38 -0700 | [diff] [blame] | 2783 | #endif |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 2784 | #ifdef CONFIG_NF_FLOW_COOKIE |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 2785 | sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_flow_cookie_attr.attr); |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 2786 | |
Parikshit Gune | d31a820 | 2022-01-05 22:15:04 +0530 | [diff] [blame] | 2787 | exit5: |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 2788 | #endif /* CONFIG_NF_FLOW_COOKIE */ |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 2789 | sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_cpu_attr.attr); |
Suruchi Suman | 23a279d | 2021-11-16 15:13:09 +0530 | [diff] [blame] | 2790 | |
Parikshit Gune | d31a820 | 2022-01-05 22:15:04 +0530 | [diff] [blame] | 2791 | exit4: |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 2792 | sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_debug_dev_attr.attr); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2793 | |
Parikshit Gune | d31a820 | 2022-01-05 22:15:04 +0530 | [diff] [blame] | 2794 | exit3: |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 2795 | kobject_put(si->sys_ipv6); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2796 | |
Parikshit Gune | d31a820 | 2022-01-05 22:15:04 +0530 | [diff] [blame] | 2797 | exit2: |
| 2798 | free_percpu(si->stats_pcpu_psc); |
| 2799 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2800 | exit1: |
Ratheesh Kannoth | 1ed9546 | 2021-10-20 07:57:45 +0530 | [diff] [blame] | 2801 | free_percpu(si->stats_pcpu); |
| 2802 | |
| 2803 | exit0: |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2804 | return result; |
| 2805 | } |
| 2806 | |
| 2807 | /* |
| 2808 | * sfe_ipv6_exit() |
| 2809 | */ |
Ratheesh Kannoth | 24fb1db | 2021-10-20 07:28:06 +0530 | [diff] [blame] | 2810 | void sfe_ipv6_exit(void) |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2811 | { |
| 2812 | struct sfe_ipv6 *si = &__si6; |
| 2813 | |
| 2814 | DEBUG_INFO("SFE IPv6 exit\n"); |
| 2815 | |
| 2816 | /* |
| 2817 | * Destroy all connections. |
| 2818 | */ |
| 2819 | sfe_ipv6_destroy_all_rules_for_dev(NULL); |
| 2820 | |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 2821 | cancel_delayed_work(&si->sync_dwork); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2822 | |
| 2823 | unregister_chrdev(si->debug_dev, "sfe_ipv6"); |
| 2824 | |
Ratheesh Kannoth | 1ed9546 | 2021-10-20 07:57:45 +0530 | [diff] [blame] | 2825 | free_percpu(si->stats_pcpu); |
Parikshit Gune | d31a820 | 2022-01-05 22:15:04 +0530 | [diff] [blame] | 2826 | free_percpu(si->stats_pcpu_psc); |
Ratheesh Kannoth | 1ed9546 | 2021-10-20 07:57:45 +0530 | [diff] [blame] | 2827 | |
Suruchi Suman | 23a279d | 2021-11-16 15:13:09 +0530 | [diff] [blame] | 2828 | #ifdef SFE_PROCESS_LOCAL_OUT |
| 2829 | #if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0)) |
| 2830 | DEBUG_TRACE("sfe: Unregister local out hook\n"); |
| 2831 | nf_unregister_hooks(sfe_ipv6_ops_local_out, ARRAY_SIZE(sfe_ipv6_ops_local_out)); |
| 2832 | #else |
| 2833 | DEBUG_TRACE("sfe: Unregister local out hook\n"); |
| 2834 | nf_unregister_net_hooks(&init_net, sfe_ipv6_ops_local_out, ARRAY_SIZE(sfe_ipv6_ops_local_out)); |
| 2835 | #endif |
| 2836 | #endif |
| 2837 | |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 2838 | #ifdef CONFIG_NF_FLOW_COOKIE |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 2839 | sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_flow_cookie_attr.attr); |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 2840 | #endif /* CONFIG_NF_FLOW_COOKIE */ |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 2841 | |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 2842 | sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_cpu_attr.attr); |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 2843 | |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 2844 | sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_debug_dev_attr.attr); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2845 | |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 2846 | kobject_put(si->sys_ipv6); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2847 | } |
| 2848 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2849 | #ifdef CONFIG_NF_FLOW_COOKIE |
| 2850 | EXPORT_SYMBOL(sfe_ipv6_register_flow_cookie_cb); |
| 2851 | EXPORT_SYMBOL(sfe_ipv6_unregister_flow_cookie_cb); |
| 2852 | #endif |