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 | |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/netdevice.h> |
| 16 | #include <linux/skbuff.h> |
Stephen Hemminger | 85ca719 | 2006-04-26 02:39:19 -0700 | [diff] [blame] | 17 | #include <linux/if_vlan.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/netfilter_bridge.h> |
| 19 | #include "br_private.h" |
| 20 | |
Stephen Hemminger | 9ef513b | 2006-05-25 15:58:54 -0700 | [diff] [blame] | 21 | /* Don't forward packets to originating port or forwarding diasabled */ |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 22 | static inline int should_deliver(const struct net_bridge_port *p, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | const struct sk_buff *skb) |
| 24 | { |
Stephen Hemminger | 9ef513b | 2006-05-25 15:58:54 -0700 | [diff] [blame] | 25 | return (skb->dev != p->dev && p->state == BR_STATE_FORWARDING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | } |
| 27 | |
Stephen Hemminger | 85ca719 | 2006-04-26 02:39:19 -0700 | [diff] [blame] | 28 | static inline unsigned packet_length(const struct sk_buff *skb) |
| 29 | { |
| 30 | return skb->len - (skb->protocol == htons(ETH_P_8021Q) ? VLAN_HLEN : 0); |
| 31 | } |
| 32 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | int br_dev_queue_push_xmit(struct sk_buff *skb) |
| 34 | { |
Herbert Xu | 7967168 | 2006-06-22 02:40:14 -0700 | [diff] [blame] | 35 | /* drop mtu oversized packets except gso */ |
Herbert Xu | 89114af | 2006-07-08 13:34:32 -0700 | [diff] [blame] | 36 | if (packet_length(skb) > skb->dev->mtu && !skb_is_gso(skb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | kfree_skb(skb); |
| 38 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | /* ip_refrag calls ip_fragment, doesn't copy the MAC header. */ |
Stephen Hemminger | 3a13813 | 2006-08-26 20:28:30 -0700 | [diff] [blame] | 40 | if (nf_bridge_maybe_copy_header(skb)) |
| 41 | kfree_skb(skb); |
Stephen Hemminger | 07317621 | 2006-08-29 17:48:17 -0700 | [diff] [blame] | 42 | else { |
Stephen Hemminger | 3a13813 | 2006-08-26 20:28:30 -0700 | [diff] [blame] | 43 | skb_push(skb, ETH_HLEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
Stephen Hemminger | 3a13813 | 2006-08-26 20:28:30 -0700 | [diff] [blame] | 45 | dev_queue_xmit(skb); |
| 46 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | int br_forward_finish(struct sk_buff *skb) |
| 53 | { |
Stephen Hemminger | 9ef513b | 2006-05-25 15:58:54 -0700 | [diff] [blame] | 54 | return NF_HOOK(PF_BRIDGE, NF_BR_POST_ROUTING, skb, NULL, skb->dev, |
| 55 | br_dev_queue_push_xmit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | static void __br_deliver(const struct net_bridge_port *to, struct sk_buff *skb) |
| 60 | { |
| 61 | skb->dev = to->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | NF_HOOK(PF_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev, |
| 63 | br_forward_finish); |
| 64 | } |
| 65 | |
| 66 | static void __br_forward(const struct net_bridge_port *to, struct sk_buff *skb) |
| 67 | { |
| 68 | struct net_device *indev; |
| 69 | |
| 70 | indev = skb->dev; |
| 71 | skb->dev = to->dev; |
Herbert Xu | 35fc92a | 2007-03-26 23:22:20 -0700 | [diff] [blame] | 72 | skb_forward_csum(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
| 74 | NF_HOOK(PF_BRIDGE, NF_BR_FORWARD, skb, indev, skb->dev, |
| 75 | br_forward_finish); |
| 76 | } |
| 77 | |
| 78 | /* called with rcu_read_lock */ |
| 79 | void br_deliver(const struct net_bridge_port *to, struct sk_buff *skb) |
| 80 | { |
| 81 | if (should_deliver(to, skb)) { |
| 82 | __br_deliver(to, skb); |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | kfree_skb(skb); |
| 87 | } |
| 88 | |
| 89 | /* called with rcu_read_lock */ |
| 90 | void br_forward(const struct net_bridge_port *to, struct sk_buff *skb) |
| 91 | { |
| 92 | if (should_deliver(to, skb)) { |
| 93 | __br_forward(to, skb); |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | kfree_skb(skb); |
| 98 | } |
| 99 | |
| 100 | /* called under bridge lock */ |
Herbert Xu | e081e1e | 2007-09-16 16:20:48 -0700 | [diff] [blame] | 101 | static void br_flood(struct net_bridge *br, struct sk_buff *skb, |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 102 | void (*__packet_hook)(const struct net_bridge_port *p, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | struct sk_buff *skb)) |
| 104 | { |
| 105 | struct net_bridge_port *p; |
| 106 | struct net_bridge_port *prev; |
| 107 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | prev = NULL; |
| 109 | |
| 110 | list_for_each_entry_rcu(p, &br->port_list, list) { |
| 111 | if (should_deliver(p, skb)) { |
| 112 | if (prev != NULL) { |
| 113 | struct sk_buff *skb2; |
| 114 | |
| 115 | if ((skb2 = skb_clone(skb, GFP_ATOMIC)) == NULL) { |
Pavel Emelyanov | a339f1c | 2008-05-21 14:13:47 -0700 | [diff] [blame] | 116 | br->dev->stats.tx_dropped++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | kfree_skb(skb); |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | __packet_hook(prev, skb2); |
| 122 | } |
| 123 | |
| 124 | prev = p; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | if (prev != NULL) { |
| 129 | __packet_hook(prev, skb); |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | kfree_skb(skb); |
| 134 | } |
| 135 | |
| 136 | |
| 137 | /* called with rcu_read_lock */ |
Herbert Xu | e081e1e | 2007-09-16 16:20:48 -0700 | [diff] [blame] | 138 | void br_flood_deliver(struct net_bridge *br, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | { |
Herbert Xu | e081e1e | 2007-09-16 16:20:48 -0700 | [diff] [blame] | 140 | br_flood(br, skb, __br_deliver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /* called under bridge lock */ |
Herbert Xu | e081e1e | 2007-09-16 16:20:48 -0700 | [diff] [blame] | 144 | void br_flood_forward(struct net_bridge *br, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | { |
Herbert Xu | e081e1e | 2007-09-16 16:20:48 -0700 | [diff] [blame] | 146 | br_flood(br, skb, __br_forward); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | } |