Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Forwarding decision |
| 3 | * Linux ethernet bridge |
| 4 | * |
| 5 | * Authors: |
| 6 | * Lennert Buytenhek <buytenh@gnu.org> |
| 7 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License |
| 10 | * as published by the Free Software Foundation; either version |
| 11 | * 2 of the License, or (at your option) any later version. |
| 12 | */ |
| 13 | |
Herbert Xu | 025d89c | 2010-02-27 19:41:43 +0000 | [diff] [blame] | 14 | #include <linux/err.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/kernel.h> |
| 17 | #include <linux/netdevice.h> |
| 18 | #include <linux/skbuff.h> |
Stephen Hemminger | 85ca719 | 2006-04-26 02:39:19 -0700 | [diff] [blame] | 19 | #include <linux/if_vlan.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/netfilter_bridge.h> |
| 21 | #include "br_private.h" |
| 22 | |
David S. Miller | 87faf3c | 2010-03-16 14:37:47 -0700 | [diff] [blame] | 23 | static int deliver_clone(const struct net_bridge_port *prev, |
| 24 | struct sk_buff *skb, |
Michael Braun | 7f7708f | 2010-03-16 00:26:22 -0700 | [diff] [blame] | 25 | void (*__packet_hook)(const struct net_bridge_port *p, |
| 26 | struct sk_buff *skb)); |
| 27 | |
Stephen Hemminger | 9ef513b | 2006-05-25 15:58:54 -0700 | [diff] [blame] | 28 | /* Don't forward packets to originating port or forwarding diasabled */ |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 29 | static inline int should_deliver(const struct net_bridge_port *p, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | const struct sk_buff *skb) |
| 31 | { |
Fischer, Anna | 3982d3d | 2009-08-13 06:55:16 +0000 | [diff] [blame] | 32 | return (((p->flags & BR_HAIRPIN_MODE) || skb->dev != p->dev) && |
| 33 | p->state == BR_STATE_FORWARDING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | } |
| 35 | |
Stephen Hemminger | 85ca719 | 2006-04-26 02:39:19 -0700 | [diff] [blame] | 36 | static inline unsigned packet_length(const struct sk_buff *skb) |
| 37 | { |
| 38 | return skb->len - (skb->protocol == htons(ETH_P_8021Q) ? VLAN_HLEN : 0); |
| 39 | } |
| 40 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | int br_dev_queue_push_xmit(struct sk_buff *skb) |
| 42 | { |
Herbert Xu | 7967168 | 2006-06-22 02:40:14 -0700 | [diff] [blame] | 43 | /* drop mtu oversized packets except gso */ |
Herbert Xu | 89114af | 2006-07-08 13:34:32 -0700 | [diff] [blame] | 44 | if (packet_length(skb) > skb->dev->mtu && !skb_is_gso(skb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | kfree_skb(skb); |
| 46 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | /* ip_refrag calls ip_fragment, doesn't copy the MAC header. */ |
Stephen Hemminger | 3a13813 | 2006-08-26 20:28:30 -0700 | [diff] [blame] | 48 | if (nf_bridge_maybe_copy_header(skb)) |
| 49 | kfree_skb(skb); |
Stephen Hemminger | 0731762 | 2006-08-29 17:48:17 -0700 | [diff] [blame] | 50 | else { |
Stephen Hemminger | 3a13813 | 2006-08-26 20:28:30 -0700 | [diff] [blame] | 51 | skb_push(skb, ETH_HLEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
Stephen Hemminger | 3a13813 | 2006-08-26 20:28:30 -0700 | [diff] [blame] | 53 | dev_queue_xmit(skb); |
| 54 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | int br_forward_finish(struct sk_buff *skb) |
| 61 | { |
Stephen Hemminger | 9ef513b | 2006-05-25 15:58:54 -0700 | [diff] [blame] | 62 | return NF_HOOK(PF_BRIDGE, NF_BR_POST_ROUTING, skb, NULL, skb->dev, |
| 63 | br_dev_queue_push_xmit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | static void __br_deliver(const struct net_bridge_port *to, struct sk_buff *skb) |
| 68 | { |
| 69 | skb->dev = to->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | NF_HOOK(PF_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev, |
| 71 | br_forward_finish); |
| 72 | } |
| 73 | |
| 74 | static void __br_forward(const struct net_bridge_port *to, struct sk_buff *skb) |
| 75 | { |
| 76 | struct net_device *indev; |
| 77 | |
Herbert Xu | 4906f99 | 2009-02-09 15:07:18 -0800 | [diff] [blame] | 78 | if (skb_warn_if_lro(skb)) { |
| 79 | kfree_skb(skb); |
| 80 | return; |
| 81 | } |
| 82 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | indev = skb->dev; |
| 84 | skb->dev = to->dev; |
Herbert Xu | 35fc92a | 2007-03-26 23:22:20 -0700 | [diff] [blame] | 85 | skb_forward_csum(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
| 87 | NF_HOOK(PF_BRIDGE, NF_BR_FORWARD, skb, indev, skb->dev, |
| 88 | br_forward_finish); |
| 89 | } |
| 90 | |
| 91 | /* called with rcu_read_lock */ |
| 92 | void br_deliver(const struct net_bridge_port *to, struct sk_buff *skb) |
| 93 | { |
| 94 | if (should_deliver(to, skb)) { |
| 95 | __br_deliver(to, skb); |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | kfree_skb(skb); |
| 100 | } |
| 101 | |
| 102 | /* called with rcu_read_lock */ |
Michael Braun | 7f7708f | 2010-03-16 00:26:22 -0700 | [diff] [blame] | 103 | void br_forward(const struct net_bridge_port *to, struct sk_buff *skb, struct sk_buff *skb0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | { |
Herbert Xu | 4906f99 | 2009-02-09 15:07:18 -0800 | [diff] [blame] | 105 | if (should_deliver(to, skb)) { |
Michael Braun | 7f7708f | 2010-03-16 00:26:22 -0700 | [diff] [blame] | 106 | if (skb0) |
| 107 | deliver_clone(to, skb, __br_forward); |
| 108 | else |
| 109 | __br_forward(to, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | return; |
| 111 | } |
| 112 | |
Michael Braun | 7f7708f | 2010-03-16 00:26:22 -0700 | [diff] [blame] | 113 | if (!skb0) |
| 114 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | } |
| 116 | |
David S. Miller | 87faf3c | 2010-03-16 14:37:47 -0700 | [diff] [blame] | 117 | static int deliver_clone(const struct net_bridge_port *prev, |
| 118 | struct sk_buff *skb, |
Herbert Xu | 025d89c | 2010-02-27 19:41:43 +0000 | [diff] [blame] | 119 | void (*__packet_hook)(const struct net_bridge_port *p, |
| 120 | struct sk_buff *skb)) |
| 121 | { |
| 122 | skb = skb_clone(skb, GFP_ATOMIC); |
| 123 | if (!skb) { |
| 124 | struct net_device *dev = BR_INPUT_SKB_CB(skb)->brdev; |
| 125 | |
| 126 | dev->stats.tx_dropped++; |
| 127 | return -ENOMEM; |
| 128 | } |
| 129 | |
| 130 | __packet_hook(prev, skb); |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | static struct net_bridge_port *maybe_deliver( |
| 135 | struct net_bridge_port *prev, struct net_bridge_port *p, |
| 136 | struct sk_buff *skb, |
| 137 | void (*__packet_hook)(const struct net_bridge_port *p, |
| 138 | struct sk_buff *skb)) |
| 139 | { |
| 140 | int err; |
| 141 | |
| 142 | if (!should_deliver(p, skb)) |
| 143 | return prev; |
| 144 | |
| 145 | if (!prev) |
| 146 | goto out; |
| 147 | |
| 148 | err = deliver_clone(prev, skb, __packet_hook); |
| 149 | if (err) |
| 150 | return ERR_PTR(err); |
| 151 | |
| 152 | out: |
| 153 | return p; |
| 154 | } |
| 155 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | /* called under bridge lock */ |
Herbert Xu | e081e1e | 2007-09-16 16:20:48 -0700 | [diff] [blame] | 157 | static void br_flood(struct net_bridge *br, struct sk_buff *skb, |
Herbert Xu | b33084b | 2010-02-27 19:41:41 +0000 | [diff] [blame] | 158 | struct sk_buff *skb0, |
| 159 | void (*__packet_hook)(const struct net_bridge_port *p, |
| 160 | struct sk_buff *skb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | { |
| 162 | struct net_bridge_port *p; |
| 163 | struct net_bridge_port *prev; |
| 164 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | prev = NULL; |
| 166 | |
| 167 | list_for_each_entry_rcu(p, &br->port_list, list) { |
Herbert Xu | 025d89c | 2010-02-27 19:41:43 +0000 | [diff] [blame] | 168 | prev = maybe_deliver(prev, p, skb, __packet_hook); |
| 169 | if (IS_ERR(prev)) |
| 170 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Herbert Xu | b33084b | 2010-02-27 19:41:41 +0000 | [diff] [blame] | 173 | if (!prev) |
| 174 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | |
Herbert Xu | 025d89c | 2010-02-27 19:41:43 +0000 | [diff] [blame] | 176 | if (skb0) |
| 177 | deliver_clone(prev, skb, __packet_hook); |
| 178 | else |
| 179 | __packet_hook(prev, skb); |
Herbert Xu | b33084b | 2010-02-27 19:41:41 +0000 | [diff] [blame] | 180 | return; |
| 181 | |
| 182 | out: |
| 183 | if (!skb0) |
| 184 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | |
| 188 | /* called with rcu_read_lock */ |
Herbert Xu | e081e1e | 2007-09-16 16:20:48 -0700 | [diff] [blame] | 189 | void br_flood_deliver(struct net_bridge *br, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | { |
Herbert Xu | b33084b | 2010-02-27 19:41:41 +0000 | [diff] [blame] | 191 | br_flood(br, skb, NULL, __br_deliver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | /* called under bridge lock */ |
Herbert Xu | b33084b | 2010-02-27 19:41:41 +0000 | [diff] [blame] | 195 | void br_flood_forward(struct net_bridge *br, struct sk_buff *skb, |
| 196 | struct sk_buff *skb2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | { |
Herbert Xu | b33084b | 2010-02-27 19:41:41 +0000 | [diff] [blame] | 198 | br_flood(br, skb, skb2, __br_forward); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | } |
Herbert Xu | 5cb5e94 | 2010-02-27 19:41:46 +0000 | [diff] [blame] | 200 | |
| 201 | #ifdef CONFIG_BRIDGE_IGMP_SNOOPING |
| 202 | /* called with rcu_read_lock */ |
| 203 | static void br_multicast_flood(struct net_bridge_mdb_entry *mdst, |
| 204 | struct sk_buff *skb, struct sk_buff *skb0, |
| 205 | void (*__packet_hook)( |
| 206 | const struct net_bridge_port *p, |
| 207 | struct sk_buff *skb)) |
| 208 | { |
| 209 | struct net_device *dev = BR_INPUT_SKB_CB(skb)->brdev; |
| 210 | struct net_bridge *br = netdev_priv(dev); |
| 211 | struct net_bridge_port *port; |
| 212 | struct net_bridge_port *lport, *rport; |
| 213 | struct net_bridge_port *prev; |
| 214 | struct net_bridge_port_group *p; |
| 215 | struct hlist_node *rp; |
| 216 | |
| 217 | prev = NULL; |
| 218 | |
stephen hemminger | 168d40e | 2010-04-27 15:01:05 +0000 | [diff] [blame] | 219 | rp = rcu_dereference(br->router_list.first); |
stephen hemminger | 83f6a74 | 2010-04-27 15:01:06 +0000 | [diff] [blame^] | 220 | p = mdst ? rcu_dereference(mdst->ports) : NULL; |
Herbert Xu | 5cb5e94 | 2010-02-27 19:41:46 +0000 | [diff] [blame] | 221 | while (p || rp) { |
| 222 | lport = p ? p->port : NULL; |
| 223 | rport = rp ? hlist_entry(rp, struct net_bridge_port, rlist) : |
| 224 | NULL; |
| 225 | |
| 226 | port = (unsigned long)lport > (unsigned long)rport ? |
| 227 | lport : rport; |
| 228 | |
| 229 | prev = maybe_deliver(prev, port, skb, __packet_hook); |
| 230 | if (IS_ERR(prev)) |
| 231 | goto out; |
| 232 | |
| 233 | if ((unsigned long)lport >= (unsigned long)port) |
stephen hemminger | 83f6a74 | 2010-04-27 15:01:06 +0000 | [diff] [blame^] | 234 | p = rcu_dereference(p->next); |
Herbert Xu | 5cb5e94 | 2010-02-27 19:41:46 +0000 | [diff] [blame] | 235 | if ((unsigned long)rport >= (unsigned long)port) |
stephen hemminger | 168d40e | 2010-04-27 15:01:05 +0000 | [diff] [blame] | 236 | rp = rcu_dereference(rp->next); |
Herbert Xu | 5cb5e94 | 2010-02-27 19:41:46 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | if (!prev) |
| 240 | goto out; |
| 241 | |
| 242 | if (skb0) |
| 243 | deliver_clone(prev, skb, __packet_hook); |
| 244 | else |
| 245 | __packet_hook(prev, skb); |
| 246 | return; |
| 247 | |
| 248 | out: |
| 249 | if (!skb0) |
| 250 | kfree_skb(skb); |
| 251 | } |
| 252 | |
| 253 | /* called with rcu_read_lock */ |
| 254 | void br_multicast_deliver(struct net_bridge_mdb_entry *mdst, |
| 255 | struct sk_buff *skb) |
| 256 | { |
| 257 | br_multicast_flood(mdst, skb, NULL, __br_deliver); |
| 258 | } |
| 259 | |
| 260 | /* called with rcu_read_lock */ |
| 261 | void br_multicast_forward(struct net_bridge_mdb_entry *mdst, |
| 262 | struct sk_buff *skb, struct sk_buff *skb2) |
| 263 | { |
| 264 | br_multicast_flood(mdst, skb, skb2, __br_forward); |
| 265 | } |
| 266 | #endif |