xref: /openbmc/linux/net/core/rtnetlink.c (revision c819e2cf)
1 /*
2  * INET		An implementation of the TCP/IP protocol suite for the LINUX
3  *		operating system.  INET is implemented using the  BSD Socket
4  *		interface as the means of communication with the user level.
5  *
6  *		Routing netlink socket interface: protocol independent part.
7  *
8  * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
9  *
10  *		This program is free software; you can redistribute it and/or
11  *		modify it under the terms of the GNU General Public License
12  *		as published by the Free Software Foundation; either version
13  *		2 of the License, or (at your option) any later version.
14  *
15  *	Fixes:
16  *	Vitaly E. Lavrov		RTA_OK arithmetics was wrong.
17  */
18 
19 #include <linux/errno.h>
20 #include <linux/module.h>
21 #include <linux/types.h>
22 #include <linux/socket.h>
23 #include <linux/kernel.h>
24 #include <linux/timer.h>
25 #include <linux/string.h>
26 #include <linux/sockios.h>
27 #include <linux/net.h>
28 #include <linux/fcntl.h>
29 #include <linux/mm.h>
30 #include <linux/slab.h>
31 #include <linux/interrupt.h>
32 #include <linux/capability.h>
33 #include <linux/skbuff.h>
34 #include <linux/init.h>
35 #include <linux/security.h>
36 #include <linux/mutex.h>
37 #include <linux/if_addr.h>
38 #include <linux/if_bridge.h>
39 #include <linux/if_vlan.h>
40 #include <linux/pci.h>
41 #include <linux/etherdevice.h>
42 
43 #include <asm/uaccess.h>
44 
45 #include <linux/inet.h>
46 #include <linux/netdevice.h>
47 #include <net/switchdev.h>
48 #include <net/ip.h>
49 #include <net/protocol.h>
50 #include <net/arp.h>
51 #include <net/route.h>
52 #include <net/udp.h>
53 #include <net/sock.h>
54 #include <net/pkt_sched.h>
55 #include <net/fib_rules.h>
56 #include <net/rtnetlink.h>
57 #include <net/net_namespace.h>
58 
59 struct rtnl_link {
60 	rtnl_doit_func		doit;
61 	rtnl_dumpit_func	dumpit;
62 	rtnl_calcit_func 	calcit;
63 };
64 
65 static DEFINE_MUTEX(rtnl_mutex);
66 
67 void rtnl_lock(void)
68 {
69 	mutex_lock(&rtnl_mutex);
70 }
71 EXPORT_SYMBOL(rtnl_lock);
72 
73 void __rtnl_unlock(void)
74 {
75 	mutex_unlock(&rtnl_mutex);
76 }
77 
78 void rtnl_unlock(void)
79 {
80 	/* This fellow will unlock it for us. */
81 	netdev_run_todo();
82 }
83 EXPORT_SYMBOL(rtnl_unlock);
84 
85 int rtnl_trylock(void)
86 {
87 	return mutex_trylock(&rtnl_mutex);
88 }
89 EXPORT_SYMBOL(rtnl_trylock);
90 
91 int rtnl_is_locked(void)
92 {
93 	return mutex_is_locked(&rtnl_mutex);
94 }
95 EXPORT_SYMBOL(rtnl_is_locked);
96 
97 #ifdef CONFIG_PROVE_LOCKING
98 int lockdep_rtnl_is_held(void)
99 {
100 	return lockdep_is_held(&rtnl_mutex);
101 }
102 EXPORT_SYMBOL(lockdep_rtnl_is_held);
103 #endif /* #ifdef CONFIG_PROVE_LOCKING */
104 
105 static struct rtnl_link *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
106 
107 static inline int rtm_msgindex(int msgtype)
108 {
109 	int msgindex = msgtype - RTM_BASE;
110 
111 	/*
112 	 * msgindex < 0 implies someone tried to register a netlink
113 	 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
114 	 * the message type has not been added to linux/rtnetlink.h
115 	 */
116 	BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
117 
118 	return msgindex;
119 }
120 
121 static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex)
122 {
123 	struct rtnl_link *tab;
124 
125 	if (protocol <= RTNL_FAMILY_MAX)
126 		tab = rtnl_msg_handlers[protocol];
127 	else
128 		tab = NULL;
129 
130 	if (tab == NULL || tab[msgindex].doit == NULL)
131 		tab = rtnl_msg_handlers[PF_UNSPEC];
132 
133 	return tab[msgindex].doit;
134 }
135 
136 static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex)
137 {
138 	struct rtnl_link *tab;
139 
140 	if (protocol <= RTNL_FAMILY_MAX)
141 		tab = rtnl_msg_handlers[protocol];
142 	else
143 		tab = NULL;
144 
145 	if (tab == NULL || tab[msgindex].dumpit == NULL)
146 		tab = rtnl_msg_handlers[PF_UNSPEC];
147 
148 	return tab[msgindex].dumpit;
149 }
150 
151 static rtnl_calcit_func rtnl_get_calcit(int protocol, int msgindex)
152 {
153 	struct rtnl_link *tab;
154 
155 	if (protocol <= RTNL_FAMILY_MAX)
156 		tab = rtnl_msg_handlers[protocol];
157 	else
158 		tab = NULL;
159 
160 	if (tab == NULL || tab[msgindex].calcit == NULL)
161 		tab = rtnl_msg_handlers[PF_UNSPEC];
162 
163 	return tab[msgindex].calcit;
164 }
165 
166 /**
167  * __rtnl_register - Register a rtnetlink message type
168  * @protocol: Protocol family or PF_UNSPEC
169  * @msgtype: rtnetlink message type
170  * @doit: Function pointer called for each request message
171  * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
172  * @calcit: Function pointer to calc size of dump message
173  *
174  * Registers the specified function pointers (at least one of them has
175  * to be non-NULL) to be called whenever a request message for the
176  * specified protocol family and message type is received.
177  *
178  * The special protocol family PF_UNSPEC may be used to define fallback
179  * function pointers for the case when no entry for the specific protocol
180  * family exists.
181  *
182  * Returns 0 on success or a negative error code.
183  */
184 int __rtnl_register(int protocol, int msgtype,
185 		    rtnl_doit_func doit, rtnl_dumpit_func dumpit,
186 		    rtnl_calcit_func calcit)
187 {
188 	struct rtnl_link *tab;
189 	int msgindex;
190 
191 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
192 	msgindex = rtm_msgindex(msgtype);
193 
194 	tab = rtnl_msg_handlers[protocol];
195 	if (tab == NULL) {
196 		tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL);
197 		if (tab == NULL)
198 			return -ENOBUFS;
199 
200 		rtnl_msg_handlers[protocol] = tab;
201 	}
202 
203 	if (doit)
204 		tab[msgindex].doit = doit;
205 
206 	if (dumpit)
207 		tab[msgindex].dumpit = dumpit;
208 
209 	if (calcit)
210 		tab[msgindex].calcit = calcit;
211 
212 	return 0;
213 }
214 EXPORT_SYMBOL_GPL(__rtnl_register);
215 
216 /**
217  * rtnl_register - Register a rtnetlink message type
218  *
219  * Identical to __rtnl_register() but panics on failure. This is useful
220  * as failure of this function is very unlikely, it can only happen due
221  * to lack of memory when allocating the chain to store all message
222  * handlers for a protocol. Meant for use in init functions where lack
223  * of memory implies no sense in continuing.
224  */
225 void rtnl_register(int protocol, int msgtype,
226 		   rtnl_doit_func doit, rtnl_dumpit_func dumpit,
227 		   rtnl_calcit_func calcit)
228 {
229 	if (__rtnl_register(protocol, msgtype, doit, dumpit, calcit) < 0)
230 		panic("Unable to register rtnetlink message handler, "
231 		      "protocol = %d, message type = %d\n",
232 		      protocol, msgtype);
233 }
234 EXPORT_SYMBOL_GPL(rtnl_register);
235 
236 /**
237  * rtnl_unregister - Unregister a rtnetlink message type
238  * @protocol: Protocol family or PF_UNSPEC
239  * @msgtype: rtnetlink message type
240  *
241  * Returns 0 on success or a negative error code.
242  */
243 int rtnl_unregister(int protocol, int msgtype)
244 {
245 	int msgindex;
246 
247 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
248 	msgindex = rtm_msgindex(msgtype);
249 
250 	if (rtnl_msg_handlers[protocol] == NULL)
251 		return -ENOENT;
252 
253 	rtnl_msg_handlers[protocol][msgindex].doit = NULL;
254 	rtnl_msg_handlers[protocol][msgindex].dumpit = NULL;
255 
256 	return 0;
257 }
258 EXPORT_SYMBOL_GPL(rtnl_unregister);
259 
260 /**
261  * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
262  * @protocol : Protocol family or PF_UNSPEC
263  *
264  * Identical to calling rtnl_unregster() for all registered message types
265  * of a certain protocol family.
266  */
267 void rtnl_unregister_all(int protocol)
268 {
269 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
270 
271 	kfree(rtnl_msg_handlers[protocol]);
272 	rtnl_msg_handlers[protocol] = NULL;
273 }
274 EXPORT_SYMBOL_GPL(rtnl_unregister_all);
275 
276 static LIST_HEAD(link_ops);
277 
278 static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
279 {
280 	const struct rtnl_link_ops *ops;
281 
282 	list_for_each_entry(ops, &link_ops, list) {
283 		if (!strcmp(ops->kind, kind))
284 			return ops;
285 	}
286 	return NULL;
287 }
288 
289 /**
290  * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
291  * @ops: struct rtnl_link_ops * to register
292  *
293  * The caller must hold the rtnl_mutex. This function should be used
294  * by drivers that create devices during module initialization. It
295  * must be called before registering the devices.
296  *
297  * Returns 0 on success or a negative error code.
298  */
299 int __rtnl_link_register(struct rtnl_link_ops *ops)
300 {
301 	if (rtnl_link_ops_get(ops->kind))
302 		return -EEXIST;
303 
304 	/* The check for setup is here because if ops
305 	 * does not have that filled up, it is not possible
306 	 * to use the ops for creating device. So do not
307 	 * fill up dellink as well. That disables rtnl_dellink.
308 	 */
309 	if (ops->setup && !ops->dellink)
310 		ops->dellink = unregister_netdevice_queue;
311 
312 	list_add_tail(&ops->list, &link_ops);
313 	return 0;
314 }
315 EXPORT_SYMBOL_GPL(__rtnl_link_register);
316 
317 /**
318  * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
319  * @ops: struct rtnl_link_ops * to register
320  *
321  * Returns 0 on success or a negative error code.
322  */
323 int rtnl_link_register(struct rtnl_link_ops *ops)
324 {
325 	int err;
326 
327 	rtnl_lock();
328 	err = __rtnl_link_register(ops);
329 	rtnl_unlock();
330 	return err;
331 }
332 EXPORT_SYMBOL_GPL(rtnl_link_register);
333 
334 static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
335 {
336 	struct net_device *dev;
337 	LIST_HEAD(list_kill);
338 
339 	for_each_netdev(net, dev) {
340 		if (dev->rtnl_link_ops == ops)
341 			ops->dellink(dev, &list_kill);
342 	}
343 	unregister_netdevice_many(&list_kill);
344 }
345 
346 /**
347  * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
348  * @ops: struct rtnl_link_ops * to unregister
349  *
350  * The caller must hold the rtnl_mutex.
351  */
352 void __rtnl_link_unregister(struct rtnl_link_ops *ops)
353 {
354 	struct net *net;
355 
356 	for_each_net(net) {
357 		__rtnl_kill_links(net, ops);
358 	}
359 	list_del(&ops->list);
360 }
361 EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
362 
363 /* Return with the rtnl_lock held when there are no network
364  * devices unregistering in any network namespace.
365  */
366 static void rtnl_lock_unregistering_all(void)
367 {
368 	struct net *net;
369 	bool unregistering;
370 	DEFINE_WAIT_FUNC(wait, woken_wake_function);
371 
372 	add_wait_queue(&netdev_unregistering_wq, &wait);
373 	for (;;) {
374 		unregistering = false;
375 		rtnl_lock();
376 		for_each_net(net) {
377 			if (net->dev_unreg_count > 0) {
378 				unregistering = true;
379 				break;
380 			}
381 		}
382 		if (!unregistering)
383 			break;
384 		__rtnl_unlock();
385 
386 		wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
387 	}
388 	remove_wait_queue(&netdev_unregistering_wq, &wait);
389 }
390 
391 /**
392  * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
393  * @ops: struct rtnl_link_ops * to unregister
394  */
395 void rtnl_link_unregister(struct rtnl_link_ops *ops)
396 {
397 	/* Close the race with cleanup_net() */
398 	mutex_lock(&net_mutex);
399 	rtnl_lock_unregistering_all();
400 	__rtnl_link_unregister(ops);
401 	rtnl_unlock();
402 	mutex_unlock(&net_mutex);
403 }
404 EXPORT_SYMBOL_GPL(rtnl_link_unregister);
405 
406 static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
407 {
408 	struct net_device *master_dev;
409 	const struct rtnl_link_ops *ops;
410 
411 	master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
412 	if (!master_dev)
413 		return 0;
414 	ops = master_dev->rtnl_link_ops;
415 	if (!ops || !ops->get_slave_size)
416 		return 0;
417 	/* IFLA_INFO_SLAVE_DATA + nested data */
418 	return nla_total_size(sizeof(struct nlattr)) +
419 	       ops->get_slave_size(master_dev, dev);
420 }
421 
422 static size_t rtnl_link_get_size(const struct net_device *dev)
423 {
424 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
425 	size_t size;
426 
427 	if (!ops)
428 		return 0;
429 
430 	size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
431 	       nla_total_size(strlen(ops->kind) + 1);  /* IFLA_INFO_KIND */
432 
433 	if (ops->get_size)
434 		/* IFLA_INFO_DATA + nested data */
435 		size += nla_total_size(sizeof(struct nlattr)) +
436 			ops->get_size(dev);
437 
438 	if (ops->get_xstats_size)
439 		/* IFLA_INFO_XSTATS */
440 		size += nla_total_size(ops->get_xstats_size(dev));
441 
442 	size += rtnl_link_get_slave_info_data_size(dev);
443 
444 	return size;
445 }
446 
447 static LIST_HEAD(rtnl_af_ops);
448 
449 static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
450 {
451 	const struct rtnl_af_ops *ops;
452 
453 	list_for_each_entry(ops, &rtnl_af_ops, list) {
454 		if (ops->family == family)
455 			return ops;
456 	}
457 
458 	return NULL;
459 }
460 
461 /**
462  * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
463  * @ops: struct rtnl_af_ops * to register
464  *
465  * Returns 0 on success or a negative error code.
466  */
467 void rtnl_af_register(struct rtnl_af_ops *ops)
468 {
469 	rtnl_lock();
470 	list_add_tail(&ops->list, &rtnl_af_ops);
471 	rtnl_unlock();
472 }
473 EXPORT_SYMBOL_GPL(rtnl_af_register);
474 
475 /**
476  * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
477  * @ops: struct rtnl_af_ops * to unregister
478  *
479  * The caller must hold the rtnl_mutex.
480  */
481 void __rtnl_af_unregister(struct rtnl_af_ops *ops)
482 {
483 	list_del(&ops->list);
484 }
485 EXPORT_SYMBOL_GPL(__rtnl_af_unregister);
486 
487 /**
488  * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
489  * @ops: struct rtnl_af_ops * to unregister
490  */
491 void rtnl_af_unregister(struct rtnl_af_ops *ops)
492 {
493 	rtnl_lock();
494 	__rtnl_af_unregister(ops);
495 	rtnl_unlock();
496 }
497 EXPORT_SYMBOL_GPL(rtnl_af_unregister);
498 
499 static size_t rtnl_link_get_af_size(const struct net_device *dev)
500 {
501 	struct rtnl_af_ops *af_ops;
502 	size_t size;
503 
504 	/* IFLA_AF_SPEC */
505 	size = nla_total_size(sizeof(struct nlattr));
506 
507 	list_for_each_entry(af_ops, &rtnl_af_ops, list) {
508 		if (af_ops->get_link_af_size) {
509 			/* AF_* + nested data */
510 			size += nla_total_size(sizeof(struct nlattr)) +
511 				af_ops->get_link_af_size(dev);
512 		}
513 	}
514 
515 	return size;
516 }
517 
518 static bool rtnl_have_link_slave_info(const struct net_device *dev)
519 {
520 	struct net_device *master_dev;
521 
522 	master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
523 	if (master_dev && master_dev->rtnl_link_ops)
524 		return true;
525 	return false;
526 }
527 
528 static int rtnl_link_slave_info_fill(struct sk_buff *skb,
529 				     const struct net_device *dev)
530 {
531 	struct net_device *master_dev;
532 	const struct rtnl_link_ops *ops;
533 	struct nlattr *slave_data;
534 	int err;
535 
536 	master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
537 	if (!master_dev)
538 		return 0;
539 	ops = master_dev->rtnl_link_ops;
540 	if (!ops)
541 		return 0;
542 	if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
543 		return -EMSGSIZE;
544 	if (ops->fill_slave_info) {
545 		slave_data = nla_nest_start(skb, IFLA_INFO_SLAVE_DATA);
546 		if (!slave_data)
547 			return -EMSGSIZE;
548 		err = ops->fill_slave_info(skb, master_dev, dev);
549 		if (err < 0)
550 			goto err_cancel_slave_data;
551 		nla_nest_end(skb, slave_data);
552 	}
553 	return 0;
554 
555 err_cancel_slave_data:
556 	nla_nest_cancel(skb, slave_data);
557 	return err;
558 }
559 
560 static int rtnl_link_info_fill(struct sk_buff *skb,
561 			       const struct net_device *dev)
562 {
563 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
564 	struct nlattr *data;
565 	int err;
566 
567 	if (!ops)
568 		return 0;
569 	if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
570 		return -EMSGSIZE;
571 	if (ops->fill_xstats) {
572 		err = ops->fill_xstats(skb, dev);
573 		if (err < 0)
574 			return err;
575 	}
576 	if (ops->fill_info) {
577 		data = nla_nest_start(skb, IFLA_INFO_DATA);
578 		if (data == NULL)
579 			return -EMSGSIZE;
580 		err = ops->fill_info(skb, dev);
581 		if (err < 0)
582 			goto err_cancel_data;
583 		nla_nest_end(skb, data);
584 	}
585 	return 0;
586 
587 err_cancel_data:
588 	nla_nest_cancel(skb, data);
589 	return err;
590 }
591 
592 static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
593 {
594 	struct nlattr *linkinfo;
595 	int err = -EMSGSIZE;
596 
597 	linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
598 	if (linkinfo == NULL)
599 		goto out;
600 
601 	err = rtnl_link_info_fill(skb, dev);
602 	if (err < 0)
603 		goto err_cancel_link;
604 
605 	err = rtnl_link_slave_info_fill(skb, dev);
606 	if (err < 0)
607 		goto err_cancel_link;
608 
609 	nla_nest_end(skb, linkinfo);
610 	return 0;
611 
612 err_cancel_link:
613 	nla_nest_cancel(skb, linkinfo);
614 out:
615 	return err;
616 }
617 
618 int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
619 {
620 	struct sock *rtnl = net->rtnl;
621 	int err = 0;
622 
623 	NETLINK_CB(skb).dst_group = group;
624 	if (echo)
625 		atomic_inc(&skb->users);
626 	netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
627 	if (echo)
628 		err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
629 	return err;
630 }
631 
632 int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
633 {
634 	struct sock *rtnl = net->rtnl;
635 
636 	return nlmsg_unicast(rtnl, skb, pid);
637 }
638 EXPORT_SYMBOL(rtnl_unicast);
639 
640 void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
641 		 struct nlmsghdr *nlh, gfp_t flags)
642 {
643 	struct sock *rtnl = net->rtnl;
644 	int report = 0;
645 
646 	if (nlh)
647 		report = nlmsg_report(nlh);
648 
649 	nlmsg_notify(rtnl, skb, pid, group, report, flags);
650 }
651 EXPORT_SYMBOL(rtnl_notify);
652 
653 void rtnl_set_sk_err(struct net *net, u32 group, int error)
654 {
655 	struct sock *rtnl = net->rtnl;
656 
657 	netlink_set_err(rtnl, 0, group, error);
658 }
659 EXPORT_SYMBOL(rtnl_set_sk_err);
660 
661 int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
662 {
663 	struct nlattr *mx;
664 	int i, valid = 0;
665 
666 	mx = nla_nest_start(skb, RTA_METRICS);
667 	if (mx == NULL)
668 		return -ENOBUFS;
669 
670 	for (i = 0; i < RTAX_MAX; i++) {
671 		if (metrics[i]) {
672 			valid++;
673 			if (nla_put_u32(skb, i+1, metrics[i]))
674 				goto nla_put_failure;
675 		}
676 	}
677 
678 	if (!valid) {
679 		nla_nest_cancel(skb, mx);
680 		return 0;
681 	}
682 
683 	return nla_nest_end(skb, mx);
684 
685 nla_put_failure:
686 	nla_nest_cancel(skb, mx);
687 	return -EMSGSIZE;
688 }
689 EXPORT_SYMBOL(rtnetlink_put_metrics);
690 
691 int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
692 		       long expires, u32 error)
693 {
694 	struct rta_cacheinfo ci = {
695 		.rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse),
696 		.rta_used = dst->__use,
697 		.rta_clntref = atomic_read(&(dst->__refcnt)),
698 		.rta_error = error,
699 		.rta_id =  id,
700 	};
701 
702 	if (expires) {
703 		unsigned long clock;
704 
705 		clock = jiffies_to_clock_t(abs(expires));
706 		clock = min_t(unsigned long, clock, INT_MAX);
707 		ci.rta_expires = (expires > 0) ? clock : -clock;
708 	}
709 	return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
710 }
711 EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
712 
713 static void set_operstate(struct net_device *dev, unsigned char transition)
714 {
715 	unsigned char operstate = dev->operstate;
716 
717 	switch (transition) {
718 	case IF_OPER_UP:
719 		if ((operstate == IF_OPER_DORMANT ||
720 		     operstate == IF_OPER_UNKNOWN) &&
721 		    !netif_dormant(dev))
722 			operstate = IF_OPER_UP;
723 		break;
724 
725 	case IF_OPER_DORMANT:
726 		if (operstate == IF_OPER_UP ||
727 		    operstate == IF_OPER_UNKNOWN)
728 			operstate = IF_OPER_DORMANT;
729 		break;
730 	}
731 
732 	if (dev->operstate != operstate) {
733 		write_lock_bh(&dev_base_lock);
734 		dev->operstate = operstate;
735 		write_unlock_bh(&dev_base_lock);
736 		netdev_state_change(dev);
737 	}
738 }
739 
740 static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
741 {
742 	return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
743 	       (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
744 }
745 
746 static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
747 					   const struct ifinfomsg *ifm)
748 {
749 	unsigned int flags = ifm->ifi_flags;
750 
751 	/* bugwards compatibility: ifi_change == 0 is treated as ~0 */
752 	if (ifm->ifi_change)
753 		flags = (flags & ifm->ifi_change) |
754 			(rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
755 
756 	return flags;
757 }
758 
759 static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
760 				 const struct rtnl_link_stats64 *b)
761 {
762 	a->rx_packets = b->rx_packets;
763 	a->tx_packets = b->tx_packets;
764 	a->rx_bytes = b->rx_bytes;
765 	a->tx_bytes = b->tx_bytes;
766 	a->rx_errors = b->rx_errors;
767 	a->tx_errors = b->tx_errors;
768 	a->rx_dropped = b->rx_dropped;
769 	a->tx_dropped = b->tx_dropped;
770 
771 	a->multicast = b->multicast;
772 	a->collisions = b->collisions;
773 
774 	a->rx_length_errors = b->rx_length_errors;
775 	a->rx_over_errors = b->rx_over_errors;
776 	a->rx_crc_errors = b->rx_crc_errors;
777 	a->rx_frame_errors = b->rx_frame_errors;
778 	a->rx_fifo_errors = b->rx_fifo_errors;
779 	a->rx_missed_errors = b->rx_missed_errors;
780 
781 	a->tx_aborted_errors = b->tx_aborted_errors;
782 	a->tx_carrier_errors = b->tx_carrier_errors;
783 	a->tx_fifo_errors = b->tx_fifo_errors;
784 	a->tx_heartbeat_errors = b->tx_heartbeat_errors;
785 	a->tx_window_errors = b->tx_window_errors;
786 
787 	a->rx_compressed = b->rx_compressed;
788 	a->tx_compressed = b->tx_compressed;
789 }
790 
791 static void copy_rtnl_link_stats64(void *v, const struct rtnl_link_stats64 *b)
792 {
793 	memcpy(v, b, sizeof(*b));
794 }
795 
796 /* All VF info */
797 static inline int rtnl_vfinfo_size(const struct net_device *dev,
798 				   u32 ext_filter_mask)
799 {
800 	if (dev->dev.parent && dev_is_pci(dev->dev.parent) &&
801 	    (ext_filter_mask & RTEXT_FILTER_VF)) {
802 		int num_vfs = dev_num_vf(dev->dev.parent);
803 		size_t size = nla_total_size(sizeof(struct nlattr));
804 		size += nla_total_size(num_vfs * sizeof(struct nlattr));
805 		size += num_vfs *
806 			(nla_total_size(sizeof(struct ifla_vf_mac)) +
807 			 nla_total_size(sizeof(struct ifla_vf_vlan)) +
808 			 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
809 			 nla_total_size(sizeof(struct ifla_vf_rate)) +
810 			 nla_total_size(sizeof(struct ifla_vf_link_state)));
811 		return size;
812 	} else
813 		return 0;
814 }
815 
816 static size_t rtnl_port_size(const struct net_device *dev,
817 			     u32 ext_filter_mask)
818 {
819 	size_t port_size = nla_total_size(4)		/* PORT_VF */
820 		+ nla_total_size(PORT_PROFILE_MAX)	/* PORT_PROFILE */
821 		+ nla_total_size(sizeof(struct ifla_port_vsi))
822 							/* PORT_VSI_TYPE */
823 		+ nla_total_size(PORT_UUID_MAX)		/* PORT_INSTANCE_UUID */
824 		+ nla_total_size(PORT_UUID_MAX)		/* PORT_HOST_UUID */
825 		+ nla_total_size(1)			/* PROT_VDP_REQUEST */
826 		+ nla_total_size(2);			/* PORT_VDP_RESPONSE */
827 	size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
828 	size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
829 		+ port_size;
830 	size_t port_self_size = nla_total_size(sizeof(struct nlattr))
831 		+ port_size;
832 
833 	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
834 	    !(ext_filter_mask & RTEXT_FILTER_VF))
835 		return 0;
836 	if (dev_num_vf(dev->dev.parent))
837 		return port_self_size + vf_ports_size +
838 			vf_port_size * dev_num_vf(dev->dev.parent);
839 	else
840 		return port_self_size;
841 }
842 
843 static noinline size_t if_nlmsg_size(const struct net_device *dev,
844 				     u32 ext_filter_mask)
845 {
846 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
847 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
848 	       + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
849 	       + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
850 	       + nla_total_size(sizeof(struct rtnl_link_ifmap))
851 	       + nla_total_size(sizeof(struct rtnl_link_stats))
852 	       + nla_total_size(sizeof(struct rtnl_link_stats64))
853 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
854 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
855 	       + nla_total_size(4) /* IFLA_TXQLEN */
856 	       + nla_total_size(4) /* IFLA_WEIGHT */
857 	       + nla_total_size(4) /* IFLA_MTU */
858 	       + nla_total_size(4) /* IFLA_LINK */
859 	       + nla_total_size(4) /* IFLA_MASTER */
860 	       + nla_total_size(1) /* IFLA_CARRIER */
861 	       + nla_total_size(4) /* IFLA_PROMISCUITY */
862 	       + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
863 	       + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
864 	       + nla_total_size(1) /* IFLA_OPERSTATE */
865 	       + nla_total_size(1) /* IFLA_LINKMODE */
866 	       + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
867 	       + nla_total_size(ext_filter_mask
868 			        & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
869 	       + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
870 	       + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
871 	       + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
872 	       + rtnl_link_get_af_size(dev) /* IFLA_AF_SPEC */
873 	       + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
874 	       + nla_total_size(MAX_PHYS_ITEM_ID_LEN); /* IFLA_PHYS_SWITCH_ID */
875 }
876 
877 static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
878 {
879 	struct nlattr *vf_ports;
880 	struct nlattr *vf_port;
881 	int vf;
882 	int err;
883 
884 	vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
885 	if (!vf_ports)
886 		return -EMSGSIZE;
887 
888 	for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
889 		vf_port = nla_nest_start(skb, IFLA_VF_PORT);
890 		if (!vf_port)
891 			goto nla_put_failure;
892 		if (nla_put_u32(skb, IFLA_PORT_VF, vf))
893 			goto nla_put_failure;
894 		err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
895 		if (err == -EMSGSIZE)
896 			goto nla_put_failure;
897 		if (err) {
898 			nla_nest_cancel(skb, vf_port);
899 			continue;
900 		}
901 		nla_nest_end(skb, vf_port);
902 	}
903 
904 	nla_nest_end(skb, vf_ports);
905 
906 	return 0;
907 
908 nla_put_failure:
909 	nla_nest_cancel(skb, vf_ports);
910 	return -EMSGSIZE;
911 }
912 
913 static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
914 {
915 	struct nlattr *port_self;
916 	int err;
917 
918 	port_self = nla_nest_start(skb, IFLA_PORT_SELF);
919 	if (!port_self)
920 		return -EMSGSIZE;
921 
922 	err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
923 	if (err) {
924 		nla_nest_cancel(skb, port_self);
925 		return (err == -EMSGSIZE) ? err : 0;
926 	}
927 
928 	nla_nest_end(skb, port_self);
929 
930 	return 0;
931 }
932 
933 static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
934 			  u32 ext_filter_mask)
935 {
936 	int err;
937 
938 	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
939 	    !(ext_filter_mask & RTEXT_FILTER_VF))
940 		return 0;
941 
942 	err = rtnl_port_self_fill(skb, dev);
943 	if (err)
944 		return err;
945 
946 	if (dev_num_vf(dev->dev.parent)) {
947 		err = rtnl_vf_ports_fill(skb, dev);
948 		if (err)
949 			return err;
950 	}
951 
952 	return 0;
953 }
954 
955 static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
956 {
957 	int err;
958 	struct netdev_phys_item_id ppid;
959 
960 	err = dev_get_phys_port_id(dev, &ppid);
961 	if (err) {
962 		if (err == -EOPNOTSUPP)
963 			return 0;
964 		return err;
965 	}
966 
967 	if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
968 		return -EMSGSIZE;
969 
970 	return 0;
971 }
972 
973 static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
974 {
975 	int err;
976 	struct netdev_phys_item_id psid;
977 
978 	err = netdev_switch_parent_id_get(dev, &psid);
979 	if (err) {
980 		if (err == -EOPNOTSUPP)
981 			return 0;
982 		return err;
983 	}
984 
985 	if (nla_put(skb, IFLA_PHYS_SWITCH_ID, psid.id_len, psid.id))
986 		return -EMSGSIZE;
987 
988 	return 0;
989 }
990 
991 static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
992 			    int type, u32 pid, u32 seq, u32 change,
993 			    unsigned int flags, u32 ext_filter_mask)
994 {
995 	struct ifinfomsg *ifm;
996 	struct nlmsghdr *nlh;
997 	struct rtnl_link_stats64 temp;
998 	const struct rtnl_link_stats64 *stats;
999 	struct nlattr *attr, *af_spec;
1000 	struct rtnl_af_ops *af_ops;
1001 	struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
1002 
1003 	ASSERT_RTNL();
1004 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1005 	if (nlh == NULL)
1006 		return -EMSGSIZE;
1007 
1008 	ifm = nlmsg_data(nlh);
1009 	ifm->ifi_family = AF_UNSPEC;
1010 	ifm->__ifi_pad = 0;
1011 	ifm->ifi_type = dev->type;
1012 	ifm->ifi_index = dev->ifindex;
1013 	ifm->ifi_flags = dev_get_flags(dev);
1014 	ifm->ifi_change = change;
1015 
1016 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1017 	    nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1018 	    nla_put_u8(skb, IFLA_OPERSTATE,
1019 		       netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1020 	    nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1021 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1022 	    nla_put_u32(skb, IFLA_GROUP, dev->group) ||
1023 	    nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
1024 	    nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
1025 #ifdef CONFIG_RPS
1026 	    nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
1027 #endif
1028 	    (dev->ifindex != dev->iflink &&
1029 	     nla_put_u32(skb, IFLA_LINK, dev->iflink)) ||
1030 	    (upper_dev &&
1031 	     nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) ||
1032 	    nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
1033 	    (dev->qdisc &&
1034 	     nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
1035 	    (dev->ifalias &&
1036 	     nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)) ||
1037 	    nla_put_u32(skb, IFLA_CARRIER_CHANGES,
1038 			atomic_read(&dev->carrier_changes)))
1039 		goto nla_put_failure;
1040 
1041 	if (1) {
1042 		struct rtnl_link_ifmap map = {
1043 			.mem_start   = dev->mem_start,
1044 			.mem_end     = dev->mem_end,
1045 			.base_addr   = dev->base_addr,
1046 			.irq         = dev->irq,
1047 			.dma         = dev->dma,
1048 			.port        = dev->if_port,
1049 		};
1050 		if (nla_put(skb, IFLA_MAP, sizeof(map), &map))
1051 			goto nla_put_failure;
1052 	}
1053 
1054 	if (dev->addr_len) {
1055 		if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1056 		    nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1057 			goto nla_put_failure;
1058 	}
1059 
1060 	if (rtnl_phys_port_id_fill(skb, dev))
1061 		goto nla_put_failure;
1062 
1063 	if (rtnl_phys_switch_id_fill(skb, dev))
1064 		goto nla_put_failure;
1065 
1066 	attr = nla_reserve(skb, IFLA_STATS,
1067 			sizeof(struct rtnl_link_stats));
1068 	if (attr == NULL)
1069 		goto nla_put_failure;
1070 
1071 	stats = dev_get_stats(dev, &temp);
1072 	copy_rtnl_link_stats(nla_data(attr), stats);
1073 
1074 	attr = nla_reserve(skb, IFLA_STATS64,
1075 			sizeof(struct rtnl_link_stats64));
1076 	if (attr == NULL)
1077 		goto nla_put_failure;
1078 	copy_rtnl_link_stats64(nla_data(attr), stats);
1079 
1080 	if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) &&
1081 	    nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)))
1082 		goto nla_put_failure;
1083 
1084 	if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent
1085 	    && (ext_filter_mask & RTEXT_FILTER_VF)) {
1086 		int i;
1087 
1088 		struct nlattr *vfinfo, *vf;
1089 		int num_vfs = dev_num_vf(dev->dev.parent);
1090 
1091 		vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1092 		if (!vfinfo)
1093 			goto nla_put_failure;
1094 		for (i = 0; i < num_vfs; i++) {
1095 			struct ifla_vf_info ivi;
1096 			struct ifla_vf_mac vf_mac;
1097 			struct ifla_vf_vlan vf_vlan;
1098 			struct ifla_vf_rate vf_rate;
1099 			struct ifla_vf_tx_rate vf_tx_rate;
1100 			struct ifla_vf_spoofchk vf_spoofchk;
1101 			struct ifla_vf_link_state vf_linkstate;
1102 
1103 			/*
1104 			 * Not all SR-IOV capable drivers support the
1105 			 * spoofcheck query.  Preset to -1 so the user
1106 			 * space tool can detect that the driver didn't
1107 			 * report anything.
1108 			 */
1109 			ivi.spoofchk = -1;
1110 			memset(ivi.mac, 0, sizeof(ivi.mac));
1111 			/* The default value for VF link state is "auto"
1112 			 * IFLA_VF_LINK_STATE_AUTO which equals zero
1113 			 */
1114 			ivi.linkstate = 0;
1115 			if (dev->netdev_ops->ndo_get_vf_config(dev, i, &ivi))
1116 				break;
1117 			vf_mac.vf =
1118 				vf_vlan.vf =
1119 				vf_rate.vf =
1120 				vf_tx_rate.vf =
1121 				vf_spoofchk.vf =
1122 				vf_linkstate.vf = ivi.vf;
1123 
1124 			memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1125 			vf_vlan.vlan = ivi.vlan;
1126 			vf_vlan.qos = ivi.qos;
1127 			vf_tx_rate.rate = ivi.max_tx_rate;
1128 			vf_rate.min_tx_rate = ivi.min_tx_rate;
1129 			vf_rate.max_tx_rate = ivi.max_tx_rate;
1130 			vf_spoofchk.setting = ivi.spoofchk;
1131 			vf_linkstate.link_state = ivi.linkstate;
1132 			vf = nla_nest_start(skb, IFLA_VF_INFO);
1133 			if (!vf) {
1134 				nla_nest_cancel(skb, vfinfo);
1135 				goto nla_put_failure;
1136 			}
1137 			if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1138 			    nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1139 			    nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1140 				    &vf_rate) ||
1141 			    nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1142 				    &vf_tx_rate) ||
1143 			    nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1144 				    &vf_spoofchk) ||
1145 			    nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1146 				    &vf_linkstate))
1147 				goto nla_put_failure;
1148 			nla_nest_end(skb, vf);
1149 		}
1150 		nla_nest_end(skb, vfinfo);
1151 	}
1152 
1153 	if (rtnl_port_fill(skb, dev, ext_filter_mask))
1154 		goto nla_put_failure;
1155 
1156 	if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
1157 		if (rtnl_link_fill(skb, dev) < 0)
1158 			goto nla_put_failure;
1159 	}
1160 
1161 	if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
1162 		goto nla_put_failure;
1163 
1164 	list_for_each_entry(af_ops, &rtnl_af_ops, list) {
1165 		if (af_ops->fill_link_af) {
1166 			struct nlattr *af;
1167 			int err;
1168 
1169 			if (!(af = nla_nest_start(skb, af_ops->family)))
1170 				goto nla_put_failure;
1171 
1172 			err = af_ops->fill_link_af(skb, dev);
1173 
1174 			/*
1175 			 * Caller may return ENODATA to indicate that there
1176 			 * was no data to be dumped. This is not an error, it
1177 			 * means we should trim the attribute header and
1178 			 * continue.
1179 			 */
1180 			if (err == -ENODATA)
1181 				nla_nest_cancel(skb, af);
1182 			else if (err < 0)
1183 				goto nla_put_failure;
1184 
1185 			nla_nest_end(skb, af);
1186 		}
1187 	}
1188 
1189 	nla_nest_end(skb, af_spec);
1190 
1191 	return nlmsg_end(skb, nlh);
1192 
1193 nla_put_failure:
1194 	nlmsg_cancel(skb, nlh);
1195 	return -EMSGSIZE;
1196 }
1197 
1198 static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1199 	[IFLA_IFNAME]		= { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1200 	[IFLA_ADDRESS]		= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1201 	[IFLA_BROADCAST]	= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1202 	[IFLA_MAP]		= { .len = sizeof(struct rtnl_link_ifmap) },
1203 	[IFLA_MTU]		= { .type = NLA_U32 },
1204 	[IFLA_LINK]		= { .type = NLA_U32 },
1205 	[IFLA_MASTER]		= { .type = NLA_U32 },
1206 	[IFLA_CARRIER]		= { .type = NLA_U8 },
1207 	[IFLA_TXQLEN]		= { .type = NLA_U32 },
1208 	[IFLA_WEIGHT]		= { .type = NLA_U32 },
1209 	[IFLA_OPERSTATE]	= { .type = NLA_U8 },
1210 	[IFLA_LINKMODE]		= { .type = NLA_U8 },
1211 	[IFLA_LINKINFO]		= { .type = NLA_NESTED },
1212 	[IFLA_NET_NS_PID]	= { .type = NLA_U32 },
1213 	[IFLA_NET_NS_FD]	= { .type = NLA_U32 },
1214 	[IFLA_IFALIAS]	        = { .type = NLA_STRING, .len = IFALIASZ-1 },
1215 	[IFLA_VFINFO_LIST]	= {. type = NLA_NESTED },
1216 	[IFLA_VF_PORTS]		= { .type = NLA_NESTED },
1217 	[IFLA_PORT_SELF]	= { .type = NLA_NESTED },
1218 	[IFLA_AF_SPEC]		= { .type = NLA_NESTED },
1219 	[IFLA_EXT_MASK]		= { .type = NLA_U32 },
1220 	[IFLA_PROMISCUITY]	= { .type = NLA_U32 },
1221 	[IFLA_NUM_TX_QUEUES]	= { .type = NLA_U32 },
1222 	[IFLA_NUM_RX_QUEUES]	= { .type = NLA_U32 },
1223 	[IFLA_PHYS_PORT_ID]	= { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1224 	[IFLA_CARRIER_CHANGES]	= { .type = NLA_U32 },  /* ignored */
1225 	[IFLA_PHYS_SWITCH_ID]	= { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1226 };
1227 
1228 static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1229 	[IFLA_INFO_KIND]	= { .type = NLA_STRING },
1230 	[IFLA_INFO_DATA]	= { .type = NLA_NESTED },
1231 	[IFLA_INFO_SLAVE_KIND]	= { .type = NLA_STRING },
1232 	[IFLA_INFO_SLAVE_DATA]	= { .type = NLA_NESTED },
1233 };
1234 
1235 static const struct nla_policy ifla_vfinfo_policy[IFLA_VF_INFO_MAX+1] = {
1236 	[IFLA_VF_INFO]		= { .type = NLA_NESTED },
1237 };
1238 
1239 static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1240 	[IFLA_VF_MAC]		= { .type = NLA_BINARY,
1241 				    .len = sizeof(struct ifla_vf_mac) },
1242 	[IFLA_VF_VLAN]		= { .type = NLA_BINARY,
1243 				    .len = sizeof(struct ifla_vf_vlan) },
1244 	[IFLA_VF_TX_RATE]	= { .type = NLA_BINARY,
1245 				    .len = sizeof(struct ifla_vf_tx_rate) },
1246 	[IFLA_VF_SPOOFCHK]	= { .type = NLA_BINARY,
1247 				    .len = sizeof(struct ifla_vf_spoofchk) },
1248 	[IFLA_VF_RATE]		= { .type = NLA_BINARY,
1249 				    .len = sizeof(struct ifla_vf_rate) },
1250 	[IFLA_VF_LINK_STATE]	= { .type = NLA_BINARY,
1251 				    .len = sizeof(struct ifla_vf_link_state) },
1252 };
1253 
1254 static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1255 	[IFLA_PORT_VF]		= { .type = NLA_U32 },
1256 	[IFLA_PORT_PROFILE]	= { .type = NLA_STRING,
1257 				    .len = PORT_PROFILE_MAX },
1258 	[IFLA_PORT_VSI_TYPE]	= { .type = NLA_BINARY,
1259 				    .len = sizeof(struct ifla_port_vsi)},
1260 	[IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1261 				      .len = PORT_UUID_MAX },
1262 	[IFLA_PORT_HOST_UUID]	= { .type = NLA_STRING,
1263 				    .len = PORT_UUID_MAX },
1264 	[IFLA_PORT_REQUEST]	= { .type = NLA_U8, },
1265 	[IFLA_PORT_RESPONSE]	= { .type = NLA_U16, },
1266 };
1267 
1268 static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
1269 {
1270 	struct net *net = sock_net(skb->sk);
1271 	int h, s_h;
1272 	int idx = 0, s_idx;
1273 	struct net_device *dev;
1274 	struct hlist_head *head;
1275 	struct nlattr *tb[IFLA_MAX+1];
1276 	u32 ext_filter_mask = 0;
1277 	int err;
1278 	int hdrlen;
1279 
1280 	s_h = cb->args[0];
1281 	s_idx = cb->args[1];
1282 
1283 	rcu_read_lock();
1284 	cb->seq = net->dev_base_seq;
1285 
1286 	/* A hack to preserve kernel<->userspace interface.
1287 	 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1288 	 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1289 	 * what iproute2 < v3.9.0 used.
1290 	 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1291 	 * attribute, its netlink message is shorter than struct ifinfomsg.
1292 	 */
1293 	hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ?
1294 		 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1295 
1296 	if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
1297 
1298 		if (tb[IFLA_EXT_MASK])
1299 			ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1300 	}
1301 
1302 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1303 		idx = 0;
1304 		head = &net->dev_index_head[h];
1305 		hlist_for_each_entry_rcu(dev, head, index_hlist) {
1306 			if (idx < s_idx)
1307 				goto cont;
1308 			err = rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
1309 					       NETLINK_CB(cb->skb).portid,
1310 					       cb->nlh->nlmsg_seq, 0,
1311 					       NLM_F_MULTI,
1312 					       ext_filter_mask);
1313 			/* If we ran out of room on the first message,
1314 			 * we're in trouble
1315 			 */
1316 			WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
1317 
1318 			if (err <= 0)
1319 				goto out;
1320 
1321 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
1322 cont:
1323 			idx++;
1324 		}
1325 	}
1326 out:
1327 	rcu_read_unlock();
1328 	cb->args[1] = idx;
1329 	cb->args[0] = h;
1330 
1331 	return skb->len;
1332 }
1333 
1334 int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len)
1335 {
1336 	return nla_parse(tb, IFLA_MAX, head, len, ifla_policy);
1337 }
1338 EXPORT_SYMBOL(rtnl_nla_parse_ifla);
1339 
1340 struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
1341 {
1342 	struct net *net;
1343 	/* Examine the link attributes and figure out which
1344 	 * network namespace we are talking about.
1345 	 */
1346 	if (tb[IFLA_NET_NS_PID])
1347 		net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
1348 	else if (tb[IFLA_NET_NS_FD])
1349 		net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
1350 	else
1351 		net = get_net(src_net);
1352 	return net;
1353 }
1354 EXPORT_SYMBOL(rtnl_link_get_net);
1355 
1356 static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
1357 {
1358 	if (dev) {
1359 		if (tb[IFLA_ADDRESS] &&
1360 		    nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
1361 			return -EINVAL;
1362 
1363 		if (tb[IFLA_BROADCAST] &&
1364 		    nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
1365 			return -EINVAL;
1366 	}
1367 
1368 	if (tb[IFLA_AF_SPEC]) {
1369 		struct nlattr *af;
1370 		int rem, err;
1371 
1372 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1373 			const struct rtnl_af_ops *af_ops;
1374 
1375 			if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1376 				return -EAFNOSUPPORT;
1377 
1378 			if (!af_ops->set_link_af)
1379 				return -EOPNOTSUPP;
1380 
1381 			if (af_ops->validate_link_af) {
1382 				err = af_ops->validate_link_af(dev, af);
1383 				if (err < 0)
1384 					return err;
1385 			}
1386 		}
1387 	}
1388 
1389 	return 0;
1390 }
1391 
1392 static int do_setvfinfo(struct net_device *dev, struct nlattr *attr)
1393 {
1394 	int rem, err = -EINVAL;
1395 	struct nlattr *vf;
1396 	const struct net_device_ops *ops = dev->netdev_ops;
1397 
1398 	nla_for_each_nested(vf, attr, rem) {
1399 		switch (nla_type(vf)) {
1400 		case IFLA_VF_MAC: {
1401 			struct ifla_vf_mac *ivm;
1402 			ivm = nla_data(vf);
1403 			err = -EOPNOTSUPP;
1404 			if (ops->ndo_set_vf_mac)
1405 				err = ops->ndo_set_vf_mac(dev, ivm->vf,
1406 							  ivm->mac);
1407 			break;
1408 		}
1409 		case IFLA_VF_VLAN: {
1410 			struct ifla_vf_vlan *ivv;
1411 			ivv = nla_data(vf);
1412 			err = -EOPNOTSUPP;
1413 			if (ops->ndo_set_vf_vlan)
1414 				err = ops->ndo_set_vf_vlan(dev, ivv->vf,
1415 							   ivv->vlan,
1416 							   ivv->qos);
1417 			break;
1418 		}
1419 		case IFLA_VF_TX_RATE: {
1420 			struct ifla_vf_tx_rate *ivt;
1421 			struct ifla_vf_info ivf;
1422 			ivt = nla_data(vf);
1423 			err = -EOPNOTSUPP;
1424 			if (ops->ndo_get_vf_config)
1425 				err = ops->ndo_get_vf_config(dev, ivt->vf,
1426 							     &ivf);
1427 			if (err)
1428 				break;
1429 			err = -EOPNOTSUPP;
1430 			if (ops->ndo_set_vf_rate)
1431 				err = ops->ndo_set_vf_rate(dev, ivt->vf,
1432 							   ivf.min_tx_rate,
1433 							   ivt->rate);
1434 			break;
1435 		}
1436 		case IFLA_VF_RATE: {
1437 			struct ifla_vf_rate *ivt;
1438 			ivt = nla_data(vf);
1439 			err = -EOPNOTSUPP;
1440 			if (ops->ndo_set_vf_rate)
1441 				err = ops->ndo_set_vf_rate(dev, ivt->vf,
1442 							   ivt->min_tx_rate,
1443 							   ivt->max_tx_rate);
1444 			break;
1445 		}
1446 		case IFLA_VF_SPOOFCHK: {
1447 			struct ifla_vf_spoofchk *ivs;
1448 			ivs = nla_data(vf);
1449 			err = -EOPNOTSUPP;
1450 			if (ops->ndo_set_vf_spoofchk)
1451 				err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
1452 							       ivs->setting);
1453 			break;
1454 		}
1455 		case IFLA_VF_LINK_STATE: {
1456 			struct ifla_vf_link_state *ivl;
1457 			ivl = nla_data(vf);
1458 			err = -EOPNOTSUPP;
1459 			if (ops->ndo_set_vf_link_state)
1460 				err = ops->ndo_set_vf_link_state(dev, ivl->vf,
1461 								 ivl->link_state);
1462 			break;
1463 		}
1464 		default:
1465 			err = -EINVAL;
1466 			break;
1467 		}
1468 		if (err)
1469 			break;
1470 	}
1471 	return err;
1472 }
1473 
1474 static int do_set_master(struct net_device *dev, int ifindex)
1475 {
1476 	struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
1477 	const struct net_device_ops *ops;
1478 	int err;
1479 
1480 	if (upper_dev) {
1481 		if (upper_dev->ifindex == ifindex)
1482 			return 0;
1483 		ops = upper_dev->netdev_ops;
1484 		if (ops->ndo_del_slave) {
1485 			err = ops->ndo_del_slave(upper_dev, dev);
1486 			if (err)
1487 				return err;
1488 		} else {
1489 			return -EOPNOTSUPP;
1490 		}
1491 	}
1492 
1493 	if (ifindex) {
1494 		upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
1495 		if (!upper_dev)
1496 			return -EINVAL;
1497 		ops = upper_dev->netdev_ops;
1498 		if (ops->ndo_add_slave) {
1499 			err = ops->ndo_add_slave(upper_dev, dev);
1500 			if (err)
1501 				return err;
1502 		} else {
1503 			return -EOPNOTSUPP;
1504 		}
1505 	}
1506 	return 0;
1507 }
1508 
1509 #define DO_SETLINK_MODIFIED	0x01
1510 /* notify flag means notify + modified. */
1511 #define DO_SETLINK_NOTIFY	0x03
1512 static int do_setlink(const struct sk_buff *skb,
1513 		      struct net_device *dev, struct ifinfomsg *ifm,
1514 		      struct nlattr **tb, char *ifname, int status)
1515 {
1516 	const struct net_device_ops *ops = dev->netdev_ops;
1517 	int err;
1518 
1519 	if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
1520 		struct net *net = rtnl_link_get_net(dev_net(dev), tb);
1521 		if (IS_ERR(net)) {
1522 			err = PTR_ERR(net);
1523 			goto errout;
1524 		}
1525 		if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
1526 			put_net(net);
1527 			err = -EPERM;
1528 			goto errout;
1529 		}
1530 		err = dev_change_net_namespace(dev, net, ifname);
1531 		put_net(net);
1532 		if (err)
1533 			goto errout;
1534 		status |= DO_SETLINK_MODIFIED;
1535 	}
1536 
1537 	if (tb[IFLA_MAP]) {
1538 		struct rtnl_link_ifmap *u_map;
1539 		struct ifmap k_map;
1540 
1541 		if (!ops->ndo_set_config) {
1542 			err = -EOPNOTSUPP;
1543 			goto errout;
1544 		}
1545 
1546 		if (!netif_device_present(dev)) {
1547 			err = -ENODEV;
1548 			goto errout;
1549 		}
1550 
1551 		u_map = nla_data(tb[IFLA_MAP]);
1552 		k_map.mem_start = (unsigned long) u_map->mem_start;
1553 		k_map.mem_end = (unsigned long) u_map->mem_end;
1554 		k_map.base_addr = (unsigned short) u_map->base_addr;
1555 		k_map.irq = (unsigned char) u_map->irq;
1556 		k_map.dma = (unsigned char) u_map->dma;
1557 		k_map.port = (unsigned char) u_map->port;
1558 
1559 		err = ops->ndo_set_config(dev, &k_map);
1560 		if (err < 0)
1561 			goto errout;
1562 
1563 		status |= DO_SETLINK_NOTIFY;
1564 	}
1565 
1566 	if (tb[IFLA_ADDRESS]) {
1567 		struct sockaddr *sa;
1568 		int len;
1569 
1570 		len = sizeof(sa_family_t) + dev->addr_len;
1571 		sa = kmalloc(len, GFP_KERNEL);
1572 		if (!sa) {
1573 			err = -ENOMEM;
1574 			goto errout;
1575 		}
1576 		sa->sa_family = dev->type;
1577 		memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
1578 		       dev->addr_len);
1579 		err = dev_set_mac_address(dev, sa);
1580 		kfree(sa);
1581 		if (err)
1582 			goto errout;
1583 		status |= DO_SETLINK_MODIFIED;
1584 	}
1585 
1586 	if (tb[IFLA_MTU]) {
1587 		err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1588 		if (err < 0)
1589 			goto errout;
1590 		status |= DO_SETLINK_MODIFIED;
1591 	}
1592 
1593 	if (tb[IFLA_GROUP]) {
1594 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
1595 		status |= DO_SETLINK_NOTIFY;
1596 	}
1597 
1598 	/*
1599 	 * Interface selected by interface index but interface
1600 	 * name provided implies that a name change has been
1601 	 * requested.
1602 	 */
1603 	if (ifm->ifi_index > 0 && ifname[0]) {
1604 		err = dev_change_name(dev, ifname);
1605 		if (err < 0)
1606 			goto errout;
1607 		status |= DO_SETLINK_MODIFIED;
1608 	}
1609 
1610 	if (tb[IFLA_IFALIAS]) {
1611 		err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
1612 				    nla_len(tb[IFLA_IFALIAS]));
1613 		if (err < 0)
1614 			goto errout;
1615 		status |= DO_SETLINK_NOTIFY;
1616 	}
1617 
1618 	if (tb[IFLA_BROADCAST]) {
1619 		nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
1620 		call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
1621 	}
1622 
1623 	if (ifm->ifi_flags || ifm->ifi_change) {
1624 		err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
1625 		if (err < 0)
1626 			goto errout;
1627 	}
1628 
1629 	if (tb[IFLA_MASTER]) {
1630 		err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
1631 		if (err)
1632 			goto errout;
1633 		status |= DO_SETLINK_MODIFIED;
1634 	}
1635 
1636 	if (tb[IFLA_CARRIER]) {
1637 		err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
1638 		if (err)
1639 			goto errout;
1640 		status |= DO_SETLINK_MODIFIED;
1641 	}
1642 
1643 	if (tb[IFLA_TXQLEN]) {
1644 		unsigned long value = nla_get_u32(tb[IFLA_TXQLEN]);
1645 
1646 		if (dev->tx_queue_len ^ value)
1647 			status |= DO_SETLINK_NOTIFY;
1648 
1649 		dev->tx_queue_len = value;
1650 	}
1651 
1652 	if (tb[IFLA_OPERSTATE])
1653 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
1654 
1655 	if (tb[IFLA_LINKMODE]) {
1656 		unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
1657 
1658 		write_lock_bh(&dev_base_lock);
1659 		if (dev->link_mode ^ value)
1660 			status |= DO_SETLINK_NOTIFY;
1661 		dev->link_mode = value;
1662 		write_unlock_bh(&dev_base_lock);
1663 	}
1664 
1665 	if (tb[IFLA_VFINFO_LIST]) {
1666 		struct nlattr *attr;
1667 		int rem;
1668 		nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
1669 			if (nla_type(attr) != IFLA_VF_INFO) {
1670 				err = -EINVAL;
1671 				goto errout;
1672 			}
1673 			err = do_setvfinfo(dev, attr);
1674 			if (err < 0)
1675 				goto errout;
1676 			status |= DO_SETLINK_NOTIFY;
1677 		}
1678 	}
1679 	err = 0;
1680 
1681 	if (tb[IFLA_VF_PORTS]) {
1682 		struct nlattr *port[IFLA_PORT_MAX+1];
1683 		struct nlattr *attr;
1684 		int vf;
1685 		int rem;
1686 
1687 		err = -EOPNOTSUPP;
1688 		if (!ops->ndo_set_vf_port)
1689 			goto errout;
1690 
1691 		nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
1692 			if (nla_type(attr) != IFLA_VF_PORT)
1693 				continue;
1694 			err = nla_parse_nested(port, IFLA_PORT_MAX,
1695 				attr, ifla_port_policy);
1696 			if (err < 0)
1697 				goto errout;
1698 			if (!port[IFLA_PORT_VF]) {
1699 				err = -EOPNOTSUPP;
1700 				goto errout;
1701 			}
1702 			vf = nla_get_u32(port[IFLA_PORT_VF]);
1703 			err = ops->ndo_set_vf_port(dev, vf, port);
1704 			if (err < 0)
1705 				goto errout;
1706 			status |= DO_SETLINK_NOTIFY;
1707 		}
1708 	}
1709 	err = 0;
1710 
1711 	if (tb[IFLA_PORT_SELF]) {
1712 		struct nlattr *port[IFLA_PORT_MAX+1];
1713 
1714 		err = nla_parse_nested(port, IFLA_PORT_MAX,
1715 			tb[IFLA_PORT_SELF], ifla_port_policy);
1716 		if (err < 0)
1717 			goto errout;
1718 
1719 		err = -EOPNOTSUPP;
1720 		if (ops->ndo_set_vf_port)
1721 			err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
1722 		if (err < 0)
1723 			goto errout;
1724 		status |= DO_SETLINK_NOTIFY;
1725 	}
1726 
1727 	if (tb[IFLA_AF_SPEC]) {
1728 		struct nlattr *af;
1729 		int rem;
1730 
1731 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1732 			const struct rtnl_af_ops *af_ops;
1733 
1734 			if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1735 				BUG();
1736 
1737 			err = af_ops->set_link_af(dev, af);
1738 			if (err < 0)
1739 				goto errout;
1740 
1741 			status |= DO_SETLINK_NOTIFY;
1742 		}
1743 	}
1744 	err = 0;
1745 
1746 errout:
1747 	if (status & DO_SETLINK_MODIFIED) {
1748 		if (status & DO_SETLINK_NOTIFY)
1749 			netdev_state_change(dev);
1750 
1751 		if (err < 0)
1752 			net_warn_ratelimited("A link change request failed with some changes committed already. Interface %s may have been left with an inconsistent configuration, please check.\n",
1753 					     dev->name);
1754 	}
1755 
1756 	return err;
1757 }
1758 
1759 static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
1760 {
1761 	struct net *net = sock_net(skb->sk);
1762 	struct ifinfomsg *ifm;
1763 	struct net_device *dev;
1764 	int err;
1765 	struct nlattr *tb[IFLA_MAX+1];
1766 	char ifname[IFNAMSIZ];
1767 
1768 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1769 	if (err < 0)
1770 		goto errout;
1771 
1772 	if (tb[IFLA_IFNAME])
1773 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1774 	else
1775 		ifname[0] = '\0';
1776 
1777 	err = -EINVAL;
1778 	ifm = nlmsg_data(nlh);
1779 	if (ifm->ifi_index > 0)
1780 		dev = __dev_get_by_index(net, ifm->ifi_index);
1781 	else if (tb[IFLA_IFNAME])
1782 		dev = __dev_get_by_name(net, ifname);
1783 	else
1784 		goto errout;
1785 
1786 	if (dev == NULL) {
1787 		err = -ENODEV;
1788 		goto errout;
1789 	}
1790 
1791 	err = validate_linkmsg(dev, tb);
1792 	if (err < 0)
1793 		goto errout;
1794 
1795 	err = do_setlink(skb, dev, ifm, tb, ifname, 0);
1796 errout:
1797 	return err;
1798 }
1799 
1800 static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
1801 {
1802 	struct net *net = sock_net(skb->sk);
1803 	const struct rtnl_link_ops *ops;
1804 	struct net_device *dev;
1805 	struct ifinfomsg *ifm;
1806 	char ifname[IFNAMSIZ];
1807 	struct nlattr *tb[IFLA_MAX+1];
1808 	int err;
1809 	LIST_HEAD(list_kill);
1810 
1811 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1812 	if (err < 0)
1813 		return err;
1814 
1815 	if (tb[IFLA_IFNAME])
1816 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1817 
1818 	ifm = nlmsg_data(nlh);
1819 	if (ifm->ifi_index > 0)
1820 		dev = __dev_get_by_index(net, ifm->ifi_index);
1821 	else if (tb[IFLA_IFNAME])
1822 		dev = __dev_get_by_name(net, ifname);
1823 	else
1824 		return -EINVAL;
1825 
1826 	if (!dev)
1827 		return -ENODEV;
1828 
1829 	ops = dev->rtnl_link_ops;
1830 	if (!ops || !ops->dellink)
1831 		return -EOPNOTSUPP;
1832 
1833 	ops->dellink(dev, &list_kill);
1834 	unregister_netdevice_many(&list_kill);
1835 	return 0;
1836 }
1837 
1838 int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
1839 {
1840 	unsigned int old_flags;
1841 	int err;
1842 
1843 	old_flags = dev->flags;
1844 	if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
1845 		err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
1846 		if (err < 0)
1847 			return err;
1848 	}
1849 
1850 	dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
1851 
1852 	__dev_notify_flags(dev, old_flags, ~0U);
1853 	return 0;
1854 }
1855 EXPORT_SYMBOL(rtnl_configure_link);
1856 
1857 struct net_device *rtnl_create_link(struct net *net,
1858 	char *ifname, unsigned char name_assign_type,
1859 	const struct rtnl_link_ops *ops, struct nlattr *tb[])
1860 {
1861 	int err;
1862 	struct net_device *dev;
1863 	unsigned int num_tx_queues = 1;
1864 	unsigned int num_rx_queues = 1;
1865 
1866 	if (tb[IFLA_NUM_TX_QUEUES])
1867 		num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
1868 	else if (ops->get_num_tx_queues)
1869 		num_tx_queues = ops->get_num_tx_queues();
1870 
1871 	if (tb[IFLA_NUM_RX_QUEUES])
1872 		num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
1873 	else if (ops->get_num_rx_queues)
1874 		num_rx_queues = ops->get_num_rx_queues();
1875 
1876 	err = -ENOMEM;
1877 	dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
1878 			       ops->setup, num_tx_queues, num_rx_queues);
1879 	if (!dev)
1880 		goto err;
1881 
1882 	dev_net_set(dev, net);
1883 	dev->rtnl_link_ops = ops;
1884 	dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
1885 
1886 	if (tb[IFLA_MTU])
1887 		dev->mtu = nla_get_u32(tb[IFLA_MTU]);
1888 	if (tb[IFLA_ADDRESS]) {
1889 		memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
1890 				nla_len(tb[IFLA_ADDRESS]));
1891 		dev->addr_assign_type = NET_ADDR_SET;
1892 	}
1893 	if (tb[IFLA_BROADCAST])
1894 		memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
1895 				nla_len(tb[IFLA_BROADCAST]));
1896 	if (tb[IFLA_TXQLEN])
1897 		dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
1898 	if (tb[IFLA_OPERSTATE])
1899 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
1900 	if (tb[IFLA_LINKMODE])
1901 		dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
1902 	if (tb[IFLA_GROUP])
1903 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
1904 
1905 	return dev;
1906 
1907 err:
1908 	return ERR_PTR(err);
1909 }
1910 EXPORT_SYMBOL(rtnl_create_link);
1911 
1912 static int rtnl_group_changelink(const struct sk_buff *skb,
1913 		struct net *net, int group,
1914 		struct ifinfomsg *ifm,
1915 		struct nlattr **tb)
1916 {
1917 	struct net_device *dev;
1918 	int err;
1919 
1920 	for_each_netdev(net, dev) {
1921 		if (dev->group == group) {
1922 			err = do_setlink(skb, dev, ifm, tb, NULL, 0);
1923 			if (err < 0)
1924 				return err;
1925 		}
1926 	}
1927 
1928 	return 0;
1929 }
1930 
1931 static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh)
1932 {
1933 	struct net *net = sock_net(skb->sk);
1934 	const struct rtnl_link_ops *ops;
1935 	const struct rtnl_link_ops *m_ops = NULL;
1936 	struct net_device *dev;
1937 	struct net_device *master_dev = NULL;
1938 	struct ifinfomsg *ifm;
1939 	char kind[MODULE_NAME_LEN];
1940 	char ifname[IFNAMSIZ];
1941 	struct nlattr *tb[IFLA_MAX+1];
1942 	struct nlattr *linkinfo[IFLA_INFO_MAX+1];
1943 	unsigned char name_assign_type = NET_NAME_USER;
1944 	int err;
1945 
1946 #ifdef CONFIG_MODULES
1947 replay:
1948 #endif
1949 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1950 	if (err < 0)
1951 		return err;
1952 
1953 	if (tb[IFLA_IFNAME])
1954 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1955 	else
1956 		ifname[0] = '\0';
1957 
1958 	ifm = nlmsg_data(nlh);
1959 	if (ifm->ifi_index > 0)
1960 		dev = __dev_get_by_index(net, ifm->ifi_index);
1961 	else {
1962 		if (ifname[0])
1963 			dev = __dev_get_by_name(net, ifname);
1964 		else
1965 			dev = NULL;
1966 	}
1967 
1968 	if (dev) {
1969 		master_dev = netdev_master_upper_dev_get(dev);
1970 		if (master_dev)
1971 			m_ops = master_dev->rtnl_link_ops;
1972 	}
1973 
1974 	err = validate_linkmsg(dev, tb);
1975 	if (err < 0)
1976 		return err;
1977 
1978 	if (tb[IFLA_LINKINFO]) {
1979 		err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
1980 				       tb[IFLA_LINKINFO], ifla_info_policy);
1981 		if (err < 0)
1982 			return err;
1983 	} else
1984 		memset(linkinfo, 0, sizeof(linkinfo));
1985 
1986 	if (linkinfo[IFLA_INFO_KIND]) {
1987 		nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
1988 		ops = rtnl_link_ops_get(kind);
1989 	} else {
1990 		kind[0] = '\0';
1991 		ops = NULL;
1992 	}
1993 
1994 	if (1) {
1995 		struct nlattr *attr[ops ? ops->maxtype + 1 : 0];
1996 		struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 0];
1997 		struct nlattr **data = NULL;
1998 		struct nlattr **slave_data = NULL;
1999 		struct net *dest_net;
2000 
2001 		if (ops) {
2002 			if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
2003 				err = nla_parse_nested(attr, ops->maxtype,
2004 						       linkinfo[IFLA_INFO_DATA],
2005 						       ops->policy);
2006 				if (err < 0)
2007 					return err;
2008 				data = attr;
2009 			}
2010 			if (ops->validate) {
2011 				err = ops->validate(tb, data);
2012 				if (err < 0)
2013 					return err;
2014 			}
2015 		}
2016 
2017 		if (m_ops) {
2018 			if (m_ops->slave_maxtype &&
2019 			    linkinfo[IFLA_INFO_SLAVE_DATA]) {
2020 				err = nla_parse_nested(slave_attr,
2021 						       m_ops->slave_maxtype,
2022 						       linkinfo[IFLA_INFO_SLAVE_DATA],
2023 						       m_ops->slave_policy);
2024 				if (err < 0)
2025 					return err;
2026 				slave_data = slave_attr;
2027 			}
2028 			if (m_ops->slave_validate) {
2029 				err = m_ops->slave_validate(tb, slave_data);
2030 				if (err < 0)
2031 					return err;
2032 			}
2033 		}
2034 
2035 		if (dev) {
2036 			int status = 0;
2037 
2038 			if (nlh->nlmsg_flags & NLM_F_EXCL)
2039 				return -EEXIST;
2040 			if (nlh->nlmsg_flags & NLM_F_REPLACE)
2041 				return -EOPNOTSUPP;
2042 
2043 			if (linkinfo[IFLA_INFO_DATA]) {
2044 				if (!ops || ops != dev->rtnl_link_ops ||
2045 				    !ops->changelink)
2046 					return -EOPNOTSUPP;
2047 
2048 				err = ops->changelink(dev, tb, data);
2049 				if (err < 0)
2050 					return err;
2051 				status |= DO_SETLINK_NOTIFY;
2052 			}
2053 
2054 			if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
2055 				if (!m_ops || !m_ops->slave_changelink)
2056 					return -EOPNOTSUPP;
2057 
2058 				err = m_ops->slave_changelink(master_dev, dev,
2059 							      tb, slave_data);
2060 				if (err < 0)
2061 					return err;
2062 				status |= DO_SETLINK_NOTIFY;
2063 			}
2064 
2065 			return do_setlink(skb, dev, ifm, tb, ifname, status);
2066 		}
2067 
2068 		if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
2069 			if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
2070 				return rtnl_group_changelink(skb, net,
2071 						nla_get_u32(tb[IFLA_GROUP]),
2072 						ifm, tb);
2073 			return -ENODEV;
2074 		}
2075 
2076 		if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
2077 			return -EOPNOTSUPP;
2078 
2079 		if (!ops) {
2080 #ifdef CONFIG_MODULES
2081 			if (kind[0]) {
2082 				__rtnl_unlock();
2083 				request_module("rtnl-link-%s", kind);
2084 				rtnl_lock();
2085 				ops = rtnl_link_ops_get(kind);
2086 				if (ops)
2087 					goto replay;
2088 			}
2089 #endif
2090 			return -EOPNOTSUPP;
2091 		}
2092 
2093 		if (!ops->setup)
2094 			return -EOPNOTSUPP;
2095 
2096 		if (!ifname[0]) {
2097 			snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
2098 			name_assign_type = NET_NAME_ENUM;
2099 		}
2100 
2101 		dest_net = rtnl_link_get_net(net, tb);
2102 		if (IS_ERR(dest_net))
2103 			return PTR_ERR(dest_net);
2104 
2105 		dev = rtnl_create_link(dest_net, ifname, name_assign_type, ops, tb);
2106 		if (IS_ERR(dev)) {
2107 			err = PTR_ERR(dev);
2108 			goto out;
2109 		}
2110 
2111 		dev->ifindex = ifm->ifi_index;
2112 
2113 		if (ops->newlink) {
2114 			err = ops->newlink(net, dev, tb, data);
2115 			/* Drivers should call free_netdev() in ->destructor
2116 			 * and unregister it on failure after registration
2117 			 * so that device could be finally freed in rtnl_unlock.
2118 			 */
2119 			if (err < 0) {
2120 				/* If device is not registered at all, free it now */
2121 				if (dev->reg_state == NETREG_UNINITIALIZED)
2122 					free_netdev(dev);
2123 				goto out;
2124 			}
2125 		} else {
2126 			err = register_netdevice(dev);
2127 			if (err < 0) {
2128 				free_netdev(dev);
2129 				goto out;
2130 			}
2131 		}
2132 		err = rtnl_configure_link(dev, ifm);
2133 		if (err < 0)
2134 			unregister_netdevice(dev);
2135 out:
2136 		put_net(dest_net);
2137 		return err;
2138 	}
2139 }
2140 
2141 static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh)
2142 {
2143 	struct net *net = sock_net(skb->sk);
2144 	struct ifinfomsg *ifm;
2145 	char ifname[IFNAMSIZ];
2146 	struct nlattr *tb[IFLA_MAX+1];
2147 	struct net_device *dev = NULL;
2148 	struct sk_buff *nskb;
2149 	int err;
2150 	u32 ext_filter_mask = 0;
2151 
2152 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2153 	if (err < 0)
2154 		return err;
2155 
2156 	if (tb[IFLA_IFNAME])
2157 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2158 
2159 	if (tb[IFLA_EXT_MASK])
2160 		ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2161 
2162 	ifm = nlmsg_data(nlh);
2163 	if (ifm->ifi_index > 0)
2164 		dev = __dev_get_by_index(net, ifm->ifi_index);
2165 	else if (tb[IFLA_IFNAME])
2166 		dev = __dev_get_by_name(net, ifname);
2167 	else
2168 		return -EINVAL;
2169 
2170 	if (dev == NULL)
2171 		return -ENODEV;
2172 
2173 	nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
2174 	if (nskb == NULL)
2175 		return -ENOBUFS;
2176 
2177 	err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid,
2178 			       nlh->nlmsg_seq, 0, 0, ext_filter_mask);
2179 	if (err < 0) {
2180 		/* -EMSGSIZE implies BUG in if_nlmsg_size */
2181 		WARN_ON(err == -EMSGSIZE);
2182 		kfree_skb(nskb);
2183 	} else
2184 		err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
2185 
2186 	return err;
2187 }
2188 
2189 static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
2190 {
2191 	struct net *net = sock_net(skb->sk);
2192 	struct net_device *dev;
2193 	struct nlattr *tb[IFLA_MAX+1];
2194 	u32 ext_filter_mask = 0;
2195 	u16 min_ifinfo_dump_size = 0;
2196 	int hdrlen;
2197 
2198 	/* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
2199 	hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2200 		 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2201 
2202 	if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
2203 		if (tb[IFLA_EXT_MASK])
2204 			ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2205 	}
2206 
2207 	if (!ext_filter_mask)
2208 		return NLMSG_GOODSIZE;
2209 	/*
2210 	 * traverse the list of net devices and compute the minimum
2211 	 * buffer size based upon the filter mask.
2212 	 */
2213 	list_for_each_entry(dev, &net->dev_base_head, dev_list) {
2214 		min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
2215 					     if_nlmsg_size(dev,
2216 						           ext_filter_mask));
2217 	}
2218 
2219 	return min_ifinfo_dump_size;
2220 }
2221 
2222 static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
2223 {
2224 	int idx;
2225 	int s_idx = cb->family;
2226 
2227 	if (s_idx == 0)
2228 		s_idx = 1;
2229 	for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
2230 		int type = cb->nlh->nlmsg_type-RTM_BASE;
2231 		if (idx < s_idx || idx == PF_PACKET)
2232 			continue;
2233 		if (rtnl_msg_handlers[idx] == NULL ||
2234 		    rtnl_msg_handlers[idx][type].dumpit == NULL)
2235 			continue;
2236 		if (idx > s_idx) {
2237 			memset(&cb->args[0], 0, sizeof(cb->args));
2238 			cb->prev_seq = 0;
2239 			cb->seq = 0;
2240 		}
2241 		if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
2242 			break;
2243 	}
2244 	cb->family = idx;
2245 
2246 	return skb->len;
2247 }
2248 
2249 struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
2250 				       unsigned int change, gfp_t flags)
2251 {
2252 	struct net *net = dev_net(dev);
2253 	struct sk_buff *skb;
2254 	int err = -ENOBUFS;
2255 	size_t if_info_size;
2256 
2257 	skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
2258 	if (skb == NULL)
2259 		goto errout;
2260 
2261 	err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0);
2262 	if (err < 0) {
2263 		/* -EMSGSIZE implies BUG in if_nlmsg_size() */
2264 		WARN_ON(err == -EMSGSIZE);
2265 		kfree_skb(skb);
2266 		goto errout;
2267 	}
2268 	return skb;
2269 errout:
2270 	if (err < 0)
2271 		rtnl_set_sk_err(net, RTNLGRP_LINK, err);
2272 	return NULL;
2273 }
2274 
2275 void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
2276 {
2277 	struct net *net = dev_net(dev);
2278 
2279 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
2280 }
2281 
2282 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
2283 		  gfp_t flags)
2284 {
2285 	struct sk_buff *skb;
2286 
2287 	skb = rtmsg_ifinfo_build_skb(type, dev, change, flags);
2288 	if (skb)
2289 		rtmsg_ifinfo_send(skb, dev, flags);
2290 }
2291 EXPORT_SYMBOL(rtmsg_ifinfo);
2292 
2293 static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
2294 				   struct net_device *dev,
2295 				   u8 *addr, u32 pid, u32 seq,
2296 				   int type, unsigned int flags,
2297 				   int nlflags)
2298 {
2299 	struct nlmsghdr *nlh;
2300 	struct ndmsg *ndm;
2301 
2302 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
2303 	if (!nlh)
2304 		return -EMSGSIZE;
2305 
2306 	ndm = nlmsg_data(nlh);
2307 	ndm->ndm_family  = AF_BRIDGE;
2308 	ndm->ndm_pad1	 = 0;
2309 	ndm->ndm_pad2    = 0;
2310 	ndm->ndm_flags	 = flags;
2311 	ndm->ndm_type	 = 0;
2312 	ndm->ndm_ifindex = dev->ifindex;
2313 	ndm->ndm_state   = NUD_PERMANENT;
2314 
2315 	if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
2316 		goto nla_put_failure;
2317 
2318 	return nlmsg_end(skb, nlh);
2319 
2320 nla_put_failure:
2321 	nlmsg_cancel(skb, nlh);
2322 	return -EMSGSIZE;
2323 }
2324 
2325 static inline size_t rtnl_fdb_nlmsg_size(void)
2326 {
2327 	return NLMSG_ALIGN(sizeof(struct ndmsg)) + nla_total_size(ETH_ALEN);
2328 }
2329 
2330 static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, int type)
2331 {
2332 	struct net *net = dev_net(dev);
2333 	struct sk_buff *skb;
2334 	int err = -ENOBUFS;
2335 
2336 	skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
2337 	if (!skb)
2338 		goto errout;
2339 
2340 	err = nlmsg_populate_fdb_fill(skb, dev, addr, 0, 0, type, NTF_SELF, 0);
2341 	if (err < 0) {
2342 		kfree_skb(skb);
2343 		goto errout;
2344 	}
2345 
2346 	rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
2347 	return;
2348 errout:
2349 	rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
2350 }
2351 
2352 /**
2353  * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
2354  */
2355 int ndo_dflt_fdb_add(struct ndmsg *ndm,
2356 		     struct nlattr *tb[],
2357 		     struct net_device *dev,
2358 		     const unsigned char *addr, u16 vid,
2359 		     u16 flags)
2360 {
2361 	int err = -EINVAL;
2362 
2363 	/* If aging addresses are supported device will need to
2364 	 * implement its own handler for this.
2365 	 */
2366 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
2367 		pr_info("%s: FDB only supports static addresses\n", dev->name);
2368 		return err;
2369 	}
2370 
2371 	if (vid) {
2372 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
2373 		return err;
2374 	}
2375 
2376 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2377 		err = dev_uc_add_excl(dev, addr);
2378 	else if (is_multicast_ether_addr(addr))
2379 		err = dev_mc_add_excl(dev, addr);
2380 
2381 	/* Only return duplicate errors if NLM_F_EXCL is set */
2382 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
2383 		err = 0;
2384 
2385 	return err;
2386 }
2387 EXPORT_SYMBOL(ndo_dflt_fdb_add);
2388 
2389 static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid)
2390 {
2391 	u16 vid = 0;
2392 
2393 	if (vlan_attr) {
2394 		if (nla_len(vlan_attr) != sizeof(u16)) {
2395 			pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan\n");
2396 			return -EINVAL;
2397 		}
2398 
2399 		vid = nla_get_u16(vlan_attr);
2400 
2401 		if (!vid || vid >= VLAN_VID_MASK) {
2402 			pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan id %d\n",
2403 				vid);
2404 			return -EINVAL;
2405 		}
2406 	}
2407 	*p_vid = vid;
2408 	return 0;
2409 }
2410 
2411 static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh)
2412 {
2413 	struct net *net = sock_net(skb->sk);
2414 	struct ndmsg *ndm;
2415 	struct nlattr *tb[NDA_MAX+1];
2416 	struct net_device *dev;
2417 	u8 *addr;
2418 	u16 vid;
2419 	int err;
2420 
2421 	err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
2422 	if (err < 0)
2423 		return err;
2424 
2425 	ndm = nlmsg_data(nlh);
2426 	if (ndm->ndm_ifindex == 0) {
2427 		pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
2428 		return -EINVAL;
2429 	}
2430 
2431 	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
2432 	if (dev == NULL) {
2433 		pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
2434 		return -ENODEV;
2435 	}
2436 
2437 	if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
2438 		pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
2439 		return -EINVAL;
2440 	}
2441 
2442 	addr = nla_data(tb[NDA_LLADDR]);
2443 
2444 	err = fdb_vid_parse(tb[NDA_VLAN], &vid);
2445 	if (err)
2446 		return err;
2447 
2448 	err = -EOPNOTSUPP;
2449 
2450 	/* Support fdb on master device the net/bridge default case */
2451 	if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
2452 	    (dev->priv_flags & IFF_BRIDGE_PORT)) {
2453 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2454 		const struct net_device_ops *ops = br_dev->netdev_ops;
2455 
2456 		err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
2457 				       nlh->nlmsg_flags);
2458 		if (err)
2459 			goto out;
2460 		else
2461 			ndm->ndm_flags &= ~NTF_MASTER;
2462 	}
2463 
2464 	/* Embedded bridge, macvlan, and any other device support */
2465 	if ((ndm->ndm_flags & NTF_SELF)) {
2466 		if (dev->netdev_ops->ndo_fdb_add)
2467 			err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
2468 							   vid,
2469 							   nlh->nlmsg_flags);
2470 		else
2471 			err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
2472 					       nlh->nlmsg_flags);
2473 
2474 		if (!err) {
2475 			rtnl_fdb_notify(dev, addr, RTM_NEWNEIGH);
2476 			ndm->ndm_flags &= ~NTF_SELF;
2477 		}
2478 	}
2479 out:
2480 	return err;
2481 }
2482 
2483 /**
2484  * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
2485  */
2486 int ndo_dflt_fdb_del(struct ndmsg *ndm,
2487 		     struct nlattr *tb[],
2488 		     struct net_device *dev,
2489 		     const unsigned char *addr, u16 vid)
2490 {
2491 	int err = -EINVAL;
2492 
2493 	/* If aging addresses are supported device will need to
2494 	 * implement its own handler for this.
2495 	 */
2496 	if (!(ndm->ndm_state & NUD_PERMANENT)) {
2497 		pr_info("%s: FDB only supports static addresses\n", dev->name);
2498 		return err;
2499 	}
2500 
2501 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2502 		err = dev_uc_del(dev, addr);
2503 	else if (is_multicast_ether_addr(addr))
2504 		err = dev_mc_del(dev, addr);
2505 
2506 	return err;
2507 }
2508 EXPORT_SYMBOL(ndo_dflt_fdb_del);
2509 
2510 static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh)
2511 {
2512 	struct net *net = sock_net(skb->sk);
2513 	struct ndmsg *ndm;
2514 	struct nlattr *tb[NDA_MAX+1];
2515 	struct net_device *dev;
2516 	int err = -EINVAL;
2517 	__u8 *addr;
2518 	u16 vid;
2519 
2520 	if (!netlink_capable(skb, CAP_NET_ADMIN))
2521 		return -EPERM;
2522 
2523 	err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
2524 	if (err < 0)
2525 		return err;
2526 
2527 	ndm = nlmsg_data(nlh);
2528 	if (ndm->ndm_ifindex == 0) {
2529 		pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
2530 		return -EINVAL;
2531 	}
2532 
2533 	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
2534 	if (dev == NULL) {
2535 		pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
2536 		return -ENODEV;
2537 	}
2538 
2539 	if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
2540 		pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n");
2541 		return -EINVAL;
2542 	}
2543 
2544 	addr = nla_data(tb[NDA_LLADDR]);
2545 
2546 	err = fdb_vid_parse(tb[NDA_VLAN], &vid);
2547 	if (err)
2548 		return err;
2549 
2550 	err = -EOPNOTSUPP;
2551 
2552 	/* Support fdb on master device the net/bridge default case */
2553 	if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
2554 	    (dev->priv_flags & IFF_BRIDGE_PORT)) {
2555 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2556 		const struct net_device_ops *ops = br_dev->netdev_ops;
2557 
2558 		if (ops->ndo_fdb_del)
2559 			err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
2560 
2561 		if (err)
2562 			goto out;
2563 		else
2564 			ndm->ndm_flags &= ~NTF_MASTER;
2565 	}
2566 
2567 	/* Embedded bridge, macvlan, and any other device support */
2568 	if (ndm->ndm_flags & NTF_SELF) {
2569 		if (dev->netdev_ops->ndo_fdb_del)
2570 			err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
2571 							   vid);
2572 		else
2573 			err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
2574 
2575 		if (!err) {
2576 			rtnl_fdb_notify(dev, addr, RTM_DELNEIGH);
2577 			ndm->ndm_flags &= ~NTF_SELF;
2578 		}
2579 	}
2580 out:
2581 	return err;
2582 }
2583 
2584 static int nlmsg_populate_fdb(struct sk_buff *skb,
2585 			      struct netlink_callback *cb,
2586 			      struct net_device *dev,
2587 			      int *idx,
2588 			      struct netdev_hw_addr_list *list)
2589 {
2590 	struct netdev_hw_addr *ha;
2591 	int err;
2592 	u32 portid, seq;
2593 
2594 	portid = NETLINK_CB(cb->skb).portid;
2595 	seq = cb->nlh->nlmsg_seq;
2596 
2597 	list_for_each_entry(ha, &list->list, list) {
2598 		if (*idx < cb->args[0])
2599 			goto skip;
2600 
2601 		err = nlmsg_populate_fdb_fill(skb, dev, ha->addr,
2602 					      portid, seq,
2603 					      RTM_NEWNEIGH, NTF_SELF,
2604 					      NLM_F_MULTI);
2605 		if (err < 0)
2606 			return err;
2607 skip:
2608 		*idx += 1;
2609 	}
2610 	return 0;
2611 }
2612 
2613 /**
2614  * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
2615  * @nlh: netlink message header
2616  * @dev: netdevice
2617  *
2618  * Default netdevice operation to dump the existing unicast address list.
2619  * Returns number of addresses from list put in skb.
2620  */
2621 int ndo_dflt_fdb_dump(struct sk_buff *skb,
2622 		      struct netlink_callback *cb,
2623 		      struct net_device *dev,
2624 		      struct net_device *filter_dev,
2625 		      int idx)
2626 {
2627 	int err;
2628 
2629 	netif_addr_lock_bh(dev);
2630 	err = nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->uc);
2631 	if (err)
2632 		goto out;
2633 	nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->mc);
2634 out:
2635 	netif_addr_unlock_bh(dev);
2636 	return idx;
2637 }
2638 EXPORT_SYMBOL(ndo_dflt_fdb_dump);
2639 
2640 static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
2641 {
2642 	struct net_device *dev;
2643 	struct nlattr *tb[IFLA_MAX+1];
2644 	struct net_device *bdev = NULL;
2645 	struct net_device *br_dev = NULL;
2646 	const struct net_device_ops *ops = NULL;
2647 	const struct net_device_ops *cops = NULL;
2648 	struct ifinfomsg *ifm = nlmsg_data(cb->nlh);
2649 	struct net *net = sock_net(skb->sk);
2650 	int brport_idx = 0;
2651 	int br_idx = 0;
2652 	int idx = 0;
2653 
2654 	if (nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
2655 			ifla_policy) == 0) {
2656 		if (tb[IFLA_MASTER])
2657 			br_idx = nla_get_u32(tb[IFLA_MASTER]);
2658 	}
2659 
2660 	brport_idx = ifm->ifi_index;
2661 
2662 	if (br_idx) {
2663 		br_dev = __dev_get_by_index(net, br_idx);
2664 		if (!br_dev)
2665 			return -ENODEV;
2666 
2667 		ops = br_dev->netdev_ops;
2668 		bdev = br_dev;
2669 	}
2670 
2671 	for_each_netdev(net, dev) {
2672 		if (brport_idx && (dev->ifindex != brport_idx))
2673 			continue;
2674 
2675 		if (!br_idx) { /* user did not specify a specific bridge */
2676 			if (dev->priv_flags & IFF_BRIDGE_PORT) {
2677 				br_dev = netdev_master_upper_dev_get(dev);
2678 				cops = br_dev->netdev_ops;
2679 			}
2680 
2681 			bdev = dev;
2682 		} else {
2683 			if (dev != br_dev &&
2684 			    !(dev->priv_flags & IFF_BRIDGE_PORT))
2685 				continue;
2686 
2687 			if (br_dev != netdev_master_upper_dev_get(dev) &&
2688 			    !(dev->priv_flags & IFF_EBRIDGE))
2689 				continue;
2690 
2691 			bdev = br_dev;
2692 			cops = ops;
2693 		}
2694 
2695 		if (dev->priv_flags & IFF_BRIDGE_PORT) {
2696 			if (cops && cops->ndo_fdb_dump)
2697 				idx = cops->ndo_fdb_dump(skb, cb, br_dev, dev,
2698 							 idx);
2699 		}
2700 
2701 		idx = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx);
2702 		if (dev->netdev_ops->ndo_fdb_dump)
2703 			idx = dev->netdev_ops->ndo_fdb_dump(skb, cb, bdev, dev,
2704 							    idx);
2705 
2706 		cops = NULL;
2707 	}
2708 
2709 	cb->args[0] = idx;
2710 	return skb->len;
2711 }
2712 
2713 static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
2714 			       unsigned int attrnum, unsigned int flag)
2715 {
2716 	if (mask & flag)
2717 		return nla_put_u8(skb, attrnum, !!(flags & flag));
2718 	return 0;
2719 }
2720 
2721 int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
2722 			    struct net_device *dev, u16 mode,
2723 			    u32 flags, u32 mask)
2724 {
2725 	struct nlmsghdr *nlh;
2726 	struct ifinfomsg *ifm;
2727 	struct nlattr *br_afspec;
2728 	struct nlattr *protinfo;
2729 	u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
2730 	struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2731 
2732 	nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), NLM_F_MULTI);
2733 	if (nlh == NULL)
2734 		return -EMSGSIZE;
2735 
2736 	ifm = nlmsg_data(nlh);
2737 	ifm->ifi_family = AF_BRIDGE;
2738 	ifm->__ifi_pad = 0;
2739 	ifm->ifi_type = dev->type;
2740 	ifm->ifi_index = dev->ifindex;
2741 	ifm->ifi_flags = dev_get_flags(dev);
2742 	ifm->ifi_change = 0;
2743 
2744 
2745 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
2746 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
2747 	    nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
2748 	    (br_dev &&
2749 	     nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
2750 	    (dev->addr_len &&
2751 	     nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
2752 	    (dev->ifindex != dev->iflink &&
2753 	     nla_put_u32(skb, IFLA_LINK, dev->iflink)))
2754 		goto nla_put_failure;
2755 
2756 	br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
2757 	if (!br_afspec)
2758 		goto nla_put_failure;
2759 
2760 	if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
2761 		nla_nest_cancel(skb, br_afspec);
2762 		goto nla_put_failure;
2763 	}
2764 
2765 	if (mode != BRIDGE_MODE_UNDEF) {
2766 		if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
2767 			nla_nest_cancel(skb, br_afspec);
2768 			goto nla_put_failure;
2769 		}
2770 	}
2771 	nla_nest_end(skb, br_afspec);
2772 
2773 	protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
2774 	if (!protinfo)
2775 		goto nla_put_failure;
2776 
2777 	if (brport_nla_put_flag(skb, flags, mask,
2778 				IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
2779 	    brport_nla_put_flag(skb, flags, mask,
2780 				IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
2781 	    brport_nla_put_flag(skb, flags, mask,
2782 				IFLA_BRPORT_FAST_LEAVE,
2783 				BR_MULTICAST_FAST_LEAVE) ||
2784 	    brport_nla_put_flag(skb, flags, mask,
2785 				IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
2786 	    brport_nla_put_flag(skb, flags, mask,
2787 				IFLA_BRPORT_LEARNING, BR_LEARNING) ||
2788 	    brport_nla_put_flag(skb, flags, mask,
2789 				IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
2790 	    brport_nla_put_flag(skb, flags, mask,
2791 				IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
2792 	    brport_nla_put_flag(skb, flags, mask,
2793 				IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
2794 		nla_nest_cancel(skb, protinfo);
2795 		goto nla_put_failure;
2796 	}
2797 
2798 	nla_nest_end(skb, protinfo);
2799 
2800 	return nlmsg_end(skb, nlh);
2801 nla_put_failure:
2802 	nlmsg_cancel(skb, nlh);
2803 	return -EMSGSIZE;
2804 }
2805 EXPORT_SYMBOL(ndo_dflt_bridge_getlink);
2806 
2807 static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
2808 {
2809 	struct net *net = sock_net(skb->sk);
2810 	struct net_device *dev;
2811 	int idx = 0;
2812 	u32 portid = NETLINK_CB(cb->skb).portid;
2813 	u32 seq = cb->nlh->nlmsg_seq;
2814 	u32 filter_mask = 0;
2815 
2816 	if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) {
2817 		struct nlattr *extfilt;
2818 
2819 		extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
2820 					  IFLA_EXT_MASK);
2821 		if (extfilt) {
2822 			if (nla_len(extfilt) < sizeof(filter_mask))
2823 				return -EINVAL;
2824 
2825 			filter_mask = nla_get_u32(extfilt);
2826 		}
2827 	}
2828 
2829 	rcu_read_lock();
2830 	for_each_netdev_rcu(net, dev) {
2831 		const struct net_device_ops *ops = dev->netdev_ops;
2832 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2833 
2834 		if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
2835 			if (idx >= cb->args[0] &&
2836 			    br_dev->netdev_ops->ndo_bridge_getlink(
2837 				    skb, portid, seq, dev, filter_mask) < 0)
2838 				break;
2839 			idx++;
2840 		}
2841 
2842 		if (ops->ndo_bridge_getlink) {
2843 			if (idx >= cb->args[0] &&
2844 			    ops->ndo_bridge_getlink(skb, portid, seq, dev,
2845 						    filter_mask) < 0)
2846 				break;
2847 			idx++;
2848 		}
2849 	}
2850 	rcu_read_unlock();
2851 	cb->args[0] = idx;
2852 
2853 	return skb->len;
2854 }
2855 
2856 static inline size_t bridge_nlmsg_size(void)
2857 {
2858 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
2859 		+ nla_total_size(IFNAMSIZ)	/* IFLA_IFNAME */
2860 		+ nla_total_size(MAX_ADDR_LEN)	/* IFLA_ADDRESS */
2861 		+ nla_total_size(sizeof(u32))	/* IFLA_MASTER */
2862 		+ nla_total_size(sizeof(u32))	/* IFLA_MTU */
2863 		+ nla_total_size(sizeof(u32))	/* IFLA_LINK */
2864 		+ nla_total_size(sizeof(u32))	/* IFLA_OPERSTATE */
2865 		+ nla_total_size(sizeof(u8))	/* IFLA_PROTINFO */
2866 		+ nla_total_size(sizeof(struct nlattr))	/* IFLA_AF_SPEC */
2867 		+ nla_total_size(sizeof(u16))	/* IFLA_BRIDGE_FLAGS */
2868 		+ nla_total_size(sizeof(u16));	/* IFLA_BRIDGE_MODE */
2869 }
2870 
2871 static int rtnl_bridge_notify(struct net_device *dev, u16 flags)
2872 {
2873 	struct net *net = dev_net(dev);
2874 	struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2875 	struct sk_buff *skb;
2876 	int err = -EOPNOTSUPP;
2877 
2878 	skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
2879 	if (!skb) {
2880 		err = -ENOMEM;
2881 		goto errout;
2882 	}
2883 
2884 	if ((!flags || (flags & BRIDGE_FLAGS_MASTER)) &&
2885 	    br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
2886 		err = br_dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0);
2887 		if (err < 0)
2888 			goto errout;
2889 	}
2890 
2891 	if ((flags & BRIDGE_FLAGS_SELF) &&
2892 	    dev->netdev_ops->ndo_bridge_getlink) {
2893 		err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0);
2894 		if (err < 0)
2895 			goto errout;
2896 	}
2897 
2898 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
2899 	return 0;
2900 errout:
2901 	WARN_ON(err == -EMSGSIZE);
2902 	kfree_skb(skb);
2903 	rtnl_set_sk_err(net, RTNLGRP_LINK, err);
2904 	return err;
2905 }
2906 
2907 static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
2908 {
2909 	struct net *net = sock_net(skb->sk);
2910 	struct ifinfomsg *ifm;
2911 	struct net_device *dev;
2912 	struct nlattr *br_spec, *attr = NULL;
2913 	int rem, err = -EOPNOTSUPP;
2914 	u16 oflags, flags = 0;
2915 	bool have_flags = false;
2916 
2917 	if (nlmsg_len(nlh) < sizeof(*ifm))
2918 		return -EINVAL;
2919 
2920 	ifm = nlmsg_data(nlh);
2921 	if (ifm->ifi_family != AF_BRIDGE)
2922 		return -EPFNOSUPPORT;
2923 
2924 	dev = __dev_get_by_index(net, ifm->ifi_index);
2925 	if (!dev) {
2926 		pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
2927 		return -ENODEV;
2928 	}
2929 
2930 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
2931 	if (br_spec) {
2932 		nla_for_each_nested(attr, br_spec, rem) {
2933 			if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
2934 				if (nla_len(attr) < sizeof(flags))
2935 					return -EINVAL;
2936 
2937 				have_flags = true;
2938 				flags = nla_get_u16(attr);
2939 				break;
2940 			}
2941 		}
2942 	}
2943 
2944 	oflags = flags;
2945 
2946 	if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
2947 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2948 
2949 		if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
2950 			err = -EOPNOTSUPP;
2951 			goto out;
2952 		}
2953 
2954 		err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh);
2955 		if (err)
2956 			goto out;
2957 
2958 		flags &= ~BRIDGE_FLAGS_MASTER;
2959 	}
2960 
2961 	if ((flags & BRIDGE_FLAGS_SELF)) {
2962 		if (!dev->netdev_ops->ndo_bridge_setlink)
2963 			err = -EOPNOTSUPP;
2964 		else
2965 			err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh);
2966 
2967 		if (!err)
2968 			flags &= ~BRIDGE_FLAGS_SELF;
2969 	}
2970 
2971 	if (have_flags)
2972 		memcpy(nla_data(attr), &flags, sizeof(flags));
2973 	/* Generate event to notify upper layer of bridge change */
2974 	if (!err)
2975 		err = rtnl_bridge_notify(dev, oflags);
2976 out:
2977 	return err;
2978 }
2979 
2980 static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
2981 {
2982 	struct net *net = sock_net(skb->sk);
2983 	struct ifinfomsg *ifm;
2984 	struct net_device *dev;
2985 	struct nlattr *br_spec, *attr = NULL;
2986 	int rem, err = -EOPNOTSUPP;
2987 	u16 oflags, flags = 0;
2988 	bool have_flags = false;
2989 
2990 	if (nlmsg_len(nlh) < sizeof(*ifm))
2991 		return -EINVAL;
2992 
2993 	ifm = nlmsg_data(nlh);
2994 	if (ifm->ifi_family != AF_BRIDGE)
2995 		return -EPFNOSUPPORT;
2996 
2997 	dev = __dev_get_by_index(net, ifm->ifi_index);
2998 	if (!dev) {
2999 		pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3000 		return -ENODEV;
3001 	}
3002 
3003 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3004 	if (br_spec) {
3005 		nla_for_each_nested(attr, br_spec, rem) {
3006 			if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
3007 				if (nla_len(attr) < sizeof(flags))
3008 					return -EINVAL;
3009 
3010 				have_flags = true;
3011 				flags = nla_get_u16(attr);
3012 				break;
3013 			}
3014 		}
3015 	}
3016 
3017 	oflags = flags;
3018 
3019 	if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3020 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3021 
3022 		if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
3023 			err = -EOPNOTSUPP;
3024 			goto out;
3025 		}
3026 
3027 		err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh);
3028 		if (err)
3029 			goto out;
3030 
3031 		flags &= ~BRIDGE_FLAGS_MASTER;
3032 	}
3033 
3034 	if ((flags & BRIDGE_FLAGS_SELF)) {
3035 		if (!dev->netdev_ops->ndo_bridge_dellink)
3036 			err = -EOPNOTSUPP;
3037 		else
3038 			err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh);
3039 
3040 		if (!err)
3041 			flags &= ~BRIDGE_FLAGS_SELF;
3042 	}
3043 
3044 	if (have_flags)
3045 		memcpy(nla_data(attr), &flags, sizeof(flags));
3046 	/* Generate event to notify upper layer of bridge change */
3047 	if (!err)
3048 		err = rtnl_bridge_notify(dev, oflags);
3049 out:
3050 	return err;
3051 }
3052 
3053 /* Process one rtnetlink message. */
3054 
3055 static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
3056 {
3057 	struct net *net = sock_net(skb->sk);
3058 	rtnl_doit_func doit;
3059 	int sz_idx, kind;
3060 	int family;
3061 	int type;
3062 	int err;
3063 
3064 	type = nlh->nlmsg_type;
3065 	if (type > RTM_MAX)
3066 		return -EOPNOTSUPP;
3067 
3068 	type -= RTM_BASE;
3069 
3070 	/* All the messages must have at least 1 byte length */
3071 	if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
3072 		return 0;
3073 
3074 	family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
3075 	sz_idx = type>>2;
3076 	kind = type&3;
3077 
3078 	if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
3079 		return -EPERM;
3080 
3081 	if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
3082 		struct sock *rtnl;
3083 		rtnl_dumpit_func dumpit;
3084 		rtnl_calcit_func calcit;
3085 		u16 min_dump_alloc = 0;
3086 
3087 		dumpit = rtnl_get_dumpit(family, type);
3088 		if (dumpit == NULL)
3089 			return -EOPNOTSUPP;
3090 		calcit = rtnl_get_calcit(family, type);
3091 		if (calcit)
3092 			min_dump_alloc = calcit(skb, nlh);
3093 
3094 		__rtnl_unlock();
3095 		rtnl = net->rtnl;
3096 		{
3097 			struct netlink_dump_control c = {
3098 				.dump		= dumpit,
3099 				.min_dump_alloc	= min_dump_alloc,
3100 			};
3101 			err = netlink_dump_start(rtnl, skb, nlh, &c);
3102 		}
3103 		rtnl_lock();
3104 		return err;
3105 	}
3106 
3107 	doit = rtnl_get_doit(family, type);
3108 	if (doit == NULL)
3109 		return -EOPNOTSUPP;
3110 
3111 	return doit(skb, nlh);
3112 }
3113 
3114 static void rtnetlink_rcv(struct sk_buff *skb)
3115 {
3116 	rtnl_lock();
3117 	netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
3118 	rtnl_unlock();
3119 }
3120 
3121 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
3122 {
3123 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3124 
3125 	switch (event) {
3126 	case NETDEV_UP:
3127 	case NETDEV_DOWN:
3128 	case NETDEV_PRE_UP:
3129 	case NETDEV_POST_INIT:
3130 	case NETDEV_REGISTER:
3131 	case NETDEV_CHANGE:
3132 	case NETDEV_PRE_TYPE_CHANGE:
3133 	case NETDEV_GOING_DOWN:
3134 	case NETDEV_UNREGISTER:
3135 	case NETDEV_UNREGISTER_FINAL:
3136 	case NETDEV_RELEASE:
3137 	case NETDEV_JOIN:
3138 		break;
3139 	default:
3140 		rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
3141 		break;
3142 	}
3143 	return NOTIFY_DONE;
3144 }
3145 
3146 static struct notifier_block rtnetlink_dev_notifier = {
3147 	.notifier_call	= rtnetlink_event,
3148 };
3149 
3150 
3151 static int __net_init rtnetlink_net_init(struct net *net)
3152 {
3153 	struct sock *sk;
3154 	struct netlink_kernel_cfg cfg = {
3155 		.groups		= RTNLGRP_MAX,
3156 		.input		= rtnetlink_rcv,
3157 		.cb_mutex	= &rtnl_mutex,
3158 		.flags		= NL_CFG_F_NONROOT_RECV,
3159 	};
3160 
3161 	sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
3162 	if (!sk)
3163 		return -ENOMEM;
3164 	net->rtnl = sk;
3165 	return 0;
3166 }
3167 
3168 static void __net_exit rtnetlink_net_exit(struct net *net)
3169 {
3170 	netlink_kernel_release(net->rtnl);
3171 	net->rtnl = NULL;
3172 }
3173 
3174 static struct pernet_operations rtnetlink_net_ops = {
3175 	.init = rtnetlink_net_init,
3176 	.exit = rtnetlink_net_exit,
3177 };
3178 
3179 void __init rtnetlink_init(void)
3180 {
3181 	if (register_pernet_subsys(&rtnetlink_net_ops))
3182 		panic("rtnetlink_init: cannot initialize rtnetlink\n");
3183 
3184 	register_netdevice_notifier(&rtnetlink_dev_notifier);
3185 
3186 	rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
3187 		      rtnl_dump_ifinfo, rtnl_calcit);
3188 	rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL);
3189 	rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL);
3190 	rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL);
3191 
3192 	rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL);
3193 	rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL);
3194 
3195 	rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, NULL);
3196 	rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, NULL);
3197 	rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, NULL);
3198 
3199 	rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, NULL);
3200 	rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, NULL);
3201 	rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, NULL);
3202 }
3203 
3204