Linux 4.19.133
[linux/fpc-iii.git] / drivers / net / ipvlan / ipvlan_main.c
blob87f605a33c37aba35b896d14f4f0dfaf6a59b688
1 /* Copyright (c) 2014 Mahesh Bandewar <maheshb@google.com>
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public License as
5 * published by the Free Software Foundation; either version 2 of
6 * the License, or (at your option) any later version.
8 */
10 #include "ipvlan.h"
12 static unsigned int ipvlan_netid __read_mostly;
14 struct ipvlan_netns {
15 unsigned int ipvl_nf_hook_refcnt;
18 static const struct nf_hook_ops ipvl_nfops[] = {
20 .hook = ipvlan_nf_input,
21 .pf = NFPROTO_IPV4,
22 .hooknum = NF_INET_LOCAL_IN,
23 .priority = INT_MAX,
25 #if IS_ENABLED(CONFIG_IPV6)
27 .hook = ipvlan_nf_input,
28 .pf = NFPROTO_IPV6,
29 .hooknum = NF_INET_LOCAL_IN,
30 .priority = INT_MAX,
32 #endif
35 static const struct l3mdev_ops ipvl_l3mdev_ops = {
36 .l3mdev_l3_rcv = ipvlan_l3_rcv,
39 static void ipvlan_adjust_mtu(struct ipvl_dev *ipvlan, struct net_device *dev)
41 ipvlan->dev->mtu = dev->mtu;
44 static int ipvlan_register_nf_hook(struct net *net)
46 struct ipvlan_netns *vnet = net_generic(net, ipvlan_netid);
47 int err = 0;
49 if (!vnet->ipvl_nf_hook_refcnt) {
50 err = nf_register_net_hooks(net, ipvl_nfops,
51 ARRAY_SIZE(ipvl_nfops));
52 if (!err)
53 vnet->ipvl_nf_hook_refcnt = 1;
54 } else {
55 vnet->ipvl_nf_hook_refcnt++;
58 return err;
61 static void ipvlan_unregister_nf_hook(struct net *net)
63 struct ipvlan_netns *vnet = net_generic(net, ipvlan_netid);
65 if (WARN_ON(!vnet->ipvl_nf_hook_refcnt))
66 return;
68 vnet->ipvl_nf_hook_refcnt--;
69 if (!vnet->ipvl_nf_hook_refcnt)
70 nf_unregister_net_hooks(net, ipvl_nfops,
71 ARRAY_SIZE(ipvl_nfops));
74 static int ipvlan_set_port_mode(struct ipvl_port *port, u16 nval)
76 struct ipvl_dev *ipvlan;
77 struct net_device *mdev = port->dev;
78 unsigned int flags;
79 int err;
81 ASSERT_RTNL();
82 if (port->mode != nval) {
83 list_for_each_entry(ipvlan, &port->ipvlans, pnode) {
84 flags = ipvlan->dev->flags;
85 if (nval == IPVLAN_MODE_L3 || nval == IPVLAN_MODE_L3S) {
86 err = dev_change_flags(ipvlan->dev,
87 flags | IFF_NOARP);
88 } else {
89 err = dev_change_flags(ipvlan->dev,
90 flags & ~IFF_NOARP);
92 if (unlikely(err))
93 goto fail;
95 if (nval == IPVLAN_MODE_L3S) {
96 /* New mode is L3S */
97 err = ipvlan_register_nf_hook(read_pnet(&port->pnet));
98 if (!err) {
99 mdev->l3mdev_ops = &ipvl_l3mdev_ops;
100 mdev->priv_flags |= IFF_L3MDEV_RX_HANDLER;
101 } else
102 goto fail;
103 } else if (port->mode == IPVLAN_MODE_L3S) {
104 /* Old mode was L3S */
105 mdev->priv_flags &= ~IFF_L3MDEV_RX_HANDLER;
106 ipvlan_unregister_nf_hook(read_pnet(&port->pnet));
107 mdev->l3mdev_ops = NULL;
109 port->mode = nval;
111 return 0;
113 fail:
114 /* Undo the flags changes that have been done so far. */
115 list_for_each_entry_continue_reverse(ipvlan, &port->ipvlans, pnode) {
116 flags = ipvlan->dev->flags;
117 if (port->mode == IPVLAN_MODE_L3 ||
118 port->mode == IPVLAN_MODE_L3S)
119 dev_change_flags(ipvlan->dev, flags | IFF_NOARP);
120 else
121 dev_change_flags(ipvlan->dev, flags & ~IFF_NOARP);
124 return err;
127 static int ipvlan_port_create(struct net_device *dev)
129 struct ipvl_port *port;
130 int err, idx;
132 port = kzalloc(sizeof(struct ipvl_port), GFP_KERNEL);
133 if (!port)
134 return -ENOMEM;
136 write_pnet(&port->pnet, dev_net(dev));
137 port->dev = dev;
138 port->mode = IPVLAN_MODE_L3;
139 INIT_LIST_HEAD(&port->ipvlans);
140 for (idx = 0; idx < IPVLAN_HASH_SIZE; idx++)
141 INIT_HLIST_HEAD(&port->hlhead[idx]);
143 skb_queue_head_init(&port->backlog);
144 INIT_WORK(&port->wq, ipvlan_process_multicast);
145 ida_init(&port->ida);
146 port->dev_id_start = 1;
148 err = netdev_rx_handler_register(dev, ipvlan_handle_frame, port);
149 if (err)
150 goto err;
152 return 0;
154 err:
155 kfree(port);
156 return err;
159 static void ipvlan_port_destroy(struct net_device *dev)
161 struct ipvl_port *port = ipvlan_port_get_rtnl(dev);
162 struct sk_buff *skb;
164 if (port->mode == IPVLAN_MODE_L3S) {
165 dev->priv_flags &= ~IFF_L3MDEV_RX_HANDLER;
166 ipvlan_unregister_nf_hook(dev_net(dev));
167 dev->l3mdev_ops = NULL;
169 netdev_rx_handler_unregister(dev);
170 cancel_work_sync(&port->wq);
171 while ((skb = __skb_dequeue(&port->backlog)) != NULL) {
172 if (skb->dev)
173 dev_put(skb->dev);
174 kfree_skb(skb);
176 ida_destroy(&port->ida);
177 kfree(port);
180 #define IPVLAN_FEATURES \
181 (NETIF_F_SG | NETIF_F_CSUM_MASK | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
182 NETIF_F_GSO | NETIF_F_TSO | NETIF_F_GSO_ROBUST | \
183 NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_GRO | NETIF_F_RXCSUM | \
184 NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_STAG_FILTER)
186 #define IPVLAN_STATE_MASK \
187 ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
189 static int ipvlan_init(struct net_device *dev)
191 struct ipvl_dev *ipvlan = netdev_priv(dev);
192 struct net_device *phy_dev = ipvlan->phy_dev;
193 struct ipvl_port *port;
194 int err;
196 dev->state = (dev->state & ~IPVLAN_STATE_MASK) |
197 (phy_dev->state & IPVLAN_STATE_MASK);
198 dev->features = phy_dev->features & IPVLAN_FEATURES;
199 dev->features |= NETIF_F_LLTX | NETIF_F_VLAN_CHALLENGED;
200 dev->gso_max_size = phy_dev->gso_max_size;
201 dev->gso_max_segs = phy_dev->gso_max_segs;
202 dev->hard_header_len = phy_dev->hard_header_len;
204 netdev_lockdep_set_classes(dev);
206 ipvlan->pcpu_stats = netdev_alloc_pcpu_stats(struct ipvl_pcpu_stats);
207 if (!ipvlan->pcpu_stats)
208 return -ENOMEM;
210 if (!netif_is_ipvlan_port(phy_dev)) {
211 err = ipvlan_port_create(phy_dev);
212 if (err < 0) {
213 free_percpu(ipvlan->pcpu_stats);
214 return err;
217 port = ipvlan_port_get_rtnl(phy_dev);
218 port->count += 1;
219 return 0;
222 static void ipvlan_uninit(struct net_device *dev)
224 struct ipvl_dev *ipvlan = netdev_priv(dev);
225 struct net_device *phy_dev = ipvlan->phy_dev;
226 struct ipvl_port *port;
228 free_percpu(ipvlan->pcpu_stats);
230 port = ipvlan_port_get_rtnl(phy_dev);
231 port->count -= 1;
232 if (!port->count)
233 ipvlan_port_destroy(port->dev);
236 static int ipvlan_open(struct net_device *dev)
238 struct ipvl_dev *ipvlan = netdev_priv(dev);
239 struct ipvl_addr *addr;
241 if (ipvlan->port->mode == IPVLAN_MODE_L3 ||
242 ipvlan->port->mode == IPVLAN_MODE_L3S)
243 dev->flags |= IFF_NOARP;
244 else
245 dev->flags &= ~IFF_NOARP;
247 rcu_read_lock();
248 list_for_each_entry_rcu(addr, &ipvlan->addrs, anode)
249 ipvlan_ht_addr_add(ipvlan, addr);
250 rcu_read_unlock();
252 return 0;
255 static int ipvlan_stop(struct net_device *dev)
257 struct ipvl_dev *ipvlan = netdev_priv(dev);
258 struct net_device *phy_dev = ipvlan->phy_dev;
259 struct ipvl_addr *addr;
261 dev_uc_unsync(phy_dev, dev);
262 dev_mc_unsync(phy_dev, dev);
264 rcu_read_lock();
265 list_for_each_entry_rcu(addr, &ipvlan->addrs, anode)
266 ipvlan_ht_addr_del(addr);
267 rcu_read_unlock();
269 return 0;
272 static netdev_tx_t ipvlan_start_xmit(struct sk_buff *skb,
273 struct net_device *dev)
275 const struct ipvl_dev *ipvlan = netdev_priv(dev);
276 int skblen = skb->len;
277 int ret;
279 ret = ipvlan_queue_xmit(skb, dev);
280 if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
281 struct ipvl_pcpu_stats *pcptr;
283 pcptr = this_cpu_ptr(ipvlan->pcpu_stats);
285 u64_stats_update_begin(&pcptr->syncp);
286 pcptr->tx_pkts++;
287 pcptr->tx_bytes += skblen;
288 u64_stats_update_end(&pcptr->syncp);
289 } else {
290 this_cpu_inc(ipvlan->pcpu_stats->tx_drps);
292 return ret;
295 static netdev_features_t ipvlan_fix_features(struct net_device *dev,
296 netdev_features_t features)
298 struct ipvl_dev *ipvlan = netdev_priv(dev);
300 return features & (ipvlan->sfeatures | ~IPVLAN_FEATURES);
303 static void ipvlan_change_rx_flags(struct net_device *dev, int change)
305 struct ipvl_dev *ipvlan = netdev_priv(dev);
306 struct net_device *phy_dev = ipvlan->phy_dev;
308 if (change & IFF_ALLMULTI)
309 dev_set_allmulti(phy_dev, dev->flags & IFF_ALLMULTI? 1 : -1);
312 static void ipvlan_set_multicast_mac_filter(struct net_device *dev)
314 struct ipvl_dev *ipvlan = netdev_priv(dev);
316 if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
317 bitmap_fill(ipvlan->mac_filters, IPVLAN_MAC_FILTER_SIZE);
318 } else {
319 struct netdev_hw_addr *ha;
320 DECLARE_BITMAP(mc_filters, IPVLAN_MAC_FILTER_SIZE);
322 bitmap_zero(mc_filters, IPVLAN_MAC_FILTER_SIZE);
323 netdev_for_each_mc_addr(ha, dev)
324 __set_bit(ipvlan_mac_hash(ha->addr), mc_filters);
326 /* Turn-on broadcast bit irrespective of address family,
327 * since broadcast is deferred to a work-queue, hence no
328 * impact on fast-path processing.
330 __set_bit(ipvlan_mac_hash(dev->broadcast), mc_filters);
332 bitmap_copy(ipvlan->mac_filters, mc_filters,
333 IPVLAN_MAC_FILTER_SIZE);
335 dev_uc_sync(ipvlan->phy_dev, dev);
336 dev_mc_sync(ipvlan->phy_dev, dev);
339 static void ipvlan_get_stats64(struct net_device *dev,
340 struct rtnl_link_stats64 *s)
342 struct ipvl_dev *ipvlan = netdev_priv(dev);
344 if (ipvlan->pcpu_stats) {
345 struct ipvl_pcpu_stats *pcptr;
346 u64 rx_pkts, rx_bytes, rx_mcast, tx_pkts, tx_bytes;
347 u32 rx_errs = 0, tx_drps = 0;
348 u32 strt;
349 int idx;
351 for_each_possible_cpu(idx) {
352 pcptr = per_cpu_ptr(ipvlan->pcpu_stats, idx);
353 do {
354 strt= u64_stats_fetch_begin_irq(&pcptr->syncp);
355 rx_pkts = pcptr->rx_pkts;
356 rx_bytes = pcptr->rx_bytes;
357 rx_mcast = pcptr->rx_mcast;
358 tx_pkts = pcptr->tx_pkts;
359 tx_bytes = pcptr->tx_bytes;
360 } while (u64_stats_fetch_retry_irq(&pcptr->syncp,
361 strt));
363 s->rx_packets += rx_pkts;
364 s->rx_bytes += rx_bytes;
365 s->multicast += rx_mcast;
366 s->tx_packets += tx_pkts;
367 s->tx_bytes += tx_bytes;
369 /* u32 values are updated without syncp protection. */
370 rx_errs += pcptr->rx_errs;
371 tx_drps += pcptr->tx_drps;
373 s->rx_errors = rx_errs;
374 s->rx_dropped = rx_errs;
375 s->tx_dropped = tx_drps;
379 static int ipvlan_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
381 struct ipvl_dev *ipvlan = netdev_priv(dev);
382 struct net_device *phy_dev = ipvlan->phy_dev;
384 return vlan_vid_add(phy_dev, proto, vid);
387 static int ipvlan_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
388 u16 vid)
390 struct ipvl_dev *ipvlan = netdev_priv(dev);
391 struct net_device *phy_dev = ipvlan->phy_dev;
393 vlan_vid_del(phy_dev, proto, vid);
394 return 0;
397 static int ipvlan_get_iflink(const struct net_device *dev)
399 struct ipvl_dev *ipvlan = netdev_priv(dev);
401 return ipvlan->phy_dev->ifindex;
404 static const struct net_device_ops ipvlan_netdev_ops = {
405 .ndo_init = ipvlan_init,
406 .ndo_uninit = ipvlan_uninit,
407 .ndo_open = ipvlan_open,
408 .ndo_stop = ipvlan_stop,
409 .ndo_start_xmit = ipvlan_start_xmit,
410 .ndo_fix_features = ipvlan_fix_features,
411 .ndo_change_rx_flags = ipvlan_change_rx_flags,
412 .ndo_set_rx_mode = ipvlan_set_multicast_mac_filter,
413 .ndo_get_stats64 = ipvlan_get_stats64,
414 .ndo_vlan_rx_add_vid = ipvlan_vlan_rx_add_vid,
415 .ndo_vlan_rx_kill_vid = ipvlan_vlan_rx_kill_vid,
416 .ndo_get_iflink = ipvlan_get_iflink,
419 static int ipvlan_hard_header(struct sk_buff *skb, struct net_device *dev,
420 unsigned short type, const void *daddr,
421 const void *saddr, unsigned len)
423 const struct ipvl_dev *ipvlan = netdev_priv(dev);
424 struct net_device *phy_dev = ipvlan->phy_dev;
426 /* TODO Probably use a different field than dev_addr so that the
427 * mac-address on the virtual device is portable and can be carried
428 * while the packets use the mac-addr on the physical device.
430 return dev_hard_header(skb, phy_dev, type, daddr,
431 saddr ? : phy_dev->dev_addr, len);
434 static const struct header_ops ipvlan_header_ops = {
435 .create = ipvlan_hard_header,
436 .parse = eth_header_parse,
437 .cache = eth_header_cache,
438 .cache_update = eth_header_cache_update,
441 static bool netif_is_ipvlan(const struct net_device *dev)
443 /* both ipvlan and ipvtap devices use the same netdev_ops */
444 return dev->netdev_ops == &ipvlan_netdev_ops;
447 static int ipvlan_ethtool_get_link_ksettings(struct net_device *dev,
448 struct ethtool_link_ksettings *cmd)
450 const struct ipvl_dev *ipvlan = netdev_priv(dev);
452 return __ethtool_get_link_ksettings(ipvlan->phy_dev, cmd);
455 static void ipvlan_ethtool_get_drvinfo(struct net_device *dev,
456 struct ethtool_drvinfo *drvinfo)
458 strlcpy(drvinfo->driver, IPVLAN_DRV, sizeof(drvinfo->driver));
459 strlcpy(drvinfo->version, IPV_DRV_VER, sizeof(drvinfo->version));
462 static u32 ipvlan_ethtool_get_msglevel(struct net_device *dev)
464 const struct ipvl_dev *ipvlan = netdev_priv(dev);
466 return ipvlan->msg_enable;
469 static void ipvlan_ethtool_set_msglevel(struct net_device *dev, u32 value)
471 struct ipvl_dev *ipvlan = netdev_priv(dev);
473 ipvlan->msg_enable = value;
476 static const struct ethtool_ops ipvlan_ethtool_ops = {
477 .get_link = ethtool_op_get_link,
478 .get_link_ksettings = ipvlan_ethtool_get_link_ksettings,
479 .get_drvinfo = ipvlan_ethtool_get_drvinfo,
480 .get_msglevel = ipvlan_ethtool_get_msglevel,
481 .set_msglevel = ipvlan_ethtool_set_msglevel,
484 static int ipvlan_nl_changelink(struct net_device *dev,
485 struct nlattr *tb[], struct nlattr *data[],
486 struct netlink_ext_ack *extack)
488 struct ipvl_dev *ipvlan = netdev_priv(dev);
489 struct ipvl_port *port = ipvlan_port_get_rtnl(ipvlan->phy_dev);
490 int err = 0;
492 if (!data)
493 return 0;
494 if (!ns_capable(dev_net(ipvlan->phy_dev)->user_ns, CAP_NET_ADMIN))
495 return -EPERM;
497 if (data[IFLA_IPVLAN_MODE]) {
498 u16 nmode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
500 err = ipvlan_set_port_mode(port, nmode);
503 if (!err && data[IFLA_IPVLAN_FLAGS]) {
504 u16 flags = nla_get_u16(data[IFLA_IPVLAN_FLAGS]);
506 if (flags & IPVLAN_F_PRIVATE)
507 ipvlan_mark_private(port);
508 else
509 ipvlan_clear_private(port);
511 if (flags & IPVLAN_F_VEPA)
512 ipvlan_mark_vepa(port);
513 else
514 ipvlan_clear_vepa(port);
517 return err;
520 static size_t ipvlan_nl_getsize(const struct net_device *dev)
522 return (0
523 + nla_total_size(2) /* IFLA_IPVLAN_MODE */
524 + nla_total_size(2) /* IFLA_IPVLAN_FLAGS */
528 static int ipvlan_nl_validate(struct nlattr *tb[], struct nlattr *data[],
529 struct netlink_ext_ack *extack)
531 if (!data)
532 return 0;
534 if (data[IFLA_IPVLAN_MODE]) {
535 u16 mode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
537 if (mode < IPVLAN_MODE_L2 || mode >= IPVLAN_MODE_MAX)
538 return -EINVAL;
540 if (data[IFLA_IPVLAN_FLAGS]) {
541 u16 flags = nla_get_u16(data[IFLA_IPVLAN_FLAGS]);
543 /* Only two bits are used at this moment. */
544 if (flags & ~(IPVLAN_F_PRIVATE | IPVLAN_F_VEPA))
545 return -EINVAL;
546 /* Also both flags can't be active at the same time. */
547 if ((flags & (IPVLAN_F_PRIVATE | IPVLAN_F_VEPA)) ==
548 (IPVLAN_F_PRIVATE | IPVLAN_F_VEPA))
549 return -EINVAL;
552 return 0;
555 static int ipvlan_nl_fillinfo(struct sk_buff *skb,
556 const struct net_device *dev)
558 struct ipvl_dev *ipvlan = netdev_priv(dev);
559 struct ipvl_port *port = ipvlan_port_get_rtnl(ipvlan->phy_dev);
560 int ret = -EINVAL;
562 if (!port)
563 goto err;
565 ret = -EMSGSIZE;
566 if (nla_put_u16(skb, IFLA_IPVLAN_MODE, port->mode))
567 goto err;
568 if (nla_put_u16(skb, IFLA_IPVLAN_FLAGS, port->flags))
569 goto err;
571 return 0;
573 err:
574 return ret;
577 int ipvlan_link_new(struct net *src_net, struct net_device *dev,
578 struct nlattr *tb[], struct nlattr *data[],
579 struct netlink_ext_ack *extack)
581 struct ipvl_dev *ipvlan = netdev_priv(dev);
582 struct ipvl_port *port;
583 struct net_device *phy_dev;
584 int err;
585 u16 mode = IPVLAN_MODE_L3;
587 if (!tb[IFLA_LINK])
588 return -EINVAL;
590 phy_dev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
591 if (!phy_dev)
592 return -ENODEV;
594 if (netif_is_ipvlan(phy_dev)) {
595 struct ipvl_dev *tmp = netdev_priv(phy_dev);
597 phy_dev = tmp->phy_dev;
598 if (!ns_capable(dev_net(phy_dev)->user_ns, CAP_NET_ADMIN))
599 return -EPERM;
600 } else if (!netif_is_ipvlan_port(phy_dev)) {
601 /* Exit early if the underlying link is invalid or busy */
602 if (phy_dev->type != ARPHRD_ETHER ||
603 phy_dev->flags & IFF_LOOPBACK) {
604 netdev_err(phy_dev,
605 "Master is either lo or non-ether device\n");
606 return -EINVAL;
609 if (netdev_is_rx_handler_busy(phy_dev)) {
610 netdev_err(phy_dev, "Device is already in use.\n");
611 return -EBUSY;
615 ipvlan->phy_dev = phy_dev;
616 ipvlan->dev = dev;
617 ipvlan->sfeatures = IPVLAN_FEATURES;
618 if (!tb[IFLA_MTU])
619 ipvlan_adjust_mtu(ipvlan, phy_dev);
620 INIT_LIST_HEAD(&ipvlan->addrs);
621 spin_lock_init(&ipvlan->addrs_lock);
623 /* TODO Probably put random address here to be presented to the
624 * world but keep using the physical-dev address for the outgoing
625 * packets.
627 memcpy(dev->dev_addr, phy_dev->dev_addr, ETH_ALEN);
629 dev->priv_flags |= IFF_NO_RX_HANDLER;
631 err = register_netdevice(dev);
632 if (err < 0)
633 return err;
635 /* ipvlan_init() would have created the port, if required */
636 port = ipvlan_port_get_rtnl(phy_dev);
637 ipvlan->port = port;
639 /* If the port-id base is at the MAX value, then wrap it around and
640 * begin from 0x1 again. This may be due to a busy system where lots
641 * of slaves are getting created and deleted.
643 if (port->dev_id_start == 0xFFFE)
644 port->dev_id_start = 0x1;
646 /* Since L2 address is shared among all IPvlan slaves including
647 * master, use unique 16 bit dev-ids to diffentiate among them.
648 * Assign IDs between 0x1 and 0xFFFE (used by the master) to each
649 * slave link [see addrconf_ifid_eui48()].
651 err = ida_simple_get(&port->ida, port->dev_id_start, 0xFFFE,
652 GFP_KERNEL);
653 if (err < 0)
654 err = ida_simple_get(&port->ida, 0x1, port->dev_id_start,
655 GFP_KERNEL);
656 if (err < 0)
657 goto unregister_netdev;
658 dev->dev_id = err;
660 /* Increment id-base to the next slot for the future assignment */
661 port->dev_id_start = err + 1;
663 err = netdev_upper_dev_link(phy_dev, dev, extack);
664 if (err)
665 goto remove_ida;
667 /* Flags are per port and latest update overrides. User has
668 * to be consistent in setting it just like the mode attribute.
670 if (data && data[IFLA_IPVLAN_FLAGS])
671 port->flags = nla_get_u16(data[IFLA_IPVLAN_FLAGS]);
673 if (data && data[IFLA_IPVLAN_MODE])
674 mode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
676 err = ipvlan_set_port_mode(port, mode);
677 if (err)
678 goto unlink_netdev;
680 list_add_tail_rcu(&ipvlan->pnode, &port->ipvlans);
681 netif_stacked_transfer_operstate(phy_dev, dev);
682 return 0;
684 unlink_netdev:
685 netdev_upper_dev_unlink(phy_dev, dev);
686 remove_ida:
687 ida_simple_remove(&port->ida, dev->dev_id);
688 unregister_netdev:
689 unregister_netdevice(dev);
690 return err;
692 EXPORT_SYMBOL_GPL(ipvlan_link_new);
694 void ipvlan_link_delete(struct net_device *dev, struct list_head *head)
696 struct ipvl_dev *ipvlan = netdev_priv(dev);
697 struct ipvl_addr *addr, *next;
699 spin_lock_bh(&ipvlan->addrs_lock);
700 list_for_each_entry_safe(addr, next, &ipvlan->addrs, anode) {
701 ipvlan_ht_addr_del(addr);
702 list_del_rcu(&addr->anode);
703 kfree_rcu(addr, rcu);
705 spin_unlock_bh(&ipvlan->addrs_lock);
707 ida_simple_remove(&ipvlan->port->ida, dev->dev_id);
708 list_del_rcu(&ipvlan->pnode);
709 unregister_netdevice_queue(dev, head);
710 netdev_upper_dev_unlink(ipvlan->phy_dev, dev);
712 EXPORT_SYMBOL_GPL(ipvlan_link_delete);
714 void ipvlan_link_setup(struct net_device *dev)
716 ether_setup(dev);
718 dev->max_mtu = ETH_MAX_MTU;
719 dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
720 dev->priv_flags |= IFF_UNICAST_FLT | IFF_NO_QUEUE;
721 dev->netdev_ops = &ipvlan_netdev_ops;
722 dev->needs_free_netdev = true;
723 dev->header_ops = &ipvlan_header_ops;
724 dev->ethtool_ops = &ipvlan_ethtool_ops;
726 EXPORT_SYMBOL_GPL(ipvlan_link_setup);
728 static const struct nla_policy ipvlan_nl_policy[IFLA_IPVLAN_MAX + 1] =
730 [IFLA_IPVLAN_MODE] = { .type = NLA_U16 },
731 [IFLA_IPVLAN_FLAGS] = { .type = NLA_U16 },
734 static struct rtnl_link_ops ipvlan_link_ops = {
735 .kind = "ipvlan",
736 .priv_size = sizeof(struct ipvl_dev),
738 .setup = ipvlan_link_setup,
739 .newlink = ipvlan_link_new,
740 .dellink = ipvlan_link_delete,
743 int ipvlan_link_register(struct rtnl_link_ops *ops)
745 ops->get_size = ipvlan_nl_getsize;
746 ops->policy = ipvlan_nl_policy;
747 ops->validate = ipvlan_nl_validate;
748 ops->fill_info = ipvlan_nl_fillinfo;
749 ops->changelink = ipvlan_nl_changelink;
750 ops->maxtype = IFLA_IPVLAN_MAX;
751 return rtnl_link_register(ops);
753 EXPORT_SYMBOL_GPL(ipvlan_link_register);
755 static int ipvlan_device_event(struct notifier_block *unused,
756 unsigned long event, void *ptr)
758 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
759 struct ipvl_dev *ipvlan, *next;
760 struct ipvl_port *port;
761 LIST_HEAD(lst_kill);
763 if (!netif_is_ipvlan_port(dev))
764 return NOTIFY_DONE;
766 port = ipvlan_port_get_rtnl(dev);
768 switch (event) {
769 case NETDEV_CHANGE:
770 list_for_each_entry(ipvlan, &port->ipvlans, pnode)
771 netif_stacked_transfer_operstate(ipvlan->phy_dev,
772 ipvlan->dev);
773 break;
775 case NETDEV_REGISTER: {
776 struct net *oldnet, *newnet = dev_net(dev);
777 struct ipvlan_netns *old_vnet;
779 oldnet = read_pnet(&port->pnet);
780 if (net_eq(newnet, oldnet))
781 break;
783 write_pnet(&port->pnet, newnet);
785 old_vnet = net_generic(oldnet, ipvlan_netid);
786 if (!old_vnet->ipvl_nf_hook_refcnt)
787 break;
789 ipvlan_register_nf_hook(newnet);
790 ipvlan_unregister_nf_hook(oldnet);
791 break;
793 case NETDEV_UNREGISTER:
794 if (dev->reg_state != NETREG_UNREGISTERING)
795 break;
797 list_for_each_entry_safe(ipvlan, next, &port->ipvlans, pnode)
798 ipvlan->dev->rtnl_link_ops->dellink(ipvlan->dev,
799 &lst_kill);
800 unregister_netdevice_many(&lst_kill);
801 break;
803 case NETDEV_FEAT_CHANGE:
804 list_for_each_entry(ipvlan, &port->ipvlans, pnode) {
805 ipvlan->dev->features = dev->features & IPVLAN_FEATURES;
806 ipvlan->dev->gso_max_size = dev->gso_max_size;
807 ipvlan->dev->gso_max_segs = dev->gso_max_segs;
808 netdev_features_change(ipvlan->dev);
810 break;
812 case NETDEV_CHANGEMTU:
813 list_for_each_entry(ipvlan, &port->ipvlans, pnode)
814 ipvlan_adjust_mtu(ipvlan, dev);
815 break;
817 case NETDEV_CHANGEADDR:
818 list_for_each_entry(ipvlan, &port->ipvlans, pnode) {
819 ether_addr_copy(ipvlan->dev->dev_addr, dev->dev_addr);
820 call_netdevice_notifiers(NETDEV_CHANGEADDR, ipvlan->dev);
822 break;
824 case NETDEV_PRE_TYPE_CHANGE:
825 /* Forbid underlying device to change its type. */
826 return NOTIFY_BAD;
828 return NOTIFY_DONE;
831 /* the caller must held the addrs lock */
832 static int ipvlan_add_addr(struct ipvl_dev *ipvlan, void *iaddr, bool is_v6)
834 struct ipvl_addr *addr;
836 addr = kzalloc(sizeof(struct ipvl_addr), GFP_ATOMIC);
837 if (!addr)
838 return -ENOMEM;
840 addr->master = ipvlan;
841 if (!is_v6) {
842 memcpy(&addr->ip4addr, iaddr, sizeof(struct in_addr));
843 addr->atype = IPVL_IPV4;
844 #if IS_ENABLED(CONFIG_IPV6)
845 } else {
846 memcpy(&addr->ip6addr, iaddr, sizeof(struct in6_addr));
847 addr->atype = IPVL_IPV6;
848 #endif
851 list_add_tail_rcu(&addr->anode, &ipvlan->addrs);
853 /* If the interface is not up, the address will be added to the hash
854 * list by ipvlan_open.
856 if (netif_running(ipvlan->dev))
857 ipvlan_ht_addr_add(ipvlan, addr);
859 return 0;
862 static void ipvlan_del_addr(struct ipvl_dev *ipvlan, void *iaddr, bool is_v6)
864 struct ipvl_addr *addr;
866 spin_lock_bh(&ipvlan->addrs_lock);
867 addr = ipvlan_find_addr(ipvlan, iaddr, is_v6);
868 if (!addr) {
869 spin_unlock_bh(&ipvlan->addrs_lock);
870 return;
873 ipvlan_ht_addr_del(addr);
874 list_del_rcu(&addr->anode);
875 spin_unlock_bh(&ipvlan->addrs_lock);
876 kfree_rcu(addr, rcu);
879 static bool ipvlan_is_valid_dev(const struct net_device *dev)
881 struct ipvl_dev *ipvlan = netdev_priv(dev);
883 if (!netif_is_ipvlan(dev))
884 return false;
886 if (!ipvlan || !ipvlan->port)
887 return false;
889 return true;
892 #if IS_ENABLED(CONFIG_IPV6)
893 static int ipvlan_add_addr6(struct ipvl_dev *ipvlan, struct in6_addr *ip6_addr)
895 int ret = -EINVAL;
897 spin_lock_bh(&ipvlan->addrs_lock);
898 if (ipvlan_addr_busy(ipvlan->port, ip6_addr, true))
899 netif_err(ipvlan, ifup, ipvlan->dev,
900 "Failed to add IPv6=%pI6c addr for %s intf\n",
901 ip6_addr, ipvlan->dev->name);
902 else
903 ret = ipvlan_add_addr(ipvlan, ip6_addr, true);
904 spin_unlock_bh(&ipvlan->addrs_lock);
905 return ret;
908 static void ipvlan_del_addr6(struct ipvl_dev *ipvlan, struct in6_addr *ip6_addr)
910 return ipvlan_del_addr(ipvlan, ip6_addr, true);
913 static int ipvlan_addr6_event(struct notifier_block *unused,
914 unsigned long event, void *ptr)
916 struct inet6_ifaddr *if6 = (struct inet6_ifaddr *)ptr;
917 struct net_device *dev = (struct net_device *)if6->idev->dev;
918 struct ipvl_dev *ipvlan = netdev_priv(dev);
920 if (!ipvlan_is_valid_dev(dev))
921 return NOTIFY_DONE;
923 switch (event) {
924 case NETDEV_UP:
925 if (ipvlan_add_addr6(ipvlan, &if6->addr))
926 return NOTIFY_BAD;
927 break;
929 case NETDEV_DOWN:
930 ipvlan_del_addr6(ipvlan, &if6->addr);
931 break;
934 return NOTIFY_OK;
937 static int ipvlan_addr6_validator_event(struct notifier_block *unused,
938 unsigned long event, void *ptr)
940 struct in6_validator_info *i6vi = (struct in6_validator_info *)ptr;
941 struct net_device *dev = (struct net_device *)i6vi->i6vi_dev->dev;
942 struct ipvl_dev *ipvlan = netdev_priv(dev);
944 if (!ipvlan_is_valid_dev(dev))
945 return NOTIFY_DONE;
947 switch (event) {
948 case NETDEV_UP:
949 if (ipvlan_addr_busy(ipvlan->port, &i6vi->i6vi_addr, true)) {
950 NL_SET_ERR_MSG(i6vi->extack,
951 "Address already assigned to an ipvlan device");
952 return notifier_from_errno(-EADDRINUSE);
954 break;
957 return NOTIFY_OK;
959 #endif
961 static int ipvlan_add_addr4(struct ipvl_dev *ipvlan, struct in_addr *ip4_addr)
963 int ret = -EINVAL;
965 spin_lock_bh(&ipvlan->addrs_lock);
966 if (ipvlan_addr_busy(ipvlan->port, ip4_addr, false))
967 netif_err(ipvlan, ifup, ipvlan->dev,
968 "Failed to add IPv4=%pI4 on %s intf.\n",
969 ip4_addr, ipvlan->dev->name);
970 else
971 ret = ipvlan_add_addr(ipvlan, ip4_addr, false);
972 spin_unlock_bh(&ipvlan->addrs_lock);
973 return ret;
976 static void ipvlan_del_addr4(struct ipvl_dev *ipvlan, struct in_addr *ip4_addr)
978 return ipvlan_del_addr(ipvlan, ip4_addr, false);
981 static int ipvlan_addr4_event(struct notifier_block *unused,
982 unsigned long event, void *ptr)
984 struct in_ifaddr *if4 = (struct in_ifaddr *)ptr;
985 struct net_device *dev = (struct net_device *)if4->ifa_dev->dev;
986 struct ipvl_dev *ipvlan = netdev_priv(dev);
987 struct in_addr ip4_addr;
989 if (!ipvlan_is_valid_dev(dev))
990 return NOTIFY_DONE;
992 switch (event) {
993 case NETDEV_UP:
994 ip4_addr.s_addr = if4->ifa_address;
995 if (ipvlan_add_addr4(ipvlan, &ip4_addr))
996 return NOTIFY_BAD;
997 break;
999 case NETDEV_DOWN:
1000 ip4_addr.s_addr = if4->ifa_address;
1001 ipvlan_del_addr4(ipvlan, &ip4_addr);
1002 break;
1005 return NOTIFY_OK;
1008 static int ipvlan_addr4_validator_event(struct notifier_block *unused,
1009 unsigned long event, void *ptr)
1011 struct in_validator_info *ivi = (struct in_validator_info *)ptr;
1012 struct net_device *dev = (struct net_device *)ivi->ivi_dev->dev;
1013 struct ipvl_dev *ipvlan = netdev_priv(dev);
1015 if (!ipvlan_is_valid_dev(dev))
1016 return NOTIFY_DONE;
1018 switch (event) {
1019 case NETDEV_UP:
1020 if (ipvlan_addr_busy(ipvlan->port, &ivi->ivi_addr, false)) {
1021 NL_SET_ERR_MSG(ivi->extack,
1022 "Address already assigned to an ipvlan device");
1023 return notifier_from_errno(-EADDRINUSE);
1025 break;
1028 return NOTIFY_OK;
1031 static struct notifier_block ipvlan_addr4_notifier_block __read_mostly = {
1032 .notifier_call = ipvlan_addr4_event,
1035 static struct notifier_block ipvlan_addr4_vtor_notifier_block __read_mostly = {
1036 .notifier_call = ipvlan_addr4_validator_event,
1039 static struct notifier_block ipvlan_notifier_block __read_mostly = {
1040 .notifier_call = ipvlan_device_event,
1043 #if IS_ENABLED(CONFIG_IPV6)
1044 static struct notifier_block ipvlan_addr6_notifier_block __read_mostly = {
1045 .notifier_call = ipvlan_addr6_event,
1048 static struct notifier_block ipvlan_addr6_vtor_notifier_block __read_mostly = {
1049 .notifier_call = ipvlan_addr6_validator_event,
1051 #endif
1053 static void ipvlan_ns_exit(struct net *net)
1055 struct ipvlan_netns *vnet = net_generic(net, ipvlan_netid);
1057 if (WARN_ON_ONCE(vnet->ipvl_nf_hook_refcnt)) {
1058 vnet->ipvl_nf_hook_refcnt = 0;
1059 nf_unregister_net_hooks(net, ipvl_nfops,
1060 ARRAY_SIZE(ipvl_nfops));
1064 static struct pernet_operations ipvlan_net_ops = {
1065 .id = &ipvlan_netid,
1066 .size = sizeof(struct ipvlan_netns),
1067 .exit = ipvlan_ns_exit,
1070 static int __init ipvlan_init_module(void)
1072 int err;
1074 ipvlan_init_secret();
1075 register_netdevice_notifier(&ipvlan_notifier_block);
1076 #if IS_ENABLED(CONFIG_IPV6)
1077 register_inet6addr_notifier(&ipvlan_addr6_notifier_block);
1078 register_inet6addr_validator_notifier(
1079 &ipvlan_addr6_vtor_notifier_block);
1080 #endif
1081 register_inetaddr_notifier(&ipvlan_addr4_notifier_block);
1082 register_inetaddr_validator_notifier(&ipvlan_addr4_vtor_notifier_block);
1084 err = register_pernet_subsys(&ipvlan_net_ops);
1085 if (err < 0)
1086 goto error;
1088 err = ipvlan_link_register(&ipvlan_link_ops);
1089 if (err < 0) {
1090 unregister_pernet_subsys(&ipvlan_net_ops);
1091 goto error;
1094 return 0;
1095 error:
1096 unregister_inetaddr_notifier(&ipvlan_addr4_notifier_block);
1097 unregister_inetaddr_validator_notifier(
1098 &ipvlan_addr4_vtor_notifier_block);
1099 #if IS_ENABLED(CONFIG_IPV6)
1100 unregister_inet6addr_notifier(&ipvlan_addr6_notifier_block);
1101 unregister_inet6addr_validator_notifier(
1102 &ipvlan_addr6_vtor_notifier_block);
1103 #endif
1104 unregister_netdevice_notifier(&ipvlan_notifier_block);
1105 return err;
1108 static void __exit ipvlan_cleanup_module(void)
1110 rtnl_link_unregister(&ipvlan_link_ops);
1111 unregister_pernet_subsys(&ipvlan_net_ops);
1112 unregister_netdevice_notifier(&ipvlan_notifier_block);
1113 unregister_inetaddr_notifier(&ipvlan_addr4_notifier_block);
1114 unregister_inetaddr_validator_notifier(
1115 &ipvlan_addr4_vtor_notifier_block);
1116 #if IS_ENABLED(CONFIG_IPV6)
1117 unregister_inet6addr_notifier(&ipvlan_addr6_notifier_block);
1118 unregister_inet6addr_validator_notifier(
1119 &ipvlan_addr6_vtor_notifier_block);
1120 #endif
1123 module_init(ipvlan_init_module);
1124 module_exit(ipvlan_cleanup_module);
1126 MODULE_LICENSE("GPL");
1127 MODULE_AUTHOR("Mahesh Bandewar <maheshb@google.com>");
1128 MODULE_DESCRIPTION("Driver for L3 (IPv6/IPv4) based VLANs");
1129 MODULE_ALIAS_RTNL_LINK("ipvlan");