GNU Linux-libre 4.14.332-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         {
26                 .hook     = ipvlan_nf_input,
27                 .pf       = NFPROTO_IPV6,
28                 .hooknum  = NF_INET_LOCAL_IN,
29                 .priority = INT_MAX,
30         },
31 };
32
33 static const struct l3mdev_ops ipvl_l3mdev_ops = {
34         .l3mdev_l3_rcv = ipvlan_l3_rcv,
35 };
36
37 static void ipvlan_adjust_mtu(struct ipvl_dev *ipvlan, struct net_device *dev)
38 {
39         ipvlan->dev->mtu = dev->mtu;
40 }
41
42 static int ipvlan_register_nf_hook(struct net *net)
43 {
44         struct ipvlan_netns *vnet = net_generic(net, ipvlan_netid);
45         int err = 0;
46
47         if (!vnet->ipvl_nf_hook_refcnt) {
48                 err = nf_register_net_hooks(net, ipvl_nfops,
49                                             ARRAY_SIZE(ipvl_nfops));
50                 if (!err)
51                         vnet->ipvl_nf_hook_refcnt = 1;
52         } else {
53                 vnet->ipvl_nf_hook_refcnt++;
54         }
55
56         return err;
57 }
58
59 static void ipvlan_unregister_nf_hook(struct net *net)
60 {
61         struct ipvlan_netns *vnet = net_generic(net, ipvlan_netid);
62
63         if (WARN_ON(!vnet->ipvl_nf_hook_refcnt))
64                 return;
65
66         vnet->ipvl_nf_hook_refcnt--;
67         if (!vnet->ipvl_nf_hook_refcnt)
68                 nf_unregister_net_hooks(net, ipvl_nfops,
69                                         ARRAY_SIZE(ipvl_nfops));
70 }
71
72 static int ipvlan_set_port_mode(struct ipvl_port *port, u16 nval)
73 {
74         struct ipvl_dev *ipvlan;
75         struct net_device *mdev = port->dev;
76         unsigned int flags;
77         int err;
78
79         ASSERT_RTNL();
80         if (port->mode != nval) {
81                 list_for_each_entry(ipvlan, &port->ipvlans, pnode) {
82                         flags = ipvlan->dev->flags;
83                         if (nval == IPVLAN_MODE_L3 || nval == IPVLAN_MODE_L3S) {
84                                 err = dev_change_flags(ipvlan->dev,
85                                                        flags | IFF_NOARP);
86                         } else {
87                                 err = dev_change_flags(ipvlan->dev,
88                                                        flags & ~IFF_NOARP);
89                         }
90                         if (unlikely(err))
91                                 goto fail;
92                 }
93                 if (nval == IPVLAN_MODE_L3S) {
94                         /* New mode is L3S */
95                         err = ipvlan_register_nf_hook(read_pnet(&port->pnet));
96                         if (!err) {
97                                 mdev->l3mdev_ops = &ipvl_l3mdev_ops;
98                                 mdev->priv_flags |= IFF_L3MDEV_RX_HANDLER;
99                         } else
100                                 goto fail;
101                 } else if (port->mode == IPVLAN_MODE_L3S) {
102                         /* Old mode was L3S */
103                         mdev->priv_flags &= ~IFF_L3MDEV_RX_HANDLER;
104                         ipvlan_unregister_nf_hook(read_pnet(&port->pnet));
105                         mdev->l3mdev_ops = NULL;
106                 }
107                 port->mode = nval;
108         }
109         return 0;
110
111 fail:
112         /* Undo the flags changes that have been done so far. */
113         list_for_each_entry_continue_reverse(ipvlan, &port->ipvlans, pnode) {
114                 flags = ipvlan->dev->flags;
115                 if (port->mode == IPVLAN_MODE_L3 ||
116                     port->mode == IPVLAN_MODE_L3S)
117                         dev_change_flags(ipvlan->dev, flags | IFF_NOARP);
118                 else
119                         dev_change_flags(ipvlan->dev, flags & ~IFF_NOARP);
120         }
121
122         return err;
123 }
124
125 static int ipvlan_port_create(struct net_device *dev)
126 {
127         struct ipvl_port *port;
128         int err, idx;
129
130         if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK) {
131                 netdev_err(dev, "Master is either lo or non-ether device\n");
132                 return -EINVAL;
133         }
134
135         if (netdev_is_rx_handler_busy(dev)) {
136                 netdev_err(dev, "Device is already in use.\n");
137                 return -EBUSY;
138         }
139
140         port = kzalloc(sizeof(struct ipvl_port), GFP_KERNEL);
141         if (!port)
142                 return -ENOMEM;
143
144         write_pnet(&port->pnet, dev_net(dev));
145         port->dev = dev;
146         port->mode = IPVLAN_MODE_L3;
147         INIT_LIST_HEAD(&port->ipvlans);
148         for (idx = 0; idx < IPVLAN_HASH_SIZE; idx++)
149                 INIT_HLIST_HEAD(&port->hlhead[idx]);
150
151         skb_queue_head_init(&port->backlog);
152         INIT_WORK(&port->wq, ipvlan_process_multicast);
153         ida_init(&port->ida);
154         port->dev_id_start = 1;
155
156         err = netdev_rx_handler_register(dev, ipvlan_handle_frame, port);
157         if (err)
158                 goto err;
159
160         dev->priv_flags |= IFF_IPVLAN_MASTER;
161         return 0;
162
163 err:
164         kfree(port);
165         return err;
166 }
167
168 static void ipvlan_port_destroy(struct net_device *dev)
169 {
170         struct ipvl_port *port = ipvlan_port_get_rtnl(dev);
171         struct sk_buff *skb;
172
173         dev->priv_flags &= ~IFF_IPVLAN_MASTER;
174         if (port->mode == IPVLAN_MODE_L3S) {
175                 dev->priv_flags &= ~IFF_L3MDEV_RX_HANDLER;
176                 ipvlan_unregister_nf_hook(dev_net(dev));
177                 dev->l3mdev_ops = NULL;
178         }
179         netdev_rx_handler_unregister(dev);
180         cancel_work_sync(&port->wq);
181         while ((skb = __skb_dequeue(&port->backlog)) != NULL) {
182                 if (skb->dev)
183                         dev_put(skb->dev);
184                 kfree_skb(skb);
185         }
186         ida_destroy(&port->ida);
187         kfree(port);
188 }
189
190 #define IPVLAN_ALWAYS_ON_OFLOADS \
191         (NETIF_F_SG | NETIF_F_HW_CSUM | \
192          NETIF_F_GSO_ROBUST | NETIF_F_GSO_SOFTWARE | NETIF_F_GSO_ENCAP_ALL)
193
194 #define IPVLAN_ALWAYS_ON \
195         (IPVLAN_ALWAYS_ON_OFLOADS | NETIF_F_LLTX | NETIF_F_VLAN_CHALLENGED)
196
197 #define IPVLAN_FEATURES \
198         (NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
199          NETIF_F_GSO | NETIF_F_TSO | NETIF_F_GSO_ROBUST | \
200          NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_GRO | NETIF_F_RXCSUM | \
201          NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_STAG_FILTER)
202
203         /* NETIF_F_GSO_ENCAP_ALL NETIF_F_GSO_SOFTWARE Newly added */
204
205 #define IPVLAN_STATE_MASK \
206         ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
207
208 static int ipvlan_init(struct net_device *dev)
209 {
210         struct ipvl_dev *ipvlan = netdev_priv(dev);
211         const struct net_device *phy_dev = ipvlan->phy_dev;
212         struct ipvl_port *port = ipvlan->port;
213
214         dev->state = (dev->state & ~IPVLAN_STATE_MASK) |
215                      (phy_dev->state & IPVLAN_STATE_MASK);
216         dev->features = phy_dev->features & IPVLAN_FEATURES;
217         dev->features |= IPVLAN_ALWAYS_ON;
218         dev->vlan_features = phy_dev->vlan_features & IPVLAN_FEATURES;
219         dev->vlan_features |= IPVLAN_ALWAYS_ON_OFLOADS;
220         dev->gso_max_size = phy_dev->gso_max_size;
221         dev->gso_max_segs = phy_dev->gso_max_segs;
222         dev->hard_header_len = phy_dev->hard_header_len;
223
224         netdev_lockdep_set_classes(dev);
225
226         ipvlan->pcpu_stats = netdev_alloc_pcpu_stats(struct ipvl_pcpu_stats);
227         if (!ipvlan->pcpu_stats)
228                 return -ENOMEM;
229
230         port->count += 1;
231
232         return 0;
233 }
234
235 static void ipvlan_uninit(struct net_device *dev)
236 {
237         struct ipvl_dev *ipvlan = netdev_priv(dev);
238         struct ipvl_port *port = ipvlan->port;
239
240         free_percpu(ipvlan->pcpu_stats);
241
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         list_for_each_entry(addr, &ipvlan->addrs, anode)
259                 ipvlan_ht_addr_add(ipvlan, addr);
260
261         return 0;
262 }
263
264 static int ipvlan_stop(struct net_device *dev)
265 {
266         struct ipvl_dev *ipvlan = netdev_priv(dev);
267         struct net_device *phy_dev = ipvlan->phy_dev;
268         struct ipvl_addr *addr;
269
270         dev_uc_unsync(phy_dev, dev);
271         dev_mc_unsync(phy_dev, dev);
272
273         list_for_each_entry(addr, &ipvlan->addrs, anode)
274                 ipvlan_ht_addr_del(addr);
275
276         return 0;
277 }
278
279 static netdev_tx_t ipvlan_start_xmit(struct sk_buff *skb,
280                                      struct net_device *dev)
281 {
282         const struct ipvl_dev *ipvlan = netdev_priv(dev);
283         int skblen = skb->len;
284         int ret;
285
286         ret = ipvlan_queue_xmit(skb, dev);
287         if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
288                 struct ipvl_pcpu_stats *pcptr;
289
290                 pcptr = this_cpu_ptr(ipvlan->pcpu_stats);
291
292                 u64_stats_update_begin(&pcptr->syncp);
293                 pcptr->tx_pkts++;
294                 pcptr->tx_bytes += skblen;
295                 u64_stats_update_end(&pcptr->syncp);
296         } else {
297                 this_cpu_inc(ipvlan->pcpu_stats->tx_drps);
298         }
299         return ret;
300 }
301
302 static netdev_features_t ipvlan_fix_features(struct net_device *dev,
303                                              netdev_features_t features)
304 {
305         struct ipvl_dev *ipvlan = netdev_priv(dev);
306
307         features |= NETIF_F_ALL_FOR_ALL;
308         features &= (ipvlan->sfeatures | ~IPVLAN_FEATURES);
309         features = netdev_increment_features(ipvlan->phy_dev->features,
310                                              features, features);
311         features |= IPVLAN_ALWAYS_ON;
312         features &= (IPVLAN_FEATURES | IPVLAN_ALWAYS_ON);
313
314         return features;
315 }
316
317 static void ipvlan_change_rx_flags(struct net_device *dev, int change)
318 {
319         struct ipvl_dev *ipvlan = netdev_priv(dev);
320         struct net_device *phy_dev = ipvlan->phy_dev;
321
322         if (change & IFF_ALLMULTI)
323                 dev_set_allmulti(phy_dev, dev->flags & IFF_ALLMULTI? 1 : -1);
324 }
325
326 static void ipvlan_set_multicast_mac_filter(struct net_device *dev)
327 {
328         struct ipvl_dev *ipvlan = netdev_priv(dev);
329
330         if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
331                 bitmap_fill(ipvlan->mac_filters, IPVLAN_MAC_FILTER_SIZE);
332         } else {
333                 struct netdev_hw_addr *ha;
334                 DECLARE_BITMAP(mc_filters, IPVLAN_MAC_FILTER_SIZE);
335
336                 bitmap_zero(mc_filters, IPVLAN_MAC_FILTER_SIZE);
337                 netdev_for_each_mc_addr(ha, dev)
338                         __set_bit(ipvlan_mac_hash(ha->addr), mc_filters);
339
340                 /* Turn-on broadcast bit irrespective of address family,
341                  * since broadcast is deferred to a work-queue, hence no
342                  * impact on fast-path processing.
343                  */
344                 __set_bit(ipvlan_mac_hash(dev->broadcast), mc_filters);
345
346                 bitmap_copy(ipvlan->mac_filters, mc_filters,
347                             IPVLAN_MAC_FILTER_SIZE);
348         }
349         dev_uc_sync(ipvlan->phy_dev, dev);
350         dev_mc_sync(ipvlan->phy_dev, dev);
351 }
352
353 static void ipvlan_get_stats64(struct net_device *dev,
354                                struct rtnl_link_stats64 *s)
355 {
356         struct ipvl_dev *ipvlan = netdev_priv(dev);
357
358         if (ipvlan->pcpu_stats) {
359                 struct ipvl_pcpu_stats *pcptr;
360                 u64 rx_pkts, rx_bytes, rx_mcast, tx_pkts, tx_bytes;
361                 u32 rx_errs = 0, tx_drps = 0;
362                 u32 strt;
363                 int idx;
364
365                 for_each_possible_cpu(idx) {
366                         pcptr = per_cpu_ptr(ipvlan->pcpu_stats, idx);
367                         do {
368                                 strt= u64_stats_fetch_begin_irq(&pcptr->syncp);
369                                 rx_pkts = pcptr->rx_pkts;
370                                 rx_bytes = pcptr->rx_bytes;
371                                 rx_mcast = pcptr->rx_mcast;
372                                 tx_pkts = pcptr->tx_pkts;
373                                 tx_bytes = pcptr->tx_bytes;
374                         } while (u64_stats_fetch_retry_irq(&pcptr->syncp,
375                                                            strt));
376
377                         s->rx_packets += rx_pkts;
378                         s->rx_bytes += rx_bytes;
379                         s->multicast += rx_mcast;
380                         s->tx_packets += tx_pkts;
381                         s->tx_bytes += tx_bytes;
382
383                         /* u32 values are updated without syncp protection. */
384                         rx_errs += pcptr->rx_errs;
385                         tx_drps += pcptr->tx_drps;
386                 }
387                 s->rx_errors = rx_errs;
388                 s->rx_dropped = rx_errs;
389                 s->tx_dropped = tx_drps;
390         }
391 }
392
393 static int ipvlan_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
394 {
395         struct ipvl_dev *ipvlan = netdev_priv(dev);
396         struct net_device *phy_dev = ipvlan->phy_dev;
397
398         return vlan_vid_add(phy_dev, proto, vid);
399 }
400
401 static int ipvlan_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
402                                    u16 vid)
403 {
404         struct ipvl_dev *ipvlan = netdev_priv(dev);
405         struct net_device *phy_dev = ipvlan->phy_dev;
406
407         vlan_vid_del(phy_dev, proto, vid);
408         return 0;
409 }
410
411 static int ipvlan_get_iflink(const struct net_device *dev)
412 {
413         struct ipvl_dev *ipvlan = netdev_priv(dev);
414
415         return ipvlan->phy_dev->ifindex;
416 }
417
418 static const struct net_device_ops ipvlan_netdev_ops = {
419         .ndo_init               = ipvlan_init,
420         .ndo_uninit             = ipvlan_uninit,
421         .ndo_open               = ipvlan_open,
422         .ndo_stop               = ipvlan_stop,
423         .ndo_start_xmit         = ipvlan_start_xmit,
424         .ndo_fix_features       = ipvlan_fix_features,
425         .ndo_change_rx_flags    = ipvlan_change_rx_flags,
426         .ndo_set_rx_mode        = ipvlan_set_multicast_mac_filter,
427         .ndo_get_stats64        = ipvlan_get_stats64,
428         .ndo_vlan_rx_add_vid    = ipvlan_vlan_rx_add_vid,
429         .ndo_vlan_rx_kill_vid   = ipvlan_vlan_rx_kill_vid,
430         .ndo_get_iflink         = ipvlan_get_iflink,
431 };
432
433 static int ipvlan_hard_header(struct sk_buff *skb, struct net_device *dev,
434                               unsigned short type, const void *daddr,
435                               const void *saddr, unsigned len)
436 {
437         const struct ipvl_dev *ipvlan = netdev_priv(dev);
438         struct net_device *phy_dev = ipvlan->phy_dev;
439
440         /* TODO Probably use a different field than dev_addr so that the
441          * mac-address on the virtual device is portable and can be carried
442          * while the packets use the mac-addr on the physical device.
443          */
444         return dev_hard_header(skb, phy_dev, type, daddr,
445                                saddr ? : dev->dev_addr, len);
446 }
447
448 static const struct header_ops ipvlan_header_ops = {
449         .create         = ipvlan_hard_header,
450         .parse          = eth_header_parse,
451         .cache          = eth_header_cache,
452         .cache_update   = eth_header_cache_update,
453 };
454
455 static int ipvlan_ethtool_get_link_ksettings(struct net_device *dev,
456                                              struct ethtool_link_ksettings *cmd)
457 {
458         const struct ipvl_dev *ipvlan = netdev_priv(dev);
459
460         return __ethtool_get_link_ksettings(ipvlan->phy_dev, cmd);
461 }
462
463 static void ipvlan_ethtool_get_drvinfo(struct net_device *dev,
464                                        struct ethtool_drvinfo *drvinfo)
465 {
466         strlcpy(drvinfo->driver, IPVLAN_DRV, sizeof(drvinfo->driver));
467         strlcpy(drvinfo->version, IPV_DRV_VER, sizeof(drvinfo->version));
468 }
469
470 static u32 ipvlan_ethtool_get_msglevel(struct net_device *dev)
471 {
472         const struct ipvl_dev *ipvlan = netdev_priv(dev);
473
474         return ipvlan->msg_enable;
475 }
476
477 static void ipvlan_ethtool_set_msglevel(struct net_device *dev, u32 value)
478 {
479         struct ipvl_dev *ipvlan = netdev_priv(dev);
480
481         ipvlan->msg_enable = value;
482 }
483
484 static const struct ethtool_ops ipvlan_ethtool_ops = {
485         .get_link       = ethtool_op_get_link,
486         .get_link_ksettings     = ipvlan_ethtool_get_link_ksettings,
487         .get_drvinfo    = ipvlan_ethtool_get_drvinfo,
488         .get_msglevel   = ipvlan_ethtool_get_msglevel,
489         .set_msglevel   = ipvlan_ethtool_set_msglevel,
490 };
491
492 static int ipvlan_nl_changelink(struct net_device *dev,
493                                 struct nlattr *tb[], struct nlattr *data[],
494                                 struct netlink_ext_ack *extack)
495 {
496         struct ipvl_dev *ipvlan = netdev_priv(dev);
497         struct ipvl_port *port = ipvlan_port_get_rtnl(ipvlan->phy_dev);
498         int err = 0;
499
500         if (!data)
501                 return 0;
502         if (!ns_capable(dev_net(ipvlan->phy_dev)->user_ns, CAP_NET_ADMIN))
503                 return -EPERM;
504
505         if (data[IFLA_IPVLAN_MODE]) {
506                 u16 nmode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
507
508                 err = ipvlan_set_port_mode(port, nmode);
509         }
510         return err;
511 }
512
513 static size_t ipvlan_nl_getsize(const struct net_device *dev)
514 {
515         return (0
516                 + nla_total_size(2) /* IFLA_IPVLAN_MODE */
517                 );
518 }
519
520 static int ipvlan_nl_validate(struct nlattr *tb[], struct nlattr *data[],
521                               struct netlink_ext_ack *extack)
522 {
523         if (data && data[IFLA_IPVLAN_MODE]) {
524                 u16 mode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
525
526                 if (mode < IPVLAN_MODE_L2 || mode >= IPVLAN_MODE_MAX)
527                         return -EINVAL;
528         }
529         return 0;
530 }
531
532 static int ipvlan_nl_fillinfo(struct sk_buff *skb,
533                               const struct net_device *dev)
534 {
535         struct ipvl_dev *ipvlan = netdev_priv(dev);
536         struct ipvl_port *port = ipvlan_port_get_rtnl(ipvlan->phy_dev);
537         int ret = -EINVAL;
538
539         if (!port)
540                 goto err;
541
542         ret = -EMSGSIZE;
543         if (nla_put_u16(skb, IFLA_IPVLAN_MODE, port->mode))
544                 goto err;
545
546         return 0;
547
548 err:
549         return ret;
550 }
551
552 int ipvlan_link_new(struct net *src_net, struct net_device *dev,
553                     struct nlattr *tb[], struct nlattr *data[],
554                     struct netlink_ext_ack *extack)
555 {
556         struct ipvl_dev *ipvlan = netdev_priv(dev);
557         struct ipvl_port *port;
558         struct net_device *phy_dev;
559         int err;
560         u16 mode = IPVLAN_MODE_L3;
561         bool create = false;
562
563         if (!tb[IFLA_LINK])
564                 return -EINVAL;
565
566         phy_dev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
567         if (!phy_dev)
568                 return -ENODEV;
569
570         if (netif_is_ipvlan(phy_dev)) {
571                 struct ipvl_dev *tmp = netdev_priv(phy_dev);
572
573                 phy_dev = tmp->phy_dev;
574                 if (!ns_capable(dev_net(phy_dev)->user_ns, CAP_NET_ADMIN))
575                         return -EPERM;
576         } else if (!netif_is_ipvlan_port(phy_dev)) {
577                 err = ipvlan_port_create(phy_dev);
578                 if (err < 0)
579                         return err;
580                 create = true;
581         }
582
583         if (data && data[IFLA_IPVLAN_MODE])
584                 mode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
585
586         port = ipvlan_port_get_rtnl(phy_dev);
587         ipvlan->phy_dev = phy_dev;
588         ipvlan->dev = dev;
589         ipvlan->port = port;
590         ipvlan->sfeatures = IPVLAN_FEATURES;
591         if (!tb[IFLA_MTU])
592                 ipvlan_adjust_mtu(ipvlan, phy_dev);
593         INIT_LIST_HEAD(&ipvlan->addrs);
594
595         /* If the port-id base is at the MAX value, then wrap it around and
596          * begin from 0x1 again. This may be due to a busy system where lots
597          * of slaves are getting created and deleted.
598          */
599         if (port->dev_id_start == 0xFFFE)
600                 port->dev_id_start = 0x1;
601
602         /* Since L2 address is shared among all IPvlan slaves including
603          * master, use unique 16 bit dev-ids to diffentiate among them.
604          * Assign IDs between 0x1 and 0xFFFE (used by the master) to each
605          * slave link [see addrconf_ifid_eui48()].
606          */
607         err = ida_simple_get(&port->ida, port->dev_id_start, 0xFFFE,
608                              GFP_KERNEL);
609         if (err < 0)
610                 err = ida_simple_get(&port->ida, 0x1, port->dev_id_start,
611                                      GFP_KERNEL);
612         if (err < 0)
613                 goto destroy_ipvlan_port;
614         dev->dev_id = err;
615         /* Increment id-base to the next slot for the future assignment */
616         port->dev_id_start = err + 1;
617
618         /* TODO Probably put random address here to be presented to the
619          * world but keep using the physical-dev address for the outgoing
620          * packets.
621          */
622         memcpy(dev->dev_addr, phy_dev->dev_addr, ETH_ALEN);
623
624         dev->priv_flags |= IFF_IPVLAN_SLAVE;
625
626         err = register_netdevice(dev);
627         if (err < 0)
628                 goto remove_ida;
629
630         err = netdev_upper_dev_link(phy_dev, dev);
631         if (err) {
632                 goto unregister_netdev;
633         }
634         err = ipvlan_set_port_mode(port, mode);
635         if (err) {
636                 goto unlink_netdev;
637         }
638
639         list_add_tail_rcu(&ipvlan->pnode, &port->ipvlans);
640         netif_stacked_transfer_operstate(phy_dev, dev);
641         return 0;
642
643 unlink_netdev:
644         netdev_upper_dev_unlink(phy_dev, dev);
645 unregister_netdev:
646         unregister_netdevice(dev);
647 remove_ida:
648         ida_simple_remove(&port->ida, dev->dev_id);
649 destroy_ipvlan_port:
650         if (create)
651                 ipvlan_port_destroy(phy_dev);
652         return err;
653 }
654 EXPORT_SYMBOL_GPL(ipvlan_link_new);
655
656 void ipvlan_link_delete(struct net_device *dev, struct list_head *head)
657 {
658         struct ipvl_dev *ipvlan = netdev_priv(dev);
659         struct ipvl_addr *addr, *next;
660
661         list_for_each_entry_safe(addr, next, &ipvlan->addrs, anode) {
662                 ipvlan_ht_addr_del(addr);
663                 list_del(&addr->anode);
664                 kfree_rcu(addr, rcu);
665         }
666
667         ida_simple_remove(&ipvlan->port->ida, dev->dev_id);
668         list_del_rcu(&ipvlan->pnode);
669         unregister_netdevice_queue(dev, head);
670         netdev_upper_dev_unlink(ipvlan->phy_dev, dev);
671 }
672 EXPORT_SYMBOL_GPL(ipvlan_link_delete);
673
674 void ipvlan_link_setup(struct net_device *dev)
675 {
676         ether_setup(dev);
677
678         dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
679         dev->priv_flags |= IFF_UNICAST_FLT | IFF_NO_QUEUE;
680         dev->netdev_ops = &ipvlan_netdev_ops;
681         dev->needs_free_netdev = true;
682         dev->header_ops = &ipvlan_header_ops;
683         dev->ethtool_ops = &ipvlan_ethtool_ops;
684 }
685 EXPORT_SYMBOL_GPL(ipvlan_link_setup);
686
687 static const struct nla_policy ipvlan_nl_policy[IFLA_IPVLAN_MAX + 1] =
688 {
689         [IFLA_IPVLAN_MODE] = { .type = NLA_U16 },
690 };
691
692 static struct rtnl_link_ops ipvlan_link_ops = {
693         .kind           = "ipvlan",
694         .priv_size      = sizeof(struct ipvl_dev),
695
696         .setup          = ipvlan_link_setup,
697         .newlink        = ipvlan_link_new,
698         .dellink        = ipvlan_link_delete,
699 };
700
701 int ipvlan_link_register(struct rtnl_link_ops *ops)
702 {
703         ops->get_size   = ipvlan_nl_getsize;
704         ops->policy     = ipvlan_nl_policy;
705         ops->validate   = ipvlan_nl_validate;
706         ops->fill_info  = ipvlan_nl_fillinfo;
707         ops->changelink = ipvlan_nl_changelink;
708         ops->maxtype    = IFLA_IPVLAN_MAX;
709         return rtnl_link_register(ops);
710 }
711 EXPORT_SYMBOL_GPL(ipvlan_link_register);
712
713 static int ipvlan_device_event(struct notifier_block *unused,
714                                unsigned long event, void *ptr)
715 {
716         struct net_device *dev = netdev_notifier_info_to_dev(ptr);
717         struct ipvl_dev *ipvlan, *next;
718         struct ipvl_port *port;
719         LIST_HEAD(lst_kill);
720
721         if (!netif_is_ipvlan_port(dev))
722                 return NOTIFY_DONE;
723
724         port = ipvlan_port_get_rtnl(dev);
725
726         switch (event) {
727         case NETDEV_CHANGE:
728                 list_for_each_entry(ipvlan, &port->ipvlans, pnode)
729                         netif_stacked_transfer_operstate(ipvlan->phy_dev,
730                                                          ipvlan->dev);
731                 break;
732
733         case NETDEV_REGISTER: {
734                 struct net *oldnet, *newnet = dev_net(dev);
735                 struct ipvlan_netns *old_vnet;
736
737                 oldnet = read_pnet(&port->pnet);
738                 if (net_eq(newnet, oldnet))
739                         break;
740
741                 write_pnet(&port->pnet, newnet);
742
743                 old_vnet = net_generic(oldnet, ipvlan_netid);
744                 if (!old_vnet->ipvl_nf_hook_refcnt)
745                         break;
746
747                 ipvlan_register_nf_hook(newnet);
748                 ipvlan_unregister_nf_hook(oldnet);
749                 break;
750         }
751         case NETDEV_UNREGISTER:
752                 if (dev->reg_state != NETREG_UNREGISTERING)
753                         break;
754
755                 list_for_each_entry_safe(ipvlan, next, &port->ipvlans,
756                                          pnode)
757                         ipvlan->dev->rtnl_link_ops->dellink(ipvlan->dev,
758                                                             &lst_kill);
759                 unregister_netdevice_many(&lst_kill);
760                 break;
761
762         case NETDEV_FEAT_CHANGE:
763                 list_for_each_entry(ipvlan, &port->ipvlans, pnode) {
764                         ipvlan->dev->gso_max_size = dev->gso_max_size;
765                         ipvlan->dev->gso_max_segs = dev->gso_max_segs;
766                         netdev_update_features(ipvlan->dev);
767                 }
768                 break;
769
770         case NETDEV_CHANGEMTU:
771                 list_for_each_entry(ipvlan, &port->ipvlans, pnode)
772                         ipvlan_adjust_mtu(ipvlan, dev);
773                 break;
774
775         case NETDEV_PRE_TYPE_CHANGE:
776                 /* Forbid underlying device to change its type. */
777                 return NOTIFY_BAD;
778         }
779         return NOTIFY_DONE;
780 }
781
782 static int ipvlan_add_addr(struct ipvl_dev *ipvlan, void *iaddr, bool is_v6)
783 {
784         struct ipvl_addr *addr;
785
786         addr = kzalloc(sizeof(struct ipvl_addr), GFP_ATOMIC);
787         if (!addr)
788                 return -ENOMEM;
789
790         addr->master = ipvlan;
791         if (is_v6) {
792                 memcpy(&addr->ip6addr, iaddr, sizeof(struct in6_addr));
793                 addr->atype = IPVL_IPV6;
794         } else {
795                 memcpy(&addr->ip4addr, iaddr, sizeof(struct in_addr));
796                 addr->atype = IPVL_IPV4;
797         }
798         list_add_tail(&addr->anode, &ipvlan->addrs);
799
800         /* If the interface is not up, the address will be added to the hash
801          * list by ipvlan_open.
802          */
803         if (netif_running(ipvlan->dev))
804                 ipvlan_ht_addr_add(ipvlan, addr);
805
806         return 0;
807 }
808
809 static void ipvlan_del_addr(struct ipvl_dev *ipvlan, void *iaddr, bool is_v6)
810 {
811         struct ipvl_addr *addr;
812
813         addr = ipvlan_find_addr(ipvlan, iaddr, is_v6);
814         if (!addr)
815                 return;
816
817         ipvlan_ht_addr_del(addr);
818         list_del(&addr->anode);
819         kfree_rcu(addr, rcu);
820
821         return;
822 }
823
824 static int ipvlan_add_addr6(struct ipvl_dev *ipvlan, struct in6_addr *ip6_addr)
825 {
826         if (ipvlan_addr_busy(ipvlan->port, ip6_addr, true)) {
827                 netif_err(ipvlan, ifup, ipvlan->dev,
828                           "Failed to add IPv6=%pI6c addr for %s intf\n",
829                           ip6_addr, ipvlan->dev->name);
830                 return -EINVAL;
831         }
832
833         return ipvlan_add_addr(ipvlan, ip6_addr, true);
834 }
835
836 static void ipvlan_del_addr6(struct ipvl_dev *ipvlan, struct in6_addr *ip6_addr)
837 {
838         return ipvlan_del_addr(ipvlan, ip6_addr, true);
839 }
840
841 static int ipvlan_addr6_event(struct notifier_block *unused,
842                               unsigned long event, void *ptr)
843 {
844         struct inet6_ifaddr *if6 = (struct inet6_ifaddr *)ptr;
845         struct net_device *dev = (struct net_device *)if6->idev->dev;
846         struct ipvl_dev *ipvlan = netdev_priv(dev);
847
848         /* FIXME IPv6 autoconf calls us from bh without RTNL */
849         if (in_softirq())
850                 return NOTIFY_DONE;
851
852         if (!netif_is_ipvlan(dev))
853                 return NOTIFY_DONE;
854
855         if (!ipvlan || !ipvlan->port)
856                 return NOTIFY_DONE;
857
858         switch (event) {
859         case NETDEV_UP:
860                 if (ipvlan_add_addr6(ipvlan, &if6->addr))
861                         return NOTIFY_BAD;
862                 break;
863
864         case NETDEV_DOWN:
865                 ipvlan_del_addr6(ipvlan, &if6->addr);
866                 break;
867         }
868
869         return NOTIFY_OK;
870 }
871
872 static int ipvlan_addr6_validator_event(struct notifier_block *unused,
873                                         unsigned long event, void *ptr)
874 {
875         struct in6_validator_info *i6vi = (struct in6_validator_info *)ptr;
876         struct net_device *dev = (struct net_device *)i6vi->i6vi_dev->dev;
877         struct ipvl_dev *ipvlan = netdev_priv(dev);
878
879         /* FIXME IPv6 autoconf calls us from bh without RTNL */
880         if (in_softirq())
881                 return NOTIFY_DONE;
882
883         if (!netif_is_ipvlan(dev))
884                 return NOTIFY_DONE;
885
886         if (!ipvlan || !ipvlan->port)
887                 return NOTIFY_DONE;
888
889         switch (event) {
890         case NETDEV_UP:
891                 if (ipvlan_addr_busy(ipvlan->port, &i6vi->i6vi_addr, true))
892                         return notifier_from_errno(-EADDRINUSE);
893                 break;
894         }
895
896         return NOTIFY_OK;
897 }
898
899 static int ipvlan_add_addr4(struct ipvl_dev *ipvlan, struct in_addr *ip4_addr)
900 {
901         if (ipvlan_addr_busy(ipvlan->port, ip4_addr, false)) {
902                 netif_err(ipvlan, ifup, ipvlan->dev,
903                           "Failed to add IPv4=%pI4 on %s intf.\n",
904                           ip4_addr, ipvlan->dev->name);
905                 return -EINVAL;
906         }
907
908         return ipvlan_add_addr(ipvlan, ip4_addr, false);
909 }
910
911 static void ipvlan_del_addr4(struct ipvl_dev *ipvlan, struct in_addr *ip4_addr)
912 {
913         return ipvlan_del_addr(ipvlan, ip4_addr, false);
914 }
915
916 static int ipvlan_addr4_event(struct notifier_block *unused,
917                               unsigned long event, void *ptr)
918 {
919         struct in_ifaddr *if4 = (struct in_ifaddr *)ptr;
920         struct net_device *dev = (struct net_device *)if4->ifa_dev->dev;
921         struct ipvl_dev *ipvlan = netdev_priv(dev);
922         struct in_addr ip4_addr;
923
924         if (!netif_is_ipvlan(dev))
925                 return NOTIFY_DONE;
926
927         if (!ipvlan || !ipvlan->port)
928                 return NOTIFY_DONE;
929
930         switch (event) {
931         case NETDEV_UP:
932                 ip4_addr.s_addr = if4->ifa_address;
933                 if (ipvlan_add_addr4(ipvlan, &ip4_addr))
934                         return NOTIFY_BAD;
935                 break;
936
937         case NETDEV_DOWN:
938                 ip4_addr.s_addr = if4->ifa_address;
939                 ipvlan_del_addr4(ipvlan, &ip4_addr);
940                 break;
941         }
942
943         return NOTIFY_OK;
944 }
945
946 static int ipvlan_addr4_validator_event(struct notifier_block *unused,
947                                         unsigned long event, void *ptr)
948 {
949         struct in_validator_info *ivi = (struct in_validator_info *)ptr;
950         struct net_device *dev = (struct net_device *)ivi->ivi_dev->dev;
951         struct ipvl_dev *ipvlan = netdev_priv(dev);
952
953         if (!netif_is_ipvlan(dev))
954                 return NOTIFY_DONE;
955
956         if (!ipvlan || !ipvlan->port)
957                 return NOTIFY_DONE;
958
959         switch (event) {
960         case NETDEV_UP:
961                 if (ipvlan_addr_busy(ipvlan->port, &ivi->ivi_addr, false))
962                         return notifier_from_errno(-EADDRINUSE);
963                 break;
964         }
965
966         return NOTIFY_OK;
967 }
968
969 static struct notifier_block ipvlan_addr4_notifier_block __read_mostly = {
970         .notifier_call = ipvlan_addr4_event,
971 };
972
973 static struct notifier_block ipvlan_addr4_vtor_notifier_block __read_mostly = {
974         .notifier_call = ipvlan_addr4_validator_event,
975 };
976
977 static struct notifier_block ipvlan_notifier_block __read_mostly = {
978         .notifier_call = ipvlan_device_event,
979 };
980
981 static struct notifier_block ipvlan_addr6_notifier_block __read_mostly = {
982         .notifier_call = ipvlan_addr6_event,
983 };
984
985 static struct notifier_block ipvlan_addr6_vtor_notifier_block __read_mostly = {
986         .notifier_call = ipvlan_addr6_validator_event,
987 };
988
989 static void ipvlan_ns_exit(struct net *net)
990 {
991         struct ipvlan_netns *vnet = net_generic(net, ipvlan_netid);
992
993         if (WARN_ON_ONCE(vnet->ipvl_nf_hook_refcnt)) {
994                 vnet->ipvl_nf_hook_refcnt = 0;
995                 nf_unregister_net_hooks(net, ipvl_nfops,
996                                         ARRAY_SIZE(ipvl_nfops));
997         }
998 }
999
1000 static struct pernet_operations ipvlan_net_ops = {
1001         .id = &ipvlan_netid,
1002         .size = sizeof(struct ipvlan_netns),
1003         .exit = ipvlan_ns_exit,
1004 };
1005
1006 static int __init ipvlan_init_module(void)
1007 {
1008         int err;
1009
1010         ipvlan_init_secret();
1011         register_netdevice_notifier(&ipvlan_notifier_block);
1012         register_inet6addr_notifier(&ipvlan_addr6_notifier_block);
1013         register_inet6addr_validator_notifier(
1014             &ipvlan_addr6_vtor_notifier_block);
1015         register_inetaddr_notifier(&ipvlan_addr4_notifier_block);
1016         register_inetaddr_validator_notifier(&ipvlan_addr4_vtor_notifier_block);
1017
1018         err = register_pernet_subsys(&ipvlan_net_ops);
1019         if (err < 0)
1020                 goto error;
1021
1022         err = ipvlan_link_register(&ipvlan_link_ops);
1023         if (err < 0) {
1024                 unregister_pernet_subsys(&ipvlan_net_ops);
1025                 goto error;
1026         }
1027
1028         return 0;
1029 error:
1030         unregister_inetaddr_notifier(&ipvlan_addr4_notifier_block);
1031         unregister_inetaddr_validator_notifier(
1032             &ipvlan_addr4_vtor_notifier_block);
1033         unregister_inet6addr_notifier(&ipvlan_addr6_notifier_block);
1034         unregister_inet6addr_validator_notifier(
1035             &ipvlan_addr6_vtor_notifier_block);
1036         unregister_netdevice_notifier(&ipvlan_notifier_block);
1037         return err;
1038 }
1039
1040 static void __exit ipvlan_cleanup_module(void)
1041 {
1042         rtnl_link_unregister(&ipvlan_link_ops);
1043         unregister_pernet_subsys(&ipvlan_net_ops);
1044         unregister_netdevice_notifier(&ipvlan_notifier_block);
1045         unregister_inetaddr_notifier(&ipvlan_addr4_notifier_block);
1046         unregister_inetaddr_validator_notifier(
1047             &ipvlan_addr4_vtor_notifier_block);
1048         unregister_inet6addr_notifier(&ipvlan_addr6_notifier_block);
1049         unregister_inet6addr_validator_notifier(
1050             &ipvlan_addr6_vtor_notifier_block);
1051 }
1052
1053 module_init(ipvlan_init_module);
1054 module_exit(ipvlan_cleanup_module);
1055
1056 MODULE_LICENSE("GPL");
1057 MODULE_AUTHOR("Mahesh Bandewar <maheshb@google.com>");
1058 MODULE_DESCRIPTION("Driver for L3 (IPv6/IPv4) based VLANs");
1059 MODULE_ALIAS_RTNL_LINK("ipvlan");