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 struct nf_hook_ops ipvl_nfops[] __read_mostly = {
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;
79 if (port->mode != nval) {
80 if (nval == IPVLAN_MODE_L3S) {
82 err = ipvlan_register_nf_hook(read_pnet(&port->pnet));
84 mdev->l3mdev_ops = &ipvl_l3mdev_ops;
85 mdev->priv_flags |= IFF_L3MDEV_MASTER;
88 } else if (port->mode == IPVLAN_MODE_L3S) {
89 /* Old mode was L3S */
90 mdev->priv_flags &= ~IFF_L3MDEV_MASTER;
91 ipvlan_unregister_nf_hook(read_pnet(&port->pnet));
92 mdev->l3mdev_ops = NULL;
94 list_for_each_entry(ipvlan, &port->ipvlans, pnode) {
95 if (nval == IPVLAN_MODE_L3 || nval == IPVLAN_MODE_L3S)
96 ipvlan->dev->flags |= IFF_NOARP;
98 ipvlan->dev->flags &= ~IFF_NOARP;
105 static int ipvlan_port_create(struct net_device *dev)
107 struct ipvl_port *port;
110 if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK) {
111 netdev_err(dev, "Master is either lo or non-ether device\n");
115 if (netdev_is_rx_handler_busy(dev)) {
116 netdev_err(dev, "Device is already in use.\n");
120 port = kzalloc(sizeof(struct ipvl_port), GFP_KERNEL);
124 write_pnet(&port->pnet, dev_net(dev));
126 port->mode = IPVLAN_MODE_L3;
127 INIT_LIST_HEAD(&port->ipvlans);
128 for (idx = 0; idx < IPVLAN_HASH_SIZE; idx++)
129 INIT_HLIST_HEAD(&port->hlhead[idx]);
131 skb_queue_head_init(&port->backlog);
132 INIT_WORK(&port->wq, ipvlan_process_multicast);
133 ida_init(&port->ida);
134 port->dev_id_start = 1;
136 err = netdev_rx_handler_register(dev, ipvlan_handle_frame, port);
140 dev->priv_flags |= IFF_IPVLAN_MASTER;
148 static void ipvlan_port_destroy(struct net_device *dev)
150 struct ipvl_port *port = ipvlan_port_get_rtnl(dev);
153 dev->priv_flags &= ~IFF_IPVLAN_MASTER;
154 if (port->mode == IPVLAN_MODE_L3S) {
155 dev->priv_flags &= ~IFF_L3MDEV_MASTER;
156 ipvlan_unregister_nf_hook(dev_net(dev));
157 dev->l3mdev_ops = NULL;
159 netdev_rx_handler_unregister(dev);
160 cancel_work_sync(&port->wq);
161 while ((skb = __skb_dequeue(&port->backlog)) != NULL) {
166 ida_destroy(&port->ida);
170 #define IPVLAN_FEATURES \
171 (NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
172 NETIF_F_GSO | NETIF_F_TSO | NETIF_F_UFO | NETIF_F_GSO_ROBUST | \
173 NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_GRO | NETIF_F_RXCSUM | \
174 NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_STAG_FILTER)
176 #define IPVLAN_STATE_MASK \
177 ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
179 static int ipvlan_init(struct net_device *dev)
181 struct ipvl_dev *ipvlan = netdev_priv(dev);
182 const struct net_device *phy_dev = ipvlan->phy_dev;
183 struct ipvl_port *port = ipvlan->port;
185 dev->state = (dev->state & ~IPVLAN_STATE_MASK) |
186 (phy_dev->state & IPVLAN_STATE_MASK);
187 dev->features = phy_dev->features & IPVLAN_FEATURES;
188 dev->features |= NETIF_F_LLTX;
189 dev->gso_max_size = phy_dev->gso_max_size;
190 dev->gso_max_segs = phy_dev->gso_max_segs;
191 dev->hard_header_len = phy_dev->hard_header_len;
193 netdev_lockdep_set_classes(dev);
195 ipvlan->pcpu_stats = alloc_percpu(struct ipvl_pcpu_stats);
196 if (!ipvlan->pcpu_stats)
204 static void ipvlan_uninit(struct net_device *dev)
206 struct ipvl_dev *ipvlan = netdev_priv(dev);
207 struct ipvl_port *port = ipvlan->port;
209 free_percpu(ipvlan->pcpu_stats);
213 ipvlan_port_destroy(port->dev);
216 static int ipvlan_open(struct net_device *dev)
218 struct ipvl_dev *ipvlan = netdev_priv(dev);
219 struct net_device *phy_dev = ipvlan->phy_dev;
220 struct ipvl_addr *addr;
222 if (ipvlan->port->mode == IPVLAN_MODE_L3 ||
223 ipvlan->port->mode == IPVLAN_MODE_L3S)
224 dev->flags |= IFF_NOARP;
226 dev->flags &= ~IFF_NOARP;
228 list_for_each_entry(addr, &ipvlan->addrs, anode)
229 ipvlan_ht_addr_add(ipvlan, addr);
231 return dev_uc_add(phy_dev, phy_dev->dev_addr);
234 static int ipvlan_stop(struct net_device *dev)
236 struct ipvl_dev *ipvlan = netdev_priv(dev);
237 struct net_device *phy_dev = ipvlan->phy_dev;
238 struct ipvl_addr *addr;
240 dev_uc_unsync(phy_dev, dev);
241 dev_mc_unsync(phy_dev, dev);
243 dev_uc_del(phy_dev, phy_dev->dev_addr);
245 list_for_each_entry(addr, &ipvlan->addrs, anode)
246 ipvlan_ht_addr_del(addr);
251 static netdev_tx_t ipvlan_start_xmit(struct sk_buff *skb,
252 struct net_device *dev)
254 const struct ipvl_dev *ipvlan = netdev_priv(dev);
255 int skblen = skb->len;
258 ret = ipvlan_queue_xmit(skb, dev);
259 if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
260 struct ipvl_pcpu_stats *pcptr;
262 pcptr = this_cpu_ptr(ipvlan->pcpu_stats);
264 u64_stats_update_begin(&pcptr->syncp);
266 pcptr->tx_bytes += skblen;
267 u64_stats_update_end(&pcptr->syncp);
269 this_cpu_inc(ipvlan->pcpu_stats->tx_drps);
274 static netdev_features_t ipvlan_fix_features(struct net_device *dev,
275 netdev_features_t features)
277 struct ipvl_dev *ipvlan = netdev_priv(dev);
279 return features & (ipvlan->sfeatures | ~IPVLAN_FEATURES);
282 static void ipvlan_change_rx_flags(struct net_device *dev, int change)
284 struct ipvl_dev *ipvlan = netdev_priv(dev);
285 struct net_device *phy_dev = ipvlan->phy_dev;
287 if (change & IFF_ALLMULTI)
288 dev_set_allmulti(phy_dev, dev->flags & IFF_ALLMULTI? 1 : -1);
291 static void ipvlan_set_multicast_mac_filter(struct net_device *dev)
293 struct ipvl_dev *ipvlan = netdev_priv(dev);
295 if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
296 bitmap_fill(ipvlan->mac_filters, IPVLAN_MAC_FILTER_SIZE);
298 struct netdev_hw_addr *ha;
299 DECLARE_BITMAP(mc_filters, IPVLAN_MAC_FILTER_SIZE);
301 bitmap_zero(mc_filters, IPVLAN_MAC_FILTER_SIZE);
302 netdev_for_each_mc_addr(ha, dev)
303 __set_bit(ipvlan_mac_hash(ha->addr), mc_filters);
305 /* Turn-on broadcast bit irrespective of address family,
306 * since broadcast is deferred to a work-queue, hence no
307 * impact on fast-path processing.
309 __set_bit(ipvlan_mac_hash(dev->broadcast), mc_filters);
311 bitmap_copy(ipvlan->mac_filters, mc_filters,
312 IPVLAN_MAC_FILTER_SIZE);
314 dev_uc_sync(ipvlan->phy_dev, dev);
315 dev_mc_sync(ipvlan->phy_dev, dev);
318 static void ipvlan_get_stats64(struct net_device *dev,
319 struct rtnl_link_stats64 *s)
321 struct ipvl_dev *ipvlan = netdev_priv(dev);
323 if (ipvlan->pcpu_stats) {
324 struct ipvl_pcpu_stats *pcptr;
325 u64 rx_pkts, rx_bytes, rx_mcast, tx_pkts, tx_bytes;
326 u32 rx_errs = 0, tx_drps = 0;
330 for_each_possible_cpu(idx) {
331 pcptr = per_cpu_ptr(ipvlan->pcpu_stats, idx);
333 strt= u64_stats_fetch_begin_irq(&pcptr->syncp);
334 rx_pkts = pcptr->rx_pkts;
335 rx_bytes = pcptr->rx_bytes;
336 rx_mcast = pcptr->rx_mcast;
337 tx_pkts = pcptr->tx_pkts;
338 tx_bytes = pcptr->tx_bytes;
339 } while (u64_stats_fetch_retry_irq(&pcptr->syncp,
342 s->rx_packets += rx_pkts;
343 s->rx_bytes += rx_bytes;
344 s->multicast += rx_mcast;
345 s->tx_packets += tx_pkts;
346 s->tx_bytes += tx_bytes;
348 /* u32 values are updated without syncp protection. */
349 rx_errs += pcptr->rx_errs;
350 tx_drps += pcptr->tx_drps;
352 s->rx_errors = rx_errs;
353 s->rx_dropped = rx_errs;
354 s->tx_dropped = tx_drps;
358 static int ipvlan_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
360 struct ipvl_dev *ipvlan = netdev_priv(dev);
361 struct net_device *phy_dev = ipvlan->phy_dev;
363 return vlan_vid_add(phy_dev, proto, vid);
366 static int ipvlan_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
369 struct ipvl_dev *ipvlan = netdev_priv(dev);
370 struct net_device *phy_dev = ipvlan->phy_dev;
372 vlan_vid_del(phy_dev, proto, vid);
376 static int ipvlan_get_iflink(const struct net_device *dev)
378 struct ipvl_dev *ipvlan = netdev_priv(dev);
380 return ipvlan->phy_dev->ifindex;
383 static const struct net_device_ops ipvlan_netdev_ops = {
384 .ndo_init = ipvlan_init,
385 .ndo_uninit = ipvlan_uninit,
386 .ndo_open = ipvlan_open,
387 .ndo_stop = ipvlan_stop,
388 .ndo_start_xmit = ipvlan_start_xmit,
389 .ndo_fix_features = ipvlan_fix_features,
390 .ndo_change_rx_flags = ipvlan_change_rx_flags,
391 .ndo_set_rx_mode = ipvlan_set_multicast_mac_filter,
392 .ndo_get_stats64 = ipvlan_get_stats64,
393 .ndo_vlan_rx_add_vid = ipvlan_vlan_rx_add_vid,
394 .ndo_vlan_rx_kill_vid = ipvlan_vlan_rx_kill_vid,
395 .ndo_get_iflink = ipvlan_get_iflink,
398 static int ipvlan_hard_header(struct sk_buff *skb, struct net_device *dev,
399 unsigned short type, const void *daddr,
400 const void *saddr, unsigned len)
402 const struct ipvl_dev *ipvlan = netdev_priv(dev);
403 struct net_device *phy_dev = ipvlan->phy_dev;
405 /* TODO Probably use a different field than dev_addr so that the
406 * mac-address on the virtual device is portable and can be carried
407 * while the packets use the mac-addr on the physical device.
409 return dev_hard_header(skb, phy_dev, type, daddr,
410 saddr ? : dev->dev_addr, len);
413 static const struct header_ops ipvlan_header_ops = {
414 .create = ipvlan_hard_header,
415 .parse = eth_header_parse,
416 .cache = eth_header_cache,
417 .cache_update = eth_header_cache_update,
420 static int ipvlan_ethtool_get_link_ksettings(struct net_device *dev,
421 struct ethtool_link_ksettings *cmd)
423 const struct ipvl_dev *ipvlan = netdev_priv(dev);
425 return __ethtool_get_link_ksettings(ipvlan->phy_dev, cmd);
428 static void ipvlan_ethtool_get_drvinfo(struct net_device *dev,
429 struct ethtool_drvinfo *drvinfo)
431 strlcpy(drvinfo->driver, IPVLAN_DRV, sizeof(drvinfo->driver));
432 strlcpy(drvinfo->version, IPV_DRV_VER, sizeof(drvinfo->version));
435 static u32 ipvlan_ethtool_get_msglevel(struct net_device *dev)
437 const struct ipvl_dev *ipvlan = netdev_priv(dev);
439 return ipvlan->msg_enable;
442 static void ipvlan_ethtool_set_msglevel(struct net_device *dev, u32 value)
444 struct ipvl_dev *ipvlan = netdev_priv(dev);
446 ipvlan->msg_enable = value;
449 static const struct ethtool_ops ipvlan_ethtool_ops = {
450 .get_link = ethtool_op_get_link,
451 .get_link_ksettings = ipvlan_ethtool_get_link_ksettings,
452 .get_drvinfo = ipvlan_ethtool_get_drvinfo,
453 .get_msglevel = ipvlan_ethtool_get_msglevel,
454 .set_msglevel = ipvlan_ethtool_set_msglevel,
457 static int ipvlan_nl_changelink(struct net_device *dev,
458 struct nlattr *tb[], struct nlattr *data[])
460 struct ipvl_dev *ipvlan = netdev_priv(dev);
461 struct ipvl_port *port = ipvlan_port_get_rtnl(ipvlan->phy_dev);
464 if (data && data[IFLA_IPVLAN_MODE]) {
465 u16 nmode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
467 err = ipvlan_set_port_mode(port, nmode);
472 static size_t ipvlan_nl_getsize(const struct net_device *dev)
475 + nla_total_size(2) /* IFLA_IPVLAN_MODE */
479 static int ipvlan_nl_validate(struct nlattr *tb[], struct nlattr *data[])
481 if (data && data[IFLA_IPVLAN_MODE]) {
482 u16 mode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
484 if (mode < IPVLAN_MODE_L2 || mode >= IPVLAN_MODE_MAX)
490 static int ipvlan_nl_fillinfo(struct sk_buff *skb,
491 const struct net_device *dev)
493 struct ipvl_dev *ipvlan = netdev_priv(dev);
494 struct ipvl_port *port = ipvlan_port_get_rtnl(ipvlan->phy_dev);
501 if (nla_put_u16(skb, IFLA_IPVLAN_MODE, port->mode))
510 int ipvlan_link_new(struct net *src_net, struct net_device *dev,
511 struct nlattr *tb[], struct nlattr *data[])
513 struct ipvl_dev *ipvlan = netdev_priv(dev);
514 struct ipvl_port *port;
515 struct net_device *phy_dev;
517 u16 mode = IPVLAN_MODE_L3;
523 phy_dev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
527 if (netif_is_ipvlan(phy_dev)) {
528 struct ipvl_dev *tmp = netdev_priv(phy_dev);
530 phy_dev = tmp->phy_dev;
531 } else if (!netif_is_ipvlan_port(phy_dev)) {
532 err = ipvlan_port_create(phy_dev);
538 if (data && data[IFLA_IPVLAN_MODE])
539 mode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
541 port = ipvlan_port_get_rtnl(phy_dev);
542 ipvlan->phy_dev = phy_dev;
545 ipvlan->sfeatures = IPVLAN_FEATURES;
546 ipvlan_adjust_mtu(ipvlan, phy_dev);
547 INIT_LIST_HEAD(&ipvlan->addrs);
549 /* If the port-id base is at the MAX value, then wrap it around and
550 * begin from 0x1 again. This may be due to a busy system where lots
551 * of slaves are getting created and deleted.
553 if (port->dev_id_start == 0xFFFE)
554 port->dev_id_start = 0x1;
556 /* Since L2 address is shared among all IPvlan slaves including
557 * master, use unique 16 bit dev-ids to diffentiate among them.
558 * Assign IDs between 0x1 and 0xFFFE (used by the master) to each
559 * slave link [see addrconf_ifid_eui48()].
561 err = ida_simple_get(&port->ida, port->dev_id_start, 0xFFFE,
564 err = ida_simple_get(&port->ida, 0x1, port->dev_id_start,
567 goto destroy_ipvlan_port;
569 /* Increment id-base to the next slot for the future assignment */
570 port->dev_id_start = err + 1;
572 /* TODO Probably put random address here to be presented to the
573 * world but keep using the physical-dev address for the outgoing
576 memcpy(dev->dev_addr, phy_dev->dev_addr, ETH_ALEN);
578 dev->priv_flags |= IFF_IPVLAN_SLAVE;
580 err = register_netdevice(dev);
584 err = netdev_upper_dev_link(phy_dev, dev);
586 goto unregister_netdev;
588 err = ipvlan_set_port_mode(port, mode);
593 list_add_tail_rcu(&ipvlan->pnode, &port->ipvlans);
594 netif_stacked_transfer_operstate(phy_dev, dev);
598 netdev_upper_dev_unlink(phy_dev, dev);
600 unregister_netdevice(dev);
602 ida_simple_remove(&port->ida, dev->dev_id);
605 ipvlan_port_destroy(phy_dev);
608 EXPORT_SYMBOL_GPL(ipvlan_link_new);
610 void ipvlan_link_delete(struct net_device *dev, struct list_head *head)
612 struct ipvl_dev *ipvlan = netdev_priv(dev);
613 struct ipvl_addr *addr, *next;
615 list_for_each_entry_safe(addr, next, &ipvlan->addrs, anode) {
616 ipvlan_ht_addr_del(addr);
617 list_del(&addr->anode);
618 kfree_rcu(addr, rcu);
621 ida_simple_remove(&ipvlan->port->ida, dev->dev_id);
622 list_del_rcu(&ipvlan->pnode);
623 unregister_netdevice_queue(dev, head);
624 netdev_upper_dev_unlink(ipvlan->phy_dev, dev);
626 EXPORT_SYMBOL_GPL(ipvlan_link_delete);
628 void ipvlan_link_setup(struct net_device *dev)
632 dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
633 dev->priv_flags |= IFF_UNICAST_FLT | IFF_NO_QUEUE;
634 dev->netdev_ops = &ipvlan_netdev_ops;
635 dev->needs_free_netdev = true;
636 dev->header_ops = &ipvlan_header_ops;
637 dev->ethtool_ops = &ipvlan_ethtool_ops;
639 EXPORT_SYMBOL_GPL(ipvlan_link_setup);
641 static const struct nla_policy ipvlan_nl_policy[IFLA_IPVLAN_MAX + 1] =
643 [IFLA_IPVLAN_MODE] = { .type = NLA_U16 },
646 static struct rtnl_link_ops ipvlan_link_ops = {
648 .priv_size = sizeof(struct ipvl_dev),
650 .setup = ipvlan_link_setup,
651 .newlink = ipvlan_link_new,
652 .dellink = ipvlan_link_delete,
655 int ipvlan_link_register(struct rtnl_link_ops *ops)
657 ops->get_size = ipvlan_nl_getsize;
658 ops->policy = ipvlan_nl_policy;
659 ops->validate = ipvlan_nl_validate;
660 ops->fill_info = ipvlan_nl_fillinfo;
661 ops->changelink = ipvlan_nl_changelink;
662 ops->maxtype = IFLA_IPVLAN_MAX;
663 return rtnl_link_register(ops);
665 EXPORT_SYMBOL_GPL(ipvlan_link_register);
667 static int ipvlan_device_event(struct notifier_block *unused,
668 unsigned long event, void *ptr)
670 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
671 struct ipvl_dev *ipvlan, *next;
672 struct ipvl_port *port;
675 if (!netif_is_ipvlan_port(dev))
678 port = ipvlan_port_get_rtnl(dev);
682 list_for_each_entry(ipvlan, &port->ipvlans, pnode)
683 netif_stacked_transfer_operstate(ipvlan->phy_dev,
687 case NETDEV_REGISTER: {
688 struct net *oldnet, *newnet = dev_net(dev);
689 struct ipvlan_netns *old_vnet;
691 oldnet = read_pnet(&port->pnet);
692 if (net_eq(newnet, oldnet))
695 write_pnet(&port->pnet, newnet);
697 old_vnet = net_generic(oldnet, ipvlan_netid);
698 if (!old_vnet->ipvl_nf_hook_refcnt)
701 ipvlan_register_nf_hook(newnet);
702 ipvlan_unregister_nf_hook(oldnet);
705 case NETDEV_UNREGISTER:
706 if (dev->reg_state != NETREG_UNREGISTERING)
709 list_for_each_entry_safe(ipvlan, next, &port->ipvlans,
711 ipvlan->dev->rtnl_link_ops->dellink(ipvlan->dev,
713 unregister_netdevice_many(&lst_kill);
716 case NETDEV_FEAT_CHANGE:
717 list_for_each_entry(ipvlan, &port->ipvlans, pnode) {
718 ipvlan->dev->features = dev->features & IPVLAN_FEATURES;
719 ipvlan->dev->gso_max_size = dev->gso_max_size;
720 ipvlan->dev->gso_max_segs = dev->gso_max_segs;
721 netdev_features_change(ipvlan->dev);
725 case NETDEV_CHANGEMTU:
726 list_for_each_entry(ipvlan, &port->ipvlans, pnode)
727 ipvlan_adjust_mtu(ipvlan, dev);
730 case NETDEV_PRE_TYPE_CHANGE:
731 /* Forbid underlying device to change its type. */
737 static int ipvlan_add_addr(struct ipvl_dev *ipvlan, void *iaddr, bool is_v6)
739 struct ipvl_addr *addr;
741 addr = kzalloc(sizeof(struct ipvl_addr), GFP_ATOMIC);
745 addr->master = ipvlan;
747 memcpy(&addr->ip6addr, iaddr, sizeof(struct in6_addr));
748 addr->atype = IPVL_IPV6;
750 memcpy(&addr->ip4addr, iaddr, sizeof(struct in_addr));
751 addr->atype = IPVL_IPV4;
753 list_add_tail(&addr->anode, &ipvlan->addrs);
755 /* If the interface is not up, the address will be added to the hash
756 * list by ipvlan_open.
758 if (netif_running(ipvlan->dev))
759 ipvlan_ht_addr_add(ipvlan, addr);
764 static void ipvlan_del_addr(struct ipvl_dev *ipvlan, void *iaddr, bool is_v6)
766 struct ipvl_addr *addr;
768 addr = ipvlan_find_addr(ipvlan, iaddr, is_v6);
772 ipvlan_ht_addr_del(addr);
773 list_del(&addr->anode);
774 kfree_rcu(addr, rcu);
779 static int ipvlan_add_addr6(struct ipvl_dev *ipvlan, struct in6_addr *ip6_addr)
781 if (ipvlan_addr_busy(ipvlan->port, ip6_addr, true)) {
782 netif_err(ipvlan, ifup, ipvlan->dev,
783 "Failed to add IPv6=%pI6c addr for %s intf\n",
784 ip6_addr, ipvlan->dev->name);
788 return ipvlan_add_addr(ipvlan, ip6_addr, true);
791 static void ipvlan_del_addr6(struct ipvl_dev *ipvlan, struct in6_addr *ip6_addr)
793 return ipvlan_del_addr(ipvlan, ip6_addr, true);
796 static int ipvlan_addr6_event(struct notifier_block *unused,
797 unsigned long event, void *ptr)
799 struct inet6_ifaddr *if6 = (struct inet6_ifaddr *)ptr;
800 struct net_device *dev = (struct net_device *)if6->idev->dev;
801 struct ipvl_dev *ipvlan = netdev_priv(dev);
803 /* FIXME IPv6 autoconf calls us from bh without RTNL */
807 if (!netif_is_ipvlan(dev))
810 if (!ipvlan || !ipvlan->port)
815 if (ipvlan_add_addr6(ipvlan, &if6->addr))
820 ipvlan_del_addr6(ipvlan, &if6->addr);
827 static int ipvlan_addr6_validator_event(struct notifier_block *unused,
828 unsigned long event, void *ptr)
830 struct in6_validator_info *i6vi = (struct in6_validator_info *)ptr;
831 struct net_device *dev = (struct net_device *)i6vi->i6vi_dev->dev;
832 struct ipvl_dev *ipvlan = netdev_priv(dev);
834 /* FIXME IPv6 autoconf calls us from bh without RTNL */
838 if (!netif_is_ipvlan(dev))
841 if (!ipvlan || !ipvlan->port)
846 if (ipvlan_addr_busy(ipvlan->port, &i6vi->i6vi_addr, true))
847 return notifier_from_errno(-EADDRINUSE);
854 static int ipvlan_add_addr4(struct ipvl_dev *ipvlan, struct in_addr *ip4_addr)
856 if (ipvlan_addr_busy(ipvlan->port, ip4_addr, false)) {
857 netif_err(ipvlan, ifup, ipvlan->dev,
858 "Failed to add IPv4=%pI4 on %s intf.\n",
859 ip4_addr, ipvlan->dev->name);
863 return ipvlan_add_addr(ipvlan, ip4_addr, false);
866 static void ipvlan_del_addr4(struct ipvl_dev *ipvlan, struct in_addr *ip4_addr)
868 return ipvlan_del_addr(ipvlan, ip4_addr, false);
871 static int ipvlan_addr4_event(struct notifier_block *unused,
872 unsigned long event, void *ptr)
874 struct in_ifaddr *if4 = (struct in_ifaddr *)ptr;
875 struct net_device *dev = (struct net_device *)if4->ifa_dev->dev;
876 struct ipvl_dev *ipvlan = netdev_priv(dev);
877 struct in_addr ip4_addr;
879 if (!netif_is_ipvlan(dev))
882 if (!ipvlan || !ipvlan->port)
887 ip4_addr.s_addr = if4->ifa_address;
888 if (ipvlan_add_addr4(ipvlan, &ip4_addr))
893 ip4_addr.s_addr = if4->ifa_address;
894 ipvlan_del_addr4(ipvlan, &ip4_addr);
901 static int ipvlan_addr4_validator_event(struct notifier_block *unused,
902 unsigned long event, void *ptr)
904 struct in_validator_info *ivi = (struct in_validator_info *)ptr;
905 struct net_device *dev = (struct net_device *)ivi->ivi_dev->dev;
906 struct ipvl_dev *ipvlan = netdev_priv(dev);
908 if (!netif_is_ipvlan(dev))
911 if (!ipvlan || !ipvlan->port)
916 if (ipvlan_addr_busy(ipvlan->port, &ivi->ivi_addr, false))
917 return notifier_from_errno(-EADDRINUSE);
924 static struct notifier_block ipvlan_addr4_notifier_block __read_mostly = {
925 .notifier_call = ipvlan_addr4_event,
928 static struct notifier_block ipvlan_addr4_vtor_notifier_block __read_mostly = {
929 .notifier_call = ipvlan_addr4_validator_event,
932 static struct notifier_block ipvlan_notifier_block __read_mostly = {
933 .notifier_call = ipvlan_device_event,
936 static struct notifier_block ipvlan_addr6_notifier_block __read_mostly = {
937 .notifier_call = ipvlan_addr6_event,
940 static struct notifier_block ipvlan_addr6_vtor_notifier_block __read_mostly = {
941 .notifier_call = ipvlan_addr6_validator_event,
944 static void ipvlan_ns_exit(struct net *net)
946 struct ipvlan_netns *vnet = net_generic(net, ipvlan_netid);
948 if (WARN_ON_ONCE(vnet->ipvl_nf_hook_refcnt)) {
949 vnet->ipvl_nf_hook_refcnt = 0;
950 nf_unregister_net_hooks(net, ipvl_nfops,
951 ARRAY_SIZE(ipvl_nfops));
955 static struct pernet_operations ipvlan_net_ops = {
957 .size = sizeof(struct ipvlan_netns),
958 .exit = ipvlan_ns_exit,
961 static int __init ipvlan_init_module(void)
965 ipvlan_init_secret();
966 register_netdevice_notifier(&ipvlan_notifier_block);
967 register_inet6addr_notifier(&ipvlan_addr6_notifier_block);
968 register_inet6addr_validator_notifier(
969 &ipvlan_addr6_vtor_notifier_block);
970 register_inetaddr_notifier(&ipvlan_addr4_notifier_block);
971 register_inetaddr_validator_notifier(&ipvlan_addr4_vtor_notifier_block);
973 err = register_pernet_subsys(&ipvlan_net_ops);
977 err = ipvlan_link_register(&ipvlan_link_ops);
979 unregister_pernet_subsys(&ipvlan_net_ops);
985 unregister_inetaddr_notifier(&ipvlan_addr4_notifier_block);
986 unregister_inetaddr_validator_notifier(
987 &ipvlan_addr4_vtor_notifier_block);
988 unregister_inet6addr_notifier(&ipvlan_addr6_notifier_block);
989 unregister_inet6addr_validator_notifier(
990 &ipvlan_addr6_vtor_notifier_block);
991 unregister_netdevice_notifier(&ipvlan_notifier_block);
995 static void __exit ipvlan_cleanup_module(void)
997 rtnl_link_unregister(&ipvlan_link_ops);
998 unregister_pernet_subsys(&ipvlan_net_ops);
999 unregister_netdevice_notifier(&ipvlan_notifier_block);
1000 unregister_inetaddr_notifier(&ipvlan_addr4_notifier_block);
1001 unregister_inetaddr_validator_notifier(
1002 &ipvlan_addr4_vtor_notifier_block);
1003 unregister_inet6addr_notifier(&ipvlan_addr6_notifier_block);
1004 unregister_inet6addr_validator_notifier(
1005 &ipvlan_addr6_vtor_notifier_block);
1008 module_init(ipvlan_init_module);
1009 module_exit(ipvlan_cleanup_module);
1011 MODULE_LICENSE("GPL");
1012 MODULE_AUTHOR("Mahesh Bandewar <maheshb@google.com>");
1013 MODULE_DESCRIPTION("Driver for L3 (IPv6/IPv4) based VLANs");
1014 MODULE_ALIAS_RTNL_LINK("ipvlan");