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