xref: /openbmc/linux/net/core/rtnetlink.c (revision d40156aa5ecbd51fed932ed4813df82b56e5ff4d)
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 
13151057f2fSThomas Graf 	return tab ? tab[msgindex].doit : NULL;
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 
14651057f2fSThomas Graf 	return tab ? tab[msgindex].dumpit : NULL;
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 
161c7ac8679SGreg Rose 	return tab ? tab[msgindex].calcit : NULL;
162c7ac8679SGreg Rose }
163c7ac8679SGreg Rose 
164e2849863SThomas Graf /**
165e2849863SThomas Graf  * __rtnl_register - Register a rtnetlink message type
166e2849863SThomas Graf  * @protocol: Protocol family or PF_UNSPEC
167e2849863SThomas Graf  * @msgtype: rtnetlink message type
168e2849863SThomas Graf  * @doit: Function pointer called for each request message
169e2849863SThomas Graf  * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
170c7ac8679SGreg Rose  * @calcit: Function pointer to calc size of dump message
171e2849863SThomas Graf  *
172e2849863SThomas Graf  * Registers the specified function pointers (at least one of them has
173e2849863SThomas Graf  * to be non-NULL) to be called whenever a request message for the
174e2849863SThomas Graf  * specified protocol family and message type is received.
175e2849863SThomas Graf  *
176e2849863SThomas Graf  * The special protocol family PF_UNSPEC may be used to define fallback
177e2849863SThomas Graf  * function pointers for the case when no entry for the specific protocol
178e2849863SThomas Graf  * family exists.
179e2849863SThomas Graf  *
180e2849863SThomas Graf  * Returns 0 on success or a negative error code.
181e2849863SThomas Graf  */
182e2849863SThomas Graf int __rtnl_register(int protocol, int msgtype,
183c7ac8679SGreg Rose 		    rtnl_doit_func doit, rtnl_dumpit_func dumpit,
184c7ac8679SGreg Rose 		    rtnl_calcit_func calcit)
185e2849863SThomas Graf {
186e2849863SThomas Graf 	struct rtnl_link *tab;
187e2849863SThomas Graf 	int msgindex;
188e2849863SThomas Graf 
18925239ceeSPatrick McHardy 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
190e2849863SThomas Graf 	msgindex = rtm_msgindex(msgtype);
191e2849863SThomas Graf 
192e2849863SThomas Graf 	tab = rtnl_msg_handlers[protocol];
193e2849863SThomas Graf 	if (tab == NULL) {
194e2849863SThomas Graf 		tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL);
195e2849863SThomas Graf 		if (tab == NULL)
196e2849863SThomas Graf 			return -ENOBUFS;
197e2849863SThomas Graf 
198e2849863SThomas Graf 		rtnl_msg_handlers[protocol] = tab;
199e2849863SThomas Graf 	}
200e2849863SThomas Graf 
201e2849863SThomas Graf 	if (doit)
202e2849863SThomas Graf 		tab[msgindex].doit = doit;
203e2849863SThomas Graf 
204e2849863SThomas Graf 	if (dumpit)
205e2849863SThomas Graf 		tab[msgindex].dumpit = dumpit;
206e2849863SThomas Graf 
207c7ac8679SGreg Rose 	if (calcit)
208c7ac8679SGreg Rose 		tab[msgindex].calcit = calcit;
209c7ac8679SGreg Rose 
210e2849863SThomas Graf 	return 0;
211e2849863SThomas Graf }
212e2849863SThomas Graf EXPORT_SYMBOL_GPL(__rtnl_register);
213e2849863SThomas Graf 
214e2849863SThomas Graf /**
215e2849863SThomas Graf  * rtnl_register - Register a rtnetlink message type
216e2849863SThomas Graf  *
217e2849863SThomas Graf  * Identical to __rtnl_register() but panics on failure. This is useful
218e2849863SThomas Graf  * as failure of this function is very unlikely, it can only happen due
219e2849863SThomas Graf  * to lack of memory when allocating the chain to store all message
220e2849863SThomas Graf  * handlers for a protocol. Meant for use in init functions where lack
22125985edcSLucas De Marchi  * of memory implies no sense in continuing.
222e2849863SThomas Graf  */
223e2849863SThomas Graf void rtnl_register(int protocol, int msgtype,
224c7ac8679SGreg Rose 		   rtnl_doit_func doit, rtnl_dumpit_func dumpit,
225c7ac8679SGreg Rose 		   rtnl_calcit_func calcit)
226e2849863SThomas Graf {
227c7ac8679SGreg Rose 	if (__rtnl_register(protocol, msgtype, doit, dumpit, calcit) < 0)
228e2849863SThomas Graf 		panic("Unable to register rtnetlink message handler, "
229e2849863SThomas Graf 		      "protocol = %d, message type = %d\n",
230e2849863SThomas Graf 		      protocol, msgtype);
231e2849863SThomas Graf }
232e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_register);
233e2849863SThomas Graf 
234e2849863SThomas Graf /**
235e2849863SThomas Graf  * rtnl_unregister - Unregister a rtnetlink message type
236e2849863SThomas Graf  * @protocol: Protocol family or PF_UNSPEC
237e2849863SThomas Graf  * @msgtype: rtnetlink message type
238e2849863SThomas Graf  *
239e2849863SThomas Graf  * Returns 0 on success or a negative error code.
240e2849863SThomas Graf  */
241e2849863SThomas Graf int rtnl_unregister(int protocol, int msgtype)
242e2849863SThomas Graf {
243e2849863SThomas Graf 	int msgindex;
244e2849863SThomas Graf 
24525239ceeSPatrick McHardy 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
246e2849863SThomas Graf 	msgindex = rtm_msgindex(msgtype);
247e2849863SThomas Graf 
248e2849863SThomas Graf 	if (rtnl_msg_handlers[protocol] == NULL)
249e2849863SThomas Graf 		return -ENOENT;
250e2849863SThomas Graf 
251e2849863SThomas Graf 	rtnl_msg_handlers[protocol][msgindex].doit = NULL;
252e2849863SThomas Graf 	rtnl_msg_handlers[protocol][msgindex].dumpit = NULL;
253e2849863SThomas Graf 
254e2849863SThomas Graf 	return 0;
255e2849863SThomas Graf }
256e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister);
257e2849863SThomas Graf 
258e2849863SThomas Graf /**
259e2849863SThomas Graf  * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
260e2849863SThomas Graf  * @protocol : Protocol family or PF_UNSPEC
261e2849863SThomas Graf  *
262e2849863SThomas Graf  * Identical to calling rtnl_unregster() for all registered message types
263e2849863SThomas Graf  * of a certain protocol family.
264e2849863SThomas Graf  */
265e2849863SThomas Graf void rtnl_unregister_all(int protocol)
266e2849863SThomas Graf {
26725239ceeSPatrick McHardy 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
268e2849863SThomas Graf 
269e2849863SThomas Graf 	kfree(rtnl_msg_handlers[protocol]);
270e2849863SThomas Graf 	rtnl_msg_handlers[protocol] = NULL;
271e2849863SThomas Graf }
272e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister_all);
2731da177e4SLinus Torvalds 
27438f7b870SPatrick McHardy static LIST_HEAD(link_ops);
27538f7b870SPatrick McHardy 
276c63044f0SEric Dumazet static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
277c63044f0SEric Dumazet {
278c63044f0SEric Dumazet 	const struct rtnl_link_ops *ops;
279c63044f0SEric Dumazet 
280c63044f0SEric Dumazet 	list_for_each_entry(ops, &link_ops, list) {
281c63044f0SEric Dumazet 		if (!strcmp(ops->kind, kind))
282c63044f0SEric Dumazet 			return ops;
283c63044f0SEric Dumazet 	}
284c63044f0SEric Dumazet 	return NULL;
285c63044f0SEric Dumazet }
286c63044f0SEric Dumazet 
28738f7b870SPatrick McHardy /**
28838f7b870SPatrick McHardy  * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
28938f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to register
29038f7b870SPatrick McHardy  *
29138f7b870SPatrick McHardy  * The caller must hold the rtnl_mutex. This function should be used
29238f7b870SPatrick McHardy  * by drivers that create devices during module initialization. It
29338f7b870SPatrick McHardy  * must be called before registering the devices.
29438f7b870SPatrick McHardy  *
29538f7b870SPatrick McHardy  * Returns 0 on success or a negative error code.
29638f7b870SPatrick McHardy  */
29738f7b870SPatrick McHardy int __rtnl_link_register(struct rtnl_link_ops *ops)
29838f7b870SPatrick McHardy {
299c63044f0SEric Dumazet 	if (rtnl_link_ops_get(ops->kind))
300c63044f0SEric Dumazet 		return -EEXIST;
301c63044f0SEric Dumazet 
3022d85cba2SPatrick McHardy 	if (!ops->dellink)
30323289a37SEric Dumazet 		ops->dellink = unregister_netdevice_queue;
3042d85cba2SPatrick McHardy 
30538f7b870SPatrick McHardy 	list_add_tail(&ops->list, &link_ops);
30638f7b870SPatrick McHardy 	return 0;
30738f7b870SPatrick McHardy }
30838f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_register);
30938f7b870SPatrick McHardy 
31038f7b870SPatrick McHardy /**
31138f7b870SPatrick McHardy  * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
31238f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to register
31338f7b870SPatrick McHardy  *
31438f7b870SPatrick McHardy  * Returns 0 on success or a negative error code.
31538f7b870SPatrick McHardy  */
31638f7b870SPatrick McHardy int rtnl_link_register(struct rtnl_link_ops *ops)
31738f7b870SPatrick McHardy {
31838f7b870SPatrick McHardy 	int err;
31938f7b870SPatrick McHardy 
32038f7b870SPatrick McHardy 	rtnl_lock();
32138f7b870SPatrick McHardy 	err = __rtnl_link_register(ops);
32238f7b870SPatrick McHardy 	rtnl_unlock();
32338f7b870SPatrick McHardy 	return err;
32438f7b870SPatrick McHardy }
32538f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_register);
32638f7b870SPatrick McHardy 
327669f87baSPavel Emelyanov static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
328669f87baSPavel Emelyanov {
329669f87baSPavel Emelyanov 	struct net_device *dev;
33023289a37SEric Dumazet 	LIST_HEAD(list_kill);
33123289a37SEric Dumazet 
332669f87baSPavel Emelyanov 	for_each_netdev(net, dev) {
33323289a37SEric Dumazet 		if (dev->rtnl_link_ops == ops)
33423289a37SEric Dumazet 			ops->dellink(dev, &list_kill);
335669f87baSPavel Emelyanov 	}
33623289a37SEric Dumazet 	unregister_netdevice_many(&list_kill);
337669f87baSPavel Emelyanov }
338669f87baSPavel Emelyanov 
33938f7b870SPatrick McHardy /**
34038f7b870SPatrick McHardy  * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
34138f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to unregister
34238f7b870SPatrick McHardy  *
3432d85cba2SPatrick McHardy  * The caller must hold the rtnl_mutex.
34438f7b870SPatrick McHardy  */
34538f7b870SPatrick McHardy void __rtnl_link_unregister(struct rtnl_link_ops *ops)
34638f7b870SPatrick McHardy {
347881d966bSEric W. Biederman 	struct net *net;
3482d85cba2SPatrick McHardy 
349881d966bSEric W. Biederman 	for_each_net(net) {
350669f87baSPavel Emelyanov 		__rtnl_kill_links(net, ops);
351881d966bSEric W. Biederman 	}
35238f7b870SPatrick McHardy 	list_del(&ops->list);
35338f7b870SPatrick McHardy }
35438f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
35538f7b870SPatrick McHardy 
35638f7b870SPatrick McHardy /**
35738f7b870SPatrick McHardy  * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
35838f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to unregister
35938f7b870SPatrick McHardy  */
36038f7b870SPatrick McHardy void rtnl_link_unregister(struct rtnl_link_ops *ops)
36138f7b870SPatrick McHardy {
36238f7b870SPatrick McHardy 	rtnl_lock();
36338f7b870SPatrick McHardy 	__rtnl_link_unregister(ops);
36438f7b870SPatrick McHardy 	rtnl_unlock();
36538f7b870SPatrick McHardy }
36638f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_unregister);
36738f7b870SPatrick McHardy 
36838f7b870SPatrick McHardy static size_t rtnl_link_get_size(const struct net_device *dev)
36938f7b870SPatrick McHardy {
37038f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
37138f7b870SPatrick McHardy 	size_t size;
37238f7b870SPatrick McHardy 
37338f7b870SPatrick McHardy 	if (!ops)
37438f7b870SPatrick McHardy 		return 0;
37538f7b870SPatrick McHardy 
376369cf77aSThomas Graf 	size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
377369cf77aSThomas Graf 	       nla_total_size(strlen(ops->kind) + 1);  /* IFLA_INFO_KIND */
37838f7b870SPatrick McHardy 
37938f7b870SPatrick McHardy 	if (ops->get_size)
38038f7b870SPatrick McHardy 		/* IFLA_INFO_DATA + nested data */
381369cf77aSThomas Graf 		size += nla_total_size(sizeof(struct nlattr)) +
38238f7b870SPatrick McHardy 			ops->get_size(dev);
38338f7b870SPatrick McHardy 
38438f7b870SPatrick McHardy 	if (ops->get_xstats_size)
385369cf77aSThomas Graf 		/* IFLA_INFO_XSTATS */
386369cf77aSThomas Graf 		size += nla_total_size(ops->get_xstats_size(dev));
38738f7b870SPatrick McHardy 
38838f7b870SPatrick McHardy 	return size;
38938f7b870SPatrick McHardy }
39038f7b870SPatrick McHardy 
391f8ff182cSThomas Graf static LIST_HEAD(rtnl_af_ops);
392f8ff182cSThomas Graf 
393f8ff182cSThomas Graf static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
394f8ff182cSThomas Graf {
395f8ff182cSThomas Graf 	const struct rtnl_af_ops *ops;
396f8ff182cSThomas Graf 
397f8ff182cSThomas Graf 	list_for_each_entry(ops, &rtnl_af_ops, list) {
398f8ff182cSThomas Graf 		if (ops->family == family)
399f8ff182cSThomas Graf 			return ops;
400f8ff182cSThomas Graf 	}
401f8ff182cSThomas Graf 
402f8ff182cSThomas Graf 	return NULL;
403f8ff182cSThomas Graf }
404f8ff182cSThomas Graf 
405f8ff182cSThomas Graf /**
406f8ff182cSThomas Graf  * __rtnl_af_register - Register rtnl_af_ops with rtnetlink.
407f8ff182cSThomas Graf  * @ops: struct rtnl_af_ops * to register
408f8ff182cSThomas Graf  *
409f8ff182cSThomas Graf  * The caller must hold the rtnl_mutex.
410f8ff182cSThomas Graf  *
411f8ff182cSThomas Graf  * Returns 0 on success or a negative error code.
412f8ff182cSThomas Graf  */
413f8ff182cSThomas Graf int __rtnl_af_register(struct rtnl_af_ops *ops)
414f8ff182cSThomas Graf {
415f8ff182cSThomas Graf 	list_add_tail(&ops->list, &rtnl_af_ops);
416f8ff182cSThomas Graf 	return 0;
417f8ff182cSThomas Graf }
418f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(__rtnl_af_register);
419f8ff182cSThomas Graf 
420f8ff182cSThomas Graf /**
421f8ff182cSThomas Graf  * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
422f8ff182cSThomas Graf  * @ops: struct rtnl_af_ops * to register
423f8ff182cSThomas Graf  *
424f8ff182cSThomas Graf  * Returns 0 on success or a negative error code.
425f8ff182cSThomas Graf  */
426f8ff182cSThomas Graf int rtnl_af_register(struct rtnl_af_ops *ops)
427f8ff182cSThomas Graf {
428f8ff182cSThomas Graf 	int err;
429f8ff182cSThomas Graf 
430f8ff182cSThomas Graf 	rtnl_lock();
431f8ff182cSThomas Graf 	err = __rtnl_af_register(ops);
432f8ff182cSThomas Graf 	rtnl_unlock();
433f8ff182cSThomas Graf 	return err;
434f8ff182cSThomas Graf }
435f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_register);
436f8ff182cSThomas Graf 
437f8ff182cSThomas Graf /**
438f8ff182cSThomas Graf  * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
439f8ff182cSThomas Graf  * @ops: struct rtnl_af_ops * to unregister
440f8ff182cSThomas Graf  *
441f8ff182cSThomas Graf  * The caller must hold the rtnl_mutex.
442f8ff182cSThomas Graf  */
443f8ff182cSThomas Graf void __rtnl_af_unregister(struct rtnl_af_ops *ops)
444f8ff182cSThomas Graf {
445f8ff182cSThomas Graf 	list_del(&ops->list);
446f8ff182cSThomas Graf }
447f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(__rtnl_af_unregister);
448f8ff182cSThomas Graf 
449f8ff182cSThomas Graf /**
450f8ff182cSThomas Graf  * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
451f8ff182cSThomas Graf  * @ops: struct rtnl_af_ops * to unregister
452f8ff182cSThomas Graf  */
453f8ff182cSThomas Graf void rtnl_af_unregister(struct rtnl_af_ops *ops)
454f8ff182cSThomas Graf {
455f8ff182cSThomas Graf 	rtnl_lock();
456f8ff182cSThomas Graf 	__rtnl_af_unregister(ops);
457f8ff182cSThomas Graf 	rtnl_unlock();
458f8ff182cSThomas Graf }
459f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_unregister);
460f8ff182cSThomas Graf 
461f8ff182cSThomas Graf static size_t rtnl_link_get_af_size(const struct net_device *dev)
462f8ff182cSThomas Graf {
463f8ff182cSThomas Graf 	struct rtnl_af_ops *af_ops;
464f8ff182cSThomas Graf 	size_t size;
465f8ff182cSThomas Graf 
466f8ff182cSThomas Graf 	/* IFLA_AF_SPEC */
467f8ff182cSThomas Graf 	size = nla_total_size(sizeof(struct nlattr));
468f8ff182cSThomas Graf 
469f8ff182cSThomas Graf 	list_for_each_entry(af_ops, &rtnl_af_ops, list) {
470f8ff182cSThomas Graf 		if (af_ops->get_link_af_size) {
471f8ff182cSThomas Graf 			/* AF_* + nested data */
472f8ff182cSThomas Graf 			size += nla_total_size(sizeof(struct nlattr)) +
473f8ff182cSThomas Graf 				af_ops->get_link_af_size(dev);
474f8ff182cSThomas Graf 		}
475f8ff182cSThomas Graf 	}
476f8ff182cSThomas Graf 
477f8ff182cSThomas Graf 	return size;
478f8ff182cSThomas Graf }
479f8ff182cSThomas Graf 
48038f7b870SPatrick McHardy static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
48138f7b870SPatrick McHardy {
48238f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
48338f7b870SPatrick McHardy 	struct nlattr *linkinfo, *data;
48438f7b870SPatrick McHardy 	int err = -EMSGSIZE;
48538f7b870SPatrick McHardy 
48638f7b870SPatrick McHardy 	linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
48738f7b870SPatrick McHardy 	if (linkinfo == NULL)
48838f7b870SPatrick McHardy 		goto out;
48938f7b870SPatrick McHardy 
49038f7b870SPatrick McHardy 	if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
49138f7b870SPatrick McHardy 		goto err_cancel_link;
49238f7b870SPatrick McHardy 	if (ops->fill_xstats) {
49338f7b870SPatrick McHardy 		err = ops->fill_xstats(skb, dev);
49438f7b870SPatrick McHardy 		if (err < 0)
49538f7b870SPatrick McHardy 			goto err_cancel_link;
49638f7b870SPatrick McHardy 	}
49738f7b870SPatrick McHardy 	if (ops->fill_info) {
49838f7b870SPatrick McHardy 		data = nla_nest_start(skb, IFLA_INFO_DATA);
49938f7b870SPatrick McHardy 		if (data == NULL)
50038f7b870SPatrick McHardy 			goto err_cancel_link;
50138f7b870SPatrick McHardy 		err = ops->fill_info(skb, dev);
50238f7b870SPatrick McHardy 		if (err < 0)
50338f7b870SPatrick McHardy 			goto err_cancel_data;
50438f7b870SPatrick McHardy 		nla_nest_end(skb, data);
50538f7b870SPatrick McHardy 	}
50638f7b870SPatrick McHardy 
50738f7b870SPatrick McHardy 	nla_nest_end(skb, linkinfo);
50838f7b870SPatrick McHardy 	return 0;
50938f7b870SPatrick McHardy 
51038f7b870SPatrick McHardy err_cancel_data:
51138f7b870SPatrick McHardy 	nla_nest_cancel(skb, data);
51238f7b870SPatrick McHardy err_cancel_link:
51338f7b870SPatrick McHardy 	nla_nest_cancel(skb, linkinfo);
51438f7b870SPatrick McHardy out:
51538f7b870SPatrick McHardy 	return err;
51638f7b870SPatrick McHardy }
51738f7b870SPatrick McHardy 
518db46edc6SThomas Graf static const int rtm_min[RTM_NR_FAMILIES] =
5191da177e4SLinus Torvalds {
520f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWLINK)]      = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
521f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWADDR)]      = NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
522f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWROUTE)]     = NLMSG_LENGTH(sizeof(struct rtmsg)),
52314c0b97dSThomas Graf 	[RTM_FAM(RTM_NEWRULE)]      = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)),
524f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWQDISC)]     = NLMSG_LENGTH(sizeof(struct tcmsg)),
525f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWTCLASS)]    = NLMSG_LENGTH(sizeof(struct tcmsg)),
526f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWTFILTER)]   = NLMSG_LENGTH(sizeof(struct tcmsg)),
527f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWACTION)]    = NLMSG_LENGTH(sizeof(struct tcamsg)),
528f90a0a74SThomas Graf 	[RTM_FAM(RTM_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
529f90a0a74SThomas Graf 	[RTM_FAM(RTM_GETANYCAST)]   = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
5301da177e4SLinus Torvalds };
5311da177e4SLinus Torvalds 
532db46edc6SThomas Graf static const int rta_max[RTM_NR_FAMILIES] =
5331da177e4SLinus Torvalds {
534f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWLINK)]      = IFLA_MAX,
535f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWADDR)]      = IFA_MAX,
536f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWROUTE)]     = RTA_MAX,
53714c0b97dSThomas Graf 	[RTM_FAM(RTM_NEWRULE)]      = FRA_MAX,
538f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWQDISC)]     = TCA_MAX,
539f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWTCLASS)]    = TCA_MAX,
540f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWTFILTER)]   = TCA_MAX,
541f90a0a74SThomas Graf 	[RTM_FAM(RTM_NEWACTION)]    = TCAA_MAX,
5421da177e4SLinus Torvalds };
5431da177e4SLinus Torvalds 
54495c96174SEric Dumazet int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
5451da177e4SLinus Torvalds {
54697c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
5471da177e4SLinus Torvalds 	int err = 0;
5481da177e4SLinus Torvalds 
549ac6d439dSPatrick McHardy 	NETLINK_CB(skb).dst_group = group;
5501da177e4SLinus Torvalds 	if (echo)
5511da177e4SLinus Torvalds 		atomic_inc(&skb->users);
5521da177e4SLinus Torvalds 	netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
5531da177e4SLinus Torvalds 	if (echo)
5541da177e4SLinus Torvalds 		err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
5551da177e4SLinus Torvalds 	return err;
5561da177e4SLinus Torvalds }
5571da177e4SLinus Torvalds 
55897c53cacSDenis V. Lunev int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
5592942e900SThomas Graf {
56097c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
56197c53cacSDenis V. Lunev 
5622942e900SThomas Graf 	return nlmsg_unicast(rtnl, skb, pid);
5632942e900SThomas Graf }
564e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unicast);
5652942e900SThomas Graf 
5661ce85fe4SPablo Neira Ayuso void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
56797676b6bSThomas Graf 		 struct nlmsghdr *nlh, gfp_t flags)
56897676b6bSThomas Graf {
56997c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
57097676b6bSThomas Graf 	int report = 0;
57197676b6bSThomas Graf 
57297676b6bSThomas Graf 	if (nlh)
57397676b6bSThomas Graf 		report = nlmsg_report(nlh);
57497676b6bSThomas Graf 
5751ce85fe4SPablo Neira Ayuso 	nlmsg_notify(rtnl, skb, pid, group, report, flags);
57697676b6bSThomas Graf }
577e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_notify);
57897676b6bSThomas Graf 
57997c53cacSDenis V. Lunev void rtnl_set_sk_err(struct net *net, u32 group, int error)
58097676b6bSThomas Graf {
58197c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
58297c53cacSDenis V. Lunev 
58397676b6bSThomas Graf 	netlink_set_err(rtnl, 0, group, error);
58497676b6bSThomas Graf }
585e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_set_sk_err);
58697676b6bSThomas Graf 
5871da177e4SLinus Torvalds int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
5881da177e4SLinus Torvalds {
5892d7202bfSThomas Graf 	struct nlattr *mx;
5902d7202bfSThomas Graf 	int i, valid = 0;
5911da177e4SLinus Torvalds 
5922d7202bfSThomas Graf 	mx = nla_nest_start(skb, RTA_METRICS);
5932d7202bfSThomas Graf 	if (mx == NULL)
5942d7202bfSThomas Graf 		return -ENOBUFS;
5952d7202bfSThomas Graf 
5961da177e4SLinus Torvalds 	for (i = 0; i < RTAX_MAX; i++) {
5972d7202bfSThomas Graf 		if (metrics[i]) {
5982d7202bfSThomas Graf 			valid++;
599a6574349SDavid S. Miller 			if (nla_put_u32(skb, i+1, metrics[i]))
600a6574349SDavid S. Miller 				goto nla_put_failure;
6011da177e4SLinus Torvalds 		}
6022d7202bfSThomas Graf 	}
6031da177e4SLinus Torvalds 
604a57d27fcSDavid S. Miller 	if (!valid) {
605a57d27fcSDavid S. Miller 		nla_nest_cancel(skb, mx);
606a57d27fcSDavid S. Miller 		return 0;
607a57d27fcSDavid S. Miller 	}
6082d7202bfSThomas Graf 
6092d7202bfSThomas Graf 	return nla_nest_end(skb, mx);
6102d7202bfSThomas Graf 
6112d7202bfSThomas Graf nla_put_failure:
612bc3ed28cSThomas Graf 	nla_nest_cancel(skb, mx);
613bc3ed28cSThomas Graf 	return -EMSGSIZE;
6141da177e4SLinus Torvalds }
615e0d087afSEric Dumazet EXPORT_SYMBOL(rtnetlink_put_metrics);
6161da177e4SLinus Torvalds 
617e3703b3dSThomas Graf int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
61887a50699SDavid S. Miller 		       long expires, u32 error)
619e3703b3dSThomas Graf {
620e3703b3dSThomas Graf 	struct rta_cacheinfo ci = {
621e3703b3dSThomas Graf 		.rta_lastuse = jiffies_to_clock_t(jiffies - dst->lastuse),
622e3703b3dSThomas Graf 		.rta_used = dst->__use,
623e3703b3dSThomas Graf 		.rta_clntref = atomic_read(&(dst->__refcnt)),
624e3703b3dSThomas Graf 		.rta_error = error,
625e3703b3dSThomas Graf 		.rta_id =  id,
626e3703b3dSThomas Graf 	};
627e3703b3dSThomas Graf 
628e3703b3dSThomas Graf 	if (expires)
629e3703b3dSThomas Graf 		ci.rta_expires = jiffies_to_clock_t(expires);
630e3703b3dSThomas Graf 
631e3703b3dSThomas Graf 	return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
632e3703b3dSThomas Graf }
633e3703b3dSThomas Graf EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
6341da177e4SLinus Torvalds 
63593b2d4a2SDavid S. Miller static void set_operstate(struct net_device *dev, unsigned char transition)
636b00055aaSStefan Rompf {
637b00055aaSStefan Rompf 	unsigned char operstate = dev->operstate;
638b00055aaSStefan Rompf 
639b00055aaSStefan Rompf 	switch (transition) {
640b00055aaSStefan Rompf 	case IF_OPER_UP:
641b00055aaSStefan Rompf 		if ((operstate == IF_OPER_DORMANT ||
642b00055aaSStefan Rompf 		     operstate == IF_OPER_UNKNOWN) &&
643b00055aaSStefan Rompf 		    !netif_dormant(dev))
644b00055aaSStefan Rompf 			operstate = IF_OPER_UP;
645b00055aaSStefan Rompf 		break;
646b00055aaSStefan Rompf 
647b00055aaSStefan Rompf 	case IF_OPER_DORMANT:
648b00055aaSStefan Rompf 		if (operstate == IF_OPER_UP ||
649b00055aaSStefan Rompf 		    operstate == IF_OPER_UNKNOWN)
650b00055aaSStefan Rompf 			operstate = IF_OPER_DORMANT;
651b00055aaSStefan Rompf 		break;
6523ff50b79SStephen Hemminger 	}
653b00055aaSStefan Rompf 
654b00055aaSStefan Rompf 	if (dev->operstate != operstate) {
655b00055aaSStefan Rompf 		write_lock_bh(&dev_base_lock);
656b00055aaSStefan Rompf 		dev->operstate = operstate;
657b00055aaSStefan Rompf 		write_unlock_bh(&dev_base_lock);
658b00055aaSStefan Rompf 		netdev_state_change(dev);
65993b2d4a2SDavid S. Miller 	}
660b00055aaSStefan Rompf }
661b00055aaSStefan Rompf 
6623729d502SPatrick McHardy static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
6633729d502SPatrick McHardy 					   const struct ifinfomsg *ifm)
6643729d502SPatrick McHardy {
6653729d502SPatrick McHardy 	unsigned int flags = ifm->ifi_flags;
6663729d502SPatrick McHardy 
6673729d502SPatrick McHardy 	/* bugwards compatibility: ifi_change == 0 is treated as ~0 */
6683729d502SPatrick McHardy 	if (ifm->ifi_change)
6693729d502SPatrick McHardy 		flags = (flags & ifm->ifi_change) |
6703729d502SPatrick McHardy 			(dev->flags & ~ifm->ifi_change);
6713729d502SPatrick McHardy 
6723729d502SPatrick McHardy 	return flags;
6733729d502SPatrick McHardy }
6743729d502SPatrick McHardy 
675b60c5115SThomas Graf static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
676be1f3c2cSBen Hutchings 				 const struct rtnl_link_stats64 *b)
6771da177e4SLinus Torvalds {
678b60c5115SThomas Graf 	a->rx_packets = b->rx_packets;
679b60c5115SThomas Graf 	a->tx_packets = b->tx_packets;
680b60c5115SThomas Graf 	a->rx_bytes = b->rx_bytes;
681b60c5115SThomas Graf 	a->tx_bytes = b->tx_bytes;
682b60c5115SThomas Graf 	a->rx_errors = b->rx_errors;
683b60c5115SThomas Graf 	a->tx_errors = b->tx_errors;
684b60c5115SThomas Graf 	a->rx_dropped = b->rx_dropped;
685b60c5115SThomas Graf 	a->tx_dropped = b->tx_dropped;
686b60c5115SThomas Graf 
687b60c5115SThomas Graf 	a->multicast = b->multicast;
688b60c5115SThomas Graf 	a->collisions = b->collisions;
689b60c5115SThomas Graf 
690b60c5115SThomas Graf 	a->rx_length_errors = b->rx_length_errors;
691b60c5115SThomas Graf 	a->rx_over_errors = b->rx_over_errors;
692b60c5115SThomas Graf 	a->rx_crc_errors = b->rx_crc_errors;
693b60c5115SThomas Graf 	a->rx_frame_errors = b->rx_frame_errors;
694b60c5115SThomas Graf 	a->rx_fifo_errors = b->rx_fifo_errors;
695b60c5115SThomas Graf 	a->rx_missed_errors = b->rx_missed_errors;
696b60c5115SThomas Graf 
697b60c5115SThomas Graf 	a->tx_aborted_errors = b->tx_aborted_errors;
698b60c5115SThomas Graf 	a->tx_carrier_errors = b->tx_carrier_errors;
699b60c5115SThomas Graf 	a->tx_fifo_errors = b->tx_fifo_errors;
700b60c5115SThomas Graf 	a->tx_heartbeat_errors = b->tx_heartbeat_errors;
701b60c5115SThomas Graf 	a->tx_window_errors = b->tx_window_errors;
702b60c5115SThomas Graf 
703b60c5115SThomas Graf 	a->rx_compressed = b->rx_compressed;
704b60c5115SThomas Graf 	a->tx_compressed = b->tx_compressed;
70510708f37SJan Engelhardt }
70610708f37SJan Engelhardt 
707be1f3c2cSBen Hutchings static void copy_rtnl_link_stats64(void *v, const struct rtnl_link_stats64 *b)
70810708f37SJan Engelhardt {
709afdcba37SEric Dumazet 	memcpy(v, b, sizeof(*b));
71010708f37SJan Engelhardt }
711b60c5115SThomas Graf 
712c02db8c6SChris Wright /* All VF info */
713115c9b81SGreg Rose static inline int rtnl_vfinfo_size(const struct net_device *dev,
714115c9b81SGreg Rose 				   u32 ext_filter_mask)
715ebc08a6fSWilliams, Mitch A {
716115c9b81SGreg Rose 	if (dev->dev.parent && dev_is_pci(dev->dev.parent) &&
717115c9b81SGreg Rose 	    (ext_filter_mask & RTEXT_FILTER_VF)) {
718c02db8c6SChris Wright 		int num_vfs = dev_num_vf(dev->dev.parent);
719045de01aSScott Feldman 		size_t size = nla_total_size(sizeof(struct nlattr));
720045de01aSScott Feldman 		size += nla_total_size(num_vfs * sizeof(struct nlattr));
721045de01aSScott Feldman 		size += num_vfs *
722045de01aSScott Feldman 			(nla_total_size(sizeof(struct ifla_vf_mac)) +
723045de01aSScott Feldman 			 nla_total_size(sizeof(struct ifla_vf_vlan)) +
7245f8444a3SGreg Rose 			 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
7255f8444a3SGreg Rose 			 nla_total_size(sizeof(struct ifla_vf_spoofchk)));
726c02db8c6SChris Wright 		return size;
727c02db8c6SChris Wright 	} else
728ebc08a6fSWilliams, Mitch A 		return 0;
729ebc08a6fSWilliams, Mitch A }
730ebc08a6fSWilliams, Mitch A 
73157b61080SScott Feldman static size_t rtnl_port_size(const struct net_device *dev)
73257b61080SScott Feldman {
73357b61080SScott Feldman 	size_t port_size = nla_total_size(4)		/* PORT_VF */
73457b61080SScott Feldman 		+ nla_total_size(PORT_PROFILE_MAX)	/* PORT_PROFILE */
73557b61080SScott Feldman 		+ nla_total_size(sizeof(struct ifla_port_vsi))
73657b61080SScott Feldman 							/* PORT_VSI_TYPE */
73757b61080SScott Feldman 		+ nla_total_size(PORT_UUID_MAX)		/* PORT_INSTANCE_UUID */
73857b61080SScott Feldman 		+ nla_total_size(PORT_UUID_MAX)		/* PORT_HOST_UUID */
73957b61080SScott Feldman 		+ nla_total_size(1)			/* PROT_VDP_REQUEST */
74057b61080SScott Feldman 		+ nla_total_size(2);			/* PORT_VDP_RESPONSE */
74157b61080SScott Feldman 	size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
74257b61080SScott Feldman 	size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
74357b61080SScott Feldman 		+ port_size;
74457b61080SScott Feldman 	size_t port_self_size = nla_total_size(sizeof(struct nlattr))
74557b61080SScott Feldman 		+ port_size;
74657b61080SScott Feldman 
74757b61080SScott Feldman 	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent)
74857b61080SScott Feldman 		return 0;
74957b61080SScott Feldman 	if (dev_num_vf(dev->dev.parent))
75057b61080SScott Feldman 		return port_self_size + vf_ports_size +
75157b61080SScott Feldman 			vf_port_size * dev_num_vf(dev->dev.parent);
75257b61080SScott Feldman 	else
75357b61080SScott Feldman 		return port_self_size;
75457b61080SScott Feldman }
75557b61080SScott Feldman 
756115c9b81SGreg Rose static noinline size_t if_nlmsg_size(const struct net_device *dev,
757115c9b81SGreg Rose 				     u32 ext_filter_mask)
758339bf98fSThomas Graf {
759339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
760339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
7610b815a1aSStephen Hemminger 	       + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
762339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
763339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rtnl_link_ifmap))
764339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rtnl_link_stats))
765adcfe196SJan Engelhardt 	       + nla_total_size(sizeof(struct rtnl_link_stats64))
766339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
767339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
768339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_TXQLEN */
769339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_WEIGHT */
770339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MTU */
771339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_LINK */
772339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MASTER */
773edbc0bb3SBen Greear 	       + nla_total_size(4) /* IFLA_PROMISCUITY */
774339bf98fSThomas Graf 	       + nla_total_size(1) /* IFLA_OPERSTATE */
77538f7b870SPatrick McHardy 	       + nla_total_size(1) /* IFLA_LINKMODE */
776115c9b81SGreg Rose 	       + nla_total_size(ext_filter_mask
777115c9b81SGreg Rose 			        & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
778115c9b81SGreg Rose 	       + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
77957b61080SScott Feldman 	       + rtnl_port_size(dev) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
780f8ff182cSThomas Graf 	       + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
781f8ff182cSThomas Graf 	       + rtnl_link_get_af_size(dev); /* IFLA_AF_SPEC */
782339bf98fSThomas Graf }
783339bf98fSThomas Graf 
78457b61080SScott Feldman static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
78557b61080SScott Feldman {
78657b61080SScott Feldman 	struct nlattr *vf_ports;
78757b61080SScott Feldman 	struct nlattr *vf_port;
78857b61080SScott Feldman 	int vf;
78957b61080SScott Feldman 	int err;
79057b61080SScott Feldman 
79157b61080SScott Feldman 	vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
79257b61080SScott Feldman 	if (!vf_ports)
79357b61080SScott Feldman 		return -EMSGSIZE;
79457b61080SScott Feldman 
79557b61080SScott Feldman 	for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
79657b61080SScott Feldman 		vf_port = nla_nest_start(skb, IFLA_VF_PORT);
7978ca94183SScott Feldman 		if (!vf_port)
7988ca94183SScott Feldman 			goto nla_put_failure;
799a6574349SDavid S. Miller 		if (nla_put_u32(skb, IFLA_PORT_VF, vf))
800a6574349SDavid S. Miller 			goto nla_put_failure;
80157b61080SScott Feldman 		err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
8028ca94183SScott Feldman 		if (err == -EMSGSIZE)
8038ca94183SScott Feldman 			goto nla_put_failure;
80457b61080SScott Feldman 		if (err) {
80557b61080SScott Feldman 			nla_nest_cancel(skb, vf_port);
80657b61080SScott Feldman 			continue;
80757b61080SScott Feldman 		}
80857b61080SScott Feldman 		nla_nest_end(skb, vf_port);
80957b61080SScott Feldman 	}
81057b61080SScott Feldman 
81157b61080SScott Feldman 	nla_nest_end(skb, vf_ports);
81257b61080SScott Feldman 
81357b61080SScott Feldman 	return 0;
8148ca94183SScott Feldman 
8158ca94183SScott Feldman nla_put_failure:
8168ca94183SScott Feldman 	nla_nest_cancel(skb, vf_ports);
8178ca94183SScott Feldman 	return -EMSGSIZE;
81857b61080SScott Feldman }
81957b61080SScott Feldman 
82057b61080SScott Feldman static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
82157b61080SScott Feldman {
82257b61080SScott Feldman 	struct nlattr *port_self;
82357b61080SScott Feldman 	int err;
82457b61080SScott Feldman 
82557b61080SScott Feldman 	port_self = nla_nest_start(skb, IFLA_PORT_SELF);
82657b61080SScott Feldman 	if (!port_self)
82757b61080SScott Feldman 		return -EMSGSIZE;
82857b61080SScott Feldman 
82957b61080SScott Feldman 	err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
83057b61080SScott Feldman 	if (err) {
83157b61080SScott Feldman 		nla_nest_cancel(skb, port_self);
8328ca94183SScott Feldman 		return (err == -EMSGSIZE) ? err : 0;
83357b61080SScott Feldman 	}
83457b61080SScott Feldman 
83557b61080SScott Feldman 	nla_nest_end(skb, port_self);
83657b61080SScott Feldman 
83757b61080SScott Feldman 	return 0;
83857b61080SScott Feldman }
83957b61080SScott Feldman 
84057b61080SScott Feldman static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev)
84157b61080SScott Feldman {
84257b61080SScott Feldman 	int err;
84357b61080SScott Feldman 
84457b61080SScott Feldman 	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent)
84557b61080SScott Feldman 		return 0;
84657b61080SScott Feldman 
84757b61080SScott Feldman 	err = rtnl_port_self_fill(skb, dev);
84857b61080SScott Feldman 	if (err)
84957b61080SScott Feldman 		return err;
85057b61080SScott Feldman 
85157b61080SScott Feldman 	if (dev_num_vf(dev->dev.parent)) {
85257b61080SScott Feldman 		err = rtnl_vf_ports_fill(skb, dev);
85357b61080SScott Feldman 		if (err)
85457b61080SScott Feldman 			return err;
85557b61080SScott Feldman 	}
85657b61080SScott Feldman 
85757b61080SScott Feldman 	return 0;
85857b61080SScott Feldman }
85957b61080SScott Feldman 
860b60c5115SThomas Graf static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
861575c3e2aSPatrick McHardy 			    int type, u32 pid, u32 seq, u32 change,
862115c9b81SGreg Rose 			    unsigned int flags, u32 ext_filter_mask)
863b60c5115SThomas Graf {
864b60c5115SThomas Graf 	struct ifinfomsg *ifm;
8651da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
86628172739SEric Dumazet 	struct rtnl_link_stats64 temp;
867be1f3c2cSBen Hutchings 	const struct rtnl_link_stats64 *stats;
868f8ff182cSThomas Graf 	struct nlattr *attr, *af_spec;
869f8ff182cSThomas Graf 	struct rtnl_af_ops *af_ops;
8701da177e4SLinus Torvalds 
8712907c35fSEric Dumazet 	ASSERT_RTNL();
872b60c5115SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
873b60c5115SThomas Graf 	if (nlh == NULL)
87426932566SPatrick McHardy 		return -EMSGSIZE;
8751da177e4SLinus Torvalds 
876b60c5115SThomas Graf 	ifm = nlmsg_data(nlh);
877b60c5115SThomas Graf 	ifm->ifi_family = AF_UNSPEC;
878b60c5115SThomas Graf 	ifm->__ifi_pad = 0;
879b60c5115SThomas Graf 	ifm->ifi_type = dev->type;
880b60c5115SThomas Graf 	ifm->ifi_index = dev->ifindex;
881b60c5115SThomas Graf 	ifm->ifi_flags = dev_get_flags(dev);
882b60c5115SThomas Graf 	ifm->ifi_change = change;
8831da177e4SLinus Torvalds 
884a6574349SDavid S. Miller 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
885a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
886a6574349SDavid S. Miller 	    nla_put_u8(skb, IFLA_OPERSTATE,
887a6574349SDavid S. Miller 		       netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
888a6574349SDavid S. Miller 	    nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
889a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
890a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_GROUP, dev->group) ||
891edbc0bb3SBen Greear 	    nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
892a6574349SDavid S. Miller 	    (dev->ifindex != dev->iflink &&
893a6574349SDavid S. Miller 	     nla_put_u32(skb, IFLA_LINK, dev->iflink)) ||
894a6574349SDavid S. Miller 	    (dev->master &&
895a6574349SDavid S. Miller 	     nla_put_u32(skb, IFLA_MASTER, dev->master->ifindex)) ||
896a6574349SDavid S. Miller 	    (dev->qdisc &&
897a6574349SDavid S. Miller 	     nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
898a6574349SDavid S. Miller 	    (dev->ifalias &&
899a6574349SDavid S. Miller 	     nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)))
900a6574349SDavid S. Miller 		goto nla_put_failure;
9010b815a1aSStephen Hemminger 
902b00055aaSStefan Rompf 	if (1) {
9031da177e4SLinus Torvalds 		struct rtnl_link_ifmap map = {
9041da177e4SLinus Torvalds 			.mem_start   = dev->mem_start,
9051da177e4SLinus Torvalds 			.mem_end     = dev->mem_end,
9061da177e4SLinus Torvalds 			.base_addr   = dev->base_addr,
9071da177e4SLinus Torvalds 			.irq         = dev->irq,
9081da177e4SLinus Torvalds 			.dma         = dev->dma,
9091da177e4SLinus Torvalds 			.port        = dev->if_port,
9101da177e4SLinus Torvalds 		};
911a6574349SDavid S. Miller 		if (nla_put(skb, IFLA_MAP, sizeof(map), &map))
912a6574349SDavid S. Miller 			goto nla_put_failure;
9131da177e4SLinus Torvalds 	}
9141da177e4SLinus Torvalds 
9151da177e4SLinus Torvalds 	if (dev->addr_len) {
916a6574349SDavid S. Miller 		if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
917a6574349SDavid S. Miller 		    nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
918a6574349SDavid S. Miller 			goto nla_put_failure;
9191da177e4SLinus Torvalds 	}
9201da177e4SLinus Torvalds 
921b60c5115SThomas Graf 	attr = nla_reserve(skb, IFLA_STATS,
922b60c5115SThomas Graf 			sizeof(struct rtnl_link_stats));
923b60c5115SThomas Graf 	if (attr == NULL)
924b60c5115SThomas Graf 		goto nla_put_failure;
925b60c5115SThomas Graf 
92628172739SEric Dumazet 	stats = dev_get_stats(dev, &temp);
927b60c5115SThomas Graf 	copy_rtnl_link_stats(nla_data(attr), stats);
9281da177e4SLinus Torvalds 
92910708f37SJan Engelhardt 	attr = nla_reserve(skb, IFLA_STATS64,
93010708f37SJan Engelhardt 			sizeof(struct rtnl_link_stats64));
93110708f37SJan Engelhardt 	if (attr == NULL)
93210708f37SJan Engelhardt 		goto nla_put_failure;
93310708f37SJan Engelhardt 	copy_rtnl_link_stats64(nla_data(attr), stats);
93410708f37SJan Engelhardt 
935a6574349SDavid S. Miller 	if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) &&
936a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)))
937a6574349SDavid S. Miller 		goto nla_put_failure;
93857b61080SScott Feldman 
939115c9b81SGreg Rose 	if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent
940115c9b81SGreg Rose 	    && (ext_filter_mask & RTEXT_FILTER_VF)) {
941ebc08a6fSWilliams, Mitch A 		int i;
942ebc08a6fSWilliams, Mitch A 
943c02db8c6SChris Wright 		struct nlattr *vfinfo, *vf;
944c02db8c6SChris Wright 		int num_vfs = dev_num_vf(dev->dev.parent);
945c02db8c6SChris Wright 
946c02db8c6SChris Wright 		vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
947c02db8c6SChris Wright 		if (!vfinfo)
948c02db8c6SChris Wright 			goto nla_put_failure;
949c02db8c6SChris Wright 		for (i = 0; i < num_vfs; i++) {
950c02db8c6SChris Wright 			struct ifla_vf_info ivi;
951c02db8c6SChris Wright 			struct ifla_vf_mac vf_mac;
952c02db8c6SChris Wright 			struct ifla_vf_vlan vf_vlan;
953c02db8c6SChris Wright 			struct ifla_vf_tx_rate vf_tx_rate;
9545f8444a3SGreg Rose 			struct ifla_vf_spoofchk vf_spoofchk;
9555f8444a3SGreg Rose 
9565f8444a3SGreg Rose 			/*
9575f8444a3SGreg Rose 			 * Not all SR-IOV capable drivers support the
9585f8444a3SGreg Rose 			 * spoofcheck query.  Preset to -1 so the user
9595f8444a3SGreg Rose 			 * space tool can detect that the driver didn't
9605f8444a3SGreg Rose 			 * report anything.
9615f8444a3SGreg Rose 			 */
9625f8444a3SGreg Rose 			ivi.spoofchk = -1;
963ebc08a6fSWilliams, Mitch A 			if (dev->netdev_ops->ndo_get_vf_config(dev, i, &ivi))
964ebc08a6fSWilliams, Mitch A 				break;
9655f8444a3SGreg Rose 			vf_mac.vf =
9665f8444a3SGreg Rose 				vf_vlan.vf =
9675f8444a3SGreg Rose 				vf_tx_rate.vf =
9685f8444a3SGreg Rose 				vf_spoofchk.vf = ivi.vf;
9695f8444a3SGreg Rose 
970c02db8c6SChris Wright 			memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
971c02db8c6SChris Wright 			vf_vlan.vlan = ivi.vlan;
972c02db8c6SChris Wright 			vf_vlan.qos = ivi.qos;
973c02db8c6SChris Wright 			vf_tx_rate.rate = ivi.tx_rate;
9745f8444a3SGreg Rose 			vf_spoofchk.setting = ivi.spoofchk;
975c02db8c6SChris Wright 			vf = nla_nest_start(skb, IFLA_VF_INFO);
976c02db8c6SChris Wright 			if (!vf) {
977c02db8c6SChris Wright 				nla_nest_cancel(skb, vfinfo);
978c02db8c6SChris Wright 				goto nla_put_failure;
979ebc08a6fSWilliams, Mitch A 			}
980a6574349SDavid S. Miller 			if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
981a6574349SDavid S. Miller 			    nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
982a6574349SDavid S. Miller 			    nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
983a6574349SDavid S. Miller 				    &vf_tx_rate) ||
984a6574349SDavid S. Miller 			    nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
985a6574349SDavid S. Miller 				    &vf_spoofchk))
986a6574349SDavid S. Miller 				goto nla_put_failure;
987c02db8c6SChris Wright 			nla_nest_end(skb, vf);
988c02db8c6SChris Wright 		}
989c02db8c6SChris Wright 		nla_nest_end(skb, vfinfo);
990ebc08a6fSWilliams, Mitch A 	}
99157b61080SScott Feldman 
99257b61080SScott Feldman 	if (rtnl_port_fill(skb, dev))
99357b61080SScott Feldman 		goto nla_put_failure;
99457b61080SScott Feldman 
99538f7b870SPatrick McHardy 	if (dev->rtnl_link_ops) {
99638f7b870SPatrick McHardy 		if (rtnl_link_fill(skb, dev) < 0)
99738f7b870SPatrick McHardy 			goto nla_put_failure;
99838f7b870SPatrick McHardy 	}
99938f7b870SPatrick McHardy 
1000f8ff182cSThomas Graf 	if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
1001f8ff182cSThomas Graf 		goto nla_put_failure;
1002f8ff182cSThomas Graf 
1003f8ff182cSThomas Graf 	list_for_each_entry(af_ops, &rtnl_af_ops, list) {
1004f8ff182cSThomas Graf 		if (af_ops->fill_link_af) {
1005f8ff182cSThomas Graf 			struct nlattr *af;
1006f8ff182cSThomas Graf 			int err;
1007f8ff182cSThomas Graf 
1008f8ff182cSThomas Graf 			if (!(af = nla_nest_start(skb, af_ops->family)))
1009f8ff182cSThomas Graf 				goto nla_put_failure;
1010f8ff182cSThomas Graf 
1011f8ff182cSThomas Graf 			err = af_ops->fill_link_af(skb, dev);
1012f8ff182cSThomas Graf 
1013f8ff182cSThomas Graf 			/*
1014f8ff182cSThomas Graf 			 * Caller may return ENODATA to indicate that there
1015f8ff182cSThomas Graf 			 * was no data to be dumped. This is not an error, it
1016f8ff182cSThomas Graf 			 * means we should trim the attribute header and
1017f8ff182cSThomas Graf 			 * continue.
1018f8ff182cSThomas Graf 			 */
1019f8ff182cSThomas Graf 			if (err == -ENODATA)
1020f8ff182cSThomas Graf 				nla_nest_cancel(skb, af);
1021f8ff182cSThomas Graf 			else if (err < 0)
1022f8ff182cSThomas Graf 				goto nla_put_failure;
1023f8ff182cSThomas Graf 
1024f8ff182cSThomas Graf 			nla_nest_end(skb, af);
1025f8ff182cSThomas Graf 		}
1026f8ff182cSThomas Graf 	}
1027f8ff182cSThomas Graf 
1028f8ff182cSThomas Graf 	nla_nest_end(skb, af_spec);
1029f8ff182cSThomas Graf 
1030b60c5115SThomas Graf 	return nlmsg_end(skb, nlh);
1031b60c5115SThomas Graf 
1032b60c5115SThomas Graf nla_put_failure:
103326932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
103426932566SPatrick McHardy 	return -EMSGSIZE;
10351da177e4SLinus Torvalds }
10361da177e4SLinus Torvalds 
1037b60c5115SThomas Graf static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
10381da177e4SLinus Torvalds {
10393b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
10407c28bd0bSEric Dumazet 	int h, s_h;
10417c28bd0bSEric Dumazet 	int idx = 0, s_idx;
10421da177e4SLinus Torvalds 	struct net_device *dev;
10437c28bd0bSEric Dumazet 	struct hlist_head *head;
10447c28bd0bSEric Dumazet 	struct hlist_node *node;
1045115c9b81SGreg Rose 	struct nlattr *tb[IFLA_MAX+1];
1046115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
10471da177e4SLinus Torvalds 
10487c28bd0bSEric Dumazet 	s_h = cb->args[0];
10497c28bd0bSEric Dumazet 	s_idx = cb->args[1];
10507c28bd0bSEric Dumazet 
1051e67f88ddSEric Dumazet 	rcu_read_lock();
10524e985adaSThomas Graf 	cb->seq = net->dev_base_seq;
10534e985adaSThomas Graf 
1054a4b64fbeSEric Dumazet 	if (nlmsg_parse(cb->nlh, sizeof(struct rtgenmsg), tb, IFLA_MAX,
1055a4b64fbeSEric Dumazet 			ifla_policy) >= 0) {
1056115c9b81SGreg Rose 
1057115c9b81SGreg Rose 		if (tb[IFLA_EXT_MASK])
1058115c9b81SGreg Rose 			ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1059a4b64fbeSEric Dumazet 	}
1060115c9b81SGreg Rose 
10617c28bd0bSEric Dumazet 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
10627562f876SPavel Emelianov 		idx = 0;
10637c28bd0bSEric Dumazet 		head = &net->dev_index_head[h];
1064e67f88ddSEric Dumazet 		hlist_for_each_entry_rcu(dev, node, head, index_hlist) {
10651da177e4SLinus Torvalds 			if (idx < s_idx)
10667562f876SPavel Emelianov 				goto cont;
1067575c3e2aSPatrick McHardy 			if (rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
1068b6544c0bSJamal Hadi Salim 					     NETLINK_CB(cb->skb).pid,
10697c28bd0bSEric Dumazet 					     cb->nlh->nlmsg_seq, 0,
1070115c9b81SGreg Rose 					     NLM_F_MULTI,
1071115c9b81SGreg Rose 					     ext_filter_mask) <= 0)
10727c28bd0bSEric Dumazet 				goto out;
10734e985adaSThomas Graf 
10744e985adaSThomas Graf 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
10757562f876SPavel Emelianov cont:
10767562f876SPavel Emelianov 			idx++;
10771da177e4SLinus Torvalds 		}
10787c28bd0bSEric Dumazet 	}
10797c28bd0bSEric Dumazet out:
1080e67f88ddSEric Dumazet 	rcu_read_unlock();
10817c28bd0bSEric Dumazet 	cb->args[1] = idx;
10827c28bd0bSEric Dumazet 	cb->args[0] = h;
10831da177e4SLinus Torvalds 
10841da177e4SLinus Torvalds 	return skb->len;
10851da177e4SLinus Torvalds }
10861da177e4SLinus Torvalds 
1087e7199288SPavel Emelianov const struct nla_policy ifla_policy[IFLA_MAX+1] = {
10885176f91eSThomas Graf 	[IFLA_IFNAME]		= { .type = NLA_STRING, .len = IFNAMSIZ-1 },
108938f7b870SPatrick McHardy 	[IFLA_ADDRESS]		= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
109038f7b870SPatrick McHardy 	[IFLA_BROADCAST]	= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
10915176f91eSThomas Graf 	[IFLA_MAP]		= { .len = sizeof(struct rtnl_link_ifmap) },
1092da5e0494SThomas Graf 	[IFLA_MTU]		= { .type = NLA_U32 },
109376e87306SThomas Graf 	[IFLA_LINK]		= { .type = NLA_U32 },
1094fbaec0eaSJiri Pirko 	[IFLA_MASTER]		= { .type = NLA_U32 },
1095da5e0494SThomas Graf 	[IFLA_TXQLEN]		= { .type = NLA_U32 },
1096da5e0494SThomas Graf 	[IFLA_WEIGHT]		= { .type = NLA_U32 },
1097da5e0494SThomas Graf 	[IFLA_OPERSTATE]	= { .type = NLA_U8 },
1098da5e0494SThomas Graf 	[IFLA_LINKMODE]		= { .type = NLA_U8 },
109976e87306SThomas Graf 	[IFLA_LINKINFO]		= { .type = NLA_NESTED },
1100d8a5ec67SEric W. Biederman 	[IFLA_NET_NS_PID]	= { .type = NLA_U32 },
1101f0630529SEric W. Biederman 	[IFLA_NET_NS_FD]	= { .type = NLA_U32 },
11020b815a1aSStephen Hemminger 	[IFLA_IFALIAS]	        = { .type = NLA_STRING, .len = IFALIASZ-1 },
1103c02db8c6SChris Wright 	[IFLA_VFINFO_LIST]	= {. type = NLA_NESTED },
110457b61080SScott Feldman 	[IFLA_VF_PORTS]		= { .type = NLA_NESTED },
110557b61080SScott Feldman 	[IFLA_PORT_SELF]	= { .type = NLA_NESTED },
1106f8ff182cSThomas Graf 	[IFLA_AF_SPEC]		= { .type = NLA_NESTED },
1107115c9b81SGreg Rose 	[IFLA_EXT_MASK]		= { .type = NLA_U32 },
1108edbc0bb3SBen Greear 	[IFLA_PROMISCUITY]	= { .type = NLA_U32 },
1109da5e0494SThomas Graf };
1110e0d087afSEric Dumazet EXPORT_SYMBOL(ifla_policy);
11111da177e4SLinus Torvalds 
111238f7b870SPatrick McHardy static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
111338f7b870SPatrick McHardy 	[IFLA_INFO_KIND]	= { .type = NLA_STRING },
111438f7b870SPatrick McHardy 	[IFLA_INFO_DATA]	= { .type = NLA_NESTED },
111538f7b870SPatrick McHardy };
111638f7b870SPatrick McHardy 
1117c02db8c6SChris Wright static const struct nla_policy ifla_vfinfo_policy[IFLA_VF_INFO_MAX+1] = {
1118c02db8c6SChris Wright 	[IFLA_VF_INFO]		= { .type = NLA_NESTED },
1119c02db8c6SChris Wright };
1120c02db8c6SChris Wright 
1121c02db8c6SChris Wright static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1122c02db8c6SChris Wright 	[IFLA_VF_MAC]		= { .type = NLA_BINARY,
1123c02db8c6SChris Wright 				    .len = sizeof(struct ifla_vf_mac) },
1124c02db8c6SChris Wright 	[IFLA_VF_VLAN]		= { .type = NLA_BINARY,
1125c02db8c6SChris Wright 				    .len = sizeof(struct ifla_vf_vlan) },
1126c02db8c6SChris Wright 	[IFLA_VF_TX_RATE]	= { .type = NLA_BINARY,
1127c02db8c6SChris Wright 				    .len = sizeof(struct ifla_vf_tx_rate) },
112848752f65SGreg Rose 	[IFLA_VF_SPOOFCHK]	= { .type = NLA_BINARY,
112948752f65SGreg Rose 				    .len = sizeof(struct ifla_vf_spoofchk) },
1130c02db8c6SChris Wright };
1131c02db8c6SChris Wright 
113257b61080SScott Feldman static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
113357b61080SScott Feldman 	[IFLA_PORT_VF]		= { .type = NLA_U32 },
113457b61080SScott Feldman 	[IFLA_PORT_PROFILE]	= { .type = NLA_STRING,
113557b61080SScott Feldman 				    .len = PORT_PROFILE_MAX },
113657b61080SScott Feldman 	[IFLA_PORT_VSI_TYPE]	= { .type = NLA_BINARY,
113757b61080SScott Feldman 				    .len = sizeof(struct ifla_port_vsi)},
113857b61080SScott Feldman 	[IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
113957b61080SScott Feldman 				      .len = PORT_UUID_MAX },
114057b61080SScott Feldman 	[IFLA_PORT_HOST_UUID]	= { .type = NLA_STRING,
114157b61080SScott Feldman 				    .len = PORT_UUID_MAX },
114257b61080SScott Feldman 	[IFLA_PORT_REQUEST]	= { .type = NLA_U8, },
114357b61080SScott Feldman 	[IFLA_PORT_RESPONSE]	= { .type = NLA_U16, },
114457b61080SScott Feldman };
114557b61080SScott Feldman 
114681adee47SEric W. Biederman struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
114781adee47SEric W. Biederman {
114881adee47SEric W. Biederman 	struct net *net;
114981adee47SEric W. Biederman 	/* Examine the link attributes and figure out which
115081adee47SEric W. Biederman 	 * network namespace we are talking about.
115181adee47SEric W. Biederman 	 */
115281adee47SEric W. Biederman 	if (tb[IFLA_NET_NS_PID])
115381adee47SEric W. Biederman 		net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
1154f0630529SEric W. Biederman 	else if (tb[IFLA_NET_NS_FD])
1155f0630529SEric W. Biederman 		net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
115681adee47SEric W. Biederman 	else
115781adee47SEric W. Biederman 		net = get_net(src_net);
115881adee47SEric W. Biederman 	return net;
115981adee47SEric W. Biederman }
116081adee47SEric W. Biederman EXPORT_SYMBOL(rtnl_link_get_net);
116181adee47SEric W. Biederman 
11621840bb13SThomas Graf static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
11631840bb13SThomas Graf {
11641840bb13SThomas Graf 	if (dev) {
11651840bb13SThomas Graf 		if (tb[IFLA_ADDRESS] &&
11661840bb13SThomas Graf 		    nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
11671840bb13SThomas Graf 			return -EINVAL;
11681840bb13SThomas Graf 
11691840bb13SThomas Graf 		if (tb[IFLA_BROADCAST] &&
11701840bb13SThomas Graf 		    nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
11711840bb13SThomas Graf 			return -EINVAL;
11721840bb13SThomas Graf 	}
11731840bb13SThomas Graf 
1174cf7afbfeSThomas Graf 	if (tb[IFLA_AF_SPEC]) {
1175cf7afbfeSThomas Graf 		struct nlattr *af;
1176cf7afbfeSThomas Graf 		int rem, err;
1177cf7afbfeSThomas Graf 
1178cf7afbfeSThomas Graf 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1179cf7afbfeSThomas Graf 			const struct rtnl_af_ops *af_ops;
1180cf7afbfeSThomas Graf 
1181cf7afbfeSThomas Graf 			if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1182cf7afbfeSThomas Graf 				return -EAFNOSUPPORT;
1183cf7afbfeSThomas Graf 
1184cf7afbfeSThomas Graf 			if (!af_ops->set_link_af)
1185cf7afbfeSThomas Graf 				return -EOPNOTSUPP;
1186cf7afbfeSThomas Graf 
1187cf7afbfeSThomas Graf 			if (af_ops->validate_link_af) {
11886d3a9a68SKurt Van Dijck 				err = af_ops->validate_link_af(dev, af);
1189cf7afbfeSThomas Graf 				if (err < 0)
1190cf7afbfeSThomas Graf 					return err;
1191cf7afbfeSThomas Graf 			}
1192cf7afbfeSThomas Graf 		}
1193cf7afbfeSThomas Graf 	}
1194cf7afbfeSThomas Graf 
11951840bb13SThomas Graf 	return 0;
11961840bb13SThomas Graf }
11971840bb13SThomas Graf 
1198c02db8c6SChris Wright static int do_setvfinfo(struct net_device *dev, struct nlattr *attr)
1199c02db8c6SChris Wright {
1200c02db8c6SChris Wright 	int rem, err = -EINVAL;
1201c02db8c6SChris Wright 	struct nlattr *vf;
1202c02db8c6SChris Wright 	const struct net_device_ops *ops = dev->netdev_ops;
1203c02db8c6SChris Wright 
1204c02db8c6SChris Wright 	nla_for_each_nested(vf, attr, rem) {
1205c02db8c6SChris Wright 		switch (nla_type(vf)) {
1206c02db8c6SChris Wright 		case IFLA_VF_MAC: {
1207c02db8c6SChris Wright 			struct ifla_vf_mac *ivm;
1208c02db8c6SChris Wright 			ivm = nla_data(vf);
1209c02db8c6SChris Wright 			err = -EOPNOTSUPP;
1210c02db8c6SChris Wright 			if (ops->ndo_set_vf_mac)
1211c02db8c6SChris Wright 				err = ops->ndo_set_vf_mac(dev, ivm->vf,
1212c02db8c6SChris Wright 							  ivm->mac);
1213c02db8c6SChris Wright 			break;
1214c02db8c6SChris Wright 		}
1215c02db8c6SChris Wright 		case IFLA_VF_VLAN: {
1216c02db8c6SChris Wright 			struct ifla_vf_vlan *ivv;
1217c02db8c6SChris Wright 			ivv = nla_data(vf);
1218c02db8c6SChris Wright 			err = -EOPNOTSUPP;
1219c02db8c6SChris Wright 			if (ops->ndo_set_vf_vlan)
1220c02db8c6SChris Wright 				err = ops->ndo_set_vf_vlan(dev, ivv->vf,
1221c02db8c6SChris Wright 							   ivv->vlan,
1222c02db8c6SChris Wright 							   ivv->qos);
1223c02db8c6SChris Wright 			break;
1224c02db8c6SChris Wright 		}
1225c02db8c6SChris Wright 		case IFLA_VF_TX_RATE: {
1226c02db8c6SChris Wright 			struct ifla_vf_tx_rate *ivt;
1227c02db8c6SChris Wright 			ivt = nla_data(vf);
1228c02db8c6SChris Wright 			err = -EOPNOTSUPP;
1229c02db8c6SChris Wright 			if (ops->ndo_set_vf_tx_rate)
1230c02db8c6SChris Wright 				err = ops->ndo_set_vf_tx_rate(dev, ivt->vf,
1231c02db8c6SChris Wright 							      ivt->rate);
1232c02db8c6SChris Wright 			break;
1233c02db8c6SChris Wright 		}
12345f8444a3SGreg Rose 		case IFLA_VF_SPOOFCHK: {
12355f8444a3SGreg Rose 			struct ifla_vf_spoofchk *ivs;
12365f8444a3SGreg Rose 			ivs = nla_data(vf);
12375f8444a3SGreg Rose 			err = -EOPNOTSUPP;
12385f8444a3SGreg Rose 			if (ops->ndo_set_vf_spoofchk)
12395f8444a3SGreg Rose 				err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
12405f8444a3SGreg Rose 							       ivs->setting);
12415f8444a3SGreg Rose 			break;
12425f8444a3SGreg Rose 		}
1243c02db8c6SChris Wright 		default:
1244c02db8c6SChris Wright 			err = -EINVAL;
1245c02db8c6SChris Wright 			break;
1246c02db8c6SChris Wright 		}
1247c02db8c6SChris Wright 		if (err)
1248c02db8c6SChris Wright 			break;
1249c02db8c6SChris Wright 	}
1250c02db8c6SChris Wright 	return err;
1251c02db8c6SChris Wright }
1252c02db8c6SChris Wright 
1253fbaec0eaSJiri Pirko static int do_set_master(struct net_device *dev, int ifindex)
1254fbaec0eaSJiri Pirko {
1255fbaec0eaSJiri Pirko 	struct net_device *master_dev;
1256fbaec0eaSJiri Pirko 	const struct net_device_ops *ops;
1257fbaec0eaSJiri Pirko 	int err;
1258fbaec0eaSJiri Pirko 
1259fbaec0eaSJiri Pirko 	if (dev->master) {
1260fbaec0eaSJiri Pirko 		if (dev->master->ifindex == ifindex)
1261fbaec0eaSJiri Pirko 			return 0;
1262fbaec0eaSJiri Pirko 		ops = dev->master->netdev_ops;
1263fbaec0eaSJiri Pirko 		if (ops->ndo_del_slave) {
1264fbaec0eaSJiri Pirko 			err = ops->ndo_del_slave(dev->master, dev);
1265fbaec0eaSJiri Pirko 			if (err)
1266fbaec0eaSJiri Pirko 				return err;
1267fbaec0eaSJiri Pirko 		} else {
1268fbaec0eaSJiri Pirko 			return -EOPNOTSUPP;
1269fbaec0eaSJiri Pirko 		}
1270fbaec0eaSJiri Pirko 	}
1271fbaec0eaSJiri Pirko 
1272fbaec0eaSJiri Pirko 	if (ifindex) {
1273fbaec0eaSJiri Pirko 		master_dev = __dev_get_by_index(dev_net(dev), ifindex);
1274fbaec0eaSJiri Pirko 		if (!master_dev)
1275fbaec0eaSJiri Pirko 			return -EINVAL;
1276fbaec0eaSJiri Pirko 		ops = master_dev->netdev_ops;
1277fbaec0eaSJiri Pirko 		if (ops->ndo_add_slave) {
1278fbaec0eaSJiri Pirko 			err = ops->ndo_add_slave(master_dev, dev);
1279fbaec0eaSJiri Pirko 			if (err)
1280fbaec0eaSJiri Pirko 				return err;
1281fbaec0eaSJiri Pirko 		} else {
1282fbaec0eaSJiri Pirko 			return -EOPNOTSUPP;
1283fbaec0eaSJiri Pirko 		}
1284fbaec0eaSJiri Pirko 	}
1285fbaec0eaSJiri Pirko 	return 0;
1286fbaec0eaSJiri Pirko }
1287fbaec0eaSJiri Pirko 
12880157f60cSPatrick McHardy static int do_setlink(struct net_device *dev, struct ifinfomsg *ifm,
128938f7b870SPatrick McHardy 		      struct nlattr **tb, char *ifname, int modified)
12900157f60cSPatrick McHardy {
1291d314774cSStephen Hemminger 	const struct net_device_ops *ops = dev->netdev_ops;
129238f7b870SPatrick McHardy 	int send_addr_notify = 0;
12930157f60cSPatrick McHardy 	int err;
12940157f60cSPatrick McHardy 
1295f0630529SEric W. Biederman 	if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
129681adee47SEric W. Biederman 		struct net *net = rtnl_link_get_net(dev_net(dev), tb);
1297d8a5ec67SEric W. Biederman 		if (IS_ERR(net)) {
1298d8a5ec67SEric W. Biederman 			err = PTR_ERR(net);
1299d8a5ec67SEric W. Biederman 			goto errout;
1300d8a5ec67SEric W. Biederman 		}
1301d8a5ec67SEric W. Biederman 		err = dev_change_net_namespace(dev, net, ifname);
1302d8a5ec67SEric W. Biederman 		put_net(net);
1303d8a5ec67SEric W. Biederman 		if (err)
1304d8a5ec67SEric W. Biederman 			goto errout;
1305d8a5ec67SEric W. Biederman 		modified = 1;
1306d8a5ec67SEric W. Biederman 	}
1307d8a5ec67SEric W. Biederman 
13080157f60cSPatrick McHardy 	if (tb[IFLA_MAP]) {
13090157f60cSPatrick McHardy 		struct rtnl_link_ifmap *u_map;
13100157f60cSPatrick McHardy 		struct ifmap k_map;
13110157f60cSPatrick McHardy 
1312d314774cSStephen Hemminger 		if (!ops->ndo_set_config) {
13130157f60cSPatrick McHardy 			err = -EOPNOTSUPP;
13140157f60cSPatrick McHardy 			goto errout;
13150157f60cSPatrick McHardy 		}
13160157f60cSPatrick McHardy 
13170157f60cSPatrick McHardy 		if (!netif_device_present(dev)) {
13180157f60cSPatrick McHardy 			err = -ENODEV;
13190157f60cSPatrick McHardy 			goto errout;
13200157f60cSPatrick McHardy 		}
13210157f60cSPatrick McHardy 
13220157f60cSPatrick McHardy 		u_map = nla_data(tb[IFLA_MAP]);
13230157f60cSPatrick McHardy 		k_map.mem_start = (unsigned long) u_map->mem_start;
13240157f60cSPatrick McHardy 		k_map.mem_end = (unsigned long) u_map->mem_end;
13250157f60cSPatrick McHardy 		k_map.base_addr = (unsigned short) u_map->base_addr;
13260157f60cSPatrick McHardy 		k_map.irq = (unsigned char) u_map->irq;
13270157f60cSPatrick McHardy 		k_map.dma = (unsigned char) u_map->dma;
13280157f60cSPatrick McHardy 		k_map.port = (unsigned char) u_map->port;
13290157f60cSPatrick McHardy 
1330d314774cSStephen Hemminger 		err = ops->ndo_set_config(dev, &k_map);
13310157f60cSPatrick McHardy 		if (err < 0)
13320157f60cSPatrick McHardy 			goto errout;
13330157f60cSPatrick McHardy 
13340157f60cSPatrick McHardy 		modified = 1;
13350157f60cSPatrick McHardy 	}
13360157f60cSPatrick McHardy 
13370157f60cSPatrick McHardy 	if (tb[IFLA_ADDRESS]) {
13380157f60cSPatrick McHardy 		struct sockaddr *sa;
13390157f60cSPatrick McHardy 		int len;
13400157f60cSPatrick McHardy 
1341d314774cSStephen Hemminger 		if (!ops->ndo_set_mac_address) {
13420157f60cSPatrick McHardy 			err = -EOPNOTSUPP;
13430157f60cSPatrick McHardy 			goto errout;
13440157f60cSPatrick McHardy 		}
13450157f60cSPatrick McHardy 
13460157f60cSPatrick McHardy 		if (!netif_device_present(dev)) {
13470157f60cSPatrick McHardy 			err = -ENODEV;
13480157f60cSPatrick McHardy 			goto errout;
13490157f60cSPatrick McHardy 		}
13500157f60cSPatrick McHardy 
13510157f60cSPatrick McHardy 		len = sizeof(sa_family_t) + dev->addr_len;
13520157f60cSPatrick McHardy 		sa = kmalloc(len, GFP_KERNEL);
13530157f60cSPatrick McHardy 		if (!sa) {
13540157f60cSPatrick McHardy 			err = -ENOMEM;
13550157f60cSPatrick McHardy 			goto errout;
13560157f60cSPatrick McHardy 		}
13570157f60cSPatrick McHardy 		sa->sa_family = dev->type;
13580157f60cSPatrick McHardy 		memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
13590157f60cSPatrick McHardy 		       dev->addr_len);
1360d314774cSStephen Hemminger 		err = ops->ndo_set_mac_address(dev, sa);
13610157f60cSPatrick McHardy 		kfree(sa);
13620157f60cSPatrick McHardy 		if (err)
13630157f60cSPatrick McHardy 			goto errout;
13640157f60cSPatrick McHardy 		send_addr_notify = 1;
13650157f60cSPatrick McHardy 		modified = 1;
13660157f60cSPatrick McHardy 	}
13670157f60cSPatrick McHardy 
13680157f60cSPatrick McHardy 	if (tb[IFLA_MTU]) {
13690157f60cSPatrick McHardy 		err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
13700157f60cSPatrick McHardy 		if (err < 0)
13710157f60cSPatrick McHardy 			goto errout;
13720157f60cSPatrick McHardy 		modified = 1;
13730157f60cSPatrick McHardy 	}
13740157f60cSPatrick McHardy 
1375cbda10faSVlad Dogaru 	if (tb[IFLA_GROUP]) {
1376cbda10faSVlad Dogaru 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
1377cbda10faSVlad Dogaru 		modified = 1;
1378cbda10faSVlad Dogaru 	}
1379cbda10faSVlad Dogaru 
13800157f60cSPatrick McHardy 	/*
13810157f60cSPatrick McHardy 	 * Interface selected by interface index but interface
13820157f60cSPatrick McHardy 	 * name provided implies that a name change has been
13830157f60cSPatrick McHardy 	 * requested.
13840157f60cSPatrick McHardy 	 */
13850157f60cSPatrick McHardy 	if (ifm->ifi_index > 0 && ifname[0]) {
13860157f60cSPatrick McHardy 		err = dev_change_name(dev, ifname);
13870157f60cSPatrick McHardy 		if (err < 0)
13880157f60cSPatrick McHardy 			goto errout;
13890157f60cSPatrick McHardy 		modified = 1;
13900157f60cSPatrick McHardy 	}
13910157f60cSPatrick McHardy 
13920b815a1aSStephen Hemminger 	if (tb[IFLA_IFALIAS]) {
13930b815a1aSStephen Hemminger 		err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
13940b815a1aSStephen Hemminger 				    nla_len(tb[IFLA_IFALIAS]));
13950b815a1aSStephen Hemminger 		if (err < 0)
13960b815a1aSStephen Hemminger 			goto errout;
13970b815a1aSStephen Hemminger 		modified = 1;
13980b815a1aSStephen Hemminger 	}
13990b815a1aSStephen Hemminger 
14000157f60cSPatrick McHardy 	if (tb[IFLA_BROADCAST]) {
14010157f60cSPatrick McHardy 		nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
14020157f60cSPatrick McHardy 		send_addr_notify = 1;
14030157f60cSPatrick McHardy 	}
14040157f60cSPatrick McHardy 
14050157f60cSPatrick McHardy 	if (ifm->ifi_flags || ifm->ifi_change) {
14063729d502SPatrick McHardy 		err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
14075f9021cfSJohannes Berg 		if (err < 0)
14085f9021cfSJohannes Berg 			goto errout;
14090157f60cSPatrick McHardy 	}
14100157f60cSPatrick McHardy 
1411fbaec0eaSJiri Pirko 	if (tb[IFLA_MASTER]) {
1412fbaec0eaSJiri Pirko 		err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
1413fbaec0eaSJiri Pirko 		if (err)
1414fbaec0eaSJiri Pirko 			goto errout;
1415fbaec0eaSJiri Pirko 		modified = 1;
1416fbaec0eaSJiri Pirko 	}
1417fbaec0eaSJiri Pirko 
141893b2d4a2SDavid S. Miller 	if (tb[IFLA_TXQLEN])
14190157f60cSPatrick McHardy 		dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
14200157f60cSPatrick McHardy 
14210157f60cSPatrick McHardy 	if (tb[IFLA_OPERSTATE])
142293b2d4a2SDavid S. Miller 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
14230157f60cSPatrick McHardy 
14240157f60cSPatrick McHardy 	if (tb[IFLA_LINKMODE]) {
14250157f60cSPatrick McHardy 		write_lock_bh(&dev_base_lock);
14260157f60cSPatrick McHardy 		dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
142793b2d4a2SDavid S. Miller 		write_unlock_bh(&dev_base_lock);
14280157f60cSPatrick McHardy 	}
14290157f60cSPatrick McHardy 
1430c02db8c6SChris Wright 	if (tb[IFLA_VFINFO_LIST]) {
1431c02db8c6SChris Wright 		struct nlattr *attr;
1432c02db8c6SChris Wright 		int rem;
1433c02db8c6SChris Wright 		nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
1434253683bbSDavid Howells 			if (nla_type(attr) != IFLA_VF_INFO) {
1435253683bbSDavid Howells 				err = -EINVAL;
1436c02db8c6SChris Wright 				goto errout;
1437253683bbSDavid Howells 			}
1438c02db8c6SChris Wright 			err = do_setvfinfo(dev, attr);
1439ebc08a6fSWilliams, Mitch A 			if (err < 0)
1440ebc08a6fSWilliams, Mitch A 				goto errout;
1441ebc08a6fSWilliams, Mitch A 			modified = 1;
1442ebc08a6fSWilliams, Mitch A 		}
1443ebc08a6fSWilliams, Mitch A 	}
14440157f60cSPatrick McHardy 	err = 0;
14450157f60cSPatrick McHardy 
144657b61080SScott Feldman 	if (tb[IFLA_VF_PORTS]) {
144757b61080SScott Feldman 		struct nlattr *port[IFLA_PORT_MAX+1];
144857b61080SScott Feldman 		struct nlattr *attr;
144957b61080SScott Feldman 		int vf;
145057b61080SScott Feldman 		int rem;
145157b61080SScott Feldman 
145257b61080SScott Feldman 		err = -EOPNOTSUPP;
145357b61080SScott Feldman 		if (!ops->ndo_set_vf_port)
145457b61080SScott Feldman 			goto errout;
145557b61080SScott Feldman 
145657b61080SScott Feldman 		nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
145757b61080SScott Feldman 			if (nla_type(attr) != IFLA_VF_PORT)
145857b61080SScott Feldman 				continue;
145957b61080SScott Feldman 			err = nla_parse_nested(port, IFLA_PORT_MAX,
146057b61080SScott Feldman 				attr, ifla_port_policy);
146157b61080SScott Feldman 			if (err < 0)
146257b61080SScott Feldman 				goto errout;
146357b61080SScott Feldman 			if (!port[IFLA_PORT_VF]) {
146457b61080SScott Feldman 				err = -EOPNOTSUPP;
146557b61080SScott Feldman 				goto errout;
146657b61080SScott Feldman 			}
146757b61080SScott Feldman 			vf = nla_get_u32(port[IFLA_PORT_VF]);
146857b61080SScott Feldman 			err = ops->ndo_set_vf_port(dev, vf, port);
146957b61080SScott Feldman 			if (err < 0)
147057b61080SScott Feldman 				goto errout;
147157b61080SScott Feldman 			modified = 1;
147257b61080SScott Feldman 		}
147357b61080SScott Feldman 	}
147457b61080SScott Feldman 	err = 0;
147557b61080SScott Feldman 
147657b61080SScott Feldman 	if (tb[IFLA_PORT_SELF]) {
147757b61080SScott Feldman 		struct nlattr *port[IFLA_PORT_MAX+1];
147857b61080SScott Feldman 
147957b61080SScott Feldman 		err = nla_parse_nested(port, IFLA_PORT_MAX,
148057b61080SScott Feldman 			tb[IFLA_PORT_SELF], ifla_port_policy);
148157b61080SScott Feldman 		if (err < 0)
148257b61080SScott Feldman 			goto errout;
148357b61080SScott Feldman 
148457b61080SScott Feldman 		err = -EOPNOTSUPP;
148557b61080SScott Feldman 		if (ops->ndo_set_vf_port)
148657b61080SScott Feldman 			err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
148757b61080SScott Feldman 		if (err < 0)
148857b61080SScott Feldman 			goto errout;
148957b61080SScott Feldman 		modified = 1;
149057b61080SScott Feldman 	}
1491f8ff182cSThomas Graf 
1492f8ff182cSThomas Graf 	if (tb[IFLA_AF_SPEC]) {
1493f8ff182cSThomas Graf 		struct nlattr *af;
1494f8ff182cSThomas Graf 		int rem;
1495f8ff182cSThomas Graf 
1496f8ff182cSThomas Graf 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1497f8ff182cSThomas Graf 			const struct rtnl_af_ops *af_ops;
1498f8ff182cSThomas Graf 
1499f8ff182cSThomas Graf 			if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1500cf7afbfeSThomas Graf 				BUG();
1501f8ff182cSThomas Graf 
1502cf7afbfeSThomas Graf 			err = af_ops->set_link_af(dev, af);
1503f8ff182cSThomas Graf 			if (err < 0)
1504f8ff182cSThomas Graf 				goto errout;
1505f8ff182cSThomas Graf 
1506f8ff182cSThomas Graf 			modified = 1;
1507f8ff182cSThomas Graf 		}
1508f8ff182cSThomas Graf 	}
150957b61080SScott Feldman 	err = 0;
151057b61080SScott Feldman 
15110157f60cSPatrick McHardy errout:
1512e87cc472SJoe Perches 	if (err < 0 && modified)
1513e87cc472SJoe 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",
1514e87cc472SJoe Perches 				     dev->name);
15150157f60cSPatrick McHardy 
15160157f60cSPatrick McHardy 	if (send_addr_notify)
15170157f60cSPatrick McHardy 		call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
1518f18da145SStefan Gula 
15190157f60cSPatrick McHardy 	return err;
15200157f60cSPatrick McHardy }
15210157f60cSPatrick McHardy 
1522da5e0494SThomas Graf static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1523da5e0494SThomas Graf {
15243b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
1525da5e0494SThomas Graf 	struct ifinfomsg *ifm;
1526da5e0494SThomas Graf 	struct net_device *dev;
15270157f60cSPatrick McHardy 	int err;
1528da5e0494SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
15291da177e4SLinus Torvalds 	char ifname[IFNAMSIZ];
15301da177e4SLinus Torvalds 
1531da5e0494SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1532da5e0494SThomas Graf 	if (err < 0)
1533da5e0494SThomas Graf 		goto errout;
15341da177e4SLinus Torvalds 
15355176f91eSThomas Graf 	if (tb[IFLA_IFNAME])
15365176f91eSThomas Graf 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
153778e5b891SPatrick McHardy 	else
153878e5b891SPatrick McHardy 		ifname[0] = '\0';
15391da177e4SLinus Torvalds 
15401da177e4SLinus Torvalds 	err = -EINVAL;
1541da5e0494SThomas Graf 	ifm = nlmsg_data(nlh);
154251055be8SPatrick McHardy 	if (ifm->ifi_index > 0)
1543a3d12891SEric Dumazet 		dev = __dev_get_by_index(net, ifm->ifi_index);
1544da5e0494SThomas Graf 	else if (tb[IFLA_IFNAME])
1545a3d12891SEric Dumazet 		dev = __dev_get_by_name(net, ifname);
1546da5e0494SThomas Graf 	else
1547da5e0494SThomas Graf 		goto errout;
15481da177e4SLinus Torvalds 
1549da5e0494SThomas Graf 	if (dev == NULL) {
1550da5e0494SThomas Graf 		err = -ENODEV;
1551da5e0494SThomas Graf 		goto errout;
1552da5e0494SThomas Graf 	}
15531da177e4SLinus Torvalds 
1554e0d087afSEric Dumazet 	err = validate_linkmsg(dev, tb);
1555e0d087afSEric Dumazet 	if (err < 0)
1556a3d12891SEric Dumazet 		goto errout;
1557da5e0494SThomas Graf 
155838f7b870SPatrick McHardy 	err = do_setlink(dev, ifm, tb, ifname, 0);
1559da5e0494SThomas Graf errout:
15601da177e4SLinus Torvalds 	return err;
15611da177e4SLinus Torvalds }
15621da177e4SLinus Torvalds 
156338f7b870SPatrick McHardy static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
156438f7b870SPatrick McHardy {
15653b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
156638f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops;
156738f7b870SPatrick McHardy 	struct net_device *dev;
156838f7b870SPatrick McHardy 	struct ifinfomsg *ifm;
156938f7b870SPatrick McHardy 	char ifname[IFNAMSIZ];
157038f7b870SPatrick McHardy 	struct nlattr *tb[IFLA_MAX+1];
157138f7b870SPatrick McHardy 	int err;
1572226bd341SEric Dumazet 	LIST_HEAD(list_kill);
157338f7b870SPatrick McHardy 
157438f7b870SPatrick McHardy 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
157538f7b870SPatrick McHardy 	if (err < 0)
157638f7b870SPatrick McHardy 		return err;
157738f7b870SPatrick McHardy 
157838f7b870SPatrick McHardy 	if (tb[IFLA_IFNAME])
157938f7b870SPatrick McHardy 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
158038f7b870SPatrick McHardy 
158138f7b870SPatrick McHardy 	ifm = nlmsg_data(nlh);
158238f7b870SPatrick McHardy 	if (ifm->ifi_index > 0)
1583881d966bSEric W. Biederman 		dev = __dev_get_by_index(net, ifm->ifi_index);
158438f7b870SPatrick McHardy 	else if (tb[IFLA_IFNAME])
1585881d966bSEric W. Biederman 		dev = __dev_get_by_name(net, ifname);
158638f7b870SPatrick McHardy 	else
158738f7b870SPatrick McHardy 		return -EINVAL;
158838f7b870SPatrick McHardy 
158938f7b870SPatrick McHardy 	if (!dev)
159038f7b870SPatrick McHardy 		return -ENODEV;
159138f7b870SPatrick McHardy 
159238f7b870SPatrick McHardy 	ops = dev->rtnl_link_ops;
159338f7b870SPatrick McHardy 	if (!ops)
159438f7b870SPatrick McHardy 		return -EOPNOTSUPP;
159538f7b870SPatrick McHardy 
1596226bd341SEric Dumazet 	ops->dellink(dev, &list_kill);
1597226bd341SEric Dumazet 	unregister_netdevice_many(&list_kill);
1598226bd341SEric Dumazet 	list_del(&list_kill);
159938f7b870SPatrick McHardy 	return 0;
160038f7b870SPatrick McHardy }
160138f7b870SPatrick McHardy 
16023729d502SPatrick McHardy int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
16033729d502SPatrick McHardy {
16043729d502SPatrick McHardy 	unsigned int old_flags;
16053729d502SPatrick McHardy 	int err;
16063729d502SPatrick McHardy 
16073729d502SPatrick McHardy 	old_flags = dev->flags;
16083729d502SPatrick McHardy 	if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
16093729d502SPatrick McHardy 		err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
16103729d502SPatrick McHardy 		if (err < 0)
16113729d502SPatrick McHardy 			return err;
16123729d502SPatrick McHardy 	}
16133729d502SPatrick McHardy 
16143729d502SPatrick McHardy 	dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
16153729d502SPatrick McHardy 	rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
16163729d502SPatrick McHardy 
16173729d502SPatrick McHardy 	__dev_notify_flags(dev, old_flags);
16183729d502SPatrick McHardy 	return 0;
16193729d502SPatrick McHardy }
16203729d502SPatrick McHardy EXPORT_SYMBOL(rtnl_configure_link);
16213729d502SPatrick McHardy 
162281adee47SEric W. Biederman struct net_device *rtnl_create_link(struct net *src_net, struct net *net,
162381adee47SEric W. Biederman 	char *ifname, const struct rtnl_link_ops *ops, struct nlattr *tb[])
1624e7199288SPavel Emelianov {
1625e7199288SPavel Emelianov 	int err;
1626e7199288SPavel Emelianov 	struct net_device *dev;
1627*d40156aaSJiri Pirko 	unsigned int num_tx_queues = 1;
1628*d40156aaSJiri Pirko 	unsigned int num_rx_queues = 1;
1629e7199288SPavel Emelianov 
1630*d40156aaSJiri Pirko 	if (ops->get_num_tx_queues)
1631*d40156aaSJiri Pirko 		num_tx_queues = ops->get_num_tx_queues();
1632*d40156aaSJiri Pirko 	if (ops->get_num_rx_queues)
1633*d40156aaSJiri Pirko 		num_rx_queues = ops->get_num_rx_queues();
1634efacb309Sstephen hemminger 
1635e7199288SPavel Emelianov 	err = -ENOMEM;
1636*d40156aaSJiri Pirko 	dev = alloc_netdev_mqs(ops->priv_size, ifname, ops->setup,
1637*d40156aaSJiri Pirko 			       num_tx_queues, num_rx_queues);
1638e7199288SPavel Emelianov 	if (!dev)
1639e7199288SPavel Emelianov 		goto err;
1640e7199288SPavel Emelianov 
164181adee47SEric W. Biederman 	dev_net_set(dev, net);
164281adee47SEric W. Biederman 	dev->rtnl_link_ops = ops;
16433729d502SPatrick McHardy 	dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
164481adee47SEric W. Biederman 
1645e7199288SPavel Emelianov 	if (tb[IFLA_MTU])
1646e7199288SPavel Emelianov 		dev->mtu = nla_get_u32(tb[IFLA_MTU]);
1647e7199288SPavel Emelianov 	if (tb[IFLA_ADDRESS])
1648e7199288SPavel Emelianov 		memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
1649e7199288SPavel Emelianov 				nla_len(tb[IFLA_ADDRESS]));
1650e7199288SPavel Emelianov 	if (tb[IFLA_BROADCAST])
1651e7199288SPavel Emelianov 		memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
1652e7199288SPavel Emelianov 				nla_len(tb[IFLA_BROADCAST]));
1653e7199288SPavel Emelianov 	if (tb[IFLA_TXQLEN])
1654e7199288SPavel Emelianov 		dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
1655e7199288SPavel Emelianov 	if (tb[IFLA_OPERSTATE])
165693b2d4a2SDavid S. Miller 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
1657e7199288SPavel Emelianov 	if (tb[IFLA_LINKMODE])
1658e7199288SPavel Emelianov 		dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
1659ffa934f1SPatrick McHardy 	if (tb[IFLA_GROUP])
1660ffa934f1SPatrick McHardy 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
1661e7199288SPavel Emelianov 
1662e7199288SPavel Emelianov 	return dev;
1663e7199288SPavel Emelianov 
1664e7199288SPavel Emelianov err:
1665e7199288SPavel Emelianov 	return ERR_PTR(err);
1666e7199288SPavel Emelianov }
1667e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_create_link);
1668e7199288SPavel Emelianov 
1669e7ed828fSVlad Dogaru static int rtnl_group_changelink(struct net *net, int group,
1670e7ed828fSVlad Dogaru 		struct ifinfomsg *ifm,
1671e7ed828fSVlad Dogaru 		struct nlattr **tb)
1672e7ed828fSVlad Dogaru {
1673e7ed828fSVlad Dogaru 	struct net_device *dev;
1674e7ed828fSVlad Dogaru 	int err;
1675e7ed828fSVlad Dogaru 
1676e7ed828fSVlad Dogaru 	for_each_netdev(net, dev) {
1677e7ed828fSVlad Dogaru 		if (dev->group == group) {
1678e7ed828fSVlad Dogaru 			err = do_setlink(dev, ifm, tb, NULL, 0);
1679e7ed828fSVlad Dogaru 			if (err < 0)
1680e7ed828fSVlad Dogaru 				return err;
1681e7ed828fSVlad Dogaru 		}
1682e7ed828fSVlad Dogaru 	}
1683e7ed828fSVlad Dogaru 
1684e7ed828fSVlad Dogaru 	return 0;
1685e7ed828fSVlad Dogaru }
1686e7ed828fSVlad Dogaru 
168738f7b870SPatrick McHardy static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
168838f7b870SPatrick McHardy {
16893b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
169038f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops;
169138f7b870SPatrick McHardy 	struct net_device *dev;
169238f7b870SPatrick McHardy 	struct ifinfomsg *ifm;
169338f7b870SPatrick McHardy 	char kind[MODULE_NAME_LEN];
169438f7b870SPatrick McHardy 	char ifname[IFNAMSIZ];
169538f7b870SPatrick McHardy 	struct nlattr *tb[IFLA_MAX+1];
169638f7b870SPatrick McHardy 	struct nlattr *linkinfo[IFLA_INFO_MAX+1];
169738f7b870SPatrick McHardy 	int err;
169838f7b870SPatrick McHardy 
169995a5afcaSJohannes Berg #ifdef CONFIG_MODULES
170038f7b870SPatrick McHardy replay:
17018072f085SThomas Graf #endif
170238f7b870SPatrick McHardy 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
170338f7b870SPatrick McHardy 	if (err < 0)
170438f7b870SPatrick McHardy 		return err;
170538f7b870SPatrick McHardy 
170638f7b870SPatrick McHardy 	if (tb[IFLA_IFNAME])
170738f7b870SPatrick McHardy 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
170838f7b870SPatrick McHardy 	else
170938f7b870SPatrick McHardy 		ifname[0] = '\0';
171038f7b870SPatrick McHardy 
171138f7b870SPatrick McHardy 	ifm = nlmsg_data(nlh);
171238f7b870SPatrick McHardy 	if (ifm->ifi_index > 0)
1713881d966bSEric W. Biederman 		dev = __dev_get_by_index(net, ifm->ifi_index);
1714e7ed828fSVlad Dogaru 	else {
1715e7ed828fSVlad Dogaru 		if (ifname[0])
1716881d966bSEric W. Biederman 			dev = __dev_get_by_name(net, ifname);
171738f7b870SPatrick McHardy 		else
171838f7b870SPatrick McHardy 			dev = NULL;
1719e7ed828fSVlad Dogaru 	}
172038f7b870SPatrick McHardy 
1721e0d087afSEric Dumazet 	err = validate_linkmsg(dev, tb);
1722e0d087afSEric Dumazet 	if (err < 0)
17231840bb13SThomas Graf 		return err;
17241840bb13SThomas Graf 
172538f7b870SPatrick McHardy 	if (tb[IFLA_LINKINFO]) {
172638f7b870SPatrick McHardy 		err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
172738f7b870SPatrick McHardy 				       tb[IFLA_LINKINFO], ifla_info_policy);
172838f7b870SPatrick McHardy 		if (err < 0)
172938f7b870SPatrick McHardy 			return err;
173038f7b870SPatrick McHardy 	} else
173138f7b870SPatrick McHardy 		memset(linkinfo, 0, sizeof(linkinfo));
173238f7b870SPatrick McHardy 
173338f7b870SPatrick McHardy 	if (linkinfo[IFLA_INFO_KIND]) {
173438f7b870SPatrick McHardy 		nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
173538f7b870SPatrick McHardy 		ops = rtnl_link_ops_get(kind);
173638f7b870SPatrick McHardy 	} else {
173738f7b870SPatrick McHardy 		kind[0] = '\0';
173838f7b870SPatrick McHardy 		ops = NULL;
173938f7b870SPatrick McHardy 	}
174038f7b870SPatrick McHardy 
174138f7b870SPatrick McHardy 	if (1) {
174238f7b870SPatrick McHardy 		struct nlattr *attr[ops ? ops->maxtype + 1 : 0], **data = NULL;
174381adee47SEric W. Biederman 		struct net *dest_net;
174438f7b870SPatrick McHardy 
174538f7b870SPatrick McHardy 		if (ops) {
174638f7b870SPatrick McHardy 			if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
174738f7b870SPatrick McHardy 				err = nla_parse_nested(attr, ops->maxtype,
174838f7b870SPatrick McHardy 						       linkinfo[IFLA_INFO_DATA],
174938f7b870SPatrick McHardy 						       ops->policy);
175038f7b870SPatrick McHardy 				if (err < 0)
175138f7b870SPatrick McHardy 					return err;
175238f7b870SPatrick McHardy 				data = attr;
175338f7b870SPatrick McHardy 			}
175438f7b870SPatrick McHardy 			if (ops->validate) {
175538f7b870SPatrick McHardy 				err = ops->validate(tb, data);
175638f7b870SPatrick McHardy 				if (err < 0)
175738f7b870SPatrick McHardy 					return err;
175838f7b870SPatrick McHardy 			}
175938f7b870SPatrick McHardy 		}
176038f7b870SPatrick McHardy 
176138f7b870SPatrick McHardy 		if (dev) {
176238f7b870SPatrick McHardy 			int modified = 0;
176338f7b870SPatrick McHardy 
176438f7b870SPatrick McHardy 			if (nlh->nlmsg_flags & NLM_F_EXCL)
176538f7b870SPatrick McHardy 				return -EEXIST;
176638f7b870SPatrick McHardy 			if (nlh->nlmsg_flags & NLM_F_REPLACE)
176738f7b870SPatrick McHardy 				return -EOPNOTSUPP;
176838f7b870SPatrick McHardy 
176938f7b870SPatrick McHardy 			if (linkinfo[IFLA_INFO_DATA]) {
177038f7b870SPatrick McHardy 				if (!ops || ops != dev->rtnl_link_ops ||
177138f7b870SPatrick McHardy 				    !ops->changelink)
177238f7b870SPatrick McHardy 					return -EOPNOTSUPP;
177338f7b870SPatrick McHardy 
177438f7b870SPatrick McHardy 				err = ops->changelink(dev, tb, data);
177538f7b870SPatrick McHardy 				if (err < 0)
177638f7b870SPatrick McHardy 					return err;
177738f7b870SPatrick McHardy 				modified = 1;
177838f7b870SPatrick McHardy 			}
177938f7b870SPatrick McHardy 
178038f7b870SPatrick McHardy 			return do_setlink(dev, ifm, tb, ifname, modified);
178138f7b870SPatrick McHardy 		}
178238f7b870SPatrick McHardy 
1783ffa934f1SPatrick McHardy 		if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
1784ffa934f1SPatrick McHardy 			if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
1785ffa934f1SPatrick McHardy 				return rtnl_group_changelink(net,
1786ffa934f1SPatrick McHardy 						nla_get_u32(tb[IFLA_GROUP]),
1787ffa934f1SPatrick McHardy 						ifm, tb);
178838f7b870SPatrick McHardy 			return -ENODEV;
1789ffa934f1SPatrick McHardy 		}
179038f7b870SPatrick McHardy 
17913729d502SPatrick McHardy 		if (ifm->ifi_index)
179238f7b870SPatrick McHardy 			return -EOPNOTSUPP;
17930e06877cSPatrick McHardy 		if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
179438f7b870SPatrick McHardy 			return -EOPNOTSUPP;
179538f7b870SPatrick McHardy 
179638f7b870SPatrick McHardy 		if (!ops) {
179795a5afcaSJohannes Berg #ifdef CONFIG_MODULES
179838f7b870SPatrick McHardy 			if (kind[0]) {
179938f7b870SPatrick McHardy 				__rtnl_unlock();
180038f7b870SPatrick McHardy 				request_module("rtnl-link-%s", kind);
180138f7b870SPatrick McHardy 				rtnl_lock();
180238f7b870SPatrick McHardy 				ops = rtnl_link_ops_get(kind);
180338f7b870SPatrick McHardy 				if (ops)
180438f7b870SPatrick McHardy 					goto replay;
180538f7b870SPatrick McHardy 			}
180638f7b870SPatrick McHardy #endif
180738f7b870SPatrick McHardy 			return -EOPNOTSUPP;
180838f7b870SPatrick McHardy 		}
180938f7b870SPatrick McHardy 
181038f7b870SPatrick McHardy 		if (!ifname[0])
181138f7b870SPatrick McHardy 			snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
181238f7b870SPatrick McHardy 
181381adee47SEric W. Biederman 		dest_net = rtnl_link_get_net(net, tb);
181413ad1774SEric W. Biederman 		if (IS_ERR(dest_net))
181513ad1774SEric W. Biederman 			return PTR_ERR(dest_net);
181613ad1774SEric W. Biederman 
181781adee47SEric W. Biederman 		dev = rtnl_create_link(net, dest_net, ifname, ops, tb);
181838f7b870SPatrick McHardy 
1819e7199288SPavel Emelianov 		if (IS_ERR(dev))
1820e7199288SPavel Emelianov 			err = PTR_ERR(dev);
1821e7199288SPavel Emelianov 		else if (ops->newlink)
182281adee47SEric W. Biederman 			err = ops->newlink(net, dev, tb, data);
18232d85cba2SPatrick McHardy 		else
18242d85cba2SPatrick McHardy 			err = register_netdevice(dev);
182580032cffSDan Carpenter 
182680032cffSDan Carpenter 		if (err < 0 && !IS_ERR(dev))
182738f7b870SPatrick McHardy 			free_netdev(dev);
182880032cffSDan Carpenter 		if (err < 0)
18293729d502SPatrick McHardy 			goto out;
183081adee47SEric W. Biederman 
18313729d502SPatrick McHardy 		err = rtnl_configure_link(dev, ifm);
18323729d502SPatrick McHardy 		if (err < 0)
18333729d502SPatrick McHardy 			unregister_netdevice(dev);
18343729d502SPatrick McHardy out:
183581adee47SEric W. Biederman 		put_net(dest_net);
183638f7b870SPatrick McHardy 		return err;
183738f7b870SPatrick McHardy 	}
183838f7b870SPatrick McHardy }
183938f7b870SPatrick McHardy 
1840b60c5115SThomas Graf static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
1841711e2c33SJean Tourrilhes {
18423b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
1843b60c5115SThomas Graf 	struct ifinfomsg *ifm;
1844a3d12891SEric Dumazet 	char ifname[IFNAMSIZ];
1845b60c5115SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
1846b60c5115SThomas Graf 	struct net_device *dev = NULL;
1847b60c5115SThomas Graf 	struct sk_buff *nskb;
1848339bf98fSThomas Graf 	int err;
1849115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
1850711e2c33SJean Tourrilhes 
1851b60c5115SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1852b60c5115SThomas Graf 	if (err < 0)
18539918f230SEric Sesterhenn 		return err;
1854b60c5115SThomas Graf 
1855a3d12891SEric Dumazet 	if (tb[IFLA_IFNAME])
1856a3d12891SEric Dumazet 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1857a3d12891SEric Dumazet 
1858115c9b81SGreg Rose 	if (tb[IFLA_EXT_MASK])
1859115c9b81SGreg Rose 		ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1860115c9b81SGreg Rose 
1861b60c5115SThomas Graf 	ifm = nlmsg_data(nlh);
1862a3d12891SEric Dumazet 	if (ifm->ifi_index > 0)
1863a3d12891SEric Dumazet 		dev = __dev_get_by_index(net, ifm->ifi_index);
1864a3d12891SEric Dumazet 	else if (tb[IFLA_IFNAME])
1865a3d12891SEric Dumazet 		dev = __dev_get_by_name(net, ifname);
1866a3d12891SEric Dumazet 	else
1867b60c5115SThomas Graf 		return -EINVAL;
1868b60c5115SThomas Graf 
1869a3d12891SEric Dumazet 	if (dev == NULL)
1870a3d12891SEric Dumazet 		return -ENODEV;
1871a3d12891SEric Dumazet 
1872115c9b81SGreg Rose 	nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
1873a3d12891SEric Dumazet 	if (nskb == NULL)
1874a3d12891SEric Dumazet 		return -ENOBUFS;
1875711e2c33SJean Tourrilhes 
1876575c3e2aSPatrick McHardy 	err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).pid,
1877115c9b81SGreg Rose 			       nlh->nlmsg_seq, 0, 0, ext_filter_mask);
187826932566SPatrick McHardy 	if (err < 0) {
187926932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in if_nlmsg_size */
188026932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
188126932566SPatrick McHardy 		kfree_skb(nskb);
1882a3d12891SEric Dumazet 	} else
188397c53cacSDenis V. Lunev 		err = rtnl_unicast(nskb, net, NETLINK_CB(skb).pid);
1884b60c5115SThomas Graf 
1885711e2c33SJean Tourrilhes 	return err;
1886711e2c33SJean Tourrilhes }
1887711e2c33SJean Tourrilhes 
1888115c9b81SGreg Rose static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
1889c7ac8679SGreg Rose {
1890115c9b81SGreg Rose 	struct net *net = sock_net(skb->sk);
1891115c9b81SGreg Rose 	struct net_device *dev;
1892115c9b81SGreg Rose 	struct nlattr *tb[IFLA_MAX+1];
1893115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
1894115c9b81SGreg Rose 	u16 min_ifinfo_dump_size = 0;
1895115c9b81SGreg Rose 
1896a4b64fbeSEric Dumazet 	if (nlmsg_parse(nlh, sizeof(struct rtgenmsg), tb, IFLA_MAX,
1897a4b64fbeSEric Dumazet 			ifla_policy) >= 0) {
1898115c9b81SGreg Rose 		if (tb[IFLA_EXT_MASK])
1899115c9b81SGreg Rose 			ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1900a4b64fbeSEric Dumazet 	}
1901115c9b81SGreg Rose 
1902115c9b81SGreg Rose 	if (!ext_filter_mask)
1903115c9b81SGreg Rose 		return NLMSG_GOODSIZE;
1904115c9b81SGreg Rose 	/*
1905115c9b81SGreg Rose 	 * traverse the list of net devices and compute the minimum
1906115c9b81SGreg Rose 	 * buffer size based upon the filter mask.
1907115c9b81SGreg Rose 	 */
1908115c9b81SGreg Rose 	list_for_each_entry(dev, &net->dev_base_head, dev_list) {
1909115c9b81SGreg Rose 		min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
1910115c9b81SGreg Rose 					     if_nlmsg_size(dev,
1911115c9b81SGreg Rose 						           ext_filter_mask));
1912115c9b81SGreg Rose 	}
1913115c9b81SGreg Rose 
1914c7ac8679SGreg Rose 	return min_ifinfo_dump_size;
1915c7ac8679SGreg Rose }
1916c7ac8679SGreg Rose 
191742bad1daSAdrian Bunk static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
19181da177e4SLinus Torvalds {
19191da177e4SLinus Torvalds 	int idx;
19201da177e4SLinus Torvalds 	int s_idx = cb->family;
19211da177e4SLinus Torvalds 
19221da177e4SLinus Torvalds 	if (s_idx == 0)
19231da177e4SLinus Torvalds 		s_idx = 1;
192425239ceeSPatrick McHardy 	for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
19251da177e4SLinus Torvalds 		int type = cb->nlh->nlmsg_type-RTM_BASE;
19261da177e4SLinus Torvalds 		if (idx < s_idx || idx == PF_PACKET)
19271da177e4SLinus Torvalds 			continue;
1928e2849863SThomas Graf 		if (rtnl_msg_handlers[idx] == NULL ||
1929e2849863SThomas Graf 		    rtnl_msg_handlers[idx][type].dumpit == NULL)
19301da177e4SLinus Torvalds 			continue;
19311da177e4SLinus Torvalds 		if (idx > s_idx)
19321da177e4SLinus Torvalds 			memset(&cb->args[0], 0, sizeof(cb->args));
1933e2849863SThomas Graf 		if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
19341da177e4SLinus Torvalds 			break;
19351da177e4SLinus Torvalds 	}
19361da177e4SLinus Torvalds 	cb->family = idx;
19371da177e4SLinus Torvalds 
19381da177e4SLinus Torvalds 	return skb->len;
19391da177e4SLinus Torvalds }
19401da177e4SLinus Torvalds 
194195c96174SEric Dumazet void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change)
19421da177e4SLinus Torvalds {
1943c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
19441da177e4SLinus Torvalds 	struct sk_buff *skb;
19450ec6d3f4SThomas Graf 	int err = -ENOBUFS;
1946c7ac8679SGreg Rose 	size_t if_info_size;
19471da177e4SLinus Torvalds 
1948115c9b81SGreg Rose 	skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), GFP_KERNEL);
19490ec6d3f4SThomas Graf 	if (skb == NULL)
19500ec6d3f4SThomas Graf 		goto errout;
19511da177e4SLinus Torvalds 
1952115c9b81SGreg Rose 	err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0);
195326932566SPatrick McHardy 	if (err < 0) {
195426932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in if_nlmsg_size() */
195526932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
195626932566SPatrick McHardy 		kfree_skb(skb);
195726932566SPatrick McHardy 		goto errout;
195826932566SPatrick McHardy 	}
19591ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
19601ce85fe4SPablo Neira Ayuso 	return;
19610ec6d3f4SThomas Graf errout:
19620ec6d3f4SThomas Graf 	if (err < 0)
19634b3da706SEric W. Biederman 		rtnl_set_sk_err(net, RTNLGRP_LINK, err);
19641da177e4SLinus Torvalds }
19651da177e4SLinus Torvalds 
1966d83b0603SJohn Fastabend static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
1967d83b0603SJohn Fastabend 				   struct net_device *dev,
1968d83b0603SJohn Fastabend 				   u8 *addr, u32 pid, u32 seq,
1969d83b0603SJohn Fastabend 				   int type, unsigned int flags)
1970d83b0603SJohn Fastabend {
1971d83b0603SJohn Fastabend 	struct nlmsghdr *nlh;
1972d83b0603SJohn Fastabend 	struct ndmsg *ndm;
1973d83b0603SJohn Fastabend 
1974d83b0603SJohn Fastabend 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), NLM_F_MULTI);
1975d83b0603SJohn Fastabend 	if (!nlh)
1976d83b0603SJohn Fastabend 		return -EMSGSIZE;
1977d83b0603SJohn Fastabend 
1978d83b0603SJohn Fastabend 	ndm = nlmsg_data(nlh);
1979d83b0603SJohn Fastabend 	ndm->ndm_family  = AF_BRIDGE;
1980d83b0603SJohn Fastabend 	ndm->ndm_pad1	 = 0;
1981d83b0603SJohn Fastabend 	ndm->ndm_pad2    = 0;
1982d83b0603SJohn Fastabend 	ndm->ndm_flags	 = flags;
1983d83b0603SJohn Fastabend 	ndm->ndm_type	 = 0;
1984d83b0603SJohn Fastabend 	ndm->ndm_ifindex = dev->ifindex;
1985d83b0603SJohn Fastabend 	ndm->ndm_state   = NUD_PERMANENT;
1986d83b0603SJohn Fastabend 
1987d83b0603SJohn Fastabend 	if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
1988d83b0603SJohn Fastabend 		goto nla_put_failure;
1989d83b0603SJohn Fastabend 
1990d83b0603SJohn Fastabend 	return nlmsg_end(skb, nlh);
1991d83b0603SJohn Fastabend 
1992d83b0603SJohn Fastabend nla_put_failure:
1993d83b0603SJohn Fastabend 	nlmsg_cancel(skb, nlh);
1994d83b0603SJohn Fastabend 	return -EMSGSIZE;
1995d83b0603SJohn Fastabend }
1996d83b0603SJohn Fastabend 
19973ff661c3SJohn Fastabend static inline size_t rtnl_fdb_nlmsg_size(void)
19983ff661c3SJohn Fastabend {
19993ff661c3SJohn Fastabend 	return NLMSG_ALIGN(sizeof(struct ndmsg)) + nla_total_size(ETH_ALEN);
20003ff661c3SJohn Fastabend }
20013ff661c3SJohn Fastabend 
20023ff661c3SJohn Fastabend static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, int type)
20033ff661c3SJohn Fastabend {
20043ff661c3SJohn Fastabend 	struct net *net = dev_net(dev);
20053ff661c3SJohn Fastabend 	struct sk_buff *skb;
20063ff661c3SJohn Fastabend 	int err = -ENOBUFS;
20073ff661c3SJohn Fastabend 
20083ff661c3SJohn Fastabend 	skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
20093ff661c3SJohn Fastabend 	if (!skb)
20103ff661c3SJohn Fastabend 		goto errout;
20113ff661c3SJohn Fastabend 
20123ff661c3SJohn Fastabend 	err = nlmsg_populate_fdb_fill(skb, dev, addr, 0, 0, type, NTF_SELF);
20133ff661c3SJohn Fastabend 	if (err < 0) {
20143ff661c3SJohn Fastabend 		kfree_skb(skb);
20153ff661c3SJohn Fastabend 		goto errout;
20163ff661c3SJohn Fastabend 	}
20173ff661c3SJohn Fastabend 
20183ff661c3SJohn Fastabend 	rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
20193ff661c3SJohn Fastabend 	return;
20203ff661c3SJohn Fastabend errout:
20213ff661c3SJohn Fastabend 	rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
20223ff661c3SJohn Fastabend }
20233ff661c3SJohn Fastabend 
202477162022SJohn Fastabend static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
202577162022SJohn Fastabend {
202677162022SJohn Fastabend 	struct net *net = sock_net(skb->sk);
202777162022SJohn Fastabend 	struct net_device *master = NULL;
202877162022SJohn Fastabend 	struct ndmsg *ndm;
202977162022SJohn Fastabend 	struct nlattr *tb[NDA_MAX+1];
203077162022SJohn Fastabend 	struct net_device *dev;
203177162022SJohn Fastabend 	u8 *addr;
203277162022SJohn Fastabend 	int err;
203377162022SJohn Fastabend 
203477162022SJohn Fastabend 	err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
203577162022SJohn Fastabend 	if (err < 0)
203677162022SJohn Fastabend 		return err;
203777162022SJohn Fastabend 
203877162022SJohn Fastabend 	ndm = nlmsg_data(nlh);
203977162022SJohn Fastabend 	if (ndm->ndm_ifindex == 0) {
204077162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
204177162022SJohn Fastabend 		return -EINVAL;
204277162022SJohn Fastabend 	}
204377162022SJohn Fastabend 
204477162022SJohn Fastabend 	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
204577162022SJohn Fastabend 	if (dev == NULL) {
204677162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
204777162022SJohn Fastabend 		return -ENODEV;
204877162022SJohn Fastabend 	}
204977162022SJohn Fastabend 
205077162022SJohn Fastabend 	if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
205177162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
205277162022SJohn Fastabend 		return -EINVAL;
205377162022SJohn Fastabend 	}
205477162022SJohn Fastabend 
205577162022SJohn Fastabend 	addr = nla_data(tb[NDA_LLADDR]);
205677162022SJohn Fastabend 	if (!is_valid_ether_addr(addr)) {
205777162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ether address\n");
205877162022SJohn Fastabend 		return -EINVAL;
205977162022SJohn Fastabend 	}
206077162022SJohn Fastabend 
206177162022SJohn Fastabend 	err = -EOPNOTSUPP;
206277162022SJohn Fastabend 
206377162022SJohn Fastabend 	/* Support fdb on master device the net/bridge default case */
206477162022SJohn Fastabend 	if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
206577162022SJohn Fastabend 	    (dev->priv_flags & IFF_BRIDGE_PORT)) {
206677162022SJohn Fastabend 		master = dev->master;
206777162022SJohn Fastabend 		err = master->netdev_ops->ndo_fdb_add(ndm, dev, addr,
206877162022SJohn Fastabend 						      nlh->nlmsg_flags);
206977162022SJohn Fastabend 		if (err)
207077162022SJohn Fastabend 			goto out;
207177162022SJohn Fastabend 		else
207277162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_MASTER;
207377162022SJohn Fastabend 	}
207477162022SJohn Fastabend 
207577162022SJohn Fastabend 	/* Embedded bridge, macvlan, and any other device support */
207677162022SJohn Fastabend 	if ((ndm->ndm_flags & NTF_SELF) && dev->netdev_ops->ndo_fdb_add) {
207777162022SJohn Fastabend 		err = dev->netdev_ops->ndo_fdb_add(ndm, dev, addr,
207877162022SJohn Fastabend 						   nlh->nlmsg_flags);
207977162022SJohn Fastabend 
20803ff661c3SJohn Fastabend 		if (!err) {
20813ff661c3SJohn Fastabend 			rtnl_fdb_notify(dev, addr, RTM_NEWNEIGH);
208277162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_SELF;
208377162022SJohn Fastabend 		}
20843ff661c3SJohn Fastabend 	}
208577162022SJohn Fastabend out:
208677162022SJohn Fastabend 	return err;
208777162022SJohn Fastabend }
208877162022SJohn Fastabend 
208977162022SJohn Fastabend static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
209077162022SJohn Fastabend {
209177162022SJohn Fastabend 	struct net *net = sock_net(skb->sk);
209277162022SJohn Fastabend 	struct ndmsg *ndm;
209377162022SJohn Fastabend 	struct nlattr *llattr;
209477162022SJohn Fastabend 	struct net_device *dev;
209577162022SJohn Fastabend 	int err = -EINVAL;
209677162022SJohn Fastabend 	__u8 *addr;
209777162022SJohn Fastabend 
209877162022SJohn Fastabend 	if (nlmsg_len(nlh) < sizeof(*ndm))
209977162022SJohn Fastabend 		return -EINVAL;
210077162022SJohn Fastabend 
210177162022SJohn Fastabend 	ndm = nlmsg_data(nlh);
210277162022SJohn Fastabend 	if (ndm->ndm_ifindex == 0) {
210377162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
210477162022SJohn Fastabend 		return -EINVAL;
210577162022SJohn Fastabend 	}
210677162022SJohn Fastabend 
210777162022SJohn Fastabend 	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
210877162022SJohn Fastabend 	if (dev == NULL) {
210977162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
211077162022SJohn Fastabend 		return -ENODEV;
211177162022SJohn Fastabend 	}
211277162022SJohn Fastabend 
211377162022SJohn Fastabend 	llattr = nlmsg_find_attr(nlh, sizeof(*ndm), NDA_LLADDR);
211477162022SJohn Fastabend 	if (llattr == NULL || nla_len(llattr) != ETH_ALEN) {
211577162022SJohn Fastabend 		pr_info("PF_BRIGDE: RTM_DELNEIGH with invalid address\n");
211677162022SJohn Fastabend 		return -EINVAL;
211777162022SJohn Fastabend 	}
211877162022SJohn Fastabend 
211977162022SJohn Fastabend 	addr = nla_data(llattr);
212077162022SJohn Fastabend 	err = -EOPNOTSUPP;
212177162022SJohn Fastabend 
212277162022SJohn Fastabend 	/* Support fdb on master device the net/bridge default case */
212377162022SJohn Fastabend 	if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
212477162022SJohn Fastabend 	    (dev->priv_flags & IFF_BRIDGE_PORT)) {
212577162022SJohn Fastabend 		struct net_device *master = dev->master;
212677162022SJohn Fastabend 
212777162022SJohn Fastabend 		if (master->netdev_ops->ndo_fdb_del)
212877162022SJohn Fastabend 			err = master->netdev_ops->ndo_fdb_del(ndm, dev, addr);
212977162022SJohn Fastabend 
213077162022SJohn Fastabend 		if (err)
213177162022SJohn Fastabend 			goto out;
213277162022SJohn Fastabend 		else
213377162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_MASTER;
213477162022SJohn Fastabend 	}
213577162022SJohn Fastabend 
213677162022SJohn Fastabend 	/* Embedded bridge, macvlan, and any other device support */
213777162022SJohn Fastabend 	if ((ndm->ndm_flags & NTF_SELF) && dev->netdev_ops->ndo_fdb_del) {
213877162022SJohn Fastabend 		err = dev->netdev_ops->ndo_fdb_del(ndm, dev, addr);
213977162022SJohn Fastabend 
21403ff661c3SJohn Fastabend 		if (!err) {
21413ff661c3SJohn Fastabend 			rtnl_fdb_notify(dev, addr, RTM_DELNEIGH);
214277162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_SELF;
214377162022SJohn Fastabend 		}
21443ff661c3SJohn Fastabend 	}
214577162022SJohn Fastabend out:
214677162022SJohn Fastabend 	return err;
214777162022SJohn Fastabend }
214877162022SJohn Fastabend 
2149d83b0603SJohn Fastabend static int nlmsg_populate_fdb(struct sk_buff *skb,
2150d83b0603SJohn Fastabend 			      struct netlink_callback *cb,
2151d83b0603SJohn Fastabend 			      struct net_device *dev,
2152d83b0603SJohn Fastabend 			      int *idx,
2153d83b0603SJohn Fastabend 			      struct netdev_hw_addr_list *list)
2154d83b0603SJohn Fastabend {
2155d83b0603SJohn Fastabend 	struct netdev_hw_addr *ha;
2156d83b0603SJohn Fastabend 	int err;
2157d83b0603SJohn Fastabend 	u32 pid, seq;
2158d83b0603SJohn Fastabend 
2159d83b0603SJohn Fastabend 	pid = NETLINK_CB(cb->skb).pid;
2160d83b0603SJohn Fastabend 	seq = cb->nlh->nlmsg_seq;
2161d83b0603SJohn Fastabend 
2162d83b0603SJohn Fastabend 	list_for_each_entry(ha, &list->list, list) {
2163d83b0603SJohn Fastabend 		if (*idx < cb->args[0])
2164d83b0603SJohn Fastabend 			goto skip;
2165d83b0603SJohn Fastabend 
2166d83b0603SJohn Fastabend 		err = nlmsg_populate_fdb_fill(skb, dev, ha->addr,
2167d83b0603SJohn Fastabend 					      pid, seq, 0, NTF_SELF);
2168d83b0603SJohn Fastabend 		if (err < 0)
2169d83b0603SJohn Fastabend 			return err;
2170d83b0603SJohn Fastabend skip:
2171d83b0603SJohn Fastabend 		*idx += 1;
2172d83b0603SJohn Fastabend 	}
2173d83b0603SJohn Fastabend 	return 0;
2174d83b0603SJohn Fastabend }
2175d83b0603SJohn Fastabend 
2176d83b0603SJohn Fastabend /**
21772c53040fSBen Hutchings  * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
2178d83b0603SJohn Fastabend  * @nlh: netlink message header
2179d83b0603SJohn Fastabend  * @dev: netdevice
2180d83b0603SJohn Fastabend  *
2181d83b0603SJohn Fastabend  * Default netdevice operation to dump the existing unicast address list.
2182d83b0603SJohn Fastabend  * Returns zero on success.
2183d83b0603SJohn Fastabend  */
2184d83b0603SJohn Fastabend int ndo_dflt_fdb_dump(struct sk_buff *skb,
2185d83b0603SJohn Fastabend 		      struct netlink_callback *cb,
2186d83b0603SJohn Fastabend 		      struct net_device *dev,
2187d83b0603SJohn Fastabend 		      int idx)
2188d83b0603SJohn Fastabend {
2189d83b0603SJohn Fastabend 	int err;
2190d83b0603SJohn Fastabend 
2191d83b0603SJohn Fastabend 	netif_addr_lock_bh(dev);
2192d83b0603SJohn Fastabend 	err = nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->uc);
2193d83b0603SJohn Fastabend 	if (err)
2194d83b0603SJohn Fastabend 		goto out;
2195d83b0603SJohn Fastabend 	nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->mc);
2196d83b0603SJohn Fastabend out:
2197d83b0603SJohn Fastabend 	netif_addr_unlock_bh(dev);
2198d83b0603SJohn Fastabend 	return idx;
2199d83b0603SJohn Fastabend }
2200d83b0603SJohn Fastabend EXPORT_SYMBOL(ndo_dflt_fdb_dump);
2201d83b0603SJohn Fastabend 
220277162022SJohn Fastabend static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
220377162022SJohn Fastabend {
220477162022SJohn Fastabend 	int idx = 0;
220577162022SJohn Fastabend 	struct net *net = sock_net(skb->sk);
220677162022SJohn Fastabend 	struct net_device *dev;
220777162022SJohn Fastabend 
220877162022SJohn Fastabend 	rcu_read_lock();
220977162022SJohn Fastabend 	for_each_netdev_rcu(net, dev) {
221077162022SJohn Fastabend 		if (dev->priv_flags & IFF_BRIDGE_PORT) {
221177162022SJohn Fastabend 			struct net_device *master = dev->master;
221277162022SJohn Fastabend 			const struct net_device_ops *ops = master->netdev_ops;
221377162022SJohn Fastabend 
221477162022SJohn Fastabend 			if (ops->ndo_fdb_dump)
221577162022SJohn Fastabend 				idx = ops->ndo_fdb_dump(skb, cb, dev, idx);
221677162022SJohn Fastabend 		}
221777162022SJohn Fastabend 
221877162022SJohn Fastabend 		if (dev->netdev_ops->ndo_fdb_dump)
221977162022SJohn Fastabend 			idx = dev->netdev_ops->ndo_fdb_dump(skb, cb, dev, idx);
222077162022SJohn Fastabend 	}
222177162022SJohn Fastabend 	rcu_read_unlock();
222277162022SJohn Fastabend 
222377162022SJohn Fastabend 	cb->args[0] = idx;
222477162022SJohn Fastabend 	return skb->len;
222577162022SJohn Fastabend }
222677162022SJohn Fastabend 
22271da177e4SLinus Torvalds /* Protected by RTNL sempahore.  */
22281da177e4SLinus Torvalds static struct rtattr **rta_buf;
22291da177e4SLinus Torvalds static int rtattr_max;
22301da177e4SLinus Torvalds 
22311da177e4SLinus Torvalds /* Process one rtnetlink message. */
22321da177e4SLinus Torvalds 
22331d00a4ebSThomas Graf static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
22341da177e4SLinus Torvalds {
22353b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
2236e2849863SThomas Graf 	rtnl_doit_func doit;
22371da177e4SLinus Torvalds 	int sz_idx, kind;
22381da177e4SLinus Torvalds 	int min_len;
22391da177e4SLinus Torvalds 	int family;
22401da177e4SLinus Torvalds 	int type;
22412907c35fSEric Dumazet 	int err;
22421da177e4SLinus Torvalds 
22431da177e4SLinus Torvalds 	type = nlh->nlmsg_type;
22441da177e4SLinus Torvalds 	if (type > RTM_MAX)
2245038890feSThomas Graf 		return -EOPNOTSUPP;
22461da177e4SLinus Torvalds 
22471da177e4SLinus Torvalds 	type -= RTM_BASE;
22481da177e4SLinus Torvalds 
22491da177e4SLinus Torvalds 	/* All the messages must have at least 1 byte length */
22501da177e4SLinus Torvalds 	if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
22511da177e4SLinus Torvalds 		return 0;
22521da177e4SLinus Torvalds 
22531da177e4SLinus Torvalds 	family = ((struct rtgenmsg *)NLMSG_DATA(nlh))->rtgen_family;
22541da177e4SLinus Torvalds 	sz_idx = type>>2;
22551da177e4SLinus Torvalds 	kind = type&3;
22561da177e4SLinus Torvalds 
2257fd778461SEric Paris 	if (kind != 2 && !capable(CAP_NET_ADMIN))
22581d00a4ebSThomas Graf 		return -EPERM;
22591da177e4SLinus Torvalds 
2260b8f3ab42SDavid S. Miller 	if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
226197c53cacSDenis V. Lunev 		struct sock *rtnl;
2262e2849863SThomas Graf 		rtnl_dumpit_func dumpit;
2263c7ac8679SGreg Rose 		rtnl_calcit_func calcit;
2264c7ac8679SGreg Rose 		u16 min_dump_alloc = 0;
22651da177e4SLinus Torvalds 
2266e2849863SThomas Graf 		dumpit = rtnl_get_dumpit(family, type);
2267e2849863SThomas Graf 		if (dumpit == NULL)
2268038890feSThomas Graf 			return -EOPNOTSUPP;
2269c7ac8679SGreg Rose 		calcit = rtnl_get_calcit(family, type);
2270c7ac8679SGreg Rose 		if (calcit)
2271115c9b81SGreg Rose 			min_dump_alloc = calcit(skb, nlh);
22721da177e4SLinus Torvalds 
22732907c35fSEric Dumazet 		__rtnl_unlock();
227497c53cacSDenis V. Lunev 		rtnl = net->rtnl;
227580d326faSPablo Neira Ayuso 		{
227680d326faSPablo Neira Ayuso 			struct netlink_dump_control c = {
227780d326faSPablo Neira Ayuso 				.dump		= dumpit,
227880d326faSPablo Neira Ayuso 				.min_dump_alloc	= min_dump_alloc,
227980d326faSPablo Neira Ayuso 			};
228080d326faSPablo Neira Ayuso 			err = netlink_dump_start(rtnl, skb, nlh, &c);
228180d326faSPablo Neira Ayuso 		}
22822907c35fSEric Dumazet 		rtnl_lock();
22832907c35fSEric Dumazet 		return err;
22841da177e4SLinus Torvalds 	}
22851da177e4SLinus Torvalds 
22861da177e4SLinus Torvalds 	memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *)));
22871da177e4SLinus Torvalds 
22881da177e4SLinus Torvalds 	min_len = rtm_min[sz_idx];
22891da177e4SLinus Torvalds 	if (nlh->nlmsg_len < min_len)
22901d00a4ebSThomas Graf 		return -EINVAL;
22911da177e4SLinus Torvalds 
22921da177e4SLinus Torvalds 	if (nlh->nlmsg_len > min_len) {
22931da177e4SLinus Torvalds 		int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
22941da177e4SLinus Torvalds 		struct rtattr *attr = (void *)nlh + NLMSG_ALIGN(min_len);
22951da177e4SLinus Torvalds 
22961da177e4SLinus Torvalds 		while (RTA_OK(attr, attrlen)) {
229795c96174SEric Dumazet 			unsigned int flavor = attr->rta_type;
22981da177e4SLinus Torvalds 			if (flavor) {
22991da177e4SLinus Torvalds 				if (flavor > rta_max[sz_idx])
23001d00a4ebSThomas Graf 					return -EINVAL;
23011da177e4SLinus Torvalds 				rta_buf[flavor-1] = attr;
23021da177e4SLinus Torvalds 			}
23031da177e4SLinus Torvalds 			attr = RTA_NEXT(attr, attrlen);
23041da177e4SLinus Torvalds 		}
23051da177e4SLinus Torvalds 	}
23061da177e4SLinus Torvalds 
2307e2849863SThomas Graf 	doit = rtnl_get_doit(family, type);
2308e2849863SThomas Graf 	if (doit == NULL)
2309038890feSThomas Graf 		return -EOPNOTSUPP;
23101da177e4SLinus Torvalds 
23111d00a4ebSThomas Graf 	return doit(skb, nlh, (void *)&rta_buf[0]);
23121da177e4SLinus Torvalds }
23131da177e4SLinus Torvalds 
2314cd40b7d3SDenis V. Lunev static void rtnetlink_rcv(struct sk_buff *skb)
23151da177e4SLinus Torvalds {
23161536cc0dSDenis V. Lunev 	rtnl_lock();
2317cd40b7d3SDenis V. Lunev 	netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
23181536cc0dSDenis V. Lunev 	rtnl_unlock();
23191da177e4SLinus Torvalds }
23201da177e4SLinus Torvalds 
23211da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
23221da177e4SLinus Torvalds {
23231da177e4SLinus Torvalds 	struct net_device *dev = ptr;
2324e9dc8653SEric W. Biederman 
23251da177e4SLinus Torvalds 	switch (event) {
23261da177e4SLinus Torvalds 	case NETDEV_UP:
23271da177e4SLinus Torvalds 	case NETDEV_DOWN:
232810de05afSPatrick McHardy 	case NETDEV_PRE_UP:
2329d90a909eSEric W. Biederman 	case NETDEV_POST_INIT:
2330d90a909eSEric W. Biederman 	case NETDEV_REGISTER:
23311da177e4SLinus Torvalds 	case NETDEV_CHANGE:
2332755d0e77SPatrick McHardy 	case NETDEV_PRE_TYPE_CHANGE:
23331da177e4SLinus Torvalds 	case NETDEV_GOING_DOWN:
2334a2835763SPatrick McHardy 	case NETDEV_UNREGISTER:
2335d90a909eSEric W. Biederman 	case NETDEV_UNREGISTER_BATCH:
2336ac3d3f81SAmerigo Wang 	case NETDEV_RELEASE:
2337ac3d3f81SAmerigo Wang 	case NETDEV_JOIN:
23381da177e4SLinus Torvalds 		break;
23391da177e4SLinus Torvalds 	default:
23401da177e4SLinus Torvalds 		rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
23411da177e4SLinus Torvalds 		break;
23421da177e4SLinus Torvalds 	}
23431da177e4SLinus Torvalds 	return NOTIFY_DONE;
23441da177e4SLinus Torvalds }
23451da177e4SLinus Torvalds 
23461da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = {
23471da177e4SLinus Torvalds 	.notifier_call	= rtnetlink_event,
23481da177e4SLinus Torvalds };
23491da177e4SLinus Torvalds 
235097c53cacSDenis V. Lunev 
23512c8c1e72SAlexey Dobriyan static int __net_init rtnetlink_net_init(struct net *net)
235297c53cacSDenis V. Lunev {
235397c53cacSDenis V. Lunev 	struct sock *sk;
2354a31f2d17SPablo Neira Ayuso 	struct netlink_kernel_cfg cfg = {
2355a31f2d17SPablo Neira Ayuso 		.groups		= RTNLGRP_MAX,
2356a31f2d17SPablo Neira Ayuso 		.input		= rtnetlink_rcv,
2357a31f2d17SPablo Neira Ayuso 		.cb_mutex	= &rtnl_mutex,
2358a31f2d17SPablo Neira Ayuso 	};
2359a31f2d17SPablo Neira Ayuso 
2360a31f2d17SPablo Neira Ayuso 	sk = netlink_kernel_create(net, NETLINK_ROUTE, THIS_MODULE, &cfg);
236197c53cacSDenis V. Lunev 	if (!sk)
236297c53cacSDenis V. Lunev 		return -ENOMEM;
236397c53cacSDenis V. Lunev 	net->rtnl = sk;
236497c53cacSDenis V. Lunev 	return 0;
236597c53cacSDenis V. Lunev }
236697c53cacSDenis V. Lunev 
23672c8c1e72SAlexey Dobriyan static void __net_exit rtnetlink_net_exit(struct net *net)
236897c53cacSDenis V. Lunev {
2369b7c6ba6eSDenis V. Lunev 	netlink_kernel_release(net->rtnl);
237097c53cacSDenis V. Lunev 	net->rtnl = NULL;
237197c53cacSDenis V. Lunev }
237297c53cacSDenis V. Lunev 
237397c53cacSDenis V. Lunev static struct pernet_operations rtnetlink_net_ops = {
237497c53cacSDenis V. Lunev 	.init = rtnetlink_net_init,
237597c53cacSDenis V. Lunev 	.exit = rtnetlink_net_exit,
237697c53cacSDenis V. Lunev };
237797c53cacSDenis V. Lunev 
23781da177e4SLinus Torvalds void __init rtnetlink_init(void)
23791da177e4SLinus Torvalds {
23801da177e4SLinus Torvalds 	int i;
23811da177e4SLinus Torvalds 
23821da177e4SLinus Torvalds 	rtattr_max = 0;
23831da177e4SLinus Torvalds 	for (i = 0; i < ARRAY_SIZE(rta_max); i++)
23841da177e4SLinus Torvalds 		if (rta_max[i] > rtattr_max)
23851da177e4SLinus Torvalds 			rtattr_max = rta_max[i];
23861da177e4SLinus Torvalds 	rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL);
23871da177e4SLinus Torvalds 	if (!rta_buf)
23881da177e4SLinus Torvalds 		panic("rtnetlink_init: cannot allocate rta_buf\n");
23891da177e4SLinus Torvalds 
239097c53cacSDenis V. Lunev 	if (register_pernet_subsys(&rtnetlink_net_ops))
23911da177e4SLinus Torvalds 		panic("rtnetlink_init: cannot initialize rtnetlink\n");
239297c53cacSDenis V. Lunev 
23931da177e4SLinus Torvalds 	netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV);
23941da177e4SLinus Torvalds 	register_netdevice_notifier(&rtnetlink_dev_notifier);
2395340d17fcSThomas Graf 
2396c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
2397c7ac8679SGreg Rose 		      rtnl_dump_ifinfo, rtnl_calcit);
2398c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL);
2399c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL);
2400c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL);
2401687ad8ccSThomas Graf 
2402c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL);
2403c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL);
240477162022SJohn Fastabend 
240577162022SJohn Fastabend 	rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, NULL);
240677162022SJohn Fastabend 	rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, NULL);
240777162022SJohn Fastabend 	rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, NULL);
24081da177e4SLinus Torvalds }
24091da177e4SLinus Torvalds 
2410