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