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