xref: /openbmc/linux/net/core/rtnetlink.c (revision 973462bbde79bb827824c73b59027a0aed5c9ca6)
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 
368ba7d49b1SJiri Pirko static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
369ba7d49b1SJiri Pirko {
370ba7d49b1SJiri Pirko 	struct net_device *master_dev;
371ba7d49b1SJiri Pirko 	const struct rtnl_link_ops *ops;
372ba7d49b1SJiri Pirko 
373ba7d49b1SJiri Pirko 	master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
374ba7d49b1SJiri Pirko 	if (!master_dev)
375ba7d49b1SJiri Pirko 		return 0;
376ba7d49b1SJiri Pirko 	ops = master_dev->rtnl_link_ops;
3776049f253SFernando Luis Vazquez Cao 	if (!ops || !ops->get_slave_size)
378ba7d49b1SJiri Pirko 		return 0;
379ba7d49b1SJiri Pirko 	/* IFLA_INFO_SLAVE_DATA + nested data */
380ba7d49b1SJiri Pirko 	return nla_total_size(sizeof(struct nlattr)) +
381ba7d49b1SJiri Pirko 	       ops->get_slave_size(master_dev, dev);
382ba7d49b1SJiri Pirko }
383ba7d49b1SJiri Pirko 
38438f7b870SPatrick McHardy static size_t rtnl_link_get_size(const struct net_device *dev)
38538f7b870SPatrick McHardy {
38638f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
38738f7b870SPatrick McHardy 	size_t size;
38838f7b870SPatrick McHardy 
38938f7b870SPatrick McHardy 	if (!ops)
39038f7b870SPatrick McHardy 		return 0;
39138f7b870SPatrick McHardy 
392369cf77aSThomas Graf 	size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
393369cf77aSThomas Graf 	       nla_total_size(strlen(ops->kind) + 1);  /* IFLA_INFO_KIND */
39438f7b870SPatrick McHardy 
39538f7b870SPatrick McHardy 	if (ops->get_size)
39638f7b870SPatrick McHardy 		/* IFLA_INFO_DATA + nested data */
397369cf77aSThomas Graf 		size += nla_total_size(sizeof(struct nlattr)) +
39838f7b870SPatrick McHardy 			ops->get_size(dev);
39938f7b870SPatrick McHardy 
40038f7b870SPatrick McHardy 	if (ops->get_xstats_size)
401369cf77aSThomas Graf 		/* IFLA_INFO_XSTATS */
402369cf77aSThomas Graf 		size += nla_total_size(ops->get_xstats_size(dev));
40338f7b870SPatrick McHardy 
404ba7d49b1SJiri Pirko 	size += rtnl_link_get_slave_info_data_size(dev);
405ba7d49b1SJiri Pirko 
40638f7b870SPatrick McHardy 	return size;
40738f7b870SPatrick McHardy }
40838f7b870SPatrick McHardy 
409f8ff182cSThomas Graf static LIST_HEAD(rtnl_af_ops);
410f8ff182cSThomas Graf 
411f8ff182cSThomas Graf static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
412f8ff182cSThomas Graf {
413f8ff182cSThomas Graf 	const struct rtnl_af_ops *ops;
414f8ff182cSThomas Graf 
415f8ff182cSThomas Graf 	list_for_each_entry(ops, &rtnl_af_ops, list) {
416f8ff182cSThomas Graf 		if (ops->family == family)
417f8ff182cSThomas Graf 			return ops;
418f8ff182cSThomas Graf 	}
419f8ff182cSThomas Graf 
420f8ff182cSThomas Graf 	return NULL;
421f8ff182cSThomas Graf }
422f8ff182cSThomas Graf 
423f8ff182cSThomas Graf /**
424f8ff182cSThomas Graf  * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
425f8ff182cSThomas Graf  * @ops: struct rtnl_af_ops * to register
426f8ff182cSThomas Graf  *
427f8ff182cSThomas Graf  * Returns 0 on success or a negative error code.
428f8ff182cSThomas Graf  */
4293678a9d8Sstephen hemminger void rtnl_af_register(struct rtnl_af_ops *ops)
430f8ff182cSThomas Graf {
431f8ff182cSThomas Graf 	rtnl_lock();
4323678a9d8Sstephen hemminger 	list_add_tail(&ops->list, &rtnl_af_ops);
433f8ff182cSThomas Graf 	rtnl_unlock();
434f8ff182cSThomas Graf }
435f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_register);
436f8ff182cSThomas Graf 
437f8ff182cSThomas Graf /**
438f8ff182cSThomas Graf  * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
439f8ff182cSThomas Graf  * @ops: struct rtnl_af_ops * to unregister
440f8ff182cSThomas Graf  *
441f8ff182cSThomas Graf  * The caller must hold the rtnl_mutex.
442f8ff182cSThomas Graf  */
443f8ff182cSThomas Graf void __rtnl_af_unregister(struct rtnl_af_ops *ops)
444f8ff182cSThomas Graf {
445f8ff182cSThomas Graf 	list_del(&ops->list);
446f8ff182cSThomas Graf }
447f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(__rtnl_af_unregister);
448f8ff182cSThomas Graf 
449f8ff182cSThomas Graf /**
450f8ff182cSThomas Graf  * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
451f8ff182cSThomas Graf  * @ops: struct rtnl_af_ops * to unregister
452f8ff182cSThomas Graf  */
453f8ff182cSThomas Graf void rtnl_af_unregister(struct rtnl_af_ops *ops)
454f8ff182cSThomas Graf {
455f8ff182cSThomas Graf 	rtnl_lock();
456f8ff182cSThomas Graf 	__rtnl_af_unregister(ops);
457f8ff182cSThomas Graf 	rtnl_unlock();
458f8ff182cSThomas Graf }
459f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_unregister);
460f8ff182cSThomas Graf 
461f8ff182cSThomas Graf static size_t rtnl_link_get_af_size(const struct net_device *dev)
462f8ff182cSThomas Graf {
463f8ff182cSThomas Graf 	struct rtnl_af_ops *af_ops;
464f8ff182cSThomas Graf 	size_t size;
465f8ff182cSThomas Graf 
466f8ff182cSThomas Graf 	/* IFLA_AF_SPEC */
467f8ff182cSThomas Graf 	size = nla_total_size(sizeof(struct nlattr));
468f8ff182cSThomas Graf 
469f8ff182cSThomas Graf 	list_for_each_entry(af_ops, &rtnl_af_ops, list) {
470f8ff182cSThomas Graf 		if (af_ops->get_link_af_size) {
471f8ff182cSThomas Graf 			/* AF_* + nested data */
472f8ff182cSThomas Graf 			size += nla_total_size(sizeof(struct nlattr)) +
473f8ff182cSThomas Graf 				af_ops->get_link_af_size(dev);
474f8ff182cSThomas Graf 		}
475f8ff182cSThomas Graf 	}
476f8ff182cSThomas Graf 
477f8ff182cSThomas Graf 	return size;
478f8ff182cSThomas Graf }
479f8ff182cSThomas Graf 
480ba7d49b1SJiri Pirko static bool rtnl_have_link_slave_info(const struct net_device *dev)
481ba7d49b1SJiri Pirko {
482ba7d49b1SJiri Pirko 	struct net_device *master_dev;
483ba7d49b1SJiri Pirko 
484ba7d49b1SJiri Pirko 	master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
485813f020cSJiri Pirko 	if (master_dev && master_dev->rtnl_link_ops)
486ba7d49b1SJiri Pirko 		return true;
487ba7d49b1SJiri Pirko 	return false;
488ba7d49b1SJiri Pirko }
489ba7d49b1SJiri Pirko 
490ba7d49b1SJiri Pirko static int rtnl_link_slave_info_fill(struct sk_buff *skb,
491ba7d49b1SJiri Pirko 				     const struct net_device *dev)
492ba7d49b1SJiri Pirko {
493ba7d49b1SJiri Pirko 	struct net_device *master_dev;
494ba7d49b1SJiri Pirko 	const struct rtnl_link_ops *ops;
495ba7d49b1SJiri Pirko 	struct nlattr *slave_data;
496ba7d49b1SJiri Pirko 	int err;
497ba7d49b1SJiri Pirko 
498ba7d49b1SJiri Pirko 	master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
499ba7d49b1SJiri Pirko 	if (!master_dev)
500ba7d49b1SJiri Pirko 		return 0;
501ba7d49b1SJiri Pirko 	ops = master_dev->rtnl_link_ops;
502ba7d49b1SJiri Pirko 	if (!ops)
503ba7d49b1SJiri Pirko 		return 0;
504ba7d49b1SJiri Pirko 	if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
505ba7d49b1SJiri Pirko 		return -EMSGSIZE;
506ba7d49b1SJiri Pirko 	if (ops->fill_slave_info) {
507ba7d49b1SJiri Pirko 		slave_data = nla_nest_start(skb, IFLA_INFO_SLAVE_DATA);
508ba7d49b1SJiri Pirko 		if (!slave_data)
509ba7d49b1SJiri Pirko 			return -EMSGSIZE;
510ba7d49b1SJiri Pirko 		err = ops->fill_slave_info(skb, master_dev, dev);
511ba7d49b1SJiri Pirko 		if (err < 0)
512ba7d49b1SJiri Pirko 			goto err_cancel_slave_data;
513ba7d49b1SJiri Pirko 		nla_nest_end(skb, slave_data);
514ba7d49b1SJiri Pirko 	}
515ba7d49b1SJiri Pirko 	return 0;
516ba7d49b1SJiri Pirko 
517ba7d49b1SJiri Pirko err_cancel_slave_data:
518ba7d49b1SJiri Pirko 	nla_nest_cancel(skb, slave_data);
519ba7d49b1SJiri Pirko 	return err;
520ba7d49b1SJiri Pirko }
521ba7d49b1SJiri Pirko 
522ba7d49b1SJiri Pirko static int rtnl_link_info_fill(struct sk_buff *skb,
523ba7d49b1SJiri Pirko 			       const struct net_device *dev)
52438f7b870SPatrick McHardy {
52538f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
526ba7d49b1SJiri Pirko 	struct nlattr *data;
527ba7d49b1SJiri Pirko 	int err;
528ba7d49b1SJiri Pirko 
529ba7d49b1SJiri Pirko 	if (!ops)
530ba7d49b1SJiri Pirko 		return 0;
531ba7d49b1SJiri Pirko 	if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
532ba7d49b1SJiri Pirko 		return -EMSGSIZE;
533ba7d49b1SJiri Pirko 	if (ops->fill_xstats) {
534ba7d49b1SJiri Pirko 		err = ops->fill_xstats(skb, dev);
535ba7d49b1SJiri Pirko 		if (err < 0)
536ba7d49b1SJiri Pirko 			return err;
537ba7d49b1SJiri Pirko 	}
538ba7d49b1SJiri Pirko 	if (ops->fill_info) {
539ba7d49b1SJiri Pirko 		data = nla_nest_start(skb, IFLA_INFO_DATA);
540ba7d49b1SJiri Pirko 		if (data == NULL)
541ba7d49b1SJiri Pirko 			return -EMSGSIZE;
542ba7d49b1SJiri Pirko 		err = ops->fill_info(skb, dev);
543ba7d49b1SJiri Pirko 		if (err < 0)
544ba7d49b1SJiri Pirko 			goto err_cancel_data;
545ba7d49b1SJiri Pirko 		nla_nest_end(skb, data);
546ba7d49b1SJiri Pirko 	}
547ba7d49b1SJiri Pirko 	return 0;
548ba7d49b1SJiri Pirko 
549ba7d49b1SJiri Pirko err_cancel_data:
550ba7d49b1SJiri Pirko 	nla_nest_cancel(skb, data);
551ba7d49b1SJiri Pirko 	return err;
552ba7d49b1SJiri Pirko }
553ba7d49b1SJiri Pirko 
554ba7d49b1SJiri Pirko static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
555ba7d49b1SJiri Pirko {
556ba7d49b1SJiri Pirko 	struct nlattr *linkinfo;
55738f7b870SPatrick McHardy 	int err = -EMSGSIZE;
55838f7b870SPatrick McHardy 
55938f7b870SPatrick McHardy 	linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
56038f7b870SPatrick McHardy 	if (linkinfo == NULL)
56138f7b870SPatrick McHardy 		goto out;
56238f7b870SPatrick McHardy 
563ba7d49b1SJiri Pirko 	err = rtnl_link_info_fill(skb, dev);
56438f7b870SPatrick McHardy 	if (err < 0)
56538f7b870SPatrick McHardy 		goto err_cancel_link;
566ba7d49b1SJiri Pirko 
567ba7d49b1SJiri Pirko 	err = rtnl_link_slave_info_fill(skb, dev);
56838f7b870SPatrick McHardy 	if (err < 0)
569ba7d49b1SJiri Pirko 		goto err_cancel_link;
57038f7b870SPatrick McHardy 
57138f7b870SPatrick McHardy 	nla_nest_end(skb, linkinfo);
57238f7b870SPatrick McHardy 	return 0;
57338f7b870SPatrick McHardy 
57438f7b870SPatrick McHardy err_cancel_link:
57538f7b870SPatrick McHardy 	nla_nest_cancel(skb, linkinfo);
57638f7b870SPatrick McHardy out:
57738f7b870SPatrick McHardy 	return err;
57838f7b870SPatrick McHardy }
57938f7b870SPatrick McHardy 
58095c96174SEric Dumazet int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
5811da177e4SLinus Torvalds {
58297c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
5831da177e4SLinus Torvalds 	int err = 0;
5841da177e4SLinus Torvalds 
585ac6d439dSPatrick McHardy 	NETLINK_CB(skb).dst_group = group;
5861da177e4SLinus Torvalds 	if (echo)
5871da177e4SLinus Torvalds 		atomic_inc(&skb->users);
5881da177e4SLinus Torvalds 	netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
5891da177e4SLinus Torvalds 	if (echo)
5901da177e4SLinus Torvalds 		err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
5911da177e4SLinus Torvalds 	return err;
5921da177e4SLinus Torvalds }
5931da177e4SLinus Torvalds 
59497c53cacSDenis V. Lunev int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
5952942e900SThomas Graf {
59697c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
59797c53cacSDenis V. Lunev 
5982942e900SThomas Graf 	return nlmsg_unicast(rtnl, skb, pid);
5992942e900SThomas Graf }
600e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unicast);
6012942e900SThomas Graf 
6021ce85fe4SPablo Neira Ayuso void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
60397676b6bSThomas Graf 		 struct nlmsghdr *nlh, gfp_t flags)
60497676b6bSThomas Graf {
60597c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
60697676b6bSThomas Graf 	int report = 0;
60797676b6bSThomas Graf 
60897676b6bSThomas Graf 	if (nlh)
60997676b6bSThomas Graf 		report = nlmsg_report(nlh);
61097676b6bSThomas Graf 
6111ce85fe4SPablo Neira Ayuso 	nlmsg_notify(rtnl, skb, pid, group, report, flags);
61297676b6bSThomas Graf }
613e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_notify);
61497676b6bSThomas Graf 
61597c53cacSDenis V. Lunev void rtnl_set_sk_err(struct net *net, u32 group, int error)
61697676b6bSThomas Graf {
61797c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
61897c53cacSDenis V. Lunev 
61997676b6bSThomas Graf 	netlink_set_err(rtnl, 0, group, error);
62097676b6bSThomas Graf }
621e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_set_sk_err);
62297676b6bSThomas Graf 
6231da177e4SLinus Torvalds int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
6241da177e4SLinus Torvalds {
6252d7202bfSThomas Graf 	struct nlattr *mx;
6262d7202bfSThomas Graf 	int i, valid = 0;
6271da177e4SLinus Torvalds 
6282d7202bfSThomas Graf 	mx = nla_nest_start(skb, RTA_METRICS);
6292d7202bfSThomas Graf 	if (mx == NULL)
6302d7202bfSThomas Graf 		return -ENOBUFS;
6312d7202bfSThomas Graf 
6321da177e4SLinus Torvalds 	for (i = 0; i < RTAX_MAX; i++) {
6332d7202bfSThomas Graf 		if (metrics[i]) {
6342d7202bfSThomas Graf 			valid++;
635a6574349SDavid S. Miller 			if (nla_put_u32(skb, i+1, metrics[i]))
636a6574349SDavid S. Miller 				goto nla_put_failure;
6371da177e4SLinus Torvalds 		}
6382d7202bfSThomas Graf 	}
6391da177e4SLinus Torvalds 
640a57d27fcSDavid S. Miller 	if (!valid) {
641a57d27fcSDavid S. Miller 		nla_nest_cancel(skb, mx);
642a57d27fcSDavid S. Miller 		return 0;
643a57d27fcSDavid S. Miller 	}
6442d7202bfSThomas Graf 
6452d7202bfSThomas Graf 	return nla_nest_end(skb, mx);
6462d7202bfSThomas Graf 
6472d7202bfSThomas Graf nla_put_failure:
648bc3ed28cSThomas Graf 	nla_nest_cancel(skb, mx);
649bc3ed28cSThomas Graf 	return -EMSGSIZE;
6501da177e4SLinus Torvalds }
651e0d087afSEric Dumazet EXPORT_SYMBOL(rtnetlink_put_metrics);
6521da177e4SLinus Torvalds 
653e3703b3dSThomas Graf int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
65487a50699SDavid S. Miller 		       long expires, u32 error)
655e3703b3dSThomas Graf {
656e3703b3dSThomas Graf 	struct rta_cacheinfo ci = {
657a399a805SEric Dumazet 		.rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse),
658e3703b3dSThomas Graf 		.rta_used = dst->__use,
659e3703b3dSThomas Graf 		.rta_clntref = atomic_read(&(dst->__refcnt)),
660e3703b3dSThomas Graf 		.rta_error = error,
661e3703b3dSThomas Graf 		.rta_id =  id,
662e3703b3dSThomas Graf 	};
663e3703b3dSThomas Graf 
6648253947eSLi Wei 	if (expires) {
6658253947eSLi Wei 		unsigned long clock;
666e3703b3dSThomas Graf 
6678253947eSLi Wei 		clock = jiffies_to_clock_t(abs(expires));
6688253947eSLi Wei 		clock = min_t(unsigned long, clock, INT_MAX);
6698253947eSLi Wei 		ci.rta_expires = (expires > 0) ? clock : -clock;
6708253947eSLi Wei 	}
671e3703b3dSThomas Graf 	return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
672e3703b3dSThomas Graf }
673e3703b3dSThomas Graf EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
6741da177e4SLinus Torvalds 
67593b2d4a2SDavid S. Miller static void set_operstate(struct net_device *dev, unsigned char transition)
676b00055aaSStefan Rompf {
677b00055aaSStefan Rompf 	unsigned char operstate = dev->operstate;
678b00055aaSStefan Rompf 
679b00055aaSStefan Rompf 	switch (transition) {
680b00055aaSStefan Rompf 	case IF_OPER_UP:
681b00055aaSStefan Rompf 		if ((operstate == IF_OPER_DORMANT ||
682b00055aaSStefan Rompf 		     operstate == IF_OPER_UNKNOWN) &&
683b00055aaSStefan Rompf 		    !netif_dormant(dev))
684b00055aaSStefan Rompf 			operstate = IF_OPER_UP;
685b00055aaSStefan Rompf 		break;
686b00055aaSStefan Rompf 
687b00055aaSStefan Rompf 	case IF_OPER_DORMANT:
688b00055aaSStefan Rompf 		if (operstate == IF_OPER_UP ||
689b00055aaSStefan Rompf 		    operstate == IF_OPER_UNKNOWN)
690b00055aaSStefan Rompf 			operstate = IF_OPER_DORMANT;
691b00055aaSStefan Rompf 		break;
6923ff50b79SStephen Hemminger 	}
693b00055aaSStefan Rompf 
694b00055aaSStefan Rompf 	if (dev->operstate != operstate) {
695b00055aaSStefan Rompf 		write_lock_bh(&dev_base_lock);
696b00055aaSStefan Rompf 		dev->operstate = operstate;
697b00055aaSStefan Rompf 		write_unlock_bh(&dev_base_lock);
698b00055aaSStefan Rompf 		netdev_state_change(dev);
69993b2d4a2SDavid S. Miller 	}
700b00055aaSStefan Rompf }
701b00055aaSStefan Rompf 
702b1beb681SJiri Benc static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
703b1beb681SJiri Benc {
704b1beb681SJiri Benc 	return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
705b1beb681SJiri Benc 	       (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
706b1beb681SJiri Benc }
707b1beb681SJiri Benc 
7083729d502SPatrick McHardy static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
7093729d502SPatrick McHardy 					   const struct ifinfomsg *ifm)
7103729d502SPatrick McHardy {
7113729d502SPatrick McHardy 	unsigned int flags = ifm->ifi_flags;
7123729d502SPatrick McHardy 
7133729d502SPatrick McHardy 	/* bugwards compatibility: ifi_change == 0 is treated as ~0 */
7143729d502SPatrick McHardy 	if (ifm->ifi_change)
7153729d502SPatrick McHardy 		flags = (flags & ifm->ifi_change) |
716b1beb681SJiri Benc 			(rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
7173729d502SPatrick McHardy 
7183729d502SPatrick McHardy 	return flags;
7193729d502SPatrick McHardy }
7203729d502SPatrick McHardy 
721b60c5115SThomas Graf static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
722be1f3c2cSBen Hutchings 				 const struct rtnl_link_stats64 *b)
7231da177e4SLinus Torvalds {
724b60c5115SThomas Graf 	a->rx_packets = b->rx_packets;
725b60c5115SThomas Graf 	a->tx_packets = b->tx_packets;
726b60c5115SThomas Graf 	a->rx_bytes = b->rx_bytes;
727b60c5115SThomas Graf 	a->tx_bytes = b->tx_bytes;
728b60c5115SThomas Graf 	a->rx_errors = b->rx_errors;
729b60c5115SThomas Graf 	a->tx_errors = b->tx_errors;
730b60c5115SThomas Graf 	a->rx_dropped = b->rx_dropped;
731b60c5115SThomas Graf 	a->tx_dropped = b->tx_dropped;
732b60c5115SThomas Graf 
733b60c5115SThomas Graf 	a->multicast = b->multicast;
734b60c5115SThomas Graf 	a->collisions = b->collisions;
735b60c5115SThomas Graf 
736b60c5115SThomas Graf 	a->rx_length_errors = b->rx_length_errors;
737b60c5115SThomas Graf 	a->rx_over_errors = b->rx_over_errors;
738b60c5115SThomas Graf 	a->rx_crc_errors = b->rx_crc_errors;
739b60c5115SThomas Graf 	a->rx_frame_errors = b->rx_frame_errors;
740b60c5115SThomas Graf 	a->rx_fifo_errors = b->rx_fifo_errors;
741b60c5115SThomas Graf 	a->rx_missed_errors = b->rx_missed_errors;
742b60c5115SThomas Graf 
743b60c5115SThomas Graf 	a->tx_aborted_errors = b->tx_aborted_errors;
744b60c5115SThomas Graf 	a->tx_carrier_errors = b->tx_carrier_errors;
745b60c5115SThomas Graf 	a->tx_fifo_errors = b->tx_fifo_errors;
746b60c5115SThomas Graf 	a->tx_heartbeat_errors = b->tx_heartbeat_errors;
747b60c5115SThomas Graf 	a->tx_window_errors = b->tx_window_errors;
748b60c5115SThomas Graf 
749b60c5115SThomas Graf 	a->rx_compressed = b->rx_compressed;
750b60c5115SThomas Graf 	a->tx_compressed = b->tx_compressed;
75110708f37SJan Engelhardt }
75210708f37SJan Engelhardt 
753be1f3c2cSBen Hutchings static void copy_rtnl_link_stats64(void *v, const struct rtnl_link_stats64 *b)
75410708f37SJan Engelhardt {
755afdcba37SEric Dumazet 	memcpy(v, b, sizeof(*b));
75610708f37SJan Engelhardt }
757b60c5115SThomas Graf 
758c02db8c6SChris Wright /* All VF info */
759115c9b81SGreg Rose static inline int rtnl_vfinfo_size(const struct net_device *dev,
760115c9b81SGreg Rose 				   u32 ext_filter_mask)
761ebc08a6fSWilliams, Mitch A {
762115c9b81SGreg Rose 	if (dev->dev.parent && dev_is_pci(dev->dev.parent) &&
763115c9b81SGreg Rose 	    (ext_filter_mask & RTEXT_FILTER_VF)) {
764c02db8c6SChris Wright 		int num_vfs = dev_num_vf(dev->dev.parent);
765045de01aSScott Feldman 		size_t size = nla_total_size(sizeof(struct nlattr));
766045de01aSScott Feldman 		size += nla_total_size(num_vfs * sizeof(struct nlattr));
767045de01aSScott Feldman 		size += num_vfs *
768045de01aSScott Feldman 			(nla_total_size(sizeof(struct ifla_vf_mac)) +
769045de01aSScott Feldman 			 nla_total_size(sizeof(struct ifla_vf_vlan)) +
7705f8444a3SGreg Rose 			 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
7715f8444a3SGreg Rose 			 nla_total_size(sizeof(struct ifla_vf_spoofchk)));
772c02db8c6SChris Wright 		return size;
773c02db8c6SChris Wright 	} else
774ebc08a6fSWilliams, Mitch A 		return 0;
775ebc08a6fSWilliams, Mitch A }
776ebc08a6fSWilliams, Mitch A 
77757b61080SScott Feldman static size_t rtnl_port_size(const struct net_device *dev)
77857b61080SScott Feldman {
77957b61080SScott Feldman 	size_t port_size = nla_total_size(4)		/* PORT_VF */
78057b61080SScott Feldman 		+ nla_total_size(PORT_PROFILE_MAX)	/* PORT_PROFILE */
78157b61080SScott Feldman 		+ nla_total_size(sizeof(struct ifla_port_vsi))
78257b61080SScott Feldman 							/* PORT_VSI_TYPE */
78357b61080SScott Feldman 		+ nla_total_size(PORT_UUID_MAX)		/* PORT_INSTANCE_UUID */
78457b61080SScott Feldman 		+ nla_total_size(PORT_UUID_MAX)		/* PORT_HOST_UUID */
78557b61080SScott Feldman 		+ nla_total_size(1)			/* PROT_VDP_REQUEST */
78657b61080SScott Feldman 		+ nla_total_size(2);			/* PORT_VDP_RESPONSE */
78757b61080SScott Feldman 	size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
78857b61080SScott Feldman 	size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
78957b61080SScott Feldman 		+ port_size;
79057b61080SScott Feldman 	size_t port_self_size = nla_total_size(sizeof(struct nlattr))
79157b61080SScott Feldman 		+ port_size;
79257b61080SScott Feldman 
79357b61080SScott Feldman 	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent)
79457b61080SScott Feldman 		return 0;
79557b61080SScott Feldman 	if (dev_num_vf(dev->dev.parent))
79657b61080SScott Feldman 		return port_self_size + vf_ports_size +
79757b61080SScott Feldman 			vf_port_size * dev_num_vf(dev->dev.parent);
79857b61080SScott Feldman 	else
79957b61080SScott Feldman 		return port_self_size;
80057b61080SScott Feldman }
80157b61080SScott Feldman 
802115c9b81SGreg Rose static noinline size_t if_nlmsg_size(const struct net_device *dev,
803115c9b81SGreg Rose 				     u32 ext_filter_mask)
804339bf98fSThomas Graf {
805339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
806339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
8070b815a1aSStephen Hemminger 	       + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
808339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
809339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rtnl_link_ifmap))
810339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rtnl_link_stats))
811adcfe196SJan Engelhardt 	       + nla_total_size(sizeof(struct rtnl_link_stats64))
812339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
813339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
814339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_TXQLEN */
815339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_WEIGHT */
816339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MTU */
817339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_LINK */
818339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MASTER */
8199a57247fSJiri Pirko 	       + nla_total_size(1) /* IFLA_CARRIER */
820edbc0bb3SBen Greear 	       + nla_total_size(4) /* IFLA_PROMISCUITY */
82176ff5cc9SJiri Pirko 	       + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
82276ff5cc9SJiri Pirko 	       + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
823339bf98fSThomas Graf 	       + nla_total_size(1) /* IFLA_OPERSTATE */
82438f7b870SPatrick McHardy 	       + nla_total_size(1) /* IFLA_LINKMODE */
8252d3b479dSdavid decotigny 	       + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
826115c9b81SGreg Rose 	       + nla_total_size(ext_filter_mask
827115c9b81SGreg Rose 			        & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
828115c9b81SGreg Rose 	       + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
82957b61080SScott Feldman 	       + rtnl_port_size(dev) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
830f8ff182cSThomas Graf 	       + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
83166cae9edSJiri Pirko 	       + rtnl_link_get_af_size(dev) /* IFLA_AF_SPEC */
83266cae9edSJiri Pirko 	       + nla_total_size(MAX_PHYS_PORT_ID_LEN); /* IFLA_PHYS_PORT_ID */
833339bf98fSThomas Graf }
834339bf98fSThomas Graf 
83557b61080SScott Feldman static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
83657b61080SScott Feldman {
83757b61080SScott Feldman 	struct nlattr *vf_ports;
83857b61080SScott Feldman 	struct nlattr *vf_port;
83957b61080SScott Feldman 	int vf;
84057b61080SScott Feldman 	int err;
84157b61080SScott Feldman 
84257b61080SScott Feldman 	vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
84357b61080SScott Feldman 	if (!vf_ports)
84457b61080SScott Feldman 		return -EMSGSIZE;
84557b61080SScott Feldman 
84657b61080SScott Feldman 	for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
84757b61080SScott Feldman 		vf_port = nla_nest_start(skb, IFLA_VF_PORT);
8488ca94183SScott Feldman 		if (!vf_port)
8498ca94183SScott Feldman 			goto nla_put_failure;
850a6574349SDavid S. Miller 		if (nla_put_u32(skb, IFLA_PORT_VF, vf))
851a6574349SDavid S. Miller 			goto nla_put_failure;
85257b61080SScott Feldman 		err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
8538ca94183SScott Feldman 		if (err == -EMSGSIZE)
8548ca94183SScott Feldman 			goto nla_put_failure;
85557b61080SScott Feldman 		if (err) {
85657b61080SScott Feldman 			nla_nest_cancel(skb, vf_port);
85757b61080SScott Feldman 			continue;
85857b61080SScott Feldman 		}
85957b61080SScott Feldman 		nla_nest_end(skb, vf_port);
86057b61080SScott Feldman 	}
86157b61080SScott Feldman 
86257b61080SScott Feldman 	nla_nest_end(skb, vf_ports);
86357b61080SScott Feldman 
86457b61080SScott Feldman 	return 0;
8658ca94183SScott Feldman 
8668ca94183SScott Feldman nla_put_failure:
8678ca94183SScott Feldman 	nla_nest_cancel(skb, vf_ports);
8688ca94183SScott Feldman 	return -EMSGSIZE;
86957b61080SScott Feldman }
87057b61080SScott Feldman 
87157b61080SScott Feldman static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
87257b61080SScott Feldman {
87357b61080SScott Feldman 	struct nlattr *port_self;
87457b61080SScott Feldman 	int err;
87557b61080SScott Feldman 
87657b61080SScott Feldman 	port_self = nla_nest_start(skb, IFLA_PORT_SELF);
87757b61080SScott Feldman 	if (!port_self)
87857b61080SScott Feldman 		return -EMSGSIZE;
87957b61080SScott Feldman 
88057b61080SScott Feldman 	err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
88157b61080SScott Feldman 	if (err) {
88257b61080SScott Feldman 		nla_nest_cancel(skb, port_self);
8838ca94183SScott Feldman 		return (err == -EMSGSIZE) ? err : 0;
88457b61080SScott Feldman 	}
88557b61080SScott Feldman 
88657b61080SScott Feldman 	nla_nest_end(skb, port_self);
88757b61080SScott Feldman 
88857b61080SScott Feldman 	return 0;
88957b61080SScott Feldman }
89057b61080SScott Feldman 
89157b61080SScott Feldman static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev)
89257b61080SScott Feldman {
89357b61080SScott Feldman 	int err;
89457b61080SScott Feldman 
89557b61080SScott Feldman 	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent)
89657b61080SScott Feldman 		return 0;
89757b61080SScott Feldman 
89857b61080SScott Feldman 	err = rtnl_port_self_fill(skb, dev);
89957b61080SScott Feldman 	if (err)
90057b61080SScott Feldman 		return err;
90157b61080SScott Feldman 
90257b61080SScott Feldman 	if (dev_num_vf(dev->dev.parent)) {
90357b61080SScott Feldman 		err = rtnl_vf_ports_fill(skb, dev);
90457b61080SScott Feldman 		if (err)
90557b61080SScott Feldman 			return err;
90657b61080SScott Feldman 	}
90757b61080SScott Feldman 
90857b61080SScott Feldman 	return 0;
90957b61080SScott Feldman }
91057b61080SScott Feldman 
91166cae9edSJiri Pirko static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
91266cae9edSJiri Pirko {
91366cae9edSJiri Pirko 	int err;
91466cae9edSJiri Pirko 	struct netdev_phys_port_id ppid;
91566cae9edSJiri Pirko 
91666cae9edSJiri Pirko 	err = dev_get_phys_port_id(dev, &ppid);
91766cae9edSJiri Pirko 	if (err) {
91866cae9edSJiri Pirko 		if (err == -EOPNOTSUPP)
91966cae9edSJiri Pirko 			return 0;
92066cae9edSJiri Pirko 		return err;
92166cae9edSJiri Pirko 	}
92266cae9edSJiri Pirko 
92366cae9edSJiri Pirko 	if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
92466cae9edSJiri Pirko 		return -EMSGSIZE;
92566cae9edSJiri Pirko 
92666cae9edSJiri Pirko 	return 0;
92766cae9edSJiri Pirko }
92866cae9edSJiri Pirko 
929b60c5115SThomas Graf static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
930575c3e2aSPatrick McHardy 			    int type, u32 pid, u32 seq, u32 change,
931115c9b81SGreg Rose 			    unsigned int flags, u32 ext_filter_mask)
932b60c5115SThomas Graf {
933b60c5115SThomas Graf 	struct ifinfomsg *ifm;
9341da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
93528172739SEric Dumazet 	struct rtnl_link_stats64 temp;
936be1f3c2cSBen Hutchings 	const struct rtnl_link_stats64 *stats;
937f8ff182cSThomas Graf 	struct nlattr *attr, *af_spec;
938f8ff182cSThomas Graf 	struct rtnl_af_ops *af_ops;
939898e5061SJiri Pirko 	struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
9401da177e4SLinus Torvalds 
9412907c35fSEric Dumazet 	ASSERT_RTNL();
942b60c5115SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
943b60c5115SThomas Graf 	if (nlh == NULL)
94426932566SPatrick McHardy 		return -EMSGSIZE;
9451da177e4SLinus Torvalds 
946b60c5115SThomas Graf 	ifm = nlmsg_data(nlh);
947b60c5115SThomas Graf 	ifm->ifi_family = AF_UNSPEC;
948b60c5115SThomas Graf 	ifm->__ifi_pad = 0;
949b60c5115SThomas Graf 	ifm->ifi_type = dev->type;
950b60c5115SThomas Graf 	ifm->ifi_index = dev->ifindex;
951b60c5115SThomas Graf 	ifm->ifi_flags = dev_get_flags(dev);
952b60c5115SThomas Graf 	ifm->ifi_change = change;
9531da177e4SLinus Torvalds 
954a6574349SDavid S. Miller 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
955a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
956a6574349SDavid S. Miller 	    nla_put_u8(skb, IFLA_OPERSTATE,
957a6574349SDavid S. Miller 		       netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
958a6574349SDavid S. Miller 	    nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
959a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
960a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_GROUP, dev->group) ||
961edbc0bb3SBen Greear 	    nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
96276ff5cc9SJiri Pirko 	    nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
9631d69c2b3SMark A. Greer #ifdef CONFIG_RPS
96476ff5cc9SJiri Pirko 	    nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
9651d69c2b3SMark A. Greer #endif
966a6574349SDavid S. Miller 	    (dev->ifindex != dev->iflink &&
967a6574349SDavid S. Miller 	     nla_put_u32(skb, IFLA_LINK, dev->iflink)) ||
968898e5061SJiri Pirko 	    (upper_dev &&
969898e5061SJiri Pirko 	     nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) ||
9709a57247fSJiri Pirko 	    nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
971a6574349SDavid S. Miller 	    (dev->qdisc &&
972a6574349SDavid S. Miller 	     nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
973a6574349SDavid S. Miller 	    (dev->ifalias &&
9742d3b479dSdavid decotigny 	     nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)) ||
9752d3b479dSdavid decotigny 	    nla_put_u32(skb, IFLA_CARRIER_CHANGES,
9762d3b479dSdavid decotigny 			atomic_read(&dev->carrier_changes)))
977a6574349SDavid S. Miller 		goto nla_put_failure;
9780b815a1aSStephen Hemminger 
979b00055aaSStefan Rompf 	if (1) {
9801da177e4SLinus Torvalds 		struct rtnl_link_ifmap map = {
9811da177e4SLinus Torvalds 			.mem_start   = dev->mem_start,
9821da177e4SLinus Torvalds 			.mem_end     = dev->mem_end,
9831da177e4SLinus Torvalds 			.base_addr   = dev->base_addr,
9841da177e4SLinus Torvalds 			.irq         = dev->irq,
9851da177e4SLinus Torvalds 			.dma         = dev->dma,
9861da177e4SLinus Torvalds 			.port        = dev->if_port,
9871da177e4SLinus Torvalds 		};
988a6574349SDavid S. Miller 		if (nla_put(skb, IFLA_MAP, sizeof(map), &map))
989a6574349SDavid S. Miller 			goto nla_put_failure;
9901da177e4SLinus Torvalds 	}
9911da177e4SLinus Torvalds 
9921da177e4SLinus Torvalds 	if (dev->addr_len) {
993a6574349SDavid S. Miller 		if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
994a6574349SDavid S. Miller 		    nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
995a6574349SDavid S. Miller 			goto nla_put_failure;
9961da177e4SLinus Torvalds 	}
9971da177e4SLinus Torvalds 
99866cae9edSJiri Pirko 	if (rtnl_phys_port_id_fill(skb, dev))
99966cae9edSJiri Pirko 		goto nla_put_failure;
100066cae9edSJiri Pirko 
1001b60c5115SThomas Graf 	attr = nla_reserve(skb, IFLA_STATS,
1002b60c5115SThomas Graf 			sizeof(struct rtnl_link_stats));
1003b60c5115SThomas Graf 	if (attr == NULL)
1004b60c5115SThomas Graf 		goto nla_put_failure;
1005b60c5115SThomas Graf 
100628172739SEric Dumazet 	stats = dev_get_stats(dev, &temp);
1007b60c5115SThomas Graf 	copy_rtnl_link_stats(nla_data(attr), stats);
10081da177e4SLinus Torvalds 
100910708f37SJan Engelhardt 	attr = nla_reserve(skb, IFLA_STATS64,
101010708f37SJan Engelhardt 			sizeof(struct rtnl_link_stats64));
101110708f37SJan Engelhardt 	if (attr == NULL)
101210708f37SJan Engelhardt 		goto nla_put_failure;
101310708f37SJan Engelhardt 	copy_rtnl_link_stats64(nla_data(attr), stats);
101410708f37SJan Engelhardt 
1015a6574349SDavid S. Miller 	if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) &&
1016a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)))
1017a6574349SDavid S. Miller 		goto nla_put_failure;
101857b61080SScott Feldman 
1019115c9b81SGreg Rose 	if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent
1020115c9b81SGreg Rose 	    && (ext_filter_mask & RTEXT_FILTER_VF)) {
1021ebc08a6fSWilliams, Mitch A 		int i;
1022ebc08a6fSWilliams, Mitch A 
1023c02db8c6SChris Wright 		struct nlattr *vfinfo, *vf;
1024c02db8c6SChris Wright 		int num_vfs = dev_num_vf(dev->dev.parent);
1025c02db8c6SChris Wright 
1026c02db8c6SChris Wright 		vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1027c02db8c6SChris Wright 		if (!vfinfo)
1028c02db8c6SChris Wright 			goto nla_put_failure;
1029c02db8c6SChris Wright 		for (i = 0; i < num_vfs; i++) {
1030c02db8c6SChris Wright 			struct ifla_vf_info ivi;
1031c02db8c6SChris Wright 			struct ifla_vf_mac vf_mac;
1032c02db8c6SChris Wright 			struct ifla_vf_vlan vf_vlan;
1033c02db8c6SChris Wright 			struct ifla_vf_tx_rate vf_tx_rate;
10345f8444a3SGreg Rose 			struct ifla_vf_spoofchk vf_spoofchk;
10351d8faf48SRony Efraim 			struct ifla_vf_link_state vf_linkstate;
10365f8444a3SGreg Rose 
10375f8444a3SGreg Rose 			/*
10385f8444a3SGreg Rose 			 * Not all SR-IOV capable drivers support the
10395f8444a3SGreg Rose 			 * spoofcheck query.  Preset to -1 so the user
10405f8444a3SGreg Rose 			 * space tool can detect that the driver didn't
10415f8444a3SGreg Rose 			 * report anything.
10425f8444a3SGreg Rose 			 */
10435f8444a3SGreg Rose 			ivi.spoofchk = -1;
104484d73cd3SMathias Krause 			memset(ivi.mac, 0, sizeof(ivi.mac));
10451d8faf48SRony Efraim 			/* The default value for VF link state is "auto"
10461d8faf48SRony Efraim 			 * IFLA_VF_LINK_STATE_AUTO which equals zero
10471d8faf48SRony Efraim 			 */
10481d8faf48SRony Efraim 			ivi.linkstate = 0;
1049ebc08a6fSWilliams, Mitch A 			if (dev->netdev_ops->ndo_get_vf_config(dev, i, &ivi))
1050ebc08a6fSWilliams, Mitch A 				break;
10515f8444a3SGreg Rose 			vf_mac.vf =
10525f8444a3SGreg Rose 				vf_vlan.vf =
10535f8444a3SGreg Rose 				vf_tx_rate.vf =
10541d8faf48SRony Efraim 				vf_spoofchk.vf =
10551d8faf48SRony Efraim 				vf_linkstate.vf = ivi.vf;
10565f8444a3SGreg Rose 
1057c02db8c6SChris Wright 			memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1058c02db8c6SChris Wright 			vf_vlan.vlan = ivi.vlan;
1059c02db8c6SChris Wright 			vf_vlan.qos = ivi.qos;
1060c02db8c6SChris Wright 			vf_tx_rate.rate = ivi.tx_rate;
10615f8444a3SGreg Rose 			vf_spoofchk.setting = ivi.spoofchk;
10621d8faf48SRony Efraim 			vf_linkstate.link_state = ivi.linkstate;
1063c02db8c6SChris Wright 			vf = nla_nest_start(skb, IFLA_VF_INFO);
1064c02db8c6SChris Wright 			if (!vf) {
1065c02db8c6SChris Wright 				nla_nest_cancel(skb, vfinfo);
1066c02db8c6SChris Wright 				goto nla_put_failure;
1067ebc08a6fSWilliams, Mitch A 			}
1068a6574349SDavid S. Miller 			if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1069a6574349SDavid S. Miller 			    nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1070a6574349SDavid S. Miller 			    nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1071a6574349SDavid S. Miller 				    &vf_tx_rate) ||
1072a6574349SDavid S. Miller 			    nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
10731d8faf48SRony Efraim 				    &vf_spoofchk) ||
10741d8faf48SRony Efraim 			    nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
10751d8faf48SRony Efraim 				    &vf_linkstate))
1076a6574349SDavid S. Miller 				goto nla_put_failure;
1077c02db8c6SChris Wright 			nla_nest_end(skb, vf);
1078c02db8c6SChris Wright 		}
1079c02db8c6SChris Wright 		nla_nest_end(skb, vfinfo);
1080ebc08a6fSWilliams, Mitch A 	}
108157b61080SScott Feldman 
108257b61080SScott Feldman 	if (rtnl_port_fill(skb, dev))
108357b61080SScott Feldman 		goto nla_put_failure;
108457b61080SScott Feldman 
1085ba7d49b1SJiri Pirko 	if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
108638f7b870SPatrick McHardy 		if (rtnl_link_fill(skb, dev) < 0)
108738f7b870SPatrick McHardy 			goto nla_put_failure;
108838f7b870SPatrick McHardy 	}
108938f7b870SPatrick McHardy 
1090f8ff182cSThomas Graf 	if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
1091f8ff182cSThomas Graf 		goto nla_put_failure;
1092f8ff182cSThomas Graf 
1093f8ff182cSThomas Graf 	list_for_each_entry(af_ops, &rtnl_af_ops, list) {
1094f8ff182cSThomas Graf 		if (af_ops->fill_link_af) {
1095f8ff182cSThomas Graf 			struct nlattr *af;
1096f8ff182cSThomas Graf 			int err;
1097f8ff182cSThomas Graf 
1098f8ff182cSThomas Graf 			if (!(af = nla_nest_start(skb, af_ops->family)))
1099f8ff182cSThomas Graf 				goto nla_put_failure;
1100f8ff182cSThomas Graf 
1101f8ff182cSThomas Graf 			err = af_ops->fill_link_af(skb, dev);
1102f8ff182cSThomas Graf 
1103f8ff182cSThomas Graf 			/*
1104f8ff182cSThomas Graf 			 * Caller may return ENODATA to indicate that there
1105f8ff182cSThomas Graf 			 * was no data to be dumped. This is not an error, it
1106f8ff182cSThomas Graf 			 * means we should trim the attribute header and
1107f8ff182cSThomas Graf 			 * continue.
1108f8ff182cSThomas Graf 			 */
1109f8ff182cSThomas Graf 			if (err == -ENODATA)
1110f8ff182cSThomas Graf 				nla_nest_cancel(skb, af);
1111f8ff182cSThomas Graf 			else if (err < 0)
1112f8ff182cSThomas Graf 				goto nla_put_failure;
1113f8ff182cSThomas Graf 
1114f8ff182cSThomas Graf 			nla_nest_end(skb, af);
1115f8ff182cSThomas Graf 		}
1116f8ff182cSThomas Graf 	}
1117f8ff182cSThomas Graf 
1118f8ff182cSThomas Graf 	nla_nest_end(skb, af_spec);
1119f8ff182cSThomas Graf 
1120b60c5115SThomas Graf 	return nlmsg_end(skb, nlh);
1121b60c5115SThomas Graf 
1122b60c5115SThomas Graf nla_put_failure:
112326932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
112426932566SPatrick McHardy 	return -EMSGSIZE;
11251da177e4SLinus Torvalds }
11261da177e4SLinus Torvalds 
1127f7b12606SJiri Pirko static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1128f7b12606SJiri Pirko 	[IFLA_IFNAME]		= { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1129f7b12606SJiri Pirko 	[IFLA_ADDRESS]		= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1130f7b12606SJiri Pirko 	[IFLA_BROADCAST]	= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1131f7b12606SJiri Pirko 	[IFLA_MAP]		= { .len = sizeof(struct rtnl_link_ifmap) },
1132f7b12606SJiri Pirko 	[IFLA_MTU]		= { .type = NLA_U32 },
1133f7b12606SJiri Pirko 	[IFLA_LINK]		= { .type = NLA_U32 },
1134f7b12606SJiri Pirko 	[IFLA_MASTER]		= { .type = NLA_U32 },
1135f7b12606SJiri Pirko 	[IFLA_CARRIER]		= { .type = NLA_U8 },
1136f7b12606SJiri Pirko 	[IFLA_TXQLEN]		= { .type = NLA_U32 },
1137f7b12606SJiri Pirko 	[IFLA_WEIGHT]		= { .type = NLA_U32 },
1138f7b12606SJiri Pirko 	[IFLA_OPERSTATE]	= { .type = NLA_U8 },
1139f7b12606SJiri Pirko 	[IFLA_LINKMODE]		= { .type = NLA_U8 },
1140f7b12606SJiri Pirko 	[IFLA_LINKINFO]		= { .type = NLA_NESTED },
1141f7b12606SJiri Pirko 	[IFLA_NET_NS_PID]	= { .type = NLA_U32 },
1142f7b12606SJiri Pirko 	[IFLA_NET_NS_FD]	= { .type = NLA_U32 },
1143f7b12606SJiri Pirko 	[IFLA_IFALIAS]	        = { .type = NLA_STRING, .len = IFALIASZ-1 },
1144f7b12606SJiri Pirko 	[IFLA_VFINFO_LIST]	= {. type = NLA_NESTED },
1145f7b12606SJiri Pirko 	[IFLA_VF_PORTS]		= { .type = NLA_NESTED },
1146f7b12606SJiri Pirko 	[IFLA_PORT_SELF]	= { .type = NLA_NESTED },
1147f7b12606SJiri Pirko 	[IFLA_AF_SPEC]		= { .type = NLA_NESTED },
1148f7b12606SJiri Pirko 	[IFLA_EXT_MASK]		= { .type = NLA_U32 },
1149f7b12606SJiri Pirko 	[IFLA_PROMISCUITY]	= { .type = NLA_U32 },
1150f7b12606SJiri Pirko 	[IFLA_NUM_TX_QUEUES]	= { .type = NLA_U32 },
1151f7b12606SJiri Pirko 	[IFLA_NUM_RX_QUEUES]	= { .type = NLA_U32 },
1152f7b12606SJiri Pirko 	[IFLA_PHYS_PORT_ID]	= { .type = NLA_BINARY, .len = MAX_PHYS_PORT_ID_LEN },
11532d3b479dSdavid decotigny 	[IFLA_CARRIER_CHANGES]	= { .type = NLA_U32 },  /* ignored */
1154f7b12606SJiri Pirko };
1155f7b12606SJiri Pirko 
1156f7b12606SJiri Pirko static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1157f7b12606SJiri Pirko 	[IFLA_INFO_KIND]	= { .type = NLA_STRING },
1158f7b12606SJiri Pirko 	[IFLA_INFO_DATA]	= { .type = NLA_NESTED },
1159f7b12606SJiri Pirko 	[IFLA_INFO_SLAVE_KIND]	= { .type = NLA_STRING },
1160f7b12606SJiri Pirko 	[IFLA_INFO_SLAVE_DATA]	= { .type = NLA_NESTED },
1161f7b12606SJiri Pirko };
1162f7b12606SJiri Pirko 
1163f7b12606SJiri Pirko static const struct nla_policy ifla_vfinfo_policy[IFLA_VF_INFO_MAX+1] = {
1164f7b12606SJiri Pirko 	[IFLA_VF_INFO]		= { .type = NLA_NESTED },
1165f7b12606SJiri Pirko };
1166f7b12606SJiri Pirko 
1167f7b12606SJiri Pirko static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1168f7b12606SJiri Pirko 	[IFLA_VF_MAC]		= { .type = NLA_BINARY,
1169f7b12606SJiri Pirko 				    .len = sizeof(struct ifla_vf_mac) },
1170f7b12606SJiri Pirko 	[IFLA_VF_VLAN]		= { .type = NLA_BINARY,
1171f7b12606SJiri Pirko 				    .len = sizeof(struct ifla_vf_vlan) },
1172f7b12606SJiri Pirko 	[IFLA_VF_TX_RATE]	= { .type = NLA_BINARY,
1173f7b12606SJiri Pirko 				    .len = sizeof(struct ifla_vf_tx_rate) },
1174f7b12606SJiri Pirko 	[IFLA_VF_SPOOFCHK]	= { .type = NLA_BINARY,
1175f7b12606SJiri Pirko 				    .len = sizeof(struct ifla_vf_spoofchk) },
1176f7b12606SJiri Pirko };
1177f7b12606SJiri Pirko 
1178f7b12606SJiri Pirko static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1179f7b12606SJiri Pirko 	[IFLA_PORT_VF]		= { .type = NLA_U32 },
1180f7b12606SJiri Pirko 	[IFLA_PORT_PROFILE]	= { .type = NLA_STRING,
1181f7b12606SJiri Pirko 				    .len = PORT_PROFILE_MAX },
1182f7b12606SJiri Pirko 	[IFLA_PORT_VSI_TYPE]	= { .type = NLA_BINARY,
1183f7b12606SJiri Pirko 				    .len = sizeof(struct ifla_port_vsi)},
1184f7b12606SJiri Pirko 	[IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1185f7b12606SJiri Pirko 				      .len = PORT_UUID_MAX },
1186f7b12606SJiri Pirko 	[IFLA_PORT_HOST_UUID]	= { .type = NLA_STRING,
1187f7b12606SJiri Pirko 				    .len = PORT_UUID_MAX },
1188f7b12606SJiri Pirko 	[IFLA_PORT_REQUEST]	= { .type = NLA_U8, },
1189f7b12606SJiri Pirko 	[IFLA_PORT_RESPONSE]	= { .type = NLA_U16, },
1190f7b12606SJiri Pirko };
1191f7b12606SJiri Pirko 
1192b60c5115SThomas Graf static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
11931da177e4SLinus Torvalds {
11943b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
11957c28bd0bSEric Dumazet 	int h, s_h;
11967c28bd0bSEric Dumazet 	int idx = 0, s_idx;
11971da177e4SLinus Torvalds 	struct net_device *dev;
11987c28bd0bSEric Dumazet 	struct hlist_head *head;
1199115c9b81SGreg Rose 	struct nlattr *tb[IFLA_MAX+1];
1200115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
1201*973462bbSDavid Gibson 	int err;
12021da177e4SLinus Torvalds 
12037c28bd0bSEric Dumazet 	s_h = cb->args[0];
12047c28bd0bSEric Dumazet 	s_idx = cb->args[1];
12057c28bd0bSEric Dumazet 
1206e67f88ddSEric Dumazet 	rcu_read_lock();
12074e985adaSThomas Graf 	cb->seq = net->dev_base_seq;
12084e985adaSThomas Graf 
120988c5b5ceSMichael Riesch 	if (nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
1210a4b64fbeSEric Dumazet 			ifla_policy) >= 0) {
1211115c9b81SGreg Rose 
1212115c9b81SGreg Rose 		if (tb[IFLA_EXT_MASK])
1213115c9b81SGreg Rose 			ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1214a4b64fbeSEric Dumazet 	}
1215115c9b81SGreg Rose 
12167c28bd0bSEric Dumazet 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
12177562f876SPavel Emelianov 		idx = 0;
12187c28bd0bSEric Dumazet 		head = &net->dev_index_head[h];
1219b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu(dev, head, index_hlist) {
12201da177e4SLinus Torvalds 			if (idx < s_idx)
12217562f876SPavel Emelianov 				goto cont;
1222*973462bbSDavid Gibson 			err = rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
122315e47304SEric W. Biederman 					       NETLINK_CB(cb->skb).portid,
12247c28bd0bSEric Dumazet 					       cb->nlh->nlmsg_seq, 0,
1225115c9b81SGreg Rose 					       NLM_F_MULTI,
1226*973462bbSDavid Gibson 					       ext_filter_mask);
1227*973462bbSDavid Gibson 			/* If we ran out of room on the first message,
1228*973462bbSDavid Gibson 			 * we're in trouble
1229*973462bbSDavid Gibson 			 */
1230*973462bbSDavid Gibson 			WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
1231*973462bbSDavid Gibson 
1232*973462bbSDavid Gibson 			if (err <= 0)
12337c28bd0bSEric Dumazet 				goto out;
12344e985adaSThomas Graf 
12354e985adaSThomas Graf 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
12367562f876SPavel Emelianov cont:
12377562f876SPavel Emelianov 			idx++;
12381da177e4SLinus Torvalds 		}
12397c28bd0bSEric Dumazet 	}
12407c28bd0bSEric Dumazet out:
1241e67f88ddSEric Dumazet 	rcu_read_unlock();
12427c28bd0bSEric Dumazet 	cb->args[1] = idx;
12437c28bd0bSEric Dumazet 	cb->args[0] = h;
12441da177e4SLinus Torvalds 
12451da177e4SLinus Torvalds 	return skb->len;
12461da177e4SLinus Torvalds }
12471da177e4SLinus Torvalds 
1248f7b12606SJiri Pirko int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len)
1249f7b12606SJiri Pirko {
1250f7b12606SJiri Pirko 	return nla_parse(tb, IFLA_MAX, head, len, ifla_policy);
1251f7b12606SJiri Pirko }
1252f7b12606SJiri Pirko EXPORT_SYMBOL(rtnl_nla_parse_ifla);
125357b61080SScott Feldman 
125481adee47SEric W. Biederman struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
125581adee47SEric W. Biederman {
125681adee47SEric W. Biederman 	struct net *net;
125781adee47SEric W. Biederman 	/* Examine the link attributes and figure out which
125881adee47SEric W. Biederman 	 * network namespace we are talking about.
125981adee47SEric W. Biederman 	 */
126081adee47SEric W. Biederman 	if (tb[IFLA_NET_NS_PID])
126181adee47SEric W. Biederman 		net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
1262f0630529SEric W. Biederman 	else if (tb[IFLA_NET_NS_FD])
1263f0630529SEric W. Biederman 		net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
126481adee47SEric W. Biederman 	else
126581adee47SEric W. Biederman 		net = get_net(src_net);
126681adee47SEric W. Biederman 	return net;
126781adee47SEric W. Biederman }
126881adee47SEric W. Biederman EXPORT_SYMBOL(rtnl_link_get_net);
126981adee47SEric W. Biederman 
12701840bb13SThomas Graf static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
12711840bb13SThomas Graf {
12721840bb13SThomas Graf 	if (dev) {
12731840bb13SThomas Graf 		if (tb[IFLA_ADDRESS] &&
12741840bb13SThomas Graf 		    nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
12751840bb13SThomas Graf 			return -EINVAL;
12761840bb13SThomas Graf 
12771840bb13SThomas Graf 		if (tb[IFLA_BROADCAST] &&
12781840bb13SThomas Graf 		    nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
12791840bb13SThomas Graf 			return -EINVAL;
12801840bb13SThomas Graf 	}
12811840bb13SThomas Graf 
1282cf7afbfeSThomas Graf 	if (tb[IFLA_AF_SPEC]) {
1283cf7afbfeSThomas Graf 		struct nlattr *af;
1284cf7afbfeSThomas Graf 		int rem, err;
1285cf7afbfeSThomas Graf 
1286cf7afbfeSThomas Graf 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1287cf7afbfeSThomas Graf 			const struct rtnl_af_ops *af_ops;
1288cf7afbfeSThomas Graf 
1289cf7afbfeSThomas Graf 			if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1290cf7afbfeSThomas Graf 				return -EAFNOSUPPORT;
1291cf7afbfeSThomas Graf 
1292cf7afbfeSThomas Graf 			if (!af_ops->set_link_af)
1293cf7afbfeSThomas Graf 				return -EOPNOTSUPP;
1294cf7afbfeSThomas Graf 
1295cf7afbfeSThomas Graf 			if (af_ops->validate_link_af) {
12966d3a9a68SKurt Van Dijck 				err = af_ops->validate_link_af(dev, af);
1297cf7afbfeSThomas Graf 				if (err < 0)
1298cf7afbfeSThomas Graf 					return err;
1299cf7afbfeSThomas Graf 			}
1300cf7afbfeSThomas Graf 		}
1301cf7afbfeSThomas Graf 	}
1302cf7afbfeSThomas Graf 
13031840bb13SThomas Graf 	return 0;
13041840bb13SThomas Graf }
13051840bb13SThomas Graf 
1306c02db8c6SChris Wright static int do_setvfinfo(struct net_device *dev, struct nlattr *attr)
1307c02db8c6SChris Wright {
1308c02db8c6SChris Wright 	int rem, err = -EINVAL;
1309c02db8c6SChris Wright 	struct nlattr *vf;
1310c02db8c6SChris Wright 	const struct net_device_ops *ops = dev->netdev_ops;
1311c02db8c6SChris Wright 
1312c02db8c6SChris Wright 	nla_for_each_nested(vf, attr, rem) {
1313c02db8c6SChris Wright 		switch (nla_type(vf)) {
1314c02db8c6SChris Wright 		case IFLA_VF_MAC: {
1315c02db8c6SChris Wright 			struct ifla_vf_mac *ivm;
1316c02db8c6SChris Wright 			ivm = nla_data(vf);
1317c02db8c6SChris Wright 			err = -EOPNOTSUPP;
1318c02db8c6SChris Wright 			if (ops->ndo_set_vf_mac)
1319c02db8c6SChris Wright 				err = ops->ndo_set_vf_mac(dev, ivm->vf,
1320c02db8c6SChris Wright 							  ivm->mac);
1321c02db8c6SChris Wright 			break;
1322c02db8c6SChris Wright 		}
1323c02db8c6SChris Wright 		case IFLA_VF_VLAN: {
1324c02db8c6SChris Wright 			struct ifla_vf_vlan *ivv;
1325c02db8c6SChris Wright 			ivv = nla_data(vf);
1326c02db8c6SChris Wright 			err = -EOPNOTSUPP;
1327c02db8c6SChris Wright 			if (ops->ndo_set_vf_vlan)
1328c02db8c6SChris Wright 				err = ops->ndo_set_vf_vlan(dev, ivv->vf,
1329c02db8c6SChris Wright 							   ivv->vlan,
1330c02db8c6SChris Wright 							   ivv->qos);
1331c02db8c6SChris Wright 			break;
1332c02db8c6SChris Wright 		}
1333c02db8c6SChris Wright 		case IFLA_VF_TX_RATE: {
1334c02db8c6SChris Wright 			struct ifla_vf_tx_rate *ivt;
1335c02db8c6SChris Wright 			ivt = nla_data(vf);
1336c02db8c6SChris Wright 			err = -EOPNOTSUPP;
1337c02db8c6SChris Wright 			if (ops->ndo_set_vf_tx_rate)
1338c02db8c6SChris Wright 				err = ops->ndo_set_vf_tx_rate(dev, ivt->vf,
1339c02db8c6SChris Wright 							      ivt->rate);
1340c02db8c6SChris Wright 			break;
1341c02db8c6SChris Wright 		}
13425f8444a3SGreg Rose 		case IFLA_VF_SPOOFCHK: {
13435f8444a3SGreg Rose 			struct ifla_vf_spoofchk *ivs;
13445f8444a3SGreg Rose 			ivs = nla_data(vf);
13455f8444a3SGreg Rose 			err = -EOPNOTSUPP;
13465f8444a3SGreg Rose 			if (ops->ndo_set_vf_spoofchk)
13475f8444a3SGreg Rose 				err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
13485f8444a3SGreg Rose 							       ivs->setting);
13495f8444a3SGreg Rose 			break;
13505f8444a3SGreg Rose 		}
13511d8faf48SRony Efraim 		case IFLA_VF_LINK_STATE: {
13521d8faf48SRony Efraim 			struct ifla_vf_link_state *ivl;
13531d8faf48SRony Efraim 			ivl = nla_data(vf);
13541d8faf48SRony Efraim 			err = -EOPNOTSUPP;
13551d8faf48SRony Efraim 			if (ops->ndo_set_vf_link_state)
13561d8faf48SRony Efraim 				err = ops->ndo_set_vf_link_state(dev, ivl->vf,
13571d8faf48SRony Efraim 								 ivl->link_state);
13581d8faf48SRony Efraim 			break;
13591d8faf48SRony Efraim 		}
1360c02db8c6SChris Wright 		default:
1361c02db8c6SChris Wright 			err = -EINVAL;
1362c02db8c6SChris Wright 			break;
1363c02db8c6SChris Wright 		}
1364c02db8c6SChris Wright 		if (err)
1365c02db8c6SChris Wright 			break;
1366c02db8c6SChris Wright 	}
1367c02db8c6SChris Wright 	return err;
1368c02db8c6SChris Wright }
1369c02db8c6SChris Wright 
1370fbaec0eaSJiri Pirko static int do_set_master(struct net_device *dev, int ifindex)
1371fbaec0eaSJiri Pirko {
1372898e5061SJiri Pirko 	struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
1373fbaec0eaSJiri Pirko 	const struct net_device_ops *ops;
1374fbaec0eaSJiri Pirko 	int err;
1375fbaec0eaSJiri Pirko 
1376898e5061SJiri Pirko 	if (upper_dev) {
1377898e5061SJiri Pirko 		if (upper_dev->ifindex == ifindex)
1378fbaec0eaSJiri Pirko 			return 0;
1379898e5061SJiri Pirko 		ops = upper_dev->netdev_ops;
1380fbaec0eaSJiri Pirko 		if (ops->ndo_del_slave) {
1381898e5061SJiri Pirko 			err = ops->ndo_del_slave(upper_dev, dev);
1382fbaec0eaSJiri Pirko 			if (err)
1383fbaec0eaSJiri Pirko 				return err;
1384fbaec0eaSJiri Pirko 		} else {
1385fbaec0eaSJiri Pirko 			return -EOPNOTSUPP;
1386fbaec0eaSJiri Pirko 		}
1387fbaec0eaSJiri Pirko 	}
1388fbaec0eaSJiri Pirko 
1389fbaec0eaSJiri Pirko 	if (ifindex) {
1390898e5061SJiri Pirko 		upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
1391898e5061SJiri Pirko 		if (!upper_dev)
1392fbaec0eaSJiri Pirko 			return -EINVAL;
1393898e5061SJiri Pirko 		ops = upper_dev->netdev_ops;
1394fbaec0eaSJiri Pirko 		if (ops->ndo_add_slave) {
1395898e5061SJiri Pirko 			err = ops->ndo_add_slave(upper_dev, dev);
1396fbaec0eaSJiri Pirko 			if (err)
1397fbaec0eaSJiri Pirko 				return err;
1398fbaec0eaSJiri Pirko 		} else {
1399fbaec0eaSJiri Pirko 			return -EOPNOTSUPP;
1400fbaec0eaSJiri Pirko 		}
1401fbaec0eaSJiri Pirko 	}
1402fbaec0eaSJiri Pirko 	return 0;
1403fbaec0eaSJiri Pirko }
1404fbaec0eaSJiri Pirko 
140590f62cf3SEric W. Biederman static int do_setlink(const struct sk_buff *skb,
140690f62cf3SEric W. Biederman 		      struct net_device *dev, struct ifinfomsg *ifm,
140738f7b870SPatrick McHardy 		      struct nlattr **tb, char *ifname, int modified)
14080157f60cSPatrick McHardy {
1409d314774cSStephen Hemminger 	const struct net_device_ops *ops = dev->netdev_ops;
14100157f60cSPatrick McHardy 	int err;
14110157f60cSPatrick McHardy 
1412f0630529SEric W. Biederman 	if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
141381adee47SEric W. Biederman 		struct net *net = rtnl_link_get_net(dev_net(dev), tb);
1414d8a5ec67SEric W. Biederman 		if (IS_ERR(net)) {
1415d8a5ec67SEric W. Biederman 			err = PTR_ERR(net);
1416d8a5ec67SEric W. Biederman 			goto errout;
1417d8a5ec67SEric W. Biederman 		}
141890f62cf3SEric W. Biederman 		if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
1419b51642f6SEric W. Biederman 			err = -EPERM;
1420b51642f6SEric W. Biederman 			goto errout;
1421b51642f6SEric W. Biederman 		}
1422d8a5ec67SEric W. Biederman 		err = dev_change_net_namespace(dev, net, ifname);
1423d8a5ec67SEric W. Biederman 		put_net(net);
1424d8a5ec67SEric W. Biederman 		if (err)
1425d8a5ec67SEric W. Biederman 			goto errout;
1426d8a5ec67SEric W. Biederman 		modified = 1;
1427d8a5ec67SEric W. Biederman 	}
1428d8a5ec67SEric W. Biederman 
14290157f60cSPatrick McHardy 	if (tb[IFLA_MAP]) {
14300157f60cSPatrick McHardy 		struct rtnl_link_ifmap *u_map;
14310157f60cSPatrick McHardy 		struct ifmap k_map;
14320157f60cSPatrick McHardy 
1433d314774cSStephen Hemminger 		if (!ops->ndo_set_config) {
14340157f60cSPatrick McHardy 			err = -EOPNOTSUPP;
14350157f60cSPatrick McHardy 			goto errout;
14360157f60cSPatrick McHardy 		}
14370157f60cSPatrick McHardy 
14380157f60cSPatrick McHardy 		if (!netif_device_present(dev)) {
14390157f60cSPatrick McHardy 			err = -ENODEV;
14400157f60cSPatrick McHardy 			goto errout;
14410157f60cSPatrick McHardy 		}
14420157f60cSPatrick McHardy 
14430157f60cSPatrick McHardy 		u_map = nla_data(tb[IFLA_MAP]);
14440157f60cSPatrick McHardy 		k_map.mem_start = (unsigned long) u_map->mem_start;
14450157f60cSPatrick McHardy 		k_map.mem_end = (unsigned long) u_map->mem_end;
14460157f60cSPatrick McHardy 		k_map.base_addr = (unsigned short) u_map->base_addr;
14470157f60cSPatrick McHardy 		k_map.irq = (unsigned char) u_map->irq;
14480157f60cSPatrick McHardy 		k_map.dma = (unsigned char) u_map->dma;
14490157f60cSPatrick McHardy 		k_map.port = (unsigned char) u_map->port;
14500157f60cSPatrick McHardy 
1451d314774cSStephen Hemminger 		err = ops->ndo_set_config(dev, &k_map);
14520157f60cSPatrick McHardy 		if (err < 0)
14530157f60cSPatrick McHardy 			goto errout;
14540157f60cSPatrick McHardy 
14550157f60cSPatrick McHardy 		modified = 1;
14560157f60cSPatrick McHardy 	}
14570157f60cSPatrick McHardy 
14580157f60cSPatrick McHardy 	if (tb[IFLA_ADDRESS]) {
14590157f60cSPatrick McHardy 		struct sockaddr *sa;
14600157f60cSPatrick McHardy 		int len;
14610157f60cSPatrick McHardy 
14620157f60cSPatrick McHardy 		len = sizeof(sa_family_t) + dev->addr_len;
14630157f60cSPatrick McHardy 		sa = kmalloc(len, GFP_KERNEL);
14640157f60cSPatrick McHardy 		if (!sa) {
14650157f60cSPatrick McHardy 			err = -ENOMEM;
14660157f60cSPatrick McHardy 			goto errout;
14670157f60cSPatrick McHardy 		}
14680157f60cSPatrick McHardy 		sa->sa_family = dev->type;
14690157f60cSPatrick McHardy 		memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
14700157f60cSPatrick McHardy 		       dev->addr_len);
1471e7c3273eSJiri Pirko 		err = dev_set_mac_address(dev, sa);
14720157f60cSPatrick McHardy 		kfree(sa);
14730157f60cSPatrick McHardy 		if (err)
14740157f60cSPatrick McHardy 			goto errout;
14750157f60cSPatrick McHardy 		modified = 1;
14760157f60cSPatrick McHardy 	}
14770157f60cSPatrick McHardy 
14780157f60cSPatrick McHardy 	if (tb[IFLA_MTU]) {
14790157f60cSPatrick McHardy 		err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
14800157f60cSPatrick McHardy 		if (err < 0)
14810157f60cSPatrick McHardy 			goto errout;
14820157f60cSPatrick McHardy 		modified = 1;
14830157f60cSPatrick McHardy 	}
14840157f60cSPatrick McHardy 
1485cbda10faSVlad Dogaru 	if (tb[IFLA_GROUP]) {
1486cbda10faSVlad Dogaru 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
1487cbda10faSVlad Dogaru 		modified = 1;
1488cbda10faSVlad Dogaru 	}
1489cbda10faSVlad Dogaru 
14900157f60cSPatrick McHardy 	/*
14910157f60cSPatrick McHardy 	 * Interface selected by interface index but interface
14920157f60cSPatrick McHardy 	 * name provided implies that a name change has been
14930157f60cSPatrick McHardy 	 * requested.
14940157f60cSPatrick McHardy 	 */
14950157f60cSPatrick McHardy 	if (ifm->ifi_index > 0 && ifname[0]) {
14960157f60cSPatrick McHardy 		err = dev_change_name(dev, ifname);
14970157f60cSPatrick McHardy 		if (err < 0)
14980157f60cSPatrick McHardy 			goto errout;
14990157f60cSPatrick McHardy 		modified = 1;
15000157f60cSPatrick McHardy 	}
15010157f60cSPatrick McHardy 
15020b815a1aSStephen Hemminger 	if (tb[IFLA_IFALIAS]) {
15030b815a1aSStephen Hemminger 		err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
15040b815a1aSStephen Hemminger 				    nla_len(tb[IFLA_IFALIAS]));
15050b815a1aSStephen Hemminger 		if (err < 0)
15060b815a1aSStephen Hemminger 			goto errout;
15070b815a1aSStephen Hemminger 		modified = 1;
15080b815a1aSStephen Hemminger 	}
15090b815a1aSStephen Hemminger 
15100157f60cSPatrick McHardy 	if (tb[IFLA_BROADCAST]) {
15110157f60cSPatrick McHardy 		nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
1512e7c3273eSJiri Pirko 		call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
15130157f60cSPatrick McHardy 	}
15140157f60cSPatrick McHardy 
15150157f60cSPatrick McHardy 	if (ifm->ifi_flags || ifm->ifi_change) {
15163729d502SPatrick McHardy 		err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
15175f9021cfSJohannes Berg 		if (err < 0)
15185f9021cfSJohannes Berg 			goto errout;
15190157f60cSPatrick McHardy 	}
15200157f60cSPatrick McHardy 
1521fbaec0eaSJiri Pirko 	if (tb[IFLA_MASTER]) {
1522fbaec0eaSJiri Pirko 		err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
1523fbaec0eaSJiri Pirko 		if (err)
1524fbaec0eaSJiri Pirko 			goto errout;
1525fbaec0eaSJiri Pirko 		modified = 1;
1526fbaec0eaSJiri Pirko 	}
1527fbaec0eaSJiri Pirko 
15289a57247fSJiri Pirko 	if (tb[IFLA_CARRIER]) {
15299a57247fSJiri Pirko 		err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
15309a57247fSJiri Pirko 		if (err)
15319a57247fSJiri Pirko 			goto errout;
15329a57247fSJiri Pirko 		modified = 1;
15339a57247fSJiri Pirko 	}
15349a57247fSJiri Pirko 
153593b2d4a2SDavid S. Miller 	if (tb[IFLA_TXQLEN])
15360157f60cSPatrick McHardy 		dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
15370157f60cSPatrick McHardy 
15380157f60cSPatrick McHardy 	if (tb[IFLA_OPERSTATE])
153993b2d4a2SDavid S. Miller 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
15400157f60cSPatrick McHardy 
15410157f60cSPatrick McHardy 	if (tb[IFLA_LINKMODE]) {
15420157f60cSPatrick McHardy 		write_lock_bh(&dev_base_lock);
15430157f60cSPatrick McHardy 		dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
154493b2d4a2SDavid S. Miller 		write_unlock_bh(&dev_base_lock);
15450157f60cSPatrick McHardy 	}
15460157f60cSPatrick McHardy 
1547c02db8c6SChris Wright 	if (tb[IFLA_VFINFO_LIST]) {
1548c02db8c6SChris Wright 		struct nlattr *attr;
1549c02db8c6SChris Wright 		int rem;
1550c02db8c6SChris Wright 		nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
1551253683bbSDavid Howells 			if (nla_type(attr) != IFLA_VF_INFO) {
1552253683bbSDavid Howells 				err = -EINVAL;
1553c02db8c6SChris Wright 				goto errout;
1554253683bbSDavid Howells 			}
1555c02db8c6SChris Wright 			err = do_setvfinfo(dev, attr);
1556ebc08a6fSWilliams, Mitch A 			if (err < 0)
1557ebc08a6fSWilliams, Mitch A 				goto errout;
1558ebc08a6fSWilliams, Mitch A 			modified = 1;
1559ebc08a6fSWilliams, Mitch A 		}
1560ebc08a6fSWilliams, Mitch A 	}
15610157f60cSPatrick McHardy 	err = 0;
15620157f60cSPatrick McHardy 
156357b61080SScott Feldman 	if (tb[IFLA_VF_PORTS]) {
156457b61080SScott Feldman 		struct nlattr *port[IFLA_PORT_MAX+1];
156557b61080SScott Feldman 		struct nlattr *attr;
156657b61080SScott Feldman 		int vf;
156757b61080SScott Feldman 		int rem;
156857b61080SScott Feldman 
156957b61080SScott Feldman 		err = -EOPNOTSUPP;
157057b61080SScott Feldman 		if (!ops->ndo_set_vf_port)
157157b61080SScott Feldman 			goto errout;
157257b61080SScott Feldman 
157357b61080SScott Feldman 		nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
157457b61080SScott Feldman 			if (nla_type(attr) != IFLA_VF_PORT)
157557b61080SScott Feldman 				continue;
157657b61080SScott Feldman 			err = nla_parse_nested(port, IFLA_PORT_MAX,
157757b61080SScott Feldman 				attr, ifla_port_policy);
157857b61080SScott Feldman 			if (err < 0)
157957b61080SScott Feldman 				goto errout;
158057b61080SScott Feldman 			if (!port[IFLA_PORT_VF]) {
158157b61080SScott Feldman 				err = -EOPNOTSUPP;
158257b61080SScott Feldman 				goto errout;
158357b61080SScott Feldman 			}
158457b61080SScott Feldman 			vf = nla_get_u32(port[IFLA_PORT_VF]);
158557b61080SScott Feldman 			err = ops->ndo_set_vf_port(dev, vf, port);
158657b61080SScott Feldman 			if (err < 0)
158757b61080SScott Feldman 				goto errout;
158857b61080SScott Feldman 			modified = 1;
158957b61080SScott Feldman 		}
159057b61080SScott Feldman 	}
159157b61080SScott Feldman 	err = 0;
159257b61080SScott Feldman 
159357b61080SScott Feldman 	if (tb[IFLA_PORT_SELF]) {
159457b61080SScott Feldman 		struct nlattr *port[IFLA_PORT_MAX+1];
159557b61080SScott Feldman 
159657b61080SScott Feldman 		err = nla_parse_nested(port, IFLA_PORT_MAX,
159757b61080SScott Feldman 			tb[IFLA_PORT_SELF], ifla_port_policy);
159857b61080SScott Feldman 		if (err < 0)
159957b61080SScott Feldman 			goto errout;
160057b61080SScott Feldman 
160157b61080SScott Feldman 		err = -EOPNOTSUPP;
160257b61080SScott Feldman 		if (ops->ndo_set_vf_port)
160357b61080SScott Feldman 			err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
160457b61080SScott Feldman 		if (err < 0)
160557b61080SScott Feldman 			goto errout;
160657b61080SScott Feldman 		modified = 1;
160757b61080SScott Feldman 	}
1608f8ff182cSThomas Graf 
1609f8ff182cSThomas Graf 	if (tb[IFLA_AF_SPEC]) {
1610f8ff182cSThomas Graf 		struct nlattr *af;
1611f8ff182cSThomas Graf 		int rem;
1612f8ff182cSThomas Graf 
1613f8ff182cSThomas Graf 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1614f8ff182cSThomas Graf 			const struct rtnl_af_ops *af_ops;
1615f8ff182cSThomas Graf 
1616f8ff182cSThomas Graf 			if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1617cf7afbfeSThomas Graf 				BUG();
1618f8ff182cSThomas Graf 
1619cf7afbfeSThomas Graf 			err = af_ops->set_link_af(dev, af);
1620f8ff182cSThomas Graf 			if (err < 0)
1621f8ff182cSThomas Graf 				goto errout;
1622f8ff182cSThomas Graf 
1623f8ff182cSThomas Graf 			modified = 1;
1624f8ff182cSThomas Graf 		}
1625f8ff182cSThomas Graf 	}
162657b61080SScott Feldman 	err = 0;
162757b61080SScott Feldman 
16280157f60cSPatrick McHardy errout:
1629e87cc472SJoe Perches 	if (err < 0 && modified)
1630e87cc472SJoe 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",
1631e87cc472SJoe Perches 				     dev->name);
16320157f60cSPatrick McHardy 
16330157f60cSPatrick McHardy 	return err;
16340157f60cSPatrick McHardy }
16350157f60cSPatrick McHardy 
1636661d2967SThomas Graf static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
1637da5e0494SThomas Graf {
16383b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
1639da5e0494SThomas Graf 	struct ifinfomsg *ifm;
1640da5e0494SThomas Graf 	struct net_device *dev;
16410157f60cSPatrick McHardy 	int err;
1642da5e0494SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
16431da177e4SLinus Torvalds 	char ifname[IFNAMSIZ];
16441da177e4SLinus Torvalds 
1645da5e0494SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1646da5e0494SThomas Graf 	if (err < 0)
1647da5e0494SThomas Graf 		goto errout;
16481da177e4SLinus Torvalds 
16495176f91eSThomas Graf 	if (tb[IFLA_IFNAME])
16505176f91eSThomas Graf 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
165178e5b891SPatrick McHardy 	else
165278e5b891SPatrick McHardy 		ifname[0] = '\0';
16531da177e4SLinus Torvalds 
16541da177e4SLinus Torvalds 	err = -EINVAL;
1655da5e0494SThomas Graf 	ifm = nlmsg_data(nlh);
165651055be8SPatrick McHardy 	if (ifm->ifi_index > 0)
1657a3d12891SEric Dumazet 		dev = __dev_get_by_index(net, ifm->ifi_index);
1658da5e0494SThomas Graf 	else if (tb[IFLA_IFNAME])
1659a3d12891SEric Dumazet 		dev = __dev_get_by_name(net, ifname);
1660da5e0494SThomas Graf 	else
1661da5e0494SThomas Graf 		goto errout;
16621da177e4SLinus Torvalds 
1663da5e0494SThomas Graf 	if (dev == NULL) {
1664da5e0494SThomas Graf 		err = -ENODEV;
1665da5e0494SThomas Graf 		goto errout;
1666da5e0494SThomas Graf 	}
16671da177e4SLinus Torvalds 
1668e0d087afSEric Dumazet 	err = validate_linkmsg(dev, tb);
1669e0d087afSEric Dumazet 	if (err < 0)
1670a3d12891SEric Dumazet 		goto errout;
1671da5e0494SThomas Graf 
167290f62cf3SEric W. Biederman 	err = do_setlink(skb, dev, ifm, tb, ifname, 0);
1673da5e0494SThomas Graf errout:
16741da177e4SLinus Torvalds 	return err;
16751da177e4SLinus Torvalds }
16761da177e4SLinus Torvalds 
1677661d2967SThomas Graf static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
167838f7b870SPatrick McHardy {
16793b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
168038f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops;
168138f7b870SPatrick McHardy 	struct net_device *dev;
168238f7b870SPatrick McHardy 	struct ifinfomsg *ifm;
168338f7b870SPatrick McHardy 	char ifname[IFNAMSIZ];
168438f7b870SPatrick McHardy 	struct nlattr *tb[IFLA_MAX+1];
168538f7b870SPatrick McHardy 	int err;
1686226bd341SEric Dumazet 	LIST_HEAD(list_kill);
168738f7b870SPatrick McHardy 
168838f7b870SPatrick McHardy 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
168938f7b870SPatrick McHardy 	if (err < 0)
169038f7b870SPatrick McHardy 		return err;
169138f7b870SPatrick McHardy 
169238f7b870SPatrick McHardy 	if (tb[IFLA_IFNAME])
169338f7b870SPatrick McHardy 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
169438f7b870SPatrick McHardy 
169538f7b870SPatrick McHardy 	ifm = nlmsg_data(nlh);
169638f7b870SPatrick McHardy 	if (ifm->ifi_index > 0)
1697881d966bSEric W. Biederman 		dev = __dev_get_by_index(net, ifm->ifi_index);
169838f7b870SPatrick McHardy 	else if (tb[IFLA_IFNAME])
1699881d966bSEric W. Biederman 		dev = __dev_get_by_name(net, ifname);
170038f7b870SPatrick McHardy 	else
170138f7b870SPatrick McHardy 		return -EINVAL;
170238f7b870SPatrick McHardy 
170338f7b870SPatrick McHardy 	if (!dev)
170438f7b870SPatrick McHardy 		return -ENODEV;
170538f7b870SPatrick McHardy 
170638f7b870SPatrick McHardy 	ops = dev->rtnl_link_ops;
170738f7b870SPatrick McHardy 	if (!ops)
170838f7b870SPatrick McHardy 		return -EOPNOTSUPP;
170938f7b870SPatrick McHardy 
1710226bd341SEric Dumazet 	ops->dellink(dev, &list_kill);
1711226bd341SEric Dumazet 	unregister_netdevice_many(&list_kill);
1712226bd341SEric Dumazet 	list_del(&list_kill);
171338f7b870SPatrick McHardy 	return 0;
171438f7b870SPatrick McHardy }
171538f7b870SPatrick McHardy 
17163729d502SPatrick McHardy int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
17173729d502SPatrick McHardy {
17183729d502SPatrick McHardy 	unsigned int old_flags;
17193729d502SPatrick McHardy 	int err;
17203729d502SPatrick McHardy 
17213729d502SPatrick McHardy 	old_flags = dev->flags;
17223729d502SPatrick McHardy 	if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
17233729d502SPatrick McHardy 		err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
17243729d502SPatrick McHardy 		if (err < 0)
17253729d502SPatrick McHardy 			return err;
17263729d502SPatrick McHardy 	}
17273729d502SPatrick McHardy 
17283729d502SPatrick McHardy 	dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
17293729d502SPatrick McHardy 
1730a528c219SNicolas Dichtel 	__dev_notify_flags(dev, old_flags, ~0U);
17313729d502SPatrick McHardy 	return 0;
17323729d502SPatrick McHardy }
17333729d502SPatrick McHardy EXPORT_SYMBOL(rtnl_configure_link);
17343729d502SPatrick McHardy 
1735c0713563SRami Rosen struct net_device *rtnl_create_link(struct net *net,
173681adee47SEric W. Biederman 	char *ifname, const struct rtnl_link_ops *ops, struct nlattr *tb[])
1737e7199288SPavel Emelianov {
1738e7199288SPavel Emelianov 	int err;
1739e7199288SPavel Emelianov 	struct net_device *dev;
1740d40156aaSJiri Pirko 	unsigned int num_tx_queues = 1;
1741d40156aaSJiri Pirko 	unsigned int num_rx_queues = 1;
1742e7199288SPavel Emelianov 
174376ff5cc9SJiri Pirko 	if (tb[IFLA_NUM_TX_QUEUES])
174476ff5cc9SJiri Pirko 		num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
174576ff5cc9SJiri Pirko 	else if (ops->get_num_tx_queues)
1746d40156aaSJiri Pirko 		num_tx_queues = ops->get_num_tx_queues();
174776ff5cc9SJiri Pirko 
174876ff5cc9SJiri Pirko 	if (tb[IFLA_NUM_RX_QUEUES])
174976ff5cc9SJiri Pirko 		num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
175076ff5cc9SJiri Pirko 	else if (ops->get_num_rx_queues)
1751d40156aaSJiri Pirko 		num_rx_queues = ops->get_num_rx_queues();
1752efacb309Sstephen hemminger 
1753e7199288SPavel Emelianov 	err = -ENOMEM;
1754d40156aaSJiri Pirko 	dev = alloc_netdev_mqs(ops->priv_size, ifname, ops->setup,
1755d40156aaSJiri Pirko 			       num_tx_queues, num_rx_queues);
1756e7199288SPavel Emelianov 	if (!dev)
1757e7199288SPavel Emelianov 		goto err;
1758e7199288SPavel Emelianov 
175981adee47SEric W. Biederman 	dev_net_set(dev, net);
176081adee47SEric W. Biederman 	dev->rtnl_link_ops = ops;
17613729d502SPatrick McHardy 	dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
176281adee47SEric W. Biederman 
1763e7199288SPavel Emelianov 	if (tb[IFLA_MTU])
1764e7199288SPavel Emelianov 		dev->mtu = nla_get_u32(tb[IFLA_MTU]);
17652afb9b53SJiri Pirko 	if (tb[IFLA_ADDRESS]) {
1766e7199288SPavel Emelianov 		memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
1767e7199288SPavel Emelianov 				nla_len(tb[IFLA_ADDRESS]));
17682afb9b53SJiri Pirko 		dev->addr_assign_type = NET_ADDR_SET;
17692afb9b53SJiri Pirko 	}
1770e7199288SPavel Emelianov 	if (tb[IFLA_BROADCAST])
1771e7199288SPavel Emelianov 		memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
1772e7199288SPavel Emelianov 				nla_len(tb[IFLA_BROADCAST]));
1773e7199288SPavel Emelianov 	if (tb[IFLA_TXQLEN])
1774e7199288SPavel Emelianov 		dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
1775e7199288SPavel Emelianov 	if (tb[IFLA_OPERSTATE])
177693b2d4a2SDavid S. Miller 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
1777e7199288SPavel Emelianov 	if (tb[IFLA_LINKMODE])
1778e7199288SPavel Emelianov 		dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
1779ffa934f1SPatrick McHardy 	if (tb[IFLA_GROUP])
1780ffa934f1SPatrick McHardy 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
1781e7199288SPavel Emelianov 
1782e7199288SPavel Emelianov 	return dev;
1783e7199288SPavel Emelianov 
1784e7199288SPavel Emelianov err:
1785e7199288SPavel Emelianov 	return ERR_PTR(err);
1786e7199288SPavel Emelianov }
1787e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_create_link);
1788e7199288SPavel Emelianov 
178990f62cf3SEric W. Biederman static int rtnl_group_changelink(const struct sk_buff *skb,
179090f62cf3SEric W. Biederman 		struct net *net, int group,
1791e7ed828fSVlad Dogaru 		struct ifinfomsg *ifm,
1792e7ed828fSVlad Dogaru 		struct nlattr **tb)
1793e7ed828fSVlad Dogaru {
1794e7ed828fSVlad Dogaru 	struct net_device *dev;
1795e7ed828fSVlad Dogaru 	int err;
1796e7ed828fSVlad Dogaru 
1797e7ed828fSVlad Dogaru 	for_each_netdev(net, dev) {
1798e7ed828fSVlad Dogaru 		if (dev->group == group) {
179990f62cf3SEric W. Biederman 			err = do_setlink(skb, dev, ifm, tb, NULL, 0);
1800e7ed828fSVlad Dogaru 			if (err < 0)
1801e7ed828fSVlad Dogaru 				return err;
1802e7ed828fSVlad Dogaru 		}
1803e7ed828fSVlad Dogaru 	}
1804e7ed828fSVlad Dogaru 
1805e7ed828fSVlad Dogaru 	return 0;
1806e7ed828fSVlad Dogaru }
1807e7ed828fSVlad Dogaru 
1808661d2967SThomas Graf static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh)
180938f7b870SPatrick McHardy {
18103b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
181138f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops;
1812ba7d49b1SJiri Pirko 	const struct rtnl_link_ops *m_ops = NULL;
181338f7b870SPatrick McHardy 	struct net_device *dev;
1814ba7d49b1SJiri Pirko 	struct net_device *master_dev = NULL;
181538f7b870SPatrick McHardy 	struct ifinfomsg *ifm;
181638f7b870SPatrick McHardy 	char kind[MODULE_NAME_LEN];
181738f7b870SPatrick McHardy 	char ifname[IFNAMSIZ];
181838f7b870SPatrick McHardy 	struct nlattr *tb[IFLA_MAX+1];
181938f7b870SPatrick McHardy 	struct nlattr *linkinfo[IFLA_INFO_MAX+1];
182038f7b870SPatrick McHardy 	int err;
182138f7b870SPatrick McHardy 
182295a5afcaSJohannes Berg #ifdef CONFIG_MODULES
182338f7b870SPatrick McHardy replay:
18248072f085SThomas Graf #endif
182538f7b870SPatrick McHardy 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
182638f7b870SPatrick McHardy 	if (err < 0)
182738f7b870SPatrick McHardy 		return err;
182838f7b870SPatrick McHardy 
182938f7b870SPatrick McHardy 	if (tb[IFLA_IFNAME])
183038f7b870SPatrick McHardy 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
183138f7b870SPatrick McHardy 	else
183238f7b870SPatrick McHardy 		ifname[0] = '\0';
183338f7b870SPatrick McHardy 
183438f7b870SPatrick McHardy 	ifm = nlmsg_data(nlh);
183538f7b870SPatrick McHardy 	if (ifm->ifi_index > 0)
1836881d966bSEric W. Biederman 		dev = __dev_get_by_index(net, ifm->ifi_index);
1837e7ed828fSVlad Dogaru 	else {
1838e7ed828fSVlad Dogaru 		if (ifname[0])
1839881d966bSEric W. Biederman 			dev = __dev_get_by_name(net, ifname);
184038f7b870SPatrick McHardy 		else
184138f7b870SPatrick McHardy 			dev = NULL;
1842e7ed828fSVlad Dogaru 	}
184338f7b870SPatrick McHardy 
1844ba7d49b1SJiri Pirko 	if (dev) {
1845ba7d49b1SJiri Pirko 		master_dev = netdev_master_upper_dev_get(dev);
1846ba7d49b1SJiri Pirko 		if (master_dev)
1847ba7d49b1SJiri Pirko 			m_ops = master_dev->rtnl_link_ops;
1848ba7d49b1SJiri Pirko 	}
1849ba7d49b1SJiri Pirko 
1850e0d087afSEric Dumazet 	err = validate_linkmsg(dev, tb);
1851e0d087afSEric Dumazet 	if (err < 0)
18521840bb13SThomas Graf 		return err;
18531840bb13SThomas Graf 
185438f7b870SPatrick McHardy 	if (tb[IFLA_LINKINFO]) {
185538f7b870SPatrick McHardy 		err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
185638f7b870SPatrick McHardy 				       tb[IFLA_LINKINFO], ifla_info_policy);
185738f7b870SPatrick McHardy 		if (err < 0)
185838f7b870SPatrick McHardy 			return err;
185938f7b870SPatrick McHardy 	} else
186038f7b870SPatrick McHardy 		memset(linkinfo, 0, sizeof(linkinfo));
186138f7b870SPatrick McHardy 
186238f7b870SPatrick McHardy 	if (linkinfo[IFLA_INFO_KIND]) {
186338f7b870SPatrick McHardy 		nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
186438f7b870SPatrick McHardy 		ops = rtnl_link_ops_get(kind);
186538f7b870SPatrick McHardy 	} else {
186638f7b870SPatrick McHardy 		kind[0] = '\0';
186738f7b870SPatrick McHardy 		ops = NULL;
186838f7b870SPatrick McHardy 	}
186938f7b870SPatrick McHardy 
187038f7b870SPatrick McHardy 	if (1) {
1871ba7d49b1SJiri Pirko 		struct nlattr *attr[ops ? ops->maxtype + 1 : 0];
1872ba7d49b1SJiri Pirko 		struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 0];
1873ba7d49b1SJiri Pirko 		struct nlattr **data = NULL;
1874ba7d49b1SJiri Pirko 		struct nlattr **slave_data = NULL;
187581adee47SEric W. Biederman 		struct net *dest_net;
187638f7b870SPatrick McHardy 
187738f7b870SPatrick McHardy 		if (ops) {
187838f7b870SPatrick McHardy 			if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
187938f7b870SPatrick McHardy 				err = nla_parse_nested(attr, ops->maxtype,
188038f7b870SPatrick McHardy 						       linkinfo[IFLA_INFO_DATA],
188138f7b870SPatrick McHardy 						       ops->policy);
188238f7b870SPatrick McHardy 				if (err < 0)
188338f7b870SPatrick McHardy 					return err;
188438f7b870SPatrick McHardy 				data = attr;
188538f7b870SPatrick McHardy 			}
188638f7b870SPatrick McHardy 			if (ops->validate) {
188738f7b870SPatrick McHardy 				err = ops->validate(tb, data);
188838f7b870SPatrick McHardy 				if (err < 0)
188938f7b870SPatrick McHardy 					return err;
189038f7b870SPatrick McHardy 			}
189138f7b870SPatrick McHardy 		}
189238f7b870SPatrick McHardy 
1893ba7d49b1SJiri Pirko 		if (m_ops) {
1894ba7d49b1SJiri Pirko 			if (m_ops->slave_maxtype &&
1895ba7d49b1SJiri Pirko 			    linkinfo[IFLA_INFO_SLAVE_DATA]) {
1896ba7d49b1SJiri Pirko 				err = nla_parse_nested(slave_attr,
1897ba7d49b1SJiri Pirko 						       m_ops->slave_maxtype,
1898ba7d49b1SJiri Pirko 						       linkinfo[IFLA_INFO_SLAVE_DATA],
1899ba7d49b1SJiri Pirko 						       m_ops->slave_policy);
1900ba7d49b1SJiri Pirko 				if (err < 0)
1901ba7d49b1SJiri Pirko 					return err;
1902ba7d49b1SJiri Pirko 				slave_data = slave_attr;
1903ba7d49b1SJiri Pirko 			}
1904ba7d49b1SJiri Pirko 			if (m_ops->slave_validate) {
1905ba7d49b1SJiri Pirko 				err = m_ops->slave_validate(tb, slave_data);
1906ba7d49b1SJiri Pirko 				if (err < 0)
1907ba7d49b1SJiri Pirko 					return err;
1908ba7d49b1SJiri Pirko 			}
1909ba7d49b1SJiri Pirko 		}
1910ba7d49b1SJiri Pirko 
191138f7b870SPatrick McHardy 		if (dev) {
191238f7b870SPatrick McHardy 			int modified = 0;
191338f7b870SPatrick McHardy 
191438f7b870SPatrick McHardy 			if (nlh->nlmsg_flags & NLM_F_EXCL)
191538f7b870SPatrick McHardy 				return -EEXIST;
191638f7b870SPatrick McHardy 			if (nlh->nlmsg_flags & NLM_F_REPLACE)
191738f7b870SPatrick McHardy 				return -EOPNOTSUPP;
191838f7b870SPatrick McHardy 
191938f7b870SPatrick McHardy 			if (linkinfo[IFLA_INFO_DATA]) {
192038f7b870SPatrick McHardy 				if (!ops || ops != dev->rtnl_link_ops ||
192138f7b870SPatrick McHardy 				    !ops->changelink)
192238f7b870SPatrick McHardy 					return -EOPNOTSUPP;
192338f7b870SPatrick McHardy 
192438f7b870SPatrick McHardy 				err = ops->changelink(dev, tb, data);
192538f7b870SPatrick McHardy 				if (err < 0)
192638f7b870SPatrick McHardy 					return err;
192738f7b870SPatrick McHardy 				modified = 1;
192838f7b870SPatrick McHardy 			}
192938f7b870SPatrick McHardy 
1930ba7d49b1SJiri Pirko 			if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
1931ba7d49b1SJiri Pirko 				if (!m_ops || !m_ops->slave_changelink)
1932ba7d49b1SJiri Pirko 					return -EOPNOTSUPP;
1933ba7d49b1SJiri Pirko 
1934ba7d49b1SJiri Pirko 				err = m_ops->slave_changelink(master_dev, dev,
1935ba7d49b1SJiri Pirko 							      tb, slave_data);
1936ba7d49b1SJiri Pirko 				if (err < 0)
1937ba7d49b1SJiri Pirko 					return err;
1938ba7d49b1SJiri Pirko 				modified = 1;
1939ba7d49b1SJiri Pirko 			}
1940ba7d49b1SJiri Pirko 
194190f62cf3SEric W. Biederman 			return do_setlink(skb, dev, ifm, tb, ifname, modified);
194238f7b870SPatrick McHardy 		}
194338f7b870SPatrick McHardy 
1944ffa934f1SPatrick McHardy 		if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
1945ffa934f1SPatrick McHardy 			if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
194690f62cf3SEric W. Biederman 				return rtnl_group_changelink(skb, net,
1947ffa934f1SPatrick McHardy 						nla_get_u32(tb[IFLA_GROUP]),
1948ffa934f1SPatrick McHardy 						ifm, tb);
194938f7b870SPatrick McHardy 			return -ENODEV;
1950ffa934f1SPatrick McHardy 		}
195138f7b870SPatrick McHardy 
19520e06877cSPatrick McHardy 		if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
195338f7b870SPatrick McHardy 			return -EOPNOTSUPP;
195438f7b870SPatrick McHardy 
195538f7b870SPatrick McHardy 		if (!ops) {
195695a5afcaSJohannes Berg #ifdef CONFIG_MODULES
195738f7b870SPatrick McHardy 			if (kind[0]) {
195838f7b870SPatrick McHardy 				__rtnl_unlock();
195938f7b870SPatrick McHardy 				request_module("rtnl-link-%s", kind);
196038f7b870SPatrick McHardy 				rtnl_lock();
196138f7b870SPatrick McHardy 				ops = rtnl_link_ops_get(kind);
196238f7b870SPatrick McHardy 				if (ops)
196338f7b870SPatrick McHardy 					goto replay;
196438f7b870SPatrick McHardy 			}
196538f7b870SPatrick McHardy #endif
196638f7b870SPatrick McHardy 			return -EOPNOTSUPP;
196738f7b870SPatrick McHardy 		}
196838f7b870SPatrick McHardy 
196938f7b870SPatrick McHardy 		if (!ifname[0])
197038f7b870SPatrick McHardy 			snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
197138f7b870SPatrick McHardy 
197281adee47SEric W. Biederman 		dest_net = rtnl_link_get_net(net, tb);
197313ad1774SEric W. Biederman 		if (IS_ERR(dest_net))
197413ad1774SEric W. Biederman 			return PTR_ERR(dest_net);
197513ad1774SEric W. Biederman 
1976c0713563SRami Rosen 		dev = rtnl_create_link(dest_net, ifname, ops, tb);
19779c7dafbfSPavel Emelyanov 		if (IS_ERR(dev)) {
1978e7199288SPavel Emelianov 			err = PTR_ERR(dev);
19799c7dafbfSPavel Emelyanov 			goto out;
19809c7dafbfSPavel Emelyanov 		}
19819c7dafbfSPavel Emelyanov 
19829c7dafbfSPavel Emelyanov 		dev->ifindex = ifm->ifi_index;
19839c7dafbfSPavel Emelyanov 
19840e0eee24SCong Wang 		if (ops->newlink) {
198581adee47SEric W. Biederman 			err = ops->newlink(net, dev, tb, data);
19860e0eee24SCong Wang 			/* Drivers should call free_netdev() in ->destructor
19870e0eee24SCong Wang 			 * and unregister it on failure so that device could be
19880e0eee24SCong Wang 			 * finally freed in rtnl_unlock.
19890e0eee24SCong Wang 			 */
19900e0eee24SCong Wang 			if (err < 0)
19910e0eee24SCong Wang 				goto out;
19920e0eee24SCong Wang 		} else {
19932d85cba2SPatrick McHardy 			err = register_netdevice(dev);
1994fce9b9beSDan Carpenter 			if (err < 0) {
199538f7b870SPatrick McHardy 				free_netdev(dev);
19963729d502SPatrick McHardy 				goto out;
1997fce9b9beSDan Carpenter 			}
19980e0eee24SCong Wang 		}
19993729d502SPatrick McHardy 		err = rtnl_configure_link(dev, ifm);
20003729d502SPatrick McHardy 		if (err < 0)
20013729d502SPatrick McHardy 			unregister_netdevice(dev);
20023729d502SPatrick McHardy out:
200381adee47SEric W. Biederman 		put_net(dest_net);
200438f7b870SPatrick McHardy 		return err;
200538f7b870SPatrick McHardy 	}
200638f7b870SPatrick McHardy }
200738f7b870SPatrick McHardy 
2008661d2967SThomas Graf static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh)
2009711e2c33SJean Tourrilhes {
20103b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
2011b60c5115SThomas Graf 	struct ifinfomsg *ifm;
2012a3d12891SEric Dumazet 	char ifname[IFNAMSIZ];
2013b60c5115SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
2014b60c5115SThomas Graf 	struct net_device *dev = NULL;
2015b60c5115SThomas Graf 	struct sk_buff *nskb;
2016339bf98fSThomas Graf 	int err;
2017115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
2018711e2c33SJean Tourrilhes 
2019b60c5115SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2020b60c5115SThomas Graf 	if (err < 0)
20219918f230SEric Sesterhenn 		return err;
2022b60c5115SThomas Graf 
2023a3d12891SEric Dumazet 	if (tb[IFLA_IFNAME])
2024a3d12891SEric Dumazet 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2025a3d12891SEric Dumazet 
2026115c9b81SGreg Rose 	if (tb[IFLA_EXT_MASK])
2027115c9b81SGreg Rose 		ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2028115c9b81SGreg Rose 
2029b60c5115SThomas Graf 	ifm = nlmsg_data(nlh);
2030a3d12891SEric Dumazet 	if (ifm->ifi_index > 0)
2031a3d12891SEric Dumazet 		dev = __dev_get_by_index(net, ifm->ifi_index);
2032a3d12891SEric Dumazet 	else if (tb[IFLA_IFNAME])
2033a3d12891SEric Dumazet 		dev = __dev_get_by_name(net, ifname);
2034a3d12891SEric Dumazet 	else
2035b60c5115SThomas Graf 		return -EINVAL;
2036b60c5115SThomas Graf 
2037a3d12891SEric Dumazet 	if (dev == NULL)
2038a3d12891SEric Dumazet 		return -ENODEV;
2039a3d12891SEric Dumazet 
2040115c9b81SGreg Rose 	nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
2041a3d12891SEric Dumazet 	if (nskb == NULL)
2042a3d12891SEric Dumazet 		return -ENOBUFS;
2043711e2c33SJean Tourrilhes 
204415e47304SEric W. Biederman 	err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid,
2045115c9b81SGreg Rose 			       nlh->nlmsg_seq, 0, 0, ext_filter_mask);
204626932566SPatrick McHardy 	if (err < 0) {
204726932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in if_nlmsg_size */
204826932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
204926932566SPatrick McHardy 		kfree_skb(nskb);
2050a3d12891SEric Dumazet 	} else
205115e47304SEric W. Biederman 		err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
2052b60c5115SThomas Graf 
2053711e2c33SJean Tourrilhes 	return err;
2054711e2c33SJean Tourrilhes }
2055711e2c33SJean Tourrilhes 
2056115c9b81SGreg Rose static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
2057c7ac8679SGreg Rose {
2058115c9b81SGreg Rose 	struct net *net = sock_net(skb->sk);
2059115c9b81SGreg Rose 	struct net_device *dev;
2060115c9b81SGreg Rose 	struct nlattr *tb[IFLA_MAX+1];
2061115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
2062115c9b81SGreg Rose 	u16 min_ifinfo_dump_size = 0;
2063115c9b81SGreg Rose 
206488c5b5ceSMichael Riesch 	if (nlmsg_parse(nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
2065a4b64fbeSEric Dumazet 			ifla_policy) >= 0) {
2066115c9b81SGreg Rose 		if (tb[IFLA_EXT_MASK])
2067115c9b81SGreg Rose 			ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2068a4b64fbeSEric Dumazet 	}
2069115c9b81SGreg Rose 
2070115c9b81SGreg Rose 	if (!ext_filter_mask)
2071115c9b81SGreg Rose 		return NLMSG_GOODSIZE;
2072115c9b81SGreg Rose 	/*
2073115c9b81SGreg Rose 	 * traverse the list of net devices and compute the minimum
2074115c9b81SGreg Rose 	 * buffer size based upon the filter mask.
2075115c9b81SGreg Rose 	 */
2076115c9b81SGreg Rose 	list_for_each_entry(dev, &net->dev_base_head, dev_list) {
2077115c9b81SGreg Rose 		min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
2078115c9b81SGreg Rose 					     if_nlmsg_size(dev,
2079115c9b81SGreg Rose 						           ext_filter_mask));
2080115c9b81SGreg Rose 	}
2081115c9b81SGreg Rose 
2082c7ac8679SGreg Rose 	return min_ifinfo_dump_size;
2083c7ac8679SGreg Rose }
2084c7ac8679SGreg Rose 
208542bad1daSAdrian Bunk static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
20861da177e4SLinus Torvalds {
20871da177e4SLinus Torvalds 	int idx;
20881da177e4SLinus Torvalds 	int s_idx = cb->family;
20891da177e4SLinus Torvalds 
20901da177e4SLinus Torvalds 	if (s_idx == 0)
20911da177e4SLinus Torvalds 		s_idx = 1;
209225239ceeSPatrick McHardy 	for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
20931da177e4SLinus Torvalds 		int type = cb->nlh->nlmsg_type-RTM_BASE;
20941da177e4SLinus Torvalds 		if (idx < s_idx || idx == PF_PACKET)
20951da177e4SLinus Torvalds 			continue;
2096e2849863SThomas Graf 		if (rtnl_msg_handlers[idx] == NULL ||
2097e2849863SThomas Graf 		    rtnl_msg_handlers[idx][type].dumpit == NULL)
20981da177e4SLinus Torvalds 			continue;
20990465277fSNicolas Dichtel 		if (idx > s_idx) {
21001da177e4SLinus Torvalds 			memset(&cb->args[0], 0, sizeof(cb->args));
21010465277fSNicolas Dichtel 			cb->prev_seq = 0;
21020465277fSNicolas Dichtel 			cb->seq = 0;
21030465277fSNicolas Dichtel 		}
2104e2849863SThomas Graf 		if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
21051da177e4SLinus Torvalds 			break;
21061da177e4SLinus Torvalds 	}
21071da177e4SLinus Torvalds 	cb->family = idx;
21081da177e4SLinus Torvalds 
21091da177e4SLinus Torvalds 	return skb->len;
21101da177e4SLinus Torvalds }
21111da177e4SLinus Torvalds 
21127f294054SAlexei Starovoitov void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
21137f294054SAlexei Starovoitov 		  gfp_t flags)
21141da177e4SLinus Torvalds {
2115c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
21161da177e4SLinus Torvalds 	struct sk_buff *skb;
21170ec6d3f4SThomas Graf 	int err = -ENOBUFS;
2118c7ac8679SGreg Rose 	size_t if_info_size;
21191da177e4SLinus Torvalds 
21207f294054SAlexei Starovoitov 	skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
21210ec6d3f4SThomas Graf 	if (skb == NULL)
21220ec6d3f4SThomas Graf 		goto errout;
21231da177e4SLinus Torvalds 
2124115c9b81SGreg Rose 	err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0);
212526932566SPatrick McHardy 	if (err < 0) {
212626932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in if_nlmsg_size() */
212726932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
212826932566SPatrick McHardy 		kfree_skb(skb);
212926932566SPatrick McHardy 		goto errout;
213026932566SPatrick McHardy 	}
21317f294054SAlexei Starovoitov 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
21321ce85fe4SPablo Neira Ayuso 	return;
21330ec6d3f4SThomas Graf errout:
21340ec6d3f4SThomas Graf 	if (err < 0)
21354b3da706SEric W. Biederman 		rtnl_set_sk_err(net, RTNLGRP_LINK, err);
21361da177e4SLinus Torvalds }
2137471cb5a3SJiri Pirko EXPORT_SYMBOL(rtmsg_ifinfo);
21381da177e4SLinus Torvalds 
2139d83b0603SJohn Fastabend static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
2140d83b0603SJohn Fastabend 				   struct net_device *dev,
2141d83b0603SJohn Fastabend 				   u8 *addr, u32 pid, u32 seq,
21421c104a6bSNicolas Dichtel 				   int type, unsigned int flags,
21431c104a6bSNicolas Dichtel 				   int nlflags)
2144d83b0603SJohn Fastabend {
2145d83b0603SJohn Fastabend 	struct nlmsghdr *nlh;
2146d83b0603SJohn Fastabend 	struct ndmsg *ndm;
2147d83b0603SJohn Fastabend 
21481c104a6bSNicolas Dichtel 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
2149d83b0603SJohn Fastabend 	if (!nlh)
2150d83b0603SJohn Fastabend 		return -EMSGSIZE;
2151d83b0603SJohn Fastabend 
2152d83b0603SJohn Fastabend 	ndm = nlmsg_data(nlh);
2153d83b0603SJohn Fastabend 	ndm->ndm_family  = AF_BRIDGE;
2154d83b0603SJohn Fastabend 	ndm->ndm_pad1	 = 0;
2155d83b0603SJohn Fastabend 	ndm->ndm_pad2    = 0;
2156d83b0603SJohn Fastabend 	ndm->ndm_flags	 = flags;
2157d83b0603SJohn Fastabend 	ndm->ndm_type	 = 0;
2158d83b0603SJohn Fastabend 	ndm->ndm_ifindex = dev->ifindex;
2159d83b0603SJohn Fastabend 	ndm->ndm_state   = NUD_PERMANENT;
2160d83b0603SJohn Fastabend 
2161d83b0603SJohn Fastabend 	if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
2162d83b0603SJohn Fastabend 		goto nla_put_failure;
2163d83b0603SJohn Fastabend 
2164d83b0603SJohn Fastabend 	return nlmsg_end(skb, nlh);
2165d83b0603SJohn Fastabend 
2166d83b0603SJohn Fastabend nla_put_failure:
2167d83b0603SJohn Fastabend 	nlmsg_cancel(skb, nlh);
2168d83b0603SJohn Fastabend 	return -EMSGSIZE;
2169d83b0603SJohn Fastabend }
2170d83b0603SJohn Fastabend 
21713ff661c3SJohn Fastabend static inline size_t rtnl_fdb_nlmsg_size(void)
21723ff661c3SJohn Fastabend {
21733ff661c3SJohn Fastabend 	return NLMSG_ALIGN(sizeof(struct ndmsg)) + nla_total_size(ETH_ALEN);
21743ff661c3SJohn Fastabend }
21753ff661c3SJohn Fastabend 
21763ff661c3SJohn Fastabend static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, int type)
21773ff661c3SJohn Fastabend {
21783ff661c3SJohn Fastabend 	struct net *net = dev_net(dev);
21793ff661c3SJohn Fastabend 	struct sk_buff *skb;
21803ff661c3SJohn Fastabend 	int err = -ENOBUFS;
21813ff661c3SJohn Fastabend 
21823ff661c3SJohn Fastabend 	skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
21833ff661c3SJohn Fastabend 	if (!skb)
21843ff661c3SJohn Fastabend 		goto errout;
21853ff661c3SJohn Fastabend 
21861c104a6bSNicolas Dichtel 	err = nlmsg_populate_fdb_fill(skb, dev, addr, 0, 0, type, NTF_SELF, 0);
21873ff661c3SJohn Fastabend 	if (err < 0) {
21883ff661c3SJohn Fastabend 		kfree_skb(skb);
21893ff661c3SJohn Fastabend 		goto errout;
21903ff661c3SJohn Fastabend 	}
21913ff661c3SJohn Fastabend 
21923ff661c3SJohn Fastabend 	rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
21933ff661c3SJohn Fastabend 	return;
21943ff661c3SJohn Fastabend errout:
21953ff661c3SJohn Fastabend 	rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
21963ff661c3SJohn Fastabend }
21973ff661c3SJohn Fastabend 
2198090096bfSVlad Yasevich /**
2199090096bfSVlad Yasevich  * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
2200090096bfSVlad Yasevich  */
2201090096bfSVlad Yasevich int ndo_dflt_fdb_add(struct ndmsg *ndm,
2202090096bfSVlad Yasevich 		     struct nlattr *tb[],
2203090096bfSVlad Yasevich 		     struct net_device *dev,
2204090096bfSVlad Yasevich 		     const unsigned char *addr,
2205090096bfSVlad Yasevich 		     u16 flags)
2206090096bfSVlad Yasevich {
2207090096bfSVlad Yasevich 	int err = -EINVAL;
2208090096bfSVlad Yasevich 
2209090096bfSVlad Yasevich 	/* If aging addresses are supported device will need to
2210090096bfSVlad Yasevich 	 * implement its own handler for this.
2211090096bfSVlad Yasevich 	 */
2212090096bfSVlad Yasevich 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
2213090096bfSVlad Yasevich 		pr_info("%s: FDB only supports static addresses\n", dev->name);
2214090096bfSVlad Yasevich 		return err;
2215090096bfSVlad Yasevich 	}
2216090096bfSVlad Yasevich 
2217090096bfSVlad Yasevich 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2218090096bfSVlad Yasevich 		err = dev_uc_add_excl(dev, addr);
2219090096bfSVlad Yasevich 	else if (is_multicast_ether_addr(addr))
2220090096bfSVlad Yasevich 		err = dev_mc_add_excl(dev, addr);
2221090096bfSVlad Yasevich 
2222090096bfSVlad Yasevich 	/* Only return duplicate errors if NLM_F_EXCL is set */
2223090096bfSVlad Yasevich 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
2224090096bfSVlad Yasevich 		err = 0;
2225090096bfSVlad Yasevich 
2226090096bfSVlad Yasevich 	return err;
2227090096bfSVlad Yasevich }
2228090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_add);
2229090096bfSVlad Yasevich 
2230661d2967SThomas Graf static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh)
223177162022SJohn Fastabend {
223277162022SJohn Fastabend 	struct net *net = sock_net(skb->sk);
223377162022SJohn Fastabend 	struct ndmsg *ndm;
223477162022SJohn Fastabend 	struct nlattr *tb[NDA_MAX+1];
223577162022SJohn Fastabend 	struct net_device *dev;
223677162022SJohn Fastabend 	u8 *addr;
223777162022SJohn Fastabend 	int err;
223877162022SJohn Fastabend 
223977162022SJohn Fastabend 	err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
224077162022SJohn Fastabend 	if (err < 0)
224177162022SJohn Fastabend 		return err;
224277162022SJohn Fastabend 
224377162022SJohn Fastabend 	ndm = nlmsg_data(nlh);
224477162022SJohn Fastabend 	if (ndm->ndm_ifindex == 0) {
224577162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
224677162022SJohn Fastabend 		return -EINVAL;
224777162022SJohn Fastabend 	}
224877162022SJohn Fastabend 
224977162022SJohn Fastabend 	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
225077162022SJohn Fastabend 	if (dev == NULL) {
225177162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
225277162022SJohn Fastabend 		return -ENODEV;
225377162022SJohn Fastabend 	}
225477162022SJohn Fastabend 
225577162022SJohn Fastabend 	if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
225677162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
225777162022SJohn Fastabend 		return -EINVAL;
225877162022SJohn Fastabend 	}
225977162022SJohn Fastabend 
226077162022SJohn Fastabend 	addr = nla_data(tb[NDA_LLADDR]);
226177162022SJohn Fastabend 
226277162022SJohn Fastabend 	err = -EOPNOTSUPP;
226377162022SJohn Fastabend 
226477162022SJohn Fastabend 	/* Support fdb on master device the net/bridge default case */
226577162022SJohn Fastabend 	if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
226677162022SJohn Fastabend 	    (dev->priv_flags & IFF_BRIDGE_PORT)) {
2267898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2268898e5061SJiri Pirko 		const struct net_device_ops *ops = br_dev->netdev_ops;
2269898e5061SJiri Pirko 
2270898e5061SJiri Pirko 		err = ops->ndo_fdb_add(ndm, tb, dev, addr, nlh->nlmsg_flags);
227177162022SJohn Fastabend 		if (err)
227277162022SJohn Fastabend 			goto out;
227377162022SJohn Fastabend 		else
227477162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_MASTER;
227577162022SJohn Fastabend 	}
227677162022SJohn Fastabend 
227777162022SJohn Fastabend 	/* Embedded bridge, macvlan, and any other device support */
2278090096bfSVlad Yasevich 	if ((ndm->ndm_flags & NTF_SELF)) {
2279090096bfSVlad Yasevich 		if (dev->netdev_ops->ndo_fdb_add)
2280090096bfSVlad Yasevich 			err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
2281090096bfSVlad Yasevich 							   nlh->nlmsg_flags);
2282090096bfSVlad Yasevich 		else
2283090096bfSVlad Yasevich 			err = ndo_dflt_fdb_add(ndm, tb, dev, addr,
228477162022SJohn Fastabend 					       nlh->nlmsg_flags);
228577162022SJohn Fastabend 
22863ff661c3SJohn Fastabend 		if (!err) {
22873ff661c3SJohn Fastabend 			rtnl_fdb_notify(dev, addr, RTM_NEWNEIGH);
228877162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_SELF;
228977162022SJohn Fastabend 		}
22903ff661c3SJohn Fastabend 	}
229177162022SJohn Fastabend out:
229277162022SJohn Fastabend 	return err;
229377162022SJohn Fastabend }
229477162022SJohn Fastabend 
2295090096bfSVlad Yasevich /**
2296090096bfSVlad Yasevich  * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
2297090096bfSVlad Yasevich  */
2298090096bfSVlad Yasevich int ndo_dflt_fdb_del(struct ndmsg *ndm,
2299090096bfSVlad Yasevich 		     struct nlattr *tb[],
2300090096bfSVlad Yasevich 		     struct net_device *dev,
2301090096bfSVlad Yasevich 		     const unsigned char *addr)
2302090096bfSVlad Yasevich {
2303090096bfSVlad Yasevich 	int err = -EOPNOTSUPP;
2304090096bfSVlad Yasevich 
2305090096bfSVlad Yasevich 	/* If aging addresses are supported device will need to
2306090096bfSVlad Yasevich 	 * implement its own handler for this.
2307090096bfSVlad Yasevich 	 */
230864535993SSridhar Samudrala 	if (!(ndm->ndm_state & NUD_PERMANENT)) {
2309090096bfSVlad Yasevich 		pr_info("%s: FDB only supports static addresses\n", dev->name);
2310090096bfSVlad Yasevich 		return -EINVAL;
2311090096bfSVlad Yasevich 	}
2312090096bfSVlad Yasevich 
2313090096bfSVlad Yasevich 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2314090096bfSVlad Yasevich 		err = dev_uc_del(dev, addr);
2315090096bfSVlad Yasevich 	else if (is_multicast_ether_addr(addr))
2316090096bfSVlad Yasevich 		err = dev_mc_del(dev, addr);
2317090096bfSVlad Yasevich 	else
2318090096bfSVlad Yasevich 		err = -EINVAL;
2319090096bfSVlad Yasevich 
2320090096bfSVlad Yasevich 	return err;
2321090096bfSVlad Yasevich }
2322090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_del);
2323090096bfSVlad Yasevich 
2324661d2967SThomas Graf static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh)
232577162022SJohn Fastabend {
232677162022SJohn Fastabend 	struct net *net = sock_net(skb->sk);
232777162022SJohn Fastabend 	struct ndmsg *ndm;
23281690be63SVlad Yasevich 	struct nlattr *tb[NDA_MAX+1];
232977162022SJohn Fastabend 	struct net_device *dev;
233077162022SJohn Fastabend 	int err = -EINVAL;
233177162022SJohn Fastabend 	__u8 *addr;
233277162022SJohn Fastabend 
233390f62cf3SEric W. Biederman 	if (!netlink_capable(skb, CAP_NET_ADMIN))
23341690be63SVlad Yasevich 		return -EPERM;
23351690be63SVlad Yasevich 
23361690be63SVlad Yasevich 	err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
23371690be63SVlad Yasevich 	if (err < 0)
23381690be63SVlad Yasevich 		return err;
233977162022SJohn Fastabend 
234077162022SJohn Fastabend 	ndm = nlmsg_data(nlh);
234177162022SJohn Fastabend 	if (ndm->ndm_ifindex == 0) {
234277162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
234377162022SJohn Fastabend 		return -EINVAL;
234477162022SJohn Fastabend 	}
234577162022SJohn Fastabend 
234677162022SJohn Fastabend 	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
234777162022SJohn Fastabend 	if (dev == NULL) {
234877162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
234977162022SJohn Fastabend 		return -ENODEV;
235077162022SJohn Fastabend 	}
235177162022SJohn Fastabend 
23521690be63SVlad Yasevich 	if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
23531690be63SVlad Yasevich 		pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n");
235477162022SJohn Fastabend 		return -EINVAL;
235577162022SJohn Fastabend 	}
235677162022SJohn Fastabend 
23571690be63SVlad Yasevich 	addr = nla_data(tb[NDA_LLADDR]);
23581690be63SVlad Yasevich 
235977162022SJohn Fastabend 	err = -EOPNOTSUPP;
236077162022SJohn Fastabend 
236177162022SJohn Fastabend 	/* Support fdb on master device the net/bridge default case */
236277162022SJohn Fastabend 	if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
236377162022SJohn Fastabend 	    (dev->priv_flags & IFF_BRIDGE_PORT)) {
2364898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2365898e5061SJiri Pirko 		const struct net_device_ops *ops = br_dev->netdev_ops;
236677162022SJohn Fastabend 
2367898e5061SJiri Pirko 		if (ops->ndo_fdb_del)
23681690be63SVlad Yasevich 			err = ops->ndo_fdb_del(ndm, tb, dev, addr);
236977162022SJohn Fastabend 
237077162022SJohn Fastabend 		if (err)
237177162022SJohn Fastabend 			goto out;
237277162022SJohn Fastabend 		else
237377162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_MASTER;
237477162022SJohn Fastabend 	}
237577162022SJohn Fastabend 
237677162022SJohn Fastabend 	/* Embedded bridge, macvlan, and any other device support */
2377090096bfSVlad Yasevich 	if (ndm->ndm_flags & NTF_SELF) {
2378090096bfSVlad Yasevich 		if (dev->netdev_ops->ndo_fdb_del)
23791690be63SVlad Yasevich 			err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr);
2380090096bfSVlad Yasevich 		else
2381090096bfSVlad Yasevich 			err = ndo_dflt_fdb_del(ndm, tb, dev, addr);
238277162022SJohn Fastabend 
23833ff661c3SJohn Fastabend 		if (!err) {
23843ff661c3SJohn Fastabend 			rtnl_fdb_notify(dev, addr, RTM_DELNEIGH);
238577162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_SELF;
238677162022SJohn Fastabend 		}
23873ff661c3SJohn Fastabend 	}
238877162022SJohn Fastabend out:
238977162022SJohn Fastabend 	return err;
239077162022SJohn Fastabend }
239177162022SJohn Fastabend 
2392d83b0603SJohn Fastabend static int nlmsg_populate_fdb(struct sk_buff *skb,
2393d83b0603SJohn Fastabend 			      struct netlink_callback *cb,
2394d83b0603SJohn Fastabend 			      struct net_device *dev,
2395d83b0603SJohn Fastabend 			      int *idx,
2396d83b0603SJohn Fastabend 			      struct netdev_hw_addr_list *list)
2397d83b0603SJohn Fastabend {
2398d83b0603SJohn Fastabend 	struct netdev_hw_addr *ha;
2399d83b0603SJohn Fastabend 	int err;
240015e47304SEric W. Biederman 	u32 portid, seq;
2401d83b0603SJohn Fastabend 
240215e47304SEric W. Biederman 	portid = NETLINK_CB(cb->skb).portid;
2403d83b0603SJohn Fastabend 	seq = cb->nlh->nlmsg_seq;
2404d83b0603SJohn Fastabend 
2405d83b0603SJohn Fastabend 	list_for_each_entry(ha, &list->list, list) {
2406d83b0603SJohn Fastabend 		if (*idx < cb->args[0])
2407d83b0603SJohn Fastabend 			goto skip;
2408d83b0603SJohn Fastabend 
2409d83b0603SJohn Fastabend 		err = nlmsg_populate_fdb_fill(skb, dev, ha->addr,
2410a7a558feSJohn Fastabend 					      portid, seq,
24111c104a6bSNicolas Dichtel 					      RTM_NEWNEIGH, NTF_SELF,
24121c104a6bSNicolas Dichtel 					      NLM_F_MULTI);
2413d83b0603SJohn Fastabend 		if (err < 0)
2414d83b0603SJohn Fastabend 			return err;
2415d83b0603SJohn Fastabend skip:
2416d83b0603SJohn Fastabend 		*idx += 1;
2417d83b0603SJohn Fastabend 	}
2418d83b0603SJohn Fastabend 	return 0;
2419d83b0603SJohn Fastabend }
2420d83b0603SJohn Fastabend 
2421d83b0603SJohn Fastabend /**
24222c53040fSBen Hutchings  * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
2423d83b0603SJohn Fastabend  * @nlh: netlink message header
2424d83b0603SJohn Fastabend  * @dev: netdevice
2425d83b0603SJohn Fastabend  *
2426d83b0603SJohn Fastabend  * Default netdevice operation to dump the existing unicast address list.
242791f3e7b1SJohn Fastabend  * Returns number of addresses from list put in skb.
2428d83b0603SJohn Fastabend  */
2429d83b0603SJohn Fastabend int ndo_dflt_fdb_dump(struct sk_buff *skb,
2430d83b0603SJohn Fastabend 		      struct netlink_callback *cb,
2431d83b0603SJohn Fastabend 		      struct net_device *dev,
2432d83b0603SJohn Fastabend 		      int idx)
2433d83b0603SJohn Fastabend {
2434d83b0603SJohn Fastabend 	int err;
2435d83b0603SJohn Fastabend 
2436d83b0603SJohn Fastabend 	netif_addr_lock_bh(dev);
2437d83b0603SJohn Fastabend 	err = nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->uc);
2438d83b0603SJohn Fastabend 	if (err)
2439d83b0603SJohn Fastabend 		goto out;
2440d83b0603SJohn Fastabend 	nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->mc);
2441d83b0603SJohn Fastabend out:
2442d83b0603SJohn Fastabend 	netif_addr_unlock_bh(dev);
2443d83b0603SJohn Fastabend 	return idx;
2444d83b0603SJohn Fastabend }
2445d83b0603SJohn Fastabend EXPORT_SYMBOL(ndo_dflt_fdb_dump);
2446d83b0603SJohn Fastabend 
244777162022SJohn Fastabend static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
244877162022SJohn Fastabend {
244977162022SJohn Fastabend 	int idx = 0;
245077162022SJohn Fastabend 	struct net *net = sock_net(skb->sk);
245177162022SJohn Fastabend 	struct net_device *dev;
245277162022SJohn Fastabend 
245377162022SJohn Fastabend 	rcu_read_lock();
245477162022SJohn Fastabend 	for_each_netdev_rcu(net, dev) {
245577162022SJohn Fastabend 		if (dev->priv_flags & IFF_BRIDGE_PORT) {
2456898e5061SJiri Pirko 			struct net_device *br_dev;
2457898e5061SJiri Pirko 			const struct net_device_ops *ops;
245877162022SJohn Fastabend 
2459898e5061SJiri Pirko 			br_dev = netdev_master_upper_dev_get(dev);
2460898e5061SJiri Pirko 			ops = br_dev->netdev_ops;
246177162022SJohn Fastabend 			if (ops->ndo_fdb_dump)
246277162022SJohn Fastabend 				idx = ops->ndo_fdb_dump(skb, cb, dev, idx);
246377162022SJohn Fastabend 		}
246477162022SJohn Fastabend 
246577162022SJohn Fastabend 		if (dev->netdev_ops->ndo_fdb_dump)
246677162022SJohn Fastabend 			idx = dev->netdev_ops->ndo_fdb_dump(skb, cb, dev, idx);
2467090096bfSVlad Yasevich 		else
246891f3e7b1SJohn Fastabend 			idx = ndo_dflt_fdb_dump(skb, cb, dev, idx);
246977162022SJohn Fastabend 	}
247077162022SJohn Fastabend 	rcu_read_unlock();
247177162022SJohn Fastabend 
247277162022SJohn Fastabend 	cb->args[0] = idx;
247377162022SJohn Fastabend 	return skb->len;
247477162022SJohn Fastabend }
247577162022SJohn Fastabend 
2476815cccbfSJohn Fastabend int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
2477815cccbfSJohn Fastabend 			    struct net_device *dev, u16 mode)
2478815cccbfSJohn Fastabend {
2479815cccbfSJohn Fastabend 	struct nlmsghdr *nlh;
2480815cccbfSJohn Fastabend 	struct ifinfomsg *ifm;
2481815cccbfSJohn Fastabend 	struct nlattr *br_afspec;
2482815cccbfSJohn Fastabend 	u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
2483898e5061SJiri Pirko 	struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2484815cccbfSJohn Fastabend 
2485815cccbfSJohn Fastabend 	nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), NLM_F_MULTI);
2486815cccbfSJohn Fastabend 	if (nlh == NULL)
2487815cccbfSJohn Fastabend 		return -EMSGSIZE;
2488815cccbfSJohn Fastabend 
2489815cccbfSJohn Fastabend 	ifm = nlmsg_data(nlh);
2490815cccbfSJohn Fastabend 	ifm->ifi_family = AF_BRIDGE;
2491815cccbfSJohn Fastabend 	ifm->__ifi_pad = 0;
2492815cccbfSJohn Fastabend 	ifm->ifi_type = dev->type;
2493815cccbfSJohn Fastabend 	ifm->ifi_index = dev->ifindex;
2494815cccbfSJohn Fastabend 	ifm->ifi_flags = dev_get_flags(dev);
2495815cccbfSJohn Fastabend 	ifm->ifi_change = 0;
2496815cccbfSJohn Fastabend 
2497815cccbfSJohn Fastabend 
2498815cccbfSJohn Fastabend 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
2499815cccbfSJohn Fastabend 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
2500815cccbfSJohn Fastabend 	    nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
2501898e5061SJiri Pirko 	    (br_dev &&
2502898e5061SJiri Pirko 	     nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
2503815cccbfSJohn Fastabend 	    (dev->addr_len &&
2504815cccbfSJohn Fastabend 	     nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
2505815cccbfSJohn Fastabend 	    (dev->ifindex != dev->iflink &&
2506815cccbfSJohn Fastabend 	     nla_put_u32(skb, IFLA_LINK, dev->iflink)))
2507815cccbfSJohn Fastabend 		goto nla_put_failure;
2508815cccbfSJohn Fastabend 
2509815cccbfSJohn Fastabend 	br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
2510815cccbfSJohn Fastabend 	if (!br_afspec)
2511815cccbfSJohn Fastabend 		goto nla_put_failure;
2512815cccbfSJohn Fastabend 
2513815cccbfSJohn Fastabend 	if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF) ||
2514815cccbfSJohn Fastabend 	    nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
2515815cccbfSJohn Fastabend 		nla_nest_cancel(skb, br_afspec);
2516815cccbfSJohn Fastabend 		goto nla_put_failure;
2517815cccbfSJohn Fastabend 	}
2518815cccbfSJohn Fastabend 	nla_nest_end(skb, br_afspec);
2519815cccbfSJohn Fastabend 
2520815cccbfSJohn Fastabend 	return nlmsg_end(skb, nlh);
2521815cccbfSJohn Fastabend nla_put_failure:
2522815cccbfSJohn Fastabend 	nlmsg_cancel(skb, nlh);
2523815cccbfSJohn Fastabend 	return -EMSGSIZE;
2524815cccbfSJohn Fastabend }
2525815cccbfSJohn Fastabend EXPORT_SYMBOL(ndo_dflt_bridge_getlink);
2526815cccbfSJohn Fastabend 
2527e5a55a89SJohn Fastabend static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
2528e5a55a89SJohn Fastabend {
2529e5a55a89SJohn Fastabend 	struct net *net = sock_net(skb->sk);
2530e5a55a89SJohn Fastabend 	struct net_device *dev;
2531e5a55a89SJohn Fastabend 	int idx = 0;
2532e5a55a89SJohn Fastabend 	u32 portid = NETLINK_CB(cb->skb).portid;
2533e5a55a89SJohn Fastabend 	u32 seq = cb->nlh->nlmsg_seq;
25346cbdceebSVlad Yasevich 	struct nlattr *extfilt;
25356cbdceebSVlad Yasevich 	u32 filter_mask = 0;
25366cbdceebSVlad Yasevich 
25373e805ad2SAsbjoern Sloth Toennesen 	extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
25386cbdceebSVlad Yasevich 				  IFLA_EXT_MASK);
25396cbdceebSVlad Yasevich 	if (extfilt)
25406cbdceebSVlad Yasevich 		filter_mask = nla_get_u32(extfilt);
2541e5a55a89SJohn Fastabend 
2542e5a55a89SJohn Fastabend 	rcu_read_lock();
2543e5a55a89SJohn Fastabend 	for_each_netdev_rcu(net, dev) {
2544e5a55a89SJohn Fastabend 		const struct net_device_ops *ops = dev->netdev_ops;
2545898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2546e5a55a89SJohn Fastabend 
2547898e5061SJiri Pirko 		if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
254825b1e679SBen Hutchings 			if (idx >= cb->args[0] &&
2549898e5061SJiri Pirko 			    br_dev->netdev_ops->ndo_bridge_getlink(
25506cbdceebSVlad Yasevich 				    skb, portid, seq, dev, filter_mask) < 0)
2551e5a55a89SJohn Fastabend 				break;
2552e5a55a89SJohn Fastabend 			idx++;
2553e5a55a89SJohn Fastabend 		}
2554e5a55a89SJohn Fastabend 
2555e5a55a89SJohn Fastabend 		if (ops->ndo_bridge_getlink) {
255625b1e679SBen Hutchings 			if (idx >= cb->args[0] &&
25576cbdceebSVlad Yasevich 			    ops->ndo_bridge_getlink(skb, portid, seq, dev,
25586cbdceebSVlad Yasevich 						    filter_mask) < 0)
2559e5a55a89SJohn Fastabend 				break;
2560e5a55a89SJohn Fastabend 			idx++;
2561e5a55a89SJohn Fastabend 		}
2562e5a55a89SJohn Fastabend 	}
2563e5a55a89SJohn Fastabend 	rcu_read_unlock();
2564e5a55a89SJohn Fastabend 	cb->args[0] = idx;
2565e5a55a89SJohn Fastabend 
2566e5a55a89SJohn Fastabend 	return skb->len;
2567e5a55a89SJohn Fastabend }
2568e5a55a89SJohn Fastabend 
25692469ffd7SJohn Fastabend static inline size_t bridge_nlmsg_size(void)
25702469ffd7SJohn Fastabend {
25712469ffd7SJohn Fastabend 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
25722469ffd7SJohn Fastabend 		+ nla_total_size(IFNAMSIZ)	/* IFLA_IFNAME */
25732469ffd7SJohn Fastabend 		+ nla_total_size(MAX_ADDR_LEN)	/* IFLA_ADDRESS */
25742469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_MASTER */
25752469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_MTU */
25762469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_LINK */
25772469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_OPERSTATE */
25782469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u8))	/* IFLA_PROTINFO */
25792469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(struct nlattr))	/* IFLA_AF_SPEC */
25802469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u16))	/* IFLA_BRIDGE_FLAGS */
25812469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u16));	/* IFLA_BRIDGE_MODE */
25822469ffd7SJohn Fastabend }
25832469ffd7SJohn Fastabend 
25842469ffd7SJohn Fastabend static int rtnl_bridge_notify(struct net_device *dev, u16 flags)
25852469ffd7SJohn Fastabend {
25862469ffd7SJohn Fastabend 	struct net *net = dev_net(dev);
2587898e5061SJiri Pirko 	struct net_device *br_dev = netdev_master_upper_dev_get(dev);
25882469ffd7SJohn Fastabend 	struct sk_buff *skb;
25892469ffd7SJohn Fastabend 	int err = -EOPNOTSUPP;
25902469ffd7SJohn Fastabend 
25912469ffd7SJohn Fastabend 	skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
25922469ffd7SJohn Fastabend 	if (!skb) {
25932469ffd7SJohn Fastabend 		err = -ENOMEM;
25942469ffd7SJohn Fastabend 		goto errout;
25952469ffd7SJohn Fastabend 	}
25962469ffd7SJohn Fastabend 
2597c38e01b8SJohn Fastabend 	if ((!flags || (flags & BRIDGE_FLAGS_MASTER)) &&
2598898e5061SJiri Pirko 	    br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
25996cbdceebSVlad Yasevich 		err = br_dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0);
26002469ffd7SJohn Fastabend 		if (err < 0)
26012469ffd7SJohn Fastabend 			goto errout;
2602c38e01b8SJohn Fastabend 	}
2603c38e01b8SJohn Fastabend 
2604c38e01b8SJohn Fastabend 	if ((flags & BRIDGE_FLAGS_SELF) &&
2605c38e01b8SJohn Fastabend 	    dev->netdev_ops->ndo_bridge_getlink) {
26066cbdceebSVlad Yasevich 		err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0);
2607c38e01b8SJohn Fastabend 		if (err < 0)
2608c38e01b8SJohn Fastabend 			goto errout;
2609c38e01b8SJohn Fastabend 	}
26102469ffd7SJohn Fastabend 
26112469ffd7SJohn Fastabend 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
26122469ffd7SJohn Fastabend 	return 0;
26132469ffd7SJohn Fastabend errout:
26142469ffd7SJohn Fastabend 	WARN_ON(err == -EMSGSIZE);
26152469ffd7SJohn Fastabend 	kfree_skb(skb);
26162469ffd7SJohn Fastabend 	rtnl_set_sk_err(net, RTNLGRP_LINK, err);
26172469ffd7SJohn Fastabend 	return err;
26182469ffd7SJohn Fastabend }
26192469ffd7SJohn Fastabend 
2620661d2967SThomas Graf static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
2621e5a55a89SJohn Fastabend {
2622e5a55a89SJohn Fastabend 	struct net *net = sock_net(skb->sk);
2623e5a55a89SJohn Fastabend 	struct ifinfomsg *ifm;
2624e5a55a89SJohn Fastabend 	struct net_device *dev;
26252469ffd7SJohn Fastabend 	struct nlattr *br_spec, *attr = NULL;
26262469ffd7SJohn Fastabend 	int rem, err = -EOPNOTSUPP;
2627c38e01b8SJohn Fastabend 	u16 oflags, flags = 0;
2628c38e01b8SJohn Fastabend 	bool have_flags = false;
2629e5a55a89SJohn Fastabend 
2630e5a55a89SJohn Fastabend 	if (nlmsg_len(nlh) < sizeof(*ifm))
2631e5a55a89SJohn Fastabend 		return -EINVAL;
2632e5a55a89SJohn Fastabend 
2633e5a55a89SJohn Fastabend 	ifm = nlmsg_data(nlh);
2634e5a55a89SJohn Fastabend 	if (ifm->ifi_family != AF_BRIDGE)
2635e5a55a89SJohn Fastabend 		return -EPFNOSUPPORT;
2636e5a55a89SJohn Fastabend 
2637e5a55a89SJohn Fastabend 	dev = __dev_get_by_index(net, ifm->ifi_index);
2638e5a55a89SJohn Fastabend 	if (!dev) {
2639e5a55a89SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
2640e5a55a89SJohn Fastabend 		return -ENODEV;
2641e5a55a89SJohn Fastabend 	}
2642e5a55a89SJohn Fastabend 
26432469ffd7SJohn Fastabend 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
26442469ffd7SJohn Fastabend 	if (br_spec) {
26452469ffd7SJohn Fastabend 		nla_for_each_nested(attr, br_spec, rem) {
26462469ffd7SJohn Fastabend 			if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
2647c38e01b8SJohn Fastabend 				have_flags = true;
26482469ffd7SJohn Fastabend 				flags = nla_get_u16(attr);
26492469ffd7SJohn Fastabend 				break;
26502469ffd7SJohn Fastabend 			}
26512469ffd7SJohn Fastabend 		}
26522469ffd7SJohn Fastabend 	}
26532469ffd7SJohn Fastabend 
2654c38e01b8SJohn Fastabend 	oflags = flags;
2655c38e01b8SJohn Fastabend 
26562469ffd7SJohn Fastabend 	if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
2657898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2658898e5061SJiri Pirko 
2659898e5061SJiri Pirko 		if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
26602469ffd7SJohn Fastabend 			err = -EOPNOTSUPP;
2661e5a55a89SJohn Fastabend 			goto out;
2662e5a55a89SJohn Fastabend 		}
2663e5a55a89SJohn Fastabend 
2664898e5061SJiri Pirko 		err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh);
26652469ffd7SJohn Fastabend 		if (err)
26662469ffd7SJohn Fastabend 			goto out;
26672469ffd7SJohn Fastabend 
26682469ffd7SJohn Fastabend 		flags &= ~BRIDGE_FLAGS_MASTER;
26692469ffd7SJohn Fastabend 	}
26702469ffd7SJohn Fastabend 
26712469ffd7SJohn Fastabend 	if ((flags & BRIDGE_FLAGS_SELF)) {
26722469ffd7SJohn Fastabend 		if (!dev->netdev_ops->ndo_bridge_setlink)
26732469ffd7SJohn Fastabend 			err = -EOPNOTSUPP;
26742469ffd7SJohn Fastabend 		else
2675e5a55a89SJohn Fastabend 			err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh);
2676e5a55a89SJohn Fastabend 
26772469ffd7SJohn Fastabend 		if (!err)
26782469ffd7SJohn Fastabend 			flags &= ~BRIDGE_FLAGS_SELF;
26792469ffd7SJohn Fastabend 	}
26802469ffd7SJohn Fastabend 
2681c38e01b8SJohn Fastabend 	if (have_flags)
26822469ffd7SJohn Fastabend 		memcpy(nla_data(attr), &flags, sizeof(flags));
26832469ffd7SJohn Fastabend 	/* Generate event to notify upper layer of bridge change */
26842469ffd7SJohn Fastabend 	if (!err)
2685c38e01b8SJohn Fastabend 		err = rtnl_bridge_notify(dev, oflags);
2686e5a55a89SJohn Fastabend out:
2687e5a55a89SJohn Fastabend 	return err;
2688e5a55a89SJohn Fastabend }
2689e5a55a89SJohn Fastabend 
2690661d2967SThomas Graf static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
2691407af329SVlad Yasevich {
2692407af329SVlad Yasevich 	struct net *net = sock_net(skb->sk);
2693407af329SVlad Yasevich 	struct ifinfomsg *ifm;
2694407af329SVlad Yasevich 	struct net_device *dev;
2695407af329SVlad Yasevich 	struct nlattr *br_spec, *attr = NULL;
2696407af329SVlad Yasevich 	int rem, err = -EOPNOTSUPP;
2697407af329SVlad Yasevich 	u16 oflags, flags = 0;
2698407af329SVlad Yasevich 	bool have_flags = false;
2699407af329SVlad Yasevich 
2700407af329SVlad Yasevich 	if (nlmsg_len(nlh) < sizeof(*ifm))
2701407af329SVlad Yasevich 		return -EINVAL;
2702407af329SVlad Yasevich 
2703407af329SVlad Yasevich 	ifm = nlmsg_data(nlh);
2704407af329SVlad Yasevich 	if (ifm->ifi_family != AF_BRIDGE)
2705407af329SVlad Yasevich 		return -EPFNOSUPPORT;
2706407af329SVlad Yasevich 
2707407af329SVlad Yasevich 	dev = __dev_get_by_index(net, ifm->ifi_index);
2708407af329SVlad Yasevich 	if (!dev) {
2709407af329SVlad Yasevich 		pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
2710407af329SVlad Yasevich 		return -ENODEV;
2711407af329SVlad Yasevich 	}
2712407af329SVlad Yasevich 
2713407af329SVlad Yasevich 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
2714407af329SVlad Yasevich 	if (br_spec) {
2715407af329SVlad Yasevich 		nla_for_each_nested(attr, br_spec, rem) {
2716407af329SVlad Yasevich 			if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
2717407af329SVlad Yasevich 				have_flags = true;
2718407af329SVlad Yasevich 				flags = nla_get_u16(attr);
2719407af329SVlad Yasevich 				break;
2720407af329SVlad Yasevich 			}
2721407af329SVlad Yasevich 		}
2722407af329SVlad Yasevich 	}
2723407af329SVlad Yasevich 
2724407af329SVlad Yasevich 	oflags = flags;
2725407af329SVlad Yasevich 
2726407af329SVlad Yasevich 	if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
2727407af329SVlad Yasevich 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2728407af329SVlad Yasevich 
2729407af329SVlad Yasevich 		if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
2730407af329SVlad Yasevich 			err = -EOPNOTSUPP;
2731407af329SVlad Yasevich 			goto out;
2732407af329SVlad Yasevich 		}
2733407af329SVlad Yasevich 
2734407af329SVlad Yasevich 		err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh);
2735407af329SVlad Yasevich 		if (err)
2736407af329SVlad Yasevich 			goto out;
2737407af329SVlad Yasevich 
2738407af329SVlad Yasevich 		flags &= ~BRIDGE_FLAGS_MASTER;
2739407af329SVlad Yasevich 	}
2740407af329SVlad Yasevich 
2741407af329SVlad Yasevich 	if ((flags & BRIDGE_FLAGS_SELF)) {
2742407af329SVlad Yasevich 		if (!dev->netdev_ops->ndo_bridge_dellink)
2743407af329SVlad Yasevich 			err = -EOPNOTSUPP;
2744407af329SVlad Yasevich 		else
2745407af329SVlad Yasevich 			err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh);
2746407af329SVlad Yasevich 
2747407af329SVlad Yasevich 		if (!err)
2748407af329SVlad Yasevich 			flags &= ~BRIDGE_FLAGS_SELF;
2749407af329SVlad Yasevich 	}
2750407af329SVlad Yasevich 
2751407af329SVlad Yasevich 	if (have_flags)
2752407af329SVlad Yasevich 		memcpy(nla_data(attr), &flags, sizeof(flags));
2753407af329SVlad Yasevich 	/* Generate event to notify upper layer of bridge change */
2754407af329SVlad Yasevich 	if (!err)
2755407af329SVlad Yasevich 		err = rtnl_bridge_notify(dev, oflags);
2756407af329SVlad Yasevich out:
2757407af329SVlad Yasevich 	return err;
2758407af329SVlad Yasevich }
2759407af329SVlad Yasevich 
27601da177e4SLinus Torvalds /* Process one rtnetlink message. */
27611da177e4SLinus Torvalds 
27621d00a4ebSThomas Graf static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
27631da177e4SLinus Torvalds {
27643b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
2765e2849863SThomas Graf 	rtnl_doit_func doit;
27661da177e4SLinus Torvalds 	int sz_idx, kind;
27671da177e4SLinus Torvalds 	int family;
27681da177e4SLinus Torvalds 	int type;
27692907c35fSEric Dumazet 	int err;
27701da177e4SLinus Torvalds 
27711da177e4SLinus Torvalds 	type = nlh->nlmsg_type;
27721da177e4SLinus Torvalds 	if (type > RTM_MAX)
2773038890feSThomas Graf 		return -EOPNOTSUPP;
27741da177e4SLinus Torvalds 
27751da177e4SLinus Torvalds 	type -= RTM_BASE;
27761da177e4SLinus Torvalds 
27771da177e4SLinus Torvalds 	/* All the messages must have at least 1 byte length */
2778573ce260SHong zhi guo 	if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
27791da177e4SLinus Torvalds 		return 0;
27801da177e4SLinus Torvalds 
2781573ce260SHong zhi guo 	family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
27821da177e4SLinus Torvalds 	sz_idx = type>>2;
27831da177e4SLinus Torvalds 	kind = type&3;
27841da177e4SLinus Torvalds 
278590f62cf3SEric W. Biederman 	if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
27861d00a4ebSThomas Graf 		return -EPERM;
27871da177e4SLinus Torvalds 
2788b8f3ab42SDavid S. Miller 	if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
278997c53cacSDenis V. Lunev 		struct sock *rtnl;
2790e2849863SThomas Graf 		rtnl_dumpit_func dumpit;
2791c7ac8679SGreg Rose 		rtnl_calcit_func calcit;
2792c7ac8679SGreg Rose 		u16 min_dump_alloc = 0;
27931da177e4SLinus Torvalds 
2794e2849863SThomas Graf 		dumpit = rtnl_get_dumpit(family, type);
2795e2849863SThomas Graf 		if (dumpit == NULL)
2796038890feSThomas Graf 			return -EOPNOTSUPP;
2797c7ac8679SGreg Rose 		calcit = rtnl_get_calcit(family, type);
2798c7ac8679SGreg Rose 		if (calcit)
2799115c9b81SGreg Rose 			min_dump_alloc = calcit(skb, nlh);
28001da177e4SLinus Torvalds 
28012907c35fSEric Dumazet 		__rtnl_unlock();
280297c53cacSDenis V. Lunev 		rtnl = net->rtnl;
280380d326faSPablo Neira Ayuso 		{
280480d326faSPablo Neira Ayuso 			struct netlink_dump_control c = {
280580d326faSPablo Neira Ayuso 				.dump		= dumpit,
280680d326faSPablo Neira Ayuso 				.min_dump_alloc	= min_dump_alloc,
280780d326faSPablo Neira Ayuso 			};
280880d326faSPablo Neira Ayuso 			err = netlink_dump_start(rtnl, skb, nlh, &c);
280980d326faSPablo Neira Ayuso 		}
28102907c35fSEric Dumazet 		rtnl_lock();
28112907c35fSEric Dumazet 		return err;
28121da177e4SLinus Torvalds 	}
28131da177e4SLinus Torvalds 
2814e2849863SThomas Graf 	doit = rtnl_get_doit(family, type);
2815e2849863SThomas Graf 	if (doit == NULL)
2816038890feSThomas Graf 		return -EOPNOTSUPP;
28171da177e4SLinus Torvalds 
2818661d2967SThomas Graf 	return doit(skb, nlh);
28191da177e4SLinus Torvalds }
28201da177e4SLinus Torvalds 
2821cd40b7d3SDenis V. Lunev static void rtnetlink_rcv(struct sk_buff *skb)
28221da177e4SLinus Torvalds {
28231536cc0dSDenis V. Lunev 	rtnl_lock();
2824cd40b7d3SDenis V. Lunev 	netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
28251536cc0dSDenis V. Lunev 	rtnl_unlock();
28261da177e4SLinus Torvalds }
28271da177e4SLinus Torvalds 
28281da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
28291da177e4SLinus Torvalds {
2830351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2831e9dc8653SEric W. Biederman 
28321da177e4SLinus Torvalds 	switch (event) {
28331da177e4SLinus Torvalds 	case NETDEV_UP:
28341da177e4SLinus Torvalds 	case NETDEV_DOWN:
283510de05afSPatrick McHardy 	case NETDEV_PRE_UP:
2836d90a909eSEric W. Biederman 	case NETDEV_POST_INIT:
2837d90a909eSEric W. Biederman 	case NETDEV_REGISTER:
28381da177e4SLinus Torvalds 	case NETDEV_CHANGE:
2839755d0e77SPatrick McHardy 	case NETDEV_PRE_TYPE_CHANGE:
28401da177e4SLinus Torvalds 	case NETDEV_GOING_DOWN:
2841a2835763SPatrick McHardy 	case NETDEV_UNREGISTER:
28420115e8e3SEric Dumazet 	case NETDEV_UNREGISTER_FINAL:
2843ac3d3f81SAmerigo Wang 	case NETDEV_RELEASE:
2844ac3d3f81SAmerigo Wang 	case NETDEV_JOIN:
28451da177e4SLinus Torvalds 		break;
28461da177e4SLinus Torvalds 	default:
28477f294054SAlexei Starovoitov 		rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
28481da177e4SLinus Torvalds 		break;
28491da177e4SLinus Torvalds 	}
28501da177e4SLinus Torvalds 	return NOTIFY_DONE;
28511da177e4SLinus Torvalds }
28521da177e4SLinus Torvalds 
28531da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = {
28541da177e4SLinus Torvalds 	.notifier_call	= rtnetlink_event,
28551da177e4SLinus Torvalds };
28561da177e4SLinus Torvalds 
285797c53cacSDenis V. Lunev 
28582c8c1e72SAlexey Dobriyan static int __net_init rtnetlink_net_init(struct net *net)
285997c53cacSDenis V. Lunev {
286097c53cacSDenis V. Lunev 	struct sock *sk;
2861a31f2d17SPablo Neira Ayuso 	struct netlink_kernel_cfg cfg = {
2862a31f2d17SPablo Neira Ayuso 		.groups		= RTNLGRP_MAX,
2863a31f2d17SPablo Neira Ayuso 		.input		= rtnetlink_rcv,
2864a31f2d17SPablo Neira Ayuso 		.cb_mutex	= &rtnl_mutex,
28659785e10aSPablo Neira Ayuso 		.flags		= NL_CFG_F_NONROOT_RECV,
2866a31f2d17SPablo Neira Ayuso 	};
2867a31f2d17SPablo Neira Ayuso 
28689f00d977SPablo Neira Ayuso 	sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
286997c53cacSDenis V. Lunev 	if (!sk)
287097c53cacSDenis V. Lunev 		return -ENOMEM;
287197c53cacSDenis V. Lunev 	net->rtnl = sk;
287297c53cacSDenis V. Lunev 	return 0;
287397c53cacSDenis V. Lunev }
287497c53cacSDenis V. Lunev 
28752c8c1e72SAlexey Dobriyan static void __net_exit rtnetlink_net_exit(struct net *net)
287697c53cacSDenis V. Lunev {
2877b7c6ba6eSDenis V. Lunev 	netlink_kernel_release(net->rtnl);
287897c53cacSDenis V. Lunev 	net->rtnl = NULL;
287997c53cacSDenis V. Lunev }
288097c53cacSDenis V. Lunev 
288197c53cacSDenis V. Lunev static struct pernet_operations rtnetlink_net_ops = {
288297c53cacSDenis V. Lunev 	.init = rtnetlink_net_init,
288397c53cacSDenis V. Lunev 	.exit = rtnetlink_net_exit,
288497c53cacSDenis V. Lunev };
288597c53cacSDenis V. Lunev 
28861da177e4SLinus Torvalds void __init rtnetlink_init(void)
28871da177e4SLinus Torvalds {
288897c53cacSDenis V. Lunev 	if (register_pernet_subsys(&rtnetlink_net_ops))
28891da177e4SLinus Torvalds 		panic("rtnetlink_init: cannot initialize rtnetlink\n");
289097c53cacSDenis V. Lunev 
28911da177e4SLinus Torvalds 	register_netdevice_notifier(&rtnetlink_dev_notifier);
2892340d17fcSThomas Graf 
2893c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
2894c7ac8679SGreg Rose 		      rtnl_dump_ifinfo, rtnl_calcit);
2895c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL);
2896c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL);
2897c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL);
2898687ad8ccSThomas Graf 
2899c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL);
2900c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL);
290177162022SJohn Fastabend 
290277162022SJohn Fastabend 	rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, NULL);
290377162022SJohn Fastabend 	rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, NULL);
290477162022SJohn Fastabend 	rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, NULL);
2905e5a55a89SJohn Fastabend 
2906e5a55a89SJohn Fastabend 	rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, NULL);
2907407af329SVlad Yasevich 	rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, NULL);
2908e5a55a89SJohn Fastabend 	rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, NULL);
29091da177e4SLinus Torvalds }
29101da177e4SLinus Torvalds 
2911