]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/net/ipvlan/ipvlan_main.c
ipvlan: fix sparse warnings
[karo-tx-linux.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 void ipvlan_adjust_mtu(struct ipvl_dev *ipvlan, struct net_device *dev)
13 {
14         ipvlan->dev->mtu = dev->mtu - ipvlan->mtu_adj;
15 }
16
17 void ipvlan_set_port_mode(struct ipvl_port *port, u32 nval)
18 {
19         struct ipvl_dev *ipvlan;
20
21         if (port->mode != nval) {
22                 list_for_each_entry(ipvlan, &port->ipvlans, pnode) {
23                         if (nval == IPVLAN_MODE_L3)
24                                 ipvlan->dev->flags |= IFF_NOARP;
25                         else
26                                 ipvlan->dev->flags &= ~IFF_NOARP;
27                 }
28                 port->mode = nval;
29         }
30 }
31
32 static int ipvlan_port_create(struct net_device *dev)
33 {
34         struct ipvl_port *port;
35         int err, idx;
36
37         if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK) {
38                 netdev_err(dev, "Master is either lo or non-ether device\n");
39                 return -EINVAL;
40         }
41         port = kzalloc(sizeof(struct ipvl_port), GFP_KERNEL);
42         if (!port)
43                 return -ENOMEM;
44
45         port->dev = dev;
46         port->mode = IPVLAN_MODE_L3;
47         INIT_LIST_HEAD(&port->ipvlans);
48         for (idx = 0; idx < IPVLAN_HASH_SIZE; idx++)
49                 INIT_HLIST_HEAD(&port->hlhead[idx]);
50
51         err = netdev_rx_handler_register(dev, ipvlan_handle_frame, port);
52         if (err)
53                 goto err;
54
55         dev->priv_flags |= IFF_IPVLAN_MASTER;
56         return 0;
57
58 err:
59         kfree_rcu(port, rcu);
60         return err;
61 }
62
63 static void ipvlan_port_destroy(struct net_device *dev)
64 {
65         struct ipvl_port *port = ipvlan_port_get_rtnl(dev);
66
67         dev->priv_flags &= ~IFF_IPVLAN_MASTER;
68         netdev_rx_handler_unregister(dev);
69         kfree_rcu(port, rcu);
70 }
71
72 /* ipvlan network devices have devices nesting below it and are a special
73  * "super class" of normal network devices; split their locks off into a
74  * separate class since they always nest.
75  */
76 static struct lock_class_key ipvlan_netdev_xmit_lock_key;
77 static struct lock_class_key ipvlan_netdev_addr_lock_key;
78
79 #define IPVLAN_FEATURES \
80         (NETIF_F_SG | NETIF_F_ALL_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
81          NETIF_F_GSO | NETIF_F_TSO | NETIF_F_UFO | NETIF_F_GSO_ROBUST | \
82          NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_GRO | NETIF_F_RXCSUM | \
83          NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_STAG_FILTER)
84
85 #define IPVLAN_STATE_MASK \
86         ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
87
88 static void ipvlan_set_lockdep_class_one(struct net_device *dev,
89                                          struct netdev_queue *txq,
90                                          void *_unused)
91 {
92         lockdep_set_class(&txq->_xmit_lock, &ipvlan_netdev_xmit_lock_key);
93 }
94
95 static void ipvlan_set_lockdep_class(struct net_device *dev)
96 {
97         lockdep_set_class(&dev->addr_list_lock, &ipvlan_netdev_addr_lock_key);
98         netdev_for_each_tx_queue(dev, ipvlan_set_lockdep_class_one, NULL);
99 }
100
101 static int ipvlan_init(struct net_device *dev)
102 {
103         struct ipvl_dev *ipvlan = netdev_priv(dev);
104         const struct net_device *phy_dev = ipvlan->phy_dev;
105
106         dev->state = (dev->state & ~IPVLAN_STATE_MASK) |
107                      (phy_dev->state & IPVLAN_STATE_MASK);
108         dev->features = phy_dev->features & IPVLAN_FEATURES;
109         dev->features |= NETIF_F_LLTX;
110         dev->gso_max_size = phy_dev->gso_max_size;
111         dev->iflink = phy_dev->ifindex;
112         dev->hard_header_len = phy_dev->hard_header_len;
113
114         ipvlan_set_lockdep_class(dev);
115
116         ipvlan->pcpu_stats = alloc_percpu(struct ipvl_pcpu_stats);
117         if (!ipvlan->pcpu_stats)
118                 return -ENOMEM;
119
120         return 0;
121 }
122
123 static void ipvlan_uninit(struct net_device *dev)
124 {
125         struct ipvl_dev *ipvlan = netdev_priv(dev);
126         struct ipvl_port *port = ipvlan->port;
127
128         if (ipvlan->pcpu_stats)
129                 free_percpu(ipvlan->pcpu_stats);
130
131         port->count -= 1;
132         if (!port->count)
133                 ipvlan_port_destroy(port->dev);
134 }
135
136 static int ipvlan_open(struct net_device *dev)
137 {
138         struct ipvl_dev *ipvlan = netdev_priv(dev);
139         struct net_device *phy_dev = ipvlan->phy_dev;
140         struct ipvl_addr *addr;
141
142         if (ipvlan->port->mode == IPVLAN_MODE_L3)
143                 dev->flags |= IFF_NOARP;
144         else
145                 dev->flags &= ~IFF_NOARP;
146
147         if (ipvlan->ipv6cnt > 0 || ipvlan->ipv4cnt > 0) {
148                 list_for_each_entry(addr, &ipvlan->addrs, anode)
149                         ipvlan_ht_addr_add(ipvlan, addr);
150         }
151         return dev_uc_add(phy_dev, phy_dev->dev_addr);
152 }
153
154 static int ipvlan_stop(struct net_device *dev)
155 {
156         struct ipvl_dev *ipvlan = netdev_priv(dev);
157         struct net_device *phy_dev = ipvlan->phy_dev;
158         struct ipvl_addr *addr;
159
160         dev_uc_unsync(phy_dev, dev);
161         dev_mc_unsync(phy_dev, dev);
162
163         dev_uc_del(phy_dev, phy_dev->dev_addr);
164
165         if (ipvlan->ipv6cnt > 0 || ipvlan->ipv4cnt > 0) {
166                 list_for_each_entry(addr, &ipvlan->addrs, anode)
167                         ipvlan_ht_addr_del(addr, !dev->dismantle);
168         }
169         return 0;
170 }
171
172 static netdev_tx_t ipvlan_start_xmit(struct sk_buff *skb,
173                                      struct net_device *dev)
174 {
175         const struct ipvl_dev *ipvlan = netdev_priv(dev);
176         int skblen = skb->len;
177         int ret;
178
179         ret = ipvlan_queue_xmit(skb, dev);
180         if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
181                 struct ipvl_pcpu_stats *pcptr;
182
183                 pcptr = this_cpu_ptr(ipvlan->pcpu_stats);
184
185                 u64_stats_update_begin(&pcptr->syncp);
186                 pcptr->tx_pkts++;
187                 pcptr->tx_bytes += skblen;
188                 u64_stats_update_end(&pcptr->syncp);
189         } else {
190                 this_cpu_inc(ipvlan->pcpu_stats->tx_drps);
191         }
192         return ret;
193 }
194
195 static netdev_features_t ipvlan_fix_features(struct net_device *dev,
196                                              netdev_features_t features)
197 {
198         struct ipvl_dev *ipvlan = netdev_priv(dev);
199
200         return features & (ipvlan->sfeatures | ~IPVLAN_FEATURES);
201 }
202
203 static void ipvlan_change_rx_flags(struct net_device *dev, int change)
204 {
205         struct ipvl_dev *ipvlan = netdev_priv(dev);
206         struct net_device *phy_dev = ipvlan->phy_dev;
207
208         if (change & IFF_ALLMULTI)
209                 dev_set_allmulti(phy_dev, dev->flags & IFF_ALLMULTI? 1 : -1);
210 }
211
212 static void ipvlan_set_broadcast_mac_filter(struct ipvl_dev *ipvlan, bool set)
213 {
214         struct net_device *dev = ipvlan->dev;
215         unsigned int hashbit = ipvlan_mac_hash(dev->broadcast);
216
217         if (set && !test_bit(hashbit, ipvlan->mac_filters))
218                 __set_bit(hashbit, ipvlan->mac_filters);
219         else if (!set && test_bit(hashbit, ipvlan->mac_filters))
220                 __clear_bit(hashbit, ipvlan->mac_filters);
221 }
222
223 static void ipvlan_set_multicast_mac_filter(struct net_device *dev)
224 {
225         struct ipvl_dev *ipvlan = netdev_priv(dev);
226
227         if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
228                 bitmap_fill(ipvlan->mac_filters, IPVLAN_MAC_FILTER_SIZE);
229         } else {
230                 struct netdev_hw_addr *ha;
231                 DECLARE_BITMAP(mc_filters, IPVLAN_MAC_FILTER_SIZE);
232
233                 bitmap_zero(mc_filters, IPVLAN_MAC_FILTER_SIZE);
234                 netdev_for_each_mc_addr(ha, dev)
235                         __set_bit(ipvlan_mac_hash(ha->addr), mc_filters);
236
237                 bitmap_copy(ipvlan->mac_filters, mc_filters,
238                             IPVLAN_MAC_FILTER_SIZE);
239         }
240         dev_uc_sync(ipvlan->phy_dev, dev);
241         dev_mc_sync(ipvlan->phy_dev, dev);
242 }
243
244 static struct rtnl_link_stats64 *ipvlan_get_stats64(struct net_device *dev,
245                                                     struct rtnl_link_stats64 *s)
246 {
247         struct ipvl_dev *ipvlan = netdev_priv(dev);
248
249         if (ipvlan->pcpu_stats) {
250                 struct ipvl_pcpu_stats *pcptr;
251                 u64 rx_pkts, rx_bytes, rx_mcast, tx_pkts, tx_bytes;
252                 u32 rx_errs = 0, tx_drps = 0;
253                 u32 strt;
254                 int idx;
255
256                 for_each_possible_cpu(idx) {
257                         pcptr = per_cpu_ptr(ipvlan->pcpu_stats, idx);
258                         do {
259                                 strt= u64_stats_fetch_begin_irq(&pcptr->syncp);
260                                 rx_pkts = pcptr->rx_pkts;
261                                 rx_bytes = pcptr->rx_bytes;
262                                 rx_mcast = pcptr->rx_mcast;
263                                 tx_pkts = pcptr->tx_pkts;
264                                 tx_bytes = pcptr->tx_bytes;
265                         } while (u64_stats_fetch_retry_irq(&pcptr->syncp,
266                                                            strt));
267
268                         s->rx_packets += rx_pkts;
269                         s->rx_bytes += rx_bytes;
270                         s->multicast += rx_mcast;
271                         s->tx_packets += tx_pkts;
272                         s->tx_bytes += tx_bytes;
273
274                         /* u32 values are updated without syncp protection. */
275                         rx_errs += pcptr->rx_errs;
276                         tx_drps += pcptr->tx_drps;
277                 }
278                 s->rx_errors = rx_errs;
279                 s->rx_dropped = rx_errs;
280                 s->tx_dropped = tx_drps;
281         }
282         return s;
283 }
284
285 static int ipvlan_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
286 {
287         struct ipvl_dev *ipvlan = netdev_priv(dev);
288         struct net_device *phy_dev = ipvlan->phy_dev;
289
290         return vlan_vid_add(phy_dev, proto, vid);
291 }
292
293 static int ipvlan_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
294                                    u16 vid)
295 {
296         struct ipvl_dev *ipvlan = netdev_priv(dev);
297         struct net_device *phy_dev = ipvlan->phy_dev;
298
299         vlan_vid_del(phy_dev, proto, vid);
300         return 0;
301 }
302
303 static const struct net_device_ops ipvlan_netdev_ops = {
304         .ndo_init               = ipvlan_init,
305         .ndo_uninit             = ipvlan_uninit,
306         .ndo_open               = ipvlan_open,
307         .ndo_stop               = ipvlan_stop,
308         .ndo_start_xmit         = ipvlan_start_xmit,
309         .ndo_fix_features       = ipvlan_fix_features,
310         .ndo_change_rx_flags    = ipvlan_change_rx_flags,
311         .ndo_set_rx_mode        = ipvlan_set_multicast_mac_filter,
312         .ndo_get_stats64        = ipvlan_get_stats64,
313         .ndo_vlan_rx_add_vid    = ipvlan_vlan_rx_add_vid,
314         .ndo_vlan_rx_kill_vid   = ipvlan_vlan_rx_kill_vid,
315 };
316
317 static int ipvlan_hard_header(struct sk_buff *skb, struct net_device *dev,
318                               unsigned short type, const void *daddr,
319                               const void *saddr, unsigned len)
320 {
321         const struct ipvl_dev *ipvlan = netdev_priv(dev);
322         struct net_device *phy_dev = ipvlan->phy_dev;
323
324         /* TODO Probably use a different field than dev_addr so that the
325          * mac-address on the virtual device is portable and can be carried
326          * while the packets use the mac-addr on the physical device.
327          */
328         return dev_hard_header(skb, phy_dev, type, daddr,
329                                saddr ? : dev->dev_addr, len);
330 }
331
332 static const struct header_ops ipvlan_header_ops = {
333         .create         = ipvlan_hard_header,
334         .rebuild        = eth_rebuild_header,
335         .parse          = eth_header_parse,
336         .cache          = eth_header_cache,
337         .cache_update   = eth_header_cache_update,
338 };
339
340 static int ipvlan_ethtool_get_settings(struct net_device *dev,
341                                        struct ethtool_cmd *cmd)
342 {
343         const struct ipvl_dev *ipvlan = netdev_priv(dev);
344
345         return __ethtool_get_settings(ipvlan->phy_dev, cmd);
346 }
347
348 static void ipvlan_ethtool_get_drvinfo(struct net_device *dev,
349                                        struct ethtool_drvinfo *drvinfo)
350 {
351         strlcpy(drvinfo->driver, IPVLAN_DRV, sizeof(drvinfo->driver));
352         strlcpy(drvinfo->version, IPV_DRV_VER, sizeof(drvinfo->version));
353 }
354
355 static u32 ipvlan_ethtool_get_msglevel(struct net_device *dev)
356 {
357         const struct ipvl_dev *ipvlan = netdev_priv(dev);
358
359         return ipvlan->msg_enable;
360 }
361
362 static void ipvlan_ethtool_set_msglevel(struct net_device *dev, u32 value)
363 {
364         struct ipvl_dev *ipvlan = netdev_priv(dev);
365
366         ipvlan->msg_enable = value;
367 }
368
369 static const struct ethtool_ops ipvlan_ethtool_ops = {
370         .get_link       = ethtool_op_get_link,
371         .get_settings   = ipvlan_ethtool_get_settings,
372         .get_drvinfo    = ipvlan_ethtool_get_drvinfo,
373         .get_msglevel   = ipvlan_ethtool_get_msglevel,
374         .set_msglevel   = ipvlan_ethtool_set_msglevel,
375 };
376
377 static int ipvlan_nl_changelink(struct net_device *dev,
378                                 struct nlattr *tb[], struct nlattr *data[])
379 {
380         struct ipvl_dev *ipvlan = netdev_priv(dev);
381         struct ipvl_port *port = ipvlan_port_get_rtnl(ipvlan->phy_dev);
382
383         if (data && data[IFLA_IPVLAN_MODE]) {
384                 u16 nmode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
385
386                 ipvlan_set_port_mode(port, nmode);
387         }
388         return 0;
389 }
390
391 static size_t ipvlan_nl_getsize(const struct net_device *dev)
392 {
393         return (0
394                 + nla_total_size(2) /* IFLA_IPVLAN_MODE */
395                 );
396 }
397
398 static int ipvlan_nl_validate(struct nlattr *tb[], struct nlattr *data[])
399 {
400         if (data && data[IFLA_IPVLAN_MODE]) {
401                 u16 mode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
402
403                 if (mode < IPVLAN_MODE_L2 || mode >= IPVLAN_MODE_MAX)
404                         return -EINVAL;
405         }
406         return 0;
407 }
408
409 static int ipvlan_nl_fillinfo(struct sk_buff *skb,
410                               const struct net_device *dev)
411 {
412         struct ipvl_dev *ipvlan = netdev_priv(dev);
413         struct ipvl_port *port = ipvlan_port_get_rtnl(ipvlan->phy_dev);
414         int ret = -EINVAL;
415
416         if (!port)
417                 goto err;
418
419         ret = -EMSGSIZE;
420         if (nla_put_u16(skb, IFLA_IPVLAN_MODE, port->mode))
421                 goto err;
422
423         return 0;
424
425 err:
426         return ret;
427 }
428
429 static int ipvlan_link_new(struct net *src_net, struct net_device *dev,
430                            struct nlattr *tb[], struct nlattr *data[])
431 {
432         struct ipvl_dev *ipvlan = netdev_priv(dev);
433         struct ipvl_port *port;
434         struct net_device *phy_dev;
435         int err;
436
437         if (!tb[IFLA_LINK])
438                 return -EINVAL;
439
440         phy_dev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
441         if (!phy_dev)
442                 return -ENODEV;
443
444         if (ipvlan_dev_slave(phy_dev)) {
445                 struct ipvl_dev *tmp = netdev_priv(phy_dev);
446
447                 phy_dev = tmp->phy_dev;
448         } else if (!ipvlan_dev_master(phy_dev)) {
449                 err = ipvlan_port_create(phy_dev);
450                 if (err < 0)
451                         return err;
452         }
453
454         port = ipvlan_port_get_rtnl(phy_dev);
455         if (data && data[IFLA_IPVLAN_MODE])
456                 port->mode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
457
458         ipvlan->phy_dev = phy_dev;
459         ipvlan->dev = dev;
460         ipvlan->port = port;
461         ipvlan->sfeatures = IPVLAN_FEATURES;
462         INIT_LIST_HEAD(&ipvlan->addrs);
463         ipvlan->ipv4cnt = 0;
464         ipvlan->ipv6cnt = 0;
465
466         /* TODO Probably put random address here to be presented to the
467          * world but keep using the physical-dev address for the outgoing
468          * packets.
469          */
470         memcpy(dev->dev_addr, phy_dev->dev_addr, ETH_ALEN);
471
472         dev->priv_flags |= IFF_IPVLAN_SLAVE;
473
474         port->count += 1;
475         err = register_netdevice(dev);
476         if (err < 0)
477                 goto ipvlan_destroy_port;
478
479         err = netdev_upper_dev_link(phy_dev, dev);
480         if (err)
481                 goto ipvlan_destroy_port;
482
483         list_add_tail_rcu(&ipvlan->pnode, &port->ipvlans);
484         netif_stacked_transfer_operstate(phy_dev, dev);
485         return 0;
486
487 ipvlan_destroy_port:
488         port->count -= 1;
489         if (!port->count)
490                 ipvlan_port_destroy(phy_dev);
491
492         return err;
493 }
494
495 static void ipvlan_link_delete(struct net_device *dev, struct list_head *head)
496 {
497         struct ipvl_dev *ipvlan = netdev_priv(dev);
498         struct ipvl_addr *addr, *next;
499
500         if (ipvlan->ipv6cnt > 0 || ipvlan->ipv4cnt > 0) {
501                 list_for_each_entry_safe(addr, next, &ipvlan->addrs, anode) {
502                         ipvlan_ht_addr_del(addr, !dev->dismantle);
503                         list_del_rcu(&addr->anode);
504                 }
505         }
506         list_del_rcu(&ipvlan->pnode);
507         unregister_netdevice_queue(dev, head);
508         netdev_upper_dev_unlink(ipvlan->phy_dev, dev);
509 }
510
511 static void ipvlan_link_setup(struct net_device *dev)
512 {
513         ether_setup(dev);
514
515         dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
516         dev->priv_flags |= IFF_UNICAST_FLT;
517         dev->netdev_ops = &ipvlan_netdev_ops;
518         dev->destructor = free_netdev;
519         dev->header_ops = &ipvlan_header_ops;
520         dev->ethtool_ops = &ipvlan_ethtool_ops;
521         dev->tx_queue_len = 0;
522 }
523
524 static const struct nla_policy ipvlan_nl_policy[IFLA_IPVLAN_MAX + 1] =
525 {
526         [IFLA_IPVLAN_MODE] = { .type = NLA_U16 },
527 };
528
529 static struct rtnl_link_ops ipvlan_link_ops = {
530         .kind           = "ipvlan",
531         .priv_size      = sizeof(struct ipvl_dev),
532
533         .get_size       = ipvlan_nl_getsize,
534         .policy         = ipvlan_nl_policy,
535         .validate       = ipvlan_nl_validate,
536         .fill_info      = ipvlan_nl_fillinfo,
537         .changelink     = ipvlan_nl_changelink,
538         .maxtype        = IFLA_IPVLAN_MAX,
539
540         .setup          = ipvlan_link_setup,
541         .newlink        = ipvlan_link_new,
542         .dellink        = ipvlan_link_delete,
543 };
544
545 static int ipvlan_link_register(struct rtnl_link_ops *ops)
546 {
547         return rtnl_link_register(ops);
548 }
549
550 static int ipvlan_device_event(struct notifier_block *unused,
551                                unsigned long event, void *ptr)
552 {
553         struct net_device *dev = netdev_notifier_info_to_dev(ptr);
554         struct ipvl_dev *ipvlan, *next;
555         struct ipvl_port *port;
556         LIST_HEAD(lst_kill);
557
558         if (!ipvlan_dev_master(dev))
559                 return NOTIFY_DONE;
560
561         port = ipvlan_port_get_rtnl(dev);
562
563         switch (event) {
564         case NETDEV_CHANGE:
565                 list_for_each_entry(ipvlan, &port->ipvlans, pnode)
566                         netif_stacked_transfer_operstate(ipvlan->phy_dev,
567                                                          ipvlan->dev);
568                 break;
569
570         case NETDEV_UNREGISTER:
571                 if (dev->reg_state != NETREG_UNREGISTERING)
572                         break;
573
574                 list_for_each_entry_safe(ipvlan, next, &port->ipvlans,
575                                          pnode)
576                         ipvlan->dev->rtnl_link_ops->dellink(ipvlan->dev,
577                                                             &lst_kill);
578                 unregister_netdevice_many(&lst_kill);
579                 break;
580
581         case NETDEV_FEAT_CHANGE:
582                 list_for_each_entry(ipvlan, &port->ipvlans, pnode) {
583                         ipvlan->dev->features = dev->features & IPVLAN_FEATURES;
584                         ipvlan->dev->gso_max_size = dev->gso_max_size;
585                         netdev_features_change(ipvlan->dev);
586                 }
587                 break;
588
589         case NETDEV_CHANGEMTU:
590                 list_for_each_entry(ipvlan, &port->ipvlans, pnode)
591                         ipvlan_adjust_mtu(ipvlan, dev);
592                 break;
593
594         case NETDEV_PRE_TYPE_CHANGE:
595                 /* Forbid underlying device to change its type. */
596                 return NOTIFY_BAD;
597         }
598         return NOTIFY_DONE;
599 }
600
601 static int ipvlan_add_addr6(struct ipvl_dev *ipvlan, struct in6_addr *ip6_addr)
602 {
603         struct ipvl_addr *addr;
604
605         if (ipvlan_addr_busy(ipvlan, ip6_addr, true)) {
606                 netif_err(ipvlan, ifup, ipvlan->dev,
607                           "Failed to add IPv6=%pI6c addr for %s intf\n",
608                           ip6_addr, ipvlan->dev->name);
609                 return -EINVAL;
610         }
611         addr = kzalloc(sizeof(struct ipvl_addr), GFP_ATOMIC);
612         if (!addr)
613                 return -ENOMEM;
614
615         addr->master = ipvlan;
616         memcpy(&addr->ip6addr, ip6_addr, sizeof(struct in6_addr));
617         addr->atype = IPVL_IPV6;
618         list_add_tail_rcu(&addr->anode, &ipvlan->addrs);
619         ipvlan->ipv6cnt++;
620         ipvlan_ht_addr_add(ipvlan, addr);
621
622         return 0;
623 }
624
625 static void ipvlan_del_addr6(struct ipvl_dev *ipvlan, struct in6_addr *ip6_addr)
626 {
627         struct ipvl_addr *addr;
628
629         addr = ipvlan_ht_addr_lookup(ipvlan->port, ip6_addr, true);
630         if (!addr)
631                 return;
632
633         ipvlan_ht_addr_del(addr, true);
634         list_del_rcu(&addr->anode);
635         ipvlan->ipv6cnt--;
636         WARN_ON(ipvlan->ipv6cnt < 0);
637         kfree_rcu(addr, rcu);
638
639         return;
640 }
641
642 static int ipvlan_addr6_event(struct notifier_block *unused,
643                               unsigned long event, void *ptr)
644 {
645         struct inet6_ifaddr *if6 = (struct inet6_ifaddr *)ptr;
646         struct net_device *dev = (struct net_device *)if6->idev->dev;
647         struct ipvl_dev *ipvlan = netdev_priv(dev);
648
649         if (!ipvlan_dev_slave(dev))
650                 return NOTIFY_DONE;
651
652         if (!ipvlan || !ipvlan->port)
653                 return NOTIFY_DONE;
654
655         switch (event) {
656         case NETDEV_UP:
657                 if (ipvlan_add_addr6(ipvlan, &if6->addr))
658                         return NOTIFY_BAD;
659                 break;
660
661         case NETDEV_DOWN:
662                 ipvlan_del_addr6(ipvlan, &if6->addr);
663                 break;
664         }
665
666         return NOTIFY_OK;
667 }
668
669 static int ipvlan_add_addr4(struct ipvl_dev *ipvlan, struct in_addr *ip4_addr)
670 {
671         struct ipvl_addr *addr;
672
673         if (ipvlan_addr_busy(ipvlan, ip4_addr, false)) {
674                 netif_err(ipvlan, ifup, ipvlan->dev,
675                           "Failed to add IPv4=%pI4 on %s intf.\n",
676                           ip4_addr, ipvlan->dev->name);
677                 return -EINVAL;
678         }
679         addr = kzalloc(sizeof(struct ipvl_addr), GFP_KERNEL);
680         if (!addr)
681                 return -ENOMEM;
682
683         addr->master = ipvlan;
684         memcpy(&addr->ip4addr, ip4_addr, sizeof(struct in_addr));
685         addr->atype = IPVL_IPV4;
686         list_add_tail_rcu(&addr->anode, &ipvlan->addrs);
687         ipvlan->ipv4cnt++;
688         ipvlan_ht_addr_add(ipvlan, addr);
689         ipvlan_set_broadcast_mac_filter(ipvlan, true);
690
691         return 0;
692 }
693
694 static void ipvlan_del_addr4(struct ipvl_dev *ipvlan, struct in_addr *ip4_addr)
695 {
696         struct ipvl_addr *addr;
697
698         addr = ipvlan_ht_addr_lookup(ipvlan->port, ip4_addr, false);
699         if (!addr)
700                 return;
701
702         ipvlan_ht_addr_del(addr, true);
703         list_del_rcu(&addr->anode);
704         ipvlan->ipv4cnt--;
705         WARN_ON(ipvlan->ipv4cnt < 0);
706         if (!ipvlan->ipv4cnt)
707             ipvlan_set_broadcast_mac_filter(ipvlan, false);
708         kfree_rcu(addr, rcu);
709
710         return;
711 }
712
713 static int ipvlan_addr4_event(struct notifier_block *unused,
714                               unsigned long event, void *ptr)
715 {
716         struct in_ifaddr *if4 = (struct in_ifaddr *)ptr;
717         struct net_device *dev = (struct net_device *)if4->ifa_dev->dev;
718         struct ipvl_dev *ipvlan = netdev_priv(dev);
719         struct in_addr ip4_addr;
720
721         if (!ipvlan_dev_slave(dev))
722                 return NOTIFY_DONE;
723
724         if (!ipvlan || !ipvlan->port)
725                 return NOTIFY_DONE;
726
727         switch (event) {
728         case NETDEV_UP:
729                 ip4_addr.s_addr = if4->ifa_address;
730                 if (ipvlan_add_addr4(ipvlan, &ip4_addr))
731                         return NOTIFY_BAD;
732                 break;
733
734         case NETDEV_DOWN:
735                 ip4_addr.s_addr = if4->ifa_address;
736                 ipvlan_del_addr4(ipvlan, &ip4_addr);
737                 break;
738         }
739
740         return NOTIFY_OK;
741 }
742
743 static struct notifier_block ipvlan_addr4_notifier_block __read_mostly = {
744         .notifier_call = ipvlan_addr4_event,
745 };
746
747 static struct notifier_block ipvlan_notifier_block __read_mostly = {
748         .notifier_call = ipvlan_device_event,
749 };
750
751 static struct notifier_block ipvlan_addr6_notifier_block __read_mostly = {
752         .notifier_call = ipvlan_addr6_event,
753 };
754
755 static int __init ipvlan_init_module(void)
756 {
757         int err;
758
759         ipvlan_init_secret();
760         register_netdevice_notifier(&ipvlan_notifier_block);
761         register_inet6addr_notifier(&ipvlan_addr6_notifier_block);
762         register_inetaddr_notifier(&ipvlan_addr4_notifier_block);
763
764         err = ipvlan_link_register(&ipvlan_link_ops);
765         if (err < 0)
766                 goto error;
767
768         return 0;
769 error:
770         unregister_inetaddr_notifier(&ipvlan_addr4_notifier_block);
771         unregister_inet6addr_notifier(&ipvlan_addr6_notifier_block);
772         unregister_netdevice_notifier(&ipvlan_notifier_block);
773         return err;
774 }
775
776 static void __exit ipvlan_cleanup_module(void)
777 {
778         rtnl_link_unregister(&ipvlan_link_ops);
779         unregister_netdevice_notifier(&ipvlan_notifier_block);
780         unregister_inetaddr_notifier(&ipvlan_addr4_notifier_block);
781         unregister_inet6addr_notifier(&ipvlan_addr6_notifier_block);
782 }
783
784 module_init(ipvlan_init_module);
785 module_exit(ipvlan_cleanup_module);
786
787 MODULE_LICENSE("GPL");
788 MODULE_AUTHOR("Mahesh Bandewar <maheshb@google.com>");
789 MODULE_DESCRIPTION("Driver for L3 (IPv6/IPv4) based VLANs");
790 MODULE_ALIAS_RTNL_LINK("ipvlan");