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