~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/bridge/br_device.c

Version: ~ [ linux-5.11-rc3 ] ~ [ linux-5.10.7 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.89 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.167 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.215 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.251 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.251 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp