xref: /openbmc/linux/drivers/net/ipvlan/ipvlan_main.c (revision bc5aa3a0)
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 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 static void ipvlan_set_port_mode(struct ipvl_port *port, u16 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 
42 	if (netif_is_macvlan_port(dev)) {
43 		netdev_err(dev, "Master is a macvlan port.\n");
44 		return -EBUSY;
45 	}
46 
47 	port = kzalloc(sizeof(struct ipvl_port), GFP_KERNEL);
48 	if (!port)
49 		return -ENOMEM;
50 
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]);
56 
57 	skb_queue_head_init(&port->backlog);
58 	INIT_WORK(&port->wq, ipvlan_process_multicast);
59 
60 	err = netdev_rx_handler_register(dev, ipvlan_handle_frame, port);
61 	if (err)
62 		goto err;
63 
64 	dev->priv_flags |= IFF_IPVLAN_MASTER;
65 	return 0;
66 
67 err:
68 	kfree_rcu(port, rcu);
69 	return err;
70 }
71 
72 static void ipvlan_port_destroy(struct net_device *dev)
73 {
74 	struct ipvl_port *port = ipvlan_port_get_rtnl(dev);
75 
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);
81 }
82 
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)
88 
89 #define IPVLAN_STATE_MASK \
90 	((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
91 
92 static int ipvlan_init(struct net_device *dev)
93 {
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;
97 
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;
105 
106 	netdev_lockdep_set_classes(dev);
107 
108 	ipvlan->pcpu_stats = alloc_percpu(struct ipvl_pcpu_stats);
109 	if (!ipvlan->pcpu_stats)
110 		return -ENOMEM;
111 
112 	port->count += 1;
113 
114 	return 0;
115 }
116 
117 static void ipvlan_uninit(struct net_device *dev)
118 {
119 	struct ipvl_dev *ipvlan = netdev_priv(dev);
120 	struct ipvl_port *port = ipvlan->port;
121 
122 	free_percpu(ipvlan->pcpu_stats);
123 
124 	port->count -= 1;
125 	if (!port->count)
126 		ipvlan_port_destroy(port->dev);
127 }
128 
129 static int ipvlan_open(struct net_device *dev)
130 {
131 	struct ipvl_dev *ipvlan = netdev_priv(dev);
132 	struct net_device *phy_dev = ipvlan->phy_dev;
133 	struct ipvl_addr *addr;
134 
135 	if (ipvlan->port->mode == IPVLAN_MODE_L3)
136 		dev->flags |= IFF_NOARP;
137 	else
138 		dev->flags &= ~IFF_NOARP;
139 
140 	list_for_each_entry(addr, &ipvlan->addrs, anode)
141 		ipvlan_ht_addr_add(ipvlan, addr);
142 
143 	return dev_uc_add(phy_dev, phy_dev->dev_addr);
144 }
145 
146 static int ipvlan_stop(struct net_device *dev)
147 {
148 	struct ipvl_dev *ipvlan = netdev_priv(dev);
149 	struct net_device *phy_dev = ipvlan->phy_dev;
150 	struct ipvl_addr *addr;
151 
152 	dev_uc_unsync(phy_dev, dev);
153 	dev_mc_unsync(phy_dev, dev);
154 
155 	dev_uc_del(phy_dev, phy_dev->dev_addr);
156 
157 	list_for_each_entry(addr, &ipvlan->addrs, anode)
158 		ipvlan_ht_addr_del(addr);
159 
160 	return 0;
161 }
162 
163 static netdev_tx_t ipvlan_start_xmit(struct sk_buff *skb,
164 				     struct net_device *dev)
165 {
166 	const struct ipvl_dev *ipvlan = netdev_priv(dev);
167 	int skblen = skb->len;
168 	int ret;
169 
170 	ret = ipvlan_queue_xmit(skb, dev);
171 	if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
172 		struct ipvl_pcpu_stats *pcptr;
173 
174 		pcptr = this_cpu_ptr(ipvlan->pcpu_stats);
175 
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);
182 	}
183 	return ret;
184 }
185 
186 static netdev_features_t ipvlan_fix_features(struct net_device *dev,
187 					     netdev_features_t features)
188 {
189 	struct ipvl_dev *ipvlan = netdev_priv(dev);
190 
191 	return features & (ipvlan->sfeatures | ~IPVLAN_FEATURES);
192 }
193 
194 static void ipvlan_change_rx_flags(struct net_device *dev, int change)
195 {
196 	struct ipvl_dev *ipvlan = netdev_priv(dev);
197 	struct net_device *phy_dev = ipvlan->phy_dev;
198 
199 	if (change & IFF_ALLMULTI)
200 		dev_set_allmulti(phy_dev, dev->flags & IFF_ALLMULTI? 1 : -1);
201 }
202 
203 static void ipvlan_set_multicast_mac_filter(struct net_device *dev)
204 {
205 	struct ipvl_dev *ipvlan = netdev_priv(dev);
206 
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);
212 
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);
216 
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.
220 		 */
221 		__set_bit(ipvlan_mac_hash(dev->broadcast), mc_filters);
222 
223 		bitmap_copy(ipvlan->mac_filters, mc_filters,
224 			    IPVLAN_MAC_FILTER_SIZE);
225 	}
226 	dev_uc_sync(ipvlan->phy_dev, dev);
227 	dev_mc_sync(ipvlan->phy_dev, dev);
228 }
229 
230 static struct rtnl_link_stats64 *ipvlan_get_stats64(struct net_device *dev,
231 						    struct rtnl_link_stats64 *s)
232 {
233 	struct ipvl_dev *ipvlan = netdev_priv(dev);
234 
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;
241 
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));
253 
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;
259 
260 			/* u32 values are updated without syncp protection. */
261 			rx_errs += pcptr->rx_errs;
262 			tx_drps += pcptr->tx_drps;
263 		}
264 		s->rx_errors = rx_errs;
265 		s->rx_dropped = rx_errs;
266 		s->tx_dropped = tx_drps;
267 	}
268 	return s;
269 }
270 
271 static int ipvlan_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
272 {
273 	struct ipvl_dev *ipvlan = netdev_priv(dev);
274 	struct net_device *phy_dev = ipvlan->phy_dev;
275 
276 	return vlan_vid_add(phy_dev, proto, vid);
277 }
278 
279 static int ipvlan_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
280 				   u16 vid)
281 {
282 	struct ipvl_dev *ipvlan = netdev_priv(dev);
283 	struct net_device *phy_dev = ipvlan->phy_dev;
284 
285 	vlan_vid_del(phy_dev, proto, vid);
286 	return 0;
287 }
288 
289 static int ipvlan_get_iflink(const struct net_device *dev)
290 {
291 	struct ipvl_dev *ipvlan = netdev_priv(dev);
292 
293 	return ipvlan->phy_dev->ifindex;
294 }
295 
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,
309 };
310 
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)
314 {
315 	const struct ipvl_dev *ipvlan = netdev_priv(dev);
316 	struct net_device *phy_dev = ipvlan->phy_dev;
317 
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.
321 	 */
322 	return dev_hard_header(skb, phy_dev, type, daddr,
323 			       saddr ? : dev->dev_addr, len);
324 }
325 
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,
331 };
332 
333 static int ipvlan_ethtool_get_link_ksettings(struct net_device *dev,
334 					     struct ethtool_link_ksettings *cmd)
335 {
336 	const struct ipvl_dev *ipvlan = netdev_priv(dev);
337 
338 	return __ethtool_get_link_ksettings(ipvlan->phy_dev, cmd);
339 }
340 
341 static void ipvlan_ethtool_get_drvinfo(struct net_device *dev,
342 				       struct ethtool_drvinfo *drvinfo)
343 {
344 	strlcpy(drvinfo->driver, IPVLAN_DRV, sizeof(drvinfo->driver));
345 	strlcpy(drvinfo->version, IPV_DRV_VER, sizeof(drvinfo->version));
346 }
347 
348 static u32 ipvlan_ethtool_get_msglevel(struct net_device *dev)
349 {
350 	const struct ipvl_dev *ipvlan = netdev_priv(dev);
351 
352 	return ipvlan->msg_enable;
353 }
354 
355 static void ipvlan_ethtool_set_msglevel(struct net_device *dev, u32 value)
356 {
357 	struct ipvl_dev *ipvlan = netdev_priv(dev);
358 
359 	ipvlan->msg_enable = value;
360 }
361 
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,
368 };
369 
370 static int ipvlan_nl_changelink(struct net_device *dev,
371 				struct nlattr *tb[], struct nlattr *data[])
372 {
373 	struct ipvl_dev *ipvlan = netdev_priv(dev);
374 	struct ipvl_port *port = ipvlan_port_get_rtnl(ipvlan->phy_dev);
375 
376 	if (data && data[IFLA_IPVLAN_MODE]) {
377 		u16 nmode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
378 
379 		ipvlan_set_port_mode(port, nmode);
380 	}
381 	return 0;
382 }
383 
384 static size_t ipvlan_nl_getsize(const struct net_device *dev)
385 {
386 	return (0
387 		+ nla_total_size(2) /* IFLA_IPVLAN_MODE */
388 		);
389 }
390 
391 static int ipvlan_nl_validate(struct nlattr *tb[], struct nlattr *data[])
392 {
393 	if (data && data[IFLA_IPVLAN_MODE]) {
394 		u16 mode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
395 
396 		if (mode < IPVLAN_MODE_L2 || mode >= IPVLAN_MODE_MAX)
397 			return -EINVAL;
398 	}
399 	return 0;
400 }
401 
402 static int ipvlan_nl_fillinfo(struct sk_buff *skb,
403 			      const struct net_device *dev)
404 {
405 	struct ipvl_dev *ipvlan = netdev_priv(dev);
406 	struct ipvl_port *port = ipvlan_port_get_rtnl(ipvlan->phy_dev);
407 	int ret = -EINVAL;
408 
409 	if (!port)
410 		goto err;
411 
412 	ret = -EMSGSIZE;
413 	if (nla_put_u16(skb, IFLA_IPVLAN_MODE, port->mode))
414 		goto err;
415 
416 	return 0;
417 
418 err:
419 	return ret;
420 }
421 
422 static int ipvlan_link_new(struct net *src_net, struct net_device *dev,
423 			   struct nlattr *tb[], struct nlattr *data[])
424 {
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;
430 
431 	if (!tb[IFLA_LINK])
432 		return -EINVAL;
433 
434 	phy_dev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
435 	if (!phy_dev)
436 		return -ENODEV;
437 
438 	if (netif_is_ipvlan(phy_dev)) {
439 		struct ipvl_dev *tmp = netdev_priv(phy_dev);
440 
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;
446 	}
447 
448 	if (data && data[IFLA_IPVLAN_MODE])
449 		mode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
450 
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);
458 
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.
462 	 */
463 	memcpy(dev->dev_addr, phy_dev->dev_addr, ETH_ALEN);
464 
465 	dev->priv_flags |= IFF_IPVLAN_SLAVE;
466 
467 	err = register_netdevice(dev);
468 	if (err < 0)
469 		return err;
470 
471 	err = netdev_upper_dev_link(phy_dev, dev);
472 	if (err) {
473 		unregister_netdevice(dev);
474 		return err;
475 	}
476 
477 	list_add_tail_rcu(&ipvlan->pnode, &port->ipvlans);
478 	ipvlan_set_port_mode(port, mode);
479 
480 	netif_stacked_transfer_operstate(phy_dev, dev);
481 	return 0;
482 }
483 
484 static void ipvlan_link_delete(struct net_device *dev, struct list_head *head)
485 {
486 	struct ipvl_dev *ipvlan = netdev_priv(dev);
487 	struct ipvl_addr *addr, *next;
488 
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);
493 	}
494 
495 	list_del_rcu(&ipvlan->pnode);
496 	unregister_netdevice_queue(dev, head);
497 	netdev_upper_dev_unlink(ipvlan->phy_dev, dev);
498 }
499 
500 static void ipvlan_link_setup(struct net_device *dev)
501 {
502 	ether_setup(dev);
503 
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;
510 }
511 
512 static const struct nla_policy ipvlan_nl_policy[IFLA_IPVLAN_MAX + 1] =
513 {
514 	[IFLA_IPVLAN_MODE] = { .type = NLA_U16 },
515 };
516 
517 static struct rtnl_link_ops ipvlan_link_ops = {
518 	.kind		= "ipvlan",
519 	.priv_size	= sizeof(struct ipvl_dev),
520 
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,
527 
528 	.setup		= ipvlan_link_setup,
529 	.newlink	= ipvlan_link_new,
530 	.dellink	= ipvlan_link_delete,
531 };
532 
533 static int ipvlan_link_register(struct rtnl_link_ops *ops)
534 {
535 	return rtnl_link_register(ops);
536 }
537 
538 static int ipvlan_device_event(struct notifier_block *unused,
539 			       unsigned long event, void *ptr)
540 {
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);
545 
546 	if (!netif_is_ipvlan_port(dev))
547 		return NOTIFY_DONE;
548 
549 	port = ipvlan_port_get_rtnl(dev);
550 
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;
557 
558 	case NETDEV_UNREGISTER:
559 		if (dev->reg_state != NETREG_UNREGISTERING)
560 			break;
561 
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;
568 
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);
575 		}
576 		break;
577 
578 	case NETDEV_CHANGEMTU:
579 		list_for_each_entry(ipvlan, &port->ipvlans, pnode)
580 			ipvlan_adjust_mtu(ipvlan, dev);
581 		break;
582 
583 	case NETDEV_PRE_TYPE_CHANGE:
584 		/* Forbid underlying device to change its type. */
585 		return NOTIFY_BAD;
586 	}
587 	return NOTIFY_DONE;
588 }
589 
590 static int ipvlan_add_addr6(struct ipvl_dev *ipvlan, struct in6_addr *ip6_addr)
591 {
592 	struct ipvl_addr *addr;
593 
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;
599 	}
600 	addr = kzalloc(sizeof(struct ipvl_addr), GFP_ATOMIC);
601 	if (!addr)
602 		return -ENOMEM;
603 
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);
608 
609 	/* If the interface is not up, the address will be added to the hash
610 	 * list by ipvlan_open.
611 	 */
612 	if (netif_running(ipvlan->dev))
613 		ipvlan_ht_addr_add(ipvlan, addr);
614 
615 	return 0;
616 }
617 
618 static void ipvlan_del_addr6(struct ipvl_dev *ipvlan, struct in6_addr *ip6_addr)
619 {
620 	struct ipvl_addr *addr;
621 
622 	addr = ipvlan_find_addr(ipvlan, ip6_addr, true);
623 	if (!addr)
624 		return;
625 
626 	ipvlan_ht_addr_del(addr);
627 	list_del(&addr->anode);
628 	kfree_rcu(addr, rcu);
629 
630 	return;
631 }
632 
633 static int ipvlan_addr6_event(struct notifier_block *unused,
634 			      unsigned long event, void *ptr)
635 {
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);
639 
640 	/* FIXME IPv6 autoconf calls us from bh without RTNL */
641 	if (in_softirq())
642 		return NOTIFY_DONE;
643 
644 	if (!netif_is_ipvlan(dev))
645 		return NOTIFY_DONE;
646 
647 	if (!ipvlan || !ipvlan->port)
648 		return NOTIFY_DONE;
649 
650 	switch (event) {
651 	case NETDEV_UP:
652 		if (ipvlan_add_addr6(ipvlan, &if6->addr))
653 			return NOTIFY_BAD;
654 		break;
655 
656 	case NETDEV_DOWN:
657 		ipvlan_del_addr6(ipvlan, &if6->addr);
658 		break;
659 	}
660 
661 	return NOTIFY_OK;
662 }
663 
664 static int ipvlan_add_addr4(struct ipvl_dev *ipvlan, struct in_addr *ip4_addr)
665 {
666 	struct ipvl_addr *addr;
667 
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;
673 	}
674 	addr = kzalloc(sizeof(struct ipvl_addr), GFP_KERNEL);
675 	if (!addr)
676 		return -ENOMEM;
677 
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);
682 
683 	/* If the interface is not up, the address will be added to the hash
684 	 * list by ipvlan_open.
685 	 */
686 	if (netif_running(ipvlan->dev))
687 		ipvlan_ht_addr_add(ipvlan, addr);
688 
689 	return 0;
690 }
691 
692 static void ipvlan_del_addr4(struct ipvl_dev *ipvlan, struct in_addr *ip4_addr)
693 {
694 	struct ipvl_addr *addr;
695 
696 	addr = ipvlan_find_addr(ipvlan, ip4_addr, false);
697 	if (!addr)
698 		return;
699 
700 	ipvlan_ht_addr_del(addr);
701 	list_del(&addr->anode);
702 	kfree_rcu(addr, rcu);
703 
704 	return;
705 }
706 
707 static int ipvlan_addr4_event(struct notifier_block *unused,
708 			      unsigned long event, void *ptr)
709 {
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;
714 
715 	if (!netif_is_ipvlan(dev))
716 		return NOTIFY_DONE;
717 
718 	if (!ipvlan || !ipvlan->port)
719 		return NOTIFY_DONE;
720 
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;
727 
728 	case NETDEV_DOWN:
729 		ip4_addr.s_addr = if4->ifa_address;
730 		ipvlan_del_addr4(ipvlan, &ip4_addr);
731 		break;
732 	}
733 
734 	return NOTIFY_OK;
735 }
736 
737 static struct notifier_block ipvlan_addr4_notifier_block __read_mostly = {
738 	.notifier_call = ipvlan_addr4_event,
739 };
740 
741 static struct notifier_block ipvlan_notifier_block __read_mostly = {
742 	.notifier_call = ipvlan_device_event,
743 };
744 
745 static struct notifier_block ipvlan_addr6_notifier_block __read_mostly = {
746 	.notifier_call = ipvlan_addr6_event,
747 };
748 
749 static int __init ipvlan_init_module(void)
750 {
751 	int err;
752 
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);
757 
758 	err = ipvlan_link_register(&ipvlan_link_ops);
759 	if (err < 0)
760 		goto error;
761 
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;
768 }
769 
770 static void __exit ipvlan_cleanup_module(void)
771 {
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);
776 }
777 
778 module_init(ipvlan_init_module);
779 module_exit(ipvlan_cleanup_module);
780 
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");
785