blob: bc1704ac6cd9f68f2b73e36443a3c8162b5f5fe1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Forwarding decision
3 * Linux ethernet bridge
4 *
5 * Authors:
6 * Lennert Buytenhek <buytenh@gnu.org>
7 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * 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 Hemminger85ca7192006-04-26 02:39:19 -070017#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/netfilter_bridge.h>
19#include "br_private.h"
20
Stephen Hemminger9ef513b2006-05-25 15:58:54 -070021/* Don't forward packets to originating port or forwarding diasabled */
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +090022static inline int should_deliver(const struct net_bridge_port *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 const struct sk_buff *skb)
24{
Fischer, Anna3982d3d2009-08-13 06:55:16 +000025 return (((p->flags & BR_HAIRPIN_MODE) || skb->dev != p->dev) &&
26 p->state == BR_STATE_FORWARDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -070027}
28
Stephen Hemminger85ca7192006-04-26 02:39:19 -070029static inline unsigned packet_length(const struct sk_buff *skb)
30{
31 return skb->len - (skb->protocol == htons(ETH_P_8021Q) ? VLAN_HLEN : 0);
32}
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034int br_dev_queue_push_xmit(struct sk_buff *skb)
35{
Herbert Xu79671682006-06-22 02:40:14 -070036 /* drop mtu oversized packets except gso */
Herbert Xu89114af2006-07-08 13:34:32 -070037 if (packet_length(skb) > skb->dev->mtu && !skb_is_gso(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 kfree_skb(skb);
39 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 /* ip_refrag calls ip_fragment, doesn't copy the MAC header. */
Stephen Hemminger3a138132006-08-26 20:28:30 -070041 if (nf_bridge_maybe_copy_header(skb))
42 kfree_skb(skb);
Stephen Hemminger07317622006-08-29 17:48:17 -070043 else {
Stephen Hemminger3a138132006-08-26 20:28:30 -070044 skb_push(skb, ETH_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Stephen Hemminger3a138132006-08-26 20:28:30 -070046 dev_queue_xmit(skb);
47 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 }
49
50 return 0;
51}
52
53int br_forward_finish(struct sk_buff *skb)
54{
Stephen Hemminger9ef513b2006-05-25 15:58:54 -070055 return NF_HOOK(PF_BRIDGE, NF_BR_POST_ROUTING, skb, NULL, skb->dev,
56 br_dev_queue_push_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Linus Torvalds1da177e2005-04-16 15:20:36 -070058}
59
60static void __br_deliver(const struct net_bridge_port *to, struct sk_buff *skb)
61{
62 skb->dev = to->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 NF_HOOK(PF_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
64 br_forward_finish);
65}
66
67static void __br_forward(const struct net_bridge_port *to, struct sk_buff *skb)
68{
69 struct net_device *indev;
70
Herbert Xu4906f992009-02-09 15:07:18 -080071 if (skb_warn_if_lro(skb)) {
72 kfree_skb(skb);
73 return;
74 }
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 indev = skb->dev;
77 skb->dev = to->dev;
Herbert Xu35fc92a2007-03-26 23:22:20 -070078 skb_forward_csum(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80 NF_HOOK(PF_BRIDGE, NF_BR_FORWARD, skb, indev, skb->dev,
81 br_forward_finish);
82}
83
84/* called with rcu_read_lock */
85void br_deliver(const struct net_bridge_port *to, struct sk_buff *skb)
86{
87 if (should_deliver(to, skb)) {
88 __br_deliver(to, skb);
89 return;
90 }
91
92 kfree_skb(skb);
93}
94
95/* called with rcu_read_lock */
96void br_forward(const struct net_bridge_port *to, struct sk_buff *skb)
97{
Herbert Xu4906f992009-02-09 15:07:18 -080098 if (should_deliver(to, skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 __br_forward(to, skb);
100 return;
101 }
102
103 kfree_skb(skb);
104}
105
106/* called under bridge lock */
Herbert Xue081e1e2007-09-16 16:20:48 -0700107static void br_flood(struct net_bridge *br, struct sk_buff *skb,
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900108 void (*__packet_hook)(const struct net_bridge_port *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 struct sk_buff *skb))
110{
111 struct net_bridge_port *p;
112 struct net_bridge_port *prev;
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 prev = NULL;
115
116 list_for_each_entry_rcu(p, &br->port_list, list) {
117 if (should_deliver(p, skb)) {
118 if (prev != NULL) {
119 struct sk_buff *skb2;
120
121 if ((skb2 = skb_clone(skb, GFP_ATOMIC)) == NULL) {
Pavel Emelyanova339f1c2008-05-21 14:13:47 -0700122 br->dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 kfree_skb(skb);
124 return;
125 }
126
127 __packet_hook(prev, skb2);
128 }
129
130 prev = p;
131 }
132 }
133
134 if (prev != NULL) {
135 __packet_hook(prev, skb);
136 return;
137 }
138
139 kfree_skb(skb);
140}
141
142
143/* called with rcu_read_lock */
Herbert Xue081e1e2007-09-16 16:20:48 -0700144void br_flood_deliver(struct net_bridge *br, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
Herbert Xue081e1e2007-09-16 16:20:48 -0700146 br_flood(br, skb, __br_deliver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
149/* called under bridge lock */
Herbert Xue081e1e2007-09-16 16:20:48 -0700150void br_flood_forward(struct net_bridge *br, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
Herbert Xue081e1e2007-09-16 16:20:48 -0700152 br_flood(br, skb, __br_forward);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}