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