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