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