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