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