blob: d2c27c808d3bad3cb7b782f731c39345098c7df6 [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{
Stephen Hemminger9ef513b2006-05-25 15:58:54 -070025 return (skb->dev != p->dev && p->state == BR_STATE_FORWARDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -070026}
27
Stephen Hemminger85ca7192006-04-26 02:39:19 -070028static 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 Torvalds1da177e2005-04-16 15:20:36 -070033int br_dev_queue_push_xmit(struct sk_buff *skb)
34{
Herbert Xu79671682006-06-22 02:40:14 -070035 /* drop mtu oversized packets except gso */
Herbert Xu89114af2006-07-08 13:34:32 -070036 if (packet_length(skb) > skb->dev->mtu && !skb_is_gso(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 kfree_skb(skb);
38 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 /* ip_refrag calls ip_fragment, doesn't copy the MAC header. */
Stephen Hemminger3a138132006-08-26 20:28:30 -070040 if (nf_bridge_maybe_copy_header(skb))
41 kfree_skb(skb);
Stephen Hemminger07317622006-08-29 17:48:17 -070042 else {
Stephen Hemminger3a138132006-08-26 20:28:30 -070043 skb_push(skb, ETH_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Stephen Hemminger3a138132006-08-26 20:28:30 -070045 dev_queue_xmit(skb);
46 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 }
48
49 return 0;
50}
51
52int br_forward_finish(struct sk_buff *skb)
53{
Stephen Hemminger9ef513b2006-05-25 15:58:54 -070054 return NF_HOOK(PF_BRIDGE, NF_BR_POST_ROUTING, skb, NULL, skb->dev,
55 br_dev_queue_push_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057}
58
59static void __br_deliver(const struct net_bridge_port *to, struct sk_buff *skb)
60{
61 skb->dev = to->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 NF_HOOK(PF_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
63 br_forward_finish);
64}
65
66static void __br_forward(const struct net_bridge_port *to, struct sk_buff *skb)
67{
68 struct net_device *indev;
69
Herbert Xu4906f992009-02-09 15:07:18 -080070 if (skb_warn_if_lro(skb)) {
71 kfree_skb(skb);
72 return;
73 }
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 indev = skb->dev;
76 skb->dev = to->dev;
Herbert Xu35fc92a2007-03-26 23:22:20 -070077 skb_forward_csum(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79 NF_HOOK(PF_BRIDGE, NF_BR_FORWARD, skb, indev, skb->dev,
80 br_forward_finish);
81}
82
83/* called with rcu_read_lock */
84void br_deliver(const struct net_bridge_port *to, struct sk_buff *skb)
85{
86 if (should_deliver(to, skb)) {
87 __br_deliver(to, skb);
88 return;
89 }
90
91 kfree_skb(skb);
92}
93
94/* called with rcu_read_lock */
95void br_forward(const struct net_bridge_port *to, struct sk_buff *skb)
96{
Herbert Xu4906f992009-02-09 15:07:18 -080097 if (should_deliver(to, skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 __br_forward(to, skb);
99 return;
100 }
101
102 kfree_skb(skb);
103}
104
105/* called under bridge lock */
Herbert Xue081e1e2007-09-16 16:20:48 -0700106static void br_flood(struct net_bridge *br, struct sk_buff *skb,
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900107 void (*__packet_hook)(const struct net_bridge_port *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 struct sk_buff *skb))
109{
110 struct net_bridge_port *p;
111 struct net_bridge_port *prev;
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 prev = NULL;
114
115 list_for_each_entry_rcu(p, &br->port_list, list) {
116 if (should_deliver(p, skb)) {
117 if (prev != NULL) {
118 struct sk_buff *skb2;
119
120 if ((skb2 = skb_clone(skb, GFP_ATOMIC)) == NULL) {
Pavel Emelyanova339f1c2008-05-21 14:13:47 -0700121 br->dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 kfree_skb(skb);
123 return;
124 }
125
126 __packet_hook(prev, skb2);
127 }
128
129 prev = p;
130 }
131 }
132
133 if (prev != NULL) {
134 __packet_hook(prev, skb);
135 return;
136 }
137
138 kfree_skb(skb);
139}
140
141
142/* called with rcu_read_lock */
Herbert Xue081e1e2007-09-16 16:20:48 -0700143void br_flood_deliver(struct net_bridge *br, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
Herbert Xue081e1e2007-09-16 16:20:48 -0700145 br_flood(br, skb, __br_deliver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
148/* called under bridge lock */
Herbert Xue081e1e2007-09-16 16:20:48 -0700149void br_flood_forward(struct net_bridge *br, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
Herbert Xue081e1e2007-09-16 16:20:48 -0700151 br_flood(br, skb, __br_forward);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152}