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. |
| 6 | * Copyright (c) 2021 Qualcomm Innovation Center, Inc. All rights reserved. |
| 7 | * |
| 8 | * Permission to use, copy, modify, and/or distribute this software for any |
| 9 | * purpose with or without fee is hereby granted, provided that the above |
| 10 | * copyright notice and this permission notice appear in all copies. |
| 11 | * |
Xiaoping Fan | a42c68b | 2015-08-07 18:00:39 -0700 | [diff] [blame] | 12 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 13 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 14 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 15 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 16 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
Ratheesh Kannoth | 24fb1db | 2021-10-20 07:28:06 +0530 | [diff] [blame] | 17 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 18 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
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> |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 26 | #include <linux/etherdevice.h> |
Tian Yang | 45f39c8 | 2020-10-06 14:07:47 -0700 | [diff] [blame] | 27 | #include <linux/version.h> |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 28 | #include <linux/lockdep.h> |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 29 | |
Ratheesh Kannoth | 24fb1db | 2021-10-20 07:28:06 +0530 | [diff] [blame] | 30 | #include "sfe_debug.h" |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 31 | #include "sfe.h" |
Ratheesh Kannoth | 24fb1db | 2021-10-20 07:28:06 +0530 | [diff] [blame] | 32 | #include "sfe_flow_cookie.h" |
| 33 | #include "sfe_ipv4.h" |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 34 | |
| 35 | static char *sfe_ipv4_exception_events_string[SFE_IPV4_EXCEPTION_EVENT_LAST] = { |
| 36 | "UDP_HEADER_INCOMPLETE", |
| 37 | "UDP_NO_CONNECTION", |
| 38 | "UDP_IP_OPTIONS_OR_INITIAL_FRAGMENT", |
| 39 | "UDP_SMALL_TTL", |
| 40 | "UDP_NEEDS_FRAGMENTATION", |
| 41 | "TCP_HEADER_INCOMPLETE", |
| 42 | "TCP_NO_CONNECTION_SLOW_FLAGS", |
| 43 | "TCP_NO_CONNECTION_FAST_FLAGS", |
| 44 | "TCP_IP_OPTIONS_OR_INITIAL_FRAGMENT", |
| 45 | "TCP_SMALL_TTL", |
| 46 | "TCP_NEEDS_FRAGMENTATION", |
| 47 | "TCP_FLAGS", |
| 48 | "TCP_SEQ_EXCEEDS_RIGHT_EDGE", |
| 49 | "TCP_SMALL_DATA_OFFS", |
| 50 | "TCP_BAD_SACK", |
| 51 | "TCP_BIG_DATA_OFFS", |
| 52 | "TCP_SEQ_BEFORE_LEFT_EDGE", |
| 53 | "TCP_ACK_EXCEEDS_RIGHT_EDGE", |
| 54 | "TCP_ACK_BEFORE_LEFT_EDGE", |
| 55 | "ICMP_HEADER_INCOMPLETE", |
| 56 | "ICMP_UNHANDLED_TYPE", |
| 57 | "ICMP_IPV4_HEADER_INCOMPLETE", |
| 58 | "ICMP_IPV4_NON_V4", |
| 59 | "ICMP_IPV4_IP_OPTIONS_INCOMPLETE", |
| 60 | "ICMP_IPV4_UDP_HEADER_INCOMPLETE", |
| 61 | "ICMP_IPV4_TCP_HEADER_INCOMPLETE", |
| 62 | "ICMP_IPV4_UNHANDLED_PROTOCOL", |
| 63 | "ICMP_NO_CONNECTION", |
| 64 | "ICMP_FLUSHED_CONNECTION", |
| 65 | "HEADER_INCOMPLETE", |
| 66 | "BAD_TOTAL_LENGTH", |
| 67 | "NON_V4", |
| 68 | "NON_INITIAL_FRAGMENT", |
| 69 | "DATAGRAM_INCOMPLETE", |
| 70 | "IP_OPTIONS_INCOMPLETE", |
| 71 | "UNHANDLED_PROTOCOL" |
| 72 | }; |
| 73 | |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 74 | static struct sfe_ipv4 __si; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 75 | |
| 76 | /* |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 77 | * sfe_ipv4_gen_ip_csum() |
| 78 | * Generate the IP checksum for an IPv4 header. |
| 79 | * |
| 80 | * Note that this function assumes that we have only 20 bytes of IP header. |
| 81 | */ |
Ratheesh Kannoth | 741f799 | 2021-10-20 07:39:52 +0530 | [diff] [blame] | 82 | static inline u16 sfe_ipv4_gen_ip_csum(struct iphdr *iph) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 83 | { |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 84 | u32 sum; |
| 85 | u16 *i = (u16 *)iph; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 86 | |
| 87 | iph->check = 0; |
| 88 | |
| 89 | /* |
| 90 | * Generate the sum. |
| 91 | */ |
| 92 | sum = i[0] + i[1] + i[2] + i[3] + i[4] + i[5] + i[6] + i[7] + i[8] + i[9]; |
| 93 | |
| 94 | /* |
| 95 | * Fold it to ones-complement form. |
| 96 | */ |
| 97 | sum = (sum & 0xffff) + (sum >> 16); |
| 98 | sum = (sum & 0xffff) + (sum >> 16); |
| 99 | |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 100 | return (u16)sum ^ 0xffff; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /* |
| 104 | * sfe_ipv4_get_connection_match_hash() |
| 105 | * Generate the hash used in connection match lookups. |
| 106 | */ |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 107 | 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] | 108 | __be32 src_ip, __be16 src_port, |
| 109 | __be32 dest_ip, __be16 dest_port) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 110 | { |
| 111 | size_t dev_addr = (size_t)dev; |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 112 | 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] | 113 | return ((hash >> SFE_IPV4_CONNECTION_HASH_SHIFT) ^ hash) & SFE_IPV4_CONNECTION_HASH_MASK; |
| 114 | } |
| 115 | |
| 116 | /* |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 117 | * sfe_ipv4_find_sfe_ipv4_connection_match_rcu() |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 118 | * Get the IPv4 flow match info that corresponds to a particular 5-tuple. |
| 119 | * |
| 120 | * On entry we must be holding the lock that protects the hash table. |
| 121 | */ |
| 122 | static struct sfe_ipv4_connection_match * |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 123 | sfe_ipv4_find_sfe_ipv4_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] | 124 | __be32 src_ip, __be16 src_port, |
| 125 | __be32 dest_ip, __be16 dest_port) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 126 | { |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 127 | struct sfe_ipv4_connection_match *cm = NULL; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 128 | unsigned int conn_match_idx; |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 129 | struct hlist_head *lhead; |
| 130 | |
| 131 | WARN_ON_ONCE(!rcu_read_lock_held()); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 132 | |
| 133 | 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] | 134 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 135 | lhead = &si->hlist_conn_match_hash_head[conn_match_idx]; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 136 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 137 | hlist_for_each_entry_rcu(cm, lhead, hnode) { |
| 138 | if (cm->match_src_port != src_port |
| 139 | || cm->match_dest_port != dest_port |
| 140 | || cm->match_src_ip != src_ip |
| 141 | || cm->match_dest_ip != dest_ip |
| 142 | || cm->match_protocol != protocol |
| 143 | || cm->match_dev != dev) { |
| 144 | continue; |
| 145 | } |
| 146 | |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 147 | this_cpu_inc(si->stats_pcpu->connection_match_hash_hits64); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 148 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 149 | break; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 150 | } |
| 151 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 152 | return cm; |
| 153 | } |
| 154 | |
| 155 | /* |
| 156 | * sfe_ipv4_connection_match_update_summary_stats() |
| 157 | * Update the summary stats for a connection match entry. |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 158 | * |
| 159 | * Stats are incremented atomically. So use atomic substraction to update summary |
| 160 | * stats. |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 161 | */ |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 162 | static inline void sfe_ipv4_connection_match_update_summary_stats(struct sfe_ipv4_connection_match *cm, |
| 163 | u32 *packets, u32 *bytes) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 164 | { |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 165 | u32 packet_count, byte_count; |
| 166 | |
| 167 | packet_count = atomic_read(&cm->rx_packet_count); |
| 168 | cm->rx_packet_count64 += packet_count; |
| 169 | atomic_sub(packet_count, &cm->rx_packet_count); |
| 170 | |
| 171 | byte_count = atomic_read(&cm->rx_byte_count); |
| 172 | cm->rx_byte_count64 += byte_count; |
| 173 | atomic_sub(byte_count, &cm->rx_byte_count); |
| 174 | |
| 175 | *packets = packet_count; |
| 176 | *bytes = byte_count; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | /* |
| 180 | * sfe_ipv4_connection_match_compute_translations() |
| 181 | * Compute port and address translations for a connection match entry. |
| 182 | */ |
| 183 | static void sfe_ipv4_connection_match_compute_translations(struct sfe_ipv4_connection_match *cm) |
| 184 | { |
| 185 | /* |
| 186 | * Before we insert the entry look to see if this is tagged as doing address |
| 187 | * translations. If it is then work out the adjustment that we need to apply |
| 188 | * to the transport checksum. |
| 189 | */ |
| 190 | if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC) { |
| 191 | /* |
| 192 | * Precompute an incremental checksum adjustment so we can |
| 193 | * edit packets in this stream very quickly. The algorithm is from RFC1624. |
| 194 | */ |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 195 | u16 src_ip_hi = cm->match_src_ip >> 16; |
| 196 | u16 src_ip_lo = cm->match_src_ip & 0xffff; |
| 197 | u32 xlate_src_ip = ~cm->xlate_src_ip; |
| 198 | u16 xlate_src_ip_hi = xlate_src_ip >> 16; |
| 199 | u16 xlate_src_ip_lo = xlate_src_ip & 0xffff; |
| 200 | u16 xlate_src_port = ~cm->xlate_src_port; |
| 201 | u32 adj; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 202 | |
| 203 | /* |
| 204 | * When we compute this fold it down to a 16-bit offset |
| 205 | * as that way we can avoid having to do a double |
| 206 | * folding of the twos-complement result because the |
| 207 | * addition of 2 16-bit values cannot cause a double |
| 208 | * wrap-around! |
| 209 | */ |
| 210 | adj = src_ip_hi + src_ip_lo + cm->match_src_port |
| 211 | + xlate_src_ip_hi + xlate_src_ip_lo + xlate_src_port; |
| 212 | adj = (adj & 0xffff) + (adj >> 16); |
| 213 | adj = (adj & 0xffff) + (adj >> 16); |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 214 | cm->xlate_src_csum_adjustment = (u16)adj; |
Nicolas Costa | ac2979c | 2014-01-14 10:35:24 -0600 | [diff] [blame] | 215 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST) { |
| 219 | /* |
| 220 | * Precompute an incremental checksum adjustment so we can |
| 221 | * edit packets in this stream very quickly. The algorithm is from RFC1624. |
| 222 | */ |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 223 | u16 dest_ip_hi = cm->match_dest_ip >> 16; |
| 224 | u16 dest_ip_lo = cm->match_dest_ip & 0xffff; |
| 225 | u32 xlate_dest_ip = ~cm->xlate_dest_ip; |
| 226 | u16 xlate_dest_ip_hi = xlate_dest_ip >> 16; |
| 227 | u16 xlate_dest_ip_lo = xlate_dest_ip & 0xffff; |
| 228 | u16 xlate_dest_port = ~cm->xlate_dest_port; |
| 229 | u32 adj; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 230 | |
| 231 | /* |
| 232 | * When we compute this fold it down to a 16-bit offset |
| 233 | * as that way we can avoid having to do a double |
| 234 | * folding of the twos-complement result because the |
| 235 | * addition of 2 16-bit values cannot cause a double |
| 236 | * wrap-around! |
| 237 | */ |
| 238 | adj = dest_ip_hi + dest_ip_lo + cm->match_dest_port |
| 239 | + xlate_dest_ip_hi + xlate_dest_ip_lo + xlate_dest_port; |
| 240 | adj = (adj & 0xffff) + (adj >> 16); |
| 241 | adj = (adj & 0xffff) + (adj >> 16); |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 242 | cm->xlate_dest_csum_adjustment = (u16)adj; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 243 | } |
Xiaoping Fan | ad755af | 2015-04-01 16:58:46 -0700 | [diff] [blame] | 244 | |
| 245 | if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC) { |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 246 | u32 adj = ~cm->match_src_ip + cm->xlate_src_ip; |
Xiaoping Fan | ad755af | 2015-04-01 16:58:46 -0700 | [diff] [blame] | 247 | if (adj < cm->xlate_src_ip) { |
| 248 | adj++; |
| 249 | } |
| 250 | |
| 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_src_partial_csum_adjustment = (u16)adj; |
Xiaoping Fan | ad755af | 2015-04-01 16:58:46 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST) { |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 257 | u32 adj = ~cm->match_dest_ip + cm->xlate_dest_ip; |
Xiaoping Fan | ad755af | 2015-04-01 16:58:46 -0700 | [diff] [blame] | 258 | if (adj < cm->xlate_dest_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_dest_partial_csum_adjustment = (u16)adj; |
Xiaoping Fan | ad755af | 2015-04-01 16:58:46 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | /* |
| 270 | * sfe_ipv4_update_summary_stats() |
| 271 | * Update the summary stats. |
| 272 | */ |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 273 | 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] | 274 | { |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 275 | int i = 0; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 276 | |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 277 | memset(stats, 0, sizeof(*stats)); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 278 | |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 279 | for_each_possible_cpu(i) { |
| 280 | const struct sfe_ipv4_stats *s = per_cpu_ptr(si->stats_pcpu, i); |
| 281 | |
| 282 | stats->connection_create_requests64 += s->connection_create_requests64; |
| 283 | stats->connection_create_collisions64 += s->connection_create_collisions64; |
| 284 | stats->connection_destroy_requests64 += s->connection_destroy_requests64; |
| 285 | stats->connection_destroy_misses64 += s->connection_destroy_misses64; |
| 286 | stats->connection_match_hash_hits64 += s->connection_match_hash_hits64; |
| 287 | stats->connection_match_hash_reorders64 += s->connection_match_hash_reorders64; |
| 288 | stats->connection_flushes64 += s->connection_flushes64; |
| 289 | stats->packets_forwarded64 += s->packets_forwarded64; |
| 290 | stats->packets_not_forwarded64 += s->packets_not_forwarded64; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 291 | } |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 292 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | /* |
| 296 | * sfe_ipv4_insert_sfe_ipv4_connection_match() |
| 297 | * Insert a connection match into the hash. |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 298 | */ |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 299 | static inline void sfe_ipv4_insert_sfe_ipv4_connection_match(struct sfe_ipv4 *si, |
| 300 | struct sfe_ipv4_connection_match *cm) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 301 | { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 302 | unsigned int conn_match_idx |
| 303 | = sfe_ipv4_get_connection_match_hash(cm->match_dev, cm->match_protocol, |
| 304 | cm->match_src_ip, cm->match_src_port, |
| 305 | cm->match_dest_ip, cm->match_dest_port); |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 306 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 307 | lockdep_assert_held(&si->lock); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 308 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 309 | 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] | 310 | #ifdef CONFIG_NF_FLOW_COOKIE |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 311 | if (!si->flow_cookie_enable) |
| 312 | return; |
| 313 | |
Xiaoping Fan | d1dc7b2 | 2015-01-23 00:43:56 -0800 | [diff] [blame] | 314 | /* |
| 315 | * Configure hardware to put a flow cookie in packet of this flow, |
| 316 | * then we can accelerate the lookup process when we received this packet. |
| 317 | */ |
| 318 | for (conn_match_idx = 1; conn_match_idx < SFE_FLOW_COOKIE_SIZE; conn_match_idx++) { |
| 319 | struct sfe_flow_cookie_entry *entry = &si->sfe_flow_cookie_table[conn_match_idx]; |
| 320 | |
| 321 | if ((NULL == entry->match) && time_is_before_jiffies(entry->last_clean_time + HZ)) { |
| 322 | flow_cookie_set_func_t func; |
| 323 | |
| 324 | rcu_read_lock(); |
| 325 | func = rcu_dereference(si->flow_cookie_set_func); |
| 326 | if (func) { |
Xiaoping Fan | 5917642 | 2015-05-22 15:58:10 -0700 | [diff] [blame] | 327 | 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] | 328 | cm->match_dest_ip, cm->match_dest_port, conn_match_idx)) { |
| 329 | entry->match = cm; |
| 330 | cm->flow_cookie = conn_match_idx; |
| 331 | } |
| 332 | } |
| 333 | rcu_read_unlock(); |
| 334 | |
| 335 | break; |
| 336 | } |
| 337 | } |
| 338 | #endif |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | /* |
| 342 | * sfe_ipv4_remove_sfe_ipv4_connection_match() |
| 343 | * Remove a connection match object from the hash. |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 344 | */ |
| 345 | static inline void sfe_ipv4_remove_sfe_ipv4_connection_match(struct sfe_ipv4 *si, struct sfe_ipv4_connection_match *cm) |
| 346 | { |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 347 | |
| 348 | lockdep_assert_held(&si->lock); |
| 349 | |
Xiaoping Fan | d1dc7b2 | 2015-01-23 00:43:56 -0800 | [diff] [blame] | 350 | #ifdef CONFIG_NF_FLOW_COOKIE |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 351 | if (si->flow_cookie_enable) { |
| 352 | /* |
| 353 | * Tell hardware that we no longer need a flow cookie in packet of this flow |
| 354 | */ |
| 355 | unsigned int conn_match_idx; |
Xiaoping Fan | d1dc7b2 | 2015-01-23 00:43:56 -0800 | [diff] [blame] | 356 | |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 357 | for (conn_match_idx = 1; conn_match_idx < SFE_FLOW_COOKIE_SIZE; conn_match_idx++) { |
| 358 | 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] | 359 | |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 360 | if (cm == entry->match) { |
| 361 | flow_cookie_set_func_t func; |
Xiaoping Fan | d1dc7b2 | 2015-01-23 00:43:56 -0800 | [diff] [blame] | 362 | |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 363 | rcu_read_lock(); |
| 364 | func = rcu_dereference(si->flow_cookie_set_func); |
| 365 | if (func) { |
| 366 | func(cm->match_protocol, cm->match_src_ip, cm->match_src_port, |
| 367 | cm->match_dest_ip, cm->match_dest_port, 0); |
| 368 | } |
| 369 | rcu_read_unlock(); |
| 370 | |
| 371 | cm->flow_cookie = 0; |
| 372 | entry->match = NULL; |
| 373 | entry->last_clean_time = jiffies; |
| 374 | break; |
Xiaoping Fan | d1dc7b2 | 2015-01-23 00:43:56 -0800 | [diff] [blame] | 375 | } |
Xiaoping Fan | d1dc7b2 | 2015-01-23 00:43:56 -0800 | [diff] [blame] | 376 | } |
| 377 | } |
| 378 | #endif |
| 379 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 380 | hlist_del_init_rcu(&cm->hnode); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 381 | |
| 382 | /* |
Matthew McClintock | af48f1e | 2014-01-23 15:29:19 -0600 | [diff] [blame] | 383 | * If the connection match entry is in the active list remove it. |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 384 | */ |
Matthew McClintock | af48f1e | 2014-01-23 15:29:19 -0600 | [diff] [blame] | 385 | if (cm->active) { |
| 386 | if (likely(cm->active_prev)) { |
| 387 | cm->active_prev->active_next = cm->active_next; |
| 388 | } else { |
| 389 | si->active_head = cm->active_next; |
| 390 | } |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 391 | |
Matthew McClintock | af48f1e | 2014-01-23 15:29:19 -0600 | [diff] [blame] | 392 | if (likely(cm->active_next)) { |
| 393 | cm->active_next->active_prev = cm->active_prev; |
| 394 | } else { |
| 395 | si->active_tail = cm->active_prev; |
| 396 | } |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 397 | } |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 398 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | /* |
| 402 | * sfe_ipv4_get_connection_hash() |
| 403 | * Generate the hash used in connection lookups. |
| 404 | */ |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 405 | 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] | 406 | __be32 dest_ip, __be16 dest_port) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 407 | { |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 408 | 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] | 409 | return ((hash >> SFE_IPV4_CONNECTION_HASH_SHIFT) ^ hash) & SFE_IPV4_CONNECTION_HASH_MASK; |
| 410 | } |
| 411 | |
| 412 | /* |
| 413 | * sfe_ipv4_find_sfe_ipv4_connection() |
| 414 | * Get the IPv4 connection info that corresponds to a particular 5-tuple. |
| 415 | * |
| 416 | * On entry we must be holding the lock that protects the hash table. |
| 417 | */ |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 418 | static inline struct sfe_ipv4_connection *sfe_ipv4_find_sfe_ipv4_connection(struct sfe_ipv4 *si, u32 protocol, |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 419 | __be32 src_ip, __be16 src_port, |
| 420 | __be32 dest_ip, __be16 dest_port) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 421 | { |
| 422 | struct sfe_ipv4_connection *c; |
| 423 | 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] | 424 | |
| 425 | lockdep_assert_held(&si->lock); |
| 426 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 427 | c = si->conn_hash[conn_idx]; |
| 428 | |
| 429 | /* |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 430 | * Will need connection entry for next create/destroy metadata, |
| 431 | * So no need to re-order entry for these requests |
| 432 | */ |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 433 | while (c) { |
| 434 | if ((c->src_port == src_port) |
| 435 | && (c->dest_port == dest_port) |
| 436 | && (c->src_ip == src_ip) |
| 437 | && (c->dest_ip == dest_ip) |
| 438 | && (c->protocol == protocol)) { |
| 439 | return c; |
| 440 | } |
| 441 | |
| 442 | c = c->next; |
| 443 | } |
| 444 | |
| 445 | return NULL; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | /* |
Matthew McClintock | be7b47d | 2013-11-27 13:26:23 -0600 | [diff] [blame] | 449 | * sfe_ipv4_mark_rule() |
| 450 | * Updates the mark for a current offloaded connection |
| 451 | * |
| 452 | * Will take hash lock upon entry |
| 453 | */ |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 454 | void sfe_ipv4_mark_rule(struct sfe_connection_mark *mark) |
Matthew McClintock | be7b47d | 2013-11-27 13:26:23 -0600 | [diff] [blame] | 455 | { |
| 456 | struct sfe_ipv4 *si = &__si; |
| 457 | struct sfe_ipv4_connection *c; |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 458 | |
Xiaoping Fan | 3c423e3 | 2015-07-03 03:09:29 -0700 | [diff] [blame] | 459 | spin_lock_bh(&si->lock); |
Matthew McClintock | be7b47d | 2013-11-27 13:26:23 -0600 | [diff] [blame] | 460 | c = sfe_ipv4_find_sfe_ipv4_connection(si, mark->protocol, |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 461 | mark->src_ip.ip, mark->src_port, |
| 462 | mark->dest_ip.ip, mark->dest_port); |
Matthew McClintock | be7b47d | 2013-11-27 13:26:23 -0600 | [diff] [blame] | 463 | if (c) { |
Nicolas Costa | f53d6fe | 2014-01-13 16:03:46 -0600 | [diff] [blame] | 464 | WARN_ON((0 != c->mark) && (0 == mark->mark)); |
Matthew McClintock | be7b47d | 2013-11-27 13:26:23 -0600 | [diff] [blame] | 465 | c->mark = mark->mark; |
| 466 | } |
Xiaoping Fan | 3c423e3 | 2015-07-03 03:09:29 -0700 | [diff] [blame] | 467 | spin_unlock_bh(&si->lock); |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 468 | |
| 469 | if (c) { |
| 470 | DEBUG_TRACE("Matching connection found for mark, " |
| 471 | "setting from %08x to %08x\n", |
| 472 | c->mark, mark->mark); |
| 473 | } |
Matthew McClintock | be7b47d | 2013-11-27 13:26:23 -0600 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | /* |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 477 | * sfe_ipv4_insert_sfe_ipv4_connection() |
| 478 | * Insert a connection into the hash. |
| 479 | * |
| 480 | * On entry we must be holding the lock that protects the hash table. |
| 481 | */ |
| 482 | static void sfe_ipv4_insert_sfe_ipv4_connection(struct sfe_ipv4 *si, struct sfe_ipv4_connection *c) |
| 483 | { |
| 484 | struct sfe_ipv4_connection **hash_head; |
| 485 | struct sfe_ipv4_connection *prev_head; |
| 486 | unsigned int conn_idx; |
| 487 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 488 | lockdep_assert_held(&si->lock); |
| 489 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 490 | /* |
| 491 | * Insert entry into the connection hash. |
| 492 | */ |
| 493 | conn_idx = sfe_ipv4_get_connection_hash(c->protocol, c->src_ip, c->src_port, |
| 494 | c->dest_ip, c->dest_port); |
| 495 | hash_head = &si->conn_hash[conn_idx]; |
| 496 | prev_head = *hash_head; |
| 497 | c->prev = NULL; |
| 498 | if (prev_head) { |
| 499 | prev_head->prev = c; |
| 500 | } |
| 501 | |
| 502 | c->next = prev_head; |
| 503 | *hash_head = c; |
| 504 | |
| 505 | /* |
| 506 | * Insert entry into the "all connections" list. |
| 507 | */ |
| 508 | if (si->all_connections_tail) { |
| 509 | c->all_connections_prev = si->all_connections_tail; |
| 510 | si->all_connections_tail->all_connections_next = c; |
| 511 | } else { |
| 512 | c->all_connections_prev = NULL; |
| 513 | si->all_connections_head = c; |
| 514 | } |
| 515 | |
| 516 | si->all_connections_tail = c; |
| 517 | c->all_connections_next = NULL; |
| 518 | si->num_connections++; |
| 519 | |
| 520 | /* |
| 521 | * Insert the connection match objects too. |
| 522 | */ |
| 523 | sfe_ipv4_insert_sfe_ipv4_connection_match(si, c->original_match); |
| 524 | sfe_ipv4_insert_sfe_ipv4_connection_match(si, c->reply_match); |
| 525 | } |
| 526 | |
| 527 | /* |
| 528 | * sfe_ipv4_remove_sfe_ipv4_connection() |
| 529 | * Remove a sfe_ipv4_connection object from the hash. |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 530 | */ |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 531 | static bool sfe_ipv4_remove_sfe_ipv4_connection(struct sfe_ipv4 *si, struct sfe_ipv4_connection *c) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 532 | { |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 533 | lockdep_assert_held(&si->lock); |
| 534 | |
| 535 | if (c->removed) { |
| 536 | DEBUG_ERROR("%px: Connection has been removed already\n", c); |
| 537 | return false; |
| 538 | } |
| 539 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 540 | /* |
| 541 | * Remove the connection match objects. |
| 542 | */ |
| 543 | sfe_ipv4_remove_sfe_ipv4_connection_match(si, c->reply_match); |
| 544 | sfe_ipv4_remove_sfe_ipv4_connection_match(si, c->original_match); |
| 545 | |
| 546 | /* |
| 547 | * Unlink the connection. |
| 548 | */ |
| 549 | if (c->prev) { |
| 550 | c->prev->next = c->next; |
| 551 | } else { |
| 552 | unsigned int conn_idx = sfe_ipv4_get_connection_hash(c->protocol, c->src_ip, c->src_port, |
| 553 | c->dest_ip, c->dest_port); |
| 554 | si->conn_hash[conn_idx] = c->next; |
| 555 | } |
| 556 | |
| 557 | if (c->next) { |
| 558 | c->next->prev = c->prev; |
| 559 | } |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 560 | |
| 561 | /* |
| 562 | * Unlink connection from all_connections list |
| 563 | */ |
| 564 | if (c->all_connections_prev) { |
| 565 | c->all_connections_prev->all_connections_next = c->all_connections_next; |
| 566 | } else { |
| 567 | si->all_connections_head = c->all_connections_next; |
| 568 | } |
| 569 | |
| 570 | if (c->all_connections_next) { |
| 571 | c->all_connections_next->all_connections_prev = c->all_connections_prev; |
| 572 | } else { |
| 573 | si->all_connections_tail = c->all_connections_prev; |
| 574 | } |
| 575 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 576 | c->removed = true; |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 577 | si->num_connections--; |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 578 | return true; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | /* |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 582 | * sfe_ipv4_gen_sync_sfe_ipv4_connection() |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 583 | * Sync a connection. |
| 584 | * |
| 585 | * 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] | 586 | * already held (while called from sfe_ipv4_periodic_sync() or isn't required |
| 587 | * (while called from sfe_ipv4_flush_sfe_ipv4_connection()) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 588 | */ |
| 589 | static void sfe_ipv4_gen_sync_sfe_ipv4_connection(struct sfe_ipv4 *si, struct sfe_ipv4_connection *c, |
Xiaoping Fan | 99cb4c1 | 2015-08-21 19:07:32 -0700 | [diff] [blame] | 590 | struct sfe_connection_sync *sis, sfe_sync_reason_t reason, |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 591 | u64 now_jiffies) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 592 | { |
| 593 | struct sfe_ipv4_connection_match *original_cm; |
| 594 | struct sfe_ipv4_connection_match *reply_cm; |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 595 | u32 packet_count, byte_count; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 596 | |
| 597 | /* |
| 598 | * Fill in the update message. |
| 599 | */ |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 600 | sis->is_v6 = 0; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 601 | sis->protocol = c->protocol; |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 602 | sis->src_ip.ip = c->src_ip; |
Xiaoping Fan | 99cb4c1 | 2015-08-21 19:07:32 -0700 | [diff] [blame] | 603 | sis->src_ip_xlate.ip = c->src_ip_xlate; |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 604 | sis->dest_ip.ip = c->dest_ip; |
Xiaoping Fan | 99cb4c1 | 2015-08-21 19:07:32 -0700 | [diff] [blame] | 605 | sis->dest_ip_xlate.ip = c->dest_ip_xlate; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 606 | sis->src_port = c->src_port; |
Xiaoping Fan | 99cb4c1 | 2015-08-21 19:07:32 -0700 | [diff] [blame] | 607 | sis->src_port_xlate = c->src_port_xlate; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 608 | sis->dest_port = c->dest_port; |
Xiaoping Fan | 99cb4c1 | 2015-08-21 19:07:32 -0700 | [diff] [blame] | 609 | sis->dest_port_xlate = c->dest_port_xlate; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 610 | |
| 611 | original_cm = c->original_match; |
| 612 | reply_cm = c->reply_match; |
| 613 | sis->src_td_max_window = original_cm->protocol_state.tcp.max_win; |
| 614 | sis->src_td_end = original_cm->protocol_state.tcp.end; |
| 615 | sis->src_td_max_end = original_cm->protocol_state.tcp.max_end; |
| 616 | sis->dest_td_max_window = reply_cm->protocol_state.tcp.max_win; |
| 617 | sis->dest_td_end = reply_cm->protocol_state.tcp.end; |
| 618 | sis->dest_td_max_end = reply_cm->protocol_state.tcp.max_end; |
| 619 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 620 | sfe_ipv4_connection_match_update_summary_stats(original_cm, &packet_count, &byte_count); |
| 621 | sis->src_new_packet_count = packet_count; |
| 622 | sis->src_new_byte_count = byte_count; |
Matthew McClintock | d0cdb80 | 2014-02-24 16:30:35 -0600 | [diff] [blame] | 623 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 624 | sfe_ipv4_connection_match_update_summary_stats(reply_cm, &packet_count, &byte_count); |
| 625 | sis->dest_new_packet_count = packet_count; |
| 626 | sis->dest_new_byte_count = byte_count; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 627 | |
Matthew McClintock | d0cdb80 | 2014-02-24 16:30:35 -0600 | [diff] [blame] | 628 | sis->src_dev = original_cm->match_dev; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 629 | sis->src_packet_count = original_cm->rx_packet_count64; |
| 630 | sis->src_byte_count = original_cm->rx_byte_count64; |
Matthew McClintock | d0cdb80 | 2014-02-24 16:30:35 -0600 | [diff] [blame] | 631 | |
| 632 | sis->dest_dev = reply_cm->match_dev; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 633 | sis->dest_packet_count = reply_cm->rx_packet_count64; |
| 634 | sis->dest_byte_count = reply_cm->rx_byte_count64; |
| 635 | |
Xiaoping Fan | 99cb4c1 | 2015-08-21 19:07:32 -0700 | [diff] [blame] | 636 | sis->reason = reason; |
| 637 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 638 | /* |
| 639 | * Get the time increment since our last sync. |
| 640 | */ |
| 641 | sis->delta_jiffies = now_jiffies - c->last_sync_jiffies; |
| 642 | c->last_sync_jiffies = now_jiffies; |
| 643 | } |
| 644 | |
| 645 | /* |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 646 | * sfe_ipv4_free_sfe_ipv4_connection_rcu() |
| 647 | * Called at RCU qs state to free the connection object. |
| 648 | */ |
| 649 | static void sfe_ipv4_free_sfe_ipv4_connection_rcu(struct rcu_head *head) |
| 650 | { |
| 651 | struct sfe_ipv4_connection *c; |
| 652 | |
| 653 | /* |
| 654 | * We dont need spin lock as the connection is already removed from link list |
| 655 | */ |
| 656 | c = container_of(head, struct sfe_ipv4_connection, rcu); |
| 657 | |
| 658 | BUG_ON(!c->removed); |
| 659 | |
| 660 | DEBUG_TRACE("%px: connecton has been deleted\n", c); |
| 661 | |
| 662 | /* |
| 663 | * Release our hold of the source and dest devices and free the memory |
| 664 | * for our connection objects. |
| 665 | */ |
| 666 | dev_put(c->original_dev); |
| 667 | dev_put(c->reply_dev); |
| 668 | kfree(c->original_match); |
| 669 | kfree(c->reply_match); |
| 670 | kfree(c); |
| 671 | } |
| 672 | |
| 673 | /* |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 674 | * sfe_ipv4_flush_sfe_ipv4_connection() |
| 675 | * Flush a connection and free all associated resources. |
| 676 | * |
| 677 | * We need to be called with bottom halves disabled locally as we need to acquire |
| 678 | * the connection hash lock and release it again. In general we're actually called |
| 679 | * from within a BH and so we're fine, but we're also called when connections are |
| 680 | * torn down. |
| 681 | */ |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 682 | static void sfe_ipv4_flush_sfe_ipv4_connection(struct sfe_ipv4 *si, |
| 683 | struct sfe_ipv4_connection *c, |
| 684 | sfe_sync_reason_t reason) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 685 | { |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 686 | u64 now_jiffies; |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 687 | sfe_sync_rule_callback_t sync_rule_callback; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 688 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 689 | BUG_ON(!c->removed); |
| 690 | |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 691 | this_cpu_inc(si->stats_pcpu->connection_flushes64); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 692 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 693 | rcu_read_lock(); |
| 694 | sync_rule_callback = rcu_dereference(si->sync_rule_callback); |
| 695 | |
| 696 | /* |
| 697 | * Generate a sync message and then sync. |
| 698 | */ |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 699 | if (sync_rule_callback) { |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 700 | struct sfe_connection_sync sis; |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 701 | now_jiffies = get_jiffies_64(); |
Xiaoping Fan | 99cb4c1 | 2015-08-21 19:07:32 -0700 | [diff] [blame] | 702 | sfe_ipv4_gen_sync_sfe_ipv4_connection(si, c, &sis, reason, now_jiffies); |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 703 | sync_rule_callback(&sis); |
| 704 | } |
| 705 | |
| 706 | rcu_read_unlock(); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 707 | |
| 708 | /* |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 709 | * Release our hold of the source and dest devices and free the memory |
| 710 | * for our connection objects. |
| 711 | */ |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 712 | call_rcu(&c->rcu, sfe_ipv4_free_sfe_ipv4_connection_rcu); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | /* |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 716 | * sfe_ipv4_exception_stats_inc() |
| 717 | * Increment exception stats. |
| 718 | */ |
| 719 | static inline void sfe_ipv4_exception_stats_inc(struct sfe_ipv4 *si, enum sfe_ipv4_exception_events reason) |
| 720 | { |
| 721 | struct sfe_ipv4_stats *stats = this_cpu_ptr(si->stats_pcpu); |
| 722 | stats->exception_events64[reason]++; |
| 723 | stats->packets_not_forwarded64++; |
| 724 | } |
| 725 | |
| 726 | /* |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 727 | * sfe_ipv4_recv_udp() |
| 728 | * Handle UDP packet receives and forwarding. |
| 729 | */ |
| 730 | static int sfe_ipv4_recv_udp(struct sfe_ipv4 *si, struct sk_buff *skb, struct net_device *dev, |
Ratheesh Kannoth | 741f799 | 2021-10-20 07:39:52 +0530 | [diff] [blame] | 731 | unsigned int len, struct iphdr *iph, unsigned int ihl, bool flush_on_find) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 732 | { |
Ratheesh Kannoth | 741f799 | 2021-10-20 07:39:52 +0530 | [diff] [blame] | 733 | struct udphdr *udph; |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 734 | __be32 src_ip; |
| 735 | __be32 dest_ip; |
| 736 | __be16 src_port; |
| 737 | __be16 dest_port; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 738 | struct sfe_ipv4_connection_match *cm; |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 739 | u8 ttl; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 740 | struct net_device *xmit_dev; |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 741 | bool ret; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 742 | |
| 743 | /* |
| 744 | * Is our packet too short to contain a valid UDP header? |
| 745 | */ |
Ratheesh Kannoth | 741f799 | 2021-10-20 07:39:52 +0530 | [diff] [blame] | 746 | if (unlikely(!pskb_may_pull(skb, (sizeof(struct udphdr) + ihl)))) { |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 747 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_UDP_HEADER_INCOMPLETE); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 748 | DEBUG_TRACE("packet too short for UDP header\n"); |
| 749 | return 0; |
| 750 | } |
| 751 | |
| 752 | /* |
| 753 | * Read the IP address and port information. Read the IP header data first |
| 754 | * because we've almost certainly got that in the cache. We may not yet have |
| 755 | * the UDP header cached though so allow more time for any prefetching. |
| 756 | */ |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 757 | src_ip = iph->saddr; |
| 758 | dest_ip = iph->daddr; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 759 | |
Ratheesh Kannoth | 741f799 | 2021-10-20 07:39:52 +0530 | [diff] [blame] | 760 | udph = (struct udphdr *)(skb->data + ihl); |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 761 | src_port = udph->source; |
| 762 | dest_port = udph->dest; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 763 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 764 | rcu_read_lock(); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 765 | |
| 766 | /* |
| 767 | * Look for a connection match. |
| 768 | */ |
Xiaoping Fan | d1dc7b2 | 2015-01-23 00:43:56 -0800 | [diff] [blame] | 769 | #ifdef CONFIG_NF_FLOW_COOKIE |
| 770 | cm = si->sfe_flow_cookie_table[skb->flow_cookie & SFE_FLOW_COOKIE_MASK].match; |
| 771 | if (unlikely(!cm)) { |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 772 | cm = sfe_ipv4_find_sfe_ipv4_connection_match_rcu(si, dev, IPPROTO_UDP, src_ip, src_port, dest_ip, dest_port); |
Xiaoping Fan | d1dc7b2 | 2015-01-23 00:43:56 -0800 | [diff] [blame] | 773 | } |
| 774 | #else |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 775 | cm = sfe_ipv4_find_sfe_ipv4_connection_match_rcu(si, dev, IPPROTO_UDP, src_ip, src_port, dest_ip, dest_port); |
Xiaoping Fan | d1dc7b2 | 2015-01-23 00:43:56 -0800 | [diff] [blame] | 776 | #endif |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 777 | if (unlikely(!cm)) { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 778 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 779 | rcu_read_unlock(); |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 780 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_UDP_NO_CONNECTION); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 781 | DEBUG_TRACE("no connection found\n"); |
| 782 | return 0; |
| 783 | } |
| 784 | |
| 785 | /* |
| 786 | * If our packet has beern marked as "flush on find" we can't actually |
| 787 | * forward it in the fast path, but now that we've found an associated |
| 788 | * connection we can flush that out before we process the packet. |
| 789 | */ |
| 790 | if (unlikely(flush_on_find)) { |
| 791 | struct sfe_ipv4_connection *c = cm->connection; |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 792 | spin_lock_bh(&si->lock); |
| 793 | ret = sfe_ipv4_remove_sfe_ipv4_connection(si, c); |
Xiaoping Fan | 3c423e3 | 2015-07-03 03:09:29 -0700 | [diff] [blame] | 794 | spin_unlock_bh(&si->lock); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 795 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 796 | if (ret) { |
| 797 | sfe_ipv4_flush_sfe_ipv4_connection(si, c, SFE_SYNC_REASON_FLUSH); |
| 798 | } |
| 799 | rcu_read_unlock(); |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 800 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_UDP_IP_OPTIONS_OR_INITIAL_FRAGMENT); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 801 | DEBUG_TRACE("flush on find\n"); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 802 | return 0; |
| 803 | } |
| 804 | |
Zhi Chen | 8748eb3 | 2015-06-18 12:58:48 -0700 | [diff] [blame] | 805 | #ifdef CONFIG_XFRM |
| 806 | /* |
| 807 | * We can't accelerate the flow on this direction, just let it go |
| 808 | * through the slow path. |
| 809 | */ |
| 810 | if (unlikely(!cm->flow_accel)) { |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 811 | rcu_read_unlock(); |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 812 | this_cpu_inc(si->stats_pcpu->packets_not_forwarded64); |
Zhi Chen | 8748eb3 | 2015-06-18 12:58:48 -0700 | [diff] [blame] | 813 | return 0; |
| 814 | } |
| 815 | #endif |
| 816 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 817 | /* |
| 818 | * Does our TTL allow forwarding? |
| 819 | */ |
| 820 | ttl = iph->ttl; |
| 821 | if (unlikely(ttl < 2)) { |
| 822 | struct sfe_ipv4_connection *c = cm->connection; |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 823 | spin_lock_bh(&si->lock); |
| 824 | ret = sfe_ipv4_remove_sfe_ipv4_connection(si, c); |
Xiaoping Fan | 3c423e3 | 2015-07-03 03:09:29 -0700 | [diff] [blame] | 825 | spin_unlock_bh(&si->lock); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 826 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 827 | if (ret) { |
| 828 | sfe_ipv4_flush_sfe_ipv4_connection(si, c, SFE_SYNC_REASON_FLUSH); |
| 829 | } |
| 830 | rcu_read_unlock(); |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 831 | |
| 832 | DEBUG_TRACE("ttl too low\n"); |
| 833 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_UDP_SMALL_TTL); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 834 | return 0; |
| 835 | } |
| 836 | |
| 837 | /* |
| 838 | * If our packet is larger than the MTU of the transmit interface then |
| 839 | * we can't forward it easily. |
| 840 | */ |
| 841 | if (unlikely(len > cm->xmit_dev_mtu)) { |
| 842 | struct sfe_ipv4_connection *c = cm->connection; |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 843 | spin_lock_bh(&si->lock); |
| 844 | ret = sfe_ipv4_remove_sfe_ipv4_connection(si, c); |
Xiaoping Fan | 3c423e3 | 2015-07-03 03:09:29 -0700 | [diff] [blame] | 845 | spin_unlock_bh(&si->lock); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 846 | |
| 847 | DEBUG_TRACE("larger than mtu\n"); |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 848 | if (ret) { |
| 849 | sfe_ipv4_flush_sfe_ipv4_connection(si, c, SFE_SYNC_REASON_FLUSH); |
| 850 | } |
| 851 | rcu_read_unlock(); |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 852 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_UDP_NEEDS_FRAGMENTATION); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 853 | return 0; |
| 854 | } |
| 855 | |
| 856 | /* |
| 857 | * From this point on we're good to modify the packet. |
| 858 | */ |
| 859 | |
| 860 | /* |
Murat Sezgin | c7dd817 | 2019-02-27 15:23:50 -0800 | [diff] [blame] | 861 | * Check if skb was cloned. If it was, unshare it. Because |
| 862 | * the data area is going to be written in this path and we don't want to |
| 863 | * change the cloned skb's data section. |
| 864 | */ |
| 865 | if (unlikely(skb_cloned(skb))) { |
Tian Yang | 45f39c8 | 2020-10-06 14:07:47 -0700 | [diff] [blame] | 866 | DEBUG_TRACE("%px: skb is a cloned skb\n", skb); |
Murat Sezgin | c7dd817 | 2019-02-27 15:23:50 -0800 | [diff] [blame] | 867 | skb = skb_unshare(skb, GFP_ATOMIC); |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 868 | if (!skb) { |
Murat Sezgin | c7dd817 | 2019-02-27 15:23:50 -0800 | [diff] [blame] | 869 | DEBUG_WARN("Failed to unshare the cloned skb\n"); |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 870 | rcu_read_unlock(); |
Murat Sezgin | c7dd817 | 2019-02-27 15:23:50 -0800 | [diff] [blame] | 871 | return 0; |
| 872 | } |
| 873 | |
| 874 | /* |
| 875 | * Update the iph and udph pointers with the unshared skb's data area. |
| 876 | */ |
Ratheesh Kannoth | 741f799 | 2021-10-20 07:39:52 +0530 | [diff] [blame] | 877 | iph = (struct iphdr *)skb->data; |
| 878 | udph = (struct udphdr *)(skb->data + ihl); |
Murat Sezgin | c7dd817 | 2019-02-27 15:23:50 -0800 | [diff] [blame] | 879 | } |
| 880 | |
| 881 | /* |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 882 | * Update DSCP |
| 883 | */ |
| 884 | if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_DSCP_REMARK)) { |
| 885 | iph->tos = (iph->tos & SFE_IPV4_DSCP_MASK) | cm->dscp; |
| 886 | } |
| 887 | |
| 888 | /* |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 889 | * Decrement our TTL. |
| 890 | */ |
| 891 | iph->ttl = ttl - 1; |
| 892 | |
| 893 | /* |
| 894 | * Do we have to perform translations of the source address/port? |
| 895 | */ |
| 896 | if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC)) { |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 897 | u16 udp_csum; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 898 | |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 899 | iph->saddr = cm->xlate_src_ip; |
| 900 | udph->source = cm->xlate_src_port; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 901 | |
| 902 | /* |
| 903 | * Do we have a non-zero UDP checksum? If we do then we need |
| 904 | * to update it. |
| 905 | */ |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 906 | udp_csum = udph->check; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 907 | if (likely(udp_csum)) { |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 908 | u32 sum; |
Xiaoping Fan | ad755af | 2015-04-01 16:58:46 -0700 | [diff] [blame] | 909 | |
| 910 | if (unlikely(skb->ip_summed == CHECKSUM_PARTIAL)) { |
| 911 | sum = udp_csum + cm->xlate_src_partial_csum_adjustment; |
| 912 | } else { |
| 913 | sum = udp_csum + cm->xlate_src_csum_adjustment; |
| 914 | } |
| 915 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 916 | sum = (sum & 0xffff) + (sum >> 16); |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 917 | udph->check = (u16)sum; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 918 | } |
| 919 | } |
| 920 | |
| 921 | /* |
| 922 | * Do we have to perform translations of the destination address/port? |
| 923 | */ |
| 924 | if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST)) { |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 925 | u16 udp_csum; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 926 | |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 927 | iph->daddr = cm->xlate_dest_ip; |
| 928 | udph->dest = cm->xlate_dest_port; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 929 | |
| 930 | /* |
| 931 | * Do we have a non-zero UDP checksum? If we do then we need |
| 932 | * to update it. |
| 933 | */ |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 934 | udp_csum = udph->check; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 935 | if (likely(udp_csum)) { |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 936 | u32 sum; |
Xiaoping Fan | ad755af | 2015-04-01 16:58:46 -0700 | [diff] [blame] | 937 | |
| 938 | if (unlikely(skb->ip_summed == CHECKSUM_PARTIAL)) { |
| 939 | sum = udp_csum + cm->xlate_dest_partial_csum_adjustment; |
| 940 | } else { |
| 941 | sum = udp_csum + cm->xlate_dest_csum_adjustment; |
| 942 | } |
| 943 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 944 | sum = (sum & 0xffff) + (sum >> 16); |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 945 | udph->check = (u16)sum; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 946 | } |
| 947 | } |
| 948 | |
| 949 | /* |
| 950 | * Replace the IP checksum. |
| 951 | */ |
| 952 | iph->check = sfe_ipv4_gen_ip_csum(iph); |
| 953 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 954 | /* |
| 955 | * Update traffic stats. |
| 956 | */ |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 957 | atomic_inc(&cm->rx_packet_count); |
| 958 | atomic_add(len, &cm->rx_byte_count); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 959 | |
| 960 | /* |
| 961 | * If we're not already on the active list then insert ourselves at the tail |
| 962 | * of the current list. |
| 963 | */ |
| 964 | if (unlikely(!cm->active)) { |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 965 | spin_lock_bh(&si->lock); |
| 966 | if (unlikely(!cm->active)) { |
| 967 | cm->active = true; |
| 968 | cm->active_prev = si->active_tail; |
| 969 | if (likely(si->active_tail)) { |
| 970 | si->active_tail->active_next = cm; |
| 971 | } else { |
| 972 | si->active_head = cm; |
| 973 | } |
| 974 | si->active_tail = cm; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 975 | } |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 976 | spin_unlock_bh(&si->lock); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | xmit_dev = cm->xmit_dev; |
| 980 | skb->dev = xmit_dev; |
| 981 | |
| 982 | /* |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 983 | * Check to see if we need to write a header. |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 984 | */ |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 985 | if (likely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_L2_HDR)) { |
| 986 | if (unlikely(!(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR))) { |
Xiaoping Fan | 2784e61 | 2015-06-25 17:57:41 -0700 | [diff] [blame] | 987 | dev_hard_header(skb, xmit_dev, ETH_P_IP, |
| 988 | cm->xmit_dest_mac, cm->xmit_src_mac, len); |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 989 | } else { |
| 990 | /* |
| 991 | * For the simple case we write this really fast. |
| 992 | */ |
Ratheesh Kannoth | 741f799 | 2021-10-20 07:39:52 +0530 | [diff] [blame] | 993 | struct ethhdr *eth = (struct ethhdr *)__skb_push(skb, ETH_HLEN); |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 994 | eth->h_proto = htons(ETH_P_IP); |
Ratheesh Kannoth | 741f799 | 2021-10-20 07:39:52 +0530 | [diff] [blame] | 995 | ether_addr_copy((u8 *)eth->h_dest, (u8 *)cm->xmit_dest_mac); |
| 996 | ether_addr_copy((u8 *)eth->h_source, (u8 *)cm->xmit_src_mac); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 997 | } |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 998 | } |
| 999 | |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 1000 | /* |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 1001 | * Update priority of skb. |
| 1002 | */ |
| 1003 | if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_PRIORITY_REMARK)) { |
| 1004 | skb->priority = cm->priority; |
| 1005 | } |
| 1006 | |
| 1007 | /* |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 1008 | * Mark outgoing packet. |
| 1009 | */ |
Cristian Prundeanu | 94fff98 | 2013-12-23 15:02:59 -0600 | [diff] [blame] | 1010 | skb->mark = cm->connection->mark; |
| 1011 | if (skb->mark) { |
| 1012 | DEBUG_TRACE("SKB MARK is NON ZERO %x\n", skb->mark); |
| 1013 | } |
| 1014 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1015 | rcu_read_unlock(); |
| 1016 | |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 1017 | this_cpu_inc(si->stats_pcpu->packets_forwarded64); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1018 | |
| 1019 | /* |
| 1020 | * We're going to check for GSO flags when we transmit the packet so |
| 1021 | * start fetching the necessary cache line now. |
| 1022 | */ |
| 1023 | prefetch(skb_shinfo(skb)); |
| 1024 | |
| 1025 | /* |
Nicolas Costa | 9ec8c7b | 2014-01-29 12:50:46 -0600 | [diff] [blame] | 1026 | * Mark that this packet has been fast forwarded. |
| 1027 | */ |
| 1028 | skb->fast_forwarded = 1; |
| 1029 | |
| 1030 | /* |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1031 | * Send the packet on its way. |
| 1032 | */ |
| 1033 | dev_queue_xmit(skb); |
| 1034 | |
| 1035 | return 1; |
| 1036 | } |
| 1037 | |
| 1038 | /* |
| 1039 | * sfe_ipv4_process_tcp_option_sack() |
| 1040 | * Parse TCP SACK option and update ack according |
| 1041 | */ |
Ratheesh Kannoth | 741f799 | 2021-10-20 07:39:52 +0530 | [diff] [blame] | 1042 | static bool sfe_ipv4_process_tcp_option_sack(const struct tcphdr *th, const u32 data_offs, |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 1043 | u32 *ack) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1044 | { |
Ratheesh Kannoth | 741f799 | 2021-10-20 07:39:52 +0530 | [diff] [blame] | 1045 | u32 length = sizeof(struct tcphdr); |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 1046 | u8 *ptr = (u8 *)th + length; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1047 | |
| 1048 | /* |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 1049 | * Ignore processing if TCP packet has only TIMESTAMP option. |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1050 | */ |
| 1051 | if (likely(data_offs == length + TCPOLEN_TIMESTAMP + 1 + 1) |
| 1052 | && likely(ptr[0] == TCPOPT_NOP) |
| 1053 | && likely(ptr[1] == TCPOPT_NOP) |
| 1054 | && likely(ptr[2] == TCPOPT_TIMESTAMP) |
| 1055 | && likely(ptr[3] == TCPOLEN_TIMESTAMP)) { |
| 1056 | return true; |
| 1057 | } |
| 1058 | |
| 1059 | /* |
| 1060 | * TCP options. Parse SACK option. |
| 1061 | */ |
| 1062 | while (length < data_offs) { |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 1063 | u8 size; |
| 1064 | u8 kind; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1065 | |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 1066 | ptr = (u8 *)th + length; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1067 | kind = *ptr; |
| 1068 | |
| 1069 | /* |
| 1070 | * NOP, for padding |
| 1071 | * Not in the switch because to fast escape and to not calculate size |
| 1072 | */ |
| 1073 | if (kind == TCPOPT_NOP) { |
| 1074 | length++; |
| 1075 | continue; |
| 1076 | } |
| 1077 | |
| 1078 | if (kind == TCPOPT_SACK) { |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 1079 | u32 sack = 0; |
| 1080 | u8 re = 1 + 1; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1081 | |
| 1082 | size = *(ptr + 1); |
| 1083 | if ((size < (1 + 1 + TCPOLEN_SACK_PERBLOCK)) |
| 1084 | || ((size - (1 + 1)) % (TCPOLEN_SACK_PERBLOCK)) |
| 1085 | || (size > (data_offs - length))) { |
| 1086 | return false; |
| 1087 | } |
| 1088 | |
| 1089 | re += 4; |
| 1090 | while (re < size) { |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 1091 | u32 sack_re; |
| 1092 | u8 *sptr = ptr + re; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1093 | sack_re = (sptr[0] << 24) | (sptr[1] << 16) | (sptr[2] << 8) | sptr[3]; |
| 1094 | if (sack_re > sack) { |
| 1095 | sack = sack_re; |
| 1096 | } |
| 1097 | re += TCPOLEN_SACK_PERBLOCK; |
| 1098 | } |
| 1099 | if (sack > *ack) { |
| 1100 | *ack = sack; |
| 1101 | } |
| 1102 | length += size; |
| 1103 | continue; |
| 1104 | } |
| 1105 | if (kind == TCPOPT_EOL) { |
| 1106 | return true; |
| 1107 | } |
| 1108 | size = *(ptr + 1); |
| 1109 | if (size < 2) { |
| 1110 | return false; |
| 1111 | } |
| 1112 | length += size; |
| 1113 | } |
| 1114 | |
| 1115 | return true; |
| 1116 | } |
| 1117 | |
| 1118 | /* |
| 1119 | * sfe_ipv4_recv_tcp() |
| 1120 | * Handle TCP packet receives and forwarding. |
| 1121 | */ |
| 1122 | static int sfe_ipv4_recv_tcp(struct sfe_ipv4 *si, struct sk_buff *skb, struct net_device *dev, |
Ratheesh Kannoth | 741f799 | 2021-10-20 07:39:52 +0530 | [diff] [blame] | 1123 | unsigned int len, struct iphdr *iph, unsigned int ihl, bool flush_on_find) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1124 | { |
Ratheesh Kannoth | 741f799 | 2021-10-20 07:39:52 +0530 | [diff] [blame] | 1125 | struct tcphdr *tcph; |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 1126 | __be32 src_ip; |
| 1127 | __be32 dest_ip; |
| 1128 | __be16 src_port; |
| 1129 | __be16 dest_port; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1130 | struct sfe_ipv4_connection_match *cm; |
| 1131 | struct sfe_ipv4_connection_match *counter_cm; |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 1132 | u8 ttl; |
| 1133 | u32 flags; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1134 | struct net_device *xmit_dev; |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1135 | bool ret; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1136 | |
| 1137 | /* |
| 1138 | * Is our packet too short to contain a valid UDP header? |
| 1139 | */ |
Ratheesh Kannoth | 741f799 | 2021-10-20 07:39:52 +0530 | [diff] [blame] | 1140 | if (unlikely(!pskb_may_pull(skb, (sizeof(struct tcphdr) + ihl)))) { |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 1141 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_TCP_HEADER_INCOMPLETE); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1142 | DEBUG_TRACE("packet too short for TCP header\n"); |
| 1143 | return 0; |
| 1144 | } |
| 1145 | |
| 1146 | /* |
| 1147 | * Read the IP address and port information. Read the IP header data first |
| 1148 | * because we've almost certainly got that in the cache. We may not yet have |
| 1149 | * the TCP header cached though so allow more time for any prefetching. |
| 1150 | */ |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 1151 | src_ip = iph->saddr; |
| 1152 | dest_ip = iph->daddr; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1153 | |
Ratheesh Kannoth | 741f799 | 2021-10-20 07:39:52 +0530 | [diff] [blame] | 1154 | tcph = (struct tcphdr *)(skb->data + ihl); |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 1155 | src_port = tcph->source; |
| 1156 | dest_port = tcph->dest; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1157 | flags = tcp_flag_word(tcph); |
| 1158 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1159 | rcu_read_lock(); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1160 | |
| 1161 | /* |
| 1162 | * Look for a connection match. |
| 1163 | */ |
Xiaoping Fan | d1dc7b2 | 2015-01-23 00:43:56 -0800 | [diff] [blame] | 1164 | #ifdef CONFIG_NF_FLOW_COOKIE |
| 1165 | cm = si->sfe_flow_cookie_table[skb->flow_cookie & SFE_FLOW_COOKIE_MASK].match; |
| 1166 | if (unlikely(!cm)) { |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1167 | cm = sfe_ipv4_find_sfe_ipv4_connection_match_rcu(si, dev, IPPROTO_TCP, src_ip, src_port, dest_ip, dest_port); |
Xiaoping Fan | d1dc7b2 | 2015-01-23 00:43:56 -0800 | [diff] [blame] | 1168 | } |
| 1169 | #else |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1170 | cm = sfe_ipv4_find_sfe_ipv4_connection_match_rcu(si, dev, IPPROTO_TCP, src_ip, src_port, dest_ip, dest_port); |
Xiaoping Fan | d1dc7b2 | 2015-01-23 00:43:56 -0800 | [diff] [blame] | 1171 | #endif |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1172 | if (unlikely(!cm)) { |
| 1173 | /* |
| 1174 | * We didn't get a connection but as TCP is connection-oriented that |
| 1175 | * may be because this is a non-fast connection (not running established). |
| 1176 | * For diagnostic purposes we differentiate this here. |
| 1177 | */ |
| 1178 | if (likely((flags & (TCP_FLAG_SYN | TCP_FLAG_RST | TCP_FLAG_FIN | TCP_FLAG_ACK)) == TCP_FLAG_ACK)) { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1179 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1180 | rcu_read_unlock(); |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 1181 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_TCP_NO_CONNECTION_FAST_FLAGS); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1182 | DEBUG_TRACE("no connection found - fast flags\n"); |
| 1183 | return 0; |
| 1184 | } |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1185 | |
| 1186 | rcu_read_unlock(); |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 1187 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_TCP_NO_CONNECTION_SLOW_FLAGS); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1188 | DEBUG_TRACE("no connection found - slow flags: 0x%x\n", |
| 1189 | flags & (TCP_FLAG_SYN | TCP_FLAG_RST | TCP_FLAG_FIN | TCP_FLAG_ACK)); |
| 1190 | return 0; |
| 1191 | } |
| 1192 | |
| 1193 | /* |
| 1194 | * If our packet has beern marked as "flush on find" we can't actually |
| 1195 | * forward it in the fast path, but now that we've found an associated |
| 1196 | * connection we can flush that out before we process the packet. |
| 1197 | */ |
| 1198 | if (unlikely(flush_on_find)) { |
| 1199 | struct sfe_ipv4_connection *c = cm->connection; |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1200 | |
| 1201 | spin_lock_bh(&si->lock); |
| 1202 | ret = sfe_ipv4_remove_sfe_ipv4_connection(si, c); |
Xiaoping Fan | 3c423e3 | 2015-07-03 03:09:29 -0700 | [diff] [blame] | 1203 | spin_unlock_bh(&si->lock); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1204 | |
| 1205 | DEBUG_TRACE("flush on find\n"); |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1206 | if (ret) { |
| 1207 | sfe_ipv4_flush_sfe_ipv4_connection(si, c, SFE_SYNC_REASON_FLUSH); |
| 1208 | } |
| 1209 | |
| 1210 | rcu_read_unlock(); |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 1211 | |
| 1212 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_TCP_IP_OPTIONS_OR_INITIAL_FRAGMENT); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1213 | return 0; |
| 1214 | } |
| 1215 | |
Zhi Chen | 8748eb3 | 2015-06-18 12:58:48 -0700 | [diff] [blame] | 1216 | #ifdef CONFIG_XFRM |
| 1217 | /* |
| 1218 | * We can't accelerate the flow on this direction, just let it go |
| 1219 | * through the slow path. |
| 1220 | */ |
| 1221 | if (unlikely(!cm->flow_accel)) { |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1222 | rcu_read_unlock(); |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 1223 | this_cpu_inc(si->stats_pcpu->packets_not_forwarded64); |
Zhi Chen | 8748eb3 | 2015-06-18 12:58:48 -0700 | [diff] [blame] | 1224 | return 0; |
| 1225 | } |
| 1226 | #endif |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1227 | /* |
| 1228 | * Does our TTL allow forwarding? |
| 1229 | */ |
| 1230 | ttl = iph->ttl; |
| 1231 | if (unlikely(ttl < 2)) { |
| 1232 | struct sfe_ipv4_connection *c = cm->connection; |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1233 | spin_lock_bh(&si->lock); |
| 1234 | ret = sfe_ipv4_remove_sfe_ipv4_connection(si, c); |
Xiaoping Fan | 3c423e3 | 2015-07-03 03:09:29 -0700 | [diff] [blame] | 1235 | spin_unlock_bh(&si->lock); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1236 | |
| 1237 | DEBUG_TRACE("ttl too low\n"); |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1238 | if (ret) { |
| 1239 | sfe_ipv4_flush_sfe_ipv4_connection(si, c, SFE_SYNC_REASON_FLUSH); |
| 1240 | } |
| 1241 | |
| 1242 | rcu_read_unlock(); |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 1243 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_TCP_SMALL_TTL); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1244 | return 0; |
| 1245 | } |
| 1246 | |
| 1247 | /* |
| 1248 | * If our packet is larger than the MTU of the transmit interface then |
| 1249 | * we can't forward it easily. |
| 1250 | */ |
Xiaoping Fan | d642a6e | 2015-04-10 15:19:06 -0700 | [diff] [blame] | 1251 | if (unlikely((len > cm->xmit_dev_mtu) && !skb_is_gso(skb))) { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1252 | struct sfe_ipv4_connection *c = cm->connection; |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1253 | spin_lock_bh(&si->lock); |
| 1254 | ret = sfe_ipv4_remove_sfe_ipv4_connection(si, c); |
Xiaoping Fan | 3c423e3 | 2015-07-03 03:09:29 -0700 | [diff] [blame] | 1255 | spin_unlock_bh(&si->lock); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1256 | |
| 1257 | DEBUG_TRACE("larger than mtu\n"); |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1258 | if (ret) { |
| 1259 | sfe_ipv4_flush_sfe_ipv4_connection(si, c, SFE_SYNC_REASON_FLUSH); |
| 1260 | } |
| 1261 | |
| 1262 | rcu_read_unlock(); |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 1263 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_UDP_IP_OPTIONS_OR_INITIAL_FRAGMENT); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1264 | return 0; |
| 1265 | } |
| 1266 | |
| 1267 | /* |
| 1268 | * Look at our TCP flags. Anything missing an ACK or that has RST, SYN or FIN |
| 1269 | * set is not a fast path packet. |
| 1270 | */ |
| 1271 | if (unlikely((flags & (TCP_FLAG_SYN | TCP_FLAG_RST | TCP_FLAG_FIN | TCP_FLAG_ACK)) != TCP_FLAG_ACK)) { |
| 1272 | struct sfe_ipv4_connection *c = cm->connection; |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1273 | spin_lock_bh(&si->lock); |
| 1274 | ret = sfe_ipv4_remove_sfe_ipv4_connection(si, c); |
Xiaoping Fan | 3c423e3 | 2015-07-03 03:09:29 -0700 | [diff] [blame] | 1275 | spin_unlock_bh(&si->lock); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1276 | |
| 1277 | DEBUG_TRACE("TCP flags: 0x%x are not fast\n", |
| 1278 | flags & (TCP_FLAG_SYN | TCP_FLAG_RST | TCP_FLAG_FIN | TCP_FLAG_ACK)); |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1279 | if (ret) { |
| 1280 | sfe_ipv4_flush_sfe_ipv4_connection(si, c, SFE_SYNC_REASON_FLUSH); |
| 1281 | } |
| 1282 | rcu_read_unlock(); |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 1283 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_TCP_FLAGS); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1284 | return 0; |
| 1285 | } |
| 1286 | |
| 1287 | counter_cm = cm->counter_match; |
| 1288 | |
| 1289 | /* |
| 1290 | * Are we doing sequence number checking? |
| 1291 | */ |
| 1292 | if (likely(!(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK))) { |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 1293 | u32 seq; |
| 1294 | u32 ack; |
| 1295 | u32 sack; |
| 1296 | u32 data_offs; |
| 1297 | u32 end; |
| 1298 | u32 left_edge; |
| 1299 | u32 scaled_win; |
| 1300 | u32 max_end; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1301 | |
| 1302 | /* |
| 1303 | * Is our sequence fully past the right hand edge of the window? |
| 1304 | */ |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 1305 | seq = ntohl(tcph->seq); |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 1306 | if (unlikely((s32)(seq - (cm->protocol_state.tcp.max_end + 1)) > 0)) { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1307 | struct sfe_ipv4_connection *c = cm->connection; |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1308 | spin_lock_bh(&si->lock); |
| 1309 | ret = sfe_ipv4_remove_sfe_ipv4_connection(si, c); |
Xiaoping Fan | 3c423e3 | 2015-07-03 03:09:29 -0700 | [diff] [blame] | 1310 | spin_unlock_bh(&si->lock); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1311 | |
| 1312 | DEBUG_TRACE("seq: %u exceeds right edge: %u\n", |
| 1313 | seq, cm->protocol_state.tcp.max_end + 1); |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1314 | if (ret) { |
| 1315 | sfe_ipv4_flush_sfe_ipv4_connection(si, c, SFE_SYNC_REASON_FLUSH); |
| 1316 | } |
| 1317 | rcu_read_unlock(); |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 1318 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_TCP_SEQ_EXCEEDS_RIGHT_EDGE); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1319 | return 0; |
| 1320 | } |
| 1321 | |
| 1322 | /* |
| 1323 | * Check that our TCP data offset isn't too short. |
| 1324 | */ |
| 1325 | data_offs = tcph->doff << 2; |
Ratheesh Kannoth | 741f799 | 2021-10-20 07:39:52 +0530 | [diff] [blame] | 1326 | if (unlikely(data_offs < sizeof(struct tcphdr))) { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1327 | struct sfe_ipv4_connection *c = cm->connection; |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1328 | spin_lock_bh(&si->lock); |
| 1329 | ret = sfe_ipv4_remove_sfe_ipv4_connection(si, c); |
Xiaoping Fan | 3c423e3 | 2015-07-03 03:09:29 -0700 | [diff] [blame] | 1330 | spin_unlock_bh(&si->lock); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1331 | |
| 1332 | DEBUG_TRACE("TCP data offset: %u, too small\n", data_offs); |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1333 | if (ret) { |
| 1334 | sfe_ipv4_flush_sfe_ipv4_connection(si, c, SFE_SYNC_REASON_FLUSH); |
| 1335 | } |
| 1336 | rcu_read_unlock(); |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 1337 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_TCP_SMALL_DATA_OFFS); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1338 | return 0; |
| 1339 | } |
| 1340 | |
| 1341 | /* |
| 1342 | * Update ACK according to any SACK option. |
| 1343 | */ |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 1344 | ack = ntohl(tcph->ack_seq); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1345 | sack = ack; |
| 1346 | if (unlikely(!sfe_ipv4_process_tcp_option_sack(tcph, data_offs, &sack))) { |
| 1347 | struct sfe_ipv4_connection *c = cm->connection; |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1348 | spin_lock_bh(&si->lock); |
| 1349 | ret = sfe_ipv4_remove_sfe_ipv4_connection(si, c); |
Xiaoping Fan | 3c423e3 | 2015-07-03 03:09:29 -0700 | [diff] [blame] | 1350 | spin_unlock_bh(&si->lock); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1351 | |
| 1352 | DEBUG_TRACE("TCP option SACK size is wrong\n"); |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1353 | if (ret) { |
| 1354 | sfe_ipv4_flush_sfe_ipv4_connection(si, c, SFE_SYNC_REASON_FLUSH); |
| 1355 | } |
| 1356 | rcu_read_unlock(); |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 1357 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_TCP_BAD_SACK); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1358 | return 0; |
| 1359 | } |
| 1360 | |
| 1361 | /* |
| 1362 | * Check that our TCP data offset isn't past the end of the packet. |
| 1363 | */ |
Ratheesh Kannoth | 741f799 | 2021-10-20 07:39:52 +0530 | [diff] [blame] | 1364 | data_offs += sizeof(struct iphdr); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1365 | if (unlikely(len < data_offs)) { |
| 1366 | struct sfe_ipv4_connection *c = cm->connection; |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1367 | spin_lock_bh(&si->lock); |
| 1368 | ret = sfe_ipv4_remove_sfe_ipv4_connection(si, c); |
Xiaoping Fan | 3c423e3 | 2015-07-03 03:09:29 -0700 | [diff] [blame] | 1369 | spin_unlock_bh(&si->lock); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1370 | |
| 1371 | DEBUG_TRACE("TCP data offset: %u, past end of packet: %u\n", |
| 1372 | data_offs, len); |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1373 | if (ret) { |
| 1374 | sfe_ipv4_flush_sfe_ipv4_connection(si, c, SFE_SYNC_REASON_FLUSH); |
| 1375 | } |
| 1376 | rcu_read_unlock(); |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 1377 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_TCP_BIG_DATA_OFFS); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1378 | return 0; |
| 1379 | } |
| 1380 | |
| 1381 | end = seq + len - data_offs; |
| 1382 | |
| 1383 | /* |
| 1384 | * Is our sequence fully before the left hand edge of the window? |
| 1385 | */ |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 1386 | if (unlikely((s32)(end - (cm->protocol_state.tcp.end |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1387 | - counter_cm->protocol_state.tcp.max_win - 1)) < 0)) { |
| 1388 | struct sfe_ipv4_connection *c = cm->connection; |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1389 | spin_lock_bh(&si->lock); |
| 1390 | ret = sfe_ipv4_remove_sfe_ipv4_connection(si, c); |
Xiaoping Fan | 3c423e3 | 2015-07-03 03:09:29 -0700 | [diff] [blame] | 1391 | spin_unlock_bh(&si->lock); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1392 | |
| 1393 | DEBUG_TRACE("seq: %u before left edge: %u\n", |
| 1394 | end, cm->protocol_state.tcp.end - counter_cm->protocol_state.tcp.max_win - 1); |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1395 | if (ret) { |
| 1396 | sfe_ipv4_flush_sfe_ipv4_connection(si, c, SFE_SYNC_REASON_FLUSH); |
| 1397 | } |
| 1398 | rcu_read_unlock(); |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 1399 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_TCP_SEQ_BEFORE_LEFT_EDGE); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1400 | return 0; |
| 1401 | } |
| 1402 | |
| 1403 | /* |
| 1404 | * Are we acking data that is to the right of what has been sent? |
| 1405 | */ |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 1406 | if (unlikely((s32)(sack - (counter_cm->protocol_state.tcp.end + 1)) > 0)) { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1407 | struct sfe_ipv4_connection *c = cm->connection; |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1408 | spin_lock_bh(&si->lock); |
| 1409 | ret = sfe_ipv4_remove_sfe_ipv4_connection(si, c); |
Xiaoping Fan | 3c423e3 | 2015-07-03 03:09:29 -0700 | [diff] [blame] | 1410 | spin_unlock_bh(&si->lock); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1411 | |
| 1412 | DEBUG_TRACE("ack: %u exceeds right edge: %u\n", |
| 1413 | sack, counter_cm->protocol_state.tcp.end + 1); |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1414 | if (ret) { |
| 1415 | sfe_ipv4_flush_sfe_ipv4_connection(si, c, SFE_SYNC_REASON_FLUSH); |
| 1416 | } |
| 1417 | rcu_read_unlock(); |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 1418 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_TCP_ACK_EXCEEDS_RIGHT_EDGE); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1419 | return 0; |
| 1420 | } |
| 1421 | |
| 1422 | /* |
| 1423 | * Is our ack too far before the left hand edge of the window? |
| 1424 | */ |
| 1425 | left_edge = counter_cm->protocol_state.tcp.end |
| 1426 | - cm->protocol_state.tcp.max_win |
| 1427 | - SFE_IPV4_TCP_MAX_ACK_WINDOW |
| 1428 | - 1; |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 1429 | if (unlikely((s32)(sack - left_edge) < 0)) { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1430 | struct sfe_ipv4_connection *c = cm->connection; |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1431 | spin_lock_bh(&si->lock); |
| 1432 | ret = sfe_ipv4_remove_sfe_ipv4_connection(si, c); |
Xiaoping Fan | 3c423e3 | 2015-07-03 03:09:29 -0700 | [diff] [blame] | 1433 | spin_unlock_bh(&si->lock); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1434 | |
| 1435 | DEBUG_TRACE("ack: %u before left edge: %u\n", sack, left_edge); |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1436 | if (ret) { |
| 1437 | sfe_ipv4_flush_sfe_ipv4_connection(si, c, SFE_SYNC_REASON_FLUSH); |
| 1438 | } |
| 1439 | rcu_read_unlock(); |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 1440 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_TCP_ACK_BEFORE_LEFT_EDGE); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1441 | return 0; |
| 1442 | } |
| 1443 | |
| 1444 | /* |
| 1445 | * Have we just seen the largest window size yet for this connection? If yes |
| 1446 | * then we need to record the new value. |
| 1447 | */ |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 1448 | scaled_win = ntohs(tcph->window) << cm->protocol_state.tcp.win_scale; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1449 | scaled_win += (sack - ack); |
| 1450 | if (unlikely(cm->protocol_state.tcp.max_win < scaled_win)) { |
| 1451 | cm->protocol_state.tcp.max_win = scaled_win; |
| 1452 | } |
| 1453 | |
| 1454 | /* |
| 1455 | * If our sequence and/or ack numbers have advanced then record the new state. |
| 1456 | */ |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 1457 | if (likely((s32)(end - cm->protocol_state.tcp.end) >= 0)) { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1458 | cm->protocol_state.tcp.end = end; |
| 1459 | } |
| 1460 | |
| 1461 | max_end = sack + scaled_win; |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 1462 | if (likely((s32)(max_end - counter_cm->protocol_state.tcp.max_end) >= 0)) { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1463 | counter_cm->protocol_state.tcp.max_end = max_end; |
| 1464 | } |
| 1465 | } |
| 1466 | |
| 1467 | /* |
| 1468 | * From this point on we're good to modify the packet. |
| 1469 | */ |
| 1470 | |
| 1471 | /* |
Murat Sezgin | c7dd817 | 2019-02-27 15:23:50 -0800 | [diff] [blame] | 1472 | * Check if skb was cloned. If it was, unshare it. Because |
| 1473 | * the data area is going to be written in this path and we don't want to |
| 1474 | * change the cloned skb's data section. |
| 1475 | */ |
| 1476 | if (unlikely(skb_cloned(skb))) { |
Tian Yang | 45f39c8 | 2020-10-06 14:07:47 -0700 | [diff] [blame] | 1477 | DEBUG_TRACE("%px: skb is a cloned skb\n", skb); |
Murat Sezgin | c7dd817 | 2019-02-27 15:23:50 -0800 | [diff] [blame] | 1478 | skb = skb_unshare(skb, GFP_ATOMIC); |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1479 | if (!skb) { |
Murat Sezgin | c7dd817 | 2019-02-27 15:23:50 -0800 | [diff] [blame] | 1480 | DEBUG_WARN("Failed to unshare the cloned skb\n"); |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1481 | rcu_read_unlock(); |
Murat Sezgin | c7dd817 | 2019-02-27 15:23:50 -0800 | [diff] [blame] | 1482 | return 0; |
| 1483 | } |
| 1484 | |
| 1485 | /* |
| 1486 | * Update the iph and tcph pointers with the unshared skb's data area. |
| 1487 | */ |
Ratheesh Kannoth | 741f799 | 2021-10-20 07:39:52 +0530 | [diff] [blame] | 1488 | iph = (struct iphdr *)skb->data; |
| 1489 | tcph = (struct tcphdr *)(skb->data + ihl); |
Murat Sezgin | c7dd817 | 2019-02-27 15:23:50 -0800 | [diff] [blame] | 1490 | } |
| 1491 | |
| 1492 | /* |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 1493 | * Update DSCP |
| 1494 | */ |
| 1495 | if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_DSCP_REMARK)) { |
| 1496 | iph->tos = (iph->tos & SFE_IPV4_DSCP_MASK) | cm->dscp; |
| 1497 | } |
| 1498 | |
| 1499 | /* |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1500 | * Decrement our TTL. |
| 1501 | */ |
| 1502 | iph->ttl = ttl - 1; |
| 1503 | |
| 1504 | /* |
| 1505 | * Do we have to perform translations of the source address/port? |
| 1506 | */ |
| 1507 | if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC)) { |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 1508 | u16 tcp_csum; |
| 1509 | u32 sum; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1510 | |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 1511 | iph->saddr = cm->xlate_src_ip; |
| 1512 | tcph->source = cm->xlate_src_port; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1513 | |
| 1514 | /* |
| 1515 | * Do we have a non-zero UDP checksum? If we do then we need |
| 1516 | * to update it. |
| 1517 | */ |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 1518 | tcp_csum = tcph->check; |
Xiaoping Fan | ad755af | 2015-04-01 16:58:46 -0700 | [diff] [blame] | 1519 | if (unlikely(skb->ip_summed == CHECKSUM_PARTIAL)) { |
| 1520 | sum = tcp_csum + cm->xlate_src_partial_csum_adjustment; |
| 1521 | } else { |
| 1522 | sum = tcp_csum + cm->xlate_src_csum_adjustment; |
| 1523 | } |
| 1524 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1525 | sum = (sum & 0xffff) + (sum >> 16); |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 1526 | tcph->check = (u16)sum; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1527 | } |
| 1528 | |
| 1529 | /* |
| 1530 | * Do we have to perform translations of the destination address/port? |
| 1531 | */ |
| 1532 | if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST)) { |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 1533 | u16 tcp_csum; |
| 1534 | u32 sum; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1535 | |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 1536 | iph->daddr = cm->xlate_dest_ip; |
| 1537 | tcph->dest = cm->xlate_dest_port; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1538 | |
| 1539 | /* |
| 1540 | * Do we have a non-zero UDP checksum? If we do then we need |
| 1541 | * to update it. |
| 1542 | */ |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 1543 | tcp_csum = tcph->check; |
Xiaoping Fan | ad755af | 2015-04-01 16:58:46 -0700 | [diff] [blame] | 1544 | if (unlikely(skb->ip_summed == CHECKSUM_PARTIAL)) { |
| 1545 | sum = tcp_csum + cm->xlate_dest_partial_csum_adjustment; |
| 1546 | } else { |
| 1547 | sum = tcp_csum + cm->xlate_dest_csum_adjustment; |
| 1548 | } |
| 1549 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1550 | sum = (sum & 0xffff) + (sum >> 16); |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 1551 | tcph->check = (u16)sum; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1552 | } |
| 1553 | |
| 1554 | /* |
| 1555 | * Replace the IP checksum. |
| 1556 | */ |
| 1557 | iph->check = sfe_ipv4_gen_ip_csum(iph); |
| 1558 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1559 | /* |
| 1560 | * Update traffic stats. |
| 1561 | */ |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1562 | atomic_inc(&cm->rx_packet_count); |
| 1563 | atomic_add(len, &cm->rx_byte_count); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1564 | |
| 1565 | /* |
| 1566 | * If we're not already on the active list then insert ourselves at the tail |
| 1567 | * of the current list. |
| 1568 | */ |
| 1569 | if (unlikely(!cm->active)) { |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1570 | spin_lock_bh(&si->lock); |
| 1571 | if (unlikely(!cm->active)) { |
| 1572 | |
| 1573 | cm->active = true; |
| 1574 | cm->active_prev = si->active_tail; |
| 1575 | if (likely(si->active_tail)) { |
| 1576 | si->active_tail->active_next = cm; |
| 1577 | } else { |
| 1578 | si->active_head = cm; |
| 1579 | } |
| 1580 | si->active_tail = cm; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1581 | } |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1582 | spin_unlock_bh(&si->lock); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1583 | } |
| 1584 | |
| 1585 | xmit_dev = cm->xmit_dev; |
| 1586 | skb->dev = xmit_dev; |
| 1587 | |
| 1588 | /* |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 1589 | * Check to see if we need to write a header. |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1590 | */ |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 1591 | if (likely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_L2_HDR)) { |
| 1592 | if (unlikely(!(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR))) { |
Xiaoping Fan | 2784e61 | 2015-06-25 17:57:41 -0700 | [diff] [blame] | 1593 | dev_hard_header(skb, xmit_dev, ETH_P_IP, |
| 1594 | cm->xmit_dest_mac, cm->xmit_src_mac, len); |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 1595 | } else { |
| 1596 | /* |
| 1597 | * For the simple case we write this really fast. |
| 1598 | */ |
Ratheesh Kannoth | 741f799 | 2021-10-20 07:39:52 +0530 | [diff] [blame] | 1599 | struct ethhdr *eth = (struct ethhdr *)__skb_push(skb, ETH_HLEN); |
| 1600 | |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 1601 | eth->h_proto = htons(ETH_P_IP); |
Ratheesh Kannoth | 741f799 | 2021-10-20 07:39:52 +0530 | [diff] [blame] | 1602 | |
| 1603 | ether_addr_copy((u8 *)eth->h_dest, (u8 *)cm->xmit_dest_mac); |
| 1604 | ether_addr_copy((u8 *)eth->h_source, (u8 *)cm->xmit_src_mac); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1605 | } |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1606 | } |
| 1607 | |
Matthew McClintock | be7b47d | 2013-11-27 13:26:23 -0600 | [diff] [blame] | 1608 | /* |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 1609 | * Update priority of skb. |
| 1610 | */ |
| 1611 | if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_PRIORITY_REMARK)) { |
| 1612 | skb->priority = cm->priority; |
| 1613 | } |
| 1614 | |
| 1615 | /* |
Matthew McClintock | be7b47d | 2013-11-27 13:26:23 -0600 | [diff] [blame] | 1616 | * Mark outgoing packet |
| 1617 | */ |
| 1618 | skb->mark = cm->connection->mark; |
| 1619 | if (skb->mark) { |
| 1620 | DEBUG_TRACE("SKB MARK is NON ZERO %x\n", skb->mark); |
| 1621 | } |
| 1622 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1623 | rcu_read_unlock(); |
| 1624 | |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 1625 | this_cpu_inc(si->stats_pcpu->packets_forwarded64); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1626 | |
| 1627 | /* |
| 1628 | * We're going to check for GSO flags when we transmit the packet so |
| 1629 | * start fetching the necessary cache line now. |
| 1630 | */ |
| 1631 | prefetch(skb_shinfo(skb)); |
| 1632 | |
| 1633 | /* |
Nicolas Costa | 9ec8c7b | 2014-01-29 12:50:46 -0600 | [diff] [blame] | 1634 | * Mark that this packet has been fast forwarded. |
| 1635 | */ |
| 1636 | skb->fast_forwarded = 1; |
| 1637 | |
| 1638 | /* |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1639 | * Send the packet on its way. |
| 1640 | */ |
| 1641 | dev_queue_xmit(skb); |
| 1642 | |
| 1643 | return 1; |
| 1644 | } |
| 1645 | |
| 1646 | /* |
| 1647 | * sfe_ipv4_recv_icmp() |
| 1648 | * Handle ICMP packet receives. |
| 1649 | * |
| 1650 | * ICMP packets aren't handled as a "fast path" and always have us process them |
| 1651 | * through the default Linux stack. What we do need to do is look for any errors |
| 1652 | * about connections we are handling in the fast path. If we find any such |
| 1653 | * connections then we want to flush their state so that the ICMP error path |
| 1654 | * within Linux has all of the correct state should it need it. |
| 1655 | */ |
| 1656 | static int sfe_ipv4_recv_icmp(struct sfe_ipv4 *si, struct sk_buff *skb, struct net_device *dev, |
Ratheesh Kannoth | 741f799 | 2021-10-20 07:39:52 +0530 | [diff] [blame] | 1657 | unsigned int len, struct iphdr *iph, unsigned int ihl) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1658 | { |
| 1659 | struct icmphdr *icmph; |
Ratheesh Kannoth | 741f799 | 2021-10-20 07:39:52 +0530 | [diff] [blame] | 1660 | struct iphdr *icmp_iph; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1661 | unsigned int icmp_ihl_words; |
| 1662 | unsigned int icmp_ihl; |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 1663 | u32 *icmp_trans_h; |
Ratheesh Kannoth | 741f799 | 2021-10-20 07:39:52 +0530 | [diff] [blame] | 1664 | struct udphdr *icmp_udph; |
| 1665 | struct tcphdr *icmp_tcph; |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 1666 | __be32 src_ip; |
| 1667 | __be32 dest_ip; |
| 1668 | __be16 src_port; |
| 1669 | __be16 dest_port; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1670 | struct sfe_ipv4_connection_match *cm; |
| 1671 | struct sfe_ipv4_connection *c; |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 1672 | u32 pull_len = sizeof(struct icmphdr) + ihl; |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1673 | bool ret; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1674 | |
| 1675 | /* |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 1676 | * Is our packet too short to contain a valid ICMP header? |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1677 | */ |
| 1678 | len -= ihl; |
Xiaoping Fan | f8260b8 | 2015-04-10 15:17:00 -0700 | [diff] [blame] | 1679 | if (!pskb_may_pull(skb, pull_len)) { |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 1680 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_ICMP_HEADER_INCOMPLETE); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1681 | |
| 1682 | DEBUG_TRACE("packet too short for ICMP header\n"); |
| 1683 | return 0; |
| 1684 | } |
| 1685 | |
| 1686 | /* |
| 1687 | * We only handle "destination unreachable" and "time exceeded" messages. |
| 1688 | */ |
| 1689 | icmph = (struct icmphdr *)(skb->data + ihl); |
| 1690 | if ((icmph->type != ICMP_DEST_UNREACH) |
| 1691 | && (icmph->type != ICMP_TIME_EXCEEDED)) { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1692 | |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 1693 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_ICMP_UNHANDLED_TYPE); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1694 | DEBUG_TRACE("unhandled ICMP type: 0x%x\n", icmph->type); |
| 1695 | return 0; |
| 1696 | } |
| 1697 | |
| 1698 | /* |
| 1699 | * Do we have the full embedded IP header? |
| 1700 | */ |
| 1701 | len -= sizeof(struct icmphdr); |
Ratheesh Kannoth | 741f799 | 2021-10-20 07:39:52 +0530 | [diff] [blame] | 1702 | pull_len += sizeof(struct iphdr); |
Xiaoping Fan | f8260b8 | 2015-04-10 15:17:00 -0700 | [diff] [blame] | 1703 | if (!pskb_may_pull(skb, pull_len)) { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1704 | |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 1705 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_ICMP_IPV4_HEADER_INCOMPLETE); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1706 | DEBUG_TRACE("Embedded IP header not complete\n"); |
| 1707 | return 0; |
| 1708 | } |
| 1709 | |
| 1710 | /* |
| 1711 | * Is our embedded IP version wrong? |
| 1712 | */ |
Ratheesh Kannoth | 741f799 | 2021-10-20 07:39:52 +0530 | [diff] [blame] | 1713 | icmp_iph = (struct iphdr *)(icmph + 1); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1714 | if (unlikely(icmp_iph->version != 4)) { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1715 | |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 1716 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_ICMP_IPV4_NON_V4); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1717 | DEBUG_TRACE("IP version: %u\n", icmp_iph->version); |
| 1718 | return 0; |
| 1719 | } |
| 1720 | |
| 1721 | /* |
| 1722 | * Do we have the full embedded IP header, including any options? |
| 1723 | */ |
| 1724 | icmp_ihl_words = icmp_iph->ihl; |
| 1725 | icmp_ihl = icmp_ihl_words << 2; |
Ratheesh Kannoth | 741f799 | 2021-10-20 07:39:52 +0530 | [diff] [blame] | 1726 | pull_len += icmp_ihl - sizeof(struct iphdr); |
Xiaoping Fan | f8260b8 | 2015-04-10 15:17:00 -0700 | [diff] [blame] | 1727 | if (!pskb_may_pull(skb, pull_len)) { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1728 | |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 1729 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_ICMP_IPV4_IP_OPTIONS_INCOMPLETE); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1730 | DEBUG_TRACE("Embedded header not large enough for IP options\n"); |
| 1731 | return 0; |
| 1732 | } |
| 1733 | |
Nicolas Costa | ac2979c | 2014-01-14 10:35:24 -0600 | [diff] [blame] | 1734 | len -= icmp_ihl; |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 1735 | icmp_trans_h = ((u32 *)icmp_iph) + icmp_ihl_words; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1736 | |
| 1737 | /* |
| 1738 | * Handle the embedded transport layer header. |
| 1739 | */ |
| 1740 | switch (icmp_iph->protocol) { |
| 1741 | case IPPROTO_UDP: |
| 1742 | /* |
| 1743 | * We should have 8 bytes of UDP header - that's enough to identify |
| 1744 | * the connection. |
| 1745 | */ |
Xiaoping Fan | f8260b8 | 2015-04-10 15:17:00 -0700 | [diff] [blame] | 1746 | pull_len += 8; |
| 1747 | if (!pskb_may_pull(skb, pull_len)) { |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 1748 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_ICMP_IPV4_UDP_HEADER_INCOMPLETE); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1749 | DEBUG_TRACE("Incomplete embedded UDP header\n"); |
| 1750 | return 0; |
| 1751 | } |
| 1752 | |
Ratheesh Kannoth | 741f799 | 2021-10-20 07:39:52 +0530 | [diff] [blame] | 1753 | icmp_udph = (struct udphdr *)icmp_trans_h; |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 1754 | src_port = icmp_udph->source; |
| 1755 | dest_port = icmp_udph->dest; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1756 | break; |
| 1757 | |
| 1758 | case IPPROTO_TCP: |
| 1759 | /* |
| 1760 | * We should have 8 bytes of TCP header - that's enough to identify |
| 1761 | * the connection. |
| 1762 | */ |
Xiaoping Fan | f8260b8 | 2015-04-10 15:17:00 -0700 | [diff] [blame] | 1763 | pull_len += 8; |
| 1764 | if (!pskb_may_pull(skb, pull_len)) { |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 1765 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_ICMP_IPV4_TCP_HEADER_INCOMPLETE); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1766 | DEBUG_TRACE("Incomplete embedded TCP header\n"); |
| 1767 | return 0; |
| 1768 | } |
| 1769 | |
Ratheesh Kannoth | 741f799 | 2021-10-20 07:39:52 +0530 | [diff] [blame] | 1770 | icmp_tcph = (struct tcphdr *)icmp_trans_h; |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 1771 | src_port = icmp_tcph->source; |
| 1772 | dest_port = icmp_tcph->dest; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1773 | break; |
| 1774 | |
| 1775 | default: |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 1776 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_ICMP_IPV4_UNHANDLED_PROTOCOL); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1777 | DEBUG_TRACE("Unhandled embedded IP protocol: %u\n", icmp_iph->protocol); |
| 1778 | return 0; |
| 1779 | } |
| 1780 | |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 1781 | src_ip = icmp_iph->saddr; |
| 1782 | dest_ip = icmp_iph->daddr; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1783 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1784 | rcu_read_lock(); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1785 | |
| 1786 | /* |
| 1787 | * Look for a connection match. Note that we reverse the source and destination |
| 1788 | * here because our embedded message contains a packet that was sent in the |
| 1789 | * opposite direction to the one in which we just received it. It will have |
| 1790 | * been sent on the interface from which we received it though so that's still |
| 1791 | * ok to use. |
| 1792 | */ |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1793 | cm = sfe_ipv4_find_sfe_ipv4_connection_match_rcu(si, dev, icmp_iph->protocol, dest_ip, dest_port, src_ip, src_port); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1794 | if (unlikely(!cm)) { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1795 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1796 | rcu_read_unlock(); |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 1797 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_ICMP_NO_CONNECTION); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1798 | DEBUG_TRACE("no connection found\n"); |
| 1799 | return 0; |
| 1800 | } |
| 1801 | |
| 1802 | /* |
| 1803 | * We found a connection so now remove it from the connection list and flush |
| 1804 | * its state. |
| 1805 | */ |
| 1806 | c = cm->connection; |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1807 | spin_lock_bh(&si->lock); |
| 1808 | ret = sfe_ipv4_remove_sfe_ipv4_connection(si, c); |
Xiaoping Fan | 3c423e3 | 2015-07-03 03:09:29 -0700 | [diff] [blame] | 1809 | spin_unlock_bh(&si->lock); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1810 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1811 | if (ret) { |
| 1812 | sfe_ipv4_flush_sfe_ipv4_connection(si, c, SFE_SYNC_REASON_FLUSH); |
| 1813 | } |
| 1814 | rcu_read_unlock(); |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 1815 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_ICMP_FLUSHED_CONNECTION); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1816 | return 0; |
| 1817 | } |
| 1818 | |
| 1819 | /* |
| 1820 | * sfe_ipv4_recv() |
Matthew McClintock | a8ad796 | 2014-01-16 16:49:30 -0600 | [diff] [blame] | 1821 | * Handle packet receives and forwaring. |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1822 | * |
| 1823 | * Returns 1 if the packet is forwarded or 0 if it isn't. |
| 1824 | */ |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 1825 | int sfe_ipv4_recv(struct net_device *dev, struct sk_buff *skb) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1826 | { |
| 1827 | struct sfe_ipv4 *si = &__si; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1828 | unsigned int len; |
| 1829 | unsigned int tot_len; |
| 1830 | unsigned int frag_off; |
| 1831 | unsigned int ihl; |
| 1832 | bool flush_on_find; |
| 1833 | bool ip_options; |
Ratheesh Kannoth | 741f799 | 2021-10-20 07:39:52 +0530 | [diff] [blame] | 1834 | struct iphdr *iph; |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 1835 | u32 protocol; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1836 | |
| 1837 | /* |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1838 | * Check that we have space for an IP header here. |
| 1839 | */ |
| 1840 | len = skb->len; |
Ratheesh Kannoth | 741f799 | 2021-10-20 07:39:52 +0530 | [diff] [blame] | 1841 | if (unlikely(!pskb_may_pull(skb, sizeof(struct iphdr)))) { |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 1842 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_HEADER_INCOMPLETE); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1843 | DEBUG_TRACE("len: %u is too short\n", len); |
| 1844 | return 0; |
| 1845 | } |
| 1846 | |
| 1847 | /* |
| 1848 | * Check that our "total length" is large enough for an IP header. |
| 1849 | */ |
Ratheesh Kannoth | 741f799 | 2021-10-20 07:39:52 +0530 | [diff] [blame] | 1850 | iph = (struct iphdr *)skb->data; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1851 | tot_len = ntohs(iph->tot_len); |
Ratheesh Kannoth | 741f799 | 2021-10-20 07:39:52 +0530 | [diff] [blame] | 1852 | if (unlikely(tot_len < sizeof(struct iphdr))) { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1853 | |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 1854 | 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] | 1855 | DEBUG_TRACE("tot_len: %u is too short\n", tot_len); |
| 1856 | return 0; |
| 1857 | } |
| 1858 | |
| 1859 | /* |
| 1860 | * Is our IP version wrong? |
| 1861 | */ |
| 1862 | if (unlikely(iph->version != 4)) { |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 1863 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_NON_V4); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1864 | DEBUG_TRACE("IP version: %u\n", iph->version); |
| 1865 | return 0; |
| 1866 | } |
| 1867 | |
| 1868 | /* |
| 1869 | * Does our datagram fit inside the skb? |
| 1870 | */ |
| 1871 | if (unlikely(tot_len > len)) { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1872 | |
| 1873 | DEBUG_TRACE("tot_len: %u, exceeds len: %u\n", tot_len, len); |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 1874 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_DATAGRAM_INCOMPLETE); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1875 | return 0; |
| 1876 | } |
| 1877 | |
| 1878 | /* |
| 1879 | * Do we have a non-initial fragment? |
Nicolas Costa | ac2979c | 2014-01-14 10:35:24 -0600 | [diff] [blame] | 1880 | */ |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1881 | frag_off = ntohs(iph->frag_off); |
| 1882 | if (unlikely(frag_off & IP_OFFSET)) { |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 1883 | 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] | 1884 | DEBUG_TRACE("non-initial fragment\n"); |
| 1885 | return 0; |
| 1886 | } |
| 1887 | |
| 1888 | /* |
| 1889 | * If we have a (first) fragment then mark it to cause any connection to flush. |
| 1890 | */ |
| 1891 | flush_on_find = unlikely(frag_off & IP_MF) ? true : false; |
| 1892 | |
| 1893 | /* |
| 1894 | * Do we have any IP options? That's definite a slow path! If we do have IP |
| 1895 | * options we need to recheck our header size. |
| 1896 | */ |
| 1897 | ihl = iph->ihl << 2; |
Ratheesh Kannoth | 741f799 | 2021-10-20 07:39:52 +0530 | [diff] [blame] | 1898 | ip_options = unlikely(ihl != sizeof(struct iphdr)) ? true : false; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1899 | if (unlikely(ip_options)) { |
| 1900 | if (unlikely(len < ihl)) { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1901 | |
| 1902 | 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^] | 1903 | 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] | 1904 | return 0; |
| 1905 | } |
| 1906 | |
| 1907 | flush_on_find = true; |
| 1908 | } |
| 1909 | |
| 1910 | protocol = iph->protocol; |
| 1911 | if (IPPROTO_UDP == protocol) { |
| 1912 | return sfe_ipv4_recv_udp(si, skb, dev, len, iph, ihl, flush_on_find); |
| 1913 | } |
| 1914 | |
| 1915 | if (IPPROTO_TCP == protocol) { |
| 1916 | return sfe_ipv4_recv_tcp(si, skb, dev, len, iph, ihl, flush_on_find); |
| 1917 | } |
| 1918 | |
| 1919 | if (IPPROTO_ICMP == protocol) { |
| 1920 | return sfe_ipv4_recv_icmp(si, skb, dev, len, iph, ihl); |
| 1921 | } |
| 1922 | |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 1923 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_UNHANDLED_PROTOCOL); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1924 | |
| 1925 | DEBUG_TRACE("not UDP, TCP or ICMP: %u\n", protocol); |
| 1926 | return 0; |
| 1927 | } |
| 1928 | |
Nicolas Costa | 436926b | 2014-01-14 10:36:22 -0600 | [diff] [blame] | 1929 | static void |
| 1930 | sfe_ipv4_update_tcp_state(struct sfe_ipv4_connection *c, |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 1931 | struct sfe_connection_create *sic) |
Nicolas Costa | 436926b | 2014-01-14 10:36:22 -0600 | [diff] [blame] | 1932 | { |
| 1933 | struct sfe_ipv4_connection_match *orig_cm; |
| 1934 | struct sfe_ipv4_connection_match *repl_cm; |
| 1935 | struct sfe_ipv4_tcp_connection_match *orig_tcp; |
| 1936 | struct sfe_ipv4_tcp_connection_match *repl_tcp; |
| 1937 | |
| 1938 | orig_cm = c->original_match; |
| 1939 | repl_cm = c->reply_match; |
| 1940 | orig_tcp = &orig_cm->protocol_state.tcp; |
| 1941 | repl_tcp = &repl_cm->protocol_state.tcp; |
| 1942 | |
| 1943 | /* update orig */ |
| 1944 | if (orig_tcp->max_win < sic->src_td_max_window) { |
| 1945 | orig_tcp->max_win = sic->src_td_max_window; |
| 1946 | } |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 1947 | if ((s32)(orig_tcp->end - sic->src_td_end) < 0) { |
Nicolas Costa | 436926b | 2014-01-14 10:36:22 -0600 | [diff] [blame] | 1948 | orig_tcp->end = sic->src_td_end; |
| 1949 | } |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 1950 | if ((s32)(orig_tcp->max_end - sic->src_td_max_end) < 0) { |
Nicolas Costa | 436926b | 2014-01-14 10:36:22 -0600 | [diff] [blame] | 1951 | orig_tcp->max_end = sic->src_td_max_end; |
| 1952 | } |
| 1953 | |
| 1954 | /* update reply */ |
| 1955 | if (repl_tcp->max_win < sic->dest_td_max_window) { |
| 1956 | repl_tcp->max_win = sic->dest_td_max_window; |
| 1957 | } |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 1958 | if ((s32)(repl_tcp->end - sic->dest_td_end) < 0) { |
Nicolas Costa | 436926b | 2014-01-14 10:36:22 -0600 | [diff] [blame] | 1959 | repl_tcp->end = sic->dest_td_end; |
| 1960 | } |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 1961 | if ((s32)(repl_tcp->max_end - sic->dest_td_max_end) < 0) { |
Nicolas Costa | 436926b | 2014-01-14 10:36:22 -0600 | [diff] [blame] | 1962 | repl_tcp->max_end = sic->dest_td_max_end; |
| 1963 | } |
| 1964 | |
| 1965 | /* update match flags */ |
| 1966 | orig_cm->flags &= ~SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK; |
| 1967 | repl_cm->flags &= ~SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK; |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 1968 | if (sic->flags & SFE_CREATE_FLAG_NO_SEQ_CHECK) { |
Nicolas Costa | 436926b | 2014-01-14 10:36:22 -0600 | [diff] [blame] | 1969 | orig_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK; |
| 1970 | repl_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK; |
| 1971 | } |
| 1972 | } |
| 1973 | |
| 1974 | static void |
| 1975 | sfe_ipv4_update_protocol_state(struct sfe_ipv4_connection *c, |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 1976 | struct sfe_connection_create *sic) |
Nicolas Costa | 436926b | 2014-01-14 10:36:22 -0600 | [diff] [blame] | 1977 | { |
| 1978 | switch (sic->protocol) { |
| 1979 | case IPPROTO_TCP: |
| 1980 | sfe_ipv4_update_tcp_state(c, sic); |
| 1981 | break; |
| 1982 | } |
| 1983 | } |
| 1984 | |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 1985 | void sfe_ipv4_update_rule(struct sfe_connection_create *sic) |
Nicolas Costa | 436926b | 2014-01-14 10:36:22 -0600 | [diff] [blame] | 1986 | { |
| 1987 | struct sfe_ipv4_connection *c; |
| 1988 | struct sfe_ipv4 *si = &__si; |
| 1989 | |
| 1990 | spin_lock_bh(&si->lock); |
| 1991 | |
| 1992 | c = sfe_ipv4_find_sfe_ipv4_connection(si, |
| 1993 | sic->protocol, |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 1994 | sic->src_ip.ip, |
Nicolas Costa | 436926b | 2014-01-14 10:36:22 -0600 | [diff] [blame] | 1995 | sic->src_port, |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 1996 | sic->dest_ip.ip, |
Nicolas Costa | 436926b | 2014-01-14 10:36:22 -0600 | [diff] [blame] | 1997 | sic->dest_port); |
| 1998 | if (c != NULL) { |
| 1999 | sfe_ipv4_update_protocol_state(c, sic); |
| 2000 | } |
| 2001 | |
| 2002 | spin_unlock_bh(&si->lock); |
| 2003 | } |
| 2004 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2005 | /* |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2006 | * sfe_ipv4_create_rule() |
| 2007 | * Create a forwarding rule. |
| 2008 | */ |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 2009 | int sfe_ipv4_create_rule(struct sfe_connection_create *sic) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2010 | { |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 2011 | struct sfe_ipv4 *si = &__si; |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 2012 | struct sfe_ipv4_connection *c, *c_old; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2013 | struct sfe_ipv4_connection_match *original_cm; |
| 2014 | struct sfe_ipv4_connection_match *reply_cm; |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 2015 | struct net_device *dest_dev; |
| 2016 | struct net_device *src_dev; |
| 2017 | |
| 2018 | dest_dev = sic->dest_dev; |
| 2019 | src_dev = sic->src_dev; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2020 | |
Matthew McClintock | 389b42a | 2014-09-24 14:05:51 -0500 | [diff] [blame] | 2021 | if (unlikely((dest_dev->reg_state != NETREG_REGISTERED) || |
| 2022 | (src_dev->reg_state != NETREG_REGISTERED))) { |
| 2023 | return -EINVAL; |
| 2024 | } |
| 2025 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 2026 | /* |
| 2027 | * Allocate the various connection tracking objects. |
| 2028 | */ |
| 2029 | c = (struct sfe_ipv4_connection *)kmalloc(sizeof(struct sfe_ipv4_connection), GFP_ATOMIC); |
| 2030 | if (unlikely(!c)) { |
| 2031 | return -ENOMEM; |
| 2032 | } |
| 2033 | |
| 2034 | original_cm = (struct sfe_ipv4_connection_match *)kmalloc(sizeof(struct sfe_ipv4_connection_match), GFP_ATOMIC); |
| 2035 | if (unlikely(!original_cm)) { |
| 2036 | kfree(c); |
| 2037 | return -ENOMEM; |
| 2038 | } |
| 2039 | |
| 2040 | reply_cm = (struct sfe_ipv4_connection_match *)kmalloc(sizeof(struct sfe_ipv4_connection_match), GFP_ATOMIC); |
| 2041 | if (unlikely(!reply_cm)) { |
| 2042 | kfree(original_cm); |
| 2043 | kfree(c); |
| 2044 | return -ENOMEM; |
| 2045 | } |
| 2046 | |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 2047 | this_cpu_inc(si->stats_pcpu->connection_create_requests64); |
| 2048 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2049 | spin_lock_bh(&si->lock); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2050 | |
| 2051 | /* |
Nicolas Costa | 436926b | 2014-01-14 10:36:22 -0600 | [diff] [blame] | 2052 | * Check to see if there is already a flow that matches the rule we're |
| 2053 | * 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] | 2054 | */ |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 2055 | c_old = sfe_ipv4_find_sfe_ipv4_connection(si, |
Nicolas Costa | 436926b | 2014-01-14 10:36:22 -0600 | [diff] [blame] | 2056 | sic->protocol, |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 2057 | sic->src_ip.ip, |
Nicolas Costa | 436926b | 2014-01-14 10:36:22 -0600 | [diff] [blame] | 2058 | sic->src_port, |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 2059 | sic->dest_ip.ip, |
Nicolas Costa | 436926b | 2014-01-14 10:36:22 -0600 | [diff] [blame] | 2060 | sic->dest_port); |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 2061 | if (c_old != NULL) { |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 2062 | this_cpu_inc(si->stats_pcpu->connection_create_collisions64); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2063 | |
| 2064 | /* |
Nicolas Costa | 436926b | 2014-01-14 10:36:22 -0600 | [diff] [blame] | 2065 | * If we already have the flow then it's likely that this |
| 2066 | * request to create the connection rule contains more |
| 2067 | * up-to-date information. Check and update accordingly. |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2068 | */ |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 2069 | sfe_ipv4_update_protocol_state(c_old, sic); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2070 | spin_unlock_bh(&si->lock); |
| 2071 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 2072 | kfree(reply_cm); |
| 2073 | kfree(original_cm); |
| 2074 | kfree(c); |
| 2075 | |
Nicolas Costa | f53d6fe | 2014-01-13 16:03:46 -0600 | [diff] [blame] | 2076 | DEBUG_TRACE("connection already exists - mark: %08x, p: %d\n" |
Tian Yang | 45f39c8 | 2020-10-06 14:07:47 -0700 | [diff] [blame] | 2077 | " s: %s:%pxM:%pI4:%u, d: %s:%pxM:%pI4:%u\n", |
Nicolas Costa | f53d6fe | 2014-01-13 16:03:46 -0600 | [diff] [blame] | 2078 | sic->mark, sic->protocol, |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 2079 | sic->src_dev->name, sic->src_mac, &sic->src_ip.ip, ntohs(sic->src_port), |
| 2080 | sic->dest_dev->name, sic->dest_mac, &sic->dest_ip.ip, ntohs(sic->dest_port)); |
Nicolas Costa | 514fde0 | 2014-01-13 15:50:29 -0600 | [diff] [blame] | 2081 | return -EADDRINUSE; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2082 | } |
| 2083 | |
| 2084 | /* |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2085 | * Fill in the "original" direction connection matching object. |
| 2086 | * Note that the transmit MAC address is "dest_mac_xlate" because |
| 2087 | * we always know both ends of a connection by their translated |
| 2088 | * addresses and not their public addresses. |
| 2089 | */ |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 2090 | original_cm->match_dev = src_dev; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2091 | original_cm->match_protocol = sic->protocol; |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 2092 | original_cm->match_src_ip = sic->src_ip.ip; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2093 | original_cm->match_src_port = sic->src_port; |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 2094 | original_cm->match_dest_ip = sic->dest_ip.ip; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2095 | original_cm->match_dest_port = sic->dest_port; |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 2096 | original_cm->xlate_src_ip = sic->src_ip_xlate.ip; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2097 | original_cm->xlate_src_port = sic->src_port_xlate; |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 2098 | original_cm->xlate_dest_ip = sic->dest_ip_xlate.ip; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2099 | original_cm->xlate_dest_port = sic->dest_port_xlate; |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 2100 | atomic_set(&original_cm->rx_packet_count, 0); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2101 | original_cm->rx_packet_count64 = 0; |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 2102 | atomic_set(&original_cm->rx_byte_count, 0); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2103 | original_cm->rx_byte_count64 = 0; |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 2104 | original_cm->xmit_dev = dest_dev; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2105 | original_cm->xmit_dev_mtu = sic->dest_mtu; |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 2106 | memcpy(original_cm->xmit_src_mac, dest_dev->dev_addr, ETH_ALEN); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2107 | memcpy(original_cm->xmit_dest_mac, sic->dest_mac_xlate, ETH_ALEN); |
| 2108 | original_cm->connection = c; |
| 2109 | original_cm->counter_match = reply_cm; |
| 2110 | original_cm->flags = 0; |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 2111 | if (sic->flags & SFE_CREATE_FLAG_REMARK_PRIORITY) { |
| 2112 | original_cm->priority = sic->src_priority; |
| 2113 | original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PRIORITY_REMARK; |
| 2114 | } |
| 2115 | if (sic->flags & SFE_CREATE_FLAG_REMARK_DSCP) { |
| 2116 | original_cm->dscp = sic->src_dscp << SFE_IPV4_DSCP_SHIFT; |
| 2117 | original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_DSCP_REMARK; |
| 2118 | } |
Xiaoping Fan | d1dc7b2 | 2015-01-23 00:43:56 -0800 | [diff] [blame] | 2119 | #ifdef CONFIG_NF_FLOW_COOKIE |
| 2120 | original_cm->flow_cookie = 0; |
| 2121 | #endif |
Zhi Chen | 8748eb3 | 2015-06-18 12:58:48 -0700 | [diff] [blame] | 2122 | #ifdef CONFIG_XFRM |
| 2123 | original_cm->flow_accel = sic->original_accel; |
| 2124 | #endif |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2125 | original_cm->active_next = NULL; |
| 2126 | original_cm->active_prev = NULL; |
| 2127 | original_cm->active = false; |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 2128 | |
| 2129 | /* |
| 2130 | * For PPP links we don't write an L2 header. For everything else we do. |
| 2131 | */ |
| 2132 | if (!(dest_dev->flags & IFF_POINTOPOINT)) { |
| 2133 | original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_L2_HDR; |
| 2134 | |
| 2135 | /* |
| 2136 | * If our dev writes Ethernet headers then we can write a really fast |
| 2137 | * version. |
| 2138 | */ |
| 2139 | if (dest_dev->header_ops) { |
| 2140 | if (dest_dev->header_ops->create == eth_header) { |
| 2141 | original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR; |
| 2142 | } |
| 2143 | } |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2144 | } |
| 2145 | |
| 2146 | /* |
| 2147 | * Fill in the "reply" direction connection matching object. |
| 2148 | */ |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 2149 | reply_cm->match_dev = dest_dev; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2150 | reply_cm->match_protocol = sic->protocol; |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 2151 | reply_cm->match_src_ip = sic->dest_ip_xlate.ip; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2152 | reply_cm->match_src_port = sic->dest_port_xlate; |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 2153 | reply_cm->match_dest_ip = sic->src_ip_xlate.ip; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2154 | reply_cm->match_dest_port = sic->src_port_xlate; |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 2155 | reply_cm->xlate_src_ip = sic->dest_ip.ip; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2156 | reply_cm->xlate_src_port = sic->dest_port; |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 2157 | reply_cm->xlate_dest_ip = sic->src_ip.ip; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2158 | reply_cm->xlate_dest_port = sic->src_port; |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 2159 | |
| 2160 | atomic_set(&reply_cm->rx_packet_count, 0); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2161 | reply_cm->rx_packet_count64 = 0; |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 2162 | atomic_set(&reply_cm->rx_byte_count, 0); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2163 | reply_cm->rx_byte_count64 = 0; |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 2164 | |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 2165 | reply_cm->xmit_dev = src_dev; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2166 | reply_cm->xmit_dev_mtu = sic->src_mtu; |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 2167 | memcpy(reply_cm->xmit_src_mac, src_dev->dev_addr, ETH_ALEN); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2168 | memcpy(reply_cm->xmit_dest_mac, sic->src_mac, ETH_ALEN); |
| 2169 | reply_cm->connection = c; |
| 2170 | reply_cm->counter_match = original_cm; |
| 2171 | reply_cm->flags = 0; |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 2172 | if (sic->flags & SFE_CREATE_FLAG_REMARK_PRIORITY) { |
| 2173 | reply_cm->priority = sic->dest_priority; |
| 2174 | reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PRIORITY_REMARK; |
| 2175 | } |
| 2176 | if (sic->flags & SFE_CREATE_FLAG_REMARK_DSCP) { |
| 2177 | reply_cm->dscp = sic->dest_dscp << SFE_IPV4_DSCP_SHIFT; |
| 2178 | reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_DSCP_REMARK; |
| 2179 | } |
Xiaoping Fan | d1dc7b2 | 2015-01-23 00:43:56 -0800 | [diff] [blame] | 2180 | #ifdef CONFIG_NF_FLOW_COOKIE |
| 2181 | reply_cm->flow_cookie = 0; |
| 2182 | #endif |
Zhi Chen | 8748eb3 | 2015-06-18 12:58:48 -0700 | [diff] [blame] | 2183 | #ifdef CONFIG_XFRM |
| 2184 | reply_cm->flow_accel = sic->reply_accel; |
| 2185 | #endif |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2186 | reply_cm->active_next = NULL; |
| 2187 | reply_cm->active_prev = NULL; |
| 2188 | reply_cm->active = false; |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 2189 | |
| 2190 | /* |
| 2191 | * For PPP links we don't write an L2 header. For everything else we do. |
| 2192 | */ |
| 2193 | if (!(src_dev->flags & IFF_POINTOPOINT)) { |
| 2194 | reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_L2_HDR; |
| 2195 | |
| 2196 | /* |
| 2197 | * If our dev writes Ethernet headers then we can write a really fast |
| 2198 | * version. |
| 2199 | */ |
| 2200 | if (src_dev->header_ops) { |
| 2201 | if (src_dev->header_ops->create == eth_header) { |
| 2202 | reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR; |
| 2203 | } |
| 2204 | } |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2205 | } |
| 2206 | |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 2207 | if (sic->dest_ip.ip != sic->dest_ip_xlate.ip || sic->dest_port != sic->dest_port_xlate) { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2208 | original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST; |
| 2209 | reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC; |
| 2210 | } |
| 2211 | |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 2212 | if (sic->src_ip.ip != sic->src_ip_xlate.ip || sic->src_port != sic->src_port_xlate) { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2213 | original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC; |
| 2214 | reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST; |
| 2215 | } |
| 2216 | |
| 2217 | c->protocol = sic->protocol; |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 2218 | c->src_ip = sic->src_ip.ip; |
| 2219 | c->src_ip_xlate = sic->src_ip_xlate.ip; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2220 | c->src_port = sic->src_port; |
| 2221 | c->src_port_xlate = sic->src_port_xlate; |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 2222 | c->original_dev = src_dev; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2223 | c->original_match = original_cm; |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 2224 | c->dest_ip = sic->dest_ip.ip; |
| 2225 | c->dest_ip_xlate = sic->dest_ip_xlate.ip; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2226 | c->dest_port = sic->dest_port; |
| 2227 | c->dest_port_xlate = sic->dest_port_xlate; |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 2228 | c->reply_dev = dest_dev; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2229 | c->reply_match = reply_cm; |
Matthew McClintock | be7b47d | 2013-11-27 13:26:23 -0600 | [diff] [blame] | 2230 | c->mark = sic->mark; |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 2231 | c->debug_read_seq = 0; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2232 | c->last_sync_jiffies = get_jiffies_64(); |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 2233 | c->removed = false; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2234 | |
| 2235 | /* |
| 2236 | * Take hold of our source and dest devices for the duration of the connection. |
| 2237 | */ |
| 2238 | dev_hold(c->original_dev); |
| 2239 | dev_hold(c->reply_dev); |
| 2240 | |
| 2241 | /* |
| 2242 | * Initialize the protocol-specific information that we track. |
| 2243 | */ |
| 2244 | switch (sic->protocol) { |
| 2245 | case IPPROTO_TCP: |
| 2246 | original_cm->protocol_state.tcp.win_scale = sic->src_td_window_scale; |
| 2247 | original_cm->protocol_state.tcp.max_win = sic->src_td_max_window ? sic->src_td_max_window : 1; |
| 2248 | original_cm->protocol_state.tcp.end = sic->src_td_end; |
| 2249 | original_cm->protocol_state.tcp.max_end = sic->src_td_max_end; |
| 2250 | reply_cm->protocol_state.tcp.win_scale = sic->dest_td_window_scale; |
| 2251 | reply_cm->protocol_state.tcp.max_win = sic->dest_td_max_window ? sic->dest_td_max_window : 1; |
| 2252 | reply_cm->protocol_state.tcp.end = sic->dest_td_end; |
| 2253 | reply_cm->protocol_state.tcp.max_end = sic->dest_td_max_end; |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 2254 | if (sic->flags & SFE_CREATE_FLAG_NO_SEQ_CHECK) { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2255 | original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK; |
| 2256 | reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK; |
| 2257 | } |
| 2258 | break; |
| 2259 | } |
| 2260 | |
| 2261 | sfe_ipv4_connection_match_compute_translations(original_cm); |
| 2262 | sfe_ipv4_connection_match_compute_translations(reply_cm); |
| 2263 | sfe_ipv4_insert_sfe_ipv4_connection(si, c); |
| 2264 | |
| 2265 | spin_unlock_bh(&si->lock); |
| 2266 | |
| 2267 | /* |
| 2268 | * We have everything we need! |
| 2269 | */ |
Nicolas Costa | f53d6fe | 2014-01-13 16:03:46 -0600 | [diff] [blame] | 2270 | DEBUG_INFO("new connection - mark: %08x, p: %d\n" |
Tian Yang | 45f39c8 | 2020-10-06 14:07:47 -0700 | [diff] [blame] | 2271 | " s: %s:%pxM(%pxM):%pI4(%pI4):%u(%u)\n" |
| 2272 | " d: %s:%pxM(%pxM):%pI4(%pI4):%u(%u)\n", |
Nicolas Costa | f53d6fe | 2014-01-13 16:03:46 -0600 | [diff] [blame] | 2273 | sic->mark, sic->protocol, |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2274 | sic->src_dev->name, sic->src_mac, sic->src_mac_xlate, |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 2275 | &sic->src_ip.ip, &sic->src_ip_xlate.ip, ntohs(sic->src_port), ntohs(sic->src_port_xlate), |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 2276 | dest_dev->name, sic->dest_mac, sic->dest_mac_xlate, |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 2277 | &sic->dest_ip.ip, &sic->dest_ip_xlate.ip, ntohs(sic->dest_port), ntohs(sic->dest_port_xlate)); |
Nicolas Costa | 514fde0 | 2014-01-13 15:50:29 -0600 | [diff] [blame] | 2278 | |
| 2279 | return 0; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2280 | } |
| 2281 | |
| 2282 | /* |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2283 | * sfe_ipv4_destroy_rule() |
| 2284 | * Destroy a forwarding rule. |
| 2285 | */ |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 2286 | void sfe_ipv4_destroy_rule(struct sfe_connection_destroy *sid) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2287 | { |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 2288 | struct sfe_ipv4 *si = &__si; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2289 | struct sfe_ipv4_connection *c; |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 2290 | bool ret; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2291 | |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 2292 | this_cpu_inc(si->stats_pcpu->connection_destroy_requests64); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2293 | spin_lock_bh(&si->lock); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2294 | |
| 2295 | /* |
| 2296 | * Check to see if we have a flow that matches the rule we're trying |
| 2297 | * to destroy. If there isn't then we can't destroy it. |
| 2298 | */ |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 2299 | c = sfe_ipv4_find_sfe_ipv4_connection(si, sid->protocol, sid->src_ip.ip, sid->src_port, |
| 2300 | sid->dest_ip.ip, sid->dest_port); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2301 | if (!c) { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2302 | spin_unlock_bh(&si->lock); |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 2303 | this_cpu_inc(si->stats_pcpu->connection_destroy_misses64); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2304 | |
| 2305 | DEBUG_TRACE("connection does not exist - p: %d, s: %pI4:%u, d: %pI4:%u\n", |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 2306 | sid->protocol, &sid->src_ip, ntohs(sid->src_port), |
| 2307 | &sid->dest_ip, ntohs(sid->dest_port)); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2308 | return; |
| 2309 | } |
| 2310 | |
| 2311 | /* |
| 2312 | * Remove our connection details from the hash tables. |
| 2313 | */ |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 2314 | ret = sfe_ipv4_remove_sfe_ipv4_connection(si, c); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2315 | spin_unlock_bh(&si->lock); |
| 2316 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 2317 | if (ret) { |
| 2318 | sfe_ipv4_flush_sfe_ipv4_connection(si, c, SFE_SYNC_REASON_DESTROY); |
| 2319 | } |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2320 | |
| 2321 | DEBUG_INFO("connection destroyed - p: %d, s: %pI4:%u, d: %pI4:%u\n", |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 2322 | sid->protocol, &sid->src_ip.ip, ntohs(sid->src_port), |
| 2323 | &sid->dest_ip.ip, ntohs(sid->dest_port)); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2324 | } |
| 2325 | |
| 2326 | /* |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 2327 | * sfe_ipv4_register_sync_rule_callback() |
| 2328 | * Register a callback for rule synchronization. |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2329 | */ |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 2330 | 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] | 2331 | { |
| 2332 | struct sfe_ipv4 *si = &__si; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2333 | |
| 2334 | spin_lock_bh(&si->lock); |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 2335 | rcu_assign_pointer(si->sync_rule_callback, sync_rule_callback); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2336 | spin_unlock_bh(&si->lock); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2337 | } |
| 2338 | |
| 2339 | /* |
| 2340 | * sfe_ipv4_get_debug_dev() |
| 2341 | */ |
| 2342 | static ssize_t sfe_ipv4_get_debug_dev(struct device *dev, |
| 2343 | struct device_attribute *attr, |
| 2344 | char *buf) |
| 2345 | { |
| 2346 | struct sfe_ipv4 *si = &__si; |
| 2347 | ssize_t count; |
| 2348 | int num; |
| 2349 | |
| 2350 | spin_lock_bh(&si->lock); |
| 2351 | num = si->debug_dev; |
| 2352 | spin_unlock_bh(&si->lock); |
| 2353 | |
| 2354 | count = snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", num); |
| 2355 | return count; |
| 2356 | } |
| 2357 | |
| 2358 | /* |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 2359 | * sysfs attributes. |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2360 | */ |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2361 | static const struct device_attribute sfe_ipv4_debug_dev_attr = |
Xiaoping Fan | e70da41 | 2016-02-26 16:47:57 -0800 | [diff] [blame] | 2362 | __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] | 2363 | |
| 2364 | /* |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 2365 | * sfe_ipv4_destroy_all_rules_for_dev() |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2366 | * Destroy all connections that match a particular device. |
| 2367 | * |
| 2368 | * If we pass dev as NULL then this destroys all connections. |
| 2369 | */ |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 2370 | void sfe_ipv4_destroy_all_rules_for_dev(struct net_device *dev) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2371 | { |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 2372 | struct sfe_ipv4 *si = &__si; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2373 | struct sfe_ipv4_connection *c; |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 2374 | bool ret; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2375 | |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 2376 | another_round: |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2377 | spin_lock_bh(&si->lock); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2378 | |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 2379 | for (c = si->all_connections_head; c; c = c->all_connections_next) { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2380 | /* |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 2381 | * Does this connection relate to the device we are destroying? |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2382 | */ |
| 2383 | if (!dev |
| 2384 | || (dev == c->original_dev) |
| 2385 | || (dev == c->reply_dev)) { |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 2386 | break; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2387 | } |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 2388 | } |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2389 | |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 2390 | if (c) { |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 2391 | ret = sfe_ipv4_remove_sfe_ipv4_connection(si, c); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2392 | } |
| 2393 | |
| 2394 | spin_unlock_bh(&si->lock); |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 2395 | |
| 2396 | if (c) { |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 2397 | if (ret) { |
| 2398 | sfe_ipv4_flush_sfe_ipv4_connection(si, c, SFE_SYNC_REASON_DESTROY); |
| 2399 | } |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 2400 | goto another_round; |
| 2401 | } |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2402 | } |
| 2403 | |
| 2404 | /* |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2405 | * sfe_ipv4_periodic_sync() |
| 2406 | */ |
Tian Yang | 45f39c8 | 2020-10-06 14:07:47 -0700 | [diff] [blame] | 2407 | #if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 15, 0)) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2408 | static void sfe_ipv4_periodic_sync(unsigned long arg) |
Tian Yang | 45f39c8 | 2020-10-06 14:07:47 -0700 | [diff] [blame] | 2409 | #else |
| 2410 | static void sfe_ipv4_periodic_sync(struct timer_list *tl) |
| 2411 | #endif |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2412 | { |
Tian Yang | 45f39c8 | 2020-10-06 14:07:47 -0700 | [diff] [blame] | 2413 | #if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 15, 0)) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2414 | struct sfe_ipv4 *si = (struct sfe_ipv4 *)arg; |
Tian Yang | 45f39c8 | 2020-10-06 14:07:47 -0700 | [diff] [blame] | 2415 | #else |
| 2416 | struct sfe_ipv4 *si = from_timer(si, tl, timer); |
| 2417 | #endif |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 2418 | u64 now_jiffies; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2419 | int quota; |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 2420 | sfe_sync_rule_callback_t sync_rule_callback; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2421 | |
| 2422 | now_jiffies = get_jiffies_64(); |
| 2423 | |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 2424 | rcu_read_lock(); |
| 2425 | sync_rule_callback = rcu_dereference(si->sync_rule_callback); |
| 2426 | if (!sync_rule_callback) { |
| 2427 | rcu_read_unlock(); |
| 2428 | goto done; |
| 2429 | } |
| 2430 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2431 | spin_lock_bh(&si->lock); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2432 | |
| 2433 | /* |
| 2434 | * Get an estimate of the number of connections to parse in this sync. |
| 2435 | */ |
| 2436 | quota = (si->num_connections + 63) / 64; |
| 2437 | |
| 2438 | /* |
| 2439 | * Walk the "active" list and sync the connection state. |
| 2440 | */ |
| 2441 | while (quota--) { |
| 2442 | struct sfe_ipv4_connection_match *cm; |
| 2443 | struct sfe_ipv4_connection_match *counter_cm; |
| 2444 | struct sfe_ipv4_connection *c; |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 2445 | struct sfe_connection_sync sis; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2446 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2447 | cm = si->active_head; |
| 2448 | if (!cm) { |
| 2449 | break; |
| 2450 | } |
| 2451 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2452 | /* |
Nicolas Costa | bafb3af | 2014-01-29 16:39:39 -0600 | [diff] [blame] | 2453 | * There's a possibility that our counter match is in the active list too. |
Matthew McClintock | af48f1e | 2014-01-23 15:29:19 -0600 | [diff] [blame] | 2454 | * If it is then remove it. |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2455 | */ |
| 2456 | counter_cm = cm->counter_match; |
| 2457 | if (counter_cm->active) { |
| 2458 | counter_cm->active = false; |
| 2459 | |
Matthew McClintock | af48f1e | 2014-01-23 15:29:19 -0600 | [diff] [blame] | 2460 | /* |
| 2461 | * We must have a connection preceding this counter match |
| 2462 | * because that's the one that got us to this point, so we don't have |
| 2463 | * to worry about removing the head of the list. |
| 2464 | */ |
| 2465 | counter_cm->active_prev->active_next = counter_cm->active_next; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2466 | |
| 2467 | if (likely(counter_cm->active_next)) { |
| 2468 | counter_cm->active_next->active_prev = counter_cm->active_prev; |
| 2469 | } else { |
| 2470 | si->active_tail = counter_cm->active_prev; |
| 2471 | } |
| 2472 | |
| 2473 | counter_cm->active_next = NULL; |
| 2474 | counter_cm->active_prev = NULL; |
| 2475 | } |
| 2476 | |
| 2477 | /* |
Matthew McClintock | af48f1e | 2014-01-23 15:29:19 -0600 | [diff] [blame] | 2478 | * Now remove the head of the active scan list. |
| 2479 | */ |
| 2480 | cm->active = false; |
| 2481 | si->active_head = cm->active_next; |
| 2482 | if (likely(cm->active_next)) { |
| 2483 | cm->active_next->active_prev = NULL; |
| 2484 | } else { |
| 2485 | si->active_tail = NULL; |
| 2486 | } |
| 2487 | cm->active_next = NULL; |
| 2488 | |
| 2489 | /* |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2490 | * Sync the connection state. |
| 2491 | */ |
| 2492 | c = cm->connection; |
Xiaoping Fan | 99cb4c1 | 2015-08-21 19:07:32 -0700 | [diff] [blame] | 2493 | sfe_ipv4_gen_sync_sfe_ipv4_connection(si, c, &sis, SFE_SYNC_REASON_STATS, now_jiffies); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2494 | |
| 2495 | /* |
| 2496 | * We don't want to be holding the lock when we sync! |
| 2497 | */ |
| 2498 | spin_unlock_bh(&si->lock); |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 2499 | sync_rule_callback(&sis); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2500 | spin_lock_bh(&si->lock); |
| 2501 | } |
| 2502 | |
| 2503 | spin_unlock_bh(&si->lock); |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 2504 | rcu_read_unlock(); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2505 | |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 2506 | done: |
Matthew McClintock | af48f1e | 2014-01-23 15:29:19 -0600 | [diff] [blame] | 2507 | mod_timer(&si->timer, jiffies + ((HZ + 99) / 100)); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2508 | } |
| 2509 | |
| 2510 | #define CHAR_DEV_MSG_SIZE 768 |
| 2511 | |
| 2512 | /* |
| 2513 | * sfe_ipv4_debug_dev_read_start() |
| 2514 | * Generate part of the XML output. |
| 2515 | */ |
| 2516 | static bool sfe_ipv4_debug_dev_read_start(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length, |
| 2517 | int *total_read, struct sfe_ipv4_debug_xml_write_state *ws) |
| 2518 | { |
| 2519 | int bytes_read; |
| 2520 | |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 2521 | si->debug_read_seq++; |
| 2522 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2523 | bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "<sfe_ipv4>\n"); |
| 2524 | if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) { |
| 2525 | return false; |
| 2526 | } |
| 2527 | |
| 2528 | *length -= bytes_read; |
| 2529 | *total_read += bytes_read; |
| 2530 | |
| 2531 | ws->state++; |
| 2532 | return true; |
| 2533 | } |
| 2534 | |
| 2535 | /* |
| 2536 | * sfe_ipv4_debug_dev_read_connections_start() |
| 2537 | * Generate part of the XML output. |
| 2538 | */ |
| 2539 | static bool sfe_ipv4_debug_dev_read_connections_start(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length, |
| 2540 | int *total_read, struct sfe_ipv4_debug_xml_write_state *ws) |
| 2541 | { |
| 2542 | int bytes_read; |
| 2543 | |
| 2544 | bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<connections>\n"); |
| 2545 | if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) { |
| 2546 | return false; |
| 2547 | } |
| 2548 | |
| 2549 | *length -= bytes_read; |
| 2550 | *total_read += bytes_read; |
| 2551 | |
| 2552 | ws->state++; |
| 2553 | return true; |
| 2554 | } |
| 2555 | |
| 2556 | /* |
| 2557 | * sfe_ipv4_debug_dev_read_connections_connection() |
| 2558 | * Generate part of the XML output. |
| 2559 | */ |
| 2560 | static bool sfe_ipv4_debug_dev_read_connections_connection(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length, |
| 2561 | int *total_read, struct sfe_ipv4_debug_xml_write_state *ws) |
| 2562 | { |
| 2563 | struct sfe_ipv4_connection *c; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2564 | struct sfe_ipv4_connection_match *original_cm; |
| 2565 | struct sfe_ipv4_connection_match *reply_cm; |
| 2566 | int bytes_read; |
| 2567 | int protocol; |
| 2568 | struct net_device *src_dev; |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 2569 | __be32 src_ip; |
| 2570 | __be32 src_ip_xlate; |
| 2571 | __be16 src_port; |
| 2572 | __be16 src_port_xlate; |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 2573 | u64 src_rx_packets; |
| 2574 | u64 src_rx_bytes; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2575 | struct net_device *dest_dev; |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 2576 | __be32 dest_ip; |
| 2577 | __be32 dest_ip_xlate; |
| 2578 | __be16 dest_port; |
| 2579 | __be16 dest_port_xlate; |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 2580 | u64 dest_rx_packets; |
| 2581 | u64 dest_rx_bytes; |
| 2582 | u64 last_sync_jiffies; |
| 2583 | u32 mark, src_priority, dest_priority, src_dscp, dest_dscp; |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 2584 | u32 packet, byte; |
Xiaoping Fan | d1dc7b2 | 2015-01-23 00:43:56 -0800 | [diff] [blame] | 2585 | #ifdef CONFIG_NF_FLOW_COOKIE |
| 2586 | int src_flow_cookie, dst_flow_cookie; |
| 2587 | #endif |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2588 | |
| 2589 | spin_lock_bh(&si->lock); |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 2590 | |
| 2591 | for (c = si->all_connections_head; c; c = c->all_connections_next) { |
| 2592 | if (c->debug_read_seq < si->debug_read_seq) { |
| 2593 | c->debug_read_seq = si->debug_read_seq; |
| 2594 | break; |
| 2595 | } |
| 2596 | } |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2597 | |
| 2598 | /* |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 2599 | * If there were no connections then move to the next state. |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2600 | */ |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 2601 | if (!c || c->removed) { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2602 | spin_unlock_bh(&si->lock); |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 2603 | ws->state++; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2604 | return true; |
| 2605 | } |
| 2606 | |
| 2607 | original_cm = c->original_match; |
| 2608 | reply_cm = c->reply_match; |
| 2609 | |
| 2610 | protocol = c->protocol; |
| 2611 | src_dev = c->original_dev; |
| 2612 | src_ip = c->src_ip; |
| 2613 | src_ip_xlate = c->src_ip_xlate; |
| 2614 | src_port = c->src_port; |
| 2615 | src_port_xlate = c->src_port_xlate; |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 2616 | src_priority = original_cm->priority; |
| 2617 | src_dscp = original_cm->dscp >> SFE_IPV4_DSCP_SHIFT; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2618 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 2619 | sfe_ipv4_connection_match_update_summary_stats(original_cm, &packet, &byte); |
| 2620 | sfe_ipv4_connection_match_update_summary_stats(reply_cm, &packet, &byte); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2621 | |
| 2622 | src_rx_packets = original_cm->rx_packet_count64; |
| 2623 | src_rx_bytes = original_cm->rx_byte_count64; |
| 2624 | dest_dev = c->reply_dev; |
| 2625 | dest_ip = c->dest_ip; |
| 2626 | dest_ip_xlate = c->dest_ip_xlate; |
| 2627 | dest_port = c->dest_port; |
| 2628 | dest_port_xlate = c->dest_port_xlate; |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 2629 | dest_priority = reply_cm->priority; |
| 2630 | dest_dscp = reply_cm->dscp >> SFE_IPV4_DSCP_SHIFT; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2631 | dest_rx_packets = reply_cm->rx_packet_count64; |
| 2632 | dest_rx_bytes = reply_cm->rx_byte_count64; |
| 2633 | last_sync_jiffies = get_jiffies_64() - c->last_sync_jiffies; |
Cristian Prundeanu | 592265e | 2013-12-26 11:01:22 -0600 | [diff] [blame] | 2634 | mark = c->mark; |
Xiaoping Fan | d1dc7b2 | 2015-01-23 00:43:56 -0800 | [diff] [blame] | 2635 | #ifdef CONFIG_NF_FLOW_COOKIE |
| 2636 | src_flow_cookie = original_cm->flow_cookie; |
| 2637 | dst_flow_cookie = reply_cm->flow_cookie; |
| 2638 | #endif |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2639 | spin_unlock_bh(&si->lock); |
| 2640 | |
| 2641 | bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t\t<connection " |
| 2642 | "protocol=\"%u\" " |
| 2643 | "src_dev=\"%s\" " |
| 2644 | "src_ip=\"%pI4\" src_ip_xlate=\"%pI4\" " |
| 2645 | "src_port=\"%u\" src_port_xlate=\"%u\" " |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 2646 | "src_priority=\"%u\" src_dscp=\"%u\" " |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2647 | "src_rx_pkts=\"%llu\" src_rx_bytes=\"%llu\" " |
| 2648 | "dest_dev=\"%s\" " |
| 2649 | "dest_ip=\"%pI4\" dest_ip_xlate=\"%pI4\" " |
| 2650 | "dest_port=\"%u\" dest_port_xlate=\"%u\" " |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 2651 | "dest_priority=\"%u\" dest_dscp=\"%u\" " |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2652 | "dest_rx_pkts=\"%llu\" dest_rx_bytes=\"%llu\" " |
Xiaoping Fan | d1dc7b2 | 2015-01-23 00:43:56 -0800 | [diff] [blame] | 2653 | #ifdef CONFIG_NF_FLOW_COOKIE |
| 2654 | "src_flow_cookie=\"%d\" dst_flow_cookie=\"%d\" " |
| 2655 | #endif |
Cristian Prundeanu | 592265e | 2013-12-26 11:01:22 -0600 | [diff] [blame] | 2656 | "last_sync=\"%llu\" " |
Nicolas Costa | bb85a2e | 2014-01-13 16:26:33 -0600 | [diff] [blame] | 2657 | "mark=\"%08x\" />\n", |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2658 | protocol, |
| 2659 | src_dev->name, |
| 2660 | &src_ip, &src_ip_xlate, |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 2661 | ntohs(src_port), ntohs(src_port_xlate), |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 2662 | src_priority, src_dscp, |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2663 | src_rx_packets, src_rx_bytes, |
| 2664 | dest_dev->name, |
| 2665 | &dest_ip, &dest_ip_xlate, |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 2666 | ntohs(dest_port), ntohs(dest_port_xlate), |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 2667 | dest_priority, dest_dscp, |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2668 | dest_rx_packets, dest_rx_bytes, |
Xiaoping Fan | d1dc7b2 | 2015-01-23 00:43:56 -0800 | [diff] [blame] | 2669 | #ifdef CONFIG_NF_FLOW_COOKIE |
| 2670 | src_flow_cookie, dst_flow_cookie, |
| 2671 | #endif |
Cristian Prundeanu | 592265e | 2013-12-26 11:01:22 -0600 | [diff] [blame] | 2672 | last_sync_jiffies, mark); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2673 | |
| 2674 | if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) { |
| 2675 | return false; |
| 2676 | } |
| 2677 | |
| 2678 | *length -= bytes_read; |
| 2679 | *total_read += bytes_read; |
| 2680 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2681 | return true; |
| 2682 | } |
| 2683 | |
| 2684 | /* |
| 2685 | * sfe_ipv4_debug_dev_read_connections_end() |
| 2686 | * Generate part of the XML output. |
| 2687 | */ |
| 2688 | static bool sfe_ipv4_debug_dev_read_connections_end(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length, |
| 2689 | int *total_read, struct sfe_ipv4_debug_xml_write_state *ws) |
| 2690 | { |
| 2691 | int bytes_read; |
| 2692 | |
| 2693 | bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t</connections>\n"); |
| 2694 | if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) { |
| 2695 | return false; |
| 2696 | } |
| 2697 | |
| 2698 | *length -= bytes_read; |
| 2699 | *total_read += bytes_read; |
| 2700 | |
| 2701 | ws->state++; |
| 2702 | return true; |
| 2703 | } |
| 2704 | |
| 2705 | /* |
| 2706 | * sfe_ipv4_debug_dev_read_exceptions_start() |
| 2707 | * Generate part of the XML output. |
| 2708 | */ |
| 2709 | static bool sfe_ipv4_debug_dev_read_exceptions_start(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length, |
| 2710 | int *total_read, struct sfe_ipv4_debug_xml_write_state *ws) |
| 2711 | { |
| 2712 | int bytes_read; |
| 2713 | |
| 2714 | bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<exceptions>\n"); |
| 2715 | if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) { |
| 2716 | return false; |
| 2717 | } |
| 2718 | |
| 2719 | *length -= bytes_read; |
| 2720 | *total_read += bytes_read; |
| 2721 | |
| 2722 | ws->state++; |
| 2723 | return true; |
| 2724 | } |
| 2725 | |
| 2726 | /* |
| 2727 | * sfe_ipv4_debug_dev_read_exceptions_exception() |
| 2728 | * Generate part of the XML output. |
| 2729 | */ |
| 2730 | static bool sfe_ipv4_debug_dev_read_exceptions_exception(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length, |
| 2731 | int *total_read, struct sfe_ipv4_debug_xml_write_state *ws) |
| 2732 | { |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 2733 | int i; |
| 2734 | u64 val = 0; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2735 | |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 2736 | for_each_possible_cpu(i) { |
| 2737 | const struct sfe_ipv4_stats *s = per_cpu_ptr(si->stats_pcpu, i); |
| 2738 | val += s->exception_events64[ws->iter_exception]; |
| 2739 | } |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2740 | |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 2741 | if (val) { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2742 | int bytes_read; |
| 2743 | |
| 2744 | bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, |
| 2745 | "\t\t<exception name=\"%s\" count=\"%llu\" />\n", |
| 2746 | sfe_ipv4_exception_events_string[ws->iter_exception], |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 2747 | val); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2748 | if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) { |
| 2749 | return false; |
| 2750 | } |
| 2751 | |
| 2752 | *length -= bytes_read; |
| 2753 | *total_read += bytes_read; |
| 2754 | } |
| 2755 | |
| 2756 | ws->iter_exception++; |
| 2757 | if (ws->iter_exception >= SFE_IPV4_EXCEPTION_EVENT_LAST) { |
| 2758 | ws->iter_exception = 0; |
| 2759 | ws->state++; |
| 2760 | } |
| 2761 | |
| 2762 | return true; |
| 2763 | } |
| 2764 | |
| 2765 | /* |
| 2766 | * sfe_ipv4_debug_dev_read_exceptions_end() |
| 2767 | * Generate part of the XML output. |
| 2768 | */ |
| 2769 | static bool sfe_ipv4_debug_dev_read_exceptions_end(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length, |
| 2770 | int *total_read, struct sfe_ipv4_debug_xml_write_state *ws) |
| 2771 | { |
| 2772 | int bytes_read; |
| 2773 | |
| 2774 | bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t</exceptions>\n"); |
| 2775 | if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) { |
| 2776 | return false; |
| 2777 | } |
| 2778 | |
| 2779 | *length -= bytes_read; |
| 2780 | *total_read += bytes_read; |
| 2781 | |
| 2782 | ws->state++; |
| 2783 | return true; |
| 2784 | } |
| 2785 | |
| 2786 | /* |
| 2787 | * sfe_ipv4_debug_dev_read_stats() |
| 2788 | * Generate part of the XML output. |
| 2789 | */ |
| 2790 | static bool sfe_ipv4_debug_dev_read_stats(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length, |
| 2791 | int *total_read, struct sfe_ipv4_debug_xml_write_state *ws) |
| 2792 | { |
| 2793 | int bytes_read; |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 2794 | struct sfe_ipv4_stats stats; |
| 2795 | unsigned int num_conn; |
| 2796 | |
| 2797 | sfe_ipv4_update_summary_stats(si, &stats); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2798 | |
| 2799 | spin_lock_bh(&si->lock); |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 2800 | num_conn = si->num_connections; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2801 | spin_unlock_bh(&si->lock); |
| 2802 | |
| 2803 | bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<stats " |
| 2804 | "num_connections=\"%u\" " |
Xiaoping Fan | 5917642 | 2015-05-22 15:58:10 -0700 | [diff] [blame] | 2805 | "pkts_forwarded=\"%llu\" pkts_not_forwarded=\"%llu\" " |
| 2806 | "create_requests=\"%llu\" create_collisions=\"%llu\" " |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2807 | "destroy_requests=\"%llu\" destroy_misses=\"%llu\" " |
| 2808 | "flushes=\"%llu\" " |
| 2809 | "hash_hits=\"%llu\" hash_reorders=\"%llu\" />\n", |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 2810 | num_conn, |
| 2811 | stats.packets_forwarded64, |
| 2812 | stats.packets_not_forwarded64, |
| 2813 | stats.connection_create_requests64, |
| 2814 | stats.connection_create_collisions64, |
| 2815 | stats.connection_destroy_requests64, |
| 2816 | stats.connection_destroy_misses64, |
| 2817 | stats.connection_flushes64, |
| 2818 | stats.connection_match_hash_hits64, |
| 2819 | stats.connection_match_hash_reorders64); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2820 | if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) { |
| 2821 | return false; |
| 2822 | } |
| 2823 | |
| 2824 | *length -= bytes_read; |
| 2825 | *total_read += bytes_read; |
| 2826 | |
| 2827 | ws->state++; |
| 2828 | return true; |
| 2829 | } |
| 2830 | |
| 2831 | /* |
| 2832 | * sfe_ipv4_debug_dev_read_end() |
| 2833 | * Generate part of the XML output. |
| 2834 | */ |
| 2835 | static bool sfe_ipv4_debug_dev_read_end(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length, |
| 2836 | int *total_read, struct sfe_ipv4_debug_xml_write_state *ws) |
| 2837 | { |
| 2838 | int bytes_read; |
| 2839 | |
| 2840 | bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "</sfe_ipv4>\n"); |
| 2841 | if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) { |
| 2842 | return false; |
| 2843 | } |
| 2844 | |
| 2845 | *length -= bytes_read; |
| 2846 | *total_read += bytes_read; |
| 2847 | |
| 2848 | ws->state++; |
| 2849 | return true; |
| 2850 | } |
| 2851 | |
| 2852 | /* |
| 2853 | * Array of write functions that write various XML elements that correspond to |
| 2854 | * our XML output state machine. |
| 2855 | */ |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 2856 | 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] | 2857 | sfe_ipv4_debug_dev_read_start, |
| 2858 | sfe_ipv4_debug_dev_read_connections_start, |
| 2859 | sfe_ipv4_debug_dev_read_connections_connection, |
| 2860 | sfe_ipv4_debug_dev_read_connections_end, |
| 2861 | sfe_ipv4_debug_dev_read_exceptions_start, |
| 2862 | sfe_ipv4_debug_dev_read_exceptions_exception, |
| 2863 | sfe_ipv4_debug_dev_read_exceptions_end, |
| 2864 | sfe_ipv4_debug_dev_read_stats, |
| 2865 | sfe_ipv4_debug_dev_read_end, |
| 2866 | }; |
| 2867 | |
| 2868 | /* |
| 2869 | * sfe_ipv4_debug_dev_read() |
| 2870 | * Send info to userspace upon read request from user |
| 2871 | */ |
| 2872 | static ssize_t sfe_ipv4_debug_dev_read(struct file *filp, char *buffer, size_t length, loff_t *offset) |
| 2873 | { |
| 2874 | char msg[CHAR_DEV_MSG_SIZE]; |
| 2875 | int total_read = 0; |
| 2876 | struct sfe_ipv4_debug_xml_write_state *ws; |
| 2877 | struct sfe_ipv4 *si = &__si; |
| 2878 | |
| 2879 | ws = (struct sfe_ipv4_debug_xml_write_state *)filp->private_data; |
| 2880 | while ((ws->state != SFE_IPV4_DEBUG_XML_STATE_DONE) && (length > CHAR_DEV_MSG_SIZE)) { |
| 2881 | if ((sfe_ipv4_debug_xml_write_methods[ws->state])(si, buffer, msg, &length, &total_read, ws)) { |
| 2882 | continue; |
| 2883 | } |
| 2884 | } |
| 2885 | |
| 2886 | return total_read; |
| 2887 | } |
| 2888 | |
| 2889 | /* |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2890 | * sfe_ipv4_debug_dev_open() |
| 2891 | */ |
| 2892 | static int sfe_ipv4_debug_dev_open(struct inode *inode, struct file *file) |
| 2893 | { |
| 2894 | struct sfe_ipv4_debug_xml_write_state *ws; |
| 2895 | |
| 2896 | ws = (struct sfe_ipv4_debug_xml_write_state *)file->private_data; |
| 2897 | if (!ws) { |
| 2898 | ws = kzalloc(sizeof(struct sfe_ipv4_debug_xml_write_state), GFP_KERNEL); |
| 2899 | if (!ws) { |
| 2900 | return -ENOMEM; |
| 2901 | } |
| 2902 | |
| 2903 | ws->state = SFE_IPV4_DEBUG_XML_STATE_START; |
| 2904 | file->private_data = ws; |
| 2905 | } |
| 2906 | |
| 2907 | return 0; |
| 2908 | } |
| 2909 | |
| 2910 | /* |
| 2911 | * sfe_ipv4_debug_dev_release() |
| 2912 | */ |
| 2913 | static int sfe_ipv4_debug_dev_release(struct inode *inode, struct file *file) |
| 2914 | { |
| 2915 | struct sfe_ipv4_debug_xml_write_state *ws; |
| 2916 | |
| 2917 | ws = (struct sfe_ipv4_debug_xml_write_state *)file->private_data; |
| 2918 | if (ws) { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2919 | /* |
| 2920 | * We've finished with our output so free the write state. |
| 2921 | */ |
| 2922 | kfree(ws); |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 2923 | file->private_data = NULL; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2924 | } |
| 2925 | |
| 2926 | return 0; |
| 2927 | } |
| 2928 | |
| 2929 | /* |
| 2930 | * File operations used in the debug char device |
| 2931 | */ |
| 2932 | static struct file_operations sfe_ipv4_debug_dev_fops = { |
| 2933 | .read = sfe_ipv4_debug_dev_read, |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2934 | .open = sfe_ipv4_debug_dev_open, |
| 2935 | .release = sfe_ipv4_debug_dev_release |
| 2936 | }; |
| 2937 | |
Xiaoping Fan | d1dc7b2 | 2015-01-23 00:43:56 -0800 | [diff] [blame] | 2938 | #ifdef CONFIG_NF_FLOW_COOKIE |
| 2939 | /* |
| 2940 | * sfe_register_flow_cookie_cb |
| 2941 | * register a function in SFE to let SFE use this function to configure flow cookie for a flow |
| 2942 | * |
| 2943 | * Hardware driver which support flow cookie should register a callback function in SFE. Then SFE |
| 2944 | * can use this function to configure flow cookie for a flow. |
| 2945 | * return: 0, success; !=0, fail |
| 2946 | */ |
| 2947 | int sfe_register_flow_cookie_cb(flow_cookie_set_func_t cb) |
| 2948 | { |
| 2949 | struct sfe_ipv4 *si = &__si; |
| 2950 | |
| 2951 | BUG_ON(!cb); |
| 2952 | |
| 2953 | if (si->flow_cookie_set_func) { |
| 2954 | return -1; |
| 2955 | } |
| 2956 | |
| 2957 | rcu_assign_pointer(si->flow_cookie_set_func, cb); |
| 2958 | return 0; |
| 2959 | } |
| 2960 | |
| 2961 | /* |
| 2962 | * sfe_unregister_flow_cookie_cb |
| 2963 | * unregister function which is used to configure flow cookie for a flow |
| 2964 | * |
| 2965 | * return: 0, success; !=0, fail |
| 2966 | */ |
| 2967 | int sfe_unregister_flow_cookie_cb(flow_cookie_set_func_t cb) |
| 2968 | { |
| 2969 | struct sfe_ipv4 *si = &__si; |
| 2970 | |
| 2971 | RCU_INIT_POINTER(si->flow_cookie_set_func, NULL); |
| 2972 | return 0; |
| 2973 | } |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 2974 | |
| 2975 | /* |
| 2976 | * sfe_ipv4_get_flow_cookie() |
| 2977 | */ |
| 2978 | static ssize_t sfe_ipv4_get_flow_cookie(struct device *dev, |
| 2979 | struct device_attribute *attr, |
| 2980 | char *buf) |
| 2981 | { |
| 2982 | struct sfe_ipv4 *si = &__si; |
Xiaoping Fan | 01c67cc | 2015-11-09 11:31:57 -0800 | [diff] [blame] | 2983 | 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] | 2984 | } |
| 2985 | |
| 2986 | /* |
| 2987 | * sfe_ipv4_set_flow_cookie() |
| 2988 | */ |
| 2989 | static ssize_t sfe_ipv4_set_flow_cookie(struct device *dev, |
| 2990 | struct device_attribute *attr, |
| 2991 | const char *buf, size_t size) |
| 2992 | { |
| 2993 | struct sfe_ipv4 *si = &__si; |
| 2994 | strict_strtol(buf, 0, (long int *)&si->flow_cookie_enable); |
| 2995 | |
| 2996 | return size; |
| 2997 | } |
| 2998 | |
| 2999 | /* |
| 3000 | * sysfs attributes. |
| 3001 | */ |
| 3002 | static const struct device_attribute sfe_ipv4_flow_cookie_attr = |
Xiaoping Fan | e70da41 | 2016-02-26 16:47:57 -0800 | [diff] [blame] | 3003 | __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] | 3004 | #endif /*CONFIG_NF_FLOW_COOKIE*/ |
| 3005 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 3006 | /* |
| 3007 | * sfe_ipv4_conn_match_hash_init() |
| 3008 | * Initialize conn match hash lists |
| 3009 | */ |
| 3010 | static void sfe_ipv4_conn_match_hash_init(struct sfe_ipv4 *si, int len) |
| 3011 | { |
| 3012 | struct hlist_head *hash_list = si->hlist_conn_match_hash_head; |
| 3013 | int i; |
| 3014 | |
| 3015 | for (i = 0; i < len; i++) { |
| 3016 | INIT_HLIST_HEAD(&hash_list[i]); |
| 3017 | } |
| 3018 | } |
| 3019 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 3020 | /* |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 3021 | * sfe_ipv4_init() |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 3022 | */ |
Ratheesh Kannoth | 24fb1db | 2021-10-20 07:28:06 +0530 | [diff] [blame] | 3023 | int sfe_ipv4_init(void) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 3024 | { |
| 3025 | struct sfe_ipv4 *si = &__si; |
| 3026 | int result = -1; |
| 3027 | |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 3028 | DEBUG_INFO("SFE IPv4 init\n"); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 3029 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 3030 | sfe_ipv4_conn_match_hash_init(si, ARRAY_SIZE(si->hlist_conn_match_hash_head)); |
| 3031 | |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 3032 | si->stats_pcpu = alloc_percpu_gfp(struct sfe_ipv4_stats, GFP_KERNEL | __GFP_ZERO); |
| 3033 | if (!si->stats_pcpu) { |
| 3034 | DEBUG_ERROR("failed to allocate stats memory for sfe_ipv4\n"); |
| 3035 | goto exit0; |
| 3036 | } |
| 3037 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 3038 | /* |
| 3039 | * Create sys/sfe_ipv4 |
| 3040 | */ |
| 3041 | si->sys_sfe_ipv4 = kobject_create_and_add("sfe_ipv4", NULL); |
| 3042 | if (!si->sys_sfe_ipv4) { |
| 3043 | DEBUG_ERROR("failed to register sfe_ipv4\n"); |
| 3044 | goto exit1; |
| 3045 | } |
| 3046 | |
| 3047 | /* |
| 3048 | * Create files, one for each parameter supported by this module. |
| 3049 | */ |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 3050 | result = sysfs_create_file(si->sys_sfe_ipv4, &sfe_ipv4_debug_dev_attr.attr); |
| 3051 | if (result) { |
| 3052 | DEBUG_ERROR("failed to register debug dev file: %d\n", result); |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 3053 | goto exit2; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 3054 | } |
| 3055 | |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 3056 | #ifdef CONFIG_NF_FLOW_COOKIE |
| 3057 | result = sysfs_create_file(si->sys_sfe_ipv4, &sfe_ipv4_flow_cookie_attr.attr); |
| 3058 | if (result) { |
| 3059 | DEBUG_ERROR("failed to register flow cookie enable file: %d\n", result); |
| 3060 | goto exit3; |
| 3061 | } |
| 3062 | #endif /* CONFIG_NF_FLOW_COOKIE */ |
| 3063 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 3064 | /* |
| 3065 | * Register our debug char device. |
| 3066 | */ |
| 3067 | result = register_chrdev(0, "sfe_ipv4", &sfe_ipv4_debug_dev_fops); |
| 3068 | if (result < 0) { |
| 3069 | DEBUG_ERROR("Failed to register chrdev: %d\n", result); |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 3070 | goto exit4; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 3071 | } |
| 3072 | |
| 3073 | si->debug_dev = result; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 3074 | |
| 3075 | /* |
| 3076 | * Create a timer to handle periodic statistics. |
| 3077 | */ |
Tian Yang | 45f39c8 | 2020-10-06 14:07:47 -0700 | [diff] [blame] | 3078 | #if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 15, 0)) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 3079 | setup_timer(&si->timer, sfe_ipv4_periodic_sync, (unsigned long)si); |
Tian Yang | 45f39c8 | 2020-10-06 14:07:47 -0700 | [diff] [blame] | 3080 | #else |
| 3081 | timer_setup(&si->timer, sfe_ipv4_periodic_sync, 0); |
| 3082 | #endif |
Matthew McClintock | af48f1e | 2014-01-23 15:29:19 -0600 | [diff] [blame] | 3083 | mod_timer(&si->timer, jiffies + ((HZ + 99) / 100)); |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 3084 | spin_lock_init(&si->lock); |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 3085 | return 0; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 3086 | |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 3087 | exit4: |
| 3088 | #ifdef CONFIG_NF_FLOW_COOKIE |
| 3089 | sysfs_remove_file(si->sys_sfe_ipv4, &sfe_ipv4_flow_cookie_attr.attr); |
| 3090 | |
| 3091 | exit3: |
| 3092 | #endif /* CONFIG_NF_FLOW_COOKIE */ |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 3093 | sysfs_remove_file(si->sys_sfe_ipv4, &sfe_ipv4_debug_dev_attr.attr); |
| 3094 | |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 3095 | exit2: |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 3096 | kobject_put(si->sys_sfe_ipv4); |
| 3097 | |
| 3098 | exit1: |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 3099 | free_percpu(si->stats_pcpu); |
| 3100 | |
| 3101 | exit0: |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 3102 | return result; |
| 3103 | } |
| 3104 | |
| 3105 | /* |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 3106 | * sfe_ipv4_exit() |
| 3107 | */ |
Ratheesh Kannoth | 24fb1db | 2021-10-20 07:28:06 +0530 | [diff] [blame] | 3108 | void sfe_ipv4_exit(void) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 3109 | { |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 3110 | struct sfe_ipv4 *si = &__si; |
| 3111 | |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 3112 | DEBUG_INFO("SFE IPv4 exit\n"); |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 3113 | /* |
| 3114 | * Destroy all connections. |
| 3115 | */ |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 3116 | sfe_ipv4_destroy_all_rules_for_dev(NULL); |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 3117 | |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 3118 | del_timer_sync(&si->timer); |
| 3119 | |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 3120 | unregister_chrdev(si->debug_dev, "sfe_ipv4"); |
| 3121 | |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 3122 | #ifdef CONFIG_NF_FLOW_COOKIE |
| 3123 | sysfs_remove_file(si->sys_sfe_ipv4, &sfe_ipv4_flow_cookie_attr.attr); |
| 3124 | #endif /* CONFIG_NF_FLOW_COOKIE */ |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 3125 | sysfs_remove_file(si->sys_sfe_ipv4, &sfe_ipv4_debug_dev_attr.attr); |
| 3126 | |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 3127 | kobject_put(si->sys_sfe_ipv4); |
| 3128 | |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame^] | 3129 | free_percpu(si->stats_pcpu); |
| 3130 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 3131 | } |
| 3132 | |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 3133 | EXPORT_SYMBOL(sfe_ipv4_recv); |
| 3134 | EXPORT_SYMBOL(sfe_ipv4_create_rule); |
| 3135 | EXPORT_SYMBOL(sfe_ipv4_destroy_rule); |
| 3136 | EXPORT_SYMBOL(sfe_ipv4_destroy_all_rules_for_dev); |
| 3137 | EXPORT_SYMBOL(sfe_ipv4_register_sync_rule_callback); |
Matthew McClintock | be7b47d | 2013-11-27 13:26:23 -0600 | [diff] [blame] | 3138 | EXPORT_SYMBOL(sfe_ipv4_mark_rule); |
Nicolas Costa | 436926b | 2014-01-14 10:36:22 -0600 | [diff] [blame] | 3139 | EXPORT_SYMBOL(sfe_ipv4_update_rule); |
Xiaoping Fan | d1dc7b2 | 2015-01-23 00:43:56 -0800 | [diff] [blame] | 3140 | #ifdef CONFIG_NF_FLOW_COOKIE |
| 3141 | EXPORT_SYMBOL(sfe_register_flow_cookie_cb); |
| 3142 | EXPORT_SYMBOL(sfe_unregister_flow_cookie_cb); |
| 3143 | #endif |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 3144 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 3145 | MODULE_DESCRIPTION("Shortcut Forwarding Engine - IPv4 edition"); |
Matthew McClintock | a322194 | 2014-01-16 11:44:26 -0600 | [diff] [blame] | 3146 | MODULE_LICENSE("Dual BSD/GPL"); |