Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 1 | /* |
| 2 | * sfe-cm.c |
| 3 | * Shortcut forwarding engine connection manager. |
| 4 | * |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 5 | * Copyright (c) 2013-2015 Qualcomm Atheros, Inc. |
Matthew McClintock | a322194 | 2014-01-16 11:44:26 -0600 | [diff] [blame] | 6 | * |
| 7 | * All Rights Reserved. |
| 8 | * Qualcomm Atheros Confidential and Proprietary. |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 9 | */ |
Matthew McClintock | a322194 | 2014-01-16 11:44:26 -0600 | [diff] [blame] | 10 | |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 11 | #include <linux/module.h> |
| 12 | #include <linux/sysfs.h> |
| 13 | #include <linux/skbuff.h> |
| 14 | #include <net/route.h> |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 15 | #include <net/ip6_route.h> |
| 16 | #include <net/addrconf.h> |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 17 | #include <linux/inetdevice.h> |
| 18 | #include <linux/netfilter_bridge.h> |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 19 | #include <linux/netfilter_ipv6.h> |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 20 | #include <net/netfilter/nf_conntrack_acct.h> |
| 21 | #include <net/netfilter/nf_conntrack_helper.h> |
| 22 | #include <net/netfilter/nf_conntrack_zones.h> |
| 23 | #include <net/netfilter/nf_conntrack_core.h> |
Matthew McClintock | bf6b5bc | 2014-02-24 12:27:14 -0600 | [diff] [blame] | 24 | #include <linux/if_bridge.h> |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 25 | |
| 26 | #include "sfe.h" |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 27 | #include "sfe_cm.h" |
Xiaoping Fan | 3f1fe51 | 2014-11-05 12:14:57 -0800 | [diff] [blame] | 28 | #include "sfe_backport.h" |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 29 | |
| 30 | /* |
| 31 | * Per-module structure. |
| 32 | */ |
| 33 | struct sfe_cm { |
| 34 | spinlock_t lock; /* Lock for SMP correctness */ |
| 35 | |
| 36 | /* |
| 37 | * Control state. |
| 38 | */ |
| 39 | struct kobject *sys_sfe_cm; /* sysfs linkage */ |
| 40 | |
| 41 | /* |
| 42 | * Callback notifiers. |
| 43 | */ |
| 44 | struct notifier_block dev_notifier; |
| 45 | /* Device notifier */ |
| 46 | struct notifier_block inet_notifier; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 47 | /* IPv4 notifier */ |
| 48 | struct notifier_block inet6_notifier; |
| 49 | /* IPv6 notifier */ |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | struct sfe_cm __sc; |
| 53 | |
| 54 | /* |
| 55 | * Expose the hook for the receive processing. |
| 56 | */ |
| 57 | extern int (*athrs_fast_nat_recv)(struct sk_buff *skb); |
| 58 | |
| 59 | /* |
| 60 | * Expose what should be a static flag in the TCP connection tracker. |
| 61 | */ |
| 62 | extern int nf_ct_tcp_no_window_check; |
| 63 | |
| 64 | /* |
| 65 | * sfe_cm_recv() |
| 66 | * Handle packet receives. |
| 67 | * |
| 68 | * Returns 1 if the packet is forwarded or 0 if it isn't. |
| 69 | */ |
| 70 | int sfe_cm_recv(struct sk_buff *skb) |
| 71 | { |
| 72 | struct net_device *dev; |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 73 | |
| 74 | /* |
| 75 | * We know that for the vast majority of packets we need the transport |
| 76 | * layer header so we may as well start to fetch it now! |
| 77 | */ |
| 78 | prefetch(skb->data + 32); |
| 79 | barrier(); |
| 80 | |
| 81 | dev = skb->dev; |
| 82 | |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 83 | /* |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 84 | * We're only interested in IPv4 and IPv6 packets. |
Xiaoping Fan | 5917642 | 2015-05-22 15:58:10 -0700 | [diff] [blame] | 85 | */ |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 86 | if (likely(htons(ETH_P_IP) == skb->protocol)) { |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 87 | #if (SFE_HOOK_ABOVE_BRIDGE) |
| 88 | struct in_device *in_dev; |
| 89 | |
| 90 | /* |
| 91 | * Does our input device support IP processing? |
| 92 | */ |
| 93 | in_dev = (struct in_device *)dev->ip_ptr; |
| 94 | if (unlikely(!in_dev)) { |
| 95 | DEBUG_TRACE("no IP processing for device: %s\n", dev->name); |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | /* |
| 100 | * Does it have an IP address? If it doesn't then we can't do anything |
| 101 | * interesting here! |
| 102 | */ |
| 103 | if (unlikely(!in_dev->ifa_list)) { |
| 104 | DEBUG_TRACE("no IP address for device: %s\n", dev->name); |
| 105 | return 0; |
| 106 | } |
| 107 | #endif |
| 108 | |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 109 | return sfe_ipv4_recv(dev, skb); |
| 110 | } |
| 111 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 112 | if (likely(htons(ETH_P_IPV6) == skb->protocol)) { |
| 113 | #if (SFE_HOOK_ABOVE_BRIDGE) |
| 114 | struct inet6_dev *in_dev; |
| 115 | |
| 116 | /* |
| 117 | * Does our input device support IPv6 processing? |
| 118 | */ |
| 119 | in_dev = (struct inet6_dev *)dev->ip6_ptr; |
| 120 | if (unlikely(!in_dev)) { |
| 121 | DEBUG_TRACE("no IPv6 processing for device: %s\n", dev->name); |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | /* |
| 126 | * Does it have an IPv6 address? If it doesn't then we can't do anything |
| 127 | * interesting here! |
| 128 | */ |
| 129 | if (unlikely(list_empty(&in_dev->addr_list))) { |
| 130 | DEBUG_TRACE("no IPv6 address for device: %s\n", dev->name); |
| 131 | return 0; |
| 132 | } |
| 133 | #endif |
| 134 | |
| 135 | return sfe_ipv6_recv(dev, skb); |
| 136 | } |
| 137 | |
Matthew McClintock | a8ad796 | 2014-01-16 16:49:30 -0600 | [diff] [blame] | 138 | DEBUG_TRACE("not IP packet\n"); |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | /* |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 143 | * sfe_cm_find_dev_and_mac_addr() |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 144 | * Find the device and MAC address for a given IPv4/IPv6 address. |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 145 | * |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 146 | * Returns true if we find the device and MAC address, otherwise false. |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 147 | * |
| 148 | * We look up the rtable entry for the address and, from its neighbour |
| 149 | * structure, obtain the hardware address. This means this function also |
| 150 | * works if the neighbours are routers too. |
| 151 | */ |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 152 | static bool sfe_cm_find_dev_and_mac_addr(sfe_ip_addr_t *addr, struct net_device **dev, uint8_t *mac_addr, int is_v4) |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 153 | { |
| 154 | struct neighbour *neigh; |
| 155 | struct rtable *rt; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 156 | struct rt6_info *rt6; |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 157 | struct dst_entry *dst; |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 158 | struct net_device *mac_dev; |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 159 | |
| 160 | /* |
| 161 | * Look up the rtable entry for the IP address then get the hardware |
| 162 | * address from its neighbour structure. This means this work when the |
| 163 | * neighbours are routers too. |
| 164 | */ |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 165 | if (likely(is_v4)) { |
| 166 | rt = ip_route_output(&init_net, addr->ip, 0, 0, 0); |
| 167 | if (unlikely(IS_ERR(rt))) { |
| 168 | goto ret_fail; |
| 169 | } |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 170 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 171 | dst = (struct dst_entry *)rt; |
| 172 | } else { |
| 173 | rt6 = rt6_lookup(&init_net, (struct in6_addr *)addr->ip6, 0, 0, 0); |
| 174 | if (!rt6) { |
| 175 | goto ret_fail; |
| 176 | } |
| 177 | |
| 178 | dst = (struct dst_entry *)rt6; |
| 179 | } |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 180 | |
| 181 | rcu_read_lock(); |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 182 | neigh = dst_neigh_lookup(dst, addr); |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 183 | if (unlikely(!neigh)) { |
| 184 | rcu_read_unlock(); |
| 185 | dst_release(dst); |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 186 | goto ret_fail; |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | if (unlikely(!(neigh->nud_state & NUD_VALID))) { |
| 190 | rcu_read_unlock(); |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 191 | neigh_release(neigh); |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 192 | dst_release(dst); |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 193 | goto ret_fail; |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 194 | } |
| 195 | |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 196 | mac_dev = neigh->dev; |
| 197 | if (!mac_dev) { |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 198 | rcu_read_unlock(); |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 199 | neigh_release(neigh); |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 200 | dst_release(dst); |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 201 | goto ret_fail; |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 202 | } |
| 203 | |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 204 | memcpy(mac_addr, neigh->ha, (size_t)mac_dev->addr_len); |
| 205 | |
| 206 | dev_hold(mac_dev); |
| 207 | *dev = mac_dev; |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 208 | rcu_read_unlock(); |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 209 | neigh_release(neigh); |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 210 | dst_release(dst); |
| 211 | |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 212 | return true; |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 213 | |
| 214 | ret_fail: |
| 215 | if (is_v4) { |
| 216 | DEBUG_TRACE("failed to find MAC address for IP: %pI4\n", &addr->ip); |
| 217 | |
| 218 | } else { |
| 219 | DEBUG_TRACE("failed to find MAC address for IP: %pI6\n", addr->ip6); |
| 220 | } |
| 221 | |
| 222 | return false; |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | /* |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 226 | * sfe_cm_post_routing() |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 227 | * Called for packets about to leave the box - either locally generated or forwarded from another interface |
| 228 | */ |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 229 | static unsigned int sfe_cm_post_routing(struct sk_buff *skb, int is_v4) |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 230 | { |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 231 | struct sfe_connection_create sic; |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 232 | struct net_device *in; |
| 233 | struct nf_conn *ct; |
| 234 | enum ip_conntrack_info ctinfo; |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 235 | struct net_device *dev; |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 236 | struct net_device *src_dev; |
| 237 | struct net_device *dest_dev; |
| 238 | struct net_device *src_br_dev = NULL; |
| 239 | struct net_device *dest_br_dev = NULL; |
| 240 | struct nf_conntrack_tuple orig_tuple; |
| 241 | struct nf_conntrack_tuple reply_tuple; |
| 242 | |
| 243 | /* |
| 244 | * Don't process broadcast or multicast packets. |
| 245 | */ |
| 246 | if (unlikely(skb->pkt_type == PACKET_BROADCAST)) { |
| 247 | DEBUG_TRACE("broadcast, ignoring\n"); |
| 248 | return NF_ACCEPT; |
| 249 | } |
| 250 | if (unlikely(skb->pkt_type == PACKET_MULTICAST)) { |
| 251 | DEBUG_TRACE("multicast, ignoring\n"); |
| 252 | return NF_ACCEPT; |
| 253 | } |
| 254 | |
| 255 | /* |
| 256 | * Don't process packets that are not being forwarded. |
| 257 | */ |
| 258 | in = dev_get_by_index(&init_net, skb->skb_iif); |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 259 | if (!in) { |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 260 | DEBUG_TRACE("packet not forwarding\n"); |
| 261 | return NF_ACCEPT; |
| 262 | } |
| 263 | |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 264 | dev_put(in); |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 265 | |
| 266 | /* |
| 267 | * Don't process packets that aren't being tracked by conntrack. |
| 268 | */ |
| 269 | ct = nf_ct_get(skb, &ctinfo); |
| 270 | if (unlikely(!ct)) { |
| 271 | DEBUG_TRACE("no conntrack connection, ignoring\n"); |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 272 | return NF_ACCEPT; |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /* |
| 276 | * Don't process untracked connections. |
| 277 | */ |
| 278 | if (unlikely(ct == &nf_conntrack_untracked)) { |
| 279 | DEBUG_TRACE("untracked connection\n"); |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 280 | return NF_ACCEPT; |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | /* |
Xiaoping Fan | 956461e | 2015-06-25 17:35:13 -0700 | [diff] [blame^] | 284 | * Unconfirmed connection may be dropped by Linux at the final step, |
| 285 | * So we don't process unconfirmed connections. |
| 286 | */ |
| 287 | if (!nf_ct_is_confirmed(ct)) { |
| 288 | DEBUG_TRACE("unconfirmed connection\n"); |
| 289 | return NF_ACCEPT; |
| 290 | } |
| 291 | |
| 292 | /* |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 293 | * Don't process connections that require support from a 'helper' (typically a NAT ALG). |
| 294 | */ |
| 295 | if (unlikely(nfct_help(ct))) { |
| 296 | DEBUG_TRACE("connection has helper\n"); |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 297 | return NF_ACCEPT; |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | /* |
| 301 | * Look up the details of our connection in conntrack. |
| 302 | * |
| 303 | * Note that the data we get from conntrack is for the "ORIGINAL" direction |
| 304 | * but our packet may actually be in the "REPLY" direction. |
| 305 | */ |
| 306 | orig_tuple = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple; |
| 307 | reply_tuple = ct->tuplehash[IP_CT_DIR_REPLY].tuple; |
| 308 | sic.protocol = (int32_t)orig_tuple.dst.protonum; |
| 309 | |
| 310 | /* |
| 311 | * Get addressing information, non-NAT first |
| 312 | */ |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 313 | if (likely(is_v4)) { |
| 314 | sic.src_ip.ip = (__be32)orig_tuple.src.u3.ip; |
| 315 | sic.dest_ip.ip = (__be32)orig_tuple.dst.u3.ip; |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 316 | |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 317 | if (ipv4_is_multicast(sic.src_ip.ip) || ipv4_is_multicast(sic.dest_ip.ip)) { |
| 318 | DEBUG_TRACE("multicast address\n"); |
| 319 | return NF_ACCEPT; |
| 320 | } |
| 321 | |
| 322 | /* |
| 323 | * NAT'ed addresses - note these are as seen from the 'reply' direction |
| 324 | * When NAT does not apply to this connection these will be identical to the above. |
| 325 | */ |
| 326 | sic.src_ip_xlate.ip = (__be32)reply_tuple.dst.u3.ip; |
| 327 | sic.dest_ip_xlate.ip = (__be32)reply_tuple.src.u3.ip; |
| 328 | } else { |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 329 | sic.src_ip.ip6[0] = *((struct sfe_ipv6_addr *)&orig_tuple.src.u3.in6); |
| 330 | sic.dest_ip.ip6[0] = *((struct sfe_ipv6_addr *)&orig_tuple.dst.u3.in6); |
| 331 | |
| 332 | if (ipv6_addr_is_multicast((struct in6_addr *)sic.src_ip.ip6) || |
| 333 | ipv6_addr_is_multicast((struct in6_addr *)sic.dest_ip.ip6)) { |
| 334 | DEBUG_TRACE("multicast address\n"); |
| 335 | return NF_ACCEPT; |
| 336 | } |
| 337 | |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 338 | /* |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 339 | * NAT'ed addresses - note these are as seen from the 'reply' direction |
| 340 | * When NAT does not apply to this connection these will be identical to the above. |
| 341 | */ |
| 342 | sic.src_ip_xlate.ip6[0] = *((struct sfe_ipv6_addr *)&reply_tuple.dst.u3.in6); |
| 343 | sic.dest_ip_xlate.ip6[0] = *((struct sfe_ipv6_addr *)&reply_tuple.src.u3.in6); |
Matthew McClintock | a11c7cd | 2014-08-06 16:41:30 -0500 | [diff] [blame] | 344 | } |
| 345 | |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 346 | sic.flags = 0; |
| 347 | |
| 348 | switch (sic.protocol) { |
| 349 | case IPPROTO_TCP: |
| 350 | sic.src_port = orig_tuple.src.u.tcp.port; |
| 351 | sic.dest_port = orig_tuple.dst.u.tcp.port; |
| 352 | sic.src_port_xlate = reply_tuple.dst.u.tcp.port; |
| 353 | sic.dest_port_xlate = reply_tuple.src.u.tcp.port; |
| 354 | sic.src_td_window_scale = ct->proto.tcp.seen[0].td_scale; |
| 355 | sic.src_td_max_window = ct->proto.tcp.seen[0].td_maxwin; |
| 356 | sic.src_td_end = ct->proto.tcp.seen[0].td_end; |
| 357 | sic.src_td_max_end = ct->proto.tcp.seen[0].td_maxend; |
| 358 | sic.dest_td_window_scale = ct->proto.tcp.seen[1].td_scale; |
| 359 | sic.dest_td_max_window = ct->proto.tcp.seen[1].td_maxwin; |
| 360 | sic.dest_td_end = ct->proto.tcp.seen[1].td_end; |
| 361 | sic.dest_td_max_end = ct->proto.tcp.seen[1].td_maxend; |
| 362 | if (nf_ct_tcp_no_window_check |
| 363 | || (ct->proto.tcp.seen[0].flags & IP_CT_TCP_FLAG_BE_LIBERAL) |
| 364 | || (ct->proto.tcp.seen[1].flags & IP_CT_TCP_FLAG_BE_LIBERAL)) { |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 365 | sic.flags |= SFE_CREATE_FLAG_NO_SEQ_CHECK; |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | /* |
| 369 | * Don't try to manage a non-established connection. |
| 370 | */ |
| 371 | if (!test_bit(IPS_ASSURED_BIT, &ct->status)) { |
| 372 | DEBUG_TRACE("non-established connection\n"); |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 373 | return NF_ACCEPT; |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | /* |
| 377 | * If the connection is shutting down do not manage it. |
| 378 | * state can not be SYN_SENT, SYN_RECV because connection is assured |
| 379 | * Not managed states: FIN_WAIT, CLOSE_WAIT, LAST_ACK, TIME_WAIT, CLOSE. |
| 380 | */ |
| 381 | spin_lock_bh(&ct->lock); |
| 382 | if (ct->proto.tcp.state != TCP_CONNTRACK_ESTABLISHED) { |
| 383 | spin_unlock_bh(&ct->lock); |
| 384 | DEBUG_TRACE("connection in termination state: %#x, s: %pI4:%u, d: %pI4:%u\n", |
| 385 | ct->proto.tcp.state, &sic.src_ip, ntohs(sic.src_port), |
| 386 | &sic.dest_ip, ntohs(sic.dest_port)); |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 387 | return NF_ACCEPT; |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 388 | } |
| 389 | spin_unlock_bh(&ct->lock); |
| 390 | break; |
| 391 | |
| 392 | case IPPROTO_UDP: |
| 393 | sic.src_port = orig_tuple.src.u.udp.port; |
| 394 | sic.dest_port = orig_tuple.dst.u.udp.port; |
| 395 | sic.src_port_xlate = reply_tuple.dst.u.udp.port; |
| 396 | sic.dest_port_xlate = reply_tuple.src.u.udp.port; |
| 397 | break; |
| 398 | |
| 399 | default: |
| 400 | DEBUG_TRACE("unhandled protocol %d\n", sic.protocol); |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 401 | return NF_ACCEPT; |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | /* |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 405 | * Get the net device and MAC addresses that correspond to the various source and |
| 406 | * destination host addresses. |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 407 | */ |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 408 | if (!sfe_cm_find_dev_and_mac_addr(&sic.src_ip, &src_dev, sic.src_mac, is_v4)) { |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 409 | return NF_ACCEPT; |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 410 | } |
| 411 | |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 412 | if (!sfe_cm_find_dev_and_mac_addr(&sic.src_ip_xlate, &dev, sic.src_mac_xlate, is_v4)) { |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 413 | goto done1; |
| 414 | } |
| 415 | |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 416 | dev_put(dev); |
| 417 | |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 418 | if (!sfe_cm_find_dev_and_mac_addr(&sic.dest_ip, &dev, sic.dest_mac, is_v4)) { |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 419 | goto done1; |
| 420 | } |
| 421 | |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 422 | dev_put(dev); |
| 423 | |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 424 | if (!sfe_cm_find_dev_and_mac_addr(&sic.dest_ip_xlate, &dest_dev, sic.dest_mac_xlate, is_v4)) { |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 425 | goto done1; |
| 426 | } |
| 427 | |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 428 | #if (!SFE_HOOK_ABOVE_BRIDGE) |
| 429 | /* |
| 430 | * Now our devices may actually be a bridge interface. If that's |
| 431 | * the case then we need to hunt down the underlying interface. |
| 432 | */ |
| 433 | if (src_dev->priv_flags & IFF_EBRIDGE) { |
| 434 | src_br_dev = br_port_dev_get(src_dev, sic.src_mac); |
| 435 | if (!src_br_dev) { |
| 436 | DEBUG_TRACE("no port found on bridge\n"); |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 437 | goto done2; |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | src_dev = src_br_dev; |
| 441 | } |
| 442 | |
| 443 | if (dest_dev->priv_flags & IFF_EBRIDGE) { |
| 444 | dest_br_dev = br_port_dev_get(dest_dev, sic.dest_mac_xlate); |
| 445 | if (!dest_br_dev) { |
| 446 | DEBUG_TRACE("no port found on bridge\n"); |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 447 | goto done3; |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | dest_dev = dest_br_dev; |
| 451 | } |
| 452 | #else |
| 453 | /* |
| 454 | * Our devices may actually be part of a bridge interface. If that's |
| 455 | * the case then find the bridge interface instead. |
| 456 | */ |
| 457 | if (src_dev->priv_flags & IFF_BRIDGE_PORT) { |
Xiaoping Fan | 3f1fe51 | 2014-11-05 12:14:57 -0800 | [diff] [blame] | 458 | src_br_dev = SFE_DEV_MASTER(src_dev); |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 459 | if (!src_br_dev) { |
| 460 | DEBUG_TRACE("no bridge found for: %s\n", src_dev->name); |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 461 | goto done2; |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | dev_hold(src_br_dev); |
| 465 | src_dev = src_br_dev; |
| 466 | } |
| 467 | |
| 468 | if (dest_dev->priv_flags & IFF_BRIDGE_PORT) { |
Xiaoping Fan | 3f1fe51 | 2014-11-05 12:14:57 -0800 | [diff] [blame] | 469 | dest_br_dev = SFE_DEV_MASTER(dest_dev); |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 470 | if (!dest_br_dev) { |
| 471 | DEBUG_TRACE("no bridge found for: %s\n", dest_dev->name); |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 472 | goto done3; |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | dev_hold(dest_br_dev); |
| 476 | dest_dev = dest_br_dev; |
| 477 | } |
| 478 | #endif |
| 479 | |
| 480 | sic.src_dev = src_dev; |
| 481 | sic.dest_dev = dest_dev; |
| 482 | |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 483 | sic.src_mtu = src_dev->mtu; |
| 484 | sic.dest_mtu = dest_dev->mtu; |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 485 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 486 | if (likely(is_v4)) { |
| 487 | sfe_ipv4_create_rule(&sic); |
| 488 | } else { |
| 489 | sfe_ipv6_create_rule(&sic); |
| 490 | } |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 491 | |
| 492 | /* |
| 493 | * If we had bridge ports then release them too. |
| 494 | */ |
| 495 | if (dest_br_dev) { |
| 496 | dev_put(dest_br_dev); |
| 497 | } |
| 498 | |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 499 | done3: |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 500 | if (src_br_dev) { |
| 501 | dev_put(src_br_dev); |
| 502 | } |
| 503 | |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 504 | done2: |
| 505 | dev_put(dest_dev); |
| 506 | |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 507 | done1: |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 508 | dev_put(src_dev); |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 509 | |
| 510 | return NF_ACCEPT; |
| 511 | } |
| 512 | |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 513 | /* |
| 514 | * sfe_cm_ipv4_post_routing_hook() |
| 515 | * Called for packets about to leave the box - either locally generated or forwarded from another interface |
| 516 | */ |
| 517 | sfe_cm_ipv4_post_routing_hook(hooknum, ops, skb, in_unused, out, okfn) |
| 518 | { |
| 519 | return sfe_cm_post_routing(skb, true); |
| 520 | } |
| 521 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 522 | /* |
| 523 | * sfe_cm_ipv6_post_routing_hook() |
| 524 | * Called for packets about to leave the box - either locally generated or forwarded from another interface |
| 525 | */ |
| 526 | sfe_cm_ipv6_post_routing_hook(hooknum, ops, skb, in_unused, out, okfn) |
| 527 | { |
| 528 | return sfe_cm_post_routing(skb, false); |
| 529 | } |
| 530 | |
| 531 | |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 532 | #ifdef CONFIG_NF_CONNTRACK_EVENTS |
| 533 | /* |
| 534 | * sfe_cm_conntrack_event() |
| 535 | * Callback event invoked when a conntrack connection's state changes. |
| 536 | */ |
Matthew McClintock | 0680e9f | 2013-11-26 15:43:10 -0600 | [diff] [blame] | 537 | #ifdef CONFIG_NF_CONNTRACK_CHAIN_EVENTS |
| 538 | static int sfe_cm_conntrack_event(struct notifier_block *this, |
Matthew McClintock | 0f5c059 | 2014-02-12 11:17:11 -0600 | [diff] [blame] | 539 | unsigned long events, void *ptr) |
Matthew McClintock | 0680e9f | 2013-11-26 15:43:10 -0600 | [diff] [blame] | 540 | #else |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 541 | static int sfe_cm_conntrack_event(unsigned int events, struct nf_ct_event *item) |
Matthew McClintock | 0680e9f | 2013-11-26 15:43:10 -0600 | [diff] [blame] | 542 | #endif |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 543 | { |
Matthew McClintock | 0f5c059 | 2014-02-12 11:17:11 -0600 | [diff] [blame] | 544 | #ifdef CONFIG_NF_CONNTRACK_CHAIN_EVENTS |
| 545 | struct nf_ct_event *item = ptr; |
| 546 | #endif |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 547 | struct sfe_connection_destroy sid; |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 548 | struct nf_conn *ct = item->ct; |
| 549 | struct nf_conntrack_tuple orig_tuple; |
| 550 | |
| 551 | /* |
| 552 | * If we don't have a conntrack entry then we're done. |
| 553 | */ |
| 554 | if (unlikely(!ct)) { |
| 555 | DEBUG_WARN("no ct in conntrack event callback\n"); |
| 556 | return NOTIFY_DONE; |
| 557 | } |
| 558 | |
| 559 | /* |
| 560 | * If this is an untracked connection then we can't have any state either. |
| 561 | */ |
| 562 | if (unlikely(ct == &nf_conntrack_untracked)) { |
| 563 | DEBUG_TRACE("ignoring untracked conn\n"); |
| 564 | return NOTIFY_DONE; |
| 565 | } |
| 566 | |
| 567 | /* |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 568 | * We're only interested in destroy events. |
| 569 | */ |
| 570 | if (unlikely(!(events & (1 << IPCT_DESTROY)))) { |
| 571 | DEBUG_TRACE("ignoring non-destroy event\n"); |
| 572 | return NOTIFY_DONE; |
| 573 | } |
| 574 | |
| 575 | orig_tuple = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple; |
| 576 | sid.protocol = (int32_t)orig_tuple.dst.protonum; |
| 577 | |
| 578 | /* |
| 579 | * Extract information from the conntrack connection. We're only interested |
| 580 | * in nominal connection information (i.e. we're ignoring any NAT information). |
| 581 | */ |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 582 | switch (sid.protocol) { |
| 583 | case IPPROTO_TCP: |
| 584 | sid.src_port = orig_tuple.src.u.tcp.port; |
| 585 | sid.dest_port = orig_tuple.dst.u.tcp.port; |
| 586 | break; |
| 587 | |
| 588 | case IPPROTO_UDP: |
| 589 | sid.src_port = orig_tuple.src.u.udp.port; |
| 590 | sid.dest_port = orig_tuple.dst.u.udp.port; |
| 591 | break; |
| 592 | |
| 593 | default: |
| 594 | DEBUG_TRACE("unhandled protocol: %d\n", sid.protocol); |
| 595 | return NOTIFY_DONE; |
| 596 | } |
| 597 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 598 | if (likely(nf_ct_l3num(ct) == AF_INET)) { |
| 599 | sid.src_ip.ip = (__be32)orig_tuple.src.u3.ip; |
| 600 | sid.dest_ip.ip = (__be32)orig_tuple.dst.u3.ip; |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 601 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 602 | sfe_ipv4_destroy_rule(&sid); |
| 603 | } else if (likely(nf_ct_l3num(ct) == AF_INET6)) { |
| 604 | sid.src_ip.ip6[0] = *((struct sfe_ipv6_addr *)&orig_tuple.src.u3.in6); |
| 605 | sid.dest_ip.ip6[0] = *((struct sfe_ipv6_addr *)&orig_tuple.dst.u3.in6); |
| 606 | |
| 607 | sfe_ipv6_destroy_rule(&sid); |
| 608 | } else { |
| 609 | DEBUG_TRACE("ignoring non-IPv4 and non-IPv6 connection\n"); |
| 610 | } |
| 611 | |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 612 | return NOTIFY_DONE; |
| 613 | } |
| 614 | |
| 615 | /* |
| 616 | * Netfilter conntrack event system to monitor connection tracking changes |
| 617 | */ |
Matthew McClintock | 0680e9f | 2013-11-26 15:43:10 -0600 | [diff] [blame] | 618 | #ifdef CONFIG_NF_CONNTRACK_CHAIN_EVENTS |
| 619 | static struct notifier_block sfe_cm_conntrack_notifier = { |
| 620 | .notifier_call = sfe_cm_conntrack_event, |
| 621 | }; |
| 622 | #else |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 623 | static struct nf_ct_event_notifier sfe_cm_conntrack_notifier = { |
| 624 | .fcn = sfe_cm_conntrack_event, |
| 625 | }; |
| 626 | #endif |
Matthew McClintock | 0680e9f | 2013-11-26 15:43:10 -0600 | [diff] [blame] | 627 | #endif |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 628 | |
| 629 | /* |
| 630 | * Structure to establish a hook into the post routing netfilter point - this |
| 631 | * will pick up local outbound and packets going from one interface to another. |
| 632 | * |
| 633 | * Note: see include/linux/netfilter_ipv4.h for info related to priority levels. |
| 634 | * We want to examine packets after NAT translation and any ALG processing. |
| 635 | */ |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 636 | static struct nf_hook_ops sfe_cm_ops_post_routing[] __read_mostly = { |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 637 | { |
Xiaoping Fan | 3f1fe51 | 2014-11-05 12:14:57 -0800 | [diff] [blame] | 638 | .hook = __sfe_cm_ipv4_post_routing_hook, |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 639 | .owner = THIS_MODULE, |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 640 | .pf = NFPROTO_IPV4, |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 641 | .hooknum = NF_INET_POST_ROUTING, |
| 642 | .priority = NF_IP_PRI_NAT_SRC + 1, |
| 643 | }, |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 644 | #ifdef SFE_SUPPORT_IPV6 |
| 645 | { |
| 646 | .hook = __sfe_cm_ipv6_post_routing_hook, |
| 647 | .owner = THIS_MODULE, |
| 648 | .pf = NFPROTO_IPV6, |
| 649 | .hooknum = NF_INET_POST_ROUTING, |
| 650 | .priority = NF_IP6_PRI_NAT_SRC + 1, |
| 651 | }, |
| 652 | #endif |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 653 | }; |
| 654 | |
| 655 | /* |
| 656 | * sfe_cm_sync_rule() |
| 657 | * Synchronize a connection's state. |
| 658 | */ |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 659 | static void sfe_cm_sync_rule(struct sfe_connection_sync *sis) |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 660 | { |
| 661 | struct nf_conntrack_tuple_hash *h; |
| 662 | struct nf_conntrack_tuple tuple; |
| 663 | struct nf_conn *ct; |
Xiaoping Fan | 3f1fe51 | 2014-11-05 12:14:57 -0800 | [diff] [blame] | 664 | SFE_NF_CONN_ACCT(acct); |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 665 | |
| 666 | /* |
| 667 | * Create a tuple so as to be able to look up a connection |
| 668 | */ |
| 669 | memset(&tuple, 0, sizeof(tuple)); |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 670 | tuple.src.u.all = (__be16)sis->src_port; |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 671 | tuple.dst.dir = IP_CT_DIR_ORIGINAL; |
| 672 | tuple.dst.protonum = (uint8_t)sis->protocol; |
| 673 | tuple.dst.u.all = (__be16)sis->dest_port; |
| 674 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 675 | if (sis->is_v6) { |
| 676 | tuple.src.u3.in6 = *((struct in6_addr *)sis->src_ip.ip6); |
| 677 | tuple.dst.u3.in6 = *((struct in6_addr *)sis->dest_ip.ip6); |
| 678 | tuple.src.l3num = AF_INET6; |
| 679 | |
| 680 | DEBUG_TRACE("update connection - p: %d, s: %pI6:%u, d: %pI6:%u\n", |
| 681 | (int)tuple.dst.protonum, |
| 682 | &tuple.src.u3.in6, (unsigned int)ntohs(tuple.src.u.all), |
| 683 | &tuple.dst.u3.in6, (unsigned int)ntohs(tuple.dst.u.all)); |
| 684 | } else { |
| 685 | tuple.src.u3.ip = sis->src_ip.ip; |
| 686 | tuple.dst.u3.ip = sis->dest_ip.ip; |
| 687 | tuple.src.l3num = AF_INET; |
| 688 | |
| 689 | DEBUG_TRACE("update connection - p: %d, s: %pI4:%u, d: %pI4:%u\n", |
| 690 | (int)tuple.dst.protonum, |
| 691 | &tuple.src.u3.ip, (unsigned int)ntohs(tuple.src.u.all), |
| 692 | &tuple.dst.u3.ip, (unsigned int)ntohs(tuple.dst.u.all)); |
| 693 | } |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 694 | |
| 695 | /* |
| 696 | * Look up conntrack connection |
| 697 | */ |
| 698 | h = nf_conntrack_find_get(&init_net, NF_CT_DEFAULT_ZONE, &tuple); |
| 699 | if (unlikely(!h)) { |
| 700 | DEBUG_TRACE("no connection found\n"); |
| 701 | return; |
| 702 | } |
| 703 | |
| 704 | ct = nf_ct_tuplehash_to_ctrack(h); |
| 705 | NF_CT_ASSERT(ct->timeout.data == (unsigned long)ct); |
| 706 | |
| 707 | /* |
| 708 | * Only update if this is not a fixed timeout |
| 709 | */ |
| 710 | if (!test_bit(IPS_FIXED_TIMEOUT_BIT, &ct->status)) { |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 711 | spin_lock_bh(&ct->lock); |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 712 | ct->timeout.expires += sis->delta_jiffies; |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 713 | spin_unlock_bh(&ct->lock); |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | acct = nf_conn_acct_find(ct); |
| 717 | if (acct) { |
| 718 | spin_lock_bh(&ct->lock); |
Xiaoping Fan | 3f1fe51 | 2014-11-05 12:14:57 -0800 | [diff] [blame] | 719 | atomic64_set(&SFE_ACCT_COUNTER(acct)[IP_CT_DIR_ORIGINAL].packets, sis->src_packet_count); |
| 720 | atomic64_set(&SFE_ACCT_COUNTER(acct)[IP_CT_DIR_ORIGINAL].bytes, sis->src_byte_count); |
| 721 | atomic64_set(&SFE_ACCT_COUNTER(acct)[IP_CT_DIR_REPLY].packets, sis->dest_packet_count); |
| 722 | atomic64_set(&SFE_ACCT_COUNTER(acct)[IP_CT_DIR_REPLY].bytes, sis->dest_byte_count); |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 723 | spin_unlock_bh(&ct->lock); |
| 724 | } |
| 725 | |
| 726 | switch (sis->protocol) { |
| 727 | case IPPROTO_TCP: |
| 728 | spin_lock_bh(&ct->lock); |
| 729 | if (ct->proto.tcp.seen[0].td_maxwin < sis->src_td_max_window) { |
| 730 | ct->proto.tcp.seen[0].td_maxwin = sis->src_td_max_window; |
| 731 | } |
| 732 | if ((int32_t)(ct->proto.tcp.seen[0].td_end - sis->src_td_end) < 0) { |
| 733 | ct->proto.tcp.seen[0].td_end = sis->src_td_end; |
| 734 | } |
| 735 | if ((int32_t)(ct->proto.tcp.seen[0].td_maxend - sis->src_td_max_end) < 0) { |
| 736 | ct->proto.tcp.seen[0].td_maxend = sis->src_td_max_end; |
| 737 | } |
| 738 | if (ct->proto.tcp.seen[1].td_maxwin < sis->dest_td_max_window) { |
| 739 | ct->proto.tcp.seen[1].td_maxwin = sis->dest_td_max_window; |
| 740 | } |
| 741 | if ((int32_t)(ct->proto.tcp.seen[1].td_end - sis->dest_td_end) < 0) { |
| 742 | ct->proto.tcp.seen[1].td_end = sis->dest_td_end; |
| 743 | } |
| 744 | if ((int32_t)(ct->proto.tcp.seen[1].td_maxend - sis->dest_td_max_end) < 0) { |
| 745 | ct->proto.tcp.seen[1].td_maxend = sis->dest_td_max_end; |
| 746 | } |
| 747 | spin_unlock_bh(&ct->lock); |
| 748 | break; |
| 749 | } |
| 750 | |
| 751 | /* |
| 752 | * Release connection |
| 753 | */ |
| 754 | nf_ct_put(ct); |
| 755 | } |
| 756 | |
| 757 | /* |
| 758 | * sfe_cm_device_event() |
| 759 | */ |
| 760 | static int sfe_cm_device_event(struct notifier_block *this, unsigned long event, void *ptr) |
| 761 | { |
| 762 | struct net_device *dev = (struct net_device *)ptr; |
| 763 | |
| 764 | switch (event) { |
| 765 | case NETDEV_DOWN: |
| 766 | if (dev) { |
| 767 | sfe_ipv4_destroy_all_rules_for_dev(dev); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 768 | sfe_ipv6_destroy_all_rules_for_dev(dev); |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 769 | } |
| 770 | break; |
| 771 | } |
| 772 | |
| 773 | return NOTIFY_DONE; |
| 774 | } |
| 775 | |
| 776 | /* |
| 777 | * sfe_cm_inet_event() |
| 778 | */ |
| 779 | static int sfe_cm_inet_event(struct notifier_block *this, unsigned long event, void *ptr) |
| 780 | { |
| 781 | struct net_device *dev = ((struct in_ifaddr *)ptr)->ifa_dev->dev; |
| 782 | return sfe_cm_device_event(this, event, dev); |
| 783 | } |
| 784 | |
| 785 | /* |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 786 | * sfe_cm_inet6_event() |
| 787 | */ |
| 788 | static int sfe_cm_inet6_event(struct notifier_block *this, unsigned long event, void *ptr) |
| 789 | { |
| 790 | struct net_device *dev = ((struct inet6_ifaddr *)ptr)->idev->dev; |
| 791 | return sfe_cm_device_event(this, event, dev); |
| 792 | } |
| 793 | |
| 794 | /* |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 795 | * sfe_cm_init() |
| 796 | */ |
| 797 | static int __init sfe_cm_init(void) |
| 798 | { |
| 799 | struct sfe_cm *sc = &__sc; |
| 800 | int result = -1; |
| 801 | |
| 802 | DEBUG_INFO("SFE CM init\n"); |
| 803 | |
| 804 | /* |
| 805 | * Create sys/sfe_cm |
| 806 | */ |
| 807 | sc->sys_sfe_cm = kobject_create_and_add("sfe_cm", NULL); |
| 808 | if (!sc->sys_sfe_cm) { |
| 809 | DEBUG_ERROR("failed to register sfe_cm\n"); |
| 810 | goto exit1; |
| 811 | } |
| 812 | |
| 813 | sc->dev_notifier.notifier_call = sfe_cm_device_event; |
| 814 | sc->dev_notifier.priority = 1; |
| 815 | register_netdevice_notifier(&sc->dev_notifier); |
| 816 | |
| 817 | sc->inet_notifier.notifier_call = sfe_cm_inet_event; |
| 818 | sc->inet_notifier.priority = 1; |
| 819 | register_inetaddr_notifier(&sc->inet_notifier); |
| 820 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 821 | sc->inet6_notifier.notifier_call = sfe_cm_inet6_event; |
| 822 | sc->inet6_notifier.priority = 1; |
| 823 | register_inet6addr_notifier(&sc->inet6_notifier); |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 824 | /* |
| 825 | * Register our netfilter hooks. |
| 826 | */ |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 827 | result = nf_register_hooks(sfe_cm_ops_post_routing, ARRAY_SIZE(sfe_cm_ops_post_routing)); |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 828 | if (result < 0) { |
| 829 | DEBUG_ERROR("can't register nf post routing hook: %d\n", result); |
Xiaoping Fan | 5917642 | 2015-05-22 15:58:10 -0700 | [diff] [blame] | 830 | goto exit2; |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | #ifdef CONFIG_NF_CONNTRACK_EVENTS |
| 834 | /* |
| 835 | * Register a notifier hook to get fast notifications of expired connections. |
| 836 | */ |
| 837 | result = nf_conntrack_register_notifier(&init_net, &sfe_cm_conntrack_notifier); |
| 838 | if (result < 0) { |
| 839 | DEBUG_ERROR("can't register nf notifier hook: %d\n", result); |
Xiaoping Fan | 5917642 | 2015-05-22 15:58:10 -0700 | [diff] [blame] | 840 | goto exit3; |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 841 | } |
| 842 | #endif |
| 843 | |
| 844 | spin_lock_init(&sc->lock); |
| 845 | |
| 846 | /* |
| 847 | * Hook the receive path in the network stack. |
| 848 | */ |
| 849 | BUG_ON(athrs_fast_nat_recv != NULL); |
| 850 | RCU_INIT_POINTER(athrs_fast_nat_recv, sfe_cm_recv); |
| 851 | |
| 852 | /* |
| 853 | * Hook the shortcut sync callback. |
| 854 | */ |
| 855 | sfe_ipv4_register_sync_rule_callback(sfe_cm_sync_rule); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 856 | sfe_ipv6_register_sync_rule_callback(sfe_cm_sync_rule); |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 857 | return 0; |
| 858 | |
| 859 | #ifdef CONFIG_NF_CONNTRACK_EVENTS |
Xiaoping Fan | 5917642 | 2015-05-22 15:58:10 -0700 | [diff] [blame] | 860 | exit3: |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 861 | #endif |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 862 | nf_unregister_hooks(sfe_cm_ops_post_routing, ARRAY_SIZE(sfe_cm_ops_post_routing)); |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 863 | |
Xiaoping Fan | 5917642 | 2015-05-22 15:58:10 -0700 | [diff] [blame] | 864 | exit2: |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 865 | unregister_inet6addr_notifier(&sc->inet6_notifier); |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 866 | unregister_inetaddr_notifier(&sc->inet_notifier); |
| 867 | unregister_netdevice_notifier(&sc->dev_notifier); |
| 868 | kobject_put(sc->sys_sfe_cm); |
| 869 | |
| 870 | exit1: |
| 871 | return result; |
| 872 | } |
| 873 | |
| 874 | /* |
| 875 | * sfe_cm_exit() |
| 876 | */ |
| 877 | static void __exit sfe_cm_exit(void) |
| 878 | { |
| 879 | struct sfe_cm *sc = &__sc; |
| 880 | |
| 881 | DEBUG_INFO("SFE CM exit\n"); |
| 882 | |
| 883 | /* |
| 884 | * Unregister our sync callback. |
| 885 | */ |
| 886 | sfe_ipv4_register_sync_rule_callback(NULL); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 887 | sfe_ipv6_register_sync_rule_callback(NULL); |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 888 | |
| 889 | /* |
| 890 | * Unregister our receive callback. |
| 891 | */ |
| 892 | RCU_INIT_POINTER(athrs_fast_nat_recv, NULL); |
| 893 | |
| 894 | /* |
| 895 | * Wait for all callbacks to complete. |
| 896 | */ |
| 897 | rcu_barrier(); |
| 898 | |
| 899 | /* |
| 900 | * Destroy all connections. |
| 901 | */ |
| 902 | sfe_ipv4_destroy_all_rules_for_dev(NULL); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 903 | sfe_ipv6_destroy_all_rules_for_dev(NULL); |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 904 | |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 905 | #ifdef CONFIG_NF_CONNTRACK_EVENTS |
| 906 | nf_conntrack_unregister_notifier(&init_net, &sfe_cm_conntrack_notifier); |
| 907 | |
| 908 | #endif |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 909 | nf_unregister_hooks(sfe_cm_ops_post_routing, ARRAY_SIZE(sfe_cm_ops_post_routing)); |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 910 | |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 911 | unregister_inet6addr_notifier(&sc->inet6_notifier); |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 912 | unregister_inetaddr_notifier(&sc->inet_notifier); |
| 913 | unregister_netdevice_notifier(&sc->dev_notifier); |
| 914 | |
| 915 | kobject_put(sc->sys_sfe_cm); |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | module_init(sfe_cm_init) |
| 919 | module_exit(sfe_cm_exit) |
| 920 | |
| 921 | MODULE_AUTHOR("Qualcomm Atheros Inc."); |
| 922 | MODULE_DESCRIPTION("Shortcut Forwarding Engine - Connection Manager"); |
Matthew McClintock | a322194 | 2014-01-16 11:44:26 -0600 | [diff] [blame] | 923 | MODULE_LICENSE("Dual BSD/GPL"); |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 924 | |