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