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.
12 static unsigned int ipvlan_netid __read_mostly;
15 unsigned int ipvl_nf_hook_refcnt;
18 static const struct nf_hook_ops ipvl_nfops[] = {
20 .hook = ipvlan_nf_input,
22 .hooknum = NF_INET_LOCAL_IN,
26 .hook = ipvlan_nf_input,
28 .hooknum = NF_INET_LOCAL_IN,
33 static const struct l3mdev_ops ipvl_l3mdev_ops = {
34 .l3mdev_l3_rcv = ipvlan_l3_rcv,
37 static void ipvlan_adjust_mtu(struct ipvl_dev *ipvlan, struct net_device *dev)
39 ipvlan->dev->mtu = dev->mtu;
42 static int ipvlan_register_nf_hook(struct net *net)
44 struct ipvlan_netns *vnet = net_generic(net, ipvlan_netid);
47 if (!vnet->ipvl_nf_hook_refcnt) {
48 err = nf_register_net_hooks(net, ipvl_nfops,
49 ARRAY_SIZE(ipvl_nfops));
51 vnet->ipvl_nf_hook_refcnt = 1;
53 vnet->ipvl_nf_hook_refcnt++;
59 static void ipvlan_unregister_nf_hook(struct net *net)
61 struct ipvlan_netns *vnet = net_generic(net, ipvlan_netid);
63 if (WARN_ON(!vnet->ipvl_nf_hook_refcnt))
66 vnet->ipvl_nf_hook_refcnt--;
67 if (!vnet->ipvl_nf_hook_refcnt)
68 nf_unregister_net_hooks(net, ipvl_nfops,
69 ARRAY_SIZE(ipvl_nfops));
72 static int ipvlan_set_port_mode(struct ipvl_port *port, u16 nval)
74 struct ipvl_dev *ipvlan;
75 struct net_device *mdev = port->dev;
80 if (port->mode != nval) {
81 list_for_each_entry(ipvlan, &port->ipvlans, pnode) {
82 flags = ipvlan->dev->flags;
83 if (nval == IPVLAN_MODE_L3 || nval == IPVLAN_MODE_L3S) {
84 err = dev_change_flags(ipvlan->dev,
87 err = dev_change_flags(ipvlan->dev,
93 if (nval == IPVLAN_MODE_L3S) {
95 err = ipvlan_register_nf_hook(read_pnet(&port->pnet));
97 mdev->l3mdev_ops = &ipvl_l3mdev_ops;
98 mdev->priv_flags |= IFF_L3MDEV_RX_HANDLER;
101 } else if (port->mode == IPVLAN_MODE_L3S) {
102 /* Old mode was L3S */
103 mdev->priv_flags &= ~IFF_L3MDEV_RX_HANDLER;
104 ipvlan_unregister_nf_hook(read_pnet(&port->pnet));
105 mdev->l3mdev_ops = NULL;
112 /* Undo the flags changes that have been done so far. */
113 list_for_each_entry_continue_reverse(ipvlan, &port->ipvlans, pnode) {
114 flags = ipvlan->dev->flags;
115 if (port->mode == IPVLAN_MODE_L3 ||
116 port->mode == IPVLAN_MODE_L3S)
117 dev_change_flags(ipvlan->dev, flags | IFF_NOARP);
119 dev_change_flags(ipvlan->dev, flags & ~IFF_NOARP);
125 static int ipvlan_port_create(struct net_device *dev)
127 struct ipvl_port *port;
130 if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK) {
131 netdev_err(dev, "Master is either lo or non-ether device\n");
135 if (netdev_is_rx_handler_busy(dev)) {
136 netdev_err(dev, "Device is already in use.\n");
140 port = kzalloc(sizeof(struct ipvl_port), GFP_KERNEL);
144 write_pnet(&port->pnet, dev_net(dev));
146 port->mode = IPVLAN_MODE_L3;
147 INIT_LIST_HEAD(&port->ipvlans);
148 for (idx = 0; idx < IPVLAN_HASH_SIZE; idx++)
149 INIT_HLIST_HEAD(&port->hlhead[idx]);
151 skb_queue_head_init(&port->backlog);
152 INIT_WORK(&port->wq, ipvlan_process_multicast);
153 ida_init(&port->ida);
154 port->dev_id_start = 1;
156 err = netdev_rx_handler_register(dev, ipvlan_handle_frame, port);
160 dev->priv_flags |= IFF_IPVLAN_MASTER;
168 static void ipvlan_port_destroy(struct net_device *dev)
170 struct ipvl_port *port = ipvlan_port_get_rtnl(dev);
173 dev->priv_flags &= ~IFF_IPVLAN_MASTER;
174 if (port->mode == IPVLAN_MODE_L3S) {
175 dev->priv_flags &= ~IFF_L3MDEV_RX_HANDLER;
176 ipvlan_unregister_nf_hook(dev_net(dev));
177 dev->l3mdev_ops = NULL;
179 netdev_rx_handler_unregister(dev);
180 cancel_work_sync(&port->wq);
181 while ((skb = __skb_dequeue(&port->backlog)) != NULL) {
186 ida_destroy(&port->ida);
190 #define IPVLAN_ALWAYS_ON_OFLOADS \
191 (NETIF_F_SG | NETIF_F_HW_CSUM | \
192 NETIF_F_GSO_ROBUST | NETIF_F_GSO_SOFTWARE | NETIF_F_GSO_ENCAP_ALL)
194 #define IPVLAN_ALWAYS_ON \
195 (IPVLAN_ALWAYS_ON_OFLOADS | NETIF_F_LLTX | NETIF_F_VLAN_CHALLENGED)
197 #define IPVLAN_FEATURES \
198 (NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
199 NETIF_F_GSO | NETIF_F_TSO | NETIF_F_GSO_ROBUST | \
200 NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_GRO | NETIF_F_RXCSUM | \
201 NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_STAG_FILTER)
203 /* NETIF_F_GSO_ENCAP_ALL NETIF_F_GSO_SOFTWARE Newly added */
205 #define IPVLAN_STATE_MASK \
206 ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
208 static int ipvlan_init(struct net_device *dev)
210 struct ipvl_dev *ipvlan = netdev_priv(dev);
211 const struct net_device *phy_dev = ipvlan->phy_dev;
212 struct ipvl_port *port = ipvlan->port;
214 dev->state = (dev->state & ~IPVLAN_STATE_MASK) |
215 (phy_dev->state & IPVLAN_STATE_MASK);
216 dev->features = phy_dev->features & IPVLAN_FEATURES;
217 dev->features |= IPVLAN_ALWAYS_ON;
218 dev->vlan_features = phy_dev->vlan_features & IPVLAN_FEATURES;
219 dev->vlan_features |= IPVLAN_ALWAYS_ON_OFLOADS;
220 dev->gso_max_size = phy_dev->gso_max_size;
221 dev->gso_max_segs = phy_dev->gso_max_segs;
222 dev->hard_header_len = phy_dev->hard_header_len;
224 netdev_lockdep_set_classes(dev);
226 ipvlan->pcpu_stats = netdev_alloc_pcpu_stats(struct ipvl_pcpu_stats);
227 if (!ipvlan->pcpu_stats)
235 static void ipvlan_uninit(struct net_device *dev)
237 struct ipvl_dev *ipvlan = netdev_priv(dev);
238 struct ipvl_port *port = ipvlan->port;
240 free_percpu(ipvlan->pcpu_stats);
244 ipvlan_port_destroy(port->dev);
247 static int ipvlan_open(struct net_device *dev)
249 struct ipvl_dev *ipvlan = netdev_priv(dev);
250 struct ipvl_addr *addr;
252 if (ipvlan->port->mode == IPVLAN_MODE_L3 ||
253 ipvlan->port->mode == IPVLAN_MODE_L3S)
254 dev->flags |= IFF_NOARP;
256 dev->flags &= ~IFF_NOARP;
258 list_for_each_entry(addr, &ipvlan->addrs, anode)
259 ipvlan_ht_addr_add(ipvlan, addr);
264 static int ipvlan_stop(struct net_device *dev)
266 struct ipvl_dev *ipvlan = netdev_priv(dev);
267 struct net_device *phy_dev = ipvlan->phy_dev;
268 struct ipvl_addr *addr;
270 dev_uc_unsync(phy_dev, dev);
271 dev_mc_unsync(phy_dev, dev);
273 list_for_each_entry(addr, &ipvlan->addrs, anode)
274 ipvlan_ht_addr_del(addr);
279 static netdev_tx_t ipvlan_start_xmit(struct sk_buff *skb,
280 struct net_device *dev)
282 const struct ipvl_dev *ipvlan = netdev_priv(dev);
283 int skblen = skb->len;
286 ret = ipvlan_queue_xmit(skb, dev);
287 if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
288 struct ipvl_pcpu_stats *pcptr;
290 pcptr = this_cpu_ptr(ipvlan->pcpu_stats);
292 u64_stats_update_begin(&pcptr->syncp);
294 pcptr->tx_bytes += skblen;
295 u64_stats_update_end(&pcptr->syncp);
297 this_cpu_inc(ipvlan->pcpu_stats->tx_drps);
302 static netdev_features_t ipvlan_fix_features(struct net_device *dev,
303 netdev_features_t features)
305 struct ipvl_dev *ipvlan = netdev_priv(dev);
307 features |= NETIF_F_ALL_FOR_ALL;
308 features &= (ipvlan->sfeatures | ~IPVLAN_FEATURES);
309 features = netdev_increment_features(ipvlan->phy_dev->features,
311 features |= IPVLAN_ALWAYS_ON;
312 features &= (IPVLAN_FEATURES | IPVLAN_ALWAYS_ON);
317 static void ipvlan_change_rx_flags(struct net_device *dev, int change)
319 struct ipvl_dev *ipvlan = netdev_priv(dev);
320 struct net_device *phy_dev = ipvlan->phy_dev;
322 if (change & IFF_ALLMULTI)
323 dev_set_allmulti(phy_dev, dev->flags & IFF_ALLMULTI? 1 : -1);
326 static void ipvlan_set_multicast_mac_filter(struct net_device *dev)
328 struct ipvl_dev *ipvlan = netdev_priv(dev);
330 if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
331 bitmap_fill(ipvlan->mac_filters, IPVLAN_MAC_FILTER_SIZE);
333 struct netdev_hw_addr *ha;
334 DECLARE_BITMAP(mc_filters, IPVLAN_MAC_FILTER_SIZE);
336 bitmap_zero(mc_filters, IPVLAN_MAC_FILTER_SIZE);
337 netdev_for_each_mc_addr(ha, dev)
338 __set_bit(ipvlan_mac_hash(ha->addr), mc_filters);
340 /* Turn-on broadcast bit irrespective of address family,
341 * since broadcast is deferred to a work-queue, hence no
342 * impact on fast-path processing.
344 __set_bit(ipvlan_mac_hash(dev->broadcast), mc_filters);
346 bitmap_copy(ipvlan->mac_filters, mc_filters,
347 IPVLAN_MAC_FILTER_SIZE);
349 dev_uc_sync(ipvlan->phy_dev, dev);
350 dev_mc_sync(ipvlan->phy_dev, dev);
353 static void ipvlan_get_stats64(struct net_device *dev,
354 struct rtnl_link_stats64 *s)
356 struct ipvl_dev *ipvlan = netdev_priv(dev);
358 if (ipvlan->pcpu_stats) {
359 struct ipvl_pcpu_stats *pcptr;
360 u64 rx_pkts, rx_bytes, rx_mcast, tx_pkts, tx_bytes;
361 u32 rx_errs = 0, tx_drps = 0;
365 for_each_possible_cpu(idx) {
366 pcptr = per_cpu_ptr(ipvlan->pcpu_stats, idx);
368 strt= u64_stats_fetch_begin_irq(&pcptr->syncp);
369 rx_pkts = pcptr->rx_pkts;
370 rx_bytes = pcptr->rx_bytes;
371 rx_mcast = pcptr->rx_mcast;
372 tx_pkts = pcptr->tx_pkts;
373 tx_bytes = pcptr->tx_bytes;
374 } while (u64_stats_fetch_retry_irq(&pcptr->syncp,
377 s->rx_packets += rx_pkts;
378 s->rx_bytes += rx_bytes;
379 s->multicast += rx_mcast;
380 s->tx_packets += tx_pkts;
381 s->tx_bytes += tx_bytes;
383 /* u32 values are updated without syncp protection. */
384 rx_errs += pcptr->rx_errs;
385 tx_drps += pcptr->tx_drps;
387 s->rx_errors = rx_errs;
388 s->rx_dropped = rx_errs;
389 s->tx_dropped = tx_drps;
393 static int ipvlan_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
395 struct ipvl_dev *ipvlan = netdev_priv(dev);
396 struct net_device *phy_dev = ipvlan->phy_dev;
398 return vlan_vid_add(phy_dev, proto, vid);
401 static int ipvlan_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
404 struct ipvl_dev *ipvlan = netdev_priv(dev);
405 struct net_device *phy_dev = ipvlan->phy_dev;
407 vlan_vid_del(phy_dev, proto, vid);
411 static int ipvlan_get_iflink(const struct net_device *dev)
413 struct ipvl_dev *ipvlan = netdev_priv(dev);
415 return ipvlan->phy_dev->ifindex;
418 static const struct net_device_ops ipvlan_netdev_ops = {
419 .ndo_init = ipvlan_init,
420 .ndo_uninit = ipvlan_uninit,
421 .ndo_open = ipvlan_open,
422 .ndo_stop = ipvlan_stop,
423 .ndo_start_xmit = ipvlan_start_xmit,
424 .ndo_fix_features = ipvlan_fix_features,
425 .ndo_change_rx_flags = ipvlan_change_rx_flags,
426 .ndo_set_rx_mode = ipvlan_set_multicast_mac_filter,
427 .ndo_get_stats64 = ipvlan_get_stats64,
428 .ndo_vlan_rx_add_vid = ipvlan_vlan_rx_add_vid,
429 .ndo_vlan_rx_kill_vid = ipvlan_vlan_rx_kill_vid,
430 .ndo_get_iflink = ipvlan_get_iflink,
433 static int ipvlan_hard_header(struct sk_buff *skb, struct net_device *dev,
434 unsigned short type, const void *daddr,
435 const void *saddr, unsigned len)
437 const struct ipvl_dev *ipvlan = netdev_priv(dev);
438 struct net_device *phy_dev = ipvlan->phy_dev;
440 /* TODO Probably use a different field than dev_addr so that the
441 * mac-address on the virtual device is portable and can be carried
442 * while the packets use the mac-addr on the physical device.
444 return dev_hard_header(skb, phy_dev, type, daddr,
445 saddr ? : dev->dev_addr, len);
448 static const struct header_ops ipvlan_header_ops = {
449 .create = ipvlan_hard_header,
450 .parse = eth_header_parse,
451 .cache = eth_header_cache,
452 .cache_update = eth_header_cache_update,
455 static int ipvlan_ethtool_get_link_ksettings(struct net_device *dev,
456 struct ethtool_link_ksettings *cmd)
458 const struct ipvl_dev *ipvlan = netdev_priv(dev);
460 return __ethtool_get_link_ksettings(ipvlan->phy_dev, cmd);
463 static void ipvlan_ethtool_get_drvinfo(struct net_device *dev,
464 struct ethtool_drvinfo *drvinfo)
466 strlcpy(drvinfo->driver, IPVLAN_DRV, sizeof(drvinfo->driver));
467 strlcpy(drvinfo->version, IPV_DRV_VER, sizeof(drvinfo->version));
470 static u32 ipvlan_ethtool_get_msglevel(struct net_device *dev)
472 const struct ipvl_dev *ipvlan = netdev_priv(dev);
474 return ipvlan->msg_enable;
477 static void ipvlan_ethtool_set_msglevel(struct net_device *dev, u32 value)
479 struct ipvl_dev *ipvlan = netdev_priv(dev);
481 ipvlan->msg_enable = value;
484 static const struct ethtool_ops ipvlan_ethtool_ops = {
485 .get_link = ethtool_op_get_link,
486 .get_link_ksettings = ipvlan_ethtool_get_link_ksettings,
487 .get_drvinfo = ipvlan_ethtool_get_drvinfo,
488 .get_msglevel = ipvlan_ethtool_get_msglevel,
489 .set_msglevel = ipvlan_ethtool_set_msglevel,
492 static int ipvlan_nl_changelink(struct net_device *dev,
493 struct nlattr *tb[], struct nlattr *data[],
494 struct netlink_ext_ack *extack)
496 struct ipvl_dev *ipvlan = netdev_priv(dev);
497 struct ipvl_port *port = ipvlan_port_get_rtnl(ipvlan->phy_dev);
502 if (!ns_capable(dev_net(ipvlan->phy_dev)->user_ns, CAP_NET_ADMIN))
505 if (data[IFLA_IPVLAN_MODE]) {
506 u16 nmode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
508 err = ipvlan_set_port_mode(port, nmode);
513 static size_t ipvlan_nl_getsize(const struct net_device *dev)
516 + nla_total_size(2) /* IFLA_IPVLAN_MODE */
520 static int ipvlan_nl_validate(struct nlattr *tb[], struct nlattr *data[],
521 struct netlink_ext_ack *extack)
523 if (data && data[IFLA_IPVLAN_MODE]) {
524 u16 mode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
526 if (mode < IPVLAN_MODE_L2 || mode >= IPVLAN_MODE_MAX)
532 static int ipvlan_nl_fillinfo(struct sk_buff *skb,
533 const struct net_device *dev)
535 struct ipvl_dev *ipvlan = netdev_priv(dev);
536 struct ipvl_port *port = ipvlan_port_get_rtnl(ipvlan->phy_dev);
543 if (nla_put_u16(skb, IFLA_IPVLAN_MODE, port->mode))
552 int ipvlan_link_new(struct net *src_net, struct net_device *dev,
553 struct nlattr *tb[], struct nlattr *data[],
554 struct netlink_ext_ack *extack)
556 struct ipvl_dev *ipvlan = netdev_priv(dev);
557 struct ipvl_port *port;
558 struct net_device *phy_dev;
560 u16 mode = IPVLAN_MODE_L3;
566 phy_dev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
570 if (netif_is_ipvlan(phy_dev)) {
571 struct ipvl_dev *tmp = netdev_priv(phy_dev);
573 phy_dev = tmp->phy_dev;
574 if (!ns_capable(dev_net(phy_dev)->user_ns, CAP_NET_ADMIN))
576 } else if (!netif_is_ipvlan_port(phy_dev)) {
577 err = ipvlan_port_create(phy_dev);
583 if (data && data[IFLA_IPVLAN_MODE])
584 mode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
586 port = ipvlan_port_get_rtnl(phy_dev);
587 ipvlan->phy_dev = phy_dev;
590 ipvlan->sfeatures = IPVLAN_FEATURES;
592 ipvlan_adjust_mtu(ipvlan, phy_dev);
593 INIT_LIST_HEAD(&ipvlan->addrs);
595 /* If the port-id base is at the MAX value, then wrap it around and
596 * begin from 0x1 again. This may be due to a busy system where lots
597 * of slaves are getting created and deleted.
599 if (port->dev_id_start == 0xFFFE)
600 port->dev_id_start = 0x1;
602 /* Since L2 address is shared among all IPvlan slaves including
603 * master, use unique 16 bit dev-ids to diffentiate among them.
604 * Assign IDs between 0x1 and 0xFFFE (used by the master) to each
605 * slave link [see addrconf_ifid_eui48()].
607 err = ida_simple_get(&port->ida, port->dev_id_start, 0xFFFE,
610 err = ida_simple_get(&port->ida, 0x1, port->dev_id_start,
613 goto destroy_ipvlan_port;
615 /* Increment id-base to the next slot for the future assignment */
616 port->dev_id_start = err + 1;
618 /* TODO Probably put random address here to be presented to the
619 * world but keep using the physical-dev address for the outgoing
622 memcpy(dev->dev_addr, phy_dev->dev_addr, ETH_ALEN);
624 dev->priv_flags |= IFF_IPVLAN_SLAVE;
626 err = register_netdevice(dev);
630 err = netdev_upper_dev_link(phy_dev, dev);
632 goto unregister_netdev;
634 err = ipvlan_set_port_mode(port, mode);
639 list_add_tail_rcu(&ipvlan->pnode, &port->ipvlans);
640 netif_stacked_transfer_operstate(phy_dev, dev);
644 netdev_upper_dev_unlink(phy_dev, dev);
646 unregister_netdevice(dev);
648 ida_simple_remove(&port->ida, dev->dev_id);
651 ipvlan_port_destroy(phy_dev);
654 EXPORT_SYMBOL_GPL(ipvlan_link_new);
656 void ipvlan_link_delete(struct net_device *dev, struct list_head *head)
658 struct ipvl_dev *ipvlan = netdev_priv(dev);
659 struct ipvl_addr *addr, *next;
661 list_for_each_entry_safe(addr, next, &ipvlan->addrs, anode) {
662 ipvlan_ht_addr_del(addr);
663 list_del(&addr->anode);
664 kfree_rcu(addr, rcu);
667 ida_simple_remove(&ipvlan->port->ida, dev->dev_id);
668 list_del_rcu(&ipvlan->pnode);
669 unregister_netdevice_queue(dev, head);
670 netdev_upper_dev_unlink(ipvlan->phy_dev, dev);
672 EXPORT_SYMBOL_GPL(ipvlan_link_delete);
674 void ipvlan_link_setup(struct net_device *dev)
678 dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
679 dev->priv_flags |= IFF_UNICAST_FLT | IFF_NO_QUEUE;
680 dev->netdev_ops = &ipvlan_netdev_ops;
681 dev->needs_free_netdev = true;
682 dev->header_ops = &ipvlan_header_ops;
683 dev->ethtool_ops = &ipvlan_ethtool_ops;
685 EXPORT_SYMBOL_GPL(ipvlan_link_setup);
687 static const struct nla_policy ipvlan_nl_policy[IFLA_IPVLAN_MAX + 1] =
689 [IFLA_IPVLAN_MODE] = { .type = NLA_U16 },
692 static struct rtnl_link_ops ipvlan_link_ops = {
694 .priv_size = sizeof(struct ipvl_dev),
696 .setup = ipvlan_link_setup,
697 .newlink = ipvlan_link_new,
698 .dellink = ipvlan_link_delete,
701 int ipvlan_link_register(struct rtnl_link_ops *ops)
703 ops->get_size = ipvlan_nl_getsize;
704 ops->policy = ipvlan_nl_policy;
705 ops->validate = ipvlan_nl_validate;
706 ops->fill_info = ipvlan_nl_fillinfo;
707 ops->changelink = ipvlan_nl_changelink;
708 ops->maxtype = IFLA_IPVLAN_MAX;
709 return rtnl_link_register(ops);
711 EXPORT_SYMBOL_GPL(ipvlan_link_register);
713 static int ipvlan_device_event(struct notifier_block *unused,
714 unsigned long event, void *ptr)
716 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
717 struct ipvl_dev *ipvlan, *next;
718 struct ipvl_port *port;
721 if (!netif_is_ipvlan_port(dev))
724 port = ipvlan_port_get_rtnl(dev);
728 list_for_each_entry(ipvlan, &port->ipvlans, pnode)
729 netif_stacked_transfer_operstate(ipvlan->phy_dev,
733 case NETDEV_REGISTER: {
734 struct net *oldnet, *newnet = dev_net(dev);
735 struct ipvlan_netns *old_vnet;
737 oldnet = read_pnet(&port->pnet);
738 if (net_eq(newnet, oldnet))
741 write_pnet(&port->pnet, newnet);
743 old_vnet = net_generic(oldnet, ipvlan_netid);
744 if (!old_vnet->ipvl_nf_hook_refcnt)
747 ipvlan_register_nf_hook(newnet);
748 ipvlan_unregister_nf_hook(oldnet);
751 case NETDEV_UNREGISTER:
752 if (dev->reg_state != NETREG_UNREGISTERING)
755 list_for_each_entry_safe(ipvlan, next, &port->ipvlans,
757 ipvlan->dev->rtnl_link_ops->dellink(ipvlan->dev,
759 unregister_netdevice_many(&lst_kill);
762 case NETDEV_FEAT_CHANGE:
763 list_for_each_entry(ipvlan, &port->ipvlans, pnode) {
764 ipvlan->dev->gso_max_size = dev->gso_max_size;
765 ipvlan->dev->gso_max_segs = dev->gso_max_segs;
766 netdev_update_features(ipvlan->dev);
770 case NETDEV_CHANGEMTU:
771 list_for_each_entry(ipvlan, &port->ipvlans, pnode)
772 ipvlan_adjust_mtu(ipvlan, dev);
775 case NETDEV_PRE_TYPE_CHANGE:
776 /* Forbid underlying device to change its type. */
782 static int ipvlan_add_addr(struct ipvl_dev *ipvlan, void *iaddr, bool is_v6)
784 struct ipvl_addr *addr;
786 addr = kzalloc(sizeof(struct ipvl_addr), GFP_ATOMIC);
790 addr->master = ipvlan;
792 memcpy(&addr->ip6addr, iaddr, sizeof(struct in6_addr));
793 addr->atype = IPVL_IPV6;
795 memcpy(&addr->ip4addr, iaddr, sizeof(struct in_addr));
796 addr->atype = IPVL_IPV4;
798 list_add_tail(&addr->anode, &ipvlan->addrs);
800 /* If the interface is not up, the address will be added to the hash
801 * list by ipvlan_open.
803 if (netif_running(ipvlan->dev))
804 ipvlan_ht_addr_add(ipvlan, addr);
809 static void ipvlan_del_addr(struct ipvl_dev *ipvlan, void *iaddr, bool is_v6)
811 struct ipvl_addr *addr;
813 addr = ipvlan_find_addr(ipvlan, iaddr, is_v6);
817 ipvlan_ht_addr_del(addr);
818 list_del(&addr->anode);
819 kfree_rcu(addr, rcu);
824 static int ipvlan_add_addr6(struct ipvl_dev *ipvlan, struct in6_addr *ip6_addr)
826 if (ipvlan_addr_busy(ipvlan->port, ip6_addr, true)) {
827 netif_err(ipvlan, ifup, ipvlan->dev,
828 "Failed to add IPv6=%pI6c addr for %s intf\n",
829 ip6_addr, ipvlan->dev->name);
833 return ipvlan_add_addr(ipvlan, ip6_addr, true);
836 static void ipvlan_del_addr6(struct ipvl_dev *ipvlan, struct in6_addr *ip6_addr)
838 return ipvlan_del_addr(ipvlan, ip6_addr, true);
841 static int ipvlan_addr6_event(struct notifier_block *unused,
842 unsigned long event, void *ptr)
844 struct inet6_ifaddr *if6 = (struct inet6_ifaddr *)ptr;
845 struct net_device *dev = (struct net_device *)if6->idev->dev;
846 struct ipvl_dev *ipvlan = netdev_priv(dev);
848 /* FIXME IPv6 autoconf calls us from bh without RTNL */
852 if (!netif_is_ipvlan(dev))
855 if (!ipvlan || !ipvlan->port)
860 if (ipvlan_add_addr6(ipvlan, &if6->addr))
865 ipvlan_del_addr6(ipvlan, &if6->addr);
872 static int ipvlan_addr6_validator_event(struct notifier_block *unused,
873 unsigned long event, void *ptr)
875 struct in6_validator_info *i6vi = (struct in6_validator_info *)ptr;
876 struct net_device *dev = (struct net_device *)i6vi->i6vi_dev->dev;
877 struct ipvl_dev *ipvlan = netdev_priv(dev);
879 /* FIXME IPv6 autoconf calls us from bh without RTNL */
883 if (!netif_is_ipvlan(dev))
886 if (!ipvlan || !ipvlan->port)
891 if (ipvlan_addr_busy(ipvlan->port, &i6vi->i6vi_addr, true))
892 return notifier_from_errno(-EADDRINUSE);
899 static int ipvlan_add_addr4(struct ipvl_dev *ipvlan, struct in_addr *ip4_addr)
901 if (ipvlan_addr_busy(ipvlan->port, ip4_addr, false)) {
902 netif_err(ipvlan, ifup, ipvlan->dev,
903 "Failed to add IPv4=%pI4 on %s intf.\n",
904 ip4_addr, ipvlan->dev->name);
908 return ipvlan_add_addr(ipvlan, ip4_addr, false);
911 static void ipvlan_del_addr4(struct ipvl_dev *ipvlan, struct in_addr *ip4_addr)
913 return ipvlan_del_addr(ipvlan, ip4_addr, false);
916 static int ipvlan_addr4_event(struct notifier_block *unused,
917 unsigned long event, void *ptr)
919 struct in_ifaddr *if4 = (struct in_ifaddr *)ptr;
920 struct net_device *dev = (struct net_device *)if4->ifa_dev->dev;
921 struct ipvl_dev *ipvlan = netdev_priv(dev);
922 struct in_addr ip4_addr;
924 if (!netif_is_ipvlan(dev))
927 if (!ipvlan || !ipvlan->port)
932 ip4_addr.s_addr = if4->ifa_address;
933 if (ipvlan_add_addr4(ipvlan, &ip4_addr))
938 ip4_addr.s_addr = if4->ifa_address;
939 ipvlan_del_addr4(ipvlan, &ip4_addr);
946 static int ipvlan_addr4_validator_event(struct notifier_block *unused,
947 unsigned long event, void *ptr)
949 struct in_validator_info *ivi = (struct in_validator_info *)ptr;
950 struct net_device *dev = (struct net_device *)ivi->ivi_dev->dev;
951 struct ipvl_dev *ipvlan = netdev_priv(dev);
953 if (!netif_is_ipvlan(dev))
956 if (!ipvlan || !ipvlan->port)
961 if (ipvlan_addr_busy(ipvlan->port, &ivi->ivi_addr, false))
962 return notifier_from_errno(-EADDRINUSE);
969 static struct notifier_block ipvlan_addr4_notifier_block __read_mostly = {
970 .notifier_call = ipvlan_addr4_event,
973 static struct notifier_block ipvlan_addr4_vtor_notifier_block __read_mostly = {
974 .notifier_call = ipvlan_addr4_validator_event,
977 static struct notifier_block ipvlan_notifier_block __read_mostly = {
978 .notifier_call = ipvlan_device_event,
981 static struct notifier_block ipvlan_addr6_notifier_block __read_mostly = {
982 .notifier_call = ipvlan_addr6_event,
985 static struct notifier_block ipvlan_addr6_vtor_notifier_block __read_mostly = {
986 .notifier_call = ipvlan_addr6_validator_event,
989 static void ipvlan_ns_exit(struct net *net)
991 struct ipvlan_netns *vnet = net_generic(net, ipvlan_netid);
993 if (WARN_ON_ONCE(vnet->ipvl_nf_hook_refcnt)) {
994 vnet->ipvl_nf_hook_refcnt = 0;
995 nf_unregister_net_hooks(net, ipvl_nfops,
996 ARRAY_SIZE(ipvl_nfops));
1000 static struct pernet_operations ipvlan_net_ops = {
1001 .id = &ipvlan_netid,
1002 .size = sizeof(struct ipvlan_netns),
1003 .exit = ipvlan_ns_exit,
1006 static int __init ipvlan_init_module(void)
1010 ipvlan_init_secret();
1011 register_netdevice_notifier(&ipvlan_notifier_block);
1012 register_inet6addr_notifier(&ipvlan_addr6_notifier_block);
1013 register_inet6addr_validator_notifier(
1014 &ipvlan_addr6_vtor_notifier_block);
1015 register_inetaddr_notifier(&ipvlan_addr4_notifier_block);
1016 register_inetaddr_validator_notifier(&ipvlan_addr4_vtor_notifier_block);
1018 err = register_pernet_subsys(&ipvlan_net_ops);
1022 err = ipvlan_link_register(&ipvlan_link_ops);
1024 unregister_pernet_subsys(&ipvlan_net_ops);
1030 unregister_inetaddr_notifier(&ipvlan_addr4_notifier_block);
1031 unregister_inetaddr_validator_notifier(
1032 &ipvlan_addr4_vtor_notifier_block);
1033 unregister_inet6addr_notifier(&ipvlan_addr6_notifier_block);
1034 unregister_inet6addr_validator_notifier(
1035 &ipvlan_addr6_vtor_notifier_block);
1036 unregister_netdevice_notifier(&ipvlan_notifier_block);
1040 static void __exit ipvlan_cleanup_module(void)
1042 rtnl_link_unregister(&ipvlan_link_ops);
1043 unregister_pernet_subsys(&ipvlan_net_ops);
1044 unregister_netdevice_notifier(&ipvlan_notifier_block);
1045 unregister_inetaddr_notifier(&ipvlan_addr4_notifier_block);
1046 unregister_inetaddr_validator_notifier(
1047 &ipvlan_addr4_vtor_notifier_block);
1048 unregister_inet6addr_notifier(&ipvlan_addr6_notifier_block);
1049 unregister_inet6addr_validator_notifier(
1050 &ipvlan_addr6_vtor_notifier_block);
1053 module_init(ipvlan_init_module);
1054 module_exit(ipvlan_cleanup_module);
1056 MODULE_LICENSE("GPL");
1057 MODULE_AUTHOR("Mahesh Bandewar <maheshb@google.com>");
1058 MODULE_DESCRIPTION("Driver for L3 (IPv6/IPv4) based VLANs");
1059 MODULE_ALIAS_RTNL_LINK("ipvlan");