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; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 924 | |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 925 | src_dev = dev_get_by_index(&init_net, msg->conn_rule.flow_top_interface_num); |
| 926 | if (!src_dev) { |
| 927 | DEBUG_WARN("%px: Unable to find src_dev corresponding to %d\n", msg, |
| 928 | msg->conn_rule.flow_top_interface_num); |
| 929 | this_cpu_inc(si->stats_pcpu->connection_create_failures64); |
| 930 | return -EINVAL; |
| 931 | } |
| 932 | |
| 933 | dest_dev = dev_get_by_index(&init_net, msg->conn_rule.return_top_interface_num); |
| 934 | if (!dest_dev) { |
| 935 | DEBUG_WARN("%px: Unable to find dest_dev corresponding to %d\n", msg, |
| 936 | msg->conn_rule.return_top_interface_num); |
| 937 | this_cpu_inc(si->stats_pcpu->connection_create_failures64); |
| 938 | dev_put(src_dev); |
| 939 | return -EINVAL; |
| 940 | } |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 941 | |
| 942 | if (unlikely((dest_dev->reg_state != NETREG_REGISTERED) || |
| 943 | (src_dev->reg_state != NETREG_REGISTERED))) { |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 944 | DEBUG_WARN("%px: src_dev=%s and dest_dev=%s are unregistered\n", msg, |
| 945 | src_dev->name, dest_dev->name); |
| 946 | this_cpu_inc(si->stats_pcpu->connection_create_failures64); |
| 947 | dev_put(src_dev); |
| 948 | dev_put(dest_dev); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 949 | return -EINVAL; |
| 950 | } |
| 951 | |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 952 | /* |
| 953 | * Allocate the various connection tracking objects. |
| 954 | */ |
| 955 | c = (struct sfe_ipv6_connection *)kmalloc(sizeof(struct sfe_ipv6_connection), GFP_ATOMIC); |
| 956 | if (unlikely(!c)) { |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 957 | DEBUG_WARN("%px: memory allocation of connection entry failed\n", msg); |
| 958 | this_cpu_inc(si->stats_pcpu->connection_create_failures64); |
| 959 | dev_put(src_dev); |
| 960 | dev_put(dest_dev); |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 961 | return -ENOMEM; |
| 962 | } |
| 963 | |
| 964 | original_cm = (struct sfe_ipv6_connection_match *)kmalloc(sizeof(struct sfe_ipv6_connection_match), GFP_ATOMIC); |
| 965 | if (unlikely(!original_cm)) { |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 966 | this_cpu_inc(si->stats_pcpu->connection_create_failures64); |
| 967 | DEBUG_WARN("%px: memory allocation of connection match entry failed\n", msg); |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 968 | kfree(c); |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 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 | reply_cm = (struct sfe_ipv6_connection_match *)kmalloc(sizeof(struct sfe_ipv6_connection_match), GFP_ATOMIC); |
| 975 | if (unlikely(!reply_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(original_cm); |
| 979 | kfree(c); |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 980 | dev_put(src_dev); |
| 981 | dev_put(dest_dev); |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 982 | return -ENOMEM; |
| 983 | } |
| 984 | |
Ratheesh Kannoth | 1ed9546 | 2021-10-20 07:57:45 +0530 | [diff] [blame] | 985 | this_cpu_inc(si->stats_pcpu->connection_create_requests64); |
| 986 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 987 | spin_lock_bh(&si->lock); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 988 | |
| 989 | /* |
| 990 | * Check to see if there is already a flow that matches the rule we're |
| 991 | * trying to create. If there is then we can't create a new one. |
| 992 | */ |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 993 | old_c = sfe_ipv6_find_connection(si, tuple->protocol, (struct sfe_ipv6_addr *)tuple->flow_ip, tuple->flow_ident, |
| 994 | (struct sfe_ipv6_addr *)tuple->return_ip, tuple->return_ident); |
| 995 | |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 996 | if (old_c != NULL) { |
Ratheesh Kannoth | 1ed9546 | 2021-10-20 07:57:45 +0530 | [diff] [blame] | 997 | this_cpu_inc(si->stats_pcpu->connection_create_collisions64); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 998 | |
| 999 | /* |
| 1000 | * If we already have the flow then it's likely that this |
| 1001 | * request to create the connection rule contains more |
| 1002 | * up-to-date information. Check and update accordingly. |
| 1003 | */ |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1004 | sfe_ipv6_update_protocol_state(old_c, msg); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1005 | spin_unlock_bh(&si->lock); |
| 1006 | |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 1007 | kfree(reply_cm); |
| 1008 | kfree(original_cm); |
| 1009 | kfree(c); |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1010 | dev_put(src_dev); |
| 1011 | dev_put(dest_dev); |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 1012 | |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1013 | DEBUG_TRACE("connection already exists - p: %d\n" |
Tian Yang | 45f39c8 | 2020-10-06 14:07:47 -0700 | [diff] [blame] | 1014 | " s: %s:%pxM:%pI6:%u, d: %s:%pxM:%pI6:%u\n", |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1015 | tuple->protocol, |
| 1016 | src_dev->name, msg->conn_rule.flow_mac, tuple->flow_ip, ntohs(tuple->flow_ident), |
| 1017 | 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] | 1018 | return -EADDRINUSE; |
| 1019 | } |
| 1020 | |
| 1021 | /* |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1022 | * Fill in the "original" direction connection matching object. |
| 1023 | * Note that the transmit MAC address is "dest_mac_xlate" because |
| 1024 | * we always know both ends of a connection by their translated |
| 1025 | * addresses and not their public addresses. |
| 1026 | */ |
| 1027 | original_cm->match_dev = src_dev; |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1028 | original_cm->match_protocol = tuple->protocol; |
| 1029 | original_cm->match_src_ip[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip; |
| 1030 | original_cm->match_src_port = tuple->flow_ident; |
| 1031 | original_cm->match_dest_ip[0] = *(struct sfe_ipv6_addr *)tuple->return_ip; |
| 1032 | original_cm->match_dest_port = tuple->return_ident; |
| 1033 | |
| 1034 | original_cm->xlate_src_ip[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip; |
| 1035 | original_cm->xlate_src_port = tuple->flow_ident; |
| 1036 | original_cm->xlate_dest_ip[0] = *(struct sfe_ipv6_addr *)tuple->return_ip; |
| 1037 | original_cm->xlate_dest_port = tuple->return_ident; |
| 1038 | |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 1039 | atomic_set(&original_cm->rx_packet_count, 0); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1040 | original_cm->rx_packet_count64 = 0; |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 1041 | atomic_set(&original_cm->rx_byte_count, 0); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1042 | original_cm->rx_byte_count64 = 0; |
| 1043 | original_cm->xmit_dev = dest_dev; |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1044 | |
| 1045 | original_cm->xmit_dev_mtu = msg->conn_rule.return_mtu; |
Ratheesh Kannoth | 29140aa | 2021-10-20 08:25:02 +0530 | [diff] [blame^] | 1046 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1047 | original_cm->connection = c; |
| 1048 | original_cm->counter_match = reply_cm; |
| 1049 | original_cm->flags = 0; |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1050 | if (msg->valid_flags & SFE_RULE_CREATE_QOS_VALID) { |
| 1051 | original_cm->priority = msg->qos_rule.flow_qos_tag; |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 1052 | original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_PRIORITY_REMARK; |
| 1053 | } |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1054 | if (msg->valid_flags & SFE_RULE_CREATE_DSCP_MARKING_VALID) { |
| 1055 | original_cm->dscp = msg->dscp_rule.flow_dscp << SFE_IPV6_DSCP_SHIFT; |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 1056 | original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_DSCP_REMARK; |
| 1057 | } |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1058 | #ifdef CONFIG_NF_FLOW_COOKIE |
| 1059 | original_cm->flow_cookie = 0; |
| 1060 | #endif |
Zhi Chen | 8748eb3 | 2015-06-18 12:58:48 -0700 | [diff] [blame] | 1061 | #ifdef CONFIG_XFRM |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1062 | if (msg->valid_flags & SFE_RULE_CREATE_DIRECTION_VALID) { |
| 1063 | original_cm->flow_accel = msg->direction_rule.flow_accel; |
| 1064 | } else { |
| 1065 | original_cm->flow_accel = 1; |
| 1066 | } |
Zhi Chen | 8748eb3 | 2015-06-18 12:58:48 -0700 | [diff] [blame] | 1067 | #endif |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1068 | |
| 1069 | /* |
Ken Zhu | bbf4965 | 2021-09-12 15:33:09 -0700 | [diff] [blame] | 1070 | * For the non-arp interface, we don't write L2 HDR. |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1071 | */ |
Ken Zhu | bbf4965 | 2021-09-12 15:33:09 -0700 | [diff] [blame] | 1072 | if (!(dest_dev->flags & IFF_NOARP)) { |
Ratheesh Kannoth | 29140aa | 2021-10-20 08:25:02 +0530 | [diff] [blame^] | 1073 | |
| 1074 | /* |
| 1075 | * Check whether the rule has configured a specific source MAC address to use. |
| 1076 | * This is needed when virtual L3 interfaces such as br-lan, macvlan, vlan are used during egress |
| 1077 | */ |
| 1078 | if ((msg->valid_flags & SFE_RULE_CREATE_SRC_MAC_VALID) && |
| 1079 | (msg->src_mac_rule.mac_valid_flags & SFE_SRC_MAC_RETURN_VALID)) { |
| 1080 | ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)msg->src_mac_rule.return_src_mac); |
| 1081 | } else { |
| 1082 | ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)dest_dev->dev_addr); |
| 1083 | } |
| 1084 | ether_addr_copy((u8 *)original_cm->xmit_dest_mac, (u8 *)msg->conn_rule.return_mac); |
| 1085 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1086 | original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_WRITE_L2_HDR; |
| 1087 | |
| 1088 | /* |
| 1089 | * 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] | 1090 | * version |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1091 | */ |
| 1092 | if (dest_dev->header_ops) { |
| 1093 | if (dest_dev->header_ops->create == eth_header) { |
| 1094 | original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR; |
| 1095 | } |
| 1096 | } |
| 1097 | } |
| 1098 | |
| 1099 | /* |
| 1100 | * Fill in the "reply" direction connection matching object. |
| 1101 | */ |
| 1102 | reply_cm->match_dev = dest_dev; |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1103 | reply_cm->match_protocol = tuple->protocol; |
| 1104 | reply_cm->match_src_ip[0] = *(struct sfe_ipv6_addr *)tuple->return_ip; |
| 1105 | reply_cm->match_src_port = tuple->return_ident; |
| 1106 | reply_cm->match_dest_ip[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip; |
| 1107 | reply_cm->match_dest_port = tuple->flow_ident; |
| 1108 | reply_cm->xlate_src_ip[0] = *(struct sfe_ipv6_addr *)tuple->return_ip; |
| 1109 | reply_cm->xlate_src_port = tuple->return_ident; |
| 1110 | reply_cm->xlate_dest_ip[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip; |
| 1111 | reply_cm->xlate_dest_port = tuple->flow_ident; |
| 1112 | |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 1113 | atomic_set(&original_cm->rx_byte_count, 0); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1114 | reply_cm->rx_packet_count64 = 0; |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 1115 | atomic_set(&reply_cm->rx_byte_count, 0); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1116 | reply_cm->rx_byte_count64 = 0; |
| 1117 | reply_cm->xmit_dev = src_dev; |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1118 | reply_cm->xmit_dev_mtu = msg->conn_rule.flow_mtu; |
Ratheesh Kannoth | 29140aa | 2021-10-20 08:25:02 +0530 | [diff] [blame^] | 1119 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1120 | reply_cm->connection = c; |
| 1121 | reply_cm->counter_match = original_cm; |
| 1122 | reply_cm->flags = 0; |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1123 | if (msg->valid_flags & SFE_RULE_CREATE_QOS_VALID) { |
| 1124 | reply_cm->priority = msg->qos_rule.return_qos_tag; |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 1125 | reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_PRIORITY_REMARK; |
| 1126 | } |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1127 | if (msg->valid_flags & SFE_RULE_CREATE_DSCP_MARKING_VALID) { |
| 1128 | reply_cm->dscp = msg->dscp_rule.return_dscp << SFE_IPV6_DSCP_SHIFT; |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 1129 | reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_DSCP_REMARK; |
| 1130 | } |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1131 | #ifdef CONFIG_NF_FLOW_COOKIE |
| 1132 | reply_cm->flow_cookie = 0; |
| 1133 | #endif |
Zhi Chen | 8748eb3 | 2015-06-18 12:58:48 -0700 | [diff] [blame] | 1134 | #ifdef CONFIG_XFRM |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1135 | if (msg->valid_flags & SFE_RULE_CREATE_DIRECTION_VALID) { |
| 1136 | reply_cm->flow_accel = msg->direction_rule.return_accel; |
| 1137 | } else { |
| 1138 | reply_cm->flow_accel = 1; |
| 1139 | } |
Zhi Chen | 8748eb3 | 2015-06-18 12:58:48 -0700 | [diff] [blame] | 1140 | #endif |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1141 | /* |
Ken Zhu | bbf4965 | 2021-09-12 15:33:09 -0700 | [diff] [blame] | 1142 | * For the non-arp interface, we don't write L2 HDR. |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1143 | */ |
Ken Zhu | bbf4965 | 2021-09-12 15:33:09 -0700 | [diff] [blame] | 1144 | if (!(src_dev->flags & IFF_NOARP)) { |
Ratheesh Kannoth | 29140aa | 2021-10-20 08:25:02 +0530 | [diff] [blame^] | 1145 | |
| 1146 | /* |
| 1147 | * Check whether the rule has configured a specific source MAC address to use. |
| 1148 | * This is needed when virtual L3 interfaces such as br-lan, macvlan, vlan are used during egress |
| 1149 | */ |
| 1150 | if ((msg->valid_flags & SFE_RULE_CREATE_SRC_MAC_VALID) && |
| 1151 | (msg->src_mac_rule.mac_valid_flags & SFE_SRC_MAC_FLOW_VALID)) { |
| 1152 | ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)msg->src_mac_rule.flow_src_mac); |
| 1153 | } else { |
| 1154 | ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)src_dev->dev_addr); |
| 1155 | } |
| 1156 | |
| 1157 | ether_addr_copy((u8 *)reply_cm->xmit_dest_mac, (u8 *)msg->conn_rule.flow_mac); |
| 1158 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1159 | reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_WRITE_L2_HDR; |
| 1160 | |
| 1161 | /* |
| 1162 | * If our dev writes Ethernet headers then we can write a really fast |
| 1163 | * version. |
| 1164 | */ |
| 1165 | if (src_dev->header_ops) { |
| 1166 | if (src_dev->header_ops->create == eth_header) { |
| 1167 | reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR; |
| 1168 | } |
| 1169 | } |
| 1170 | } |
| 1171 | |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1172 | /* |
| 1173 | * No support for NAT in ipv6 |
| 1174 | */ |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1175 | |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1176 | c->protocol = tuple->protocol; |
| 1177 | c->src_ip[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip; |
| 1178 | c->src_ip_xlate[0] = *(struct sfe_ipv6_addr *)tuple->flow_ip; |
| 1179 | c->src_port = tuple->flow_ident; |
| 1180 | c->src_port_xlate = tuple->flow_ident; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1181 | c->original_dev = src_dev; |
| 1182 | c->original_match = original_cm; |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1183 | |
| 1184 | c->dest_ip[0] = *(struct sfe_ipv6_addr *)tuple->return_ip; |
| 1185 | c->dest_ip_xlate[0] = *(struct sfe_ipv6_addr *)tuple->return_ip; |
| 1186 | c->dest_port = tuple->return_ident; |
| 1187 | c->dest_port_xlate = tuple->return_ident; |
| 1188 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1189 | c->reply_dev = dest_dev; |
| 1190 | c->reply_match = reply_cm; |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1191 | c->mark = 0; /* No mark support */ |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 1192 | c->debug_read_seq = 0; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1193 | c->last_sync_jiffies = get_jiffies_64(); |
Ratheesh Kannoth | 1ed9546 | 2021-10-20 07:57:45 +0530 | [diff] [blame] | 1194 | c->removed = false; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1195 | |
| 1196 | /* |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1197 | * Initialize the protocol-specific information that we track. |
| 1198 | */ |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1199 | switch (tuple->protocol) { |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1200 | case IPPROTO_TCP: |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1201 | original_cm->protocol_state.tcp.win_scale = msg->tcp_rule.flow_window_scale; |
| 1202 | original_cm->protocol_state.tcp.max_win = msg->tcp_rule.flow_max_window ? msg->tcp_rule.flow_max_window : 1; |
| 1203 | original_cm->protocol_state.tcp.end = msg->tcp_rule.flow_end; |
| 1204 | original_cm->protocol_state.tcp.max_end = msg->tcp_rule.flow_max_end; |
| 1205 | reply_cm->protocol_state.tcp.win_scale = msg->tcp_rule.return_window_scale; |
| 1206 | reply_cm->protocol_state.tcp.max_win = msg->tcp_rule.return_max_window ? msg->tcp_rule.return_max_window : 1; |
| 1207 | reply_cm->protocol_state.tcp.end = msg->tcp_rule.return_end; |
| 1208 | reply_cm->protocol_state.tcp.max_end = msg->tcp_rule.return_max_end; |
| 1209 | if (msg->rule_flags & SFE_RULE_CREATE_FLAG_NO_SEQ_CHECK) { |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1210 | original_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK; |
| 1211 | reply_cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK; |
| 1212 | } |
| 1213 | break; |
| 1214 | } |
| 1215 | |
| 1216 | sfe_ipv6_connection_match_compute_translations(original_cm); |
| 1217 | sfe_ipv6_connection_match_compute_translations(reply_cm); |
| 1218 | sfe_ipv6_insert_connection(si, c); |
| 1219 | |
| 1220 | spin_unlock_bh(&si->lock); |
| 1221 | |
| 1222 | /* |
| 1223 | * We have everything we need! |
| 1224 | */ |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1225 | DEBUG_INFO("new connection - p: %d\n" |
Tian Yang | 45f39c8 | 2020-10-06 14:07:47 -0700 | [diff] [blame] | 1226 | " s: %s:%pxM(%pxM):%pI6(%pI6):%u(%u)\n" |
| 1227 | " d: %s:%pxM(%pxM):%pI6(%pI6):%u(%u)\n", |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1228 | tuple->protocol, |
| 1229 | src_dev->name, msg->conn_rule.flow_mac, NULL, |
| 1230 | (void *)tuple->flow_ip, (void *)tuple->flow_ip, ntohs(tuple->flow_ident), ntohs(tuple->flow_ident), |
| 1231 | dest_dev->name, NULL, msg->conn_rule.return_mac, |
| 1232 | (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] | 1233 | |
| 1234 | return 0; |
| 1235 | } |
| 1236 | |
| 1237 | /* |
| 1238 | * sfe_ipv6_destroy_rule() |
| 1239 | * Destroy a forwarding rule. |
| 1240 | */ |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1241 | void sfe_ipv6_destroy_rule(struct sfe_ipv6_rule_destroy_msg *msg) |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1242 | { |
| 1243 | struct sfe_ipv6 *si = &__si6; |
| 1244 | struct sfe_ipv6_connection *c; |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 1245 | bool ret; |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1246 | struct sfe_ipv6_5tuple *tuple = &msg->tuple; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1247 | |
Ratheesh Kannoth | 1ed9546 | 2021-10-20 07:57:45 +0530 | [diff] [blame] | 1248 | this_cpu_inc(si->stats_pcpu->connection_destroy_requests64); |
| 1249 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1250 | spin_lock_bh(&si->lock); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1251 | |
| 1252 | /* |
| 1253 | * Check to see if we have a flow that matches the rule we're trying |
| 1254 | * to destroy. If there isn't then we can't destroy it. |
| 1255 | */ |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1256 | c = sfe_ipv6_find_connection(si, tuple->protocol, (struct sfe_ipv6_addr *)tuple->flow_ip, tuple->flow_ident, |
| 1257 | (struct sfe_ipv6_addr *)tuple->return_ip, tuple->return_ident); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1258 | if (!c) { |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1259 | spin_unlock_bh(&si->lock); |
| 1260 | |
Ratheesh Kannoth | 1ed9546 | 2021-10-20 07:57:45 +0530 | [diff] [blame] | 1261 | this_cpu_inc(si->stats_pcpu->connection_destroy_misses64); |
| 1262 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1263 | 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] | 1264 | tuple->protocol, tuple->flow_ip, ntohs(tuple->flow_ident), |
| 1265 | tuple->return_ip, ntohs(tuple->return_ident)); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1266 | return; |
| 1267 | } |
| 1268 | |
| 1269 | /* |
| 1270 | * Remove our connection details from the hash tables. |
| 1271 | */ |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 1272 | ret = sfe_ipv6_remove_connection(si, c); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1273 | spin_unlock_bh(&si->lock); |
| 1274 | |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 1275 | if (ret) { |
| 1276 | sfe_ipv6_flush_connection(si, c, SFE_SYNC_REASON_DESTROY); |
| 1277 | } |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1278 | |
| 1279 | 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] | 1280 | tuple->protocol, tuple->flow_ip, ntohs(tuple->flow_ident), |
| 1281 | tuple->return_ip, ntohs(tuple->return_ident)); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1282 | } |
| 1283 | |
| 1284 | /* |
| 1285 | * sfe_ipv6_register_sync_rule_callback() |
| 1286 | * Register a callback for rule synchronization. |
| 1287 | */ |
| 1288 | void sfe_ipv6_register_sync_rule_callback(sfe_sync_rule_callback_t sync_rule_callback) |
| 1289 | { |
| 1290 | struct sfe_ipv6 *si = &__si6; |
| 1291 | |
| 1292 | spin_lock_bh(&si->lock); |
| 1293 | rcu_assign_pointer(si->sync_rule_callback, sync_rule_callback); |
| 1294 | spin_unlock_bh(&si->lock); |
| 1295 | } |
| 1296 | |
| 1297 | /* |
| 1298 | * sfe_ipv6_get_debug_dev() |
| 1299 | */ |
| 1300 | static ssize_t sfe_ipv6_get_debug_dev(struct device *dev, |
| 1301 | struct device_attribute *attr, |
| 1302 | char *buf) |
| 1303 | { |
| 1304 | struct sfe_ipv6 *si = &__si6; |
| 1305 | ssize_t count; |
| 1306 | int num; |
| 1307 | |
| 1308 | spin_lock_bh(&si->lock); |
| 1309 | num = si->debug_dev; |
| 1310 | spin_unlock_bh(&si->lock); |
| 1311 | |
| 1312 | count = snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", num); |
| 1313 | return count; |
| 1314 | } |
| 1315 | |
| 1316 | /* |
| 1317 | * sfe_ipv6_destroy_all_rules_for_dev() |
| 1318 | * Destroy all connections that match a particular device. |
| 1319 | * |
| 1320 | * If we pass dev as NULL then this destroys all connections. |
| 1321 | */ |
| 1322 | void sfe_ipv6_destroy_all_rules_for_dev(struct net_device *dev) |
| 1323 | { |
| 1324 | struct sfe_ipv6 *si = &__si6; |
| 1325 | struct sfe_ipv6_connection *c; |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 1326 | bool ret; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1327 | |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 1328 | another_round: |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1329 | spin_lock_bh(&si->lock); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1330 | |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 1331 | for (c = si->all_connections_head; c; c = c->all_connections_next) { |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1332 | /* |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 1333 | * Does this connection relate to the device we are destroying? |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1334 | */ |
| 1335 | if (!dev |
| 1336 | || (dev == c->original_dev) |
| 1337 | || (dev == c->reply_dev)) { |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 1338 | break; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1339 | } |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 1340 | } |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1341 | |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 1342 | if (c) { |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 1343 | ret = sfe_ipv6_remove_connection(si, c); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1344 | } |
| 1345 | |
| 1346 | spin_unlock_bh(&si->lock); |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 1347 | |
| 1348 | if (c) { |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 1349 | if (ret) { |
| 1350 | sfe_ipv6_flush_connection(si, c, SFE_SYNC_REASON_DESTROY); |
| 1351 | } |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 1352 | goto another_round; |
| 1353 | } |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1354 | } |
| 1355 | |
| 1356 | /* |
| 1357 | * sfe_ipv6_periodic_sync() |
| 1358 | */ |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 1359 | static void sfe_ipv6_periodic_sync(struct work_struct *work) |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1360 | { |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 1361 | 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] | 1362 | u64 now_jiffies; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1363 | int quota; |
| 1364 | sfe_sync_rule_callback_t sync_rule_callback; |
Ken Zhu | 32b9539 | 2021-09-03 13:52:04 -0700 | [diff] [blame] | 1365 | struct sfe_ipv6_connection *c; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1366 | |
| 1367 | now_jiffies = get_jiffies_64(); |
| 1368 | |
| 1369 | rcu_read_lock(); |
| 1370 | sync_rule_callback = rcu_dereference(si->sync_rule_callback); |
| 1371 | if (!sync_rule_callback) { |
| 1372 | rcu_read_unlock(); |
| 1373 | goto done; |
| 1374 | } |
| 1375 | |
| 1376 | spin_lock_bh(&si->lock); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1377 | |
| 1378 | /* |
Ken Zhu | 32b9539 | 2021-09-03 13:52:04 -0700 | [diff] [blame] | 1379 | * If we have reached the end of the connection list, walk from |
| 1380 | * the connection head. |
| 1381 | */ |
| 1382 | c = si->wc_next; |
| 1383 | if (unlikely(!c)) { |
| 1384 | c = si->all_connections_head; |
| 1385 | } |
| 1386 | /* |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1387 | * Get an estimate of the number of connections to parse in this sync. |
| 1388 | */ |
| 1389 | quota = (si->num_connections + 63) / 64; |
| 1390 | |
| 1391 | /* |
Ken Zhu | 32b9539 | 2021-09-03 13:52:04 -0700 | [diff] [blame] | 1392 | * Walk the "all connection" list and sync the connection state. |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1393 | */ |
Ken Zhu | 32b9539 | 2021-09-03 13:52:04 -0700 | [diff] [blame] | 1394 | while (likely(c && quota)) { |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1395 | struct sfe_ipv6_connection_match *cm; |
| 1396 | struct sfe_ipv6_connection_match *counter_cm; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1397 | struct sfe_connection_sync sis; |
| 1398 | |
Ken Zhu | 32b9539 | 2021-09-03 13:52:04 -0700 | [diff] [blame] | 1399 | cm = c->original_match; |
| 1400 | counter_cm = c->reply_match; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1401 | |
| 1402 | /* |
Ken Zhu | 32b9539 | 2021-09-03 13:52:04 -0700 | [diff] [blame] | 1403 | * Didn't receive packets in the origial direction or reply |
| 1404 | * direction, move to the next connection. |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1405 | */ |
Ken Zhu | 32b9539 | 2021-09-03 13:52:04 -0700 | [diff] [blame] | 1406 | if (!atomic_read(&cm->rx_packet_count) && !atomic_read(&counter_cm->rx_packet_count)) { |
| 1407 | c = c->all_connections_next; |
| 1408 | continue; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1409 | } |
| 1410 | |
Ken Zhu | 32b9539 | 2021-09-03 13:52:04 -0700 | [diff] [blame] | 1411 | quota--; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1412 | |
| 1413 | /* |
| 1414 | * Sync the connection state. |
| 1415 | */ |
Xiaoping Fan | 99cb4c1 | 2015-08-21 19:07:32 -0700 | [diff] [blame] | 1416 | 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] | 1417 | |
Ken Zhu | 32b9539 | 2021-09-03 13:52:04 -0700 | [diff] [blame] | 1418 | si->wc_next = c->all_connections_next; |
| 1419 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1420 | spin_unlock_bh(&si->lock); |
| 1421 | sync_rule_callback(&sis); |
| 1422 | spin_lock_bh(&si->lock); |
Ken Zhu | 32b9539 | 2021-09-03 13:52:04 -0700 | [diff] [blame] | 1423 | |
| 1424 | /* |
| 1425 | * c must be set and used in the same lock/unlock window; |
| 1426 | * because c could be removed when we don't hold the lock, |
| 1427 | * so delay grabbing until after the callback and relock. |
| 1428 | */ |
| 1429 | c = si->wc_next; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1430 | } |
| 1431 | |
Ken Zhu | 32b9539 | 2021-09-03 13:52:04 -0700 | [diff] [blame] | 1432 | /* |
| 1433 | * At the end of loop, put wc_next to the connection we left |
| 1434 | */ |
| 1435 | si->wc_next = c; |
| 1436 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1437 | spin_unlock_bh(&si->lock); |
| 1438 | rcu_read_unlock(); |
| 1439 | |
| 1440 | done: |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 1441 | 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] | 1442 | } |
| 1443 | |
| 1444 | /* |
| 1445 | * sfe_ipv6_debug_dev_read_start() |
| 1446 | * Generate part of the XML output. |
| 1447 | */ |
| 1448 | static bool sfe_ipv6_debug_dev_read_start(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length, |
| 1449 | int *total_read, struct sfe_ipv6_debug_xml_write_state *ws) |
| 1450 | { |
| 1451 | int bytes_read; |
| 1452 | |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 1453 | si->debug_read_seq++; |
| 1454 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1455 | bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "<sfe_ipv6>\n"); |
| 1456 | if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) { |
| 1457 | return false; |
| 1458 | } |
| 1459 | |
| 1460 | *length -= bytes_read; |
| 1461 | *total_read += bytes_read; |
| 1462 | |
| 1463 | ws->state++; |
| 1464 | return true; |
| 1465 | } |
| 1466 | |
| 1467 | /* |
| 1468 | * sfe_ipv6_debug_dev_read_connections_start() |
| 1469 | * Generate part of the XML output. |
| 1470 | */ |
| 1471 | static bool sfe_ipv6_debug_dev_read_connections_start(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length, |
| 1472 | int *total_read, struct sfe_ipv6_debug_xml_write_state *ws) |
| 1473 | { |
| 1474 | int bytes_read; |
| 1475 | |
| 1476 | bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<connections>\n"); |
| 1477 | if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) { |
| 1478 | return false; |
| 1479 | } |
| 1480 | |
| 1481 | *length -= bytes_read; |
| 1482 | *total_read += bytes_read; |
| 1483 | |
| 1484 | ws->state++; |
| 1485 | return true; |
| 1486 | } |
| 1487 | |
| 1488 | /* |
| 1489 | * sfe_ipv6_debug_dev_read_connections_connection() |
| 1490 | * Generate part of the XML output. |
| 1491 | */ |
| 1492 | static bool sfe_ipv6_debug_dev_read_connections_connection(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length, |
| 1493 | int *total_read, struct sfe_ipv6_debug_xml_write_state *ws) |
| 1494 | { |
| 1495 | struct sfe_ipv6_connection *c; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1496 | struct sfe_ipv6_connection_match *original_cm; |
| 1497 | struct sfe_ipv6_connection_match *reply_cm; |
| 1498 | int bytes_read; |
| 1499 | int protocol; |
| 1500 | struct net_device *src_dev; |
| 1501 | struct sfe_ipv6_addr src_ip; |
| 1502 | struct sfe_ipv6_addr src_ip_xlate; |
| 1503 | __be16 src_port; |
| 1504 | __be16 src_port_xlate; |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 1505 | u64 src_rx_packets; |
| 1506 | u64 src_rx_bytes; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1507 | struct net_device *dest_dev; |
| 1508 | struct sfe_ipv6_addr dest_ip; |
| 1509 | struct sfe_ipv6_addr dest_ip_xlate; |
| 1510 | __be16 dest_port; |
| 1511 | __be16 dest_port_xlate; |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 1512 | u64 dest_rx_packets; |
| 1513 | u64 dest_rx_bytes; |
| 1514 | u64 last_sync_jiffies; |
| 1515 | u32 mark, src_priority, dest_priority, src_dscp, dest_dscp; |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 1516 | u32 packet, byte; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1517 | #ifdef CONFIG_NF_FLOW_COOKIE |
| 1518 | int src_flow_cookie, dst_flow_cookie; |
| 1519 | #endif |
| 1520 | |
| 1521 | spin_lock_bh(&si->lock); |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 1522 | |
| 1523 | for (c = si->all_connections_head; c; c = c->all_connections_next) { |
| 1524 | if (c->debug_read_seq < si->debug_read_seq) { |
| 1525 | c->debug_read_seq = si->debug_read_seq; |
| 1526 | break; |
| 1527 | } |
| 1528 | } |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1529 | |
| 1530 | /* |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 1531 | * If there were no connections then move to the next state. |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1532 | */ |
| 1533 | if (!c) { |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1534 | spin_unlock_bh(&si->lock); |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 1535 | ws->state++; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1536 | return true; |
| 1537 | } |
| 1538 | |
| 1539 | original_cm = c->original_match; |
| 1540 | reply_cm = c->reply_match; |
| 1541 | |
| 1542 | protocol = c->protocol; |
| 1543 | src_dev = c->original_dev; |
| 1544 | src_ip = c->src_ip[0]; |
| 1545 | src_ip_xlate = c->src_ip_xlate[0]; |
| 1546 | src_port = c->src_port; |
| 1547 | src_port_xlate = c->src_port_xlate; |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 1548 | src_priority = original_cm->priority; |
| 1549 | src_dscp = original_cm->dscp >> SFE_IPV6_DSCP_SHIFT; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1550 | |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 1551 | sfe_ipv6_connection_match_update_summary_stats(original_cm, &packet, &byte); |
| 1552 | sfe_ipv6_connection_match_update_summary_stats(reply_cm, &packet, &byte); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1553 | |
| 1554 | src_rx_packets = original_cm->rx_packet_count64; |
| 1555 | src_rx_bytes = original_cm->rx_byte_count64; |
| 1556 | dest_dev = c->reply_dev; |
| 1557 | dest_ip = c->dest_ip[0]; |
| 1558 | dest_ip_xlate = c->dest_ip_xlate[0]; |
| 1559 | dest_port = c->dest_port; |
| 1560 | dest_port_xlate = c->dest_port_xlate; |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 1561 | dest_priority = reply_cm->priority; |
| 1562 | dest_dscp = reply_cm->dscp >> SFE_IPV6_DSCP_SHIFT; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1563 | dest_rx_packets = reply_cm->rx_packet_count64; |
| 1564 | dest_rx_bytes = reply_cm->rx_byte_count64; |
| 1565 | last_sync_jiffies = get_jiffies_64() - c->last_sync_jiffies; |
| 1566 | mark = c->mark; |
| 1567 | #ifdef CONFIG_NF_FLOW_COOKIE |
| 1568 | src_flow_cookie = original_cm->flow_cookie; |
| 1569 | dst_flow_cookie = reply_cm->flow_cookie; |
| 1570 | #endif |
| 1571 | spin_unlock_bh(&si->lock); |
| 1572 | |
| 1573 | bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t\t<connection " |
| 1574 | "protocol=\"%u\" " |
| 1575 | "src_dev=\"%s\" " |
| 1576 | "src_ip=\"%pI6\" src_ip_xlate=\"%pI6\" " |
| 1577 | "src_port=\"%u\" src_port_xlate=\"%u\" " |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 1578 | "src_priority=\"%u\" src_dscp=\"%u\" " |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1579 | "src_rx_pkts=\"%llu\" src_rx_bytes=\"%llu\" " |
| 1580 | "dest_dev=\"%s\" " |
| 1581 | "dest_ip=\"%pI6\" dest_ip_xlate=\"%pI6\" " |
| 1582 | "dest_port=\"%u\" dest_port_xlate=\"%u\" " |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 1583 | "dest_priority=\"%u\" dest_dscp=\"%u\" " |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1584 | "dest_rx_pkts=\"%llu\" dest_rx_bytes=\"%llu\" " |
| 1585 | #ifdef CONFIG_NF_FLOW_COOKIE |
| 1586 | "src_flow_cookie=\"%d\" dst_flow_cookie=\"%d\" " |
| 1587 | #endif |
| 1588 | "last_sync=\"%llu\" " |
| 1589 | "mark=\"%08x\" />\n", |
| 1590 | protocol, |
| 1591 | src_dev->name, |
| 1592 | &src_ip, &src_ip_xlate, |
| 1593 | ntohs(src_port), ntohs(src_port_xlate), |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 1594 | src_priority, src_dscp, |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1595 | src_rx_packets, src_rx_bytes, |
| 1596 | dest_dev->name, |
| 1597 | &dest_ip, &dest_ip_xlate, |
| 1598 | ntohs(dest_port), ntohs(dest_port_xlate), |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 1599 | dest_priority, dest_dscp, |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1600 | dest_rx_packets, dest_rx_bytes, |
| 1601 | #ifdef CONFIG_NF_FLOW_COOKIE |
| 1602 | src_flow_cookie, dst_flow_cookie, |
| 1603 | #endif |
| 1604 | last_sync_jiffies, mark); |
| 1605 | |
| 1606 | if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) { |
| 1607 | return false; |
| 1608 | } |
| 1609 | |
| 1610 | *length -= bytes_read; |
| 1611 | *total_read += bytes_read; |
| 1612 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1613 | return true; |
| 1614 | } |
| 1615 | |
| 1616 | /* |
| 1617 | * sfe_ipv6_debug_dev_read_connections_end() |
| 1618 | * Generate part of the XML output. |
| 1619 | */ |
| 1620 | static bool sfe_ipv6_debug_dev_read_connections_end(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length, |
| 1621 | int *total_read, struct sfe_ipv6_debug_xml_write_state *ws) |
| 1622 | { |
| 1623 | int bytes_read; |
| 1624 | |
| 1625 | bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t</connections>\n"); |
| 1626 | if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) { |
| 1627 | return false; |
| 1628 | } |
| 1629 | |
| 1630 | *length -= bytes_read; |
| 1631 | *total_read += bytes_read; |
| 1632 | |
| 1633 | ws->state++; |
| 1634 | return true; |
| 1635 | } |
| 1636 | |
| 1637 | /* |
| 1638 | * sfe_ipv6_debug_dev_read_exceptions_start() |
| 1639 | * Generate part of the XML output. |
| 1640 | */ |
| 1641 | static bool sfe_ipv6_debug_dev_read_exceptions_start(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length, |
| 1642 | int *total_read, struct sfe_ipv6_debug_xml_write_state *ws) |
| 1643 | { |
| 1644 | int bytes_read; |
| 1645 | |
| 1646 | bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<exceptions>\n"); |
| 1647 | if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) { |
| 1648 | return false; |
| 1649 | } |
| 1650 | |
| 1651 | *length -= bytes_read; |
| 1652 | *total_read += bytes_read; |
| 1653 | |
| 1654 | ws->state++; |
| 1655 | return true; |
| 1656 | } |
| 1657 | |
| 1658 | /* |
| 1659 | * sfe_ipv6_debug_dev_read_exceptions_exception() |
| 1660 | * Generate part of the XML output. |
| 1661 | */ |
| 1662 | static bool sfe_ipv6_debug_dev_read_exceptions_exception(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length, |
| 1663 | int *total_read, struct sfe_ipv6_debug_xml_write_state *ws) |
| 1664 | { |
Ratheesh Kannoth | 1ed9546 | 2021-10-20 07:57:45 +0530 | [diff] [blame] | 1665 | int i; |
| 1666 | u64 val = 0; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1667 | |
Ratheesh Kannoth | 1ed9546 | 2021-10-20 07:57:45 +0530 | [diff] [blame] | 1668 | for_each_possible_cpu(i) { |
| 1669 | const struct sfe_ipv6_stats *s = per_cpu_ptr(si->stats_pcpu, i); |
| 1670 | val += s->exception_events64[ws->iter_exception]; |
| 1671 | } |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1672 | |
Ratheesh Kannoth | 1ed9546 | 2021-10-20 07:57:45 +0530 | [diff] [blame] | 1673 | if (val) { |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1674 | int bytes_read; |
| 1675 | |
| 1676 | bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, |
| 1677 | "\t\t<exception name=\"%s\" count=\"%llu\" />\n", |
| 1678 | sfe_ipv6_exception_events_string[ws->iter_exception], |
Ratheesh Kannoth | 1ed9546 | 2021-10-20 07:57:45 +0530 | [diff] [blame] | 1679 | val); |
| 1680 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1681 | if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) { |
| 1682 | return false; |
| 1683 | } |
| 1684 | |
| 1685 | *length -= bytes_read; |
| 1686 | *total_read += bytes_read; |
| 1687 | } |
| 1688 | |
| 1689 | ws->iter_exception++; |
| 1690 | if (ws->iter_exception >= SFE_IPV6_EXCEPTION_EVENT_LAST) { |
| 1691 | ws->iter_exception = 0; |
| 1692 | ws->state++; |
| 1693 | } |
| 1694 | |
| 1695 | return true; |
| 1696 | } |
| 1697 | |
| 1698 | /* |
| 1699 | * sfe_ipv6_debug_dev_read_exceptions_end() |
| 1700 | * Generate part of the XML output. |
| 1701 | */ |
| 1702 | static bool sfe_ipv6_debug_dev_read_exceptions_end(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length, |
| 1703 | int *total_read, struct sfe_ipv6_debug_xml_write_state *ws) |
| 1704 | { |
| 1705 | int bytes_read; |
| 1706 | |
| 1707 | bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t</exceptions>\n"); |
| 1708 | if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) { |
| 1709 | return false; |
| 1710 | } |
| 1711 | |
| 1712 | *length -= bytes_read; |
| 1713 | *total_read += bytes_read; |
| 1714 | |
| 1715 | ws->state++; |
| 1716 | return true; |
| 1717 | } |
| 1718 | |
| 1719 | /* |
| 1720 | * sfe_ipv6_debug_dev_read_stats() |
| 1721 | * Generate part of the XML output. |
| 1722 | */ |
| 1723 | static bool sfe_ipv6_debug_dev_read_stats(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length, |
| 1724 | int *total_read, struct sfe_ipv6_debug_xml_write_state *ws) |
| 1725 | { |
| 1726 | int bytes_read; |
Ratheesh Kannoth | 1ed9546 | 2021-10-20 07:57:45 +0530 | [diff] [blame] | 1727 | struct sfe_ipv6_stats stats; |
| 1728 | unsigned int num_conn; |
| 1729 | |
| 1730 | sfe_ipv6_update_summary_stats(si, &stats); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1731 | |
| 1732 | spin_lock_bh(&si->lock); |
Ratheesh Kannoth | 1ed9546 | 2021-10-20 07:57:45 +0530 | [diff] [blame] | 1733 | num_conn = si->num_connections; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1734 | spin_unlock_bh(&si->lock); |
| 1735 | |
| 1736 | bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<stats " |
| 1737 | "num_connections=\"%u\" " |
| 1738 | "pkts_forwarded=\"%llu\" pkts_not_forwarded=\"%llu\" " |
| 1739 | "create_requests=\"%llu\" create_collisions=\"%llu\" " |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1740 | "create_failures=\"%llu\" " |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1741 | "destroy_requests=\"%llu\" destroy_misses=\"%llu\" " |
| 1742 | "flushes=\"%llu\" " |
| 1743 | "hash_hits=\"%llu\" hash_reorders=\"%llu\" />\n", |
Ratheesh Kannoth | 1ed9546 | 2021-10-20 07:57:45 +0530 | [diff] [blame] | 1744 | |
| 1745 | num_conn, |
| 1746 | stats.packets_forwarded64, |
| 1747 | stats.packets_not_forwarded64, |
| 1748 | stats.connection_create_requests64, |
| 1749 | stats.connection_create_collisions64, |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1750 | stats.connection_create_failures64, |
Ratheesh Kannoth | 1ed9546 | 2021-10-20 07:57:45 +0530 | [diff] [blame] | 1751 | stats.connection_destroy_requests64, |
| 1752 | stats.connection_destroy_misses64, |
| 1753 | stats.connection_flushes64, |
| 1754 | stats.connection_match_hash_hits64, |
| 1755 | stats.connection_match_hash_reorders64); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1756 | if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) { |
| 1757 | return false; |
| 1758 | } |
| 1759 | |
| 1760 | *length -= bytes_read; |
| 1761 | *total_read += bytes_read; |
| 1762 | |
| 1763 | ws->state++; |
| 1764 | return true; |
| 1765 | } |
| 1766 | |
| 1767 | /* |
| 1768 | * sfe_ipv6_debug_dev_read_end() |
| 1769 | * Generate part of the XML output. |
| 1770 | */ |
| 1771 | static bool sfe_ipv6_debug_dev_read_end(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length, |
| 1772 | int *total_read, struct sfe_ipv6_debug_xml_write_state *ws) |
| 1773 | { |
| 1774 | int bytes_read; |
| 1775 | |
| 1776 | bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "</sfe_ipv6>\n"); |
| 1777 | if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) { |
| 1778 | return false; |
| 1779 | } |
| 1780 | |
| 1781 | *length -= bytes_read; |
| 1782 | *total_read += bytes_read; |
| 1783 | |
| 1784 | ws->state++; |
| 1785 | return true; |
| 1786 | } |
| 1787 | |
| 1788 | /* |
| 1789 | * Array of write functions that write various XML elements that correspond to |
| 1790 | * our XML output state machine. |
| 1791 | */ |
| 1792 | static sfe_ipv6_debug_xml_write_method_t sfe_ipv6_debug_xml_write_methods[SFE_IPV6_DEBUG_XML_STATE_DONE] = { |
| 1793 | sfe_ipv6_debug_dev_read_start, |
| 1794 | sfe_ipv6_debug_dev_read_connections_start, |
| 1795 | sfe_ipv6_debug_dev_read_connections_connection, |
| 1796 | sfe_ipv6_debug_dev_read_connections_end, |
| 1797 | sfe_ipv6_debug_dev_read_exceptions_start, |
| 1798 | sfe_ipv6_debug_dev_read_exceptions_exception, |
| 1799 | sfe_ipv6_debug_dev_read_exceptions_end, |
| 1800 | sfe_ipv6_debug_dev_read_stats, |
| 1801 | sfe_ipv6_debug_dev_read_end, |
| 1802 | }; |
| 1803 | |
| 1804 | /* |
| 1805 | * sfe_ipv6_debug_dev_read() |
| 1806 | * Send info to userspace upon read request from user |
| 1807 | */ |
| 1808 | static ssize_t sfe_ipv6_debug_dev_read(struct file *filp, char *buffer, size_t length, loff_t *offset) |
| 1809 | { |
| 1810 | char msg[CHAR_DEV_MSG_SIZE]; |
| 1811 | int total_read = 0; |
| 1812 | struct sfe_ipv6_debug_xml_write_state *ws; |
| 1813 | struct sfe_ipv6 *si = &__si6; |
| 1814 | |
| 1815 | ws = (struct sfe_ipv6_debug_xml_write_state *)filp->private_data; |
| 1816 | while ((ws->state != SFE_IPV6_DEBUG_XML_STATE_DONE) && (length > CHAR_DEV_MSG_SIZE)) { |
| 1817 | if ((sfe_ipv6_debug_xml_write_methods[ws->state])(si, buffer, msg, &length, &total_read, ws)) { |
| 1818 | continue; |
| 1819 | } |
| 1820 | } |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1821 | return total_read; |
| 1822 | } |
| 1823 | |
| 1824 | /* |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1825 | * sfe_ipv6_debug_dev_open() |
| 1826 | */ |
| 1827 | static int sfe_ipv6_debug_dev_open(struct inode *inode, struct file *file) |
| 1828 | { |
| 1829 | struct sfe_ipv6_debug_xml_write_state *ws; |
| 1830 | |
| 1831 | ws = (struct sfe_ipv6_debug_xml_write_state *)file->private_data; |
| 1832 | if (ws) { |
| 1833 | return 0; |
| 1834 | } |
| 1835 | |
| 1836 | ws = kzalloc(sizeof(struct sfe_ipv6_debug_xml_write_state), GFP_KERNEL); |
| 1837 | if (!ws) { |
| 1838 | return -ENOMEM; |
| 1839 | } |
| 1840 | |
| 1841 | ws->state = SFE_IPV6_DEBUG_XML_STATE_START; |
| 1842 | file->private_data = ws; |
| 1843 | |
| 1844 | return 0; |
| 1845 | } |
| 1846 | |
| 1847 | /* |
| 1848 | * sfe_ipv6_debug_dev_release() |
| 1849 | */ |
| 1850 | static int sfe_ipv6_debug_dev_release(struct inode *inode, struct file *file) |
| 1851 | { |
| 1852 | struct sfe_ipv6_debug_xml_write_state *ws; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1853 | |
| 1854 | ws = (struct sfe_ipv6_debug_xml_write_state *)file->private_data; |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 1855 | if (ws) { |
| 1856 | /* |
| 1857 | * We've finished with our output so free the write state. |
| 1858 | */ |
| 1859 | kfree(ws); |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 1860 | file->private_data = NULL; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1861 | } |
| 1862 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1863 | return 0; |
| 1864 | } |
| 1865 | |
| 1866 | /* |
| 1867 | * File operations used in the debug char device |
| 1868 | */ |
| 1869 | static struct file_operations sfe_ipv6_debug_dev_fops = { |
| 1870 | .read = sfe_ipv6_debug_dev_read, |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1871 | .open = sfe_ipv6_debug_dev_open, |
| 1872 | .release = sfe_ipv6_debug_dev_release |
| 1873 | }; |
| 1874 | |
| 1875 | #ifdef CONFIG_NF_FLOW_COOKIE |
| 1876 | /* |
| 1877 | * sfe_ipv6_register_flow_cookie_cb |
| 1878 | * register a function in SFE to let SFE use this function to configure flow cookie for a flow |
| 1879 | * |
| 1880 | * Hardware driver which support flow cookie should register a callback function in SFE. Then SFE |
| 1881 | * can use this function to configure flow cookie for a flow. |
| 1882 | * return: 0, success; !=0, fail |
| 1883 | */ |
| 1884 | int sfe_ipv6_register_flow_cookie_cb(sfe_ipv6_flow_cookie_set_func_t cb) |
| 1885 | { |
| 1886 | struct sfe_ipv6 *si = &__si6; |
| 1887 | |
| 1888 | BUG_ON(!cb); |
| 1889 | |
| 1890 | if (si->flow_cookie_set_func) { |
| 1891 | return -1; |
| 1892 | } |
| 1893 | |
| 1894 | rcu_assign_pointer(si->flow_cookie_set_func, cb); |
| 1895 | return 0; |
| 1896 | } |
| 1897 | |
| 1898 | /* |
| 1899 | * sfe_ipv6_unregister_flow_cookie_cb |
| 1900 | * unregister function which is used to configure flow cookie for a flow |
| 1901 | * |
| 1902 | * return: 0, success; !=0, fail |
| 1903 | */ |
| 1904 | int sfe_ipv6_unregister_flow_cookie_cb(sfe_ipv6_flow_cookie_set_func_t cb) |
| 1905 | { |
| 1906 | struct sfe_ipv6 *si = &__si6; |
| 1907 | |
| 1908 | RCU_INIT_POINTER(si->flow_cookie_set_func, NULL); |
| 1909 | return 0; |
| 1910 | } |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 1911 | |
| 1912 | /* |
| 1913 | * sfe_ipv6_get_flow_cookie() |
| 1914 | */ |
| 1915 | static ssize_t sfe_ipv6_get_flow_cookie(struct device *dev, |
| 1916 | struct device_attribute *attr, |
| 1917 | char *buf) |
| 1918 | { |
| 1919 | struct sfe_ipv6 *si = &__si6; |
Xiaoping Fan | 01c67cc | 2015-11-09 11:31:57 -0800 | [diff] [blame] | 1920 | 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] | 1921 | } |
| 1922 | |
| 1923 | /* |
| 1924 | * sfe_ipv6_set_flow_cookie() |
| 1925 | */ |
| 1926 | static ssize_t sfe_ipv6_set_flow_cookie(struct device *dev, |
| 1927 | struct device_attribute *attr, |
| 1928 | const char *buf, size_t size) |
| 1929 | { |
| 1930 | struct sfe_ipv6 *si = &__si6; |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 1931 | si->flow_cookie_enable = strict_strtol(buf, NULL, 0); |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 1932 | |
| 1933 | return size; |
| 1934 | } |
| 1935 | |
| 1936 | /* |
| 1937 | * sysfs attributes. |
| 1938 | */ |
| 1939 | static const struct device_attribute sfe_ipv6_flow_cookie_attr = |
Xiaoping Fan | e70da41 | 2016-02-26 16:47:57 -0800 | [diff] [blame] | 1940 | __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] | 1941 | #endif /*CONFIG_NF_FLOW_COOKIE*/ |
| 1942 | |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 1943 | /* |
| 1944 | * sfe_ipv6_get_cpu() |
| 1945 | */ |
| 1946 | static ssize_t sfe_ipv6_get_cpu(struct device *dev, |
| 1947 | struct device_attribute *attr, |
| 1948 | char *buf) |
| 1949 | { |
| 1950 | struct sfe_ipv6 *si = &__si6; |
| 1951 | return snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", si->work_cpu); |
| 1952 | } |
| 1953 | |
| 1954 | /* |
| 1955 | * sfe_ipv4_set_cpu() |
| 1956 | */ |
| 1957 | static ssize_t sfe_ipv6_set_cpu(struct device *dev, |
| 1958 | struct device_attribute *attr, |
| 1959 | const char *buf, size_t size) |
| 1960 | { |
| 1961 | struct sfe_ipv6 *si = &__si6; |
| 1962 | int work_cpu; |
| 1963 | |
| 1964 | work_cpu = simple_strtol(buf, NULL, 0); |
| 1965 | if ((work_cpu >= 0) && (work_cpu <= NR_CPUS)) { |
| 1966 | si->work_cpu = work_cpu; |
| 1967 | } else { |
| 1968 | dev_err(dev, "%s is not in valid range[0,%d]", buf, NR_CPUS); |
| 1969 | } |
| 1970 | |
| 1971 | return size; |
| 1972 | } |
| 1973 | /* |
| 1974 | * sysfs attributes. |
| 1975 | */ |
| 1976 | static const struct device_attribute sfe_ipv6_cpu_attr = |
| 1977 | __ATTR(stat_work_cpu, S_IWUSR | S_IRUGO, sfe_ipv6_get_cpu, sfe_ipv6_set_cpu); |
| 1978 | |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 1979 | /* |
| 1980 | * sfe_ipv6_hash_init() |
| 1981 | * Initialize conn match hash lists |
| 1982 | */ |
| 1983 | static void sfe_ipv6_conn_match_hash_init(struct sfe_ipv6 *si, int len) |
| 1984 | { |
| 1985 | struct hlist_head *hash_list = si->hlist_conn_match_hash_head; |
| 1986 | int i; |
| 1987 | |
| 1988 | for (i = 0; i < len; i++) { |
| 1989 | INIT_HLIST_HEAD(&hash_list[i]); |
| 1990 | } |
| 1991 | } |
| 1992 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1993 | /* |
| 1994 | * sfe_ipv6_init() |
| 1995 | */ |
Ratheesh Kannoth | 24fb1db | 2021-10-20 07:28:06 +0530 | [diff] [blame] | 1996 | int sfe_ipv6_init(void) |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 1997 | { |
| 1998 | struct sfe_ipv6 *si = &__si6; |
| 1999 | int result = -1; |
| 2000 | |
| 2001 | DEBUG_INFO("SFE IPv6 init\n"); |
| 2002 | |
Ratheesh Kannoth | a212fc5 | 2021-10-20 07:50:32 +0530 | [diff] [blame] | 2003 | sfe_ipv6_conn_match_hash_init(si, ARRAY_SIZE(si->hlist_conn_match_hash_head)); |
| 2004 | |
Ratheesh Kannoth | 1ed9546 | 2021-10-20 07:57:45 +0530 | [diff] [blame] | 2005 | si->stats_pcpu = alloc_percpu_gfp(struct sfe_ipv6_stats, GFP_KERNEL | __GFP_ZERO); |
| 2006 | if (!si->stats_pcpu) { |
| 2007 | DEBUG_ERROR("failed to allocate stats memory for sfe_ipv6\n"); |
| 2008 | goto exit0; |
| 2009 | } |
| 2010 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2011 | /* |
| 2012 | * Create sys/sfe_ipv6 |
| 2013 | */ |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 2014 | si->sys_ipv6 = kobject_create_and_add("sfe_ipv6", NULL); |
| 2015 | if (!si->sys_ipv6) { |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2016 | DEBUG_ERROR("failed to register sfe_ipv6\n"); |
| 2017 | goto exit1; |
| 2018 | } |
| 2019 | |
| 2020 | /* |
| 2021 | * Create files, one for each parameter supported by this module. |
| 2022 | */ |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 2023 | 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] | 2024 | if (result) { |
| 2025 | DEBUG_ERROR("failed to register debug dev file: %d\n", result); |
| 2026 | goto exit2; |
| 2027 | } |
| 2028 | |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 2029 | result = sysfs_create_file(si->sys_ipv6, &sfe_ipv6_cpu_attr.attr); |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 2030 | if (result) { |
| 2031 | DEBUG_ERROR("failed to register debug dev file: %d\n", result); |
| 2032 | goto exit3; |
| 2033 | } |
| 2034 | |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 2035 | #ifdef CONFIG_NF_FLOW_COOKIE |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 2036 | 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] | 2037 | if (result) { |
| 2038 | DEBUG_ERROR("failed to register flow cookie enable file: %d\n", result); |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 2039 | goto exit4; |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 2040 | } |
| 2041 | #endif /* CONFIG_NF_FLOW_COOKIE */ |
| 2042 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2043 | /* |
| 2044 | * Register our debug char device. |
| 2045 | */ |
| 2046 | result = register_chrdev(0, "sfe_ipv6", &sfe_ipv6_debug_dev_fops); |
| 2047 | if (result < 0) { |
| 2048 | DEBUG_ERROR("Failed to register chrdev: %d\n", result); |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 2049 | goto exit5; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2050 | } |
| 2051 | |
| 2052 | si->debug_dev = result; |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 2053 | si->work_cpu = WORK_CPU_UNBOUND; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2054 | |
| 2055 | /* |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 2056 | * Create work to handle periodic statistics. |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2057 | */ |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 2058 | INIT_DELAYED_WORK(&(si->sync_dwork), sfe_ipv6_periodic_sync); |
| 2059 | 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] | 2060 | spin_lock_init(&si->lock); |
| 2061 | |
| 2062 | return 0; |
| 2063 | |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 2064 | exit5: |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 2065 | #ifdef CONFIG_NF_FLOW_COOKIE |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 2066 | sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_flow_cookie_attr.attr); |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 2067 | |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 2068 | exit4: |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 2069 | #endif /* CONFIG_NF_FLOW_COOKIE */ |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 2070 | sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_cpu_attr.attr); |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 2071 | exit3: |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 2072 | sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_debug_dev_attr.attr); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2073 | |
| 2074 | exit2: |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 2075 | kobject_put(si->sys_ipv6); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2076 | |
| 2077 | exit1: |
Ratheesh Kannoth | 1ed9546 | 2021-10-20 07:57:45 +0530 | [diff] [blame] | 2078 | free_percpu(si->stats_pcpu); |
| 2079 | |
| 2080 | exit0: |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2081 | return result; |
| 2082 | } |
| 2083 | |
| 2084 | /* |
| 2085 | * sfe_ipv6_exit() |
| 2086 | */ |
Ratheesh Kannoth | 24fb1db | 2021-10-20 07:28:06 +0530 | [diff] [blame] | 2087 | void sfe_ipv6_exit(void) |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2088 | { |
| 2089 | struct sfe_ipv6 *si = &__si6; |
| 2090 | |
| 2091 | DEBUG_INFO("SFE IPv6 exit\n"); |
| 2092 | |
| 2093 | /* |
| 2094 | * Destroy all connections. |
| 2095 | */ |
| 2096 | sfe_ipv6_destroy_all_rules_for_dev(NULL); |
| 2097 | |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 2098 | cancel_delayed_work(&si->sync_dwork); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2099 | |
| 2100 | unregister_chrdev(si->debug_dev, "sfe_ipv6"); |
| 2101 | |
Ratheesh Kannoth | 1ed9546 | 2021-10-20 07:57:45 +0530 | [diff] [blame] | 2102 | free_percpu(si->stats_pcpu); |
| 2103 | |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 2104 | #ifdef 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_flow_cookie_attr.attr); |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 2106 | #endif /* CONFIG_NF_FLOW_COOKIE */ |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 2107 | |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 2108 | sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_cpu_attr.attr); |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 2109 | |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 2110 | sysfs_remove_file(si->sys_ipv6, &sfe_ipv6_debug_dev_attr.attr); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2111 | |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 2112 | kobject_put(si->sys_ipv6); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2113 | } |
| 2114 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 2115 | #ifdef CONFIG_NF_FLOW_COOKIE |
| 2116 | EXPORT_SYMBOL(sfe_ipv6_register_flow_cookie_cb); |
| 2117 | EXPORT_SYMBOL(sfe_ipv6_unregister_flow_cookie_cb); |
| 2118 | #endif |