xref: /openbmc/linux/net/core/rtnetlink.c (revision 200b916f3575bdf11609cb447661b8d5957b0bbf)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * INET		An implementation of the TCP/IP protocol suite for the LINUX
31da177e4SLinus Torvalds  *		operating system.  INET is implemented using the  BSD Socket
41da177e4SLinus Torvalds  *		interface as the means of communication with the user level.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *		Routing netlink socket interface: protocol independent part.
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  *		This program is free software; you can redistribute it and/or
111da177e4SLinus Torvalds  *		modify it under the terms of the GNU General Public License
121da177e4SLinus Torvalds  *		as published by the Free Software Foundation; either version
131da177e4SLinus Torvalds  *		2 of the License, or (at your option) any later version.
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  *	Fixes:
161da177e4SLinus Torvalds  *	Vitaly E. Lavrov		RTA_OK arithmetics was wrong.
171da177e4SLinus Torvalds  */
181da177e4SLinus Torvalds 
191da177e4SLinus Torvalds #include <linux/errno.h>
201da177e4SLinus Torvalds #include <linux/module.h>
211da177e4SLinus Torvalds #include <linux/types.h>
221da177e4SLinus Torvalds #include <linux/socket.h>
231da177e4SLinus Torvalds #include <linux/kernel.h>
241da177e4SLinus Torvalds #include <linux/timer.h>
251da177e4SLinus Torvalds #include <linux/string.h>
261da177e4SLinus Torvalds #include <linux/sockios.h>
271da177e4SLinus Torvalds #include <linux/net.h>
281da177e4SLinus Torvalds #include <linux/fcntl.h>
291da177e4SLinus Torvalds #include <linux/mm.h>
301da177e4SLinus Torvalds #include <linux/slab.h>
311da177e4SLinus Torvalds #include <linux/interrupt.h>
321da177e4SLinus Torvalds #include <linux/capability.h>
331da177e4SLinus Torvalds #include <linux/skbuff.h>
341da177e4SLinus Torvalds #include <linux/init.h>
351da177e4SLinus Torvalds #include <linux/security.h>
366756ae4bSStephen Hemminger #include <linux/mutex.h>
371823730fSThomas Graf #include <linux/if_addr.h>
3877162022SJohn Fastabend #include <linux/if_bridge.h>
39ebc08a6fSWilliams, Mitch A #include <linux/pci.h>
4077162022SJohn Fastabend #include <linux/etherdevice.h>
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds #include <asm/uaccess.h>
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds #include <linux/inet.h>
451da177e4SLinus Torvalds #include <linux/netdevice.h>
461da177e4SLinus Torvalds #include <net/ip.h>
471da177e4SLinus Torvalds #include <net/protocol.h>
481da177e4SLinus Torvalds #include <net/arp.h>
491da177e4SLinus Torvalds #include <net/route.h>
501da177e4SLinus Torvalds #include <net/udp.h>
511da177e4SLinus Torvalds #include <net/sock.h>
521da177e4SLinus Torvalds #include <net/pkt_sched.h>
5314c0b97dSThomas Graf #include <net/fib_rules.h>
54e2849863SThomas Graf #include <net/rtnetlink.h>
5530ffee84SJohannes Berg #include <net/net_namespace.h>
561da177e4SLinus Torvalds 
57e0d087afSEric Dumazet struct rtnl_link {
58e2849863SThomas Graf 	rtnl_doit_func		doit;
59e2849863SThomas Graf 	rtnl_dumpit_func	dumpit;
60c7ac8679SGreg Rose 	rtnl_calcit_func 	calcit;
61e2849863SThomas Graf };
62e2849863SThomas Graf 
636756ae4bSStephen Hemminger static DEFINE_MUTEX(rtnl_mutex);
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds void rtnl_lock(void)
661da177e4SLinus Torvalds {
676756ae4bSStephen Hemminger 	mutex_lock(&rtnl_mutex);
681da177e4SLinus Torvalds }
69e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_lock);
701da177e4SLinus Torvalds 
716756ae4bSStephen Hemminger void __rtnl_unlock(void)
721da177e4SLinus Torvalds {
736756ae4bSStephen Hemminger 	mutex_unlock(&rtnl_mutex);
741da177e4SLinus Torvalds }
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds void rtnl_unlock(void)
771da177e4SLinus Torvalds {
7858ec3b4dSHerbert Xu 	/* This fellow will unlock it for us. */
791da177e4SLinus Torvalds 	netdev_run_todo();
801da177e4SLinus Torvalds }
81e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unlock);
821da177e4SLinus Torvalds 
836756ae4bSStephen Hemminger int rtnl_trylock(void)
846756ae4bSStephen Hemminger {
856756ae4bSStephen Hemminger 	return mutex_trylock(&rtnl_mutex);
866756ae4bSStephen Hemminger }
87e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_trylock);
886756ae4bSStephen Hemminger 
89c9c1014bSPatrick McHardy int rtnl_is_locked(void)
90c9c1014bSPatrick McHardy {
91c9c1014bSPatrick McHardy 	return mutex_is_locked(&rtnl_mutex);
92c9c1014bSPatrick McHardy }
93e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_is_locked);
94c9c1014bSPatrick McHardy 
95a898def2SPaul E. McKenney #ifdef CONFIG_PROVE_LOCKING
96a898def2SPaul E. McKenney int lockdep_rtnl_is_held(void)
97a898def2SPaul E. McKenney {
98a898def2SPaul E. McKenney 	return lockdep_is_held(&rtnl_mutex);
99a898def2SPaul E. McKenney }
100a898def2SPaul E. McKenney EXPORT_SYMBOL(lockdep_rtnl_is_held);
101a898def2SPaul E. McKenney #endif /* #ifdef CONFIG_PROVE_LOCKING */
102a898def2SPaul E. McKenney 
10325239ceeSPatrick McHardy static struct rtnl_link *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
104e2849863SThomas Graf 
105e2849863SThomas Graf static inline int rtm_msgindex(int msgtype)
106e2849863SThomas Graf {
107e2849863SThomas Graf 	int msgindex = msgtype - RTM_BASE;
108e2849863SThomas Graf 
109e2849863SThomas Graf 	/*
110e2849863SThomas Graf 	 * msgindex < 0 implies someone tried to register a netlink
111e2849863SThomas Graf 	 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
112e2849863SThomas Graf 	 * the message type has not been added to linux/rtnetlink.h
113e2849863SThomas Graf 	 */
114e2849863SThomas Graf 	BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
115e2849863SThomas Graf 
116e2849863SThomas Graf 	return msgindex;
117e2849863SThomas Graf }
118e2849863SThomas Graf 
119e2849863SThomas Graf static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex)
120e2849863SThomas Graf {
121e2849863SThomas Graf 	struct rtnl_link *tab;
122e2849863SThomas Graf 
12325239ceeSPatrick McHardy 	if (protocol <= RTNL_FAMILY_MAX)
124e2849863SThomas Graf 		tab = rtnl_msg_handlers[protocol];
1250f87b1ddSPatrick McHardy 	else
1260f87b1ddSPatrick McHardy 		tab = NULL;
1270f87b1ddSPatrick McHardy 
12851057f2fSThomas Graf 	if (tab == NULL || tab[msgindex].doit == NULL)
129e2849863SThomas Graf 		tab = rtnl_msg_handlers[PF_UNSPEC];
130e2849863SThomas Graf 
131c80bbeaeSHans Zhang 	return tab[msgindex].doit;
132e2849863SThomas Graf }
133e2849863SThomas Graf 
134e2849863SThomas Graf static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex)
135e2849863SThomas Graf {
136e2849863SThomas Graf 	struct rtnl_link *tab;
137e2849863SThomas Graf 
13825239ceeSPatrick McHardy 	if (protocol <= RTNL_FAMILY_MAX)
139e2849863SThomas Graf 		tab = rtnl_msg_handlers[protocol];
1400f87b1ddSPatrick McHardy 	else
1410f87b1ddSPatrick McHardy 		tab = NULL;
1420f87b1ddSPatrick McHardy 
14351057f2fSThomas Graf 	if (tab == NULL || tab[msgindex].dumpit == NULL)
144e2849863SThomas Graf 		tab = rtnl_msg_handlers[PF_UNSPEC];
145e2849863SThomas Graf 
146c80bbeaeSHans Zhang 	return tab[msgindex].dumpit;
147e2849863SThomas Graf }
148e2849863SThomas Graf 
149c7ac8679SGreg Rose static rtnl_calcit_func rtnl_get_calcit(int protocol, int msgindex)
150c7ac8679SGreg Rose {
151c7ac8679SGreg Rose 	struct rtnl_link *tab;
152c7ac8679SGreg Rose 
153c7ac8679SGreg Rose 	if (protocol <= RTNL_FAMILY_MAX)
154c7ac8679SGreg Rose 		tab = rtnl_msg_handlers[protocol];
155c7ac8679SGreg Rose 	else
156c7ac8679SGreg Rose 		tab = NULL;
157c7ac8679SGreg Rose 
158c7ac8679SGreg Rose 	if (tab == NULL || tab[msgindex].calcit == NULL)
159c7ac8679SGreg Rose 		tab = rtnl_msg_handlers[PF_UNSPEC];
160c7ac8679SGreg Rose 
161c80bbeaeSHans Zhang 	return tab[msgindex].calcit;
162c7ac8679SGreg Rose }
163c7ac8679SGreg Rose 
164e2849863SThomas Graf /**
165e2849863SThomas Graf  * __rtnl_register - Register a rtnetlink message type
166e2849863SThomas Graf  * @protocol: Protocol family or PF_UNSPEC
167e2849863SThomas Graf  * @msgtype: rtnetlink message type
168e2849863SThomas Graf  * @doit: Function pointer called for each request message
169e2849863SThomas Graf  * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
170c7ac8679SGreg Rose  * @calcit: Function pointer to calc size of dump message
171e2849863SThomas Graf  *
172e2849863SThomas Graf  * Registers the specified function pointers (at least one of them has
173e2849863SThomas Graf  * to be non-NULL) to be called whenever a request message for the
174e2849863SThomas Graf  * specified protocol family and message type is received.
175e2849863SThomas Graf  *
176e2849863SThomas Graf  * The special protocol family PF_UNSPEC may be used to define fallback
177e2849863SThomas Graf  * function pointers for the case when no entry for the specific protocol
178e2849863SThomas Graf  * family exists.
179e2849863SThomas Graf  *
180e2849863SThomas Graf  * Returns 0 on success or a negative error code.
181e2849863SThomas Graf  */
182e2849863SThomas Graf int __rtnl_register(int protocol, int msgtype,
183c7ac8679SGreg Rose 		    rtnl_doit_func doit, rtnl_dumpit_func dumpit,
184c7ac8679SGreg Rose 		    rtnl_calcit_func calcit)
185e2849863SThomas Graf {
186e2849863SThomas Graf 	struct rtnl_link *tab;
187e2849863SThomas Graf 	int msgindex;
188e2849863SThomas Graf 
18925239ceeSPatrick McHardy 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
190e2849863SThomas Graf 	msgindex = rtm_msgindex(msgtype);
191e2849863SThomas Graf 
192e2849863SThomas Graf 	tab = rtnl_msg_handlers[protocol];
193e2849863SThomas Graf 	if (tab == NULL) {
194e2849863SThomas Graf 		tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL);
195e2849863SThomas Graf 		if (tab == NULL)
196e2849863SThomas Graf 			return -ENOBUFS;
197e2849863SThomas Graf 
198e2849863SThomas Graf 		rtnl_msg_handlers[protocol] = tab;
199e2849863SThomas Graf 	}
200e2849863SThomas Graf 
201e2849863SThomas Graf 	if (doit)
202e2849863SThomas Graf 		tab[msgindex].doit = doit;
203e2849863SThomas Graf 
204e2849863SThomas Graf 	if (dumpit)
205e2849863SThomas Graf 		tab[msgindex].dumpit = dumpit;
206e2849863SThomas Graf 
207c7ac8679SGreg Rose 	if (calcit)
208c7ac8679SGreg Rose 		tab[msgindex].calcit = calcit;
209c7ac8679SGreg Rose 
210e2849863SThomas Graf 	return 0;
211e2849863SThomas Graf }
212e2849863SThomas Graf EXPORT_SYMBOL_GPL(__rtnl_register);
213e2849863SThomas Graf 
214e2849863SThomas Graf /**
215e2849863SThomas Graf  * rtnl_register - Register a rtnetlink message type
216e2849863SThomas Graf  *
217e2849863SThomas Graf  * Identical to __rtnl_register() but panics on failure. This is useful
218e2849863SThomas Graf  * as failure of this function is very unlikely, it can only happen due
219e2849863SThomas Graf  * to lack of memory when allocating the chain to store all message
220e2849863SThomas Graf  * handlers for a protocol. Meant for use in init functions where lack
22125985edcSLucas De Marchi  * of memory implies no sense in continuing.
222e2849863SThomas Graf  */
223e2849863SThomas Graf void rtnl_register(int protocol, int msgtype,
224c7ac8679SGreg Rose 		   rtnl_doit_func doit, rtnl_dumpit_func dumpit,
225c7ac8679SGreg Rose 		   rtnl_calcit_func calcit)
226e2849863SThomas Graf {
227c7ac8679SGreg Rose 	if (__rtnl_register(protocol, msgtype, doit, dumpit, calcit) < 0)
228e2849863SThomas Graf 		panic("Unable to register rtnetlink message handler, "
229e2849863SThomas Graf 		      "protocol = %d, message type = %d\n",
230e2849863SThomas Graf 		      protocol, msgtype);
231e2849863SThomas Graf }
232e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_register);
233e2849863SThomas Graf 
234e2849863SThomas Graf /**
235e2849863SThomas Graf  * rtnl_unregister - Unregister a rtnetlink message type
236e2849863SThomas Graf  * @protocol: Protocol family or PF_UNSPEC
237e2849863SThomas Graf  * @msgtype: rtnetlink message type
238e2849863SThomas Graf  *
239e2849863SThomas Graf  * Returns 0 on success or a negative error code.
240e2849863SThomas Graf  */
241e2849863SThomas Graf int rtnl_unregister(int protocol, int msgtype)
242e2849863SThomas Graf {
243e2849863SThomas Graf 	int msgindex;
244e2849863SThomas Graf 
24525239ceeSPatrick McHardy 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
246e2849863SThomas Graf 	msgindex = rtm_msgindex(msgtype);
247e2849863SThomas Graf 
248e2849863SThomas Graf 	if (rtnl_msg_handlers[protocol] == NULL)
249e2849863SThomas Graf 		return -ENOENT;
250e2849863SThomas Graf 
251e2849863SThomas Graf 	rtnl_msg_handlers[protocol][msgindex].doit = NULL;
252e2849863SThomas Graf 	rtnl_msg_handlers[protocol][msgindex].dumpit = NULL;
253e2849863SThomas Graf 
254e2849863SThomas Graf 	return 0;
255e2849863SThomas Graf }
256e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister);
257e2849863SThomas Graf 
258e2849863SThomas Graf /**
259e2849863SThomas Graf  * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
260e2849863SThomas Graf  * @protocol : Protocol family or PF_UNSPEC
261e2849863SThomas Graf  *
262e2849863SThomas Graf  * Identical to calling rtnl_unregster() for all registered message types
263e2849863SThomas Graf  * of a certain protocol family.
264e2849863SThomas Graf  */
265e2849863SThomas Graf void rtnl_unregister_all(int protocol)
266e2849863SThomas Graf {
26725239ceeSPatrick McHardy 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
268e2849863SThomas Graf 
269e2849863SThomas Graf 	kfree(rtnl_msg_handlers[protocol]);
270e2849863SThomas Graf 	rtnl_msg_handlers[protocol] = NULL;
271e2849863SThomas Graf }
272e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister_all);
2731da177e4SLinus Torvalds 
27438f7b870SPatrick McHardy static LIST_HEAD(link_ops);
27538f7b870SPatrick McHardy 
276c63044f0SEric Dumazet static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
277c63044f0SEric Dumazet {
278c63044f0SEric Dumazet 	const struct rtnl_link_ops *ops;
279c63044f0SEric Dumazet 
280c63044f0SEric Dumazet 	list_for_each_entry(ops, &link_ops, list) {
281c63044f0SEric Dumazet 		if (!strcmp(ops->kind, kind))
282c63044f0SEric Dumazet 			return ops;
283c63044f0SEric Dumazet 	}
284c63044f0SEric Dumazet 	return NULL;
285c63044f0SEric Dumazet }
286c63044f0SEric Dumazet 
28738f7b870SPatrick McHardy /**
28838f7b870SPatrick McHardy  * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
28938f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to register
29038f7b870SPatrick McHardy  *
29138f7b870SPatrick McHardy  * The caller must hold the rtnl_mutex. This function should be used
29238f7b870SPatrick McHardy  * by drivers that create devices during module initialization. It
29338f7b870SPatrick McHardy  * must be called before registering the devices.
29438f7b870SPatrick McHardy  *
29538f7b870SPatrick McHardy  * Returns 0 on success or a negative error code.
29638f7b870SPatrick McHardy  */
29738f7b870SPatrick McHardy int __rtnl_link_register(struct rtnl_link_ops *ops)
29838f7b870SPatrick McHardy {
299c63044f0SEric Dumazet 	if (rtnl_link_ops_get(ops->kind))
300c63044f0SEric Dumazet 		return -EEXIST;
301c63044f0SEric Dumazet 
3022d85cba2SPatrick McHardy 	if (!ops->dellink)
30323289a37SEric Dumazet 		ops->dellink = unregister_netdevice_queue;
3042d85cba2SPatrick McHardy 
30538f7b870SPatrick McHardy 	list_add_tail(&ops->list, &link_ops);
30638f7b870SPatrick McHardy 	return 0;
30738f7b870SPatrick McHardy }
30838f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_register);
30938f7b870SPatrick McHardy 
31038f7b870SPatrick McHardy /**
31138f7b870SPatrick McHardy  * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
31238f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to register
31338f7b870SPatrick McHardy  *
31438f7b870SPatrick McHardy  * Returns 0 on success or a negative error code.
31538f7b870SPatrick McHardy  */
31638f7b870SPatrick McHardy int rtnl_link_register(struct rtnl_link_ops *ops)
31738f7b870SPatrick McHardy {
31838f7b870SPatrick McHardy 	int err;
31938f7b870SPatrick McHardy 
32038f7b870SPatrick McHardy 	rtnl_lock();
32138f7b870SPatrick McHardy 	err = __rtnl_link_register(ops);
32238f7b870SPatrick McHardy 	rtnl_unlock();
32338f7b870SPatrick McHardy 	return err;
32438f7b870SPatrick McHardy }
32538f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_register);
32638f7b870SPatrick McHardy 
327669f87baSPavel Emelyanov static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
328669f87baSPavel Emelyanov {
329669f87baSPavel Emelyanov 	struct net_device *dev;
33023289a37SEric Dumazet 	LIST_HEAD(list_kill);
33123289a37SEric Dumazet 
332669f87baSPavel Emelyanov 	for_each_netdev(net, dev) {
33323289a37SEric Dumazet 		if (dev->rtnl_link_ops == ops)
33423289a37SEric Dumazet 			ops->dellink(dev, &list_kill);
335669f87baSPavel Emelyanov 	}
33623289a37SEric Dumazet 	unregister_netdevice_many(&list_kill);
337669f87baSPavel Emelyanov }
338669f87baSPavel Emelyanov 
33938f7b870SPatrick McHardy /**
34038f7b870SPatrick McHardy  * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
34138f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to unregister
34238f7b870SPatrick McHardy  *
3432d85cba2SPatrick McHardy  * The caller must hold the rtnl_mutex.
34438f7b870SPatrick McHardy  */
34538f7b870SPatrick McHardy void __rtnl_link_unregister(struct rtnl_link_ops *ops)
34638f7b870SPatrick McHardy {
347881d966bSEric W. Biederman 	struct net *net;
3482d85cba2SPatrick McHardy 
349881d966bSEric W. Biederman 	for_each_net(net) {
350669f87baSPavel Emelyanov 		__rtnl_kill_links(net, ops);
351881d966bSEric W. Biederman 	}
35238f7b870SPatrick McHardy 	list_del(&ops->list);
35338f7b870SPatrick McHardy }
35438f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
35538f7b870SPatrick McHardy 
356*200b916fSCong Wang /* Return with the rtnl_lock held when there are no network
357*200b916fSCong Wang  * devices unregistering in any network namespace.
358*200b916fSCong Wang  */
359*200b916fSCong Wang static void rtnl_lock_unregistering_all(void)
360*200b916fSCong Wang {
361*200b916fSCong Wang 	struct net *net;
362*200b916fSCong Wang 	bool unregistering;
363*200b916fSCong Wang 	DEFINE_WAIT(wait);
364*200b916fSCong Wang 
365*200b916fSCong Wang 	for (;;) {
366*200b916fSCong Wang 		prepare_to_wait(&netdev_unregistering_wq, &wait,
367*200b916fSCong Wang 				TASK_UNINTERRUPTIBLE);
368*200b916fSCong Wang 		unregistering = false;
369*200b916fSCong Wang 		rtnl_lock();
370*200b916fSCong Wang 		for_each_net(net) {
371*200b916fSCong Wang 			if (net->dev_unreg_count > 0) {
372*200b916fSCong Wang 				unregistering = true;
373*200b916fSCong Wang 				break;
374*200b916fSCong Wang 			}
375*200b916fSCong Wang 		}
376*200b916fSCong Wang 		if (!unregistering)
377*200b916fSCong Wang 			break;
378*200b916fSCong Wang 		__rtnl_unlock();
379*200b916fSCong Wang 		schedule();
380*200b916fSCong Wang 	}
381*200b916fSCong Wang 	finish_wait(&netdev_unregistering_wq, &wait);
382*200b916fSCong Wang }
383*200b916fSCong Wang 
38438f7b870SPatrick McHardy /**
38538f7b870SPatrick McHardy  * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
38638f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to unregister
38738f7b870SPatrick McHardy  */
38838f7b870SPatrick McHardy void rtnl_link_unregister(struct rtnl_link_ops *ops)
38938f7b870SPatrick McHardy {
390*200b916fSCong Wang 	/* Close the race with cleanup_net() */
391*200b916fSCong Wang 	mutex_lock(&net_mutex);
392*200b916fSCong Wang 	rtnl_lock_unregistering_all();
39338f7b870SPatrick McHardy 	__rtnl_link_unregister(ops);
39438f7b870SPatrick McHardy 	rtnl_unlock();
395*200b916fSCong Wang 	mutex_unlock(&net_mutex);
39638f7b870SPatrick McHardy }
39738f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_unregister);
39838f7b870SPatrick McHardy 
399ba7d49b1SJiri Pirko static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
400ba7d49b1SJiri Pirko {
401ba7d49b1SJiri Pirko 	struct net_device *master_dev;
402ba7d49b1SJiri Pirko 	const struct rtnl_link_ops *ops;
403ba7d49b1SJiri Pirko 
404ba7d49b1SJiri Pirko 	master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
405ba7d49b1SJiri Pirko 	if (!master_dev)
406ba7d49b1SJiri Pirko 		return 0;
407ba7d49b1SJiri Pirko 	ops = master_dev->rtnl_link_ops;
4086049f253SFernando Luis Vazquez Cao 	if (!ops || !ops->get_slave_size)
409ba7d49b1SJiri Pirko 		return 0;
410ba7d49b1SJiri Pirko 	/* IFLA_INFO_SLAVE_DATA + nested data */
411ba7d49b1SJiri Pirko 	return nla_total_size(sizeof(struct nlattr)) +
412ba7d49b1SJiri Pirko 	       ops->get_slave_size(master_dev, dev);
413ba7d49b1SJiri Pirko }
414ba7d49b1SJiri Pirko 
41538f7b870SPatrick McHardy static size_t rtnl_link_get_size(const struct net_device *dev)
41638f7b870SPatrick McHardy {
41738f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
41838f7b870SPatrick McHardy 	size_t size;
41938f7b870SPatrick McHardy 
42038f7b870SPatrick McHardy 	if (!ops)
42138f7b870SPatrick McHardy 		return 0;
42238f7b870SPatrick McHardy 
423369cf77aSThomas Graf 	size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
424369cf77aSThomas Graf 	       nla_total_size(strlen(ops->kind) + 1);  /* IFLA_INFO_KIND */
42538f7b870SPatrick McHardy 
42638f7b870SPatrick McHardy 	if (ops->get_size)
42738f7b870SPatrick McHardy 		/* IFLA_INFO_DATA + nested data */
428369cf77aSThomas Graf 		size += nla_total_size(sizeof(struct nlattr)) +
42938f7b870SPatrick McHardy 			ops->get_size(dev);
43038f7b870SPatrick McHardy 
43138f7b870SPatrick McHardy 	if (ops->get_xstats_size)
432369cf77aSThomas Graf 		/* IFLA_INFO_XSTATS */
433369cf77aSThomas Graf 		size += nla_total_size(ops->get_xstats_size(dev));
43438f7b870SPatrick McHardy 
435ba7d49b1SJiri Pirko 	size += rtnl_link_get_slave_info_data_size(dev);
436ba7d49b1SJiri Pirko 
43738f7b870SPatrick McHardy 	return size;
43838f7b870SPatrick McHardy }
43938f7b870SPatrick McHardy 
440f8ff182cSThomas Graf static LIST_HEAD(rtnl_af_ops);
441f8ff182cSThomas Graf 
442f8ff182cSThomas Graf static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
443f8ff182cSThomas Graf {
444f8ff182cSThomas Graf 	const struct rtnl_af_ops *ops;
445f8ff182cSThomas Graf 
446f8ff182cSThomas Graf 	list_for_each_entry(ops, &rtnl_af_ops, list) {
447f8ff182cSThomas Graf 		if (ops->family == family)
448f8ff182cSThomas Graf 			return ops;
449f8ff182cSThomas Graf 	}
450f8ff182cSThomas Graf 
451f8ff182cSThomas Graf 	return NULL;
452f8ff182cSThomas Graf }
453f8ff182cSThomas Graf 
454f8ff182cSThomas Graf /**
455f8ff182cSThomas Graf  * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
456f8ff182cSThomas Graf  * @ops: struct rtnl_af_ops * to register
457f8ff182cSThomas Graf  *
458f8ff182cSThomas Graf  * Returns 0 on success or a negative error code.
459f8ff182cSThomas Graf  */
4603678a9d8Sstephen hemminger void rtnl_af_register(struct rtnl_af_ops *ops)
461f8ff182cSThomas Graf {
462f8ff182cSThomas Graf 	rtnl_lock();
4633678a9d8Sstephen hemminger 	list_add_tail(&ops->list, &rtnl_af_ops);
464f8ff182cSThomas Graf 	rtnl_unlock();
465f8ff182cSThomas Graf }
466f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_register);
467f8ff182cSThomas Graf 
468f8ff182cSThomas Graf /**
469f8ff182cSThomas Graf  * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
470f8ff182cSThomas Graf  * @ops: struct rtnl_af_ops * to unregister
471f8ff182cSThomas Graf  *
472f8ff182cSThomas Graf  * The caller must hold the rtnl_mutex.
473f8ff182cSThomas Graf  */
474f8ff182cSThomas Graf void __rtnl_af_unregister(struct rtnl_af_ops *ops)
475f8ff182cSThomas Graf {
476f8ff182cSThomas Graf 	list_del(&ops->list);
477f8ff182cSThomas Graf }
478f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(__rtnl_af_unregister);
479f8ff182cSThomas Graf 
480f8ff182cSThomas Graf /**
481f8ff182cSThomas Graf  * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
482f8ff182cSThomas Graf  * @ops: struct rtnl_af_ops * to unregister
483f8ff182cSThomas Graf  */
484f8ff182cSThomas Graf void rtnl_af_unregister(struct rtnl_af_ops *ops)
485f8ff182cSThomas Graf {
486f8ff182cSThomas Graf 	rtnl_lock();
487f8ff182cSThomas Graf 	__rtnl_af_unregister(ops);
488f8ff182cSThomas Graf 	rtnl_unlock();
489f8ff182cSThomas Graf }
490f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_unregister);
491f8ff182cSThomas Graf 
492f8ff182cSThomas Graf static size_t rtnl_link_get_af_size(const struct net_device *dev)
493f8ff182cSThomas Graf {
494f8ff182cSThomas Graf 	struct rtnl_af_ops *af_ops;
495f8ff182cSThomas Graf 	size_t size;
496f8ff182cSThomas Graf 
497f8ff182cSThomas Graf 	/* IFLA_AF_SPEC */
498f8ff182cSThomas Graf 	size = nla_total_size(sizeof(struct nlattr));
499f8ff182cSThomas Graf 
500f8ff182cSThomas Graf 	list_for_each_entry(af_ops, &rtnl_af_ops, list) {
501f8ff182cSThomas Graf 		if (af_ops->get_link_af_size) {
502f8ff182cSThomas Graf 			/* AF_* + nested data */
503f8ff182cSThomas Graf 			size += nla_total_size(sizeof(struct nlattr)) +
504f8ff182cSThomas Graf 				af_ops->get_link_af_size(dev);
505f8ff182cSThomas Graf 		}
506f8ff182cSThomas Graf 	}
507f8ff182cSThomas Graf 
508f8ff182cSThomas Graf 	return size;
509f8ff182cSThomas Graf }
510f8ff182cSThomas Graf 
511ba7d49b1SJiri Pirko static bool rtnl_have_link_slave_info(const struct net_device *dev)
512ba7d49b1SJiri Pirko {
513ba7d49b1SJiri Pirko 	struct net_device *master_dev;
514ba7d49b1SJiri Pirko 
515ba7d49b1SJiri Pirko 	master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
516813f020cSJiri Pirko 	if (master_dev && master_dev->rtnl_link_ops)
517ba7d49b1SJiri Pirko 		return true;
518ba7d49b1SJiri Pirko 	return false;
519ba7d49b1SJiri Pirko }
520ba7d49b1SJiri Pirko 
521ba7d49b1SJiri Pirko static int rtnl_link_slave_info_fill(struct sk_buff *skb,
522ba7d49b1SJiri Pirko 				     const struct net_device *dev)
523ba7d49b1SJiri Pirko {
524ba7d49b1SJiri Pirko 	struct net_device *master_dev;
525ba7d49b1SJiri Pirko 	const struct rtnl_link_ops *ops;
526ba7d49b1SJiri Pirko 	struct nlattr *slave_data;
527ba7d49b1SJiri Pirko 	int err;
528ba7d49b1SJiri Pirko 
529ba7d49b1SJiri Pirko 	master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
530ba7d49b1SJiri Pirko 	if (!master_dev)
531ba7d49b1SJiri Pirko 		return 0;
532ba7d49b1SJiri Pirko 	ops = master_dev->rtnl_link_ops;
533ba7d49b1SJiri Pirko 	if (!ops)
534ba7d49b1SJiri Pirko 		return 0;
535ba7d49b1SJiri Pirko 	if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
536ba7d49b1SJiri Pirko 		return -EMSGSIZE;
537ba7d49b1SJiri Pirko 	if (ops->fill_slave_info) {
538ba7d49b1SJiri Pirko 		slave_data = nla_nest_start(skb, IFLA_INFO_SLAVE_DATA);
539ba7d49b1SJiri Pirko 		if (!slave_data)
540ba7d49b1SJiri Pirko 			return -EMSGSIZE;
541ba7d49b1SJiri Pirko 		err = ops->fill_slave_info(skb, master_dev, dev);
542ba7d49b1SJiri Pirko 		if (err < 0)
543ba7d49b1SJiri Pirko 			goto err_cancel_slave_data;
544ba7d49b1SJiri Pirko 		nla_nest_end(skb, slave_data);
545ba7d49b1SJiri Pirko 	}
546ba7d49b1SJiri Pirko 	return 0;
547ba7d49b1SJiri Pirko 
548ba7d49b1SJiri Pirko err_cancel_slave_data:
549ba7d49b1SJiri Pirko 	nla_nest_cancel(skb, slave_data);
550ba7d49b1SJiri Pirko 	return err;
551ba7d49b1SJiri Pirko }
552ba7d49b1SJiri Pirko 
553ba7d49b1SJiri Pirko static int rtnl_link_info_fill(struct sk_buff *skb,
554ba7d49b1SJiri Pirko 			       const struct net_device *dev)
55538f7b870SPatrick McHardy {
55638f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
557ba7d49b1SJiri Pirko 	struct nlattr *data;
558ba7d49b1SJiri Pirko 	int err;
559ba7d49b1SJiri Pirko 
560ba7d49b1SJiri Pirko 	if (!ops)
561ba7d49b1SJiri Pirko 		return 0;
562ba7d49b1SJiri Pirko 	if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
563ba7d49b1SJiri Pirko 		return -EMSGSIZE;
564ba7d49b1SJiri Pirko 	if (ops->fill_xstats) {
565ba7d49b1SJiri Pirko 		err = ops->fill_xstats(skb, dev);
566ba7d49b1SJiri Pirko 		if (err < 0)
567ba7d49b1SJiri Pirko 			return err;
568ba7d49b1SJiri Pirko 	}
569ba7d49b1SJiri Pirko 	if (ops->fill_info) {
570ba7d49b1SJiri Pirko 		data = nla_nest_start(skb, IFLA_INFO_DATA);
571ba7d49b1SJiri Pirko 		if (data == NULL)
572ba7d49b1SJiri Pirko 			return -EMSGSIZE;
573ba7d49b1SJiri Pirko 		err = ops->fill_info(skb, dev);
574ba7d49b1SJiri Pirko 		if (err < 0)
575ba7d49b1SJiri Pirko 			goto err_cancel_data;
576ba7d49b1SJiri Pirko 		nla_nest_end(skb, data);
577ba7d49b1SJiri Pirko 	}
578ba7d49b1SJiri Pirko 	return 0;
579ba7d49b1SJiri Pirko 
580ba7d49b1SJiri Pirko err_cancel_data:
581ba7d49b1SJiri Pirko 	nla_nest_cancel(skb, data);
582ba7d49b1SJiri Pirko 	return err;
583ba7d49b1SJiri Pirko }
584ba7d49b1SJiri Pirko 
585ba7d49b1SJiri Pirko static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
586ba7d49b1SJiri Pirko {
587ba7d49b1SJiri Pirko 	struct nlattr *linkinfo;
58838f7b870SPatrick McHardy 	int err = -EMSGSIZE;
58938f7b870SPatrick McHardy 
59038f7b870SPatrick McHardy 	linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
59138f7b870SPatrick McHardy 	if (linkinfo == NULL)
59238f7b870SPatrick McHardy 		goto out;
59338f7b870SPatrick McHardy 
594ba7d49b1SJiri Pirko 	err = rtnl_link_info_fill(skb, dev);
59538f7b870SPatrick McHardy 	if (err < 0)
59638f7b870SPatrick McHardy 		goto err_cancel_link;
597ba7d49b1SJiri Pirko 
598ba7d49b1SJiri Pirko 	err = rtnl_link_slave_info_fill(skb, dev);
59938f7b870SPatrick McHardy 	if (err < 0)
600ba7d49b1SJiri Pirko 		goto err_cancel_link;
60138f7b870SPatrick McHardy 
60238f7b870SPatrick McHardy 	nla_nest_end(skb, linkinfo);
60338f7b870SPatrick McHardy 	return 0;
60438f7b870SPatrick McHardy 
60538f7b870SPatrick McHardy err_cancel_link:
60638f7b870SPatrick McHardy 	nla_nest_cancel(skb, linkinfo);
60738f7b870SPatrick McHardy out:
60838f7b870SPatrick McHardy 	return err;
60938f7b870SPatrick McHardy }
61038f7b870SPatrick McHardy 
61195c96174SEric Dumazet int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
6121da177e4SLinus Torvalds {
61397c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
6141da177e4SLinus Torvalds 	int err = 0;
6151da177e4SLinus Torvalds 
616ac6d439dSPatrick McHardy 	NETLINK_CB(skb).dst_group = group;
6171da177e4SLinus Torvalds 	if (echo)
6181da177e4SLinus Torvalds 		atomic_inc(&skb->users);
6191da177e4SLinus Torvalds 	netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
6201da177e4SLinus Torvalds 	if (echo)
6211da177e4SLinus Torvalds 		err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
6221da177e4SLinus Torvalds 	return err;
6231da177e4SLinus Torvalds }
6241da177e4SLinus Torvalds 
62597c53cacSDenis V. Lunev int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
6262942e900SThomas Graf {
62797c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
62897c53cacSDenis V. Lunev 
6292942e900SThomas Graf 	return nlmsg_unicast(rtnl, skb, pid);
6302942e900SThomas Graf }
631e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unicast);
6322942e900SThomas Graf 
6331ce85fe4SPablo Neira Ayuso void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
63497676b6bSThomas Graf 		 struct nlmsghdr *nlh, gfp_t flags)
63597676b6bSThomas Graf {
63697c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
63797676b6bSThomas Graf 	int report = 0;
63897676b6bSThomas Graf 
63997676b6bSThomas Graf 	if (nlh)
64097676b6bSThomas Graf 		report = nlmsg_report(nlh);
64197676b6bSThomas Graf 
6421ce85fe4SPablo Neira Ayuso 	nlmsg_notify(rtnl, skb, pid, group, report, flags);
64397676b6bSThomas Graf }
644e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_notify);
64597676b6bSThomas Graf 
64697c53cacSDenis V. Lunev void rtnl_set_sk_err(struct net *net, u32 group, int error)
64797676b6bSThomas Graf {
64897c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
64997c53cacSDenis V. Lunev 
65097676b6bSThomas Graf 	netlink_set_err(rtnl, 0, group, error);
65197676b6bSThomas Graf }
652e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_set_sk_err);
65397676b6bSThomas Graf 
6541da177e4SLinus Torvalds int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
6551da177e4SLinus Torvalds {
6562d7202bfSThomas Graf 	struct nlattr *mx;
6572d7202bfSThomas Graf 	int i, valid = 0;
6581da177e4SLinus Torvalds 
6592d7202bfSThomas Graf 	mx = nla_nest_start(skb, RTA_METRICS);
6602d7202bfSThomas Graf 	if (mx == NULL)
6612d7202bfSThomas Graf 		return -ENOBUFS;
6622d7202bfSThomas Graf 
6631da177e4SLinus Torvalds 	for (i = 0; i < RTAX_MAX; i++) {
6642d7202bfSThomas Graf 		if (metrics[i]) {
6652d7202bfSThomas Graf 			valid++;
666a6574349SDavid S. Miller 			if (nla_put_u32(skb, i+1, metrics[i]))
667a6574349SDavid S. Miller 				goto nla_put_failure;
6681da177e4SLinus Torvalds 		}
6692d7202bfSThomas Graf 	}
6701da177e4SLinus Torvalds 
671a57d27fcSDavid S. Miller 	if (!valid) {
672a57d27fcSDavid S. Miller 		nla_nest_cancel(skb, mx);
673a57d27fcSDavid S. Miller 		return 0;
674a57d27fcSDavid S. Miller 	}
6752d7202bfSThomas Graf 
6762d7202bfSThomas Graf 	return nla_nest_end(skb, mx);
6772d7202bfSThomas Graf 
6782d7202bfSThomas Graf nla_put_failure:
679bc3ed28cSThomas Graf 	nla_nest_cancel(skb, mx);
680bc3ed28cSThomas Graf 	return -EMSGSIZE;
6811da177e4SLinus Torvalds }
682e0d087afSEric Dumazet EXPORT_SYMBOL(rtnetlink_put_metrics);
6831da177e4SLinus Torvalds 
684e3703b3dSThomas Graf int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
68587a50699SDavid S. Miller 		       long expires, u32 error)
686e3703b3dSThomas Graf {
687e3703b3dSThomas Graf 	struct rta_cacheinfo ci = {
688a399a805SEric Dumazet 		.rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse),
689e3703b3dSThomas Graf 		.rta_used = dst->__use,
690e3703b3dSThomas Graf 		.rta_clntref = atomic_read(&(dst->__refcnt)),
691e3703b3dSThomas Graf 		.rta_error = error,
692e3703b3dSThomas Graf 		.rta_id =  id,
693e3703b3dSThomas Graf 	};
694e3703b3dSThomas Graf 
6958253947eSLi Wei 	if (expires) {
6968253947eSLi Wei 		unsigned long clock;
697e3703b3dSThomas Graf 
6988253947eSLi Wei 		clock = jiffies_to_clock_t(abs(expires));
6998253947eSLi Wei 		clock = min_t(unsigned long, clock, INT_MAX);
7008253947eSLi Wei 		ci.rta_expires = (expires > 0) ? clock : -clock;
7018253947eSLi Wei 	}
702e3703b3dSThomas Graf 	return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
703e3703b3dSThomas Graf }
704e3703b3dSThomas Graf EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
7051da177e4SLinus Torvalds 
70693b2d4a2SDavid S. Miller static void set_operstate(struct net_device *dev, unsigned char transition)
707b00055aaSStefan Rompf {
708b00055aaSStefan Rompf 	unsigned char operstate = dev->operstate;
709b00055aaSStefan Rompf 
710b00055aaSStefan Rompf 	switch (transition) {
711b00055aaSStefan Rompf 	case IF_OPER_UP:
712b00055aaSStefan Rompf 		if ((operstate == IF_OPER_DORMANT ||
713b00055aaSStefan Rompf 		     operstate == IF_OPER_UNKNOWN) &&
714b00055aaSStefan Rompf 		    !netif_dormant(dev))
715b00055aaSStefan Rompf 			operstate = IF_OPER_UP;
716b00055aaSStefan Rompf 		break;
717b00055aaSStefan Rompf 
718b00055aaSStefan Rompf 	case IF_OPER_DORMANT:
719b00055aaSStefan Rompf 		if (operstate == IF_OPER_UP ||
720b00055aaSStefan Rompf 		    operstate == IF_OPER_UNKNOWN)
721b00055aaSStefan Rompf 			operstate = IF_OPER_DORMANT;
722b00055aaSStefan Rompf 		break;
7233ff50b79SStephen Hemminger 	}
724b00055aaSStefan Rompf 
725b00055aaSStefan Rompf 	if (dev->operstate != operstate) {
726b00055aaSStefan Rompf 		write_lock_bh(&dev_base_lock);
727b00055aaSStefan Rompf 		dev->operstate = operstate;
728b00055aaSStefan Rompf 		write_unlock_bh(&dev_base_lock);
729b00055aaSStefan Rompf 		netdev_state_change(dev);
73093b2d4a2SDavid S. Miller 	}
731b00055aaSStefan Rompf }
732b00055aaSStefan Rompf 
733b1beb681SJiri Benc static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
734b1beb681SJiri Benc {
735b1beb681SJiri Benc 	return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
736b1beb681SJiri Benc 	       (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
737b1beb681SJiri Benc }
738b1beb681SJiri Benc 
7393729d502SPatrick McHardy static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
7403729d502SPatrick McHardy 					   const struct ifinfomsg *ifm)
7413729d502SPatrick McHardy {
7423729d502SPatrick McHardy 	unsigned int flags = ifm->ifi_flags;
7433729d502SPatrick McHardy 
7443729d502SPatrick McHardy 	/* bugwards compatibility: ifi_change == 0 is treated as ~0 */
7453729d502SPatrick McHardy 	if (ifm->ifi_change)
7463729d502SPatrick McHardy 		flags = (flags & ifm->ifi_change) |
747b1beb681SJiri Benc 			(rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
7483729d502SPatrick McHardy 
7493729d502SPatrick McHardy 	return flags;
7503729d502SPatrick McHardy }
7513729d502SPatrick McHardy 
752b60c5115SThomas Graf static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
753be1f3c2cSBen Hutchings 				 const struct rtnl_link_stats64 *b)
7541da177e4SLinus Torvalds {
755b60c5115SThomas Graf 	a->rx_packets = b->rx_packets;
756b60c5115SThomas Graf 	a->tx_packets = b->tx_packets;
757b60c5115SThomas Graf 	a->rx_bytes = b->rx_bytes;
758b60c5115SThomas Graf 	a->tx_bytes = b->tx_bytes;
759b60c5115SThomas Graf 	a->rx_errors = b->rx_errors;
760b60c5115SThomas Graf 	a->tx_errors = b->tx_errors;
761b60c5115SThomas Graf 	a->rx_dropped = b->rx_dropped;
762b60c5115SThomas Graf 	a->tx_dropped = b->tx_dropped;
763b60c5115SThomas Graf 
764b60c5115SThomas Graf 	a->multicast = b->multicast;
765b60c5115SThomas Graf 	a->collisions = b->collisions;
766b60c5115SThomas Graf 
767b60c5115SThomas Graf 	a->rx_length_errors = b->rx_length_errors;
768b60c5115SThomas Graf 	a->rx_over_errors = b->rx_over_errors;
769b60c5115SThomas Graf 	a->rx_crc_errors = b->rx_crc_errors;
770b60c5115SThomas Graf 	a->rx_frame_errors = b->rx_frame_errors;
771b60c5115SThomas Graf 	a->rx_fifo_errors = b->rx_fifo_errors;
772b60c5115SThomas Graf 	a->rx_missed_errors = b->rx_missed_errors;
773b60c5115SThomas Graf 
774b60c5115SThomas Graf 	a->tx_aborted_errors = b->tx_aborted_errors;
775b60c5115SThomas Graf 	a->tx_carrier_errors = b->tx_carrier_errors;
776b60c5115SThomas Graf 	a->tx_fifo_errors = b->tx_fifo_errors;
777b60c5115SThomas Graf 	a->tx_heartbeat_errors = b->tx_heartbeat_errors;
778b60c5115SThomas Graf 	a->tx_window_errors = b->tx_window_errors;
779b60c5115SThomas Graf 
780b60c5115SThomas Graf 	a->rx_compressed = b->rx_compressed;
781b60c5115SThomas Graf 	a->tx_compressed = b->tx_compressed;
78210708f37SJan Engelhardt }
78310708f37SJan Engelhardt 
784be1f3c2cSBen Hutchings static void copy_rtnl_link_stats64(void *v, const struct rtnl_link_stats64 *b)
78510708f37SJan Engelhardt {
786afdcba37SEric Dumazet 	memcpy(v, b, sizeof(*b));
78710708f37SJan Engelhardt }
788b60c5115SThomas Graf 
789c02db8c6SChris Wright /* All VF info */
790115c9b81SGreg Rose static inline int rtnl_vfinfo_size(const struct net_device *dev,
791115c9b81SGreg Rose 				   u32 ext_filter_mask)
792ebc08a6fSWilliams, Mitch A {
793115c9b81SGreg Rose 	if (dev->dev.parent && dev_is_pci(dev->dev.parent) &&
794115c9b81SGreg Rose 	    (ext_filter_mask & RTEXT_FILTER_VF)) {
795c02db8c6SChris Wright 		int num_vfs = dev_num_vf(dev->dev.parent);
796045de01aSScott Feldman 		size_t size = nla_total_size(sizeof(struct nlattr));
797045de01aSScott Feldman 		size += nla_total_size(num_vfs * sizeof(struct nlattr));
798045de01aSScott Feldman 		size += num_vfs *
799045de01aSScott Feldman 			(nla_total_size(sizeof(struct ifla_vf_mac)) +
800045de01aSScott Feldman 			 nla_total_size(sizeof(struct ifla_vf_vlan)) +
8015f8444a3SGreg Rose 			 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
8025f8444a3SGreg Rose 			 nla_total_size(sizeof(struct ifla_vf_spoofchk)));
803c02db8c6SChris Wright 		return size;
804c02db8c6SChris Wright 	} else
805ebc08a6fSWilliams, Mitch A 		return 0;
806ebc08a6fSWilliams, Mitch A }
807ebc08a6fSWilliams, Mitch A 
808c53864fdSDavid Gibson static size_t rtnl_port_size(const struct net_device *dev,
809c53864fdSDavid Gibson 			     u32 ext_filter_mask)
81057b61080SScott Feldman {
81157b61080SScott Feldman 	size_t port_size = nla_total_size(4)		/* PORT_VF */
81257b61080SScott Feldman 		+ nla_total_size(PORT_PROFILE_MAX)	/* PORT_PROFILE */
81357b61080SScott Feldman 		+ nla_total_size(sizeof(struct ifla_port_vsi))
81457b61080SScott Feldman 							/* PORT_VSI_TYPE */
81557b61080SScott Feldman 		+ nla_total_size(PORT_UUID_MAX)		/* PORT_INSTANCE_UUID */
81657b61080SScott Feldman 		+ nla_total_size(PORT_UUID_MAX)		/* PORT_HOST_UUID */
81757b61080SScott Feldman 		+ nla_total_size(1)			/* PROT_VDP_REQUEST */
81857b61080SScott Feldman 		+ nla_total_size(2);			/* PORT_VDP_RESPONSE */
81957b61080SScott Feldman 	size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
82057b61080SScott Feldman 	size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
82157b61080SScott Feldman 		+ port_size;
82257b61080SScott Feldman 	size_t port_self_size = nla_total_size(sizeof(struct nlattr))
82357b61080SScott Feldman 		+ port_size;
82457b61080SScott Feldman 
825c53864fdSDavid Gibson 	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
826c53864fdSDavid Gibson 	    !(ext_filter_mask & RTEXT_FILTER_VF))
82757b61080SScott Feldman 		return 0;
82857b61080SScott Feldman 	if (dev_num_vf(dev->dev.parent))
82957b61080SScott Feldman 		return port_self_size + vf_ports_size +
83057b61080SScott Feldman 			vf_port_size * dev_num_vf(dev->dev.parent);
83157b61080SScott Feldman 	else
83257b61080SScott Feldman 		return port_self_size;
83357b61080SScott Feldman }
83457b61080SScott Feldman 
835115c9b81SGreg Rose static noinline size_t if_nlmsg_size(const struct net_device *dev,
836115c9b81SGreg Rose 				     u32 ext_filter_mask)
837339bf98fSThomas Graf {
838339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
839339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
8400b815a1aSStephen Hemminger 	       + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
841339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
842339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rtnl_link_ifmap))
843339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rtnl_link_stats))
844adcfe196SJan Engelhardt 	       + nla_total_size(sizeof(struct rtnl_link_stats64))
845339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
846339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
847339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_TXQLEN */
848339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_WEIGHT */
849339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MTU */
850339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_LINK */
851339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MASTER */
8529a57247fSJiri Pirko 	       + nla_total_size(1) /* IFLA_CARRIER */
853edbc0bb3SBen Greear 	       + nla_total_size(4) /* IFLA_PROMISCUITY */
85476ff5cc9SJiri Pirko 	       + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
85576ff5cc9SJiri Pirko 	       + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
856339bf98fSThomas Graf 	       + nla_total_size(1) /* IFLA_OPERSTATE */
85738f7b870SPatrick McHardy 	       + nla_total_size(1) /* IFLA_LINKMODE */
8582d3b479dSdavid decotigny 	       + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
859115c9b81SGreg Rose 	       + nla_total_size(ext_filter_mask
860115c9b81SGreg Rose 			        & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
861115c9b81SGreg Rose 	       + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
862c53864fdSDavid Gibson 	       + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
863f8ff182cSThomas Graf 	       + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
86466cae9edSJiri Pirko 	       + rtnl_link_get_af_size(dev) /* IFLA_AF_SPEC */
86566cae9edSJiri Pirko 	       + nla_total_size(MAX_PHYS_PORT_ID_LEN); /* IFLA_PHYS_PORT_ID */
866339bf98fSThomas Graf }
867339bf98fSThomas Graf 
86857b61080SScott Feldman static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
86957b61080SScott Feldman {
87057b61080SScott Feldman 	struct nlattr *vf_ports;
87157b61080SScott Feldman 	struct nlattr *vf_port;
87257b61080SScott Feldman 	int vf;
87357b61080SScott Feldman 	int err;
87457b61080SScott Feldman 
87557b61080SScott Feldman 	vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
87657b61080SScott Feldman 	if (!vf_ports)
87757b61080SScott Feldman 		return -EMSGSIZE;
87857b61080SScott Feldman 
87957b61080SScott Feldman 	for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
88057b61080SScott Feldman 		vf_port = nla_nest_start(skb, IFLA_VF_PORT);
8818ca94183SScott Feldman 		if (!vf_port)
8828ca94183SScott Feldman 			goto nla_put_failure;
883a6574349SDavid S. Miller 		if (nla_put_u32(skb, IFLA_PORT_VF, vf))
884a6574349SDavid S. Miller 			goto nla_put_failure;
88557b61080SScott Feldman 		err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
8868ca94183SScott Feldman 		if (err == -EMSGSIZE)
8878ca94183SScott Feldman 			goto nla_put_failure;
88857b61080SScott Feldman 		if (err) {
88957b61080SScott Feldman 			nla_nest_cancel(skb, vf_port);
89057b61080SScott Feldman 			continue;
89157b61080SScott Feldman 		}
89257b61080SScott Feldman 		nla_nest_end(skb, vf_port);
89357b61080SScott Feldman 	}
89457b61080SScott Feldman 
89557b61080SScott Feldman 	nla_nest_end(skb, vf_ports);
89657b61080SScott Feldman 
89757b61080SScott Feldman 	return 0;
8988ca94183SScott Feldman 
8998ca94183SScott Feldman nla_put_failure:
9008ca94183SScott Feldman 	nla_nest_cancel(skb, vf_ports);
9018ca94183SScott Feldman 	return -EMSGSIZE;
90257b61080SScott Feldman }
90357b61080SScott Feldman 
90457b61080SScott Feldman static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
90557b61080SScott Feldman {
90657b61080SScott Feldman 	struct nlattr *port_self;
90757b61080SScott Feldman 	int err;
90857b61080SScott Feldman 
90957b61080SScott Feldman 	port_self = nla_nest_start(skb, IFLA_PORT_SELF);
91057b61080SScott Feldman 	if (!port_self)
91157b61080SScott Feldman 		return -EMSGSIZE;
91257b61080SScott Feldman 
91357b61080SScott Feldman 	err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
91457b61080SScott Feldman 	if (err) {
91557b61080SScott Feldman 		nla_nest_cancel(skb, port_self);
9168ca94183SScott Feldman 		return (err == -EMSGSIZE) ? err : 0;
91757b61080SScott Feldman 	}
91857b61080SScott Feldman 
91957b61080SScott Feldman 	nla_nest_end(skb, port_self);
92057b61080SScott Feldman 
92157b61080SScott Feldman 	return 0;
92257b61080SScott Feldman }
92357b61080SScott Feldman 
924c53864fdSDavid Gibson static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
925c53864fdSDavid Gibson 			  u32 ext_filter_mask)
92657b61080SScott Feldman {
92757b61080SScott Feldman 	int err;
92857b61080SScott Feldman 
929c53864fdSDavid Gibson 	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
930c53864fdSDavid Gibson 	    !(ext_filter_mask & RTEXT_FILTER_VF))
93157b61080SScott Feldman 		return 0;
93257b61080SScott Feldman 
93357b61080SScott Feldman 	err = rtnl_port_self_fill(skb, dev);
93457b61080SScott Feldman 	if (err)
93557b61080SScott Feldman 		return err;
93657b61080SScott Feldman 
93757b61080SScott Feldman 	if (dev_num_vf(dev->dev.parent)) {
93857b61080SScott Feldman 		err = rtnl_vf_ports_fill(skb, dev);
93957b61080SScott Feldman 		if (err)
94057b61080SScott Feldman 			return err;
94157b61080SScott Feldman 	}
94257b61080SScott Feldman 
94357b61080SScott Feldman 	return 0;
94457b61080SScott Feldman }
94557b61080SScott Feldman 
94666cae9edSJiri Pirko static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
94766cae9edSJiri Pirko {
94866cae9edSJiri Pirko 	int err;
94966cae9edSJiri Pirko 	struct netdev_phys_port_id ppid;
95066cae9edSJiri Pirko 
95166cae9edSJiri Pirko 	err = dev_get_phys_port_id(dev, &ppid);
95266cae9edSJiri Pirko 	if (err) {
95366cae9edSJiri Pirko 		if (err == -EOPNOTSUPP)
95466cae9edSJiri Pirko 			return 0;
95566cae9edSJiri Pirko 		return err;
95666cae9edSJiri Pirko 	}
95766cae9edSJiri Pirko 
95866cae9edSJiri Pirko 	if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
95966cae9edSJiri Pirko 		return -EMSGSIZE;
96066cae9edSJiri Pirko 
96166cae9edSJiri Pirko 	return 0;
96266cae9edSJiri Pirko }
96366cae9edSJiri Pirko 
964b60c5115SThomas Graf static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
965575c3e2aSPatrick McHardy 			    int type, u32 pid, u32 seq, u32 change,
966115c9b81SGreg Rose 			    unsigned int flags, u32 ext_filter_mask)
967b60c5115SThomas Graf {
968b60c5115SThomas Graf 	struct ifinfomsg *ifm;
9691da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
97028172739SEric Dumazet 	struct rtnl_link_stats64 temp;
971be1f3c2cSBen Hutchings 	const struct rtnl_link_stats64 *stats;
972f8ff182cSThomas Graf 	struct nlattr *attr, *af_spec;
973f8ff182cSThomas Graf 	struct rtnl_af_ops *af_ops;
974898e5061SJiri Pirko 	struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
9751da177e4SLinus Torvalds 
9762907c35fSEric Dumazet 	ASSERT_RTNL();
977b60c5115SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
978b60c5115SThomas Graf 	if (nlh == NULL)
97926932566SPatrick McHardy 		return -EMSGSIZE;
9801da177e4SLinus Torvalds 
981b60c5115SThomas Graf 	ifm = nlmsg_data(nlh);
982b60c5115SThomas Graf 	ifm->ifi_family = AF_UNSPEC;
983b60c5115SThomas Graf 	ifm->__ifi_pad = 0;
984b60c5115SThomas Graf 	ifm->ifi_type = dev->type;
985b60c5115SThomas Graf 	ifm->ifi_index = dev->ifindex;
986b60c5115SThomas Graf 	ifm->ifi_flags = dev_get_flags(dev);
987b60c5115SThomas Graf 	ifm->ifi_change = change;
9881da177e4SLinus Torvalds 
989a6574349SDavid S. Miller 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
990a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
991a6574349SDavid S. Miller 	    nla_put_u8(skb, IFLA_OPERSTATE,
992a6574349SDavid S. Miller 		       netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
993a6574349SDavid S. Miller 	    nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
994a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
995a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_GROUP, dev->group) ||
996edbc0bb3SBen Greear 	    nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
99776ff5cc9SJiri Pirko 	    nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
9981d69c2b3SMark A. Greer #ifdef CONFIG_RPS
99976ff5cc9SJiri Pirko 	    nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
10001d69c2b3SMark A. Greer #endif
1001a6574349SDavid S. Miller 	    (dev->ifindex != dev->iflink &&
1002a6574349SDavid S. Miller 	     nla_put_u32(skb, IFLA_LINK, dev->iflink)) ||
1003898e5061SJiri Pirko 	    (upper_dev &&
1004898e5061SJiri Pirko 	     nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) ||
10059a57247fSJiri Pirko 	    nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
1006a6574349SDavid S. Miller 	    (dev->qdisc &&
1007a6574349SDavid S. Miller 	     nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
1008a6574349SDavid S. Miller 	    (dev->ifalias &&
10092d3b479dSdavid decotigny 	     nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)) ||
10102d3b479dSdavid decotigny 	    nla_put_u32(skb, IFLA_CARRIER_CHANGES,
10112d3b479dSdavid decotigny 			atomic_read(&dev->carrier_changes)))
1012a6574349SDavid S. Miller 		goto nla_put_failure;
10130b815a1aSStephen Hemminger 
1014b00055aaSStefan Rompf 	if (1) {
10151da177e4SLinus Torvalds 		struct rtnl_link_ifmap map = {
10161da177e4SLinus Torvalds 			.mem_start   = dev->mem_start,
10171da177e4SLinus Torvalds 			.mem_end     = dev->mem_end,
10181da177e4SLinus Torvalds 			.base_addr   = dev->base_addr,
10191da177e4SLinus Torvalds 			.irq         = dev->irq,
10201da177e4SLinus Torvalds 			.dma         = dev->dma,
10211da177e4SLinus Torvalds 			.port        = dev->if_port,
10221da177e4SLinus Torvalds 		};
1023a6574349SDavid S. Miller 		if (nla_put(skb, IFLA_MAP, sizeof(map), &map))
1024a6574349SDavid S. Miller 			goto nla_put_failure;
10251da177e4SLinus Torvalds 	}
10261da177e4SLinus Torvalds 
10271da177e4SLinus Torvalds 	if (dev->addr_len) {
1028a6574349SDavid S. Miller 		if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1029a6574349SDavid S. Miller 		    nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1030a6574349SDavid S. Miller 			goto nla_put_failure;
10311da177e4SLinus Torvalds 	}
10321da177e4SLinus Torvalds 
103366cae9edSJiri Pirko 	if (rtnl_phys_port_id_fill(skb, dev))
103466cae9edSJiri Pirko 		goto nla_put_failure;
103566cae9edSJiri Pirko 
1036b60c5115SThomas Graf 	attr = nla_reserve(skb, IFLA_STATS,
1037b60c5115SThomas Graf 			sizeof(struct rtnl_link_stats));
1038b60c5115SThomas Graf 	if (attr == NULL)
1039b60c5115SThomas Graf 		goto nla_put_failure;
1040b60c5115SThomas Graf 
104128172739SEric Dumazet 	stats = dev_get_stats(dev, &temp);
1042b60c5115SThomas Graf 	copy_rtnl_link_stats(nla_data(attr), stats);
10431da177e4SLinus Torvalds 
104410708f37SJan Engelhardt 	attr = nla_reserve(skb, IFLA_STATS64,
104510708f37SJan Engelhardt 			sizeof(struct rtnl_link_stats64));
104610708f37SJan Engelhardt 	if (attr == NULL)
104710708f37SJan Engelhardt 		goto nla_put_failure;
104810708f37SJan Engelhardt 	copy_rtnl_link_stats64(nla_data(attr), stats);
104910708f37SJan Engelhardt 
1050a6574349SDavid S. Miller 	if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) &&
1051a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)))
1052a6574349SDavid S. Miller 		goto nla_put_failure;
105357b61080SScott Feldman 
1054115c9b81SGreg Rose 	if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent
1055115c9b81SGreg Rose 	    && (ext_filter_mask & RTEXT_FILTER_VF)) {
1056ebc08a6fSWilliams, Mitch A 		int i;
1057ebc08a6fSWilliams, Mitch A 
1058c02db8c6SChris Wright 		struct nlattr *vfinfo, *vf;
1059c02db8c6SChris Wright 		int num_vfs = dev_num_vf(dev->dev.parent);
1060c02db8c6SChris Wright 
1061c02db8c6SChris Wright 		vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1062c02db8c6SChris Wright 		if (!vfinfo)
1063c02db8c6SChris Wright 			goto nla_put_failure;
1064c02db8c6SChris Wright 		for (i = 0; i < num_vfs; i++) {
1065c02db8c6SChris Wright 			struct ifla_vf_info ivi;
1066c02db8c6SChris Wright 			struct ifla_vf_mac vf_mac;
1067c02db8c6SChris Wright 			struct ifla_vf_vlan vf_vlan;
1068c02db8c6SChris Wright 			struct ifla_vf_tx_rate vf_tx_rate;
10695f8444a3SGreg Rose 			struct ifla_vf_spoofchk vf_spoofchk;
10701d8faf48SRony Efraim 			struct ifla_vf_link_state vf_linkstate;
10715f8444a3SGreg Rose 
10725f8444a3SGreg Rose 			/*
10735f8444a3SGreg Rose 			 * Not all SR-IOV capable drivers support the
10745f8444a3SGreg Rose 			 * spoofcheck query.  Preset to -1 so the user
10755f8444a3SGreg Rose 			 * space tool can detect that the driver didn't
10765f8444a3SGreg Rose 			 * report anything.
10775f8444a3SGreg Rose 			 */
10785f8444a3SGreg Rose 			ivi.spoofchk = -1;
107984d73cd3SMathias Krause 			memset(ivi.mac, 0, sizeof(ivi.mac));
10801d8faf48SRony Efraim 			/* The default value for VF link state is "auto"
10811d8faf48SRony Efraim 			 * IFLA_VF_LINK_STATE_AUTO which equals zero
10821d8faf48SRony Efraim 			 */
10831d8faf48SRony Efraim 			ivi.linkstate = 0;
1084ebc08a6fSWilliams, Mitch A 			if (dev->netdev_ops->ndo_get_vf_config(dev, i, &ivi))
1085ebc08a6fSWilliams, Mitch A 				break;
10865f8444a3SGreg Rose 			vf_mac.vf =
10875f8444a3SGreg Rose 				vf_vlan.vf =
10885f8444a3SGreg Rose 				vf_tx_rate.vf =
10891d8faf48SRony Efraim 				vf_spoofchk.vf =
10901d8faf48SRony Efraim 				vf_linkstate.vf = ivi.vf;
10915f8444a3SGreg Rose 
1092c02db8c6SChris Wright 			memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1093c02db8c6SChris Wright 			vf_vlan.vlan = ivi.vlan;
1094c02db8c6SChris Wright 			vf_vlan.qos = ivi.qos;
1095c02db8c6SChris Wright 			vf_tx_rate.rate = ivi.tx_rate;
10965f8444a3SGreg Rose 			vf_spoofchk.setting = ivi.spoofchk;
10971d8faf48SRony Efraim 			vf_linkstate.link_state = ivi.linkstate;
1098c02db8c6SChris Wright 			vf = nla_nest_start(skb, IFLA_VF_INFO);
1099c02db8c6SChris Wright 			if (!vf) {
1100c02db8c6SChris Wright 				nla_nest_cancel(skb, vfinfo);
1101c02db8c6SChris Wright 				goto nla_put_failure;
1102ebc08a6fSWilliams, Mitch A 			}
1103a6574349SDavid S. Miller 			if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1104a6574349SDavid S. Miller 			    nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1105a6574349SDavid S. Miller 			    nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1106a6574349SDavid S. Miller 				    &vf_tx_rate) ||
1107a6574349SDavid S. Miller 			    nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
11081d8faf48SRony Efraim 				    &vf_spoofchk) ||
11091d8faf48SRony Efraim 			    nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
11101d8faf48SRony Efraim 				    &vf_linkstate))
1111a6574349SDavid S. Miller 				goto nla_put_failure;
1112c02db8c6SChris Wright 			nla_nest_end(skb, vf);
1113c02db8c6SChris Wright 		}
1114c02db8c6SChris Wright 		nla_nest_end(skb, vfinfo);
1115ebc08a6fSWilliams, Mitch A 	}
111657b61080SScott Feldman 
1117c53864fdSDavid Gibson 	if (rtnl_port_fill(skb, dev, ext_filter_mask))
111857b61080SScott Feldman 		goto nla_put_failure;
111957b61080SScott Feldman 
1120ba7d49b1SJiri Pirko 	if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
112138f7b870SPatrick McHardy 		if (rtnl_link_fill(skb, dev) < 0)
112238f7b870SPatrick McHardy 			goto nla_put_failure;
112338f7b870SPatrick McHardy 	}
112438f7b870SPatrick McHardy 
1125f8ff182cSThomas Graf 	if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
1126f8ff182cSThomas Graf 		goto nla_put_failure;
1127f8ff182cSThomas Graf 
1128f8ff182cSThomas Graf 	list_for_each_entry(af_ops, &rtnl_af_ops, list) {
1129f8ff182cSThomas Graf 		if (af_ops->fill_link_af) {
1130f8ff182cSThomas Graf 			struct nlattr *af;
1131f8ff182cSThomas Graf 			int err;
1132f8ff182cSThomas Graf 
1133f8ff182cSThomas Graf 			if (!(af = nla_nest_start(skb, af_ops->family)))
1134f8ff182cSThomas Graf 				goto nla_put_failure;
1135f8ff182cSThomas Graf 
1136f8ff182cSThomas Graf 			err = af_ops->fill_link_af(skb, dev);
1137f8ff182cSThomas Graf 
1138f8ff182cSThomas Graf 			/*
1139f8ff182cSThomas Graf 			 * Caller may return ENODATA to indicate that there
1140f8ff182cSThomas Graf 			 * was no data to be dumped. This is not an error, it
1141f8ff182cSThomas Graf 			 * means we should trim the attribute header and
1142f8ff182cSThomas Graf 			 * continue.
1143f8ff182cSThomas Graf 			 */
1144f8ff182cSThomas Graf 			if (err == -ENODATA)
1145f8ff182cSThomas Graf 				nla_nest_cancel(skb, af);
1146f8ff182cSThomas Graf 			else if (err < 0)
1147f8ff182cSThomas Graf 				goto nla_put_failure;
1148f8ff182cSThomas Graf 
1149f8ff182cSThomas Graf 			nla_nest_end(skb, af);
1150f8ff182cSThomas Graf 		}
1151f8ff182cSThomas Graf 	}
1152f8ff182cSThomas Graf 
1153f8ff182cSThomas Graf 	nla_nest_end(skb, af_spec);
1154f8ff182cSThomas Graf 
1155b60c5115SThomas Graf 	return nlmsg_end(skb, nlh);
1156b60c5115SThomas Graf 
1157b60c5115SThomas Graf nla_put_failure:
115826932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
115926932566SPatrick McHardy 	return -EMSGSIZE;
11601da177e4SLinus Torvalds }
11611da177e4SLinus Torvalds 
1162f7b12606SJiri Pirko static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1163f7b12606SJiri Pirko 	[IFLA_IFNAME]		= { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1164f7b12606SJiri Pirko 	[IFLA_ADDRESS]		= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1165f7b12606SJiri Pirko 	[IFLA_BROADCAST]	= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1166f7b12606SJiri Pirko 	[IFLA_MAP]		= { .len = sizeof(struct rtnl_link_ifmap) },
1167f7b12606SJiri Pirko 	[IFLA_MTU]		= { .type = NLA_U32 },
1168f7b12606SJiri Pirko 	[IFLA_LINK]		= { .type = NLA_U32 },
1169f7b12606SJiri Pirko 	[IFLA_MASTER]		= { .type = NLA_U32 },
1170f7b12606SJiri Pirko 	[IFLA_CARRIER]		= { .type = NLA_U8 },
1171f7b12606SJiri Pirko 	[IFLA_TXQLEN]		= { .type = NLA_U32 },
1172f7b12606SJiri Pirko 	[IFLA_WEIGHT]		= { .type = NLA_U32 },
1173f7b12606SJiri Pirko 	[IFLA_OPERSTATE]	= { .type = NLA_U8 },
1174f7b12606SJiri Pirko 	[IFLA_LINKMODE]		= { .type = NLA_U8 },
1175f7b12606SJiri Pirko 	[IFLA_LINKINFO]		= { .type = NLA_NESTED },
1176f7b12606SJiri Pirko 	[IFLA_NET_NS_PID]	= { .type = NLA_U32 },
1177f7b12606SJiri Pirko 	[IFLA_NET_NS_FD]	= { .type = NLA_U32 },
1178f7b12606SJiri Pirko 	[IFLA_IFALIAS]	        = { .type = NLA_STRING, .len = IFALIASZ-1 },
1179f7b12606SJiri Pirko 	[IFLA_VFINFO_LIST]	= {. type = NLA_NESTED },
1180f7b12606SJiri Pirko 	[IFLA_VF_PORTS]		= { .type = NLA_NESTED },
1181f7b12606SJiri Pirko 	[IFLA_PORT_SELF]	= { .type = NLA_NESTED },
1182f7b12606SJiri Pirko 	[IFLA_AF_SPEC]		= { .type = NLA_NESTED },
1183f7b12606SJiri Pirko 	[IFLA_EXT_MASK]		= { .type = NLA_U32 },
1184f7b12606SJiri Pirko 	[IFLA_PROMISCUITY]	= { .type = NLA_U32 },
1185f7b12606SJiri Pirko 	[IFLA_NUM_TX_QUEUES]	= { .type = NLA_U32 },
1186f7b12606SJiri Pirko 	[IFLA_NUM_RX_QUEUES]	= { .type = NLA_U32 },
1187f7b12606SJiri Pirko 	[IFLA_PHYS_PORT_ID]	= { .type = NLA_BINARY, .len = MAX_PHYS_PORT_ID_LEN },
11882d3b479dSdavid decotigny 	[IFLA_CARRIER_CHANGES]	= { .type = NLA_U32 },  /* ignored */
1189f7b12606SJiri Pirko };
1190f7b12606SJiri Pirko 
1191f7b12606SJiri Pirko static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1192f7b12606SJiri Pirko 	[IFLA_INFO_KIND]	= { .type = NLA_STRING },
1193f7b12606SJiri Pirko 	[IFLA_INFO_DATA]	= { .type = NLA_NESTED },
1194f7b12606SJiri Pirko 	[IFLA_INFO_SLAVE_KIND]	= { .type = NLA_STRING },
1195f7b12606SJiri Pirko 	[IFLA_INFO_SLAVE_DATA]	= { .type = NLA_NESTED },
1196f7b12606SJiri Pirko };
1197f7b12606SJiri Pirko 
1198f7b12606SJiri Pirko static const struct nla_policy ifla_vfinfo_policy[IFLA_VF_INFO_MAX+1] = {
1199f7b12606SJiri Pirko 	[IFLA_VF_INFO]		= { .type = NLA_NESTED },
1200f7b12606SJiri Pirko };
1201f7b12606SJiri Pirko 
1202f7b12606SJiri Pirko static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1203f7b12606SJiri Pirko 	[IFLA_VF_MAC]		= { .type = NLA_BINARY,
1204f7b12606SJiri Pirko 				    .len = sizeof(struct ifla_vf_mac) },
1205f7b12606SJiri Pirko 	[IFLA_VF_VLAN]		= { .type = NLA_BINARY,
1206f7b12606SJiri Pirko 				    .len = sizeof(struct ifla_vf_vlan) },
1207f7b12606SJiri Pirko 	[IFLA_VF_TX_RATE]	= { .type = NLA_BINARY,
1208f7b12606SJiri Pirko 				    .len = sizeof(struct ifla_vf_tx_rate) },
1209f7b12606SJiri Pirko 	[IFLA_VF_SPOOFCHK]	= { .type = NLA_BINARY,
1210f7b12606SJiri Pirko 				    .len = sizeof(struct ifla_vf_spoofchk) },
1211f7b12606SJiri Pirko };
1212f7b12606SJiri Pirko 
1213f7b12606SJiri Pirko static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1214f7b12606SJiri Pirko 	[IFLA_PORT_VF]		= { .type = NLA_U32 },
1215f7b12606SJiri Pirko 	[IFLA_PORT_PROFILE]	= { .type = NLA_STRING,
1216f7b12606SJiri Pirko 				    .len = PORT_PROFILE_MAX },
1217f7b12606SJiri Pirko 	[IFLA_PORT_VSI_TYPE]	= { .type = NLA_BINARY,
1218f7b12606SJiri Pirko 				    .len = sizeof(struct ifla_port_vsi)},
1219f7b12606SJiri Pirko 	[IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1220f7b12606SJiri Pirko 				      .len = PORT_UUID_MAX },
1221f7b12606SJiri Pirko 	[IFLA_PORT_HOST_UUID]	= { .type = NLA_STRING,
1222f7b12606SJiri Pirko 				    .len = PORT_UUID_MAX },
1223f7b12606SJiri Pirko 	[IFLA_PORT_REQUEST]	= { .type = NLA_U8, },
1224f7b12606SJiri Pirko 	[IFLA_PORT_RESPONSE]	= { .type = NLA_U16, },
1225f7b12606SJiri Pirko };
1226f7b12606SJiri Pirko 
1227b60c5115SThomas Graf static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
12281da177e4SLinus Torvalds {
12293b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
12307c28bd0bSEric Dumazet 	int h, s_h;
12317c28bd0bSEric Dumazet 	int idx = 0, s_idx;
12321da177e4SLinus Torvalds 	struct net_device *dev;
12337c28bd0bSEric Dumazet 	struct hlist_head *head;
1234115c9b81SGreg Rose 	struct nlattr *tb[IFLA_MAX+1];
1235115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
1236973462bbSDavid Gibson 	int err;
12371da177e4SLinus Torvalds 
12387c28bd0bSEric Dumazet 	s_h = cb->args[0];
12397c28bd0bSEric Dumazet 	s_idx = cb->args[1];
12407c28bd0bSEric Dumazet 
1241e67f88ddSEric Dumazet 	rcu_read_lock();
12424e985adaSThomas Graf 	cb->seq = net->dev_base_seq;
12434e985adaSThomas Graf 
124488c5b5ceSMichael Riesch 	if (nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
1245a4b64fbeSEric Dumazet 			ifla_policy) >= 0) {
1246115c9b81SGreg Rose 
1247115c9b81SGreg Rose 		if (tb[IFLA_EXT_MASK])
1248115c9b81SGreg Rose 			ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1249a4b64fbeSEric Dumazet 	}
1250115c9b81SGreg Rose 
12517c28bd0bSEric Dumazet 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
12527562f876SPavel Emelianov 		idx = 0;
12537c28bd0bSEric Dumazet 		head = &net->dev_index_head[h];
1254b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu(dev, head, index_hlist) {
12551da177e4SLinus Torvalds 			if (idx < s_idx)
12567562f876SPavel Emelianov 				goto cont;
1257973462bbSDavid Gibson 			err = rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
125815e47304SEric W. Biederman 					       NETLINK_CB(cb->skb).portid,
12597c28bd0bSEric Dumazet 					       cb->nlh->nlmsg_seq, 0,
1260115c9b81SGreg Rose 					       NLM_F_MULTI,
1261973462bbSDavid Gibson 					       ext_filter_mask);
1262973462bbSDavid Gibson 			/* If we ran out of room on the first message,
1263973462bbSDavid Gibson 			 * we're in trouble
1264973462bbSDavid Gibson 			 */
1265973462bbSDavid Gibson 			WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
1266973462bbSDavid Gibson 
1267973462bbSDavid Gibson 			if (err <= 0)
12687c28bd0bSEric Dumazet 				goto out;
12694e985adaSThomas Graf 
12704e985adaSThomas Graf 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
12717562f876SPavel Emelianov cont:
12727562f876SPavel Emelianov 			idx++;
12731da177e4SLinus Torvalds 		}
12747c28bd0bSEric Dumazet 	}
12757c28bd0bSEric Dumazet out:
1276e67f88ddSEric Dumazet 	rcu_read_unlock();
12777c28bd0bSEric Dumazet 	cb->args[1] = idx;
12787c28bd0bSEric Dumazet 	cb->args[0] = h;
12791da177e4SLinus Torvalds 
12801da177e4SLinus Torvalds 	return skb->len;
12811da177e4SLinus Torvalds }
12821da177e4SLinus Torvalds 
1283f7b12606SJiri Pirko int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len)
1284f7b12606SJiri Pirko {
1285f7b12606SJiri Pirko 	return nla_parse(tb, IFLA_MAX, head, len, ifla_policy);
1286f7b12606SJiri Pirko }
1287f7b12606SJiri Pirko EXPORT_SYMBOL(rtnl_nla_parse_ifla);
128857b61080SScott Feldman 
128981adee47SEric W. Biederman struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
129081adee47SEric W. Biederman {
129181adee47SEric W. Biederman 	struct net *net;
129281adee47SEric W. Biederman 	/* Examine the link attributes and figure out which
129381adee47SEric W. Biederman 	 * network namespace we are talking about.
129481adee47SEric W. Biederman 	 */
129581adee47SEric W. Biederman 	if (tb[IFLA_NET_NS_PID])
129681adee47SEric W. Biederman 		net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
1297f0630529SEric W. Biederman 	else if (tb[IFLA_NET_NS_FD])
1298f0630529SEric W. Biederman 		net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
129981adee47SEric W. Biederman 	else
130081adee47SEric W. Biederman 		net = get_net(src_net);
130181adee47SEric W. Biederman 	return net;
130281adee47SEric W. Biederman }
130381adee47SEric W. Biederman EXPORT_SYMBOL(rtnl_link_get_net);
130481adee47SEric W. Biederman 
13051840bb13SThomas Graf static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
13061840bb13SThomas Graf {
13071840bb13SThomas Graf 	if (dev) {
13081840bb13SThomas Graf 		if (tb[IFLA_ADDRESS] &&
13091840bb13SThomas Graf 		    nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
13101840bb13SThomas Graf 			return -EINVAL;
13111840bb13SThomas Graf 
13121840bb13SThomas Graf 		if (tb[IFLA_BROADCAST] &&
13131840bb13SThomas Graf 		    nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
13141840bb13SThomas Graf 			return -EINVAL;
13151840bb13SThomas Graf 	}
13161840bb13SThomas Graf 
1317cf7afbfeSThomas Graf 	if (tb[IFLA_AF_SPEC]) {
1318cf7afbfeSThomas Graf 		struct nlattr *af;
1319cf7afbfeSThomas Graf 		int rem, err;
1320cf7afbfeSThomas Graf 
1321cf7afbfeSThomas Graf 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1322cf7afbfeSThomas Graf 			const struct rtnl_af_ops *af_ops;
1323cf7afbfeSThomas Graf 
1324cf7afbfeSThomas Graf 			if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1325cf7afbfeSThomas Graf 				return -EAFNOSUPPORT;
1326cf7afbfeSThomas Graf 
1327cf7afbfeSThomas Graf 			if (!af_ops->set_link_af)
1328cf7afbfeSThomas Graf 				return -EOPNOTSUPP;
1329cf7afbfeSThomas Graf 
1330cf7afbfeSThomas Graf 			if (af_ops->validate_link_af) {
13316d3a9a68SKurt Van Dijck 				err = af_ops->validate_link_af(dev, af);
1332cf7afbfeSThomas Graf 				if (err < 0)
1333cf7afbfeSThomas Graf 					return err;
1334cf7afbfeSThomas Graf 			}
1335cf7afbfeSThomas Graf 		}
1336cf7afbfeSThomas Graf 	}
1337cf7afbfeSThomas Graf 
13381840bb13SThomas Graf 	return 0;
13391840bb13SThomas Graf }
13401840bb13SThomas Graf 
1341c02db8c6SChris Wright static int do_setvfinfo(struct net_device *dev, struct nlattr *attr)
1342c02db8c6SChris Wright {
1343c02db8c6SChris Wright 	int rem, err = -EINVAL;
1344c02db8c6SChris Wright 	struct nlattr *vf;
1345c02db8c6SChris Wright 	const struct net_device_ops *ops = dev->netdev_ops;
1346c02db8c6SChris Wright 
1347c02db8c6SChris Wright 	nla_for_each_nested(vf, attr, rem) {
1348c02db8c6SChris Wright 		switch (nla_type(vf)) {
1349c02db8c6SChris Wright 		case IFLA_VF_MAC: {
1350c02db8c6SChris Wright 			struct ifla_vf_mac *ivm;
1351c02db8c6SChris Wright 			ivm = nla_data(vf);
1352c02db8c6SChris Wright 			err = -EOPNOTSUPP;
1353c02db8c6SChris Wright 			if (ops->ndo_set_vf_mac)
1354c02db8c6SChris Wright 				err = ops->ndo_set_vf_mac(dev, ivm->vf,
1355c02db8c6SChris Wright 							  ivm->mac);
1356c02db8c6SChris Wright 			break;
1357c02db8c6SChris Wright 		}
1358c02db8c6SChris Wright 		case IFLA_VF_VLAN: {
1359c02db8c6SChris Wright 			struct ifla_vf_vlan *ivv;
1360c02db8c6SChris Wright 			ivv = nla_data(vf);
1361c02db8c6SChris Wright 			err = -EOPNOTSUPP;
1362c02db8c6SChris Wright 			if (ops->ndo_set_vf_vlan)
1363c02db8c6SChris Wright 				err = ops->ndo_set_vf_vlan(dev, ivv->vf,
1364c02db8c6SChris Wright 							   ivv->vlan,
1365c02db8c6SChris Wright 							   ivv->qos);
1366c02db8c6SChris Wright 			break;
1367c02db8c6SChris Wright 		}
1368c02db8c6SChris Wright 		case IFLA_VF_TX_RATE: {
1369c02db8c6SChris Wright 			struct ifla_vf_tx_rate *ivt;
1370c02db8c6SChris Wright 			ivt = nla_data(vf);
1371c02db8c6SChris Wright 			err = -EOPNOTSUPP;
1372c02db8c6SChris Wright 			if (ops->ndo_set_vf_tx_rate)
1373c02db8c6SChris Wright 				err = ops->ndo_set_vf_tx_rate(dev, ivt->vf,
1374c02db8c6SChris Wright 							      ivt->rate);
1375c02db8c6SChris Wright 			break;
1376c02db8c6SChris Wright 		}
13775f8444a3SGreg Rose 		case IFLA_VF_SPOOFCHK: {
13785f8444a3SGreg Rose 			struct ifla_vf_spoofchk *ivs;
13795f8444a3SGreg Rose 			ivs = nla_data(vf);
13805f8444a3SGreg Rose 			err = -EOPNOTSUPP;
13815f8444a3SGreg Rose 			if (ops->ndo_set_vf_spoofchk)
13825f8444a3SGreg Rose 				err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
13835f8444a3SGreg Rose 							       ivs->setting);
13845f8444a3SGreg Rose 			break;
13855f8444a3SGreg Rose 		}
13861d8faf48SRony Efraim 		case IFLA_VF_LINK_STATE: {
13871d8faf48SRony Efraim 			struct ifla_vf_link_state *ivl;
13881d8faf48SRony Efraim 			ivl = nla_data(vf);
13891d8faf48SRony Efraim 			err = -EOPNOTSUPP;
13901d8faf48SRony Efraim 			if (ops->ndo_set_vf_link_state)
13911d8faf48SRony Efraim 				err = ops->ndo_set_vf_link_state(dev, ivl->vf,
13921d8faf48SRony Efraim 								 ivl->link_state);
13931d8faf48SRony Efraim 			break;
13941d8faf48SRony Efraim 		}
1395c02db8c6SChris Wright 		default:
1396c02db8c6SChris Wright 			err = -EINVAL;
1397c02db8c6SChris Wright 			break;
1398c02db8c6SChris Wright 		}
1399c02db8c6SChris Wright 		if (err)
1400c02db8c6SChris Wright 			break;
1401c02db8c6SChris Wright 	}
1402c02db8c6SChris Wright 	return err;
1403c02db8c6SChris Wright }
1404c02db8c6SChris Wright 
1405fbaec0eaSJiri Pirko static int do_set_master(struct net_device *dev, int ifindex)
1406fbaec0eaSJiri Pirko {
1407898e5061SJiri Pirko 	struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
1408fbaec0eaSJiri Pirko 	const struct net_device_ops *ops;
1409fbaec0eaSJiri Pirko 	int err;
1410fbaec0eaSJiri Pirko 
1411898e5061SJiri Pirko 	if (upper_dev) {
1412898e5061SJiri Pirko 		if (upper_dev->ifindex == ifindex)
1413fbaec0eaSJiri Pirko 			return 0;
1414898e5061SJiri Pirko 		ops = upper_dev->netdev_ops;
1415fbaec0eaSJiri Pirko 		if (ops->ndo_del_slave) {
1416898e5061SJiri Pirko 			err = ops->ndo_del_slave(upper_dev, dev);
1417fbaec0eaSJiri Pirko 			if (err)
1418fbaec0eaSJiri Pirko 				return err;
1419fbaec0eaSJiri Pirko 		} else {
1420fbaec0eaSJiri Pirko 			return -EOPNOTSUPP;
1421fbaec0eaSJiri Pirko 		}
1422fbaec0eaSJiri Pirko 	}
1423fbaec0eaSJiri Pirko 
1424fbaec0eaSJiri Pirko 	if (ifindex) {
1425898e5061SJiri Pirko 		upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
1426898e5061SJiri Pirko 		if (!upper_dev)
1427fbaec0eaSJiri Pirko 			return -EINVAL;
1428898e5061SJiri Pirko 		ops = upper_dev->netdev_ops;
1429fbaec0eaSJiri Pirko 		if (ops->ndo_add_slave) {
1430898e5061SJiri Pirko 			err = ops->ndo_add_slave(upper_dev, dev);
1431fbaec0eaSJiri Pirko 			if (err)
1432fbaec0eaSJiri Pirko 				return err;
1433fbaec0eaSJiri Pirko 		} else {
1434fbaec0eaSJiri Pirko 			return -EOPNOTSUPP;
1435fbaec0eaSJiri Pirko 		}
1436fbaec0eaSJiri Pirko 	}
1437fbaec0eaSJiri Pirko 	return 0;
1438fbaec0eaSJiri Pirko }
1439fbaec0eaSJiri Pirko 
144090f62cf3SEric W. Biederman static int do_setlink(const struct sk_buff *skb,
144190f62cf3SEric W. Biederman 		      struct net_device *dev, struct ifinfomsg *ifm,
144238f7b870SPatrick McHardy 		      struct nlattr **tb, char *ifname, int modified)
14430157f60cSPatrick McHardy {
1444d314774cSStephen Hemminger 	const struct net_device_ops *ops = dev->netdev_ops;
14450157f60cSPatrick McHardy 	int err;
14460157f60cSPatrick McHardy 
1447f0630529SEric W. Biederman 	if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
144881adee47SEric W. Biederman 		struct net *net = rtnl_link_get_net(dev_net(dev), tb);
1449d8a5ec67SEric W. Biederman 		if (IS_ERR(net)) {
1450d8a5ec67SEric W. Biederman 			err = PTR_ERR(net);
1451d8a5ec67SEric W. Biederman 			goto errout;
1452d8a5ec67SEric W. Biederman 		}
145390f62cf3SEric W. Biederman 		if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
1454b51642f6SEric W. Biederman 			err = -EPERM;
1455b51642f6SEric W. Biederman 			goto errout;
1456b51642f6SEric W. Biederman 		}
1457d8a5ec67SEric W. Biederman 		err = dev_change_net_namespace(dev, net, ifname);
1458d8a5ec67SEric W. Biederman 		put_net(net);
1459d8a5ec67SEric W. Biederman 		if (err)
1460d8a5ec67SEric W. Biederman 			goto errout;
1461d8a5ec67SEric W. Biederman 		modified = 1;
1462d8a5ec67SEric W. Biederman 	}
1463d8a5ec67SEric W. Biederman 
14640157f60cSPatrick McHardy 	if (tb[IFLA_MAP]) {
14650157f60cSPatrick McHardy 		struct rtnl_link_ifmap *u_map;
14660157f60cSPatrick McHardy 		struct ifmap k_map;
14670157f60cSPatrick McHardy 
1468d314774cSStephen Hemminger 		if (!ops->ndo_set_config) {
14690157f60cSPatrick McHardy 			err = -EOPNOTSUPP;
14700157f60cSPatrick McHardy 			goto errout;
14710157f60cSPatrick McHardy 		}
14720157f60cSPatrick McHardy 
14730157f60cSPatrick McHardy 		if (!netif_device_present(dev)) {
14740157f60cSPatrick McHardy 			err = -ENODEV;
14750157f60cSPatrick McHardy 			goto errout;
14760157f60cSPatrick McHardy 		}
14770157f60cSPatrick McHardy 
14780157f60cSPatrick McHardy 		u_map = nla_data(tb[IFLA_MAP]);
14790157f60cSPatrick McHardy 		k_map.mem_start = (unsigned long) u_map->mem_start;
14800157f60cSPatrick McHardy 		k_map.mem_end = (unsigned long) u_map->mem_end;
14810157f60cSPatrick McHardy 		k_map.base_addr = (unsigned short) u_map->base_addr;
14820157f60cSPatrick McHardy 		k_map.irq = (unsigned char) u_map->irq;
14830157f60cSPatrick McHardy 		k_map.dma = (unsigned char) u_map->dma;
14840157f60cSPatrick McHardy 		k_map.port = (unsigned char) u_map->port;
14850157f60cSPatrick McHardy 
1486d314774cSStephen Hemminger 		err = ops->ndo_set_config(dev, &k_map);
14870157f60cSPatrick McHardy 		if (err < 0)
14880157f60cSPatrick McHardy 			goto errout;
14890157f60cSPatrick McHardy 
14900157f60cSPatrick McHardy 		modified = 1;
14910157f60cSPatrick McHardy 	}
14920157f60cSPatrick McHardy 
14930157f60cSPatrick McHardy 	if (tb[IFLA_ADDRESS]) {
14940157f60cSPatrick McHardy 		struct sockaddr *sa;
14950157f60cSPatrick McHardy 		int len;
14960157f60cSPatrick McHardy 
14970157f60cSPatrick McHardy 		len = sizeof(sa_family_t) + dev->addr_len;
14980157f60cSPatrick McHardy 		sa = kmalloc(len, GFP_KERNEL);
14990157f60cSPatrick McHardy 		if (!sa) {
15000157f60cSPatrick McHardy 			err = -ENOMEM;
15010157f60cSPatrick McHardy 			goto errout;
15020157f60cSPatrick McHardy 		}
15030157f60cSPatrick McHardy 		sa->sa_family = dev->type;
15040157f60cSPatrick McHardy 		memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
15050157f60cSPatrick McHardy 		       dev->addr_len);
1506e7c3273eSJiri Pirko 		err = dev_set_mac_address(dev, sa);
15070157f60cSPatrick McHardy 		kfree(sa);
15080157f60cSPatrick McHardy 		if (err)
15090157f60cSPatrick McHardy 			goto errout;
15100157f60cSPatrick McHardy 		modified = 1;
15110157f60cSPatrick McHardy 	}
15120157f60cSPatrick McHardy 
15130157f60cSPatrick McHardy 	if (tb[IFLA_MTU]) {
15140157f60cSPatrick McHardy 		err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
15150157f60cSPatrick McHardy 		if (err < 0)
15160157f60cSPatrick McHardy 			goto errout;
15170157f60cSPatrick McHardy 		modified = 1;
15180157f60cSPatrick McHardy 	}
15190157f60cSPatrick McHardy 
1520cbda10faSVlad Dogaru 	if (tb[IFLA_GROUP]) {
1521cbda10faSVlad Dogaru 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
1522cbda10faSVlad Dogaru 		modified = 1;
1523cbda10faSVlad Dogaru 	}
1524cbda10faSVlad Dogaru 
15250157f60cSPatrick McHardy 	/*
15260157f60cSPatrick McHardy 	 * Interface selected by interface index but interface
15270157f60cSPatrick McHardy 	 * name provided implies that a name change has been
15280157f60cSPatrick McHardy 	 * requested.
15290157f60cSPatrick McHardy 	 */
15300157f60cSPatrick McHardy 	if (ifm->ifi_index > 0 && ifname[0]) {
15310157f60cSPatrick McHardy 		err = dev_change_name(dev, ifname);
15320157f60cSPatrick McHardy 		if (err < 0)
15330157f60cSPatrick McHardy 			goto errout;
15340157f60cSPatrick McHardy 		modified = 1;
15350157f60cSPatrick McHardy 	}
15360157f60cSPatrick McHardy 
15370b815a1aSStephen Hemminger 	if (tb[IFLA_IFALIAS]) {
15380b815a1aSStephen Hemminger 		err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
15390b815a1aSStephen Hemminger 				    nla_len(tb[IFLA_IFALIAS]));
15400b815a1aSStephen Hemminger 		if (err < 0)
15410b815a1aSStephen Hemminger 			goto errout;
15420b815a1aSStephen Hemminger 		modified = 1;
15430b815a1aSStephen Hemminger 	}
15440b815a1aSStephen Hemminger 
15450157f60cSPatrick McHardy 	if (tb[IFLA_BROADCAST]) {
15460157f60cSPatrick McHardy 		nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
1547e7c3273eSJiri Pirko 		call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
15480157f60cSPatrick McHardy 	}
15490157f60cSPatrick McHardy 
15500157f60cSPatrick McHardy 	if (ifm->ifi_flags || ifm->ifi_change) {
15513729d502SPatrick McHardy 		err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
15525f9021cfSJohannes Berg 		if (err < 0)
15535f9021cfSJohannes Berg 			goto errout;
15540157f60cSPatrick McHardy 	}
15550157f60cSPatrick McHardy 
1556fbaec0eaSJiri Pirko 	if (tb[IFLA_MASTER]) {
1557fbaec0eaSJiri Pirko 		err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
1558fbaec0eaSJiri Pirko 		if (err)
1559fbaec0eaSJiri Pirko 			goto errout;
1560fbaec0eaSJiri Pirko 		modified = 1;
1561fbaec0eaSJiri Pirko 	}
1562fbaec0eaSJiri Pirko 
15639a57247fSJiri Pirko 	if (tb[IFLA_CARRIER]) {
15649a57247fSJiri Pirko 		err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
15659a57247fSJiri Pirko 		if (err)
15669a57247fSJiri Pirko 			goto errout;
15679a57247fSJiri Pirko 		modified = 1;
15689a57247fSJiri Pirko 	}
15699a57247fSJiri Pirko 
157093b2d4a2SDavid S. Miller 	if (tb[IFLA_TXQLEN])
15710157f60cSPatrick McHardy 		dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
15720157f60cSPatrick McHardy 
15730157f60cSPatrick McHardy 	if (tb[IFLA_OPERSTATE])
157493b2d4a2SDavid S. Miller 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
15750157f60cSPatrick McHardy 
15760157f60cSPatrick McHardy 	if (tb[IFLA_LINKMODE]) {
15770157f60cSPatrick McHardy 		write_lock_bh(&dev_base_lock);
15780157f60cSPatrick McHardy 		dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
157993b2d4a2SDavid S. Miller 		write_unlock_bh(&dev_base_lock);
15800157f60cSPatrick McHardy 	}
15810157f60cSPatrick McHardy 
1582c02db8c6SChris Wright 	if (tb[IFLA_VFINFO_LIST]) {
1583c02db8c6SChris Wright 		struct nlattr *attr;
1584c02db8c6SChris Wright 		int rem;
1585c02db8c6SChris Wright 		nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
1586253683bbSDavid Howells 			if (nla_type(attr) != IFLA_VF_INFO) {
1587253683bbSDavid Howells 				err = -EINVAL;
1588c02db8c6SChris Wright 				goto errout;
1589253683bbSDavid Howells 			}
1590c02db8c6SChris Wright 			err = do_setvfinfo(dev, attr);
1591ebc08a6fSWilliams, Mitch A 			if (err < 0)
1592ebc08a6fSWilliams, Mitch A 				goto errout;
1593ebc08a6fSWilliams, Mitch A 			modified = 1;
1594ebc08a6fSWilliams, Mitch A 		}
1595ebc08a6fSWilliams, Mitch A 	}
15960157f60cSPatrick McHardy 	err = 0;
15970157f60cSPatrick McHardy 
159857b61080SScott Feldman 	if (tb[IFLA_VF_PORTS]) {
159957b61080SScott Feldman 		struct nlattr *port[IFLA_PORT_MAX+1];
160057b61080SScott Feldman 		struct nlattr *attr;
160157b61080SScott Feldman 		int vf;
160257b61080SScott Feldman 		int rem;
160357b61080SScott Feldman 
160457b61080SScott Feldman 		err = -EOPNOTSUPP;
160557b61080SScott Feldman 		if (!ops->ndo_set_vf_port)
160657b61080SScott Feldman 			goto errout;
160757b61080SScott Feldman 
160857b61080SScott Feldman 		nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
160957b61080SScott Feldman 			if (nla_type(attr) != IFLA_VF_PORT)
161057b61080SScott Feldman 				continue;
161157b61080SScott Feldman 			err = nla_parse_nested(port, IFLA_PORT_MAX,
161257b61080SScott Feldman 				attr, ifla_port_policy);
161357b61080SScott Feldman 			if (err < 0)
161457b61080SScott Feldman 				goto errout;
161557b61080SScott Feldman 			if (!port[IFLA_PORT_VF]) {
161657b61080SScott Feldman 				err = -EOPNOTSUPP;
161757b61080SScott Feldman 				goto errout;
161857b61080SScott Feldman 			}
161957b61080SScott Feldman 			vf = nla_get_u32(port[IFLA_PORT_VF]);
162057b61080SScott Feldman 			err = ops->ndo_set_vf_port(dev, vf, port);
162157b61080SScott Feldman 			if (err < 0)
162257b61080SScott Feldman 				goto errout;
162357b61080SScott Feldman 			modified = 1;
162457b61080SScott Feldman 		}
162557b61080SScott Feldman 	}
162657b61080SScott Feldman 	err = 0;
162757b61080SScott Feldman 
162857b61080SScott Feldman 	if (tb[IFLA_PORT_SELF]) {
162957b61080SScott Feldman 		struct nlattr *port[IFLA_PORT_MAX+1];
163057b61080SScott Feldman 
163157b61080SScott Feldman 		err = nla_parse_nested(port, IFLA_PORT_MAX,
163257b61080SScott Feldman 			tb[IFLA_PORT_SELF], ifla_port_policy);
163357b61080SScott Feldman 		if (err < 0)
163457b61080SScott Feldman 			goto errout;
163557b61080SScott Feldman 
163657b61080SScott Feldman 		err = -EOPNOTSUPP;
163757b61080SScott Feldman 		if (ops->ndo_set_vf_port)
163857b61080SScott Feldman 			err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
163957b61080SScott Feldman 		if (err < 0)
164057b61080SScott Feldman 			goto errout;
164157b61080SScott Feldman 		modified = 1;
164257b61080SScott Feldman 	}
1643f8ff182cSThomas Graf 
1644f8ff182cSThomas Graf 	if (tb[IFLA_AF_SPEC]) {
1645f8ff182cSThomas Graf 		struct nlattr *af;
1646f8ff182cSThomas Graf 		int rem;
1647f8ff182cSThomas Graf 
1648f8ff182cSThomas Graf 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1649f8ff182cSThomas Graf 			const struct rtnl_af_ops *af_ops;
1650f8ff182cSThomas Graf 
1651f8ff182cSThomas Graf 			if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1652cf7afbfeSThomas Graf 				BUG();
1653f8ff182cSThomas Graf 
1654cf7afbfeSThomas Graf 			err = af_ops->set_link_af(dev, af);
1655f8ff182cSThomas Graf 			if (err < 0)
1656f8ff182cSThomas Graf 				goto errout;
1657f8ff182cSThomas Graf 
1658f8ff182cSThomas Graf 			modified = 1;
1659f8ff182cSThomas Graf 		}
1660f8ff182cSThomas Graf 	}
166157b61080SScott Feldman 	err = 0;
166257b61080SScott Feldman 
16630157f60cSPatrick McHardy errout:
1664e87cc472SJoe Perches 	if (err < 0 && modified)
1665e87cc472SJoe 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",
1666e87cc472SJoe Perches 				     dev->name);
16670157f60cSPatrick McHardy 
16680157f60cSPatrick McHardy 	return err;
16690157f60cSPatrick McHardy }
16700157f60cSPatrick McHardy 
1671661d2967SThomas Graf static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
1672da5e0494SThomas Graf {
16733b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
1674da5e0494SThomas Graf 	struct ifinfomsg *ifm;
1675da5e0494SThomas Graf 	struct net_device *dev;
16760157f60cSPatrick McHardy 	int err;
1677da5e0494SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
16781da177e4SLinus Torvalds 	char ifname[IFNAMSIZ];
16791da177e4SLinus Torvalds 
1680da5e0494SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1681da5e0494SThomas Graf 	if (err < 0)
1682da5e0494SThomas Graf 		goto errout;
16831da177e4SLinus Torvalds 
16845176f91eSThomas Graf 	if (tb[IFLA_IFNAME])
16855176f91eSThomas Graf 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
168678e5b891SPatrick McHardy 	else
168778e5b891SPatrick McHardy 		ifname[0] = '\0';
16881da177e4SLinus Torvalds 
16891da177e4SLinus Torvalds 	err = -EINVAL;
1690da5e0494SThomas Graf 	ifm = nlmsg_data(nlh);
169151055be8SPatrick McHardy 	if (ifm->ifi_index > 0)
1692a3d12891SEric Dumazet 		dev = __dev_get_by_index(net, ifm->ifi_index);
1693da5e0494SThomas Graf 	else if (tb[IFLA_IFNAME])
1694a3d12891SEric Dumazet 		dev = __dev_get_by_name(net, ifname);
1695da5e0494SThomas Graf 	else
1696da5e0494SThomas Graf 		goto errout;
16971da177e4SLinus Torvalds 
1698da5e0494SThomas Graf 	if (dev == NULL) {
1699da5e0494SThomas Graf 		err = -ENODEV;
1700da5e0494SThomas Graf 		goto errout;
1701da5e0494SThomas Graf 	}
17021da177e4SLinus Torvalds 
1703e0d087afSEric Dumazet 	err = validate_linkmsg(dev, tb);
1704e0d087afSEric Dumazet 	if (err < 0)
1705a3d12891SEric Dumazet 		goto errout;
1706da5e0494SThomas Graf 
170790f62cf3SEric W. Biederman 	err = do_setlink(skb, dev, ifm, tb, ifname, 0);
1708da5e0494SThomas Graf errout:
17091da177e4SLinus Torvalds 	return err;
17101da177e4SLinus Torvalds }
17111da177e4SLinus Torvalds 
1712661d2967SThomas Graf static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
171338f7b870SPatrick McHardy {
17143b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
171538f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops;
171638f7b870SPatrick McHardy 	struct net_device *dev;
171738f7b870SPatrick McHardy 	struct ifinfomsg *ifm;
171838f7b870SPatrick McHardy 	char ifname[IFNAMSIZ];
171938f7b870SPatrick McHardy 	struct nlattr *tb[IFLA_MAX+1];
172038f7b870SPatrick McHardy 	int err;
1721226bd341SEric Dumazet 	LIST_HEAD(list_kill);
172238f7b870SPatrick McHardy 
172338f7b870SPatrick McHardy 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
172438f7b870SPatrick McHardy 	if (err < 0)
172538f7b870SPatrick McHardy 		return err;
172638f7b870SPatrick McHardy 
172738f7b870SPatrick McHardy 	if (tb[IFLA_IFNAME])
172838f7b870SPatrick McHardy 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
172938f7b870SPatrick McHardy 
173038f7b870SPatrick McHardy 	ifm = nlmsg_data(nlh);
173138f7b870SPatrick McHardy 	if (ifm->ifi_index > 0)
1732881d966bSEric W. Biederman 		dev = __dev_get_by_index(net, ifm->ifi_index);
173338f7b870SPatrick McHardy 	else if (tb[IFLA_IFNAME])
1734881d966bSEric W. Biederman 		dev = __dev_get_by_name(net, ifname);
173538f7b870SPatrick McHardy 	else
173638f7b870SPatrick McHardy 		return -EINVAL;
173738f7b870SPatrick McHardy 
173838f7b870SPatrick McHardy 	if (!dev)
173938f7b870SPatrick McHardy 		return -ENODEV;
174038f7b870SPatrick McHardy 
174138f7b870SPatrick McHardy 	ops = dev->rtnl_link_ops;
174238f7b870SPatrick McHardy 	if (!ops)
174338f7b870SPatrick McHardy 		return -EOPNOTSUPP;
174438f7b870SPatrick McHardy 
1745226bd341SEric Dumazet 	ops->dellink(dev, &list_kill);
1746226bd341SEric Dumazet 	unregister_netdevice_many(&list_kill);
1747226bd341SEric Dumazet 	list_del(&list_kill);
174838f7b870SPatrick McHardy 	return 0;
174938f7b870SPatrick McHardy }
175038f7b870SPatrick McHardy 
17513729d502SPatrick McHardy int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
17523729d502SPatrick McHardy {
17533729d502SPatrick McHardy 	unsigned int old_flags;
17543729d502SPatrick McHardy 	int err;
17553729d502SPatrick McHardy 
17563729d502SPatrick McHardy 	old_flags = dev->flags;
17573729d502SPatrick McHardy 	if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
17583729d502SPatrick McHardy 		err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
17593729d502SPatrick McHardy 		if (err < 0)
17603729d502SPatrick McHardy 			return err;
17613729d502SPatrick McHardy 	}
17623729d502SPatrick McHardy 
17633729d502SPatrick McHardy 	dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
17643729d502SPatrick McHardy 
1765a528c219SNicolas Dichtel 	__dev_notify_flags(dev, old_flags, ~0U);
17663729d502SPatrick McHardy 	return 0;
17673729d502SPatrick McHardy }
17683729d502SPatrick McHardy EXPORT_SYMBOL(rtnl_configure_link);
17693729d502SPatrick McHardy 
1770c0713563SRami Rosen struct net_device *rtnl_create_link(struct net *net,
177181adee47SEric W. Biederman 	char *ifname, const struct rtnl_link_ops *ops, struct nlattr *tb[])
1772e7199288SPavel Emelianov {
1773e7199288SPavel Emelianov 	int err;
1774e7199288SPavel Emelianov 	struct net_device *dev;
1775d40156aaSJiri Pirko 	unsigned int num_tx_queues = 1;
1776d40156aaSJiri Pirko 	unsigned int num_rx_queues = 1;
1777e7199288SPavel Emelianov 
177876ff5cc9SJiri Pirko 	if (tb[IFLA_NUM_TX_QUEUES])
177976ff5cc9SJiri Pirko 		num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
178076ff5cc9SJiri Pirko 	else if (ops->get_num_tx_queues)
1781d40156aaSJiri Pirko 		num_tx_queues = ops->get_num_tx_queues();
178276ff5cc9SJiri Pirko 
178376ff5cc9SJiri Pirko 	if (tb[IFLA_NUM_RX_QUEUES])
178476ff5cc9SJiri Pirko 		num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
178576ff5cc9SJiri Pirko 	else if (ops->get_num_rx_queues)
1786d40156aaSJiri Pirko 		num_rx_queues = ops->get_num_rx_queues();
1787efacb309Sstephen hemminger 
1788e7199288SPavel Emelianov 	err = -ENOMEM;
1789d40156aaSJiri Pirko 	dev = alloc_netdev_mqs(ops->priv_size, ifname, ops->setup,
1790d40156aaSJiri Pirko 			       num_tx_queues, num_rx_queues);
1791e7199288SPavel Emelianov 	if (!dev)
1792e7199288SPavel Emelianov 		goto err;
1793e7199288SPavel Emelianov 
179481adee47SEric W. Biederman 	dev_net_set(dev, net);
179581adee47SEric W. Biederman 	dev->rtnl_link_ops = ops;
17963729d502SPatrick McHardy 	dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
179781adee47SEric W. Biederman 
1798e7199288SPavel Emelianov 	if (tb[IFLA_MTU])
1799e7199288SPavel Emelianov 		dev->mtu = nla_get_u32(tb[IFLA_MTU]);
18002afb9b53SJiri Pirko 	if (tb[IFLA_ADDRESS]) {
1801e7199288SPavel Emelianov 		memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
1802e7199288SPavel Emelianov 				nla_len(tb[IFLA_ADDRESS]));
18032afb9b53SJiri Pirko 		dev->addr_assign_type = NET_ADDR_SET;
18042afb9b53SJiri Pirko 	}
1805e7199288SPavel Emelianov 	if (tb[IFLA_BROADCAST])
1806e7199288SPavel Emelianov 		memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
1807e7199288SPavel Emelianov 				nla_len(tb[IFLA_BROADCAST]));
1808e7199288SPavel Emelianov 	if (tb[IFLA_TXQLEN])
1809e7199288SPavel Emelianov 		dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
1810e7199288SPavel Emelianov 	if (tb[IFLA_OPERSTATE])
181193b2d4a2SDavid S. Miller 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
1812e7199288SPavel Emelianov 	if (tb[IFLA_LINKMODE])
1813e7199288SPavel Emelianov 		dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
1814ffa934f1SPatrick McHardy 	if (tb[IFLA_GROUP])
1815ffa934f1SPatrick McHardy 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
1816e7199288SPavel Emelianov 
1817e7199288SPavel Emelianov 	return dev;
1818e7199288SPavel Emelianov 
1819e7199288SPavel Emelianov err:
1820e7199288SPavel Emelianov 	return ERR_PTR(err);
1821e7199288SPavel Emelianov }
1822e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_create_link);
1823e7199288SPavel Emelianov 
182490f62cf3SEric W. Biederman static int rtnl_group_changelink(const struct sk_buff *skb,
182590f62cf3SEric W. Biederman 		struct net *net, int group,
1826e7ed828fSVlad Dogaru 		struct ifinfomsg *ifm,
1827e7ed828fSVlad Dogaru 		struct nlattr **tb)
1828e7ed828fSVlad Dogaru {
1829e7ed828fSVlad Dogaru 	struct net_device *dev;
1830e7ed828fSVlad Dogaru 	int err;
1831e7ed828fSVlad Dogaru 
1832e7ed828fSVlad Dogaru 	for_each_netdev(net, dev) {
1833e7ed828fSVlad Dogaru 		if (dev->group == group) {
183490f62cf3SEric W. Biederman 			err = do_setlink(skb, dev, ifm, tb, NULL, 0);
1835e7ed828fSVlad Dogaru 			if (err < 0)
1836e7ed828fSVlad Dogaru 				return err;
1837e7ed828fSVlad Dogaru 		}
1838e7ed828fSVlad Dogaru 	}
1839e7ed828fSVlad Dogaru 
1840e7ed828fSVlad Dogaru 	return 0;
1841e7ed828fSVlad Dogaru }
1842e7ed828fSVlad Dogaru 
1843661d2967SThomas Graf static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh)
184438f7b870SPatrick McHardy {
18453b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
184638f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops;
1847ba7d49b1SJiri Pirko 	const struct rtnl_link_ops *m_ops = NULL;
184838f7b870SPatrick McHardy 	struct net_device *dev;
1849ba7d49b1SJiri Pirko 	struct net_device *master_dev = NULL;
185038f7b870SPatrick McHardy 	struct ifinfomsg *ifm;
185138f7b870SPatrick McHardy 	char kind[MODULE_NAME_LEN];
185238f7b870SPatrick McHardy 	char ifname[IFNAMSIZ];
185338f7b870SPatrick McHardy 	struct nlattr *tb[IFLA_MAX+1];
185438f7b870SPatrick McHardy 	struct nlattr *linkinfo[IFLA_INFO_MAX+1];
185538f7b870SPatrick McHardy 	int err;
185638f7b870SPatrick McHardy 
185795a5afcaSJohannes Berg #ifdef CONFIG_MODULES
185838f7b870SPatrick McHardy replay:
18598072f085SThomas Graf #endif
186038f7b870SPatrick McHardy 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
186138f7b870SPatrick McHardy 	if (err < 0)
186238f7b870SPatrick McHardy 		return err;
186338f7b870SPatrick McHardy 
186438f7b870SPatrick McHardy 	if (tb[IFLA_IFNAME])
186538f7b870SPatrick McHardy 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
186638f7b870SPatrick McHardy 	else
186738f7b870SPatrick McHardy 		ifname[0] = '\0';
186838f7b870SPatrick McHardy 
186938f7b870SPatrick McHardy 	ifm = nlmsg_data(nlh);
187038f7b870SPatrick McHardy 	if (ifm->ifi_index > 0)
1871881d966bSEric W. Biederman 		dev = __dev_get_by_index(net, ifm->ifi_index);
1872e7ed828fSVlad Dogaru 	else {
1873e7ed828fSVlad Dogaru 		if (ifname[0])
1874881d966bSEric W. Biederman 			dev = __dev_get_by_name(net, ifname);
187538f7b870SPatrick McHardy 		else
187638f7b870SPatrick McHardy 			dev = NULL;
1877e7ed828fSVlad Dogaru 	}
187838f7b870SPatrick McHardy 
1879ba7d49b1SJiri Pirko 	if (dev) {
1880ba7d49b1SJiri Pirko 		master_dev = netdev_master_upper_dev_get(dev);
1881ba7d49b1SJiri Pirko 		if (master_dev)
1882ba7d49b1SJiri Pirko 			m_ops = master_dev->rtnl_link_ops;
1883ba7d49b1SJiri Pirko 	}
1884ba7d49b1SJiri Pirko 
1885e0d087afSEric Dumazet 	err = validate_linkmsg(dev, tb);
1886e0d087afSEric Dumazet 	if (err < 0)
18871840bb13SThomas Graf 		return err;
18881840bb13SThomas Graf 
188938f7b870SPatrick McHardy 	if (tb[IFLA_LINKINFO]) {
189038f7b870SPatrick McHardy 		err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
189138f7b870SPatrick McHardy 				       tb[IFLA_LINKINFO], ifla_info_policy);
189238f7b870SPatrick McHardy 		if (err < 0)
189338f7b870SPatrick McHardy 			return err;
189438f7b870SPatrick McHardy 	} else
189538f7b870SPatrick McHardy 		memset(linkinfo, 0, sizeof(linkinfo));
189638f7b870SPatrick McHardy 
189738f7b870SPatrick McHardy 	if (linkinfo[IFLA_INFO_KIND]) {
189838f7b870SPatrick McHardy 		nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
189938f7b870SPatrick McHardy 		ops = rtnl_link_ops_get(kind);
190038f7b870SPatrick McHardy 	} else {
190138f7b870SPatrick McHardy 		kind[0] = '\0';
190238f7b870SPatrick McHardy 		ops = NULL;
190338f7b870SPatrick McHardy 	}
190438f7b870SPatrick McHardy 
190538f7b870SPatrick McHardy 	if (1) {
1906ba7d49b1SJiri Pirko 		struct nlattr *attr[ops ? ops->maxtype + 1 : 0];
1907ba7d49b1SJiri Pirko 		struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 0];
1908ba7d49b1SJiri Pirko 		struct nlattr **data = NULL;
1909ba7d49b1SJiri Pirko 		struct nlattr **slave_data = NULL;
191081adee47SEric W. Biederman 		struct net *dest_net;
191138f7b870SPatrick McHardy 
191238f7b870SPatrick McHardy 		if (ops) {
191338f7b870SPatrick McHardy 			if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
191438f7b870SPatrick McHardy 				err = nla_parse_nested(attr, ops->maxtype,
191538f7b870SPatrick McHardy 						       linkinfo[IFLA_INFO_DATA],
191638f7b870SPatrick McHardy 						       ops->policy);
191738f7b870SPatrick McHardy 				if (err < 0)
191838f7b870SPatrick McHardy 					return err;
191938f7b870SPatrick McHardy 				data = attr;
192038f7b870SPatrick McHardy 			}
192138f7b870SPatrick McHardy 			if (ops->validate) {
192238f7b870SPatrick McHardy 				err = ops->validate(tb, data);
192338f7b870SPatrick McHardy 				if (err < 0)
192438f7b870SPatrick McHardy 					return err;
192538f7b870SPatrick McHardy 			}
192638f7b870SPatrick McHardy 		}
192738f7b870SPatrick McHardy 
1928ba7d49b1SJiri Pirko 		if (m_ops) {
1929ba7d49b1SJiri Pirko 			if (m_ops->slave_maxtype &&
1930ba7d49b1SJiri Pirko 			    linkinfo[IFLA_INFO_SLAVE_DATA]) {
1931ba7d49b1SJiri Pirko 				err = nla_parse_nested(slave_attr,
1932ba7d49b1SJiri Pirko 						       m_ops->slave_maxtype,
1933ba7d49b1SJiri Pirko 						       linkinfo[IFLA_INFO_SLAVE_DATA],
1934ba7d49b1SJiri Pirko 						       m_ops->slave_policy);
1935ba7d49b1SJiri Pirko 				if (err < 0)
1936ba7d49b1SJiri Pirko 					return err;
1937ba7d49b1SJiri Pirko 				slave_data = slave_attr;
1938ba7d49b1SJiri Pirko 			}
1939ba7d49b1SJiri Pirko 			if (m_ops->slave_validate) {
1940ba7d49b1SJiri Pirko 				err = m_ops->slave_validate(tb, slave_data);
1941ba7d49b1SJiri Pirko 				if (err < 0)
1942ba7d49b1SJiri Pirko 					return err;
1943ba7d49b1SJiri Pirko 			}
1944ba7d49b1SJiri Pirko 		}
1945ba7d49b1SJiri Pirko 
194638f7b870SPatrick McHardy 		if (dev) {
194738f7b870SPatrick McHardy 			int modified = 0;
194838f7b870SPatrick McHardy 
194938f7b870SPatrick McHardy 			if (nlh->nlmsg_flags & NLM_F_EXCL)
195038f7b870SPatrick McHardy 				return -EEXIST;
195138f7b870SPatrick McHardy 			if (nlh->nlmsg_flags & NLM_F_REPLACE)
195238f7b870SPatrick McHardy 				return -EOPNOTSUPP;
195338f7b870SPatrick McHardy 
195438f7b870SPatrick McHardy 			if (linkinfo[IFLA_INFO_DATA]) {
195538f7b870SPatrick McHardy 				if (!ops || ops != dev->rtnl_link_ops ||
195638f7b870SPatrick McHardy 				    !ops->changelink)
195738f7b870SPatrick McHardy 					return -EOPNOTSUPP;
195838f7b870SPatrick McHardy 
195938f7b870SPatrick McHardy 				err = ops->changelink(dev, tb, data);
196038f7b870SPatrick McHardy 				if (err < 0)
196138f7b870SPatrick McHardy 					return err;
196238f7b870SPatrick McHardy 				modified = 1;
196338f7b870SPatrick McHardy 			}
196438f7b870SPatrick McHardy 
1965ba7d49b1SJiri Pirko 			if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
1966ba7d49b1SJiri Pirko 				if (!m_ops || !m_ops->slave_changelink)
1967ba7d49b1SJiri Pirko 					return -EOPNOTSUPP;
1968ba7d49b1SJiri Pirko 
1969ba7d49b1SJiri Pirko 				err = m_ops->slave_changelink(master_dev, dev,
1970ba7d49b1SJiri Pirko 							      tb, slave_data);
1971ba7d49b1SJiri Pirko 				if (err < 0)
1972ba7d49b1SJiri Pirko 					return err;
1973ba7d49b1SJiri Pirko 				modified = 1;
1974ba7d49b1SJiri Pirko 			}
1975ba7d49b1SJiri Pirko 
197690f62cf3SEric W. Biederman 			return do_setlink(skb, dev, ifm, tb, ifname, modified);
197738f7b870SPatrick McHardy 		}
197838f7b870SPatrick McHardy 
1979ffa934f1SPatrick McHardy 		if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
1980ffa934f1SPatrick McHardy 			if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
198190f62cf3SEric W. Biederman 				return rtnl_group_changelink(skb, net,
1982ffa934f1SPatrick McHardy 						nla_get_u32(tb[IFLA_GROUP]),
1983ffa934f1SPatrick McHardy 						ifm, tb);
198438f7b870SPatrick McHardy 			return -ENODEV;
1985ffa934f1SPatrick McHardy 		}
198638f7b870SPatrick McHardy 
19870e06877cSPatrick McHardy 		if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
198838f7b870SPatrick McHardy 			return -EOPNOTSUPP;
198938f7b870SPatrick McHardy 
199038f7b870SPatrick McHardy 		if (!ops) {
199195a5afcaSJohannes Berg #ifdef CONFIG_MODULES
199238f7b870SPatrick McHardy 			if (kind[0]) {
199338f7b870SPatrick McHardy 				__rtnl_unlock();
199438f7b870SPatrick McHardy 				request_module("rtnl-link-%s", kind);
199538f7b870SPatrick McHardy 				rtnl_lock();
199638f7b870SPatrick McHardy 				ops = rtnl_link_ops_get(kind);
199738f7b870SPatrick McHardy 				if (ops)
199838f7b870SPatrick McHardy 					goto replay;
199938f7b870SPatrick McHardy 			}
200038f7b870SPatrick McHardy #endif
200138f7b870SPatrick McHardy 			return -EOPNOTSUPP;
200238f7b870SPatrick McHardy 		}
200338f7b870SPatrick McHardy 
200438f7b870SPatrick McHardy 		if (!ifname[0])
200538f7b870SPatrick McHardy 			snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
200638f7b870SPatrick McHardy 
200781adee47SEric W. Biederman 		dest_net = rtnl_link_get_net(net, tb);
200813ad1774SEric W. Biederman 		if (IS_ERR(dest_net))
200913ad1774SEric W. Biederman 			return PTR_ERR(dest_net);
201013ad1774SEric W. Biederman 
2011c0713563SRami Rosen 		dev = rtnl_create_link(dest_net, ifname, ops, tb);
20129c7dafbfSPavel Emelyanov 		if (IS_ERR(dev)) {
2013e7199288SPavel Emelianov 			err = PTR_ERR(dev);
20149c7dafbfSPavel Emelyanov 			goto out;
20159c7dafbfSPavel Emelyanov 		}
20169c7dafbfSPavel Emelyanov 
20179c7dafbfSPavel Emelyanov 		dev->ifindex = ifm->ifi_index;
20189c7dafbfSPavel Emelyanov 
20190e0eee24SCong Wang 		if (ops->newlink) {
202081adee47SEric W. Biederman 			err = ops->newlink(net, dev, tb, data);
20210e0eee24SCong Wang 			/* Drivers should call free_netdev() in ->destructor
20220e0eee24SCong Wang 			 * and unregister it on failure so that device could be
20230e0eee24SCong Wang 			 * finally freed in rtnl_unlock.
20240e0eee24SCong Wang 			 */
20250e0eee24SCong Wang 			if (err < 0)
20260e0eee24SCong Wang 				goto out;
20270e0eee24SCong Wang 		} else {
20282d85cba2SPatrick McHardy 			err = register_netdevice(dev);
2029fce9b9beSDan Carpenter 			if (err < 0) {
203038f7b870SPatrick McHardy 				free_netdev(dev);
20313729d502SPatrick McHardy 				goto out;
2032fce9b9beSDan Carpenter 			}
20330e0eee24SCong Wang 		}
20343729d502SPatrick McHardy 		err = rtnl_configure_link(dev, ifm);
20353729d502SPatrick McHardy 		if (err < 0)
20363729d502SPatrick McHardy 			unregister_netdevice(dev);
20373729d502SPatrick McHardy out:
203881adee47SEric W. Biederman 		put_net(dest_net);
203938f7b870SPatrick McHardy 		return err;
204038f7b870SPatrick McHardy 	}
204138f7b870SPatrick McHardy }
204238f7b870SPatrick McHardy 
2043661d2967SThomas Graf static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh)
2044711e2c33SJean Tourrilhes {
20453b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
2046b60c5115SThomas Graf 	struct ifinfomsg *ifm;
2047a3d12891SEric Dumazet 	char ifname[IFNAMSIZ];
2048b60c5115SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
2049b60c5115SThomas Graf 	struct net_device *dev = NULL;
2050b60c5115SThomas Graf 	struct sk_buff *nskb;
2051339bf98fSThomas Graf 	int err;
2052115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
2053711e2c33SJean Tourrilhes 
2054b60c5115SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2055b60c5115SThomas Graf 	if (err < 0)
20569918f230SEric Sesterhenn 		return err;
2057b60c5115SThomas Graf 
2058a3d12891SEric Dumazet 	if (tb[IFLA_IFNAME])
2059a3d12891SEric Dumazet 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2060a3d12891SEric Dumazet 
2061115c9b81SGreg Rose 	if (tb[IFLA_EXT_MASK])
2062115c9b81SGreg Rose 		ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2063115c9b81SGreg Rose 
2064b60c5115SThomas Graf 	ifm = nlmsg_data(nlh);
2065a3d12891SEric Dumazet 	if (ifm->ifi_index > 0)
2066a3d12891SEric Dumazet 		dev = __dev_get_by_index(net, ifm->ifi_index);
2067a3d12891SEric Dumazet 	else if (tb[IFLA_IFNAME])
2068a3d12891SEric Dumazet 		dev = __dev_get_by_name(net, ifname);
2069a3d12891SEric Dumazet 	else
2070b60c5115SThomas Graf 		return -EINVAL;
2071b60c5115SThomas Graf 
2072a3d12891SEric Dumazet 	if (dev == NULL)
2073a3d12891SEric Dumazet 		return -ENODEV;
2074a3d12891SEric Dumazet 
2075115c9b81SGreg Rose 	nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
2076a3d12891SEric Dumazet 	if (nskb == NULL)
2077a3d12891SEric Dumazet 		return -ENOBUFS;
2078711e2c33SJean Tourrilhes 
207915e47304SEric W. Biederman 	err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid,
2080115c9b81SGreg Rose 			       nlh->nlmsg_seq, 0, 0, ext_filter_mask);
208126932566SPatrick McHardy 	if (err < 0) {
208226932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in if_nlmsg_size */
208326932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
208426932566SPatrick McHardy 		kfree_skb(nskb);
2085a3d12891SEric Dumazet 	} else
208615e47304SEric W. Biederman 		err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
2087b60c5115SThomas Graf 
2088711e2c33SJean Tourrilhes 	return err;
2089711e2c33SJean Tourrilhes }
2090711e2c33SJean Tourrilhes 
2091115c9b81SGreg Rose static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
2092c7ac8679SGreg Rose {
2093115c9b81SGreg Rose 	struct net *net = sock_net(skb->sk);
2094115c9b81SGreg Rose 	struct net_device *dev;
2095115c9b81SGreg Rose 	struct nlattr *tb[IFLA_MAX+1];
2096115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
2097115c9b81SGreg Rose 	u16 min_ifinfo_dump_size = 0;
2098115c9b81SGreg Rose 
209988c5b5ceSMichael Riesch 	if (nlmsg_parse(nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
2100a4b64fbeSEric Dumazet 			ifla_policy) >= 0) {
2101115c9b81SGreg Rose 		if (tb[IFLA_EXT_MASK])
2102115c9b81SGreg Rose 			ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2103a4b64fbeSEric Dumazet 	}
2104115c9b81SGreg Rose 
2105115c9b81SGreg Rose 	if (!ext_filter_mask)
2106115c9b81SGreg Rose 		return NLMSG_GOODSIZE;
2107115c9b81SGreg Rose 	/*
2108115c9b81SGreg Rose 	 * traverse the list of net devices and compute the minimum
2109115c9b81SGreg Rose 	 * buffer size based upon the filter mask.
2110115c9b81SGreg Rose 	 */
2111115c9b81SGreg Rose 	list_for_each_entry(dev, &net->dev_base_head, dev_list) {
2112115c9b81SGreg Rose 		min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
2113115c9b81SGreg Rose 					     if_nlmsg_size(dev,
2114115c9b81SGreg Rose 						           ext_filter_mask));
2115115c9b81SGreg Rose 	}
2116115c9b81SGreg Rose 
2117c7ac8679SGreg Rose 	return min_ifinfo_dump_size;
2118c7ac8679SGreg Rose }
2119c7ac8679SGreg Rose 
212042bad1daSAdrian Bunk static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
21211da177e4SLinus Torvalds {
21221da177e4SLinus Torvalds 	int idx;
21231da177e4SLinus Torvalds 	int s_idx = cb->family;
21241da177e4SLinus Torvalds 
21251da177e4SLinus Torvalds 	if (s_idx == 0)
21261da177e4SLinus Torvalds 		s_idx = 1;
212725239ceeSPatrick McHardy 	for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
21281da177e4SLinus Torvalds 		int type = cb->nlh->nlmsg_type-RTM_BASE;
21291da177e4SLinus Torvalds 		if (idx < s_idx || idx == PF_PACKET)
21301da177e4SLinus Torvalds 			continue;
2131e2849863SThomas Graf 		if (rtnl_msg_handlers[idx] == NULL ||
2132e2849863SThomas Graf 		    rtnl_msg_handlers[idx][type].dumpit == NULL)
21331da177e4SLinus Torvalds 			continue;
21340465277fSNicolas Dichtel 		if (idx > s_idx) {
21351da177e4SLinus Torvalds 			memset(&cb->args[0], 0, sizeof(cb->args));
21360465277fSNicolas Dichtel 			cb->prev_seq = 0;
21370465277fSNicolas Dichtel 			cb->seq = 0;
21380465277fSNicolas Dichtel 		}
2139e2849863SThomas Graf 		if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
21401da177e4SLinus Torvalds 			break;
21411da177e4SLinus Torvalds 	}
21421da177e4SLinus Torvalds 	cb->family = idx;
21431da177e4SLinus Torvalds 
21441da177e4SLinus Torvalds 	return skb->len;
21451da177e4SLinus Torvalds }
21461da177e4SLinus Torvalds 
21477f294054SAlexei Starovoitov void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
21487f294054SAlexei Starovoitov 		  gfp_t flags)
21491da177e4SLinus Torvalds {
2150c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
21511da177e4SLinus Torvalds 	struct sk_buff *skb;
21520ec6d3f4SThomas Graf 	int err = -ENOBUFS;
2153c7ac8679SGreg Rose 	size_t if_info_size;
21541da177e4SLinus Torvalds 
21557f294054SAlexei Starovoitov 	skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
21560ec6d3f4SThomas Graf 	if (skb == NULL)
21570ec6d3f4SThomas Graf 		goto errout;
21581da177e4SLinus Torvalds 
2159115c9b81SGreg Rose 	err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0);
216026932566SPatrick McHardy 	if (err < 0) {
216126932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in if_nlmsg_size() */
216226932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
216326932566SPatrick McHardy 		kfree_skb(skb);
216426932566SPatrick McHardy 		goto errout;
216526932566SPatrick McHardy 	}
21667f294054SAlexei Starovoitov 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
21671ce85fe4SPablo Neira Ayuso 	return;
21680ec6d3f4SThomas Graf errout:
21690ec6d3f4SThomas Graf 	if (err < 0)
21704b3da706SEric W. Biederman 		rtnl_set_sk_err(net, RTNLGRP_LINK, err);
21711da177e4SLinus Torvalds }
2172471cb5a3SJiri Pirko EXPORT_SYMBOL(rtmsg_ifinfo);
21731da177e4SLinus Torvalds 
2174d83b0603SJohn Fastabend static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
2175d83b0603SJohn Fastabend 				   struct net_device *dev,
2176d83b0603SJohn Fastabend 				   u8 *addr, u32 pid, u32 seq,
21771c104a6bSNicolas Dichtel 				   int type, unsigned int flags,
21781c104a6bSNicolas Dichtel 				   int nlflags)
2179d83b0603SJohn Fastabend {
2180d83b0603SJohn Fastabend 	struct nlmsghdr *nlh;
2181d83b0603SJohn Fastabend 	struct ndmsg *ndm;
2182d83b0603SJohn Fastabend 
21831c104a6bSNicolas Dichtel 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
2184d83b0603SJohn Fastabend 	if (!nlh)
2185d83b0603SJohn Fastabend 		return -EMSGSIZE;
2186d83b0603SJohn Fastabend 
2187d83b0603SJohn Fastabend 	ndm = nlmsg_data(nlh);
2188d83b0603SJohn Fastabend 	ndm->ndm_family  = AF_BRIDGE;
2189d83b0603SJohn Fastabend 	ndm->ndm_pad1	 = 0;
2190d83b0603SJohn Fastabend 	ndm->ndm_pad2    = 0;
2191d83b0603SJohn Fastabend 	ndm->ndm_flags	 = flags;
2192d83b0603SJohn Fastabend 	ndm->ndm_type	 = 0;
2193d83b0603SJohn Fastabend 	ndm->ndm_ifindex = dev->ifindex;
2194d83b0603SJohn Fastabend 	ndm->ndm_state   = NUD_PERMANENT;
2195d83b0603SJohn Fastabend 
2196d83b0603SJohn Fastabend 	if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
2197d83b0603SJohn Fastabend 		goto nla_put_failure;
2198d83b0603SJohn Fastabend 
2199d83b0603SJohn Fastabend 	return nlmsg_end(skb, nlh);
2200d83b0603SJohn Fastabend 
2201d83b0603SJohn Fastabend nla_put_failure:
2202d83b0603SJohn Fastabend 	nlmsg_cancel(skb, nlh);
2203d83b0603SJohn Fastabend 	return -EMSGSIZE;
2204d83b0603SJohn Fastabend }
2205d83b0603SJohn Fastabend 
22063ff661c3SJohn Fastabend static inline size_t rtnl_fdb_nlmsg_size(void)
22073ff661c3SJohn Fastabend {
22083ff661c3SJohn Fastabend 	return NLMSG_ALIGN(sizeof(struct ndmsg)) + nla_total_size(ETH_ALEN);
22093ff661c3SJohn Fastabend }
22103ff661c3SJohn Fastabend 
22113ff661c3SJohn Fastabend static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, int type)
22123ff661c3SJohn Fastabend {
22133ff661c3SJohn Fastabend 	struct net *net = dev_net(dev);
22143ff661c3SJohn Fastabend 	struct sk_buff *skb;
22153ff661c3SJohn Fastabend 	int err = -ENOBUFS;
22163ff661c3SJohn Fastabend 
22173ff661c3SJohn Fastabend 	skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
22183ff661c3SJohn Fastabend 	if (!skb)
22193ff661c3SJohn Fastabend 		goto errout;
22203ff661c3SJohn Fastabend 
22211c104a6bSNicolas Dichtel 	err = nlmsg_populate_fdb_fill(skb, dev, addr, 0, 0, type, NTF_SELF, 0);
22223ff661c3SJohn Fastabend 	if (err < 0) {
22233ff661c3SJohn Fastabend 		kfree_skb(skb);
22243ff661c3SJohn Fastabend 		goto errout;
22253ff661c3SJohn Fastabend 	}
22263ff661c3SJohn Fastabend 
22273ff661c3SJohn Fastabend 	rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
22283ff661c3SJohn Fastabend 	return;
22293ff661c3SJohn Fastabend errout:
22303ff661c3SJohn Fastabend 	rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
22313ff661c3SJohn Fastabend }
22323ff661c3SJohn Fastabend 
2233090096bfSVlad Yasevich /**
2234090096bfSVlad Yasevich  * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
2235090096bfSVlad Yasevich  */
2236090096bfSVlad Yasevich int ndo_dflt_fdb_add(struct ndmsg *ndm,
2237090096bfSVlad Yasevich 		     struct nlattr *tb[],
2238090096bfSVlad Yasevich 		     struct net_device *dev,
2239090096bfSVlad Yasevich 		     const unsigned char *addr,
2240090096bfSVlad Yasevich 		     u16 flags)
2241090096bfSVlad Yasevich {
2242090096bfSVlad Yasevich 	int err = -EINVAL;
2243090096bfSVlad Yasevich 
2244090096bfSVlad Yasevich 	/* If aging addresses are supported device will need to
2245090096bfSVlad Yasevich 	 * implement its own handler for this.
2246090096bfSVlad Yasevich 	 */
2247090096bfSVlad Yasevich 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
2248090096bfSVlad Yasevich 		pr_info("%s: FDB only supports static addresses\n", dev->name);
2249090096bfSVlad Yasevich 		return err;
2250090096bfSVlad Yasevich 	}
2251090096bfSVlad Yasevich 
2252090096bfSVlad Yasevich 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2253090096bfSVlad Yasevich 		err = dev_uc_add_excl(dev, addr);
2254090096bfSVlad Yasevich 	else if (is_multicast_ether_addr(addr))
2255090096bfSVlad Yasevich 		err = dev_mc_add_excl(dev, addr);
2256090096bfSVlad Yasevich 
2257090096bfSVlad Yasevich 	/* Only return duplicate errors if NLM_F_EXCL is set */
2258090096bfSVlad Yasevich 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
2259090096bfSVlad Yasevich 		err = 0;
2260090096bfSVlad Yasevich 
2261090096bfSVlad Yasevich 	return err;
2262090096bfSVlad Yasevich }
2263090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_add);
2264090096bfSVlad Yasevich 
2265661d2967SThomas Graf static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh)
226677162022SJohn Fastabend {
226777162022SJohn Fastabend 	struct net *net = sock_net(skb->sk);
226877162022SJohn Fastabend 	struct ndmsg *ndm;
226977162022SJohn Fastabend 	struct nlattr *tb[NDA_MAX+1];
227077162022SJohn Fastabend 	struct net_device *dev;
227177162022SJohn Fastabend 	u8 *addr;
227277162022SJohn Fastabend 	int err;
227377162022SJohn Fastabend 
227477162022SJohn Fastabend 	err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
227577162022SJohn Fastabend 	if (err < 0)
227677162022SJohn Fastabend 		return err;
227777162022SJohn Fastabend 
227877162022SJohn Fastabend 	ndm = nlmsg_data(nlh);
227977162022SJohn Fastabend 	if (ndm->ndm_ifindex == 0) {
228077162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
228177162022SJohn Fastabend 		return -EINVAL;
228277162022SJohn Fastabend 	}
228377162022SJohn Fastabend 
228477162022SJohn Fastabend 	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
228577162022SJohn Fastabend 	if (dev == NULL) {
228677162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
228777162022SJohn Fastabend 		return -ENODEV;
228877162022SJohn Fastabend 	}
228977162022SJohn Fastabend 
229077162022SJohn Fastabend 	if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
229177162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
229277162022SJohn Fastabend 		return -EINVAL;
229377162022SJohn Fastabend 	}
229477162022SJohn Fastabend 
229577162022SJohn Fastabend 	addr = nla_data(tb[NDA_LLADDR]);
229677162022SJohn Fastabend 
229777162022SJohn Fastabend 	err = -EOPNOTSUPP;
229877162022SJohn Fastabend 
229977162022SJohn Fastabend 	/* Support fdb on master device the net/bridge default case */
230077162022SJohn Fastabend 	if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
230177162022SJohn Fastabend 	    (dev->priv_flags & IFF_BRIDGE_PORT)) {
2302898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2303898e5061SJiri Pirko 		const struct net_device_ops *ops = br_dev->netdev_ops;
2304898e5061SJiri Pirko 
2305898e5061SJiri Pirko 		err = ops->ndo_fdb_add(ndm, tb, dev, addr, nlh->nlmsg_flags);
230677162022SJohn Fastabend 		if (err)
230777162022SJohn Fastabend 			goto out;
230877162022SJohn Fastabend 		else
230977162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_MASTER;
231077162022SJohn Fastabend 	}
231177162022SJohn Fastabend 
231277162022SJohn Fastabend 	/* Embedded bridge, macvlan, and any other device support */
2313090096bfSVlad Yasevich 	if ((ndm->ndm_flags & NTF_SELF)) {
2314090096bfSVlad Yasevich 		if (dev->netdev_ops->ndo_fdb_add)
2315090096bfSVlad Yasevich 			err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
2316090096bfSVlad Yasevich 							   nlh->nlmsg_flags);
2317090096bfSVlad Yasevich 		else
2318090096bfSVlad Yasevich 			err = ndo_dflt_fdb_add(ndm, tb, dev, addr,
231977162022SJohn Fastabend 					       nlh->nlmsg_flags);
232077162022SJohn Fastabend 
23213ff661c3SJohn Fastabend 		if (!err) {
23223ff661c3SJohn Fastabend 			rtnl_fdb_notify(dev, addr, RTM_NEWNEIGH);
232377162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_SELF;
232477162022SJohn Fastabend 		}
23253ff661c3SJohn Fastabend 	}
232677162022SJohn Fastabend out:
232777162022SJohn Fastabend 	return err;
232877162022SJohn Fastabend }
232977162022SJohn Fastabend 
2330090096bfSVlad Yasevich /**
2331090096bfSVlad Yasevich  * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
2332090096bfSVlad Yasevich  */
2333090096bfSVlad Yasevich int ndo_dflt_fdb_del(struct ndmsg *ndm,
2334090096bfSVlad Yasevich 		     struct nlattr *tb[],
2335090096bfSVlad Yasevich 		     struct net_device *dev,
2336090096bfSVlad Yasevich 		     const unsigned char *addr)
2337090096bfSVlad Yasevich {
2338090096bfSVlad Yasevich 	int err = -EOPNOTSUPP;
2339090096bfSVlad Yasevich 
2340090096bfSVlad Yasevich 	/* If aging addresses are supported device will need to
2341090096bfSVlad Yasevich 	 * implement its own handler for this.
2342090096bfSVlad Yasevich 	 */
234364535993SSridhar Samudrala 	if (!(ndm->ndm_state & NUD_PERMANENT)) {
2344090096bfSVlad Yasevich 		pr_info("%s: FDB only supports static addresses\n", dev->name);
2345090096bfSVlad Yasevich 		return -EINVAL;
2346090096bfSVlad Yasevich 	}
2347090096bfSVlad Yasevich 
2348090096bfSVlad Yasevich 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2349090096bfSVlad Yasevich 		err = dev_uc_del(dev, addr);
2350090096bfSVlad Yasevich 	else if (is_multicast_ether_addr(addr))
2351090096bfSVlad Yasevich 		err = dev_mc_del(dev, addr);
2352090096bfSVlad Yasevich 	else
2353090096bfSVlad Yasevich 		err = -EINVAL;
2354090096bfSVlad Yasevich 
2355090096bfSVlad Yasevich 	return err;
2356090096bfSVlad Yasevich }
2357090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_del);
2358090096bfSVlad Yasevich 
2359661d2967SThomas Graf static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh)
236077162022SJohn Fastabend {
236177162022SJohn Fastabend 	struct net *net = sock_net(skb->sk);
236277162022SJohn Fastabend 	struct ndmsg *ndm;
23631690be63SVlad Yasevich 	struct nlattr *tb[NDA_MAX+1];
236477162022SJohn Fastabend 	struct net_device *dev;
236577162022SJohn Fastabend 	int err = -EINVAL;
236677162022SJohn Fastabend 	__u8 *addr;
236777162022SJohn Fastabend 
236890f62cf3SEric W. Biederman 	if (!netlink_capable(skb, CAP_NET_ADMIN))
23691690be63SVlad Yasevich 		return -EPERM;
23701690be63SVlad Yasevich 
23711690be63SVlad Yasevich 	err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
23721690be63SVlad Yasevich 	if (err < 0)
23731690be63SVlad Yasevich 		return err;
237477162022SJohn Fastabend 
237577162022SJohn Fastabend 	ndm = nlmsg_data(nlh);
237677162022SJohn Fastabend 	if (ndm->ndm_ifindex == 0) {
237777162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
237877162022SJohn Fastabend 		return -EINVAL;
237977162022SJohn Fastabend 	}
238077162022SJohn Fastabend 
238177162022SJohn Fastabend 	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
238277162022SJohn Fastabend 	if (dev == NULL) {
238377162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
238477162022SJohn Fastabend 		return -ENODEV;
238577162022SJohn Fastabend 	}
238677162022SJohn Fastabend 
23871690be63SVlad Yasevich 	if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
23881690be63SVlad Yasevich 		pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n");
238977162022SJohn Fastabend 		return -EINVAL;
239077162022SJohn Fastabend 	}
239177162022SJohn Fastabend 
23921690be63SVlad Yasevich 	addr = nla_data(tb[NDA_LLADDR]);
23931690be63SVlad Yasevich 
239477162022SJohn Fastabend 	err = -EOPNOTSUPP;
239577162022SJohn Fastabend 
239677162022SJohn Fastabend 	/* Support fdb on master device the net/bridge default case */
239777162022SJohn Fastabend 	if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
239877162022SJohn Fastabend 	    (dev->priv_flags & IFF_BRIDGE_PORT)) {
2399898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2400898e5061SJiri Pirko 		const struct net_device_ops *ops = br_dev->netdev_ops;
240177162022SJohn Fastabend 
2402898e5061SJiri Pirko 		if (ops->ndo_fdb_del)
24031690be63SVlad Yasevich 			err = ops->ndo_fdb_del(ndm, tb, dev, addr);
240477162022SJohn Fastabend 
240577162022SJohn Fastabend 		if (err)
240677162022SJohn Fastabend 			goto out;
240777162022SJohn Fastabend 		else
240877162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_MASTER;
240977162022SJohn Fastabend 	}
241077162022SJohn Fastabend 
241177162022SJohn Fastabend 	/* Embedded bridge, macvlan, and any other device support */
2412090096bfSVlad Yasevich 	if (ndm->ndm_flags & NTF_SELF) {
2413090096bfSVlad Yasevich 		if (dev->netdev_ops->ndo_fdb_del)
24141690be63SVlad Yasevich 			err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr);
2415090096bfSVlad Yasevich 		else
2416090096bfSVlad Yasevich 			err = ndo_dflt_fdb_del(ndm, tb, dev, addr);
241777162022SJohn Fastabend 
24183ff661c3SJohn Fastabend 		if (!err) {
24193ff661c3SJohn Fastabend 			rtnl_fdb_notify(dev, addr, RTM_DELNEIGH);
242077162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_SELF;
242177162022SJohn Fastabend 		}
24223ff661c3SJohn Fastabend 	}
242377162022SJohn Fastabend out:
242477162022SJohn Fastabend 	return err;
242577162022SJohn Fastabend }
242677162022SJohn Fastabend 
2427d83b0603SJohn Fastabend static int nlmsg_populate_fdb(struct sk_buff *skb,
2428d83b0603SJohn Fastabend 			      struct netlink_callback *cb,
2429d83b0603SJohn Fastabend 			      struct net_device *dev,
2430d83b0603SJohn Fastabend 			      int *idx,
2431d83b0603SJohn Fastabend 			      struct netdev_hw_addr_list *list)
2432d83b0603SJohn Fastabend {
2433d83b0603SJohn Fastabend 	struct netdev_hw_addr *ha;
2434d83b0603SJohn Fastabend 	int err;
243515e47304SEric W. Biederman 	u32 portid, seq;
2436d83b0603SJohn Fastabend 
243715e47304SEric W. Biederman 	portid = NETLINK_CB(cb->skb).portid;
2438d83b0603SJohn Fastabend 	seq = cb->nlh->nlmsg_seq;
2439d83b0603SJohn Fastabend 
2440d83b0603SJohn Fastabend 	list_for_each_entry(ha, &list->list, list) {
2441d83b0603SJohn Fastabend 		if (*idx < cb->args[0])
2442d83b0603SJohn Fastabend 			goto skip;
2443d83b0603SJohn Fastabend 
2444d83b0603SJohn Fastabend 		err = nlmsg_populate_fdb_fill(skb, dev, ha->addr,
2445a7a558feSJohn Fastabend 					      portid, seq,
24461c104a6bSNicolas Dichtel 					      RTM_NEWNEIGH, NTF_SELF,
24471c104a6bSNicolas Dichtel 					      NLM_F_MULTI);
2448d83b0603SJohn Fastabend 		if (err < 0)
2449d83b0603SJohn Fastabend 			return err;
2450d83b0603SJohn Fastabend skip:
2451d83b0603SJohn Fastabend 		*idx += 1;
2452d83b0603SJohn Fastabend 	}
2453d83b0603SJohn Fastabend 	return 0;
2454d83b0603SJohn Fastabend }
2455d83b0603SJohn Fastabend 
2456d83b0603SJohn Fastabend /**
24572c53040fSBen Hutchings  * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
2458d83b0603SJohn Fastabend  * @nlh: netlink message header
2459d83b0603SJohn Fastabend  * @dev: netdevice
2460d83b0603SJohn Fastabend  *
2461d83b0603SJohn Fastabend  * Default netdevice operation to dump the existing unicast address list.
246291f3e7b1SJohn Fastabend  * Returns number of addresses from list put in skb.
2463d83b0603SJohn Fastabend  */
2464d83b0603SJohn Fastabend int ndo_dflt_fdb_dump(struct sk_buff *skb,
2465d83b0603SJohn Fastabend 		      struct netlink_callback *cb,
2466d83b0603SJohn Fastabend 		      struct net_device *dev,
2467d83b0603SJohn Fastabend 		      int idx)
2468d83b0603SJohn Fastabend {
2469d83b0603SJohn Fastabend 	int err;
2470d83b0603SJohn Fastabend 
2471d83b0603SJohn Fastabend 	netif_addr_lock_bh(dev);
2472d83b0603SJohn Fastabend 	err = nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->uc);
2473d83b0603SJohn Fastabend 	if (err)
2474d83b0603SJohn Fastabend 		goto out;
2475d83b0603SJohn Fastabend 	nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->mc);
2476d83b0603SJohn Fastabend out:
2477d83b0603SJohn Fastabend 	netif_addr_unlock_bh(dev);
2478d83b0603SJohn Fastabend 	return idx;
2479d83b0603SJohn Fastabend }
2480d83b0603SJohn Fastabend EXPORT_SYMBOL(ndo_dflt_fdb_dump);
2481d83b0603SJohn Fastabend 
248277162022SJohn Fastabend static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
248377162022SJohn Fastabend {
248477162022SJohn Fastabend 	int idx = 0;
248577162022SJohn Fastabend 	struct net *net = sock_net(skb->sk);
248677162022SJohn Fastabend 	struct net_device *dev;
248777162022SJohn Fastabend 
248877162022SJohn Fastabend 	rcu_read_lock();
248977162022SJohn Fastabend 	for_each_netdev_rcu(net, dev) {
249077162022SJohn Fastabend 		if (dev->priv_flags & IFF_BRIDGE_PORT) {
2491898e5061SJiri Pirko 			struct net_device *br_dev;
2492898e5061SJiri Pirko 			const struct net_device_ops *ops;
249377162022SJohn Fastabend 
2494898e5061SJiri Pirko 			br_dev = netdev_master_upper_dev_get(dev);
2495898e5061SJiri Pirko 			ops = br_dev->netdev_ops;
249677162022SJohn Fastabend 			if (ops->ndo_fdb_dump)
249777162022SJohn Fastabend 				idx = ops->ndo_fdb_dump(skb, cb, dev, idx);
249877162022SJohn Fastabend 		}
249977162022SJohn Fastabend 
250077162022SJohn Fastabend 		if (dev->netdev_ops->ndo_fdb_dump)
250177162022SJohn Fastabend 			idx = dev->netdev_ops->ndo_fdb_dump(skb, cb, dev, idx);
2502090096bfSVlad Yasevich 		else
250391f3e7b1SJohn Fastabend 			idx = ndo_dflt_fdb_dump(skb, cb, dev, idx);
250477162022SJohn Fastabend 	}
250577162022SJohn Fastabend 	rcu_read_unlock();
250677162022SJohn Fastabend 
250777162022SJohn Fastabend 	cb->args[0] = idx;
250877162022SJohn Fastabend 	return skb->len;
250977162022SJohn Fastabend }
251077162022SJohn Fastabend 
2511815cccbfSJohn Fastabend int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
2512815cccbfSJohn Fastabend 			    struct net_device *dev, u16 mode)
2513815cccbfSJohn Fastabend {
2514815cccbfSJohn Fastabend 	struct nlmsghdr *nlh;
2515815cccbfSJohn Fastabend 	struct ifinfomsg *ifm;
2516815cccbfSJohn Fastabend 	struct nlattr *br_afspec;
2517815cccbfSJohn Fastabend 	u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
2518898e5061SJiri Pirko 	struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2519815cccbfSJohn Fastabend 
2520815cccbfSJohn Fastabend 	nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), NLM_F_MULTI);
2521815cccbfSJohn Fastabend 	if (nlh == NULL)
2522815cccbfSJohn Fastabend 		return -EMSGSIZE;
2523815cccbfSJohn Fastabend 
2524815cccbfSJohn Fastabend 	ifm = nlmsg_data(nlh);
2525815cccbfSJohn Fastabend 	ifm->ifi_family = AF_BRIDGE;
2526815cccbfSJohn Fastabend 	ifm->__ifi_pad = 0;
2527815cccbfSJohn Fastabend 	ifm->ifi_type = dev->type;
2528815cccbfSJohn Fastabend 	ifm->ifi_index = dev->ifindex;
2529815cccbfSJohn Fastabend 	ifm->ifi_flags = dev_get_flags(dev);
2530815cccbfSJohn Fastabend 	ifm->ifi_change = 0;
2531815cccbfSJohn Fastabend 
2532815cccbfSJohn Fastabend 
2533815cccbfSJohn Fastabend 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
2534815cccbfSJohn Fastabend 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
2535815cccbfSJohn Fastabend 	    nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
2536898e5061SJiri Pirko 	    (br_dev &&
2537898e5061SJiri Pirko 	     nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
2538815cccbfSJohn Fastabend 	    (dev->addr_len &&
2539815cccbfSJohn Fastabend 	     nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
2540815cccbfSJohn Fastabend 	    (dev->ifindex != dev->iflink &&
2541815cccbfSJohn Fastabend 	     nla_put_u32(skb, IFLA_LINK, dev->iflink)))
2542815cccbfSJohn Fastabend 		goto nla_put_failure;
2543815cccbfSJohn Fastabend 
2544815cccbfSJohn Fastabend 	br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
2545815cccbfSJohn Fastabend 	if (!br_afspec)
2546815cccbfSJohn Fastabend 		goto nla_put_failure;
2547815cccbfSJohn Fastabend 
2548815cccbfSJohn Fastabend 	if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF) ||
2549815cccbfSJohn Fastabend 	    nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
2550815cccbfSJohn Fastabend 		nla_nest_cancel(skb, br_afspec);
2551815cccbfSJohn Fastabend 		goto nla_put_failure;
2552815cccbfSJohn Fastabend 	}
2553815cccbfSJohn Fastabend 	nla_nest_end(skb, br_afspec);
2554815cccbfSJohn Fastabend 
2555815cccbfSJohn Fastabend 	return nlmsg_end(skb, nlh);
2556815cccbfSJohn Fastabend nla_put_failure:
2557815cccbfSJohn Fastabend 	nlmsg_cancel(skb, nlh);
2558815cccbfSJohn Fastabend 	return -EMSGSIZE;
2559815cccbfSJohn Fastabend }
2560815cccbfSJohn Fastabend EXPORT_SYMBOL(ndo_dflt_bridge_getlink);
2561815cccbfSJohn Fastabend 
2562e5a55a89SJohn Fastabend static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
2563e5a55a89SJohn Fastabend {
2564e5a55a89SJohn Fastabend 	struct net *net = sock_net(skb->sk);
2565e5a55a89SJohn Fastabend 	struct net_device *dev;
2566e5a55a89SJohn Fastabend 	int idx = 0;
2567e5a55a89SJohn Fastabend 	u32 portid = NETLINK_CB(cb->skb).portid;
2568e5a55a89SJohn Fastabend 	u32 seq = cb->nlh->nlmsg_seq;
25696cbdceebSVlad Yasevich 	struct nlattr *extfilt;
25706cbdceebSVlad Yasevich 	u32 filter_mask = 0;
25716cbdceebSVlad Yasevich 
25723e805ad2SAsbjoern Sloth Toennesen 	extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
25736cbdceebSVlad Yasevich 				  IFLA_EXT_MASK);
25746cbdceebSVlad Yasevich 	if (extfilt)
25756cbdceebSVlad Yasevich 		filter_mask = nla_get_u32(extfilt);
2576e5a55a89SJohn Fastabend 
2577e5a55a89SJohn Fastabend 	rcu_read_lock();
2578e5a55a89SJohn Fastabend 	for_each_netdev_rcu(net, dev) {
2579e5a55a89SJohn Fastabend 		const struct net_device_ops *ops = dev->netdev_ops;
2580898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2581e5a55a89SJohn Fastabend 
2582898e5061SJiri Pirko 		if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
258325b1e679SBen Hutchings 			if (idx >= cb->args[0] &&
2584898e5061SJiri Pirko 			    br_dev->netdev_ops->ndo_bridge_getlink(
25856cbdceebSVlad Yasevich 				    skb, portid, seq, dev, filter_mask) < 0)
2586e5a55a89SJohn Fastabend 				break;
2587e5a55a89SJohn Fastabend 			idx++;
2588e5a55a89SJohn Fastabend 		}
2589e5a55a89SJohn Fastabend 
2590e5a55a89SJohn Fastabend 		if (ops->ndo_bridge_getlink) {
259125b1e679SBen Hutchings 			if (idx >= cb->args[0] &&
25926cbdceebSVlad Yasevich 			    ops->ndo_bridge_getlink(skb, portid, seq, dev,
25936cbdceebSVlad Yasevich 						    filter_mask) < 0)
2594e5a55a89SJohn Fastabend 				break;
2595e5a55a89SJohn Fastabend 			idx++;
2596e5a55a89SJohn Fastabend 		}
2597e5a55a89SJohn Fastabend 	}
2598e5a55a89SJohn Fastabend 	rcu_read_unlock();
2599e5a55a89SJohn Fastabend 	cb->args[0] = idx;
2600e5a55a89SJohn Fastabend 
2601e5a55a89SJohn Fastabend 	return skb->len;
2602e5a55a89SJohn Fastabend }
2603e5a55a89SJohn Fastabend 
26042469ffd7SJohn Fastabend static inline size_t bridge_nlmsg_size(void)
26052469ffd7SJohn Fastabend {
26062469ffd7SJohn Fastabend 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
26072469ffd7SJohn Fastabend 		+ nla_total_size(IFNAMSIZ)	/* IFLA_IFNAME */
26082469ffd7SJohn Fastabend 		+ nla_total_size(MAX_ADDR_LEN)	/* IFLA_ADDRESS */
26092469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_MASTER */
26102469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_MTU */
26112469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_LINK */
26122469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_OPERSTATE */
26132469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u8))	/* IFLA_PROTINFO */
26142469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(struct nlattr))	/* IFLA_AF_SPEC */
26152469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u16))	/* IFLA_BRIDGE_FLAGS */
26162469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u16));	/* IFLA_BRIDGE_MODE */
26172469ffd7SJohn Fastabend }
26182469ffd7SJohn Fastabend 
26192469ffd7SJohn Fastabend static int rtnl_bridge_notify(struct net_device *dev, u16 flags)
26202469ffd7SJohn Fastabend {
26212469ffd7SJohn Fastabend 	struct net *net = dev_net(dev);
2622898e5061SJiri Pirko 	struct net_device *br_dev = netdev_master_upper_dev_get(dev);
26232469ffd7SJohn Fastabend 	struct sk_buff *skb;
26242469ffd7SJohn Fastabend 	int err = -EOPNOTSUPP;
26252469ffd7SJohn Fastabend 
26262469ffd7SJohn Fastabend 	skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
26272469ffd7SJohn Fastabend 	if (!skb) {
26282469ffd7SJohn Fastabend 		err = -ENOMEM;
26292469ffd7SJohn Fastabend 		goto errout;
26302469ffd7SJohn Fastabend 	}
26312469ffd7SJohn Fastabend 
2632c38e01b8SJohn Fastabend 	if ((!flags || (flags & BRIDGE_FLAGS_MASTER)) &&
2633898e5061SJiri Pirko 	    br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
26346cbdceebSVlad Yasevich 		err = br_dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0);
26352469ffd7SJohn Fastabend 		if (err < 0)
26362469ffd7SJohn Fastabend 			goto errout;
2637c38e01b8SJohn Fastabend 	}
2638c38e01b8SJohn Fastabend 
2639c38e01b8SJohn Fastabend 	if ((flags & BRIDGE_FLAGS_SELF) &&
2640c38e01b8SJohn Fastabend 	    dev->netdev_ops->ndo_bridge_getlink) {
26416cbdceebSVlad Yasevich 		err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0);
2642c38e01b8SJohn Fastabend 		if (err < 0)
2643c38e01b8SJohn Fastabend 			goto errout;
2644c38e01b8SJohn Fastabend 	}
26452469ffd7SJohn Fastabend 
26462469ffd7SJohn Fastabend 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
26472469ffd7SJohn Fastabend 	return 0;
26482469ffd7SJohn Fastabend errout:
26492469ffd7SJohn Fastabend 	WARN_ON(err == -EMSGSIZE);
26502469ffd7SJohn Fastabend 	kfree_skb(skb);
26512469ffd7SJohn Fastabend 	rtnl_set_sk_err(net, RTNLGRP_LINK, err);
26522469ffd7SJohn Fastabend 	return err;
26532469ffd7SJohn Fastabend }
26542469ffd7SJohn Fastabend 
2655661d2967SThomas Graf static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
2656e5a55a89SJohn Fastabend {
2657e5a55a89SJohn Fastabend 	struct net *net = sock_net(skb->sk);
2658e5a55a89SJohn Fastabend 	struct ifinfomsg *ifm;
2659e5a55a89SJohn Fastabend 	struct net_device *dev;
26602469ffd7SJohn Fastabend 	struct nlattr *br_spec, *attr = NULL;
26612469ffd7SJohn Fastabend 	int rem, err = -EOPNOTSUPP;
2662c38e01b8SJohn Fastabend 	u16 oflags, flags = 0;
2663c38e01b8SJohn Fastabend 	bool have_flags = false;
2664e5a55a89SJohn Fastabend 
2665e5a55a89SJohn Fastabend 	if (nlmsg_len(nlh) < sizeof(*ifm))
2666e5a55a89SJohn Fastabend 		return -EINVAL;
2667e5a55a89SJohn Fastabend 
2668e5a55a89SJohn Fastabend 	ifm = nlmsg_data(nlh);
2669e5a55a89SJohn Fastabend 	if (ifm->ifi_family != AF_BRIDGE)
2670e5a55a89SJohn Fastabend 		return -EPFNOSUPPORT;
2671e5a55a89SJohn Fastabend 
2672e5a55a89SJohn Fastabend 	dev = __dev_get_by_index(net, ifm->ifi_index);
2673e5a55a89SJohn Fastabend 	if (!dev) {
2674e5a55a89SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
2675e5a55a89SJohn Fastabend 		return -ENODEV;
2676e5a55a89SJohn Fastabend 	}
2677e5a55a89SJohn Fastabend 
26782469ffd7SJohn Fastabend 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
26792469ffd7SJohn Fastabend 	if (br_spec) {
26802469ffd7SJohn Fastabend 		nla_for_each_nested(attr, br_spec, rem) {
26812469ffd7SJohn Fastabend 			if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
2682c38e01b8SJohn Fastabend 				have_flags = true;
26832469ffd7SJohn Fastabend 				flags = nla_get_u16(attr);
26842469ffd7SJohn Fastabend 				break;
26852469ffd7SJohn Fastabend 			}
26862469ffd7SJohn Fastabend 		}
26872469ffd7SJohn Fastabend 	}
26882469ffd7SJohn Fastabend 
2689c38e01b8SJohn Fastabend 	oflags = flags;
2690c38e01b8SJohn Fastabend 
26912469ffd7SJohn Fastabend 	if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
2692898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2693898e5061SJiri Pirko 
2694898e5061SJiri Pirko 		if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
26952469ffd7SJohn Fastabend 			err = -EOPNOTSUPP;
2696e5a55a89SJohn Fastabend 			goto out;
2697e5a55a89SJohn Fastabend 		}
2698e5a55a89SJohn Fastabend 
2699898e5061SJiri Pirko 		err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh);
27002469ffd7SJohn Fastabend 		if (err)
27012469ffd7SJohn Fastabend 			goto out;
27022469ffd7SJohn Fastabend 
27032469ffd7SJohn Fastabend 		flags &= ~BRIDGE_FLAGS_MASTER;
27042469ffd7SJohn Fastabend 	}
27052469ffd7SJohn Fastabend 
27062469ffd7SJohn Fastabend 	if ((flags & BRIDGE_FLAGS_SELF)) {
27072469ffd7SJohn Fastabend 		if (!dev->netdev_ops->ndo_bridge_setlink)
27082469ffd7SJohn Fastabend 			err = -EOPNOTSUPP;
27092469ffd7SJohn Fastabend 		else
2710e5a55a89SJohn Fastabend 			err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh);
2711e5a55a89SJohn Fastabend 
27122469ffd7SJohn Fastabend 		if (!err)
27132469ffd7SJohn Fastabend 			flags &= ~BRIDGE_FLAGS_SELF;
27142469ffd7SJohn Fastabend 	}
27152469ffd7SJohn Fastabend 
2716c38e01b8SJohn Fastabend 	if (have_flags)
27172469ffd7SJohn Fastabend 		memcpy(nla_data(attr), &flags, sizeof(flags));
27182469ffd7SJohn Fastabend 	/* Generate event to notify upper layer of bridge change */
27192469ffd7SJohn Fastabend 	if (!err)
2720c38e01b8SJohn Fastabend 		err = rtnl_bridge_notify(dev, oflags);
2721e5a55a89SJohn Fastabend out:
2722e5a55a89SJohn Fastabend 	return err;
2723e5a55a89SJohn Fastabend }
2724e5a55a89SJohn Fastabend 
2725661d2967SThomas Graf static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
2726407af329SVlad Yasevich {
2727407af329SVlad Yasevich 	struct net *net = sock_net(skb->sk);
2728407af329SVlad Yasevich 	struct ifinfomsg *ifm;
2729407af329SVlad Yasevich 	struct net_device *dev;
2730407af329SVlad Yasevich 	struct nlattr *br_spec, *attr = NULL;
2731407af329SVlad Yasevich 	int rem, err = -EOPNOTSUPP;
2732407af329SVlad Yasevich 	u16 oflags, flags = 0;
2733407af329SVlad Yasevich 	bool have_flags = false;
2734407af329SVlad Yasevich 
2735407af329SVlad Yasevich 	if (nlmsg_len(nlh) < sizeof(*ifm))
2736407af329SVlad Yasevich 		return -EINVAL;
2737407af329SVlad Yasevich 
2738407af329SVlad Yasevich 	ifm = nlmsg_data(nlh);
2739407af329SVlad Yasevich 	if (ifm->ifi_family != AF_BRIDGE)
2740407af329SVlad Yasevich 		return -EPFNOSUPPORT;
2741407af329SVlad Yasevich 
2742407af329SVlad Yasevich 	dev = __dev_get_by_index(net, ifm->ifi_index);
2743407af329SVlad Yasevich 	if (!dev) {
2744407af329SVlad Yasevich 		pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
2745407af329SVlad Yasevich 		return -ENODEV;
2746407af329SVlad Yasevich 	}
2747407af329SVlad Yasevich 
2748407af329SVlad Yasevich 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
2749407af329SVlad Yasevich 	if (br_spec) {
2750407af329SVlad Yasevich 		nla_for_each_nested(attr, br_spec, rem) {
2751407af329SVlad Yasevich 			if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
2752407af329SVlad Yasevich 				have_flags = true;
2753407af329SVlad Yasevich 				flags = nla_get_u16(attr);
2754407af329SVlad Yasevich 				break;
2755407af329SVlad Yasevich 			}
2756407af329SVlad Yasevich 		}
2757407af329SVlad Yasevich 	}
2758407af329SVlad Yasevich 
2759407af329SVlad Yasevich 	oflags = flags;
2760407af329SVlad Yasevich 
2761407af329SVlad Yasevich 	if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
2762407af329SVlad Yasevich 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2763407af329SVlad Yasevich 
2764407af329SVlad Yasevich 		if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
2765407af329SVlad Yasevich 			err = -EOPNOTSUPP;
2766407af329SVlad Yasevich 			goto out;
2767407af329SVlad Yasevich 		}
2768407af329SVlad Yasevich 
2769407af329SVlad Yasevich 		err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh);
2770407af329SVlad Yasevich 		if (err)
2771407af329SVlad Yasevich 			goto out;
2772407af329SVlad Yasevich 
2773407af329SVlad Yasevich 		flags &= ~BRIDGE_FLAGS_MASTER;
2774407af329SVlad Yasevich 	}
2775407af329SVlad Yasevich 
2776407af329SVlad Yasevich 	if ((flags & BRIDGE_FLAGS_SELF)) {
2777407af329SVlad Yasevich 		if (!dev->netdev_ops->ndo_bridge_dellink)
2778407af329SVlad Yasevich 			err = -EOPNOTSUPP;
2779407af329SVlad Yasevich 		else
2780407af329SVlad Yasevich 			err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh);
2781407af329SVlad Yasevich 
2782407af329SVlad Yasevich 		if (!err)
2783407af329SVlad Yasevich 			flags &= ~BRIDGE_FLAGS_SELF;
2784407af329SVlad Yasevich 	}
2785407af329SVlad Yasevich 
2786407af329SVlad Yasevich 	if (have_flags)
2787407af329SVlad Yasevich 		memcpy(nla_data(attr), &flags, sizeof(flags));
2788407af329SVlad Yasevich 	/* Generate event to notify upper layer of bridge change */
2789407af329SVlad Yasevich 	if (!err)
2790407af329SVlad Yasevich 		err = rtnl_bridge_notify(dev, oflags);
2791407af329SVlad Yasevich out:
2792407af329SVlad Yasevich 	return err;
2793407af329SVlad Yasevich }
2794407af329SVlad Yasevich 
27951da177e4SLinus Torvalds /* Process one rtnetlink message. */
27961da177e4SLinus Torvalds 
27971d00a4ebSThomas Graf static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
27981da177e4SLinus Torvalds {
27993b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
2800e2849863SThomas Graf 	rtnl_doit_func doit;
28011da177e4SLinus Torvalds 	int sz_idx, kind;
28021da177e4SLinus Torvalds 	int family;
28031da177e4SLinus Torvalds 	int type;
28042907c35fSEric Dumazet 	int err;
28051da177e4SLinus Torvalds 
28061da177e4SLinus Torvalds 	type = nlh->nlmsg_type;
28071da177e4SLinus Torvalds 	if (type > RTM_MAX)
2808038890feSThomas Graf 		return -EOPNOTSUPP;
28091da177e4SLinus Torvalds 
28101da177e4SLinus Torvalds 	type -= RTM_BASE;
28111da177e4SLinus Torvalds 
28121da177e4SLinus Torvalds 	/* All the messages must have at least 1 byte length */
2813573ce260SHong zhi guo 	if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
28141da177e4SLinus Torvalds 		return 0;
28151da177e4SLinus Torvalds 
2816573ce260SHong zhi guo 	family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
28171da177e4SLinus Torvalds 	sz_idx = type>>2;
28181da177e4SLinus Torvalds 	kind = type&3;
28191da177e4SLinus Torvalds 
282090f62cf3SEric W. Biederman 	if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
28211d00a4ebSThomas Graf 		return -EPERM;
28221da177e4SLinus Torvalds 
2823b8f3ab42SDavid S. Miller 	if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
282497c53cacSDenis V. Lunev 		struct sock *rtnl;
2825e2849863SThomas Graf 		rtnl_dumpit_func dumpit;
2826c7ac8679SGreg Rose 		rtnl_calcit_func calcit;
2827c7ac8679SGreg Rose 		u16 min_dump_alloc = 0;
28281da177e4SLinus Torvalds 
2829e2849863SThomas Graf 		dumpit = rtnl_get_dumpit(family, type);
2830e2849863SThomas Graf 		if (dumpit == NULL)
2831038890feSThomas Graf 			return -EOPNOTSUPP;
2832c7ac8679SGreg Rose 		calcit = rtnl_get_calcit(family, type);
2833c7ac8679SGreg Rose 		if (calcit)
2834115c9b81SGreg Rose 			min_dump_alloc = calcit(skb, nlh);
28351da177e4SLinus Torvalds 
28362907c35fSEric Dumazet 		__rtnl_unlock();
283797c53cacSDenis V. Lunev 		rtnl = net->rtnl;
283880d326faSPablo Neira Ayuso 		{
283980d326faSPablo Neira Ayuso 			struct netlink_dump_control c = {
284080d326faSPablo Neira Ayuso 				.dump		= dumpit,
284180d326faSPablo Neira Ayuso 				.min_dump_alloc	= min_dump_alloc,
284280d326faSPablo Neira Ayuso 			};
284380d326faSPablo Neira Ayuso 			err = netlink_dump_start(rtnl, skb, nlh, &c);
284480d326faSPablo Neira Ayuso 		}
28452907c35fSEric Dumazet 		rtnl_lock();
28462907c35fSEric Dumazet 		return err;
28471da177e4SLinus Torvalds 	}
28481da177e4SLinus Torvalds 
2849e2849863SThomas Graf 	doit = rtnl_get_doit(family, type);
2850e2849863SThomas Graf 	if (doit == NULL)
2851038890feSThomas Graf 		return -EOPNOTSUPP;
28521da177e4SLinus Torvalds 
2853661d2967SThomas Graf 	return doit(skb, nlh);
28541da177e4SLinus Torvalds }
28551da177e4SLinus Torvalds 
2856cd40b7d3SDenis V. Lunev static void rtnetlink_rcv(struct sk_buff *skb)
28571da177e4SLinus Torvalds {
28581536cc0dSDenis V. Lunev 	rtnl_lock();
2859cd40b7d3SDenis V. Lunev 	netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
28601536cc0dSDenis V. Lunev 	rtnl_unlock();
28611da177e4SLinus Torvalds }
28621da177e4SLinus Torvalds 
28631da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
28641da177e4SLinus Torvalds {
2865351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2866e9dc8653SEric W. Biederman 
28671da177e4SLinus Torvalds 	switch (event) {
28681da177e4SLinus Torvalds 	case NETDEV_UP:
28691da177e4SLinus Torvalds 	case NETDEV_DOWN:
287010de05afSPatrick McHardy 	case NETDEV_PRE_UP:
2871d90a909eSEric W. Biederman 	case NETDEV_POST_INIT:
2872d90a909eSEric W. Biederman 	case NETDEV_REGISTER:
28731da177e4SLinus Torvalds 	case NETDEV_CHANGE:
2874755d0e77SPatrick McHardy 	case NETDEV_PRE_TYPE_CHANGE:
28751da177e4SLinus Torvalds 	case NETDEV_GOING_DOWN:
2876a2835763SPatrick McHardy 	case NETDEV_UNREGISTER:
28770115e8e3SEric Dumazet 	case NETDEV_UNREGISTER_FINAL:
2878ac3d3f81SAmerigo Wang 	case NETDEV_RELEASE:
2879ac3d3f81SAmerigo Wang 	case NETDEV_JOIN:
28801da177e4SLinus Torvalds 		break;
28811da177e4SLinus Torvalds 	default:
28827f294054SAlexei Starovoitov 		rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
28831da177e4SLinus Torvalds 		break;
28841da177e4SLinus Torvalds 	}
28851da177e4SLinus Torvalds 	return NOTIFY_DONE;
28861da177e4SLinus Torvalds }
28871da177e4SLinus Torvalds 
28881da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = {
28891da177e4SLinus Torvalds 	.notifier_call	= rtnetlink_event,
28901da177e4SLinus Torvalds };
28911da177e4SLinus Torvalds 
289297c53cacSDenis V. Lunev 
28932c8c1e72SAlexey Dobriyan static int __net_init rtnetlink_net_init(struct net *net)
289497c53cacSDenis V. Lunev {
289597c53cacSDenis V. Lunev 	struct sock *sk;
2896a31f2d17SPablo Neira Ayuso 	struct netlink_kernel_cfg cfg = {
2897a31f2d17SPablo Neira Ayuso 		.groups		= RTNLGRP_MAX,
2898a31f2d17SPablo Neira Ayuso 		.input		= rtnetlink_rcv,
2899a31f2d17SPablo Neira Ayuso 		.cb_mutex	= &rtnl_mutex,
29009785e10aSPablo Neira Ayuso 		.flags		= NL_CFG_F_NONROOT_RECV,
2901a31f2d17SPablo Neira Ayuso 	};
2902a31f2d17SPablo Neira Ayuso 
29039f00d977SPablo Neira Ayuso 	sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
290497c53cacSDenis V. Lunev 	if (!sk)
290597c53cacSDenis V. Lunev 		return -ENOMEM;
290697c53cacSDenis V. Lunev 	net->rtnl = sk;
290797c53cacSDenis V. Lunev 	return 0;
290897c53cacSDenis V. Lunev }
290997c53cacSDenis V. Lunev 
29102c8c1e72SAlexey Dobriyan static void __net_exit rtnetlink_net_exit(struct net *net)
291197c53cacSDenis V. Lunev {
2912b7c6ba6eSDenis V. Lunev 	netlink_kernel_release(net->rtnl);
291397c53cacSDenis V. Lunev 	net->rtnl = NULL;
291497c53cacSDenis V. Lunev }
291597c53cacSDenis V. Lunev 
291697c53cacSDenis V. Lunev static struct pernet_operations rtnetlink_net_ops = {
291797c53cacSDenis V. Lunev 	.init = rtnetlink_net_init,
291897c53cacSDenis V. Lunev 	.exit = rtnetlink_net_exit,
291997c53cacSDenis V. Lunev };
292097c53cacSDenis V. Lunev 
29211da177e4SLinus Torvalds void __init rtnetlink_init(void)
29221da177e4SLinus Torvalds {
292397c53cacSDenis V. Lunev 	if (register_pernet_subsys(&rtnetlink_net_ops))
29241da177e4SLinus Torvalds 		panic("rtnetlink_init: cannot initialize rtnetlink\n");
292597c53cacSDenis V. Lunev 
29261da177e4SLinus Torvalds 	register_netdevice_notifier(&rtnetlink_dev_notifier);
2927340d17fcSThomas Graf 
2928c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
2929c7ac8679SGreg Rose 		      rtnl_dump_ifinfo, rtnl_calcit);
2930c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL);
2931c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL);
2932c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL);
2933687ad8ccSThomas Graf 
2934c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL);
2935c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL);
293677162022SJohn Fastabend 
293777162022SJohn Fastabend 	rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, NULL);
293877162022SJohn Fastabend 	rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, NULL);
293977162022SJohn Fastabend 	rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, NULL);
2940e5a55a89SJohn Fastabend 
2941e5a55a89SJohn Fastabend 	rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, NULL);
2942407af329SVlad Yasevich 	rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, NULL);
2943e5a55a89SJohn Fastabend 	rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, NULL);
29441da177e4SLinus Torvalds }
29451da177e4SLinus Torvalds 
2946