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