xref: /openbmc/linux/net/core/rtnetlink.c (revision 471cb5a33dcbd7c529684a2ac7ba4451414ee4a7)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * INET		An implementation of the TCP/IP protocol suite for the LINUX
31da177e4SLinus Torvalds  *		operating system.  INET is implemented using the  BSD Socket
41da177e4SLinus Torvalds  *		interface as the means of communication with the user level.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *		Routing netlink socket interface: protocol independent part.
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  *		This program is free software; you can redistribute it and/or
111da177e4SLinus Torvalds  *		modify it under the terms of the GNU General Public License
121da177e4SLinus Torvalds  *		as published by the Free Software Foundation; either version
131da177e4SLinus Torvalds  *		2 of the License, or (at your option) any later version.
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  *	Fixes:
161da177e4SLinus Torvalds  *	Vitaly E. Lavrov		RTA_OK arithmetics was wrong.
171da177e4SLinus Torvalds  */
181da177e4SLinus Torvalds 
191da177e4SLinus Torvalds #include <linux/errno.h>
201da177e4SLinus Torvalds #include <linux/module.h>
211da177e4SLinus Torvalds #include <linux/types.h>
221da177e4SLinus Torvalds #include <linux/socket.h>
231da177e4SLinus Torvalds #include <linux/kernel.h>
241da177e4SLinus Torvalds #include <linux/timer.h>
251da177e4SLinus Torvalds #include <linux/string.h>
261da177e4SLinus Torvalds #include <linux/sockios.h>
271da177e4SLinus Torvalds #include <linux/net.h>
281da177e4SLinus Torvalds #include <linux/fcntl.h>
291da177e4SLinus Torvalds #include <linux/mm.h>
301da177e4SLinus Torvalds #include <linux/slab.h>
311da177e4SLinus Torvalds #include <linux/interrupt.h>
321da177e4SLinus Torvalds #include <linux/capability.h>
331da177e4SLinus Torvalds #include <linux/skbuff.h>
341da177e4SLinus Torvalds #include <linux/init.h>
351da177e4SLinus Torvalds #include <linux/security.h>
366756ae4bSStephen Hemminger #include <linux/mutex.h>
371823730fSThomas Graf #include <linux/if_addr.h>
3877162022SJohn Fastabend #include <linux/if_bridge.h>
39ebc08a6fSWilliams, Mitch A #include <linux/pci.h>
4077162022SJohn Fastabend #include <linux/etherdevice.h>
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds #include <asm/uaccess.h>
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds #include <linux/inet.h>
451da177e4SLinus Torvalds #include <linux/netdevice.h>
461da177e4SLinus Torvalds #include <net/ip.h>
471da177e4SLinus Torvalds #include <net/protocol.h>
481da177e4SLinus Torvalds #include <net/arp.h>
491da177e4SLinus Torvalds #include <net/route.h>
501da177e4SLinus Torvalds #include <net/udp.h>
511da177e4SLinus Torvalds #include <net/sock.h>
521da177e4SLinus Torvalds #include <net/pkt_sched.h>
5314c0b97dSThomas Graf #include <net/fib_rules.h>
54e2849863SThomas Graf #include <net/rtnetlink.h>
5530ffee84SJohannes Berg #include <net/net_namespace.h>
561da177e4SLinus Torvalds 
57e0d087afSEric Dumazet struct rtnl_link {
58e2849863SThomas Graf 	rtnl_doit_func		doit;
59e2849863SThomas Graf 	rtnl_dumpit_func	dumpit;
60c7ac8679SGreg Rose 	rtnl_calcit_func 	calcit;
61e2849863SThomas Graf };
62e2849863SThomas Graf 
636756ae4bSStephen Hemminger static DEFINE_MUTEX(rtnl_mutex);
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds void rtnl_lock(void)
661da177e4SLinus Torvalds {
676756ae4bSStephen Hemminger 	mutex_lock(&rtnl_mutex);
681da177e4SLinus Torvalds }
69e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_lock);
701da177e4SLinus Torvalds 
716756ae4bSStephen Hemminger void __rtnl_unlock(void)
721da177e4SLinus Torvalds {
736756ae4bSStephen Hemminger 	mutex_unlock(&rtnl_mutex);
741da177e4SLinus Torvalds }
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds void rtnl_unlock(void)
771da177e4SLinus Torvalds {
7858ec3b4dSHerbert Xu 	/* This fellow will unlock it for us. */
791da177e4SLinus Torvalds 	netdev_run_todo();
801da177e4SLinus Torvalds }
81e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unlock);
821da177e4SLinus Torvalds 
836756ae4bSStephen Hemminger int rtnl_trylock(void)
846756ae4bSStephen Hemminger {
856756ae4bSStephen Hemminger 	return mutex_trylock(&rtnl_mutex);
866756ae4bSStephen Hemminger }
87e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_trylock);
886756ae4bSStephen Hemminger 
89c9c1014bSPatrick McHardy int rtnl_is_locked(void)
90c9c1014bSPatrick McHardy {
91c9c1014bSPatrick McHardy 	return mutex_is_locked(&rtnl_mutex);
92c9c1014bSPatrick McHardy }
93e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_is_locked);
94c9c1014bSPatrick McHardy 
95a898def2SPaul E. McKenney #ifdef CONFIG_PROVE_LOCKING
96a898def2SPaul E. McKenney int lockdep_rtnl_is_held(void)
97a898def2SPaul E. McKenney {
98a898def2SPaul E. McKenney 	return lockdep_is_held(&rtnl_mutex);
99a898def2SPaul E. McKenney }
100a898def2SPaul E. McKenney EXPORT_SYMBOL(lockdep_rtnl_is_held);
101a898def2SPaul E. McKenney #endif /* #ifdef CONFIG_PROVE_LOCKING */
102a898def2SPaul E. McKenney 
10325239ceeSPatrick McHardy static struct rtnl_link *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
104e2849863SThomas Graf 
105e2849863SThomas Graf static inline int rtm_msgindex(int msgtype)
106e2849863SThomas Graf {
107e2849863SThomas Graf 	int msgindex = msgtype - RTM_BASE;
108e2849863SThomas Graf 
109e2849863SThomas Graf 	/*
110e2849863SThomas Graf 	 * msgindex < 0 implies someone tried to register a netlink
111e2849863SThomas Graf 	 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
112e2849863SThomas Graf 	 * the message type has not been added to linux/rtnetlink.h
113e2849863SThomas Graf 	 */
114e2849863SThomas Graf 	BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
115e2849863SThomas Graf 
116e2849863SThomas Graf 	return msgindex;
117e2849863SThomas Graf }
118e2849863SThomas Graf 
119e2849863SThomas Graf static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex)
120e2849863SThomas Graf {
121e2849863SThomas Graf 	struct rtnl_link *tab;
122e2849863SThomas Graf 
12325239ceeSPatrick McHardy 	if (protocol <= RTNL_FAMILY_MAX)
124e2849863SThomas Graf 		tab = rtnl_msg_handlers[protocol];
1250f87b1ddSPatrick McHardy 	else
1260f87b1ddSPatrick McHardy 		tab = NULL;
1270f87b1ddSPatrick McHardy 
12851057f2fSThomas Graf 	if (tab == NULL || tab[msgindex].doit == NULL)
129e2849863SThomas Graf 		tab = rtnl_msg_handlers[PF_UNSPEC];
130e2849863SThomas Graf 
131c80bbeaeSHans Zhang 	return tab[msgindex].doit;
132e2849863SThomas Graf }
133e2849863SThomas Graf 
134e2849863SThomas Graf static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex)
135e2849863SThomas Graf {
136e2849863SThomas Graf 	struct rtnl_link *tab;
137e2849863SThomas Graf 
13825239ceeSPatrick McHardy 	if (protocol <= RTNL_FAMILY_MAX)
139e2849863SThomas Graf 		tab = rtnl_msg_handlers[protocol];
1400f87b1ddSPatrick McHardy 	else
1410f87b1ddSPatrick McHardy 		tab = NULL;
1420f87b1ddSPatrick McHardy 
14351057f2fSThomas Graf 	if (tab == NULL || tab[msgindex].dumpit == NULL)
144e2849863SThomas Graf 		tab = rtnl_msg_handlers[PF_UNSPEC];
145e2849863SThomas Graf 
146c80bbeaeSHans Zhang 	return tab[msgindex].dumpit;
147e2849863SThomas Graf }
148e2849863SThomas Graf 
149c7ac8679SGreg Rose static rtnl_calcit_func rtnl_get_calcit(int protocol, int msgindex)
150c7ac8679SGreg Rose {
151c7ac8679SGreg Rose 	struct rtnl_link *tab;
152c7ac8679SGreg Rose 
153c7ac8679SGreg Rose 	if (protocol <= RTNL_FAMILY_MAX)
154c7ac8679SGreg Rose 		tab = rtnl_msg_handlers[protocol];
155c7ac8679SGreg Rose 	else
156c7ac8679SGreg Rose 		tab = NULL;
157c7ac8679SGreg Rose 
158c7ac8679SGreg Rose 	if (tab == NULL || tab[msgindex].calcit == NULL)
159c7ac8679SGreg Rose 		tab = rtnl_msg_handlers[PF_UNSPEC];
160c7ac8679SGreg Rose 
161c80bbeaeSHans Zhang 	return tab[msgindex].calcit;
162c7ac8679SGreg Rose }
163c7ac8679SGreg Rose 
164e2849863SThomas Graf /**
165e2849863SThomas Graf  * __rtnl_register - Register a rtnetlink message type
166e2849863SThomas Graf  * @protocol: Protocol family or PF_UNSPEC
167e2849863SThomas Graf  * @msgtype: rtnetlink message type
168e2849863SThomas Graf  * @doit: Function pointer called for each request message
169e2849863SThomas Graf  * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
170c7ac8679SGreg Rose  * @calcit: Function pointer to calc size of dump message
171e2849863SThomas Graf  *
172e2849863SThomas Graf  * Registers the specified function pointers (at least one of them has
173e2849863SThomas Graf  * to be non-NULL) to be called whenever a request message for the
174e2849863SThomas Graf  * specified protocol family and message type is received.
175e2849863SThomas Graf  *
176e2849863SThomas Graf  * The special protocol family PF_UNSPEC may be used to define fallback
177e2849863SThomas Graf  * function pointers for the case when no entry for the specific protocol
178e2849863SThomas Graf  * family exists.
179e2849863SThomas Graf  *
180e2849863SThomas Graf  * Returns 0 on success or a negative error code.
181e2849863SThomas Graf  */
182e2849863SThomas Graf int __rtnl_register(int protocol, int msgtype,
183c7ac8679SGreg Rose 		    rtnl_doit_func doit, rtnl_dumpit_func dumpit,
184c7ac8679SGreg Rose 		    rtnl_calcit_func calcit)
185e2849863SThomas Graf {
186e2849863SThomas Graf 	struct rtnl_link *tab;
187e2849863SThomas Graf 	int msgindex;
188e2849863SThomas Graf 
18925239ceeSPatrick McHardy 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
190e2849863SThomas Graf 	msgindex = rtm_msgindex(msgtype);
191e2849863SThomas Graf 
192e2849863SThomas Graf 	tab = rtnl_msg_handlers[protocol];
193e2849863SThomas Graf 	if (tab == NULL) {
194e2849863SThomas Graf 		tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL);
195e2849863SThomas Graf 		if (tab == NULL)
196e2849863SThomas Graf 			return -ENOBUFS;
197e2849863SThomas Graf 
198e2849863SThomas Graf 		rtnl_msg_handlers[protocol] = tab;
199e2849863SThomas Graf 	}
200e2849863SThomas Graf 
201e2849863SThomas Graf 	if (doit)
202e2849863SThomas Graf 		tab[msgindex].doit = doit;
203e2849863SThomas Graf 
204e2849863SThomas Graf 	if (dumpit)
205e2849863SThomas Graf 		tab[msgindex].dumpit = dumpit;
206e2849863SThomas Graf 
207c7ac8679SGreg Rose 	if (calcit)
208c7ac8679SGreg Rose 		tab[msgindex].calcit = calcit;
209c7ac8679SGreg Rose 
210e2849863SThomas Graf 	return 0;
211e2849863SThomas Graf }
212e2849863SThomas Graf EXPORT_SYMBOL_GPL(__rtnl_register);
213e2849863SThomas Graf 
214e2849863SThomas Graf /**
215e2849863SThomas Graf  * rtnl_register - Register a rtnetlink message type
216e2849863SThomas Graf  *
217e2849863SThomas Graf  * Identical to __rtnl_register() but panics on failure. This is useful
218e2849863SThomas Graf  * as failure of this function is very unlikely, it can only happen due
219e2849863SThomas Graf  * to lack of memory when allocating the chain to store all message
220e2849863SThomas Graf  * handlers for a protocol. Meant for use in init functions where lack
22125985edcSLucas De Marchi  * of memory implies no sense in continuing.
222e2849863SThomas Graf  */
223e2849863SThomas Graf void rtnl_register(int protocol, int msgtype,
224c7ac8679SGreg Rose 		   rtnl_doit_func doit, rtnl_dumpit_func dumpit,
225c7ac8679SGreg Rose 		   rtnl_calcit_func calcit)
226e2849863SThomas Graf {
227c7ac8679SGreg Rose 	if (__rtnl_register(protocol, msgtype, doit, dumpit, calcit) < 0)
228e2849863SThomas Graf 		panic("Unable to register rtnetlink message handler, "
229e2849863SThomas Graf 		      "protocol = %d, message type = %d\n",
230e2849863SThomas Graf 		      protocol, msgtype);
231e2849863SThomas Graf }
232e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_register);
233e2849863SThomas Graf 
234e2849863SThomas Graf /**
235e2849863SThomas Graf  * rtnl_unregister - Unregister a rtnetlink message type
236e2849863SThomas Graf  * @protocol: Protocol family or PF_UNSPEC
237e2849863SThomas Graf  * @msgtype: rtnetlink message type
238e2849863SThomas Graf  *
239e2849863SThomas Graf  * Returns 0 on success or a negative error code.
240e2849863SThomas Graf  */
241e2849863SThomas Graf int rtnl_unregister(int protocol, int msgtype)
242e2849863SThomas Graf {
243e2849863SThomas Graf 	int msgindex;
244e2849863SThomas Graf 
24525239ceeSPatrick McHardy 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
246e2849863SThomas Graf 	msgindex = rtm_msgindex(msgtype);
247e2849863SThomas Graf 
248e2849863SThomas Graf 	if (rtnl_msg_handlers[protocol] == NULL)
249e2849863SThomas Graf 		return -ENOENT;
250e2849863SThomas Graf 
251e2849863SThomas Graf 	rtnl_msg_handlers[protocol][msgindex].doit = NULL;
252e2849863SThomas Graf 	rtnl_msg_handlers[protocol][msgindex].dumpit = NULL;
253e2849863SThomas Graf 
254e2849863SThomas Graf 	return 0;
255e2849863SThomas Graf }
256e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister);
257e2849863SThomas Graf 
258e2849863SThomas Graf /**
259e2849863SThomas Graf  * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
260e2849863SThomas Graf  * @protocol : Protocol family or PF_UNSPEC
261e2849863SThomas Graf  *
262e2849863SThomas Graf  * Identical to calling rtnl_unregster() for all registered message types
263e2849863SThomas Graf  * of a certain protocol family.
264e2849863SThomas Graf  */
265e2849863SThomas Graf void rtnl_unregister_all(int protocol)
266e2849863SThomas Graf {
26725239ceeSPatrick McHardy 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
268e2849863SThomas Graf 
269e2849863SThomas Graf 	kfree(rtnl_msg_handlers[protocol]);
270e2849863SThomas Graf 	rtnl_msg_handlers[protocol] = NULL;
271e2849863SThomas Graf }
272e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister_all);
2731da177e4SLinus Torvalds 
27438f7b870SPatrick McHardy static LIST_HEAD(link_ops);
27538f7b870SPatrick McHardy 
276c63044f0SEric Dumazet static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
277c63044f0SEric Dumazet {
278c63044f0SEric Dumazet 	const struct rtnl_link_ops *ops;
279c63044f0SEric Dumazet 
280c63044f0SEric Dumazet 	list_for_each_entry(ops, &link_ops, list) {
281c63044f0SEric Dumazet 		if (!strcmp(ops->kind, kind))
282c63044f0SEric Dumazet 			return ops;
283c63044f0SEric Dumazet 	}
284c63044f0SEric Dumazet 	return NULL;
285c63044f0SEric Dumazet }
286c63044f0SEric Dumazet 
28738f7b870SPatrick McHardy /**
28838f7b870SPatrick McHardy  * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
28938f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to register
29038f7b870SPatrick McHardy  *
29138f7b870SPatrick McHardy  * The caller must hold the rtnl_mutex. This function should be used
29238f7b870SPatrick McHardy  * by drivers that create devices during module initialization. It
29338f7b870SPatrick McHardy  * must be called before registering the devices.
29438f7b870SPatrick McHardy  *
29538f7b870SPatrick McHardy  * Returns 0 on success or a negative error code.
29638f7b870SPatrick McHardy  */
29738f7b870SPatrick McHardy int __rtnl_link_register(struct rtnl_link_ops *ops)
29838f7b870SPatrick McHardy {
299c63044f0SEric Dumazet 	if (rtnl_link_ops_get(ops->kind))
300c63044f0SEric Dumazet 		return -EEXIST;
301c63044f0SEric Dumazet 
3022d85cba2SPatrick McHardy 	if (!ops->dellink)
30323289a37SEric Dumazet 		ops->dellink = unregister_netdevice_queue;
3042d85cba2SPatrick McHardy 
30538f7b870SPatrick McHardy 	list_add_tail(&ops->list, &link_ops);
30638f7b870SPatrick McHardy 	return 0;
30738f7b870SPatrick McHardy }
30838f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_register);
30938f7b870SPatrick McHardy 
31038f7b870SPatrick McHardy /**
31138f7b870SPatrick McHardy  * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
31238f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to register
31338f7b870SPatrick McHardy  *
31438f7b870SPatrick McHardy  * Returns 0 on success or a negative error code.
31538f7b870SPatrick McHardy  */
31638f7b870SPatrick McHardy int rtnl_link_register(struct rtnl_link_ops *ops)
31738f7b870SPatrick McHardy {
31838f7b870SPatrick McHardy 	int err;
31938f7b870SPatrick McHardy 
32038f7b870SPatrick McHardy 	rtnl_lock();
32138f7b870SPatrick McHardy 	err = __rtnl_link_register(ops);
32238f7b870SPatrick McHardy 	rtnl_unlock();
32338f7b870SPatrick McHardy 	return err;
32438f7b870SPatrick McHardy }
32538f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_register);
32638f7b870SPatrick McHardy 
327669f87baSPavel Emelyanov static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
328669f87baSPavel Emelyanov {
329669f87baSPavel Emelyanov 	struct net_device *dev;
33023289a37SEric Dumazet 	LIST_HEAD(list_kill);
33123289a37SEric Dumazet 
332669f87baSPavel Emelyanov 	for_each_netdev(net, dev) {
33323289a37SEric Dumazet 		if (dev->rtnl_link_ops == ops)
33423289a37SEric Dumazet 			ops->dellink(dev, &list_kill);
335669f87baSPavel Emelyanov 	}
33623289a37SEric Dumazet 	unregister_netdevice_many(&list_kill);
337669f87baSPavel Emelyanov }
338669f87baSPavel Emelyanov 
33938f7b870SPatrick McHardy /**
34038f7b870SPatrick McHardy  * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
34138f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to unregister
34238f7b870SPatrick McHardy  *
3432d85cba2SPatrick McHardy  * The caller must hold the rtnl_mutex.
34438f7b870SPatrick McHardy  */
34538f7b870SPatrick McHardy void __rtnl_link_unregister(struct rtnl_link_ops *ops)
34638f7b870SPatrick McHardy {
347881d966bSEric W. Biederman 	struct net *net;
3482d85cba2SPatrick McHardy 
349881d966bSEric W. Biederman 	for_each_net(net) {
350669f87baSPavel Emelyanov 		__rtnl_kill_links(net, ops);
351881d966bSEric W. Biederman 	}
35238f7b870SPatrick McHardy 	list_del(&ops->list);
35338f7b870SPatrick McHardy }
35438f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
35538f7b870SPatrick McHardy 
35638f7b870SPatrick McHardy /**
35738f7b870SPatrick McHardy  * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
35838f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to unregister
35938f7b870SPatrick McHardy  */
36038f7b870SPatrick McHardy void rtnl_link_unregister(struct rtnl_link_ops *ops)
36138f7b870SPatrick McHardy {
36238f7b870SPatrick McHardy 	rtnl_lock();
36338f7b870SPatrick McHardy 	__rtnl_link_unregister(ops);
36438f7b870SPatrick McHardy 	rtnl_unlock();
36538f7b870SPatrick McHardy }
36638f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_unregister);
36738f7b870SPatrick McHardy 
36838f7b870SPatrick McHardy static size_t rtnl_link_get_size(const struct net_device *dev)
36938f7b870SPatrick McHardy {
37038f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
37138f7b870SPatrick McHardy 	size_t size;
37238f7b870SPatrick McHardy 
37338f7b870SPatrick McHardy 	if (!ops)
37438f7b870SPatrick McHardy 		return 0;
37538f7b870SPatrick McHardy 
376369cf77aSThomas Graf 	size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
377369cf77aSThomas Graf 	       nla_total_size(strlen(ops->kind) + 1);  /* IFLA_INFO_KIND */
37838f7b870SPatrick McHardy 
37938f7b870SPatrick McHardy 	if (ops->get_size)
38038f7b870SPatrick McHardy 		/* IFLA_INFO_DATA + nested data */
381369cf77aSThomas Graf 		size += nla_total_size(sizeof(struct nlattr)) +
38238f7b870SPatrick McHardy 			ops->get_size(dev);
38338f7b870SPatrick McHardy 
38438f7b870SPatrick McHardy 	if (ops->get_xstats_size)
385369cf77aSThomas Graf 		/* IFLA_INFO_XSTATS */
386369cf77aSThomas Graf 		size += nla_total_size(ops->get_xstats_size(dev));
38738f7b870SPatrick McHardy 
38838f7b870SPatrick McHardy 	return size;
38938f7b870SPatrick McHardy }
39038f7b870SPatrick McHardy 
391f8ff182cSThomas Graf static LIST_HEAD(rtnl_af_ops);
392f8ff182cSThomas Graf 
393f8ff182cSThomas Graf static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
394f8ff182cSThomas Graf {
395f8ff182cSThomas Graf 	const struct rtnl_af_ops *ops;
396f8ff182cSThomas Graf 
397f8ff182cSThomas Graf 	list_for_each_entry(ops, &rtnl_af_ops, list) {
398f8ff182cSThomas Graf 		if (ops->family == family)
399f8ff182cSThomas Graf 			return ops;
400f8ff182cSThomas Graf 	}
401f8ff182cSThomas Graf 
402f8ff182cSThomas Graf 	return NULL;
403f8ff182cSThomas Graf }
404f8ff182cSThomas Graf 
405f8ff182cSThomas Graf /**
406f8ff182cSThomas Graf  * __rtnl_af_register - Register rtnl_af_ops with rtnetlink.
407f8ff182cSThomas Graf  * @ops: struct rtnl_af_ops * to register
408f8ff182cSThomas Graf  *
409f8ff182cSThomas Graf  * The caller must hold the rtnl_mutex.
410f8ff182cSThomas Graf  *
411f8ff182cSThomas Graf  * Returns 0 on success or a negative error code.
412f8ff182cSThomas Graf  */
413f8ff182cSThomas Graf int __rtnl_af_register(struct rtnl_af_ops *ops)
414f8ff182cSThomas Graf {
415f8ff182cSThomas Graf 	list_add_tail(&ops->list, &rtnl_af_ops);
416f8ff182cSThomas Graf 	return 0;
417f8ff182cSThomas Graf }
418f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(__rtnl_af_register);
419f8ff182cSThomas Graf 
420f8ff182cSThomas Graf /**
421f8ff182cSThomas Graf  * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
422f8ff182cSThomas Graf  * @ops: struct rtnl_af_ops * to register
423f8ff182cSThomas Graf  *
424f8ff182cSThomas Graf  * Returns 0 on success or a negative error code.
425f8ff182cSThomas Graf  */
426f8ff182cSThomas Graf int rtnl_af_register(struct rtnl_af_ops *ops)
427f8ff182cSThomas Graf {
428f8ff182cSThomas Graf 	int err;
429f8ff182cSThomas Graf 
430f8ff182cSThomas Graf 	rtnl_lock();
431f8ff182cSThomas Graf 	err = __rtnl_af_register(ops);
432f8ff182cSThomas Graf 	rtnl_unlock();
433f8ff182cSThomas Graf 	return err;
434f8ff182cSThomas Graf }
435f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_register);
436f8ff182cSThomas Graf 
437f8ff182cSThomas Graf /**
438f8ff182cSThomas Graf  * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
439f8ff182cSThomas Graf  * @ops: struct rtnl_af_ops * to unregister
440f8ff182cSThomas Graf  *
441f8ff182cSThomas Graf  * The caller must hold the rtnl_mutex.
442f8ff182cSThomas Graf  */
443f8ff182cSThomas Graf void __rtnl_af_unregister(struct rtnl_af_ops *ops)
444f8ff182cSThomas Graf {
445f8ff182cSThomas Graf 	list_del(&ops->list);
446f8ff182cSThomas Graf }
447f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(__rtnl_af_unregister);
448f8ff182cSThomas Graf 
449f8ff182cSThomas Graf /**
450f8ff182cSThomas Graf  * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
451f8ff182cSThomas Graf  * @ops: struct rtnl_af_ops * to unregister
452f8ff182cSThomas Graf  */
453f8ff182cSThomas Graf void rtnl_af_unregister(struct rtnl_af_ops *ops)
454f8ff182cSThomas Graf {
455f8ff182cSThomas Graf 	rtnl_lock();
456f8ff182cSThomas Graf 	__rtnl_af_unregister(ops);
457f8ff182cSThomas Graf 	rtnl_unlock();
458f8ff182cSThomas Graf }
459f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_unregister);
460f8ff182cSThomas Graf 
461f8ff182cSThomas Graf static size_t rtnl_link_get_af_size(const struct net_device *dev)
462f8ff182cSThomas Graf {
463f8ff182cSThomas Graf 	struct rtnl_af_ops *af_ops;
464f8ff182cSThomas Graf 	size_t size;
465f8ff182cSThomas Graf 
466f8ff182cSThomas Graf 	/* IFLA_AF_SPEC */
467f8ff182cSThomas Graf 	size = nla_total_size(sizeof(struct nlattr));
468f8ff182cSThomas Graf 
469f8ff182cSThomas Graf 	list_for_each_entry(af_ops, &rtnl_af_ops, list) {
470f8ff182cSThomas Graf 		if (af_ops->get_link_af_size) {
471f8ff182cSThomas Graf 			/* AF_* + nested data */
472f8ff182cSThomas Graf 			size += nla_total_size(sizeof(struct nlattr)) +
473f8ff182cSThomas Graf 				af_ops->get_link_af_size(dev);
474f8ff182cSThomas Graf 		}
475f8ff182cSThomas Graf 	}
476f8ff182cSThomas Graf 
477f8ff182cSThomas Graf 	return size;
478f8ff182cSThomas Graf }
479f8ff182cSThomas Graf 
48038f7b870SPatrick McHardy static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
48138f7b870SPatrick McHardy {
48238f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
48338f7b870SPatrick McHardy 	struct nlattr *linkinfo, *data;
48438f7b870SPatrick McHardy 	int err = -EMSGSIZE;
48538f7b870SPatrick McHardy 
48638f7b870SPatrick McHardy 	linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
48738f7b870SPatrick McHardy 	if (linkinfo == NULL)
48838f7b870SPatrick McHardy 		goto out;
48938f7b870SPatrick McHardy 
49038f7b870SPatrick McHardy 	if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
49138f7b870SPatrick McHardy 		goto err_cancel_link;
49238f7b870SPatrick McHardy 	if (ops->fill_xstats) {
49338f7b870SPatrick McHardy 		err = ops->fill_xstats(skb, dev);
49438f7b870SPatrick McHardy 		if (err < 0)
49538f7b870SPatrick McHardy 			goto err_cancel_link;
49638f7b870SPatrick McHardy 	}
49738f7b870SPatrick McHardy 	if (ops->fill_info) {
49838f7b870SPatrick McHardy 		data = nla_nest_start(skb, IFLA_INFO_DATA);
49938f7b870SPatrick McHardy 		if (data == NULL)
50038f7b870SPatrick McHardy 			goto err_cancel_link;
50138f7b870SPatrick McHardy 		err = ops->fill_info(skb, dev);
50238f7b870SPatrick McHardy 		if (err < 0)
50338f7b870SPatrick McHardy 			goto err_cancel_data;
50438f7b870SPatrick McHardy 		nla_nest_end(skb, data);
50538f7b870SPatrick McHardy 	}
50638f7b870SPatrick McHardy 
50738f7b870SPatrick McHardy 	nla_nest_end(skb, linkinfo);
50838f7b870SPatrick McHardy 	return 0;
50938f7b870SPatrick McHardy 
51038f7b870SPatrick McHardy err_cancel_data:
51138f7b870SPatrick McHardy 	nla_nest_cancel(skb, data);
51238f7b870SPatrick McHardy err_cancel_link:
51338f7b870SPatrick McHardy 	nla_nest_cancel(skb, linkinfo);
51438f7b870SPatrick McHardy out:
51538f7b870SPatrick McHardy 	return err;
51638f7b870SPatrick McHardy }
51738f7b870SPatrick McHardy 
518db46edc6SThomas Graf static const int rtm_min[RTM_NR_FAMILIES] =
5191da177e4SLinus Torvalds {
520f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWLINK)]      = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
521f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWADDR)]      = NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
522f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWROUTE)]     = NLMSG_LENGTH(sizeof(struct rtmsg)),
52314c0b97dSThomas Graf 	[RTM_FAM(RTM_NEWRULE)]      = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)),
524f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWQDISC)]     = NLMSG_LENGTH(sizeof(struct tcmsg)),
525f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWTCLASS)]    = NLMSG_LENGTH(sizeof(struct tcmsg)),
526f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWTFILTER)]   = NLMSG_LENGTH(sizeof(struct tcmsg)),
527f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWACTION)]    = NLMSG_LENGTH(sizeof(struct tcamsg)),
528f90a0a74SThomas Graf 	[RTM_FAM(RTM_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
529f90a0a74SThomas Graf 	[RTM_FAM(RTM_GETANYCAST)]   = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
5301da177e4SLinus Torvalds };
5311da177e4SLinus Torvalds 
532db46edc6SThomas Graf static const int rta_max[RTM_NR_FAMILIES] =
5331da177e4SLinus Torvalds {
534f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWLINK)]      = IFLA_MAX,
535f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWADDR)]      = IFA_MAX,
536f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWROUTE)]     = RTA_MAX,
53714c0b97dSThomas Graf 	[RTM_FAM(RTM_NEWRULE)]      = FRA_MAX,
538f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWQDISC)]     = TCA_MAX,
539f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWTCLASS)]    = TCA_MAX,
540f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWTFILTER)]   = TCA_MAX,
541f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWACTION)]    = TCAA_MAX,
5421da177e4SLinus Torvalds };
5431da177e4SLinus Torvalds 
54495c96174SEric Dumazet int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
5451da177e4SLinus Torvalds {
54697c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
5471da177e4SLinus Torvalds 	int err = 0;
5481da177e4SLinus Torvalds 
549ac6d439dSPatrick McHardy 	NETLINK_CB(skb).dst_group = group;
5501da177e4SLinus Torvalds 	if (echo)
5511da177e4SLinus Torvalds 		atomic_inc(&skb->users);
5521da177e4SLinus Torvalds 	netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
5531da177e4SLinus Torvalds 	if (echo)
5541da177e4SLinus Torvalds 		err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
5551da177e4SLinus Torvalds 	return err;
5561da177e4SLinus Torvalds }
5571da177e4SLinus Torvalds 
55897c53cacSDenis V. Lunev int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
5592942e900SThomas Graf {
56097c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
56197c53cacSDenis V. Lunev 
5622942e900SThomas Graf 	return nlmsg_unicast(rtnl, skb, pid);
5632942e900SThomas Graf }
564e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unicast);
5652942e900SThomas Graf 
5661ce85fe4SPablo Neira Ayuso void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
56797676b6bSThomas Graf 		 struct nlmsghdr *nlh, gfp_t flags)
56897676b6bSThomas Graf {
56997c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
57097676b6bSThomas Graf 	int report = 0;
57197676b6bSThomas Graf 
57297676b6bSThomas Graf 	if (nlh)
57397676b6bSThomas Graf 		report = nlmsg_report(nlh);
57497676b6bSThomas Graf 
5751ce85fe4SPablo Neira Ayuso 	nlmsg_notify(rtnl, skb, pid, group, report, flags);
57697676b6bSThomas Graf }
577e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_notify);
57897676b6bSThomas Graf 
57997c53cacSDenis V. Lunev void rtnl_set_sk_err(struct net *net, u32 group, int error)
58097676b6bSThomas Graf {
58197c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
58297c53cacSDenis V. Lunev 
58397676b6bSThomas Graf 	netlink_set_err(rtnl, 0, group, error);
58497676b6bSThomas Graf }
585e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_set_sk_err);
58697676b6bSThomas Graf 
5871da177e4SLinus Torvalds int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
5881da177e4SLinus Torvalds {
5892d7202bfSThomas Graf 	struct nlattr *mx;
5902d7202bfSThomas Graf 	int i, valid = 0;
5911da177e4SLinus Torvalds 
5922d7202bfSThomas Graf 	mx = nla_nest_start(skb, RTA_METRICS);
5932d7202bfSThomas Graf 	if (mx == NULL)
5942d7202bfSThomas Graf 		return -ENOBUFS;
5952d7202bfSThomas Graf 
5961da177e4SLinus Torvalds 	for (i = 0; i < RTAX_MAX; i++) {
5972d7202bfSThomas Graf 		if (metrics[i]) {
5982d7202bfSThomas Graf 			valid++;
599a6574349SDavid S. Miller 			if (nla_put_u32(skb, i+1, metrics[i]))
600a6574349SDavid S. Miller 				goto nla_put_failure;
6011da177e4SLinus Torvalds 		}
6022d7202bfSThomas Graf 	}
6031da177e4SLinus Torvalds 
604a57d27fcSDavid S. Miller 	if (!valid) {
605a57d27fcSDavid S. Miller 		nla_nest_cancel(skb, mx);
606a57d27fcSDavid S. Miller 		return 0;
607a57d27fcSDavid S. Miller 	}
6082d7202bfSThomas Graf 
6092d7202bfSThomas Graf 	return nla_nest_end(skb, mx);
6102d7202bfSThomas Graf 
6112d7202bfSThomas Graf nla_put_failure:
612bc3ed28cSThomas Graf 	nla_nest_cancel(skb, mx);
613bc3ed28cSThomas Graf 	return -EMSGSIZE;
6141da177e4SLinus Torvalds }
615e0d087afSEric Dumazet EXPORT_SYMBOL(rtnetlink_put_metrics);
6161da177e4SLinus Torvalds 
617e3703b3dSThomas Graf int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
61887a50699SDavid S. Miller 		       long expires, u32 error)
619e3703b3dSThomas Graf {
620e3703b3dSThomas Graf 	struct rta_cacheinfo ci = {
621a399a805SEric Dumazet 		.rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse),
622e3703b3dSThomas Graf 		.rta_used = dst->__use,
623e3703b3dSThomas Graf 		.rta_clntref = atomic_read(&(dst->__refcnt)),
624e3703b3dSThomas Graf 		.rta_error = error,
625e3703b3dSThomas Graf 		.rta_id =  id,
626e3703b3dSThomas Graf 	};
627e3703b3dSThomas Graf 
6288253947eSLi Wei 	if (expires) {
6298253947eSLi Wei 		unsigned long clock;
630e3703b3dSThomas Graf 
6318253947eSLi Wei 		clock = jiffies_to_clock_t(abs(expires));
6328253947eSLi Wei 		clock = min_t(unsigned long, clock, INT_MAX);
6338253947eSLi Wei 		ci.rta_expires = (expires > 0) ? clock : -clock;
6348253947eSLi Wei 	}
635e3703b3dSThomas Graf 	return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
636e3703b3dSThomas Graf }
637e3703b3dSThomas Graf EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
6381da177e4SLinus Torvalds 
63993b2d4a2SDavid S. Miller static void set_operstate(struct net_device *dev, unsigned char transition)
640b00055aaSStefan Rompf {
641b00055aaSStefan Rompf 	unsigned char operstate = dev->operstate;
642b00055aaSStefan Rompf 
643b00055aaSStefan Rompf 	switch (transition) {
644b00055aaSStefan Rompf 	case IF_OPER_UP:
645b00055aaSStefan Rompf 		if ((operstate == IF_OPER_DORMANT ||
646b00055aaSStefan Rompf 		     operstate == IF_OPER_UNKNOWN) &&
647b00055aaSStefan Rompf 		    !netif_dormant(dev))
648b00055aaSStefan Rompf 			operstate = IF_OPER_UP;
649b00055aaSStefan Rompf 		break;
650b00055aaSStefan Rompf 
651b00055aaSStefan Rompf 	case IF_OPER_DORMANT:
652b00055aaSStefan Rompf 		if (operstate == IF_OPER_UP ||
653b00055aaSStefan Rompf 		    operstate == IF_OPER_UNKNOWN)
654b00055aaSStefan Rompf 			operstate = IF_OPER_DORMANT;
655b00055aaSStefan Rompf 		break;
6563ff50b79SStephen Hemminger 	}
657b00055aaSStefan Rompf 
658b00055aaSStefan Rompf 	if (dev->operstate != operstate) {
659b00055aaSStefan Rompf 		write_lock_bh(&dev_base_lock);
660b00055aaSStefan Rompf 		dev->operstate = operstate;
661b00055aaSStefan Rompf 		write_unlock_bh(&dev_base_lock);
662b00055aaSStefan Rompf 		netdev_state_change(dev);
66393b2d4a2SDavid S. Miller 	}
664b00055aaSStefan Rompf }
665b00055aaSStefan Rompf 
666b1beb681SJiri Benc static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
667b1beb681SJiri Benc {
668b1beb681SJiri Benc 	return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
669b1beb681SJiri Benc 	       (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
670b1beb681SJiri Benc }
671b1beb681SJiri Benc 
6723729d502SPatrick McHardy static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
6733729d502SPatrick McHardy 					   const struct ifinfomsg *ifm)
6743729d502SPatrick McHardy {
6753729d502SPatrick McHardy 	unsigned int flags = ifm->ifi_flags;
6763729d502SPatrick McHardy 
6773729d502SPatrick McHardy 	/* bugwards compatibility: ifi_change == 0 is treated as ~0 */
6783729d502SPatrick McHardy 	if (ifm->ifi_change)
6793729d502SPatrick McHardy 		flags = (flags & ifm->ifi_change) |
680b1beb681SJiri Benc 			(rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
6813729d502SPatrick McHardy 
6823729d502SPatrick McHardy 	return flags;
6833729d502SPatrick McHardy }
6843729d502SPatrick McHardy 
685b60c5115SThomas Graf static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
686be1f3c2cSBen Hutchings 				 const struct rtnl_link_stats64 *b)
6871da177e4SLinus Torvalds {
688b60c5115SThomas Graf 	a->rx_packets = b->rx_packets;
689b60c5115SThomas Graf 	a->tx_packets = b->tx_packets;
690b60c5115SThomas Graf 	a->rx_bytes = b->rx_bytes;
691b60c5115SThomas Graf 	a->tx_bytes = b->tx_bytes;
692b60c5115SThomas Graf 	a->rx_errors = b->rx_errors;
693b60c5115SThomas Graf 	a->tx_errors = b->tx_errors;
694b60c5115SThomas Graf 	a->rx_dropped = b->rx_dropped;
695b60c5115SThomas Graf 	a->tx_dropped = b->tx_dropped;
696b60c5115SThomas Graf 
697b60c5115SThomas Graf 	a->multicast = b->multicast;
698b60c5115SThomas Graf 	a->collisions = b->collisions;
699b60c5115SThomas Graf 
700b60c5115SThomas Graf 	a->rx_length_errors = b->rx_length_errors;
701b60c5115SThomas Graf 	a->rx_over_errors = b->rx_over_errors;
702b60c5115SThomas Graf 	a->rx_crc_errors = b->rx_crc_errors;
703b60c5115SThomas Graf 	a->rx_frame_errors = b->rx_frame_errors;
704b60c5115SThomas Graf 	a->rx_fifo_errors = b->rx_fifo_errors;
705b60c5115SThomas Graf 	a->rx_missed_errors = b->rx_missed_errors;
706b60c5115SThomas Graf 
707b60c5115SThomas Graf 	a->tx_aborted_errors = b->tx_aborted_errors;
708b60c5115SThomas Graf 	a->tx_carrier_errors = b->tx_carrier_errors;
709b60c5115SThomas Graf 	a->tx_fifo_errors = b->tx_fifo_errors;
710b60c5115SThomas Graf 	a->tx_heartbeat_errors = b->tx_heartbeat_errors;
711b60c5115SThomas Graf 	a->tx_window_errors = b->tx_window_errors;
712b60c5115SThomas Graf 
713b60c5115SThomas Graf 	a->rx_compressed = b->rx_compressed;
714b60c5115SThomas Graf 	a->tx_compressed = b->tx_compressed;
71510708f37SJan Engelhardt }
71610708f37SJan Engelhardt 
717be1f3c2cSBen Hutchings static void copy_rtnl_link_stats64(void *v, const struct rtnl_link_stats64 *b)
71810708f37SJan Engelhardt {
719afdcba37SEric Dumazet 	memcpy(v, b, sizeof(*b));
72010708f37SJan Engelhardt }
721b60c5115SThomas Graf 
722c02db8c6SChris Wright /* All VF info */
723115c9b81SGreg Rose static inline int rtnl_vfinfo_size(const struct net_device *dev,
724115c9b81SGreg Rose 				   u32 ext_filter_mask)
725ebc08a6fSWilliams, Mitch A {
726115c9b81SGreg Rose 	if (dev->dev.parent && dev_is_pci(dev->dev.parent) &&
727115c9b81SGreg Rose 	    (ext_filter_mask & RTEXT_FILTER_VF)) {
728c02db8c6SChris Wright 		int num_vfs = dev_num_vf(dev->dev.parent);
729045de01aSScott Feldman 		size_t size = nla_total_size(sizeof(struct nlattr));
730045de01aSScott Feldman 		size += nla_total_size(num_vfs * sizeof(struct nlattr));
731045de01aSScott Feldman 		size += num_vfs *
732045de01aSScott Feldman 			(nla_total_size(sizeof(struct ifla_vf_mac)) +
733045de01aSScott Feldman 			 nla_total_size(sizeof(struct ifla_vf_vlan)) +
7345f8444a3SGreg Rose 			 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
7355f8444a3SGreg Rose 			 nla_total_size(sizeof(struct ifla_vf_spoofchk)));
736c02db8c6SChris Wright 		return size;
737c02db8c6SChris Wright 	} else
738ebc08a6fSWilliams, Mitch A 		return 0;
739ebc08a6fSWilliams, Mitch A }
740ebc08a6fSWilliams, Mitch A 
74157b61080SScott Feldman static size_t rtnl_port_size(const struct net_device *dev)
74257b61080SScott Feldman {
74357b61080SScott Feldman 	size_t port_size = nla_total_size(4)		/* PORT_VF */
74457b61080SScott Feldman 		+ nla_total_size(PORT_PROFILE_MAX)	/* PORT_PROFILE */
74557b61080SScott Feldman 		+ nla_total_size(sizeof(struct ifla_port_vsi))
74657b61080SScott Feldman 							/* PORT_VSI_TYPE */
74757b61080SScott Feldman 		+ nla_total_size(PORT_UUID_MAX)		/* PORT_INSTANCE_UUID */
74857b61080SScott Feldman 		+ nla_total_size(PORT_UUID_MAX)		/* PORT_HOST_UUID */
74957b61080SScott Feldman 		+ nla_total_size(1)			/* PROT_VDP_REQUEST */
75057b61080SScott Feldman 		+ nla_total_size(2);			/* PORT_VDP_RESPONSE */
75157b61080SScott Feldman 	size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
75257b61080SScott Feldman 	size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
75357b61080SScott Feldman 		+ port_size;
75457b61080SScott Feldman 	size_t port_self_size = nla_total_size(sizeof(struct nlattr))
75557b61080SScott Feldman 		+ port_size;
75657b61080SScott Feldman 
75757b61080SScott Feldman 	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent)
75857b61080SScott Feldman 		return 0;
75957b61080SScott Feldman 	if (dev_num_vf(dev->dev.parent))
76057b61080SScott Feldman 		return port_self_size + vf_ports_size +
76157b61080SScott Feldman 			vf_port_size * dev_num_vf(dev->dev.parent);
76257b61080SScott Feldman 	else
76357b61080SScott Feldman 		return port_self_size;
76457b61080SScott Feldman }
76557b61080SScott Feldman 
766115c9b81SGreg Rose static noinline size_t if_nlmsg_size(const struct net_device *dev,
767115c9b81SGreg Rose 				     u32 ext_filter_mask)
768339bf98fSThomas Graf {
769339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
770339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
7710b815a1aSStephen Hemminger 	       + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
772339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
773339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rtnl_link_ifmap))
774339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rtnl_link_stats))
775adcfe196SJan Engelhardt 	       + nla_total_size(sizeof(struct rtnl_link_stats64))
776339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
777339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
778339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_TXQLEN */
779339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_WEIGHT */
780339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MTU */
781339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_LINK */
782339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MASTER */
7839a57247fSJiri Pirko 	       + nla_total_size(1) /* IFLA_CARRIER */
784edbc0bb3SBen Greear 	       + nla_total_size(4) /* IFLA_PROMISCUITY */
78576ff5cc9SJiri Pirko 	       + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
78676ff5cc9SJiri Pirko 	       + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
787339bf98fSThomas Graf 	       + nla_total_size(1) /* IFLA_OPERSTATE */
78838f7b870SPatrick McHardy 	       + nla_total_size(1) /* IFLA_LINKMODE */
789115c9b81SGreg Rose 	       + nla_total_size(ext_filter_mask
790115c9b81SGreg Rose 			        & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
791115c9b81SGreg Rose 	       + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
79257b61080SScott Feldman 	       + rtnl_port_size(dev) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
793f8ff182cSThomas Graf 	       + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
794f8ff182cSThomas Graf 	       + rtnl_link_get_af_size(dev); /* IFLA_AF_SPEC */
795339bf98fSThomas Graf }
796339bf98fSThomas Graf 
79757b61080SScott Feldman static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
79857b61080SScott Feldman {
79957b61080SScott Feldman 	struct nlattr *vf_ports;
80057b61080SScott Feldman 	struct nlattr *vf_port;
80157b61080SScott Feldman 	int vf;
80257b61080SScott Feldman 	int err;
80357b61080SScott Feldman 
80457b61080SScott Feldman 	vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
80557b61080SScott Feldman 	if (!vf_ports)
80657b61080SScott Feldman 		return -EMSGSIZE;
80757b61080SScott Feldman 
80857b61080SScott Feldman 	for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
80957b61080SScott Feldman 		vf_port = nla_nest_start(skb, IFLA_VF_PORT);
8108ca94183SScott Feldman 		if (!vf_port)
8118ca94183SScott Feldman 			goto nla_put_failure;
812a6574349SDavid S. Miller 		if (nla_put_u32(skb, IFLA_PORT_VF, vf))
813a6574349SDavid S. Miller 			goto nla_put_failure;
81457b61080SScott Feldman 		err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
8158ca94183SScott Feldman 		if (err == -EMSGSIZE)
8168ca94183SScott Feldman 			goto nla_put_failure;
81757b61080SScott Feldman 		if (err) {
81857b61080SScott Feldman 			nla_nest_cancel(skb, vf_port);
81957b61080SScott Feldman 			continue;
82057b61080SScott Feldman 		}
82157b61080SScott Feldman 		nla_nest_end(skb, vf_port);
82257b61080SScott Feldman 	}
82357b61080SScott Feldman 
82457b61080SScott Feldman 	nla_nest_end(skb, vf_ports);
82557b61080SScott Feldman 
82657b61080SScott Feldman 	return 0;
8278ca94183SScott Feldman 
8288ca94183SScott Feldman nla_put_failure:
8298ca94183SScott Feldman 	nla_nest_cancel(skb, vf_ports);
8308ca94183SScott Feldman 	return -EMSGSIZE;
83157b61080SScott Feldman }
83257b61080SScott Feldman 
83357b61080SScott Feldman static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
83457b61080SScott Feldman {
83557b61080SScott Feldman 	struct nlattr *port_self;
83657b61080SScott Feldman 	int err;
83757b61080SScott Feldman 
83857b61080SScott Feldman 	port_self = nla_nest_start(skb, IFLA_PORT_SELF);
83957b61080SScott Feldman 	if (!port_self)
84057b61080SScott Feldman 		return -EMSGSIZE;
84157b61080SScott Feldman 
84257b61080SScott Feldman 	err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
84357b61080SScott Feldman 	if (err) {
84457b61080SScott Feldman 		nla_nest_cancel(skb, port_self);
8458ca94183SScott Feldman 		return (err == -EMSGSIZE) ? err : 0;
84657b61080SScott Feldman 	}
84757b61080SScott Feldman 
84857b61080SScott Feldman 	nla_nest_end(skb, port_self);
84957b61080SScott Feldman 
85057b61080SScott Feldman 	return 0;
85157b61080SScott Feldman }
85257b61080SScott Feldman 
85357b61080SScott Feldman static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev)
85457b61080SScott Feldman {
85557b61080SScott Feldman 	int err;
85657b61080SScott Feldman 
85757b61080SScott Feldman 	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent)
85857b61080SScott Feldman 		return 0;
85957b61080SScott Feldman 
86057b61080SScott Feldman 	err = rtnl_port_self_fill(skb, dev);
86157b61080SScott Feldman 	if (err)
86257b61080SScott Feldman 		return err;
86357b61080SScott Feldman 
86457b61080SScott Feldman 	if (dev_num_vf(dev->dev.parent)) {
86557b61080SScott Feldman 		err = rtnl_vf_ports_fill(skb, dev);
86657b61080SScott Feldman 		if (err)
86757b61080SScott Feldman 			return err;
86857b61080SScott Feldman 	}
86957b61080SScott Feldman 
87057b61080SScott Feldman 	return 0;
87157b61080SScott Feldman }
87257b61080SScott Feldman 
873b60c5115SThomas Graf static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
874575c3e2aSPatrick McHardy 			    int type, u32 pid, u32 seq, u32 change,
875115c9b81SGreg Rose 			    unsigned int flags, u32 ext_filter_mask)
876b60c5115SThomas Graf {
877b60c5115SThomas Graf 	struct ifinfomsg *ifm;
8781da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
87928172739SEric Dumazet 	struct rtnl_link_stats64 temp;
880be1f3c2cSBen Hutchings 	const struct rtnl_link_stats64 *stats;
881f8ff182cSThomas Graf 	struct nlattr *attr, *af_spec;
882f8ff182cSThomas Graf 	struct rtnl_af_ops *af_ops;
883898e5061SJiri Pirko 	struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
8841da177e4SLinus Torvalds 
8852907c35fSEric Dumazet 	ASSERT_RTNL();
886b60c5115SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
887b60c5115SThomas Graf 	if (nlh == NULL)
88826932566SPatrick McHardy 		return -EMSGSIZE;
8891da177e4SLinus Torvalds 
890b60c5115SThomas Graf 	ifm = nlmsg_data(nlh);
891b60c5115SThomas Graf 	ifm->ifi_family = AF_UNSPEC;
892b60c5115SThomas Graf 	ifm->__ifi_pad = 0;
893b60c5115SThomas Graf 	ifm->ifi_type = dev->type;
894b60c5115SThomas Graf 	ifm->ifi_index = dev->ifindex;
895b60c5115SThomas Graf 	ifm->ifi_flags = dev_get_flags(dev);
896b60c5115SThomas Graf 	ifm->ifi_change = change;
8971da177e4SLinus Torvalds 
898a6574349SDavid S. Miller 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
899a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
900a6574349SDavid S. Miller 	    nla_put_u8(skb, IFLA_OPERSTATE,
901a6574349SDavid S. Miller 		       netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
902a6574349SDavid S. Miller 	    nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
903a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
904a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_GROUP, dev->group) ||
905edbc0bb3SBen Greear 	    nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
90676ff5cc9SJiri Pirko 	    nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
9071d69c2b3SMark A. Greer #ifdef CONFIG_RPS
90876ff5cc9SJiri Pirko 	    nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
9091d69c2b3SMark A. Greer #endif
910a6574349SDavid S. Miller 	    (dev->ifindex != dev->iflink &&
911a6574349SDavid S. Miller 	     nla_put_u32(skb, IFLA_LINK, dev->iflink)) ||
912898e5061SJiri Pirko 	    (upper_dev &&
913898e5061SJiri Pirko 	     nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) ||
9149a57247fSJiri Pirko 	    nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
915a6574349SDavid S. Miller 	    (dev->qdisc &&
916a6574349SDavid S. Miller 	     nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
917a6574349SDavid S. Miller 	    (dev->ifalias &&
918a6574349SDavid S. Miller 	     nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)))
919a6574349SDavid S. Miller 		goto nla_put_failure;
9200b815a1aSStephen Hemminger 
921b00055aaSStefan Rompf 	if (1) {
9221da177e4SLinus Torvalds 		struct rtnl_link_ifmap map = {
9231da177e4SLinus Torvalds 			.mem_start   = dev->mem_start,
9241da177e4SLinus Torvalds 			.mem_end     = dev->mem_end,
9251da177e4SLinus Torvalds 			.base_addr   = dev->base_addr,
9261da177e4SLinus Torvalds 			.irq         = dev->irq,
9271da177e4SLinus Torvalds 			.dma         = dev->dma,
9281da177e4SLinus Torvalds 			.port        = dev->if_port,
9291da177e4SLinus Torvalds 		};
930a6574349SDavid S. Miller 		if (nla_put(skb, IFLA_MAP, sizeof(map), &map))
931a6574349SDavid S. Miller 			goto nla_put_failure;
9321da177e4SLinus Torvalds 	}
9331da177e4SLinus Torvalds 
9341da177e4SLinus Torvalds 	if (dev->addr_len) {
935a6574349SDavid S. Miller 		if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
936a6574349SDavid S. Miller 		    nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
937a6574349SDavid S. Miller 			goto nla_put_failure;
9381da177e4SLinus Torvalds 	}
9391da177e4SLinus Torvalds 
940b60c5115SThomas Graf 	attr = nla_reserve(skb, IFLA_STATS,
941b60c5115SThomas Graf 			sizeof(struct rtnl_link_stats));
942b60c5115SThomas Graf 	if (attr == NULL)
943b60c5115SThomas Graf 		goto nla_put_failure;
944b60c5115SThomas Graf 
94528172739SEric Dumazet 	stats = dev_get_stats(dev, &temp);
946b60c5115SThomas Graf 	copy_rtnl_link_stats(nla_data(attr), stats);
9471da177e4SLinus Torvalds 
94810708f37SJan Engelhardt 	attr = nla_reserve(skb, IFLA_STATS64,
94910708f37SJan Engelhardt 			sizeof(struct rtnl_link_stats64));
95010708f37SJan Engelhardt 	if (attr == NULL)
95110708f37SJan Engelhardt 		goto nla_put_failure;
95210708f37SJan Engelhardt 	copy_rtnl_link_stats64(nla_data(attr), stats);
95310708f37SJan Engelhardt 
954a6574349SDavid S. Miller 	if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) &&
955a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)))
956a6574349SDavid S. Miller 		goto nla_put_failure;
95757b61080SScott Feldman 
958115c9b81SGreg Rose 	if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent
959115c9b81SGreg Rose 	    && (ext_filter_mask & RTEXT_FILTER_VF)) {
960ebc08a6fSWilliams, Mitch A 		int i;
961ebc08a6fSWilliams, Mitch A 
962c02db8c6SChris Wright 		struct nlattr *vfinfo, *vf;
963c02db8c6SChris Wright 		int num_vfs = dev_num_vf(dev->dev.parent);
964c02db8c6SChris Wright 
965c02db8c6SChris Wright 		vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
966c02db8c6SChris Wright 		if (!vfinfo)
967c02db8c6SChris Wright 			goto nla_put_failure;
968c02db8c6SChris Wright 		for (i = 0; i < num_vfs; i++) {
969c02db8c6SChris Wright 			struct ifla_vf_info ivi;
970c02db8c6SChris Wright 			struct ifla_vf_mac vf_mac;
971c02db8c6SChris Wright 			struct ifla_vf_vlan vf_vlan;
972c02db8c6SChris Wright 			struct ifla_vf_tx_rate vf_tx_rate;
9735f8444a3SGreg Rose 			struct ifla_vf_spoofchk vf_spoofchk;
9745f8444a3SGreg Rose 
9755f8444a3SGreg Rose 			/*
9765f8444a3SGreg Rose 			 * Not all SR-IOV capable drivers support the
9775f8444a3SGreg Rose 			 * spoofcheck query.  Preset to -1 so the user
9785f8444a3SGreg Rose 			 * space tool can detect that the driver didn't
9795f8444a3SGreg Rose 			 * report anything.
9805f8444a3SGreg Rose 			 */
9815f8444a3SGreg Rose 			ivi.spoofchk = -1;
982ebc08a6fSWilliams, Mitch A 			if (dev->netdev_ops->ndo_get_vf_config(dev, i, &ivi))
983ebc08a6fSWilliams, Mitch A 				break;
9845f8444a3SGreg Rose 			vf_mac.vf =
9855f8444a3SGreg Rose 				vf_vlan.vf =
9865f8444a3SGreg Rose 				vf_tx_rate.vf =
9875f8444a3SGreg Rose 				vf_spoofchk.vf = ivi.vf;
9885f8444a3SGreg Rose 
989c02db8c6SChris Wright 			memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
990c02db8c6SChris Wright 			vf_vlan.vlan = ivi.vlan;
991c02db8c6SChris Wright 			vf_vlan.qos = ivi.qos;
992c02db8c6SChris Wright 			vf_tx_rate.rate = ivi.tx_rate;
9935f8444a3SGreg Rose 			vf_spoofchk.setting = ivi.spoofchk;
994c02db8c6SChris Wright 			vf = nla_nest_start(skb, IFLA_VF_INFO);
995c02db8c6SChris Wright 			if (!vf) {
996c02db8c6SChris Wright 				nla_nest_cancel(skb, vfinfo);
997c02db8c6SChris Wright 				goto nla_put_failure;
998ebc08a6fSWilliams, Mitch A 			}
999a6574349SDavid S. Miller 			if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1000a6574349SDavid S. Miller 			    nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1001a6574349SDavid S. Miller 			    nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1002a6574349SDavid S. Miller 				    &vf_tx_rate) ||
1003a6574349SDavid S. Miller 			    nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1004a6574349SDavid S. Miller 				    &vf_spoofchk))
1005a6574349SDavid S. Miller 				goto nla_put_failure;
1006c02db8c6SChris Wright 			nla_nest_end(skb, vf);
1007c02db8c6SChris Wright 		}
1008c02db8c6SChris Wright 		nla_nest_end(skb, vfinfo);
1009ebc08a6fSWilliams, Mitch A 	}
101057b61080SScott Feldman 
101157b61080SScott Feldman 	if (rtnl_port_fill(skb, dev))
101257b61080SScott Feldman 		goto nla_put_failure;
101357b61080SScott Feldman 
101438f7b870SPatrick McHardy 	if (dev->rtnl_link_ops) {
101538f7b870SPatrick McHardy 		if (rtnl_link_fill(skb, dev) < 0)
101638f7b870SPatrick McHardy 			goto nla_put_failure;
101738f7b870SPatrick McHardy 	}
101838f7b870SPatrick McHardy 
1019f8ff182cSThomas Graf 	if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
1020f8ff182cSThomas Graf 		goto nla_put_failure;
1021f8ff182cSThomas Graf 
1022f8ff182cSThomas Graf 	list_for_each_entry(af_ops, &rtnl_af_ops, list) {
1023f8ff182cSThomas Graf 		if (af_ops->fill_link_af) {
1024f8ff182cSThomas Graf 			struct nlattr *af;
1025f8ff182cSThomas Graf 			int err;
1026f8ff182cSThomas Graf 
1027f8ff182cSThomas Graf 			if (!(af = nla_nest_start(skb, af_ops->family)))
1028f8ff182cSThomas Graf 				goto nla_put_failure;
1029f8ff182cSThomas Graf 
1030f8ff182cSThomas Graf 			err = af_ops->fill_link_af(skb, dev);
1031f8ff182cSThomas Graf 
1032f8ff182cSThomas Graf 			/*
1033f8ff182cSThomas Graf 			 * Caller may return ENODATA to indicate that there
1034f8ff182cSThomas Graf 			 * was no data to be dumped. This is not an error, it
1035f8ff182cSThomas Graf 			 * means we should trim the attribute header and
1036f8ff182cSThomas Graf 			 * continue.
1037f8ff182cSThomas Graf 			 */
1038f8ff182cSThomas Graf 			if (err == -ENODATA)
1039f8ff182cSThomas Graf 				nla_nest_cancel(skb, af);
1040f8ff182cSThomas Graf 			else if (err < 0)
1041f8ff182cSThomas Graf 				goto nla_put_failure;
1042f8ff182cSThomas Graf 
1043f8ff182cSThomas Graf 			nla_nest_end(skb, af);
1044f8ff182cSThomas Graf 		}
1045f8ff182cSThomas Graf 	}
1046f8ff182cSThomas Graf 
1047f8ff182cSThomas Graf 	nla_nest_end(skb, af_spec);
1048f8ff182cSThomas Graf 
1049b60c5115SThomas Graf 	return nlmsg_end(skb, nlh);
1050b60c5115SThomas Graf 
1051b60c5115SThomas Graf nla_put_failure:
105226932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
105326932566SPatrick McHardy 	return -EMSGSIZE;
10541da177e4SLinus Torvalds }
10551da177e4SLinus Torvalds 
1056b60c5115SThomas Graf static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
10571da177e4SLinus Torvalds {
10583b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
10597c28bd0bSEric Dumazet 	int h, s_h;
10607c28bd0bSEric Dumazet 	int idx = 0, s_idx;
10611da177e4SLinus Torvalds 	struct net_device *dev;
10627c28bd0bSEric Dumazet 	struct hlist_head *head;
10637c28bd0bSEric Dumazet 	struct hlist_node *node;
1064115c9b81SGreg Rose 	struct nlattr *tb[IFLA_MAX+1];
1065115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
10661da177e4SLinus Torvalds 
10677c28bd0bSEric Dumazet 	s_h = cb->args[0];
10687c28bd0bSEric Dumazet 	s_idx = cb->args[1];
10697c28bd0bSEric Dumazet 
1070e67f88ddSEric Dumazet 	rcu_read_lock();
10714e985adaSThomas Graf 	cb->seq = net->dev_base_seq;
10724e985adaSThomas Graf 
1073a4b64fbeSEric Dumazet 	if (nlmsg_parse(cb->nlh, sizeof(struct rtgenmsg), tb, IFLA_MAX,
1074a4b64fbeSEric Dumazet 			ifla_policy) >= 0) {
1075115c9b81SGreg Rose 
1076115c9b81SGreg Rose 		if (tb[IFLA_EXT_MASK])
1077115c9b81SGreg Rose 			ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1078a4b64fbeSEric Dumazet 	}
1079115c9b81SGreg Rose 
10807c28bd0bSEric Dumazet 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
10817562f876SPavel Emelianov 		idx = 0;
10827c28bd0bSEric Dumazet 		head = &net->dev_index_head[h];
1083e67f88ddSEric Dumazet 		hlist_for_each_entry_rcu(dev, node, head, index_hlist) {
10841da177e4SLinus Torvalds 			if (idx < s_idx)
10857562f876SPavel Emelianov 				goto cont;
1086575c3e2aSPatrick McHardy 			if (rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
108715e47304SEric W. Biederman 					     NETLINK_CB(cb->skb).portid,
10887c28bd0bSEric Dumazet 					     cb->nlh->nlmsg_seq, 0,
1089115c9b81SGreg Rose 					     NLM_F_MULTI,
1090115c9b81SGreg Rose 					     ext_filter_mask) <= 0)
10917c28bd0bSEric Dumazet 				goto out;
10924e985adaSThomas Graf 
10934e985adaSThomas Graf 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
10947562f876SPavel Emelianov cont:
10957562f876SPavel Emelianov 			idx++;
10961da177e4SLinus Torvalds 		}
10977c28bd0bSEric Dumazet 	}
10987c28bd0bSEric Dumazet out:
1099e67f88ddSEric Dumazet 	rcu_read_unlock();
11007c28bd0bSEric Dumazet 	cb->args[1] = idx;
11017c28bd0bSEric Dumazet 	cb->args[0] = h;
11021da177e4SLinus Torvalds 
11031da177e4SLinus Torvalds 	return skb->len;
11041da177e4SLinus Torvalds }
11051da177e4SLinus Torvalds 
1106e7199288SPavel Emelianov const struct nla_policy ifla_policy[IFLA_MAX+1] = {
11075176f91eSThomas Graf 	[IFLA_IFNAME]		= { .type = NLA_STRING, .len = IFNAMSIZ-1 },
110838f7b870SPatrick McHardy 	[IFLA_ADDRESS]		= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
110938f7b870SPatrick McHardy 	[IFLA_BROADCAST]	= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
11105176f91eSThomas Graf 	[IFLA_MAP]		= { .len = sizeof(struct rtnl_link_ifmap) },
1111da5e0494SThomas Graf 	[IFLA_MTU]		= { .type = NLA_U32 },
111276e87306SThomas Graf 	[IFLA_LINK]		= { .type = NLA_U32 },
1113fbaec0eaSJiri Pirko 	[IFLA_MASTER]		= { .type = NLA_U32 },
11149a57247fSJiri Pirko 	[IFLA_CARRIER]		= { .type = NLA_U8 },
1115da5e0494SThomas Graf 	[IFLA_TXQLEN]		= { .type = NLA_U32 },
1116da5e0494SThomas Graf 	[IFLA_WEIGHT]		= { .type = NLA_U32 },
1117da5e0494SThomas Graf 	[IFLA_OPERSTATE]	= { .type = NLA_U8 },
1118da5e0494SThomas Graf 	[IFLA_LINKMODE]		= { .type = NLA_U8 },
111976e87306SThomas Graf 	[IFLA_LINKINFO]		= { .type = NLA_NESTED },
1120d8a5ec67SEric W. Biederman 	[IFLA_NET_NS_PID]	= { .type = NLA_U32 },
1121f0630529SEric W. Biederman 	[IFLA_NET_NS_FD]	= { .type = NLA_U32 },
11220b815a1aSStephen Hemminger 	[IFLA_IFALIAS]	        = { .type = NLA_STRING, .len = IFALIASZ-1 },
1123c02db8c6SChris Wright 	[IFLA_VFINFO_LIST]	= {. type = NLA_NESTED },
112457b61080SScott Feldman 	[IFLA_VF_PORTS]		= { .type = NLA_NESTED },
112557b61080SScott Feldman 	[IFLA_PORT_SELF]	= { .type = NLA_NESTED },
1126f8ff182cSThomas Graf 	[IFLA_AF_SPEC]		= { .type = NLA_NESTED },
1127115c9b81SGreg Rose 	[IFLA_EXT_MASK]		= { .type = NLA_U32 },
1128edbc0bb3SBen Greear 	[IFLA_PROMISCUITY]	= { .type = NLA_U32 },
112976ff5cc9SJiri Pirko 	[IFLA_NUM_TX_QUEUES]	= { .type = NLA_U32 },
113076ff5cc9SJiri Pirko 	[IFLA_NUM_RX_QUEUES]	= { .type = NLA_U32 },
1131da5e0494SThomas Graf };
1132e0d087afSEric Dumazet EXPORT_SYMBOL(ifla_policy);
11331da177e4SLinus Torvalds 
113438f7b870SPatrick McHardy static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
113538f7b870SPatrick McHardy 	[IFLA_INFO_KIND]	= { .type = NLA_STRING },
113638f7b870SPatrick McHardy 	[IFLA_INFO_DATA]	= { .type = NLA_NESTED },
113738f7b870SPatrick McHardy };
113838f7b870SPatrick McHardy 
1139c02db8c6SChris Wright static const struct nla_policy ifla_vfinfo_policy[IFLA_VF_INFO_MAX+1] = {
1140c02db8c6SChris Wright 	[IFLA_VF_INFO]		= { .type = NLA_NESTED },
1141c02db8c6SChris Wright };
1142c02db8c6SChris Wright 
1143c02db8c6SChris Wright static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1144c02db8c6SChris Wright 	[IFLA_VF_MAC]		= { .type = NLA_BINARY,
1145c02db8c6SChris Wright 				    .len = sizeof(struct ifla_vf_mac) },
1146c02db8c6SChris Wright 	[IFLA_VF_VLAN]		= { .type = NLA_BINARY,
1147c02db8c6SChris Wright 				    .len = sizeof(struct ifla_vf_vlan) },
1148c02db8c6SChris Wright 	[IFLA_VF_TX_RATE]	= { .type = NLA_BINARY,
1149c02db8c6SChris Wright 				    .len = sizeof(struct ifla_vf_tx_rate) },
115048752f65SGreg Rose 	[IFLA_VF_SPOOFCHK]	= { .type = NLA_BINARY,
115148752f65SGreg Rose 				    .len = sizeof(struct ifla_vf_spoofchk) },
1152c02db8c6SChris Wright };
1153c02db8c6SChris Wright 
115457b61080SScott Feldman static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
115557b61080SScott Feldman 	[IFLA_PORT_VF]		= { .type = NLA_U32 },
115657b61080SScott Feldman 	[IFLA_PORT_PROFILE]	= { .type = NLA_STRING,
115757b61080SScott Feldman 				    .len = PORT_PROFILE_MAX },
115857b61080SScott Feldman 	[IFLA_PORT_VSI_TYPE]	= { .type = NLA_BINARY,
115957b61080SScott Feldman 				    .len = sizeof(struct ifla_port_vsi)},
116057b61080SScott Feldman 	[IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
116157b61080SScott Feldman 				      .len = PORT_UUID_MAX },
116257b61080SScott Feldman 	[IFLA_PORT_HOST_UUID]	= { .type = NLA_STRING,
116357b61080SScott Feldman 				    .len = PORT_UUID_MAX },
116457b61080SScott Feldman 	[IFLA_PORT_REQUEST]	= { .type = NLA_U8, },
116557b61080SScott Feldman 	[IFLA_PORT_RESPONSE]	= { .type = NLA_U16, },
116657b61080SScott Feldman };
116757b61080SScott Feldman 
116881adee47SEric W. Biederman struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
116981adee47SEric W. Biederman {
117081adee47SEric W. Biederman 	struct net *net;
117181adee47SEric W. Biederman 	/* Examine the link attributes and figure out which
117281adee47SEric W. Biederman 	 * network namespace we are talking about.
117381adee47SEric W. Biederman 	 */
117481adee47SEric W. Biederman 	if (tb[IFLA_NET_NS_PID])
117581adee47SEric W. Biederman 		net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
1176f0630529SEric W. Biederman 	else if (tb[IFLA_NET_NS_FD])
1177f0630529SEric W. Biederman 		net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
117881adee47SEric W. Biederman 	else
117981adee47SEric W. Biederman 		net = get_net(src_net);
118081adee47SEric W. Biederman 	return net;
118181adee47SEric W. Biederman }
118281adee47SEric W. Biederman EXPORT_SYMBOL(rtnl_link_get_net);
118381adee47SEric W. Biederman 
11841840bb13SThomas Graf static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
11851840bb13SThomas Graf {
11861840bb13SThomas Graf 	if (dev) {
11871840bb13SThomas Graf 		if (tb[IFLA_ADDRESS] &&
11881840bb13SThomas Graf 		    nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
11891840bb13SThomas Graf 			return -EINVAL;
11901840bb13SThomas Graf 
11911840bb13SThomas Graf 		if (tb[IFLA_BROADCAST] &&
11921840bb13SThomas Graf 		    nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
11931840bb13SThomas Graf 			return -EINVAL;
11941840bb13SThomas Graf 	}
11951840bb13SThomas Graf 
1196cf7afbfeSThomas Graf 	if (tb[IFLA_AF_SPEC]) {
1197cf7afbfeSThomas Graf 		struct nlattr *af;
1198cf7afbfeSThomas Graf 		int rem, err;
1199cf7afbfeSThomas Graf 
1200cf7afbfeSThomas Graf 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1201cf7afbfeSThomas Graf 			const struct rtnl_af_ops *af_ops;
1202cf7afbfeSThomas Graf 
1203cf7afbfeSThomas Graf 			if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1204cf7afbfeSThomas Graf 				return -EAFNOSUPPORT;
1205cf7afbfeSThomas Graf 
1206cf7afbfeSThomas Graf 			if (!af_ops->set_link_af)
1207cf7afbfeSThomas Graf 				return -EOPNOTSUPP;
1208cf7afbfeSThomas Graf 
1209cf7afbfeSThomas Graf 			if (af_ops->validate_link_af) {
12106d3a9a68SKurt Van Dijck 				err = af_ops->validate_link_af(dev, af);
1211cf7afbfeSThomas Graf 				if (err < 0)
1212cf7afbfeSThomas Graf 					return err;
1213cf7afbfeSThomas Graf 			}
1214cf7afbfeSThomas Graf 		}
1215cf7afbfeSThomas Graf 	}
1216cf7afbfeSThomas Graf 
12171840bb13SThomas Graf 	return 0;
12181840bb13SThomas Graf }
12191840bb13SThomas Graf 
1220c02db8c6SChris Wright static int do_setvfinfo(struct net_device *dev, struct nlattr *attr)
1221c02db8c6SChris Wright {
1222c02db8c6SChris Wright 	int rem, err = -EINVAL;
1223c02db8c6SChris Wright 	struct nlattr *vf;
1224c02db8c6SChris Wright 	const struct net_device_ops *ops = dev->netdev_ops;
1225c02db8c6SChris Wright 
1226c02db8c6SChris Wright 	nla_for_each_nested(vf, attr, rem) {
1227c02db8c6SChris Wright 		switch (nla_type(vf)) {
1228c02db8c6SChris Wright 		case IFLA_VF_MAC: {
1229c02db8c6SChris Wright 			struct ifla_vf_mac *ivm;
1230c02db8c6SChris Wright 			ivm = nla_data(vf);
1231c02db8c6SChris Wright 			err = -EOPNOTSUPP;
1232c02db8c6SChris Wright 			if (ops->ndo_set_vf_mac)
1233c02db8c6SChris Wright 				err = ops->ndo_set_vf_mac(dev, ivm->vf,
1234c02db8c6SChris Wright 							  ivm->mac);
1235c02db8c6SChris Wright 			break;
1236c02db8c6SChris Wright 		}
1237c02db8c6SChris Wright 		case IFLA_VF_VLAN: {
1238c02db8c6SChris Wright 			struct ifla_vf_vlan *ivv;
1239c02db8c6SChris Wright 			ivv = nla_data(vf);
1240c02db8c6SChris Wright 			err = -EOPNOTSUPP;
1241c02db8c6SChris Wright 			if (ops->ndo_set_vf_vlan)
1242c02db8c6SChris Wright 				err = ops->ndo_set_vf_vlan(dev, ivv->vf,
1243c02db8c6SChris Wright 							   ivv->vlan,
1244c02db8c6SChris Wright 							   ivv->qos);
1245c02db8c6SChris Wright 			break;
1246c02db8c6SChris Wright 		}
1247c02db8c6SChris Wright 		case IFLA_VF_TX_RATE: {
1248c02db8c6SChris Wright 			struct ifla_vf_tx_rate *ivt;
1249c02db8c6SChris Wright 			ivt = nla_data(vf);
1250c02db8c6SChris Wright 			err = -EOPNOTSUPP;
1251c02db8c6SChris Wright 			if (ops->ndo_set_vf_tx_rate)
1252c02db8c6SChris Wright 				err = ops->ndo_set_vf_tx_rate(dev, ivt->vf,
1253c02db8c6SChris Wright 							      ivt->rate);
1254c02db8c6SChris Wright 			break;
1255c02db8c6SChris Wright 		}
12565f8444a3SGreg Rose 		case IFLA_VF_SPOOFCHK: {
12575f8444a3SGreg Rose 			struct ifla_vf_spoofchk *ivs;
12585f8444a3SGreg Rose 			ivs = nla_data(vf);
12595f8444a3SGreg Rose 			err = -EOPNOTSUPP;
12605f8444a3SGreg Rose 			if (ops->ndo_set_vf_spoofchk)
12615f8444a3SGreg Rose 				err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
12625f8444a3SGreg Rose 							       ivs->setting);
12635f8444a3SGreg Rose 			break;
12645f8444a3SGreg Rose 		}
1265c02db8c6SChris Wright 		default:
1266c02db8c6SChris Wright 			err = -EINVAL;
1267c02db8c6SChris Wright 			break;
1268c02db8c6SChris Wright 		}
1269c02db8c6SChris Wright 		if (err)
1270c02db8c6SChris Wright 			break;
1271c02db8c6SChris Wright 	}
1272c02db8c6SChris Wright 	return err;
1273c02db8c6SChris Wright }
1274c02db8c6SChris Wright 
1275fbaec0eaSJiri Pirko static int do_set_master(struct net_device *dev, int ifindex)
1276fbaec0eaSJiri Pirko {
1277898e5061SJiri Pirko 	struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
1278fbaec0eaSJiri Pirko 	const struct net_device_ops *ops;
1279fbaec0eaSJiri Pirko 	int err;
1280fbaec0eaSJiri Pirko 
1281898e5061SJiri Pirko 	if (upper_dev) {
1282898e5061SJiri Pirko 		if (upper_dev->ifindex == ifindex)
1283fbaec0eaSJiri Pirko 			return 0;
1284898e5061SJiri Pirko 		ops = upper_dev->netdev_ops;
1285fbaec0eaSJiri Pirko 		if (ops->ndo_del_slave) {
1286898e5061SJiri Pirko 			err = ops->ndo_del_slave(upper_dev, dev);
1287fbaec0eaSJiri Pirko 			if (err)
1288fbaec0eaSJiri Pirko 				return err;
1289fbaec0eaSJiri Pirko 		} else {
1290fbaec0eaSJiri Pirko 			return -EOPNOTSUPP;
1291fbaec0eaSJiri Pirko 		}
1292fbaec0eaSJiri Pirko 	}
1293fbaec0eaSJiri Pirko 
1294fbaec0eaSJiri Pirko 	if (ifindex) {
1295898e5061SJiri Pirko 		upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
1296898e5061SJiri Pirko 		if (!upper_dev)
1297fbaec0eaSJiri Pirko 			return -EINVAL;
1298898e5061SJiri Pirko 		ops = upper_dev->netdev_ops;
1299fbaec0eaSJiri Pirko 		if (ops->ndo_add_slave) {
1300898e5061SJiri Pirko 			err = ops->ndo_add_slave(upper_dev, dev);
1301fbaec0eaSJiri Pirko 			if (err)
1302fbaec0eaSJiri Pirko 				return err;
1303fbaec0eaSJiri Pirko 		} else {
1304fbaec0eaSJiri Pirko 			return -EOPNOTSUPP;
1305fbaec0eaSJiri Pirko 		}
1306fbaec0eaSJiri Pirko 	}
1307fbaec0eaSJiri Pirko 	return 0;
1308fbaec0eaSJiri Pirko }
1309fbaec0eaSJiri Pirko 
13100157f60cSPatrick McHardy static int do_setlink(struct net_device *dev, struct ifinfomsg *ifm,
131138f7b870SPatrick McHardy 		      struct nlattr **tb, char *ifname, int modified)
13120157f60cSPatrick McHardy {
1313d314774cSStephen Hemminger 	const struct net_device_ops *ops = dev->netdev_ops;
13140157f60cSPatrick McHardy 	int err;
13150157f60cSPatrick McHardy 
1316f0630529SEric W. Biederman 	if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
131781adee47SEric W. Biederman 		struct net *net = rtnl_link_get_net(dev_net(dev), tb);
1318d8a5ec67SEric W. Biederman 		if (IS_ERR(net)) {
1319d8a5ec67SEric W. Biederman 			err = PTR_ERR(net);
1320d8a5ec67SEric W. Biederman 			goto errout;
1321d8a5ec67SEric W. Biederman 		}
1322b51642f6SEric W. Biederman 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) {
1323b51642f6SEric W. Biederman 			err = -EPERM;
1324b51642f6SEric W. Biederman 			goto errout;
1325b51642f6SEric W. Biederman 		}
1326d8a5ec67SEric W. Biederman 		err = dev_change_net_namespace(dev, net, ifname);
1327d8a5ec67SEric W. Biederman 		put_net(net);
1328d8a5ec67SEric W. Biederman 		if (err)
1329d8a5ec67SEric W. Biederman 			goto errout;
1330d8a5ec67SEric W. Biederman 		modified = 1;
1331d8a5ec67SEric W. Biederman 	}
1332d8a5ec67SEric W. Biederman 
13330157f60cSPatrick McHardy 	if (tb[IFLA_MAP]) {
13340157f60cSPatrick McHardy 		struct rtnl_link_ifmap *u_map;
13350157f60cSPatrick McHardy 		struct ifmap k_map;
13360157f60cSPatrick McHardy 
1337d314774cSStephen Hemminger 		if (!ops->ndo_set_config) {
13380157f60cSPatrick McHardy 			err = -EOPNOTSUPP;
13390157f60cSPatrick McHardy 			goto errout;
13400157f60cSPatrick McHardy 		}
13410157f60cSPatrick McHardy 
13420157f60cSPatrick McHardy 		if (!netif_device_present(dev)) {
13430157f60cSPatrick McHardy 			err = -ENODEV;
13440157f60cSPatrick McHardy 			goto errout;
13450157f60cSPatrick McHardy 		}
13460157f60cSPatrick McHardy 
13470157f60cSPatrick McHardy 		u_map = nla_data(tb[IFLA_MAP]);
13480157f60cSPatrick McHardy 		k_map.mem_start = (unsigned long) u_map->mem_start;
13490157f60cSPatrick McHardy 		k_map.mem_end = (unsigned long) u_map->mem_end;
13500157f60cSPatrick McHardy 		k_map.base_addr = (unsigned short) u_map->base_addr;
13510157f60cSPatrick McHardy 		k_map.irq = (unsigned char) u_map->irq;
13520157f60cSPatrick McHardy 		k_map.dma = (unsigned char) u_map->dma;
13530157f60cSPatrick McHardy 		k_map.port = (unsigned char) u_map->port;
13540157f60cSPatrick McHardy 
1355d314774cSStephen Hemminger 		err = ops->ndo_set_config(dev, &k_map);
13560157f60cSPatrick McHardy 		if (err < 0)
13570157f60cSPatrick McHardy 			goto errout;
13580157f60cSPatrick McHardy 
13590157f60cSPatrick McHardy 		modified = 1;
13600157f60cSPatrick McHardy 	}
13610157f60cSPatrick McHardy 
13620157f60cSPatrick McHardy 	if (tb[IFLA_ADDRESS]) {
13630157f60cSPatrick McHardy 		struct sockaddr *sa;
13640157f60cSPatrick McHardy 		int len;
13650157f60cSPatrick McHardy 
13660157f60cSPatrick McHardy 		len = sizeof(sa_family_t) + dev->addr_len;
13670157f60cSPatrick McHardy 		sa = kmalloc(len, GFP_KERNEL);
13680157f60cSPatrick McHardy 		if (!sa) {
13690157f60cSPatrick McHardy 			err = -ENOMEM;
13700157f60cSPatrick McHardy 			goto errout;
13710157f60cSPatrick McHardy 		}
13720157f60cSPatrick McHardy 		sa->sa_family = dev->type;
13730157f60cSPatrick McHardy 		memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
13740157f60cSPatrick McHardy 		       dev->addr_len);
1375e7c3273eSJiri Pirko 		err = dev_set_mac_address(dev, sa);
13760157f60cSPatrick McHardy 		kfree(sa);
13770157f60cSPatrick McHardy 		if (err)
13780157f60cSPatrick McHardy 			goto errout;
13790157f60cSPatrick McHardy 		modified = 1;
13800157f60cSPatrick McHardy 	}
13810157f60cSPatrick McHardy 
13820157f60cSPatrick McHardy 	if (tb[IFLA_MTU]) {
13830157f60cSPatrick McHardy 		err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
13840157f60cSPatrick McHardy 		if (err < 0)
13850157f60cSPatrick McHardy 			goto errout;
13860157f60cSPatrick McHardy 		modified = 1;
13870157f60cSPatrick McHardy 	}
13880157f60cSPatrick McHardy 
1389cbda10faSVlad Dogaru 	if (tb[IFLA_GROUP]) {
1390cbda10faSVlad Dogaru 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
1391cbda10faSVlad Dogaru 		modified = 1;
1392cbda10faSVlad Dogaru 	}
1393cbda10faSVlad Dogaru 
13940157f60cSPatrick McHardy 	/*
13950157f60cSPatrick McHardy 	 * Interface selected by interface index but interface
13960157f60cSPatrick McHardy 	 * name provided implies that a name change has been
13970157f60cSPatrick McHardy 	 * requested.
13980157f60cSPatrick McHardy 	 */
13990157f60cSPatrick McHardy 	if (ifm->ifi_index > 0 && ifname[0]) {
14000157f60cSPatrick McHardy 		err = dev_change_name(dev, ifname);
14010157f60cSPatrick McHardy 		if (err < 0)
14020157f60cSPatrick McHardy 			goto errout;
14030157f60cSPatrick McHardy 		modified = 1;
14040157f60cSPatrick McHardy 	}
14050157f60cSPatrick McHardy 
14060b815a1aSStephen Hemminger 	if (tb[IFLA_IFALIAS]) {
14070b815a1aSStephen Hemminger 		err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
14080b815a1aSStephen Hemminger 				    nla_len(tb[IFLA_IFALIAS]));
14090b815a1aSStephen Hemminger 		if (err < 0)
14100b815a1aSStephen Hemminger 			goto errout;
14110b815a1aSStephen Hemminger 		modified = 1;
14120b815a1aSStephen Hemminger 	}
14130b815a1aSStephen Hemminger 
14140157f60cSPatrick McHardy 	if (tb[IFLA_BROADCAST]) {
14150157f60cSPatrick McHardy 		nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
1416e7c3273eSJiri Pirko 		call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
14170157f60cSPatrick McHardy 	}
14180157f60cSPatrick McHardy 
14190157f60cSPatrick McHardy 	if (ifm->ifi_flags || ifm->ifi_change) {
14203729d502SPatrick McHardy 		err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
14215f9021cfSJohannes Berg 		if (err < 0)
14225f9021cfSJohannes Berg 			goto errout;
14230157f60cSPatrick McHardy 	}
14240157f60cSPatrick McHardy 
1425fbaec0eaSJiri Pirko 	if (tb[IFLA_MASTER]) {
1426fbaec0eaSJiri Pirko 		err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
1427fbaec0eaSJiri Pirko 		if (err)
1428fbaec0eaSJiri Pirko 			goto errout;
1429fbaec0eaSJiri Pirko 		modified = 1;
1430fbaec0eaSJiri Pirko 	}
1431fbaec0eaSJiri Pirko 
14329a57247fSJiri Pirko 	if (tb[IFLA_CARRIER]) {
14339a57247fSJiri Pirko 		err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
14349a57247fSJiri Pirko 		if (err)
14359a57247fSJiri Pirko 			goto errout;
14369a57247fSJiri Pirko 		modified = 1;
14379a57247fSJiri Pirko 	}
14389a57247fSJiri Pirko 
143993b2d4a2SDavid S. Miller 	if (tb[IFLA_TXQLEN])
14400157f60cSPatrick McHardy 		dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
14410157f60cSPatrick McHardy 
14420157f60cSPatrick McHardy 	if (tb[IFLA_OPERSTATE])
144393b2d4a2SDavid S. Miller 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
14440157f60cSPatrick McHardy 
14450157f60cSPatrick McHardy 	if (tb[IFLA_LINKMODE]) {
14460157f60cSPatrick McHardy 		write_lock_bh(&dev_base_lock);
14470157f60cSPatrick McHardy 		dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
144893b2d4a2SDavid S. Miller 		write_unlock_bh(&dev_base_lock);
14490157f60cSPatrick McHardy 	}
14500157f60cSPatrick McHardy 
1451c02db8c6SChris Wright 	if (tb[IFLA_VFINFO_LIST]) {
1452c02db8c6SChris Wright 		struct nlattr *attr;
1453c02db8c6SChris Wright 		int rem;
1454c02db8c6SChris Wright 		nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
1455253683bbSDavid Howells 			if (nla_type(attr) != IFLA_VF_INFO) {
1456253683bbSDavid Howells 				err = -EINVAL;
1457c02db8c6SChris Wright 				goto errout;
1458253683bbSDavid Howells 			}
1459c02db8c6SChris Wright 			err = do_setvfinfo(dev, attr);
1460ebc08a6fSWilliams, Mitch A 			if (err < 0)
1461ebc08a6fSWilliams, Mitch A 				goto errout;
1462ebc08a6fSWilliams, Mitch A 			modified = 1;
1463ebc08a6fSWilliams, Mitch A 		}
1464ebc08a6fSWilliams, Mitch A 	}
14650157f60cSPatrick McHardy 	err = 0;
14660157f60cSPatrick McHardy 
146757b61080SScott Feldman 	if (tb[IFLA_VF_PORTS]) {
146857b61080SScott Feldman 		struct nlattr *port[IFLA_PORT_MAX+1];
146957b61080SScott Feldman 		struct nlattr *attr;
147057b61080SScott Feldman 		int vf;
147157b61080SScott Feldman 		int rem;
147257b61080SScott Feldman 
147357b61080SScott Feldman 		err = -EOPNOTSUPP;
147457b61080SScott Feldman 		if (!ops->ndo_set_vf_port)
147557b61080SScott Feldman 			goto errout;
147657b61080SScott Feldman 
147757b61080SScott Feldman 		nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
147857b61080SScott Feldman 			if (nla_type(attr) != IFLA_VF_PORT)
147957b61080SScott Feldman 				continue;
148057b61080SScott Feldman 			err = nla_parse_nested(port, IFLA_PORT_MAX,
148157b61080SScott Feldman 				attr, ifla_port_policy);
148257b61080SScott Feldman 			if (err < 0)
148357b61080SScott Feldman 				goto errout;
148457b61080SScott Feldman 			if (!port[IFLA_PORT_VF]) {
148557b61080SScott Feldman 				err = -EOPNOTSUPP;
148657b61080SScott Feldman 				goto errout;
148757b61080SScott Feldman 			}
148857b61080SScott Feldman 			vf = nla_get_u32(port[IFLA_PORT_VF]);
148957b61080SScott Feldman 			err = ops->ndo_set_vf_port(dev, vf, port);
149057b61080SScott Feldman 			if (err < 0)
149157b61080SScott Feldman 				goto errout;
149257b61080SScott Feldman 			modified = 1;
149357b61080SScott Feldman 		}
149457b61080SScott Feldman 	}
149557b61080SScott Feldman 	err = 0;
149657b61080SScott Feldman 
149757b61080SScott Feldman 	if (tb[IFLA_PORT_SELF]) {
149857b61080SScott Feldman 		struct nlattr *port[IFLA_PORT_MAX+1];
149957b61080SScott Feldman 
150057b61080SScott Feldman 		err = nla_parse_nested(port, IFLA_PORT_MAX,
150157b61080SScott Feldman 			tb[IFLA_PORT_SELF], ifla_port_policy);
150257b61080SScott Feldman 		if (err < 0)
150357b61080SScott Feldman 			goto errout;
150457b61080SScott Feldman 
150557b61080SScott Feldman 		err = -EOPNOTSUPP;
150657b61080SScott Feldman 		if (ops->ndo_set_vf_port)
150757b61080SScott Feldman 			err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
150857b61080SScott Feldman 		if (err < 0)
150957b61080SScott Feldman 			goto errout;
151057b61080SScott Feldman 		modified = 1;
151157b61080SScott Feldman 	}
1512f8ff182cSThomas Graf 
1513f8ff182cSThomas Graf 	if (tb[IFLA_AF_SPEC]) {
1514f8ff182cSThomas Graf 		struct nlattr *af;
1515f8ff182cSThomas Graf 		int rem;
1516f8ff182cSThomas Graf 
1517f8ff182cSThomas Graf 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1518f8ff182cSThomas Graf 			const struct rtnl_af_ops *af_ops;
1519f8ff182cSThomas Graf 
1520f8ff182cSThomas Graf 			if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1521cf7afbfeSThomas Graf 				BUG();
1522f8ff182cSThomas Graf 
1523cf7afbfeSThomas Graf 			err = af_ops->set_link_af(dev, af);
1524f8ff182cSThomas Graf 			if (err < 0)
1525f8ff182cSThomas Graf 				goto errout;
1526f8ff182cSThomas Graf 
1527f8ff182cSThomas Graf 			modified = 1;
1528f8ff182cSThomas Graf 		}
1529f8ff182cSThomas Graf 	}
153057b61080SScott Feldman 	err = 0;
153157b61080SScott Feldman 
15320157f60cSPatrick McHardy errout:
1533e87cc472SJoe Perches 	if (err < 0 && modified)
1534e87cc472SJoe Perches 		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",
1535e87cc472SJoe Perches 				     dev->name);
15360157f60cSPatrick McHardy 
15370157f60cSPatrick McHardy 	return err;
15380157f60cSPatrick McHardy }
15390157f60cSPatrick McHardy 
1540da5e0494SThomas Graf static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1541da5e0494SThomas Graf {
15423b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
1543da5e0494SThomas Graf 	struct ifinfomsg *ifm;
1544da5e0494SThomas Graf 	struct net_device *dev;
15450157f60cSPatrick McHardy 	int err;
1546da5e0494SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
15471da177e4SLinus Torvalds 	char ifname[IFNAMSIZ];
15481da177e4SLinus Torvalds 
1549da5e0494SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1550da5e0494SThomas Graf 	if (err < 0)
1551da5e0494SThomas Graf 		goto errout;
15521da177e4SLinus Torvalds 
15535176f91eSThomas Graf 	if (tb[IFLA_IFNAME])
15545176f91eSThomas Graf 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
155578e5b891SPatrick McHardy 	else
155678e5b891SPatrick McHardy 		ifname[0] = '\0';
15571da177e4SLinus Torvalds 
15581da177e4SLinus Torvalds 	err = -EINVAL;
1559da5e0494SThomas Graf 	ifm = nlmsg_data(nlh);
156051055be8SPatrick McHardy 	if (ifm->ifi_index > 0)
1561a3d12891SEric Dumazet 		dev = __dev_get_by_index(net, ifm->ifi_index);
1562da5e0494SThomas Graf 	else if (tb[IFLA_IFNAME])
1563a3d12891SEric Dumazet 		dev = __dev_get_by_name(net, ifname);
1564da5e0494SThomas Graf 	else
1565da5e0494SThomas Graf 		goto errout;
15661da177e4SLinus Torvalds 
1567da5e0494SThomas Graf 	if (dev == NULL) {
1568da5e0494SThomas Graf 		err = -ENODEV;
1569da5e0494SThomas Graf 		goto errout;
1570da5e0494SThomas Graf 	}
15711da177e4SLinus Torvalds 
1572e0d087afSEric Dumazet 	err = validate_linkmsg(dev, tb);
1573e0d087afSEric Dumazet 	if (err < 0)
1574a3d12891SEric Dumazet 		goto errout;
1575da5e0494SThomas Graf 
157638f7b870SPatrick McHardy 	err = do_setlink(dev, ifm, tb, ifname, 0);
1577da5e0494SThomas Graf errout:
15781da177e4SLinus Torvalds 	return err;
15791da177e4SLinus Torvalds }
15801da177e4SLinus Torvalds 
158138f7b870SPatrick McHardy static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
158238f7b870SPatrick McHardy {
15833b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
158438f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops;
158538f7b870SPatrick McHardy 	struct net_device *dev;
158638f7b870SPatrick McHardy 	struct ifinfomsg *ifm;
158738f7b870SPatrick McHardy 	char ifname[IFNAMSIZ];
158838f7b870SPatrick McHardy 	struct nlattr *tb[IFLA_MAX+1];
158938f7b870SPatrick McHardy 	int err;
1590226bd341SEric Dumazet 	LIST_HEAD(list_kill);
159138f7b870SPatrick McHardy 
159238f7b870SPatrick McHardy 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
159338f7b870SPatrick McHardy 	if (err < 0)
159438f7b870SPatrick McHardy 		return err;
159538f7b870SPatrick McHardy 
159638f7b870SPatrick McHardy 	if (tb[IFLA_IFNAME])
159738f7b870SPatrick McHardy 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
159838f7b870SPatrick McHardy 
159938f7b870SPatrick McHardy 	ifm = nlmsg_data(nlh);
160038f7b870SPatrick McHardy 	if (ifm->ifi_index > 0)
1601881d966bSEric W. Biederman 		dev = __dev_get_by_index(net, ifm->ifi_index);
160238f7b870SPatrick McHardy 	else if (tb[IFLA_IFNAME])
1603881d966bSEric W. Biederman 		dev = __dev_get_by_name(net, ifname);
160438f7b870SPatrick McHardy 	else
160538f7b870SPatrick McHardy 		return -EINVAL;
160638f7b870SPatrick McHardy 
160738f7b870SPatrick McHardy 	if (!dev)
160838f7b870SPatrick McHardy 		return -ENODEV;
160938f7b870SPatrick McHardy 
161038f7b870SPatrick McHardy 	ops = dev->rtnl_link_ops;
161138f7b870SPatrick McHardy 	if (!ops)
161238f7b870SPatrick McHardy 		return -EOPNOTSUPP;
161338f7b870SPatrick McHardy 
1614226bd341SEric Dumazet 	ops->dellink(dev, &list_kill);
1615226bd341SEric Dumazet 	unregister_netdevice_many(&list_kill);
1616226bd341SEric Dumazet 	list_del(&list_kill);
161738f7b870SPatrick McHardy 	return 0;
161838f7b870SPatrick McHardy }
161938f7b870SPatrick McHardy 
16203729d502SPatrick McHardy int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
16213729d502SPatrick McHardy {
16223729d502SPatrick McHardy 	unsigned int old_flags;
16233729d502SPatrick McHardy 	int err;
16243729d502SPatrick McHardy 
16253729d502SPatrick McHardy 	old_flags = dev->flags;
16263729d502SPatrick McHardy 	if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
16273729d502SPatrick McHardy 		err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
16283729d502SPatrick McHardy 		if (err < 0)
16293729d502SPatrick McHardy 			return err;
16303729d502SPatrick McHardy 	}
16313729d502SPatrick McHardy 
16323729d502SPatrick McHardy 	dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
16333729d502SPatrick McHardy 	rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
16343729d502SPatrick McHardy 
16353729d502SPatrick McHardy 	__dev_notify_flags(dev, old_flags);
16363729d502SPatrick McHardy 	return 0;
16373729d502SPatrick McHardy }
16383729d502SPatrick McHardy EXPORT_SYMBOL(rtnl_configure_link);
16393729d502SPatrick McHardy 
1640c0713563SRami Rosen struct net_device *rtnl_create_link(struct net *net,
164181adee47SEric W. Biederman 	char *ifname, const struct rtnl_link_ops *ops, struct nlattr *tb[])
1642e7199288SPavel Emelianov {
1643e7199288SPavel Emelianov 	int err;
1644e7199288SPavel Emelianov 	struct net_device *dev;
1645d40156aaSJiri Pirko 	unsigned int num_tx_queues = 1;
1646d40156aaSJiri Pirko 	unsigned int num_rx_queues = 1;
1647e7199288SPavel Emelianov 
164876ff5cc9SJiri Pirko 	if (tb[IFLA_NUM_TX_QUEUES])
164976ff5cc9SJiri Pirko 		num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
165076ff5cc9SJiri Pirko 	else if (ops->get_num_tx_queues)
1651d40156aaSJiri Pirko 		num_tx_queues = ops->get_num_tx_queues();
165276ff5cc9SJiri Pirko 
165376ff5cc9SJiri Pirko 	if (tb[IFLA_NUM_RX_QUEUES])
165476ff5cc9SJiri Pirko 		num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
165576ff5cc9SJiri Pirko 	else if (ops->get_num_rx_queues)
1656d40156aaSJiri Pirko 		num_rx_queues = ops->get_num_rx_queues();
1657efacb309Sstephen hemminger 
1658e7199288SPavel Emelianov 	err = -ENOMEM;
1659d40156aaSJiri Pirko 	dev = alloc_netdev_mqs(ops->priv_size, ifname, ops->setup,
1660d40156aaSJiri Pirko 			       num_tx_queues, num_rx_queues);
1661e7199288SPavel Emelianov 	if (!dev)
1662e7199288SPavel Emelianov 		goto err;
1663e7199288SPavel Emelianov 
166481adee47SEric W. Biederman 	dev_net_set(dev, net);
166581adee47SEric W. Biederman 	dev->rtnl_link_ops = ops;
16663729d502SPatrick McHardy 	dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
166781adee47SEric W. Biederman 
1668e7199288SPavel Emelianov 	if (tb[IFLA_MTU])
1669e7199288SPavel Emelianov 		dev->mtu = nla_get_u32(tb[IFLA_MTU]);
1670e7199288SPavel Emelianov 	if (tb[IFLA_ADDRESS])
1671e7199288SPavel Emelianov 		memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
1672e7199288SPavel Emelianov 				nla_len(tb[IFLA_ADDRESS]));
1673e7199288SPavel Emelianov 	if (tb[IFLA_BROADCAST])
1674e7199288SPavel Emelianov 		memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
1675e7199288SPavel Emelianov 				nla_len(tb[IFLA_BROADCAST]));
1676e7199288SPavel Emelianov 	if (tb[IFLA_TXQLEN])
1677e7199288SPavel Emelianov 		dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
1678e7199288SPavel Emelianov 	if (tb[IFLA_OPERSTATE])
167993b2d4a2SDavid S. Miller 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
1680e7199288SPavel Emelianov 	if (tb[IFLA_LINKMODE])
1681e7199288SPavel Emelianov 		dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
1682ffa934f1SPatrick McHardy 	if (tb[IFLA_GROUP])
1683ffa934f1SPatrick McHardy 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
1684e7199288SPavel Emelianov 
1685e7199288SPavel Emelianov 	return dev;
1686e7199288SPavel Emelianov 
1687e7199288SPavel Emelianov err:
1688e7199288SPavel Emelianov 	return ERR_PTR(err);
1689e7199288SPavel Emelianov }
1690e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_create_link);
1691e7199288SPavel Emelianov 
1692e7ed828fSVlad Dogaru static int rtnl_group_changelink(struct net *net, int group,
1693e7ed828fSVlad Dogaru 		struct ifinfomsg *ifm,
1694e7ed828fSVlad Dogaru 		struct nlattr **tb)
1695e7ed828fSVlad Dogaru {
1696e7ed828fSVlad Dogaru 	struct net_device *dev;
1697e7ed828fSVlad Dogaru 	int err;
1698e7ed828fSVlad Dogaru 
1699e7ed828fSVlad Dogaru 	for_each_netdev(net, dev) {
1700e7ed828fSVlad Dogaru 		if (dev->group == group) {
1701e7ed828fSVlad Dogaru 			err = do_setlink(dev, ifm, tb, NULL, 0);
1702e7ed828fSVlad Dogaru 			if (err < 0)
1703e7ed828fSVlad Dogaru 				return err;
1704e7ed828fSVlad Dogaru 		}
1705e7ed828fSVlad Dogaru 	}
1706e7ed828fSVlad Dogaru 
1707e7ed828fSVlad Dogaru 	return 0;
1708e7ed828fSVlad Dogaru }
1709e7ed828fSVlad Dogaru 
171038f7b870SPatrick McHardy static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
171138f7b870SPatrick McHardy {
17123b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
171338f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops;
171438f7b870SPatrick McHardy 	struct net_device *dev;
171538f7b870SPatrick McHardy 	struct ifinfomsg *ifm;
171638f7b870SPatrick McHardy 	char kind[MODULE_NAME_LEN];
171738f7b870SPatrick McHardy 	char ifname[IFNAMSIZ];
171838f7b870SPatrick McHardy 	struct nlattr *tb[IFLA_MAX+1];
171938f7b870SPatrick McHardy 	struct nlattr *linkinfo[IFLA_INFO_MAX+1];
172038f7b870SPatrick McHardy 	int err;
172138f7b870SPatrick McHardy 
172295a5afcaSJohannes Berg #ifdef CONFIG_MODULES
172338f7b870SPatrick McHardy replay:
17248072f085SThomas Graf #endif
172538f7b870SPatrick McHardy 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
172638f7b870SPatrick McHardy 	if (err < 0)
172738f7b870SPatrick McHardy 		return err;
172838f7b870SPatrick McHardy 
172938f7b870SPatrick McHardy 	if (tb[IFLA_IFNAME])
173038f7b870SPatrick McHardy 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
173138f7b870SPatrick McHardy 	else
173238f7b870SPatrick McHardy 		ifname[0] = '\0';
173338f7b870SPatrick McHardy 
173438f7b870SPatrick McHardy 	ifm = nlmsg_data(nlh);
173538f7b870SPatrick McHardy 	if (ifm->ifi_index > 0)
1736881d966bSEric W. Biederman 		dev = __dev_get_by_index(net, ifm->ifi_index);
1737e7ed828fSVlad Dogaru 	else {
1738e7ed828fSVlad Dogaru 		if (ifname[0])
1739881d966bSEric W. Biederman 			dev = __dev_get_by_name(net, ifname);
174038f7b870SPatrick McHardy 		else
174138f7b870SPatrick McHardy 			dev = NULL;
1742e7ed828fSVlad Dogaru 	}
174338f7b870SPatrick McHardy 
1744e0d087afSEric Dumazet 	err = validate_linkmsg(dev, tb);
1745e0d087afSEric Dumazet 	if (err < 0)
17461840bb13SThomas Graf 		return err;
17471840bb13SThomas Graf 
174838f7b870SPatrick McHardy 	if (tb[IFLA_LINKINFO]) {
174938f7b870SPatrick McHardy 		err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
175038f7b870SPatrick McHardy 				       tb[IFLA_LINKINFO], ifla_info_policy);
175138f7b870SPatrick McHardy 		if (err < 0)
175238f7b870SPatrick McHardy 			return err;
175338f7b870SPatrick McHardy 	} else
175438f7b870SPatrick McHardy 		memset(linkinfo, 0, sizeof(linkinfo));
175538f7b870SPatrick McHardy 
175638f7b870SPatrick McHardy 	if (linkinfo[IFLA_INFO_KIND]) {
175738f7b870SPatrick McHardy 		nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
175838f7b870SPatrick McHardy 		ops = rtnl_link_ops_get(kind);
175938f7b870SPatrick McHardy 	} else {
176038f7b870SPatrick McHardy 		kind[0] = '\0';
176138f7b870SPatrick McHardy 		ops = NULL;
176238f7b870SPatrick McHardy 	}
176338f7b870SPatrick McHardy 
176438f7b870SPatrick McHardy 	if (1) {
176538f7b870SPatrick McHardy 		struct nlattr *attr[ops ? ops->maxtype + 1 : 0], **data = NULL;
176681adee47SEric W. Biederman 		struct net *dest_net;
176738f7b870SPatrick McHardy 
176838f7b870SPatrick McHardy 		if (ops) {
176938f7b870SPatrick McHardy 			if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
177038f7b870SPatrick McHardy 				err = nla_parse_nested(attr, ops->maxtype,
177138f7b870SPatrick McHardy 						       linkinfo[IFLA_INFO_DATA],
177238f7b870SPatrick McHardy 						       ops->policy);
177338f7b870SPatrick McHardy 				if (err < 0)
177438f7b870SPatrick McHardy 					return err;
177538f7b870SPatrick McHardy 				data = attr;
177638f7b870SPatrick McHardy 			}
177738f7b870SPatrick McHardy 			if (ops->validate) {
177838f7b870SPatrick McHardy 				err = ops->validate(tb, data);
177938f7b870SPatrick McHardy 				if (err < 0)
178038f7b870SPatrick McHardy 					return err;
178138f7b870SPatrick McHardy 			}
178238f7b870SPatrick McHardy 		}
178338f7b870SPatrick McHardy 
178438f7b870SPatrick McHardy 		if (dev) {
178538f7b870SPatrick McHardy 			int modified = 0;
178638f7b870SPatrick McHardy 
178738f7b870SPatrick McHardy 			if (nlh->nlmsg_flags & NLM_F_EXCL)
178838f7b870SPatrick McHardy 				return -EEXIST;
178938f7b870SPatrick McHardy 			if (nlh->nlmsg_flags & NLM_F_REPLACE)
179038f7b870SPatrick McHardy 				return -EOPNOTSUPP;
179138f7b870SPatrick McHardy 
179238f7b870SPatrick McHardy 			if (linkinfo[IFLA_INFO_DATA]) {
179338f7b870SPatrick McHardy 				if (!ops || ops != dev->rtnl_link_ops ||
179438f7b870SPatrick McHardy 				    !ops->changelink)
179538f7b870SPatrick McHardy 					return -EOPNOTSUPP;
179638f7b870SPatrick McHardy 
179738f7b870SPatrick McHardy 				err = ops->changelink(dev, tb, data);
179838f7b870SPatrick McHardy 				if (err < 0)
179938f7b870SPatrick McHardy 					return err;
180038f7b870SPatrick McHardy 				modified = 1;
180138f7b870SPatrick McHardy 			}
180238f7b870SPatrick McHardy 
180338f7b870SPatrick McHardy 			return do_setlink(dev, ifm, tb, ifname, modified);
180438f7b870SPatrick McHardy 		}
180538f7b870SPatrick McHardy 
1806ffa934f1SPatrick McHardy 		if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
1807ffa934f1SPatrick McHardy 			if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
1808ffa934f1SPatrick McHardy 				return rtnl_group_changelink(net,
1809ffa934f1SPatrick McHardy 						nla_get_u32(tb[IFLA_GROUP]),
1810ffa934f1SPatrick McHardy 						ifm, tb);
181138f7b870SPatrick McHardy 			return -ENODEV;
1812ffa934f1SPatrick McHardy 		}
181338f7b870SPatrick McHardy 
18140e06877cSPatrick McHardy 		if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
181538f7b870SPatrick McHardy 			return -EOPNOTSUPP;
181638f7b870SPatrick McHardy 
181738f7b870SPatrick McHardy 		if (!ops) {
181895a5afcaSJohannes Berg #ifdef CONFIG_MODULES
181938f7b870SPatrick McHardy 			if (kind[0]) {
182038f7b870SPatrick McHardy 				__rtnl_unlock();
182138f7b870SPatrick McHardy 				request_module("rtnl-link-%s", kind);
182238f7b870SPatrick McHardy 				rtnl_lock();
182338f7b870SPatrick McHardy 				ops = rtnl_link_ops_get(kind);
182438f7b870SPatrick McHardy 				if (ops)
182538f7b870SPatrick McHardy 					goto replay;
182638f7b870SPatrick McHardy 			}
182738f7b870SPatrick McHardy #endif
182838f7b870SPatrick McHardy 			return -EOPNOTSUPP;
182938f7b870SPatrick McHardy 		}
183038f7b870SPatrick McHardy 
183138f7b870SPatrick McHardy 		if (!ifname[0])
183238f7b870SPatrick McHardy 			snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
183338f7b870SPatrick McHardy 
183481adee47SEric W. Biederman 		dest_net = rtnl_link_get_net(net, tb);
183513ad1774SEric W. Biederman 		if (IS_ERR(dest_net))
183613ad1774SEric W. Biederman 			return PTR_ERR(dest_net);
183713ad1774SEric W. Biederman 
1838c0713563SRami Rosen 		dev = rtnl_create_link(dest_net, ifname, ops, tb);
18399c7dafbfSPavel Emelyanov 		if (IS_ERR(dev)) {
1840e7199288SPavel Emelianov 			err = PTR_ERR(dev);
18419c7dafbfSPavel Emelyanov 			goto out;
18429c7dafbfSPavel Emelyanov 		}
18439c7dafbfSPavel Emelyanov 
18449c7dafbfSPavel Emelyanov 		dev->ifindex = ifm->ifi_index;
18459c7dafbfSPavel Emelyanov 
18469c7dafbfSPavel Emelyanov 		if (ops->newlink)
184781adee47SEric W. Biederman 			err = ops->newlink(net, dev, tb, data);
18482d85cba2SPatrick McHardy 		else
18492d85cba2SPatrick McHardy 			err = register_netdevice(dev);
185080032cffSDan Carpenter 
185180032cffSDan Carpenter 		if (err < 0 && !IS_ERR(dev))
185238f7b870SPatrick McHardy 			free_netdev(dev);
185380032cffSDan Carpenter 		if (err < 0)
18543729d502SPatrick McHardy 			goto out;
185581adee47SEric W. Biederman 
18563729d502SPatrick McHardy 		err = rtnl_configure_link(dev, ifm);
18573729d502SPatrick McHardy 		if (err < 0)
18583729d502SPatrick McHardy 			unregister_netdevice(dev);
18593729d502SPatrick McHardy out:
186081adee47SEric W. Biederman 		put_net(dest_net);
186138f7b870SPatrick McHardy 		return err;
186238f7b870SPatrick McHardy 	}
186338f7b870SPatrick McHardy }
186438f7b870SPatrick McHardy 
1865b60c5115SThomas Graf static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
1866711e2c33SJean Tourrilhes {
18673b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
1868b60c5115SThomas Graf 	struct ifinfomsg *ifm;
1869a3d12891SEric Dumazet 	char ifname[IFNAMSIZ];
1870b60c5115SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
1871b60c5115SThomas Graf 	struct net_device *dev = NULL;
1872b60c5115SThomas Graf 	struct sk_buff *nskb;
1873339bf98fSThomas Graf 	int err;
1874115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
1875711e2c33SJean Tourrilhes 
1876b60c5115SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1877b60c5115SThomas Graf 	if (err < 0)
18789918f230SEric Sesterhenn 		return err;
1879b60c5115SThomas Graf 
1880a3d12891SEric Dumazet 	if (tb[IFLA_IFNAME])
1881a3d12891SEric Dumazet 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1882a3d12891SEric Dumazet 
1883115c9b81SGreg Rose 	if (tb[IFLA_EXT_MASK])
1884115c9b81SGreg Rose 		ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1885115c9b81SGreg Rose 
1886b60c5115SThomas Graf 	ifm = nlmsg_data(nlh);
1887a3d12891SEric Dumazet 	if (ifm->ifi_index > 0)
1888a3d12891SEric Dumazet 		dev = __dev_get_by_index(net, ifm->ifi_index);
1889a3d12891SEric Dumazet 	else if (tb[IFLA_IFNAME])
1890a3d12891SEric Dumazet 		dev = __dev_get_by_name(net, ifname);
1891a3d12891SEric Dumazet 	else
1892b60c5115SThomas Graf 		return -EINVAL;
1893b60c5115SThomas Graf 
1894a3d12891SEric Dumazet 	if (dev == NULL)
1895a3d12891SEric Dumazet 		return -ENODEV;
1896a3d12891SEric Dumazet 
1897115c9b81SGreg Rose 	nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
1898a3d12891SEric Dumazet 	if (nskb == NULL)
1899a3d12891SEric Dumazet 		return -ENOBUFS;
1900711e2c33SJean Tourrilhes 
190115e47304SEric W. Biederman 	err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid,
1902115c9b81SGreg Rose 			       nlh->nlmsg_seq, 0, 0, ext_filter_mask);
190326932566SPatrick McHardy 	if (err < 0) {
190426932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in if_nlmsg_size */
190526932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
190626932566SPatrick McHardy 		kfree_skb(nskb);
1907a3d12891SEric Dumazet 	} else
190815e47304SEric W. Biederman 		err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
1909b60c5115SThomas Graf 
1910711e2c33SJean Tourrilhes 	return err;
1911711e2c33SJean Tourrilhes }
1912711e2c33SJean Tourrilhes 
1913115c9b81SGreg Rose static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
1914c7ac8679SGreg Rose {
1915115c9b81SGreg Rose 	struct net *net = sock_net(skb->sk);
1916115c9b81SGreg Rose 	struct net_device *dev;
1917115c9b81SGreg Rose 	struct nlattr *tb[IFLA_MAX+1];
1918115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
1919115c9b81SGreg Rose 	u16 min_ifinfo_dump_size = 0;
1920115c9b81SGreg Rose 
1921a4b64fbeSEric Dumazet 	if (nlmsg_parse(nlh, sizeof(struct rtgenmsg), tb, IFLA_MAX,
1922a4b64fbeSEric Dumazet 			ifla_policy) >= 0) {
1923115c9b81SGreg Rose 		if (tb[IFLA_EXT_MASK])
1924115c9b81SGreg Rose 			ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1925a4b64fbeSEric Dumazet 	}
1926115c9b81SGreg Rose 
1927115c9b81SGreg Rose 	if (!ext_filter_mask)
1928115c9b81SGreg Rose 		return NLMSG_GOODSIZE;
1929115c9b81SGreg Rose 	/*
1930115c9b81SGreg Rose 	 * traverse the list of net devices and compute the minimum
1931115c9b81SGreg Rose 	 * buffer size based upon the filter mask.
1932115c9b81SGreg Rose 	 */
1933115c9b81SGreg Rose 	list_for_each_entry(dev, &net->dev_base_head, dev_list) {
1934115c9b81SGreg Rose 		min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
1935115c9b81SGreg Rose 					     if_nlmsg_size(dev,
1936115c9b81SGreg Rose 						           ext_filter_mask));
1937115c9b81SGreg Rose 	}
1938115c9b81SGreg Rose 
1939c7ac8679SGreg Rose 	return min_ifinfo_dump_size;
1940c7ac8679SGreg Rose }
1941c7ac8679SGreg Rose 
194242bad1daSAdrian Bunk static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
19431da177e4SLinus Torvalds {
19441da177e4SLinus Torvalds 	int idx;
19451da177e4SLinus Torvalds 	int s_idx = cb->family;
19461da177e4SLinus Torvalds 
19471da177e4SLinus Torvalds 	if (s_idx == 0)
19481da177e4SLinus Torvalds 		s_idx = 1;
194925239ceeSPatrick McHardy 	for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
19501da177e4SLinus Torvalds 		int type = cb->nlh->nlmsg_type-RTM_BASE;
19511da177e4SLinus Torvalds 		if (idx < s_idx || idx == PF_PACKET)
19521da177e4SLinus Torvalds 			continue;
1953e2849863SThomas Graf 		if (rtnl_msg_handlers[idx] == NULL ||
1954e2849863SThomas Graf 		    rtnl_msg_handlers[idx][type].dumpit == NULL)
19551da177e4SLinus Torvalds 			continue;
19561da177e4SLinus Torvalds 		if (idx > s_idx)
19571da177e4SLinus Torvalds 			memset(&cb->args[0], 0, sizeof(cb->args));
1958e2849863SThomas Graf 		if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
19591da177e4SLinus Torvalds 			break;
19601da177e4SLinus Torvalds 	}
19611da177e4SLinus Torvalds 	cb->family = idx;
19621da177e4SLinus Torvalds 
19631da177e4SLinus Torvalds 	return skb->len;
19641da177e4SLinus Torvalds }
19651da177e4SLinus Torvalds 
196695c96174SEric Dumazet void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change)
19671da177e4SLinus Torvalds {
1968c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
19691da177e4SLinus Torvalds 	struct sk_buff *skb;
19700ec6d3f4SThomas Graf 	int err = -ENOBUFS;
1971c7ac8679SGreg Rose 	size_t if_info_size;
19721da177e4SLinus Torvalds 
1973115c9b81SGreg Rose 	skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), GFP_KERNEL);
19740ec6d3f4SThomas Graf 	if (skb == NULL)
19750ec6d3f4SThomas Graf 		goto errout;
19761da177e4SLinus Torvalds 
1977115c9b81SGreg Rose 	err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0);
197826932566SPatrick McHardy 	if (err < 0) {
197926932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in if_nlmsg_size() */
198026932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
198126932566SPatrick McHardy 		kfree_skb(skb);
198226932566SPatrick McHardy 		goto errout;
198326932566SPatrick McHardy 	}
19841ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
19851ce85fe4SPablo Neira Ayuso 	return;
19860ec6d3f4SThomas Graf errout:
19870ec6d3f4SThomas Graf 	if (err < 0)
19884b3da706SEric W. Biederman 		rtnl_set_sk_err(net, RTNLGRP_LINK, err);
19891da177e4SLinus Torvalds }
1990*471cb5a3SJiri Pirko EXPORT_SYMBOL(rtmsg_ifinfo);
19911da177e4SLinus Torvalds 
1992d83b0603SJohn Fastabend static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
1993d83b0603SJohn Fastabend 				   struct net_device *dev,
1994d83b0603SJohn Fastabend 				   u8 *addr, u32 pid, u32 seq,
1995d83b0603SJohn Fastabend 				   int type, unsigned int flags)
1996d83b0603SJohn Fastabend {
1997d83b0603SJohn Fastabend 	struct nlmsghdr *nlh;
1998d83b0603SJohn Fastabend 	struct ndmsg *ndm;
1999d83b0603SJohn Fastabend 
2000d83b0603SJohn Fastabend 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), NLM_F_MULTI);
2001d83b0603SJohn Fastabend 	if (!nlh)
2002d83b0603SJohn Fastabend 		return -EMSGSIZE;
2003d83b0603SJohn Fastabend 
2004d83b0603SJohn Fastabend 	ndm = nlmsg_data(nlh);
2005d83b0603SJohn Fastabend 	ndm->ndm_family  = AF_BRIDGE;
2006d83b0603SJohn Fastabend 	ndm->ndm_pad1	 = 0;
2007d83b0603SJohn Fastabend 	ndm->ndm_pad2    = 0;
2008d83b0603SJohn Fastabend 	ndm->ndm_flags	 = flags;
2009d83b0603SJohn Fastabend 	ndm->ndm_type	 = 0;
2010d83b0603SJohn Fastabend 	ndm->ndm_ifindex = dev->ifindex;
2011d83b0603SJohn Fastabend 	ndm->ndm_state   = NUD_PERMANENT;
2012d83b0603SJohn Fastabend 
2013d83b0603SJohn Fastabend 	if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
2014d83b0603SJohn Fastabend 		goto nla_put_failure;
2015d83b0603SJohn Fastabend 
2016d83b0603SJohn Fastabend 	return nlmsg_end(skb, nlh);
2017d83b0603SJohn Fastabend 
2018d83b0603SJohn Fastabend nla_put_failure:
2019d83b0603SJohn Fastabend 	nlmsg_cancel(skb, nlh);
2020d83b0603SJohn Fastabend 	return -EMSGSIZE;
2021d83b0603SJohn Fastabend }
2022d83b0603SJohn Fastabend 
20233ff661c3SJohn Fastabend static inline size_t rtnl_fdb_nlmsg_size(void)
20243ff661c3SJohn Fastabend {
20253ff661c3SJohn Fastabend 	return NLMSG_ALIGN(sizeof(struct ndmsg)) + nla_total_size(ETH_ALEN);
20263ff661c3SJohn Fastabend }
20273ff661c3SJohn Fastabend 
20283ff661c3SJohn Fastabend static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, int type)
20293ff661c3SJohn Fastabend {
20303ff661c3SJohn Fastabend 	struct net *net = dev_net(dev);
20313ff661c3SJohn Fastabend 	struct sk_buff *skb;
20323ff661c3SJohn Fastabend 	int err = -ENOBUFS;
20333ff661c3SJohn Fastabend 
20343ff661c3SJohn Fastabend 	skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
20353ff661c3SJohn Fastabend 	if (!skb)
20363ff661c3SJohn Fastabend 		goto errout;
20373ff661c3SJohn Fastabend 
20383ff661c3SJohn Fastabend 	err = nlmsg_populate_fdb_fill(skb, dev, addr, 0, 0, type, NTF_SELF);
20393ff661c3SJohn Fastabend 	if (err < 0) {
20403ff661c3SJohn Fastabend 		kfree_skb(skb);
20413ff661c3SJohn Fastabend 		goto errout;
20423ff661c3SJohn Fastabend 	}
20433ff661c3SJohn Fastabend 
20443ff661c3SJohn Fastabend 	rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
20453ff661c3SJohn Fastabend 	return;
20463ff661c3SJohn Fastabend errout:
20473ff661c3SJohn Fastabend 	rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
20483ff661c3SJohn Fastabend }
20493ff661c3SJohn Fastabend 
205077162022SJohn Fastabend static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
205177162022SJohn Fastabend {
205277162022SJohn Fastabend 	struct net *net = sock_net(skb->sk);
205377162022SJohn Fastabend 	struct ndmsg *ndm;
205477162022SJohn Fastabend 	struct nlattr *tb[NDA_MAX+1];
205577162022SJohn Fastabend 	struct net_device *dev;
205677162022SJohn Fastabend 	u8 *addr;
205777162022SJohn Fastabend 	int err;
205877162022SJohn Fastabend 
2059dfc47ef8SEric W. Biederman 	if (!capable(CAP_NET_ADMIN))
2060dfc47ef8SEric W. Biederman 		return -EPERM;
2061dfc47ef8SEric W. Biederman 
206277162022SJohn Fastabend 	err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
206377162022SJohn Fastabend 	if (err < 0)
206477162022SJohn Fastabend 		return err;
206577162022SJohn Fastabend 
206677162022SJohn Fastabend 	ndm = nlmsg_data(nlh);
206777162022SJohn Fastabend 	if (ndm->ndm_ifindex == 0) {
206877162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
206977162022SJohn Fastabend 		return -EINVAL;
207077162022SJohn Fastabend 	}
207177162022SJohn Fastabend 
207277162022SJohn Fastabend 	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
207377162022SJohn Fastabend 	if (dev == NULL) {
207477162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
207577162022SJohn Fastabend 		return -ENODEV;
207677162022SJohn Fastabend 	}
207777162022SJohn Fastabend 
207877162022SJohn Fastabend 	if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
207977162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
208077162022SJohn Fastabend 		return -EINVAL;
208177162022SJohn Fastabend 	}
208277162022SJohn Fastabend 
208377162022SJohn Fastabend 	addr = nla_data(tb[NDA_LLADDR]);
208477162022SJohn Fastabend 	if (!is_valid_ether_addr(addr)) {
208577162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ether address\n");
208677162022SJohn Fastabend 		return -EINVAL;
208777162022SJohn Fastabend 	}
208877162022SJohn Fastabend 
208977162022SJohn Fastabend 	err = -EOPNOTSUPP;
209077162022SJohn Fastabend 
209177162022SJohn Fastabend 	/* Support fdb on master device the net/bridge default case */
209277162022SJohn Fastabend 	if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
209377162022SJohn Fastabend 	    (dev->priv_flags & IFF_BRIDGE_PORT)) {
2094898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2095898e5061SJiri Pirko 		const struct net_device_ops *ops = br_dev->netdev_ops;
2096898e5061SJiri Pirko 
2097898e5061SJiri Pirko 		err = ops->ndo_fdb_add(ndm, tb, dev, addr, nlh->nlmsg_flags);
209877162022SJohn Fastabend 		if (err)
209977162022SJohn Fastabend 			goto out;
210077162022SJohn Fastabend 		else
210177162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_MASTER;
210277162022SJohn Fastabend 	}
210377162022SJohn Fastabend 
210477162022SJohn Fastabend 	/* Embedded bridge, macvlan, and any other device support */
210577162022SJohn Fastabend 	if ((ndm->ndm_flags & NTF_SELF) && dev->netdev_ops->ndo_fdb_add) {
2106edc7d573Sstephen hemminger 		err = dev->netdev_ops->ndo_fdb_add(ndm, tb,
2107edc7d573Sstephen hemminger 						   dev, addr,
210877162022SJohn Fastabend 						   nlh->nlmsg_flags);
210977162022SJohn Fastabend 
21103ff661c3SJohn Fastabend 		if (!err) {
21113ff661c3SJohn Fastabend 			rtnl_fdb_notify(dev, addr, RTM_NEWNEIGH);
211277162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_SELF;
211377162022SJohn Fastabend 		}
21143ff661c3SJohn Fastabend 	}
211577162022SJohn Fastabend out:
211677162022SJohn Fastabend 	return err;
211777162022SJohn Fastabend }
211877162022SJohn Fastabend 
211977162022SJohn Fastabend static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
212077162022SJohn Fastabend {
212177162022SJohn Fastabend 	struct net *net = sock_net(skb->sk);
212277162022SJohn Fastabend 	struct ndmsg *ndm;
212377162022SJohn Fastabend 	struct nlattr *llattr;
212477162022SJohn Fastabend 	struct net_device *dev;
212577162022SJohn Fastabend 	int err = -EINVAL;
212677162022SJohn Fastabend 	__u8 *addr;
212777162022SJohn Fastabend 
2128dfc47ef8SEric W. Biederman 	if (!capable(CAP_NET_ADMIN))
2129dfc47ef8SEric W. Biederman 		return -EPERM;
2130dfc47ef8SEric W. Biederman 
213177162022SJohn Fastabend 	if (nlmsg_len(nlh) < sizeof(*ndm))
213277162022SJohn Fastabend 		return -EINVAL;
213377162022SJohn Fastabend 
213477162022SJohn Fastabend 	ndm = nlmsg_data(nlh);
213577162022SJohn Fastabend 	if (ndm->ndm_ifindex == 0) {
213677162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
213777162022SJohn Fastabend 		return -EINVAL;
213877162022SJohn Fastabend 	}
213977162022SJohn Fastabend 
214077162022SJohn Fastabend 	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
214177162022SJohn Fastabend 	if (dev == NULL) {
214277162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
214377162022SJohn Fastabend 		return -ENODEV;
214477162022SJohn Fastabend 	}
214577162022SJohn Fastabend 
214677162022SJohn Fastabend 	llattr = nlmsg_find_attr(nlh, sizeof(*ndm), NDA_LLADDR);
214777162022SJohn Fastabend 	if (llattr == NULL || nla_len(llattr) != ETH_ALEN) {
214877162022SJohn Fastabend 		pr_info("PF_BRIGDE: RTM_DELNEIGH with invalid address\n");
214977162022SJohn Fastabend 		return -EINVAL;
215077162022SJohn Fastabend 	}
215177162022SJohn Fastabend 
215277162022SJohn Fastabend 	addr = nla_data(llattr);
215377162022SJohn Fastabend 	err = -EOPNOTSUPP;
215477162022SJohn Fastabend 
215577162022SJohn Fastabend 	/* Support fdb on master device the net/bridge default case */
215677162022SJohn Fastabend 	if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
215777162022SJohn Fastabend 	    (dev->priv_flags & IFF_BRIDGE_PORT)) {
2158898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2159898e5061SJiri Pirko 		const struct net_device_ops *ops = br_dev->netdev_ops;
216077162022SJohn Fastabend 
2161898e5061SJiri Pirko 		if (ops->ndo_fdb_del)
2162898e5061SJiri Pirko 			err = ops->ndo_fdb_del(ndm, dev, addr);
216377162022SJohn Fastabend 
216477162022SJohn Fastabend 		if (err)
216577162022SJohn Fastabend 			goto out;
216677162022SJohn Fastabend 		else
216777162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_MASTER;
216877162022SJohn Fastabend 	}
216977162022SJohn Fastabend 
217077162022SJohn Fastabend 	/* Embedded bridge, macvlan, and any other device support */
217177162022SJohn Fastabend 	if ((ndm->ndm_flags & NTF_SELF) && dev->netdev_ops->ndo_fdb_del) {
217277162022SJohn Fastabend 		err = dev->netdev_ops->ndo_fdb_del(ndm, dev, addr);
217377162022SJohn Fastabend 
21743ff661c3SJohn Fastabend 		if (!err) {
21753ff661c3SJohn Fastabend 			rtnl_fdb_notify(dev, addr, RTM_DELNEIGH);
217677162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_SELF;
217777162022SJohn Fastabend 		}
21783ff661c3SJohn Fastabend 	}
217977162022SJohn Fastabend out:
218077162022SJohn Fastabend 	return err;
218177162022SJohn Fastabend }
218277162022SJohn Fastabend 
2183d83b0603SJohn Fastabend static int nlmsg_populate_fdb(struct sk_buff *skb,
2184d83b0603SJohn Fastabend 			      struct netlink_callback *cb,
2185d83b0603SJohn Fastabend 			      struct net_device *dev,
2186d83b0603SJohn Fastabend 			      int *idx,
2187d83b0603SJohn Fastabend 			      struct netdev_hw_addr_list *list)
2188d83b0603SJohn Fastabend {
2189d83b0603SJohn Fastabend 	struct netdev_hw_addr *ha;
2190d83b0603SJohn Fastabend 	int err;
219115e47304SEric W. Biederman 	u32 portid, seq;
2192d83b0603SJohn Fastabend 
219315e47304SEric W. Biederman 	portid = NETLINK_CB(cb->skb).portid;
2194d83b0603SJohn Fastabend 	seq = cb->nlh->nlmsg_seq;
2195d83b0603SJohn Fastabend 
2196d83b0603SJohn Fastabend 	list_for_each_entry(ha, &list->list, list) {
2197d83b0603SJohn Fastabend 		if (*idx < cb->args[0])
2198d83b0603SJohn Fastabend 			goto skip;
2199d83b0603SJohn Fastabend 
2200d83b0603SJohn Fastabend 		err = nlmsg_populate_fdb_fill(skb, dev, ha->addr,
2201a7a558feSJohn Fastabend 					      portid, seq,
2202a7a558feSJohn Fastabend 					      RTM_NEWNEIGH, NTF_SELF);
2203d83b0603SJohn Fastabend 		if (err < 0)
2204d83b0603SJohn Fastabend 			return err;
2205d83b0603SJohn Fastabend skip:
2206d83b0603SJohn Fastabend 		*idx += 1;
2207d83b0603SJohn Fastabend 	}
2208d83b0603SJohn Fastabend 	return 0;
2209d83b0603SJohn Fastabend }
2210d83b0603SJohn Fastabend 
2211d83b0603SJohn Fastabend /**
22122c53040fSBen Hutchings  * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
2213d83b0603SJohn Fastabend  * @nlh: netlink message header
2214d83b0603SJohn Fastabend  * @dev: netdevice
2215d83b0603SJohn Fastabend  *
2216d83b0603SJohn Fastabend  * Default netdevice operation to dump the existing unicast address list.
2217d83b0603SJohn Fastabend  * Returns zero on success.
2218d83b0603SJohn Fastabend  */
2219d83b0603SJohn Fastabend int ndo_dflt_fdb_dump(struct sk_buff *skb,
2220d83b0603SJohn Fastabend 		      struct netlink_callback *cb,
2221d83b0603SJohn Fastabend 		      struct net_device *dev,
2222d83b0603SJohn Fastabend 		      int idx)
2223d83b0603SJohn Fastabend {
2224d83b0603SJohn Fastabend 	int err;
2225d83b0603SJohn Fastabend 
2226d83b0603SJohn Fastabend 	netif_addr_lock_bh(dev);
2227d83b0603SJohn Fastabend 	err = nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->uc);
2228d83b0603SJohn Fastabend 	if (err)
2229d83b0603SJohn Fastabend 		goto out;
2230d83b0603SJohn Fastabend 	nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->mc);
2231d83b0603SJohn Fastabend out:
2232d83b0603SJohn Fastabend 	netif_addr_unlock_bh(dev);
2233d83b0603SJohn Fastabend 	return idx;
2234d83b0603SJohn Fastabend }
2235d83b0603SJohn Fastabend EXPORT_SYMBOL(ndo_dflt_fdb_dump);
2236d83b0603SJohn Fastabend 
223777162022SJohn Fastabend static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
223877162022SJohn Fastabend {
223977162022SJohn Fastabend 	int idx = 0;
224077162022SJohn Fastabend 	struct net *net = sock_net(skb->sk);
224177162022SJohn Fastabend 	struct net_device *dev;
224277162022SJohn Fastabend 
224377162022SJohn Fastabend 	rcu_read_lock();
224477162022SJohn Fastabend 	for_each_netdev_rcu(net, dev) {
224577162022SJohn Fastabend 		if (dev->priv_flags & IFF_BRIDGE_PORT) {
2246898e5061SJiri Pirko 			struct net_device *br_dev;
2247898e5061SJiri Pirko 			const struct net_device_ops *ops;
224877162022SJohn Fastabend 
2249898e5061SJiri Pirko 			br_dev = netdev_master_upper_dev_get(dev);
2250898e5061SJiri Pirko 			ops = br_dev->netdev_ops;
225177162022SJohn Fastabend 			if (ops->ndo_fdb_dump)
225277162022SJohn Fastabend 				idx = ops->ndo_fdb_dump(skb, cb, dev, idx);
225377162022SJohn Fastabend 		}
225477162022SJohn Fastabend 
225577162022SJohn Fastabend 		if (dev->netdev_ops->ndo_fdb_dump)
225677162022SJohn Fastabend 			idx = dev->netdev_ops->ndo_fdb_dump(skb, cb, dev, idx);
225777162022SJohn Fastabend 	}
225877162022SJohn Fastabend 	rcu_read_unlock();
225977162022SJohn Fastabend 
226077162022SJohn Fastabend 	cb->args[0] = idx;
226177162022SJohn Fastabend 	return skb->len;
226277162022SJohn Fastabend }
226377162022SJohn Fastabend 
2264815cccbfSJohn Fastabend int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
2265815cccbfSJohn Fastabend 			    struct net_device *dev, u16 mode)
2266815cccbfSJohn Fastabend {
2267815cccbfSJohn Fastabend 	struct nlmsghdr *nlh;
2268815cccbfSJohn Fastabend 	struct ifinfomsg *ifm;
2269815cccbfSJohn Fastabend 	struct nlattr *br_afspec;
2270815cccbfSJohn Fastabend 	u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
2271898e5061SJiri Pirko 	struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2272815cccbfSJohn Fastabend 
2273815cccbfSJohn Fastabend 	nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), NLM_F_MULTI);
2274815cccbfSJohn Fastabend 	if (nlh == NULL)
2275815cccbfSJohn Fastabend 		return -EMSGSIZE;
2276815cccbfSJohn Fastabend 
2277815cccbfSJohn Fastabend 	ifm = nlmsg_data(nlh);
2278815cccbfSJohn Fastabend 	ifm->ifi_family = AF_BRIDGE;
2279815cccbfSJohn Fastabend 	ifm->__ifi_pad = 0;
2280815cccbfSJohn Fastabend 	ifm->ifi_type = dev->type;
2281815cccbfSJohn Fastabend 	ifm->ifi_index = dev->ifindex;
2282815cccbfSJohn Fastabend 	ifm->ifi_flags = dev_get_flags(dev);
2283815cccbfSJohn Fastabend 	ifm->ifi_change = 0;
2284815cccbfSJohn Fastabend 
2285815cccbfSJohn Fastabend 
2286815cccbfSJohn Fastabend 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
2287815cccbfSJohn Fastabend 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
2288815cccbfSJohn Fastabend 	    nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
2289898e5061SJiri Pirko 	    (br_dev &&
2290898e5061SJiri Pirko 	     nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
2291815cccbfSJohn Fastabend 	    (dev->addr_len &&
2292815cccbfSJohn Fastabend 	     nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
2293815cccbfSJohn Fastabend 	    (dev->ifindex != dev->iflink &&
2294815cccbfSJohn Fastabend 	     nla_put_u32(skb, IFLA_LINK, dev->iflink)))
2295815cccbfSJohn Fastabend 		goto nla_put_failure;
2296815cccbfSJohn Fastabend 
2297815cccbfSJohn Fastabend 	br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
2298815cccbfSJohn Fastabend 	if (!br_afspec)
2299815cccbfSJohn Fastabend 		goto nla_put_failure;
2300815cccbfSJohn Fastabend 
2301815cccbfSJohn Fastabend 	if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF) ||
2302815cccbfSJohn Fastabend 	    nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
2303815cccbfSJohn Fastabend 		nla_nest_cancel(skb, br_afspec);
2304815cccbfSJohn Fastabend 		goto nla_put_failure;
2305815cccbfSJohn Fastabend 	}
2306815cccbfSJohn Fastabend 	nla_nest_end(skb, br_afspec);
2307815cccbfSJohn Fastabend 
2308815cccbfSJohn Fastabend 	return nlmsg_end(skb, nlh);
2309815cccbfSJohn Fastabend nla_put_failure:
2310815cccbfSJohn Fastabend 	nlmsg_cancel(skb, nlh);
2311815cccbfSJohn Fastabend 	return -EMSGSIZE;
2312815cccbfSJohn Fastabend }
2313815cccbfSJohn Fastabend EXPORT_SYMBOL(ndo_dflt_bridge_getlink);
2314815cccbfSJohn Fastabend 
2315e5a55a89SJohn Fastabend static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
2316e5a55a89SJohn Fastabend {
2317e5a55a89SJohn Fastabend 	struct net *net = sock_net(skb->sk);
2318e5a55a89SJohn Fastabend 	struct net_device *dev;
2319e5a55a89SJohn Fastabend 	int idx = 0;
2320e5a55a89SJohn Fastabend 	u32 portid = NETLINK_CB(cb->skb).portid;
2321e5a55a89SJohn Fastabend 	u32 seq = cb->nlh->nlmsg_seq;
2322e5a55a89SJohn Fastabend 
2323e5a55a89SJohn Fastabend 	rcu_read_lock();
2324e5a55a89SJohn Fastabend 	for_each_netdev_rcu(net, dev) {
2325e5a55a89SJohn Fastabend 		const struct net_device_ops *ops = dev->netdev_ops;
2326898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2327e5a55a89SJohn Fastabend 
2328898e5061SJiri Pirko 		if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
232925b1e679SBen Hutchings 			if (idx >= cb->args[0] &&
2330898e5061SJiri Pirko 			    br_dev->netdev_ops->ndo_bridge_getlink(
233125b1e679SBen Hutchings 				    skb, portid, seq, dev) < 0)
2332e5a55a89SJohn Fastabend 				break;
2333e5a55a89SJohn Fastabend 			idx++;
2334e5a55a89SJohn Fastabend 		}
2335e5a55a89SJohn Fastabend 
2336e5a55a89SJohn Fastabend 		if (ops->ndo_bridge_getlink) {
233725b1e679SBen Hutchings 			if (idx >= cb->args[0] &&
233825b1e679SBen Hutchings 			    ops->ndo_bridge_getlink(skb, portid, seq, dev) < 0)
2339e5a55a89SJohn Fastabend 				break;
2340e5a55a89SJohn Fastabend 			idx++;
2341e5a55a89SJohn Fastabend 		}
2342e5a55a89SJohn Fastabend 	}
2343e5a55a89SJohn Fastabend 	rcu_read_unlock();
2344e5a55a89SJohn Fastabend 	cb->args[0] = idx;
2345e5a55a89SJohn Fastabend 
2346e5a55a89SJohn Fastabend 	return skb->len;
2347e5a55a89SJohn Fastabend }
2348e5a55a89SJohn Fastabend 
23492469ffd7SJohn Fastabend static inline size_t bridge_nlmsg_size(void)
23502469ffd7SJohn Fastabend {
23512469ffd7SJohn Fastabend 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
23522469ffd7SJohn Fastabend 		+ nla_total_size(IFNAMSIZ)	/* IFLA_IFNAME */
23532469ffd7SJohn Fastabend 		+ nla_total_size(MAX_ADDR_LEN)	/* IFLA_ADDRESS */
23542469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_MASTER */
23552469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_MTU */
23562469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_LINK */
23572469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_OPERSTATE */
23582469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u8))	/* IFLA_PROTINFO */
23592469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(struct nlattr))	/* IFLA_AF_SPEC */
23602469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u16))	/* IFLA_BRIDGE_FLAGS */
23612469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u16));	/* IFLA_BRIDGE_MODE */
23622469ffd7SJohn Fastabend }
23632469ffd7SJohn Fastabend 
23642469ffd7SJohn Fastabend static int rtnl_bridge_notify(struct net_device *dev, u16 flags)
23652469ffd7SJohn Fastabend {
23662469ffd7SJohn Fastabend 	struct net *net = dev_net(dev);
2367898e5061SJiri Pirko 	struct net_device *br_dev = netdev_master_upper_dev_get(dev);
23682469ffd7SJohn Fastabend 	struct sk_buff *skb;
23692469ffd7SJohn Fastabend 	int err = -EOPNOTSUPP;
23702469ffd7SJohn Fastabend 
23712469ffd7SJohn Fastabend 	skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
23722469ffd7SJohn Fastabend 	if (!skb) {
23732469ffd7SJohn Fastabend 		err = -ENOMEM;
23742469ffd7SJohn Fastabend 		goto errout;
23752469ffd7SJohn Fastabend 	}
23762469ffd7SJohn Fastabend 
2377c38e01b8SJohn Fastabend 	if ((!flags || (flags & BRIDGE_FLAGS_MASTER)) &&
2378898e5061SJiri Pirko 	    br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
2379898e5061SJiri Pirko 		err = br_dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev);
23802469ffd7SJohn Fastabend 		if (err < 0)
23812469ffd7SJohn Fastabend 			goto errout;
2382c38e01b8SJohn Fastabend 	}
2383c38e01b8SJohn Fastabend 
2384c38e01b8SJohn Fastabend 	if ((flags & BRIDGE_FLAGS_SELF) &&
2385c38e01b8SJohn Fastabend 	    dev->netdev_ops->ndo_bridge_getlink) {
2386c38e01b8SJohn Fastabend 		err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev);
2387c38e01b8SJohn Fastabend 		if (err < 0)
2388c38e01b8SJohn Fastabend 			goto errout;
2389c38e01b8SJohn Fastabend 	}
23902469ffd7SJohn Fastabend 
23912469ffd7SJohn Fastabend 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
23922469ffd7SJohn Fastabend 	return 0;
23932469ffd7SJohn Fastabend errout:
23942469ffd7SJohn Fastabend 	WARN_ON(err == -EMSGSIZE);
23952469ffd7SJohn Fastabend 	kfree_skb(skb);
23962469ffd7SJohn Fastabend 	rtnl_set_sk_err(net, RTNLGRP_LINK, err);
23972469ffd7SJohn Fastabend 	return err;
23982469ffd7SJohn Fastabend }
23992469ffd7SJohn Fastabend 
2400e5a55a89SJohn Fastabend static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2401e5a55a89SJohn Fastabend 			       void *arg)
2402e5a55a89SJohn Fastabend {
2403e5a55a89SJohn Fastabend 	struct net *net = sock_net(skb->sk);
2404e5a55a89SJohn Fastabend 	struct ifinfomsg *ifm;
2405e5a55a89SJohn Fastabend 	struct net_device *dev;
24062469ffd7SJohn Fastabend 	struct nlattr *br_spec, *attr = NULL;
24072469ffd7SJohn Fastabend 	int rem, err = -EOPNOTSUPP;
2408c38e01b8SJohn Fastabend 	u16 oflags, flags = 0;
2409c38e01b8SJohn Fastabend 	bool have_flags = false;
2410e5a55a89SJohn Fastabend 
2411e5a55a89SJohn Fastabend 	if (nlmsg_len(nlh) < sizeof(*ifm))
2412e5a55a89SJohn Fastabend 		return -EINVAL;
2413e5a55a89SJohn Fastabend 
2414e5a55a89SJohn Fastabend 	ifm = nlmsg_data(nlh);
2415e5a55a89SJohn Fastabend 	if (ifm->ifi_family != AF_BRIDGE)
2416e5a55a89SJohn Fastabend 		return -EPFNOSUPPORT;
2417e5a55a89SJohn Fastabend 
2418e5a55a89SJohn Fastabend 	dev = __dev_get_by_index(net, ifm->ifi_index);
2419e5a55a89SJohn Fastabend 	if (!dev) {
2420e5a55a89SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
2421e5a55a89SJohn Fastabend 		return -ENODEV;
2422e5a55a89SJohn Fastabend 	}
2423e5a55a89SJohn Fastabend 
24242469ffd7SJohn Fastabend 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
24252469ffd7SJohn Fastabend 	if (br_spec) {
24262469ffd7SJohn Fastabend 		nla_for_each_nested(attr, br_spec, rem) {
24272469ffd7SJohn Fastabend 			if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
2428c38e01b8SJohn Fastabend 				have_flags = true;
24292469ffd7SJohn Fastabend 				flags = nla_get_u16(attr);
24302469ffd7SJohn Fastabend 				break;
24312469ffd7SJohn Fastabend 			}
24322469ffd7SJohn Fastabend 		}
24332469ffd7SJohn Fastabend 	}
24342469ffd7SJohn Fastabend 
2435c38e01b8SJohn Fastabend 	oflags = flags;
2436c38e01b8SJohn Fastabend 
24372469ffd7SJohn Fastabend 	if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
2438898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2439898e5061SJiri Pirko 
2440898e5061SJiri Pirko 		if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
24412469ffd7SJohn Fastabend 			err = -EOPNOTSUPP;
2442e5a55a89SJohn Fastabend 			goto out;
2443e5a55a89SJohn Fastabend 		}
2444e5a55a89SJohn Fastabend 
2445898e5061SJiri Pirko 		err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh);
24462469ffd7SJohn Fastabend 		if (err)
24472469ffd7SJohn Fastabend 			goto out;
24482469ffd7SJohn Fastabend 
24492469ffd7SJohn Fastabend 		flags &= ~BRIDGE_FLAGS_MASTER;
24502469ffd7SJohn Fastabend 	}
24512469ffd7SJohn Fastabend 
24522469ffd7SJohn Fastabend 	if ((flags & BRIDGE_FLAGS_SELF)) {
24532469ffd7SJohn Fastabend 		if (!dev->netdev_ops->ndo_bridge_setlink)
24542469ffd7SJohn Fastabend 			err = -EOPNOTSUPP;
24552469ffd7SJohn Fastabend 		else
2456e5a55a89SJohn Fastabend 			err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh);
2457e5a55a89SJohn Fastabend 
24582469ffd7SJohn Fastabend 		if (!err)
24592469ffd7SJohn Fastabend 			flags &= ~BRIDGE_FLAGS_SELF;
24602469ffd7SJohn Fastabend 	}
24612469ffd7SJohn Fastabend 
2462c38e01b8SJohn Fastabend 	if (have_flags)
24632469ffd7SJohn Fastabend 		memcpy(nla_data(attr), &flags, sizeof(flags));
24642469ffd7SJohn Fastabend 	/* Generate event to notify upper layer of bridge change */
24652469ffd7SJohn Fastabend 	if (!err)
2466c38e01b8SJohn Fastabend 		err = rtnl_bridge_notify(dev, oflags);
2467e5a55a89SJohn Fastabend out:
2468e5a55a89SJohn Fastabend 	return err;
2469e5a55a89SJohn Fastabend }
2470e5a55a89SJohn Fastabend 
24711da177e4SLinus Torvalds /* Protected by RTNL sempahore.  */
24721da177e4SLinus Torvalds static struct rtattr **rta_buf;
24731da177e4SLinus Torvalds static int rtattr_max;
24741da177e4SLinus Torvalds 
24751da177e4SLinus Torvalds /* Process one rtnetlink message. */
24761da177e4SLinus Torvalds 
24771d00a4ebSThomas Graf static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
24781da177e4SLinus Torvalds {
24793b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
2480e2849863SThomas Graf 	rtnl_doit_func doit;
24811da177e4SLinus Torvalds 	int sz_idx, kind;
24821da177e4SLinus Torvalds 	int min_len;
24831da177e4SLinus Torvalds 	int family;
24841da177e4SLinus Torvalds 	int type;
24852907c35fSEric Dumazet 	int err;
24861da177e4SLinus Torvalds 
24871da177e4SLinus Torvalds 	type = nlh->nlmsg_type;
24881da177e4SLinus Torvalds 	if (type > RTM_MAX)
2489038890feSThomas Graf 		return -EOPNOTSUPP;
24901da177e4SLinus Torvalds 
24911da177e4SLinus Torvalds 	type -= RTM_BASE;
24921da177e4SLinus Torvalds 
24931da177e4SLinus Torvalds 	/* All the messages must have at least 1 byte length */
24941da177e4SLinus Torvalds 	if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
24951da177e4SLinus Torvalds 		return 0;
24961da177e4SLinus Torvalds 
24971da177e4SLinus Torvalds 	family = ((struct rtgenmsg *)NLMSG_DATA(nlh))->rtgen_family;
24981da177e4SLinus Torvalds 	sz_idx = type>>2;
24991da177e4SLinus Torvalds 	kind = type&3;
25001da177e4SLinus Torvalds 
2501dfc47ef8SEric W. Biederman 	if (kind != 2 && !ns_capable(net->user_ns, CAP_NET_ADMIN))
25021d00a4ebSThomas Graf 		return -EPERM;
25031da177e4SLinus Torvalds 
2504b8f3ab42SDavid S. Miller 	if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
250597c53cacSDenis V. Lunev 		struct sock *rtnl;
2506e2849863SThomas Graf 		rtnl_dumpit_func dumpit;
2507c7ac8679SGreg Rose 		rtnl_calcit_func calcit;
2508c7ac8679SGreg Rose 		u16 min_dump_alloc = 0;
25091da177e4SLinus Torvalds 
2510e2849863SThomas Graf 		dumpit = rtnl_get_dumpit(family, type);
2511e2849863SThomas Graf 		if (dumpit == NULL)
2512038890feSThomas Graf 			return -EOPNOTSUPP;
2513c7ac8679SGreg Rose 		calcit = rtnl_get_calcit(family, type);
2514c7ac8679SGreg Rose 		if (calcit)
2515115c9b81SGreg Rose 			min_dump_alloc = calcit(skb, nlh);
25161da177e4SLinus Torvalds 
25172907c35fSEric Dumazet 		__rtnl_unlock();
251897c53cacSDenis V. Lunev 		rtnl = net->rtnl;
251980d326faSPablo Neira Ayuso 		{
252080d326faSPablo Neira Ayuso 			struct netlink_dump_control c = {
252180d326faSPablo Neira Ayuso 				.dump		= dumpit,
252280d326faSPablo Neira Ayuso 				.min_dump_alloc	= min_dump_alloc,
252380d326faSPablo Neira Ayuso 			};
252480d326faSPablo Neira Ayuso 			err = netlink_dump_start(rtnl, skb, nlh, &c);
252580d326faSPablo Neira Ayuso 		}
25262907c35fSEric Dumazet 		rtnl_lock();
25272907c35fSEric Dumazet 		return err;
25281da177e4SLinus Torvalds 	}
25291da177e4SLinus Torvalds 
25301da177e4SLinus Torvalds 	memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *)));
25311da177e4SLinus Torvalds 
25321da177e4SLinus Torvalds 	min_len = rtm_min[sz_idx];
25331da177e4SLinus Torvalds 	if (nlh->nlmsg_len < min_len)
25341d00a4ebSThomas Graf 		return -EINVAL;
25351da177e4SLinus Torvalds 
25361da177e4SLinus Torvalds 	if (nlh->nlmsg_len > min_len) {
25371da177e4SLinus Torvalds 		int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
25381da177e4SLinus Torvalds 		struct rtattr *attr = (void *)nlh + NLMSG_ALIGN(min_len);
25391da177e4SLinus Torvalds 
25401da177e4SLinus Torvalds 		while (RTA_OK(attr, attrlen)) {
254195c96174SEric Dumazet 			unsigned int flavor = attr->rta_type;
25421da177e4SLinus Torvalds 			if (flavor) {
25431da177e4SLinus Torvalds 				if (flavor > rta_max[sz_idx])
25441d00a4ebSThomas Graf 					return -EINVAL;
25451da177e4SLinus Torvalds 				rta_buf[flavor-1] = attr;
25461da177e4SLinus Torvalds 			}
25471da177e4SLinus Torvalds 			attr = RTA_NEXT(attr, attrlen);
25481da177e4SLinus Torvalds 		}
25491da177e4SLinus Torvalds 	}
25501da177e4SLinus Torvalds 
2551e2849863SThomas Graf 	doit = rtnl_get_doit(family, type);
2552e2849863SThomas Graf 	if (doit == NULL)
2553038890feSThomas Graf 		return -EOPNOTSUPP;
25541da177e4SLinus Torvalds 
25551d00a4ebSThomas Graf 	return doit(skb, nlh, (void *)&rta_buf[0]);
25561da177e4SLinus Torvalds }
25571da177e4SLinus Torvalds 
2558cd40b7d3SDenis V. Lunev static void rtnetlink_rcv(struct sk_buff *skb)
25591da177e4SLinus Torvalds {
25601536cc0dSDenis V. Lunev 	rtnl_lock();
2561cd40b7d3SDenis V. Lunev 	netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
25621536cc0dSDenis V. Lunev 	rtnl_unlock();
25631da177e4SLinus Torvalds }
25641da177e4SLinus Torvalds 
25651da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
25661da177e4SLinus Torvalds {
25671da177e4SLinus Torvalds 	struct net_device *dev = ptr;
2568e9dc8653SEric W. Biederman 
25691da177e4SLinus Torvalds 	switch (event) {
25701da177e4SLinus Torvalds 	case NETDEV_UP:
25711da177e4SLinus Torvalds 	case NETDEV_DOWN:
257210de05afSPatrick McHardy 	case NETDEV_PRE_UP:
2573d90a909eSEric W. Biederman 	case NETDEV_POST_INIT:
2574d90a909eSEric W. Biederman 	case NETDEV_REGISTER:
25751da177e4SLinus Torvalds 	case NETDEV_CHANGE:
2576755d0e77SPatrick McHardy 	case NETDEV_PRE_TYPE_CHANGE:
25771da177e4SLinus Torvalds 	case NETDEV_GOING_DOWN:
2578a2835763SPatrick McHardy 	case NETDEV_UNREGISTER:
25790115e8e3SEric Dumazet 	case NETDEV_UNREGISTER_FINAL:
2580ac3d3f81SAmerigo Wang 	case NETDEV_RELEASE:
2581ac3d3f81SAmerigo Wang 	case NETDEV_JOIN:
25821da177e4SLinus Torvalds 		break;
25831da177e4SLinus Torvalds 	default:
25841da177e4SLinus Torvalds 		rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
25851da177e4SLinus Torvalds 		break;
25861da177e4SLinus Torvalds 	}
25871da177e4SLinus Torvalds 	return NOTIFY_DONE;
25881da177e4SLinus Torvalds }
25891da177e4SLinus Torvalds 
25901da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = {
25911da177e4SLinus Torvalds 	.notifier_call	= rtnetlink_event,
25921da177e4SLinus Torvalds };
25931da177e4SLinus Torvalds 
259497c53cacSDenis V. Lunev 
25952c8c1e72SAlexey Dobriyan static int __net_init rtnetlink_net_init(struct net *net)
259697c53cacSDenis V. Lunev {
259797c53cacSDenis V. Lunev 	struct sock *sk;
2598a31f2d17SPablo Neira Ayuso 	struct netlink_kernel_cfg cfg = {
2599a31f2d17SPablo Neira Ayuso 		.groups		= RTNLGRP_MAX,
2600a31f2d17SPablo Neira Ayuso 		.input		= rtnetlink_rcv,
2601a31f2d17SPablo Neira Ayuso 		.cb_mutex	= &rtnl_mutex,
26029785e10aSPablo Neira Ayuso 		.flags		= NL_CFG_F_NONROOT_RECV,
2603a31f2d17SPablo Neira Ayuso 	};
2604a31f2d17SPablo Neira Ayuso 
26059f00d977SPablo Neira Ayuso 	sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
260697c53cacSDenis V. Lunev 	if (!sk)
260797c53cacSDenis V. Lunev 		return -ENOMEM;
260897c53cacSDenis V. Lunev 	net->rtnl = sk;
260997c53cacSDenis V. Lunev 	return 0;
261097c53cacSDenis V. Lunev }
261197c53cacSDenis V. Lunev 
26122c8c1e72SAlexey Dobriyan static void __net_exit rtnetlink_net_exit(struct net *net)
261397c53cacSDenis V. Lunev {
2614b7c6ba6eSDenis V. Lunev 	netlink_kernel_release(net->rtnl);
261597c53cacSDenis V. Lunev 	net->rtnl = NULL;
261697c53cacSDenis V. Lunev }
261797c53cacSDenis V. Lunev 
261897c53cacSDenis V. Lunev static struct pernet_operations rtnetlink_net_ops = {
261997c53cacSDenis V. Lunev 	.init = rtnetlink_net_init,
262097c53cacSDenis V. Lunev 	.exit = rtnetlink_net_exit,
262197c53cacSDenis V. Lunev };
262297c53cacSDenis V. Lunev 
26231da177e4SLinus Torvalds void __init rtnetlink_init(void)
26241da177e4SLinus Torvalds {
26251da177e4SLinus Torvalds 	int i;
26261da177e4SLinus Torvalds 
26271da177e4SLinus Torvalds 	rtattr_max = 0;
26281da177e4SLinus Torvalds 	for (i = 0; i < ARRAY_SIZE(rta_max); i++)
26291da177e4SLinus Torvalds 		if (rta_max[i] > rtattr_max)
26301da177e4SLinus Torvalds 			rtattr_max = rta_max[i];
26311da177e4SLinus Torvalds 	rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL);
26321da177e4SLinus Torvalds 	if (!rta_buf)
26331da177e4SLinus Torvalds 		panic("rtnetlink_init: cannot allocate rta_buf\n");
26341da177e4SLinus Torvalds 
263597c53cacSDenis V. Lunev 	if (register_pernet_subsys(&rtnetlink_net_ops))
26361da177e4SLinus Torvalds 		panic("rtnetlink_init: cannot initialize rtnetlink\n");
263797c53cacSDenis V. Lunev 
26381da177e4SLinus Torvalds 	register_netdevice_notifier(&rtnetlink_dev_notifier);
2639340d17fcSThomas Graf 
2640c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
2641c7ac8679SGreg Rose 		      rtnl_dump_ifinfo, rtnl_calcit);
2642c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL);
2643c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL);
2644c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL);
2645687ad8ccSThomas Graf 
2646c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL);
2647c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL);
264877162022SJohn Fastabend 
264977162022SJohn Fastabend 	rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, NULL);
265077162022SJohn Fastabend 	rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, NULL);
265177162022SJohn Fastabend 	rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, NULL);
2652e5a55a89SJohn Fastabend 
2653e5a55a89SJohn Fastabend 	rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, NULL);
2654e5a55a89SJohn Fastabend 	rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, NULL);
26551da177e4SLinus Torvalds }
26561da177e4SLinus Torvalds 
2657