GNU Linux-libre 4.19.286-gnu1
[releases.git] / drivers / net / ipvlan / ipvlan_main.c
1 /* Copyright (c) 2014 Mahesh Bandewar <maheshb@google.com>
2  *
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.
7  *
8  */
9
10 #include "ipvlan.h"
11
12 static unsigned int ipvlan_netid __read_mostly;
13
14 struct ipvlan_netns {
15         unsigned int ipvl_nf_hook_refcnt;
16 };
17
18 static const struct nf_hook_ops ipvl_nfops[] = {
19         {
20                 .hook     = ipvlan_nf_input,
21                 .pf       = NFPROTO_IPV4,
22                 .hooknum  = NF_INET_LOCAL_IN,
23                 .priority = INT_MAX,
24         },
25 #if IS_ENABLED(CONFIG_IPV6)
26         {
27                 .hook     = ipvlan_nf_input,
28                 .pf       = NFPROTO_IPV6,
29                 .hooknum  = NF_INET_LOCAL_IN,
30                 .priority = INT_MAX,
31         },
32 #endif
33 };
34
35 static const struct l3mdev_ops ipvl_l3mdev_ops = {
36         .l3mdev_l3_rcv = ipvlan_l3_rcv,
37 };
38
39 static void ipvlan_adjust_mtu(struct ipvl_dev *ipvlan, struct net_device *dev)
40 {
41         ipvlan->dev->mtu = dev->mtu;
42 }
43
44 static int ipvlan_register_nf_hook(struct net *net)
45 {
46         struct ipvlan_netns *vnet = net_generic(net, ipvlan_netid);
47         int err = 0;
48
49         if (!vnet->ipvl_nf_hook_refcnt) {
50                 err = nf_register_net_hooks(net, ipvl_nfops,
51                                             ARRAY_SIZE(ipvl_nfops));
52                 if (!err)
53                         vnet->ipvl_nf_hook_refcnt = 1;
54         } else {
55                 vnet->ipvl_nf_hook_refcnt++;
56         }
57
58         return err;
59 }
60
61 static void ipvlan_unregister_nf_hook(struct net *net)
62 {
63         struct ipvlan_netns *vnet = net_generic(net, ipvlan_netid);
64
65         if (WARN_ON(!vnet->ipvl_nf_hook_refcnt))
66                 return;
67
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));
72 }
73
74 static int ipvlan_set_port_mode(struct ipvl_port *port, u16 nval)
75 {
76         struct ipvl_dev *ipvlan;
77         struct net_device *mdev = port->dev;
78         unsigned int flags;
79         int err;
80
81         ASSERT_RTNL();
82         if (port->mode != nval) {
83                 list_for_each_entry(ipvlan, &port->ipvlans, pnode) {
84                         flags = ipvlan->dev->flags;
85                         if (nval == IPVLAN_MODE_L3 || nval == IPVLAN_MODE_L3S) {
86                                 err = dev_change_flags(ipvlan->dev,
87                                                        flags | IFF_NOARP);
88                         } else {
89                                 err = dev_change_flags(ipvlan->dev,
90                                                        flags & ~IFF_NOARP);
91                         }
92                         if (unlikely(err))
93                                 goto fail;
94                 }
95                 if (nval == IPVLAN_MODE_L3S) {
96                         /* New mode is L3S */
97                         err = ipvlan_register_nf_hook(read_pnet(&port->pnet));
98                         if (!err) {
99                                 mdev->l3mdev_ops = &ipvl_l3mdev_ops;
100                                 mdev->priv_flags |= IFF_L3MDEV_RX_HANDLER;
101                         } else
102                                 goto fail;
103                 } else if (port->mode == IPVLAN_MODE_L3S) {
104                         /* Old mode was L3S */
105                         mdev->priv_flags &= ~IFF_L3MDEV_RX_HANDLER;
106                         ipvlan_unregister_nf_hook(read_pnet(&port->pnet));
107                         mdev->l3mdev_ops = NULL;
108                 }
109                 port->mode = nval;
110         }
111         return 0;
112
113 fail:
114         /* Undo the flags changes that have been done so far. */
115         list_for_each_entry_continue_reverse(ipvlan, &port->ipvlans, pnode) {
116                 flags = ipvlan->dev->flags;
117                 if (port->mode == IPVLAN_MODE_L3 ||
118                     port->mode == IPVLAN_MODE_L3S)
119                         dev_change_flags(ipvlan->dev, flags | IFF_NOARP);
120                 else
121                         dev_change_flags(ipvlan->dev, flags & ~IFF_NOARP);
122         }
123
124         return err;
125 }
126
127 static int ipvlan_port_create(struct net_device *dev)
128 {
129         struct ipvl_port *port;
130         int err, idx;
131
132         port = kzalloc(sizeof(struct ipvl_port), GFP_KERNEL);
133         if (!port)
134                 return -ENOMEM;
135
136         write_pnet(&port->pnet, dev_net(dev));
137         port->dev = dev;
138         port->mode = IPVLAN_MODE_L3;
139         INIT_LIST_HEAD(&port->ipvlans);
140         for (idx = 0; idx < IPVLAN_HASH_SIZE; idx++)
141                 INIT_HLIST_HEAD(&port->hlhead[idx]);
142
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;
147
148         err = netdev_rx_handler_register(dev, ipvlan_handle_frame, port);
149         if (err)
150                 goto err;
151
152         return 0;
153
154 err:
155         kfree(port);
156         return err;
157 }
158
159 static void ipvlan_port_destroy(struct net_device *dev)
160 {
161         struct ipvl_port *port = ipvlan_port_get_rtnl(dev);
162         struct sk_buff *skb;
163
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;
168         }
169         netdev_rx_handler_unregister(dev);
170         cancel_work_sync(&port->wq);
171         while ((skb = __skb_dequeue(&port->backlog)) != NULL) {
172                 if (skb->dev)
173                         dev_put(skb->dev);
174                 kfree_skb(skb);
175         }
176         ida_destroy(&port->ida);
177         kfree(port);
178 }
179
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)
183
184 #define IPVLAN_ALWAYS_ON \
185         (IPVLAN_ALWAYS_ON_OFLOADS | NETIF_F_LLTX | NETIF_F_VLAN_CHALLENGED)
186
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)
192
193         /* NETIF_F_GSO_ENCAP_ALL NETIF_F_GSO_SOFTWARE Newly added */
194
195 #define IPVLAN_STATE_MASK \
196         ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
197
198 static int ipvlan_init(struct net_device *dev)
199 {
200         struct ipvl_dev *ipvlan = netdev_priv(dev);
201         struct net_device *phy_dev = ipvlan->phy_dev;
202         struct ipvl_port *port;
203         int err;
204
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;
214
215         netdev_lockdep_set_classes(dev);
216
217         ipvlan->pcpu_stats = netdev_alloc_pcpu_stats(struct ipvl_pcpu_stats);
218         if (!ipvlan->pcpu_stats)
219                 return -ENOMEM;
220
221         if (!netif_is_ipvlan_port(phy_dev)) {
222                 err = ipvlan_port_create(phy_dev);
223                 if (err < 0) {
224                         free_percpu(ipvlan->pcpu_stats);
225                         return err;
226                 }
227         }
228         port = ipvlan_port_get_rtnl(phy_dev);
229         port->count += 1;
230         return 0;
231 }
232
233 static void ipvlan_uninit(struct net_device *dev)
234 {
235         struct ipvl_dev *ipvlan = netdev_priv(dev);
236         struct net_device *phy_dev = ipvlan->phy_dev;
237         struct ipvl_port *port;
238
239         free_percpu(ipvlan->pcpu_stats);
240
241         port = ipvlan_port_get_rtnl(phy_dev);
242         port->count -= 1;
243         if (!port->count)
244                 ipvlan_port_destroy(port->dev);
245 }
246
247 static int ipvlan_open(struct net_device *dev)
248 {
249         struct ipvl_dev *ipvlan = netdev_priv(dev);
250         struct ipvl_addr *addr;
251
252         if (ipvlan->port->mode == IPVLAN_MODE_L3 ||
253             ipvlan->port->mode == IPVLAN_MODE_L3S)
254                 dev->flags |= IFF_NOARP;
255         else
256                 dev->flags &= ~IFF_NOARP;
257
258         rcu_read_lock();
259         list_for_each_entry_rcu(addr, &ipvlan->addrs, anode)
260                 ipvlan_ht_addr_add(ipvlan, addr);
261         rcu_read_unlock();
262
263         return 0;
264 }
265
266 static int ipvlan_stop(struct net_device *dev)
267 {
268         struct ipvl_dev *ipvlan = netdev_priv(dev);
269         struct net_device *phy_dev = ipvlan->phy_dev;
270         struct ipvl_addr *addr;
271
272         dev_uc_unsync(phy_dev, dev);
273         dev_mc_unsync(phy_dev, dev);
274
275         rcu_read_lock();
276         list_for_each_entry_rcu(addr, &ipvlan->addrs, anode)
277                 ipvlan_ht_addr_del(addr);
278         rcu_read_unlock();
279
280         return 0;
281 }
282
283 static netdev_tx_t ipvlan_start_xmit(struct sk_buff *skb,
284                                      struct net_device *dev)
285 {
286         const struct ipvl_dev *ipvlan = netdev_priv(dev);
287         int skblen = skb->len;
288         int ret;
289
290         ret = ipvlan_queue_xmit(skb, dev);
291         if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
292                 struct ipvl_pcpu_stats *pcptr;
293
294                 pcptr = this_cpu_ptr(ipvlan->pcpu_stats);
295
296                 u64_stats_update_begin(&pcptr->syncp);
297                 pcptr->tx_pkts++;
298                 pcptr->tx_bytes += skblen;
299                 u64_stats_update_end(&pcptr->syncp);
300         } else {
301                 this_cpu_inc(ipvlan->pcpu_stats->tx_drps);
302         }
303         return ret;
304 }
305
306 static netdev_features_t ipvlan_fix_features(struct net_device *dev,
307                                              netdev_features_t features)
308 {
309         struct ipvl_dev *ipvlan = netdev_priv(dev);
310
311         features |= NETIF_F_ALL_FOR_ALL;
312         features &= (ipvlan->sfeatures | ~IPVLAN_FEATURES);
313         features = netdev_increment_features(ipvlan->phy_dev->features,
314                                              features, features);
315         features |= IPVLAN_ALWAYS_ON;
316         features &= (IPVLAN_FEATURES | IPVLAN_ALWAYS_ON);
317
318         return features;
319 }
320
321 static void ipvlan_change_rx_flags(struct net_device *dev, int change)
322 {
323         struct ipvl_dev *ipvlan = netdev_priv(dev);
324         struct net_device *phy_dev = ipvlan->phy_dev;
325
326         if (change & IFF_ALLMULTI)
327                 dev_set_allmulti(phy_dev, dev->flags & IFF_ALLMULTI? 1 : -1);
328 }
329
330 static void ipvlan_set_multicast_mac_filter(struct net_device *dev)
331 {
332         struct ipvl_dev *ipvlan = netdev_priv(dev);
333
334         if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
335                 bitmap_fill(ipvlan->mac_filters, IPVLAN_MAC_FILTER_SIZE);
336         } else {
337                 struct netdev_hw_addr *ha;
338                 DECLARE_BITMAP(mc_filters, IPVLAN_MAC_FILTER_SIZE);
339
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);
343
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.
347                  */
348                 __set_bit(ipvlan_mac_hash(dev->broadcast), mc_filters);
349
350                 bitmap_copy(ipvlan->mac_filters, mc_filters,
351                             IPVLAN_MAC_FILTER_SIZE);
352         }
353         dev_uc_sync(ipvlan->phy_dev, dev);
354         dev_mc_sync(ipvlan->phy_dev, dev);
355 }
356
357 static void ipvlan_get_stats64(struct net_device *dev,
358                                struct rtnl_link_stats64 *s)
359 {
360         struct ipvl_dev *ipvlan = netdev_priv(dev);
361
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;
366                 u32 strt;
367                 int idx;
368
369                 for_each_possible_cpu(idx) {
370                         pcptr = per_cpu_ptr(ipvlan->pcpu_stats, idx);
371                         do {
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,
379                                                            strt));
380
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;
386
387                         /* u32 values are updated without syncp protection. */
388                         rx_errs += pcptr->rx_errs;
389                         tx_drps += pcptr->tx_drps;
390                 }
391                 s->rx_errors = rx_errs;
392                 s->rx_dropped = rx_errs;
393                 s->tx_dropped = tx_drps;
394         }
395 }
396
397 static int ipvlan_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
398 {
399         struct ipvl_dev *ipvlan = netdev_priv(dev);
400         struct net_device *phy_dev = ipvlan->phy_dev;
401
402         return vlan_vid_add(phy_dev, proto, vid);
403 }
404
405 static int ipvlan_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
406                                    u16 vid)
407 {
408         struct ipvl_dev *ipvlan = netdev_priv(dev);
409         struct net_device *phy_dev = ipvlan->phy_dev;
410
411         vlan_vid_del(phy_dev, proto, vid);
412         return 0;
413 }
414
415 static int ipvlan_get_iflink(const struct net_device *dev)
416 {
417         struct ipvl_dev *ipvlan = netdev_priv(dev);
418
419         return ipvlan->phy_dev->ifindex;
420 }
421
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,
435 };
436
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)
440 {
441         const struct ipvl_dev *ipvlan = netdev_priv(dev);
442         struct net_device *phy_dev = ipvlan->phy_dev;
443
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.
447          */
448         return dev_hard_header(skb, phy_dev, type, daddr,
449                                saddr ? : phy_dev->dev_addr, len);
450 }
451
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,
457 };
458
459 static bool netif_is_ipvlan(const struct net_device *dev)
460 {
461         /* both ipvlan and ipvtap devices use the same netdev_ops */
462         return dev->netdev_ops == &ipvlan_netdev_ops;
463 }
464
465 static int ipvlan_ethtool_get_link_ksettings(struct net_device *dev,
466                                              struct ethtool_link_ksettings *cmd)
467 {
468         const struct ipvl_dev *ipvlan = netdev_priv(dev);
469
470         return __ethtool_get_link_ksettings(ipvlan->phy_dev, cmd);
471 }
472
473 static void ipvlan_ethtool_get_drvinfo(struct net_device *dev,
474                                        struct ethtool_drvinfo *drvinfo)
475 {
476         strlcpy(drvinfo->driver, IPVLAN_DRV, sizeof(drvinfo->driver));
477         strlcpy(drvinfo->version, IPV_DRV_VER, sizeof(drvinfo->version));
478 }
479
480 static u32 ipvlan_ethtool_get_msglevel(struct net_device *dev)
481 {
482         const struct ipvl_dev *ipvlan = netdev_priv(dev);
483
484         return ipvlan->msg_enable;
485 }
486
487 static void ipvlan_ethtool_set_msglevel(struct net_device *dev, u32 value)
488 {
489         struct ipvl_dev *ipvlan = netdev_priv(dev);
490
491         ipvlan->msg_enable = value;
492 }
493
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,
500 };
501
502 static int ipvlan_nl_changelink(struct net_device *dev,
503                                 struct nlattr *tb[], struct nlattr *data[],
504                                 struct netlink_ext_ack *extack)
505 {
506         struct ipvl_dev *ipvlan = netdev_priv(dev);
507         struct ipvl_port *port = ipvlan_port_get_rtnl(ipvlan->phy_dev);
508         int err = 0;
509
510         if (!data)
511                 return 0;
512         if (!ns_capable(dev_net(ipvlan->phy_dev)->user_ns, CAP_NET_ADMIN))
513                 return -EPERM;
514
515         if (data[IFLA_IPVLAN_MODE]) {
516                 u16 nmode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
517
518                 err = ipvlan_set_port_mode(port, nmode);
519         }
520
521         if (!err && data[IFLA_IPVLAN_FLAGS]) {
522                 u16 flags = nla_get_u16(data[IFLA_IPVLAN_FLAGS]);
523
524                 if (flags & IPVLAN_F_PRIVATE)
525                         ipvlan_mark_private(port);
526                 else
527                         ipvlan_clear_private(port);
528
529                 if (flags & IPVLAN_F_VEPA)
530                         ipvlan_mark_vepa(port);
531                 else
532                         ipvlan_clear_vepa(port);
533         }
534
535         return err;
536 }
537
538 static size_t ipvlan_nl_getsize(const struct net_device *dev)
539 {
540         return (0
541                 + nla_total_size(2) /* IFLA_IPVLAN_MODE */
542                 + nla_total_size(2) /* IFLA_IPVLAN_FLAGS */
543                 );
544 }
545
546 static int ipvlan_nl_validate(struct nlattr *tb[], struct nlattr *data[],
547                               struct netlink_ext_ack *extack)
548 {
549         if (!data)
550                 return 0;
551
552         if (data[IFLA_IPVLAN_MODE]) {
553                 u16 mode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
554
555                 if (mode < IPVLAN_MODE_L2 || mode >= IPVLAN_MODE_MAX)
556                         return -EINVAL;
557         }
558         if (data[IFLA_IPVLAN_FLAGS]) {
559                 u16 flags = nla_get_u16(data[IFLA_IPVLAN_FLAGS]);
560
561                 /* Only two bits are used at this moment. */
562                 if (flags & ~(IPVLAN_F_PRIVATE | IPVLAN_F_VEPA))
563                         return -EINVAL;
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))
567                         return -EINVAL;
568         }
569
570         return 0;
571 }
572
573 static int ipvlan_nl_fillinfo(struct sk_buff *skb,
574                               const struct net_device *dev)
575 {
576         struct ipvl_dev *ipvlan = netdev_priv(dev);
577         struct ipvl_port *port = ipvlan_port_get_rtnl(ipvlan->phy_dev);
578         int ret = -EINVAL;
579
580         if (!port)
581                 goto err;
582
583         ret = -EMSGSIZE;
584         if (nla_put_u16(skb, IFLA_IPVLAN_MODE, port->mode))
585                 goto err;
586         if (nla_put_u16(skb, IFLA_IPVLAN_FLAGS, port->flags))
587                 goto err;
588
589         return 0;
590
591 err:
592         return ret;
593 }
594
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)
598 {
599         struct ipvl_dev *ipvlan = netdev_priv(dev);
600         struct ipvl_port *port;
601         struct net_device *phy_dev;
602         int err;
603         u16 mode = IPVLAN_MODE_L3;
604
605         if (!tb[IFLA_LINK])
606                 return -EINVAL;
607
608         phy_dev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
609         if (!phy_dev)
610                 return -ENODEV;
611
612         if (netif_is_ipvlan(phy_dev)) {
613                 struct ipvl_dev *tmp = netdev_priv(phy_dev);
614
615                 phy_dev = tmp->phy_dev;
616                 if (!ns_capable(dev_net(phy_dev)->user_ns, CAP_NET_ADMIN))
617                         return -EPERM;
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) {
622                         netdev_err(phy_dev,
623                                    "Master is either lo or non-ether device\n");
624                         return -EINVAL;
625                 }
626
627                 if (netdev_is_rx_handler_busy(phy_dev)) {
628                         netdev_err(phy_dev, "Device is already in use.\n");
629                         return -EBUSY;
630                 }
631         }
632
633         ipvlan->phy_dev = phy_dev;
634         ipvlan->dev = dev;
635         ipvlan->sfeatures = IPVLAN_FEATURES;
636         if (!tb[IFLA_MTU])
637                 ipvlan_adjust_mtu(ipvlan, phy_dev);
638         INIT_LIST_HEAD(&ipvlan->addrs);
639         spin_lock_init(&ipvlan->addrs_lock);
640
641         /* TODO Probably put random address here to be presented to the
642          * world but keep using the physical-dev address for the outgoing
643          * packets.
644          */
645         memcpy(dev->dev_addr, phy_dev->dev_addr, ETH_ALEN);
646
647         dev->priv_flags |= IFF_NO_RX_HANDLER;
648
649         err = register_netdevice(dev);
650         if (err < 0)
651                 return err;
652
653         /* ipvlan_init() would have created the port, if required */
654         port = ipvlan_port_get_rtnl(phy_dev);
655         ipvlan->port = port;
656
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.
660          */
661         if (port->dev_id_start == 0xFFFE)
662                 port->dev_id_start = 0x1;
663
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()].
668          */
669         err = ida_simple_get(&port->ida, port->dev_id_start, 0xFFFE,
670                              GFP_KERNEL);
671         if (err < 0)
672                 err = ida_simple_get(&port->ida, 0x1, port->dev_id_start,
673                                      GFP_KERNEL);
674         if (err < 0)
675                 goto unregister_netdev;
676         dev->dev_id = err;
677
678         /* Increment id-base to the next slot for the future assignment */
679         port->dev_id_start = err + 1;
680
681         err = netdev_upper_dev_link(phy_dev, dev, extack);
682         if (err)
683                 goto remove_ida;
684
685         /* Flags are per port and latest update overrides. User has
686          * to be consistent in setting it just like the mode attribute.
687          */
688         if (data && data[IFLA_IPVLAN_FLAGS])
689                 port->flags = nla_get_u16(data[IFLA_IPVLAN_FLAGS]);
690
691         if (data && data[IFLA_IPVLAN_MODE])
692                 mode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
693
694         err = ipvlan_set_port_mode(port, mode);
695         if (err)
696                 goto unlink_netdev;
697
698         list_add_tail_rcu(&ipvlan->pnode, &port->ipvlans);
699         netif_stacked_transfer_operstate(phy_dev, dev);
700         return 0;
701
702 unlink_netdev:
703         netdev_upper_dev_unlink(phy_dev, dev);
704 remove_ida:
705         ida_simple_remove(&port->ida, dev->dev_id);
706 unregister_netdev:
707         unregister_netdevice(dev);
708         return err;
709 }
710 EXPORT_SYMBOL_GPL(ipvlan_link_new);
711
712 void ipvlan_link_delete(struct net_device *dev, struct list_head *head)
713 {
714         struct ipvl_dev *ipvlan = netdev_priv(dev);
715         struct ipvl_addr *addr, *next;
716
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);
722         }
723         spin_unlock_bh(&ipvlan->addrs_lock);
724
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);
729 }
730 EXPORT_SYMBOL_GPL(ipvlan_link_delete);
731
732 void ipvlan_link_setup(struct net_device *dev)
733 {
734         ether_setup(dev);
735
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;
743 }
744 EXPORT_SYMBOL_GPL(ipvlan_link_setup);
745
746 static const struct nla_policy ipvlan_nl_policy[IFLA_IPVLAN_MAX + 1] =
747 {
748         [IFLA_IPVLAN_MODE] = { .type = NLA_U16 },
749         [IFLA_IPVLAN_FLAGS] = { .type = NLA_U16 },
750 };
751
752 static struct rtnl_link_ops ipvlan_link_ops = {
753         .kind           = "ipvlan",
754         .priv_size      = sizeof(struct ipvl_dev),
755
756         .setup          = ipvlan_link_setup,
757         .newlink        = ipvlan_link_new,
758         .dellink        = ipvlan_link_delete,
759 };
760
761 int ipvlan_link_register(struct rtnl_link_ops *ops)
762 {
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);
770 }
771 EXPORT_SYMBOL_GPL(ipvlan_link_register);
772
773 static int ipvlan_device_event(struct notifier_block *unused,
774                                unsigned long event, void *ptr)
775 {
776         struct net_device *dev = netdev_notifier_info_to_dev(ptr);
777         struct ipvl_dev *ipvlan, *next;
778         struct ipvl_port *port;
779         LIST_HEAD(lst_kill);
780
781         if (!netif_is_ipvlan_port(dev))
782                 return NOTIFY_DONE;
783
784         port = ipvlan_port_get_rtnl(dev);
785
786         switch (event) {
787         case NETDEV_CHANGE:
788                 list_for_each_entry(ipvlan, &port->ipvlans, pnode)
789                         netif_stacked_transfer_operstate(ipvlan->phy_dev,
790                                                          ipvlan->dev);
791                 break;
792
793         case NETDEV_REGISTER: {
794                 struct net *oldnet, *newnet = dev_net(dev);
795                 struct ipvlan_netns *old_vnet;
796
797                 oldnet = read_pnet(&port->pnet);
798                 if (net_eq(newnet, oldnet))
799                         break;
800
801                 write_pnet(&port->pnet, newnet);
802
803                 old_vnet = net_generic(oldnet, ipvlan_netid);
804                 if (!old_vnet->ipvl_nf_hook_refcnt)
805                         break;
806
807                 ipvlan_register_nf_hook(newnet);
808                 ipvlan_unregister_nf_hook(oldnet);
809                 break;
810         }
811         case NETDEV_UNREGISTER:
812                 if (dev->reg_state != NETREG_UNREGISTERING)
813                         break;
814
815                 list_for_each_entry_safe(ipvlan, next, &port->ipvlans, pnode)
816                         ipvlan->dev->rtnl_link_ops->dellink(ipvlan->dev,
817                                                             &lst_kill);
818                 unregister_netdevice_many(&lst_kill);
819                 break;
820
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);
826                 }
827                 break;
828
829         case NETDEV_CHANGEMTU:
830                 list_for_each_entry(ipvlan, &port->ipvlans, pnode)
831                         ipvlan_adjust_mtu(ipvlan, dev);
832                 break;
833
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);
838                 }
839                 break;
840
841         case NETDEV_PRE_TYPE_CHANGE:
842                 /* Forbid underlying device to change its type. */
843                 return NOTIFY_BAD;
844         }
845         return NOTIFY_DONE;
846 }
847
848 /* the caller must held the addrs lock */
849 static int ipvlan_add_addr(struct ipvl_dev *ipvlan, void *iaddr, bool is_v6)
850 {
851         struct ipvl_addr *addr;
852
853         addr = kzalloc(sizeof(struct ipvl_addr), GFP_ATOMIC);
854         if (!addr)
855                 return -ENOMEM;
856
857         addr->master = ipvlan;
858         if (!is_v6) {
859                 memcpy(&addr->ip4addr, iaddr, sizeof(struct in_addr));
860                 addr->atype = IPVL_IPV4;
861 #if IS_ENABLED(CONFIG_IPV6)
862         } else {
863                 memcpy(&addr->ip6addr, iaddr, sizeof(struct in6_addr));
864                 addr->atype = IPVL_IPV6;
865 #endif
866         }
867
868         list_add_tail_rcu(&addr->anode, &ipvlan->addrs);
869
870         /* If the interface is not up, the address will be added to the hash
871          * list by ipvlan_open.
872          */
873         if (netif_running(ipvlan->dev))
874                 ipvlan_ht_addr_add(ipvlan, addr);
875
876         return 0;
877 }
878
879 static void ipvlan_del_addr(struct ipvl_dev *ipvlan, void *iaddr, bool is_v6)
880 {
881         struct ipvl_addr *addr;
882
883         spin_lock_bh(&ipvlan->addrs_lock);
884         addr = ipvlan_find_addr(ipvlan, iaddr, is_v6);
885         if (!addr) {
886                 spin_unlock_bh(&ipvlan->addrs_lock);
887                 return;
888         }
889
890         ipvlan_ht_addr_del(addr);
891         list_del_rcu(&addr->anode);
892         spin_unlock_bh(&ipvlan->addrs_lock);
893         kfree_rcu(addr, rcu);
894 }
895
896 static bool ipvlan_is_valid_dev(const struct net_device *dev)
897 {
898         struct ipvl_dev *ipvlan = netdev_priv(dev);
899
900         if (!netif_is_ipvlan(dev))
901                 return false;
902
903         if (!ipvlan || !ipvlan->port)
904                 return false;
905
906         return true;
907 }
908
909 #if IS_ENABLED(CONFIG_IPV6)
910 static int ipvlan_add_addr6(struct ipvl_dev *ipvlan, struct in6_addr *ip6_addr)
911 {
912         int ret = -EINVAL;
913
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);
919         else
920                 ret = ipvlan_add_addr(ipvlan, ip6_addr, true);
921         spin_unlock_bh(&ipvlan->addrs_lock);
922         return ret;
923 }
924
925 static void ipvlan_del_addr6(struct ipvl_dev *ipvlan, struct in6_addr *ip6_addr)
926 {
927         return ipvlan_del_addr(ipvlan, ip6_addr, true);
928 }
929
930 static int ipvlan_addr6_event(struct notifier_block *unused,
931                               unsigned long event, void *ptr)
932 {
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);
936
937         if (!ipvlan_is_valid_dev(dev))
938                 return NOTIFY_DONE;
939
940         switch (event) {
941         case NETDEV_UP:
942                 if (ipvlan_add_addr6(ipvlan, &if6->addr))
943                         return NOTIFY_BAD;
944                 break;
945
946         case NETDEV_DOWN:
947                 ipvlan_del_addr6(ipvlan, &if6->addr);
948                 break;
949         }
950
951         return NOTIFY_OK;
952 }
953
954 static int ipvlan_addr6_validator_event(struct notifier_block *unused,
955                                         unsigned long event, void *ptr)
956 {
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);
960
961         if (!ipvlan_is_valid_dev(dev))
962                 return NOTIFY_DONE;
963
964         switch (event) {
965         case NETDEV_UP:
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);
970                 }
971                 break;
972         }
973
974         return NOTIFY_OK;
975 }
976 #endif
977
978 static int ipvlan_add_addr4(struct ipvl_dev *ipvlan, struct in_addr *ip4_addr)
979 {
980         int ret = -EINVAL;
981
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);
987         else
988                 ret = ipvlan_add_addr(ipvlan, ip4_addr, false);
989         spin_unlock_bh(&ipvlan->addrs_lock);
990         return ret;
991 }
992
993 static void ipvlan_del_addr4(struct ipvl_dev *ipvlan, struct in_addr *ip4_addr)
994 {
995         return ipvlan_del_addr(ipvlan, ip4_addr, false);
996 }
997
998 static int ipvlan_addr4_event(struct notifier_block *unused,
999                               unsigned long event, void *ptr)
1000 {
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;
1005
1006         if (!ipvlan_is_valid_dev(dev))
1007                 return NOTIFY_DONE;
1008
1009         switch (event) {
1010         case NETDEV_UP:
1011                 ip4_addr.s_addr = if4->ifa_address;
1012                 if (ipvlan_add_addr4(ipvlan, &ip4_addr))
1013                         return NOTIFY_BAD;
1014                 break;
1015
1016         case NETDEV_DOWN:
1017                 ip4_addr.s_addr = if4->ifa_address;
1018                 ipvlan_del_addr4(ipvlan, &ip4_addr);
1019                 break;
1020         }
1021
1022         return NOTIFY_OK;
1023 }
1024
1025 static int ipvlan_addr4_validator_event(struct notifier_block *unused,
1026                                         unsigned long event, void *ptr)
1027 {
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);
1031
1032         if (!ipvlan_is_valid_dev(dev))
1033                 return NOTIFY_DONE;
1034
1035         switch (event) {
1036         case NETDEV_UP:
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);
1041                 }
1042                 break;
1043         }
1044
1045         return NOTIFY_OK;
1046 }
1047
1048 static struct notifier_block ipvlan_addr4_notifier_block __read_mostly = {
1049         .notifier_call = ipvlan_addr4_event,
1050 };
1051
1052 static struct notifier_block ipvlan_addr4_vtor_notifier_block __read_mostly = {
1053         .notifier_call = ipvlan_addr4_validator_event,
1054 };
1055
1056 static struct notifier_block ipvlan_notifier_block __read_mostly = {
1057         .notifier_call = ipvlan_device_event,
1058 };
1059
1060 #if IS_ENABLED(CONFIG_IPV6)
1061 static struct notifier_block ipvlan_addr6_notifier_block __read_mostly = {
1062         .notifier_call = ipvlan_addr6_event,
1063 };
1064
1065 static struct notifier_block ipvlan_addr6_vtor_notifier_block __read_mostly = {
1066         .notifier_call = ipvlan_addr6_validator_event,
1067 };
1068 #endif
1069
1070 static void ipvlan_ns_exit(struct net *net)
1071 {
1072         struct ipvlan_netns *vnet = net_generic(net, ipvlan_netid);
1073
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));
1078         }
1079 }
1080
1081 static struct pernet_operations ipvlan_net_ops = {
1082         .id = &ipvlan_netid,
1083         .size = sizeof(struct ipvlan_netns),
1084         .exit = ipvlan_ns_exit,
1085 };
1086
1087 static int __init ipvlan_init_module(void)
1088 {
1089         int err;
1090
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);
1097 #endif
1098         register_inetaddr_notifier(&ipvlan_addr4_notifier_block);
1099         register_inetaddr_validator_notifier(&ipvlan_addr4_vtor_notifier_block);
1100
1101         err = register_pernet_subsys(&ipvlan_net_ops);
1102         if (err < 0)
1103                 goto error;
1104
1105         err = ipvlan_link_register(&ipvlan_link_ops);
1106         if (err < 0) {
1107                 unregister_pernet_subsys(&ipvlan_net_ops);
1108                 goto error;
1109         }
1110
1111         return 0;
1112 error:
1113         unregister_inetaddr_notifier(&ipvlan_addr4_notifier_block);
1114         unregister_inetaddr_validator_notifier(
1115             &ipvlan_addr4_vtor_notifier_block);
1116 #if IS_ENABLED(CONFIG_IPV6)
1117         unregister_inet6addr_notifier(&ipvlan_addr6_notifier_block);
1118         unregister_inet6addr_validator_notifier(
1119             &ipvlan_addr6_vtor_notifier_block);
1120 #endif
1121         unregister_netdevice_notifier(&ipvlan_notifier_block);
1122         return err;
1123 }
1124
1125 static void __exit ipvlan_cleanup_module(void)
1126 {
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);
1137 #endif
1138 }
1139
1140 module_init(ipvlan_init_module);
1141 module_exit(ipvlan_cleanup_module);
1142
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");