Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /* Copyright (c) 2014 Mahesh Bandewar <maheshb@google.com>
0003  */
0004 
0005 #include <linux/ethtool.h>
0006 
0007 #include "ipvlan.h"
0008 
0009 static int ipvlan_set_port_mode(struct ipvl_port *port, u16 nval,
0010                 struct netlink_ext_ack *extack)
0011 {
0012     struct ipvl_dev *ipvlan;
0013     unsigned int flags;
0014     int err;
0015 
0016     ASSERT_RTNL();
0017     if (port->mode != nval) {
0018         list_for_each_entry(ipvlan, &port->ipvlans, pnode) {
0019             flags = ipvlan->dev->flags;
0020             if (nval == IPVLAN_MODE_L3 || nval == IPVLAN_MODE_L3S) {
0021                 err = dev_change_flags(ipvlan->dev,
0022                                flags | IFF_NOARP,
0023                                extack);
0024             } else {
0025                 err = dev_change_flags(ipvlan->dev,
0026                                flags & ~IFF_NOARP,
0027                                extack);
0028             }
0029             if (unlikely(err))
0030                 goto fail;
0031         }
0032         if (nval == IPVLAN_MODE_L3S) {
0033             /* New mode is L3S */
0034             err = ipvlan_l3s_register(port);
0035             if (err)
0036                 goto fail;
0037         } else if (port->mode == IPVLAN_MODE_L3S) {
0038             /* Old mode was L3S */
0039             ipvlan_l3s_unregister(port);
0040         }
0041         port->mode = nval;
0042     }
0043     return 0;
0044 
0045 fail:
0046     /* Undo the flags changes that have been done so far. */
0047     list_for_each_entry_continue_reverse(ipvlan, &port->ipvlans, pnode) {
0048         flags = ipvlan->dev->flags;
0049         if (port->mode == IPVLAN_MODE_L3 ||
0050             port->mode == IPVLAN_MODE_L3S)
0051             dev_change_flags(ipvlan->dev, flags | IFF_NOARP,
0052                      NULL);
0053         else
0054             dev_change_flags(ipvlan->dev, flags & ~IFF_NOARP,
0055                      NULL);
0056     }
0057 
0058     return err;
0059 }
0060 
0061 static int ipvlan_port_create(struct net_device *dev)
0062 {
0063     struct ipvl_port *port;
0064     int err, idx;
0065 
0066     port = kzalloc(sizeof(struct ipvl_port), GFP_KERNEL);
0067     if (!port)
0068         return -ENOMEM;
0069 
0070     write_pnet(&port->pnet, dev_net(dev));
0071     port->dev = dev;
0072     port->mode = IPVLAN_MODE_L3;
0073     INIT_LIST_HEAD(&port->ipvlans);
0074     for (idx = 0; idx < IPVLAN_HASH_SIZE; idx++)
0075         INIT_HLIST_HEAD(&port->hlhead[idx]);
0076 
0077     skb_queue_head_init(&port->backlog);
0078     INIT_WORK(&port->wq, ipvlan_process_multicast);
0079     ida_init(&port->ida);
0080     port->dev_id_start = 1;
0081 
0082     err = netdev_rx_handler_register(dev, ipvlan_handle_frame, port);
0083     if (err)
0084         goto err;
0085 
0086     return 0;
0087 
0088 err:
0089     kfree(port);
0090     return err;
0091 }
0092 
0093 static void ipvlan_port_destroy(struct net_device *dev)
0094 {
0095     struct ipvl_port *port = ipvlan_port_get_rtnl(dev);
0096     struct sk_buff *skb;
0097 
0098     if (port->mode == IPVLAN_MODE_L3S)
0099         ipvlan_l3s_unregister(port);
0100     netdev_rx_handler_unregister(dev);
0101     cancel_work_sync(&port->wq);
0102     while ((skb = __skb_dequeue(&port->backlog)) != NULL) {
0103         dev_put(skb->dev);
0104         kfree_skb(skb);
0105     }
0106     ida_destroy(&port->ida);
0107     kfree(port);
0108 }
0109 
0110 #define IPVLAN_ALWAYS_ON_OFLOADS \
0111     (NETIF_F_SG | NETIF_F_HW_CSUM | \
0112      NETIF_F_GSO_ROBUST | NETIF_F_GSO_SOFTWARE | NETIF_F_GSO_ENCAP_ALL)
0113 
0114 #define IPVLAN_ALWAYS_ON \
0115     (IPVLAN_ALWAYS_ON_OFLOADS | NETIF_F_LLTX | NETIF_F_VLAN_CHALLENGED)
0116 
0117 #define IPVLAN_FEATURES \
0118     (NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
0119      NETIF_F_GSO | NETIF_F_ALL_TSO | NETIF_F_GSO_ROBUST | \
0120      NETIF_F_GRO | NETIF_F_RXCSUM | \
0121      NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_STAG_FILTER)
0122 
0123     /* NETIF_F_GSO_ENCAP_ALL NETIF_F_GSO_SOFTWARE Newly added */
0124 
0125 #define IPVLAN_STATE_MASK \
0126     ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
0127 
0128 static int ipvlan_init(struct net_device *dev)
0129 {
0130     struct ipvl_dev *ipvlan = netdev_priv(dev);
0131     struct net_device *phy_dev = ipvlan->phy_dev;
0132     struct ipvl_port *port;
0133     int err;
0134 
0135     dev->state = (dev->state & ~IPVLAN_STATE_MASK) |
0136              (phy_dev->state & IPVLAN_STATE_MASK);
0137     dev->features = phy_dev->features & IPVLAN_FEATURES;
0138     dev->features |= IPVLAN_ALWAYS_ON;
0139     dev->vlan_features = phy_dev->vlan_features & IPVLAN_FEATURES;
0140     dev->vlan_features |= IPVLAN_ALWAYS_ON_OFLOADS;
0141     dev->hw_enc_features |= dev->features;
0142     netif_inherit_tso_max(dev, phy_dev);
0143     dev->hard_header_len = phy_dev->hard_header_len;
0144 
0145     netdev_lockdep_set_classes(dev);
0146 
0147     ipvlan->pcpu_stats = netdev_alloc_pcpu_stats(struct ipvl_pcpu_stats);
0148     if (!ipvlan->pcpu_stats)
0149         return -ENOMEM;
0150 
0151     if (!netif_is_ipvlan_port(phy_dev)) {
0152         err = ipvlan_port_create(phy_dev);
0153         if (err < 0) {
0154             free_percpu(ipvlan->pcpu_stats);
0155             return err;
0156         }
0157     }
0158     port = ipvlan_port_get_rtnl(phy_dev);
0159     port->count += 1;
0160     return 0;
0161 }
0162 
0163 static void ipvlan_uninit(struct net_device *dev)
0164 {
0165     struct ipvl_dev *ipvlan = netdev_priv(dev);
0166     struct net_device *phy_dev = ipvlan->phy_dev;
0167     struct ipvl_port *port;
0168 
0169     free_percpu(ipvlan->pcpu_stats);
0170 
0171     port = ipvlan_port_get_rtnl(phy_dev);
0172     port->count -= 1;
0173     if (!port->count)
0174         ipvlan_port_destroy(port->dev);
0175 }
0176 
0177 static int ipvlan_open(struct net_device *dev)
0178 {
0179     struct ipvl_dev *ipvlan = netdev_priv(dev);
0180     struct ipvl_addr *addr;
0181 
0182     if (ipvlan->port->mode == IPVLAN_MODE_L3 ||
0183         ipvlan->port->mode == IPVLAN_MODE_L3S)
0184         dev->flags |= IFF_NOARP;
0185     else
0186         dev->flags &= ~IFF_NOARP;
0187 
0188     rcu_read_lock();
0189     list_for_each_entry_rcu(addr, &ipvlan->addrs, anode)
0190         ipvlan_ht_addr_add(ipvlan, addr);
0191     rcu_read_unlock();
0192 
0193     return 0;
0194 }
0195 
0196 static int ipvlan_stop(struct net_device *dev)
0197 {
0198     struct ipvl_dev *ipvlan = netdev_priv(dev);
0199     struct net_device *phy_dev = ipvlan->phy_dev;
0200     struct ipvl_addr *addr;
0201 
0202     dev_uc_unsync(phy_dev, dev);
0203     dev_mc_unsync(phy_dev, dev);
0204 
0205     rcu_read_lock();
0206     list_for_each_entry_rcu(addr, &ipvlan->addrs, anode)
0207         ipvlan_ht_addr_del(addr);
0208     rcu_read_unlock();
0209 
0210     return 0;
0211 }
0212 
0213 static netdev_tx_t ipvlan_start_xmit(struct sk_buff *skb,
0214                      struct net_device *dev)
0215 {
0216     const struct ipvl_dev *ipvlan = netdev_priv(dev);
0217     int skblen = skb->len;
0218     int ret;
0219 
0220     ret = ipvlan_queue_xmit(skb, dev);
0221     if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
0222         struct ipvl_pcpu_stats *pcptr;
0223 
0224         pcptr = this_cpu_ptr(ipvlan->pcpu_stats);
0225 
0226         u64_stats_update_begin(&pcptr->syncp);
0227         u64_stats_inc(&pcptr->tx_pkts);
0228         u64_stats_add(&pcptr->tx_bytes, skblen);
0229         u64_stats_update_end(&pcptr->syncp);
0230     } else {
0231         this_cpu_inc(ipvlan->pcpu_stats->tx_drps);
0232     }
0233     return ret;
0234 }
0235 
0236 static netdev_features_t ipvlan_fix_features(struct net_device *dev,
0237                          netdev_features_t features)
0238 {
0239     struct ipvl_dev *ipvlan = netdev_priv(dev);
0240 
0241     features |= NETIF_F_ALL_FOR_ALL;
0242     features &= (ipvlan->sfeatures | ~IPVLAN_FEATURES);
0243     features = netdev_increment_features(ipvlan->phy_dev->features,
0244                          features, features);
0245     features |= IPVLAN_ALWAYS_ON;
0246     features &= (IPVLAN_FEATURES | IPVLAN_ALWAYS_ON);
0247 
0248     return features;
0249 }
0250 
0251 static void ipvlan_change_rx_flags(struct net_device *dev, int change)
0252 {
0253     struct ipvl_dev *ipvlan = netdev_priv(dev);
0254     struct net_device *phy_dev = ipvlan->phy_dev;
0255 
0256     if (change & IFF_ALLMULTI)
0257         dev_set_allmulti(phy_dev, dev->flags & IFF_ALLMULTI? 1 : -1);
0258 }
0259 
0260 static void ipvlan_set_multicast_mac_filter(struct net_device *dev)
0261 {
0262     struct ipvl_dev *ipvlan = netdev_priv(dev);
0263 
0264     if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
0265         bitmap_fill(ipvlan->mac_filters, IPVLAN_MAC_FILTER_SIZE);
0266     } else {
0267         struct netdev_hw_addr *ha;
0268         DECLARE_BITMAP(mc_filters, IPVLAN_MAC_FILTER_SIZE);
0269 
0270         bitmap_zero(mc_filters, IPVLAN_MAC_FILTER_SIZE);
0271         netdev_for_each_mc_addr(ha, dev)
0272             __set_bit(ipvlan_mac_hash(ha->addr), mc_filters);
0273 
0274         /* Turn-on broadcast bit irrespective of address family,
0275          * since broadcast is deferred to a work-queue, hence no
0276          * impact on fast-path processing.
0277          */
0278         __set_bit(ipvlan_mac_hash(dev->broadcast), mc_filters);
0279 
0280         bitmap_copy(ipvlan->mac_filters, mc_filters,
0281                 IPVLAN_MAC_FILTER_SIZE);
0282     }
0283     dev_uc_sync(ipvlan->phy_dev, dev);
0284     dev_mc_sync(ipvlan->phy_dev, dev);
0285 }
0286 
0287 static void ipvlan_get_stats64(struct net_device *dev,
0288                    struct rtnl_link_stats64 *s)
0289 {
0290     struct ipvl_dev *ipvlan = netdev_priv(dev);
0291 
0292     if (ipvlan->pcpu_stats) {
0293         struct ipvl_pcpu_stats *pcptr;
0294         u64 rx_pkts, rx_bytes, rx_mcast, tx_pkts, tx_bytes;
0295         u32 rx_errs = 0, tx_drps = 0;
0296         u32 strt;
0297         int idx;
0298 
0299         for_each_possible_cpu(idx) {
0300             pcptr = per_cpu_ptr(ipvlan->pcpu_stats, idx);
0301             do {
0302                 strt= u64_stats_fetch_begin_irq(&pcptr->syncp);
0303                 rx_pkts = u64_stats_read(&pcptr->rx_pkts);
0304                 rx_bytes = u64_stats_read(&pcptr->rx_bytes);
0305                 rx_mcast = u64_stats_read(&pcptr->rx_mcast);
0306                 tx_pkts = u64_stats_read(&pcptr->tx_pkts);
0307                 tx_bytes = u64_stats_read(&pcptr->tx_bytes);
0308             } while (u64_stats_fetch_retry_irq(&pcptr->syncp,
0309                                strt));
0310 
0311             s->rx_packets += rx_pkts;
0312             s->rx_bytes += rx_bytes;
0313             s->multicast += rx_mcast;
0314             s->tx_packets += tx_pkts;
0315             s->tx_bytes += tx_bytes;
0316 
0317             /* u32 values are updated without syncp protection. */
0318             rx_errs += READ_ONCE(pcptr->rx_errs);
0319             tx_drps += READ_ONCE(pcptr->tx_drps);
0320         }
0321         s->rx_errors = rx_errs;
0322         s->rx_dropped = rx_errs;
0323         s->tx_dropped = tx_drps;
0324     }
0325 }
0326 
0327 static int ipvlan_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
0328 {
0329     struct ipvl_dev *ipvlan = netdev_priv(dev);
0330     struct net_device *phy_dev = ipvlan->phy_dev;
0331 
0332     return vlan_vid_add(phy_dev, proto, vid);
0333 }
0334 
0335 static int ipvlan_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
0336                    u16 vid)
0337 {
0338     struct ipvl_dev *ipvlan = netdev_priv(dev);
0339     struct net_device *phy_dev = ipvlan->phy_dev;
0340 
0341     vlan_vid_del(phy_dev, proto, vid);
0342     return 0;
0343 }
0344 
0345 static int ipvlan_get_iflink(const struct net_device *dev)
0346 {
0347     struct ipvl_dev *ipvlan = netdev_priv(dev);
0348 
0349     return ipvlan->phy_dev->ifindex;
0350 }
0351 
0352 static const struct net_device_ops ipvlan_netdev_ops = {
0353     .ndo_init       = ipvlan_init,
0354     .ndo_uninit     = ipvlan_uninit,
0355     .ndo_open       = ipvlan_open,
0356     .ndo_stop       = ipvlan_stop,
0357     .ndo_start_xmit     = ipvlan_start_xmit,
0358     .ndo_fix_features   = ipvlan_fix_features,
0359     .ndo_change_rx_flags    = ipvlan_change_rx_flags,
0360     .ndo_set_rx_mode    = ipvlan_set_multicast_mac_filter,
0361     .ndo_get_stats64    = ipvlan_get_stats64,
0362     .ndo_vlan_rx_add_vid    = ipvlan_vlan_rx_add_vid,
0363     .ndo_vlan_rx_kill_vid   = ipvlan_vlan_rx_kill_vid,
0364     .ndo_get_iflink     = ipvlan_get_iflink,
0365 };
0366 
0367 static int ipvlan_hard_header(struct sk_buff *skb, struct net_device *dev,
0368                   unsigned short type, const void *daddr,
0369                   const void *saddr, unsigned len)
0370 {
0371     const struct ipvl_dev *ipvlan = netdev_priv(dev);
0372     struct net_device *phy_dev = ipvlan->phy_dev;
0373 
0374     /* TODO Probably use a different field than dev_addr so that the
0375      * mac-address on the virtual device is portable and can be carried
0376      * while the packets use the mac-addr on the physical device.
0377      */
0378     return dev_hard_header(skb, phy_dev, type, daddr,
0379                    saddr ? : phy_dev->dev_addr, len);
0380 }
0381 
0382 static const struct header_ops ipvlan_header_ops = {
0383     .create     = ipvlan_hard_header,
0384     .parse      = eth_header_parse,
0385     .cache      = eth_header_cache,
0386     .cache_update   = eth_header_cache_update,
0387 };
0388 
0389 static void ipvlan_adjust_mtu(struct ipvl_dev *ipvlan, struct net_device *dev)
0390 {
0391     ipvlan->dev->mtu = dev->mtu;
0392 }
0393 
0394 static bool netif_is_ipvlan(const struct net_device *dev)
0395 {
0396     /* both ipvlan and ipvtap devices use the same netdev_ops */
0397     return dev->netdev_ops == &ipvlan_netdev_ops;
0398 }
0399 
0400 static int ipvlan_ethtool_get_link_ksettings(struct net_device *dev,
0401                          struct ethtool_link_ksettings *cmd)
0402 {
0403     const struct ipvl_dev *ipvlan = netdev_priv(dev);
0404 
0405     return __ethtool_get_link_ksettings(ipvlan->phy_dev, cmd);
0406 }
0407 
0408 static void ipvlan_ethtool_get_drvinfo(struct net_device *dev,
0409                        struct ethtool_drvinfo *drvinfo)
0410 {
0411     strlcpy(drvinfo->driver, IPVLAN_DRV, sizeof(drvinfo->driver));
0412     strlcpy(drvinfo->version, IPV_DRV_VER, sizeof(drvinfo->version));
0413 }
0414 
0415 static u32 ipvlan_ethtool_get_msglevel(struct net_device *dev)
0416 {
0417     const struct ipvl_dev *ipvlan = netdev_priv(dev);
0418 
0419     return ipvlan->msg_enable;
0420 }
0421 
0422 static void ipvlan_ethtool_set_msglevel(struct net_device *dev, u32 value)
0423 {
0424     struct ipvl_dev *ipvlan = netdev_priv(dev);
0425 
0426     ipvlan->msg_enable = value;
0427 }
0428 
0429 static const struct ethtool_ops ipvlan_ethtool_ops = {
0430     .get_link   = ethtool_op_get_link,
0431     .get_link_ksettings = ipvlan_ethtool_get_link_ksettings,
0432     .get_drvinfo    = ipvlan_ethtool_get_drvinfo,
0433     .get_msglevel   = ipvlan_ethtool_get_msglevel,
0434     .set_msglevel   = ipvlan_ethtool_set_msglevel,
0435 };
0436 
0437 static int ipvlan_nl_changelink(struct net_device *dev,
0438                 struct nlattr *tb[], struct nlattr *data[],
0439                 struct netlink_ext_ack *extack)
0440 {
0441     struct ipvl_dev *ipvlan = netdev_priv(dev);
0442     struct ipvl_port *port = ipvlan_port_get_rtnl(ipvlan->phy_dev);
0443     int err = 0;
0444 
0445     if (!data)
0446         return 0;
0447     if (!ns_capable(dev_net(ipvlan->phy_dev)->user_ns, CAP_NET_ADMIN))
0448         return -EPERM;
0449 
0450     if (data[IFLA_IPVLAN_MODE]) {
0451         u16 nmode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
0452 
0453         err = ipvlan_set_port_mode(port, nmode, extack);
0454     }
0455 
0456     if (!err && data[IFLA_IPVLAN_FLAGS]) {
0457         u16 flags = nla_get_u16(data[IFLA_IPVLAN_FLAGS]);
0458 
0459         if (flags & IPVLAN_F_PRIVATE)
0460             ipvlan_mark_private(port);
0461         else
0462             ipvlan_clear_private(port);
0463 
0464         if (flags & IPVLAN_F_VEPA)
0465             ipvlan_mark_vepa(port);
0466         else
0467             ipvlan_clear_vepa(port);
0468     }
0469 
0470     return err;
0471 }
0472 
0473 static size_t ipvlan_nl_getsize(const struct net_device *dev)
0474 {
0475     return (0
0476         + nla_total_size(2) /* IFLA_IPVLAN_MODE */
0477         + nla_total_size(2) /* IFLA_IPVLAN_FLAGS */
0478         );
0479 }
0480 
0481 static int ipvlan_nl_validate(struct nlattr *tb[], struct nlattr *data[],
0482                   struct netlink_ext_ack *extack)
0483 {
0484     if (!data)
0485         return 0;
0486 
0487     if (data[IFLA_IPVLAN_MODE]) {
0488         u16 mode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
0489 
0490         if (mode >= IPVLAN_MODE_MAX)
0491             return -EINVAL;
0492     }
0493     if (data[IFLA_IPVLAN_FLAGS]) {
0494         u16 flags = nla_get_u16(data[IFLA_IPVLAN_FLAGS]);
0495 
0496         /* Only two bits are used at this moment. */
0497         if (flags & ~(IPVLAN_F_PRIVATE | IPVLAN_F_VEPA))
0498             return -EINVAL;
0499         /* Also both flags can't be active at the same time. */
0500         if ((flags & (IPVLAN_F_PRIVATE | IPVLAN_F_VEPA)) ==
0501             (IPVLAN_F_PRIVATE | IPVLAN_F_VEPA))
0502             return -EINVAL;
0503     }
0504 
0505     return 0;
0506 }
0507 
0508 static int ipvlan_nl_fillinfo(struct sk_buff *skb,
0509                   const struct net_device *dev)
0510 {
0511     struct ipvl_dev *ipvlan = netdev_priv(dev);
0512     struct ipvl_port *port = ipvlan_port_get_rtnl(ipvlan->phy_dev);
0513     int ret = -EINVAL;
0514 
0515     if (!port)
0516         goto err;
0517 
0518     ret = -EMSGSIZE;
0519     if (nla_put_u16(skb, IFLA_IPVLAN_MODE, port->mode))
0520         goto err;
0521     if (nla_put_u16(skb, IFLA_IPVLAN_FLAGS, port->flags))
0522         goto err;
0523 
0524     return 0;
0525 
0526 err:
0527     return ret;
0528 }
0529 
0530 int ipvlan_link_new(struct net *src_net, struct net_device *dev,
0531             struct nlattr *tb[], struct nlattr *data[],
0532             struct netlink_ext_ack *extack)
0533 {
0534     struct ipvl_dev *ipvlan = netdev_priv(dev);
0535     struct ipvl_port *port;
0536     struct net_device *phy_dev;
0537     int err;
0538     u16 mode = IPVLAN_MODE_L3;
0539 
0540     if (!tb[IFLA_LINK])
0541         return -EINVAL;
0542 
0543     phy_dev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
0544     if (!phy_dev)
0545         return -ENODEV;
0546 
0547     if (netif_is_ipvlan(phy_dev)) {
0548         struct ipvl_dev *tmp = netdev_priv(phy_dev);
0549 
0550         phy_dev = tmp->phy_dev;
0551         if (!ns_capable(dev_net(phy_dev)->user_ns, CAP_NET_ADMIN))
0552             return -EPERM;
0553     } else if (!netif_is_ipvlan_port(phy_dev)) {
0554         /* Exit early if the underlying link is invalid or busy */
0555         if (phy_dev->type != ARPHRD_ETHER ||
0556             phy_dev->flags & IFF_LOOPBACK) {
0557             netdev_err(phy_dev,
0558                    "Master is either lo or non-ether device\n");
0559             return -EINVAL;
0560         }
0561 
0562         if (netdev_is_rx_handler_busy(phy_dev)) {
0563             netdev_err(phy_dev, "Device is already in use.\n");
0564             return -EBUSY;
0565         }
0566     }
0567 
0568     ipvlan->phy_dev = phy_dev;
0569     ipvlan->dev = dev;
0570     ipvlan->sfeatures = IPVLAN_FEATURES;
0571     if (!tb[IFLA_MTU])
0572         ipvlan_adjust_mtu(ipvlan, phy_dev);
0573     INIT_LIST_HEAD(&ipvlan->addrs);
0574     spin_lock_init(&ipvlan->addrs_lock);
0575 
0576     /* TODO Probably put random address here to be presented to the
0577      * world but keep using the physical-dev address for the outgoing
0578      * packets.
0579      */
0580     eth_hw_addr_set(dev, phy_dev->dev_addr);
0581 
0582     dev->priv_flags |= IFF_NO_RX_HANDLER;
0583 
0584     err = register_netdevice(dev);
0585     if (err < 0)
0586         return err;
0587 
0588     /* ipvlan_init() would have created the port, if required */
0589     port = ipvlan_port_get_rtnl(phy_dev);
0590     ipvlan->port = port;
0591 
0592     /* If the port-id base is at the MAX value, then wrap it around and
0593      * begin from 0x1 again. This may be due to a busy system where lots
0594      * of slaves are getting created and deleted.
0595      */
0596     if (port->dev_id_start == 0xFFFE)
0597         port->dev_id_start = 0x1;
0598 
0599     /* Since L2 address is shared among all IPvlan slaves including
0600      * master, use unique 16 bit dev-ids to diffentiate among them.
0601      * Assign IDs between 0x1 and 0xFFFE (used by the master) to each
0602      * slave link [see addrconf_ifid_eui48()].
0603      */
0604     err = ida_simple_get(&port->ida, port->dev_id_start, 0xFFFE,
0605                  GFP_KERNEL);
0606     if (err < 0)
0607         err = ida_simple_get(&port->ida, 0x1, port->dev_id_start,
0608                      GFP_KERNEL);
0609     if (err < 0)
0610         goto unregister_netdev;
0611     dev->dev_id = err;
0612 
0613     /* Increment id-base to the next slot for the future assignment */
0614     port->dev_id_start = err + 1;
0615 
0616     err = netdev_upper_dev_link(phy_dev, dev, extack);
0617     if (err)
0618         goto remove_ida;
0619 
0620     /* Flags are per port and latest update overrides. User has
0621      * to be consistent in setting it just like the mode attribute.
0622      */
0623     if (data && data[IFLA_IPVLAN_FLAGS])
0624         port->flags = nla_get_u16(data[IFLA_IPVLAN_FLAGS]);
0625 
0626     if (data && data[IFLA_IPVLAN_MODE])
0627         mode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
0628 
0629     err = ipvlan_set_port_mode(port, mode, extack);
0630     if (err)
0631         goto unlink_netdev;
0632 
0633     list_add_tail_rcu(&ipvlan->pnode, &port->ipvlans);
0634     netif_stacked_transfer_operstate(phy_dev, dev);
0635     return 0;
0636 
0637 unlink_netdev:
0638     netdev_upper_dev_unlink(phy_dev, dev);
0639 remove_ida:
0640     ida_simple_remove(&port->ida, dev->dev_id);
0641 unregister_netdev:
0642     unregister_netdevice(dev);
0643     return err;
0644 }
0645 EXPORT_SYMBOL_GPL(ipvlan_link_new);
0646 
0647 void ipvlan_link_delete(struct net_device *dev, struct list_head *head)
0648 {
0649     struct ipvl_dev *ipvlan = netdev_priv(dev);
0650     struct ipvl_addr *addr, *next;
0651 
0652     spin_lock_bh(&ipvlan->addrs_lock);
0653     list_for_each_entry_safe(addr, next, &ipvlan->addrs, anode) {
0654         ipvlan_ht_addr_del(addr);
0655         list_del_rcu(&addr->anode);
0656         kfree_rcu(addr, rcu);
0657     }
0658     spin_unlock_bh(&ipvlan->addrs_lock);
0659 
0660     ida_simple_remove(&ipvlan->port->ida, dev->dev_id);
0661     list_del_rcu(&ipvlan->pnode);
0662     unregister_netdevice_queue(dev, head);
0663     netdev_upper_dev_unlink(ipvlan->phy_dev, dev);
0664 }
0665 EXPORT_SYMBOL_GPL(ipvlan_link_delete);
0666 
0667 void ipvlan_link_setup(struct net_device *dev)
0668 {
0669     ether_setup(dev);
0670 
0671     dev->max_mtu = ETH_MAX_MTU;
0672     dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
0673     dev->priv_flags |= IFF_UNICAST_FLT | IFF_NO_QUEUE;
0674     dev->netdev_ops = &ipvlan_netdev_ops;
0675     dev->needs_free_netdev = true;
0676     dev->header_ops = &ipvlan_header_ops;
0677     dev->ethtool_ops = &ipvlan_ethtool_ops;
0678 }
0679 EXPORT_SYMBOL_GPL(ipvlan_link_setup);
0680 
0681 static const struct nla_policy ipvlan_nl_policy[IFLA_IPVLAN_MAX + 1] =
0682 {
0683     [IFLA_IPVLAN_MODE] = { .type = NLA_U16 },
0684     [IFLA_IPVLAN_FLAGS] = { .type = NLA_U16 },
0685 };
0686 
0687 static struct net *ipvlan_get_link_net(const struct net_device *dev)
0688 {
0689     struct ipvl_dev *ipvlan = netdev_priv(dev);
0690 
0691     return dev_net(ipvlan->phy_dev);
0692 }
0693 
0694 static struct rtnl_link_ops ipvlan_link_ops = {
0695     .kind       = "ipvlan",
0696     .priv_size  = sizeof(struct ipvl_dev),
0697 
0698     .setup      = ipvlan_link_setup,
0699     .newlink    = ipvlan_link_new,
0700     .dellink    = ipvlan_link_delete,
0701     .get_link_net   = ipvlan_get_link_net,
0702 };
0703 
0704 int ipvlan_link_register(struct rtnl_link_ops *ops)
0705 {
0706     ops->get_size   = ipvlan_nl_getsize;
0707     ops->policy = ipvlan_nl_policy;
0708     ops->validate   = ipvlan_nl_validate;
0709     ops->fill_info  = ipvlan_nl_fillinfo;
0710     ops->changelink = ipvlan_nl_changelink;
0711     ops->maxtype    = IFLA_IPVLAN_MAX;
0712     return rtnl_link_register(ops);
0713 }
0714 EXPORT_SYMBOL_GPL(ipvlan_link_register);
0715 
0716 static int ipvlan_device_event(struct notifier_block *unused,
0717                    unsigned long event, void *ptr)
0718 {
0719     struct netlink_ext_ack *extack = netdev_notifier_info_to_extack(ptr);
0720     struct netdev_notifier_pre_changeaddr_info *prechaddr_info;
0721     struct net_device *dev = netdev_notifier_info_to_dev(ptr);
0722     struct ipvl_dev *ipvlan, *next;
0723     struct ipvl_port *port;
0724     LIST_HEAD(lst_kill);
0725     int err;
0726 
0727     if (!netif_is_ipvlan_port(dev))
0728         return NOTIFY_DONE;
0729 
0730     port = ipvlan_port_get_rtnl(dev);
0731 
0732     switch (event) {
0733     case NETDEV_UP:
0734     case NETDEV_CHANGE:
0735         list_for_each_entry(ipvlan, &port->ipvlans, pnode)
0736             netif_stacked_transfer_operstate(ipvlan->phy_dev,
0737                              ipvlan->dev);
0738         break;
0739 
0740     case NETDEV_REGISTER: {
0741         struct net *oldnet, *newnet = dev_net(dev);
0742 
0743         oldnet = read_pnet(&port->pnet);
0744         if (net_eq(newnet, oldnet))
0745             break;
0746 
0747         write_pnet(&port->pnet, newnet);
0748 
0749         ipvlan_migrate_l3s_hook(oldnet, newnet);
0750         break;
0751     }
0752     case NETDEV_UNREGISTER:
0753         if (dev->reg_state != NETREG_UNREGISTERING)
0754             break;
0755 
0756         list_for_each_entry_safe(ipvlan, next, &port->ipvlans, pnode)
0757             ipvlan->dev->rtnl_link_ops->dellink(ipvlan->dev,
0758                                 &lst_kill);
0759         unregister_netdevice_many(&lst_kill);
0760         break;
0761 
0762     case NETDEV_FEAT_CHANGE:
0763         list_for_each_entry(ipvlan, &port->ipvlans, pnode) {
0764             netif_inherit_tso_max(ipvlan->dev, dev);
0765             netdev_update_features(ipvlan->dev);
0766         }
0767         break;
0768 
0769     case NETDEV_CHANGEMTU:
0770         list_for_each_entry(ipvlan, &port->ipvlans, pnode)
0771             ipvlan_adjust_mtu(ipvlan, dev);
0772         break;
0773 
0774     case NETDEV_PRE_CHANGEADDR:
0775         prechaddr_info = ptr;
0776         list_for_each_entry(ipvlan, &port->ipvlans, pnode) {
0777             err = dev_pre_changeaddr_notify(ipvlan->dev,
0778                             prechaddr_info->dev_addr,
0779                             extack);
0780             if (err)
0781                 return notifier_from_errno(err);
0782         }
0783         break;
0784 
0785     case NETDEV_CHANGEADDR:
0786         list_for_each_entry(ipvlan, &port->ipvlans, pnode) {
0787             eth_hw_addr_set(ipvlan->dev, dev->dev_addr);
0788             call_netdevice_notifiers(NETDEV_CHANGEADDR, ipvlan->dev);
0789         }
0790         break;
0791 
0792     case NETDEV_PRE_TYPE_CHANGE:
0793         /* Forbid underlying device to change its type. */
0794         return NOTIFY_BAD;
0795     }
0796     return NOTIFY_DONE;
0797 }
0798 
0799 /* the caller must held the addrs lock */
0800 static int ipvlan_add_addr(struct ipvl_dev *ipvlan, void *iaddr, bool is_v6)
0801 {
0802     struct ipvl_addr *addr;
0803 
0804     addr = kzalloc(sizeof(struct ipvl_addr), GFP_ATOMIC);
0805     if (!addr)
0806         return -ENOMEM;
0807 
0808     addr->master = ipvlan;
0809     if (!is_v6) {
0810         memcpy(&addr->ip4addr, iaddr, sizeof(struct in_addr));
0811         addr->atype = IPVL_IPV4;
0812 #if IS_ENABLED(CONFIG_IPV6)
0813     } else {
0814         memcpy(&addr->ip6addr, iaddr, sizeof(struct in6_addr));
0815         addr->atype = IPVL_IPV6;
0816 #endif
0817     }
0818 
0819     list_add_tail_rcu(&addr->anode, &ipvlan->addrs);
0820 
0821     /* If the interface is not up, the address will be added to the hash
0822      * list by ipvlan_open.
0823      */
0824     if (netif_running(ipvlan->dev))
0825         ipvlan_ht_addr_add(ipvlan, addr);
0826 
0827     return 0;
0828 }
0829 
0830 static void ipvlan_del_addr(struct ipvl_dev *ipvlan, void *iaddr, bool is_v6)
0831 {
0832     struct ipvl_addr *addr;
0833 
0834     spin_lock_bh(&ipvlan->addrs_lock);
0835     addr = ipvlan_find_addr(ipvlan, iaddr, is_v6);
0836     if (!addr) {
0837         spin_unlock_bh(&ipvlan->addrs_lock);
0838         return;
0839     }
0840 
0841     ipvlan_ht_addr_del(addr);
0842     list_del_rcu(&addr->anode);
0843     spin_unlock_bh(&ipvlan->addrs_lock);
0844     kfree_rcu(addr, rcu);
0845 }
0846 
0847 static bool ipvlan_is_valid_dev(const struct net_device *dev)
0848 {
0849     struct ipvl_dev *ipvlan = netdev_priv(dev);
0850 
0851     if (!netif_is_ipvlan(dev))
0852         return false;
0853 
0854     if (!ipvlan || !ipvlan->port)
0855         return false;
0856 
0857     return true;
0858 }
0859 
0860 #if IS_ENABLED(CONFIG_IPV6)
0861 static int ipvlan_add_addr6(struct ipvl_dev *ipvlan, struct in6_addr *ip6_addr)
0862 {
0863     int ret = -EINVAL;
0864 
0865     spin_lock_bh(&ipvlan->addrs_lock);
0866     if (ipvlan_addr_busy(ipvlan->port, ip6_addr, true))
0867         netif_err(ipvlan, ifup, ipvlan->dev,
0868               "Failed to add IPv6=%pI6c addr for %s intf\n",
0869               ip6_addr, ipvlan->dev->name);
0870     else
0871         ret = ipvlan_add_addr(ipvlan, ip6_addr, true);
0872     spin_unlock_bh(&ipvlan->addrs_lock);
0873     return ret;
0874 }
0875 
0876 static void ipvlan_del_addr6(struct ipvl_dev *ipvlan, struct in6_addr *ip6_addr)
0877 {
0878     return ipvlan_del_addr(ipvlan, ip6_addr, true);
0879 }
0880 
0881 static int ipvlan_addr6_event(struct notifier_block *unused,
0882                   unsigned long event, void *ptr)
0883 {
0884     struct inet6_ifaddr *if6 = (struct inet6_ifaddr *)ptr;
0885     struct net_device *dev = (struct net_device *)if6->idev->dev;
0886     struct ipvl_dev *ipvlan = netdev_priv(dev);
0887 
0888     if (!ipvlan_is_valid_dev(dev))
0889         return NOTIFY_DONE;
0890 
0891     switch (event) {
0892     case NETDEV_UP:
0893         if (ipvlan_add_addr6(ipvlan, &if6->addr))
0894             return NOTIFY_BAD;
0895         break;
0896 
0897     case NETDEV_DOWN:
0898         ipvlan_del_addr6(ipvlan, &if6->addr);
0899         break;
0900     }
0901 
0902     return NOTIFY_OK;
0903 }
0904 
0905 static int ipvlan_addr6_validator_event(struct notifier_block *unused,
0906                     unsigned long event, void *ptr)
0907 {
0908     struct in6_validator_info *i6vi = (struct in6_validator_info *)ptr;
0909     struct net_device *dev = (struct net_device *)i6vi->i6vi_dev->dev;
0910     struct ipvl_dev *ipvlan = netdev_priv(dev);
0911 
0912     if (!ipvlan_is_valid_dev(dev))
0913         return NOTIFY_DONE;
0914 
0915     switch (event) {
0916     case NETDEV_UP:
0917         if (ipvlan_addr_busy(ipvlan->port, &i6vi->i6vi_addr, true)) {
0918             NL_SET_ERR_MSG(i6vi->extack,
0919                        "Address already assigned to an ipvlan device");
0920             return notifier_from_errno(-EADDRINUSE);
0921         }
0922         break;
0923     }
0924 
0925     return NOTIFY_OK;
0926 }
0927 #endif
0928 
0929 static int ipvlan_add_addr4(struct ipvl_dev *ipvlan, struct in_addr *ip4_addr)
0930 {
0931     int ret = -EINVAL;
0932 
0933     spin_lock_bh(&ipvlan->addrs_lock);
0934     if (ipvlan_addr_busy(ipvlan->port, ip4_addr, false))
0935         netif_err(ipvlan, ifup, ipvlan->dev,
0936               "Failed to add IPv4=%pI4 on %s intf.\n",
0937               ip4_addr, ipvlan->dev->name);
0938     else
0939         ret = ipvlan_add_addr(ipvlan, ip4_addr, false);
0940     spin_unlock_bh(&ipvlan->addrs_lock);
0941     return ret;
0942 }
0943 
0944 static void ipvlan_del_addr4(struct ipvl_dev *ipvlan, struct in_addr *ip4_addr)
0945 {
0946     return ipvlan_del_addr(ipvlan, ip4_addr, false);
0947 }
0948 
0949 static int ipvlan_addr4_event(struct notifier_block *unused,
0950                   unsigned long event, void *ptr)
0951 {
0952     struct in_ifaddr *if4 = (struct in_ifaddr *)ptr;
0953     struct net_device *dev = (struct net_device *)if4->ifa_dev->dev;
0954     struct ipvl_dev *ipvlan = netdev_priv(dev);
0955     struct in_addr ip4_addr;
0956 
0957     if (!ipvlan_is_valid_dev(dev))
0958         return NOTIFY_DONE;
0959 
0960     switch (event) {
0961     case NETDEV_UP:
0962         ip4_addr.s_addr = if4->ifa_address;
0963         if (ipvlan_add_addr4(ipvlan, &ip4_addr))
0964             return NOTIFY_BAD;
0965         break;
0966 
0967     case NETDEV_DOWN:
0968         ip4_addr.s_addr = if4->ifa_address;
0969         ipvlan_del_addr4(ipvlan, &ip4_addr);
0970         break;
0971     }
0972 
0973     return NOTIFY_OK;
0974 }
0975 
0976 static int ipvlan_addr4_validator_event(struct notifier_block *unused,
0977                     unsigned long event, void *ptr)
0978 {
0979     struct in_validator_info *ivi = (struct in_validator_info *)ptr;
0980     struct net_device *dev = (struct net_device *)ivi->ivi_dev->dev;
0981     struct ipvl_dev *ipvlan = netdev_priv(dev);
0982 
0983     if (!ipvlan_is_valid_dev(dev))
0984         return NOTIFY_DONE;
0985 
0986     switch (event) {
0987     case NETDEV_UP:
0988         if (ipvlan_addr_busy(ipvlan->port, &ivi->ivi_addr, false)) {
0989             NL_SET_ERR_MSG(ivi->extack,
0990                        "Address already assigned to an ipvlan device");
0991             return notifier_from_errno(-EADDRINUSE);
0992         }
0993         break;
0994     }
0995 
0996     return NOTIFY_OK;
0997 }
0998 
0999 static struct notifier_block ipvlan_addr4_notifier_block __read_mostly = {
1000     .notifier_call = ipvlan_addr4_event,
1001 };
1002 
1003 static struct notifier_block ipvlan_addr4_vtor_notifier_block __read_mostly = {
1004     .notifier_call = ipvlan_addr4_validator_event,
1005 };
1006 
1007 static struct notifier_block ipvlan_notifier_block __read_mostly = {
1008     .notifier_call = ipvlan_device_event,
1009 };
1010 
1011 #if IS_ENABLED(CONFIG_IPV6)
1012 static struct notifier_block ipvlan_addr6_notifier_block __read_mostly = {
1013     .notifier_call = ipvlan_addr6_event,
1014 };
1015 
1016 static struct notifier_block ipvlan_addr6_vtor_notifier_block __read_mostly = {
1017     .notifier_call = ipvlan_addr6_validator_event,
1018 };
1019 #endif
1020 
1021 static int __init ipvlan_init_module(void)
1022 {
1023     int err;
1024 
1025     ipvlan_init_secret();
1026     register_netdevice_notifier(&ipvlan_notifier_block);
1027 #if IS_ENABLED(CONFIG_IPV6)
1028     register_inet6addr_notifier(&ipvlan_addr6_notifier_block);
1029     register_inet6addr_validator_notifier(
1030         &ipvlan_addr6_vtor_notifier_block);
1031 #endif
1032     register_inetaddr_notifier(&ipvlan_addr4_notifier_block);
1033     register_inetaddr_validator_notifier(&ipvlan_addr4_vtor_notifier_block);
1034 
1035     err = ipvlan_l3s_init();
1036     if (err < 0)
1037         goto error;
1038 
1039     err = ipvlan_link_register(&ipvlan_link_ops);
1040     if (err < 0) {
1041         ipvlan_l3s_cleanup();
1042         goto error;
1043     }
1044 
1045     return 0;
1046 error:
1047     unregister_inetaddr_notifier(&ipvlan_addr4_notifier_block);
1048     unregister_inetaddr_validator_notifier(
1049         &ipvlan_addr4_vtor_notifier_block);
1050 #if IS_ENABLED(CONFIG_IPV6)
1051     unregister_inet6addr_notifier(&ipvlan_addr6_notifier_block);
1052     unregister_inet6addr_validator_notifier(
1053         &ipvlan_addr6_vtor_notifier_block);
1054 #endif
1055     unregister_netdevice_notifier(&ipvlan_notifier_block);
1056     return err;
1057 }
1058 
1059 static void __exit ipvlan_cleanup_module(void)
1060 {
1061     rtnl_link_unregister(&ipvlan_link_ops);
1062     ipvlan_l3s_cleanup();
1063     unregister_netdevice_notifier(&ipvlan_notifier_block);
1064     unregister_inetaddr_notifier(&ipvlan_addr4_notifier_block);
1065     unregister_inetaddr_validator_notifier(
1066         &ipvlan_addr4_vtor_notifier_block);
1067 #if IS_ENABLED(CONFIG_IPV6)
1068     unregister_inet6addr_notifier(&ipvlan_addr6_notifier_block);
1069     unregister_inet6addr_validator_notifier(
1070         &ipvlan_addr6_vtor_notifier_block);
1071 #endif
1072 }
1073 
1074 module_init(ipvlan_init_module);
1075 module_exit(ipvlan_cleanup_module);
1076 
1077 MODULE_LICENSE("GPL");
1078 MODULE_AUTHOR("Mahesh Bandewar <maheshb@google.com>");
1079 MODULE_DESCRIPTION("Driver for L3 (IPv6/IPv4) based VLANs");
1080 MODULE_ALIAS_RTNL_LINK("ipvlan");