bridge: Implement vlan ingress/egress policy with PVID.
[linux-3.10.git] / net / bridge / br_input.c
1 /*
2  *      Handle incoming frames
3  *      Linux ethernet bridge
4  *
5  *      Authors:
6  *      Lennert Buytenhek               <buytenh@gnu.org>
7  *
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/slab.h>
15 #include <linux/kernel.h>
16 #include <linux/netdevice.h>
17 #include <linux/etherdevice.h>
18 #include <linux/netfilter_bridge.h>
19 #include <linux/export.h>
20 #include <linux/rculist.h>
21 #include "br_private.h"
22
23 /* Hook for brouter */
24 br_should_route_hook_t __rcu *br_should_route_hook __read_mostly;
25 EXPORT_SYMBOL(br_should_route_hook);
26
27 static int br_pass_frame_up(struct sk_buff *skb)
28 {
29         struct net_device *indev, *brdev = BR_INPUT_SKB_CB(skb)->brdev;
30         struct net_bridge *br = netdev_priv(brdev);
31         struct br_cpu_netstats *brstats = this_cpu_ptr(br->stats);
32
33         u64_stats_update_begin(&brstats->syncp);
34         brstats->rx_packets++;
35         brstats->rx_bytes += skb->len;
36         u64_stats_update_end(&brstats->syncp);
37
38         /* Bridge is just like any other port.  Make sure the
39          * packet is allowed except in promisc modue when someone
40          * may be running packet capture.
41          */
42         if (!(brdev->flags & IFF_PROMISC) &&
43             !br_allowed_egress(br, br_get_vlan_info(br), skb)) {
44                 kfree_skb(skb);
45                 return NET_RX_DROP;
46         }
47
48         skb = br_handle_vlan(br, br_get_vlan_info(br), skb);
49         if (!skb)
50                 return NET_RX_DROP;
51
52         indev = skb->dev;
53         skb->dev = brdev;
54
55         return NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_IN, skb, indev, NULL,
56                        netif_receive_skb);
57 }
58
59 /* note: already called with rcu_read_lock */
60 int br_handle_frame_finish(struct sk_buff *skb)
61 {
62         const unsigned char *dest = eth_hdr(skb)->h_dest;
63         struct net_bridge_port *p = br_port_get_rcu(skb->dev);
64         struct net_bridge *br;
65         struct net_bridge_fdb_entry *dst;
66         struct net_bridge_mdb_entry *mdst;
67         struct sk_buff *skb2;
68         u16 vid = 0;
69
70         if (!p || p->state == BR_STATE_DISABLED)
71                 goto drop;
72
73         if (!br_allowed_ingress(p->br, nbp_get_vlan_info(p), skb, &vid))
74                 goto drop;
75
76         /* insert into forwarding database after filtering to avoid spoofing */
77         br = p->br;
78         br_fdb_update(br, p, eth_hdr(skb)->h_source);
79
80         if (!is_broadcast_ether_addr(dest) && is_multicast_ether_addr(dest) &&
81             br_multicast_rcv(br, p, skb))
82                 goto drop;
83
84         if (p->state == BR_STATE_LEARNING)
85                 goto drop;
86
87         BR_INPUT_SKB_CB(skb)->brdev = br->dev;
88
89         /* The packet skb2 goes to the local host (NULL to skip). */
90         skb2 = NULL;
91
92         if (br->dev->flags & IFF_PROMISC)
93                 skb2 = skb;
94
95         dst = NULL;
96
97         if (is_broadcast_ether_addr(dest))
98                 skb2 = skb;
99         else if (is_multicast_ether_addr(dest)) {
100                 mdst = br_mdb_get(br, skb);
101                 if (mdst || BR_INPUT_SKB_CB_MROUTERS_ONLY(skb)) {
102                         if ((mdst && mdst->mglist) ||
103                             br_multicast_is_router(br))
104                                 skb2 = skb;
105                         br_multicast_forward(mdst, skb, skb2);
106                         skb = NULL;
107                         if (!skb2)
108                                 goto out;
109                 } else
110                         skb2 = skb;
111
112                 br->dev->stats.multicast++;
113         } else if ((dst = __br_fdb_get(br, dest)) && dst->is_local) {
114                 skb2 = skb;
115                 /* Do not forward the packet since it's local. */
116                 skb = NULL;
117         }
118
119         if (skb) {
120                 if (dst) {
121                         dst->used = jiffies;
122                         br_forward(dst->dst, skb, skb2);
123                 } else
124                         br_flood_forward(br, skb, skb2);
125         }
126
127         if (skb2)
128                 return br_pass_frame_up(skb2);
129
130 out:
131         return 0;
132 drop:
133         kfree_skb(skb);
134         goto out;
135 }
136
137 /* note: already called with rcu_read_lock */
138 static int br_handle_local_finish(struct sk_buff *skb)
139 {
140         struct net_bridge_port *p = br_port_get_rcu(skb->dev);
141
142         br_fdb_update(p->br, p, eth_hdr(skb)->h_source);
143         return 0;        /* process further */
144 }
145
146 /*
147  * Return NULL if skb is handled
148  * note: already called with rcu_read_lock
149  */
150 rx_handler_result_t br_handle_frame(struct sk_buff **pskb)
151 {
152         struct net_bridge_port *p;
153         struct sk_buff *skb = *pskb;
154         const unsigned char *dest = eth_hdr(skb)->h_dest;
155         br_should_route_hook_t *rhook;
156
157         if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
158                 return RX_HANDLER_PASS;
159
160         if (!is_valid_ether_addr(eth_hdr(skb)->h_source))
161                 goto drop;
162
163         skb = skb_share_check(skb, GFP_ATOMIC);
164         if (!skb)
165                 return RX_HANDLER_CONSUMED;
166
167         p = br_port_get_rcu(skb->dev);
168
169         if (unlikely(is_link_local_ether_addr(dest))) {
170                 /*
171                  * See IEEE 802.1D Table 7-10 Reserved addresses
172                  *
173                  * Assignment                           Value
174                  * Bridge Group Address         01-80-C2-00-00-00
175                  * (MAC Control) 802.3          01-80-C2-00-00-01
176                  * (Link Aggregation) 802.3     01-80-C2-00-00-02
177                  * 802.1X PAE address           01-80-C2-00-00-03
178                  *
179                  * 802.1AB LLDP                 01-80-C2-00-00-0E
180                  *
181                  * Others reserved for future standardization
182                  */
183                 switch (dest[5]) {
184                 case 0x00:      /* Bridge Group Address */
185                         /* If STP is turned off,
186                            then must forward to keep loop detection */
187                         if (p->br->stp_enabled == BR_NO_STP)
188                                 goto forward;
189                         break;
190
191                 case 0x01:      /* IEEE MAC (Pause) */
192                         goto drop;
193
194                 default:
195                         /* Allow selective forwarding for most other protocols */
196                         if (p->br->group_fwd_mask & (1u << dest[5]))
197                                 goto forward;
198                 }
199
200                 /* Deliver packet to local host only */
201                 if (NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_IN, skb, skb->dev,
202                             NULL, br_handle_local_finish)) {
203                         return RX_HANDLER_CONSUMED; /* consumed by filter */
204                 } else {
205                         *pskb = skb;
206                         return RX_HANDLER_PASS; /* continue processing */
207                 }
208         }
209
210 forward:
211         switch (p->state) {
212         case BR_STATE_FORWARDING:
213                 rhook = rcu_dereference(br_should_route_hook);
214                 if (rhook) {
215                         if ((*rhook)(skb)) {
216                                 *pskb = skb;
217                                 return RX_HANDLER_PASS;
218                         }
219                         dest = eth_hdr(skb)->h_dest;
220                 }
221                 /* fall through */
222         case BR_STATE_LEARNING:
223                 if (ether_addr_equal(p->br->dev->dev_addr, dest))
224                         skb->pkt_type = PACKET_HOST;
225
226                 NF_HOOK(NFPROTO_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
227                         br_handle_frame_finish);
228                 break;
229         default:
230 drop:
231                 kfree_skb(skb);
232         }
233         return RX_HANDLER_CONSUMED;
234 }