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