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