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