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