Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Handle firewalling |
| 3 | * Linux ethernet bridge |
| 4 | * |
| 5 | * Authors: |
| 6 | * Lennert Buytenhek <buytenh@gnu.org> |
| 7 | * Bart De Schuymer (maintainer) <bdschuym@pandora.be> |
| 8 | * |
| 9 | * Changes: |
| 10 | * Apr 29 2003: physdev module support (bdschuym) |
| 11 | * Jun 19 2003: let arptables see bridged ARP traffic (bdschuym) |
| 12 | * Oct 06 2003: filter encapsulated IP/ARP VLAN traffic on untagged bridge |
| 13 | * (bdschuym) |
| 14 | * Sep 01 2004: add IPv6 filtering (bdschuym) |
| 15 | * |
| 16 | * This program is free software; you can redistribute it and/or |
| 17 | * modify it under the terms of the GNU General Public License |
| 18 | * as published by the Free Software Foundation; either version |
| 19 | * 2 of the License, or (at your option) any later version. |
| 20 | * |
| 21 | * Lennert dedicates this file to Kerstin Wurdinger. |
| 22 | */ |
| 23 | |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/kernel.h> |
| 26 | #include <linux/ip.h> |
| 27 | #include <linux/netdevice.h> |
| 28 | #include <linux/skbuff.h> |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 29 | #include <linux/if_arp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <linux/if_ether.h> |
| 31 | #include <linux/if_vlan.h> |
| 32 | #include <linux/netfilter_bridge.h> |
| 33 | #include <linux/netfilter_ipv4.h> |
| 34 | #include <linux/netfilter_ipv6.h> |
| 35 | #include <linux/netfilter_arp.h> |
| 36 | #include <linux/in_route.h> |
Bart De Schuymer | f216f08 | 2006-12-05 13:45:21 -0800 | [diff] [blame] | 37 | #include <linux/inetdevice.h> |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 38 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <net/ip.h> |
| 40 | #include <net/ipv6.h> |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 41 | #include <net/route.h> |
| 42 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include <asm/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include "br_private.h" |
| 45 | #ifdef CONFIG_SYSCTL |
| 46 | #include <linux/sysctl.h> |
| 47 | #endif |
| 48 | |
| 49 | #define skb_origaddr(skb) (((struct bridge_skb_cb *) \ |
| 50 | (skb->nf_bridge->data))->daddr.ipv4) |
| 51 | #define store_orig_dstaddr(skb) (skb_origaddr(skb) = (skb)->nh.iph->daddr) |
| 52 | #define dnat_took_place(skb) (skb_origaddr(skb) != (skb)->nh.iph->daddr) |
| 53 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | #ifdef CONFIG_SYSCTL |
| 55 | static struct ctl_table_header *brnf_sysctl_header; |
Brian Haley | 9c1ea14 | 2006-09-18 00:03:41 -0700 | [diff] [blame] | 56 | static int brnf_call_iptables __read_mostly = 1; |
| 57 | static int brnf_call_ip6tables __read_mostly = 1; |
| 58 | static int brnf_call_arptables __read_mostly = 1; |
| 59 | static int brnf_filter_vlan_tagged __read_mostly = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | #else |
| 61 | #define brnf_filter_vlan_tagged 1 |
| 62 | #endif |
| 63 | |
Stephen Hemminger | 8b42ec3 | 2006-03-20 22:58:05 -0800 | [diff] [blame] | 64 | static __be16 inline vlan_proto(const struct sk_buff *skb) |
| 65 | { |
| 66 | return vlan_eth_hdr(skb)->h_vlan_encapsulated_proto; |
| 67 | } |
| 68 | |
| 69 | #define IS_VLAN_IP(skb) \ |
| 70 | (skb->protocol == htons(ETH_P_8021Q) && \ |
| 71 | vlan_proto(skb) == htons(ETH_P_IP) && \ |
| 72 | brnf_filter_vlan_tagged) |
| 73 | |
| 74 | #define IS_VLAN_IPV6(skb) \ |
| 75 | (skb->protocol == htons(ETH_P_8021Q) && \ |
| 76 | vlan_proto(skb) == htons(ETH_P_IPV6) &&\ |
| 77 | brnf_filter_vlan_tagged) |
| 78 | |
| 79 | #define IS_VLAN_ARP(skb) \ |
| 80 | (skb->protocol == htons(ETH_P_8021Q) && \ |
| 81 | vlan_proto(skb) == htons(ETH_P_ARP) && \ |
| 82 | brnf_filter_vlan_tagged) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
| 84 | /* We need these fake structures to make netfilter happy -- |
| 85 | * lots of places assume that skb->dst != NULL, which isn't |
| 86 | * all that unreasonable. |
| 87 | * |
| 88 | * Currently, we fill in the PMTU entry because netfilter |
| 89 | * refragmentation needs it, and the rt_flags entry because |
| 90 | * ipt_REJECT needs it. Future netfilter modules might |
| 91 | * require us to fill additional fields. */ |
| 92 | static struct net_device __fake_net_device = { |
| 93 | .hard_header_len = ETH_HLEN |
| 94 | }; |
| 95 | |
| 96 | static struct rtable __fake_rtable = { |
| 97 | .u = { |
| 98 | .dst = { |
| 99 | .__refcnt = ATOMIC_INIT(1), |
| 100 | .dev = &__fake_net_device, |
| 101 | .path = &__fake_rtable.u.dst, |
| 102 | .metrics = {[RTAX_MTU - 1] = 1500}, |
Patrick McHardy | 42cf93c | 2006-02-21 13:37:35 -0800 | [diff] [blame] | 103 | .flags = DST_NOXFRM, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | } |
| 105 | }, |
| 106 | .rt_flags = 0, |
| 107 | }; |
| 108 | |
Stephen Hemminger | 5dce971 | 2006-02-09 17:09:38 -0800 | [diff] [blame] | 109 | static inline struct net_device *bridge_parent(const struct net_device *dev) |
| 110 | { |
| 111 | struct net_bridge_port *port = rcu_dereference(dev->br_port); |
| 112 | |
| 113 | return port ? port->br->dev : NULL; |
| 114 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
Stephen Hemminger | fdeabde | 2006-03-20 22:58:21 -0800 | [diff] [blame] | 116 | static inline struct nf_bridge_info *nf_bridge_alloc(struct sk_buff *skb) |
| 117 | { |
| 118 | skb->nf_bridge = kzalloc(sizeof(struct nf_bridge_info), GFP_ATOMIC); |
| 119 | if (likely(skb->nf_bridge)) |
| 120 | atomic_set(&(skb->nf_bridge->use), 1); |
| 121 | |
| 122 | return skb->nf_bridge; |
| 123 | } |
| 124 | |
| 125 | static inline void nf_bridge_save_header(struct sk_buff *skb) |
| 126 | { |
Stephen Hemminger | 07317621 | 2006-08-29 17:48:17 -0700 | [diff] [blame] | 127 | int header_size = ETH_HLEN; |
Stephen Hemminger | fdeabde | 2006-03-20 22:58:21 -0800 | [diff] [blame] | 128 | |
| 129 | if (skb->protocol == htons(ETH_P_8021Q)) |
Stephen Hemminger | 07317621 | 2006-08-29 17:48:17 -0700 | [diff] [blame] | 130 | header_size += VLAN_HLEN; |
Stephen Hemminger | fdeabde | 2006-03-20 22:58:21 -0800 | [diff] [blame] | 131 | |
| 132 | memcpy(skb->nf_bridge->data, skb->data - header_size, header_size); |
| 133 | } |
| 134 | |
Stephen Hemminger | 07317621 | 2006-08-29 17:48:17 -0700 | [diff] [blame] | 135 | /* |
| 136 | * When forwarding bridge frames, we save a copy of the original |
| 137 | * header before processing. |
| 138 | */ |
| 139 | int nf_bridge_copy_header(struct sk_buff *skb) |
| 140 | { |
| 141 | int err; |
| 142 | int header_size = ETH_HLEN; |
| 143 | |
| 144 | if (skb->protocol == htons(ETH_P_8021Q)) |
| 145 | header_size += VLAN_HLEN; |
| 146 | |
| 147 | err = skb_cow(skb, header_size); |
| 148 | if (err) |
| 149 | return err; |
| 150 | |
| 151 | memcpy(skb->data - header_size, skb->nf_bridge->data, header_size); |
| 152 | |
| 153 | if (skb->protocol == htons(ETH_P_8021Q)) |
| 154 | __skb_push(skb, VLAN_HLEN); |
| 155 | return 0; |
| 156 | } |
| 157 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | /* PF_BRIDGE/PRE_ROUTING *********************************************/ |
| 159 | /* Undo the changes made for ip6tables PREROUTING and continue the |
| 160 | * bridge PRE_ROUTING hook. */ |
| 161 | static int br_nf_pre_routing_finish_ipv6(struct sk_buff *skb) |
| 162 | { |
| 163 | struct nf_bridge_info *nf_bridge = skb->nf_bridge; |
| 164 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | if (nf_bridge->mask & BRNF_PKT_TYPE) { |
| 166 | skb->pkt_type = PACKET_OTHERHOST; |
| 167 | nf_bridge->mask ^= BRNF_PKT_TYPE; |
| 168 | } |
| 169 | nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING; |
| 170 | |
| 171 | skb->dst = (struct dst_entry *)&__fake_rtable; |
| 172 | dst_hold(skb->dst); |
| 173 | |
| 174 | skb->dev = nf_bridge->physindev; |
Stephen Hemminger | f8a2602 | 2006-03-20 22:57:46 -0800 | [diff] [blame] | 175 | if (skb->protocol == htons(ETH_P_8021Q)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | skb_push(skb, VLAN_HLEN); |
| 177 | skb->nh.raw -= VLAN_HLEN; |
| 178 | } |
| 179 | NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL, |
| 180 | br_handle_frame_finish, 1); |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | static void __br_dnat_complain(void) |
| 186 | { |
| 187 | static unsigned long last_complaint; |
| 188 | |
| 189 | if (jiffies - last_complaint >= 5 * HZ) { |
| 190 | printk(KERN_WARNING "Performing cross-bridge DNAT requires IP " |
Stephen Hemminger | 789bc3e | 2006-03-20 22:57:32 -0800 | [diff] [blame] | 191 | "forwarding to be enabled\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | last_complaint = jiffies; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | /* This requires some explaining. If DNAT has taken place, |
| 197 | * we will need to fix up the destination Ethernet address, |
| 198 | * and this is a tricky process. |
| 199 | * |
| 200 | * There are two cases to consider: |
| 201 | * 1. The packet was DNAT'ed to a device in the same bridge |
| 202 | * port group as it was received on. We can still bridge |
| 203 | * the packet. |
| 204 | * 2. The packet was DNAT'ed to a different device, either |
| 205 | * a non-bridged device or another bridge port group. |
| 206 | * The packet will need to be routed. |
| 207 | * |
| 208 | * The correct way of distinguishing between these two cases is to |
| 209 | * call ip_route_input() and to look at skb->dst->dev, which is |
| 210 | * changed to the destination device if ip_route_input() succeeds. |
| 211 | * |
| 212 | * Let us first consider the case that ip_route_input() succeeds: |
| 213 | * |
| 214 | * If skb->dst->dev equals the logical bridge device the packet |
| 215 | * came in on, we can consider this bridging. We then call |
| 216 | * skb->dst->output() which will make the packet enter br_nf_local_out() |
| 217 | * not much later. In that function it is assured that the iptables |
| 218 | * FORWARD chain is traversed for the packet. |
| 219 | * |
| 220 | * Otherwise, the packet is considered to be routed and we just |
| 221 | * change the destination MAC address so that the packet will |
Bart De Schuymer | f216f08 | 2006-12-05 13:45:21 -0800 | [diff] [blame] | 222 | * later be passed up to the IP stack to be routed. For a redirected |
| 223 | * packet, ip_route_input() will give back the localhost as output device, |
| 224 | * which differs from the bridge device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | * |
| 226 | * Let us now consider the case that ip_route_input() fails: |
| 227 | * |
Bart De Schuymer | f216f08 | 2006-12-05 13:45:21 -0800 | [diff] [blame] | 228 | * This can be because the destination address is martian, in which case |
| 229 | * the packet will be dropped. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | * After a "echo '0' > /proc/sys/net/ipv4/ip_forward" ip_route_input() |
| 231 | * will fail, while __ip_route_output_key() will return success. The source |
| 232 | * address for __ip_route_output_key() is set to zero, so __ip_route_output_key |
| 233 | * thinks we're handling a locally generated packet and won't care |
| 234 | * if IP forwarding is allowed. We send a warning message to the users's |
| 235 | * log telling her to put IP forwarding on. |
| 236 | * |
| 237 | * ip_route_input() will also fail if there is no route available. |
| 238 | * In that case we just drop the packet. |
| 239 | * |
| 240 | * --Lennert, 20020411 |
| 241 | * --Bart, 20020416 (updated) |
Bart De Schuymer | f216f08 | 2006-12-05 13:45:21 -0800 | [diff] [blame] | 242 | * --Bart, 20021007 (updated) |
| 243 | * --Bart, 20062711 (updated) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | static int br_nf_pre_routing_finish_bridge(struct sk_buff *skb) |
| 245 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | if (skb->pkt_type == PACKET_OTHERHOST) { |
| 247 | skb->pkt_type = PACKET_HOST; |
| 248 | skb->nf_bridge->mask |= BRNF_PKT_TYPE; |
| 249 | } |
| 250 | skb->nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING; |
| 251 | |
| 252 | skb->dev = bridge_parent(skb->dev); |
Stephen Hemminger | 5dce971 | 2006-02-09 17:09:38 -0800 | [diff] [blame] | 253 | if (!skb->dev) |
| 254 | kfree_skb(skb); |
| 255 | else { |
Stephen Hemminger | f8a2602 | 2006-03-20 22:57:46 -0800 | [diff] [blame] | 256 | if (skb->protocol == htons(ETH_P_8021Q)) { |
Stephen Hemminger | 5dce971 | 2006-02-09 17:09:38 -0800 | [diff] [blame] | 257 | skb_pull(skb, VLAN_HLEN); |
| 258 | skb->nh.raw += VLAN_HLEN; |
| 259 | } |
| 260 | skb->dst->output(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | static int br_nf_pre_routing_finish(struct sk_buff *skb) |
| 266 | { |
| 267 | struct net_device *dev = skb->dev; |
| 268 | struct iphdr *iph = skb->nh.iph; |
| 269 | struct nf_bridge_info *nf_bridge = skb->nf_bridge; |
Bart De Schuymer | f216f08 | 2006-12-05 13:45:21 -0800 | [diff] [blame] | 270 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | if (nf_bridge->mask & BRNF_PKT_TYPE) { |
| 273 | skb->pkt_type = PACKET_OTHERHOST; |
| 274 | nf_bridge->mask ^= BRNF_PKT_TYPE; |
| 275 | } |
| 276 | nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | if (dnat_took_place(skb)) { |
Bart De Schuymer | f216f08 | 2006-12-05 13:45:21 -0800 | [diff] [blame] | 278 | if ((err = ip_route_input(skb, iph->daddr, iph->saddr, iph->tos, dev))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | struct rtable *rt; |
Stephen Hemminger | 789bc3e | 2006-03-20 22:57:32 -0800 | [diff] [blame] | 280 | struct flowi fl = { |
| 281 | .nl_u = { |
| 282 | .ip4_u = { |
| 283 | .daddr = iph->daddr, |
| 284 | .saddr = 0, |
| 285 | .tos = RT_TOS(iph->tos) }, |
| 286 | }, |
| 287 | .proto = 0, |
| 288 | }; |
Bart De Schuymer | f216f08 | 2006-12-05 13:45:21 -0800 | [diff] [blame] | 289 | struct in_device *in_dev = in_dev_get(dev); |
| 290 | |
| 291 | /* If err equals -EHOSTUNREACH the error is due to a |
| 292 | * martian destination or due to the fact that |
| 293 | * forwarding is disabled. For most martian packets, |
| 294 | * ip_route_output_key() will fail. It won't fail for 2 types of |
| 295 | * martian destinations: loopback destinations and destination |
| 296 | * 0.0.0.0. In both cases the packet will be dropped because the |
| 297 | * destination is the loopback device and not the bridge. */ |
| 298 | if (err != -EHOSTUNREACH || !in_dev || IN_DEV_FORWARD(in_dev)) |
| 299 | goto free_skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | |
| 301 | if (!ip_route_output_key(&rt, &fl)) { |
Bart De Schuymer | 1c011be | 2005-09-14 20:55:16 -0700 | [diff] [blame] | 302 | /* - Bridged-and-DNAT'ed traffic doesn't |
Bart De Schuymer | f216f08 | 2006-12-05 13:45:21 -0800 | [diff] [blame] | 303 | * require ip_forwarding. */ |
| 304 | if (((struct dst_entry *)rt)->dev == dev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | skb->dst = (struct dst_entry *)rt; |
| 306 | goto bridged_dnat; |
| 307 | } |
Bart De Schuymer | f216f08 | 2006-12-05 13:45:21 -0800 | [diff] [blame] | 308 | /* we are sure that forwarding is disabled, so printing |
| 309 | * this message is no problem. Note that the packet could |
| 310 | * still have a martian destination address, in which case |
| 311 | * the packet could be dropped even if forwarding were enabled */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | __br_dnat_complain(); |
| 313 | dst_release((struct dst_entry *)rt); |
| 314 | } |
Bart De Schuymer | f216f08 | 2006-12-05 13:45:21 -0800 | [diff] [blame] | 315 | free_skb: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | kfree_skb(skb); |
| 317 | return 0; |
| 318 | } else { |
| 319 | if (skb->dst->dev == dev) { |
| 320 | bridged_dnat: |
| 321 | /* Tell br_nf_local_out this is a |
| 322 | * bridged frame */ |
| 323 | nf_bridge->mask |= BRNF_BRIDGED_DNAT; |
| 324 | skb->dev = nf_bridge->physindev; |
| 325 | if (skb->protocol == |
Stephen Hemminger | f8a2602 | 2006-03-20 22:57:46 -0800 | [diff] [blame] | 326 | htons(ETH_P_8021Q)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | skb_push(skb, VLAN_HLEN); |
| 328 | skb->nh.raw -= VLAN_HLEN; |
| 329 | } |
| 330 | NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING, |
| 331 | skb, skb->dev, NULL, |
| 332 | br_nf_pre_routing_finish_bridge, |
| 333 | 1); |
| 334 | return 0; |
| 335 | } |
Stephen Hemminger | 789bc3e | 2006-03-20 22:57:32 -0800 | [diff] [blame] | 336 | memcpy(eth_hdr(skb)->h_dest, dev->dev_addr, ETH_ALEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | skb->pkt_type = PACKET_HOST; |
| 338 | } |
| 339 | } else { |
| 340 | skb->dst = (struct dst_entry *)&__fake_rtable; |
| 341 | dst_hold(skb->dst); |
| 342 | } |
| 343 | |
| 344 | skb->dev = nf_bridge->physindev; |
Stephen Hemminger | f8a2602 | 2006-03-20 22:57:46 -0800 | [diff] [blame] | 345 | if (skb->protocol == htons(ETH_P_8021Q)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | skb_push(skb, VLAN_HLEN); |
| 347 | skb->nh.raw -= VLAN_HLEN; |
| 348 | } |
| 349 | NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL, |
| 350 | br_handle_frame_finish, 1); |
| 351 | |
| 352 | return 0; |
| 353 | } |
| 354 | |
| 355 | /* Some common code for IPv4/IPv6 */ |
Stephen Hemminger | 5dce971 | 2006-02-09 17:09:38 -0800 | [diff] [blame] | 356 | static struct net_device *setup_pre_routing(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | { |
| 358 | struct nf_bridge_info *nf_bridge = skb->nf_bridge; |
| 359 | |
| 360 | if (skb->pkt_type == PACKET_OTHERHOST) { |
| 361 | skb->pkt_type = PACKET_HOST; |
| 362 | nf_bridge->mask |= BRNF_PKT_TYPE; |
| 363 | } |
| 364 | |
| 365 | nf_bridge->mask |= BRNF_NF_BRIDGE_PREROUTING; |
| 366 | nf_bridge->physindev = skb->dev; |
| 367 | skb->dev = bridge_parent(skb->dev); |
Stephen Hemminger | 5dce971 | 2006-02-09 17:09:38 -0800 | [diff] [blame] | 368 | |
| 369 | return skb->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | /* We only check the length. A bridge shouldn't do any hop-by-hop stuff anyway */ |
| 373 | static int check_hbh_len(struct sk_buff *skb) |
| 374 | { |
Stephen Hemminger | 789bc3e | 2006-03-20 22:57:32 -0800 | [diff] [blame] | 375 | unsigned char *raw = (u8 *) (skb->nh.ipv6h + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | u32 pkt_len; |
| 377 | int off = raw - skb->nh.raw; |
Stephen Hemminger | 789bc3e | 2006-03-20 22:57:32 -0800 | [diff] [blame] | 378 | int len = (raw[1] + 1) << 3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | |
| 380 | if ((raw + len) - skb->data > skb_headlen(skb)) |
| 381 | goto bad; |
| 382 | |
| 383 | off += 2; |
| 384 | len -= 2; |
| 385 | |
| 386 | while (len > 0) { |
Stephen Hemminger | 789bc3e | 2006-03-20 22:57:32 -0800 | [diff] [blame] | 387 | int optlen = skb->nh.raw[off + 1] + 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | |
| 389 | switch (skb->nh.raw[off]) { |
| 390 | case IPV6_TLV_PAD0: |
| 391 | optlen = 1; |
| 392 | break; |
| 393 | |
| 394 | case IPV6_TLV_PADN: |
| 395 | break; |
| 396 | |
| 397 | case IPV6_TLV_JUMBO: |
Stephen Hemminger | 789bc3e | 2006-03-20 22:57:32 -0800 | [diff] [blame] | 398 | if (skb->nh.raw[off + 1] != 4 || (off & 3) != 2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | goto bad; |
Al Viro | 47c183fa | 2006-11-14 21:11:51 -0800 | [diff] [blame] | 400 | pkt_len = ntohl(*(__be32 *) (skb->nh.raw + off + 2)); |
Bart De Schuymer | b036648 | 2005-12-19 14:00:08 -0800 | [diff] [blame] | 401 | if (pkt_len <= IPV6_MAXPLEN || |
| 402 | skb->nh.ipv6h->payload_len) |
| 403 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | if (pkt_len > skb->len - sizeof(struct ipv6hdr)) |
| 405 | goto bad; |
Bart De Schuymer | b036648 | 2005-12-19 14:00:08 -0800 | [diff] [blame] | 406 | if (pskb_trim_rcsum(skb, |
Stephen Hemminger | 789bc3e | 2006-03-20 22:57:32 -0800 | [diff] [blame] | 407 | pkt_len + sizeof(struct ipv6hdr))) |
Bart De Schuymer | b036648 | 2005-12-19 14:00:08 -0800 | [diff] [blame] | 408 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | break; |
| 410 | default: |
| 411 | if (optlen > len) |
| 412 | goto bad; |
| 413 | break; |
| 414 | } |
| 415 | off += optlen; |
| 416 | len -= optlen; |
| 417 | } |
| 418 | if (len == 0) |
| 419 | return 0; |
| 420 | bad: |
| 421 | return -1; |
| 422 | |
| 423 | } |
| 424 | |
| 425 | /* Replicate the checks that IPv6 does on packet reception and pass the packet |
| 426 | * to ip6tables, which doesn't support NAT, so things are fairly simple. */ |
| 427 | static unsigned int br_nf_pre_routing_ipv6(unsigned int hook, |
Stephen Hemminger | 789bc3e | 2006-03-20 22:57:32 -0800 | [diff] [blame] | 428 | struct sk_buff *skb, |
| 429 | const struct net_device *in, |
| 430 | const struct net_device *out, |
| 431 | int (*okfn)(struct sk_buff *)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | { |
| 433 | struct ipv6hdr *hdr; |
| 434 | u32 pkt_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | |
| 436 | if (skb->len < sizeof(struct ipv6hdr)) |
| 437 | goto inhdr_error; |
| 438 | |
| 439 | if (!pskb_may_pull(skb, sizeof(struct ipv6hdr))) |
| 440 | goto inhdr_error; |
| 441 | |
| 442 | hdr = skb->nh.ipv6h; |
| 443 | |
| 444 | if (hdr->version != 6) |
| 445 | goto inhdr_error; |
| 446 | |
| 447 | pkt_len = ntohs(hdr->payload_len); |
| 448 | |
| 449 | if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) { |
| 450 | if (pkt_len + sizeof(struct ipv6hdr) > skb->len) |
| 451 | goto inhdr_error; |
Herbert Xu | b38dfee | 2006-06-09 16:13:01 -0700 | [diff] [blame] | 452 | if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr))) |
| 453 | goto inhdr_error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | } |
| 455 | if (hdr->nexthdr == NEXTHDR_HOP && check_hbh_len(skb)) |
Stephen Hemminger | 789bc3e | 2006-03-20 22:57:32 -0800 | [diff] [blame] | 456 | goto inhdr_error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | |
Stephen Hemminger | 789bc3e | 2006-03-20 22:57:32 -0800 | [diff] [blame] | 458 | nf_bridge_put(skb->nf_bridge); |
Stephen Hemminger | fdeabde | 2006-03-20 22:58:21 -0800 | [diff] [blame] | 459 | if (!nf_bridge_alloc(skb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | return NF_DROP; |
Stephen Hemminger | 5dce971 | 2006-02-09 17:09:38 -0800 | [diff] [blame] | 461 | if (!setup_pre_routing(skb)) |
| 462 | return NF_DROP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | |
| 464 | NF_HOOK(PF_INET6, NF_IP6_PRE_ROUTING, skb, skb->dev, NULL, |
| 465 | br_nf_pre_routing_finish_ipv6); |
| 466 | |
| 467 | return NF_STOLEN; |
| 468 | |
| 469 | inhdr_error: |
| 470 | return NF_DROP; |
| 471 | } |
| 472 | |
| 473 | /* Direct IPv6 traffic to br_nf_pre_routing_ipv6. |
| 474 | * Replicate the checks that IPv4 does on packet reception. |
| 475 | * Set skb->dev to the bridge device (i.e. parent of the |
| 476 | * receiving device) to make netfilter happy, the REDIRECT |
| 477 | * target in particular. Save the original destination IP |
| 478 | * address to be able to detect DNAT afterwards. */ |
| 479 | static unsigned int br_nf_pre_routing(unsigned int hook, struct sk_buff **pskb, |
Stephen Hemminger | ee02b3a | 2006-01-06 13:13:29 -0800 | [diff] [blame] | 480 | const struct net_device *in, |
| 481 | const struct net_device *out, |
| 482 | int (*okfn)(struct sk_buff *)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | { |
| 484 | struct iphdr *iph; |
| 485 | __u32 len; |
| 486 | struct sk_buff *skb = *pskb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | |
Stephen Hemminger | 8b42ec3 | 2006-03-20 22:58:05 -0800 | [diff] [blame] | 488 | if (skb->protocol == htons(ETH_P_IPV6) || IS_VLAN_IPV6(skb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | #ifdef CONFIG_SYSCTL |
| 490 | if (!brnf_call_ip6tables) |
| 491 | return NF_ACCEPT; |
| 492 | #endif |
| 493 | if ((skb = skb_share_check(*pskb, GFP_ATOMIC)) == NULL) |
| 494 | goto out; |
| 495 | |
Stephen Hemminger | f8a2602 | 2006-03-20 22:57:46 -0800 | [diff] [blame] | 496 | if (skb->protocol == htons(ETH_P_8021Q)) { |
Herbert Xu | cbb042f | 2006-03-20 22:43:56 -0800 | [diff] [blame] | 497 | skb_pull_rcsum(skb, VLAN_HLEN); |
Stephen Hemminger | ee02b3a | 2006-01-06 13:13:29 -0800 | [diff] [blame] | 498 | skb->nh.raw += VLAN_HLEN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | } |
| 500 | return br_nf_pre_routing_ipv6(hook, skb, in, out, okfn); |
| 501 | } |
| 502 | #ifdef CONFIG_SYSCTL |
| 503 | if (!brnf_call_iptables) |
| 504 | return NF_ACCEPT; |
| 505 | #endif |
| 506 | |
Stephen Hemminger | 8b42ec3 | 2006-03-20 22:58:05 -0800 | [diff] [blame] | 507 | if (skb->protocol != htons(ETH_P_IP) && !IS_VLAN_IP(skb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | return NF_ACCEPT; |
| 509 | |
| 510 | if ((skb = skb_share_check(*pskb, GFP_ATOMIC)) == NULL) |
| 511 | goto out; |
| 512 | |
Stephen Hemminger | f8a2602 | 2006-03-20 22:57:46 -0800 | [diff] [blame] | 513 | if (skb->protocol == htons(ETH_P_8021Q)) { |
Herbert Xu | cbb042f | 2006-03-20 22:43:56 -0800 | [diff] [blame] | 514 | skb_pull_rcsum(skb, VLAN_HLEN); |
Stephen Hemminger | ee02b3a | 2006-01-06 13:13:29 -0800 | [diff] [blame] | 515 | skb->nh.raw += VLAN_HLEN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | if (!pskb_may_pull(skb, sizeof(struct iphdr))) |
| 519 | goto inhdr_error; |
| 520 | |
| 521 | iph = skb->nh.iph; |
| 522 | if (iph->ihl < 5 || iph->version != 4) |
| 523 | goto inhdr_error; |
| 524 | |
Stephen Hemminger | 789bc3e | 2006-03-20 22:57:32 -0800 | [diff] [blame] | 525 | if (!pskb_may_pull(skb, 4 * iph->ihl)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | goto inhdr_error; |
| 527 | |
| 528 | iph = skb->nh.iph; |
Stephen Hemminger | 789bc3e | 2006-03-20 22:57:32 -0800 | [diff] [blame] | 529 | if (ip_fast_csum((__u8 *) iph, iph->ihl) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | goto inhdr_error; |
| 531 | |
| 532 | len = ntohs(iph->tot_len); |
Stephen Hemminger | 789bc3e | 2006-03-20 22:57:32 -0800 | [diff] [blame] | 533 | if (skb->len < len || len < 4 * iph->ihl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | goto inhdr_error; |
| 535 | |
Herbert Xu | b38dfee | 2006-06-09 16:13:01 -0700 | [diff] [blame] | 536 | pskb_trim_rcsum(skb, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | |
Stephen Hemminger | 789bc3e | 2006-03-20 22:57:32 -0800 | [diff] [blame] | 538 | nf_bridge_put(skb->nf_bridge); |
Stephen Hemminger | fdeabde | 2006-03-20 22:58:21 -0800 | [diff] [blame] | 539 | if (!nf_bridge_alloc(skb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | return NF_DROP; |
Stephen Hemminger | 5dce971 | 2006-02-09 17:09:38 -0800 | [diff] [blame] | 541 | if (!setup_pre_routing(skb)) |
| 542 | return NF_DROP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | store_orig_dstaddr(skb); |
| 544 | |
| 545 | NF_HOOK(PF_INET, NF_IP_PRE_ROUTING, skb, skb->dev, NULL, |
| 546 | br_nf_pre_routing_finish); |
| 547 | |
| 548 | return NF_STOLEN; |
| 549 | |
| 550 | inhdr_error: |
Stephen Hemminger | 789bc3e | 2006-03-20 22:57:32 -0800 | [diff] [blame] | 551 | // IP_INC_STATS_BH(IpInHdrErrors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | out: |
| 553 | return NF_DROP; |
| 554 | } |
| 555 | |
| 556 | |
| 557 | /* PF_BRIDGE/LOCAL_IN ************************************************/ |
| 558 | /* The packet is locally destined, which requires a real |
| 559 | * dst_entry, so detach the fake one. On the way up, the |
| 560 | * packet would pass through PRE_ROUTING again (which already |
| 561 | * took place when the packet entered the bridge), but we |
| 562 | * register an IPv4 PRE_ROUTING 'sabotage' hook that will |
| 563 | * prevent this from happening. */ |
| 564 | static unsigned int br_nf_local_in(unsigned int hook, struct sk_buff **pskb, |
Stephen Hemminger | 789bc3e | 2006-03-20 22:57:32 -0800 | [diff] [blame] | 565 | const struct net_device *in, |
| 566 | const struct net_device *out, |
| 567 | int (*okfn)(struct sk_buff *)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | { |
| 569 | struct sk_buff *skb = *pskb; |
| 570 | |
| 571 | if (skb->dst == (struct dst_entry *)&__fake_rtable) { |
| 572 | dst_release(skb->dst); |
| 573 | skb->dst = NULL; |
| 574 | } |
| 575 | |
| 576 | return NF_ACCEPT; |
| 577 | } |
| 578 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | /* PF_BRIDGE/FORWARD *************************************************/ |
| 580 | static int br_nf_forward_finish(struct sk_buff *skb) |
| 581 | { |
| 582 | struct nf_bridge_info *nf_bridge = skb->nf_bridge; |
| 583 | struct net_device *in; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | |
Stephen Hemminger | 8b42ec3 | 2006-03-20 22:58:05 -0800 | [diff] [blame] | 585 | if (skb->protocol != htons(ETH_P_ARP) && !IS_VLAN_ARP(skb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | in = nf_bridge->physindev; |
| 587 | if (nf_bridge->mask & BRNF_PKT_TYPE) { |
| 588 | skb->pkt_type = PACKET_OTHERHOST; |
| 589 | nf_bridge->mask ^= BRNF_PKT_TYPE; |
| 590 | } |
| 591 | } else { |
| 592 | in = *((struct net_device **)(skb->cb)); |
| 593 | } |
Stephen Hemminger | f8a2602 | 2006-03-20 22:57:46 -0800 | [diff] [blame] | 594 | if (skb->protocol == htons(ETH_P_8021Q)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | skb_push(skb, VLAN_HLEN); |
| 596 | skb->nh.raw -= VLAN_HLEN; |
| 597 | } |
| 598 | NF_HOOK_THRESH(PF_BRIDGE, NF_BR_FORWARD, skb, in, |
Stephen Hemminger | 789bc3e | 2006-03-20 22:57:32 -0800 | [diff] [blame] | 599 | skb->dev, br_forward_finish, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | return 0; |
| 601 | } |
| 602 | |
| 603 | /* This is the 'purely bridged' case. For IP, we pass the packet to |
| 604 | * netfilter with indev and outdev set to the bridge device, |
| 605 | * but we are still able to filter on the 'real' indev/outdev |
| 606 | * because of the physdev module. For ARP, indev and outdev are the |
| 607 | * bridge ports. */ |
| 608 | static unsigned int br_nf_forward_ip(unsigned int hook, struct sk_buff **pskb, |
Stephen Hemminger | 789bc3e | 2006-03-20 22:57:32 -0800 | [diff] [blame] | 609 | const struct net_device *in, |
| 610 | const struct net_device *out, |
| 611 | int (*okfn)(struct sk_buff *)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | { |
| 613 | struct sk_buff *skb = *pskb; |
| 614 | struct nf_bridge_info *nf_bridge; |
Stephen Hemminger | 5dce971 | 2006-02-09 17:09:38 -0800 | [diff] [blame] | 615 | struct net_device *parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | int pf; |
| 617 | |
| 618 | if (!skb->nf_bridge) |
| 619 | return NF_ACCEPT; |
| 620 | |
Stephen Hemminger | 5dce971 | 2006-02-09 17:09:38 -0800 | [diff] [blame] | 621 | parent = bridge_parent(out); |
| 622 | if (!parent) |
| 623 | return NF_DROP; |
| 624 | |
Stephen Hemminger | 8b42ec3 | 2006-03-20 22:58:05 -0800 | [diff] [blame] | 625 | if (skb->protocol == htons(ETH_P_IP) || IS_VLAN_IP(skb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | pf = PF_INET; |
| 627 | else |
| 628 | pf = PF_INET6; |
| 629 | |
Stephen Hemminger | f8a2602 | 2006-03-20 22:57:46 -0800 | [diff] [blame] | 630 | if (skb->protocol == htons(ETH_P_8021Q)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | skb_pull(*pskb, VLAN_HLEN); |
| 632 | (*pskb)->nh.raw += VLAN_HLEN; |
| 633 | } |
| 634 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | nf_bridge = skb->nf_bridge; |
| 636 | if (skb->pkt_type == PACKET_OTHERHOST) { |
| 637 | skb->pkt_type = PACKET_HOST; |
| 638 | nf_bridge->mask |= BRNF_PKT_TYPE; |
| 639 | } |
| 640 | |
| 641 | /* The physdev module checks on this */ |
| 642 | nf_bridge->mask |= BRNF_BRIDGED; |
| 643 | nf_bridge->physoutdev = skb->dev; |
| 644 | |
Stephen Hemminger | 5dce971 | 2006-02-09 17:09:38 -0800 | [diff] [blame] | 645 | NF_HOOK(pf, NF_IP_FORWARD, skb, bridge_parent(in), parent, |
| 646 | br_nf_forward_finish); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | |
| 648 | return NF_STOLEN; |
| 649 | } |
| 650 | |
| 651 | static unsigned int br_nf_forward_arp(unsigned int hook, struct sk_buff **pskb, |
Stephen Hemminger | 789bc3e | 2006-03-20 22:57:32 -0800 | [diff] [blame] | 652 | const struct net_device *in, |
| 653 | const struct net_device *out, |
| 654 | int (*okfn)(struct sk_buff *)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | { |
| 656 | struct sk_buff *skb = *pskb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | struct net_device **d = (struct net_device **)(skb->cb); |
| 658 | |
| 659 | #ifdef CONFIG_SYSCTL |
| 660 | if (!brnf_call_arptables) |
| 661 | return NF_ACCEPT; |
| 662 | #endif |
| 663 | |
Stephen Hemminger | f8a2602 | 2006-03-20 22:57:46 -0800 | [diff] [blame] | 664 | if (skb->protocol != htons(ETH_P_ARP)) { |
Stephen Hemminger | 8b42ec3 | 2006-03-20 22:58:05 -0800 | [diff] [blame] | 665 | if (!IS_VLAN_ARP(skb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | return NF_ACCEPT; |
| 667 | skb_pull(*pskb, VLAN_HLEN); |
| 668 | (*pskb)->nh.raw += VLAN_HLEN; |
| 669 | } |
| 670 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | if (skb->nh.arph->ar_pln != 4) { |
Stephen Hemminger | 8b42ec3 | 2006-03-20 22:58:05 -0800 | [diff] [blame] | 672 | if (IS_VLAN_ARP(skb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | skb_push(*pskb, VLAN_HLEN); |
| 674 | (*pskb)->nh.raw -= VLAN_HLEN; |
| 675 | } |
| 676 | return NF_ACCEPT; |
| 677 | } |
| 678 | *d = (struct net_device *)in; |
| 679 | NF_HOOK(NF_ARP, NF_ARP_FORWARD, skb, (struct net_device *)in, |
| 680 | (struct net_device *)out, br_nf_forward_finish); |
| 681 | |
| 682 | return NF_STOLEN; |
| 683 | } |
| 684 | |
Patrick McHardy | 2bf540b | 2006-12-13 16:54:25 -0800 | [diff] [blame^] | 685 | /* PF_BRIDGE/LOCAL_OUT *********************************************** |
| 686 | * |
| 687 | * This function sees both locally originated IP packets and forwarded |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | * IP packets (in both cases the destination device is a bridge |
| 689 | * device). It also sees bridged-and-DNAT'ed packets. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | * |
| 691 | * If (nf_bridge->mask & BRNF_BRIDGED_DNAT) then the packet is bridged |
| 692 | * and we fake the PF_BRIDGE/FORWARD hook. The function br_nf_forward() |
| 693 | * will then fake the PF_INET/FORWARD hook. br_nf_local_out() has priority |
| 694 | * NF_BR_PRI_FIRST, so no relevant PF_BRIDGE/INPUT functions have been nor |
| 695 | * will be executed. |
Patrick McHardy | 2bf540b | 2006-12-13 16:54:25 -0800 | [diff] [blame^] | 696 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | static unsigned int br_nf_local_out(unsigned int hook, struct sk_buff **pskb, |
Stephen Hemminger | 789bc3e | 2006-03-20 22:57:32 -0800 | [diff] [blame] | 698 | const struct net_device *in, |
| 699 | const struct net_device *out, |
| 700 | int (*okfn)(struct sk_buff *)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | { |
Patrick McHardy | 2bf540b | 2006-12-13 16:54:25 -0800 | [diff] [blame^] | 702 | struct net_device *realindev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | struct sk_buff *skb = *pskb; |
| 704 | struct nf_bridge_info *nf_bridge; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | |
| 706 | if (!skb->nf_bridge) |
| 707 | return NF_ACCEPT; |
| 708 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | nf_bridge = skb->nf_bridge; |
Patrick McHardy | 2bf540b | 2006-12-13 16:54:25 -0800 | [diff] [blame^] | 710 | if (!(nf_bridge->mask & BRNF_BRIDGED_DNAT)) |
| 711 | return NF_ACCEPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | |
| 713 | /* Bridged, take PF_BRIDGE/FORWARD. |
| 714 | * (see big note in front of br_nf_pre_routing_finish) */ |
Patrick McHardy | 2bf540b | 2006-12-13 16:54:25 -0800 | [diff] [blame^] | 715 | nf_bridge->physoutdev = skb->dev; |
| 716 | realindev = nf_bridge->physindev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | |
Patrick McHardy | 2bf540b | 2006-12-13 16:54:25 -0800 | [diff] [blame^] | 718 | if (nf_bridge->mask & BRNF_PKT_TYPE) { |
| 719 | skb->pkt_type = PACKET_OTHERHOST; |
| 720 | nf_bridge->mask ^= BRNF_PKT_TYPE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | } |
Stephen Hemminger | f8a2602 | 2006-03-20 22:57:46 -0800 | [diff] [blame] | 722 | if (skb->protocol == htons(ETH_P_8021Q)) { |
Patrick McHardy | 2bf540b | 2006-12-13 16:54:25 -0800 | [diff] [blame^] | 723 | skb_push(skb, VLAN_HLEN); |
| 724 | skb->nh.raw -= VLAN_HLEN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | } |
| 726 | |
Patrick McHardy | 2bf540b | 2006-12-13 16:54:25 -0800 | [diff] [blame^] | 727 | NF_HOOK(PF_BRIDGE, NF_BR_FORWARD, skb, realindev, skb->dev, |
| 728 | br_forward_finish); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | return NF_STOLEN; |
| 730 | } |
| 731 | |
Patrick McHardy | 2e2f7ae | 2006-04-04 13:42:35 -0700 | [diff] [blame] | 732 | static int br_nf_dev_queue_xmit(struct sk_buff *skb) |
| 733 | { |
| 734 | if (skb->protocol == htons(ETH_P_IP) && |
| 735 | skb->len > skb->dev->mtu && |
Herbert Xu | 89114af | 2006-07-08 13:34:32 -0700 | [diff] [blame] | 736 | !skb_is_gso(skb)) |
Patrick McHardy | 2e2f7ae | 2006-04-04 13:42:35 -0700 | [diff] [blame] | 737 | return ip_fragment(skb, br_dev_queue_push_xmit); |
| 738 | else |
| 739 | return br_dev_queue_push_xmit(skb); |
| 740 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | |
| 742 | /* PF_BRIDGE/POST_ROUTING ********************************************/ |
| 743 | static unsigned int br_nf_post_routing(unsigned int hook, struct sk_buff **pskb, |
Stephen Hemminger | 789bc3e | 2006-03-20 22:57:32 -0800 | [diff] [blame] | 744 | const struct net_device *in, |
| 745 | const struct net_device *out, |
| 746 | int (*okfn)(struct sk_buff *)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | { |
| 748 | struct sk_buff *skb = *pskb; |
| 749 | struct nf_bridge_info *nf_bridge = (*pskb)->nf_bridge; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | struct net_device *realoutdev = bridge_parent(skb->dev); |
| 751 | int pf; |
| 752 | |
| 753 | #ifdef CONFIG_NETFILTER_DEBUG |
| 754 | /* Be very paranoid. This probably won't happen anymore, but let's |
| 755 | * keep the check just to be sure... */ |
| 756 | if (skb->mac.raw < skb->head || skb->mac.raw + ETH_HLEN > skb->data) { |
| 757 | printk(KERN_CRIT "br_netfilter: Argh!! br_nf_post_routing: " |
Stephen Hemminger | 8394e9b | 2006-08-29 17:49:31 -0700 | [diff] [blame] | 758 | "bad mac.raw pointer.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | goto print_error; |
| 760 | } |
| 761 | #endif |
| 762 | |
| 763 | if (!nf_bridge) |
| 764 | return NF_ACCEPT; |
| 765 | |
Stephen Hemminger | 5dce971 | 2006-02-09 17:09:38 -0800 | [diff] [blame] | 766 | if (!realoutdev) |
| 767 | return NF_DROP; |
| 768 | |
Stephen Hemminger | 8b42ec3 | 2006-03-20 22:58:05 -0800 | [diff] [blame] | 769 | if (skb->protocol == htons(ETH_P_IP) || IS_VLAN_IP(skb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | pf = PF_INET; |
| 771 | else |
| 772 | pf = PF_INET6; |
| 773 | |
| 774 | #ifdef CONFIG_NETFILTER_DEBUG |
| 775 | if (skb->dst == NULL) { |
Stephen Hemminger | 8394e9b | 2006-08-29 17:49:31 -0700 | [diff] [blame] | 776 | printk(KERN_INFO "br_netfilter post_routing: skb->dst == NULL\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | goto print_error; |
| 778 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | #endif |
| 780 | |
| 781 | /* We assume any code from br_dev_queue_push_xmit onwards doesn't care |
| 782 | * about the value of skb->pkt_type. */ |
| 783 | if (skb->pkt_type == PACKET_OTHERHOST) { |
| 784 | skb->pkt_type = PACKET_HOST; |
| 785 | nf_bridge->mask |= BRNF_PKT_TYPE; |
| 786 | } |
| 787 | |
Stephen Hemminger | f8a2602 | 2006-03-20 22:57:46 -0800 | [diff] [blame] | 788 | if (skb->protocol == htons(ETH_P_8021Q)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | skb_pull(skb, VLAN_HLEN); |
| 790 | skb->nh.raw += VLAN_HLEN; |
| 791 | } |
| 792 | |
| 793 | nf_bridge_save_header(skb); |
| 794 | |
| 795 | #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE) |
| 796 | if (nf_bridge->netoutdev) |
| 797 | realoutdev = nf_bridge->netoutdev; |
| 798 | #endif |
| 799 | NF_HOOK(pf, NF_IP_POST_ROUTING, skb, NULL, realoutdev, |
Patrick McHardy | 2e2f7ae | 2006-04-04 13:42:35 -0700 | [diff] [blame] | 800 | br_nf_dev_queue_xmit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | |
| 802 | return NF_STOLEN; |
| 803 | |
| 804 | #ifdef CONFIG_NETFILTER_DEBUG |
| 805 | print_error: |
| 806 | if (skb->dev != NULL) { |
| 807 | printk("[%s]", skb->dev->name); |
Stephen Hemminger | 178a325 | 2006-02-13 15:43:58 -0800 | [diff] [blame] | 808 | if (realoutdev) |
| 809 | printk("[%s]", realoutdev->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | } |
| 811 | printk(" head:%p, raw:%p, data:%p\n", skb->head, skb->mac.raw, |
Stephen Hemminger | 789bc3e | 2006-03-20 22:57:32 -0800 | [diff] [blame] | 812 | skb->data); |
Stephen Hemminger | 8394e9b | 2006-08-29 17:49:31 -0700 | [diff] [blame] | 813 | dump_stack(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | return NF_ACCEPT; |
| 815 | #endif |
| 816 | } |
| 817 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | /* IP/SABOTAGE *****************************************************/ |
| 819 | /* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING |
| 820 | * for the second time. */ |
| 821 | static unsigned int ip_sabotage_in(unsigned int hook, struct sk_buff **pskb, |
Stephen Hemminger | 789bc3e | 2006-03-20 22:57:32 -0800 | [diff] [blame] | 822 | const struct net_device *in, |
| 823 | const struct net_device *out, |
| 824 | int (*okfn)(struct sk_buff *)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | { |
| 826 | if ((*pskb)->nf_bridge && |
| 827 | !((*pskb)->nf_bridge->mask & BRNF_NF_BRIDGE_PREROUTING)) { |
| 828 | return NF_STOP; |
| 829 | } |
| 830 | |
| 831 | return NF_ACCEPT; |
| 832 | } |
| 833 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | /* For br_nf_local_out we need (prio = NF_BR_PRI_FIRST), to insure that innocent |
| 835 | * PF_BRIDGE/NF_BR_LOCAL_OUT functions don't get bridged traffic as input. |
| 836 | * For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because |
| 837 | * ip_refrag() can return NF_STOLEN. */ |
| 838 | static struct nf_hook_ops br_nf_ops[] = { |
| 839 | { .hook = br_nf_pre_routing, |
| 840 | .owner = THIS_MODULE, |
| 841 | .pf = PF_BRIDGE, |
| 842 | .hooknum = NF_BR_PRE_ROUTING, |
| 843 | .priority = NF_BR_PRI_BRNF, }, |
| 844 | { .hook = br_nf_local_in, |
| 845 | .owner = THIS_MODULE, |
| 846 | .pf = PF_BRIDGE, |
| 847 | .hooknum = NF_BR_LOCAL_IN, |
| 848 | .priority = NF_BR_PRI_BRNF, }, |
| 849 | { .hook = br_nf_forward_ip, |
| 850 | .owner = THIS_MODULE, |
| 851 | .pf = PF_BRIDGE, |
| 852 | .hooknum = NF_BR_FORWARD, |
| 853 | .priority = NF_BR_PRI_BRNF - 1, }, |
| 854 | { .hook = br_nf_forward_arp, |
| 855 | .owner = THIS_MODULE, |
| 856 | .pf = PF_BRIDGE, |
| 857 | .hooknum = NF_BR_FORWARD, |
| 858 | .priority = NF_BR_PRI_BRNF, }, |
| 859 | { .hook = br_nf_local_out, |
| 860 | .owner = THIS_MODULE, |
| 861 | .pf = PF_BRIDGE, |
| 862 | .hooknum = NF_BR_LOCAL_OUT, |
| 863 | .priority = NF_BR_PRI_FIRST, }, |
| 864 | { .hook = br_nf_post_routing, |
| 865 | .owner = THIS_MODULE, |
| 866 | .pf = PF_BRIDGE, |
| 867 | .hooknum = NF_BR_POST_ROUTING, |
| 868 | .priority = NF_BR_PRI_LAST, }, |
| 869 | { .hook = ip_sabotage_in, |
| 870 | .owner = THIS_MODULE, |
| 871 | .pf = PF_INET, |
| 872 | .hooknum = NF_IP_PRE_ROUTING, |
| 873 | .priority = NF_IP_PRI_FIRST, }, |
| 874 | { .hook = ip_sabotage_in, |
| 875 | .owner = THIS_MODULE, |
| 876 | .pf = PF_INET6, |
| 877 | .hooknum = NF_IP6_PRE_ROUTING, |
| 878 | .priority = NF_IP6_PRI_FIRST, }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | }; |
| 880 | |
| 881 | #ifdef CONFIG_SYSCTL |
| 882 | static |
Stephen Hemminger | 789bc3e | 2006-03-20 22:57:32 -0800 | [diff] [blame] | 883 | int brnf_sysctl_call_tables(ctl_table * ctl, int write, struct file *filp, |
| 884 | void __user * buffer, size_t * lenp, loff_t * ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | { |
| 886 | int ret; |
| 887 | |
| 888 | ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos); |
| 889 | |
| 890 | if (write && *(int *)(ctl->data)) |
| 891 | *(int *)(ctl->data) = 1; |
| 892 | return ret; |
| 893 | } |
| 894 | |
| 895 | static ctl_table brnf_table[] = { |
| 896 | { |
| 897 | .ctl_name = NET_BRIDGE_NF_CALL_ARPTABLES, |
| 898 | .procname = "bridge-nf-call-arptables", |
| 899 | .data = &brnf_call_arptables, |
| 900 | .maxlen = sizeof(int), |
| 901 | .mode = 0644, |
| 902 | .proc_handler = &brnf_sysctl_call_tables, |
| 903 | }, |
| 904 | { |
| 905 | .ctl_name = NET_BRIDGE_NF_CALL_IPTABLES, |
| 906 | .procname = "bridge-nf-call-iptables", |
| 907 | .data = &brnf_call_iptables, |
| 908 | .maxlen = sizeof(int), |
| 909 | .mode = 0644, |
| 910 | .proc_handler = &brnf_sysctl_call_tables, |
| 911 | }, |
| 912 | { |
| 913 | .ctl_name = NET_BRIDGE_NF_CALL_IP6TABLES, |
| 914 | .procname = "bridge-nf-call-ip6tables", |
| 915 | .data = &brnf_call_ip6tables, |
| 916 | .maxlen = sizeof(int), |
| 917 | .mode = 0644, |
| 918 | .proc_handler = &brnf_sysctl_call_tables, |
| 919 | }, |
| 920 | { |
| 921 | .ctl_name = NET_BRIDGE_NF_FILTER_VLAN_TAGGED, |
| 922 | .procname = "bridge-nf-filter-vlan-tagged", |
| 923 | .data = &brnf_filter_vlan_tagged, |
| 924 | .maxlen = sizeof(int), |
| 925 | .mode = 0644, |
| 926 | .proc_handler = &brnf_sysctl_call_tables, |
| 927 | }, |
| 928 | { .ctl_name = 0 } |
| 929 | }; |
| 930 | |
| 931 | static ctl_table brnf_bridge_table[] = { |
| 932 | { |
| 933 | .ctl_name = NET_BRIDGE, |
| 934 | .procname = "bridge", |
| 935 | .mode = 0555, |
| 936 | .child = brnf_table, |
| 937 | }, |
| 938 | { .ctl_name = 0 } |
| 939 | }; |
| 940 | |
| 941 | static ctl_table brnf_net_table[] = { |
| 942 | { |
| 943 | .ctl_name = CTL_NET, |
| 944 | .procname = "net", |
| 945 | .mode = 0555, |
| 946 | .child = brnf_bridge_table, |
| 947 | }, |
| 948 | { .ctl_name = 0 } |
| 949 | }; |
| 950 | #endif |
| 951 | |
| 952 | int br_netfilter_init(void) |
| 953 | { |
| 954 | int i; |
| 955 | |
| 956 | for (i = 0; i < ARRAY_SIZE(br_nf_ops); i++) { |
| 957 | int ret; |
| 958 | |
| 959 | if ((ret = nf_register_hook(&br_nf_ops[i])) >= 0) |
| 960 | continue; |
| 961 | |
| 962 | while (i--) |
| 963 | nf_unregister_hook(&br_nf_ops[i]); |
| 964 | |
| 965 | return ret; |
| 966 | } |
| 967 | |
| 968 | #ifdef CONFIG_SYSCTL |
| 969 | brnf_sysctl_header = register_sysctl_table(brnf_net_table, 0); |
| 970 | if (brnf_sysctl_header == NULL) { |
Stephen Hemminger | 789bc3e | 2006-03-20 22:57:32 -0800 | [diff] [blame] | 971 | printk(KERN_WARNING |
| 972 | "br_netfilter: can't register to sysctl.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | for (i = 0; i < ARRAY_SIZE(br_nf_ops); i++) |
| 974 | nf_unregister_hook(&br_nf_ops[i]); |
| 975 | return -EFAULT; |
| 976 | } |
| 977 | #endif |
| 978 | |
| 979 | printk(KERN_NOTICE "Bridge firewalling registered\n"); |
| 980 | |
| 981 | return 0; |
| 982 | } |
| 983 | |
| 984 | void br_netfilter_fini(void) |
| 985 | { |
| 986 | int i; |
| 987 | |
| 988 | for (i = ARRAY_SIZE(br_nf_ops) - 1; i >= 0; i--) |
| 989 | nf_unregister_hook(&br_nf_ops[i]); |
| 990 | #ifdef CONFIG_SYSCTL |
| 991 | unregister_sysctl_table(brnf_sysctl_header); |
| 992 | #endif |
| 993 | } |