xref: /openbmc/linux/net/core/rtnetlink.c (revision 661d2967b3f1b34eeaa7e212e7b9bbe8ee072b59)
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 
51895c96174SEric Dumazet int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
5191da177e4SLinus Torvalds {
52097c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
5211da177e4SLinus Torvalds 	int err = 0;
5221da177e4SLinus Torvalds 
523ac6d439dSPatrick McHardy 	NETLINK_CB(skb).dst_group = group;
5241da177e4SLinus Torvalds 	if (echo)
5251da177e4SLinus Torvalds 		atomic_inc(&skb->users);
5261da177e4SLinus Torvalds 	netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
5271da177e4SLinus Torvalds 	if (echo)
5281da177e4SLinus Torvalds 		err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
5291da177e4SLinus Torvalds 	return err;
5301da177e4SLinus Torvalds }
5311da177e4SLinus Torvalds 
53297c53cacSDenis V. Lunev int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
5332942e900SThomas Graf {
53497c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
53597c53cacSDenis V. Lunev 
5362942e900SThomas Graf 	return nlmsg_unicast(rtnl, skb, pid);
5372942e900SThomas Graf }
538e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unicast);
5392942e900SThomas Graf 
5401ce85fe4SPablo Neira Ayuso void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
54197676b6bSThomas Graf 		 struct nlmsghdr *nlh, gfp_t flags)
54297676b6bSThomas Graf {
54397c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
54497676b6bSThomas Graf 	int report = 0;
54597676b6bSThomas Graf 
54697676b6bSThomas Graf 	if (nlh)
54797676b6bSThomas Graf 		report = nlmsg_report(nlh);
54897676b6bSThomas Graf 
5491ce85fe4SPablo Neira Ayuso 	nlmsg_notify(rtnl, skb, pid, group, report, flags);
55097676b6bSThomas Graf }
551e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_notify);
55297676b6bSThomas Graf 
55397c53cacSDenis V. Lunev void rtnl_set_sk_err(struct net *net, u32 group, int error)
55497676b6bSThomas Graf {
55597c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
55697c53cacSDenis V. Lunev 
55797676b6bSThomas Graf 	netlink_set_err(rtnl, 0, group, error);
55897676b6bSThomas Graf }
559e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_set_sk_err);
56097676b6bSThomas Graf 
5611da177e4SLinus Torvalds int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
5621da177e4SLinus Torvalds {
5632d7202bfSThomas Graf 	struct nlattr *mx;
5642d7202bfSThomas Graf 	int i, valid = 0;
5651da177e4SLinus Torvalds 
5662d7202bfSThomas Graf 	mx = nla_nest_start(skb, RTA_METRICS);
5672d7202bfSThomas Graf 	if (mx == NULL)
5682d7202bfSThomas Graf 		return -ENOBUFS;
5692d7202bfSThomas Graf 
5701da177e4SLinus Torvalds 	for (i = 0; i < RTAX_MAX; i++) {
5712d7202bfSThomas Graf 		if (metrics[i]) {
5722d7202bfSThomas Graf 			valid++;
573a6574349SDavid S. Miller 			if (nla_put_u32(skb, i+1, metrics[i]))
574a6574349SDavid S. Miller 				goto nla_put_failure;
5751da177e4SLinus Torvalds 		}
5762d7202bfSThomas Graf 	}
5771da177e4SLinus Torvalds 
578a57d27fcSDavid S. Miller 	if (!valid) {
579a57d27fcSDavid S. Miller 		nla_nest_cancel(skb, mx);
580a57d27fcSDavid S. Miller 		return 0;
581a57d27fcSDavid S. Miller 	}
5822d7202bfSThomas Graf 
5832d7202bfSThomas Graf 	return nla_nest_end(skb, mx);
5842d7202bfSThomas Graf 
5852d7202bfSThomas Graf nla_put_failure:
586bc3ed28cSThomas Graf 	nla_nest_cancel(skb, mx);
587bc3ed28cSThomas Graf 	return -EMSGSIZE;
5881da177e4SLinus Torvalds }
589e0d087afSEric Dumazet EXPORT_SYMBOL(rtnetlink_put_metrics);
5901da177e4SLinus Torvalds 
591e3703b3dSThomas Graf int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
59287a50699SDavid S. Miller 		       long expires, u32 error)
593e3703b3dSThomas Graf {
594e3703b3dSThomas Graf 	struct rta_cacheinfo ci = {
595a399a805SEric Dumazet 		.rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse),
596e3703b3dSThomas Graf 		.rta_used = dst->__use,
597e3703b3dSThomas Graf 		.rta_clntref = atomic_read(&(dst->__refcnt)),
598e3703b3dSThomas Graf 		.rta_error = error,
599e3703b3dSThomas Graf 		.rta_id =  id,
600e3703b3dSThomas Graf 	};
601e3703b3dSThomas Graf 
6028253947eSLi Wei 	if (expires) {
6038253947eSLi Wei 		unsigned long clock;
604e3703b3dSThomas Graf 
6058253947eSLi Wei 		clock = jiffies_to_clock_t(abs(expires));
6068253947eSLi Wei 		clock = min_t(unsigned long, clock, INT_MAX);
6078253947eSLi Wei 		ci.rta_expires = (expires > 0) ? clock : -clock;
6088253947eSLi Wei 	}
609e3703b3dSThomas Graf 	return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
610e3703b3dSThomas Graf }
611e3703b3dSThomas Graf EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
6121da177e4SLinus Torvalds 
61393b2d4a2SDavid S. Miller static void set_operstate(struct net_device *dev, unsigned char transition)
614b00055aaSStefan Rompf {
615b00055aaSStefan Rompf 	unsigned char operstate = dev->operstate;
616b00055aaSStefan Rompf 
617b00055aaSStefan Rompf 	switch (transition) {
618b00055aaSStefan Rompf 	case IF_OPER_UP:
619b00055aaSStefan Rompf 		if ((operstate == IF_OPER_DORMANT ||
620b00055aaSStefan Rompf 		     operstate == IF_OPER_UNKNOWN) &&
621b00055aaSStefan Rompf 		    !netif_dormant(dev))
622b00055aaSStefan Rompf 			operstate = IF_OPER_UP;
623b00055aaSStefan Rompf 		break;
624b00055aaSStefan Rompf 
625b00055aaSStefan Rompf 	case IF_OPER_DORMANT:
626b00055aaSStefan Rompf 		if (operstate == IF_OPER_UP ||
627b00055aaSStefan Rompf 		    operstate == IF_OPER_UNKNOWN)
628b00055aaSStefan Rompf 			operstate = IF_OPER_DORMANT;
629b00055aaSStefan Rompf 		break;
6303ff50b79SStephen Hemminger 	}
631b00055aaSStefan Rompf 
632b00055aaSStefan Rompf 	if (dev->operstate != operstate) {
633b00055aaSStefan Rompf 		write_lock_bh(&dev_base_lock);
634b00055aaSStefan Rompf 		dev->operstate = operstate;
635b00055aaSStefan Rompf 		write_unlock_bh(&dev_base_lock);
636b00055aaSStefan Rompf 		netdev_state_change(dev);
63793b2d4a2SDavid S. Miller 	}
638b00055aaSStefan Rompf }
639b00055aaSStefan Rompf 
640b1beb681SJiri Benc static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
641b1beb681SJiri Benc {
642b1beb681SJiri Benc 	return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
643b1beb681SJiri Benc 	       (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
644b1beb681SJiri Benc }
645b1beb681SJiri Benc 
6463729d502SPatrick McHardy static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
6473729d502SPatrick McHardy 					   const struct ifinfomsg *ifm)
6483729d502SPatrick McHardy {
6493729d502SPatrick McHardy 	unsigned int flags = ifm->ifi_flags;
6503729d502SPatrick McHardy 
6513729d502SPatrick McHardy 	/* bugwards compatibility: ifi_change == 0 is treated as ~0 */
6523729d502SPatrick McHardy 	if (ifm->ifi_change)
6533729d502SPatrick McHardy 		flags = (flags & ifm->ifi_change) |
654b1beb681SJiri Benc 			(rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
6553729d502SPatrick McHardy 
6563729d502SPatrick McHardy 	return flags;
6573729d502SPatrick McHardy }
6583729d502SPatrick McHardy 
659b60c5115SThomas Graf static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
660be1f3c2cSBen Hutchings 				 const struct rtnl_link_stats64 *b)
6611da177e4SLinus Torvalds {
662b60c5115SThomas Graf 	a->rx_packets = b->rx_packets;
663b60c5115SThomas Graf 	a->tx_packets = b->tx_packets;
664b60c5115SThomas Graf 	a->rx_bytes = b->rx_bytes;
665b60c5115SThomas Graf 	a->tx_bytes = b->tx_bytes;
666b60c5115SThomas Graf 	a->rx_errors = b->rx_errors;
667b60c5115SThomas Graf 	a->tx_errors = b->tx_errors;
668b60c5115SThomas Graf 	a->rx_dropped = b->rx_dropped;
669b60c5115SThomas Graf 	a->tx_dropped = b->tx_dropped;
670b60c5115SThomas Graf 
671b60c5115SThomas Graf 	a->multicast = b->multicast;
672b60c5115SThomas Graf 	a->collisions = b->collisions;
673b60c5115SThomas Graf 
674b60c5115SThomas Graf 	a->rx_length_errors = b->rx_length_errors;
675b60c5115SThomas Graf 	a->rx_over_errors = b->rx_over_errors;
676b60c5115SThomas Graf 	a->rx_crc_errors = b->rx_crc_errors;
677b60c5115SThomas Graf 	a->rx_frame_errors = b->rx_frame_errors;
678b60c5115SThomas Graf 	a->rx_fifo_errors = b->rx_fifo_errors;
679b60c5115SThomas Graf 	a->rx_missed_errors = b->rx_missed_errors;
680b60c5115SThomas Graf 
681b60c5115SThomas Graf 	a->tx_aborted_errors = b->tx_aborted_errors;
682b60c5115SThomas Graf 	a->tx_carrier_errors = b->tx_carrier_errors;
683b60c5115SThomas Graf 	a->tx_fifo_errors = b->tx_fifo_errors;
684b60c5115SThomas Graf 	a->tx_heartbeat_errors = b->tx_heartbeat_errors;
685b60c5115SThomas Graf 	a->tx_window_errors = b->tx_window_errors;
686b60c5115SThomas Graf 
687b60c5115SThomas Graf 	a->rx_compressed = b->rx_compressed;
688b60c5115SThomas Graf 	a->tx_compressed = b->tx_compressed;
68910708f37SJan Engelhardt }
69010708f37SJan Engelhardt 
691be1f3c2cSBen Hutchings static void copy_rtnl_link_stats64(void *v, const struct rtnl_link_stats64 *b)
69210708f37SJan Engelhardt {
693afdcba37SEric Dumazet 	memcpy(v, b, sizeof(*b));
69410708f37SJan Engelhardt }
695b60c5115SThomas Graf 
696c02db8c6SChris Wright /* All VF info */
697115c9b81SGreg Rose static inline int rtnl_vfinfo_size(const struct net_device *dev,
698115c9b81SGreg Rose 				   u32 ext_filter_mask)
699ebc08a6fSWilliams, Mitch A {
700115c9b81SGreg Rose 	if (dev->dev.parent && dev_is_pci(dev->dev.parent) &&
701115c9b81SGreg Rose 	    (ext_filter_mask & RTEXT_FILTER_VF)) {
702c02db8c6SChris Wright 		int num_vfs = dev_num_vf(dev->dev.parent);
703045de01aSScott Feldman 		size_t size = nla_total_size(sizeof(struct nlattr));
704045de01aSScott Feldman 		size += nla_total_size(num_vfs * sizeof(struct nlattr));
705045de01aSScott Feldman 		size += num_vfs *
706045de01aSScott Feldman 			(nla_total_size(sizeof(struct ifla_vf_mac)) +
707045de01aSScott Feldman 			 nla_total_size(sizeof(struct ifla_vf_vlan)) +
7085f8444a3SGreg Rose 			 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
7095f8444a3SGreg Rose 			 nla_total_size(sizeof(struct ifla_vf_spoofchk)));
710c02db8c6SChris Wright 		return size;
711c02db8c6SChris Wright 	} else
712ebc08a6fSWilliams, Mitch A 		return 0;
713ebc08a6fSWilliams, Mitch A }
714ebc08a6fSWilliams, Mitch A 
71557b61080SScott Feldman static size_t rtnl_port_size(const struct net_device *dev)
71657b61080SScott Feldman {
71757b61080SScott Feldman 	size_t port_size = nla_total_size(4)		/* PORT_VF */
71857b61080SScott Feldman 		+ nla_total_size(PORT_PROFILE_MAX)	/* PORT_PROFILE */
71957b61080SScott Feldman 		+ nla_total_size(sizeof(struct ifla_port_vsi))
72057b61080SScott Feldman 							/* PORT_VSI_TYPE */
72157b61080SScott Feldman 		+ nla_total_size(PORT_UUID_MAX)		/* PORT_INSTANCE_UUID */
72257b61080SScott Feldman 		+ nla_total_size(PORT_UUID_MAX)		/* PORT_HOST_UUID */
72357b61080SScott Feldman 		+ nla_total_size(1)			/* PROT_VDP_REQUEST */
72457b61080SScott Feldman 		+ nla_total_size(2);			/* PORT_VDP_RESPONSE */
72557b61080SScott Feldman 	size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
72657b61080SScott Feldman 	size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
72757b61080SScott Feldman 		+ port_size;
72857b61080SScott Feldman 	size_t port_self_size = nla_total_size(sizeof(struct nlattr))
72957b61080SScott Feldman 		+ port_size;
73057b61080SScott Feldman 
73157b61080SScott Feldman 	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent)
73257b61080SScott Feldman 		return 0;
73357b61080SScott Feldman 	if (dev_num_vf(dev->dev.parent))
73457b61080SScott Feldman 		return port_self_size + vf_ports_size +
73557b61080SScott Feldman 			vf_port_size * dev_num_vf(dev->dev.parent);
73657b61080SScott Feldman 	else
73757b61080SScott Feldman 		return port_self_size;
73857b61080SScott Feldman }
73957b61080SScott Feldman 
740115c9b81SGreg Rose static noinline size_t if_nlmsg_size(const struct net_device *dev,
741115c9b81SGreg Rose 				     u32 ext_filter_mask)
742339bf98fSThomas Graf {
743339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
744339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
7450b815a1aSStephen Hemminger 	       + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
746339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
747339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rtnl_link_ifmap))
748339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rtnl_link_stats))
749adcfe196SJan Engelhardt 	       + nla_total_size(sizeof(struct rtnl_link_stats64))
750339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
751339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
752339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_TXQLEN */
753339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_WEIGHT */
754339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MTU */
755339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_LINK */
756339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MASTER */
7579a57247fSJiri Pirko 	       + nla_total_size(1) /* IFLA_CARRIER */
758edbc0bb3SBen Greear 	       + nla_total_size(4) /* IFLA_PROMISCUITY */
75976ff5cc9SJiri Pirko 	       + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
76076ff5cc9SJiri Pirko 	       + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
761339bf98fSThomas Graf 	       + nla_total_size(1) /* IFLA_OPERSTATE */
76238f7b870SPatrick McHardy 	       + nla_total_size(1) /* IFLA_LINKMODE */
763115c9b81SGreg Rose 	       + nla_total_size(ext_filter_mask
764115c9b81SGreg Rose 			        & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
765115c9b81SGreg Rose 	       + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
76657b61080SScott Feldman 	       + rtnl_port_size(dev) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
767f8ff182cSThomas Graf 	       + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
768f8ff182cSThomas Graf 	       + rtnl_link_get_af_size(dev); /* IFLA_AF_SPEC */
769339bf98fSThomas Graf }
770339bf98fSThomas Graf 
77157b61080SScott Feldman static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
77257b61080SScott Feldman {
77357b61080SScott Feldman 	struct nlattr *vf_ports;
77457b61080SScott Feldman 	struct nlattr *vf_port;
77557b61080SScott Feldman 	int vf;
77657b61080SScott Feldman 	int err;
77757b61080SScott Feldman 
77857b61080SScott Feldman 	vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
77957b61080SScott Feldman 	if (!vf_ports)
78057b61080SScott Feldman 		return -EMSGSIZE;
78157b61080SScott Feldman 
78257b61080SScott Feldman 	for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
78357b61080SScott Feldman 		vf_port = nla_nest_start(skb, IFLA_VF_PORT);
7848ca94183SScott Feldman 		if (!vf_port)
7858ca94183SScott Feldman 			goto nla_put_failure;
786a6574349SDavid S. Miller 		if (nla_put_u32(skb, IFLA_PORT_VF, vf))
787a6574349SDavid S. Miller 			goto nla_put_failure;
78857b61080SScott Feldman 		err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
7898ca94183SScott Feldman 		if (err == -EMSGSIZE)
7908ca94183SScott Feldman 			goto nla_put_failure;
79157b61080SScott Feldman 		if (err) {
79257b61080SScott Feldman 			nla_nest_cancel(skb, vf_port);
79357b61080SScott Feldman 			continue;
79457b61080SScott Feldman 		}
79557b61080SScott Feldman 		nla_nest_end(skb, vf_port);
79657b61080SScott Feldman 	}
79757b61080SScott Feldman 
79857b61080SScott Feldman 	nla_nest_end(skb, vf_ports);
79957b61080SScott Feldman 
80057b61080SScott Feldman 	return 0;
8018ca94183SScott Feldman 
8028ca94183SScott Feldman nla_put_failure:
8038ca94183SScott Feldman 	nla_nest_cancel(skb, vf_ports);
8048ca94183SScott Feldman 	return -EMSGSIZE;
80557b61080SScott Feldman }
80657b61080SScott Feldman 
80757b61080SScott Feldman static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
80857b61080SScott Feldman {
80957b61080SScott Feldman 	struct nlattr *port_self;
81057b61080SScott Feldman 	int err;
81157b61080SScott Feldman 
81257b61080SScott Feldman 	port_self = nla_nest_start(skb, IFLA_PORT_SELF);
81357b61080SScott Feldman 	if (!port_self)
81457b61080SScott Feldman 		return -EMSGSIZE;
81557b61080SScott Feldman 
81657b61080SScott Feldman 	err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
81757b61080SScott Feldman 	if (err) {
81857b61080SScott Feldman 		nla_nest_cancel(skb, port_self);
8198ca94183SScott Feldman 		return (err == -EMSGSIZE) ? err : 0;
82057b61080SScott Feldman 	}
82157b61080SScott Feldman 
82257b61080SScott Feldman 	nla_nest_end(skb, port_self);
82357b61080SScott Feldman 
82457b61080SScott Feldman 	return 0;
82557b61080SScott Feldman }
82657b61080SScott Feldman 
82757b61080SScott Feldman static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev)
82857b61080SScott Feldman {
82957b61080SScott Feldman 	int err;
83057b61080SScott Feldman 
83157b61080SScott Feldman 	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent)
83257b61080SScott Feldman 		return 0;
83357b61080SScott Feldman 
83457b61080SScott Feldman 	err = rtnl_port_self_fill(skb, dev);
83557b61080SScott Feldman 	if (err)
83657b61080SScott Feldman 		return err;
83757b61080SScott Feldman 
83857b61080SScott Feldman 	if (dev_num_vf(dev->dev.parent)) {
83957b61080SScott Feldman 		err = rtnl_vf_ports_fill(skb, dev);
84057b61080SScott Feldman 		if (err)
84157b61080SScott Feldman 			return err;
84257b61080SScott Feldman 	}
84357b61080SScott Feldman 
84457b61080SScott Feldman 	return 0;
84557b61080SScott Feldman }
84657b61080SScott Feldman 
847b60c5115SThomas Graf static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
848575c3e2aSPatrick McHardy 			    int type, u32 pid, u32 seq, u32 change,
849115c9b81SGreg Rose 			    unsigned int flags, u32 ext_filter_mask)
850b60c5115SThomas Graf {
851b60c5115SThomas Graf 	struct ifinfomsg *ifm;
8521da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
85328172739SEric Dumazet 	struct rtnl_link_stats64 temp;
854be1f3c2cSBen Hutchings 	const struct rtnl_link_stats64 *stats;
855f8ff182cSThomas Graf 	struct nlattr *attr, *af_spec;
856f8ff182cSThomas Graf 	struct rtnl_af_ops *af_ops;
857898e5061SJiri Pirko 	struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
8581da177e4SLinus Torvalds 
8592907c35fSEric Dumazet 	ASSERT_RTNL();
860b60c5115SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
861b60c5115SThomas Graf 	if (nlh == NULL)
86226932566SPatrick McHardy 		return -EMSGSIZE;
8631da177e4SLinus Torvalds 
864b60c5115SThomas Graf 	ifm = nlmsg_data(nlh);
865b60c5115SThomas Graf 	ifm->ifi_family = AF_UNSPEC;
866b60c5115SThomas Graf 	ifm->__ifi_pad = 0;
867b60c5115SThomas Graf 	ifm->ifi_type = dev->type;
868b60c5115SThomas Graf 	ifm->ifi_index = dev->ifindex;
869b60c5115SThomas Graf 	ifm->ifi_flags = dev_get_flags(dev);
870b60c5115SThomas Graf 	ifm->ifi_change = change;
8711da177e4SLinus Torvalds 
872a6574349SDavid S. Miller 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
873a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
874a6574349SDavid S. Miller 	    nla_put_u8(skb, IFLA_OPERSTATE,
875a6574349SDavid S. Miller 		       netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
876a6574349SDavid S. Miller 	    nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
877a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
878a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_GROUP, dev->group) ||
879edbc0bb3SBen Greear 	    nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
88076ff5cc9SJiri Pirko 	    nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
8811d69c2b3SMark A. Greer #ifdef CONFIG_RPS
88276ff5cc9SJiri Pirko 	    nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
8831d69c2b3SMark A. Greer #endif
884a6574349SDavid S. Miller 	    (dev->ifindex != dev->iflink &&
885a6574349SDavid S. Miller 	     nla_put_u32(skb, IFLA_LINK, dev->iflink)) ||
886898e5061SJiri Pirko 	    (upper_dev &&
887898e5061SJiri Pirko 	     nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) ||
8889a57247fSJiri Pirko 	    nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
889a6574349SDavid S. Miller 	    (dev->qdisc &&
890a6574349SDavid S. Miller 	     nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
891a6574349SDavid S. Miller 	    (dev->ifalias &&
892a6574349SDavid S. Miller 	     nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)))
893a6574349SDavid S. Miller 		goto nla_put_failure;
8940b815a1aSStephen Hemminger 
895b00055aaSStefan Rompf 	if (1) {
8961da177e4SLinus Torvalds 		struct rtnl_link_ifmap map = {
8971da177e4SLinus Torvalds 			.mem_start   = dev->mem_start,
8981da177e4SLinus Torvalds 			.mem_end     = dev->mem_end,
8991da177e4SLinus Torvalds 			.base_addr   = dev->base_addr,
9001da177e4SLinus Torvalds 			.irq         = dev->irq,
9011da177e4SLinus Torvalds 			.dma         = dev->dma,
9021da177e4SLinus Torvalds 			.port        = dev->if_port,
9031da177e4SLinus Torvalds 		};
904a6574349SDavid S. Miller 		if (nla_put(skb, IFLA_MAP, sizeof(map), &map))
905a6574349SDavid S. Miller 			goto nla_put_failure;
9061da177e4SLinus Torvalds 	}
9071da177e4SLinus Torvalds 
9081da177e4SLinus Torvalds 	if (dev->addr_len) {
909a6574349SDavid S. Miller 		if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
910a6574349SDavid S. Miller 		    nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
911a6574349SDavid S. Miller 			goto nla_put_failure;
9121da177e4SLinus Torvalds 	}
9131da177e4SLinus Torvalds 
914b60c5115SThomas Graf 	attr = nla_reserve(skb, IFLA_STATS,
915b60c5115SThomas Graf 			sizeof(struct rtnl_link_stats));
916b60c5115SThomas Graf 	if (attr == NULL)
917b60c5115SThomas Graf 		goto nla_put_failure;
918b60c5115SThomas Graf 
91928172739SEric Dumazet 	stats = dev_get_stats(dev, &temp);
920b60c5115SThomas Graf 	copy_rtnl_link_stats(nla_data(attr), stats);
9211da177e4SLinus Torvalds 
92210708f37SJan Engelhardt 	attr = nla_reserve(skb, IFLA_STATS64,
92310708f37SJan Engelhardt 			sizeof(struct rtnl_link_stats64));
92410708f37SJan Engelhardt 	if (attr == NULL)
92510708f37SJan Engelhardt 		goto nla_put_failure;
92610708f37SJan Engelhardt 	copy_rtnl_link_stats64(nla_data(attr), stats);
92710708f37SJan Engelhardt 
928a6574349SDavid S. Miller 	if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) &&
929a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)))
930a6574349SDavid S. Miller 		goto nla_put_failure;
93157b61080SScott Feldman 
932115c9b81SGreg Rose 	if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent
933115c9b81SGreg Rose 	    && (ext_filter_mask & RTEXT_FILTER_VF)) {
934ebc08a6fSWilliams, Mitch A 		int i;
935ebc08a6fSWilliams, Mitch A 
936c02db8c6SChris Wright 		struct nlattr *vfinfo, *vf;
937c02db8c6SChris Wright 		int num_vfs = dev_num_vf(dev->dev.parent);
938c02db8c6SChris Wright 
939c02db8c6SChris Wright 		vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
940c02db8c6SChris Wright 		if (!vfinfo)
941c02db8c6SChris Wright 			goto nla_put_failure;
942c02db8c6SChris Wright 		for (i = 0; i < num_vfs; i++) {
943c02db8c6SChris Wright 			struct ifla_vf_info ivi;
944c02db8c6SChris Wright 			struct ifla_vf_mac vf_mac;
945c02db8c6SChris Wright 			struct ifla_vf_vlan vf_vlan;
946c02db8c6SChris Wright 			struct ifla_vf_tx_rate vf_tx_rate;
9475f8444a3SGreg Rose 			struct ifla_vf_spoofchk vf_spoofchk;
9485f8444a3SGreg Rose 
9495f8444a3SGreg Rose 			/*
9505f8444a3SGreg Rose 			 * Not all SR-IOV capable drivers support the
9515f8444a3SGreg Rose 			 * spoofcheck query.  Preset to -1 so the user
9525f8444a3SGreg Rose 			 * space tool can detect that the driver didn't
9535f8444a3SGreg Rose 			 * report anything.
9545f8444a3SGreg Rose 			 */
9555f8444a3SGreg Rose 			ivi.spoofchk = -1;
95684d73cd3SMathias Krause 			memset(ivi.mac, 0, sizeof(ivi.mac));
957ebc08a6fSWilliams, Mitch A 			if (dev->netdev_ops->ndo_get_vf_config(dev, i, &ivi))
958ebc08a6fSWilliams, Mitch A 				break;
9595f8444a3SGreg Rose 			vf_mac.vf =
9605f8444a3SGreg Rose 				vf_vlan.vf =
9615f8444a3SGreg Rose 				vf_tx_rate.vf =
9625f8444a3SGreg Rose 				vf_spoofchk.vf = ivi.vf;
9635f8444a3SGreg Rose 
964c02db8c6SChris Wright 			memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
965c02db8c6SChris Wright 			vf_vlan.vlan = ivi.vlan;
966c02db8c6SChris Wright 			vf_vlan.qos = ivi.qos;
967c02db8c6SChris Wright 			vf_tx_rate.rate = ivi.tx_rate;
9685f8444a3SGreg Rose 			vf_spoofchk.setting = ivi.spoofchk;
969c02db8c6SChris Wright 			vf = nla_nest_start(skb, IFLA_VF_INFO);
970c02db8c6SChris Wright 			if (!vf) {
971c02db8c6SChris Wright 				nla_nest_cancel(skb, vfinfo);
972c02db8c6SChris Wright 				goto nla_put_failure;
973ebc08a6fSWilliams, Mitch A 			}
974a6574349SDavid S. Miller 			if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
975a6574349SDavid S. Miller 			    nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
976a6574349SDavid S. Miller 			    nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
977a6574349SDavid S. Miller 				    &vf_tx_rate) ||
978a6574349SDavid S. Miller 			    nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
979a6574349SDavid S. Miller 				    &vf_spoofchk))
980a6574349SDavid S. Miller 				goto nla_put_failure;
981c02db8c6SChris Wright 			nla_nest_end(skb, vf);
982c02db8c6SChris Wright 		}
983c02db8c6SChris Wright 		nla_nest_end(skb, vfinfo);
984ebc08a6fSWilliams, Mitch A 	}
98557b61080SScott Feldman 
98657b61080SScott Feldman 	if (rtnl_port_fill(skb, dev))
98757b61080SScott Feldman 		goto nla_put_failure;
98857b61080SScott Feldman 
98938f7b870SPatrick McHardy 	if (dev->rtnl_link_ops) {
99038f7b870SPatrick McHardy 		if (rtnl_link_fill(skb, dev) < 0)
99138f7b870SPatrick McHardy 			goto nla_put_failure;
99238f7b870SPatrick McHardy 	}
99338f7b870SPatrick McHardy 
994f8ff182cSThomas Graf 	if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
995f8ff182cSThomas Graf 		goto nla_put_failure;
996f8ff182cSThomas Graf 
997f8ff182cSThomas Graf 	list_for_each_entry(af_ops, &rtnl_af_ops, list) {
998f8ff182cSThomas Graf 		if (af_ops->fill_link_af) {
999f8ff182cSThomas Graf 			struct nlattr *af;
1000f8ff182cSThomas Graf 			int err;
1001f8ff182cSThomas Graf 
1002f8ff182cSThomas Graf 			if (!(af = nla_nest_start(skb, af_ops->family)))
1003f8ff182cSThomas Graf 				goto nla_put_failure;
1004f8ff182cSThomas Graf 
1005f8ff182cSThomas Graf 			err = af_ops->fill_link_af(skb, dev);
1006f8ff182cSThomas Graf 
1007f8ff182cSThomas Graf 			/*
1008f8ff182cSThomas Graf 			 * Caller may return ENODATA to indicate that there
1009f8ff182cSThomas Graf 			 * was no data to be dumped. This is not an error, it
1010f8ff182cSThomas Graf 			 * means we should trim the attribute header and
1011f8ff182cSThomas Graf 			 * continue.
1012f8ff182cSThomas Graf 			 */
1013f8ff182cSThomas Graf 			if (err == -ENODATA)
1014f8ff182cSThomas Graf 				nla_nest_cancel(skb, af);
1015f8ff182cSThomas Graf 			else if (err < 0)
1016f8ff182cSThomas Graf 				goto nla_put_failure;
1017f8ff182cSThomas Graf 
1018f8ff182cSThomas Graf 			nla_nest_end(skb, af);
1019f8ff182cSThomas Graf 		}
1020f8ff182cSThomas Graf 	}
1021f8ff182cSThomas Graf 
1022f8ff182cSThomas Graf 	nla_nest_end(skb, af_spec);
1023f8ff182cSThomas Graf 
1024b60c5115SThomas Graf 	return nlmsg_end(skb, nlh);
1025b60c5115SThomas Graf 
1026b60c5115SThomas Graf nla_put_failure:
102726932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
102826932566SPatrick McHardy 	return -EMSGSIZE;
10291da177e4SLinus Torvalds }
10301da177e4SLinus Torvalds 
1031b60c5115SThomas Graf static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
10321da177e4SLinus Torvalds {
10333b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
10347c28bd0bSEric Dumazet 	int h, s_h;
10357c28bd0bSEric Dumazet 	int idx = 0, s_idx;
10361da177e4SLinus Torvalds 	struct net_device *dev;
10377c28bd0bSEric Dumazet 	struct hlist_head *head;
1038115c9b81SGreg Rose 	struct nlattr *tb[IFLA_MAX+1];
1039115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
10401da177e4SLinus Torvalds 
10417c28bd0bSEric Dumazet 	s_h = cb->args[0];
10427c28bd0bSEric Dumazet 	s_idx = cb->args[1];
10437c28bd0bSEric Dumazet 
1044e67f88ddSEric Dumazet 	rcu_read_lock();
10454e985adaSThomas Graf 	cb->seq = net->dev_base_seq;
10464e985adaSThomas Graf 
1047a4b64fbeSEric Dumazet 	if (nlmsg_parse(cb->nlh, sizeof(struct rtgenmsg), tb, IFLA_MAX,
1048a4b64fbeSEric Dumazet 			ifla_policy) >= 0) {
1049115c9b81SGreg Rose 
1050115c9b81SGreg Rose 		if (tb[IFLA_EXT_MASK])
1051115c9b81SGreg Rose 			ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1052a4b64fbeSEric Dumazet 	}
1053115c9b81SGreg Rose 
10547c28bd0bSEric Dumazet 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
10557562f876SPavel Emelianov 		idx = 0;
10567c28bd0bSEric Dumazet 		head = &net->dev_index_head[h];
1057b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu(dev, head, index_hlist) {
10581da177e4SLinus Torvalds 			if (idx < s_idx)
10597562f876SPavel Emelianov 				goto cont;
1060575c3e2aSPatrick McHardy 			if (rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
106115e47304SEric W. Biederman 					     NETLINK_CB(cb->skb).portid,
10627c28bd0bSEric Dumazet 					     cb->nlh->nlmsg_seq, 0,
1063115c9b81SGreg Rose 					     NLM_F_MULTI,
1064115c9b81SGreg Rose 					     ext_filter_mask) <= 0)
10657c28bd0bSEric Dumazet 				goto out;
10664e985adaSThomas Graf 
10674e985adaSThomas Graf 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
10687562f876SPavel Emelianov cont:
10697562f876SPavel Emelianov 			idx++;
10701da177e4SLinus Torvalds 		}
10717c28bd0bSEric Dumazet 	}
10727c28bd0bSEric Dumazet out:
1073e67f88ddSEric Dumazet 	rcu_read_unlock();
10747c28bd0bSEric Dumazet 	cb->args[1] = idx;
10757c28bd0bSEric Dumazet 	cb->args[0] = h;
10761da177e4SLinus Torvalds 
10771da177e4SLinus Torvalds 	return skb->len;
10781da177e4SLinus Torvalds }
10791da177e4SLinus Torvalds 
1080e7199288SPavel Emelianov const struct nla_policy ifla_policy[IFLA_MAX+1] = {
10815176f91eSThomas Graf 	[IFLA_IFNAME]		= { .type = NLA_STRING, .len = IFNAMSIZ-1 },
108238f7b870SPatrick McHardy 	[IFLA_ADDRESS]		= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
108338f7b870SPatrick McHardy 	[IFLA_BROADCAST]	= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
10845176f91eSThomas Graf 	[IFLA_MAP]		= { .len = sizeof(struct rtnl_link_ifmap) },
1085da5e0494SThomas Graf 	[IFLA_MTU]		= { .type = NLA_U32 },
108676e87306SThomas Graf 	[IFLA_LINK]		= { .type = NLA_U32 },
1087fbaec0eaSJiri Pirko 	[IFLA_MASTER]		= { .type = NLA_U32 },
10889a57247fSJiri Pirko 	[IFLA_CARRIER]		= { .type = NLA_U8 },
1089da5e0494SThomas Graf 	[IFLA_TXQLEN]		= { .type = NLA_U32 },
1090da5e0494SThomas Graf 	[IFLA_WEIGHT]		= { .type = NLA_U32 },
1091da5e0494SThomas Graf 	[IFLA_OPERSTATE]	= { .type = NLA_U8 },
1092da5e0494SThomas Graf 	[IFLA_LINKMODE]		= { .type = NLA_U8 },
109376e87306SThomas Graf 	[IFLA_LINKINFO]		= { .type = NLA_NESTED },
1094d8a5ec67SEric W. Biederman 	[IFLA_NET_NS_PID]	= { .type = NLA_U32 },
1095f0630529SEric W. Biederman 	[IFLA_NET_NS_FD]	= { .type = NLA_U32 },
10960b815a1aSStephen Hemminger 	[IFLA_IFALIAS]	        = { .type = NLA_STRING, .len = IFALIASZ-1 },
1097c02db8c6SChris Wright 	[IFLA_VFINFO_LIST]	= {. type = NLA_NESTED },
109857b61080SScott Feldman 	[IFLA_VF_PORTS]		= { .type = NLA_NESTED },
109957b61080SScott Feldman 	[IFLA_PORT_SELF]	= { .type = NLA_NESTED },
1100f8ff182cSThomas Graf 	[IFLA_AF_SPEC]		= { .type = NLA_NESTED },
1101115c9b81SGreg Rose 	[IFLA_EXT_MASK]		= { .type = NLA_U32 },
1102edbc0bb3SBen Greear 	[IFLA_PROMISCUITY]	= { .type = NLA_U32 },
110376ff5cc9SJiri Pirko 	[IFLA_NUM_TX_QUEUES]	= { .type = NLA_U32 },
110476ff5cc9SJiri Pirko 	[IFLA_NUM_RX_QUEUES]	= { .type = NLA_U32 },
1105da5e0494SThomas Graf };
1106e0d087afSEric Dumazet EXPORT_SYMBOL(ifla_policy);
11071da177e4SLinus Torvalds 
110838f7b870SPatrick McHardy static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
110938f7b870SPatrick McHardy 	[IFLA_INFO_KIND]	= { .type = NLA_STRING },
111038f7b870SPatrick McHardy 	[IFLA_INFO_DATA]	= { .type = NLA_NESTED },
111138f7b870SPatrick McHardy };
111238f7b870SPatrick McHardy 
1113c02db8c6SChris Wright static const struct nla_policy ifla_vfinfo_policy[IFLA_VF_INFO_MAX+1] = {
1114c02db8c6SChris Wright 	[IFLA_VF_INFO]		= { .type = NLA_NESTED },
1115c02db8c6SChris Wright };
1116c02db8c6SChris Wright 
1117c02db8c6SChris Wright static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1118c02db8c6SChris Wright 	[IFLA_VF_MAC]		= { .type = NLA_BINARY,
1119c02db8c6SChris Wright 				    .len = sizeof(struct ifla_vf_mac) },
1120c02db8c6SChris Wright 	[IFLA_VF_VLAN]		= { .type = NLA_BINARY,
1121c02db8c6SChris Wright 				    .len = sizeof(struct ifla_vf_vlan) },
1122c02db8c6SChris Wright 	[IFLA_VF_TX_RATE]	= { .type = NLA_BINARY,
1123c02db8c6SChris Wright 				    .len = sizeof(struct ifla_vf_tx_rate) },
112448752f65SGreg Rose 	[IFLA_VF_SPOOFCHK]	= { .type = NLA_BINARY,
112548752f65SGreg Rose 				    .len = sizeof(struct ifla_vf_spoofchk) },
1126c02db8c6SChris Wright };
1127c02db8c6SChris Wright 
112857b61080SScott Feldman static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
112957b61080SScott Feldman 	[IFLA_PORT_VF]		= { .type = NLA_U32 },
113057b61080SScott Feldman 	[IFLA_PORT_PROFILE]	= { .type = NLA_STRING,
113157b61080SScott Feldman 				    .len = PORT_PROFILE_MAX },
113257b61080SScott Feldman 	[IFLA_PORT_VSI_TYPE]	= { .type = NLA_BINARY,
113357b61080SScott Feldman 				    .len = sizeof(struct ifla_port_vsi)},
113457b61080SScott Feldman 	[IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
113557b61080SScott Feldman 				      .len = PORT_UUID_MAX },
113657b61080SScott Feldman 	[IFLA_PORT_HOST_UUID]	= { .type = NLA_STRING,
113757b61080SScott Feldman 				    .len = PORT_UUID_MAX },
113857b61080SScott Feldman 	[IFLA_PORT_REQUEST]	= { .type = NLA_U8, },
113957b61080SScott Feldman 	[IFLA_PORT_RESPONSE]	= { .type = NLA_U16, },
114057b61080SScott Feldman };
114157b61080SScott Feldman 
114281adee47SEric W. Biederman struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
114381adee47SEric W. Biederman {
114481adee47SEric W. Biederman 	struct net *net;
114581adee47SEric W. Biederman 	/* Examine the link attributes and figure out which
114681adee47SEric W. Biederman 	 * network namespace we are talking about.
114781adee47SEric W. Biederman 	 */
114881adee47SEric W. Biederman 	if (tb[IFLA_NET_NS_PID])
114981adee47SEric W. Biederman 		net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
1150f0630529SEric W. Biederman 	else if (tb[IFLA_NET_NS_FD])
1151f0630529SEric W. Biederman 		net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
115281adee47SEric W. Biederman 	else
115381adee47SEric W. Biederman 		net = get_net(src_net);
115481adee47SEric W. Biederman 	return net;
115581adee47SEric W. Biederman }
115681adee47SEric W. Biederman EXPORT_SYMBOL(rtnl_link_get_net);
115781adee47SEric W. Biederman 
11581840bb13SThomas Graf static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
11591840bb13SThomas Graf {
11601840bb13SThomas Graf 	if (dev) {
11611840bb13SThomas Graf 		if (tb[IFLA_ADDRESS] &&
11621840bb13SThomas Graf 		    nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
11631840bb13SThomas Graf 			return -EINVAL;
11641840bb13SThomas Graf 
11651840bb13SThomas Graf 		if (tb[IFLA_BROADCAST] &&
11661840bb13SThomas Graf 		    nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
11671840bb13SThomas Graf 			return -EINVAL;
11681840bb13SThomas Graf 	}
11691840bb13SThomas Graf 
1170cf7afbfeSThomas Graf 	if (tb[IFLA_AF_SPEC]) {
1171cf7afbfeSThomas Graf 		struct nlattr *af;
1172cf7afbfeSThomas Graf 		int rem, err;
1173cf7afbfeSThomas Graf 
1174cf7afbfeSThomas Graf 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1175cf7afbfeSThomas Graf 			const struct rtnl_af_ops *af_ops;
1176cf7afbfeSThomas Graf 
1177cf7afbfeSThomas Graf 			if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1178cf7afbfeSThomas Graf 				return -EAFNOSUPPORT;
1179cf7afbfeSThomas Graf 
1180cf7afbfeSThomas Graf 			if (!af_ops->set_link_af)
1181cf7afbfeSThomas Graf 				return -EOPNOTSUPP;
1182cf7afbfeSThomas Graf 
1183cf7afbfeSThomas Graf 			if (af_ops->validate_link_af) {
11846d3a9a68SKurt Van Dijck 				err = af_ops->validate_link_af(dev, af);
1185cf7afbfeSThomas Graf 				if (err < 0)
1186cf7afbfeSThomas Graf 					return err;
1187cf7afbfeSThomas Graf 			}
1188cf7afbfeSThomas Graf 		}
1189cf7afbfeSThomas Graf 	}
1190cf7afbfeSThomas Graf 
11911840bb13SThomas Graf 	return 0;
11921840bb13SThomas Graf }
11931840bb13SThomas Graf 
1194c02db8c6SChris Wright static int do_setvfinfo(struct net_device *dev, struct nlattr *attr)
1195c02db8c6SChris Wright {
1196c02db8c6SChris Wright 	int rem, err = -EINVAL;
1197c02db8c6SChris Wright 	struct nlattr *vf;
1198c02db8c6SChris Wright 	const struct net_device_ops *ops = dev->netdev_ops;
1199c02db8c6SChris Wright 
1200c02db8c6SChris Wright 	nla_for_each_nested(vf, attr, rem) {
1201c02db8c6SChris Wright 		switch (nla_type(vf)) {
1202c02db8c6SChris Wright 		case IFLA_VF_MAC: {
1203c02db8c6SChris Wright 			struct ifla_vf_mac *ivm;
1204c02db8c6SChris Wright 			ivm = nla_data(vf);
1205c02db8c6SChris Wright 			err = -EOPNOTSUPP;
1206c02db8c6SChris Wright 			if (ops->ndo_set_vf_mac)
1207c02db8c6SChris Wright 				err = ops->ndo_set_vf_mac(dev, ivm->vf,
1208c02db8c6SChris Wright 							  ivm->mac);
1209c02db8c6SChris Wright 			break;
1210c02db8c6SChris Wright 		}
1211c02db8c6SChris Wright 		case IFLA_VF_VLAN: {
1212c02db8c6SChris Wright 			struct ifla_vf_vlan *ivv;
1213c02db8c6SChris Wright 			ivv = nla_data(vf);
1214c02db8c6SChris Wright 			err = -EOPNOTSUPP;
1215c02db8c6SChris Wright 			if (ops->ndo_set_vf_vlan)
1216c02db8c6SChris Wright 				err = ops->ndo_set_vf_vlan(dev, ivv->vf,
1217c02db8c6SChris Wright 							   ivv->vlan,
1218c02db8c6SChris Wright 							   ivv->qos);
1219c02db8c6SChris Wright 			break;
1220c02db8c6SChris Wright 		}
1221c02db8c6SChris Wright 		case IFLA_VF_TX_RATE: {
1222c02db8c6SChris Wright 			struct ifla_vf_tx_rate *ivt;
1223c02db8c6SChris Wright 			ivt = nla_data(vf);
1224c02db8c6SChris Wright 			err = -EOPNOTSUPP;
1225c02db8c6SChris Wright 			if (ops->ndo_set_vf_tx_rate)
1226c02db8c6SChris Wright 				err = ops->ndo_set_vf_tx_rate(dev, ivt->vf,
1227c02db8c6SChris Wright 							      ivt->rate);
1228c02db8c6SChris Wright 			break;
1229c02db8c6SChris Wright 		}
12305f8444a3SGreg Rose 		case IFLA_VF_SPOOFCHK: {
12315f8444a3SGreg Rose 			struct ifla_vf_spoofchk *ivs;
12325f8444a3SGreg Rose 			ivs = nla_data(vf);
12335f8444a3SGreg Rose 			err = -EOPNOTSUPP;
12345f8444a3SGreg Rose 			if (ops->ndo_set_vf_spoofchk)
12355f8444a3SGreg Rose 				err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
12365f8444a3SGreg Rose 							       ivs->setting);
12375f8444a3SGreg Rose 			break;
12385f8444a3SGreg Rose 		}
1239c02db8c6SChris Wright 		default:
1240c02db8c6SChris Wright 			err = -EINVAL;
1241c02db8c6SChris Wright 			break;
1242c02db8c6SChris Wright 		}
1243c02db8c6SChris Wright 		if (err)
1244c02db8c6SChris Wright 			break;
1245c02db8c6SChris Wright 	}
1246c02db8c6SChris Wright 	return err;
1247c02db8c6SChris Wright }
1248c02db8c6SChris Wright 
1249fbaec0eaSJiri Pirko static int do_set_master(struct net_device *dev, int ifindex)
1250fbaec0eaSJiri Pirko {
1251898e5061SJiri Pirko 	struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
1252fbaec0eaSJiri Pirko 	const struct net_device_ops *ops;
1253fbaec0eaSJiri Pirko 	int err;
1254fbaec0eaSJiri Pirko 
1255898e5061SJiri Pirko 	if (upper_dev) {
1256898e5061SJiri Pirko 		if (upper_dev->ifindex == ifindex)
1257fbaec0eaSJiri Pirko 			return 0;
1258898e5061SJiri Pirko 		ops = upper_dev->netdev_ops;
1259fbaec0eaSJiri Pirko 		if (ops->ndo_del_slave) {
1260898e5061SJiri Pirko 			err = ops->ndo_del_slave(upper_dev, dev);
1261fbaec0eaSJiri Pirko 			if (err)
1262fbaec0eaSJiri Pirko 				return err;
1263fbaec0eaSJiri Pirko 		} else {
1264fbaec0eaSJiri Pirko 			return -EOPNOTSUPP;
1265fbaec0eaSJiri Pirko 		}
1266fbaec0eaSJiri Pirko 	}
1267fbaec0eaSJiri Pirko 
1268fbaec0eaSJiri Pirko 	if (ifindex) {
1269898e5061SJiri Pirko 		upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
1270898e5061SJiri Pirko 		if (!upper_dev)
1271fbaec0eaSJiri Pirko 			return -EINVAL;
1272898e5061SJiri Pirko 		ops = upper_dev->netdev_ops;
1273fbaec0eaSJiri Pirko 		if (ops->ndo_add_slave) {
1274898e5061SJiri Pirko 			err = ops->ndo_add_slave(upper_dev, dev);
1275fbaec0eaSJiri Pirko 			if (err)
1276fbaec0eaSJiri Pirko 				return err;
1277fbaec0eaSJiri Pirko 		} else {
1278fbaec0eaSJiri Pirko 			return -EOPNOTSUPP;
1279fbaec0eaSJiri Pirko 		}
1280fbaec0eaSJiri Pirko 	}
1281fbaec0eaSJiri Pirko 	return 0;
1282fbaec0eaSJiri Pirko }
1283fbaec0eaSJiri Pirko 
12840157f60cSPatrick McHardy static int do_setlink(struct net_device *dev, struct ifinfomsg *ifm,
128538f7b870SPatrick McHardy 		      struct nlattr **tb, char *ifname, int modified)
12860157f60cSPatrick McHardy {
1287d314774cSStephen Hemminger 	const struct net_device_ops *ops = dev->netdev_ops;
12880157f60cSPatrick McHardy 	int err;
12890157f60cSPatrick McHardy 
1290f0630529SEric W. Biederman 	if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
129181adee47SEric W. Biederman 		struct net *net = rtnl_link_get_net(dev_net(dev), tb);
1292d8a5ec67SEric W. Biederman 		if (IS_ERR(net)) {
1293d8a5ec67SEric W. Biederman 			err = PTR_ERR(net);
1294d8a5ec67SEric W. Biederman 			goto errout;
1295d8a5ec67SEric W. Biederman 		}
1296b51642f6SEric W. Biederman 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) {
1297b51642f6SEric W. Biederman 			err = -EPERM;
1298b51642f6SEric W. Biederman 			goto errout;
1299b51642f6SEric W. Biederman 		}
1300d8a5ec67SEric W. Biederman 		err = dev_change_net_namespace(dev, net, ifname);
1301d8a5ec67SEric W. Biederman 		put_net(net);
1302d8a5ec67SEric W. Biederman 		if (err)
1303d8a5ec67SEric W. Biederman 			goto errout;
1304d8a5ec67SEric W. Biederman 		modified = 1;
1305d8a5ec67SEric W. Biederman 	}
1306d8a5ec67SEric W. Biederman 
13070157f60cSPatrick McHardy 	if (tb[IFLA_MAP]) {
13080157f60cSPatrick McHardy 		struct rtnl_link_ifmap *u_map;
13090157f60cSPatrick McHardy 		struct ifmap k_map;
13100157f60cSPatrick McHardy 
1311d314774cSStephen Hemminger 		if (!ops->ndo_set_config) {
13120157f60cSPatrick McHardy 			err = -EOPNOTSUPP;
13130157f60cSPatrick McHardy 			goto errout;
13140157f60cSPatrick McHardy 		}
13150157f60cSPatrick McHardy 
13160157f60cSPatrick McHardy 		if (!netif_device_present(dev)) {
13170157f60cSPatrick McHardy 			err = -ENODEV;
13180157f60cSPatrick McHardy 			goto errout;
13190157f60cSPatrick McHardy 		}
13200157f60cSPatrick McHardy 
13210157f60cSPatrick McHardy 		u_map = nla_data(tb[IFLA_MAP]);
13220157f60cSPatrick McHardy 		k_map.mem_start = (unsigned long) u_map->mem_start;
13230157f60cSPatrick McHardy 		k_map.mem_end = (unsigned long) u_map->mem_end;
13240157f60cSPatrick McHardy 		k_map.base_addr = (unsigned short) u_map->base_addr;
13250157f60cSPatrick McHardy 		k_map.irq = (unsigned char) u_map->irq;
13260157f60cSPatrick McHardy 		k_map.dma = (unsigned char) u_map->dma;
13270157f60cSPatrick McHardy 		k_map.port = (unsigned char) u_map->port;
13280157f60cSPatrick McHardy 
1329d314774cSStephen Hemminger 		err = ops->ndo_set_config(dev, &k_map);
13300157f60cSPatrick McHardy 		if (err < 0)
13310157f60cSPatrick McHardy 			goto errout;
13320157f60cSPatrick McHardy 
13330157f60cSPatrick McHardy 		modified = 1;
13340157f60cSPatrick McHardy 	}
13350157f60cSPatrick McHardy 
13360157f60cSPatrick McHardy 	if (tb[IFLA_ADDRESS]) {
13370157f60cSPatrick McHardy 		struct sockaddr *sa;
13380157f60cSPatrick McHardy 		int len;
13390157f60cSPatrick McHardy 
13400157f60cSPatrick McHardy 		len = sizeof(sa_family_t) + dev->addr_len;
13410157f60cSPatrick McHardy 		sa = kmalloc(len, GFP_KERNEL);
13420157f60cSPatrick McHardy 		if (!sa) {
13430157f60cSPatrick McHardy 			err = -ENOMEM;
13440157f60cSPatrick McHardy 			goto errout;
13450157f60cSPatrick McHardy 		}
13460157f60cSPatrick McHardy 		sa->sa_family = dev->type;
13470157f60cSPatrick McHardy 		memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
13480157f60cSPatrick McHardy 		       dev->addr_len);
1349e7c3273eSJiri Pirko 		err = dev_set_mac_address(dev, sa);
13500157f60cSPatrick McHardy 		kfree(sa);
13510157f60cSPatrick McHardy 		if (err)
13520157f60cSPatrick McHardy 			goto errout;
13530157f60cSPatrick McHardy 		modified = 1;
13540157f60cSPatrick McHardy 	}
13550157f60cSPatrick McHardy 
13560157f60cSPatrick McHardy 	if (tb[IFLA_MTU]) {
13570157f60cSPatrick McHardy 		err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
13580157f60cSPatrick McHardy 		if (err < 0)
13590157f60cSPatrick McHardy 			goto errout;
13600157f60cSPatrick McHardy 		modified = 1;
13610157f60cSPatrick McHardy 	}
13620157f60cSPatrick McHardy 
1363cbda10faSVlad Dogaru 	if (tb[IFLA_GROUP]) {
1364cbda10faSVlad Dogaru 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
1365cbda10faSVlad Dogaru 		modified = 1;
1366cbda10faSVlad Dogaru 	}
1367cbda10faSVlad Dogaru 
13680157f60cSPatrick McHardy 	/*
13690157f60cSPatrick McHardy 	 * Interface selected by interface index but interface
13700157f60cSPatrick McHardy 	 * name provided implies that a name change has been
13710157f60cSPatrick McHardy 	 * requested.
13720157f60cSPatrick McHardy 	 */
13730157f60cSPatrick McHardy 	if (ifm->ifi_index > 0 && ifname[0]) {
13740157f60cSPatrick McHardy 		err = dev_change_name(dev, ifname);
13750157f60cSPatrick McHardy 		if (err < 0)
13760157f60cSPatrick McHardy 			goto errout;
13770157f60cSPatrick McHardy 		modified = 1;
13780157f60cSPatrick McHardy 	}
13790157f60cSPatrick McHardy 
13800b815a1aSStephen Hemminger 	if (tb[IFLA_IFALIAS]) {
13810b815a1aSStephen Hemminger 		err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
13820b815a1aSStephen Hemminger 				    nla_len(tb[IFLA_IFALIAS]));
13830b815a1aSStephen Hemminger 		if (err < 0)
13840b815a1aSStephen Hemminger 			goto errout;
13850b815a1aSStephen Hemminger 		modified = 1;
13860b815a1aSStephen Hemminger 	}
13870b815a1aSStephen Hemminger 
13880157f60cSPatrick McHardy 	if (tb[IFLA_BROADCAST]) {
13890157f60cSPatrick McHardy 		nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
1390e7c3273eSJiri Pirko 		call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
13910157f60cSPatrick McHardy 	}
13920157f60cSPatrick McHardy 
13930157f60cSPatrick McHardy 	if (ifm->ifi_flags || ifm->ifi_change) {
13943729d502SPatrick McHardy 		err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
13955f9021cfSJohannes Berg 		if (err < 0)
13965f9021cfSJohannes Berg 			goto errout;
13970157f60cSPatrick McHardy 	}
13980157f60cSPatrick McHardy 
1399fbaec0eaSJiri Pirko 	if (tb[IFLA_MASTER]) {
1400fbaec0eaSJiri Pirko 		err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
1401fbaec0eaSJiri Pirko 		if (err)
1402fbaec0eaSJiri Pirko 			goto errout;
1403fbaec0eaSJiri Pirko 		modified = 1;
1404fbaec0eaSJiri Pirko 	}
1405fbaec0eaSJiri Pirko 
14069a57247fSJiri Pirko 	if (tb[IFLA_CARRIER]) {
14079a57247fSJiri Pirko 		err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
14089a57247fSJiri Pirko 		if (err)
14099a57247fSJiri Pirko 			goto errout;
14109a57247fSJiri Pirko 		modified = 1;
14119a57247fSJiri Pirko 	}
14129a57247fSJiri Pirko 
141393b2d4a2SDavid S. Miller 	if (tb[IFLA_TXQLEN])
14140157f60cSPatrick McHardy 		dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
14150157f60cSPatrick McHardy 
14160157f60cSPatrick McHardy 	if (tb[IFLA_OPERSTATE])
141793b2d4a2SDavid S. Miller 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
14180157f60cSPatrick McHardy 
14190157f60cSPatrick McHardy 	if (tb[IFLA_LINKMODE]) {
14200157f60cSPatrick McHardy 		write_lock_bh(&dev_base_lock);
14210157f60cSPatrick McHardy 		dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
142293b2d4a2SDavid S. Miller 		write_unlock_bh(&dev_base_lock);
14230157f60cSPatrick McHardy 	}
14240157f60cSPatrick McHardy 
1425c02db8c6SChris Wright 	if (tb[IFLA_VFINFO_LIST]) {
1426c02db8c6SChris Wright 		struct nlattr *attr;
1427c02db8c6SChris Wright 		int rem;
1428c02db8c6SChris Wright 		nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
1429253683bbSDavid Howells 			if (nla_type(attr) != IFLA_VF_INFO) {
1430253683bbSDavid Howells 				err = -EINVAL;
1431c02db8c6SChris Wright 				goto errout;
1432253683bbSDavid Howells 			}
1433c02db8c6SChris Wright 			err = do_setvfinfo(dev, attr);
1434ebc08a6fSWilliams, Mitch A 			if (err < 0)
1435ebc08a6fSWilliams, Mitch A 				goto errout;
1436ebc08a6fSWilliams, Mitch A 			modified = 1;
1437ebc08a6fSWilliams, Mitch A 		}
1438ebc08a6fSWilliams, Mitch A 	}
14390157f60cSPatrick McHardy 	err = 0;
14400157f60cSPatrick McHardy 
144157b61080SScott Feldman 	if (tb[IFLA_VF_PORTS]) {
144257b61080SScott Feldman 		struct nlattr *port[IFLA_PORT_MAX+1];
144357b61080SScott Feldman 		struct nlattr *attr;
144457b61080SScott Feldman 		int vf;
144557b61080SScott Feldman 		int rem;
144657b61080SScott Feldman 
144757b61080SScott Feldman 		err = -EOPNOTSUPP;
144857b61080SScott Feldman 		if (!ops->ndo_set_vf_port)
144957b61080SScott Feldman 			goto errout;
145057b61080SScott Feldman 
145157b61080SScott Feldman 		nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
145257b61080SScott Feldman 			if (nla_type(attr) != IFLA_VF_PORT)
145357b61080SScott Feldman 				continue;
145457b61080SScott Feldman 			err = nla_parse_nested(port, IFLA_PORT_MAX,
145557b61080SScott Feldman 				attr, ifla_port_policy);
145657b61080SScott Feldman 			if (err < 0)
145757b61080SScott Feldman 				goto errout;
145857b61080SScott Feldman 			if (!port[IFLA_PORT_VF]) {
145957b61080SScott Feldman 				err = -EOPNOTSUPP;
146057b61080SScott Feldman 				goto errout;
146157b61080SScott Feldman 			}
146257b61080SScott Feldman 			vf = nla_get_u32(port[IFLA_PORT_VF]);
146357b61080SScott Feldman 			err = ops->ndo_set_vf_port(dev, vf, port);
146457b61080SScott Feldman 			if (err < 0)
146557b61080SScott Feldman 				goto errout;
146657b61080SScott Feldman 			modified = 1;
146757b61080SScott Feldman 		}
146857b61080SScott Feldman 	}
146957b61080SScott Feldman 	err = 0;
147057b61080SScott Feldman 
147157b61080SScott Feldman 	if (tb[IFLA_PORT_SELF]) {
147257b61080SScott Feldman 		struct nlattr *port[IFLA_PORT_MAX+1];
147357b61080SScott Feldman 
147457b61080SScott Feldman 		err = nla_parse_nested(port, IFLA_PORT_MAX,
147557b61080SScott Feldman 			tb[IFLA_PORT_SELF], ifla_port_policy);
147657b61080SScott Feldman 		if (err < 0)
147757b61080SScott Feldman 			goto errout;
147857b61080SScott Feldman 
147957b61080SScott Feldman 		err = -EOPNOTSUPP;
148057b61080SScott Feldman 		if (ops->ndo_set_vf_port)
148157b61080SScott Feldman 			err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
148257b61080SScott Feldman 		if (err < 0)
148357b61080SScott Feldman 			goto errout;
148457b61080SScott Feldman 		modified = 1;
148557b61080SScott Feldman 	}
1486f8ff182cSThomas Graf 
1487f8ff182cSThomas Graf 	if (tb[IFLA_AF_SPEC]) {
1488f8ff182cSThomas Graf 		struct nlattr *af;
1489f8ff182cSThomas Graf 		int rem;
1490f8ff182cSThomas Graf 
1491f8ff182cSThomas Graf 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1492f8ff182cSThomas Graf 			const struct rtnl_af_ops *af_ops;
1493f8ff182cSThomas Graf 
1494f8ff182cSThomas Graf 			if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1495cf7afbfeSThomas Graf 				BUG();
1496f8ff182cSThomas Graf 
1497cf7afbfeSThomas Graf 			err = af_ops->set_link_af(dev, af);
1498f8ff182cSThomas Graf 			if (err < 0)
1499f8ff182cSThomas Graf 				goto errout;
1500f8ff182cSThomas Graf 
1501f8ff182cSThomas Graf 			modified = 1;
1502f8ff182cSThomas Graf 		}
1503f8ff182cSThomas Graf 	}
150457b61080SScott Feldman 	err = 0;
150557b61080SScott Feldman 
15060157f60cSPatrick McHardy errout:
1507e87cc472SJoe Perches 	if (err < 0 && modified)
1508e87cc472SJoe 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",
1509e87cc472SJoe Perches 				     dev->name);
15100157f60cSPatrick McHardy 
15110157f60cSPatrick McHardy 	return err;
15120157f60cSPatrick McHardy }
15130157f60cSPatrick McHardy 
1514*661d2967SThomas Graf static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
1515da5e0494SThomas Graf {
15163b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
1517da5e0494SThomas Graf 	struct ifinfomsg *ifm;
1518da5e0494SThomas Graf 	struct net_device *dev;
15190157f60cSPatrick McHardy 	int err;
1520da5e0494SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
15211da177e4SLinus Torvalds 	char ifname[IFNAMSIZ];
15221da177e4SLinus Torvalds 
1523da5e0494SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1524da5e0494SThomas Graf 	if (err < 0)
1525da5e0494SThomas Graf 		goto errout;
15261da177e4SLinus Torvalds 
15275176f91eSThomas Graf 	if (tb[IFLA_IFNAME])
15285176f91eSThomas Graf 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
152978e5b891SPatrick McHardy 	else
153078e5b891SPatrick McHardy 		ifname[0] = '\0';
15311da177e4SLinus Torvalds 
15321da177e4SLinus Torvalds 	err = -EINVAL;
1533da5e0494SThomas Graf 	ifm = nlmsg_data(nlh);
153451055be8SPatrick McHardy 	if (ifm->ifi_index > 0)
1535a3d12891SEric Dumazet 		dev = __dev_get_by_index(net, ifm->ifi_index);
1536da5e0494SThomas Graf 	else if (tb[IFLA_IFNAME])
1537a3d12891SEric Dumazet 		dev = __dev_get_by_name(net, ifname);
1538da5e0494SThomas Graf 	else
1539da5e0494SThomas Graf 		goto errout;
15401da177e4SLinus Torvalds 
1541da5e0494SThomas Graf 	if (dev == NULL) {
1542da5e0494SThomas Graf 		err = -ENODEV;
1543da5e0494SThomas Graf 		goto errout;
1544da5e0494SThomas Graf 	}
15451da177e4SLinus Torvalds 
1546e0d087afSEric Dumazet 	err = validate_linkmsg(dev, tb);
1547e0d087afSEric Dumazet 	if (err < 0)
1548a3d12891SEric Dumazet 		goto errout;
1549da5e0494SThomas Graf 
155038f7b870SPatrick McHardy 	err = do_setlink(dev, ifm, tb, ifname, 0);
1551da5e0494SThomas Graf errout:
15521da177e4SLinus Torvalds 	return err;
15531da177e4SLinus Torvalds }
15541da177e4SLinus Torvalds 
1555*661d2967SThomas Graf static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
155638f7b870SPatrick McHardy {
15573b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
155838f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops;
155938f7b870SPatrick McHardy 	struct net_device *dev;
156038f7b870SPatrick McHardy 	struct ifinfomsg *ifm;
156138f7b870SPatrick McHardy 	char ifname[IFNAMSIZ];
156238f7b870SPatrick McHardy 	struct nlattr *tb[IFLA_MAX+1];
156338f7b870SPatrick McHardy 	int err;
1564226bd341SEric Dumazet 	LIST_HEAD(list_kill);
156538f7b870SPatrick McHardy 
156638f7b870SPatrick McHardy 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
156738f7b870SPatrick McHardy 	if (err < 0)
156838f7b870SPatrick McHardy 		return err;
156938f7b870SPatrick McHardy 
157038f7b870SPatrick McHardy 	if (tb[IFLA_IFNAME])
157138f7b870SPatrick McHardy 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
157238f7b870SPatrick McHardy 
157338f7b870SPatrick McHardy 	ifm = nlmsg_data(nlh);
157438f7b870SPatrick McHardy 	if (ifm->ifi_index > 0)
1575881d966bSEric W. Biederman 		dev = __dev_get_by_index(net, ifm->ifi_index);
157638f7b870SPatrick McHardy 	else if (tb[IFLA_IFNAME])
1577881d966bSEric W. Biederman 		dev = __dev_get_by_name(net, ifname);
157838f7b870SPatrick McHardy 	else
157938f7b870SPatrick McHardy 		return -EINVAL;
158038f7b870SPatrick McHardy 
158138f7b870SPatrick McHardy 	if (!dev)
158238f7b870SPatrick McHardy 		return -ENODEV;
158338f7b870SPatrick McHardy 
158438f7b870SPatrick McHardy 	ops = dev->rtnl_link_ops;
158538f7b870SPatrick McHardy 	if (!ops)
158638f7b870SPatrick McHardy 		return -EOPNOTSUPP;
158738f7b870SPatrick McHardy 
1588226bd341SEric Dumazet 	ops->dellink(dev, &list_kill);
1589226bd341SEric Dumazet 	unregister_netdevice_many(&list_kill);
1590226bd341SEric Dumazet 	list_del(&list_kill);
159138f7b870SPatrick McHardy 	return 0;
159238f7b870SPatrick McHardy }
159338f7b870SPatrick McHardy 
15943729d502SPatrick McHardy int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
15953729d502SPatrick McHardy {
15963729d502SPatrick McHardy 	unsigned int old_flags;
15973729d502SPatrick McHardy 	int err;
15983729d502SPatrick McHardy 
15993729d502SPatrick McHardy 	old_flags = dev->flags;
16003729d502SPatrick McHardy 	if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
16013729d502SPatrick McHardy 		err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
16023729d502SPatrick McHardy 		if (err < 0)
16033729d502SPatrick McHardy 			return err;
16043729d502SPatrick McHardy 	}
16053729d502SPatrick McHardy 
16063729d502SPatrick McHardy 	dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
16073729d502SPatrick McHardy 	rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
16083729d502SPatrick McHardy 
16093729d502SPatrick McHardy 	__dev_notify_flags(dev, old_flags);
16103729d502SPatrick McHardy 	return 0;
16113729d502SPatrick McHardy }
16123729d502SPatrick McHardy EXPORT_SYMBOL(rtnl_configure_link);
16133729d502SPatrick McHardy 
1614c0713563SRami Rosen struct net_device *rtnl_create_link(struct net *net,
161581adee47SEric W. Biederman 	char *ifname, const struct rtnl_link_ops *ops, struct nlattr *tb[])
1616e7199288SPavel Emelianov {
1617e7199288SPavel Emelianov 	int err;
1618e7199288SPavel Emelianov 	struct net_device *dev;
1619d40156aaSJiri Pirko 	unsigned int num_tx_queues = 1;
1620d40156aaSJiri Pirko 	unsigned int num_rx_queues = 1;
1621e7199288SPavel Emelianov 
162276ff5cc9SJiri Pirko 	if (tb[IFLA_NUM_TX_QUEUES])
162376ff5cc9SJiri Pirko 		num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
162476ff5cc9SJiri Pirko 	else if (ops->get_num_tx_queues)
1625d40156aaSJiri Pirko 		num_tx_queues = ops->get_num_tx_queues();
162676ff5cc9SJiri Pirko 
162776ff5cc9SJiri Pirko 	if (tb[IFLA_NUM_RX_QUEUES])
162876ff5cc9SJiri Pirko 		num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
162976ff5cc9SJiri Pirko 	else if (ops->get_num_rx_queues)
1630d40156aaSJiri Pirko 		num_rx_queues = ops->get_num_rx_queues();
1631efacb309Sstephen hemminger 
1632e7199288SPavel Emelianov 	err = -ENOMEM;
1633d40156aaSJiri Pirko 	dev = alloc_netdev_mqs(ops->priv_size, ifname, ops->setup,
1634d40156aaSJiri Pirko 			       num_tx_queues, num_rx_queues);
1635e7199288SPavel Emelianov 	if (!dev)
1636e7199288SPavel Emelianov 		goto err;
1637e7199288SPavel Emelianov 
163881adee47SEric W. Biederman 	dev_net_set(dev, net);
163981adee47SEric W. Biederman 	dev->rtnl_link_ops = ops;
16403729d502SPatrick McHardy 	dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
164181adee47SEric W. Biederman 
1642e7199288SPavel Emelianov 	if (tb[IFLA_MTU])
1643e7199288SPavel Emelianov 		dev->mtu = nla_get_u32(tb[IFLA_MTU]);
16442afb9b53SJiri Pirko 	if (tb[IFLA_ADDRESS]) {
1645e7199288SPavel Emelianov 		memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
1646e7199288SPavel Emelianov 				nla_len(tb[IFLA_ADDRESS]));
16472afb9b53SJiri Pirko 		dev->addr_assign_type = NET_ADDR_SET;
16482afb9b53SJiri Pirko 	}
1649e7199288SPavel Emelianov 	if (tb[IFLA_BROADCAST])
1650e7199288SPavel Emelianov 		memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
1651e7199288SPavel Emelianov 				nla_len(tb[IFLA_BROADCAST]));
1652e7199288SPavel Emelianov 	if (tb[IFLA_TXQLEN])
1653e7199288SPavel Emelianov 		dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
1654e7199288SPavel Emelianov 	if (tb[IFLA_OPERSTATE])
165593b2d4a2SDavid S. Miller 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
1656e7199288SPavel Emelianov 	if (tb[IFLA_LINKMODE])
1657e7199288SPavel Emelianov 		dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
1658ffa934f1SPatrick McHardy 	if (tb[IFLA_GROUP])
1659ffa934f1SPatrick McHardy 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
1660e7199288SPavel Emelianov 
1661e7199288SPavel Emelianov 	return dev;
1662e7199288SPavel Emelianov 
1663e7199288SPavel Emelianov err:
1664e7199288SPavel Emelianov 	return ERR_PTR(err);
1665e7199288SPavel Emelianov }
1666e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_create_link);
1667e7199288SPavel Emelianov 
1668e7ed828fSVlad Dogaru static int rtnl_group_changelink(struct net *net, int group,
1669e7ed828fSVlad Dogaru 		struct ifinfomsg *ifm,
1670e7ed828fSVlad Dogaru 		struct nlattr **tb)
1671e7ed828fSVlad Dogaru {
1672e7ed828fSVlad Dogaru 	struct net_device *dev;
1673e7ed828fSVlad Dogaru 	int err;
1674e7ed828fSVlad Dogaru 
1675e7ed828fSVlad Dogaru 	for_each_netdev(net, dev) {
1676e7ed828fSVlad Dogaru 		if (dev->group == group) {
1677e7ed828fSVlad Dogaru 			err = do_setlink(dev, ifm, tb, NULL, 0);
1678e7ed828fSVlad Dogaru 			if (err < 0)
1679e7ed828fSVlad Dogaru 				return err;
1680e7ed828fSVlad Dogaru 		}
1681e7ed828fSVlad Dogaru 	}
1682e7ed828fSVlad Dogaru 
1683e7ed828fSVlad Dogaru 	return 0;
1684e7ed828fSVlad Dogaru }
1685e7ed828fSVlad Dogaru 
1686*661d2967SThomas Graf static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh)
168738f7b870SPatrick McHardy {
16883b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
168938f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops;
169038f7b870SPatrick McHardy 	struct net_device *dev;
169138f7b870SPatrick McHardy 	struct ifinfomsg *ifm;
169238f7b870SPatrick McHardy 	char kind[MODULE_NAME_LEN];
169338f7b870SPatrick McHardy 	char ifname[IFNAMSIZ];
169438f7b870SPatrick McHardy 	struct nlattr *tb[IFLA_MAX+1];
169538f7b870SPatrick McHardy 	struct nlattr *linkinfo[IFLA_INFO_MAX+1];
169638f7b870SPatrick McHardy 	int err;
169738f7b870SPatrick McHardy 
169895a5afcaSJohannes Berg #ifdef CONFIG_MODULES
169938f7b870SPatrick McHardy replay:
17008072f085SThomas Graf #endif
170138f7b870SPatrick McHardy 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
170238f7b870SPatrick McHardy 	if (err < 0)
170338f7b870SPatrick McHardy 		return err;
170438f7b870SPatrick McHardy 
170538f7b870SPatrick McHardy 	if (tb[IFLA_IFNAME])
170638f7b870SPatrick McHardy 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
170738f7b870SPatrick McHardy 	else
170838f7b870SPatrick McHardy 		ifname[0] = '\0';
170938f7b870SPatrick McHardy 
171038f7b870SPatrick McHardy 	ifm = nlmsg_data(nlh);
171138f7b870SPatrick McHardy 	if (ifm->ifi_index > 0)
1712881d966bSEric W. Biederman 		dev = __dev_get_by_index(net, ifm->ifi_index);
1713e7ed828fSVlad Dogaru 	else {
1714e7ed828fSVlad Dogaru 		if (ifname[0])
1715881d966bSEric W. Biederman 			dev = __dev_get_by_name(net, ifname);
171638f7b870SPatrick McHardy 		else
171738f7b870SPatrick McHardy 			dev = NULL;
1718e7ed828fSVlad Dogaru 	}
171938f7b870SPatrick McHardy 
1720e0d087afSEric Dumazet 	err = validate_linkmsg(dev, tb);
1721e0d087afSEric Dumazet 	if (err < 0)
17221840bb13SThomas Graf 		return err;
17231840bb13SThomas Graf 
172438f7b870SPatrick McHardy 	if (tb[IFLA_LINKINFO]) {
172538f7b870SPatrick McHardy 		err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
172638f7b870SPatrick McHardy 				       tb[IFLA_LINKINFO], ifla_info_policy);
172738f7b870SPatrick McHardy 		if (err < 0)
172838f7b870SPatrick McHardy 			return err;
172938f7b870SPatrick McHardy 	} else
173038f7b870SPatrick McHardy 		memset(linkinfo, 0, sizeof(linkinfo));
173138f7b870SPatrick McHardy 
173238f7b870SPatrick McHardy 	if (linkinfo[IFLA_INFO_KIND]) {
173338f7b870SPatrick McHardy 		nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
173438f7b870SPatrick McHardy 		ops = rtnl_link_ops_get(kind);
173538f7b870SPatrick McHardy 	} else {
173638f7b870SPatrick McHardy 		kind[0] = '\0';
173738f7b870SPatrick McHardy 		ops = NULL;
173838f7b870SPatrick McHardy 	}
173938f7b870SPatrick McHardy 
174038f7b870SPatrick McHardy 	if (1) {
174138f7b870SPatrick McHardy 		struct nlattr *attr[ops ? ops->maxtype + 1 : 0], **data = NULL;
174281adee47SEric W. Biederman 		struct net *dest_net;
174338f7b870SPatrick McHardy 
174438f7b870SPatrick McHardy 		if (ops) {
174538f7b870SPatrick McHardy 			if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
174638f7b870SPatrick McHardy 				err = nla_parse_nested(attr, ops->maxtype,
174738f7b870SPatrick McHardy 						       linkinfo[IFLA_INFO_DATA],
174838f7b870SPatrick McHardy 						       ops->policy);
174938f7b870SPatrick McHardy 				if (err < 0)
175038f7b870SPatrick McHardy 					return err;
175138f7b870SPatrick McHardy 				data = attr;
175238f7b870SPatrick McHardy 			}
175338f7b870SPatrick McHardy 			if (ops->validate) {
175438f7b870SPatrick McHardy 				err = ops->validate(tb, data);
175538f7b870SPatrick McHardy 				if (err < 0)
175638f7b870SPatrick McHardy 					return err;
175738f7b870SPatrick McHardy 			}
175838f7b870SPatrick McHardy 		}
175938f7b870SPatrick McHardy 
176038f7b870SPatrick McHardy 		if (dev) {
176138f7b870SPatrick McHardy 			int modified = 0;
176238f7b870SPatrick McHardy 
176338f7b870SPatrick McHardy 			if (nlh->nlmsg_flags & NLM_F_EXCL)
176438f7b870SPatrick McHardy 				return -EEXIST;
176538f7b870SPatrick McHardy 			if (nlh->nlmsg_flags & NLM_F_REPLACE)
176638f7b870SPatrick McHardy 				return -EOPNOTSUPP;
176738f7b870SPatrick McHardy 
176838f7b870SPatrick McHardy 			if (linkinfo[IFLA_INFO_DATA]) {
176938f7b870SPatrick McHardy 				if (!ops || ops != dev->rtnl_link_ops ||
177038f7b870SPatrick McHardy 				    !ops->changelink)
177138f7b870SPatrick McHardy 					return -EOPNOTSUPP;
177238f7b870SPatrick McHardy 
177338f7b870SPatrick McHardy 				err = ops->changelink(dev, tb, data);
177438f7b870SPatrick McHardy 				if (err < 0)
177538f7b870SPatrick McHardy 					return err;
177638f7b870SPatrick McHardy 				modified = 1;
177738f7b870SPatrick McHardy 			}
177838f7b870SPatrick McHardy 
177938f7b870SPatrick McHardy 			return do_setlink(dev, ifm, tb, ifname, modified);
178038f7b870SPatrick McHardy 		}
178138f7b870SPatrick McHardy 
1782ffa934f1SPatrick McHardy 		if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
1783ffa934f1SPatrick McHardy 			if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
1784ffa934f1SPatrick McHardy 				return rtnl_group_changelink(net,
1785ffa934f1SPatrick McHardy 						nla_get_u32(tb[IFLA_GROUP]),
1786ffa934f1SPatrick McHardy 						ifm, tb);
178738f7b870SPatrick McHardy 			return -ENODEV;
1788ffa934f1SPatrick McHardy 		}
178938f7b870SPatrick McHardy 
17900e06877cSPatrick McHardy 		if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
179138f7b870SPatrick McHardy 			return -EOPNOTSUPP;
179238f7b870SPatrick McHardy 
179338f7b870SPatrick McHardy 		if (!ops) {
179495a5afcaSJohannes Berg #ifdef CONFIG_MODULES
179538f7b870SPatrick McHardy 			if (kind[0]) {
179638f7b870SPatrick McHardy 				__rtnl_unlock();
179738f7b870SPatrick McHardy 				request_module("rtnl-link-%s", kind);
179838f7b870SPatrick McHardy 				rtnl_lock();
179938f7b870SPatrick McHardy 				ops = rtnl_link_ops_get(kind);
180038f7b870SPatrick McHardy 				if (ops)
180138f7b870SPatrick McHardy 					goto replay;
180238f7b870SPatrick McHardy 			}
180338f7b870SPatrick McHardy #endif
180438f7b870SPatrick McHardy 			return -EOPNOTSUPP;
180538f7b870SPatrick McHardy 		}
180638f7b870SPatrick McHardy 
180738f7b870SPatrick McHardy 		if (!ifname[0])
180838f7b870SPatrick McHardy 			snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
180938f7b870SPatrick McHardy 
181081adee47SEric W. Biederman 		dest_net = rtnl_link_get_net(net, tb);
181113ad1774SEric W. Biederman 		if (IS_ERR(dest_net))
181213ad1774SEric W. Biederman 			return PTR_ERR(dest_net);
181313ad1774SEric W. Biederman 
1814c0713563SRami Rosen 		dev = rtnl_create_link(dest_net, ifname, ops, tb);
18159c7dafbfSPavel Emelyanov 		if (IS_ERR(dev)) {
1816e7199288SPavel Emelianov 			err = PTR_ERR(dev);
18179c7dafbfSPavel Emelyanov 			goto out;
18189c7dafbfSPavel Emelyanov 		}
18199c7dafbfSPavel Emelyanov 
18209c7dafbfSPavel Emelyanov 		dev->ifindex = ifm->ifi_index;
18219c7dafbfSPavel Emelyanov 
18229c7dafbfSPavel Emelyanov 		if (ops->newlink)
182381adee47SEric W. Biederman 			err = ops->newlink(net, dev, tb, data);
18242d85cba2SPatrick McHardy 		else
18252d85cba2SPatrick McHardy 			err = register_netdevice(dev);
182680032cffSDan Carpenter 
182780032cffSDan Carpenter 		if (err < 0 && !IS_ERR(dev))
182838f7b870SPatrick McHardy 			free_netdev(dev);
182980032cffSDan Carpenter 		if (err < 0)
18303729d502SPatrick McHardy 			goto out;
183181adee47SEric W. Biederman 
18323729d502SPatrick McHardy 		err = rtnl_configure_link(dev, ifm);
18333729d502SPatrick McHardy 		if (err < 0)
18343729d502SPatrick McHardy 			unregister_netdevice(dev);
18353729d502SPatrick McHardy out:
183681adee47SEric W. Biederman 		put_net(dest_net);
183738f7b870SPatrick McHardy 		return err;
183838f7b870SPatrick McHardy 	}
183938f7b870SPatrick McHardy }
184038f7b870SPatrick McHardy 
1841*661d2967SThomas Graf static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh)
1842711e2c33SJean Tourrilhes {
18433b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
1844b60c5115SThomas Graf 	struct ifinfomsg *ifm;
1845a3d12891SEric Dumazet 	char ifname[IFNAMSIZ];
1846b60c5115SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
1847b60c5115SThomas Graf 	struct net_device *dev = NULL;
1848b60c5115SThomas Graf 	struct sk_buff *nskb;
1849339bf98fSThomas Graf 	int err;
1850115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
1851711e2c33SJean Tourrilhes 
1852b60c5115SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1853b60c5115SThomas Graf 	if (err < 0)
18549918f230SEric Sesterhenn 		return err;
1855b60c5115SThomas Graf 
1856a3d12891SEric Dumazet 	if (tb[IFLA_IFNAME])
1857a3d12891SEric Dumazet 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1858a3d12891SEric Dumazet 
1859115c9b81SGreg Rose 	if (tb[IFLA_EXT_MASK])
1860115c9b81SGreg Rose 		ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1861115c9b81SGreg Rose 
1862b60c5115SThomas Graf 	ifm = nlmsg_data(nlh);
1863a3d12891SEric Dumazet 	if (ifm->ifi_index > 0)
1864a3d12891SEric Dumazet 		dev = __dev_get_by_index(net, ifm->ifi_index);
1865a3d12891SEric Dumazet 	else if (tb[IFLA_IFNAME])
1866a3d12891SEric Dumazet 		dev = __dev_get_by_name(net, ifname);
1867a3d12891SEric Dumazet 	else
1868b60c5115SThomas Graf 		return -EINVAL;
1869b60c5115SThomas Graf 
1870a3d12891SEric Dumazet 	if (dev == NULL)
1871a3d12891SEric Dumazet 		return -ENODEV;
1872a3d12891SEric Dumazet 
1873115c9b81SGreg Rose 	nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
1874a3d12891SEric Dumazet 	if (nskb == NULL)
1875a3d12891SEric Dumazet 		return -ENOBUFS;
1876711e2c33SJean Tourrilhes 
187715e47304SEric W. Biederman 	err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid,
1878115c9b81SGreg Rose 			       nlh->nlmsg_seq, 0, 0, ext_filter_mask);
187926932566SPatrick McHardy 	if (err < 0) {
188026932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in if_nlmsg_size */
188126932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
188226932566SPatrick McHardy 		kfree_skb(nskb);
1883a3d12891SEric Dumazet 	} else
188415e47304SEric W. Biederman 		err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
1885b60c5115SThomas Graf 
1886711e2c33SJean Tourrilhes 	return err;
1887711e2c33SJean Tourrilhes }
1888711e2c33SJean Tourrilhes 
1889115c9b81SGreg Rose static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
1890c7ac8679SGreg Rose {
1891115c9b81SGreg Rose 	struct net *net = sock_net(skb->sk);
1892115c9b81SGreg Rose 	struct net_device *dev;
1893115c9b81SGreg Rose 	struct nlattr *tb[IFLA_MAX+1];
1894115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
1895115c9b81SGreg Rose 	u16 min_ifinfo_dump_size = 0;
1896115c9b81SGreg Rose 
1897a4b64fbeSEric Dumazet 	if (nlmsg_parse(nlh, sizeof(struct rtgenmsg), tb, IFLA_MAX,
1898a4b64fbeSEric Dumazet 			ifla_policy) >= 0) {
1899115c9b81SGreg Rose 		if (tb[IFLA_EXT_MASK])
1900115c9b81SGreg Rose 			ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1901a4b64fbeSEric Dumazet 	}
1902115c9b81SGreg Rose 
1903115c9b81SGreg Rose 	if (!ext_filter_mask)
1904115c9b81SGreg Rose 		return NLMSG_GOODSIZE;
1905115c9b81SGreg Rose 	/*
1906115c9b81SGreg Rose 	 * traverse the list of net devices and compute the minimum
1907115c9b81SGreg Rose 	 * buffer size based upon the filter mask.
1908115c9b81SGreg Rose 	 */
1909115c9b81SGreg Rose 	list_for_each_entry(dev, &net->dev_base_head, dev_list) {
1910115c9b81SGreg Rose 		min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
1911115c9b81SGreg Rose 					     if_nlmsg_size(dev,
1912115c9b81SGreg Rose 						           ext_filter_mask));
1913115c9b81SGreg Rose 	}
1914115c9b81SGreg Rose 
1915c7ac8679SGreg Rose 	return min_ifinfo_dump_size;
1916c7ac8679SGreg Rose }
1917c7ac8679SGreg Rose 
191842bad1daSAdrian Bunk static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
19191da177e4SLinus Torvalds {
19201da177e4SLinus Torvalds 	int idx;
19211da177e4SLinus Torvalds 	int s_idx = cb->family;
19221da177e4SLinus Torvalds 
19231da177e4SLinus Torvalds 	if (s_idx == 0)
19241da177e4SLinus Torvalds 		s_idx = 1;
192525239ceeSPatrick McHardy 	for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
19261da177e4SLinus Torvalds 		int type = cb->nlh->nlmsg_type-RTM_BASE;
19271da177e4SLinus Torvalds 		if (idx < s_idx || idx == PF_PACKET)
19281da177e4SLinus Torvalds 			continue;
1929e2849863SThomas Graf 		if (rtnl_msg_handlers[idx] == NULL ||
1930e2849863SThomas Graf 		    rtnl_msg_handlers[idx][type].dumpit == NULL)
19311da177e4SLinus Torvalds 			continue;
19321da177e4SLinus Torvalds 		if (idx > s_idx)
19331da177e4SLinus Torvalds 			memset(&cb->args[0], 0, sizeof(cb->args));
1934e2849863SThomas Graf 		if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
19351da177e4SLinus Torvalds 			break;
19361da177e4SLinus Torvalds 	}
19371da177e4SLinus Torvalds 	cb->family = idx;
19381da177e4SLinus Torvalds 
19391da177e4SLinus Torvalds 	return skb->len;
19401da177e4SLinus Torvalds }
19411da177e4SLinus Torvalds 
194295c96174SEric Dumazet void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change)
19431da177e4SLinus Torvalds {
1944c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
19451da177e4SLinus Torvalds 	struct sk_buff *skb;
19460ec6d3f4SThomas Graf 	int err = -ENOBUFS;
1947c7ac8679SGreg Rose 	size_t if_info_size;
19481da177e4SLinus Torvalds 
1949115c9b81SGreg Rose 	skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), GFP_KERNEL);
19500ec6d3f4SThomas Graf 	if (skb == NULL)
19510ec6d3f4SThomas Graf 		goto errout;
19521da177e4SLinus Torvalds 
1953115c9b81SGreg Rose 	err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0);
195426932566SPatrick McHardy 	if (err < 0) {
195526932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in if_nlmsg_size() */
195626932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
195726932566SPatrick McHardy 		kfree_skb(skb);
195826932566SPatrick McHardy 		goto errout;
195926932566SPatrick McHardy 	}
19601ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
19611ce85fe4SPablo Neira Ayuso 	return;
19620ec6d3f4SThomas Graf errout:
19630ec6d3f4SThomas Graf 	if (err < 0)
19644b3da706SEric W. Biederman 		rtnl_set_sk_err(net, RTNLGRP_LINK, err);
19651da177e4SLinus Torvalds }
1966471cb5a3SJiri Pirko EXPORT_SYMBOL(rtmsg_ifinfo);
19671da177e4SLinus Torvalds 
1968d83b0603SJohn Fastabend static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
1969d83b0603SJohn Fastabend 				   struct net_device *dev,
1970d83b0603SJohn Fastabend 				   u8 *addr, u32 pid, u32 seq,
1971d83b0603SJohn Fastabend 				   int type, unsigned int flags)
1972d83b0603SJohn Fastabend {
1973d83b0603SJohn Fastabend 	struct nlmsghdr *nlh;
1974d83b0603SJohn Fastabend 	struct ndmsg *ndm;
1975d83b0603SJohn Fastabend 
1976d83b0603SJohn Fastabend 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), NLM_F_MULTI);
1977d83b0603SJohn Fastabend 	if (!nlh)
1978d83b0603SJohn Fastabend 		return -EMSGSIZE;
1979d83b0603SJohn Fastabend 
1980d83b0603SJohn Fastabend 	ndm = nlmsg_data(nlh);
1981d83b0603SJohn Fastabend 	ndm->ndm_family  = AF_BRIDGE;
1982d83b0603SJohn Fastabend 	ndm->ndm_pad1	 = 0;
1983d83b0603SJohn Fastabend 	ndm->ndm_pad2    = 0;
1984d83b0603SJohn Fastabend 	ndm->ndm_flags	 = flags;
1985d83b0603SJohn Fastabend 	ndm->ndm_type	 = 0;
1986d83b0603SJohn Fastabend 	ndm->ndm_ifindex = dev->ifindex;
1987d83b0603SJohn Fastabend 	ndm->ndm_state   = NUD_PERMANENT;
1988d83b0603SJohn Fastabend 
1989d83b0603SJohn Fastabend 	if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
1990d83b0603SJohn Fastabend 		goto nla_put_failure;
1991d83b0603SJohn Fastabend 
1992d83b0603SJohn Fastabend 	return nlmsg_end(skb, nlh);
1993d83b0603SJohn Fastabend 
1994d83b0603SJohn Fastabend nla_put_failure:
1995d83b0603SJohn Fastabend 	nlmsg_cancel(skb, nlh);
1996d83b0603SJohn Fastabend 	return -EMSGSIZE;
1997d83b0603SJohn Fastabend }
1998d83b0603SJohn Fastabend 
19993ff661c3SJohn Fastabend static inline size_t rtnl_fdb_nlmsg_size(void)
20003ff661c3SJohn Fastabend {
20013ff661c3SJohn Fastabend 	return NLMSG_ALIGN(sizeof(struct ndmsg)) + nla_total_size(ETH_ALEN);
20023ff661c3SJohn Fastabend }
20033ff661c3SJohn Fastabend 
20043ff661c3SJohn Fastabend static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, int type)
20053ff661c3SJohn Fastabend {
20063ff661c3SJohn Fastabend 	struct net *net = dev_net(dev);
20073ff661c3SJohn Fastabend 	struct sk_buff *skb;
20083ff661c3SJohn Fastabend 	int err = -ENOBUFS;
20093ff661c3SJohn Fastabend 
20103ff661c3SJohn Fastabend 	skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
20113ff661c3SJohn Fastabend 	if (!skb)
20123ff661c3SJohn Fastabend 		goto errout;
20133ff661c3SJohn Fastabend 
20143ff661c3SJohn Fastabend 	err = nlmsg_populate_fdb_fill(skb, dev, addr, 0, 0, type, NTF_SELF);
20153ff661c3SJohn Fastabend 	if (err < 0) {
20163ff661c3SJohn Fastabend 		kfree_skb(skb);
20173ff661c3SJohn Fastabend 		goto errout;
20183ff661c3SJohn Fastabend 	}
20193ff661c3SJohn Fastabend 
20203ff661c3SJohn Fastabend 	rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
20213ff661c3SJohn Fastabend 	return;
20223ff661c3SJohn Fastabend errout:
20233ff661c3SJohn Fastabend 	rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
20243ff661c3SJohn Fastabend }
20253ff661c3SJohn Fastabend 
2026090096bfSVlad Yasevich /**
2027090096bfSVlad Yasevich  * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
2028090096bfSVlad Yasevich  */
2029090096bfSVlad Yasevich int ndo_dflt_fdb_add(struct ndmsg *ndm,
2030090096bfSVlad Yasevich 		     struct nlattr *tb[],
2031090096bfSVlad Yasevich 		     struct net_device *dev,
2032090096bfSVlad Yasevich 		     const unsigned char *addr,
2033090096bfSVlad Yasevich 		     u16 flags)
2034090096bfSVlad Yasevich {
2035090096bfSVlad Yasevich 	int err = -EINVAL;
2036090096bfSVlad Yasevich 
2037090096bfSVlad Yasevich 	/* If aging addresses are supported device will need to
2038090096bfSVlad Yasevich 	 * implement its own handler for this.
2039090096bfSVlad Yasevich 	 */
2040090096bfSVlad Yasevich 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
2041090096bfSVlad Yasevich 		pr_info("%s: FDB only supports static addresses\n", dev->name);
2042090096bfSVlad Yasevich 		return err;
2043090096bfSVlad Yasevich 	}
2044090096bfSVlad Yasevich 
2045090096bfSVlad Yasevich 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2046090096bfSVlad Yasevich 		err = dev_uc_add_excl(dev, addr);
2047090096bfSVlad Yasevich 	else if (is_multicast_ether_addr(addr))
2048090096bfSVlad Yasevich 		err = dev_mc_add_excl(dev, addr);
2049090096bfSVlad Yasevich 
2050090096bfSVlad Yasevich 	/* Only return duplicate errors if NLM_F_EXCL is set */
2051090096bfSVlad Yasevich 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
2052090096bfSVlad Yasevich 		err = 0;
2053090096bfSVlad Yasevich 
2054090096bfSVlad Yasevich 	return err;
2055090096bfSVlad Yasevich }
2056090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_add);
2057090096bfSVlad Yasevich 
2058*661d2967SThomas Graf static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh)
205977162022SJohn Fastabend {
206077162022SJohn Fastabend 	struct net *net = sock_net(skb->sk);
206177162022SJohn Fastabend 	struct ndmsg *ndm;
206277162022SJohn Fastabend 	struct nlattr *tb[NDA_MAX+1];
206377162022SJohn Fastabend 	struct net_device *dev;
206477162022SJohn Fastabend 	u8 *addr;
206577162022SJohn Fastabend 	int err;
206677162022SJohn Fastabend 
206777162022SJohn Fastabend 	err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
206877162022SJohn Fastabend 	if (err < 0)
206977162022SJohn Fastabend 		return err;
207077162022SJohn Fastabend 
207177162022SJohn Fastabend 	ndm = nlmsg_data(nlh);
207277162022SJohn Fastabend 	if (ndm->ndm_ifindex == 0) {
207377162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
207477162022SJohn Fastabend 		return -EINVAL;
207577162022SJohn Fastabend 	}
207677162022SJohn Fastabend 
207777162022SJohn Fastabend 	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
207877162022SJohn Fastabend 	if (dev == NULL) {
207977162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
208077162022SJohn Fastabend 		return -ENODEV;
208177162022SJohn Fastabend 	}
208277162022SJohn Fastabend 
208377162022SJohn Fastabend 	if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
208477162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
208577162022SJohn Fastabend 		return -EINVAL;
208677162022SJohn Fastabend 	}
208777162022SJohn Fastabend 
208877162022SJohn Fastabend 	addr = nla_data(tb[NDA_LLADDR]);
20896681712dSDavid Stevens 	if (is_zero_ether_addr(addr)) {
209077162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ether address\n");
209177162022SJohn Fastabend 		return -EINVAL;
209277162022SJohn Fastabend 	}
209377162022SJohn Fastabend 
209477162022SJohn Fastabend 	err = -EOPNOTSUPP;
209577162022SJohn Fastabend 
209677162022SJohn Fastabend 	/* Support fdb on master device the net/bridge default case */
209777162022SJohn Fastabend 	if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
209877162022SJohn Fastabend 	    (dev->priv_flags & IFF_BRIDGE_PORT)) {
2099898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2100898e5061SJiri Pirko 		const struct net_device_ops *ops = br_dev->netdev_ops;
2101898e5061SJiri Pirko 
2102898e5061SJiri Pirko 		err = ops->ndo_fdb_add(ndm, tb, dev, addr, nlh->nlmsg_flags);
210377162022SJohn Fastabend 		if (err)
210477162022SJohn Fastabend 			goto out;
210577162022SJohn Fastabend 		else
210677162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_MASTER;
210777162022SJohn Fastabend 	}
210877162022SJohn Fastabend 
210977162022SJohn Fastabend 	/* Embedded bridge, macvlan, and any other device support */
2110090096bfSVlad Yasevich 	if ((ndm->ndm_flags & NTF_SELF)) {
2111090096bfSVlad Yasevich 		if (dev->netdev_ops->ndo_fdb_add)
2112090096bfSVlad Yasevich 			err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
2113090096bfSVlad Yasevich 							   nlh->nlmsg_flags);
2114090096bfSVlad Yasevich 		else
2115090096bfSVlad Yasevich 			err = ndo_dflt_fdb_add(ndm, tb, dev, addr,
211677162022SJohn Fastabend 					       nlh->nlmsg_flags);
211777162022SJohn Fastabend 
21183ff661c3SJohn Fastabend 		if (!err) {
21193ff661c3SJohn Fastabend 			rtnl_fdb_notify(dev, addr, RTM_NEWNEIGH);
212077162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_SELF;
212177162022SJohn Fastabend 		}
21223ff661c3SJohn Fastabend 	}
212377162022SJohn Fastabend out:
212477162022SJohn Fastabend 	return err;
212577162022SJohn Fastabend }
212677162022SJohn Fastabend 
2127090096bfSVlad Yasevich /**
2128090096bfSVlad Yasevich  * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
2129090096bfSVlad Yasevich  */
2130090096bfSVlad Yasevich int ndo_dflt_fdb_del(struct ndmsg *ndm,
2131090096bfSVlad Yasevich 		     struct nlattr *tb[],
2132090096bfSVlad Yasevich 		     struct net_device *dev,
2133090096bfSVlad Yasevich 		     const unsigned char *addr)
2134090096bfSVlad Yasevich {
2135090096bfSVlad Yasevich 	int err = -EOPNOTSUPP;
2136090096bfSVlad Yasevich 
2137090096bfSVlad Yasevich 	/* If aging addresses are supported device will need to
2138090096bfSVlad Yasevich 	 * implement its own handler for this.
2139090096bfSVlad Yasevich 	 */
2140090096bfSVlad Yasevich 	if (ndm->ndm_state & NUD_PERMANENT) {
2141090096bfSVlad Yasevich 		pr_info("%s: FDB only supports static addresses\n", dev->name);
2142090096bfSVlad Yasevich 		return -EINVAL;
2143090096bfSVlad Yasevich 	}
2144090096bfSVlad Yasevich 
2145090096bfSVlad Yasevich 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2146090096bfSVlad Yasevich 		err = dev_uc_del(dev, addr);
2147090096bfSVlad Yasevich 	else if (is_multicast_ether_addr(addr))
2148090096bfSVlad Yasevich 		err = dev_mc_del(dev, addr);
2149090096bfSVlad Yasevich 	else
2150090096bfSVlad Yasevich 		err = -EINVAL;
2151090096bfSVlad Yasevich 
2152090096bfSVlad Yasevich 	return err;
2153090096bfSVlad Yasevich }
2154090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_del);
2155090096bfSVlad Yasevich 
2156*661d2967SThomas Graf static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh)
215777162022SJohn Fastabend {
215877162022SJohn Fastabend 	struct net *net = sock_net(skb->sk);
215977162022SJohn Fastabend 	struct ndmsg *ndm;
21601690be63SVlad Yasevich 	struct nlattr *tb[NDA_MAX+1];
216177162022SJohn Fastabend 	struct net_device *dev;
216277162022SJohn Fastabend 	int err = -EINVAL;
216377162022SJohn Fastabend 	__u8 *addr;
216477162022SJohn Fastabend 
21651690be63SVlad Yasevich 	if (!capable(CAP_NET_ADMIN))
21661690be63SVlad Yasevich 		return -EPERM;
21671690be63SVlad Yasevich 
21681690be63SVlad Yasevich 	err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
21691690be63SVlad Yasevich 	if (err < 0)
21701690be63SVlad Yasevich 		return err;
217177162022SJohn Fastabend 
217277162022SJohn Fastabend 	ndm = nlmsg_data(nlh);
217377162022SJohn Fastabend 	if (ndm->ndm_ifindex == 0) {
217477162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
217577162022SJohn Fastabend 		return -EINVAL;
217677162022SJohn Fastabend 	}
217777162022SJohn Fastabend 
217877162022SJohn Fastabend 	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
217977162022SJohn Fastabend 	if (dev == NULL) {
218077162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
218177162022SJohn Fastabend 		return -ENODEV;
218277162022SJohn Fastabend 	}
218377162022SJohn Fastabend 
21841690be63SVlad Yasevich 	if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
21851690be63SVlad Yasevich 		pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n");
218677162022SJohn Fastabend 		return -EINVAL;
218777162022SJohn Fastabend 	}
218877162022SJohn Fastabend 
21891690be63SVlad Yasevich 	addr = nla_data(tb[NDA_LLADDR]);
21901690be63SVlad Yasevich 	if (!is_valid_ether_addr(addr)) {
21911690be63SVlad Yasevich 		pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ether address\n");
21921690be63SVlad Yasevich 		return -EINVAL;
21931690be63SVlad Yasevich 	}
21941690be63SVlad Yasevich 
219577162022SJohn Fastabend 	err = -EOPNOTSUPP;
219677162022SJohn Fastabend 
219777162022SJohn Fastabend 	/* Support fdb on master device the net/bridge default case */
219877162022SJohn Fastabend 	if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
219977162022SJohn Fastabend 	    (dev->priv_flags & IFF_BRIDGE_PORT)) {
2200898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2201898e5061SJiri Pirko 		const struct net_device_ops *ops = br_dev->netdev_ops;
220277162022SJohn Fastabend 
2203898e5061SJiri Pirko 		if (ops->ndo_fdb_del)
22041690be63SVlad Yasevich 			err = ops->ndo_fdb_del(ndm, tb, dev, addr);
220577162022SJohn Fastabend 
220677162022SJohn Fastabend 		if (err)
220777162022SJohn Fastabend 			goto out;
220877162022SJohn Fastabend 		else
220977162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_MASTER;
221077162022SJohn Fastabend 	}
221177162022SJohn Fastabend 
221277162022SJohn Fastabend 	/* Embedded bridge, macvlan, and any other device support */
2213090096bfSVlad Yasevich 	if (ndm->ndm_flags & NTF_SELF) {
2214090096bfSVlad Yasevich 		if (dev->netdev_ops->ndo_fdb_del)
22151690be63SVlad Yasevich 			err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr);
2216090096bfSVlad Yasevich 		else
2217090096bfSVlad Yasevich 			err = ndo_dflt_fdb_del(ndm, tb, dev, addr);
221877162022SJohn Fastabend 
22193ff661c3SJohn Fastabend 		if (!err) {
22203ff661c3SJohn Fastabend 			rtnl_fdb_notify(dev, addr, RTM_DELNEIGH);
222177162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_SELF;
222277162022SJohn Fastabend 		}
22233ff661c3SJohn Fastabend 	}
222477162022SJohn Fastabend out:
222577162022SJohn Fastabend 	return err;
222677162022SJohn Fastabend }
222777162022SJohn Fastabend 
2228d83b0603SJohn Fastabend static int nlmsg_populate_fdb(struct sk_buff *skb,
2229d83b0603SJohn Fastabend 			      struct netlink_callback *cb,
2230d83b0603SJohn Fastabend 			      struct net_device *dev,
2231d83b0603SJohn Fastabend 			      int *idx,
2232d83b0603SJohn Fastabend 			      struct netdev_hw_addr_list *list)
2233d83b0603SJohn Fastabend {
2234d83b0603SJohn Fastabend 	struct netdev_hw_addr *ha;
2235d83b0603SJohn Fastabend 	int err;
223615e47304SEric W. Biederman 	u32 portid, seq;
2237d83b0603SJohn Fastabend 
223815e47304SEric W. Biederman 	portid = NETLINK_CB(cb->skb).portid;
2239d83b0603SJohn Fastabend 	seq = cb->nlh->nlmsg_seq;
2240d83b0603SJohn Fastabend 
2241d83b0603SJohn Fastabend 	list_for_each_entry(ha, &list->list, list) {
2242d83b0603SJohn Fastabend 		if (*idx < cb->args[0])
2243d83b0603SJohn Fastabend 			goto skip;
2244d83b0603SJohn Fastabend 
2245d83b0603SJohn Fastabend 		err = nlmsg_populate_fdb_fill(skb, dev, ha->addr,
2246a7a558feSJohn Fastabend 					      portid, seq,
2247a7a558feSJohn Fastabend 					      RTM_NEWNEIGH, NTF_SELF);
2248d83b0603SJohn Fastabend 		if (err < 0)
2249d83b0603SJohn Fastabend 			return err;
2250d83b0603SJohn Fastabend skip:
2251d83b0603SJohn Fastabend 		*idx += 1;
2252d83b0603SJohn Fastabend 	}
2253d83b0603SJohn Fastabend 	return 0;
2254d83b0603SJohn Fastabend }
2255d83b0603SJohn Fastabend 
2256d83b0603SJohn Fastabend /**
22572c53040fSBen Hutchings  * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
2258d83b0603SJohn Fastabend  * @nlh: netlink message header
2259d83b0603SJohn Fastabend  * @dev: netdevice
2260d83b0603SJohn Fastabend  *
2261d83b0603SJohn Fastabend  * Default netdevice operation to dump the existing unicast address list.
2262d83b0603SJohn Fastabend  * Returns zero on success.
2263d83b0603SJohn Fastabend  */
2264d83b0603SJohn Fastabend int ndo_dflt_fdb_dump(struct sk_buff *skb,
2265d83b0603SJohn Fastabend 		      struct netlink_callback *cb,
2266d83b0603SJohn Fastabend 		      struct net_device *dev,
2267d83b0603SJohn Fastabend 		      int idx)
2268d83b0603SJohn Fastabend {
2269d83b0603SJohn Fastabend 	int err;
2270d83b0603SJohn Fastabend 
2271d83b0603SJohn Fastabend 	netif_addr_lock_bh(dev);
2272d83b0603SJohn Fastabend 	err = nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->uc);
2273d83b0603SJohn Fastabend 	if (err)
2274d83b0603SJohn Fastabend 		goto out;
2275d83b0603SJohn Fastabend 	nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->mc);
2276d83b0603SJohn Fastabend out:
2277d83b0603SJohn Fastabend 	netif_addr_unlock_bh(dev);
2278d83b0603SJohn Fastabend 	return idx;
2279d83b0603SJohn Fastabend }
2280d83b0603SJohn Fastabend EXPORT_SYMBOL(ndo_dflt_fdb_dump);
2281d83b0603SJohn Fastabend 
228277162022SJohn Fastabend static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
228377162022SJohn Fastabend {
228477162022SJohn Fastabend 	int idx = 0;
228577162022SJohn Fastabend 	struct net *net = sock_net(skb->sk);
228677162022SJohn Fastabend 	struct net_device *dev;
228777162022SJohn Fastabend 
228877162022SJohn Fastabend 	rcu_read_lock();
228977162022SJohn Fastabend 	for_each_netdev_rcu(net, dev) {
229077162022SJohn Fastabend 		if (dev->priv_flags & IFF_BRIDGE_PORT) {
2291898e5061SJiri Pirko 			struct net_device *br_dev;
2292898e5061SJiri Pirko 			const struct net_device_ops *ops;
229377162022SJohn Fastabend 
2294898e5061SJiri Pirko 			br_dev = netdev_master_upper_dev_get(dev);
2295898e5061SJiri Pirko 			ops = br_dev->netdev_ops;
229677162022SJohn Fastabend 			if (ops->ndo_fdb_dump)
229777162022SJohn Fastabend 				idx = ops->ndo_fdb_dump(skb, cb, dev, idx);
229877162022SJohn Fastabend 		}
229977162022SJohn Fastabend 
230077162022SJohn Fastabend 		if (dev->netdev_ops->ndo_fdb_dump)
230177162022SJohn Fastabend 			idx = dev->netdev_ops->ndo_fdb_dump(skb, cb, dev, idx);
2302090096bfSVlad Yasevich 		else
2303090096bfSVlad Yasevich 			ndo_dflt_fdb_dump(skb, cb, dev, idx);
230477162022SJohn Fastabend 	}
230577162022SJohn Fastabend 	rcu_read_unlock();
230677162022SJohn Fastabend 
230777162022SJohn Fastabend 	cb->args[0] = idx;
230877162022SJohn Fastabend 	return skb->len;
230977162022SJohn Fastabend }
231077162022SJohn Fastabend 
2311815cccbfSJohn Fastabend int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
2312815cccbfSJohn Fastabend 			    struct net_device *dev, u16 mode)
2313815cccbfSJohn Fastabend {
2314815cccbfSJohn Fastabend 	struct nlmsghdr *nlh;
2315815cccbfSJohn Fastabend 	struct ifinfomsg *ifm;
2316815cccbfSJohn Fastabend 	struct nlattr *br_afspec;
2317815cccbfSJohn Fastabend 	u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
2318898e5061SJiri Pirko 	struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2319815cccbfSJohn Fastabend 
2320815cccbfSJohn Fastabend 	nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), NLM_F_MULTI);
2321815cccbfSJohn Fastabend 	if (nlh == NULL)
2322815cccbfSJohn Fastabend 		return -EMSGSIZE;
2323815cccbfSJohn Fastabend 
2324815cccbfSJohn Fastabend 	ifm = nlmsg_data(nlh);
2325815cccbfSJohn Fastabend 	ifm->ifi_family = AF_BRIDGE;
2326815cccbfSJohn Fastabend 	ifm->__ifi_pad = 0;
2327815cccbfSJohn Fastabend 	ifm->ifi_type = dev->type;
2328815cccbfSJohn Fastabend 	ifm->ifi_index = dev->ifindex;
2329815cccbfSJohn Fastabend 	ifm->ifi_flags = dev_get_flags(dev);
2330815cccbfSJohn Fastabend 	ifm->ifi_change = 0;
2331815cccbfSJohn Fastabend 
2332815cccbfSJohn Fastabend 
2333815cccbfSJohn Fastabend 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
2334815cccbfSJohn Fastabend 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
2335815cccbfSJohn Fastabend 	    nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
2336898e5061SJiri Pirko 	    (br_dev &&
2337898e5061SJiri Pirko 	     nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
2338815cccbfSJohn Fastabend 	    (dev->addr_len &&
2339815cccbfSJohn Fastabend 	     nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
2340815cccbfSJohn Fastabend 	    (dev->ifindex != dev->iflink &&
2341815cccbfSJohn Fastabend 	     nla_put_u32(skb, IFLA_LINK, dev->iflink)))
2342815cccbfSJohn Fastabend 		goto nla_put_failure;
2343815cccbfSJohn Fastabend 
2344815cccbfSJohn Fastabend 	br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
2345815cccbfSJohn Fastabend 	if (!br_afspec)
2346815cccbfSJohn Fastabend 		goto nla_put_failure;
2347815cccbfSJohn Fastabend 
2348815cccbfSJohn Fastabend 	if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF) ||
2349815cccbfSJohn Fastabend 	    nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
2350815cccbfSJohn Fastabend 		nla_nest_cancel(skb, br_afspec);
2351815cccbfSJohn Fastabend 		goto nla_put_failure;
2352815cccbfSJohn Fastabend 	}
2353815cccbfSJohn Fastabend 	nla_nest_end(skb, br_afspec);
2354815cccbfSJohn Fastabend 
2355815cccbfSJohn Fastabend 	return nlmsg_end(skb, nlh);
2356815cccbfSJohn Fastabend nla_put_failure:
2357815cccbfSJohn Fastabend 	nlmsg_cancel(skb, nlh);
2358815cccbfSJohn Fastabend 	return -EMSGSIZE;
2359815cccbfSJohn Fastabend }
2360815cccbfSJohn Fastabend EXPORT_SYMBOL(ndo_dflt_bridge_getlink);
2361815cccbfSJohn Fastabend 
2362e5a55a89SJohn Fastabend static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
2363e5a55a89SJohn Fastabend {
2364e5a55a89SJohn Fastabend 	struct net *net = sock_net(skb->sk);
2365e5a55a89SJohn Fastabend 	struct net_device *dev;
2366e5a55a89SJohn Fastabend 	int idx = 0;
2367e5a55a89SJohn Fastabend 	u32 portid = NETLINK_CB(cb->skb).portid;
2368e5a55a89SJohn Fastabend 	u32 seq = cb->nlh->nlmsg_seq;
23696cbdceebSVlad Yasevich 	struct nlattr *extfilt;
23706cbdceebSVlad Yasevich 	u32 filter_mask = 0;
23716cbdceebSVlad Yasevich 
23726cbdceebSVlad Yasevich 	extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct rtgenmsg),
23736cbdceebSVlad Yasevich 				  IFLA_EXT_MASK);
23746cbdceebSVlad Yasevich 	if (extfilt)
23756cbdceebSVlad Yasevich 		filter_mask = nla_get_u32(extfilt);
2376e5a55a89SJohn Fastabend 
2377e5a55a89SJohn Fastabend 	rcu_read_lock();
2378e5a55a89SJohn Fastabend 	for_each_netdev_rcu(net, dev) {
2379e5a55a89SJohn Fastabend 		const struct net_device_ops *ops = dev->netdev_ops;
2380898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2381e5a55a89SJohn Fastabend 
2382898e5061SJiri Pirko 		if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
238325b1e679SBen Hutchings 			if (idx >= cb->args[0] &&
2384898e5061SJiri Pirko 			    br_dev->netdev_ops->ndo_bridge_getlink(
23856cbdceebSVlad Yasevich 				    skb, portid, seq, dev, filter_mask) < 0)
2386e5a55a89SJohn Fastabend 				break;
2387e5a55a89SJohn Fastabend 			idx++;
2388e5a55a89SJohn Fastabend 		}
2389e5a55a89SJohn Fastabend 
2390e5a55a89SJohn Fastabend 		if (ops->ndo_bridge_getlink) {
239125b1e679SBen Hutchings 			if (idx >= cb->args[0] &&
23926cbdceebSVlad Yasevich 			    ops->ndo_bridge_getlink(skb, portid, seq, dev,
23936cbdceebSVlad Yasevich 						    filter_mask) < 0)
2394e5a55a89SJohn Fastabend 				break;
2395e5a55a89SJohn Fastabend 			idx++;
2396e5a55a89SJohn Fastabend 		}
2397e5a55a89SJohn Fastabend 	}
2398e5a55a89SJohn Fastabend 	rcu_read_unlock();
2399e5a55a89SJohn Fastabend 	cb->args[0] = idx;
2400e5a55a89SJohn Fastabend 
2401e5a55a89SJohn Fastabend 	return skb->len;
2402e5a55a89SJohn Fastabend }
2403e5a55a89SJohn Fastabend 
24042469ffd7SJohn Fastabend static inline size_t bridge_nlmsg_size(void)
24052469ffd7SJohn Fastabend {
24062469ffd7SJohn Fastabend 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
24072469ffd7SJohn Fastabend 		+ nla_total_size(IFNAMSIZ)	/* IFLA_IFNAME */
24082469ffd7SJohn Fastabend 		+ nla_total_size(MAX_ADDR_LEN)	/* IFLA_ADDRESS */
24092469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_MASTER */
24102469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_MTU */
24112469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_LINK */
24122469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_OPERSTATE */
24132469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u8))	/* IFLA_PROTINFO */
24142469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(struct nlattr))	/* IFLA_AF_SPEC */
24152469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u16))	/* IFLA_BRIDGE_FLAGS */
24162469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u16));	/* IFLA_BRIDGE_MODE */
24172469ffd7SJohn Fastabend }
24182469ffd7SJohn Fastabend 
24192469ffd7SJohn Fastabend static int rtnl_bridge_notify(struct net_device *dev, u16 flags)
24202469ffd7SJohn Fastabend {
24212469ffd7SJohn Fastabend 	struct net *net = dev_net(dev);
2422898e5061SJiri Pirko 	struct net_device *br_dev = netdev_master_upper_dev_get(dev);
24232469ffd7SJohn Fastabend 	struct sk_buff *skb;
24242469ffd7SJohn Fastabend 	int err = -EOPNOTSUPP;
24252469ffd7SJohn Fastabend 
24262469ffd7SJohn Fastabend 	skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
24272469ffd7SJohn Fastabend 	if (!skb) {
24282469ffd7SJohn Fastabend 		err = -ENOMEM;
24292469ffd7SJohn Fastabend 		goto errout;
24302469ffd7SJohn Fastabend 	}
24312469ffd7SJohn Fastabend 
2432c38e01b8SJohn Fastabend 	if ((!flags || (flags & BRIDGE_FLAGS_MASTER)) &&
2433898e5061SJiri Pirko 	    br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
24346cbdceebSVlad Yasevich 		err = br_dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0);
24352469ffd7SJohn Fastabend 		if (err < 0)
24362469ffd7SJohn Fastabend 			goto errout;
2437c38e01b8SJohn Fastabend 	}
2438c38e01b8SJohn Fastabend 
2439c38e01b8SJohn Fastabend 	if ((flags & BRIDGE_FLAGS_SELF) &&
2440c38e01b8SJohn Fastabend 	    dev->netdev_ops->ndo_bridge_getlink) {
24416cbdceebSVlad Yasevich 		err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0);
2442c38e01b8SJohn Fastabend 		if (err < 0)
2443c38e01b8SJohn Fastabend 			goto errout;
2444c38e01b8SJohn Fastabend 	}
24452469ffd7SJohn Fastabend 
24462469ffd7SJohn Fastabend 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
24472469ffd7SJohn Fastabend 	return 0;
24482469ffd7SJohn Fastabend errout:
24492469ffd7SJohn Fastabend 	WARN_ON(err == -EMSGSIZE);
24502469ffd7SJohn Fastabend 	kfree_skb(skb);
24512469ffd7SJohn Fastabend 	rtnl_set_sk_err(net, RTNLGRP_LINK, err);
24522469ffd7SJohn Fastabend 	return err;
24532469ffd7SJohn Fastabend }
24542469ffd7SJohn Fastabend 
2455*661d2967SThomas Graf static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
2456e5a55a89SJohn Fastabend {
2457e5a55a89SJohn Fastabend 	struct net *net = sock_net(skb->sk);
2458e5a55a89SJohn Fastabend 	struct ifinfomsg *ifm;
2459e5a55a89SJohn Fastabend 	struct net_device *dev;
24602469ffd7SJohn Fastabend 	struct nlattr *br_spec, *attr = NULL;
24612469ffd7SJohn Fastabend 	int rem, err = -EOPNOTSUPP;
2462c38e01b8SJohn Fastabend 	u16 oflags, flags = 0;
2463c38e01b8SJohn Fastabend 	bool have_flags = false;
2464e5a55a89SJohn Fastabend 
2465e5a55a89SJohn Fastabend 	if (nlmsg_len(nlh) < sizeof(*ifm))
2466e5a55a89SJohn Fastabend 		return -EINVAL;
2467e5a55a89SJohn Fastabend 
2468e5a55a89SJohn Fastabend 	ifm = nlmsg_data(nlh);
2469e5a55a89SJohn Fastabend 	if (ifm->ifi_family != AF_BRIDGE)
2470e5a55a89SJohn Fastabend 		return -EPFNOSUPPORT;
2471e5a55a89SJohn Fastabend 
2472e5a55a89SJohn Fastabend 	dev = __dev_get_by_index(net, ifm->ifi_index);
2473e5a55a89SJohn Fastabend 	if (!dev) {
2474e5a55a89SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
2475e5a55a89SJohn Fastabend 		return -ENODEV;
2476e5a55a89SJohn Fastabend 	}
2477e5a55a89SJohn Fastabend 
24782469ffd7SJohn Fastabend 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
24792469ffd7SJohn Fastabend 	if (br_spec) {
24802469ffd7SJohn Fastabend 		nla_for_each_nested(attr, br_spec, rem) {
24812469ffd7SJohn Fastabend 			if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
2482c38e01b8SJohn Fastabend 				have_flags = true;
24832469ffd7SJohn Fastabend 				flags = nla_get_u16(attr);
24842469ffd7SJohn Fastabend 				break;
24852469ffd7SJohn Fastabend 			}
24862469ffd7SJohn Fastabend 		}
24872469ffd7SJohn Fastabend 	}
24882469ffd7SJohn Fastabend 
2489c38e01b8SJohn Fastabend 	oflags = flags;
2490c38e01b8SJohn Fastabend 
24912469ffd7SJohn Fastabend 	if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
2492898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2493898e5061SJiri Pirko 
2494898e5061SJiri Pirko 		if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
24952469ffd7SJohn Fastabend 			err = -EOPNOTSUPP;
2496e5a55a89SJohn Fastabend 			goto out;
2497e5a55a89SJohn Fastabend 		}
2498e5a55a89SJohn Fastabend 
2499898e5061SJiri Pirko 		err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh);
25002469ffd7SJohn Fastabend 		if (err)
25012469ffd7SJohn Fastabend 			goto out;
25022469ffd7SJohn Fastabend 
25032469ffd7SJohn Fastabend 		flags &= ~BRIDGE_FLAGS_MASTER;
25042469ffd7SJohn Fastabend 	}
25052469ffd7SJohn Fastabend 
25062469ffd7SJohn Fastabend 	if ((flags & BRIDGE_FLAGS_SELF)) {
25072469ffd7SJohn Fastabend 		if (!dev->netdev_ops->ndo_bridge_setlink)
25082469ffd7SJohn Fastabend 			err = -EOPNOTSUPP;
25092469ffd7SJohn Fastabend 		else
2510e5a55a89SJohn Fastabend 			err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh);
2511e5a55a89SJohn Fastabend 
25122469ffd7SJohn Fastabend 		if (!err)
25132469ffd7SJohn Fastabend 			flags &= ~BRIDGE_FLAGS_SELF;
25142469ffd7SJohn Fastabend 	}
25152469ffd7SJohn Fastabend 
2516c38e01b8SJohn Fastabend 	if (have_flags)
25172469ffd7SJohn Fastabend 		memcpy(nla_data(attr), &flags, sizeof(flags));
25182469ffd7SJohn Fastabend 	/* Generate event to notify upper layer of bridge change */
25192469ffd7SJohn Fastabend 	if (!err)
2520c38e01b8SJohn Fastabend 		err = rtnl_bridge_notify(dev, oflags);
2521e5a55a89SJohn Fastabend out:
2522e5a55a89SJohn Fastabend 	return err;
2523e5a55a89SJohn Fastabend }
2524e5a55a89SJohn Fastabend 
2525*661d2967SThomas Graf static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
2526407af329SVlad Yasevich {
2527407af329SVlad Yasevich 	struct net *net = sock_net(skb->sk);
2528407af329SVlad Yasevich 	struct ifinfomsg *ifm;
2529407af329SVlad Yasevich 	struct net_device *dev;
2530407af329SVlad Yasevich 	struct nlattr *br_spec, *attr = NULL;
2531407af329SVlad Yasevich 	int rem, err = -EOPNOTSUPP;
2532407af329SVlad Yasevich 	u16 oflags, flags = 0;
2533407af329SVlad Yasevich 	bool have_flags = false;
2534407af329SVlad Yasevich 
2535407af329SVlad Yasevich 	if (nlmsg_len(nlh) < sizeof(*ifm))
2536407af329SVlad Yasevich 		return -EINVAL;
2537407af329SVlad Yasevich 
2538407af329SVlad Yasevich 	ifm = nlmsg_data(nlh);
2539407af329SVlad Yasevich 	if (ifm->ifi_family != AF_BRIDGE)
2540407af329SVlad Yasevich 		return -EPFNOSUPPORT;
2541407af329SVlad Yasevich 
2542407af329SVlad Yasevich 	dev = __dev_get_by_index(net, ifm->ifi_index);
2543407af329SVlad Yasevich 	if (!dev) {
2544407af329SVlad Yasevich 		pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
2545407af329SVlad Yasevich 		return -ENODEV;
2546407af329SVlad Yasevich 	}
2547407af329SVlad Yasevich 
2548407af329SVlad Yasevich 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
2549407af329SVlad Yasevich 	if (br_spec) {
2550407af329SVlad Yasevich 		nla_for_each_nested(attr, br_spec, rem) {
2551407af329SVlad Yasevich 			if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
2552407af329SVlad Yasevich 				have_flags = true;
2553407af329SVlad Yasevich 				flags = nla_get_u16(attr);
2554407af329SVlad Yasevich 				break;
2555407af329SVlad Yasevich 			}
2556407af329SVlad Yasevich 		}
2557407af329SVlad Yasevich 	}
2558407af329SVlad Yasevich 
2559407af329SVlad Yasevich 	oflags = flags;
2560407af329SVlad Yasevich 
2561407af329SVlad Yasevich 	if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
2562407af329SVlad Yasevich 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2563407af329SVlad Yasevich 
2564407af329SVlad Yasevich 		if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
2565407af329SVlad Yasevich 			err = -EOPNOTSUPP;
2566407af329SVlad Yasevich 			goto out;
2567407af329SVlad Yasevich 		}
2568407af329SVlad Yasevich 
2569407af329SVlad Yasevich 		err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh);
2570407af329SVlad Yasevich 		if (err)
2571407af329SVlad Yasevich 			goto out;
2572407af329SVlad Yasevich 
2573407af329SVlad Yasevich 		flags &= ~BRIDGE_FLAGS_MASTER;
2574407af329SVlad Yasevich 	}
2575407af329SVlad Yasevich 
2576407af329SVlad Yasevich 	if ((flags & BRIDGE_FLAGS_SELF)) {
2577407af329SVlad Yasevich 		if (!dev->netdev_ops->ndo_bridge_dellink)
2578407af329SVlad Yasevich 			err = -EOPNOTSUPP;
2579407af329SVlad Yasevich 		else
2580407af329SVlad Yasevich 			err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh);
2581407af329SVlad Yasevich 
2582407af329SVlad Yasevich 		if (!err)
2583407af329SVlad Yasevich 			flags &= ~BRIDGE_FLAGS_SELF;
2584407af329SVlad Yasevich 	}
2585407af329SVlad Yasevich 
2586407af329SVlad Yasevich 	if (have_flags)
2587407af329SVlad Yasevich 		memcpy(nla_data(attr), &flags, sizeof(flags));
2588407af329SVlad Yasevich 	/* Generate event to notify upper layer of bridge change */
2589407af329SVlad Yasevich 	if (!err)
2590407af329SVlad Yasevich 		err = rtnl_bridge_notify(dev, oflags);
2591407af329SVlad Yasevich out:
2592407af329SVlad Yasevich 	return err;
2593407af329SVlad Yasevich }
2594407af329SVlad Yasevich 
25951da177e4SLinus Torvalds /* Process one rtnetlink message. */
25961da177e4SLinus Torvalds 
25971d00a4ebSThomas Graf static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
25981da177e4SLinus Torvalds {
25993b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
2600e2849863SThomas Graf 	rtnl_doit_func doit;
26011da177e4SLinus Torvalds 	int sz_idx, kind;
26021da177e4SLinus Torvalds 	int family;
26031da177e4SLinus Torvalds 	int type;
26042907c35fSEric Dumazet 	int err;
26051da177e4SLinus Torvalds 
26061da177e4SLinus Torvalds 	type = nlh->nlmsg_type;
26071da177e4SLinus Torvalds 	if (type > RTM_MAX)
2608038890feSThomas Graf 		return -EOPNOTSUPP;
26091da177e4SLinus Torvalds 
26101da177e4SLinus Torvalds 	type -= RTM_BASE;
26111da177e4SLinus Torvalds 
26121da177e4SLinus Torvalds 	/* All the messages must have at least 1 byte length */
26131da177e4SLinus Torvalds 	if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
26141da177e4SLinus Torvalds 		return 0;
26151da177e4SLinus Torvalds 
26161da177e4SLinus Torvalds 	family = ((struct rtgenmsg *)NLMSG_DATA(nlh))->rtgen_family;
26171da177e4SLinus Torvalds 	sz_idx = type>>2;
26181da177e4SLinus Torvalds 	kind = type&3;
26191da177e4SLinus Torvalds 
2620dfc47ef8SEric W. Biederman 	if (kind != 2 && !ns_capable(net->user_ns, CAP_NET_ADMIN))
26211d00a4ebSThomas Graf 		return -EPERM;
26221da177e4SLinus Torvalds 
2623b8f3ab42SDavid S. Miller 	if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
262497c53cacSDenis V. Lunev 		struct sock *rtnl;
2625e2849863SThomas Graf 		rtnl_dumpit_func dumpit;
2626c7ac8679SGreg Rose 		rtnl_calcit_func calcit;
2627c7ac8679SGreg Rose 		u16 min_dump_alloc = 0;
26281da177e4SLinus Torvalds 
2629e2849863SThomas Graf 		dumpit = rtnl_get_dumpit(family, type);
2630e2849863SThomas Graf 		if (dumpit == NULL)
2631038890feSThomas Graf 			return -EOPNOTSUPP;
2632c7ac8679SGreg Rose 		calcit = rtnl_get_calcit(family, type);
2633c7ac8679SGreg Rose 		if (calcit)
2634115c9b81SGreg Rose 			min_dump_alloc = calcit(skb, nlh);
26351da177e4SLinus Torvalds 
26362907c35fSEric Dumazet 		__rtnl_unlock();
263797c53cacSDenis V. Lunev 		rtnl = net->rtnl;
263880d326faSPablo Neira Ayuso 		{
263980d326faSPablo Neira Ayuso 			struct netlink_dump_control c = {
264080d326faSPablo Neira Ayuso 				.dump		= dumpit,
264180d326faSPablo Neira Ayuso 				.min_dump_alloc	= min_dump_alloc,
264280d326faSPablo Neira Ayuso 			};
264380d326faSPablo Neira Ayuso 			err = netlink_dump_start(rtnl, skb, nlh, &c);
264480d326faSPablo Neira Ayuso 		}
26452907c35fSEric Dumazet 		rtnl_lock();
26462907c35fSEric Dumazet 		return err;
26471da177e4SLinus Torvalds 	}
26481da177e4SLinus Torvalds 
2649e2849863SThomas Graf 	doit = rtnl_get_doit(family, type);
2650e2849863SThomas Graf 	if (doit == NULL)
2651038890feSThomas Graf 		return -EOPNOTSUPP;
26521da177e4SLinus Torvalds 
2653*661d2967SThomas Graf 	return doit(skb, nlh);
26541da177e4SLinus Torvalds }
26551da177e4SLinus Torvalds 
2656cd40b7d3SDenis V. Lunev static void rtnetlink_rcv(struct sk_buff *skb)
26571da177e4SLinus Torvalds {
26581536cc0dSDenis V. Lunev 	rtnl_lock();
2659cd40b7d3SDenis V. Lunev 	netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
26601536cc0dSDenis V. Lunev 	rtnl_unlock();
26611da177e4SLinus Torvalds }
26621da177e4SLinus Torvalds 
26631da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
26641da177e4SLinus Torvalds {
26651da177e4SLinus Torvalds 	struct net_device *dev = ptr;
2666e9dc8653SEric W. Biederman 
26671da177e4SLinus Torvalds 	switch (event) {
26681da177e4SLinus Torvalds 	case NETDEV_UP:
26691da177e4SLinus Torvalds 	case NETDEV_DOWN:
267010de05afSPatrick McHardy 	case NETDEV_PRE_UP:
2671d90a909eSEric W. Biederman 	case NETDEV_POST_INIT:
2672d90a909eSEric W. Biederman 	case NETDEV_REGISTER:
26731da177e4SLinus Torvalds 	case NETDEV_CHANGE:
2674755d0e77SPatrick McHardy 	case NETDEV_PRE_TYPE_CHANGE:
26751da177e4SLinus Torvalds 	case NETDEV_GOING_DOWN:
2676a2835763SPatrick McHardy 	case NETDEV_UNREGISTER:
26770115e8e3SEric Dumazet 	case NETDEV_UNREGISTER_FINAL:
2678ac3d3f81SAmerigo Wang 	case NETDEV_RELEASE:
2679ac3d3f81SAmerigo Wang 	case NETDEV_JOIN:
26801da177e4SLinus Torvalds 		break;
26811da177e4SLinus Torvalds 	default:
26821da177e4SLinus Torvalds 		rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
26831da177e4SLinus Torvalds 		break;
26841da177e4SLinus Torvalds 	}
26851da177e4SLinus Torvalds 	return NOTIFY_DONE;
26861da177e4SLinus Torvalds }
26871da177e4SLinus Torvalds 
26881da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = {
26891da177e4SLinus Torvalds 	.notifier_call	= rtnetlink_event,
26901da177e4SLinus Torvalds };
26911da177e4SLinus Torvalds 
269297c53cacSDenis V. Lunev 
26932c8c1e72SAlexey Dobriyan static int __net_init rtnetlink_net_init(struct net *net)
269497c53cacSDenis V. Lunev {
269597c53cacSDenis V. Lunev 	struct sock *sk;
2696a31f2d17SPablo Neira Ayuso 	struct netlink_kernel_cfg cfg = {
2697a31f2d17SPablo Neira Ayuso 		.groups		= RTNLGRP_MAX,
2698a31f2d17SPablo Neira Ayuso 		.input		= rtnetlink_rcv,
2699a31f2d17SPablo Neira Ayuso 		.cb_mutex	= &rtnl_mutex,
27009785e10aSPablo Neira Ayuso 		.flags		= NL_CFG_F_NONROOT_RECV,
2701a31f2d17SPablo Neira Ayuso 	};
2702a31f2d17SPablo Neira Ayuso 
27039f00d977SPablo Neira Ayuso 	sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
270497c53cacSDenis V. Lunev 	if (!sk)
270597c53cacSDenis V. Lunev 		return -ENOMEM;
270697c53cacSDenis V. Lunev 	net->rtnl = sk;
270797c53cacSDenis V. Lunev 	return 0;
270897c53cacSDenis V. Lunev }
270997c53cacSDenis V. Lunev 
27102c8c1e72SAlexey Dobriyan static void __net_exit rtnetlink_net_exit(struct net *net)
271197c53cacSDenis V. Lunev {
2712b7c6ba6eSDenis V. Lunev 	netlink_kernel_release(net->rtnl);
271397c53cacSDenis V. Lunev 	net->rtnl = NULL;
271497c53cacSDenis V. Lunev }
271597c53cacSDenis V. Lunev 
271697c53cacSDenis V. Lunev static struct pernet_operations rtnetlink_net_ops = {
271797c53cacSDenis V. Lunev 	.init = rtnetlink_net_init,
271897c53cacSDenis V. Lunev 	.exit = rtnetlink_net_exit,
271997c53cacSDenis V. Lunev };
272097c53cacSDenis V. Lunev 
27211da177e4SLinus Torvalds void __init rtnetlink_init(void)
27221da177e4SLinus Torvalds {
272397c53cacSDenis V. Lunev 	if (register_pernet_subsys(&rtnetlink_net_ops))
27241da177e4SLinus Torvalds 		panic("rtnetlink_init: cannot initialize rtnetlink\n");
272597c53cacSDenis V. Lunev 
27261da177e4SLinus Torvalds 	register_netdevice_notifier(&rtnetlink_dev_notifier);
2727340d17fcSThomas Graf 
2728c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
2729c7ac8679SGreg Rose 		      rtnl_dump_ifinfo, rtnl_calcit);
2730c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL);
2731c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL);
2732c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL);
2733687ad8ccSThomas Graf 
2734c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL);
2735c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL);
273677162022SJohn Fastabend 
273777162022SJohn Fastabend 	rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, NULL);
273877162022SJohn Fastabend 	rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, NULL);
273977162022SJohn Fastabend 	rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, NULL);
2740e5a55a89SJohn Fastabend 
2741e5a55a89SJohn Fastabend 	rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, NULL);
2742407af329SVlad Yasevich 	rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, NULL);
2743e5a55a89SJohn Fastabend 	rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, NULL);
27441da177e4SLinus Torvalds }
27451da177e4SLinus Torvalds 
2746