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