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