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