reset: zynq: add driver Kconfig option
[linux/fpc-iii.git] / drivers / net / ipvlan / ipvlan_main.c
blob18b4e8c7f68aa1824838f583f6d03fd19705f8a1
1 /* Copyright (c) 2014 Mahesh Bandewar <maheshb@google.com>
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public License as
5 * published by the Free Software Foundation; either version 2 of
6 * the License, or (at your option) any later version.
8 */
10 #include "ipvlan.h"
12 static void ipvlan_adjust_mtu(struct ipvl_dev *ipvlan, struct net_device *dev)
14 ipvlan->dev->mtu = dev->mtu - ipvlan->mtu_adj;
17 static void ipvlan_set_port_mode(struct ipvl_port *port, u16 nval)
19 struct ipvl_dev *ipvlan;
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;
28 port->mode = nval;
32 static int ipvlan_port_create(struct net_device *dev)
34 struct ipvl_port *port;
35 int err, idx;
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;
42 if (netif_is_macvlan_port(dev)) {
43 netdev_err(dev, "Master is a macvlan port.\n");
44 return -EBUSY;
47 port = kzalloc(sizeof(struct ipvl_port), GFP_KERNEL);
48 if (!port)
49 return -ENOMEM;
51 port->dev = dev;
52 port->mode = IPVLAN_MODE_L3;
53 INIT_LIST_HEAD(&port->ipvlans);
54 for (idx = 0; idx < IPVLAN_HASH_SIZE; idx++)
55 INIT_HLIST_HEAD(&port->hlhead[idx]);
57 skb_queue_head_init(&port->backlog);
58 INIT_WORK(&port->wq, ipvlan_process_multicast);
60 err = netdev_rx_handler_register(dev, ipvlan_handle_frame, port);
61 if (err)
62 goto err;
64 dev->priv_flags |= IFF_IPVLAN_MASTER;
65 return 0;
67 err:
68 kfree_rcu(port, rcu);
69 return err;
72 static void ipvlan_port_destroy(struct net_device *dev)
74 struct ipvl_port *port = ipvlan_port_get_rtnl(dev);
76 dev->priv_flags &= ~IFF_IPVLAN_MASTER;
77 netdev_rx_handler_unregister(dev);
78 cancel_work_sync(&port->wq);
79 __skb_queue_purge(&port->backlog);
80 kfree_rcu(port, rcu);
83 #define IPVLAN_FEATURES \
84 (NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
85 NETIF_F_GSO | NETIF_F_TSO | NETIF_F_UFO | NETIF_F_GSO_ROBUST | \
86 NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_GRO | NETIF_F_RXCSUM | \
87 NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_STAG_FILTER)
89 #define IPVLAN_STATE_MASK \
90 ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
92 static int ipvlan_init(struct net_device *dev)
94 struct ipvl_dev *ipvlan = netdev_priv(dev);
95 const struct net_device *phy_dev = ipvlan->phy_dev;
96 struct ipvl_port *port = ipvlan->port;
98 dev->state = (dev->state & ~IPVLAN_STATE_MASK) |
99 (phy_dev->state & IPVLAN_STATE_MASK);
100 dev->features = phy_dev->features & IPVLAN_FEATURES;
101 dev->features |= NETIF_F_LLTX;
102 dev->gso_max_size = phy_dev->gso_max_size;
103 dev->gso_max_segs = phy_dev->gso_max_segs;
104 dev->hard_header_len = phy_dev->hard_header_len;
106 netdev_lockdep_set_classes(dev);
108 ipvlan->pcpu_stats = alloc_percpu(struct ipvl_pcpu_stats);
109 if (!ipvlan->pcpu_stats)
110 return -ENOMEM;
112 port->count += 1;
114 return 0;
117 static void ipvlan_uninit(struct net_device *dev)
119 struct ipvl_dev *ipvlan = netdev_priv(dev);
120 struct ipvl_port *port = ipvlan->port;
122 free_percpu(ipvlan->pcpu_stats);
124 port->count -= 1;
125 if (!port->count)
126 ipvlan_port_destroy(port->dev);
129 static int ipvlan_open(struct net_device *dev)
131 struct ipvl_dev *ipvlan = netdev_priv(dev);
132 struct net_device *phy_dev = ipvlan->phy_dev;
133 struct ipvl_addr *addr;
135 if (ipvlan->port->mode == IPVLAN_MODE_L3)
136 dev->flags |= IFF_NOARP;
137 else
138 dev->flags &= ~IFF_NOARP;
140 list_for_each_entry(addr, &ipvlan->addrs, anode)
141 ipvlan_ht_addr_add(ipvlan, addr);
143 return dev_uc_add(phy_dev, phy_dev->dev_addr);
146 static int ipvlan_stop(struct net_device *dev)
148 struct ipvl_dev *ipvlan = netdev_priv(dev);
149 struct net_device *phy_dev = ipvlan->phy_dev;
150 struct ipvl_addr *addr;
152 dev_uc_unsync(phy_dev, dev);
153 dev_mc_unsync(phy_dev, dev);
155 dev_uc_del(phy_dev, phy_dev->dev_addr);
157 list_for_each_entry(addr, &ipvlan->addrs, anode)
158 ipvlan_ht_addr_del(addr);
160 return 0;
163 static netdev_tx_t ipvlan_start_xmit(struct sk_buff *skb,
164 struct net_device *dev)
166 const struct ipvl_dev *ipvlan = netdev_priv(dev);
167 int skblen = skb->len;
168 int ret;
170 ret = ipvlan_queue_xmit(skb, dev);
171 if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
172 struct ipvl_pcpu_stats *pcptr;
174 pcptr = this_cpu_ptr(ipvlan->pcpu_stats);
176 u64_stats_update_begin(&pcptr->syncp);
177 pcptr->tx_pkts++;
178 pcptr->tx_bytes += skblen;
179 u64_stats_update_end(&pcptr->syncp);
180 } else {
181 this_cpu_inc(ipvlan->pcpu_stats->tx_drps);
183 return ret;
186 static netdev_features_t ipvlan_fix_features(struct net_device *dev,
187 netdev_features_t features)
189 struct ipvl_dev *ipvlan = netdev_priv(dev);
191 return features & (ipvlan->sfeatures | ~IPVLAN_FEATURES);
194 static void ipvlan_change_rx_flags(struct net_device *dev, int change)
196 struct ipvl_dev *ipvlan = netdev_priv(dev);
197 struct net_device *phy_dev = ipvlan->phy_dev;
199 if (change & IFF_ALLMULTI)
200 dev_set_allmulti(phy_dev, dev->flags & IFF_ALLMULTI? 1 : -1);
203 static void ipvlan_set_multicast_mac_filter(struct net_device *dev)
205 struct ipvl_dev *ipvlan = netdev_priv(dev);
207 if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
208 bitmap_fill(ipvlan->mac_filters, IPVLAN_MAC_FILTER_SIZE);
209 } else {
210 struct netdev_hw_addr *ha;
211 DECLARE_BITMAP(mc_filters, IPVLAN_MAC_FILTER_SIZE);
213 bitmap_zero(mc_filters, IPVLAN_MAC_FILTER_SIZE);
214 netdev_for_each_mc_addr(ha, dev)
215 __set_bit(ipvlan_mac_hash(ha->addr), mc_filters);
217 /* Turn-on broadcast bit irrespective of address family,
218 * since broadcast is deferred to a work-queue, hence no
219 * impact on fast-path processing.
221 __set_bit(ipvlan_mac_hash(dev->broadcast), mc_filters);
223 bitmap_copy(ipvlan->mac_filters, mc_filters,
224 IPVLAN_MAC_FILTER_SIZE);
226 dev_uc_sync(ipvlan->phy_dev, dev);
227 dev_mc_sync(ipvlan->phy_dev, dev);
230 static struct rtnl_link_stats64 *ipvlan_get_stats64(struct net_device *dev,
231 struct rtnl_link_stats64 *s)
233 struct ipvl_dev *ipvlan = netdev_priv(dev);
235 if (ipvlan->pcpu_stats) {
236 struct ipvl_pcpu_stats *pcptr;
237 u64 rx_pkts, rx_bytes, rx_mcast, tx_pkts, tx_bytes;
238 u32 rx_errs = 0, tx_drps = 0;
239 u32 strt;
240 int idx;
242 for_each_possible_cpu(idx) {
243 pcptr = per_cpu_ptr(ipvlan->pcpu_stats, idx);
244 do {
245 strt= u64_stats_fetch_begin_irq(&pcptr->syncp);
246 rx_pkts = pcptr->rx_pkts;
247 rx_bytes = pcptr->rx_bytes;
248 rx_mcast = pcptr->rx_mcast;
249 tx_pkts = pcptr->tx_pkts;
250 tx_bytes = pcptr->tx_bytes;
251 } while (u64_stats_fetch_retry_irq(&pcptr->syncp,
252 strt));
254 s->rx_packets += rx_pkts;
255 s->rx_bytes += rx_bytes;
256 s->multicast += rx_mcast;
257 s->tx_packets += tx_pkts;
258 s->tx_bytes += tx_bytes;
260 /* u32 values are updated without syncp protection. */
261 rx_errs += pcptr->rx_errs;
262 tx_drps += pcptr->tx_drps;
264 s->rx_errors = rx_errs;
265 s->rx_dropped = rx_errs;
266 s->tx_dropped = tx_drps;
268 return s;
271 static int ipvlan_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
273 struct ipvl_dev *ipvlan = netdev_priv(dev);
274 struct net_device *phy_dev = ipvlan->phy_dev;
276 return vlan_vid_add(phy_dev, proto, vid);
279 static int ipvlan_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
280 u16 vid)
282 struct ipvl_dev *ipvlan = netdev_priv(dev);
283 struct net_device *phy_dev = ipvlan->phy_dev;
285 vlan_vid_del(phy_dev, proto, vid);
286 return 0;
289 static int ipvlan_get_iflink(const struct net_device *dev)
291 struct ipvl_dev *ipvlan = netdev_priv(dev);
293 return ipvlan->phy_dev->ifindex;
296 static const struct net_device_ops ipvlan_netdev_ops = {
297 .ndo_init = ipvlan_init,
298 .ndo_uninit = ipvlan_uninit,
299 .ndo_open = ipvlan_open,
300 .ndo_stop = ipvlan_stop,
301 .ndo_start_xmit = ipvlan_start_xmit,
302 .ndo_fix_features = ipvlan_fix_features,
303 .ndo_change_rx_flags = ipvlan_change_rx_flags,
304 .ndo_set_rx_mode = ipvlan_set_multicast_mac_filter,
305 .ndo_get_stats64 = ipvlan_get_stats64,
306 .ndo_vlan_rx_add_vid = ipvlan_vlan_rx_add_vid,
307 .ndo_vlan_rx_kill_vid = ipvlan_vlan_rx_kill_vid,
308 .ndo_get_iflink = ipvlan_get_iflink,
311 static int ipvlan_hard_header(struct sk_buff *skb, struct net_device *dev,
312 unsigned short type, const void *daddr,
313 const void *saddr, unsigned len)
315 const struct ipvl_dev *ipvlan = netdev_priv(dev);
316 struct net_device *phy_dev = ipvlan->phy_dev;
318 /* TODO Probably use a different field than dev_addr so that the
319 * mac-address on the virtual device is portable and can be carried
320 * while the packets use the mac-addr on the physical device.
322 return dev_hard_header(skb, phy_dev, type, daddr,
323 saddr ? : dev->dev_addr, len);
326 static const struct header_ops ipvlan_header_ops = {
327 .create = ipvlan_hard_header,
328 .parse = eth_header_parse,
329 .cache = eth_header_cache,
330 .cache_update = eth_header_cache_update,
333 static int ipvlan_ethtool_get_link_ksettings(struct net_device *dev,
334 struct ethtool_link_ksettings *cmd)
336 const struct ipvl_dev *ipvlan = netdev_priv(dev);
338 return __ethtool_get_link_ksettings(ipvlan->phy_dev, cmd);
341 static void ipvlan_ethtool_get_drvinfo(struct net_device *dev,
342 struct ethtool_drvinfo *drvinfo)
344 strlcpy(drvinfo->driver, IPVLAN_DRV, sizeof(drvinfo->driver));
345 strlcpy(drvinfo->version, IPV_DRV_VER, sizeof(drvinfo->version));
348 static u32 ipvlan_ethtool_get_msglevel(struct net_device *dev)
350 const struct ipvl_dev *ipvlan = netdev_priv(dev);
352 return ipvlan->msg_enable;
355 static void ipvlan_ethtool_set_msglevel(struct net_device *dev, u32 value)
357 struct ipvl_dev *ipvlan = netdev_priv(dev);
359 ipvlan->msg_enable = value;
362 static const struct ethtool_ops ipvlan_ethtool_ops = {
363 .get_link = ethtool_op_get_link,
364 .get_link_ksettings = ipvlan_ethtool_get_link_ksettings,
365 .get_drvinfo = ipvlan_ethtool_get_drvinfo,
366 .get_msglevel = ipvlan_ethtool_get_msglevel,
367 .set_msglevel = ipvlan_ethtool_set_msglevel,
370 static int ipvlan_nl_changelink(struct net_device *dev,
371 struct nlattr *tb[], struct nlattr *data[])
373 struct ipvl_dev *ipvlan = netdev_priv(dev);
374 struct ipvl_port *port = ipvlan_port_get_rtnl(ipvlan->phy_dev);
376 if (data && data[IFLA_IPVLAN_MODE]) {
377 u16 nmode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
379 ipvlan_set_port_mode(port, nmode);
381 return 0;
384 static size_t ipvlan_nl_getsize(const struct net_device *dev)
386 return (0
387 + nla_total_size(2) /* IFLA_IPVLAN_MODE */
391 static int ipvlan_nl_validate(struct nlattr *tb[], struct nlattr *data[])
393 if (data && data[IFLA_IPVLAN_MODE]) {
394 u16 mode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
396 if (mode < IPVLAN_MODE_L2 || mode >= IPVLAN_MODE_MAX)
397 return -EINVAL;
399 return 0;
402 static int ipvlan_nl_fillinfo(struct sk_buff *skb,
403 const struct net_device *dev)
405 struct ipvl_dev *ipvlan = netdev_priv(dev);
406 struct ipvl_port *port = ipvlan_port_get_rtnl(ipvlan->phy_dev);
407 int ret = -EINVAL;
409 if (!port)
410 goto err;
412 ret = -EMSGSIZE;
413 if (nla_put_u16(skb, IFLA_IPVLAN_MODE, port->mode))
414 goto err;
416 return 0;
418 err:
419 return ret;
422 static int ipvlan_link_new(struct net *src_net, struct net_device *dev,
423 struct nlattr *tb[], struct nlattr *data[])
425 struct ipvl_dev *ipvlan = netdev_priv(dev);
426 struct ipvl_port *port;
427 struct net_device *phy_dev;
428 int err;
429 u16 mode = IPVLAN_MODE_L3;
431 if (!tb[IFLA_LINK])
432 return -EINVAL;
434 phy_dev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
435 if (!phy_dev)
436 return -ENODEV;
438 if (netif_is_ipvlan(phy_dev)) {
439 struct ipvl_dev *tmp = netdev_priv(phy_dev);
441 phy_dev = tmp->phy_dev;
442 } else if (!netif_is_ipvlan_port(phy_dev)) {
443 err = ipvlan_port_create(phy_dev);
444 if (err < 0)
445 return err;
448 if (data && data[IFLA_IPVLAN_MODE])
449 mode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
451 port = ipvlan_port_get_rtnl(phy_dev);
452 ipvlan->phy_dev = phy_dev;
453 ipvlan->dev = dev;
454 ipvlan->port = port;
455 ipvlan->sfeatures = IPVLAN_FEATURES;
456 ipvlan_adjust_mtu(ipvlan, phy_dev);
457 INIT_LIST_HEAD(&ipvlan->addrs);
459 /* TODO Probably put random address here to be presented to the
460 * world but keep using the physical-dev address for the outgoing
461 * packets.
463 memcpy(dev->dev_addr, phy_dev->dev_addr, ETH_ALEN);
465 dev->priv_flags |= IFF_IPVLAN_SLAVE;
467 err = register_netdevice(dev);
468 if (err < 0)
469 return err;
471 err = netdev_upper_dev_link(phy_dev, dev);
472 if (err) {
473 unregister_netdevice(dev);
474 return err;
477 list_add_tail_rcu(&ipvlan->pnode, &port->ipvlans);
478 ipvlan_set_port_mode(port, mode);
480 netif_stacked_transfer_operstate(phy_dev, dev);
481 return 0;
484 static void ipvlan_link_delete(struct net_device *dev, struct list_head *head)
486 struct ipvl_dev *ipvlan = netdev_priv(dev);
487 struct ipvl_addr *addr, *next;
489 list_for_each_entry_safe(addr, next, &ipvlan->addrs, anode) {
490 ipvlan_ht_addr_del(addr);
491 list_del(&addr->anode);
492 kfree_rcu(addr, rcu);
495 list_del_rcu(&ipvlan->pnode);
496 unregister_netdevice_queue(dev, head);
497 netdev_upper_dev_unlink(ipvlan->phy_dev, dev);
500 static void ipvlan_link_setup(struct net_device *dev)
502 ether_setup(dev);
504 dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
505 dev->priv_flags |= IFF_UNICAST_FLT | IFF_NO_QUEUE;
506 dev->netdev_ops = &ipvlan_netdev_ops;
507 dev->destructor = free_netdev;
508 dev->header_ops = &ipvlan_header_ops;
509 dev->ethtool_ops = &ipvlan_ethtool_ops;
512 static const struct nla_policy ipvlan_nl_policy[IFLA_IPVLAN_MAX + 1] =
514 [IFLA_IPVLAN_MODE] = { .type = NLA_U16 },
517 static struct rtnl_link_ops ipvlan_link_ops = {
518 .kind = "ipvlan",
519 .priv_size = sizeof(struct ipvl_dev),
521 .get_size = ipvlan_nl_getsize,
522 .policy = ipvlan_nl_policy,
523 .validate = ipvlan_nl_validate,
524 .fill_info = ipvlan_nl_fillinfo,
525 .changelink = ipvlan_nl_changelink,
526 .maxtype = IFLA_IPVLAN_MAX,
528 .setup = ipvlan_link_setup,
529 .newlink = ipvlan_link_new,
530 .dellink = ipvlan_link_delete,
533 static int ipvlan_link_register(struct rtnl_link_ops *ops)
535 return rtnl_link_register(ops);
538 static int ipvlan_device_event(struct notifier_block *unused,
539 unsigned long event, void *ptr)
541 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
542 struct ipvl_dev *ipvlan, *next;
543 struct ipvl_port *port;
544 LIST_HEAD(lst_kill);
546 if (!netif_is_ipvlan_port(dev))
547 return NOTIFY_DONE;
549 port = ipvlan_port_get_rtnl(dev);
551 switch (event) {
552 case NETDEV_CHANGE:
553 list_for_each_entry(ipvlan, &port->ipvlans, pnode)
554 netif_stacked_transfer_operstate(ipvlan->phy_dev,
555 ipvlan->dev);
556 break;
558 case NETDEV_UNREGISTER:
559 if (dev->reg_state != NETREG_UNREGISTERING)
560 break;
562 list_for_each_entry_safe(ipvlan, next, &port->ipvlans,
563 pnode)
564 ipvlan->dev->rtnl_link_ops->dellink(ipvlan->dev,
565 &lst_kill);
566 unregister_netdevice_many(&lst_kill);
567 break;
569 case NETDEV_FEAT_CHANGE:
570 list_for_each_entry(ipvlan, &port->ipvlans, pnode) {
571 ipvlan->dev->features = dev->features & IPVLAN_FEATURES;
572 ipvlan->dev->gso_max_size = dev->gso_max_size;
573 ipvlan->dev->gso_max_segs = dev->gso_max_segs;
574 netdev_features_change(ipvlan->dev);
576 break;
578 case NETDEV_CHANGEMTU:
579 list_for_each_entry(ipvlan, &port->ipvlans, pnode)
580 ipvlan_adjust_mtu(ipvlan, dev);
581 break;
583 case NETDEV_PRE_TYPE_CHANGE:
584 /* Forbid underlying device to change its type. */
585 return NOTIFY_BAD;
587 return NOTIFY_DONE;
590 static int ipvlan_add_addr6(struct ipvl_dev *ipvlan, struct in6_addr *ip6_addr)
592 struct ipvl_addr *addr;
594 if (ipvlan_addr_busy(ipvlan->port, ip6_addr, true)) {
595 netif_err(ipvlan, ifup, ipvlan->dev,
596 "Failed to add IPv6=%pI6c addr for %s intf\n",
597 ip6_addr, ipvlan->dev->name);
598 return -EINVAL;
600 addr = kzalloc(sizeof(struct ipvl_addr), GFP_ATOMIC);
601 if (!addr)
602 return -ENOMEM;
604 addr->master = ipvlan;
605 memcpy(&addr->ip6addr, ip6_addr, sizeof(struct in6_addr));
606 addr->atype = IPVL_IPV6;
607 list_add_tail(&addr->anode, &ipvlan->addrs);
609 /* If the interface is not up, the address will be added to the hash
610 * list by ipvlan_open.
612 if (netif_running(ipvlan->dev))
613 ipvlan_ht_addr_add(ipvlan, addr);
615 return 0;
618 static void ipvlan_del_addr6(struct ipvl_dev *ipvlan, struct in6_addr *ip6_addr)
620 struct ipvl_addr *addr;
622 addr = ipvlan_find_addr(ipvlan, ip6_addr, true);
623 if (!addr)
624 return;
626 ipvlan_ht_addr_del(addr);
627 list_del(&addr->anode);
628 kfree_rcu(addr, rcu);
630 return;
633 static int ipvlan_addr6_event(struct notifier_block *unused,
634 unsigned long event, void *ptr)
636 struct inet6_ifaddr *if6 = (struct inet6_ifaddr *)ptr;
637 struct net_device *dev = (struct net_device *)if6->idev->dev;
638 struct ipvl_dev *ipvlan = netdev_priv(dev);
640 /* FIXME IPv6 autoconf calls us from bh without RTNL */
641 if (in_softirq())
642 return NOTIFY_DONE;
644 if (!netif_is_ipvlan(dev))
645 return NOTIFY_DONE;
647 if (!ipvlan || !ipvlan->port)
648 return NOTIFY_DONE;
650 switch (event) {
651 case NETDEV_UP:
652 if (ipvlan_add_addr6(ipvlan, &if6->addr))
653 return NOTIFY_BAD;
654 break;
656 case NETDEV_DOWN:
657 ipvlan_del_addr6(ipvlan, &if6->addr);
658 break;
661 return NOTIFY_OK;
664 static int ipvlan_add_addr4(struct ipvl_dev *ipvlan, struct in_addr *ip4_addr)
666 struct ipvl_addr *addr;
668 if (ipvlan_addr_busy(ipvlan->port, ip4_addr, false)) {
669 netif_err(ipvlan, ifup, ipvlan->dev,
670 "Failed to add IPv4=%pI4 on %s intf.\n",
671 ip4_addr, ipvlan->dev->name);
672 return -EINVAL;
674 addr = kzalloc(sizeof(struct ipvl_addr), GFP_KERNEL);
675 if (!addr)
676 return -ENOMEM;
678 addr->master = ipvlan;
679 memcpy(&addr->ip4addr, ip4_addr, sizeof(struct in_addr));
680 addr->atype = IPVL_IPV4;
681 list_add_tail(&addr->anode, &ipvlan->addrs);
683 /* If the interface is not up, the address will be added to the hash
684 * list by ipvlan_open.
686 if (netif_running(ipvlan->dev))
687 ipvlan_ht_addr_add(ipvlan, addr);
689 return 0;
692 static void ipvlan_del_addr4(struct ipvl_dev *ipvlan, struct in_addr *ip4_addr)
694 struct ipvl_addr *addr;
696 addr = ipvlan_find_addr(ipvlan, ip4_addr, false);
697 if (!addr)
698 return;
700 ipvlan_ht_addr_del(addr);
701 list_del(&addr->anode);
702 kfree_rcu(addr, rcu);
704 return;
707 static int ipvlan_addr4_event(struct notifier_block *unused,
708 unsigned long event, void *ptr)
710 struct in_ifaddr *if4 = (struct in_ifaddr *)ptr;
711 struct net_device *dev = (struct net_device *)if4->ifa_dev->dev;
712 struct ipvl_dev *ipvlan = netdev_priv(dev);
713 struct in_addr ip4_addr;
715 if (!netif_is_ipvlan(dev))
716 return NOTIFY_DONE;
718 if (!ipvlan || !ipvlan->port)
719 return NOTIFY_DONE;
721 switch (event) {
722 case NETDEV_UP:
723 ip4_addr.s_addr = if4->ifa_address;
724 if (ipvlan_add_addr4(ipvlan, &ip4_addr))
725 return NOTIFY_BAD;
726 break;
728 case NETDEV_DOWN:
729 ip4_addr.s_addr = if4->ifa_address;
730 ipvlan_del_addr4(ipvlan, &ip4_addr);
731 break;
734 return NOTIFY_OK;
737 static struct notifier_block ipvlan_addr4_notifier_block __read_mostly = {
738 .notifier_call = ipvlan_addr4_event,
741 static struct notifier_block ipvlan_notifier_block __read_mostly = {
742 .notifier_call = ipvlan_device_event,
745 static struct notifier_block ipvlan_addr6_notifier_block __read_mostly = {
746 .notifier_call = ipvlan_addr6_event,
749 static int __init ipvlan_init_module(void)
751 int err;
753 ipvlan_init_secret();
754 register_netdevice_notifier(&ipvlan_notifier_block);
755 register_inet6addr_notifier(&ipvlan_addr6_notifier_block);
756 register_inetaddr_notifier(&ipvlan_addr4_notifier_block);
758 err = ipvlan_link_register(&ipvlan_link_ops);
759 if (err < 0)
760 goto error;
762 return 0;
763 error:
764 unregister_inetaddr_notifier(&ipvlan_addr4_notifier_block);
765 unregister_inet6addr_notifier(&ipvlan_addr6_notifier_block);
766 unregister_netdevice_notifier(&ipvlan_notifier_block);
767 return err;
770 static void __exit ipvlan_cleanup_module(void)
772 rtnl_link_unregister(&ipvlan_link_ops);
773 unregister_netdevice_notifier(&ipvlan_notifier_block);
774 unregister_inetaddr_notifier(&ipvlan_addr4_notifier_block);
775 unregister_inet6addr_notifier(&ipvlan_addr6_notifier_block);
778 module_init(ipvlan_init_module);
779 module_exit(ipvlan_cleanup_module);
781 MODULE_LICENSE("GPL");
782 MODULE_AUTHOR("Mahesh Bandewar <maheshb@google.com>");
783 MODULE_DESCRIPTION("Driver for L3 (IPv6/IPv4) based VLANs");
784 MODULE_ALIAS_RTNL_LINK("ipvlan");