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 | |
Stephen Hemminger | 9ef513b | 2006-05-25 15:58:54 -0700 | [diff] [blame] | 22 | /* Don't forward packets to originating port or forwarding diasabled */ |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 23 | static inline int should_deliver(const struct net_bridge_port *p, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | const struct sk_buff *skb) |
| 25 | { |
Fischer, Anna | 3982d3d | 2009-08-13 06:55:16 +0000 | [diff] [blame] | 26 | return (((p->flags & BR_HAIRPIN_MODE) || skb->dev != p->dev) && |
| 27 | p->state == BR_STATE_FORWARDING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | } |
| 29 | |
Stephen Hemminger | 85ca719 | 2006-04-26 02:39:19 -0700 | [diff] [blame] | 30 | static inline unsigned packet_length(const struct sk_buff *skb) |
| 31 | { |
| 32 | return skb->len - (skb->protocol == htons(ETH_P_8021Q) ? VLAN_HLEN : 0); |
| 33 | } |
| 34 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | int br_dev_queue_push_xmit(struct sk_buff *skb) |
| 36 | { |
Herbert Xu | 7967168 | 2006-06-22 02:40:14 -0700 | [diff] [blame] | 37 | /* drop mtu oversized packets except gso */ |
Herbert Xu | 89114af | 2006-07-08 13:34:32 -0700 | [diff] [blame] | 38 | if (packet_length(skb) > skb->dev->mtu && !skb_is_gso(skb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | kfree_skb(skb); |
| 40 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | /* ip_refrag calls ip_fragment, doesn't copy the MAC header. */ |
Stephen Hemminger | 3a13813 | 2006-08-26 20:28:30 -0700 | [diff] [blame] | 42 | if (nf_bridge_maybe_copy_header(skb)) |
| 43 | kfree_skb(skb); |
Stephen Hemminger | 0731762 | 2006-08-29 17:48:17 -0700 | [diff] [blame] | 44 | else { |
Stephen Hemminger | 3a13813 | 2006-08-26 20:28:30 -0700 | [diff] [blame] | 45 | skb_push(skb, ETH_HLEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
Stephen Hemminger | 3a13813 | 2006-08-26 20:28:30 -0700 | [diff] [blame] | 47 | dev_queue_xmit(skb); |
| 48 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | int br_forward_finish(struct sk_buff *skb) |
| 55 | { |
Stephen Hemminger | 9ef513b | 2006-05-25 15:58:54 -0700 | [diff] [blame] | 56 | return NF_HOOK(PF_BRIDGE, NF_BR_POST_ROUTING, skb, NULL, skb->dev, |
| 57 | br_dev_queue_push_xmit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | static void __br_deliver(const struct net_bridge_port *to, struct sk_buff *skb) |
| 62 | { |
| 63 | skb->dev = to->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | NF_HOOK(PF_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev, |
| 65 | br_forward_finish); |
| 66 | } |
| 67 | |
| 68 | static void __br_forward(const struct net_bridge_port *to, struct sk_buff *skb) |
| 69 | { |
| 70 | struct net_device *indev; |
| 71 | |
Herbert Xu | 4906f99 | 2009-02-09 15:07:18 -0800 | [diff] [blame] | 72 | if (skb_warn_if_lro(skb)) { |
| 73 | kfree_skb(skb); |
| 74 | return; |
| 75 | } |
| 76 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | indev = skb->dev; |
| 78 | skb->dev = to->dev; |
Herbert Xu | 35fc92a | 2007-03-26 23:22:20 -0700 | [diff] [blame] | 79 | skb_forward_csum(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
| 81 | NF_HOOK(PF_BRIDGE, NF_BR_FORWARD, skb, indev, skb->dev, |
| 82 | br_forward_finish); |
| 83 | } |
| 84 | |
| 85 | /* called with rcu_read_lock */ |
| 86 | void br_deliver(const struct net_bridge_port *to, struct sk_buff *skb) |
| 87 | { |
| 88 | if (should_deliver(to, skb)) { |
| 89 | __br_deliver(to, skb); |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | kfree_skb(skb); |
| 94 | } |
| 95 | |
| 96 | /* called with rcu_read_lock */ |
| 97 | void br_forward(const struct net_bridge_port *to, struct sk_buff *skb) |
| 98 | { |
Herbert Xu | 4906f99 | 2009-02-09 15:07:18 -0800 | [diff] [blame] | 99 | if (should_deliver(to, skb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | __br_forward(to, skb); |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | kfree_skb(skb); |
| 105 | } |
| 106 | |
Herbert Xu | 025d89c | 2010-02-27 19:41:43 +0000 | [diff] [blame^] | 107 | static int deliver_clone(struct net_bridge_port *prev, struct sk_buff *skb, |
| 108 | void (*__packet_hook)(const struct net_bridge_port *p, |
| 109 | struct sk_buff *skb)) |
| 110 | { |
| 111 | skb = skb_clone(skb, GFP_ATOMIC); |
| 112 | if (!skb) { |
| 113 | struct net_device *dev = BR_INPUT_SKB_CB(skb)->brdev; |
| 114 | |
| 115 | dev->stats.tx_dropped++; |
| 116 | return -ENOMEM; |
| 117 | } |
| 118 | |
| 119 | __packet_hook(prev, skb); |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | static struct net_bridge_port *maybe_deliver( |
| 124 | struct net_bridge_port *prev, struct net_bridge_port *p, |
| 125 | struct sk_buff *skb, |
| 126 | void (*__packet_hook)(const struct net_bridge_port *p, |
| 127 | struct sk_buff *skb)) |
| 128 | { |
| 129 | int err; |
| 130 | |
| 131 | if (!should_deliver(p, skb)) |
| 132 | return prev; |
| 133 | |
| 134 | if (!prev) |
| 135 | goto out; |
| 136 | |
| 137 | err = deliver_clone(prev, skb, __packet_hook); |
| 138 | if (err) |
| 139 | return ERR_PTR(err); |
| 140 | |
| 141 | out: |
| 142 | return p; |
| 143 | } |
| 144 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | /* called under bridge lock */ |
Herbert Xu | e081e1e | 2007-09-16 16:20:48 -0700 | [diff] [blame] | 146 | static void br_flood(struct net_bridge *br, struct sk_buff *skb, |
Herbert Xu | b33084b | 2010-02-27 19:41:41 +0000 | [diff] [blame] | 147 | struct sk_buff *skb0, |
| 148 | void (*__packet_hook)(const struct net_bridge_port *p, |
| 149 | struct sk_buff *skb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | { |
| 151 | struct net_bridge_port *p; |
| 152 | struct net_bridge_port *prev; |
| 153 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | prev = NULL; |
| 155 | |
| 156 | list_for_each_entry_rcu(p, &br->port_list, list) { |
Herbert Xu | 025d89c | 2010-02-27 19:41:43 +0000 | [diff] [blame^] | 157 | prev = maybe_deliver(prev, p, skb, __packet_hook); |
| 158 | if (IS_ERR(prev)) |
| 159 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Herbert Xu | b33084b | 2010-02-27 19:41:41 +0000 | [diff] [blame] | 162 | if (!prev) |
| 163 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
Herbert Xu | 025d89c | 2010-02-27 19:41:43 +0000 | [diff] [blame^] | 165 | if (skb0) |
| 166 | deliver_clone(prev, skb, __packet_hook); |
| 167 | else |
| 168 | __packet_hook(prev, skb); |
Herbert Xu | b33084b | 2010-02-27 19:41:41 +0000 | [diff] [blame] | 169 | return; |
| 170 | |
| 171 | out: |
| 172 | if (!skb0) |
| 173 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | |
| 177 | /* called with rcu_read_lock */ |
Herbert Xu | e081e1e | 2007-09-16 16:20:48 -0700 | [diff] [blame] | 178 | void br_flood_deliver(struct net_bridge *br, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | { |
Herbert Xu | b33084b | 2010-02-27 19:41:41 +0000 | [diff] [blame] | 180 | br_flood(br, skb, NULL, __br_deliver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | /* called under bridge lock */ |
Herbert Xu | b33084b | 2010-02-27 19:41:41 +0000 | [diff] [blame] | 184 | void br_flood_forward(struct net_bridge *br, struct sk_buff *skb, |
| 185 | struct sk_buff *skb2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | { |
Herbert Xu | b33084b | 2010-02-27 19:41:41 +0000 | [diff] [blame] | 187 | br_flood(br, skb, skb2, __br_forward); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | } |