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