of: MSI: Simplify irqdomain lookup
[linux/fpc-iii.git] / net / bridge / br_device.c
blob5e88d3e17546618a728bb429fc61e20d5f8d79dc
1 /*
2 * Device handling code
3 * Linux ethernet bridge
5 * Authors:
6 * Lennert Buytenhek <buytenh@gnu.org>
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.
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>
22 #include <asm/uaccess.h>
23 #include "br_private.h"
25 #define COMMON_FEATURES (NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA | \
26 NETIF_F_GSO_MASK | NETIF_F_HW_CSUM)
28 const struct nf_br_ops __rcu *nf_br_ops __read_mostly;
29 EXPORT_SYMBOL_GPL(nf_br_ops);
31 /* net device transmit always called with BH disabled */
32 netdev_tx_t br_dev_xmit(struct sk_buff *skb, struct net_device *dev)
34 struct net_bridge *br = netdev_priv(dev);
35 const unsigned char *dest = skb->data;
36 struct net_bridge_fdb_entry *dst;
37 struct net_bridge_mdb_entry *mdst;
38 struct pcpu_sw_netstats *brstats = this_cpu_ptr(br->stats);
39 const struct nf_br_ops *nf_ops;
40 u16 vid = 0;
42 rcu_read_lock();
43 nf_ops = rcu_dereference(nf_br_ops);
44 if (nf_ops && nf_ops->br_dev_xmit_hook(skb)) {
45 rcu_read_unlock();
46 return NETDEV_TX_OK;
49 u64_stats_update_begin(&brstats->syncp);
50 brstats->tx_packets++;
51 brstats->tx_bytes += skb->len;
52 u64_stats_update_end(&brstats->syncp);
54 BR_INPUT_SKB_CB(skb)->brdev = dev;
56 skb_reset_mac_header(skb);
57 skb_pull(skb, ETH_HLEN);
59 if (!br_allowed_ingress(br, br_vlan_group_rcu(br), skb, &vid))
60 goto out;
62 if (is_broadcast_ether_addr(dest))
63 br_flood_deliver(br, skb, false);
64 else if (is_multicast_ether_addr(dest)) {
65 if (unlikely(netpoll_tx_running(dev))) {
66 br_flood_deliver(br, skb, false);
67 goto out;
69 if (br_multicast_rcv(br, NULL, skb, vid)) {
70 kfree_skb(skb);
71 goto out;
74 mdst = br_mdb_get(br, skb, vid);
75 if ((mdst || BR_INPUT_SKB_CB_MROUTERS_ONLY(skb)) &&
76 br_multicast_querier_exists(br, eth_hdr(skb)))
77 br_multicast_deliver(mdst, skb);
78 else
79 br_flood_deliver(br, skb, false);
80 } else if ((dst = __br_fdb_get(br, dest, vid)) != NULL)
81 br_deliver(dst->dst, skb);
82 else
83 br_flood_deliver(br, skb, true);
85 out:
86 rcu_read_unlock();
87 return NETDEV_TX_OK;
90 static int br_dev_init(struct net_device *dev)
92 struct net_bridge *br = netdev_priv(dev);
93 int err;
95 br->stats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
96 if (!br->stats)
97 return -ENOMEM;
99 err = br_vlan_init(br);
100 if (err)
101 free_percpu(br->stats);
103 return err;
106 static int br_dev_open(struct net_device *dev)
108 struct net_bridge *br = netdev_priv(dev);
110 netdev_update_features(dev);
111 netif_start_queue(dev);
112 br_stp_enable_bridge(br);
113 br_multicast_open(br);
115 return 0;
118 static void br_dev_set_multicast_list(struct net_device *dev)
122 static void br_dev_change_rx_flags(struct net_device *dev, int change)
124 if (change & IFF_PROMISC)
125 br_manage_promisc(netdev_priv(dev));
128 static int br_dev_stop(struct net_device *dev)
130 struct net_bridge *br = netdev_priv(dev);
132 br_stp_disable_bridge(br);
133 br_multicast_stop(br);
135 netif_stop_queue(dev);
137 return 0;
140 static struct rtnl_link_stats64 *br_get_stats64(struct net_device *dev,
141 struct rtnl_link_stats64 *stats)
143 struct net_bridge *br = netdev_priv(dev);
144 struct pcpu_sw_netstats tmp, sum = { 0 };
145 unsigned int cpu;
147 for_each_possible_cpu(cpu) {
148 unsigned int start;
149 const struct pcpu_sw_netstats *bstats
150 = per_cpu_ptr(br->stats, cpu);
151 do {
152 start = u64_stats_fetch_begin_irq(&bstats->syncp);
153 memcpy(&tmp, bstats, sizeof(tmp));
154 } while (u64_stats_fetch_retry_irq(&bstats->syncp, start));
155 sum.tx_bytes += tmp.tx_bytes;
156 sum.tx_packets += tmp.tx_packets;
157 sum.rx_bytes += tmp.rx_bytes;
158 sum.rx_packets += tmp.rx_packets;
161 stats->tx_bytes = sum.tx_bytes;
162 stats->tx_packets = sum.tx_packets;
163 stats->rx_bytes = sum.rx_bytes;
164 stats->rx_packets = sum.rx_packets;
166 return stats;
169 static int br_change_mtu(struct net_device *dev, int new_mtu)
171 struct net_bridge *br = netdev_priv(dev);
172 if (new_mtu < 68 || new_mtu > br_min_mtu(br))
173 return -EINVAL;
175 dev->mtu = new_mtu;
177 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
178 /* remember the MTU in the rtable for PMTU */
179 dst_metric_set(&br->fake_rtable.dst, RTAX_MTU, new_mtu);
180 #endif
182 return 0;
185 /* Allow setting mac address to any valid ethernet address. */
186 static int br_set_mac_address(struct net_device *dev, void *p)
188 struct net_bridge *br = netdev_priv(dev);
189 struct sockaddr *addr = p;
191 if (!is_valid_ether_addr(addr->sa_data))
192 return -EADDRNOTAVAIL;
194 spin_lock_bh(&br->lock);
195 if (!ether_addr_equal(dev->dev_addr, addr->sa_data)) {
196 /* Mac address will be changed in br_stp_change_bridge_id(). */
197 br_stp_change_bridge_id(br, addr->sa_data);
199 spin_unlock_bh(&br->lock);
201 return 0;
204 static void br_getinfo(struct net_device *dev, struct ethtool_drvinfo *info)
206 strlcpy(info->driver, "bridge", sizeof(info->driver));
207 strlcpy(info->version, BR_VERSION, sizeof(info->version));
208 strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
209 strlcpy(info->bus_info, "N/A", sizeof(info->bus_info));
212 static netdev_features_t br_fix_features(struct net_device *dev,
213 netdev_features_t features)
215 struct net_bridge *br = netdev_priv(dev);
217 return br_features_recompute(br, features);
220 #ifdef CONFIG_NET_POLL_CONTROLLER
221 static void br_poll_controller(struct net_device *br_dev)
225 static void br_netpoll_cleanup(struct net_device *dev)
227 struct net_bridge *br = netdev_priv(dev);
228 struct net_bridge_port *p;
230 list_for_each_entry(p, &br->port_list, list)
231 br_netpoll_disable(p);
234 static int __br_netpoll_enable(struct net_bridge_port *p)
236 struct netpoll *np;
237 int err;
239 np = kzalloc(sizeof(*p->np), GFP_KERNEL);
240 if (!np)
241 return -ENOMEM;
243 err = __netpoll_setup(np, p->dev);
244 if (err) {
245 kfree(np);
246 return err;
249 p->np = np;
250 return err;
253 int br_netpoll_enable(struct net_bridge_port *p)
255 if (!p->br->dev->npinfo)
256 return 0;
258 return __br_netpoll_enable(p);
261 static int br_netpoll_setup(struct net_device *dev, struct netpoll_info *ni)
263 struct net_bridge *br = netdev_priv(dev);
264 struct net_bridge_port *p;
265 int err = 0;
267 list_for_each_entry(p, &br->port_list, list) {
268 if (!p->dev)
269 continue;
270 err = __br_netpoll_enable(p);
271 if (err)
272 goto fail;
275 out:
276 return err;
278 fail:
279 br_netpoll_cleanup(dev);
280 goto out;
283 void br_netpoll_disable(struct net_bridge_port *p)
285 struct netpoll *np = p->np;
287 if (!np)
288 return;
290 p->np = NULL;
292 __netpoll_free_async(np);
295 #endif
297 static int br_add_slave(struct net_device *dev, struct net_device *slave_dev)
300 struct net_bridge *br = netdev_priv(dev);
302 return br_add_if(br, slave_dev);
305 static int br_del_slave(struct net_device *dev, struct net_device *slave_dev)
307 struct net_bridge *br = netdev_priv(dev);
309 return br_del_if(br, slave_dev);
312 static const struct ethtool_ops br_ethtool_ops = {
313 .get_drvinfo = br_getinfo,
314 .get_link = ethtool_op_get_link,
317 static const struct net_device_ops br_netdev_ops = {
318 .ndo_open = br_dev_open,
319 .ndo_stop = br_dev_stop,
320 .ndo_init = br_dev_init,
321 .ndo_start_xmit = br_dev_xmit,
322 .ndo_get_stats64 = br_get_stats64,
323 .ndo_set_mac_address = br_set_mac_address,
324 .ndo_set_rx_mode = br_dev_set_multicast_list,
325 .ndo_change_rx_flags = br_dev_change_rx_flags,
326 .ndo_change_mtu = br_change_mtu,
327 .ndo_do_ioctl = br_dev_ioctl,
328 #ifdef CONFIG_NET_POLL_CONTROLLER
329 .ndo_netpoll_setup = br_netpoll_setup,
330 .ndo_netpoll_cleanup = br_netpoll_cleanup,
331 .ndo_poll_controller = br_poll_controller,
332 #endif
333 .ndo_add_slave = br_add_slave,
334 .ndo_del_slave = br_del_slave,
335 .ndo_fix_features = br_fix_features,
336 .ndo_fdb_add = br_fdb_add,
337 .ndo_fdb_del = br_fdb_delete,
338 .ndo_fdb_dump = br_fdb_dump,
339 .ndo_bridge_getlink = br_getlink,
340 .ndo_bridge_setlink = br_setlink,
341 .ndo_bridge_dellink = br_dellink,
342 .ndo_features_check = passthru_features_check,
345 static void br_dev_free(struct net_device *dev)
347 struct net_bridge *br = netdev_priv(dev);
349 free_percpu(br->stats);
350 free_netdev(dev);
353 static struct device_type br_type = {
354 .name = "bridge",
357 void br_dev_setup(struct net_device *dev)
359 struct net_bridge *br = netdev_priv(dev);
361 eth_hw_addr_random(dev);
362 ether_setup(dev);
364 dev->netdev_ops = &br_netdev_ops;
365 dev->destructor = br_dev_free;
366 dev->ethtool_ops = &br_ethtool_ops;
367 SET_NETDEV_DEVTYPE(dev, &br_type);
368 dev->priv_flags = IFF_EBRIDGE | IFF_NO_QUEUE;
370 dev->features = COMMON_FEATURES | NETIF_F_LLTX | NETIF_F_NETNS_LOCAL |
371 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
372 dev->hw_features = COMMON_FEATURES | NETIF_F_HW_VLAN_CTAG_TX |
373 NETIF_F_HW_VLAN_STAG_TX;
374 dev->vlan_features = COMMON_FEATURES;
376 br->dev = dev;
377 spin_lock_init(&br->lock);
378 INIT_LIST_HEAD(&br->port_list);
379 spin_lock_init(&br->hash_lock);
381 br->bridge_id.prio[0] = 0x80;
382 br->bridge_id.prio[1] = 0x00;
384 ether_addr_copy(br->group_addr, eth_reserved_addr_base);
386 br->stp_enabled = BR_NO_STP;
387 br->group_fwd_mask = BR_GROUPFWD_DEFAULT;
388 br->group_fwd_mask_required = BR_GROUPFWD_DEFAULT;
390 br->designated_root = br->bridge_id;
391 br->bridge_max_age = br->max_age = 20 * HZ;
392 br->bridge_hello_time = br->hello_time = 2 * HZ;
393 br->bridge_forward_delay = br->forward_delay = 15 * HZ;
394 br->ageing_time = BR_DEFAULT_AGEING_TIME;
396 br_netfilter_rtable_init(br);
397 br_stp_timer_init(br);
398 br_multicast_init(br);