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,
25 #if IS_ENABLED(CONFIG_IPV6)
27 .hook = ipvlan_nf_input,
29 .hooknum = NF_INET_LOCAL_IN,
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);
49 if (!vnet->ipvl_nf_hook_refcnt) {
50 err = nf_register_net_hooks(net, ipvl_nfops,
51 ARRAY_SIZE(ipvl_nfops));
53 vnet->ipvl_nf_hook_refcnt = 1;
55 vnet->ipvl_nf_hook_refcnt++;
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))
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;
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,
89 err = dev_change_flags(ipvlan->dev,
95 if (nval == IPVLAN_MODE_L3S) {
97 err = ipvlan_register_nf_hook(read_pnet(&port->pnet));
99 mdev->l3mdev_ops = &ipvl_l3mdev_ops;
100 mdev->priv_flags |= IFF_L3MDEV_RX_HANDLER;
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;
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);
121 dev_change_flags(ipvlan->dev, flags & ~IFF_NOARP);
127 static int ipvlan_port_create(struct net_device *dev)
129 struct ipvl_port *port;
132 port = kzalloc(sizeof(struct ipvl_port), GFP_KERNEL);
136 write_pnet(&port->pnet, dev_net(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);
159 static void ipvlan_port_destroy(struct net_device *dev)
161 struct ipvl_port *port = ipvlan_port_get_rtnl(dev);
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) {
176 ida_destroy(&port->ida);
180 #define IPVLAN_ALWAYS_ON_OFLOADS \
181 (NETIF_F_SG | NETIF_F_HW_CSUM | \
182 NETIF_F_GSO_ROBUST | NETIF_F_GSO_SOFTWARE | NETIF_F_GSO_ENCAP_ALL)
184 #define IPVLAN_ALWAYS_ON \
185 (IPVLAN_ALWAYS_ON_OFLOADS | NETIF_F_LLTX | NETIF_F_VLAN_CHALLENGED)
187 #define IPVLAN_FEATURES \
188 (NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
189 NETIF_F_GSO | NETIF_F_TSO | NETIF_F_GSO_ROBUST | \
190 NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_GRO | NETIF_F_RXCSUM | \
191 NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_STAG_FILTER)
193 /* NETIF_F_GSO_ENCAP_ALL NETIF_F_GSO_SOFTWARE Newly added */
195 #define IPVLAN_STATE_MASK \
196 ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
198 static int ipvlan_init(struct net_device *dev)
200 struct ipvl_dev *ipvlan = netdev_priv(dev);
201 struct net_device *phy_dev = ipvlan->phy_dev;
202 struct ipvl_port *port;
205 dev->state = (dev->state & ~IPVLAN_STATE_MASK) |
206 (phy_dev->state & IPVLAN_STATE_MASK);
207 dev->features = phy_dev->features & IPVLAN_FEATURES;
208 dev->features |= IPVLAN_ALWAYS_ON;
209 dev->vlan_features = phy_dev->vlan_features & IPVLAN_FEATURES;
210 dev->vlan_features |= IPVLAN_ALWAYS_ON_OFLOADS;
211 dev->gso_max_size = phy_dev->gso_max_size;
212 dev->gso_max_segs = phy_dev->gso_max_segs;
213 dev->hard_header_len = phy_dev->hard_header_len;
215 netdev_lockdep_set_classes(dev);
217 ipvlan->pcpu_stats = netdev_alloc_pcpu_stats(struct ipvl_pcpu_stats);
218 if (!ipvlan->pcpu_stats)
221 if (!netif_is_ipvlan_port(phy_dev)) {
222 err = ipvlan_port_create(phy_dev);
224 free_percpu(ipvlan->pcpu_stats);
228 port = ipvlan_port_get_rtnl(phy_dev);
233 static void ipvlan_uninit(struct net_device *dev)
235 struct ipvl_dev *ipvlan = netdev_priv(dev);
236 struct net_device *phy_dev = ipvlan->phy_dev;
237 struct ipvl_port *port;
239 free_percpu(ipvlan->pcpu_stats);
241 port = ipvlan_port_get_rtnl(phy_dev);
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;
259 list_for_each_entry_rcu(addr, &ipvlan->addrs, anode)
260 ipvlan_ht_addr_add(ipvlan, addr);
266 static int ipvlan_stop(struct net_device *dev)
268 struct ipvl_dev *ipvlan = netdev_priv(dev);
269 struct net_device *phy_dev = ipvlan->phy_dev;
270 struct ipvl_addr *addr;
272 dev_uc_unsync(phy_dev, dev);
273 dev_mc_unsync(phy_dev, dev);
276 list_for_each_entry_rcu(addr, &ipvlan->addrs, anode)
277 ipvlan_ht_addr_del(addr);
283 static netdev_tx_t ipvlan_start_xmit(struct sk_buff *skb,
284 struct net_device *dev)
286 const struct ipvl_dev *ipvlan = netdev_priv(dev);
287 int skblen = skb->len;
290 ret = ipvlan_queue_xmit(skb, dev);
291 if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
292 struct ipvl_pcpu_stats *pcptr;
294 pcptr = this_cpu_ptr(ipvlan->pcpu_stats);
296 u64_stats_update_begin(&pcptr->syncp);
298 pcptr->tx_bytes += skblen;
299 u64_stats_update_end(&pcptr->syncp);
301 this_cpu_inc(ipvlan->pcpu_stats->tx_drps);
306 static netdev_features_t ipvlan_fix_features(struct net_device *dev,
307 netdev_features_t features)
309 struct ipvl_dev *ipvlan = netdev_priv(dev);
311 features |= NETIF_F_ALL_FOR_ALL;
312 features &= (ipvlan->sfeatures | ~IPVLAN_FEATURES);
313 features = netdev_increment_features(ipvlan->phy_dev->features,
315 features |= IPVLAN_ALWAYS_ON;
316 features &= (IPVLAN_FEATURES | IPVLAN_ALWAYS_ON);
321 static void ipvlan_change_rx_flags(struct net_device *dev, int change)
323 struct ipvl_dev *ipvlan = netdev_priv(dev);
324 struct net_device *phy_dev = ipvlan->phy_dev;
326 if (change & IFF_ALLMULTI)
327 dev_set_allmulti(phy_dev, dev->flags & IFF_ALLMULTI? 1 : -1);
330 static void ipvlan_set_multicast_mac_filter(struct net_device *dev)
332 struct ipvl_dev *ipvlan = netdev_priv(dev);
334 if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
335 bitmap_fill(ipvlan->mac_filters, IPVLAN_MAC_FILTER_SIZE);
337 struct netdev_hw_addr *ha;
338 DECLARE_BITMAP(mc_filters, IPVLAN_MAC_FILTER_SIZE);
340 bitmap_zero(mc_filters, IPVLAN_MAC_FILTER_SIZE);
341 netdev_for_each_mc_addr(ha, dev)
342 __set_bit(ipvlan_mac_hash(ha->addr), mc_filters);
344 /* Turn-on broadcast bit irrespective of address family,
345 * since broadcast is deferred to a work-queue, hence no
346 * impact on fast-path processing.
348 __set_bit(ipvlan_mac_hash(dev->broadcast), mc_filters);
350 bitmap_copy(ipvlan->mac_filters, mc_filters,
351 IPVLAN_MAC_FILTER_SIZE);
353 dev_uc_sync(ipvlan->phy_dev, dev);
354 dev_mc_sync(ipvlan->phy_dev, dev);
357 static void ipvlan_get_stats64(struct net_device *dev,
358 struct rtnl_link_stats64 *s)
360 struct ipvl_dev *ipvlan = netdev_priv(dev);
362 if (ipvlan->pcpu_stats) {
363 struct ipvl_pcpu_stats *pcptr;
364 u64 rx_pkts, rx_bytes, rx_mcast, tx_pkts, tx_bytes;
365 u32 rx_errs = 0, tx_drps = 0;
369 for_each_possible_cpu(idx) {
370 pcptr = per_cpu_ptr(ipvlan->pcpu_stats, idx);
372 strt= u64_stats_fetch_begin_irq(&pcptr->syncp);
373 rx_pkts = pcptr->rx_pkts;
374 rx_bytes = pcptr->rx_bytes;
375 rx_mcast = pcptr->rx_mcast;
376 tx_pkts = pcptr->tx_pkts;
377 tx_bytes = pcptr->tx_bytes;
378 } while (u64_stats_fetch_retry_irq(&pcptr->syncp,
381 s->rx_packets += rx_pkts;
382 s->rx_bytes += rx_bytes;
383 s->multicast += rx_mcast;
384 s->tx_packets += tx_pkts;
385 s->tx_bytes += tx_bytes;
387 /* u32 values are updated without syncp protection. */
388 rx_errs += pcptr->rx_errs;
389 tx_drps += pcptr->tx_drps;
391 s->rx_errors = rx_errs;
392 s->rx_dropped = rx_errs;
393 s->tx_dropped = tx_drps;
397 static int ipvlan_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
399 struct ipvl_dev *ipvlan = netdev_priv(dev);
400 struct net_device *phy_dev = ipvlan->phy_dev;
402 return vlan_vid_add(phy_dev, proto, vid);
405 static int ipvlan_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
408 struct ipvl_dev *ipvlan = netdev_priv(dev);
409 struct net_device *phy_dev = ipvlan->phy_dev;
411 vlan_vid_del(phy_dev, proto, vid);
415 static int ipvlan_get_iflink(const struct net_device *dev)
417 struct ipvl_dev *ipvlan = netdev_priv(dev);
419 return ipvlan->phy_dev->ifindex;
422 static const struct net_device_ops ipvlan_netdev_ops = {
423 .ndo_init = ipvlan_init,
424 .ndo_uninit = ipvlan_uninit,
425 .ndo_open = ipvlan_open,
426 .ndo_stop = ipvlan_stop,
427 .ndo_start_xmit = ipvlan_start_xmit,
428 .ndo_fix_features = ipvlan_fix_features,
429 .ndo_change_rx_flags = ipvlan_change_rx_flags,
430 .ndo_set_rx_mode = ipvlan_set_multicast_mac_filter,
431 .ndo_get_stats64 = ipvlan_get_stats64,
432 .ndo_vlan_rx_add_vid = ipvlan_vlan_rx_add_vid,
433 .ndo_vlan_rx_kill_vid = ipvlan_vlan_rx_kill_vid,
434 .ndo_get_iflink = ipvlan_get_iflink,
437 static int ipvlan_hard_header(struct sk_buff *skb, struct net_device *dev,
438 unsigned short type, const void *daddr,
439 const void *saddr, unsigned len)
441 const struct ipvl_dev *ipvlan = netdev_priv(dev);
442 struct net_device *phy_dev = ipvlan->phy_dev;
444 /* TODO Probably use a different field than dev_addr so that the
445 * mac-address on the virtual device is portable and can be carried
446 * while the packets use the mac-addr on the physical device.
448 return dev_hard_header(skb, phy_dev, type, daddr,
449 saddr ? : phy_dev->dev_addr, len);
452 static const struct header_ops ipvlan_header_ops = {
453 .create = ipvlan_hard_header,
454 .parse = eth_header_parse,
455 .cache = eth_header_cache,
456 .cache_update = eth_header_cache_update,
459 static bool netif_is_ipvlan(const struct net_device *dev)
461 /* both ipvlan and ipvtap devices use the same netdev_ops */
462 return dev->netdev_ops == &ipvlan_netdev_ops;
465 static int ipvlan_ethtool_get_link_ksettings(struct net_device *dev,
466 struct ethtool_link_ksettings *cmd)
468 const struct ipvl_dev *ipvlan = netdev_priv(dev);
470 return __ethtool_get_link_ksettings(ipvlan->phy_dev, cmd);
473 static void ipvlan_ethtool_get_drvinfo(struct net_device *dev,
474 struct ethtool_drvinfo *drvinfo)
476 strlcpy(drvinfo->driver, IPVLAN_DRV, sizeof(drvinfo->driver));
477 strlcpy(drvinfo->version, IPV_DRV_VER, sizeof(drvinfo->version));
480 static u32 ipvlan_ethtool_get_msglevel(struct net_device *dev)
482 const struct ipvl_dev *ipvlan = netdev_priv(dev);
484 return ipvlan->msg_enable;
487 static void ipvlan_ethtool_set_msglevel(struct net_device *dev, u32 value)
489 struct ipvl_dev *ipvlan = netdev_priv(dev);
491 ipvlan->msg_enable = value;
494 static const struct ethtool_ops ipvlan_ethtool_ops = {
495 .get_link = ethtool_op_get_link,
496 .get_link_ksettings = ipvlan_ethtool_get_link_ksettings,
497 .get_drvinfo = ipvlan_ethtool_get_drvinfo,
498 .get_msglevel = ipvlan_ethtool_get_msglevel,
499 .set_msglevel = ipvlan_ethtool_set_msglevel,
502 static int ipvlan_nl_changelink(struct net_device *dev,
503 struct nlattr *tb[], struct nlattr *data[],
504 struct netlink_ext_ack *extack)
506 struct ipvl_dev *ipvlan = netdev_priv(dev);
507 struct ipvl_port *port = ipvlan_port_get_rtnl(ipvlan->phy_dev);
512 if (!ns_capable(dev_net(ipvlan->phy_dev)->user_ns, CAP_NET_ADMIN))
515 if (data[IFLA_IPVLAN_MODE]) {
516 u16 nmode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
518 err = ipvlan_set_port_mode(port, nmode);
521 if (!err && data[IFLA_IPVLAN_FLAGS]) {
522 u16 flags = nla_get_u16(data[IFLA_IPVLAN_FLAGS]);
524 if (flags & IPVLAN_F_PRIVATE)
525 ipvlan_mark_private(port);
527 ipvlan_clear_private(port);
529 if (flags & IPVLAN_F_VEPA)
530 ipvlan_mark_vepa(port);
532 ipvlan_clear_vepa(port);
538 static size_t ipvlan_nl_getsize(const struct net_device *dev)
541 + nla_total_size(2) /* IFLA_IPVLAN_MODE */
542 + nla_total_size(2) /* IFLA_IPVLAN_FLAGS */
546 static int ipvlan_nl_validate(struct nlattr *tb[], struct nlattr *data[],
547 struct netlink_ext_ack *extack)
552 if (data[IFLA_IPVLAN_MODE]) {
553 u16 mode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
555 if (mode < IPVLAN_MODE_L2 || mode >= IPVLAN_MODE_MAX)
558 if (data[IFLA_IPVLAN_FLAGS]) {
559 u16 flags = nla_get_u16(data[IFLA_IPVLAN_FLAGS]);
561 /* Only two bits are used at this moment. */
562 if (flags & ~(IPVLAN_F_PRIVATE | IPVLAN_F_VEPA))
564 /* Also both flags can't be active at the same time. */
565 if ((flags & (IPVLAN_F_PRIVATE | IPVLAN_F_VEPA)) ==
566 (IPVLAN_F_PRIVATE | IPVLAN_F_VEPA))
573 static int ipvlan_nl_fillinfo(struct sk_buff *skb,
574 const struct net_device *dev)
576 struct ipvl_dev *ipvlan = netdev_priv(dev);
577 struct ipvl_port *port = ipvlan_port_get_rtnl(ipvlan->phy_dev);
584 if (nla_put_u16(skb, IFLA_IPVLAN_MODE, port->mode))
586 if (nla_put_u16(skb, IFLA_IPVLAN_FLAGS, port->flags))
595 int ipvlan_link_new(struct net *src_net, struct net_device *dev,
596 struct nlattr *tb[], struct nlattr *data[],
597 struct netlink_ext_ack *extack)
599 struct ipvl_dev *ipvlan = netdev_priv(dev);
600 struct ipvl_port *port;
601 struct net_device *phy_dev;
603 u16 mode = IPVLAN_MODE_L3;
608 phy_dev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
612 if (netif_is_ipvlan(phy_dev)) {
613 struct ipvl_dev *tmp = netdev_priv(phy_dev);
615 phy_dev = tmp->phy_dev;
616 if (!ns_capable(dev_net(phy_dev)->user_ns, CAP_NET_ADMIN))
618 } else if (!netif_is_ipvlan_port(phy_dev)) {
619 /* Exit early if the underlying link is invalid or busy */
620 if (phy_dev->type != ARPHRD_ETHER ||
621 phy_dev->flags & IFF_LOOPBACK) {
623 "Master is either lo or non-ether device\n");
627 if (netdev_is_rx_handler_busy(phy_dev)) {
628 netdev_err(phy_dev, "Device is already in use.\n");
633 ipvlan->phy_dev = phy_dev;
635 ipvlan->sfeatures = IPVLAN_FEATURES;
637 ipvlan_adjust_mtu(ipvlan, phy_dev);
638 INIT_LIST_HEAD(&ipvlan->addrs);
639 spin_lock_init(&ipvlan->addrs_lock);
641 /* TODO Probably put random address here to be presented to the
642 * world but keep using the physical-dev address for the outgoing
645 memcpy(dev->dev_addr, phy_dev->dev_addr, ETH_ALEN);
647 dev->priv_flags |= IFF_NO_RX_HANDLER;
649 err = register_netdevice(dev);
653 /* ipvlan_init() would have created the port, if required */
654 port = ipvlan_port_get_rtnl(phy_dev);
657 /* If the port-id base is at the MAX value, then wrap it around and
658 * begin from 0x1 again. This may be due to a busy system where lots
659 * of slaves are getting created and deleted.
661 if (port->dev_id_start == 0xFFFE)
662 port->dev_id_start = 0x1;
664 /* Since L2 address is shared among all IPvlan slaves including
665 * master, use unique 16 bit dev-ids to diffentiate among them.
666 * Assign IDs between 0x1 and 0xFFFE (used by the master) to each
667 * slave link [see addrconf_ifid_eui48()].
669 err = ida_simple_get(&port->ida, port->dev_id_start, 0xFFFE,
672 err = ida_simple_get(&port->ida, 0x1, port->dev_id_start,
675 goto unregister_netdev;
678 /* Increment id-base to the next slot for the future assignment */
679 port->dev_id_start = err + 1;
681 err = netdev_upper_dev_link(phy_dev, dev, extack);
685 /* Flags are per port and latest update overrides. User has
686 * to be consistent in setting it just like the mode attribute.
688 if (data && data[IFLA_IPVLAN_FLAGS])
689 port->flags = nla_get_u16(data[IFLA_IPVLAN_FLAGS]);
691 if (data && data[IFLA_IPVLAN_MODE])
692 mode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
694 err = ipvlan_set_port_mode(port, mode);
698 list_add_tail_rcu(&ipvlan->pnode, &port->ipvlans);
699 netif_stacked_transfer_operstate(phy_dev, dev);
703 netdev_upper_dev_unlink(phy_dev, dev);
705 ida_simple_remove(&port->ida, dev->dev_id);
707 unregister_netdevice(dev);
710 EXPORT_SYMBOL_GPL(ipvlan_link_new);
712 void ipvlan_link_delete(struct net_device *dev, struct list_head *head)
714 struct ipvl_dev *ipvlan = netdev_priv(dev);
715 struct ipvl_addr *addr, *next;
717 spin_lock_bh(&ipvlan->addrs_lock);
718 list_for_each_entry_safe(addr, next, &ipvlan->addrs, anode) {
719 ipvlan_ht_addr_del(addr);
720 list_del_rcu(&addr->anode);
721 kfree_rcu(addr, rcu);
723 spin_unlock_bh(&ipvlan->addrs_lock);
725 ida_simple_remove(&ipvlan->port->ida, dev->dev_id);
726 list_del_rcu(&ipvlan->pnode);
727 unregister_netdevice_queue(dev, head);
728 netdev_upper_dev_unlink(ipvlan->phy_dev, dev);
730 EXPORT_SYMBOL_GPL(ipvlan_link_delete);
732 void ipvlan_link_setup(struct net_device *dev)
736 dev->max_mtu = ETH_MAX_MTU;
737 dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
738 dev->priv_flags |= IFF_UNICAST_FLT | IFF_NO_QUEUE;
739 dev->netdev_ops = &ipvlan_netdev_ops;
740 dev->needs_free_netdev = true;
741 dev->header_ops = &ipvlan_header_ops;
742 dev->ethtool_ops = &ipvlan_ethtool_ops;
744 EXPORT_SYMBOL_GPL(ipvlan_link_setup);
746 static const struct nla_policy ipvlan_nl_policy[IFLA_IPVLAN_MAX + 1] =
748 [IFLA_IPVLAN_MODE] = { .type = NLA_U16 },
749 [IFLA_IPVLAN_FLAGS] = { .type = NLA_U16 },
752 static struct rtnl_link_ops ipvlan_link_ops = {
754 .priv_size = sizeof(struct ipvl_dev),
756 .setup = ipvlan_link_setup,
757 .newlink = ipvlan_link_new,
758 .dellink = ipvlan_link_delete,
761 int ipvlan_link_register(struct rtnl_link_ops *ops)
763 ops->get_size = ipvlan_nl_getsize;
764 ops->policy = ipvlan_nl_policy;
765 ops->validate = ipvlan_nl_validate;
766 ops->fill_info = ipvlan_nl_fillinfo;
767 ops->changelink = ipvlan_nl_changelink;
768 ops->maxtype = IFLA_IPVLAN_MAX;
769 return rtnl_link_register(ops);
771 EXPORT_SYMBOL_GPL(ipvlan_link_register);
773 static int ipvlan_device_event(struct notifier_block *unused,
774 unsigned long event, void *ptr)
776 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
777 struct ipvl_dev *ipvlan, *next;
778 struct ipvl_port *port;
781 if (!netif_is_ipvlan_port(dev))
784 port = ipvlan_port_get_rtnl(dev);
788 list_for_each_entry(ipvlan, &port->ipvlans, pnode)
789 netif_stacked_transfer_operstate(ipvlan->phy_dev,
793 case NETDEV_REGISTER: {
794 struct net *oldnet, *newnet = dev_net(dev);
795 struct ipvlan_netns *old_vnet;
797 oldnet = read_pnet(&port->pnet);
798 if (net_eq(newnet, oldnet))
801 write_pnet(&port->pnet, newnet);
803 old_vnet = net_generic(oldnet, ipvlan_netid);
804 if (!old_vnet->ipvl_nf_hook_refcnt)
807 ipvlan_register_nf_hook(newnet);
808 ipvlan_unregister_nf_hook(oldnet);
811 case NETDEV_UNREGISTER:
812 if (dev->reg_state != NETREG_UNREGISTERING)
815 list_for_each_entry_safe(ipvlan, next, &port->ipvlans, pnode)
816 ipvlan->dev->rtnl_link_ops->dellink(ipvlan->dev,
818 unregister_netdevice_many(&lst_kill);
821 case NETDEV_FEAT_CHANGE:
822 list_for_each_entry(ipvlan, &port->ipvlans, pnode) {
823 ipvlan->dev->gso_max_size = dev->gso_max_size;
824 ipvlan->dev->gso_max_segs = dev->gso_max_segs;
825 netdev_update_features(ipvlan->dev);
829 case NETDEV_CHANGEMTU:
830 list_for_each_entry(ipvlan, &port->ipvlans, pnode)
831 ipvlan_adjust_mtu(ipvlan, dev);
834 case NETDEV_CHANGEADDR:
835 list_for_each_entry(ipvlan, &port->ipvlans, pnode) {
836 ether_addr_copy(ipvlan->dev->dev_addr, dev->dev_addr);
837 call_netdevice_notifiers(NETDEV_CHANGEADDR, ipvlan->dev);
841 case NETDEV_PRE_TYPE_CHANGE:
842 /* Forbid underlying device to change its type. */
848 /* the caller must held the addrs lock */
849 static int ipvlan_add_addr(struct ipvl_dev *ipvlan, void *iaddr, bool is_v6)
851 struct ipvl_addr *addr;
853 addr = kzalloc(sizeof(struct ipvl_addr), GFP_ATOMIC);
857 addr->master = ipvlan;
859 memcpy(&addr->ip4addr, iaddr, sizeof(struct in_addr));
860 addr->atype = IPVL_IPV4;
861 #if IS_ENABLED(CONFIG_IPV6)
863 memcpy(&addr->ip6addr, iaddr, sizeof(struct in6_addr));
864 addr->atype = IPVL_IPV6;
868 list_add_tail_rcu(&addr->anode, &ipvlan->addrs);
870 /* If the interface is not up, the address will be added to the hash
871 * list by ipvlan_open.
873 if (netif_running(ipvlan->dev))
874 ipvlan_ht_addr_add(ipvlan, addr);
879 static void ipvlan_del_addr(struct ipvl_dev *ipvlan, void *iaddr, bool is_v6)
881 struct ipvl_addr *addr;
883 spin_lock_bh(&ipvlan->addrs_lock);
884 addr = ipvlan_find_addr(ipvlan, iaddr, is_v6);
886 spin_unlock_bh(&ipvlan->addrs_lock);
890 ipvlan_ht_addr_del(addr);
891 list_del_rcu(&addr->anode);
892 spin_unlock_bh(&ipvlan->addrs_lock);
893 kfree_rcu(addr, rcu);
896 static bool ipvlan_is_valid_dev(const struct net_device *dev)
898 struct ipvl_dev *ipvlan = netdev_priv(dev);
900 if (!netif_is_ipvlan(dev))
903 if (!ipvlan || !ipvlan->port)
909 #if IS_ENABLED(CONFIG_IPV6)
910 static int ipvlan_add_addr6(struct ipvl_dev *ipvlan, struct in6_addr *ip6_addr)
914 spin_lock_bh(&ipvlan->addrs_lock);
915 if (ipvlan_addr_busy(ipvlan->port, ip6_addr, true))
916 netif_err(ipvlan, ifup, ipvlan->dev,
917 "Failed to add IPv6=%pI6c addr for %s intf\n",
918 ip6_addr, ipvlan->dev->name);
920 ret = ipvlan_add_addr(ipvlan, ip6_addr, true);
921 spin_unlock_bh(&ipvlan->addrs_lock);
925 static void ipvlan_del_addr6(struct ipvl_dev *ipvlan, struct in6_addr *ip6_addr)
927 return ipvlan_del_addr(ipvlan, ip6_addr, true);
930 static int ipvlan_addr6_event(struct notifier_block *unused,
931 unsigned long event, void *ptr)
933 struct inet6_ifaddr *if6 = (struct inet6_ifaddr *)ptr;
934 struct net_device *dev = (struct net_device *)if6->idev->dev;
935 struct ipvl_dev *ipvlan = netdev_priv(dev);
937 if (!ipvlan_is_valid_dev(dev))
942 if (ipvlan_add_addr6(ipvlan, &if6->addr))
947 ipvlan_del_addr6(ipvlan, &if6->addr);
954 static int ipvlan_addr6_validator_event(struct notifier_block *unused,
955 unsigned long event, void *ptr)
957 struct in6_validator_info *i6vi = (struct in6_validator_info *)ptr;
958 struct net_device *dev = (struct net_device *)i6vi->i6vi_dev->dev;
959 struct ipvl_dev *ipvlan = netdev_priv(dev);
961 if (!ipvlan_is_valid_dev(dev))
966 if (ipvlan_addr_busy(ipvlan->port, &i6vi->i6vi_addr, true)) {
967 NL_SET_ERR_MSG(i6vi->extack,
968 "Address already assigned to an ipvlan device");
969 return notifier_from_errno(-EADDRINUSE);
978 static int ipvlan_add_addr4(struct ipvl_dev *ipvlan, struct in_addr *ip4_addr)
982 spin_lock_bh(&ipvlan->addrs_lock);
983 if (ipvlan_addr_busy(ipvlan->port, ip4_addr, false))
984 netif_err(ipvlan, ifup, ipvlan->dev,
985 "Failed to add IPv4=%pI4 on %s intf.\n",
986 ip4_addr, ipvlan->dev->name);
988 ret = ipvlan_add_addr(ipvlan, ip4_addr, false);
989 spin_unlock_bh(&ipvlan->addrs_lock);
993 static void ipvlan_del_addr4(struct ipvl_dev *ipvlan, struct in_addr *ip4_addr)
995 return ipvlan_del_addr(ipvlan, ip4_addr, false);
998 static int ipvlan_addr4_event(struct notifier_block *unused,
999 unsigned long event, void *ptr)
1001 struct in_ifaddr *if4 = (struct in_ifaddr *)ptr;
1002 struct net_device *dev = (struct net_device *)if4->ifa_dev->dev;
1003 struct ipvl_dev *ipvlan = netdev_priv(dev);
1004 struct in_addr ip4_addr;
1006 if (!ipvlan_is_valid_dev(dev))
1011 ip4_addr.s_addr = if4->ifa_address;
1012 if (ipvlan_add_addr4(ipvlan, &ip4_addr))
1017 ip4_addr.s_addr = if4->ifa_address;
1018 ipvlan_del_addr4(ipvlan, &ip4_addr);
1025 static int ipvlan_addr4_validator_event(struct notifier_block *unused,
1026 unsigned long event, void *ptr)
1028 struct in_validator_info *ivi = (struct in_validator_info *)ptr;
1029 struct net_device *dev = (struct net_device *)ivi->ivi_dev->dev;
1030 struct ipvl_dev *ipvlan = netdev_priv(dev);
1032 if (!ipvlan_is_valid_dev(dev))
1037 if (ipvlan_addr_busy(ipvlan->port, &ivi->ivi_addr, false)) {
1038 NL_SET_ERR_MSG(ivi->extack,
1039 "Address already assigned to an ipvlan device");
1040 return notifier_from_errno(-EADDRINUSE);
1048 static struct notifier_block ipvlan_addr4_notifier_block __read_mostly = {
1049 .notifier_call = ipvlan_addr4_event,
1052 static struct notifier_block ipvlan_addr4_vtor_notifier_block __read_mostly = {
1053 .notifier_call = ipvlan_addr4_validator_event,
1056 static struct notifier_block ipvlan_notifier_block __read_mostly = {
1057 .notifier_call = ipvlan_device_event,
1060 #if IS_ENABLED(CONFIG_IPV6)
1061 static struct notifier_block ipvlan_addr6_notifier_block __read_mostly = {
1062 .notifier_call = ipvlan_addr6_event,
1065 static struct notifier_block ipvlan_addr6_vtor_notifier_block __read_mostly = {
1066 .notifier_call = ipvlan_addr6_validator_event,
1070 static void ipvlan_ns_exit(struct net *net)
1072 struct ipvlan_netns *vnet = net_generic(net, ipvlan_netid);
1074 if (WARN_ON_ONCE(vnet->ipvl_nf_hook_refcnt)) {
1075 vnet->ipvl_nf_hook_refcnt = 0;
1076 nf_unregister_net_hooks(net, ipvl_nfops,
1077 ARRAY_SIZE(ipvl_nfops));
1081 static struct pernet_operations ipvlan_net_ops = {
1082 .id = &ipvlan_netid,
1083 .size = sizeof(struct ipvlan_netns),
1084 .exit = ipvlan_ns_exit,
1087 static int __init ipvlan_init_module(void)
1091 ipvlan_init_secret();
1092 register_netdevice_notifier(&ipvlan_notifier_block);
1093 #if IS_ENABLED(CONFIG_IPV6)
1094 register_inet6addr_notifier(&ipvlan_addr6_notifier_block);
1095 register_inet6addr_validator_notifier(
1096 &ipvlan_addr6_vtor_notifier_block);
1098 register_inetaddr_notifier(&ipvlan_addr4_notifier_block);
1099 register_inetaddr_validator_notifier(&ipvlan_addr4_vtor_notifier_block);
1101 err = register_pernet_subsys(&ipvlan_net_ops);
1105 err = ipvlan_link_register(&ipvlan_link_ops);
1107 unregister_pernet_subsys(&ipvlan_net_ops);
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);
1121 unregister_netdevice_notifier(&ipvlan_notifier_block);
1125 static void __exit ipvlan_cleanup_module(void)
1127 rtnl_link_unregister(&ipvlan_link_ops);
1128 unregister_pernet_subsys(&ipvlan_net_ops);
1129 unregister_netdevice_notifier(&ipvlan_notifier_block);
1130 unregister_inetaddr_notifier(&ipvlan_addr4_notifier_block);
1131 unregister_inetaddr_validator_notifier(
1132 &ipvlan_addr4_vtor_notifier_block);
1133 #if IS_ENABLED(CONFIG_IPV6)
1134 unregister_inet6addr_notifier(&ipvlan_addr6_notifier_block);
1135 unregister_inet6addr_validator_notifier(
1136 &ipvlan_addr6_vtor_notifier_block);
1140 module_init(ipvlan_init_module);
1141 module_exit(ipvlan_cleanup_module);
1143 MODULE_LICENSE("GPL");
1144 MODULE_AUTHOR("Mahesh Bandewar <maheshb@google.com>");
1145 MODULE_DESCRIPTION("Driver for L3 (IPv6/IPv4) based VLANs");
1146 MODULE_ALIAS_RTNL_LINK("ipvlan");