1 /*
2 * Device handling code
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/kernel.h>
15 #include <linux/netdevice.h>
16 #include <linux/netpoll.h>
17 #include <linux/etherdevice.h>
18 #include <linux/ethtool.h>
19 #include <linux/list.h>
20 #include <linux/netfilter_bridge.h>
21
22 #include <linux/uaccess.h>
23 #include "br_private.h"
24
25 #define COMMON_FEATURES (NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA | \
26 NETIF_F_GSO_MASK | NETIF_F_HW_CSUM)
27
28 const struct nf_br_ops __rcu *nf_br_ops __read_mostly;
29 EXPORT_SYMBOL_GPL(nf_br_ops);
30
31 static struct lock_class_key bridge_netdev_addr_lock_key;
32
33 /* net device transmit always called with BH disabled */
br_dev_xmit(struct sk_buff * skb,struct net_device * dev)34 netdev_tx_t br_dev_xmit(struct sk_buff *skb, struct net_device *dev)
35 {
36 struct net_bridge *br = netdev_priv(dev);
37 struct net_bridge_fdb_entry *dst;
38 struct net_bridge_mdb_entry *mdst;
39 struct pcpu_sw_netstats *brstats = this_cpu_ptr(br->stats);
40 const struct nf_br_ops *nf_ops;
41 const unsigned char *dest;
42 u16 vid = 0;
43
44 rcu_read_lock();
45 nf_ops = rcu_dereference(nf_br_ops);
46 if (nf_ops && nf_ops->br_dev_xmit_hook(skb)) {
47 rcu_read_unlock();
48 return NETDEV_TX_OK;
49 }
50
51 u64_stats_update_begin(&brstats->syncp);
52 brstats->tx_packets++;
53 brstats->tx_bytes += skb->len;
54 u64_stats_update_end(&brstats->syncp);
55
56 br_switchdev_frame_unmark(skb);
57 BR_INPUT_SKB_CB(skb)->brdev = dev;
58
59 skb_reset_mac_header(skb);
60 skb_pull(skb, ETH_HLEN);
61
62 if (!br_allowed_ingress(br, br_vlan_group_rcu(br), skb, &vid))
63 goto out;
64
65 dest = eth_hdr(skb)->h_dest;
66 if (is_broadcast_ether_addr(dest)) {
67 br_flood(br, skb, BR_PKT_BROADCAST, false, true);
68 } else if (is_multicast_ether_addr(dest)) {
69 if (unlikely(netpoll_tx_running(dev))) {
70 br_flood(br, skb, BR_PKT_MULTICAST, false, true);
71 goto out;
72 }
73 if (br_multicast_rcv(br, NULL, skb, vid)) {
74 kfree_skb(skb);
75 goto out;
76 }
77
78 mdst = br_mdb_get(br, skb, vid);
79 if ((mdst || BR_INPUT_SKB_CB_MROUTERS_ONLY(skb)) &&
80 br_multicast_querier_exists(br, eth_hdr(skb)))
81 br_multicast_flood(mdst, skb, false, true);
82 else
83 br_flood(br, skb, BR_PKT_MULTICAST, false, true);
84 } else if ((dst = br_fdb_find_rcu(br, dest, vid)) != NULL) {
85 br_forward(dst->dst, skb, false, true);
86 } else {
87 br_flood(br, skb, BR_PKT_UNICAST, false, true);
88 }
89 out:
90 rcu_read_unlock();
91 return NETDEV_TX_OK;
92 }
93
br_set_lockdep_class(struct net_device * dev)94 static void br_set_lockdep_class(struct net_device *dev)
95 {
96 lockdep_set_class(&dev->addr_list_lock, &bridge_netdev_addr_lock_key);
97 }
98
br_dev_init(struct net_device * dev)99 static int br_dev_init(struct net_device *dev)
100 {
101 struct net_bridge *br = netdev_priv(dev);
102 int err;
103
104 br->stats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
105 if (!br->stats)
106 return -ENOMEM;
107
108 err = br_vlan_init(br);
109 if (err) {
110 free_percpu(br->stats);
111 return err;
112 }
113
114 err = br_multicast_init_stats(br);
115 if (err) {
116 free_percpu(br->stats);
117 br_vlan_flush(br);
118 }
119 br_set_lockdep_class(dev);
120
121 return err;
122 }
123
br_dev_uninit(struct net_device * dev)124 static void br_dev_uninit(struct net_device *dev)
125 {
126 struct net_bridge *br = netdev_priv(dev);
127
128 br_multicast_dev_del(br);
129 br_multicast_uninit_stats(br);
130 br_vlan_flush(br);
131 free_percpu(br->stats);
132 }
133
br_dev_open(struct net_device * dev)134 static int br_dev_open(struct net_device *dev)
135 {
136 struct net_bridge *br = netdev_priv(dev);
137
138 netdev_update_features(dev);
139 netif_start_queue(dev);
140 br_stp_enable_bridge(br);
141 br_multicast_open(br);
142
143 return 0;
144 }
145
br_dev_set_multicast_list(struct net_device * dev)146 static void br_dev_set_multicast_list(struct net_device *dev)
147 {
148 }
149
br_dev_change_rx_flags(struct net_device * dev,int change)150 static void br_dev_change_rx_flags(struct net_device *dev, int change)
151 {
152 if (change & IFF_PROMISC)
153 br_manage_promisc(netdev_priv(dev));
154 }
155
br_dev_stop(struct net_device * dev)156 static int br_dev_stop(struct net_device *dev)
157 {
158 struct net_bridge *br = netdev_priv(dev);
159
160 br_stp_disable_bridge(br);
161 br_multicast_stop(br);
162
163 netif_stop_queue(dev);
164
165 return 0;
166 }
167
br_get_stats64(struct net_device * dev,struct rtnl_link_stats64 * stats)168 static void br_get_stats64(struct net_device *dev,
169 struct rtnl_link_stats64 *stats)
170 {
171 struct net_bridge *br = netdev_priv(dev);
172 struct pcpu_sw_netstats tmp, sum = { 0 };
173 unsigned int cpu;
174
175 for_each_possible_cpu(cpu) {
176 unsigned int start;
177 const struct pcpu_sw_netstats *bstats
178 = per_cpu_ptr(br->stats, cpu);
179 do {
180 start = u64_stats_fetch_begin_irq(&bstats->syncp);
181 memcpy(&tmp, bstats, sizeof(tmp));
182 } while (u64_stats_fetch_retry_irq(&bstats->syncp, start));
183 sum.tx_bytes += tmp.tx_bytes;
184 sum.tx_packets += tmp.tx_packets;
185 sum.rx_bytes += tmp.rx_bytes;
186 sum.rx_packets += tmp.rx_packets;
187 }
188
189 stats->tx_bytes = sum.tx_bytes;
190 stats->tx_packets = sum.tx_packets;
191 stats->rx_bytes = sum.rx_bytes;
192 stats->rx_packets = sum.rx_packets;
193 }
194
br_change_mtu(struct net_device * dev,int new_mtu)195 static int br_change_mtu(struct net_device *dev, int new_mtu)
196 {
197 struct net_bridge *br = netdev_priv(dev);
198 if (new_mtu > br_min_mtu(br))
199 return -EINVAL;
200
201 dev->mtu = new_mtu;
202
203 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
204 /* remember the MTU in the rtable for PMTU */
205 dst_metric_set(&br->fake_rtable.dst, RTAX_MTU, new_mtu);
206 #endif
207
208 return 0;
209 }
210
211 /* Allow setting mac address to any valid ethernet address. */
br_set_mac_address(struct net_device * dev,void * p)212 static int br_set_mac_address(struct net_device *dev, void *p)
213 {
214 struct net_bridge *br = netdev_priv(dev);
215 struct sockaddr *addr = p;
216
217 if (!is_valid_ether_addr(addr->sa_data))
218 return -EADDRNOTAVAIL;
219
220 /* dev_set_mac_addr() can be called by a master device on bridge's
221 * NETDEV_UNREGISTER, but since it's being destroyed do nothing
222 */
223 if (dev->reg_state != NETREG_REGISTERED)
224 return -EBUSY;
225
226 spin_lock_bh(&br->lock);
227 if (!ether_addr_equal(dev->dev_addr, addr->sa_data)) {
228 /* Mac address will be changed in br_stp_change_bridge_id(). */
229 br_stp_change_bridge_id(br, addr->sa_data);
230 }
231 spin_unlock_bh(&br->lock);
232
233 return 0;
234 }
235
br_getinfo(struct net_device * dev,struct ethtool_drvinfo * info)236 static void br_getinfo(struct net_device *dev, struct ethtool_drvinfo *info)
237 {
238 strlcpy(info->driver, "bridge", sizeof(info->driver));
239 strlcpy(info->version, BR_VERSION, sizeof(info->version));
240 strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
241 strlcpy(info->bus_info, "N/A", sizeof(info->bus_info));
242 }
243
br_fix_features(struct net_device * dev,netdev_features_t features)244 static netdev_features_t br_fix_features(struct net_device *dev,
245 netdev_features_t features)
246 {
247 struct net_bridge *br = netdev_priv(dev);
248
249 return br_features_recompute(br, features);
250 }
251
252 #ifdef CONFIG_NET_POLL_CONTROLLER
br_poll_controller(struct net_device * br_dev)253 static void br_poll_controller(struct net_device *br_dev)
254 {
255 }
256
br_netpoll_cleanup(struct net_device * dev)257 static void br_netpoll_cleanup(struct net_device *dev)
258 {
259 struct net_bridge *br = netdev_priv(dev);
260 struct net_bridge_port *p;
261
262 list_for_each_entry(p, &br->port_list, list)
263 br_netpoll_disable(p);
264 }
265
__br_netpoll_enable(struct net_bridge_port * p)266 static int __br_netpoll_enable(struct net_bridge_port *p)
267 {
268 struct netpoll *np;
269 int err;
270
271 np = kzalloc(sizeof(*p->np), GFP_KERNEL);
272 if (!np)
273 return -ENOMEM;
274
275 err = __netpoll_setup(np, p->dev);
276 if (err) {
277 kfree(np);
278 return err;
279 }
280
281 p->np = np;
282 return err;
283 }
284
br_netpoll_enable(struct net_bridge_port * p)285 int br_netpoll_enable(struct net_bridge_port *p)
286 {
287 if (!p->br->dev->npinfo)
288 return 0;
289
290 return __br_netpoll_enable(p);
291 }
292
br_netpoll_setup(struct net_device * dev,struct netpoll_info * ni)293 static int br_netpoll_setup(struct net_device *dev, struct netpoll_info *ni)
294 {
295 struct net_bridge *br = netdev_priv(dev);
296 struct net_bridge_port *p;
297 int err = 0;
298
299 list_for_each_entry(p, &br->port_list, list) {
300 if (!p->dev)
301 continue;
302 err = __br_netpoll_enable(p);
303 if (err)
304 goto fail;
305 }
306
307 out:
308 return err;
309
310 fail:
311 br_netpoll_cleanup(dev);
312 goto out;
313 }
314
br_netpoll_disable(struct net_bridge_port * p)315 void br_netpoll_disable(struct net_bridge_port *p)
316 {
317 struct netpoll *np = p->np;
318
319 if (!np)
320 return;
321
322 p->np = NULL;
323
324 __netpoll_free_async(np);
325 }
326
327 #endif
328
br_add_slave(struct net_device * dev,struct net_device * slave_dev)329 static int br_add_slave(struct net_device *dev, struct net_device *slave_dev)
330
331 {
332 struct net_bridge *br = netdev_priv(dev);
333
334 return br_add_if(br, slave_dev);
335 }
336
br_del_slave(struct net_device * dev,struct net_device * slave_dev)337 static int br_del_slave(struct net_device *dev, struct net_device *slave_dev)
338 {
339 struct net_bridge *br = netdev_priv(dev);
340
341 return br_del_if(br, slave_dev);
342 }
343
344 static const struct ethtool_ops br_ethtool_ops = {
345 .get_drvinfo = br_getinfo,
346 .get_link = ethtool_op_get_link,
347 };
348
349 static const struct net_device_ops br_netdev_ops = {
350 .ndo_open = br_dev_open,
351 .ndo_stop = br_dev_stop,
352 .ndo_init = br_dev_init,
353 .ndo_uninit = br_dev_uninit,
354 .ndo_start_xmit = br_dev_xmit,
355 .ndo_get_stats64 = br_get_stats64,
356 .ndo_set_mac_address = br_set_mac_address,
357 .ndo_set_rx_mode = br_dev_set_multicast_list,
358 .ndo_change_rx_flags = br_dev_change_rx_flags,
359 .ndo_change_mtu = br_change_mtu,
360 .ndo_do_ioctl = br_dev_ioctl,
361 #ifdef CONFIG_NET_POLL_CONTROLLER
362 .ndo_netpoll_setup = br_netpoll_setup,
363 .ndo_netpoll_cleanup = br_netpoll_cleanup,
364 .ndo_poll_controller = br_poll_controller,
365 #endif
366 .ndo_add_slave = br_add_slave,
367 .ndo_del_slave = br_del_slave,
368 .ndo_fix_features = br_fix_features,
369 .ndo_fdb_add = br_fdb_add,
370 .ndo_fdb_del = br_fdb_delete,
371 .ndo_fdb_dump = br_fdb_dump,
372 .ndo_bridge_getlink = br_getlink,
373 .ndo_bridge_setlink = br_setlink,
374 .ndo_bridge_dellink = br_dellink,
375 .ndo_features_check = passthru_features_check,
376 };
377
378 static struct device_type br_type = {
379 .name = "bridge",
380 };
381
br_dev_setup(struct net_device * dev)382 void br_dev_setup(struct net_device *dev)
383 {
384 struct net_bridge *br = netdev_priv(dev);
385
386 eth_hw_addr_random(dev);
387 ether_setup(dev);
388
389 dev->netdev_ops = &br_netdev_ops;
390 dev->needs_free_netdev = true;
391 dev->ethtool_ops = &br_ethtool_ops;
392 SET_NETDEV_DEVTYPE(dev, &br_type);
393 dev->priv_flags = IFF_EBRIDGE | IFF_NO_QUEUE;
394
395 dev->features = COMMON_FEATURES | NETIF_F_LLTX | NETIF_F_NETNS_LOCAL |
396 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
397 dev->hw_features = COMMON_FEATURES | NETIF_F_HW_VLAN_CTAG_TX |
398 NETIF_F_HW_VLAN_STAG_TX;
399 dev->vlan_features = COMMON_FEATURES;
400
401 br->dev = dev;
402 spin_lock_init(&br->lock);
403 INIT_LIST_HEAD(&br->port_list);
404 spin_lock_init(&br->hash_lock);
405
406 br->bridge_id.prio[0] = 0x80;
407 br->bridge_id.prio[1] = 0x00;
408
409 ether_addr_copy(br->group_addr, eth_reserved_addr_base);
410
411 br->stp_enabled = BR_NO_STP;
412 br->group_fwd_mask = BR_GROUPFWD_DEFAULT;
413 br->group_fwd_mask_required = BR_GROUPFWD_DEFAULT;
414
415 br->designated_root = br->bridge_id;
416 br->bridge_max_age = br->max_age = 20 * HZ;
417 br->bridge_hello_time = br->hello_time = 2 * HZ;
418 br->bridge_forward_delay = br->forward_delay = 15 * HZ;
419 br->bridge_ageing_time = br->ageing_time = BR_DEFAULT_AGEING_TIME;
420 dev->max_mtu = ETH_MAX_MTU;
421
422 br_netfilter_rtable_init(br);
423 br_stp_timer_init(br);
424 br_multicast_init(br);
425 INIT_DELAYED_WORK(&br->gc_work, br_fdb_cleanup);
426 }
427