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