xref: /openbmc/linux/net/core/rtnetlink.c (revision 1d3ee88ae0d605629bf369ab0b868dae8ca62a48)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * INET		An implementation of the TCP/IP protocol suite for the LINUX
31da177e4SLinus Torvalds  *		operating system.  INET is implemented using the  BSD Socket
41da177e4SLinus Torvalds  *		interface as the means of communication with the user level.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *		Routing netlink socket interface: protocol independent part.
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  *		This program is free software; you can redistribute it and/or
111da177e4SLinus Torvalds  *		modify it under the terms of the GNU General Public License
121da177e4SLinus Torvalds  *		as published by the Free Software Foundation; either version
131da177e4SLinus Torvalds  *		2 of the License, or (at your option) any later version.
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  *	Fixes:
161da177e4SLinus Torvalds  *	Vitaly E. Lavrov		RTA_OK arithmetics was wrong.
171da177e4SLinus Torvalds  */
181da177e4SLinus Torvalds 
191da177e4SLinus Torvalds #include <linux/errno.h>
201da177e4SLinus Torvalds #include <linux/module.h>
211da177e4SLinus Torvalds #include <linux/types.h>
221da177e4SLinus Torvalds #include <linux/socket.h>
231da177e4SLinus Torvalds #include <linux/kernel.h>
241da177e4SLinus Torvalds #include <linux/timer.h>
251da177e4SLinus Torvalds #include <linux/string.h>
261da177e4SLinus Torvalds #include <linux/sockios.h>
271da177e4SLinus Torvalds #include <linux/net.h>
281da177e4SLinus Torvalds #include <linux/fcntl.h>
291da177e4SLinus Torvalds #include <linux/mm.h>
301da177e4SLinus Torvalds #include <linux/slab.h>
311da177e4SLinus Torvalds #include <linux/interrupt.h>
321da177e4SLinus Torvalds #include <linux/capability.h>
331da177e4SLinus Torvalds #include <linux/skbuff.h>
341da177e4SLinus Torvalds #include <linux/init.h>
351da177e4SLinus Torvalds #include <linux/security.h>
366756ae4bSStephen Hemminger #include <linux/mutex.h>
371823730fSThomas Graf #include <linux/if_addr.h>
3877162022SJohn Fastabend #include <linux/if_bridge.h>
39ebc08a6fSWilliams, Mitch A #include <linux/pci.h>
4077162022SJohn Fastabend #include <linux/etherdevice.h>
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds #include <asm/uaccess.h>
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds #include <linux/inet.h>
451da177e4SLinus Torvalds #include <linux/netdevice.h>
461da177e4SLinus Torvalds #include <net/ip.h>
471da177e4SLinus Torvalds #include <net/protocol.h>
481da177e4SLinus Torvalds #include <net/arp.h>
491da177e4SLinus Torvalds #include <net/route.h>
501da177e4SLinus Torvalds #include <net/udp.h>
511da177e4SLinus Torvalds #include <net/sock.h>
521da177e4SLinus Torvalds #include <net/pkt_sched.h>
5314c0b97dSThomas Graf #include <net/fib_rules.h>
54e2849863SThomas Graf #include <net/rtnetlink.h>
5530ffee84SJohannes Berg #include <net/net_namespace.h>
561da177e4SLinus Torvalds 
57e0d087afSEric Dumazet struct rtnl_link {
58e2849863SThomas Graf 	rtnl_doit_func		doit;
59e2849863SThomas Graf 	rtnl_dumpit_func	dumpit;
60c7ac8679SGreg Rose 	rtnl_calcit_func 	calcit;
61e2849863SThomas Graf };
62e2849863SThomas Graf 
636756ae4bSStephen Hemminger static DEFINE_MUTEX(rtnl_mutex);
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds void rtnl_lock(void)
661da177e4SLinus Torvalds {
676756ae4bSStephen Hemminger 	mutex_lock(&rtnl_mutex);
681da177e4SLinus Torvalds }
69e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_lock);
701da177e4SLinus Torvalds 
716756ae4bSStephen Hemminger void __rtnl_unlock(void)
721da177e4SLinus Torvalds {
736756ae4bSStephen Hemminger 	mutex_unlock(&rtnl_mutex);
741da177e4SLinus Torvalds }
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds void rtnl_unlock(void)
771da177e4SLinus Torvalds {
7858ec3b4dSHerbert Xu 	/* This fellow will unlock it for us. */
791da177e4SLinus Torvalds 	netdev_run_todo();
801da177e4SLinus Torvalds }
81e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unlock);
821da177e4SLinus Torvalds 
836756ae4bSStephen Hemminger int rtnl_trylock(void)
846756ae4bSStephen Hemminger {
856756ae4bSStephen Hemminger 	return mutex_trylock(&rtnl_mutex);
866756ae4bSStephen Hemminger }
87e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_trylock);
886756ae4bSStephen Hemminger 
89c9c1014bSPatrick McHardy int rtnl_is_locked(void)
90c9c1014bSPatrick McHardy {
91c9c1014bSPatrick McHardy 	return mutex_is_locked(&rtnl_mutex);
92c9c1014bSPatrick McHardy }
93e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_is_locked);
94c9c1014bSPatrick McHardy 
95a898def2SPaul E. McKenney #ifdef CONFIG_PROVE_LOCKING
96a898def2SPaul E. McKenney int lockdep_rtnl_is_held(void)
97a898def2SPaul E. McKenney {
98a898def2SPaul E. McKenney 	return lockdep_is_held(&rtnl_mutex);
99a898def2SPaul E. McKenney }
100a898def2SPaul E. McKenney EXPORT_SYMBOL(lockdep_rtnl_is_held);
101a898def2SPaul E. McKenney #endif /* #ifdef CONFIG_PROVE_LOCKING */
102a898def2SPaul E. McKenney 
10325239ceeSPatrick McHardy static struct rtnl_link *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
104e2849863SThomas Graf 
105e2849863SThomas Graf static inline int rtm_msgindex(int msgtype)
106e2849863SThomas Graf {
107e2849863SThomas Graf 	int msgindex = msgtype - RTM_BASE;
108e2849863SThomas Graf 
109e2849863SThomas Graf 	/*
110e2849863SThomas Graf 	 * msgindex < 0 implies someone tried to register a netlink
111e2849863SThomas Graf 	 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
112e2849863SThomas Graf 	 * the message type has not been added to linux/rtnetlink.h
113e2849863SThomas Graf 	 */
114e2849863SThomas Graf 	BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
115e2849863SThomas Graf 
116e2849863SThomas Graf 	return msgindex;
117e2849863SThomas Graf }
118e2849863SThomas Graf 
119e2849863SThomas Graf static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex)
120e2849863SThomas Graf {
121e2849863SThomas Graf 	struct rtnl_link *tab;
122e2849863SThomas Graf 
12325239ceeSPatrick McHardy 	if (protocol <= RTNL_FAMILY_MAX)
124e2849863SThomas Graf 		tab = rtnl_msg_handlers[protocol];
1250f87b1ddSPatrick McHardy 	else
1260f87b1ddSPatrick McHardy 		tab = NULL;
1270f87b1ddSPatrick McHardy 
12851057f2fSThomas Graf 	if (tab == NULL || tab[msgindex].doit == NULL)
129e2849863SThomas Graf 		tab = rtnl_msg_handlers[PF_UNSPEC];
130e2849863SThomas Graf 
131c80bbeaeSHans Zhang 	return tab[msgindex].doit;
132e2849863SThomas Graf }
133e2849863SThomas Graf 
134e2849863SThomas Graf static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex)
135e2849863SThomas Graf {
136e2849863SThomas Graf 	struct rtnl_link *tab;
137e2849863SThomas Graf 
13825239ceeSPatrick McHardy 	if (protocol <= RTNL_FAMILY_MAX)
139e2849863SThomas Graf 		tab = rtnl_msg_handlers[protocol];
1400f87b1ddSPatrick McHardy 	else
1410f87b1ddSPatrick McHardy 		tab = NULL;
1420f87b1ddSPatrick McHardy 
14351057f2fSThomas Graf 	if (tab == NULL || tab[msgindex].dumpit == NULL)
144e2849863SThomas Graf 		tab = rtnl_msg_handlers[PF_UNSPEC];
145e2849863SThomas Graf 
146c80bbeaeSHans Zhang 	return tab[msgindex].dumpit;
147e2849863SThomas Graf }
148e2849863SThomas Graf 
149c7ac8679SGreg Rose static rtnl_calcit_func rtnl_get_calcit(int protocol, int msgindex)
150c7ac8679SGreg Rose {
151c7ac8679SGreg Rose 	struct rtnl_link *tab;
152c7ac8679SGreg Rose 
153c7ac8679SGreg Rose 	if (protocol <= RTNL_FAMILY_MAX)
154c7ac8679SGreg Rose 		tab = rtnl_msg_handlers[protocol];
155c7ac8679SGreg Rose 	else
156c7ac8679SGreg Rose 		tab = NULL;
157c7ac8679SGreg Rose 
158c7ac8679SGreg Rose 	if (tab == NULL || tab[msgindex].calcit == NULL)
159c7ac8679SGreg Rose 		tab = rtnl_msg_handlers[PF_UNSPEC];
160c7ac8679SGreg Rose 
161c80bbeaeSHans Zhang 	return tab[msgindex].calcit;
162c7ac8679SGreg Rose }
163c7ac8679SGreg Rose 
164e2849863SThomas Graf /**
165e2849863SThomas Graf  * __rtnl_register - Register a rtnetlink message type
166e2849863SThomas Graf  * @protocol: Protocol family or PF_UNSPEC
167e2849863SThomas Graf  * @msgtype: rtnetlink message type
168e2849863SThomas Graf  * @doit: Function pointer called for each request message
169e2849863SThomas Graf  * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
170c7ac8679SGreg Rose  * @calcit: Function pointer to calc size of dump message
171e2849863SThomas Graf  *
172e2849863SThomas Graf  * Registers the specified function pointers (at least one of them has
173e2849863SThomas Graf  * to be non-NULL) to be called whenever a request message for the
174e2849863SThomas Graf  * specified protocol family and message type is received.
175e2849863SThomas Graf  *
176e2849863SThomas Graf  * The special protocol family PF_UNSPEC may be used to define fallback
177e2849863SThomas Graf  * function pointers for the case when no entry for the specific protocol
178e2849863SThomas Graf  * family exists.
179e2849863SThomas Graf  *
180e2849863SThomas Graf  * Returns 0 on success or a negative error code.
181e2849863SThomas Graf  */
182e2849863SThomas Graf int __rtnl_register(int protocol, int msgtype,
183c7ac8679SGreg Rose 		    rtnl_doit_func doit, rtnl_dumpit_func dumpit,
184c7ac8679SGreg Rose 		    rtnl_calcit_func calcit)
185e2849863SThomas Graf {
186e2849863SThomas Graf 	struct rtnl_link *tab;
187e2849863SThomas Graf 	int msgindex;
188e2849863SThomas Graf 
18925239ceeSPatrick McHardy 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
190e2849863SThomas Graf 	msgindex = rtm_msgindex(msgtype);
191e2849863SThomas Graf 
192e2849863SThomas Graf 	tab = rtnl_msg_handlers[protocol];
193e2849863SThomas Graf 	if (tab == NULL) {
194e2849863SThomas Graf 		tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL);
195e2849863SThomas Graf 		if (tab == NULL)
196e2849863SThomas Graf 			return -ENOBUFS;
197e2849863SThomas Graf 
198e2849863SThomas Graf 		rtnl_msg_handlers[protocol] = tab;
199e2849863SThomas Graf 	}
200e2849863SThomas Graf 
201e2849863SThomas Graf 	if (doit)
202e2849863SThomas Graf 		tab[msgindex].doit = doit;
203e2849863SThomas Graf 
204e2849863SThomas Graf 	if (dumpit)
205e2849863SThomas Graf 		tab[msgindex].dumpit = dumpit;
206e2849863SThomas Graf 
207c7ac8679SGreg Rose 	if (calcit)
208c7ac8679SGreg Rose 		tab[msgindex].calcit = calcit;
209c7ac8679SGreg Rose 
210e2849863SThomas Graf 	return 0;
211e2849863SThomas Graf }
212e2849863SThomas Graf EXPORT_SYMBOL_GPL(__rtnl_register);
213e2849863SThomas Graf 
214e2849863SThomas Graf /**
215e2849863SThomas Graf  * rtnl_register - Register a rtnetlink message type
216e2849863SThomas Graf  *
217e2849863SThomas Graf  * Identical to __rtnl_register() but panics on failure. This is useful
218e2849863SThomas Graf  * as failure of this function is very unlikely, it can only happen due
219e2849863SThomas Graf  * to lack of memory when allocating the chain to store all message
220e2849863SThomas Graf  * handlers for a protocol. Meant for use in init functions where lack
22125985edcSLucas De Marchi  * of memory implies no sense in continuing.
222e2849863SThomas Graf  */
223e2849863SThomas Graf void rtnl_register(int protocol, int msgtype,
224c7ac8679SGreg Rose 		   rtnl_doit_func doit, rtnl_dumpit_func dumpit,
225c7ac8679SGreg Rose 		   rtnl_calcit_func calcit)
226e2849863SThomas Graf {
227c7ac8679SGreg Rose 	if (__rtnl_register(protocol, msgtype, doit, dumpit, calcit) < 0)
228e2849863SThomas Graf 		panic("Unable to register rtnetlink message handler, "
229e2849863SThomas Graf 		      "protocol = %d, message type = %d\n",
230e2849863SThomas Graf 		      protocol, msgtype);
231e2849863SThomas Graf }
232e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_register);
233e2849863SThomas Graf 
234e2849863SThomas Graf /**
235e2849863SThomas Graf  * rtnl_unregister - Unregister a rtnetlink message type
236e2849863SThomas Graf  * @protocol: Protocol family or PF_UNSPEC
237e2849863SThomas Graf  * @msgtype: rtnetlink message type
238e2849863SThomas Graf  *
239e2849863SThomas Graf  * Returns 0 on success or a negative error code.
240e2849863SThomas Graf  */
241e2849863SThomas Graf int rtnl_unregister(int protocol, int msgtype)
242e2849863SThomas Graf {
243e2849863SThomas Graf 	int msgindex;
244e2849863SThomas Graf 
24525239ceeSPatrick McHardy 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
246e2849863SThomas Graf 	msgindex = rtm_msgindex(msgtype);
247e2849863SThomas Graf 
248e2849863SThomas Graf 	if (rtnl_msg_handlers[protocol] == NULL)
249e2849863SThomas Graf 		return -ENOENT;
250e2849863SThomas Graf 
251e2849863SThomas Graf 	rtnl_msg_handlers[protocol][msgindex].doit = NULL;
252e2849863SThomas Graf 	rtnl_msg_handlers[protocol][msgindex].dumpit = NULL;
253e2849863SThomas Graf 
254e2849863SThomas Graf 	return 0;
255e2849863SThomas Graf }
256e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister);
257e2849863SThomas Graf 
258e2849863SThomas Graf /**
259e2849863SThomas Graf  * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
260e2849863SThomas Graf  * @protocol : Protocol family or PF_UNSPEC
261e2849863SThomas Graf  *
262e2849863SThomas Graf  * Identical to calling rtnl_unregster() for all registered message types
263e2849863SThomas Graf  * of a certain protocol family.
264e2849863SThomas Graf  */
265e2849863SThomas Graf void rtnl_unregister_all(int protocol)
266e2849863SThomas Graf {
26725239ceeSPatrick McHardy 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
268e2849863SThomas Graf 
269e2849863SThomas Graf 	kfree(rtnl_msg_handlers[protocol]);
270e2849863SThomas Graf 	rtnl_msg_handlers[protocol] = NULL;
271e2849863SThomas Graf }
272e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister_all);
2731da177e4SLinus Torvalds 
27438f7b870SPatrick McHardy static LIST_HEAD(link_ops);
27538f7b870SPatrick McHardy 
276c63044f0SEric Dumazet static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
277c63044f0SEric Dumazet {
278c63044f0SEric Dumazet 	const struct rtnl_link_ops *ops;
279c63044f0SEric Dumazet 
280c63044f0SEric Dumazet 	list_for_each_entry(ops, &link_ops, list) {
281c63044f0SEric Dumazet 		if (!strcmp(ops->kind, kind))
282c63044f0SEric Dumazet 			return ops;
283c63044f0SEric Dumazet 	}
284c63044f0SEric Dumazet 	return NULL;
285c63044f0SEric Dumazet }
286c63044f0SEric Dumazet 
28738f7b870SPatrick McHardy /**
28838f7b870SPatrick McHardy  * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
28938f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to register
29038f7b870SPatrick McHardy  *
29138f7b870SPatrick McHardy  * The caller must hold the rtnl_mutex. This function should be used
29238f7b870SPatrick McHardy  * by drivers that create devices during module initialization. It
29338f7b870SPatrick McHardy  * must be called before registering the devices.
29438f7b870SPatrick McHardy  *
29538f7b870SPatrick McHardy  * Returns 0 on success or a negative error code.
29638f7b870SPatrick McHardy  */
29738f7b870SPatrick McHardy int __rtnl_link_register(struct rtnl_link_ops *ops)
29838f7b870SPatrick McHardy {
299c63044f0SEric Dumazet 	if (rtnl_link_ops_get(ops->kind))
300c63044f0SEric Dumazet 		return -EEXIST;
301c63044f0SEric Dumazet 
3022d85cba2SPatrick McHardy 	if (!ops->dellink)
30323289a37SEric Dumazet 		ops->dellink = unregister_netdevice_queue;
3042d85cba2SPatrick McHardy 
30538f7b870SPatrick McHardy 	list_add_tail(&ops->list, &link_ops);
30638f7b870SPatrick McHardy 	return 0;
30738f7b870SPatrick McHardy }
30838f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_register);
30938f7b870SPatrick McHardy 
31038f7b870SPatrick McHardy /**
31138f7b870SPatrick McHardy  * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
31238f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to register
31338f7b870SPatrick McHardy  *
31438f7b870SPatrick McHardy  * Returns 0 on success or a negative error code.
31538f7b870SPatrick McHardy  */
31638f7b870SPatrick McHardy int rtnl_link_register(struct rtnl_link_ops *ops)
31738f7b870SPatrick McHardy {
31838f7b870SPatrick McHardy 	int err;
31938f7b870SPatrick McHardy 
32038f7b870SPatrick McHardy 	rtnl_lock();
32138f7b870SPatrick McHardy 	err = __rtnl_link_register(ops);
32238f7b870SPatrick McHardy 	rtnl_unlock();
32338f7b870SPatrick McHardy 	return err;
32438f7b870SPatrick McHardy }
32538f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_register);
32638f7b870SPatrick McHardy 
327669f87baSPavel Emelyanov static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
328669f87baSPavel Emelyanov {
329669f87baSPavel Emelyanov 	struct net_device *dev;
33023289a37SEric Dumazet 	LIST_HEAD(list_kill);
33123289a37SEric Dumazet 
332669f87baSPavel Emelyanov 	for_each_netdev(net, dev) {
33323289a37SEric Dumazet 		if (dev->rtnl_link_ops == ops)
33423289a37SEric Dumazet 			ops->dellink(dev, &list_kill);
335669f87baSPavel Emelyanov 	}
33623289a37SEric Dumazet 	unregister_netdevice_many(&list_kill);
337669f87baSPavel Emelyanov }
338669f87baSPavel Emelyanov 
33938f7b870SPatrick McHardy /**
34038f7b870SPatrick McHardy  * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
34138f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to unregister
34238f7b870SPatrick McHardy  *
3432d85cba2SPatrick McHardy  * The caller must hold the rtnl_mutex.
34438f7b870SPatrick McHardy  */
34538f7b870SPatrick McHardy void __rtnl_link_unregister(struct rtnl_link_ops *ops)
34638f7b870SPatrick McHardy {
347881d966bSEric W. Biederman 	struct net *net;
3482d85cba2SPatrick McHardy 
349881d966bSEric W. Biederman 	for_each_net(net) {
350669f87baSPavel Emelyanov 		__rtnl_kill_links(net, ops);
351881d966bSEric W. Biederman 	}
35238f7b870SPatrick McHardy 	list_del(&ops->list);
35338f7b870SPatrick McHardy }
35438f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
35538f7b870SPatrick McHardy 
35638f7b870SPatrick McHardy /**
35738f7b870SPatrick McHardy  * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
35838f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to unregister
35938f7b870SPatrick McHardy  */
36038f7b870SPatrick McHardy void rtnl_link_unregister(struct rtnl_link_ops *ops)
36138f7b870SPatrick McHardy {
36238f7b870SPatrick McHardy 	rtnl_lock();
36338f7b870SPatrick McHardy 	__rtnl_link_unregister(ops);
36438f7b870SPatrick McHardy 	rtnl_unlock();
36538f7b870SPatrick McHardy }
36638f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_unregister);
36738f7b870SPatrick McHardy 
36838f7b870SPatrick McHardy static size_t rtnl_link_get_size(const struct net_device *dev)
36938f7b870SPatrick McHardy {
37038f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
37138f7b870SPatrick McHardy 	size_t size;
37238f7b870SPatrick McHardy 
37338f7b870SPatrick McHardy 	if (!ops)
37438f7b870SPatrick McHardy 		return 0;
37538f7b870SPatrick McHardy 
376369cf77aSThomas Graf 	size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
377369cf77aSThomas Graf 	       nla_total_size(strlen(ops->kind) + 1);  /* IFLA_INFO_KIND */
37838f7b870SPatrick McHardy 
37938f7b870SPatrick McHardy 	if (ops->get_size)
38038f7b870SPatrick McHardy 		/* IFLA_INFO_DATA + nested data */
381369cf77aSThomas Graf 		size += nla_total_size(sizeof(struct nlattr)) +
38238f7b870SPatrick McHardy 			ops->get_size(dev);
38338f7b870SPatrick McHardy 
38438f7b870SPatrick McHardy 	if (ops->get_xstats_size)
385369cf77aSThomas Graf 		/* IFLA_INFO_XSTATS */
386369cf77aSThomas Graf 		size += nla_total_size(ops->get_xstats_size(dev));
38738f7b870SPatrick McHardy 
38838f7b870SPatrick McHardy 	return size;
38938f7b870SPatrick McHardy }
39038f7b870SPatrick McHardy 
391f8ff182cSThomas Graf static LIST_HEAD(rtnl_af_ops);
392f8ff182cSThomas Graf 
393f8ff182cSThomas Graf static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
394f8ff182cSThomas Graf {
395f8ff182cSThomas Graf 	const struct rtnl_af_ops *ops;
396f8ff182cSThomas Graf 
397f8ff182cSThomas Graf 	list_for_each_entry(ops, &rtnl_af_ops, list) {
398f8ff182cSThomas Graf 		if (ops->family == family)
399f8ff182cSThomas Graf 			return ops;
400f8ff182cSThomas Graf 	}
401f8ff182cSThomas Graf 
402f8ff182cSThomas Graf 	return NULL;
403f8ff182cSThomas Graf }
404f8ff182cSThomas Graf 
405f8ff182cSThomas Graf /**
406f8ff182cSThomas Graf  * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
407f8ff182cSThomas Graf  * @ops: struct rtnl_af_ops * to register
408f8ff182cSThomas Graf  *
409f8ff182cSThomas Graf  * Returns 0 on success or a negative error code.
410f8ff182cSThomas Graf  */
4113678a9d8Sstephen hemminger void rtnl_af_register(struct rtnl_af_ops *ops)
412f8ff182cSThomas Graf {
413f8ff182cSThomas Graf 	rtnl_lock();
4143678a9d8Sstephen hemminger 	list_add_tail(&ops->list, &rtnl_af_ops);
415f8ff182cSThomas Graf 	rtnl_unlock();
416f8ff182cSThomas Graf }
417f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_register);
418f8ff182cSThomas Graf 
419f8ff182cSThomas Graf /**
420f8ff182cSThomas Graf  * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
421f8ff182cSThomas Graf  * @ops: struct rtnl_af_ops * to unregister
422f8ff182cSThomas Graf  *
423f8ff182cSThomas Graf  * The caller must hold the rtnl_mutex.
424f8ff182cSThomas Graf  */
425f8ff182cSThomas Graf void __rtnl_af_unregister(struct rtnl_af_ops *ops)
426f8ff182cSThomas Graf {
427f8ff182cSThomas Graf 	list_del(&ops->list);
428f8ff182cSThomas Graf }
429f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(__rtnl_af_unregister);
430f8ff182cSThomas Graf 
431f8ff182cSThomas Graf /**
432f8ff182cSThomas Graf  * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
433f8ff182cSThomas Graf  * @ops: struct rtnl_af_ops * to unregister
434f8ff182cSThomas Graf  */
435f8ff182cSThomas Graf void rtnl_af_unregister(struct rtnl_af_ops *ops)
436f8ff182cSThomas Graf {
437f8ff182cSThomas Graf 	rtnl_lock();
438f8ff182cSThomas Graf 	__rtnl_af_unregister(ops);
439f8ff182cSThomas Graf 	rtnl_unlock();
440f8ff182cSThomas Graf }
441f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_unregister);
442f8ff182cSThomas Graf 
443f8ff182cSThomas Graf static size_t rtnl_link_get_af_size(const struct net_device *dev)
444f8ff182cSThomas Graf {
445f8ff182cSThomas Graf 	struct rtnl_af_ops *af_ops;
446f8ff182cSThomas Graf 	size_t size;
447f8ff182cSThomas Graf 
448f8ff182cSThomas Graf 	/* IFLA_AF_SPEC */
449f8ff182cSThomas Graf 	size = nla_total_size(sizeof(struct nlattr));
450f8ff182cSThomas Graf 
451f8ff182cSThomas Graf 	list_for_each_entry(af_ops, &rtnl_af_ops, list) {
452f8ff182cSThomas Graf 		if (af_ops->get_link_af_size) {
453f8ff182cSThomas Graf 			/* AF_* + nested data */
454f8ff182cSThomas Graf 			size += nla_total_size(sizeof(struct nlattr)) +
455f8ff182cSThomas Graf 				af_ops->get_link_af_size(dev);
456f8ff182cSThomas Graf 		}
457f8ff182cSThomas Graf 	}
458f8ff182cSThomas Graf 
459f8ff182cSThomas Graf 	return size;
460f8ff182cSThomas Graf }
461f8ff182cSThomas Graf 
46238f7b870SPatrick McHardy static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
46338f7b870SPatrick McHardy {
46438f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
46538f7b870SPatrick McHardy 	struct nlattr *linkinfo, *data;
46638f7b870SPatrick McHardy 	int err = -EMSGSIZE;
46738f7b870SPatrick McHardy 
46838f7b870SPatrick McHardy 	linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
46938f7b870SPatrick McHardy 	if (linkinfo == NULL)
47038f7b870SPatrick McHardy 		goto out;
47138f7b870SPatrick McHardy 
47238f7b870SPatrick McHardy 	if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
47338f7b870SPatrick McHardy 		goto err_cancel_link;
47438f7b870SPatrick McHardy 	if (ops->fill_xstats) {
47538f7b870SPatrick McHardy 		err = ops->fill_xstats(skb, dev);
47638f7b870SPatrick McHardy 		if (err < 0)
47738f7b870SPatrick McHardy 			goto err_cancel_link;
47838f7b870SPatrick McHardy 	}
47938f7b870SPatrick McHardy 	if (ops->fill_info) {
48038f7b870SPatrick McHardy 		data = nla_nest_start(skb, IFLA_INFO_DATA);
481fcca143dSWei Yongjun 		if (data == NULL) {
482fcca143dSWei Yongjun 			err = -EMSGSIZE;
48338f7b870SPatrick McHardy 			goto err_cancel_link;
484fcca143dSWei Yongjun 		}
48538f7b870SPatrick McHardy 		err = ops->fill_info(skb, dev);
48638f7b870SPatrick McHardy 		if (err < 0)
48738f7b870SPatrick McHardy 			goto err_cancel_data;
48838f7b870SPatrick McHardy 		nla_nest_end(skb, data);
48938f7b870SPatrick McHardy 	}
49038f7b870SPatrick McHardy 
49138f7b870SPatrick McHardy 	nla_nest_end(skb, linkinfo);
49238f7b870SPatrick McHardy 	return 0;
49338f7b870SPatrick McHardy 
49438f7b870SPatrick McHardy err_cancel_data:
49538f7b870SPatrick McHardy 	nla_nest_cancel(skb, data);
49638f7b870SPatrick McHardy err_cancel_link:
49738f7b870SPatrick McHardy 	nla_nest_cancel(skb, linkinfo);
49838f7b870SPatrick McHardy out:
49938f7b870SPatrick McHardy 	return err;
50038f7b870SPatrick McHardy }
50138f7b870SPatrick McHardy 
50295c96174SEric Dumazet int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
5031da177e4SLinus Torvalds {
50497c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
5051da177e4SLinus Torvalds 	int err = 0;
5061da177e4SLinus Torvalds 
507ac6d439dSPatrick McHardy 	NETLINK_CB(skb).dst_group = group;
5081da177e4SLinus Torvalds 	if (echo)
5091da177e4SLinus Torvalds 		atomic_inc(&skb->users);
5101da177e4SLinus Torvalds 	netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
5111da177e4SLinus Torvalds 	if (echo)
5121da177e4SLinus Torvalds 		err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
5131da177e4SLinus Torvalds 	return err;
5141da177e4SLinus Torvalds }
5151da177e4SLinus Torvalds 
51697c53cacSDenis V. Lunev int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
5172942e900SThomas Graf {
51897c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
51997c53cacSDenis V. Lunev 
5202942e900SThomas Graf 	return nlmsg_unicast(rtnl, skb, pid);
5212942e900SThomas Graf }
522e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unicast);
5232942e900SThomas Graf 
5241ce85fe4SPablo Neira Ayuso void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
52597676b6bSThomas Graf 		 struct nlmsghdr *nlh, gfp_t flags)
52697676b6bSThomas Graf {
52797c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
52897676b6bSThomas Graf 	int report = 0;
52997676b6bSThomas Graf 
53097676b6bSThomas Graf 	if (nlh)
53197676b6bSThomas Graf 		report = nlmsg_report(nlh);
53297676b6bSThomas Graf 
5331ce85fe4SPablo Neira Ayuso 	nlmsg_notify(rtnl, skb, pid, group, report, flags);
53497676b6bSThomas Graf }
535e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_notify);
53697676b6bSThomas Graf 
53797c53cacSDenis V. Lunev void rtnl_set_sk_err(struct net *net, u32 group, int error)
53897676b6bSThomas Graf {
53997c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
54097c53cacSDenis V. Lunev 
54197676b6bSThomas Graf 	netlink_set_err(rtnl, 0, group, error);
54297676b6bSThomas Graf }
543e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_set_sk_err);
54497676b6bSThomas Graf 
5451da177e4SLinus Torvalds int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
5461da177e4SLinus Torvalds {
5472d7202bfSThomas Graf 	struct nlattr *mx;
5482d7202bfSThomas Graf 	int i, valid = 0;
5491da177e4SLinus Torvalds 
5502d7202bfSThomas Graf 	mx = nla_nest_start(skb, RTA_METRICS);
5512d7202bfSThomas Graf 	if (mx == NULL)
5522d7202bfSThomas Graf 		return -ENOBUFS;
5532d7202bfSThomas Graf 
5541da177e4SLinus Torvalds 	for (i = 0; i < RTAX_MAX; i++) {
5552d7202bfSThomas Graf 		if (metrics[i]) {
5562d7202bfSThomas Graf 			valid++;
557a6574349SDavid S. Miller 			if (nla_put_u32(skb, i+1, metrics[i]))
558a6574349SDavid S. Miller 				goto nla_put_failure;
5591da177e4SLinus Torvalds 		}
5602d7202bfSThomas Graf 	}
5611da177e4SLinus Torvalds 
562a57d27fcSDavid S. Miller 	if (!valid) {
563a57d27fcSDavid S. Miller 		nla_nest_cancel(skb, mx);
564a57d27fcSDavid S. Miller 		return 0;
565a57d27fcSDavid S. Miller 	}
5662d7202bfSThomas Graf 
5672d7202bfSThomas Graf 	return nla_nest_end(skb, mx);
5682d7202bfSThomas Graf 
5692d7202bfSThomas Graf nla_put_failure:
570bc3ed28cSThomas Graf 	nla_nest_cancel(skb, mx);
571bc3ed28cSThomas Graf 	return -EMSGSIZE;
5721da177e4SLinus Torvalds }
573e0d087afSEric Dumazet EXPORT_SYMBOL(rtnetlink_put_metrics);
5741da177e4SLinus Torvalds 
575e3703b3dSThomas Graf int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
57687a50699SDavid S. Miller 		       long expires, u32 error)
577e3703b3dSThomas Graf {
578e3703b3dSThomas Graf 	struct rta_cacheinfo ci = {
579a399a805SEric Dumazet 		.rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse),
580e3703b3dSThomas Graf 		.rta_used = dst->__use,
581e3703b3dSThomas Graf 		.rta_clntref = atomic_read(&(dst->__refcnt)),
582e3703b3dSThomas Graf 		.rta_error = error,
583e3703b3dSThomas Graf 		.rta_id =  id,
584e3703b3dSThomas Graf 	};
585e3703b3dSThomas Graf 
5868253947eSLi Wei 	if (expires) {
5878253947eSLi Wei 		unsigned long clock;
588e3703b3dSThomas Graf 
5898253947eSLi Wei 		clock = jiffies_to_clock_t(abs(expires));
5908253947eSLi Wei 		clock = min_t(unsigned long, clock, INT_MAX);
5918253947eSLi Wei 		ci.rta_expires = (expires > 0) ? clock : -clock;
5928253947eSLi Wei 	}
593e3703b3dSThomas Graf 	return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
594e3703b3dSThomas Graf }
595e3703b3dSThomas Graf EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
5961da177e4SLinus Torvalds 
59793b2d4a2SDavid S. Miller static void set_operstate(struct net_device *dev, unsigned char transition)
598b00055aaSStefan Rompf {
599b00055aaSStefan Rompf 	unsigned char operstate = dev->operstate;
600b00055aaSStefan Rompf 
601b00055aaSStefan Rompf 	switch (transition) {
602b00055aaSStefan Rompf 	case IF_OPER_UP:
603b00055aaSStefan Rompf 		if ((operstate == IF_OPER_DORMANT ||
604b00055aaSStefan Rompf 		     operstate == IF_OPER_UNKNOWN) &&
605b00055aaSStefan Rompf 		    !netif_dormant(dev))
606b00055aaSStefan Rompf 			operstate = IF_OPER_UP;
607b00055aaSStefan Rompf 		break;
608b00055aaSStefan Rompf 
609b00055aaSStefan Rompf 	case IF_OPER_DORMANT:
610b00055aaSStefan Rompf 		if (operstate == IF_OPER_UP ||
611b00055aaSStefan Rompf 		    operstate == IF_OPER_UNKNOWN)
612b00055aaSStefan Rompf 			operstate = IF_OPER_DORMANT;
613b00055aaSStefan Rompf 		break;
6143ff50b79SStephen Hemminger 	}
615b00055aaSStefan Rompf 
616b00055aaSStefan Rompf 	if (dev->operstate != operstate) {
617b00055aaSStefan Rompf 		write_lock_bh(&dev_base_lock);
618b00055aaSStefan Rompf 		dev->operstate = operstate;
619b00055aaSStefan Rompf 		write_unlock_bh(&dev_base_lock);
620b00055aaSStefan Rompf 		netdev_state_change(dev);
62193b2d4a2SDavid S. Miller 	}
622b00055aaSStefan Rompf }
623b00055aaSStefan Rompf 
624b1beb681SJiri Benc static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
625b1beb681SJiri Benc {
626b1beb681SJiri Benc 	return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
627b1beb681SJiri Benc 	       (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
628b1beb681SJiri Benc }
629b1beb681SJiri Benc 
6303729d502SPatrick McHardy static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
6313729d502SPatrick McHardy 					   const struct ifinfomsg *ifm)
6323729d502SPatrick McHardy {
6333729d502SPatrick McHardy 	unsigned int flags = ifm->ifi_flags;
6343729d502SPatrick McHardy 
6353729d502SPatrick McHardy 	/* bugwards compatibility: ifi_change == 0 is treated as ~0 */
6363729d502SPatrick McHardy 	if (ifm->ifi_change)
6373729d502SPatrick McHardy 		flags = (flags & ifm->ifi_change) |
638b1beb681SJiri Benc 			(rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
6393729d502SPatrick McHardy 
6403729d502SPatrick McHardy 	return flags;
6413729d502SPatrick McHardy }
6423729d502SPatrick McHardy 
643b60c5115SThomas Graf static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
644be1f3c2cSBen Hutchings 				 const struct rtnl_link_stats64 *b)
6451da177e4SLinus Torvalds {
646b60c5115SThomas Graf 	a->rx_packets = b->rx_packets;
647b60c5115SThomas Graf 	a->tx_packets = b->tx_packets;
648b60c5115SThomas Graf 	a->rx_bytes = b->rx_bytes;
649b60c5115SThomas Graf 	a->tx_bytes = b->tx_bytes;
650b60c5115SThomas Graf 	a->rx_errors = b->rx_errors;
651b60c5115SThomas Graf 	a->tx_errors = b->tx_errors;
652b60c5115SThomas Graf 	a->rx_dropped = b->rx_dropped;
653b60c5115SThomas Graf 	a->tx_dropped = b->tx_dropped;
654b60c5115SThomas Graf 
655b60c5115SThomas Graf 	a->multicast = b->multicast;
656b60c5115SThomas Graf 	a->collisions = b->collisions;
657b60c5115SThomas Graf 
658b60c5115SThomas Graf 	a->rx_length_errors = b->rx_length_errors;
659b60c5115SThomas Graf 	a->rx_over_errors = b->rx_over_errors;
660b60c5115SThomas Graf 	a->rx_crc_errors = b->rx_crc_errors;
661b60c5115SThomas Graf 	a->rx_frame_errors = b->rx_frame_errors;
662b60c5115SThomas Graf 	a->rx_fifo_errors = b->rx_fifo_errors;
663b60c5115SThomas Graf 	a->rx_missed_errors = b->rx_missed_errors;
664b60c5115SThomas Graf 
665b60c5115SThomas Graf 	a->tx_aborted_errors = b->tx_aborted_errors;
666b60c5115SThomas Graf 	a->tx_carrier_errors = b->tx_carrier_errors;
667b60c5115SThomas Graf 	a->tx_fifo_errors = b->tx_fifo_errors;
668b60c5115SThomas Graf 	a->tx_heartbeat_errors = b->tx_heartbeat_errors;
669b60c5115SThomas Graf 	a->tx_window_errors = b->tx_window_errors;
670b60c5115SThomas Graf 
671b60c5115SThomas Graf 	a->rx_compressed = b->rx_compressed;
672b60c5115SThomas Graf 	a->tx_compressed = b->tx_compressed;
67310708f37SJan Engelhardt }
67410708f37SJan Engelhardt 
675be1f3c2cSBen Hutchings static void copy_rtnl_link_stats64(void *v, const struct rtnl_link_stats64 *b)
67610708f37SJan Engelhardt {
677afdcba37SEric Dumazet 	memcpy(v, b, sizeof(*b));
67810708f37SJan Engelhardt }
679b60c5115SThomas Graf 
680c02db8c6SChris Wright /* All VF info */
681115c9b81SGreg Rose static inline int rtnl_vfinfo_size(const struct net_device *dev,
682115c9b81SGreg Rose 				   u32 ext_filter_mask)
683ebc08a6fSWilliams, Mitch A {
684115c9b81SGreg Rose 	if (dev->dev.parent && dev_is_pci(dev->dev.parent) &&
685115c9b81SGreg Rose 	    (ext_filter_mask & RTEXT_FILTER_VF)) {
686c02db8c6SChris Wright 		int num_vfs = dev_num_vf(dev->dev.parent);
687045de01aSScott Feldman 		size_t size = nla_total_size(sizeof(struct nlattr));
688045de01aSScott Feldman 		size += nla_total_size(num_vfs * sizeof(struct nlattr));
689045de01aSScott Feldman 		size += num_vfs *
690045de01aSScott Feldman 			(nla_total_size(sizeof(struct ifla_vf_mac)) +
691045de01aSScott Feldman 			 nla_total_size(sizeof(struct ifla_vf_vlan)) +
6925f8444a3SGreg Rose 			 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
6935f8444a3SGreg Rose 			 nla_total_size(sizeof(struct ifla_vf_spoofchk)));
694c02db8c6SChris Wright 		return size;
695c02db8c6SChris Wright 	} else
696ebc08a6fSWilliams, Mitch A 		return 0;
697ebc08a6fSWilliams, Mitch A }
698ebc08a6fSWilliams, Mitch A 
69957b61080SScott Feldman static size_t rtnl_port_size(const struct net_device *dev)
70057b61080SScott Feldman {
70157b61080SScott Feldman 	size_t port_size = nla_total_size(4)		/* PORT_VF */
70257b61080SScott Feldman 		+ nla_total_size(PORT_PROFILE_MAX)	/* PORT_PROFILE */
70357b61080SScott Feldman 		+ nla_total_size(sizeof(struct ifla_port_vsi))
70457b61080SScott Feldman 							/* PORT_VSI_TYPE */
70557b61080SScott Feldman 		+ nla_total_size(PORT_UUID_MAX)		/* PORT_INSTANCE_UUID */
70657b61080SScott Feldman 		+ nla_total_size(PORT_UUID_MAX)		/* PORT_HOST_UUID */
70757b61080SScott Feldman 		+ nla_total_size(1)			/* PROT_VDP_REQUEST */
70857b61080SScott Feldman 		+ nla_total_size(2);			/* PORT_VDP_RESPONSE */
70957b61080SScott Feldman 	size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
71057b61080SScott Feldman 	size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
71157b61080SScott Feldman 		+ port_size;
71257b61080SScott Feldman 	size_t port_self_size = nla_total_size(sizeof(struct nlattr))
71357b61080SScott Feldman 		+ port_size;
71457b61080SScott Feldman 
71557b61080SScott Feldman 	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent)
71657b61080SScott Feldman 		return 0;
71757b61080SScott Feldman 	if (dev_num_vf(dev->dev.parent))
71857b61080SScott Feldman 		return port_self_size + vf_ports_size +
71957b61080SScott Feldman 			vf_port_size * dev_num_vf(dev->dev.parent);
72057b61080SScott Feldman 	else
72157b61080SScott Feldman 		return port_self_size;
72257b61080SScott Feldman }
72357b61080SScott Feldman 
724*1d3ee88aSsfeldma@cumulusnetworks.com static size_t rtnl_bond_slave_size(const struct net_device *dev)
725*1d3ee88aSsfeldma@cumulusnetworks.com {
726*1d3ee88aSsfeldma@cumulusnetworks.com 	struct net_device *bond;
727*1d3ee88aSsfeldma@cumulusnetworks.com 	size_t slave_size =
728*1d3ee88aSsfeldma@cumulusnetworks.com 		nla_total_size(sizeof(struct nlattr)) +	/* IFLA_SLAVE */
729*1d3ee88aSsfeldma@cumulusnetworks.com 		nla_total_size(1) +	/* IFLA_SLAVE_STATE */
730*1d3ee88aSsfeldma@cumulusnetworks.com 		nla_total_size(1) +	/* IFLA_SLAVE_MII_STATUS */
731*1d3ee88aSsfeldma@cumulusnetworks.com 		nla_total_size(4) +	/* IFLA_SLAVE_LINK_FAILURE_COUNT */
732*1d3ee88aSsfeldma@cumulusnetworks.com 		nla_total_size(MAX_ADDR_LEN) +	/* IFLA_SLAVE_PERM_HWADDR */
733*1d3ee88aSsfeldma@cumulusnetworks.com 		nla_total_size(2) +	/* IFLA_SLAVE_QUEUE_ID */
734*1d3ee88aSsfeldma@cumulusnetworks.com 		nla_total_size(2) +	/* IFLA_SLAVE_AD_AGGREGATOR_ID */
735*1d3ee88aSsfeldma@cumulusnetworks.com 		0;
736*1d3ee88aSsfeldma@cumulusnetworks.com 
737*1d3ee88aSsfeldma@cumulusnetworks.com 	if (netif_is_bond_slave((struct net_device *)dev)) {
738*1d3ee88aSsfeldma@cumulusnetworks.com 		bond = netdev_master_upper_dev_get((struct net_device *)dev);
739*1d3ee88aSsfeldma@cumulusnetworks.com 		if (bond && bond->netdev_ops->ndo_get_slave)
740*1d3ee88aSsfeldma@cumulusnetworks.com 			return slave_size;
741*1d3ee88aSsfeldma@cumulusnetworks.com 	}
742*1d3ee88aSsfeldma@cumulusnetworks.com 
743*1d3ee88aSsfeldma@cumulusnetworks.com 	return 0;
744*1d3ee88aSsfeldma@cumulusnetworks.com }
745*1d3ee88aSsfeldma@cumulusnetworks.com 
746115c9b81SGreg Rose static noinline size_t if_nlmsg_size(const struct net_device *dev,
747115c9b81SGreg Rose 				     u32 ext_filter_mask)
748339bf98fSThomas Graf {
749339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
750339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
7510b815a1aSStephen Hemminger 	       + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
752339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
753339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rtnl_link_ifmap))
754339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rtnl_link_stats))
755adcfe196SJan Engelhardt 	       + nla_total_size(sizeof(struct rtnl_link_stats64))
756339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
757339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
758339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_TXQLEN */
759339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_WEIGHT */
760339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MTU */
761339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_LINK */
762339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MASTER */
7639a57247fSJiri Pirko 	       + nla_total_size(1) /* IFLA_CARRIER */
764edbc0bb3SBen Greear 	       + nla_total_size(4) /* IFLA_PROMISCUITY */
76576ff5cc9SJiri Pirko 	       + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
76676ff5cc9SJiri Pirko 	       + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
767339bf98fSThomas Graf 	       + nla_total_size(1) /* IFLA_OPERSTATE */
76838f7b870SPatrick McHardy 	       + nla_total_size(1) /* IFLA_LINKMODE */
769115c9b81SGreg Rose 	       + nla_total_size(ext_filter_mask
770115c9b81SGreg Rose 			        & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
771115c9b81SGreg Rose 	       + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
77257b61080SScott Feldman 	       + rtnl_port_size(dev) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
773f8ff182cSThomas Graf 	       + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
77466cae9edSJiri Pirko 	       + rtnl_link_get_af_size(dev) /* IFLA_AF_SPEC */
775*1d3ee88aSsfeldma@cumulusnetworks.com 	       + rtnl_bond_slave_size(dev) /* IFLA_SLAVE */
77666cae9edSJiri Pirko 	       + nla_total_size(MAX_PHYS_PORT_ID_LEN); /* IFLA_PHYS_PORT_ID */
777339bf98fSThomas Graf }
778339bf98fSThomas Graf 
77957b61080SScott Feldman static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
78057b61080SScott Feldman {
78157b61080SScott Feldman 	struct nlattr *vf_ports;
78257b61080SScott Feldman 	struct nlattr *vf_port;
78357b61080SScott Feldman 	int vf;
78457b61080SScott Feldman 	int err;
78557b61080SScott Feldman 
78657b61080SScott Feldman 	vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
78757b61080SScott Feldman 	if (!vf_ports)
78857b61080SScott Feldman 		return -EMSGSIZE;
78957b61080SScott Feldman 
79057b61080SScott Feldman 	for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
79157b61080SScott Feldman 		vf_port = nla_nest_start(skb, IFLA_VF_PORT);
7928ca94183SScott Feldman 		if (!vf_port)
7938ca94183SScott Feldman 			goto nla_put_failure;
794a6574349SDavid S. Miller 		if (nla_put_u32(skb, IFLA_PORT_VF, vf))
795a6574349SDavid S. Miller 			goto nla_put_failure;
79657b61080SScott Feldman 		err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
7978ca94183SScott Feldman 		if (err == -EMSGSIZE)
7988ca94183SScott Feldman 			goto nla_put_failure;
79957b61080SScott Feldman 		if (err) {
80057b61080SScott Feldman 			nla_nest_cancel(skb, vf_port);
80157b61080SScott Feldman 			continue;
80257b61080SScott Feldman 		}
80357b61080SScott Feldman 		nla_nest_end(skb, vf_port);
80457b61080SScott Feldman 	}
80557b61080SScott Feldman 
80657b61080SScott Feldman 	nla_nest_end(skb, vf_ports);
80757b61080SScott Feldman 
80857b61080SScott Feldman 	return 0;
8098ca94183SScott Feldman 
8108ca94183SScott Feldman nla_put_failure:
8118ca94183SScott Feldman 	nla_nest_cancel(skb, vf_ports);
8128ca94183SScott Feldman 	return -EMSGSIZE;
81357b61080SScott Feldman }
81457b61080SScott Feldman 
81557b61080SScott Feldman static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
81657b61080SScott Feldman {
81757b61080SScott Feldman 	struct nlattr *port_self;
81857b61080SScott Feldman 	int err;
81957b61080SScott Feldman 
82057b61080SScott Feldman 	port_self = nla_nest_start(skb, IFLA_PORT_SELF);
82157b61080SScott Feldman 	if (!port_self)
82257b61080SScott Feldman 		return -EMSGSIZE;
82357b61080SScott Feldman 
82457b61080SScott Feldman 	err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
82557b61080SScott Feldman 	if (err) {
82657b61080SScott Feldman 		nla_nest_cancel(skb, port_self);
8278ca94183SScott Feldman 		return (err == -EMSGSIZE) ? err : 0;
82857b61080SScott Feldman 	}
82957b61080SScott Feldman 
83057b61080SScott Feldman 	nla_nest_end(skb, port_self);
83157b61080SScott Feldman 
83257b61080SScott Feldman 	return 0;
83357b61080SScott Feldman }
83457b61080SScott Feldman 
83557b61080SScott Feldman static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev)
83657b61080SScott Feldman {
83757b61080SScott Feldman 	int err;
83857b61080SScott Feldman 
83957b61080SScott Feldman 	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent)
84057b61080SScott Feldman 		return 0;
84157b61080SScott Feldman 
84257b61080SScott Feldman 	err = rtnl_port_self_fill(skb, dev);
84357b61080SScott Feldman 	if (err)
84457b61080SScott Feldman 		return err;
84557b61080SScott Feldman 
84657b61080SScott Feldman 	if (dev_num_vf(dev->dev.parent)) {
84757b61080SScott Feldman 		err = rtnl_vf_ports_fill(skb, dev);
84857b61080SScott Feldman 		if (err)
84957b61080SScott Feldman 			return err;
85057b61080SScott Feldman 	}
85157b61080SScott Feldman 
85257b61080SScott Feldman 	return 0;
85357b61080SScott Feldman }
85457b61080SScott Feldman 
85566cae9edSJiri Pirko static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
85666cae9edSJiri Pirko {
85766cae9edSJiri Pirko 	int err;
85866cae9edSJiri Pirko 	struct netdev_phys_port_id ppid;
85966cae9edSJiri Pirko 
86066cae9edSJiri Pirko 	err = dev_get_phys_port_id(dev, &ppid);
86166cae9edSJiri Pirko 	if (err) {
86266cae9edSJiri Pirko 		if (err == -EOPNOTSUPP)
86366cae9edSJiri Pirko 			return 0;
86466cae9edSJiri Pirko 		return err;
86566cae9edSJiri Pirko 	}
86666cae9edSJiri Pirko 
86766cae9edSJiri Pirko 	if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
86866cae9edSJiri Pirko 		return -EMSGSIZE;
86966cae9edSJiri Pirko 
87066cae9edSJiri Pirko 	return 0;
87166cae9edSJiri Pirko }
87266cae9edSJiri Pirko 
873*1d3ee88aSsfeldma@cumulusnetworks.com static size_t rtnl_bond_slave_fill(struct sk_buff *skb, struct net_device *dev)
874*1d3ee88aSsfeldma@cumulusnetworks.com {
875*1d3ee88aSsfeldma@cumulusnetworks.com 	struct net_device *bond;
876*1d3ee88aSsfeldma@cumulusnetworks.com 	struct nlattr *nest;
877*1d3ee88aSsfeldma@cumulusnetworks.com 	int err;
878*1d3ee88aSsfeldma@cumulusnetworks.com 
879*1d3ee88aSsfeldma@cumulusnetworks.com 	if (!netif_is_bond_slave(dev))
880*1d3ee88aSsfeldma@cumulusnetworks.com 		return 0;
881*1d3ee88aSsfeldma@cumulusnetworks.com 
882*1d3ee88aSsfeldma@cumulusnetworks.com 	bond = netdev_master_upper_dev_get(dev);
883*1d3ee88aSsfeldma@cumulusnetworks.com 	if (!bond || !bond->netdev_ops->ndo_get_slave)
884*1d3ee88aSsfeldma@cumulusnetworks.com 		return 0;
885*1d3ee88aSsfeldma@cumulusnetworks.com 
886*1d3ee88aSsfeldma@cumulusnetworks.com 	nest = nla_nest_start(skb, IFLA_SLAVE);
887*1d3ee88aSsfeldma@cumulusnetworks.com 	if (!nest)
888*1d3ee88aSsfeldma@cumulusnetworks.com 		return -EMSGSIZE;
889*1d3ee88aSsfeldma@cumulusnetworks.com 
890*1d3ee88aSsfeldma@cumulusnetworks.com 	err = bond->netdev_ops->ndo_get_slave(dev, skb);
891*1d3ee88aSsfeldma@cumulusnetworks.com 	if (err) {
892*1d3ee88aSsfeldma@cumulusnetworks.com 		nla_nest_cancel(skb, nest);
893*1d3ee88aSsfeldma@cumulusnetworks.com 		return (err == -EMSGSIZE) ? err : 0;
894*1d3ee88aSsfeldma@cumulusnetworks.com 	}
895*1d3ee88aSsfeldma@cumulusnetworks.com 
896*1d3ee88aSsfeldma@cumulusnetworks.com 	nla_nest_end(skb, nest);
897*1d3ee88aSsfeldma@cumulusnetworks.com 
898*1d3ee88aSsfeldma@cumulusnetworks.com 	return 0;
899*1d3ee88aSsfeldma@cumulusnetworks.com }
900*1d3ee88aSsfeldma@cumulusnetworks.com 
901b60c5115SThomas Graf static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
902575c3e2aSPatrick McHardy 			    int type, u32 pid, u32 seq, u32 change,
903115c9b81SGreg Rose 			    unsigned int flags, u32 ext_filter_mask)
904b60c5115SThomas Graf {
905b60c5115SThomas Graf 	struct ifinfomsg *ifm;
9061da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
90728172739SEric Dumazet 	struct rtnl_link_stats64 temp;
908be1f3c2cSBen Hutchings 	const struct rtnl_link_stats64 *stats;
909f8ff182cSThomas Graf 	struct nlattr *attr, *af_spec;
910f8ff182cSThomas Graf 	struct rtnl_af_ops *af_ops;
911898e5061SJiri Pirko 	struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
9121da177e4SLinus Torvalds 
9132907c35fSEric Dumazet 	ASSERT_RTNL();
914b60c5115SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
915b60c5115SThomas Graf 	if (nlh == NULL)
91626932566SPatrick McHardy 		return -EMSGSIZE;
9171da177e4SLinus Torvalds 
918b60c5115SThomas Graf 	ifm = nlmsg_data(nlh);
919b60c5115SThomas Graf 	ifm->ifi_family = AF_UNSPEC;
920b60c5115SThomas Graf 	ifm->__ifi_pad = 0;
921b60c5115SThomas Graf 	ifm->ifi_type = dev->type;
922b60c5115SThomas Graf 	ifm->ifi_index = dev->ifindex;
923b60c5115SThomas Graf 	ifm->ifi_flags = dev_get_flags(dev);
924b60c5115SThomas Graf 	ifm->ifi_change = change;
9251da177e4SLinus Torvalds 
926a6574349SDavid S. Miller 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
927a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
928a6574349SDavid S. Miller 	    nla_put_u8(skb, IFLA_OPERSTATE,
929a6574349SDavid S. Miller 		       netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
930a6574349SDavid S. Miller 	    nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
931a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
932a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_GROUP, dev->group) ||
933edbc0bb3SBen Greear 	    nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
93476ff5cc9SJiri Pirko 	    nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
9351d69c2b3SMark A. Greer #ifdef CONFIG_RPS
93676ff5cc9SJiri Pirko 	    nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
9371d69c2b3SMark A. Greer #endif
938a6574349SDavid S. Miller 	    (dev->ifindex != dev->iflink &&
939a6574349SDavid S. Miller 	     nla_put_u32(skb, IFLA_LINK, dev->iflink)) ||
940898e5061SJiri Pirko 	    (upper_dev &&
941898e5061SJiri Pirko 	     nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) ||
9429a57247fSJiri Pirko 	    nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
943a6574349SDavid S. Miller 	    (dev->qdisc &&
944a6574349SDavid S. Miller 	     nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
945a6574349SDavid S. Miller 	    (dev->ifalias &&
946a6574349SDavid S. Miller 	     nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)))
947a6574349SDavid S. Miller 		goto nla_put_failure;
9480b815a1aSStephen Hemminger 
949b00055aaSStefan Rompf 	if (1) {
9501da177e4SLinus Torvalds 		struct rtnl_link_ifmap map = {
9511da177e4SLinus Torvalds 			.mem_start   = dev->mem_start,
9521da177e4SLinus Torvalds 			.mem_end     = dev->mem_end,
9531da177e4SLinus Torvalds 			.base_addr   = dev->base_addr,
9541da177e4SLinus Torvalds 			.irq         = dev->irq,
9551da177e4SLinus Torvalds 			.dma         = dev->dma,
9561da177e4SLinus Torvalds 			.port        = dev->if_port,
9571da177e4SLinus Torvalds 		};
958a6574349SDavid S. Miller 		if (nla_put(skb, IFLA_MAP, sizeof(map), &map))
959a6574349SDavid S. Miller 			goto nla_put_failure;
9601da177e4SLinus Torvalds 	}
9611da177e4SLinus Torvalds 
9621da177e4SLinus Torvalds 	if (dev->addr_len) {
963a6574349SDavid S. Miller 		if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
964a6574349SDavid S. Miller 		    nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
965a6574349SDavid S. Miller 			goto nla_put_failure;
9661da177e4SLinus Torvalds 	}
9671da177e4SLinus Torvalds 
96866cae9edSJiri Pirko 	if (rtnl_phys_port_id_fill(skb, dev))
96966cae9edSJiri Pirko 		goto nla_put_failure;
97066cae9edSJiri Pirko 
971b60c5115SThomas Graf 	attr = nla_reserve(skb, IFLA_STATS,
972b60c5115SThomas Graf 			sizeof(struct rtnl_link_stats));
973b60c5115SThomas Graf 	if (attr == NULL)
974b60c5115SThomas Graf 		goto nla_put_failure;
975b60c5115SThomas Graf 
97628172739SEric Dumazet 	stats = dev_get_stats(dev, &temp);
977b60c5115SThomas Graf 	copy_rtnl_link_stats(nla_data(attr), stats);
9781da177e4SLinus Torvalds 
97910708f37SJan Engelhardt 	attr = nla_reserve(skb, IFLA_STATS64,
98010708f37SJan Engelhardt 			sizeof(struct rtnl_link_stats64));
98110708f37SJan Engelhardt 	if (attr == NULL)
98210708f37SJan Engelhardt 		goto nla_put_failure;
98310708f37SJan Engelhardt 	copy_rtnl_link_stats64(nla_data(attr), stats);
98410708f37SJan Engelhardt 
985a6574349SDavid S. Miller 	if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) &&
986a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)))
987a6574349SDavid S. Miller 		goto nla_put_failure;
98857b61080SScott Feldman 
989115c9b81SGreg Rose 	if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent
990115c9b81SGreg Rose 	    && (ext_filter_mask & RTEXT_FILTER_VF)) {
991ebc08a6fSWilliams, Mitch A 		int i;
992ebc08a6fSWilliams, Mitch A 
993c02db8c6SChris Wright 		struct nlattr *vfinfo, *vf;
994c02db8c6SChris Wright 		int num_vfs = dev_num_vf(dev->dev.parent);
995c02db8c6SChris Wright 
996c02db8c6SChris Wright 		vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
997c02db8c6SChris Wright 		if (!vfinfo)
998c02db8c6SChris Wright 			goto nla_put_failure;
999c02db8c6SChris Wright 		for (i = 0; i < num_vfs; i++) {
1000c02db8c6SChris Wright 			struct ifla_vf_info ivi;
1001c02db8c6SChris Wright 			struct ifla_vf_mac vf_mac;
1002c02db8c6SChris Wright 			struct ifla_vf_vlan vf_vlan;
1003c02db8c6SChris Wright 			struct ifla_vf_tx_rate vf_tx_rate;
10045f8444a3SGreg Rose 			struct ifla_vf_spoofchk vf_spoofchk;
10051d8faf48SRony Efraim 			struct ifla_vf_link_state vf_linkstate;
10065f8444a3SGreg Rose 
10075f8444a3SGreg Rose 			/*
10085f8444a3SGreg Rose 			 * Not all SR-IOV capable drivers support the
10095f8444a3SGreg Rose 			 * spoofcheck query.  Preset to -1 so the user
10105f8444a3SGreg Rose 			 * space tool can detect that the driver didn't
10115f8444a3SGreg Rose 			 * report anything.
10125f8444a3SGreg Rose 			 */
10135f8444a3SGreg Rose 			ivi.spoofchk = -1;
101484d73cd3SMathias Krause 			memset(ivi.mac, 0, sizeof(ivi.mac));
10151d8faf48SRony Efraim 			/* The default value for VF link state is "auto"
10161d8faf48SRony Efraim 			 * IFLA_VF_LINK_STATE_AUTO which equals zero
10171d8faf48SRony Efraim 			 */
10181d8faf48SRony Efraim 			ivi.linkstate = 0;
1019ebc08a6fSWilliams, Mitch A 			if (dev->netdev_ops->ndo_get_vf_config(dev, i, &ivi))
1020ebc08a6fSWilliams, Mitch A 				break;
10215f8444a3SGreg Rose 			vf_mac.vf =
10225f8444a3SGreg Rose 				vf_vlan.vf =
10235f8444a3SGreg Rose 				vf_tx_rate.vf =
10241d8faf48SRony Efraim 				vf_spoofchk.vf =
10251d8faf48SRony Efraim 				vf_linkstate.vf = ivi.vf;
10265f8444a3SGreg Rose 
1027c02db8c6SChris Wright 			memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1028c02db8c6SChris Wright 			vf_vlan.vlan = ivi.vlan;
1029c02db8c6SChris Wright 			vf_vlan.qos = ivi.qos;
1030c02db8c6SChris Wright 			vf_tx_rate.rate = ivi.tx_rate;
10315f8444a3SGreg Rose 			vf_spoofchk.setting = ivi.spoofchk;
10321d8faf48SRony Efraim 			vf_linkstate.link_state = ivi.linkstate;
1033c02db8c6SChris Wright 			vf = nla_nest_start(skb, IFLA_VF_INFO);
1034c02db8c6SChris Wright 			if (!vf) {
1035c02db8c6SChris Wright 				nla_nest_cancel(skb, vfinfo);
1036c02db8c6SChris Wright 				goto nla_put_failure;
1037ebc08a6fSWilliams, Mitch A 			}
1038a6574349SDavid S. Miller 			if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1039a6574349SDavid S. Miller 			    nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1040a6574349SDavid S. Miller 			    nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1041a6574349SDavid S. Miller 				    &vf_tx_rate) ||
1042a6574349SDavid S. Miller 			    nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
10431d8faf48SRony Efraim 				    &vf_spoofchk) ||
10441d8faf48SRony Efraim 			    nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
10451d8faf48SRony Efraim 				    &vf_linkstate))
1046a6574349SDavid S. Miller 				goto nla_put_failure;
1047c02db8c6SChris Wright 			nla_nest_end(skb, vf);
1048c02db8c6SChris Wright 		}
1049c02db8c6SChris Wright 		nla_nest_end(skb, vfinfo);
1050ebc08a6fSWilliams, Mitch A 	}
105157b61080SScott Feldman 
105257b61080SScott Feldman 	if (rtnl_port_fill(skb, dev))
105357b61080SScott Feldman 		goto nla_put_failure;
105457b61080SScott Feldman 
1055*1d3ee88aSsfeldma@cumulusnetworks.com 	if (rtnl_bond_slave_fill(skb, dev))
1056*1d3ee88aSsfeldma@cumulusnetworks.com 		goto nla_put_failure;
1057*1d3ee88aSsfeldma@cumulusnetworks.com 
105838f7b870SPatrick McHardy 	if (dev->rtnl_link_ops) {
105938f7b870SPatrick McHardy 		if (rtnl_link_fill(skb, dev) < 0)
106038f7b870SPatrick McHardy 			goto nla_put_failure;
106138f7b870SPatrick McHardy 	}
106238f7b870SPatrick McHardy 
1063f8ff182cSThomas Graf 	if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
1064f8ff182cSThomas Graf 		goto nla_put_failure;
1065f8ff182cSThomas Graf 
1066f8ff182cSThomas Graf 	list_for_each_entry(af_ops, &rtnl_af_ops, list) {
1067f8ff182cSThomas Graf 		if (af_ops->fill_link_af) {
1068f8ff182cSThomas Graf 			struct nlattr *af;
1069f8ff182cSThomas Graf 			int err;
1070f8ff182cSThomas Graf 
1071f8ff182cSThomas Graf 			if (!(af = nla_nest_start(skb, af_ops->family)))
1072f8ff182cSThomas Graf 				goto nla_put_failure;
1073f8ff182cSThomas Graf 
1074f8ff182cSThomas Graf 			err = af_ops->fill_link_af(skb, dev);
1075f8ff182cSThomas Graf 
1076f8ff182cSThomas Graf 			/*
1077f8ff182cSThomas Graf 			 * Caller may return ENODATA to indicate that there
1078f8ff182cSThomas Graf 			 * was no data to be dumped. This is not an error, it
1079f8ff182cSThomas Graf 			 * means we should trim the attribute header and
1080f8ff182cSThomas Graf 			 * continue.
1081f8ff182cSThomas Graf 			 */
1082f8ff182cSThomas Graf 			if (err == -ENODATA)
1083f8ff182cSThomas Graf 				nla_nest_cancel(skb, af);
1084f8ff182cSThomas Graf 			else if (err < 0)
1085f8ff182cSThomas Graf 				goto nla_put_failure;
1086f8ff182cSThomas Graf 
1087f8ff182cSThomas Graf 			nla_nest_end(skb, af);
1088f8ff182cSThomas Graf 		}
1089f8ff182cSThomas Graf 	}
1090f8ff182cSThomas Graf 
1091f8ff182cSThomas Graf 	nla_nest_end(skb, af_spec);
1092f8ff182cSThomas Graf 
1093b60c5115SThomas Graf 	return nlmsg_end(skb, nlh);
1094b60c5115SThomas Graf 
1095b60c5115SThomas Graf nla_put_failure:
109626932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
109726932566SPatrick McHardy 	return -EMSGSIZE;
10981da177e4SLinus Torvalds }
10991da177e4SLinus Torvalds 
1100b60c5115SThomas Graf static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
11011da177e4SLinus Torvalds {
11023b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
11037c28bd0bSEric Dumazet 	int h, s_h;
11047c28bd0bSEric Dumazet 	int idx = 0, s_idx;
11051da177e4SLinus Torvalds 	struct net_device *dev;
11067c28bd0bSEric Dumazet 	struct hlist_head *head;
1107115c9b81SGreg Rose 	struct nlattr *tb[IFLA_MAX+1];
1108115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
11091da177e4SLinus Torvalds 
11107c28bd0bSEric Dumazet 	s_h = cb->args[0];
11117c28bd0bSEric Dumazet 	s_idx = cb->args[1];
11127c28bd0bSEric Dumazet 
1113e67f88ddSEric Dumazet 	rcu_read_lock();
11144e985adaSThomas Graf 	cb->seq = net->dev_base_seq;
11154e985adaSThomas Graf 
111688c5b5ceSMichael Riesch 	if (nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
1117a4b64fbeSEric Dumazet 			ifla_policy) >= 0) {
1118115c9b81SGreg Rose 
1119115c9b81SGreg Rose 		if (tb[IFLA_EXT_MASK])
1120115c9b81SGreg Rose 			ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1121a4b64fbeSEric Dumazet 	}
1122115c9b81SGreg Rose 
11237c28bd0bSEric Dumazet 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
11247562f876SPavel Emelianov 		idx = 0;
11257c28bd0bSEric Dumazet 		head = &net->dev_index_head[h];
1126b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu(dev, head, index_hlist) {
11271da177e4SLinus Torvalds 			if (idx < s_idx)
11287562f876SPavel Emelianov 				goto cont;
1129575c3e2aSPatrick McHardy 			if (rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
113015e47304SEric W. Biederman 					     NETLINK_CB(cb->skb).portid,
11317c28bd0bSEric Dumazet 					     cb->nlh->nlmsg_seq, 0,
1132115c9b81SGreg Rose 					     NLM_F_MULTI,
1133115c9b81SGreg Rose 					     ext_filter_mask) <= 0)
11347c28bd0bSEric Dumazet 				goto out;
11354e985adaSThomas Graf 
11364e985adaSThomas Graf 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
11377562f876SPavel Emelianov cont:
11387562f876SPavel Emelianov 			idx++;
11391da177e4SLinus Torvalds 		}
11407c28bd0bSEric Dumazet 	}
11417c28bd0bSEric Dumazet out:
1142e67f88ddSEric Dumazet 	rcu_read_unlock();
11437c28bd0bSEric Dumazet 	cb->args[1] = idx;
11447c28bd0bSEric Dumazet 	cb->args[0] = h;
11451da177e4SLinus Torvalds 
11461da177e4SLinus Torvalds 	return skb->len;
11471da177e4SLinus Torvalds }
11481da177e4SLinus Torvalds 
1149e7199288SPavel Emelianov const struct nla_policy ifla_policy[IFLA_MAX+1] = {
11505176f91eSThomas Graf 	[IFLA_IFNAME]		= { .type = NLA_STRING, .len = IFNAMSIZ-1 },
115138f7b870SPatrick McHardy 	[IFLA_ADDRESS]		= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
115238f7b870SPatrick McHardy 	[IFLA_BROADCAST]	= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
11535176f91eSThomas Graf 	[IFLA_MAP]		= { .len = sizeof(struct rtnl_link_ifmap) },
1154da5e0494SThomas Graf 	[IFLA_MTU]		= { .type = NLA_U32 },
115576e87306SThomas Graf 	[IFLA_LINK]		= { .type = NLA_U32 },
1156fbaec0eaSJiri Pirko 	[IFLA_MASTER]		= { .type = NLA_U32 },
11579a57247fSJiri Pirko 	[IFLA_CARRIER]		= { .type = NLA_U8 },
1158da5e0494SThomas Graf 	[IFLA_TXQLEN]		= { .type = NLA_U32 },
1159da5e0494SThomas Graf 	[IFLA_WEIGHT]		= { .type = NLA_U32 },
1160da5e0494SThomas Graf 	[IFLA_OPERSTATE]	= { .type = NLA_U8 },
1161da5e0494SThomas Graf 	[IFLA_LINKMODE]		= { .type = NLA_U8 },
116276e87306SThomas Graf 	[IFLA_LINKINFO]		= { .type = NLA_NESTED },
1163d8a5ec67SEric W. Biederman 	[IFLA_NET_NS_PID]	= { .type = NLA_U32 },
1164f0630529SEric W. Biederman 	[IFLA_NET_NS_FD]	= { .type = NLA_U32 },
11650b815a1aSStephen Hemminger 	[IFLA_IFALIAS]	        = { .type = NLA_STRING, .len = IFALIASZ-1 },
1166c02db8c6SChris Wright 	[IFLA_VFINFO_LIST]	= {. type = NLA_NESTED },
116757b61080SScott Feldman 	[IFLA_VF_PORTS]		= { .type = NLA_NESTED },
116857b61080SScott Feldman 	[IFLA_PORT_SELF]	= { .type = NLA_NESTED },
1169f8ff182cSThomas Graf 	[IFLA_AF_SPEC]		= { .type = NLA_NESTED },
1170115c9b81SGreg Rose 	[IFLA_EXT_MASK]		= { .type = NLA_U32 },
1171edbc0bb3SBen Greear 	[IFLA_PROMISCUITY]	= { .type = NLA_U32 },
117276ff5cc9SJiri Pirko 	[IFLA_NUM_TX_QUEUES]	= { .type = NLA_U32 },
117376ff5cc9SJiri Pirko 	[IFLA_NUM_RX_QUEUES]	= { .type = NLA_U32 },
117466cae9edSJiri Pirko 	[IFLA_PHYS_PORT_ID]	= { .type = NLA_BINARY, .len = MAX_PHYS_PORT_ID_LEN },
1175da5e0494SThomas Graf };
1176e0d087afSEric Dumazet EXPORT_SYMBOL(ifla_policy);
11771da177e4SLinus Torvalds 
117838f7b870SPatrick McHardy static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
117938f7b870SPatrick McHardy 	[IFLA_INFO_KIND]	= { .type = NLA_STRING },
118038f7b870SPatrick McHardy 	[IFLA_INFO_DATA]	= { .type = NLA_NESTED },
118138f7b870SPatrick McHardy };
118238f7b870SPatrick McHardy 
1183c02db8c6SChris Wright static const struct nla_policy ifla_vfinfo_policy[IFLA_VF_INFO_MAX+1] = {
1184c02db8c6SChris Wright 	[IFLA_VF_INFO]		= { .type = NLA_NESTED },
1185c02db8c6SChris Wright };
1186c02db8c6SChris Wright 
1187c02db8c6SChris Wright static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1188c02db8c6SChris Wright 	[IFLA_VF_MAC]		= { .type = NLA_BINARY,
1189c02db8c6SChris Wright 				    .len = sizeof(struct ifla_vf_mac) },
1190c02db8c6SChris Wright 	[IFLA_VF_VLAN]		= { .type = NLA_BINARY,
1191c02db8c6SChris Wright 				    .len = sizeof(struct ifla_vf_vlan) },
1192c02db8c6SChris Wright 	[IFLA_VF_TX_RATE]	= { .type = NLA_BINARY,
1193c02db8c6SChris Wright 				    .len = sizeof(struct ifla_vf_tx_rate) },
119448752f65SGreg Rose 	[IFLA_VF_SPOOFCHK]	= { .type = NLA_BINARY,
119548752f65SGreg Rose 				    .len = sizeof(struct ifla_vf_spoofchk) },
1196c02db8c6SChris Wright };
1197c02db8c6SChris Wright 
119857b61080SScott Feldman static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
119957b61080SScott Feldman 	[IFLA_PORT_VF]		= { .type = NLA_U32 },
120057b61080SScott Feldman 	[IFLA_PORT_PROFILE]	= { .type = NLA_STRING,
120157b61080SScott Feldman 				    .len = PORT_PROFILE_MAX },
120257b61080SScott Feldman 	[IFLA_PORT_VSI_TYPE]	= { .type = NLA_BINARY,
120357b61080SScott Feldman 				    .len = sizeof(struct ifla_port_vsi)},
120457b61080SScott Feldman 	[IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
120557b61080SScott Feldman 				      .len = PORT_UUID_MAX },
120657b61080SScott Feldman 	[IFLA_PORT_HOST_UUID]	= { .type = NLA_STRING,
120757b61080SScott Feldman 				    .len = PORT_UUID_MAX },
120857b61080SScott Feldman 	[IFLA_PORT_REQUEST]	= { .type = NLA_U8, },
120957b61080SScott Feldman 	[IFLA_PORT_RESPONSE]	= { .type = NLA_U16, },
121057b61080SScott Feldman };
121157b61080SScott Feldman 
121281adee47SEric W. Biederman struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
121381adee47SEric W. Biederman {
121481adee47SEric W. Biederman 	struct net *net;
121581adee47SEric W. Biederman 	/* Examine the link attributes and figure out which
121681adee47SEric W. Biederman 	 * network namespace we are talking about.
121781adee47SEric W. Biederman 	 */
121881adee47SEric W. Biederman 	if (tb[IFLA_NET_NS_PID])
121981adee47SEric W. Biederman 		net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
1220f0630529SEric W. Biederman 	else if (tb[IFLA_NET_NS_FD])
1221f0630529SEric W. Biederman 		net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
122281adee47SEric W. Biederman 	else
122381adee47SEric W. Biederman 		net = get_net(src_net);
122481adee47SEric W. Biederman 	return net;
122581adee47SEric W. Biederman }
122681adee47SEric W. Biederman EXPORT_SYMBOL(rtnl_link_get_net);
122781adee47SEric W. Biederman 
12281840bb13SThomas Graf static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
12291840bb13SThomas Graf {
12301840bb13SThomas Graf 	if (dev) {
12311840bb13SThomas Graf 		if (tb[IFLA_ADDRESS] &&
12321840bb13SThomas Graf 		    nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
12331840bb13SThomas Graf 			return -EINVAL;
12341840bb13SThomas Graf 
12351840bb13SThomas Graf 		if (tb[IFLA_BROADCAST] &&
12361840bb13SThomas Graf 		    nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
12371840bb13SThomas Graf 			return -EINVAL;
12381840bb13SThomas Graf 	}
12391840bb13SThomas Graf 
1240cf7afbfeSThomas Graf 	if (tb[IFLA_AF_SPEC]) {
1241cf7afbfeSThomas Graf 		struct nlattr *af;
1242cf7afbfeSThomas Graf 		int rem, err;
1243cf7afbfeSThomas Graf 
1244cf7afbfeSThomas Graf 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1245cf7afbfeSThomas Graf 			const struct rtnl_af_ops *af_ops;
1246cf7afbfeSThomas Graf 
1247cf7afbfeSThomas Graf 			if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1248cf7afbfeSThomas Graf 				return -EAFNOSUPPORT;
1249cf7afbfeSThomas Graf 
1250cf7afbfeSThomas Graf 			if (!af_ops->set_link_af)
1251cf7afbfeSThomas Graf 				return -EOPNOTSUPP;
1252cf7afbfeSThomas Graf 
1253cf7afbfeSThomas Graf 			if (af_ops->validate_link_af) {
12546d3a9a68SKurt Van Dijck 				err = af_ops->validate_link_af(dev, af);
1255cf7afbfeSThomas Graf 				if (err < 0)
1256cf7afbfeSThomas Graf 					return err;
1257cf7afbfeSThomas Graf 			}
1258cf7afbfeSThomas Graf 		}
1259cf7afbfeSThomas Graf 	}
1260cf7afbfeSThomas Graf 
12611840bb13SThomas Graf 	return 0;
12621840bb13SThomas Graf }
12631840bb13SThomas Graf 
1264c02db8c6SChris Wright static int do_setvfinfo(struct net_device *dev, struct nlattr *attr)
1265c02db8c6SChris Wright {
1266c02db8c6SChris Wright 	int rem, err = -EINVAL;
1267c02db8c6SChris Wright 	struct nlattr *vf;
1268c02db8c6SChris Wright 	const struct net_device_ops *ops = dev->netdev_ops;
1269c02db8c6SChris Wright 
1270c02db8c6SChris Wright 	nla_for_each_nested(vf, attr, rem) {
1271c02db8c6SChris Wright 		switch (nla_type(vf)) {
1272c02db8c6SChris Wright 		case IFLA_VF_MAC: {
1273c02db8c6SChris Wright 			struct ifla_vf_mac *ivm;
1274c02db8c6SChris Wright 			ivm = nla_data(vf);
1275c02db8c6SChris Wright 			err = -EOPNOTSUPP;
1276c02db8c6SChris Wright 			if (ops->ndo_set_vf_mac)
1277c02db8c6SChris Wright 				err = ops->ndo_set_vf_mac(dev, ivm->vf,
1278c02db8c6SChris Wright 							  ivm->mac);
1279c02db8c6SChris Wright 			break;
1280c02db8c6SChris Wright 		}
1281c02db8c6SChris Wright 		case IFLA_VF_VLAN: {
1282c02db8c6SChris Wright 			struct ifla_vf_vlan *ivv;
1283c02db8c6SChris Wright 			ivv = nla_data(vf);
1284c02db8c6SChris Wright 			err = -EOPNOTSUPP;
1285c02db8c6SChris Wright 			if (ops->ndo_set_vf_vlan)
1286c02db8c6SChris Wright 				err = ops->ndo_set_vf_vlan(dev, ivv->vf,
1287c02db8c6SChris Wright 							   ivv->vlan,
1288c02db8c6SChris Wright 							   ivv->qos);
1289c02db8c6SChris Wright 			break;
1290c02db8c6SChris Wright 		}
1291c02db8c6SChris Wright 		case IFLA_VF_TX_RATE: {
1292c02db8c6SChris Wright 			struct ifla_vf_tx_rate *ivt;
1293c02db8c6SChris Wright 			ivt = nla_data(vf);
1294c02db8c6SChris Wright 			err = -EOPNOTSUPP;
1295c02db8c6SChris Wright 			if (ops->ndo_set_vf_tx_rate)
1296c02db8c6SChris Wright 				err = ops->ndo_set_vf_tx_rate(dev, ivt->vf,
1297c02db8c6SChris Wright 							      ivt->rate);
1298c02db8c6SChris Wright 			break;
1299c02db8c6SChris Wright 		}
13005f8444a3SGreg Rose 		case IFLA_VF_SPOOFCHK: {
13015f8444a3SGreg Rose 			struct ifla_vf_spoofchk *ivs;
13025f8444a3SGreg Rose 			ivs = nla_data(vf);
13035f8444a3SGreg Rose 			err = -EOPNOTSUPP;
13045f8444a3SGreg Rose 			if (ops->ndo_set_vf_spoofchk)
13055f8444a3SGreg Rose 				err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
13065f8444a3SGreg Rose 							       ivs->setting);
13075f8444a3SGreg Rose 			break;
13085f8444a3SGreg Rose 		}
13091d8faf48SRony Efraim 		case IFLA_VF_LINK_STATE: {
13101d8faf48SRony Efraim 			struct ifla_vf_link_state *ivl;
13111d8faf48SRony Efraim 			ivl = nla_data(vf);
13121d8faf48SRony Efraim 			err = -EOPNOTSUPP;
13131d8faf48SRony Efraim 			if (ops->ndo_set_vf_link_state)
13141d8faf48SRony Efraim 				err = ops->ndo_set_vf_link_state(dev, ivl->vf,
13151d8faf48SRony Efraim 								 ivl->link_state);
13161d8faf48SRony Efraim 			break;
13171d8faf48SRony Efraim 		}
1318c02db8c6SChris Wright 		default:
1319c02db8c6SChris Wright 			err = -EINVAL;
1320c02db8c6SChris Wright 			break;
1321c02db8c6SChris Wright 		}
1322c02db8c6SChris Wright 		if (err)
1323c02db8c6SChris Wright 			break;
1324c02db8c6SChris Wright 	}
1325c02db8c6SChris Wright 	return err;
1326c02db8c6SChris Wright }
1327c02db8c6SChris Wright 
1328fbaec0eaSJiri Pirko static int do_set_master(struct net_device *dev, int ifindex)
1329fbaec0eaSJiri Pirko {
1330898e5061SJiri Pirko 	struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
1331fbaec0eaSJiri Pirko 	const struct net_device_ops *ops;
1332fbaec0eaSJiri Pirko 	int err;
1333fbaec0eaSJiri Pirko 
1334898e5061SJiri Pirko 	if (upper_dev) {
1335898e5061SJiri Pirko 		if (upper_dev->ifindex == ifindex)
1336fbaec0eaSJiri Pirko 			return 0;
1337898e5061SJiri Pirko 		ops = upper_dev->netdev_ops;
1338fbaec0eaSJiri Pirko 		if (ops->ndo_del_slave) {
1339898e5061SJiri Pirko 			err = ops->ndo_del_slave(upper_dev, dev);
1340fbaec0eaSJiri Pirko 			if (err)
1341fbaec0eaSJiri Pirko 				return err;
1342fbaec0eaSJiri Pirko 		} else {
1343fbaec0eaSJiri Pirko 			return -EOPNOTSUPP;
1344fbaec0eaSJiri Pirko 		}
1345fbaec0eaSJiri Pirko 	}
1346fbaec0eaSJiri Pirko 
1347fbaec0eaSJiri Pirko 	if (ifindex) {
1348898e5061SJiri Pirko 		upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
1349898e5061SJiri Pirko 		if (!upper_dev)
1350fbaec0eaSJiri Pirko 			return -EINVAL;
1351898e5061SJiri Pirko 		ops = upper_dev->netdev_ops;
1352fbaec0eaSJiri Pirko 		if (ops->ndo_add_slave) {
1353898e5061SJiri Pirko 			err = ops->ndo_add_slave(upper_dev, dev);
1354fbaec0eaSJiri Pirko 			if (err)
1355fbaec0eaSJiri Pirko 				return err;
1356fbaec0eaSJiri Pirko 		} else {
1357fbaec0eaSJiri Pirko 			return -EOPNOTSUPP;
1358fbaec0eaSJiri Pirko 		}
1359fbaec0eaSJiri Pirko 	}
1360fbaec0eaSJiri Pirko 	return 0;
1361fbaec0eaSJiri Pirko }
1362fbaec0eaSJiri Pirko 
13630157f60cSPatrick McHardy static int do_setlink(struct net_device *dev, struct ifinfomsg *ifm,
136438f7b870SPatrick McHardy 		      struct nlattr **tb, char *ifname, int modified)
13650157f60cSPatrick McHardy {
1366d314774cSStephen Hemminger 	const struct net_device_ops *ops = dev->netdev_ops;
13670157f60cSPatrick McHardy 	int err;
13680157f60cSPatrick McHardy 
1369f0630529SEric W. Biederman 	if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
137081adee47SEric W. Biederman 		struct net *net = rtnl_link_get_net(dev_net(dev), tb);
1371d8a5ec67SEric W. Biederman 		if (IS_ERR(net)) {
1372d8a5ec67SEric W. Biederman 			err = PTR_ERR(net);
1373d8a5ec67SEric W. Biederman 			goto errout;
1374d8a5ec67SEric W. Biederman 		}
1375b51642f6SEric W. Biederman 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) {
1376b51642f6SEric W. Biederman 			err = -EPERM;
1377b51642f6SEric W. Biederman 			goto errout;
1378b51642f6SEric W. Biederman 		}
1379d8a5ec67SEric W. Biederman 		err = dev_change_net_namespace(dev, net, ifname);
1380d8a5ec67SEric W. Biederman 		put_net(net);
1381d8a5ec67SEric W. Biederman 		if (err)
1382d8a5ec67SEric W. Biederman 			goto errout;
1383d8a5ec67SEric W. Biederman 		modified = 1;
1384d8a5ec67SEric W. Biederman 	}
1385d8a5ec67SEric W. Biederman 
13860157f60cSPatrick McHardy 	if (tb[IFLA_MAP]) {
13870157f60cSPatrick McHardy 		struct rtnl_link_ifmap *u_map;
13880157f60cSPatrick McHardy 		struct ifmap k_map;
13890157f60cSPatrick McHardy 
1390d314774cSStephen Hemminger 		if (!ops->ndo_set_config) {
13910157f60cSPatrick McHardy 			err = -EOPNOTSUPP;
13920157f60cSPatrick McHardy 			goto errout;
13930157f60cSPatrick McHardy 		}
13940157f60cSPatrick McHardy 
13950157f60cSPatrick McHardy 		if (!netif_device_present(dev)) {
13960157f60cSPatrick McHardy 			err = -ENODEV;
13970157f60cSPatrick McHardy 			goto errout;
13980157f60cSPatrick McHardy 		}
13990157f60cSPatrick McHardy 
14000157f60cSPatrick McHardy 		u_map = nla_data(tb[IFLA_MAP]);
14010157f60cSPatrick McHardy 		k_map.mem_start = (unsigned long) u_map->mem_start;
14020157f60cSPatrick McHardy 		k_map.mem_end = (unsigned long) u_map->mem_end;
14030157f60cSPatrick McHardy 		k_map.base_addr = (unsigned short) u_map->base_addr;
14040157f60cSPatrick McHardy 		k_map.irq = (unsigned char) u_map->irq;
14050157f60cSPatrick McHardy 		k_map.dma = (unsigned char) u_map->dma;
14060157f60cSPatrick McHardy 		k_map.port = (unsigned char) u_map->port;
14070157f60cSPatrick McHardy 
1408d314774cSStephen Hemminger 		err = ops->ndo_set_config(dev, &k_map);
14090157f60cSPatrick McHardy 		if (err < 0)
14100157f60cSPatrick McHardy 			goto errout;
14110157f60cSPatrick McHardy 
14120157f60cSPatrick McHardy 		modified = 1;
14130157f60cSPatrick McHardy 	}
14140157f60cSPatrick McHardy 
14150157f60cSPatrick McHardy 	if (tb[IFLA_ADDRESS]) {
14160157f60cSPatrick McHardy 		struct sockaddr *sa;
14170157f60cSPatrick McHardy 		int len;
14180157f60cSPatrick McHardy 
14190157f60cSPatrick McHardy 		len = sizeof(sa_family_t) + dev->addr_len;
14200157f60cSPatrick McHardy 		sa = kmalloc(len, GFP_KERNEL);
14210157f60cSPatrick McHardy 		if (!sa) {
14220157f60cSPatrick McHardy 			err = -ENOMEM;
14230157f60cSPatrick McHardy 			goto errout;
14240157f60cSPatrick McHardy 		}
14250157f60cSPatrick McHardy 		sa->sa_family = dev->type;
14260157f60cSPatrick McHardy 		memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
14270157f60cSPatrick McHardy 		       dev->addr_len);
1428e7c3273eSJiri Pirko 		err = dev_set_mac_address(dev, sa);
14290157f60cSPatrick McHardy 		kfree(sa);
14300157f60cSPatrick McHardy 		if (err)
14310157f60cSPatrick McHardy 			goto errout;
14320157f60cSPatrick McHardy 		modified = 1;
14330157f60cSPatrick McHardy 	}
14340157f60cSPatrick McHardy 
14350157f60cSPatrick McHardy 	if (tb[IFLA_MTU]) {
14360157f60cSPatrick McHardy 		err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
14370157f60cSPatrick McHardy 		if (err < 0)
14380157f60cSPatrick McHardy 			goto errout;
14390157f60cSPatrick McHardy 		modified = 1;
14400157f60cSPatrick McHardy 	}
14410157f60cSPatrick McHardy 
1442cbda10faSVlad Dogaru 	if (tb[IFLA_GROUP]) {
1443cbda10faSVlad Dogaru 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
1444cbda10faSVlad Dogaru 		modified = 1;
1445cbda10faSVlad Dogaru 	}
1446cbda10faSVlad Dogaru 
14470157f60cSPatrick McHardy 	/*
14480157f60cSPatrick McHardy 	 * Interface selected by interface index but interface
14490157f60cSPatrick McHardy 	 * name provided implies that a name change has been
14500157f60cSPatrick McHardy 	 * requested.
14510157f60cSPatrick McHardy 	 */
14520157f60cSPatrick McHardy 	if (ifm->ifi_index > 0 && ifname[0]) {
14530157f60cSPatrick McHardy 		err = dev_change_name(dev, ifname);
14540157f60cSPatrick McHardy 		if (err < 0)
14550157f60cSPatrick McHardy 			goto errout;
14560157f60cSPatrick McHardy 		modified = 1;
14570157f60cSPatrick McHardy 	}
14580157f60cSPatrick McHardy 
14590b815a1aSStephen Hemminger 	if (tb[IFLA_IFALIAS]) {
14600b815a1aSStephen Hemminger 		err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
14610b815a1aSStephen Hemminger 				    nla_len(tb[IFLA_IFALIAS]));
14620b815a1aSStephen Hemminger 		if (err < 0)
14630b815a1aSStephen Hemminger 			goto errout;
14640b815a1aSStephen Hemminger 		modified = 1;
14650b815a1aSStephen Hemminger 	}
14660b815a1aSStephen Hemminger 
14670157f60cSPatrick McHardy 	if (tb[IFLA_BROADCAST]) {
14680157f60cSPatrick McHardy 		nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
1469e7c3273eSJiri Pirko 		call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
14700157f60cSPatrick McHardy 	}
14710157f60cSPatrick McHardy 
14720157f60cSPatrick McHardy 	if (ifm->ifi_flags || ifm->ifi_change) {
14733729d502SPatrick McHardy 		err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
14745f9021cfSJohannes Berg 		if (err < 0)
14755f9021cfSJohannes Berg 			goto errout;
14760157f60cSPatrick McHardy 	}
14770157f60cSPatrick McHardy 
1478fbaec0eaSJiri Pirko 	if (tb[IFLA_MASTER]) {
1479fbaec0eaSJiri Pirko 		err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
1480fbaec0eaSJiri Pirko 		if (err)
1481fbaec0eaSJiri Pirko 			goto errout;
1482fbaec0eaSJiri Pirko 		modified = 1;
1483fbaec0eaSJiri Pirko 	}
1484fbaec0eaSJiri Pirko 
14859a57247fSJiri Pirko 	if (tb[IFLA_CARRIER]) {
14869a57247fSJiri Pirko 		err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
14879a57247fSJiri Pirko 		if (err)
14889a57247fSJiri Pirko 			goto errout;
14899a57247fSJiri Pirko 		modified = 1;
14909a57247fSJiri Pirko 	}
14919a57247fSJiri Pirko 
149293b2d4a2SDavid S. Miller 	if (tb[IFLA_TXQLEN])
14930157f60cSPatrick McHardy 		dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
14940157f60cSPatrick McHardy 
14950157f60cSPatrick McHardy 	if (tb[IFLA_OPERSTATE])
149693b2d4a2SDavid S. Miller 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
14970157f60cSPatrick McHardy 
14980157f60cSPatrick McHardy 	if (tb[IFLA_LINKMODE]) {
14990157f60cSPatrick McHardy 		write_lock_bh(&dev_base_lock);
15000157f60cSPatrick McHardy 		dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
150193b2d4a2SDavid S. Miller 		write_unlock_bh(&dev_base_lock);
15020157f60cSPatrick McHardy 	}
15030157f60cSPatrick McHardy 
1504c02db8c6SChris Wright 	if (tb[IFLA_VFINFO_LIST]) {
1505c02db8c6SChris Wright 		struct nlattr *attr;
1506c02db8c6SChris Wright 		int rem;
1507c02db8c6SChris Wright 		nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
1508253683bbSDavid Howells 			if (nla_type(attr) != IFLA_VF_INFO) {
1509253683bbSDavid Howells 				err = -EINVAL;
1510c02db8c6SChris Wright 				goto errout;
1511253683bbSDavid Howells 			}
1512c02db8c6SChris Wright 			err = do_setvfinfo(dev, attr);
1513ebc08a6fSWilliams, Mitch A 			if (err < 0)
1514ebc08a6fSWilliams, Mitch A 				goto errout;
1515ebc08a6fSWilliams, Mitch A 			modified = 1;
1516ebc08a6fSWilliams, Mitch A 		}
1517ebc08a6fSWilliams, Mitch A 	}
15180157f60cSPatrick McHardy 	err = 0;
15190157f60cSPatrick McHardy 
152057b61080SScott Feldman 	if (tb[IFLA_VF_PORTS]) {
152157b61080SScott Feldman 		struct nlattr *port[IFLA_PORT_MAX+1];
152257b61080SScott Feldman 		struct nlattr *attr;
152357b61080SScott Feldman 		int vf;
152457b61080SScott Feldman 		int rem;
152557b61080SScott Feldman 
152657b61080SScott Feldman 		err = -EOPNOTSUPP;
152757b61080SScott Feldman 		if (!ops->ndo_set_vf_port)
152857b61080SScott Feldman 			goto errout;
152957b61080SScott Feldman 
153057b61080SScott Feldman 		nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
153157b61080SScott Feldman 			if (nla_type(attr) != IFLA_VF_PORT)
153257b61080SScott Feldman 				continue;
153357b61080SScott Feldman 			err = nla_parse_nested(port, IFLA_PORT_MAX,
153457b61080SScott Feldman 				attr, ifla_port_policy);
153557b61080SScott Feldman 			if (err < 0)
153657b61080SScott Feldman 				goto errout;
153757b61080SScott Feldman 			if (!port[IFLA_PORT_VF]) {
153857b61080SScott Feldman 				err = -EOPNOTSUPP;
153957b61080SScott Feldman 				goto errout;
154057b61080SScott Feldman 			}
154157b61080SScott Feldman 			vf = nla_get_u32(port[IFLA_PORT_VF]);
154257b61080SScott Feldman 			err = ops->ndo_set_vf_port(dev, vf, port);
154357b61080SScott Feldman 			if (err < 0)
154457b61080SScott Feldman 				goto errout;
154557b61080SScott Feldman 			modified = 1;
154657b61080SScott Feldman 		}
154757b61080SScott Feldman 	}
154857b61080SScott Feldman 	err = 0;
154957b61080SScott Feldman 
155057b61080SScott Feldman 	if (tb[IFLA_PORT_SELF]) {
155157b61080SScott Feldman 		struct nlattr *port[IFLA_PORT_MAX+1];
155257b61080SScott Feldman 
155357b61080SScott Feldman 		err = nla_parse_nested(port, IFLA_PORT_MAX,
155457b61080SScott Feldman 			tb[IFLA_PORT_SELF], ifla_port_policy);
155557b61080SScott Feldman 		if (err < 0)
155657b61080SScott Feldman 			goto errout;
155757b61080SScott Feldman 
155857b61080SScott Feldman 		err = -EOPNOTSUPP;
155957b61080SScott Feldman 		if (ops->ndo_set_vf_port)
156057b61080SScott Feldman 			err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
156157b61080SScott Feldman 		if (err < 0)
156257b61080SScott Feldman 			goto errout;
156357b61080SScott Feldman 		modified = 1;
156457b61080SScott Feldman 	}
1565f8ff182cSThomas Graf 
1566f8ff182cSThomas Graf 	if (tb[IFLA_AF_SPEC]) {
1567f8ff182cSThomas Graf 		struct nlattr *af;
1568f8ff182cSThomas Graf 		int rem;
1569f8ff182cSThomas Graf 
1570f8ff182cSThomas Graf 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1571f8ff182cSThomas Graf 			const struct rtnl_af_ops *af_ops;
1572f8ff182cSThomas Graf 
1573f8ff182cSThomas Graf 			if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1574cf7afbfeSThomas Graf 				BUG();
1575f8ff182cSThomas Graf 
1576cf7afbfeSThomas Graf 			err = af_ops->set_link_af(dev, af);
1577f8ff182cSThomas Graf 			if (err < 0)
1578f8ff182cSThomas Graf 				goto errout;
1579f8ff182cSThomas Graf 
1580f8ff182cSThomas Graf 			modified = 1;
1581f8ff182cSThomas Graf 		}
1582f8ff182cSThomas Graf 	}
158357b61080SScott Feldman 	err = 0;
158457b61080SScott Feldman 
15850157f60cSPatrick McHardy errout:
1586e87cc472SJoe Perches 	if (err < 0 && modified)
1587e87cc472SJoe 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",
1588e87cc472SJoe Perches 				     dev->name);
15890157f60cSPatrick McHardy 
15900157f60cSPatrick McHardy 	return err;
15910157f60cSPatrick McHardy }
15920157f60cSPatrick McHardy 
1593661d2967SThomas Graf static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
1594da5e0494SThomas Graf {
15953b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
1596da5e0494SThomas Graf 	struct ifinfomsg *ifm;
1597da5e0494SThomas Graf 	struct net_device *dev;
15980157f60cSPatrick McHardy 	int err;
1599da5e0494SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
16001da177e4SLinus Torvalds 	char ifname[IFNAMSIZ];
16011da177e4SLinus Torvalds 
1602da5e0494SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1603da5e0494SThomas Graf 	if (err < 0)
1604da5e0494SThomas Graf 		goto errout;
16051da177e4SLinus Torvalds 
16065176f91eSThomas Graf 	if (tb[IFLA_IFNAME])
16075176f91eSThomas Graf 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
160878e5b891SPatrick McHardy 	else
160978e5b891SPatrick McHardy 		ifname[0] = '\0';
16101da177e4SLinus Torvalds 
16111da177e4SLinus Torvalds 	err = -EINVAL;
1612da5e0494SThomas Graf 	ifm = nlmsg_data(nlh);
161351055be8SPatrick McHardy 	if (ifm->ifi_index > 0)
1614a3d12891SEric Dumazet 		dev = __dev_get_by_index(net, ifm->ifi_index);
1615da5e0494SThomas Graf 	else if (tb[IFLA_IFNAME])
1616a3d12891SEric Dumazet 		dev = __dev_get_by_name(net, ifname);
1617da5e0494SThomas Graf 	else
1618da5e0494SThomas Graf 		goto errout;
16191da177e4SLinus Torvalds 
1620da5e0494SThomas Graf 	if (dev == NULL) {
1621da5e0494SThomas Graf 		err = -ENODEV;
1622da5e0494SThomas Graf 		goto errout;
1623da5e0494SThomas Graf 	}
16241da177e4SLinus Torvalds 
1625e0d087afSEric Dumazet 	err = validate_linkmsg(dev, tb);
1626e0d087afSEric Dumazet 	if (err < 0)
1627a3d12891SEric Dumazet 		goto errout;
1628da5e0494SThomas Graf 
162938f7b870SPatrick McHardy 	err = do_setlink(dev, ifm, tb, ifname, 0);
1630da5e0494SThomas Graf errout:
16311da177e4SLinus Torvalds 	return err;
16321da177e4SLinus Torvalds }
16331da177e4SLinus Torvalds 
1634661d2967SThomas Graf static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
163538f7b870SPatrick McHardy {
16363b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
163738f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops;
163838f7b870SPatrick McHardy 	struct net_device *dev;
163938f7b870SPatrick McHardy 	struct ifinfomsg *ifm;
164038f7b870SPatrick McHardy 	char ifname[IFNAMSIZ];
164138f7b870SPatrick McHardy 	struct nlattr *tb[IFLA_MAX+1];
164238f7b870SPatrick McHardy 	int err;
1643226bd341SEric Dumazet 	LIST_HEAD(list_kill);
164438f7b870SPatrick McHardy 
164538f7b870SPatrick McHardy 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
164638f7b870SPatrick McHardy 	if (err < 0)
164738f7b870SPatrick McHardy 		return err;
164838f7b870SPatrick McHardy 
164938f7b870SPatrick McHardy 	if (tb[IFLA_IFNAME])
165038f7b870SPatrick McHardy 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
165138f7b870SPatrick McHardy 
165238f7b870SPatrick McHardy 	ifm = nlmsg_data(nlh);
165338f7b870SPatrick McHardy 	if (ifm->ifi_index > 0)
1654881d966bSEric W. Biederman 		dev = __dev_get_by_index(net, ifm->ifi_index);
165538f7b870SPatrick McHardy 	else if (tb[IFLA_IFNAME])
1656881d966bSEric W. Biederman 		dev = __dev_get_by_name(net, ifname);
165738f7b870SPatrick McHardy 	else
165838f7b870SPatrick McHardy 		return -EINVAL;
165938f7b870SPatrick McHardy 
166038f7b870SPatrick McHardy 	if (!dev)
166138f7b870SPatrick McHardy 		return -ENODEV;
166238f7b870SPatrick McHardy 
166338f7b870SPatrick McHardy 	ops = dev->rtnl_link_ops;
166438f7b870SPatrick McHardy 	if (!ops)
166538f7b870SPatrick McHardy 		return -EOPNOTSUPP;
166638f7b870SPatrick McHardy 
1667226bd341SEric Dumazet 	ops->dellink(dev, &list_kill);
1668226bd341SEric Dumazet 	unregister_netdevice_many(&list_kill);
1669226bd341SEric Dumazet 	list_del(&list_kill);
167038f7b870SPatrick McHardy 	return 0;
167138f7b870SPatrick McHardy }
167238f7b870SPatrick McHardy 
16733729d502SPatrick McHardy int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
16743729d502SPatrick McHardy {
16753729d502SPatrick McHardy 	unsigned int old_flags;
16763729d502SPatrick McHardy 	int err;
16773729d502SPatrick McHardy 
16783729d502SPatrick McHardy 	old_flags = dev->flags;
16793729d502SPatrick McHardy 	if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
16803729d502SPatrick McHardy 		err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
16813729d502SPatrick McHardy 		if (err < 0)
16823729d502SPatrick McHardy 			return err;
16833729d502SPatrick McHardy 	}
16843729d502SPatrick McHardy 
16853729d502SPatrick McHardy 	dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
16863729d502SPatrick McHardy 
1687a528c219SNicolas Dichtel 	__dev_notify_flags(dev, old_flags, ~0U);
16883729d502SPatrick McHardy 	return 0;
16893729d502SPatrick McHardy }
16903729d502SPatrick McHardy EXPORT_SYMBOL(rtnl_configure_link);
16913729d502SPatrick McHardy 
1692c0713563SRami Rosen struct net_device *rtnl_create_link(struct net *net,
169381adee47SEric W. Biederman 	char *ifname, const struct rtnl_link_ops *ops, struct nlattr *tb[])
1694e7199288SPavel Emelianov {
1695e7199288SPavel Emelianov 	int err;
1696e7199288SPavel Emelianov 	struct net_device *dev;
1697d40156aaSJiri Pirko 	unsigned int num_tx_queues = 1;
1698d40156aaSJiri Pirko 	unsigned int num_rx_queues = 1;
1699e7199288SPavel Emelianov 
170076ff5cc9SJiri Pirko 	if (tb[IFLA_NUM_TX_QUEUES])
170176ff5cc9SJiri Pirko 		num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
170276ff5cc9SJiri Pirko 	else if (ops->get_num_tx_queues)
1703d40156aaSJiri Pirko 		num_tx_queues = ops->get_num_tx_queues();
170476ff5cc9SJiri Pirko 
170576ff5cc9SJiri Pirko 	if (tb[IFLA_NUM_RX_QUEUES])
170676ff5cc9SJiri Pirko 		num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
170776ff5cc9SJiri Pirko 	else if (ops->get_num_rx_queues)
1708d40156aaSJiri Pirko 		num_rx_queues = ops->get_num_rx_queues();
1709efacb309Sstephen hemminger 
1710e7199288SPavel Emelianov 	err = -ENOMEM;
1711d40156aaSJiri Pirko 	dev = alloc_netdev_mqs(ops->priv_size, ifname, ops->setup,
1712d40156aaSJiri Pirko 			       num_tx_queues, num_rx_queues);
1713e7199288SPavel Emelianov 	if (!dev)
1714e7199288SPavel Emelianov 		goto err;
1715e7199288SPavel Emelianov 
171681adee47SEric W. Biederman 	dev_net_set(dev, net);
171781adee47SEric W. Biederman 	dev->rtnl_link_ops = ops;
17183729d502SPatrick McHardy 	dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
171981adee47SEric W. Biederman 
1720e7199288SPavel Emelianov 	if (tb[IFLA_MTU])
1721e7199288SPavel Emelianov 		dev->mtu = nla_get_u32(tb[IFLA_MTU]);
17222afb9b53SJiri Pirko 	if (tb[IFLA_ADDRESS]) {
1723e7199288SPavel Emelianov 		memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
1724e7199288SPavel Emelianov 				nla_len(tb[IFLA_ADDRESS]));
17252afb9b53SJiri Pirko 		dev->addr_assign_type = NET_ADDR_SET;
17262afb9b53SJiri Pirko 	}
1727e7199288SPavel Emelianov 	if (tb[IFLA_BROADCAST])
1728e7199288SPavel Emelianov 		memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
1729e7199288SPavel Emelianov 				nla_len(tb[IFLA_BROADCAST]));
1730e7199288SPavel Emelianov 	if (tb[IFLA_TXQLEN])
1731e7199288SPavel Emelianov 		dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
1732e7199288SPavel Emelianov 	if (tb[IFLA_OPERSTATE])
173393b2d4a2SDavid S. Miller 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
1734e7199288SPavel Emelianov 	if (tb[IFLA_LINKMODE])
1735e7199288SPavel Emelianov 		dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
1736ffa934f1SPatrick McHardy 	if (tb[IFLA_GROUP])
1737ffa934f1SPatrick McHardy 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
1738e7199288SPavel Emelianov 
1739e7199288SPavel Emelianov 	return dev;
1740e7199288SPavel Emelianov 
1741e7199288SPavel Emelianov err:
1742e7199288SPavel Emelianov 	return ERR_PTR(err);
1743e7199288SPavel Emelianov }
1744e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_create_link);
1745e7199288SPavel Emelianov 
1746e7ed828fSVlad Dogaru static int rtnl_group_changelink(struct net *net, int group,
1747e7ed828fSVlad Dogaru 		struct ifinfomsg *ifm,
1748e7ed828fSVlad Dogaru 		struct nlattr **tb)
1749e7ed828fSVlad Dogaru {
1750e7ed828fSVlad Dogaru 	struct net_device *dev;
1751e7ed828fSVlad Dogaru 	int err;
1752e7ed828fSVlad Dogaru 
1753e7ed828fSVlad Dogaru 	for_each_netdev(net, dev) {
1754e7ed828fSVlad Dogaru 		if (dev->group == group) {
1755e7ed828fSVlad Dogaru 			err = do_setlink(dev, ifm, tb, NULL, 0);
1756e7ed828fSVlad Dogaru 			if (err < 0)
1757e7ed828fSVlad Dogaru 				return err;
1758e7ed828fSVlad Dogaru 		}
1759e7ed828fSVlad Dogaru 	}
1760e7ed828fSVlad Dogaru 
1761e7ed828fSVlad Dogaru 	return 0;
1762e7ed828fSVlad Dogaru }
1763e7ed828fSVlad Dogaru 
1764661d2967SThomas Graf static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh)
176538f7b870SPatrick McHardy {
17663b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
176738f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops;
176838f7b870SPatrick McHardy 	struct net_device *dev;
176938f7b870SPatrick McHardy 	struct ifinfomsg *ifm;
177038f7b870SPatrick McHardy 	char kind[MODULE_NAME_LEN];
177138f7b870SPatrick McHardy 	char ifname[IFNAMSIZ];
177238f7b870SPatrick McHardy 	struct nlattr *tb[IFLA_MAX+1];
177338f7b870SPatrick McHardy 	struct nlattr *linkinfo[IFLA_INFO_MAX+1];
177438f7b870SPatrick McHardy 	int err;
177538f7b870SPatrick McHardy 
177695a5afcaSJohannes Berg #ifdef CONFIG_MODULES
177738f7b870SPatrick McHardy replay:
17788072f085SThomas Graf #endif
177938f7b870SPatrick McHardy 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
178038f7b870SPatrick McHardy 	if (err < 0)
178138f7b870SPatrick McHardy 		return err;
178238f7b870SPatrick McHardy 
178338f7b870SPatrick McHardy 	if (tb[IFLA_IFNAME])
178438f7b870SPatrick McHardy 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
178538f7b870SPatrick McHardy 	else
178638f7b870SPatrick McHardy 		ifname[0] = '\0';
178738f7b870SPatrick McHardy 
178838f7b870SPatrick McHardy 	ifm = nlmsg_data(nlh);
178938f7b870SPatrick McHardy 	if (ifm->ifi_index > 0)
1790881d966bSEric W. Biederman 		dev = __dev_get_by_index(net, ifm->ifi_index);
1791e7ed828fSVlad Dogaru 	else {
1792e7ed828fSVlad Dogaru 		if (ifname[0])
1793881d966bSEric W. Biederman 			dev = __dev_get_by_name(net, ifname);
179438f7b870SPatrick McHardy 		else
179538f7b870SPatrick McHardy 			dev = NULL;
1796e7ed828fSVlad Dogaru 	}
179738f7b870SPatrick McHardy 
1798e0d087afSEric Dumazet 	err = validate_linkmsg(dev, tb);
1799e0d087afSEric Dumazet 	if (err < 0)
18001840bb13SThomas Graf 		return err;
18011840bb13SThomas Graf 
180238f7b870SPatrick McHardy 	if (tb[IFLA_LINKINFO]) {
180338f7b870SPatrick McHardy 		err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
180438f7b870SPatrick McHardy 				       tb[IFLA_LINKINFO], ifla_info_policy);
180538f7b870SPatrick McHardy 		if (err < 0)
180638f7b870SPatrick McHardy 			return err;
180738f7b870SPatrick McHardy 	} else
180838f7b870SPatrick McHardy 		memset(linkinfo, 0, sizeof(linkinfo));
180938f7b870SPatrick McHardy 
181038f7b870SPatrick McHardy 	if (linkinfo[IFLA_INFO_KIND]) {
181138f7b870SPatrick McHardy 		nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
181238f7b870SPatrick McHardy 		ops = rtnl_link_ops_get(kind);
181338f7b870SPatrick McHardy 	} else {
181438f7b870SPatrick McHardy 		kind[0] = '\0';
181538f7b870SPatrick McHardy 		ops = NULL;
181638f7b870SPatrick McHardy 	}
181738f7b870SPatrick McHardy 
181838f7b870SPatrick McHardy 	if (1) {
181938f7b870SPatrick McHardy 		struct nlattr *attr[ops ? ops->maxtype + 1 : 0], **data = NULL;
182081adee47SEric W. Biederman 		struct net *dest_net;
182138f7b870SPatrick McHardy 
182238f7b870SPatrick McHardy 		if (ops) {
182338f7b870SPatrick McHardy 			if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
182438f7b870SPatrick McHardy 				err = nla_parse_nested(attr, ops->maxtype,
182538f7b870SPatrick McHardy 						       linkinfo[IFLA_INFO_DATA],
182638f7b870SPatrick McHardy 						       ops->policy);
182738f7b870SPatrick McHardy 				if (err < 0)
182838f7b870SPatrick McHardy 					return err;
182938f7b870SPatrick McHardy 				data = attr;
183038f7b870SPatrick McHardy 			}
183138f7b870SPatrick McHardy 			if (ops->validate) {
183238f7b870SPatrick McHardy 				err = ops->validate(tb, data);
183338f7b870SPatrick McHardy 				if (err < 0)
183438f7b870SPatrick McHardy 					return err;
183538f7b870SPatrick McHardy 			}
183638f7b870SPatrick McHardy 		}
183738f7b870SPatrick McHardy 
183838f7b870SPatrick McHardy 		if (dev) {
183938f7b870SPatrick McHardy 			int modified = 0;
184038f7b870SPatrick McHardy 
184138f7b870SPatrick McHardy 			if (nlh->nlmsg_flags & NLM_F_EXCL)
184238f7b870SPatrick McHardy 				return -EEXIST;
184338f7b870SPatrick McHardy 			if (nlh->nlmsg_flags & NLM_F_REPLACE)
184438f7b870SPatrick McHardy 				return -EOPNOTSUPP;
184538f7b870SPatrick McHardy 
184638f7b870SPatrick McHardy 			if (linkinfo[IFLA_INFO_DATA]) {
184738f7b870SPatrick McHardy 				if (!ops || ops != dev->rtnl_link_ops ||
184838f7b870SPatrick McHardy 				    !ops->changelink)
184938f7b870SPatrick McHardy 					return -EOPNOTSUPP;
185038f7b870SPatrick McHardy 
185138f7b870SPatrick McHardy 				err = ops->changelink(dev, tb, data);
185238f7b870SPatrick McHardy 				if (err < 0)
185338f7b870SPatrick McHardy 					return err;
185438f7b870SPatrick McHardy 				modified = 1;
185538f7b870SPatrick McHardy 			}
185638f7b870SPatrick McHardy 
185738f7b870SPatrick McHardy 			return do_setlink(dev, ifm, tb, ifname, modified);
185838f7b870SPatrick McHardy 		}
185938f7b870SPatrick McHardy 
1860ffa934f1SPatrick McHardy 		if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
1861ffa934f1SPatrick McHardy 			if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
1862ffa934f1SPatrick McHardy 				return rtnl_group_changelink(net,
1863ffa934f1SPatrick McHardy 						nla_get_u32(tb[IFLA_GROUP]),
1864ffa934f1SPatrick McHardy 						ifm, tb);
186538f7b870SPatrick McHardy 			return -ENODEV;
1866ffa934f1SPatrick McHardy 		}
186738f7b870SPatrick McHardy 
18680e06877cSPatrick McHardy 		if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
186938f7b870SPatrick McHardy 			return -EOPNOTSUPP;
187038f7b870SPatrick McHardy 
187138f7b870SPatrick McHardy 		if (!ops) {
187295a5afcaSJohannes Berg #ifdef CONFIG_MODULES
187338f7b870SPatrick McHardy 			if (kind[0]) {
187438f7b870SPatrick McHardy 				__rtnl_unlock();
187538f7b870SPatrick McHardy 				request_module("rtnl-link-%s", kind);
187638f7b870SPatrick McHardy 				rtnl_lock();
187738f7b870SPatrick McHardy 				ops = rtnl_link_ops_get(kind);
187838f7b870SPatrick McHardy 				if (ops)
187938f7b870SPatrick McHardy 					goto replay;
188038f7b870SPatrick McHardy 			}
188138f7b870SPatrick McHardy #endif
188238f7b870SPatrick McHardy 			return -EOPNOTSUPP;
188338f7b870SPatrick McHardy 		}
188438f7b870SPatrick McHardy 
188538f7b870SPatrick McHardy 		if (!ifname[0])
188638f7b870SPatrick McHardy 			snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
188738f7b870SPatrick McHardy 
188881adee47SEric W. Biederman 		dest_net = rtnl_link_get_net(net, tb);
188913ad1774SEric W. Biederman 		if (IS_ERR(dest_net))
189013ad1774SEric W. Biederman 			return PTR_ERR(dest_net);
189113ad1774SEric W. Biederman 
1892c0713563SRami Rosen 		dev = rtnl_create_link(dest_net, ifname, ops, tb);
18939c7dafbfSPavel Emelyanov 		if (IS_ERR(dev)) {
1894e7199288SPavel Emelianov 			err = PTR_ERR(dev);
18959c7dafbfSPavel Emelyanov 			goto out;
18969c7dafbfSPavel Emelyanov 		}
18979c7dafbfSPavel Emelyanov 
18989c7dafbfSPavel Emelyanov 		dev->ifindex = ifm->ifi_index;
18999c7dafbfSPavel Emelyanov 
19009c7dafbfSPavel Emelyanov 		if (ops->newlink)
190181adee47SEric W. Biederman 			err = ops->newlink(net, dev, tb, data);
19022d85cba2SPatrick McHardy 		else
19032d85cba2SPatrick McHardy 			err = register_netdevice(dev);
190480032cffSDan Carpenter 
1905fce9b9beSDan Carpenter 		if (err < 0) {
190638f7b870SPatrick McHardy 			free_netdev(dev);
19073729d502SPatrick McHardy 			goto out;
1908fce9b9beSDan Carpenter 		}
190981adee47SEric W. Biederman 
19103729d502SPatrick McHardy 		err = rtnl_configure_link(dev, ifm);
19113729d502SPatrick McHardy 		if (err < 0)
19123729d502SPatrick McHardy 			unregister_netdevice(dev);
19133729d502SPatrick McHardy out:
191481adee47SEric W. Biederman 		put_net(dest_net);
191538f7b870SPatrick McHardy 		return err;
191638f7b870SPatrick McHardy 	}
191738f7b870SPatrick McHardy }
191838f7b870SPatrick McHardy 
1919661d2967SThomas Graf static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh)
1920711e2c33SJean Tourrilhes {
19213b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
1922b60c5115SThomas Graf 	struct ifinfomsg *ifm;
1923a3d12891SEric Dumazet 	char ifname[IFNAMSIZ];
1924b60c5115SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
1925b60c5115SThomas Graf 	struct net_device *dev = NULL;
1926b60c5115SThomas Graf 	struct sk_buff *nskb;
1927339bf98fSThomas Graf 	int err;
1928115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
1929711e2c33SJean Tourrilhes 
1930b60c5115SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1931b60c5115SThomas Graf 	if (err < 0)
19329918f230SEric Sesterhenn 		return err;
1933b60c5115SThomas Graf 
1934a3d12891SEric Dumazet 	if (tb[IFLA_IFNAME])
1935a3d12891SEric Dumazet 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1936a3d12891SEric Dumazet 
1937115c9b81SGreg Rose 	if (tb[IFLA_EXT_MASK])
1938115c9b81SGreg Rose 		ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1939115c9b81SGreg Rose 
1940b60c5115SThomas Graf 	ifm = nlmsg_data(nlh);
1941a3d12891SEric Dumazet 	if (ifm->ifi_index > 0)
1942a3d12891SEric Dumazet 		dev = __dev_get_by_index(net, ifm->ifi_index);
1943a3d12891SEric Dumazet 	else if (tb[IFLA_IFNAME])
1944a3d12891SEric Dumazet 		dev = __dev_get_by_name(net, ifname);
1945a3d12891SEric Dumazet 	else
1946b60c5115SThomas Graf 		return -EINVAL;
1947b60c5115SThomas Graf 
1948a3d12891SEric Dumazet 	if (dev == NULL)
1949a3d12891SEric Dumazet 		return -ENODEV;
1950a3d12891SEric Dumazet 
1951115c9b81SGreg Rose 	nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
1952a3d12891SEric Dumazet 	if (nskb == NULL)
1953a3d12891SEric Dumazet 		return -ENOBUFS;
1954711e2c33SJean Tourrilhes 
195515e47304SEric W. Biederman 	err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid,
1956115c9b81SGreg Rose 			       nlh->nlmsg_seq, 0, 0, ext_filter_mask);
195726932566SPatrick McHardy 	if (err < 0) {
195826932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in if_nlmsg_size */
195926932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
196026932566SPatrick McHardy 		kfree_skb(nskb);
1961a3d12891SEric Dumazet 	} else
196215e47304SEric W. Biederman 		err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
1963b60c5115SThomas Graf 
1964711e2c33SJean Tourrilhes 	return err;
1965711e2c33SJean Tourrilhes }
1966711e2c33SJean Tourrilhes 
1967115c9b81SGreg Rose static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
1968c7ac8679SGreg Rose {
1969115c9b81SGreg Rose 	struct net *net = sock_net(skb->sk);
1970115c9b81SGreg Rose 	struct net_device *dev;
1971115c9b81SGreg Rose 	struct nlattr *tb[IFLA_MAX+1];
1972115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
1973115c9b81SGreg Rose 	u16 min_ifinfo_dump_size = 0;
1974115c9b81SGreg Rose 
197588c5b5ceSMichael Riesch 	if (nlmsg_parse(nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
1976a4b64fbeSEric Dumazet 			ifla_policy) >= 0) {
1977115c9b81SGreg Rose 		if (tb[IFLA_EXT_MASK])
1978115c9b81SGreg Rose 			ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1979a4b64fbeSEric Dumazet 	}
1980115c9b81SGreg Rose 
1981115c9b81SGreg Rose 	if (!ext_filter_mask)
1982115c9b81SGreg Rose 		return NLMSG_GOODSIZE;
1983115c9b81SGreg Rose 	/*
1984115c9b81SGreg Rose 	 * traverse the list of net devices and compute the minimum
1985115c9b81SGreg Rose 	 * buffer size based upon the filter mask.
1986115c9b81SGreg Rose 	 */
1987115c9b81SGreg Rose 	list_for_each_entry(dev, &net->dev_base_head, dev_list) {
1988115c9b81SGreg Rose 		min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
1989115c9b81SGreg Rose 					     if_nlmsg_size(dev,
1990115c9b81SGreg Rose 						           ext_filter_mask));
1991115c9b81SGreg Rose 	}
1992115c9b81SGreg Rose 
1993c7ac8679SGreg Rose 	return min_ifinfo_dump_size;
1994c7ac8679SGreg Rose }
1995c7ac8679SGreg Rose 
199642bad1daSAdrian Bunk static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
19971da177e4SLinus Torvalds {
19981da177e4SLinus Torvalds 	int idx;
19991da177e4SLinus Torvalds 	int s_idx = cb->family;
20001da177e4SLinus Torvalds 
20011da177e4SLinus Torvalds 	if (s_idx == 0)
20021da177e4SLinus Torvalds 		s_idx = 1;
200325239ceeSPatrick McHardy 	for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
20041da177e4SLinus Torvalds 		int type = cb->nlh->nlmsg_type-RTM_BASE;
20051da177e4SLinus Torvalds 		if (idx < s_idx || idx == PF_PACKET)
20061da177e4SLinus Torvalds 			continue;
2007e2849863SThomas Graf 		if (rtnl_msg_handlers[idx] == NULL ||
2008e2849863SThomas Graf 		    rtnl_msg_handlers[idx][type].dumpit == NULL)
20091da177e4SLinus Torvalds 			continue;
20100465277fSNicolas Dichtel 		if (idx > s_idx) {
20111da177e4SLinus Torvalds 			memset(&cb->args[0], 0, sizeof(cb->args));
20120465277fSNicolas Dichtel 			cb->prev_seq = 0;
20130465277fSNicolas Dichtel 			cb->seq = 0;
20140465277fSNicolas Dichtel 		}
2015e2849863SThomas Graf 		if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
20161da177e4SLinus Torvalds 			break;
20171da177e4SLinus Torvalds 	}
20181da177e4SLinus Torvalds 	cb->family = idx;
20191da177e4SLinus Torvalds 
20201da177e4SLinus Torvalds 	return skb->len;
20211da177e4SLinus Torvalds }
20221da177e4SLinus Torvalds 
20237f294054SAlexei Starovoitov void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
20247f294054SAlexei Starovoitov 		  gfp_t flags)
20251da177e4SLinus Torvalds {
2026c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
20271da177e4SLinus Torvalds 	struct sk_buff *skb;
20280ec6d3f4SThomas Graf 	int err = -ENOBUFS;
2029c7ac8679SGreg Rose 	size_t if_info_size;
20301da177e4SLinus Torvalds 
20317f294054SAlexei Starovoitov 	skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
20320ec6d3f4SThomas Graf 	if (skb == NULL)
20330ec6d3f4SThomas Graf 		goto errout;
20341da177e4SLinus Torvalds 
2035115c9b81SGreg Rose 	err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0);
203626932566SPatrick McHardy 	if (err < 0) {
203726932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in if_nlmsg_size() */
203826932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
203926932566SPatrick McHardy 		kfree_skb(skb);
204026932566SPatrick McHardy 		goto errout;
204126932566SPatrick McHardy 	}
20427f294054SAlexei Starovoitov 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
20431ce85fe4SPablo Neira Ayuso 	return;
20440ec6d3f4SThomas Graf errout:
20450ec6d3f4SThomas Graf 	if (err < 0)
20464b3da706SEric W. Biederman 		rtnl_set_sk_err(net, RTNLGRP_LINK, err);
20471da177e4SLinus Torvalds }
2048471cb5a3SJiri Pirko EXPORT_SYMBOL(rtmsg_ifinfo);
20491da177e4SLinus Torvalds 
2050d83b0603SJohn Fastabend static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
2051d83b0603SJohn Fastabend 				   struct net_device *dev,
2052d83b0603SJohn Fastabend 				   u8 *addr, u32 pid, u32 seq,
2053d83b0603SJohn Fastabend 				   int type, unsigned int flags)
2054d83b0603SJohn Fastabend {
2055d83b0603SJohn Fastabend 	struct nlmsghdr *nlh;
2056d83b0603SJohn Fastabend 	struct ndmsg *ndm;
2057d83b0603SJohn Fastabend 
2058d83b0603SJohn Fastabend 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), NLM_F_MULTI);
2059d83b0603SJohn Fastabend 	if (!nlh)
2060d83b0603SJohn Fastabend 		return -EMSGSIZE;
2061d83b0603SJohn Fastabend 
2062d83b0603SJohn Fastabend 	ndm = nlmsg_data(nlh);
2063d83b0603SJohn Fastabend 	ndm->ndm_family  = AF_BRIDGE;
2064d83b0603SJohn Fastabend 	ndm->ndm_pad1	 = 0;
2065d83b0603SJohn Fastabend 	ndm->ndm_pad2    = 0;
2066d83b0603SJohn Fastabend 	ndm->ndm_flags	 = flags;
2067d83b0603SJohn Fastabend 	ndm->ndm_type	 = 0;
2068d83b0603SJohn Fastabend 	ndm->ndm_ifindex = dev->ifindex;
2069d83b0603SJohn Fastabend 	ndm->ndm_state   = NUD_PERMANENT;
2070d83b0603SJohn Fastabend 
2071d83b0603SJohn Fastabend 	if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
2072d83b0603SJohn Fastabend 		goto nla_put_failure;
2073d83b0603SJohn Fastabend 
2074d83b0603SJohn Fastabend 	return nlmsg_end(skb, nlh);
2075d83b0603SJohn Fastabend 
2076d83b0603SJohn Fastabend nla_put_failure:
2077d83b0603SJohn Fastabend 	nlmsg_cancel(skb, nlh);
2078d83b0603SJohn Fastabend 	return -EMSGSIZE;
2079d83b0603SJohn Fastabend }
2080d83b0603SJohn Fastabend 
20813ff661c3SJohn Fastabend static inline size_t rtnl_fdb_nlmsg_size(void)
20823ff661c3SJohn Fastabend {
20833ff661c3SJohn Fastabend 	return NLMSG_ALIGN(sizeof(struct ndmsg)) + nla_total_size(ETH_ALEN);
20843ff661c3SJohn Fastabend }
20853ff661c3SJohn Fastabend 
20863ff661c3SJohn Fastabend static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, int type)
20873ff661c3SJohn Fastabend {
20883ff661c3SJohn Fastabend 	struct net *net = dev_net(dev);
20893ff661c3SJohn Fastabend 	struct sk_buff *skb;
20903ff661c3SJohn Fastabend 	int err = -ENOBUFS;
20913ff661c3SJohn Fastabend 
20923ff661c3SJohn Fastabend 	skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
20933ff661c3SJohn Fastabend 	if (!skb)
20943ff661c3SJohn Fastabend 		goto errout;
20953ff661c3SJohn Fastabend 
20963ff661c3SJohn Fastabend 	err = nlmsg_populate_fdb_fill(skb, dev, addr, 0, 0, type, NTF_SELF);
20973ff661c3SJohn Fastabend 	if (err < 0) {
20983ff661c3SJohn Fastabend 		kfree_skb(skb);
20993ff661c3SJohn Fastabend 		goto errout;
21003ff661c3SJohn Fastabend 	}
21013ff661c3SJohn Fastabend 
21023ff661c3SJohn Fastabend 	rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
21033ff661c3SJohn Fastabend 	return;
21043ff661c3SJohn Fastabend errout:
21053ff661c3SJohn Fastabend 	rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
21063ff661c3SJohn Fastabend }
21073ff661c3SJohn Fastabend 
2108090096bfSVlad Yasevich /**
2109090096bfSVlad Yasevich  * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
2110090096bfSVlad Yasevich  */
2111090096bfSVlad Yasevich int ndo_dflt_fdb_add(struct ndmsg *ndm,
2112090096bfSVlad Yasevich 		     struct nlattr *tb[],
2113090096bfSVlad Yasevich 		     struct net_device *dev,
2114090096bfSVlad Yasevich 		     const unsigned char *addr,
2115090096bfSVlad Yasevich 		     u16 flags)
2116090096bfSVlad Yasevich {
2117090096bfSVlad Yasevich 	int err = -EINVAL;
2118090096bfSVlad Yasevich 
2119090096bfSVlad Yasevich 	/* If aging addresses are supported device will need to
2120090096bfSVlad Yasevich 	 * implement its own handler for this.
2121090096bfSVlad Yasevich 	 */
2122090096bfSVlad Yasevich 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
2123090096bfSVlad Yasevich 		pr_info("%s: FDB only supports static addresses\n", dev->name);
2124090096bfSVlad Yasevich 		return err;
2125090096bfSVlad Yasevich 	}
2126090096bfSVlad Yasevich 
2127090096bfSVlad Yasevich 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2128090096bfSVlad Yasevich 		err = dev_uc_add_excl(dev, addr);
2129090096bfSVlad Yasevich 	else if (is_multicast_ether_addr(addr))
2130090096bfSVlad Yasevich 		err = dev_mc_add_excl(dev, addr);
2131090096bfSVlad Yasevich 
2132090096bfSVlad Yasevich 	/* Only return duplicate errors if NLM_F_EXCL is set */
2133090096bfSVlad Yasevich 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
2134090096bfSVlad Yasevich 		err = 0;
2135090096bfSVlad Yasevich 
2136090096bfSVlad Yasevich 	return err;
2137090096bfSVlad Yasevich }
2138090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_add);
2139090096bfSVlad Yasevich 
2140661d2967SThomas Graf static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh)
214177162022SJohn Fastabend {
214277162022SJohn Fastabend 	struct net *net = sock_net(skb->sk);
214377162022SJohn Fastabend 	struct ndmsg *ndm;
214477162022SJohn Fastabend 	struct nlattr *tb[NDA_MAX+1];
214577162022SJohn Fastabend 	struct net_device *dev;
214677162022SJohn Fastabend 	u8 *addr;
214777162022SJohn Fastabend 	int err;
214877162022SJohn Fastabend 
214977162022SJohn Fastabend 	err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
215077162022SJohn Fastabend 	if (err < 0)
215177162022SJohn Fastabend 		return err;
215277162022SJohn Fastabend 
215377162022SJohn Fastabend 	ndm = nlmsg_data(nlh);
215477162022SJohn Fastabend 	if (ndm->ndm_ifindex == 0) {
215577162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
215677162022SJohn Fastabend 		return -EINVAL;
215777162022SJohn Fastabend 	}
215877162022SJohn Fastabend 
215977162022SJohn Fastabend 	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
216077162022SJohn Fastabend 	if (dev == NULL) {
216177162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
216277162022SJohn Fastabend 		return -ENODEV;
216377162022SJohn Fastabend 	}
216477162022SJohn Fastabend 
216577162022SJohn Fastabend 	if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
216677162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
216777162022SJohn Fastabend 		return -EINVAL;
216877162022SJohn Fastabend 	}
216977162022SJohn Fastabend 
217077162022SJohn Fastabend 	addr = nla_data(tb[NDA_LLADDR]);
217177162022SJohn Fastabend 
217277162022SJohn Fastabend 	err = -EOPNOTSUPP;
217377162022SJohn Fastabend 
217477162022SJohn Fastabend 	/* Support fdb on master device the net/bridge default case */
217577162022SJohn Fastabend 	if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
217677162022SJohn Fastabend 	    (dev->priv_flags & IFF_BRIDGE_PORT)) {
2177898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2178898e5061SJiri Pirko 		const struct net_device_ops *ops = br_dev->netdev_ops;
2179898e5061SJiri Pirko 
2180898e5061SJiri Pirko 		err = ops->ndo_fdb_add(ndm, tb, dev, addr, nlh->nlmsg_flags);
218177162022SJohn Fastabend 		if (err)
218277162022SJohn Fastabend 			goto out;
218377162022SJohn Fastabend 		else
218477162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_MASTER;
218577162022SJohn Fastabend 	}
218677162022SJohn Fastabend 
218777162022SJohn Fastabend 	/* Embedded bridge, macvlan, and any other device support */
2188090096bfSVlad Yasevich 	if ((ndm->ndm_flags & NTF_SELF)) {
2189090096bfSVlad Yasevich 		if (dev->netdev_ops->ndo_fdb_add)
2190090096bfSVlad Yasevich 			err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
2191090096bfSVlad Yasevich 							   nlh->nlmsg_flags);
2192090096bfSVlad Yasevich 		else
2193090096bfSVlad Yasevich 			err = ndo_dflt_fdb_add(ndm, tb, dev, addr,
219477162022SJohn Fastabend 					       nlh->nlmsg_flags);
219577162022SJohn Fastabend 
21963ff661c3SJohn Fastabend 		if (!err) {
21973ff661c3SJohn Fastabend 			rtnl_fdb_notify(dev, addr, RTM_NEWNEIGH);
219877162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_SELF;
219977162022SJohn Fastabend 		}
22003ff661c3SJohn Fastabend 	}
220177162022SJohn Fastabend out:
220277162022SJohn Fastabend 	return err;
220377162022SJohn Fastabend }
220477162022SJohn Fastabend 
2205090096bfSVlad Yasevich /**
2206090096bfSVlad Yasevich  * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
2207090096bfSVlad Yasevich  */
2208090096bfSVlad Yasevich int ndo_dflt_fdb_del(struct ndmsg *ndm,
2209090096bfSVlad Yasevich 		     struct nlattr *tb[],
2210090096bfSVlad Yasevich 		     struct net_device *dev,
2211090096bfSVlad Yasevich 		     const unsigned char *addr)
2212090096bfSVlad Yasevich {
2213090096bfSVlad Yasevich 	int err = -EOPNOTSUPP;
2214090096bfSVlad Yasevich 
2215090096bfSVlad Yasevich 	/* If aging addresses are supported device will need to
2216090096bfSVlad Yasevich 	 * implement its own handler for this.
2217090096bfSVlad Yasevich 	 */
221864535993SSridhar Samudrala 	if (!(ndm->ndm_state & NUD_PERMANENT)) {
2219090096bfSVlad Yasevich 		pr_info("%s: FDB only supports static addresses\n", dev->name);
2220090096bfSVlad Yasevich 		return -EINVAL;
2221090096bfSVlad Yasevich 	}
2222090096bfSVlad Yasevich 
2223090096bfSVlad Yasevich 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2224090096bfSVlad Yasevich 		err = dev_uc_del(dev, addr);
2225090096bfSVlad Yasevich 	else if (is_multicast_ether_addr(addr))
2226090096bfSVlad Yasevich 		err = dev_mc_del(dev, addr);
2227090096bfSVlad Yasevich 	else
2228090096bfSVlad Yasevich 		err = -EINVAL;
2229090096bfSVlad Yasevich 
2230090096bfSVlad Yasevich 	return err;
2231090096bfSVlad Yasevich }
2232090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_del);
2233090096bfSVlad Yasevich 
2234661d2967SThomas Graf static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh)
223577162022SJohn Fastabend {
223677162022SJohn Fastabend 	struct net *net = sock_net(skb->sk);
223777162022SJohn Fastabend 	struct ndmsg *ndm;
22381690be63SVlad Yasevich 	struct nlattr *tb[NDA_MAX+1];
223977162022SJohn Fastabend 	struct net_device *dev;
224077162022SJohn Fastabend 	int err = -EINVAL;
224177162022SJohn Fastabend 	__u8 *addr;
224277162022SJohn Fastabend 
22431690be63SVlad Yasevich 	if (!capable(CAP_NET_ADMIN))
22441690be63SVlad Yasevich 		return -EPERM;
22451690be63SVlad Yasevich 
22461690be63SVlad Yasevich 	err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
22471690be63SVlad Yasevich 	if (err < 0)
22481690be63SVlad Yasevich 		return err;
224977162022SJohn Fastabend 
225077162022SJohn Fastabend 	ndm = nlmsg_data(nlh);
225177162022SJohn Fastabend 	if (ndm->ndm_ifindex == 0) {
225277162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
225377162022SJohn Fastabend 		return -EINVAL;
225477162022SJohn Fastabend 	}
225577162022SJohn Fastabend 
225677162022SJohn Fastabend 	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
225777162022SJohn Fastabend 	if (dev == NULL) {
225877162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
225977162022SJohn Fastabend 		return -ENODEV;
226077162022SJohn Fastabend 	}
226177162022SJohn Fastabend 
22621690be63SVlad Yasevich 	if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
22631690be63SVlad Yasevich 		pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n");
226477162022SJohn Fastabend 		return -EINVAL;
226577162022SJohn Fastabend 	}
226677162022SJohn Fastabend 
22671690be63SVlad Yasevich 	addr = nla_data(tb[NDA_LLADDR]);
22681690be63SVlad Yasevich 
226977162022SJohn Fastabend 	err = -EOPNOTSUPP;
227077162022SJohn Fastabend 
227177162022SJohn Fastabend 	/* Support fdb on master device the net/bridge default case */
227277162022SJohn Fastabend 	if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
227377162022SJohn Fastabend 	    (dev->priv_flags & IFF_BRIDGE_PORT)) {
2274898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2275898e5061SJiri Pirko 		const struct net_device_ops *ops = br_dev->netdev_ops;
227677162022SJohn Fastabend 
2277898e5061SJiri Pirko 		if (ops->ndo_fdb_del)
22781690be63SVlad Yasevich 			err = ops->ndo_fdb_del(ndm, tb, dev, addr);
227977162022SJohn Fastabend 
228077162022SJohn Fastabend 		if (err)
228177162022SJohn Fastabend 			goto out;
228277162022SJohn Fastabend 		else
228377162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_MASTER;
228477162022SJohn Fastabend 	}
228577162022SJohn Fastabend 
228677162022SJohn Fastabend 	/* Embedded bridge, macvlan, and any other device support */
2287090096bfSVlad Yasevich 	if (ndm->ndm_flags & NTF_SELF) {
2288090096bfSVlad Yasevich 		if (dev->netdev_ops->ndo_fdb_del)
22891690be63SVlad Yasevich 			err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr);
2290090096bfSVlad Yasevich 		else
2291090096bfSVlad Yasevich 			err = ndo_dflt_fdb_del(ndm, tb, dev, addr);
229277162022SJohn Fastabend 
22933ff661c3SJohn Fastabend 		if (!err) {
22943ff661c3SJohn Fastabend 			rtnl_fdb_notify(dev, addr, RTM_DELNEIGH);
229577162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_SELF;
229677162022SJohn Fastabend 		}
22973ff661c3SJohn Fastabend 	}
229877162022SJohn Fastabend out:
229977162022SJohn Fastabend 	return err;
230077162022SJohn Fastabend }
230177162022SJohn Fastabend 
2302d83b0603SJohn Fastabend static int nlmsg_populate_fdb(struct sk_buff *skb,
2303d83b0603SJohn Fastabend 			      struct netlink_callback *cb,
2304d83b0603SJohn Fastabend 			      struct net_device *dev,
2305d83b0603SJohn Fastabend 			      int *idx,
2306d83b0603SJohn Fastabend 			      struct netdev_hw_addr_list *list)
2307d83b0603SJohn Fastabend {
2308d83b0603SJohn Fastabend 	struct netdev_hw_addr *ha;
2309d83b0603SJohn Fastabend 	int err;
231015e47304SEric W. Biederman 	u32 portid, seq;
2311d83b0603SJohn Fastabend 
231215e47304SEric W. Biederman 	portid = NETLINK_CB(cb->skb).portid;
2313d83b0603SJohn Fastabend 	seq = cb->nlh->nlmsg_seq;
2314d83b0603SJohn Fastabend 
2315d83b0603SJohn Fastabend 	list_for_each_entry(ha, &list->list, list) {
2316d83b0603SJohn Fastabend 		if (*idx < cb->args[0])
2317d83b0603SJohn Fastabend 			goto skip;
2318d83b0603SJohn Fastabend 
2319d83b0603SJohn Fastabend 		err = nlmsg_populate_fdb_fill(skb, dev, ha->addr,
2320a7a558feSJohn Fastabend 					      portid, seq,
2321a7a558feSJohn Fastabend 					      RTM_NEWNEIGH, NTF_SELF);
2322d83b0603SJohn Fastabend 		if (err < 0)
2323d83b0603SJohn Fastabend 			return err;
2324d83b0603SJohn Fastabend skip:
2325d83b0603SJohn Fastabend 		*idx += 1;
2326d83b0603SJohn Fastabend 	}
2327d83b0603SJohn Fastabend 	return 0;
2328d83b0603SJohn Fastabend }
2329d83b0603SJohn Fastabend 
2330d83b0603SJohn Fastabend /**
23312c53040fSBen Hutchings  * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
2332d83b0603SJohn Fastabend  * @nlh: netlink message header
2333d83b0603SJohn Fastabend  * @dev: netdevice
2334d83b0603SJohn Fastabend  *
2335d83b0603SJohn Fastabend  * Default netdevice operation to dump the existing unicast address list.
233691f3e7b1SJohn Fastabend  * Returns number of addresses from list put in skb.
2337d83b0603SJohn Fastabend  */
2338d83b0603SJohn Fastabend int ndo_dflt_fdb_dump(struct sk_buff *skb,
2339d83b0603SJohn Fastabend 		      struct netlink_callback *cb,
2340d83b0603SJohn Fastabend 		      struct net_device *dev,
2341d83b0603SJohn Fastabend 		      int idx)
2342d83b0603SJohn Fastabend {
2343d83b0603SJohn Fastabend 	int err;
2344d83b0603SJohn Fastabend 
2345d83b0603SJohn Fastabend 	netif_addr_lock_bh(dev);
2346d83b0603SJohn Fastabend 	err = nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->uc);
2347d83b0603SJohn Fastabend 	if (err)
2348d83b0603SJohn Fastabend 		goto out;
2349d83b0603SJohn Fastabend 	nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->mc);
2350d83b0603SJohn Fastabend out:
2351d83b0603SJohn Fastabend 	netif_addr_unlock_bh(dev);
2352d83b0603SJohn Fastabend 	return idx;
2353d83b0603SJohn Fastabend }
2354d83b0603SJohn Fastabend EXPORT_SYMBOL(ndo_dflt_fdb_dump);
2355d83b0603SJohn Fastabend 
235677162022SJohn Fastabend static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
235777162022SJohn Fastabend {
235877162022SJohn Fastabend 	int idx = 0;
235977162022SJohn Fastabend 	struct net *net = sock_net(skb->sk);
236077162022SJohn Fastabend 	struct net_device *dev;
236177162022SJohn Fastabend 
236277162022SJohn Fastabend 	rcu_read_lock();
236377162022SJohn Fastabend 	for_each_netdev_rcu(net, dev) {
236477162022SJohn Fastabend 		if (dev->priv_flags & IFF_BRIDGE_PORT) {
2365898e5061SJiri Pirko 			struct net_device *br_dev;
2366898e5061SJiri Pirko 			const struct net_device_ops *ops;
236777162022SJohn Fastabend 
2368898e5061SJiri Pirko 			br_dev = netdev_master_upper_dev_get(dev);
2369898e5061SJiri Pirko 			ops = br_dev->netdev_ops;
237077162022SJohn Fastabend 			if (ops->ndo_fdb_dump)
237177162022SJohn Fastabend 				idx = ops->ndo_fdb_dump(skb, cb, dev, idx);
237277162022SJohn Fastabend 		}
237377162022SJohn Fastabend 
237477162022SJohn Fastabend 		if (dev->netdev_ops->ndo_fdb_dump)
237577162022SJohn Fastabend 			idx = dev->netdev_ops->ndo_fdb_dump(skb, cb, dev, idx);
2376090096bfSVlad Yasevich 		else
237791f3e7b1SJohn Fastabend 			idx = ndo_dflt_fdb_dump(skb, cb, dev, idx);
237877162022SJohn Fastabend 	}
237977162022SJohn Fastabend 	rcu_read_unlock();
238077162022SJohn Fastabend 
238177162022SJohn Fastabend 	cb->args[0] = idx;
238277162022SJohn Fastabend 	return skb->len;
238377162022SJohn Fastabend }
238477162022SJohn Fastabend 
2385815cccbfSJohn Fastabend int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
2386815cccbfSJohn Fastabend 			    struct net_device *dev, u16 mode)
2387815cccbfSJohn Fastabend {
2388815cccbfSJohn Fastabend 	struct nlmsghdr *nlh;
2389815cccbfSJohn Fastabend 	struct ifinfomsg *ifm;
2390815cccbfSJohn Fastabend 	struct nlattr *br_afspec;
2391815cccbfSJohn Fastabend 	u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
2392898e5061SJiri Pirko 	struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2393815cccbfSJohn Fastabend 
2394815cccbfSJohn Fastabend 	nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), NLM_F_MULTI);
2395815cccbfSJohn Fastabend 	if (nlh == NULL)
2396815cccbfSJohn Fastabend 		return -EMSGSIZE;
2397815cccbfSJohn Fastabend 
2398815cccbfSJohn Fastabend 	ifm = nlmsg_data(nlh);
2399815cccbfSJohn Fastabend 	ifm->ifi_family = AF_BRIDGE;
2400815cccbfSJohn Fastabend 	ifm->__ifi_pad = 0;
2401815cccbfSJohn Fastabend 	ifm->ifi_type = dev->type;
2402815cccbfSJohn Fastabend 	ifm->ifi_index = dev->ifindex;
2403815cccbfSJohn Fastabend 	ifm->ifi_flags = dev_get_flags(dev);
2404815cccbfSJohn Fastabend 	ifm->ifi_change = 0;
2405815cccbfSJohn Fastabend 
2406815cccbfSJohn Fastabend 
2407815cccbfSJohn Fastabend 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
2408815cccbfSJohn Fastabend 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
2409815cccbfSJohn Fastabend 	    nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
2410898e5061SJiri Pirko 	    (br_dev &&
2411898e5061SJiri Pirko 	     nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
2412815cccbfSJohn Fastabend 	    (dev->addr_len &&
2413815cccbfSJohn Fastabend 	     nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
2414815cccbfSJohn Fastabend 	    (dev->ifindex != dev->iflink &&
2415815cccbfSJohn Fastabend 	     nla_put_u32(skb, IFLA_LINK, dev->iflink)))
2416815cccbfSJohn Fastabend 		goto nla_put_failure;
2417815cccbfSJohn Fastabend 
2418815cccbfSJohn Fastabend 	br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
2419815cccbfSJohn Fastabend 	if (!br_afspec)
2420815cccbfSJohn Fastabend 		goto nla_put_failure;
2421815cccbfSJohn Fastabend 
2422815cccbfSJohn Fastabend 	if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF) ||
2423815cccbfSJohn Fastabend 	    nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
2424815cccbfSJohn Fastabend 		nla_nest_cancel(skb, br_afspec);
2425815cccbfSJohn Fastabend 		goto nla_put_failure;
2426815cccbfSJohn Fastabend 	}
2427815cccbfSJohn Fastabend 	nla_nest_end(skb, br_afspec);
2428815cccbfSJohn Fastabend 
2429815cccbfSJohn Fastabend 	return nlmsg_end(skb, nlh);
2430815cccbfSJohn Fastabend nla_put_failure:
2431815cccbfSJohn Fastabend 	nlmsg_cancel(skb, nlh);
2432815cccbfSJohn Fastabend 	return -EMSGSIZE;
2433815cccbfSJohn Fastabend }
2434815cccbfSJohn Fastabend EXPORT_SYMBOL(ndo_dflt_bridge_getlink);
2435815cccbfSJohn Fastabend 
2436e5a55a89SJohn Fastabend static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
2437e5a55a89SJohn Fastabend {
2438e5a55a89SJohn Fastabend 	struct net *net = sock_net(skb->sk);
2439e5a55a89SJohn Fastabend 	struct net_device *dev;
2440e5a55a89SJohn Fastabend 	int idx = 0;
2441e5a55a89SJohn Fastabend 	u32 portid = NETLINK_CB(cb->skb).portid;
2442e5a55a89SJohn Fastabend 	u32 seq = cb->nlh->nlmsg_seq;
24436cbdceebSVlad Yasevich 	struct nlattr *extfilt;
24446cbdceebSVlad Yasevich 	u32 filter_mask = 0;
24456cbdceebSVlad Yasevich 
24463e805ad2SAsbjoern Sloth Toennesen 	extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
24476cbdceebSVlad Yasevich 				  IFLA_EXT_MASK);
24486cbdceebSVlad Yasevich 	if (extfilt)
24496cbdceebSVlad Yasevich 		filter_mask = nla_get_u32(extfilt);
2450e5a55a89SJohn Fastabend 
2451e5a55a89SJohn Fastabend 	rcu_read_lock();
2452e5a55a89SJohn Fastabend 	for_each_netdev_rcu(net, dev) {
2453e5a55a89SJohn Fastabend 		const struct net_device_ops *ops = dev->netdev_ops;
2454898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2455e5a55a89SJohn Fastabend 
2456898e5061SJiri Pirko 		if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
245725b1e679SBen Hutchings 			if (idx >= cb->args[0] &&
2458898e5061SJiri Pirko 			    br_dev->netdev_ops->ndo_bridge_getlink(
24596cbdceebSVlad Yasevich 				    skb, portid, seq, dev, filter_mask) < 0)
2460e5a55a89SJohn Fastabend 				break;
2461e5a55a89SJohn Fastabend 			idx++;
2462e5a55a89SJohn Fastabend 		}
2463e5a55a89SJohn Fastabend 
2464e5a55a89SJohn Fastabend 		if (ops->ndo_bridge_getlink) {
246525b1e679SBen Hutchings 			if (idx >= cb->args[0] &&
24666cbdceebSVlad Yasevich 			    ops->ndo_bridge_getlink(skb, portid, seq, dev,
24676cbdceebSVlad Yasevich 						    filter_mask) < 0)
2468e5a55a89SJohn Fastabend 				break;
2469e5a55a89SJohn Fastabend 			idx++;
2470e5a55a89SJohn Fastabend 		}
2471e5a55a89SJohn Fastabend 	}
2472e5a55a89SJohn Fastabend 	rcu_read_unlock();
2473e5a55a89SJohn Fastabend 	cb->args[0] = idx;
2474e5a55a89SJohn Fastabend 
2475e5a55a89SJohn Fastabend 	return skb->len;
2476e5a55a89SJohn Fastabend }
2477e5a55a89SJohn Fastabend 
24782469ffd7SJohn Fastabend static inline size_t bridge_nlmsg_size(void)
24792469ffd7SJohn Fastabend {
24802469ffd7SJohn Fastabend 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
24812469ffd7SJohn Fastabend 		+ nla_total_size(IFNAMSIZ)	/* IFLA_IFNAME */
24822469ffd7SJohn Fastabend 		+ nla_total_size(MAX_ADDR_LEN)	/* IFLA_ADDRESS */
24832469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_MASTER */
24842469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_MTU */
24852469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_LINK */
24862469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_OPERSTATE */
24872469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u8))	/* IFLA_PROTINFO */
24882469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(struct nlattr))	/* IFLA_AF_SPEC */
24892469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u16))	/* IFLA_BRIDGE_FLAGS */
24902469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u16));	/* IFLA_BRIDGE_MODE */
24912469ffd7SJohn Fastabend }
24922469ffd7SJohn Fastabend 
24932469ffd7SJohn Fastabend static int rtnl_bridge_notify(struct net_device *dev, u16 flags)
24942469ffd7SJohn Fastabend {
24952469ffd7SJohn Fastabend 	struct net *net = dev_net(dev);
2496898e5061SJiri Pirko 	struct net_device *br_dev = netdev_master_upper_dev_get(dev);
24972469ffd7SJohn Fastabend 	struct sk_buff *skb;
24982469ffd7SJohn Fastabend 	int err = -EOPNOTSUPP;
24992469ffd7SJohn Fastabend 
25002469ffd7SJohn Fastabend 	skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
25012469ffd7SJohn Fastabend 	if (!skb) {
25022469ffd7SJohn Fastabend 		err = -ENOMEM;
25032469ffd7SJohn Fastabend 		goto errout;
25042469ffd7SJohn Fastabend 	}
25052469ffd7SJohn Fastabend 
2506c38e01b8SJohn Fastabend 	if ((!flags || (flags & BRIDGE_FLAGS_MASTER)) &&
2507898e5061SJiri Pirko 	    br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
25086cbdceebSVlad Yasevich 		err = br_dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0);
25092469ffd7SJohn Fastabend 		if (err < 0)
25102469ffd7SJohn Fastabend 			goto errout;
2511c38e01b8SJohn Fastabend 	}
2512c38e01b8SJohn Fastabend 
2513c38e01b8SJohn Fastabend 	if ((flags & BRIDGE_FLAGS_SELF) &&
2514c38e01b8SJohn Fastabend 	    dev->netdev_ops->ndo_bridge_getlink) {
25156cbdceebSVlad Yasevich 		err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0);
2516c38e01b8SJohn Fastabend 		if (err < 0)
2517c38e01b8SJohn Fastabend 			goto errout;
2518c38e01b8SJohn Fastabend 	}
25192469ffd7SJohn Fastabend 
25202469ffd7SJohn Fastabend 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
25212469ffd7SJohn Fastabend 	return 0;
25222469ffd7SJohn Fastabend errout:
25232469ffd7SJohn Fastabend 	WARN_ON(err == -EMSGSIZE);
25242469ffd7SJohn Fastabend 	kfree_skb(skb);
25252469ffd7SJohn Fastabend 	rtnl_set_sk_err(net, RTNLGRP_LINK, err);
25262469ffd7SJohn Fastabend 	return err;
25272469ffd7SJohn Fastabend }
25282469ffd7SJohn Fastabend 
2529661d2967SThomas Graf static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
2530e5a55a89SJohn Fastabend {
2531e5a55a89SJohn Fastabend 	struct net *net = sock_net(skb->sk);
2532e5a55a89SJohn Fastabend 	struct ifinfomsg *ifm;
2533e5a55a89SJohn Fastabend 	struct net_device *dev;
25342469ffd7SJohn Fastabend 	struct nlattr *br_spec, *attr = NULL;
25352469ffd7SJohn Fastabend 	int rem, err = -EOPNOTSUPP;
2536c38e01b8SJohn Fastabend 	u16 oflags, flags = 0;
2537c38e01b8SJohn Fastabend 	bool have_flags = false;
2538e5a55a89SJohn Fastabend 
2539e5a55a89SJohn Fastabend 	if (nlmsg_len(nlh) < sizeof(*ifm))
2540e5a55a89SJohn Fastabend 		return -EINVAL;
2541e5a55a89SJohn Fastabend 
2542e5a55a89SJohn Fastabend 	ifm = nlmsg_data(nlh);
2543e5a55a89SJohn Fastabend 	if (ifm->ifi_family != AF_BRIDGE)
2544e5a55a89SJohn Fastabend 		return -EPFNOSUPPORT;
2545e5a55a89SJohn Fastabend 
2546e5a55a89SJohn Fastabend 	dev = __dev_get_by_index(net, ifm->ifi_index);
2547e5a55a89SJohn Fastabend 	if (!dev) {
2548e5a55a89SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
2549e5a55a89SJohn Fastabend 		return -ENODEV;
2550e5a55a89SJohn Fastabend 	}
2551e5a55a89SJohn Fastabend 
25522469ffd7SJohn Fastabend 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
25532469ffd7SJohn Fastabend 	if (br_spec) {
25542469ffd7SJohn Fastabend 		nla_for_each_nested(attr, br_spec, rem) {
25552469ffd7SJohn Fastabend 			if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
2556c38e01b8SJohn Fastabend 				have_flags = true;
25572469ffd7SJohn Fastabend 				flags = nla_get_u16(attr);
25582469ffd7SJohn Fastabend 				break;
25592469ffd7SJohn Fastabend 			}
25602469ffd7SJohn Fastabend 		}
25612469ffd7SJohn Fastabend 	}
25622469ffd7SJohn Fastabend 
2563c38e01b8SJohn Fastabend 	oflags = flags;
2564c38e01b8SJohn Fastabend 
25652469ffd7SJohn Fastabend 	if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
2566898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2567898e5061SJiri Pirko 
2568898e5061SJiri Pirko 		if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
25692469ffd7SJohn Fastabend 			err = -EOPNOTSUPP;
2570e5a55a89SJohn Fastabend 			goto out;
2571e5a55a89SJohn Fastabend 		}
2572e5a55a89SJohn Fastabend 
2573898e5061SJiri Pirko 		err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh);
25742469ffd7SJohn Fastabend 		if (err)
25752469ffd7SJohn Fastabend 			goto out;
25762469ffd7SJohn Fastabend 
25772469ffd7SJohn Fastabend 		flags &= ~BRIDGE_FLAGS_MASTER;
25782469ffd7SJohn Fastabend 	}
25792469ffd7SJohn Fastabend 
25802469ffd7SJohn Fastabend 	if ((flags & BRIDGE_FLAGS_SELF)) {
25812469ffd7SJohn Fastabend 		if (!dev->netdev_ops->ndo_bridge_setlink)
25822469ffd7SJohn Fastabend 			err = -EOPNOTSUPP;
25832469ffd7SJohn Fastabend 		else
2584e5a55a89SJohn Fastabend 			err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh);
2585e5a55a89SJohn Fastabend 
25862469ffd7SJohn Fastabend 		if (!err)
25872469ffd7SJohn Fastabend 			flags &= ~BRIDGE_FLAGS_SELF;
25882469ffd7SJohn Fastabend 	}
25892469ffd7SJohn Fastabend 
2590c38e01b8SJohn Fastabend 	if (have_flags)
25912469ffd7SJohn Fastabend 		memcpy(nla_data(attr), &flags, sizeof(flags));
25922469ffd7SJohn Fastabend 	/* Generate event to notify upper layer of bridge change */
25932469ffd7SJohn Fastabend 	if (!err)
2594c38e01b8SJohn Fastabend 		err = rtnl_bridge_notify(dev, oflags);
2595e5a55a89SJohn Fastabend out:
2596e5a55a89SJohn Fastabend 	return err;
2597e5a55a89SJohn Fastabend }
2598e5a55a89SJohn Fastabend 
2599661d2967SThomas Graf static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
2600407af329SVlad Yasevich {
2601407af329SVlad Yasevich 	struct net *net = sock_net(skb->sk);
2602407af329SVlad Yasevich 	struct ifinfomsg *ifm;
2603407af329SVlad Yasevich 	struct net_device *dev;
2604407af329SVlad Yasevich 	struct nlattr *br_spec, *attr = NULL;
2605407af329SVlad Yasevich 	int rem, err = -EOPNOTSUPP;
2606407af329SVlad Yasevich 	u16 oflags, flags = 0;
2607407af329SVlad Yasevich 	bool have_flags = false;
2608407af329SVlad Yasevich 
2609407af329SVlad Yasevich 	if (nlmsg_len(nlh) < sizeof(*ifm))
2610407af329SVlad Yasevich 		return -EINVAL;
2611407af329SVlad Yasevich 
2612407af329SVlad Yasevich 	ifm = nlmsg_data(nlh);
2613407af329SVlad Yasevich 	if (ifm->ifi_family != AF_BRIDGE)
2614407af329SVlad Yasevich 		return -EPFNOSUPPORT;
2615407af329SVlad Yasevich 
2616407af329SVlad Yasevich 	dev = __dev_get_by_index(net, ifm->ifi_index);
2617407af329SVlad Yasevich 	if (!dev) {
2618407af329SVlad Yasevich 		pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
2619407af329SVlad Yasevich 		return -ENODEV;
2620407af329SVlad Yasevich 	}
2621407af329SVlad Yasevich 
2622407af329SVlad Yasevich 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
2623407af329SVlad Yasevich 	if (br_spec) {
2624407af329SVlad Yasevich 		nla_for_each_nested(attr, br_spec, rem) {
2625407af329SVlad Yasevich 			if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
2626407af329SVlad Yasevich 				have_flags = true;
2627407af329SVlad Yasevich 				flags = nla_get_u16(attr);
2628407af329SVlad Yasevich 				break;
2629407af329SVlad Yasevich 			}
2630407af329SVlad Yasevich 		}
2631407af329SVlad Yasevich 	}
2632407af329SVlad Yasevich 
2633407af329SVlad Yasevich 	oflags = flags;
2634407af329SVlad Yasevich 
2635407af329SVlad Yasevich 	if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
2636407af329SVlad Yasevich 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2637407af329SVlad Yasevich 
2638407af329SVlad Yasevich 		if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
2639407af329SVlad Yasevich 			err = -EOPNOTSUPP;
2640407af329SVlad Yasevich 			goto out;
2641407af329SVlad Yasevich 		}
2642407af329SVlad Yasevich 
2643407af329SVlad Yasevich 		err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh);
2644407af329SVlad Yasevich 		if (err)
2645407af329SVlad Yasevich 			goto out;
2646407af329SVlad Yasevich 
2647407af329SVlad Yasevich 		flags &= ~BRIDGE_FLAGS_MASTER;
2648407af329SVlad Yasevich 	}
2649407af329SVlad Yasevich 
2650407af329SVlad Yasevich 	if ((flags & BRIDGE_FLAGS_SELF)) {
2651407af329SVlad Yasevich 		if (!dev->netdev_ops->ndo_bridge_dellink)
2652407af329SVlad Yasevich 			err = -EOPNOTSUPP;
2653407af329SVlad Yasevich 		else
2654407af329SVlad Yasevich 			err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh);
2655407af329SVlad Yasevich 
2656407af329SVlad Yasevich 		if (!err)
2657407af329SVlad Yasevich 			flags &= ~BRIDGE_FLAGS_SELF;
2658407af329SVlad Yasevich 	}
2659407af329SVlad Yasevich 
2660407af329SVlad Yasevich 	if (have_flags)
2661407af329SVlad Yasevich 		memcpy(nla_data(attr), &flags, sizeof(flags));
2662407af329SVlad Yasevich 	/* Generate event to notify upper layer of bridge change */
2663407af329SVlad Yasevich 	if (!err)
2664407af329SVlad Yasevich 		err = rtnl_bridge_notify(dev, oflags);
2665407af329SVlad Yasevich out:
2666407af329SVlad Yasevich 	return err;
2667407af329SVlad Yasevich }
2668407af329SVlad Yasevich 
26691da177e4SLinus Torvalds /* Process one rtnetlink message. */
26701da177e4SLinus Torvalds 
26711d00a4ebSThomas Graf static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
26721da177e4SLinus Torvalds {
26733b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
2674e2849863SThomas Graf 	rtnl_doit_func doit;
26751da177e4SLinus Torvalds 	int sz_idx, kind;
26761da177e4SLinus Torvalds 	int family;
26771da177e4SLinus Torvalds 	int type;
26782907c35fSEric Dumazet 	int err;
26791da177e4SLinus Torvalds 
26801da177e4SLinus Torvalds 	type = nlh->nlmsg_type;
26811da177e4SLinus Torvalds 	if (type > RTM_MAX)
2682038890feSThomas Graf 		return -EOPNOTSUPP;
26831da177e4SLinus Torvalds 
26841da177e4SLinus Torvalds 	type -= RTM_BASE;
26851da177e4SLinus Torvalds 
26861da177e4SLinus Torvalds 	/* All the messages must have at least 1 byte length */
2687573ce260SHong zhi guo 	if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
26881da177e4SLinus Torvalds 		return 0;
26891da177e4SLinus Torvalds 
2690573ce260SHong zhi guo 	family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
26911da177e4SLinus Torvalds 	sz_idx = type>>2;
26921da177e4SLinus Torvalds 	kind = type&3;
26931da177e4SLinus Torvalds 
2694dfc47ef8SEric W. Biederman 	if (kind != 2 && !ns_capable(net->user_ns, CAP_NET_ADMIN))
26951d00a4ebSThomas Graf 		return -EPERM;
26961da177e4SLinus Torvalds 
2697b8f3ab42SDavid S. Miller 	if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
269897c53cacSDenis V. Lunev 		struct sock *rtnl;
2699e2849863SThomas Graf 		rtnl_dumpit_func dumpit;
2700c7ac8679SGreg Rose 		rtnl_calcit_func calcit;
2701c7ac8679SGreg Rose 		u16 min_dump_alloc = 0;
27021da177e4SLinus Torvalds 
2703e2849863SThomas Graf 		dumpit = rtnl_get_dumpit(family, type);
2704e2849863SThomas Graf 		if (dumpit == NULL)
2705038890feSThomas Graf 			return -EOPNOTSUPP;
2706c7ac8679SGreg Rose 		calcit = rtnl_get_calcit(family, type);
2707c7ac8679SGreg Rose 		if (calcit)
2708115c9b81SGreg Rose 			min_dump_alloc = calcit(skb, nlh);
27091da177e4SLinus Torvalds 
27102907c35fSEric Dumazet 		__rtnl_unlock();
271197c53cacSDenis V. Lunev 		rtnl = net->rtnl;
271280d326faSPablo Neira Ayuso 		{
271380d326faSPablo Neira Ayuso 			struct netlink_dump_control c = {
271480d326faSPablo Neira Ayuso 				.dump		= dumpit,
271580d326faSPablo Neira Ayuso 				.min_dump_alloc	= min_dump_alloc,
271680d326faSPablo Neira Ayuso 			};
271780d326faSPablo Neira Ayuso 			err = netlink_dump_start(rtnl, skb, nlh, &c);
271880d326faSPablo Neira Ayuso 		}
27192907c35fSEric Dumazet 		rtnl_lock();
27202907c35fSEric Dumazet 		return err;
27211da177e4SLinus Torvalds 	}
27221da177e4SLinus Torvalds 
2723e2849863SThomas Graf 	doit = rtnl_get_doit(family, type);
2724e2849863SThomas Graf 	if (doit == NULL)
2725038890feSThomas Graf 		return -EOPNOTSUPP;
27261da177e4SLinus Torvalds 
2727661d2967SThomas Graf 	return doit(skb, nlh);
27281da177e4SLinus Torvalds }
27291da177e4SLinus Torvalds 
2730cd40b7d3SDenis V. Lunev static void rtnetlink_rcv(struct sk_buff *skb)
27311da177e4SLinus Torvalds {
27321536cc0dSDenis V. Lunev 	rtnl_lock();
2733cd40b7d3SDenis V. Lunev 	netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
27341536cc0dSDenis V. Lunev 	rtnl_unlock();
27351da177e4SLinus Torvalds }
27361da177e4SLinus Torvalds 
27371da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
27381da177e4SLinus Torvalds {
2739351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2740e9dc8653SEric W. Biederman 
27411da177e4SLinus Torvalds 	switch (event) {
27421da177e4SLinus Torvalds 	case NETDEV_UP:
27431da177e4SLinus Torvalds 	case NETDEV_DOWN:
274410de05afSPatrick McHardy 	case NETDEV_PRE_UP:
2745d90a909eSEric W. Biederman 	case NETDEV_POST_INIT:
2746d90a909eSEric W. Biederman 	case NETDEV_REGISTER:
27471da177e4SLinus Torvalds 	case NETDEV_CHANGE:
2748755d0e77SPatrick McHardy 	case NETDEV_PRE_TYPE_CHANGE:
27491da177e4SLinus Torvalds 	case NETDEV_GOING_DOWN:
2750a2835763SPatrick McHardy 	case NETDEV_UNREGISTER:
27510115e8e3SEric Dumazet 	case NETDEV_UNREGISTER_FINAL:
2752ac3d3f81SAmerigo Wang 	case NETDEV_RELEASE:
2753ac3d3f81SAmerigo Wang 	case NETDEV_JOIN:
27541da177e4SLinus Torvalds 		break;
27551da177e4SLinus Torvalds 	default:
27567f294054SAlexei Starovoitov 		rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
27571da177e4SLinus Torvalds 		break;
27581da177e4SLinus Torvalds 	}
27591da177e4SLinus Torvalds 	return NOTIFY_DONE;
27601da177e4SLinus Torvalds }
27611da177e4SLinus Torvalds 
27621da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = {
27631da177e4SLinus Torvalds 	.notifier_call	= rtnetlink_event,
27641da177e4SLinus Torvalds };
27651da177e4SLinus Torvalds 
276697c53cacSDenis V. Lunev 
27672c8c1e72SAlexey Dobriyan static int __net_init rtnetlink_net_init(struct net *net)
276897c53cacSDenis V. Lunev {
276997c53cacSDenis V. Lunev 	struct sock *sk;
2770a31f2d17SPablo Neira Ayuso 	struct netlink_kernel_cfg cfg = {
2771a31f2d17SPablo Neira Ayuso 		.groups		= RTNLGRP_MAX,
2772a31f2d17SPablo Neira Ayuso 		.input		= rtnetlink_rcv,
2773a31f2d17SPablo Neira Ayuso 		.cb_mutex	= &rtnl_mutex,
27749785e10aSPablo Neira Ayuso 		.flags		= NL_CFG_F_NONROOT_RECV,
2775a31f2d17SPablo Neira Ayuso 	};
2776a31f2d17SPablo Neira Ayuso 
27779f00d977SPablo Neira Ayuso 	sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
277897c53cacSDenis V. Lunev 	if (!sk)
277997c53cacSDenis V. Lunev 		return -ENOMEM;
278097c53cacSDenis V. Lunev 	net->rtnl = sk;
278197c53cacSDenis V. Lunev 	return 0;
278297c53cacSDenis V. Lunev }
278397c53cacSDenis V. Lunev 
27842c8c1e72SAlexey Dobriyan static void __net_exit rtnetlink_net_exit(struct net *net)
278597c53cacSDenis V. Lunev {
2786b7c6ba6eSDenis V. Lunev 	netlink_kernel_release(net->rtnl);
278797c53cacSDenis V. Lunev 	net->rtnl = NULL;
278897c53cacSDenis V. Lunev }
278997c53cacSDenis V. Lunev 
279097c53cacSDenis V. Lunev static struct pernet_operations rtnetlink_net_ops = {
279197c53cacSDenis V. Lunev 	.init = rtnetlink_net_init,
279297c53cacSDenis V. Lunev 	.exit = rtnetlink_net_exit,
279397c53cacSDenis V. Lunev };
279497c53cacSDenis V. Lunev 
27951da177e4SLinus Torvalds void __init rtnetlink_init(void)
27961da177e4SLinus Torvalds {
279797c53cacSDenis V. Lunev 	if (register_pernet_subsys(&rtnetlink_net_ops))
27981da177e4SLinus Torvalds 		panic("rtnetlink_init: cannot initialize rtnetlink\n");
279997c53cacSDenis V. Lunev 
28001da177e4SLinus Torvalds 	register_netdevice_notifier(&rtnetlink_dev_notifier);
2801340d17fcSThomas Graf 
2802c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
2803c7ac8679SGreg Rose 		      rtnl_dump_ifinfo, rtnl_calcit);
2804c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL);
2805c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL);
2806c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL);
2807687ad8ccSThomas Graf 
2808c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL);
2809c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL);
281077162022SJohn Fastabend 
281177162022SJohn Fastabend 	rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, NULL);
281277162022SJohn Fastabend 	rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, NULL);
281377162022SJohn Fastabend 	rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, NULL);
2814e5a55a89SJohn Fastabend 
2815e5a55a89SJohn Fastabend 	rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, NULL);
2816407af329SVlad Yasevich 	rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, NULL);
2817e5a55a89SJohn Fastabend 	rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, NULL);
28181da177e4SLinus Torvalds }
28191da177e4SLinus Torvalds 
2820