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