xref: /openbmc/linux/net/core/rtnetlink.c (revision 0cc09020)
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 
19ee5d032fSJakub Kicinski #include <linux/bitops.h>
201da177e4SLinus Torvalds #include <linux/errno.h>
211da177e4SLinus Torvalds #include <linux/module.h>
221da177e4SLinus Torvalds #include <linux/types.h>
231da177e4SLinus Torvalds #include <linux/socket.h>
241da177e4SLinus Torvalds #include <linux/kernel.h>
251da177e4SLinus Torvalds #include <linux/timer.h>
261da177e4SLinus Torvalds #include <linux/string.h>
271da177e4SLinus Torvalds #include <linux/sockios.h>
281da177e4SLinus Torvalds #include <linux/net.h>
291da177e4SLinus Torvalds #include <linux/fcntl.h>
301da177e4SLinus Torvalds #include <linux/mm.h>
311da177e4SLinus Torvalds #include <linux/slab.h>
321da177e4SLinus Torvalds #include <linux/interrupt.h>
331da177e4SLinus Torvalds #include <linux/capability.h>
341da177e4SLinus Torvalds #include <linux/skbuff.h>
351da177e4SLinus Torvalds #include <linux/init.h>
361da177e4SLinus Torvalds #include <linux/security.h>
376756ae4bSStephen Hemminger #include <linux/mutex.h>
381823730fSThomas Graf #include <linux/if_addr.h>
3977162022SJohn Fastabend #include <linux/if_bridge.h>
40f6f6424bSJiri Pirko #include <linux/if_vlan.h>
41ebc08a6fSWilliams, Mitch A #include <linux/pci.h>
4277162022SJohn Fastabend #include <linux/etherdevice.h>
4358038695SMartin KaFai Lau #include <linux/bpf.h>
441da177e4SLinus Torvalds 
457c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds #include <linux/inet.h>
481da177e4SLinus Torvalds #include <linux/netdevice.h>
4982f28412SJiri Pirko #include <net/switchdev.h>
501da177e4SLinus Torvalds #include <net/ip.h>
511da177e4SLinus Torvalds #include <net/protocol.h>
521da177e4SLinus Torvalds #include <net/arp.h>
531da177e4SLinus Torvalds #include <net/route.h>
541da177e4SLinus Torvalds #include <net/udp.h>
55ea697639SDaniel Borkmann #include <net/tcp.h>
561da177e4SLinus Torvalds #include <net/sock.h>
571da177e4SLinus Torvalds #include <net/pkt_sched.h>
5814c0b97dSThomas Graf #include <net/fib_rules.h>
59e2849863SThomas Graf #include <net/rtnetlink.h>
6030ffee84SJohannes Berg #include <net/net_namespace.h>
611da177e4SLinus Torvalds 
62e0d087afSEric Dumazet struct rtnl_link {
63e2849863SThomas Graf 	rtnl_doit_func		doit;
64e2849863SThomas Graf 	rtnl_dumpit_func	dumpit;
65e2849863SThomas Graf };
66e2849863SThomas Graf 
676756ae4bSStephen Hemminger static DEFINE_MUTEX(rtnl_mutex);
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds void rtnl_lock(void)
701da177e4SLinus Torvalds {
716756ae4bSStephen Hemminger 	mutex_lock(&rtnl_mutex);
721da177e4SLinus Torvalds }
73e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_lock);
741da177e4SLinus Torvalds 
751b5c5493SEric Dumazet static struct sk_buff *defer_kfree_skb_list;
761b5c5493SEric Dumazet void rtnl_kfree_skbs(struct sk_buff *head, struct sk_buff *tail)
771b5c5493SEric Dumazet {
781b5c5493SEric Dumazet 	if (head && tail) {
791b5c5493SEric Dumazet 		tail->next = defer_kfree_skb_list;
801b5c5493SEric Dumazet 		defer_kfree_skb_list = head;
811b5c5493SEric Dumazet 	}
821b5c5493SEric Dumazet }
831b5c5493SEric Dumazet EXPORT_SYMBOL(rtnl_kfree_skbs);
841b5c5493SEric Dumazet 
856756ae4bSStephen Hemminger void __rtnl_unlock(void)
861da177e4SLinus Torvalds {
871b5c5493SEric Dumazet 	struct sk_buff *head = defer_kfree_skb_list;
881b5c5493SEric Dumazet 
891b5c5493SEric Dumazet 	defer_kfree_skb_list = NULL;
901b5c5493SEric Dumazet 
916756ae4bSStephen Hemminger 	mutex_unlock(&rtnl_mutex);
921b5c5493SEric Dumazet 
931b5c5493SEric Dumazet 	while (head) {
941b5c5493SEric Dumazet 		struct sk_buff *next = head->next;
951b5c5493SEric Dumazet 
961b5c5493SEric Dumazet 		kfree_skb(head);
971b5c5493SEric Dumazet 		cond_resched();
981b5c5493SEric Dumazet 		head = next;
991b5c5493SEric Dumazet 	}
1001da177e4SLinus Torvalds }
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds void rtnl_unlock(void)
1031da177e4SLinus Torvalds {
10458ec3b4dSHerbert Xu 	/* This fellow will unlock it for us. */
1051da177e4SLinus Torvalds 	netdev_run_todo();
1061da177e4SLinus Torvalds }
107e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unlock);
1081da177e4SLinus Torvalds 
1096756ae4bSStephen Hemminger int rtnl_trylock(void)
1106756ae4bSStephen Hemminger {
1116756ae4bSStephen Hemminger 	return mutex_trylock(&rtnl_mutex);
1126756ae4bSStephen Hemminger }
113e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_trylock);
1146756ae4bSStephen Hemminger 
115c9c1014bSPatrick McHardy int rtnl_is_locked(void)
116c9c1014bSPatrick McHardy {
117c9c1014bSPatrick McHardy 	return mutex_is_locked(&rtnl_mutex);
118c9c1014bSPatrick McHardy }
119e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_is_locked);
120c9c1014bSPatrick McHardy 
121a898def2SPaul E. McKenney #ifdef CONFIG_PROVE_LOCKING
1220cbf3343SYaowei Bai bool lockdep_rtnl_is_held(void)
123a898def2SPaul E. McKenney {
124a898def2SPaul E. McKenney 	return lockdep_is_held(&rtnl_mutex);
125a898def2SPaul E. McKenney }
126a898def2SPaul E. McKenney EXPORT_SYMBOL(lockdep_rtnl_is_held);
127a898def2SPaul E. McKenney #endif /* #ifdef CONFIG_PROVE_LOCKING */
128a898def2SPaul E. McKenney 
12925239ceeSPatrick McHardy static struct rtnl_link *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
130019a3169SFlorian Westphal static refcount_t rtnl_msg_handlers_ref[RTNL_FAMILY_MAX + 1];
131e2849863SThomas Graf 
132e2849863SThomas Graf static inline int rtm_msgindex(int msgtype)
133e2849863SThomas Graf {
134e2849863SThomas Graf 	int msgindex = msgtype - RTM_BASE;
135e2849863SThomas Graf 
136e2849863SThomas Graf 	/*
137e2849863SThomas Graf 	 * msgindex < 0 implies someone tried to register a netlink
138e2849863SThomas Graf 	 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
139e2849863SThomas Graf 	 * the message type has not been added to linux/rtnetlink.h
140e2849863SThomas Graf 	 */
141e2849863SThomas Graf 	BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
142e2849863SThomas Graf 
143e2849863SThomas Graf 	return msgindex;
144e2849863SThomas Graf }
145e2849863SThomas Graf 
146e2849863SThomas Graf static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex)
147e2849863SThomas Graf {
148e2849863SThomas Graf 	struct rtnl_link *tab;
149e2849863SThomas Graf 
15025239ceeSPatrick McHardy 	if (protocol <= RTNL_FAMILY_MAX)
151e2849863SThomas Graf 		tab = rtnl_msg_handlers[protocol];
1520f87b1ddSPatrick McHardy 	else
1530f87b1ddSPatrick McHardy 		tab = NULL;
1540f87b1ddSPatrick McHardy 
15551057f2fSThomas Graf 	if (tab == NULL || tab[msgindex].doit == NULL)
156e2849863SThomas Graf 		tab = rtnl_msg_handlers[PF_UNSPEC];
157e2849863SThomas Graf 
158c80bbeaeSHans Zhang 	return tab[msgindex].doit;
159e2849863SThomas Graf }
160e2849863SThomas Graf 
161e2849863SThomas Graf static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex)
162e2849863SThomas Graf {
163e2849863SThomas Graf 	struct rtnl_link *tab;
164e2849863SThomas Graf 
16525239ceeSPatrick McHardy 	if (protocol <= RTNL_FAMILY_MAX)
166e2849863SThomas Graf 		tab = rtnl_msg_handlers[protocol];
1670f87b1ddSPatrick McHardy 	else
1680f87b1ddSPatrick McHardy 		tab = NULL;
1690f87b1ddSPatrick McHardy 
17051057f2fSThomas Graf 	if (tab == NULL || tab[msgindex].dumpit == NULL)
171e2849863SThomas Graf 		tab = rtnl_msg_handlers[PF_UNSPEC];
172e2849863SThomas Graf 
173c80bbeaeSHans Zhang 	return tab[msgindex].dumpit;
174e2849863SThomas Graf }
175e2849863SThomas Graf 
176e2849863SThomas Graf /**
177e2849863SThomas Graf  * __rtnl_register - Register a rtnetlink message type
178e2849863SThomas Graf  * @protocol: Protocol family or PF_UNSPEC
179e2849863SThomas Graf  * @msgtype: rtnetlink message type
180e2849863SThomas Graf  * @doit: Function pointer called for each request message
181e2849863SThomas Graf  * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
182b97bac64SFlorian Westphal  * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
183e2849863SThomas Graf  *
184e2849863SThomas Graf  * Registers the specified function pointers (at least one of them has
185e2849863SThomas Graf  * to be non-NULL) to be called whenever a request message for the
186e2849863SThomas Graf  * specified protocol family and message type is received.
187e2849863SThomas Graf  *
188e2849863SThomas Graf  * The special protocol family PF_UNSPEC may be used to define fallback
189e2849863SThomas Graf  * function pointers for the case when no entry for the specific protocol
190e2849863SThomas Graf  * family exists.
191e2849863SThomas Graf  *
192e2849863SThomas Graf  * Returns 0 on success or a negative error code.
193e2849863SThomas Graf  */
194e2849863SThomas Graf int __rtnl_register(int protocol, int msgtype,
195c7ac8679SGreg Rose 		    rtnl_doit_func doit, rtnl_dumpit_func dumpit,
196b97bac64SFlorian Westphal 		    unsigned int flags)
197e2849863SThomas Graf {
198e2849863SThomas Graf 	struct rtnl_link *tab;
199e2849863SThomas Graf 	int msgindex;
200e2849863SThomas Graf 
20125239ceeSPatrick McHardy 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
202e2849863SThomas Graf 	msgindex = rtm_msgindex(msgtype);
203e2849863SThomas Graf 
204e2849863SThomas Graf 	tab = rtnl_msg_handlers[protocol];
205e2849863SThomas Graf 	if (tab == NULL) {
206e2849863SThomas Graf 		tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL);
207e2849863SThomas Graf 		if (tab == NULL)
208e2849863SThomas Graf 			return -ENOBUFS;
209e2849863SThomas Graf 
210e2849863SThomas Graf 		rtnl_msg_handlers[protocol] = tab;
211e2849863SThomas Graf 	}
212e2849863SThomas Graf 
213e2849863SThomas Graf 	if (doit)
214e2849863SThomas Graf 		tab[msgindex].doit = doit;
215e2849863SThomas Graf 
216e2849863SThomas Graf 	if (dumpit)
217e2849863SThomas Graf 		tab[msgindex].dumpit = dumpit;
218e2849863SThomas Graf 
219e2849863SThomas Graf 	return 0;
220e2849863SThomas Graf }
221e2849863SThomas Graf EXPORT_SYMBOL_GPL(__rtnl_register);
222e2849863SThomas Graf 
223e2849863SThomas Graf /**
224e2849863SThomas Graf  * rtnl_register - Register a rtnetlink message type
225e2849863SThomas Graf  *
226e2849863SThomas Graf  * Identical to __rtnl_register() but panics on failure. This is useful
227e2849863SThomas Graf  * as failure of this function is very unlikely, it can only happen due
228e2849863SThomas Graf  * to lack of memory when allocating the chain to store all message
229e2849863SThomas Graf  * handlers for a protocol. Meant for use in init functions where lack
23025985edcSLucas De Marchi  * of memory implies no sense in continuing.
231e2849863SThomas Graf  */
232e2849863SThomas Graf void rtnl_register(int protocol, int msgtype,
233c7ac8679SGreg Rose 		   rtnl_doit_func doit, rtnl_dumpit_func dumpit,
234b97bac64SFlorian Westphal 		   unsigned int flags)
235e2849863SThomas Graf {
236b97bac64SFlorian Westphal 	if (__rtnl_register(protocol, msgtype, doit, dumpit, flags) < 0)
237e2849863SThomas Graf 		panic("Unable to register rtnetlink message handler, "
238e2849863SThomas Graf 		      "protocol = %d, message type = %d\n",
239e2849863SThomas Graf 		      protocol, msgtype);
240e2849863SThomas Graf }
241e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_register);
242e2849863SThomas Graf 
243e2849863SThomas Graf /**
244e2849863SThomas Graf  * rtnl_unregister - Unregister a rtnetlink message type
245e2849863SThomas Graf  * @protocol: Protocol family or PF_UNSPEC
246e2849863SThomas Graf  * @msgtype: rtnetlink message type
247e2849863SThomas Graf  *
248e2849863SThomas Graf  * Returns 0 on success or a negative error code.
249e2849863SThomas Graf  */
250e2849863SThomas Graf int rtnl_unregister(int protocol, int msgtype)
251e2849863SThomas Graf {
252e2849863SThomas Graf 	int msgindex;
253e2849863SThomas Graf 
25425239ceeSPatrick McHardy 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
255e2849863SThomas Graf 	msgindex = rtm_msgindex(msgtype);
256e2849863SThomas Graf 
257e2849863SThomas Graf 	if (rtnl_msg_handlers[protocol] == NULL)
258e2849863SThomas Graf 		return -ENOENT;
259e2849863SThomas Graf 
260e2849863SThomas Graf 	rtnl_msg_handlers[protocol][msgindex].doit = NULL;
261e2849863SThomas Graf 	rtnl_msg_handlers[protocol][msgindex].dumpit = NULL;
262e2849863SThomas Graf 
263e2849863SThomas Graf 	return 0;
264e2849863SThomas Graf }
265e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister);
266e2849863SThomas Graf 
267e2849863SThomas Graf /**
268e2849863SThomas Graf  * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
269e2849863SThomas Graf  * @protocol : Protocol family or PF_UNSPEC
270e2849863SThomas Graf  *
271e2849863SThomas Graf  * Identical to calling rtnl_unregster() for all registered message types
272e2849863SThomas Graf  * of a certain protocol family.
273e2849863SThomas Graf  */
274e2849863SThomas Graf void rtnl_unregister_all(int protocol)
275e2849863SThomas Graf {
276019a3169SFlorian Westphal 	struct rtnl_link *handlers;
277019a3169SFlorian Westphal 
27825239ceeSPatrick McHardy 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
279e2849863SThomas Graf 
280019a3169SFlorian Westphal 	rtnl_lock();
281019a3169SFlorian Westphal 	handlers = rtnl_msg_handlers[protocol];
282e2849863SThomas Graf 	rtnl_msg_handlers[protocol] = NULL;
283019a3169SFlorian Westphal 	rtnl_unlock();
284019a3169SFlorian Westphal 
285019a3169SFlorian Westphal 	while (refcount_read(&rtnl_msg_handlers_ref[protocol]) > 0)
286019a3169SFlorian Westphal 		schedule();
287019a3169SFlorian Westphal 	kfree(handlers);
288e2849863SThomas Graf }
289e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister_all);
2901da177e4SLinus Torvalds 
29138f7b870SPatrick McHardy static LIST_HEAD(link_ops);
29238f7b870SPatrick McHardy 
293c63044f0SEric Dumazet static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
294c63044f0SEric Dumazet {
295c63044f0SEric Dumazet 	const struct rtnl_link_ops *ops;
296c63044f0SEric Dumazet 
297c63044f0SEric Dumazet 	list_for_each_entry(ops, &link_ops, list) {
298c63044f0SEric Dumazet 		if (!strcmp(ops->kind, kind))
299c63044f0SEric Dumazet 			return ops;
300c63044f0SEric Dumazet 	}
301c63044f0SEric Dumazet 	return NULL;
302c63044f0SEric Dumazet }
303c63044f0SEric Dumazet 
30438f7b870SPatrick McHardy /**
30538f7b870SPatrick McHardy  * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
30638f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to register
30738f7b870SPatrick McHardy  *
30838f7b870SPatrick McHardy  * The caller must hold the rtnl_mutex. This function should be used
30938f7b870SPatrick McHardy  * by drivers that create devices during module initialization. It
31038f7b870SPatrick McHardy  * must be called before registering the devices.
31138f7b870SPatrick McHardy  *
31238f7b870SPatrick McHardy  * Returns 0 on success or a negative error code.
31338f7b870SPatrick McHardy  */
31438f7b870SPatrick McHardy int __rtnl_link_register(struct rtnl_link_ops *ops)
31538f7b870SPatrick McHardy {
316c63044f0SEric Dumazet 	if (rtnl_link_ops_get(ops->kind))
317c63044f0SEric Dumazet 		return -EEXIST;
318c63044f0SEric Dumazet 
319b0ab2fabSJiri Pirko 	/* The check for setup is here because if ops
320b0ab2fabSJiri Pirko 	 * does not have that filled up, it is not possible
321b0ab2fabSJiri Pirko 	 * to use the ops for creating device. So do not
322b0ab2fabSJiri Pirko 	 * fill up dellink as well. That disables rtnl_dellink.
323b0ab2fabSJiri Pirko 	 */
324b0ab2fabSJiri Pirko 	if (ops->setup && !ops->dellink)
32523289a37SEric Dumazet 		ops->dellink = unregister_netdevice_queue;
3262d85cba2SPatrick McHardy 
32738f7b870SPatrick McHardy 	list_add_tail(&ops->list, &link_ops);
32838f7b870SPatrick McHardy 	return 0;
32938f7b870SPatrick McHardy }
33038f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_register);
33138f7b870SPatrick McHardy 
33238f7b870SPatrick McHardy /**
33338f7b870SPatrick McHardy  * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
33438f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to register
33538f7b870SPatrick McHardy  *
33638f7b870SPatrick McHardy  * Returns 0 on success or a negative error code.
33738f7b870SPatrick McHardy  */
33838f7b870SPatrick McHardy int rtnl_link_register(struct rtnl_link_ops *ops)
33938f7b870SPatrick McHardy {
34038f7b870SPatrick McHardy 	int err;
34138f7b870SPatrick McHardy 
34238f7b870SPatrick McHardy 	rtnl_lock();
34338f7b870SPatrick McHardy 	err = __rtnl_link_register(ops);
34438f7b870SPatrick McHardy 	rtnl_unlock();
34538f7b870SPatrick McHardy 	return err;
34638f7b870SPatrick McHardy }
34738f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_register);
34838f7b870SPatrick McHardy 
349669f87baSPavel Emelyanov static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
350669f87baSPavel Emelyanov {
351669f87baSPavel Emelyanov 	struct net_device *dev;
35223289a37SEric Dumazet 	LIST_HEAD(list_kill);
35323289a37SEric Dumazet 
354669f87baSPavel Emelyanov 	for_each_netdev(net, dev) {
35523289a37SEric Dumazet 		if (dev->rtnl_link_ops == ops)
35623289a37SEric Dumazet 			ops->dellink(dev, &list_kill);
357669f87baSPavel Emelyanov 	}
35823289a37SEric Dumazet 	unregister_netdevice_many(&list_kill);
359669f87baSPavel Emelyanov }
360669f87baSPavel Emelyanov 
36138f7b870SPatrick McHardy /**
36238f7b870SPatrick McHardy  * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
36338f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to unregister
36438f7b870SPatrick McHardy  *
3652d85cba2SPatrick McHardy  * The caller must hold the rtnl_mutex.
36638f7b870SPatrick McHardy  */
36738f7b870SPatrick McHardy void __rtnl_link_unregister(struct rtnl_link_ops *ops)
36838f7b870SPatrick McHardy {
369881d966bSEric W. Biederman 	struct net *net;
3702d85cba2SPatrick McHardy 
371881d966bSEric W. Biederman 	for_each_net(net) {
372669f87baSPavel Emelyanov 		__rtnl_kill_links(net, ops);
373881d966bSEric W. Biederman 	}
37438f7b870SPatrick McHardy 	list_del(&ops->list);
37538f7b870SPatrick McHardy }
37638f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
37738f7b870SPatrick McHardy 
378200b916fSCong Wang /* Return with the rtnl_lock held when there are no network
379200b916fSCong Wang  * devices unregistering in any network namespace.
380200b916fSCong Wang  */
381200b916fSCong Wang static void rtnl_lock_unregistering_all(void)
382200b916fSCong Wang {
383200b916fSCong Wang 	struct net *net;
384200b916fSCong Wang 	bool unregistering;
385ff960a73SPeter Zijlstra 	DEFINE_WAIT_FUNC(wait, woken_wake_function);
386200b916fSCong Wang 
387ff960a73SPeter Zijlstra 	add_wait_queue(&netdev_unregistering_wq, &wait);
388200b916fSCong Wang 	for (;;) {
389200b916fSCong Wang 		unregistering = false;
390200b916fSCong Wang 		rtnl_lock();
391200b916fSCong Wang 		for_each_net(net) {
392200b916fSCong Wang 			if (net->dev_unreg_count > 0) {
393200b916fSCong Wang 				unregistering = true;
394200b916fSCong Wang 				break;
395200b916fSCong Wang 			}
396200b916fSCong Wang 		}
397200b916fSCong Wang 		if (!unregistering)
398200b916fSCong Wang 			break;
399200b916fSCong Wang 		__rtnl_unlock();
400ff960a73SPeter Zijlstra 
401ff960a73SPeter Zijlstra 		wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
402200b916fSCong Wang 	}
403ff960a73SPeter Zijlstra 	remove_wait_queue(&netdev_unregistering_wq, &wait);
404200b916fSCong Wang }
405200b916fSCong Wang 
40638f7b870SPatrick McHardy /**
40738f7b870SPatrick McHardy  * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
40838f7b870SPatrick McHardy  * @ops: struct rtnl_link_ops * to unregister
40938f7b870SPatrick McHardy  */
41038f7b870SPatrick McHardy void rtnl_link_unregister(struct rtnl_link_ops *ops)
41138f7b870SPatrick McHardy {
412200b916fSCong Wang 	/* Close the race with cleanup_net() */
413200b916fSCong Wang 	mutex_lock(&net_mutex);
414200b916fSCong Wang 	rtnl_lock_unregistering_all();
41538f7b870SPatrick McHardy 	__rtnl_link_unregister(ops);
41638f7b870SPatrick McHardy 	rtnl_unlock();
417200b916fSCong Wang 	mutex_unlock(&net_mutex);
41838f7b870SPatrick McHardy }
41938f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_unregister);
42038f7b870SPatrick McHardy 
421ba7d49b1SJiri Pirko static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
422ba7d49b1SJiri Pirko {
423ba7d49b1SJiri Pirko 	struct net_device *master_dev;
424ba7d49b1SJiri Pirko 	const struct rtnl_link_ops *ops;
425ba7d49b1SJiri Pirko 
426ba7d49b1SJiri Pirko 	master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
427ba7d49b1SJiri Pirko 	if (!master_dev)
428ba7d49b1SJiri Pirko 		return 0;
429ba7d49b1SJiri Pirko 	ops = master_dev->rtnl_link_ops;
4306049f253SFernando Luis Vazquez Cao 	if (!ops || !ops->get_slave_size)
431ba7d49b1SJiri Pirko 		return 0;
432ba7d49b1SJiri Pirko 	/* IFLA_INFO_SLAVE_DATA + nested data */
433ba7d49b1SJiri Pirko 	return nla_total_size(sizeof(struct nlattr)) +
434ba7d49b1SJiri Pirko 	       ops->get_slave_size(master_dev, dev);
435ba7d49b1SJiri Pirko }
436ba7d49b1SJiri Pirko 
43738f7b870SPatrick McHardy static size_t rtnl_link_get_size(const struct net_device *dev)
43838f7b870SPatrick McHardy {
43938f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
44038f7b870SPatrick McHardy 	size_t size;
44138f7b870SPatrick McHardy 
44238f7b870SPatrick McHardy 	if (!ops)
44338f7b870SPatrick McHardy 		return 0;
44438f7b870SPatrick McHardy 
445369cf77aSThomas Graf 	size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
446369cf77aSThomas Graf 	       nla_total_size(strlen(ops->kind) + 1);  /* IFLA_INFO_KIND */
44738f7b870SPatrick McHardy 
44838f7b870SPatrick McHardy 	if (ops->get_size)
44938f7b870SPatrick McHardy 		/* IFLA_INFO_DATA + nested data */
450369cf77aSThomas Graf 		size += nla_total_size(sizeof(struct nlattr)) +
45138f7b870SPatrick McHardy 			ops->get_size(dev);
45238f7b870SPatrick McHardy 
45338f7b870SPatrick McHardy 	if (ops->get_xstats_size)
454369cf77aSThomas Graf 		/* IFLA_INFO_XSTATS */
455369cf77aSThomas Graf 		size += nla_total_size(ops->get_xstats_size(dev));
45638f7b870SPatrick McHardy 
457ba7d49b1SJiri Pirko 	size += rtnl_link_get_slave_info_data_size(dev);
458ba7d49b1SJiri Pirko 
45938f7b870SPatrick McHardy 	return size;
46038f7b870SPatrick McHardy }
46138f7b870SPatrick McHardy 
462f8ff182cSThomas Graf static LIST_HEAD(rtnl_af_ops);
463f8ff182cSThomas Graf 
464f8ff182cSThomas Graf static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
465f8ff182cSThomas Graf {
466f8ff182cSThomas Graf 	const struct rtnl_af_ops *ops;
467f8ff182cSThomas Graf 
468f8ff182cSThomas Graf 	list_for_each_entry(ops, &rtnl_af_ops, list) {
469f8ff182cSThomas Graf 		if (ops->family == family)
470f8ff182cSThomas Graf 			return ops;
471f8ff182cSThomas Graf 	}
472f8ff182cSThomas Graf 
473f8ff182cSThomas Graf 	return NULL;
474f8ff182cSThomas Graf }
475f8ff182cSThomas Graf 
476f8ff182cSThomas Graf /**
477f8ff182cSThomas Graf  * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
478f8ff182cSThomas Graf  * @ops: struct rtnl_af_ops * to register
479f8ff182cSThomas Graf  *
480f8ff182cSThomas Graf  * Returns 0 on success or a negative error code.
481f8ff182cSThomas Graf  */
4823678a9d8Sstephen hemminger void rtnl_af_register(struct rtnl_af_ops *ops)
483f8ff182cSThomas Graf {
484f8ff182cSThomas Graf 	rtnl_lock();
4853678a9d8Sstephen hemminger 	list_add_tail(&ops->list, &rtnl_af_ops);
486f8ff182cSThomas Graf 	rtnl_unlock();
487f8ff182cSThomas Graf }
488f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_register);
489f8ff182cSThomas Graf 
490f8ff182cSThomas Graf /**
491f8ff182cSThomas Graf  * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
492f8ff182cSThomas Graf  * @ops: struct rtnl_af_ops * to unregister
493f8ff182cSThomas Graf  *
494f8ff182cSThomas Graf  * The caller must hold the rtnl_mutex.
495f8ff182cSThomas Graf  */
496f8ff182cSThomas Graf void __rtnl_af_unregister(struct rtnl_af_ops *ops)
497f8ff182cSThomas Graf {
498f8ff182cSThomas Graf 	list_del(&ops->list);
499f8ff182cSThomas Graf }
500f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(__rtnl_af_unregister);
501f8ff182cSThomas Graf 
502f8ff182cSThomas Graf /**
503f8ff182cSThomas Graf  * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
504f8ff182cSThomas Graf  * @ops: struct rtnl_af_ops * to unregister
505f8ff182cSThomas Graf  */
506f8ff182cSThomas Graf void rtnl_af_unregister(struct rtnl_af_ops *ops)
507f8ff182cSThomas Graf {
508f8ff182cSThomas Graf 	rtnl_lock();
509f8ff182cSThomas Graf 	__rtnl_af_unregister(ops);
510f8ff182cSThomas Graf 	rtnl_unlock();
511f8ff182cSThomas Graf }
512f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_unregister);
513f8ff182cSThomas Graf 
514b1974ed0SArad, Ronen static size_t rtnl_link_get_af_size(const struct net_device *dev,
515b1974ed0SArad, Ronen 				    u32 ext_filter_mask)
516f8ff182cSThomas Graf {
517f8ff182cSThomas Graf 	struct rtnl_af_ops *af_ops;
518f8ff182cSThomas Graf 	size_t size;
519f8ff182cSThomas Graf 
520f8ff182cSThomas Graf 	/* IFLA_AF_SPEC */
521f8ff182cSThomas Graf 	size = nla_total_size(sizeof(struct nlattr));
522f8ff182cSThomas Graf 
523f8ff182cSThomas Graf 	list_for_each_entry(af_ops, &rtnl_af_ops, list) {
524f8ff182cSThomas Graf 		if (af_ops->get_link_af_size) {
525f8ff182cSThomas Graf 			/* AF_* + nested data */
526f8ff182cSThomas Graf 			size += nla_total_size(sizeof(struct nlattr)) +
527b1974ed0SArad, Ronen 				af_ops->get_link_af_size(dev, ext_filter_mask);
528f8ff182cSThomas Graf 		}
529f8ff182cSThomas Graf 	}
530f8ff182cSThomas Graf 
531f8ff182cSThomas Graf 	return size;
532f8ff182cSThomas Graf }
533f8ff182cSThomas Graf 
534ba7d49b1SJiri Pirko static bool rtnl_have_link_slave_info(const struct net_device *dev)
535ba7d49b1SJiri Pirko {
536ba7d49b1SJiri Pirko 	struct net_device *master_dev;
537ba7d49b1SJiri Pirko 
538ba7d49b1SJiri Pirko 	master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
539813f020cSJiri Pirko 	if (master_dev && master_dev->rtnl_link_ops)
540ba7d49b1SJiri Pirko 		return true;
541ba7d49b1SJiri Pirko 	return false;
542ba7d49b1SJiri Pirko }
543ba7d49b1SJiri Pirko 
544ba7d49b1SJiri Pirko static int rtnl_link_slave_info_fill(struct sk_buff *skb,
545ba7d49b1SJiri Pirko 				     const struct net_device *dev)
546ba7d49b1SJiri Pirko {
547ba7d49b1SJiri Pirko 	struct net_device *master_dev;
548ba7d49b1SJiri Pirko 	const struct rtnl_link_ops *ops;
549ba7d49b1SJiri Pirko 	struct nlattr *slave_data;
550ba7d49b1SJiri Pirko 	int err;
551ba7d49b1SJiri Pirko 
552ba7d49b1SJiri Pirko 	master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
553ba7d49b1SJiri Pirko 	if (!master_dev)
554ba7d49b1SJiri Pirko 		return 0;
555ba7d49b1SJiri Pirko 	ops = master_dev->rtnl_link_ops;
556ba7d49b1SJiri Pirko 	if (!ops)
557ba7d49b1SJiri Pirko 		return 0;
558ba7d49b1SJiri Pirko 	if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
559ba7d49b1SJiri Pirko 		return -EMSGSIZE;
560ba7d49b1SJiri Pirko 	if (ops->fill_slave_info) {
561ba7d49b1SJiri Pirko 		slave_data = nla_nest_start(skb, IFLA_INFO_SLAVE_DATA);
562ba7d49b1SJiri Pirko 		if (!slave_data)
563ba7d49b1SJiri Pirko 			return -EMSGSIZE;
564ba7d49b1SJiri Pirko 		err = ops->fill_slave_info(skb, master_dev, dev);
565ba7d49b1SJiri Pirko 		if (err < 0)
566ba7d49b1SJiri Pirko 			goto err_cancel_slave_data;
567ba7d49b1SJiri Pirko 		nla_nest_end(skb, slave_data);
568ba7d49b1SJiri Pirko 	}
569ba7d49b1SJiri Pirko 	return 0;
570ba7d49b1SJiri Pirko 
571ba7d49b1SJiri Pirko err_cancel_slave_data:
572ba7d49b1SJiri Pirko 	nla_nest_cancel(skb, slave_data);
573ba7d49b1SJiri Pirko 	return err;
574ba7d49b1SJiri Pirko }
575ba7d49b1SJiri Pirko 
576ba7d49b1SJiri Pirko static int rtnl_link_info_fill(struct sk_buff *skb,
577ba7d49b1SJiri Pirko 			       const struct net_device *dev)
57838f7b870SPatrick McHardy {
57938f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
580ba7d49b1SJiri Pirko 	struct nlattr *data;
581ba7d49b1SJiri Pirko 	int err;
582ba7d49b1SJiri Pirko 
583ba7d49b1SJiri Pirko 	if (!ops)
584ba7d49b1SJiri Pirko 		return 0;
585ba7d49b1SJiri Pirko 	if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
586ba7d49b1SJiri Pirko 		return -EMSGSIZE;
587ba7d49b1SJiri Pirko 	if (ops->fill_xstats) {
588ba7d49b1SJiri Pirko 		err = ops->fill_xstats(skb, dev);
589ba7d49b1SJiri Pirko 		if (err < 0)
590ba7d49b1SJiri Pirko 			return err;
591ba7d49b1SJiri Pirko 	}
592ba7d49b1SJiri Pirko 	if (ops->fill_info) {
593ba7d49b1SJiri Pirko 		data = nla_nest_start(skb, IFLA_INFO_DATA);
594ba7d49b1SJiri Pirko 		if (data == NULL)
595ba7d49b1SJiri Pirko 			return -EMSGSIZE;
596ba7d49b1SJiri Pirko 		err = ops->fill_info(skb, dev);
597ba7d49b1SJiri Pirko 		if (err < 0)
598ba7d49b1SJiri Pirko 			goto err_cancel_data;
599ba7d49b1SJiri Pirko 		nla_nest_end(skb, data);
600ba7d49b1SJiri Pirko 	}
601ba7d49b1SJiri Pirko 	return 0;
602ba7d49b1SJiri Pirko 
603ba7d49b1SJiri Pirko err_cancel_data:
604ba7d49b1SJiri Pirko 	nla_nest_cancel(skb, data);
605ba7d49b1SJiri Pirko 	return err;
606ba7d49b1SJiri Pirko }
607ba7d49b1SJiri Pirko 
608ba7d49b1SJiri Pirko static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
609ba7d49b1SJiri Pirko {
610ba7d49b1SJiri Pirko 	struct nlattr *linkinfo;
61138f7b870SPatrick McHardy 	int err = -EMSGSIZE;
61238f7b870SPatrick McHardy 
61338f7b870SPatrick McHardy 	linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
61438f7b870SPatrick McHardy 	if (linkinfo == NULL)
61538f7b870SPatrick McHardy 		goto out;
61638f7b870SPatrick McHardy 
617ba7d49b1SJiri Pirko 	err = rtnl_link_info_fill(skb, dev);
61838f7b870SPatrick McHardy 	if (err < 0)
61938f7b870SPatrick McHardy 		goto err_cancel_link;
620ba7d49b1SJiri Pirko 
621ba7d49b1SJiri Pirko 	err = rtnl_link_slave_info_fill(skb, dev);
62238f7b870SPatrick McHardy 	if (err < 0)
623ba7d49b1SJiri Pirko 		goto err_cancel_link;
62438f7b870SPatrick McHardy 
62538f7b870SPatrick McHardy 	nla_nest_end(skb, linkinfo);
62638f7b870SPatrick McHardy 	return 0;
62738f7b870SPatrick McHardy 
62838f7b870SPatrick McHardy err_cancel_link:
62938f7b870SPatrick McHardy 	nla_nest_cancel(skb, linkinfo);
63038f7b870SPatrick McHardy out:
63138f7b870SPatrick McHardy 	return err;
63238f7b870SPatrick McHardy }
63338f7b870SPatrick McHardy 
63495c96174SEric Dumazet int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
6351da177e4SLinus Torvalds {
63697c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
6371da177e4SLinus Torvalds 	int err = 0;
6381da177e4SLinus Torvalds 
639ac6d439dSPatrick McHardy 	NETLINK_CB(skb).dst_group = group;
6401da177e4SLinus Torvalds 	if (echo)
64163354797SReshetova, Elena 		refcount_inc(&skb->users);
6421da177e4SLinus Torvalds 	netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
6431da177e4SLinus Torvalds 	if (echo)
6441da177e4SLinus Torvalds 		err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
6451da177e4SLinus Torvalds 	return err;
6461da177e4SLinus Torvalds }
6471da177e4SLinus Torvalds 
64897c53cacSDenis V. Lunev int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
6492942e900SThomas Graf {
65097c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
65197c53cacSDenis V. Lunev 
6522942e900SThomas Graf 	return nlmsg_unicast(rtnl, skb, pid);
6532942e900SThomas Graf }
654e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unicast);
6552942e900SThomas Graf 
6561ce85fe4SPablo Neira Ayuso void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
65797676b6bSThomas Graf 		 struct nlmsghdr *nlh, gfp_t flags)
65897676b6bSThomas Graf {
65997c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
66097676b6bSThomas Graf 	int report = 0;
66197676b6bSThomas Graf 
66297676b6bSThomas Graf 	if (nlh)
66397676b6bSThomas Graf 		report = nlmsg_report(nlh);
66497676b6bSThomas Graf 
6651ce85fe4SPablo Neira Ayuso 	nlmsg_notify(rtnl, skb, pid, group, report, flags);
66697676b6bSThomas Graf }
667e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_notify);
66897676b6bSThomas Graf 
66997c53cacSDenis V. Lunev void rtnl_set_sk_err(struct net *net, u32 group, int error)
67097676b6bSThomas Graf {
67197c53cacSDenis V. Lunev 	struct sock *rtnl = net->rtnl;
67297c53cacSDenis V. Lunev 
67397676b6bSThomas Graf 	netlink_set_err(rtnl, 0, group, error);
67497676b6bSThomas Graf }
675e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_set_sk_err);
67697676b6bSThomas Graf 
6771da177e4SLinus Torvalds int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
6781da177e4SLinus Torvalds {
6792d7202bfSThomas Graf 	struct nlattr *mx;
6802d7202bfSThomas Graf 	int i, valid = 0;
6811da177e4SLinus Torvalds 
6822d7202bfSThomas Graf 	mx = nla_nest_start(skb, RTA_METRICS);
6832d7202bfSThomas Graf 	if (mx == NULL)
6842d7202bfSThomas Graf 		return -ENOBUFS;
6852d7202bfSThomas Graf 
6861da177e4SLinus Torvalds 	for (i = 0; i < RTAX_MAX; i++) {
6872d7202bfSThomas Graf 		if (metrics[i]) {
688ea697639SDaniel Borkmann 			if (i == RTAX_CC_ALGO - 1) {
689ea697639SDaniel Borkmann 				char tmp[TCP_CA_NAME_MAX], *name;
690ea697639SDaniel Borkmann 
691ea697639SDaniel Borkmann 				name = tcp_ca_get_name_by_key(metrics[i], tmp);
692ea697639SDaniel Borkmann 				if (!name)
693ea697639SDaniel Borkmann 					continue;
694ea697639SDaniel Borkmann 				if (nla_put_string(skb, i + 1, name))
695ea697639SDaniel Borkmann 					goto nla_put_failure;
696c3a8d947SDaniel Borkmann 			} else if (i == RTAX_FEATURES - 1) {
697c3a8d947SDaniel Borkmann 				u32 user_features = metrics[i] & RTAX_FEATURE_MASK;
698c3a8d947SDaniel Borkmann 
699f8edcd12SPhil Sutter 				if (!user_features)
700f8edcd12SPhil Sutter 					continue;
701c3a8d947SDaniel Borkmann 				BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK);
702c3a8d947SDaniel Borkmann 				if (nla_put_u32(skb, i + 1, user_features))
703c3a8d947SDaniel Borkmann 					goto nla_put_failure;
704ea697639SDaniel Borkmann 			} else {
705a6574349SDavid S. Miller 				if (nla_put_u32(skb, i + 1, metrics[i]))
706a6574349SDavid S. Miller 					goto nla_put_failure;
7071da177e4SLinus Torvalds 			}
708ea697639SDaniel Borkmann 			valid++;
709ea697639SDaniel Borkmann 		}
7102d7202bfSThomas Graf 	}
7111da177e4SLinus Torvalds 
712a57d27fcSDavid S. Miller 	if (!valid) {
713a57d27fcSDavid S. Miller 		nla_nest_cancel(skb, mx);
714a57d27fcSDavid S. Miller 		return 0;
715a57d27fcSDavid S. Miller 	}
7162d7202bfSThomas Graf 
7172d7202bfSThomas Graf 	return nla_nest_end(skb, mx);
7182d7202bfSThomas Graf 
7192d7202bfSThomas Graf nla_put_failure:
720bc3ed28cSThomas Graf 	nla_nest_cancel(skb, mx);
721bc3ed28cSThomas Graf 	return -EMSGSIZE;
7221da177e4SLinus Torvalds }
723e0d087afSEric Dumazet EXPORT_SYMBOL(rtnetlink_put_metrics);
7241da177e4SLinus Torvalds 
725e3703b3dSThomas Graf int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
72687a50699SDavid S. Miller 		       long expires, u32 error)
727e3703b3dSThomas Graf {
728e3703b3dSThomas Graf 	struct rta_cacheinfo ci = {
729a399a805SEric Dumazet 		.rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse),
730e3703b3dSThomas Graf 		.rta_used = dst->__use,
731e3703b3dSThomas Graf 		.rta_clntref = atomic_read(&(dst->__refcnt)),
732e3703b3dSThomas Graf 		.rta_error = error,
733e3703b3dSThomas Graf 		.rta_id =  id,
734e3703b3dSThomas Graf 	};
735e3703b3dSThomas Graf 
7368253947eSLi Wei 	if (expires) {
7378253947eSLi Wei 		unsigned long clock;
738e3703b3dSThomas Graf 
7398253947eSLi Wei 		clock = jiffies_to_clock_t(abs(expires));
7408253947eSLi Wei 		clock = min_t(unsigned long, clock, INT_MAX);
7418253947eSLi Wei 		ci.rta_expires = (expires > 0) ? clock : -clock;
7428253947eSLi Wei 	}
743e3703b3dSThomas Graf 	return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
744e3703b3dSThomas Graf }
745e3703b3dSThomas Graf EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
7461da177e4SLinus Torvalds 
74793b2d4a2SDavid S. Miller static void set_operstate(struct net_device *dev, unsigned char transition)
748b00055aaSStefan Rompf {
749b00055aaSStefan Rompf 	unsigned char operstate = dev->operstate;
750b00055aaSStefan Rompf 
751b00055aaSStefan Rompf 	switch (transition) {
752b00055aaSStefan Rompf 	case IF_OPER_UP:
753b00055aaSStefan Rompf 		if ((operstate == IF_OPER_DORMANT ||
754b00055aaSStefan Rompf 		     operstate == IF_OPER_UNKNOWN) &&
755b00055aaSStefan Rompf 		    !netif_dormant(dev))
756b00055aaSStefan Rompf 			operstate = IF_OPER_UP;
757b00055aaSStefan Rompf 		break;
758b00055aaSStefan Rompf 
759b00055aaSStefan Rompf 	case IF_OPER_DORMANT:
760b00055aaSStefan Rompf 		if (operstate == IF_OPER_UP ||
761b00055aaSStefan Rompf 		    operstate == IF_OPER_UNKNOWN)
762b00055aaSStefan Rompf 			operstate = IF_OPER_DORMANT;
763b00055aaSStefan Rompf 		break;
7643ff50b79SStephen Hemminger 	}
765b00055aaSStefan Rompf 
766b00055aaSStefan Rompf 	if (dev->operstate != operstate) {
767b00055aaSStefan Rompf 		write_lock_bh(&dev_base_lock);
768b00055aaSStefan Rompf 		dev->operstate = operstate;
769b00055aaSStefan Rompf 		write_unlock_bh(&dev_base_lock);
770b00055aaSStefan Rompf 		netdev_state_change(dev);
77193b2d4a2SDavid S. Miller 	}
772b00055aaSStefan Rompf }
773b00055aaSStefan Rompf 
774b1beb681SJiri Benc static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
775b1beb681SJiri Benc {
776b1beb681SJiri Benc 	return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
777b1beb681SJiri Benc 	       (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
778b1beb681SJiri Benc }
779b1beb681SJiri Benc 
7803729d502SPatrick McHardy static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
7813729d502SPatrick McHardy 					   const struct ifinfomsg *ifm)
7823729d502SPatrick McHardy {
7833729d502SPatrick McHardy 	unsigned int flags = ifm->ifi_flags;
7843729d502SPatrick McHardy 
7853729d502SPatrick McHardy 	/* bugwards compatibility: ifi_change == 0 is treated as ~0 */
7863729d502SPatrick McHardy 	if (ifm->ifi_change)
7873729d502SPatrick McHardy 		flags = (flags & ifm->ifi_change) |
788b1beb681SJiri Benc 			(rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
7893729d502SPatrick McHardy 
7903729d502SPatrick McHardy 	return flags;
7913729d502SPatrick McHardy }
7923729d502SPatrick McHardy 
793b60c5115SThomas Graf static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
794be1f3c2cSBen Hutchings 				 const struct rtnl_link_stats64 *b)
7951da177e4SLinus Torvalds {
796b60c5115SThomas Graf 	a->rx_packets = b->rx_packets;
797b60c5115SThomas Graf 	a->tx_packets = b->tx_packets;
798b60c5115SThomas Graf 	a->rx_bytes = b->rx_bytes;
799b60c5115SThomas Graf 	a->tx_bytes = b->tx_bytes;
800b60c5115SThomas Graf 	a->rx_errors = b->rx_errors;
801b60c5115SThomas Graf 	a->tx_errors = b->tx_errors;
802b60c5115SThomas Graf 	a->rx_dropped = b->rx_dropped;
803b60c5115SThomas Graf 	a->tx_dropped = b->tx_dropped;
804b60c5115SThomas Graf 
805b60c5115SThomas Graf 	a->multicast = b->multicast;
806b60c5115SThomas Graf 	a->collisions = b->collisions;
807b60c5115SThomas Graf 
808b60c5115SThomas Graf 	a->rx_length_errors = b->rx_length_errors;
809b60c5115SThomas Graf 	a->rx_over_errors = b->rx_over_errors;
810b60c5115SThomas Graf 	a->rx_crc_errors = b->rx_crc_errors;
811b60c5115SThomas Graf 	a->rx_frame_errors = b->rx_frame_errors;
812b60c5115SThomas Graf 	a->rx_fifo_errors = b->rx_fifo_errors;
813b60c5115SThomas Graf 	a->rx_missed_errors = b->rx_missed_errors;
814b60c5115SThomas Graf 
815b60c5115SThomas Graf 	a->tx_aborted_errors = b->tx_aborted_errors;
816b60c5115SThomas Graf 	a->tx_carrier_errors = b->tx_carrier_errors;
817b60c5115SThomas Graf 	a->tx_fifo_errors = b->tx_fifo_errors;
818b60c5115SThomas Graf 	a->tx_heartbeat_errors = b->tx_heartbeat_errors;
819b60c5115SThomas Graf 	a->tx_window_errors = b->tx_window_errors;
820b60c5115SThomas Graf 
821b60c5115SThomas Graf 	a->rx_compressed = b->rx_compressed;
822b60c5115SThomas Graf 	a->tx_compressed = b->tx_compressed;
8236e7333d3SJarod Wilson 
8246e7333d3SJarod Wilson 	a->rx_nohandler = b->rx_nohandler;
82510708f37SJan Engelhardt }
82610708f37SJan Engelhardt 
827c02db8c6SChris Wright /* All VF info */
828115c9b81SGreg Rose static inline int rtnl_vfinfo_size(const struct net_device *dev,
829115c9b81SGreg Rose 				   u32 ext_filter_mask)
830ebc08a6fSWilliams, Mitch A {
8319af15c38SPhil Sutter 	if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) {
832c02db8c6SChris Wright 		int num_vfs = dev_num_vf(dev->dev.parent);
8337e75f74aSSabrina Dubroca 		size_t size = nla_total_size(0);
834045de01aSScott Feldman 		size += num_vfs *
8357e75f74aSSabrina Dubroca 			(nla_total_size(0) +
8367e75f74aSSabrina Dubroca 			 nla_total_size(sizeof(struct ifla_vf_mac)) +
8377e75f74aSSabrina Dubroca 			 nla_total_size(sizeof(struct ifla_vf_vlan)) +
8387e75f74aSSabrina Dubroca 			 nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
83979aab093SMoshe Shemesh 			 nla_total_size(MAX_VLAN_LIST_LEN *
84079aab093SMoshe Shemesh 					sizeof(struct ifla_vf_vlan_info)) +
841ed616689SSucheta Chakraborty 			 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
8427e75f74aSSabrina Dubroca 			 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
843945a3676SJiri Benc 			 nla_total_size(sizeof(struct ifla_vf_rate)) +
84401a3d796SVlad Zolotarov 			 nla_total_size(sizeof(struct ifla_vf_link_state)) +
8453b766cd8SEran Ben Elisha 			 nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
8467e75f74aSSabrina Dubroca 			 nla_total_size(0) + /* nest IFLA_VF_STATS */
8473b766cd8SEran Ben Elisha 			 /* IFLA_VF_STATS_RX_PACKETS */
848343a6d8eSNicolas Dichtel 			 nla_total_size_64bit(sizeof(__u64)) +
8493b766cd8SEran Ben Elisha 			 /* IFLA_VF_STATS_TX_PACKETS */
850343a6d8eSNicolas Dichtel 			 nla_total_size_64bit(sizeof(__u64)) +
8513b766cd8SEran Ben Elisha 			 /* IFLA_VF_STATS_RX_BYTES */
852343a6d8eSNicolas Dichtel 			 nla_total_size_64bit(sizeof(__u64)) +
8533b766cd8SEran Ben Elisha 			 /* IFLA_VF_STATS_TX_BYTES */
854343a6d8eSNicolas Dichtel 			 nla_total_size_64bit(sizeof(__u64)) +
8553b766cd8SEran Ben Elisha 			 /* IFLA_VF_STATS_BROADCAST */
856343a6d8eSNicolas Dichtel 			 nla_total_size_64bit(sizeof(__u64)) +
8573b766cd8SEran Ben Elisha 			 /* IFLA_VF_STATS_MULTICAST */
858343a6d8eSNicolas Dichtel 			 nla_total_size_64bit(sizeof(__u64)) +
859dd461d6aSHiroshi Shimamoto 			 nla_total_size(sizeof(struct ifla_vf_trust)));
860c02db8c6SChris Wright 		return size;
861c02db8c6SChris Wright 	} else
862ebc08a6fSWilliams, Mitch A 		return 0;
863ebc08a6fSWilliams, Mitch A }
864ebc08a6fSWilliams, Mitch A 
865c53864fdSDavid Gibson static size_t rtnl_port_size(const struct net_device *dev,
866c53864fdSDavid Gibson 			     u32 ext_filter_mask)
86757b61080SScott Feldman {
86857b61080SScott Feldman 	size_t port_size = nla_total_size(4)		/* PORT_VF */
86957b61080SScott Feldman 		+ nla_total_size(PORT_PROFILE_MAX)	/* PORT_PROFILE */
87057b61080SScott Feldman 		+ nla_total_size(PORT_UUID_MAX)		/* PORT_INSTANCE_UUID */
87157b61080SScott Feldman 		+ nla_total_size(PORT_UUID_MAX)		/* PORT_HOST_UUID */
87257b61080SScott Feldman 		+ nla_total_size(1)			/* PROT_VDP_REQUEST */
87357b61080SScott Feldman 		+ nla_total_size(2);			/* PORT_VDP_RESPONSE */
87457b61080SScott Feldman 	size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
87557b61080SScott Feldman 	size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
87657b61080SScott Feldman 		+ port_size;
87757b61080SScott Feldman 	size_t port_self_size = nla_total_size(sizeof(struct nlattr))
87857b61080SScott Feldman 		+ port_size;
87957b61080SScott Feldman 
880c53864fdSDavid Gibson 	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
881c53864fdSDavid Gibson 	    !(ext_filter_mask & RTEXT_FILTER_VF))
88257b61080SScott Feldman 		return 0;
88357b61080SScott Feldman 	if (dev_num_vf(dev->dev.parent))
88457b61080SScott Feldman 		return port_self_size + vf_ports_size +
88557b61080SScott Feldman 			vf_port_size * dev_num_vf(dev->dev.parent);
88657b61080SScott Feldman 	else
88757b61080SScott Feldman 		return port_self_size;
88857b61080SScott Feldman }
88957b61080SScott Feldman 
890b5cdae32SDavid S. Miller static size_t rtnl_xdp_size(void)
891d1fdd913SBrenden Blanco {
892b3cfaa31SSabrina Dubroca 	size_t xdp_size = nla_total_size(0) +	/* nest IFLA_XDP */
89358038695SMartin KaFai Lau 			  nla_total_size(1) +	/* XDP_ATTACHED */
89458038695SMartin KaFai Lau 			  nla_total_size(4);	/* XDP_PROG_ID */
895d1fdd913SBrenden Blanco 
896d1fdd913SBrenden Blanco 	return xdp_size;
897d1fdd913SBrenden Blanco }
898d1fdd913SBrenden Blanco 
899115c9b81SGreg Rose static noinline size_t if_nlmsg_size(const struct net_device *dev,
900115c9b81SGreg Rose 				     u32 ext_filter_mask)
901339bf98fSThomas Graf {
902339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
903339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
9040b815a1aSStephen Hemminger 	       + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
905339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
906270cb4d0SNicolas Dichtel 	       + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
907339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rtnl_link_stats))
90835c58459SDavid S. Miller 	       + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
909339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
910339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
911339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_TXQLEN */
912339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_WEIGHT */
913339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MTU */
914339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_LINK */
915339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MASTER */
9169a57247fSJiri Pirko 	       + nla_total_size(1) /* IFLA_CARRIER */
917edbc0bb3SBen Greear 	       + nla_total_size(4) /* IFLA_PROMISCUITY */
91876ff5cc9SJiri Pirko 	       + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
91976ff5cc9SJiri Pirko 	       + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
9206919756cSTobias Klauser 	       + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
9216919756cSTobias Klauser 	       + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
922339bf98fSThomas Graf 	       + nla_total_size(1) /* IFLA_OPERSTATE */
92338f7b870SPatrick McHardy 	       + nla_total_size(1) /* IFLA_LINKMODE */
9242d3b479dSdavid decotigny 	       + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
925d37512a2SNicolas Dichtel 	       + nla_total_size(4) /* IFLA_LINK_NETNSID */
926db833d40SSerhey Popovych 	       + nla_total_size(4) /* IFLA_GROUP */
927115c9b81SGreg Rose 	       + nla_total_size(ext_filter_mask
928115c9b81SGreg Rose 			        & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
929115c9b81SGreg Rose 	       + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
930c53864fdSDavid Gibson 	       + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
931f8ff182cSThomas Graf 	       + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
932b1974ed0SArad, Ronen 	       + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
93382f28412SJiri Pirko 	       + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
93488d6378bSAnuradha Karuppiah 	       + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
935c57c7a95SNicolas Dichtel 	       + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
936b5cdae32SDavid S. Miller 	       + rtnl_xdp_size() /* IFLA_XDP */
9373d3ea5afSVlad Yasevich 	       + nla_total_size(4)  /* IFLA_EVENT */
93888d6378bSAnuradha Karuppiah 	       + nla_total_size(1); /* IFLA_PROTO_DOWN */
93988d6378bSAnuradha Karuppiah 
940339bf98fSThomas Graf }
941339bf98fSThomas Graf 
94257b61080SScott Feldman static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
94357b61080SScott Feldman {
94457b61080SScott Feldman 	struct nlattr *vf_ports;
94557b61080SScott Feldman 	struct nlattr *vf_port;
94657b61080SScott Feldman 	int vf;
94757b61080SScott Feldman 	int err;
94857b61080SScott Feldman 
94957b61080SScott Feldman 	vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
95057b61080SScott Feldman 	if (!vf_ports)
95157b61080SScott Feldman 		return -EMSGSIZE;
95257b61080SScott Feldman 
95357b61080SScott Feldman 	for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
95457b61080SScott Feldman 		vf_port = nla_nest_start(skb, IFLA_VF_PORT);
9558ca94183SScott Feldman 		if (!vf_port)
9568ca94183SScott Feldman 			goto nla_put_failure;
957a6574349SDavid S. Miller 		if (nla_put_u32(skb, IFLA_PORT_VF, vf))
958a6574349SDavid S. Miller 			goto nla_put_failure;
95957b61080SScott Feldman 		err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
9608ca94183SScott Feldman 		if (err == -EMSGSIZE)
9618ca94183SScott Feldman 			goto nla_put_failure;
96257b61080SScott Feldman 		if (err) {
96357b61080SScott Feldman 			nla_nest_cancel(skb, vf_port);
96457b61080SScott Feldman 			continue;
96557b61080SScott Feldman 		}
96657b61080SScott Feldman 		nla_nest_end(skb, vf_port);
96757b61080SScott Feldman 	}
96857b61080SScott Feldman 
96957b61080SScott Feldman 	nla_nest_end(skb, vf_ports);
97057b61080SScott Feldman 
97157b61080SScott Feldman 	return 0;
9728ca94183SScott Feldman 
9738ca94183SScott Feldman nla_put_failure:
9748ca94183SScott Feldman 	nla_nest_cancel(skb, vf_ports);
9758ca94183SScott Feldman 	return -EMSGSIZE;
97657b61080SScott Feldman }
97757b61080SScott Feldman 
97857b61080SScott Feldman static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
97957b61080SScott Feldman {
98057b61080SScott Feldman 	struct nlattr *port_self;
98157b61080SScott Feldman 	int err;
98257b61080SScott Feldman 
98357b61080SScott Feldman 	port_self = nla_nest_start(skb, IFLA_PORT_SELF);
98457b61080SScott Feldman 	if (!port_self)
98557b61080SScott Feldman 		return -EMSGSIZE;
98657b61080SScott Feldman 
98757b61080SScott Feldman 	err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
98857b61080SScott Feldman 	if (err) {
98957b61080SScott Feldman 		nla_nest_cancel(skb, port_self);
9908ca94183SScott Feldman 		return (err == -EMSGSIZE) ? err : 0;
99157b61080SScott Feldman 	}
99257b61080SScott Feldman 
99357b61080SScott Feldman 	nla_nest_end(skb, port_self);
99457b61080SScott Feldman 
99557b61080SScott Feldman 	return 0;
99657b61080SScott Feldman }
99757b61080SScott Feldman 
998c53864fdSDavid Gibson static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
999c53864fdSDavid Gibson 			  u32 ext_filter_mask)
100057b61080SScott Feldman {
100157b61080SScott Feldman 	int err;
100257b61080SScott Feldman 
1003c53864fdSDavid Gibson 	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
1004c53864fdSDavid Gibson 	    !(ext_filter_mask & RTEXT_FILTER_VF))
100557b61080SScott Feldman 		return 0;
100657b61080SScott Feldman 
100757b61080SScott Feldman 	err = rtnl_port_self_fill(skb, dev);
100857b61080SScott Feldman 	if (err)
100957b61080SScott Feldman 		return err;
101057b61080SScott Feldman 
101157b61080SScott Feldman 	if (dev_num_vf(dev->dev.parent)) {
101257b61080SScott Feldman 		err = rtnl_vf_ports_fill(skb, dev);
101357b61080SScott Feldman 		if (err)
101457b61080SScott Feldman 			return err;
101557b61080SScott Feldman 	}
101657b61080SScott Feldman 
101757b61080SScott Feldman 	return 0;
101857b61080SScott Feldman }
101957b61080SScott Feldman 
102066cae9edSJiri Pirko static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
102166cae9edSJiri Pirko {
102266cae9edSJiri Pirko 	int err;
102302637fceSJiri Pirko 	struct netdev_phys_item_id ppid;
102466cae9edSJiri Pirko 
102566cae9edSJiri Pirko 	err = dev_get_phys_port_id(dev, &ppid);
102666cae9edSJiri Pirko 	if (err) {
102766cae9edSJiri Pirko 		if (err == -EOPNOTSUPP)
102866cae9edSJiri Pirko 			return 0;
102966cae9edSJiri Pirko 		return err;
103066cae9edSJiri Pirko 	}
103166cae9edSJiri Pirko 
103266cae9edSJiri Pirko 	if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
103366cae9edSJiri Pirko 		return -EMSGSIZE;
103466cae9edSJiri Pirko 
103566cae9edSJiri Pirko 	return 0;
103666cae9edSJiri Pirko }
103766cae9edSJiri Pirko 
1038db24a904SDavid Ahern static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1039db24a904SDavid Ahern {
1040db24a904SDavid Ahern 	char name[IFNAMSIZ];
1041db24a904SDavid Ahern 	int err;
1042db24a904SDavid Ahern 
1043db24a904SDavid Ahern 	err = dev_get_phys_port_name(dev, name, sizeof(name));
1044db24a904SDavid Ahern 	if (err) {
1045db24a904SDavid Ahern 		if (err == -EOPNOTSUPP)
1046db24a904SDavid Ahern 			return 0;
1047db24a904SDavid Ahern 		return err;
1048db24a904SDavid Ahern 	}
1049db24a904SDavid Ahern 
105077ef033bSMichal Schmidt 	if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
1051db24a904SDavid Ahern 		return -EMSGSIZE;
1052db24a904SDavid Ahern 
1053db24a904SDavid Ahern 	return 0;
1054db24a904SDavid Ahern }
1055db24a904SDavid Ahern 
105682f28412SJiri Pirko static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
105782f28412SJiri Pirko {
105882f28412SJiri Pirko 	int err;
1059f8e20a9fSScott Feldman 	struct switchdev_attr attr = {
10606ff64f6fSIdo Schimmel 		.orig_dev = dev,
10611f868398SJiri Pirko 		.id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
1062f8e20a9fSScott Feldman 		.flags = SWITCHDEV_F_NO_RECURSE,
1063f8e20a9fSScott Feldman 	};
106482f28412SJiri Pirko 
1065f8e20a9fSScott Feldman 	err = switchdev_port_attr_get(dev, &attr);
106682f28412SJiri Pirko 	if (err) {
106782f28412SJiri Pirko 		if (err == -EOPNOTSUPP)
106882f28412SJiri Pirko 			return 0;
106982f28412SJiri Pirko 		return err;
107082f28412SJiri Pirko 	}
107182f28412SJiri Pirko 
107242275bd8SScott Feldman 	if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len,
107342275bd8SScott Feldman 		    attr.u.ppid.id))
107482f28412SJiri Pirko 		return -EMSGSIZE;
107582f28412SJiri Pirko 
107682f28412SJiri Pirko 	return 0;
107782f28412SJiri Pirko }
107882f28412SJiri Pirko 
1079b22b941bSHannes Frederic Sowa static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1080b22b941bSHannes Frederic Sowa 					      struct net_device *dev)
1081b22b941bSHannes Frederic Sowa {
1082550bce59SRoopa Prabhu 	struct rtnl_link_stats64 *sp;
1083b22b941bSHannes Frederic Sowa 	struct nlattr *attr;
1084b22b941bSHannes Frederic Sowa 
108558414d32SNicolas Dichtel 	attr = nla_reserve_64bit(skb, IFLA_STATS64,
108658414d32SNicolas Dichtel 				 sizeof(struct rtnl_link_stats64), IFLA_PAD);
1087b22b941bSHannes Frederic Sowa 	if (!attr)
1088b22b941bSHannes Frederic Sowa 		return -EMSGSIZE;
1089b22b941bSHannes Frederic Sowa 
1090550bce59SRoopa Prabhu 	sp = nla_data(attr);
1091550bce59SRoopa Prabhu 	dev_get_stats(dev, sp);
1092550bce59SRoopa Prabhu 
1093550bce59SRoopa Prabhu 	attr = nla_reserve(skb, IFLA_STATS,
1094550bce59SRoopa Prabhu 			   sizeof(struct rtnl_link_stats));
1095550bce59SRoopa Prabhu 	if (!attr)
1096550bce59SRoopa Prabhu 		return -EMSGSIZE;
1097550bce59SRoopa Prabhu 
1098550bce59SRoopa Prabhu 	copy_rtnl_link_stats(nla_data(attr), sp);
1099b22b941bSHannes Frederic Sowa 
1100b22b941bSHannes Frederic Sowa 	return 0;
1101b22b941bSHannes Frederic Sowa }
1102b22b941bSHannes Frederic Sowa 
1103b22b941bSHannes Frederic Sowa static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1104b22b941bSHannes Frederic Sowa 					       struct net_device *dev,
1105b22b941bSHannes Frederic Sowa 					       int vfs_num,
1106b22b941bSHannes Frederic Sowa 					       struct nlattr *vfinfo)
1107b22b941bSHannes Frederic Sowa {
1108b22b941bSHannes Frederic Sowa 	struct ifla_vf_rss_query_en vf_rss_query_en;
110979aab093SMoshe Shemesh 	struct nlattr *vf, *vfstats, *vfvlanlist;
1110b22b941bSHannes Frederic Sowa 	struct ifla_vf_link_state vf_linkstate;
111179aab093SMoshe Shemesh 	struct ifla_vf_vlan_info vf_vlan_info;
1112b22b941bSHannes Frederic Sowa 	struct ifla_vf_spoofchk vf_spoofchk;
1113b22b941bSHannes Frederic Sowa 	struct ifla_vf_tx_rate vf_tx_rate;
1114b22b941bSHannes Frederic Sowa 	struct ifla_vf_stats vf_stats;
1115b22b941bSHannes Frederic Sowa 	struct ifla_vf_trust vf_trust;
1116b22b941bSHannes Frederic Sowa 	struct ifla_vf_vlan vf_vlan;
1117b22b941bSHannes Frederic Sowa 	struct ifla_vf_rate vf_rate;
1118b22b941bSHannes Frederic Sowa 	struct ifla_vf_mac vf_mac;
1119b22b941bSHannes Frederic Sowa 	struct ifla_vf_info ivi;
1120b22b941bSHannes Frederic Sowa 
11210eed9cf5SMintz, Yuval 	memset(&ivi, 0, sizeof(ivi));
11220eed9cf5SMintz, Yuval 
1123b22b941bSHannes Frederic Sowa 	/* Not all SR-IOV capable drivers support the
1124b22b941bSHannes Frederic Sowa 	 * spoofcheck and "RSS query enable" query.  Preset to
1125b22b941bSHannes Frederic Sowa 	 * -1 so the user space tool can detect that the driver
1126b22b941bSHannes Frederic Sowa 	 * didn't report anything.
1127b22b941bSHannes Frederic Sowa 	 */
1128b22b941bSHannes Frederic Sowa 	ivi.spoofchk = -1;
1129b22b941bSHannes Frederic Sowa 	ivi.rss_query_en = -1;
1130b22b941bSHannes Frederic Sowa 	ivi.trusted = -1;
1131b22b941bSHannes Frederic Sowa 	/* The default value for VF link state is "auto"
1132b22b941bSHannes Frederic Sowa 	 * IFLA_VF_LINK_STATE_AUTO which equals zero
1133b22b941bSHannes Frederic Sowa 	 */
1134b22b941bSHannes Frederic Sowa 	ivi.linkstate = 0;
113579aab093SMoshe Shemesh 	/* VLAN Protocol by default is 802.1Q */
113679aab093SMoshe Shemesh 	ivi.vlan_proto = htons(ETH_P_8021Q);
1137b22b941bSHannes Frederic Sowa 	if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1138b22b941bSHannes Frederic Sowa 		return 0;
1139b22b941bSHannes Frederic Sowa 
1140775f4f05SDan Carpenter 	memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
1141775f4f05SDan Carpenter 
1142b22b941bSHannes Frederic Sowa 	vf_mac.vf =
1143b22b941bSHannes Frederic Sowa 		vf_vlan.vf =
114479aab093SMoshe Shemesh 		vf_vlan_info.vf =
1145b22b941bSHannes Frederic Sowa 		vf_rate.vf =
1146b22b941bSHannes Frederic Sowa 		vf_tx_rate.vf =
1147b22b941bSHannes Frederic Sowa 		vf_spoofchk.vf =
1148b22b941bSHannes Frederic Sowa 		vf_linkstate.vf =
1149b22b941bSHannes Frederic Sowa 		vf_rss_query_en.vf =
1150b22b941bSHannes Frederic Sowa 		vf_trust.vf = ivi.vf;
1151b22b941bSHannes Frederic Sowa 
1152b22b941bSHannes Frederic Sowa 	memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1153b22b941bSHannes Frederic Sowa 	vf_vlan.vlan = ivi.vlan;
1154b22b941bSHannes Frederic Sowa 	vf_vlan.qos = ivi.qos;
115579aab093SMoshe Shemesh 	vf_vlan_info.vlan = ivi.vlan;
115679aab093SMoshe Shemesh 	vf_vlan_info.qos = ivi.qos;
115779aab093SMoshe Shemesh 	vf_vlan_info.vlan_proto = ivi.vlan_proto;
1158b22b941bSHannes Frederic Sowa 	vf_tx_rate.rate = ivi.max_tx_rate;
1159b22b941bSHannes Frederic Sowa 	vf_rate.min_tx_rate = ivi.min_tx_rate;
1160b22b941bSHannes Frederic Sowa 	vf_rate.max_tx_rate = ivi.max_tx_rate;
1161b22b941bSHannes Frederic Sowa 	vf_spoofchk.setting = ivi.spoofchk;
1162b22b941bSHannes Frederic Sowa 	vf_linkstate.link_state = ivi.linkstate;
1163b22b941bSHannes Frederic Sowa 	vf_rss_query_en.setting = ivi.rss_query_en;
1164b22b941bSHannes Frederic Sowa 	vf_trust.setting = ivi.trusted;
1165b22b941bSHannes Frederic Sowa 	vf = nla_nest_start(skb, IFLA_VF_INFO);
116679aab093SMoshe Shemesh 	if (!vf)
116779aab093SMoshe Shemesh 		goto nla_put_vfinfo_failure;
1168b22b941bSHannes Frederic Sowa 	if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1169b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1170b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1171b22b941bSHannes Frederic Sowa 		    &vf_rate) ||
1172b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1173b22b941bSHannes Frederic Sowa 		    &vf_tx_rate) ||
1174b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1175b22b941bSHannes Frederic Sowa 		    &vf_spoofchk) ||
1176b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1177b22b941bSHannes Frederic Sowa 		    &vf_linkstate) ||
1178b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1179b22b941bSHannes Frederic Sowa 		    sizeof(vf_rss_query_en),
1180b22b941bSHannes Frederic Sowa 		    &vf_rss_query_en) ||
1181b22b941bSHannes Frederic Sowa 	    nla_put(skb, IFLA_VF_TRUST,
1182b22b941bSHannes Frederic Sowa 		    sizeof(vf_trust), &vf_trust))
118379aab093SMoshe Shemesh 		goto nla_put_vf_failure;
118479aab093SMoshe Shemesh 	vfvlanlist = nla_nest_start(skb, IFLA_VF_VLAN_LIST);
118579aab093SMoshe Shemesh 	if (!vfvlanlist)
118679aab093SMoshe Shemesh 		goto nla_put_vf_failure;
118779aab093SMoshe Shemesh 	if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
118879aab093SMoshe Shemesh 		    &vf_vlan_info)) {
118979aab093SMoshe Shemesh 		nla_nest_cancel(skb, vfvlanlist);
119079aab093SMoshe Shemesh 		goto nla_put_vf_failure;
119179aab093SMoshe Shemesh 	}
119279aab093SMoshe Shemesh 	nla_nest_end(skb, vfvlanlist);
1193b22b941bSHannes Frederic Sowa 	memset(&vf_stats, 0, sizeof(vf_stats));
1194b22b941bSHannes Frederic Sowa 	if (dev->netdev_ops->ndo_get_vf_stats)
1195b22b941bSHannes Frederic Sowa 		dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1196b22b941bSHannes Frederic Sowa 						&vf_stats);
1197b22b941bSHannes Frederic Sowa 	vfstats = nla_nest_start(skb, IFLA_VF_STATS);
119879aab093SMoshe Shemesh 	if (!vfstats)
119979aab093SMoshe Shemesh 		goto nla_put_vf_failure;
1200343a6d8eSNicolas Dichtel 	if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1201343a6d8eSNicolas Dichtel 			      vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1202343a6d8eSNicolas Dichtel 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1203343a6d8eSNicolas Dichtel 			      vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1204343a6d8eSNicolas Dichtel 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1205343a6d8eSNicolas Dichtel 			      vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1206343a6d8eSNicolas Dichtel 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1207343a6d8eSNicolas Dichtel 			      vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1208343a6d8eSNicolas Dichtel 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1209343a6d8eSNicolas Dichtel 			      vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1210343a6d8eSNicolas Dichtel 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
121179aab093SMoshe Shemesh 			      vf_stats.multicast, IFLA_VF_STATS_PAD)) {
121279aab093SMoshe Shemesh 		nla_nest_cancel(skb, vfstats);
121379aab093SMoshe Shemesh 		goto nla_put_vf_failure;
121479aab093SMoshe Shemesh 	}
1215b22b941bSHannes Frederic Sowa 	nla_nest_end(skb, vfstats);
1216b22b941bSHannes Frederic Sowa 	nla_nest_end(skb, vf);
1217b22b941bSHannes Frederic Sowa 	return 0;
121879aab093SMoshe Shemesh 
121979aab093SMoshe Shemesh nla_put_vf_failure:
122079aab093SMoshe Shemesh 	nla_nest_cancel(skb, vf);
122179aab093SMoshe Shemesh nla_put_vfinfo_failure:
122279aab093SMoshe Shemesh 	nla_nest_cancel(skb, vfinfo);
122379aab093SMoshe Shemesh 	return -EMSGSIZE;
1224b22b941bSHannes Frederic Sowa }
1225b22b941bSHannes Frederic Sowa 
1226b22b941bSHannes Frederic Sowa static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1227b22b941bSHannes Frederic Sowa {
12285f8e4474SKangjie Lu 	struct rtnl_link_ifmap map;
12295f8e4474SKangjie Lu 
12305f8e4474SKangjie Lu 	memset(&map, 0, sizeof(map));
12315f8e4474SKangjie Lu 	map.mem_start   = dev->mem_start;
12325f8e4474SKangjie Lu 	map.mem_end     = dev->mem_end;
12335f8e4474SKangjie Lu 	map.base_addr   = dev->base_addr;
12345f8e4474SKangjie Lu 	map.irq         = dev->irq;
12355f8e4474SKangjie Lu 	map.dma         = dev->dma;
12365f8e4474SKangjie Lu 	map.port        = dev->if_port;
12375f8e4474SKangjie Lu 
1238270cb4d0SNicolas Dichtel 	if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
1239b22b941bSHannes Frederic Sowa 		return -EMSGSIZE;
1240b22b941bSHannes Frederic Sowa 
1241b22b941bSHannes Frederic Sowa 	return 0;
1242b22b941bSHannes Frederic Sowa }
1243b22b941bSHannes Frederic Sowa 
124458038695SMartin KaFai Lau static u8 rtnl_xdp_attached_mode(struct net_device *dev, u32 *prog_id)
1245d67b9cd2SDaniel Borkmann {
1246d67b9cd2SDaniel Borkmann 	const struct net_device_ops *ops = dev->netdev_ops;
124758038695SMartin KaFai Lau 	const struct bpf_prog *generic_xdp_prog;
1248d67b9cd2SDaniel Borkmann 
1249d67b9cd2SDaniel Borkmann 	ASSERT_RTNL();
1250d67b9cd2SDaniel Borkmann 
125158038695SMartin KaFai Lau 	*prog_id = 0;
125258038695SMartin KaFai Lau 	generic_xdp_prog = rtnl_dereference(dev->xdp_prog);
125358038695SMartin KaFai Lau 	if (generic_xdp_prog) {
125458038695SMartin KaFai Lau 		*prog_id = generic_xdp_prog->aux->id;
1255d67b9cd2SDaniel Borkmann 		return XDP_ATTACHED_SKB;
125658038695SMartin KaFai Lau 	}
1257ce158e58SJakub Kicinski 	if (!ops->ndo_xdp)
1258d67b9cd2SDaniel Borkmann 		return XDP_ATTACHED_NONE;
1259ce158e58SJakub Kicinski 
1260ce158e58SJakub Kicinski 	return __dev_xdp_attached(dev, ops->ndo_xdp, prog_id);
1261d67b9cd2SDaniel Borkmann }
1262d67b9cd2SDaniel Borkmann 
1263d1fdd913SBrenden Blanco static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1264d1fdd913SBrenden Blanco {
1265d1fdd913SBrenden Blanco 	struct nlattr *xdp;
126658038695SMartin KaFai Lau 	u32 prog_id;
1267d1fdd913SBrenden Blanco 	int err;
1268d1fdd913SBrenden Blanco 
1269d1fdd913SBrenden Blanco 	xdp = nla_nest_start(skb, IFLA_XDP);
1270d1fdd913SBrenden Blanco 	if (!xdp)
1271d1fdd913SBrenden Blanco 		return -EMSGSIZE;
1272b5cdae32SDavid S. Miller 
1273d67b9cd2SDaniel Borkmann 	err = nla_put_u8(skb, IFLA_XDP_ATTACHED,
127458038695SMartin KaFai Lau 			 rtnl_xdp_attached_mode(dev, &prog_id));
1275d1fdd913SBrenden Blanco 	if (err)
1276d1fdd913SBrenden Blanco 		goto err_cancel;
1277d1fdd913SBrenden Blanco 
127858038695SMartin KaFai Lau 	if (prog_id) {
127958038695SMartin KaFai Lau 		err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
128058038695SMartin KaFai Lau 		if (err)
128158038695SMartin KaFai Lau 			goto err_cancel;
128258038695SMartin KaFai Lau 	}
128358038695SMartin KaFai Lau 
1284d1fdd913SBrenden Blanco 	nla_nest_end(skb, xdp);
1285d1fdd913SBrenden Blanco 	return 0;
1286d1fdd913SBrenden Blanco 
1287d1fdd913SBrenden Blanco err_cancel:
1288d1fdd913SBrenden Blanco 	nla_nest_cancel(skb, xdp);
1289d1fdd913SBrenden Blanco 	return err;
1290d1fdd913SBrenden Blanco }
1291d1fdd913SBrenden Blanco 
12923d3ea5afSVlad Yasevich static u32 rtnl_get_event(unsigned long event)
12933d3ea5afSVlad Yasevich {
12943d3ea5afSVlad Yasevich 	u32 rtnl_event_type = IFLA_EVENT_NONE;
12953d3ea5afSVlad Yasevich 
12963d3ea5afSVlad Yasevich 	switch (event) {
12973d3ea5afSVlad Yasevich 	case NETDEV_REBOOT:
12983d3ea5afSVlad Yasevich 		rtnl_event_type = IFLA_EVENT_REBOOT;
12993d3ea5afSVlad Yasevich 		break;
13003d3ea5afSVlad Yasevich 	case NETDEV_FEAT_CHANGE:
13013d3ea5afSVlad Yasevich 		rtnl_event_type = IFLA_EVENT_FEATURES;
13023d3ea5afSVlad Yasevich 		break;
13033d3ea5afSVlad Yasevich 	case NETDEV_BONDING_FAILOVER:
13043d3ea5afSVlad Yasevich 		rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER;
13053d3ea5afSVlad Yasevich 		break;
13063d3ea5afSVlad Yasevich 	case NETDEV_NOTIFY_PEERS:
13073d3ea5afSVlad Yasevich 		rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS;
13083d3ea5afSVlad Yasevich 		break;
13093d3ea5afSVlad Yasevich 	case NETDEV_RESEND_IGMP:
13103d3ea5afSVlad Yasevich 		rtnl_event_type = IFLA_EVENT_IGMP_RESEND;
13113d3ea5afSVlad Yasevich 		break;
13123d3ea5afSVlad Yasevich 	case NETDEV_CHANGEINFODATA:
13133d3ea5afSVlad Yasevich 		rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS;
13143d3ea5afSVlad Yasevich 		break;
13153d3ea5afSVlad Yasevich 	default:
13163d3ea5afSVlad Yasevich 		break;
13173d3ea5afSVlad Yasevich 	}
13183d3ea5afSVlad Yasevich 
13193d3ea5afSVlad Yasevich 	return rtnl_event_type;
13203d3ea5afSVlad Yasevich }
13213d3ea5afSVlad Yasevich 
1322b60c5115SThomas Graf static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
1323575c3e2aSPatrick McHardy 			    int type, u32 pid, u32 seq, u32 change,
13243d3ea5afSVlad Yasevich 			    unsigned int flags, u32 ext_filter_mask,
13253d3ea5afSVlad Yasevich 			    u32 event)
1326b60c5115SThomas Graf {
1327b60c5115SThomas Graf 	struct ifinfomsg *ifm;
13281da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
1329b22b941bSHannes Frederic Sowa 	struct nlattr *af_spec;
1330f8ff182cSThomas Graf 	struct rtnl_af_ops *af_ops;
1331898e5061SJiri Pirko 	struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
13321da177e4SLinus Torvalds 
13332907c35fSEric Dumazet 	ASSERT_RTNL();
1334b60c5115SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1335b60c5115SThomas Graf 	if (nlh == NULL)
133626932566SPatrick McHardy 		return -EMSGSIZE;
13371da177e4SLinus Torvalds 
1338b60c5115SThomas Graf 	ifm = nlmsg_data(nlh);
1339b60c5115SThomas Graf 	ifm->ifi_family = AF_UNSPEC;
1340b60c5115SThomas Graf 	ifm->__ifi_pad = 0;
1341b60c5115SThomas Graf 	ifm->ifi_type = dev->type;
1342b60c5115SThomas Graf 	ifm->ifi_index = dev->ifindex;
1343b60c5115SThomas Graf 	ifm->ifi_flags = dev_get_flags(dev);
1344b60c5115SThomas Graf 	ifm->ifi_change = change;
13451da177e4SLinus Torvalds 
1346a6574349SDavid S. Miller 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1347a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1348a6574349SDavid S. Miller 	    nla_put_u8(skb, IFLA_OPERSTATE,
1349a6574349SDavid S. Miller 		       netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1350a6574349SDavid S. Miller 	    nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1351a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1352a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_GROUP, dev->group) ||
1353edbc0bb3SBen Greear 	    nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
135476ff5cc9SJiri Pirko 	    nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
1355c70ce028SEric Dumazet 	    nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1356c70ce028SEric Dumazet 	    nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
13571d69c2b3SMark A. Greer #ifdef CONFIG_RPS
135876ff5cc9SJiri Pirko 	    nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
13591d69c2b3SMark A. Greer #endif
1360a54acb3aSNicolas Dichtel 	    (dev->ifindex != dev_get_iflink(dev) &&
1361a54acb3aSNicolas Dichtel 	     nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
1362898e5061SJiri Pirko 	    (upper_dev &&
1363898e5061SJiri Pirko 	     nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) ||
13649a57247fSJiri Pirko 	    nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
1365a6574349SDavid S. Miller 	    (dev->qdisc &&
1366a6574349SDavid S. Miller 	     nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
1367a6574349SDavid S. Miller 	    (dev->ifalias &&
13682d3b479dSdavid decotigny 	     nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)) ||
13692d3b479dSdavid decotigny 	    nla_put_u32(skb, IFLA_CARRIER_CHANGES,
137088d6378bSAnuradha Karuppiah 			atomic_read(&dev->carrier_changes)) ||
137188d6378bSAnuradha Karuppiah 	    nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down))
1372a6574349SDavid S. Miller 		goto nla_put_failure;
13730b815a1aSStephen Hemminger 
13743d3ea5afSVlad Yasevich 	if (event != IFLA_EVENT_NONE) {
13753d3ea5afSVlad Yasevich 		if (nla_put_u32(skb, IFLA_EVENT, event))
13763d3ea5afSVlad Yasevich 			goto nla_put_failure;
13773d3ea5afSVlad Yasevich 	}
13783d3ea5afSVlad Yasevich 
1379b22b941bSHannes Frederic Sowa 	if (rtnl_fill_link_ifmap(skb, dev))
1380a6574349SDavid S. Miller 		goto nla_put_failure;
13811da177e4SLinus Torvalds 
13821da177e4SLinus Torvalds 	if (dev->addr_len) {
1383a6574349SDavid S. Miller 		if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1384a6574349SDavid S. Miller 		    nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1385a6574349SDavid S. Miller 			goto nla_put_failure;
13861da177e4SLinus Torvalds 	}
13871da177e4SLinus Torvalds 
138866cae9edSJiri Pirko 	if (rtnl_phys_port_id_fill(skb, dev))
138966cae9edSJiri Pirko 		goto nla_put_failure;
139066cae9edSJiri Pirko 
1391db24a904SDavid Ahern 	if (rtnl_phys_port_name_fill(skb, dev))
1392db24a904SDavid Ahern 		goto nla_put_failure;
1393db24a904SDavid Ahern 
139482f28412SJiri Pirko 	if (rtnl_phys_switch_id_fill(skb, dev))
139582f28412SJiri Pirko 		goto nla_put_failure;
139682f28412SJiri Pirko 
1397b22b941bSHannes Frederic Sowa 	if (rtnl_fill_stats(skb, dev))
1398b60c5115SThomas Graf 		goto nla_put_failure;
1399b60c5115SThomas Graf 
1400a6574349SDavid S. Miller 	if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) &&
1401a6574349SDavid S. Miller 	    nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)))
1402a6574349SDavid S. Miller 		goto nla_put_failure;
140357b61080SScott Feldman 
1404b22b941bSHannes Frederic Sowa 	if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent &&
1405b22b941bSHannes Frederic Sowa 	    ext_filter_mask & RTEXT_FILTER_VF) {
1406ebc08a6fSWilliams, Mitch A 		int i;
1407b22b941bSHannes Frederic Sowa 		struct nlattr *vfinfo;
1408c02db8c6SChris Wright 		int num_vfs = dev_num_vf(dev->dev.parent);
1409c02db8c6SChris Wright 
1410c02db8c6SChris Wright 		vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1411c02db8c6SChris Wright 		if (!vfinfo)
1412c02db8c6SChris Wright 			goto nla_put_failure;
1413c02db8c6SChris Wright 		for (i = 0; i < num_vfs; i++) {
1414b22b941bSHannes Frederic Sowa 			if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
1415b22b941bSHannes Frederic Sowa 				goto nla_put_failure;
1416b22b941bSHannes Frederic Sowa 		}
14175f8444a3SGreg Rose 
1418c02db8c6SChris Wright 		nla_nest_end(skb, vfinfo);
1419ebc08a6fSWilliams, Mitch A 	}
142057b61080SScott Feldman 
1421c53864fdSDavid Gibson 	if (rtnl_port_fill(skb, dev, ext_filter_mask))
142257b61080SScott Feldman 		goto nla_put_failure;
142357b61080SScott Feldman 
1424d1fdd913SBrenden Blanco 	if (rtnl_xdp_fill(skb, dev))
1425d1fdd913SBrenden Blanco 		goto nla_put_failure;
1426d1fdd913SBrenden Blanco 
1427ba7d49b1SJiri Pirko 	if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
142838f7b870SPatrick McHardy 		if (rtnl_link_fill(skb, dev) < 0)
142938f7b870SPatrick McHardy 			goto nla_put_failure;
143038f7b870SPatrick McHardy 	}
143138f7b870SPatrick McHardy 
1432d37512a2SNicolas Dichtel 	if (dev->rtnl_link_ops &&
1433d37512a2SNicolas Dichtel 	    dev->rtnl_link_ops->get_link_net) {
1434d37512a2SNicolas Dichtel 		struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1435d37512a2SNicolas Dichtel 
1436d37512a2SNicolas Dichtel 		if (!net_eq(dev_net(dev), link_net)) {
14377a0877d4SNicolas Dichtel 			int id = peernet2id_alloc(dev_net(dev), link_net);
1438d37512a2SNicolas Dichtel 
1439d37512a2SNicolas Dichtel 			if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1440d37512a2SNicolas Dichtel 				goto nla_put_failure;
1441d37512a2SNicolas Dichtel 		}
1442d37512a2SNicolas Dichtel 	}
1443d37512a2SNicolas Dichtel 
1444f8ff182cSThomas Graf 	if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
1445f8ff182cSThomas Graf 		goto nla_put_failure;
1446f8ff182cSThomas Graf 
1447f8ff182cSThomas Graf 	list_for_each_entry(af_ops, &rtnl_af_ops, list) {
1448f8ff182cSThomas Graf 		if (af_ops->fill_link_af) {
1449f8ff182cSThomas Graf 			struct nlattr *af;
1450f8ff182cSThomas Graf 			int err;
1451f8ff182cSThomas Graf 
1452f8ff182cSThomas Graf 			if (!(af = nla_nest_start(skb, af_ops->family)))
1453f8ff182cSThomas Graf 				goto nla_put_failure;
1454f8ff182cSThomas Graf 
1455d5566fd7SSowmini Varadhan 			err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1456f8ff182cSThomas Graf 
1457f8ff182cSThomas Graf 			/*
1458f8ff182cSThomas Graf 			 * Caller may return ENODATA to indicate that there
1459f8ff182cSThomas Graf 			 * was no data to be dumped. This is not an error, it
1460f8ff182cSThomas Graf 			 * means we should trim the attribute header and
1461f8ff182cSThomas Graf 			 * continue.
1462f8ff182cSThomas Graf 			 */
1463f8ff182cSThomas Graf 			if (err == -ENODATA)
1464f8ff182cSThomas Graf 				nla_nest_cancel(skb, af);
1465f8ff182cSThomas Graf 			else if (err < 0)
1466f8ff182cSThomas Graf 				goto nla_put_failure;
1467f8ff182cSThomas Graf 
1468f8ff182cSThomas Graf 			nla_nest_end(skb, af);
1469f8ff182cSThomas Graf 		}
1470f8ff182cSThomas Graf 	}
1471f8ff182cSThomas Graf 
1472f8ff182cSThomas Graf 	nla_nest_end(skb, af_spec);
1473f8ff182cSThomas Graf 
1474053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
1475053c095aSJohannes Berg 	return 0;
1476b60c5115SThomas Graf 
1477b60c5115SThomas Graf nla_put_failure:
147826932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
147926932566SPatrick McHardy 	return -EMSGSIZE;
14801da177e4SLinus Torvalds }
14811da177e4SLinus Torvalds 
1482f7b12606SJiri Pirko static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1483f7b12606SJiri Pirko 	[IFLA_IFNAME]		= { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1484f7b12606SJiri Pirko 	[IFLA_ADDRESS]		= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1485f7b12606SJiri Pirko 	[IFLA_BROADCAST]	= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1486f7b12606SJiri Pirko 	[IFLA_MAP]		= { .len = sizeof(struct rtnl_link_ifmap) },
1487f7b12606SJiri Pirko 	[IFLA_MTU]		= { .type = NLA_U32 },
1488f7b12606SJiri Pirko 	[IFLA_LINK]		= { .type = NLA_U32 },
1489f7b12606SJiri Pirko 	[IFLA_MASTER]		= { .type = NLA_U32 },
1490f7b12606SJiri Pirko 	[IFLA_CARRIER]		= { .type = NLA_U8 },
1491f7b12606SJiri Pirko 	[IFLA_TXQLEN]		= { .type = NLA_U32 },
1492f7b12606SJiri Pirko 	[IFLA_WEIGHT]		= { .type = NLA_U32 },
1493f7b12606SJiri Pirko 	[IFLA_OPERSTATE]	= { .type = NLA_U8 },
1494f7b12606SJiri Pirko 	[IFLA_LINKMODE]		= { .type = NLA_U8 },
1495f7b12606SJiri Pirko 	[IFLA_LINKINFO]		= { .type = NLA_NESTED },
1496f7b12606SJiri Pirko 	[IFLA_NET_NS_PID]	= { .type = NLA_U32 },
1497f7b12606SJiri Pirko 	[IFLA_NET_NS_FD]	= { .type = NLA_U32 },
1498f7b12606SJiri Pirko 	[IFLA_IFALIAS]	        = { .type = NLA_STRING, .len = IFALIASZ-1 },
1499f7b12606SJiri Pirko 	[IFLA_VFINFO_LIST]	= {. type = NLA_NESTED },
1500f7b12606SJiri Pirko 	[IFLA_VF_PORTS]		= { .type = NLA_NESTED },
1501f7b12606SJiri Pirko 	[IFLA_PORT_SELF]	= { .type = NLA_NESTED },
1502f7b12606SJiri Pirko 	[IFLA_AF_SPEC]		= { .type = NLA_NESTED },
1503f7b12606SJiri Pirko 	[IFLA_EXT_MASK]		= { .type = NLA_U32 },
1504f7b12606SJiri Pirko 	[IFLA_PROMISCUITY]	= { .type = NLA_U32 },
1505f7b12606SJiri Pirko 	[IFLA_NUM_TX_QUEUES]	= { .type = NLA_U32 },
1506f7b12606SJiri Pirko 	[IFLA_NUM_RX_QUEUES]	= { .type = NLA_U32 },
150702637fceSJiri Pirko 	[IFLA_PHYS_PORT_ID]	= { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
15082d3b479dSdavid decotigny 	[IFLA_CARRIER_CHANGES]	= { .type = NLA_U32 },  /* ignored */
150982f28412SJiri Pirko 	[IFLA_PHYS_SWITCH_ID]	= { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1510317f4810SNicolas Dichtel 	[IFLA_LINK_NETNSID]	= { .type = NLA_S32 },
151188d6378bSAnuradha Karuppiah 	[IFLA_PROTO_DOWN]	= { .type = NLA_U8 },
1512d1fdd913SBrenden Blanco 	[IFLA_XDP]		= { .type = NLA_NESTED },
15133d3ea5afSVlad Yasevich 	[IFLA_EVENT]		= { .type = NLA_U32 },
1514db833d40SSerhey Popovych 	[IFLA_GROUP]		= { .type = NLA_U32 },
1515f7b12606SJiri Pirko };
1516f7b12606SJiri Pirko 
1517f7b12606SJiri Pirko static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1518f7b12606SJiri Pirko 	[IFLA_INFO_KIND]	= { .type = NLA_STRING },
1519f7b12606SJiri Pirko 	[IFLA_INFO_DATA]	= { .type = NLA_NESTED },
1520f7b12606SJiri Pirko 	[IFLA_INFO_SLAVE_KIND]	= { .type = NLA_STRING },
1521f7b12606SJiri Pirko 	[IFLA_INFO_SLAVE_DATA]	= { .type = NLA_NESTED },
1522f7b12606SJiri Pirko };
1523f7b12606SJiri Pirko 
1524f7b12606SJiri Pirko static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1525364d5716SDaniel Borkmann 	[IFLA_VF_MAC]		= { .len = sizeof(struct ifla_vf_mac) },
1526364d5716SDaniel Borkmann 	[IFLA_VF_VLAN]		= { .len = sizeof(struct ifla_vf_vlan) },
152779aab093SMoshe Shemesh 	[IFLA_VF_VLAN_LIST]     = { .type = NLA_NESTED },
1528364d5716SDaniel Borkmann 	[IFLA_VF_TX_RATE]	= { .len = sizeof(struct ifla_vf_tx_rate) },
1529364d5716SDaniel Borkmann 	[IFLA_VF_SPOOFCHK]	= { .len = sizeof(struct ifla_vf_spoofchk) },
1530364d5716SDaniel Borkmann 	[IFLA_VF_RATE]		= { .len = sizeof(struct ifla_vf_rate) },
1531364d5716SDaniel Borkmann 	[IFLA_VF_LINK_STATE]	= { .len = sizeof(struct ifla_vf_link_state) },
153201a3d796SVlad Zolotarov 	[IFLA_VF_RSS_QUERY_EN]	= { .len = sizeof(struct ifla_vf_rss_query_en) },
15333b766cd8SEran Ben Elisha 	[IFLA_VF_STATS]		= { .type = NLA_NESTED },
1534dd461d6aSHiroshi Shimamoto 	[IFLA_VF_TRUST]		= { .len = sizeof(struct ifla_vf_trust) },
1535cc8e27ccSEli Cohen 	[IFLA_VF_IB_NODE_GUID]	= { .len = sizeof(struct ifla_vf_guid) },
1536cc8e27ccSEli Cohen 	[IFLA_VF_IB_PORT_GUID]	= { .len = sizeof(struct ifla_vf_guid) },
15373b766cd8SEran Ben Elisha };
15383b766cd8SEran Ben Elisha 
1539f7b12606SJiri Pirko static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1540f7b12606SJiri Pirko 	[IFLA_PORT_VF]		= { .type = NLA_U32 },
1541f7b12606SJiri Pirko 	[IFLA_PORT_PROFILE]	= { .type = NLA_STRING,
1542f7b12606SJiri Pirko 				    .len = PORT_PROFILE_MAX },
1543f7b12606SJiri Pirko 	[IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1544f7b12606SJiri Pirko 				      .len = PORT_UUID_MAX },
1545f7b12606SJiri Pirko 	[IFLA_PORT_HOST_UUID]	= { .type = NLA_STRING,
1546f7b12606SJiri Pirko 				    .len = PORT_UUID_MAX },
1547f7b12606SJiri Pirko 	[IFLA_PORT_REQUEST]	= { .type = NLA_U8, },
1548f7b12606SJiri Pirko 	[IFLA_PORT_RESPONSE]	= { .type = NLA_U16, },
1549025331dfSDaniel Borkmann 
1550025331dfSDaniel Borkmann 	/* Unused, but we need to keep it here since user space could
1551025331dfSDaniel Borkmann 	 * fill it. It's also broken with regard to NLA_BINARY use in
1552025331dfSDaniel Borkmann 	 * combination with structs.
1553025331dfSDaniel Borkmann 	 */
1554025331dfSDaniel Borkmann 	[IFLA_PORT_VSI_TYPE]	= { .type = NLA_BINARY,
1555025331dfSDaniel Borkmann 				    .len = sizeof(struct ifla_port_vsi) },
1556f7b12606SJiri Pirko };
1557f7b12606SJiri Pirko 
1558d1fdd913SBrenden Blanco static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
1559d1fdd913SBrenden Blanco 	[IFLA_XDP_FD]		= { .type = NLA_S32 },
1560d1fdd913SBrenden Blanco 	[IFLA_XDP_ATTACHED]	= { .type = NLA_U8 },
156185de8576SDaniel Borkmann 	[IFLA_XDP_FLAGS]	= { .type = NLA_U32 },
156258038695SMartin KaFai Lau 	[IFLA_XDP_PROG_ID]	= { .type = NLA_U32 },
1563d1fdd913SBrenden Blanco };
1564d1fdd913SBrenden Blanco 
1565dc599f76SDavid Ahern static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1566dc599f76SDavid Ahern {
1567dc599f76SDavid Ahern 	const struct rtnl_link_ops *ops = NULL;
1568dc599f76SDavid Ahern 	struct nlattr *linfo[IFLA_INFO_MAX + 1];
1569dc599f76SDavid Ahern 
1570fceb6435SJohannes Berg 	if (nla_parse_nested(linfo, IFLA_INFO_MAX, nla,
1571fceb6435SJohannes Berg 			     ifla_info_policy, NULL) < 0)
1572dc599f76SDavid Ahern 		return NULL;
1573dc599f76SDavid Ahern 
1574dc599f76SDavid Ahern 	if (linfo[IFLA_INFO_KIND]) {
1575dc599f76SDavid Ahern 		char kind[MODULE_NAME_LEN];
1576dc599f76SDavid Ahern 
1577dc599f76SDavid Ahern 		nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1578dc599f76SDavid Ahern 		ops = rtnl_link_ops_get(kind);
1579dc599f76SDavid Ahern 	}
1580dc599f76SDavid Ahern 
1581dc599f76SDavid Ahern 	return ops;
1582dc599f76SDavid Ahern }
1583dc599f76SDavid Ahern 
1584dc599f76SDavid Ahern static bool link_master_filtered(struct net_device *dev, int master_idx)
1585dc599f76SDavid Ahern {
1586dc599f76SDavid Ahern 	struct net_device *master;
1587dc599f76SDavid Ahern 
1588dc599f76SDavid Ahern 	if (!master_idx)
1589dc599f76SDavid Ahern 		return false;
1590dc599f76SDavid Ahern 
1591dc599f76SDavid Ahern 	master = netdev_master_upper_dev_get(dev);
1592dc599f76SDavid Ahern 	if (!master || master->ifindex != master_idx)
1593dc599f76SDavid Ahern 		return true;
1594dc599f76SDavid Ahern 
1595dc599f76SDavid Ahern 	return false;
1596dc599f76SDavid Ahern }
1597dc599f76SDavid Ahern 
1598dc599f76SDavid Ahern static bool link_kind_filtered(const struct net_device *dev,
1599dc599f76SDavid Ahern 			       const struct rtnl_link_ops *kind_ops)
1600dc599f76SDavid Ahern {
1601dc599f76SDavid Ahern 	if (kind_ops && dev->rtnl_link_ops != kind_ops)
1602dc599f76SDavid Ahern 		return true;
1603dc599f76SDavid Ahern 
1604dc599f76SDavid Ahern 	return false;
1605dc599f76SDavid Ahern }
1606dc599f76SDavid Ahern 
1607dc599f76SDavid Ahern static bool link_dump_filtered(struct net_device *dev,
1608dc599f76SDavid Ahern 			       int master_idx,
1609dc599f76SDavid Ahern 			       const struct rtnl_link_ops *kind_ops)
1610dc599f76SDavid Ahern {
1611dc599f76SDavid Ahern 	if (link_master_filtered(dev, master_idx) ||
1612dc599f76SDavid Ahern 	    link_kind_filtered(dev, kind_ops))
1613dc599f76SDavid Ahern 		return true;
1614dc599f76SDavid Ahern 
1615dc599f76SDavid Ahern 	return false;
1616dc599f76SDavid Ahern }
1617dc599f76SDavid Ahern 
1618b60c5115SThomas Graf static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
16191da177e4SLinus Torvalds {
16203b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
16217c28bd0bSEric Dumazet 	int h, s_h;
16227c28bd0bSEric Dumazet 	int idx = 0, s_idx;
16231da177e4SLinus Torvalds 	struct net_device *dev;
16247c28bd0bSEric Dumazet 	struct hlist_head *head;
1625115c9b81SGreg Rose 	struct nlattr *tb[IFLA_MAX+1];
1626115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
1627dc599f76SDavid Ahern 	const struct rtnl_link_ops *kind_ops = NULL;
1628dc599f76SDavid Ahern 	unsigned int flags = NLM_F_MULTI;
1629dc599f76SDavid Ahern 	int master_idx = 0;
1630973462bbSDavid Gibson 	int err;
1631e5eca6d4SMichal Schmidt 	int hdrlen;
16321da177e4SLinus Torvalds 
16337c28bd0bSEric Dumazet 	s_h = cb->args[0];
16347c28bd0bSEric Dumazet 	s_idx = cb->args[1];
16357c28bd0bSEric Dumazet 
16364e985adaSThomas Graf 	cb->seq = net->dev_base_seq;
16374e985adaSThomas Graf 
1638e5eca6d4SMichal Schmidt 	/* A hack to preserve kernel<->userspace interface.
1639e5eca6d4SMichal Schmidt 	 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1640e5eca6d4SMichal Schmidt 	 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1641e5eca6d4SMichal Schmidt 	 * what iproute2 < v3.9.0 used.
1642e5eca6d4SMichal Schmidt 	 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1643e5eca6d4SMichal Schmidt 	 * attribute, its netlink message is shorter than struct ifinfomsg.
1644e5eca6d4SMichal Schmidt 	 */
1645e5eca6d4SMichal Schmidt 	hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ?
1646e5eca6d4SMichal Schmidt 		 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1647e5eca6d4SMichal Schmidt 
1648fceb6435SJohannes Berg 	if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX,
1649fceb6435SJohannes Berg 			ifla_policy, NULL) >= 0) {
1650115c9b81SGreg Rose 		if (tb[IFLA_EXT_MASK])
1651115c9b81SGreg Rose 			ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1652dc599f76SDavid Ahern 
1653dc599f76SDavid Ahern 		if (tb[IFLA_MASTER])
1654dc599f76SDavid Ahern 			master_idx = nla_get_u32(tb[IFLA_MASTER]);
1655dc599f76SDavid Ahern 
1656dc599f76SDavid Ahern 		if (tb[IFLA_LINKINFO])
1657dc599f76SDavid Ahern 			kind_ops = linkinfo_to_kind_ops(tb[IFLA_LINKINFO]);
1658dc599f76SDavid Ahern 
1659dc599f76SDavid Ahern 		if (master_idx || kind_ops)
1660dc599f76SDavid Ahern 			flags |= NLM_F_DUMP_FILTERED;
1661a4b64fbeSEric Dumazet 	}
1662115c9b81SGreg Rose 
16637c28bd0bSEric Dumazet 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
16647562f876SPavel Emelianov 		idx = 0;
16657c28bd0bSEric Dumazet 		head = &net->dev_index_head[h];
1666cac5e65eSEric Dumazet 		hlist_for_each_entry(dev, head, index_hlist) {
1667dc599f76SDavid Ahern 			if (link_dump_filtered(dev, master_idx, kind_ops))
16683f0ae05dSZhang Shengju 				goto cont;
16691da177e4SLinus Torvalds 			if (idx < s_idx)
16707562f876SPavel Emelianov 				goto cont;
1671973462bbSDavid Gibson 			err = rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
167215e47304SEric W. Biederman 					       NETLINK_CB(cb->skb).portid,
16737c28bd0bSEric Dumazet 					       cb->nlh->nlmsg_seq, 0,
1674dc599f76SDavid Ahern 					       flags,
16753d3ea5afSVlad Yasevich 					       ext_filter_mask, 0);
1676973462bbSDavid Gibson 
1677f6c5775fSDavid Ahern 			if (err < 0) {
1678f6c5775fSDavid Ahern 				if (likely(skb->len))
16797c28bd0bSEric Dumazet 					goto out;
16804e985adaSThomas Graf 
1681f6c5775fSDavid Ahern 				goto out_err;
1682f6c5775fSDavid Ahern 			}
1683f6c5775fSDavid Ahern 
16844e985adaSThomas Graf 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
16857562f876SPavel Emelianov cont:
16867562f876SPavel Emelianov 			idx++;
16871da177e4SLinus Torvalds 		}
16887c28bd0bSEric Dumazet 	}
16897c28bd0bSEric Dumazet out:
1690f6c5775fSDavid Ahern 	err = skb->len;
1691f6c5775fSDavid Ahern out_err:
16927c28bd0bSEric Dumazet 	cb->args[1] = idx;
16937c28bd0bSEric Dumazet 	cb->args[0] = h;
16941da177e4SLinus Torvalds 
1695f6c5775fSDavid Ahern 	return err;
16961da177e4SLinus Torvalds }
16971da177e4SLinus Torvalds 
1698fceb6435SJohannes Berg int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len,
1699fceb6435SJohannes Berg 			struct netlink_ext_ack *exterr)
1700f7b12606SJiri Pirko {
1701fceb6435SJohannes Berg 	return nla_parse(tb, IFLA_MAX, head, len, ifla_policy, exterr);
1702f7b12606SJiri Pirko }
1703f7b12606SJiri Pirko EXPORT_SYMBOL(rtnl_nla_parse_ifla);
170457b61080SScott Feldman 
170581adee47SEric W. Biederman struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
170681adee47SEric W. Biederman {
170781adee47SEric W. Biederman 	struct net *net;
170881adee47SEric W. Biederman 	/* Examine the link attributes and figure out which
170981adee47SEric W. Biederman 	 * network namespace we are talking about.
171081adee47SEric W. Biederman 	 */
171181adee47SEric W. Biederman 	if (tb[IFLA_NET_NS_PID])
171281adee47SEric W. Biederman 		net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
1713f0630529SEric W. Biederman 	else if (tb[IFLA_NET_NS_FD])
1714f0630529SEric W. Biederman 		net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
171581adee47SEric W. Biederman 	else
171681adee47SEric W. Biederman 		net = get_net(src_net);
171781adee47SEric W. Biederman 	return net;
171881adee47SEric W. Biederman }
171981adee47SEric W. Biederman EXPORT_SYMBOL(rtnl_link_get_net);
172081adee47SEric W. Biederman 
17211840bb13SThomas Graf static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
17221840bb13SThomas Graf {
17231840bb13SThomas Graf 	if (dev) {
17241840bb13SThomas Graf 		if (tb[IFLA_ADDRESS] &&
17251840bb13SThomas Graf 		    nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
17261840bb13SThomas Graf 			return -EINVAL;
17271840bb13SThomas Graf 
17281840bb13SThomas Graf 		if (tb[IFLA_BROADCAST] &&
17291840bb13SThomas Graf 		    nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
17301840bb13SThomas Graf 			return -EINVAL;
17311840bb13SThomas Graf 	}
17321840bb13SThomas Graf 
1733cf7afbfeSThomas Graf 	if (tb[IFLA_AF_SPEC]) {
1734cf7afbfeSThomas Graf 		struct nlattr *af;
1735cf7afbfeSThomas Graf 		int rem, err;
1736cf7afbfeSThomas Graf 
1737cf7afbfeSThomas Graf 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1738cf7afbfeSThomas Graf 			const struct rtnl_af_ops *af_ops;
1739cf7afbfeSThomas Graf 
1740cf7afbfeSThomas Graf 			if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1741cf7afbfeSThomas Graf 				return -EAFNOSUPPORT;
1742cf7afbfeSThomas Graf 
1743cf7afbfeSThomas Graf 			if (!af_ops->set_link_af)
1744cf7afbfeSThomas Graf 				return -EOPNOTSUPP;
1745cf7afbfeSThomas Graf 
1746cf7afbfeSThomas Graf 			if (af_ops->validate_link_af) {
17476d3a9a68SKurt Van Dijck 				err = af_ops->validate_link_af(dev, af);
1748cf7afbfeSThomas Graf 				if (err < 0)
1749cf7afbfeSThomas Graf 					return err;
1750cf7afbfeSThomas Graf 			}
1751cf7afbfeSThomas Graf 		}
1752cf7afbfeSThomas Graf 	}
1753cf7afbfeSThomas Graf 
17541840bb13SThomas Graf 	return 0;
17551840bb13SThomas Graf }
17561840bb13SThomas Graf 
1757cc8e27ccSEli Cohen static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
1758cc8e27ccSEli Cohen 				  int guid_type)
1759cc8e27ccSEli Cohen {
1760cc8e27ccSEli Cohen 	const struct net_device_ops *ops = dev->netdev_ops;
1761cc8e27ccSEli Cohen 
1762cc8e27ccSEli Cohen 	return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
1763cc8e27ccSEli Cohen }
1764cc8e27ccSEli Cohen 
1765cc8e27ccSEli Cohen static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
1766cc8e27ccSEli Cohen {
1767cc8e27ccSEli Cohen 	if (dev->type != ARPHRD_INFINIBAND)
1768cc8e27ccSEli Cohen 		return -EOPNOTSUPP;
1769cc8e27ccSEli Cohen 
1770cc8e27ccSEli Cohen 	return handle_infiniband_guid(dev, ivt, guid_type);
1771cc8e27ccSEli Cohen }
1772cc8e27ccSEli Cohen 
17734f7d2cdfSDaniel Borkmann static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
1774c02db8c6SChris Wright {
1775c02db8c6SChris Wright 	const struct net_device_ops *ops = dev->netdev_ops;
17764f7d2cdfSDaniel Borkmann 	int err = -EINVAL;
1777c02db8c6SChris Wright 
17784f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_MAC]) {
17794f7d2cdfSDaniel Borkmann 		struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
17804f7d2cdfSDaniel Borkmann 
1781c02db8c6SChris Wright 		err = -EOPNOTSUPP;
1782c02db8c6SChris Wright 		if (ops->ndo_set_vf_mac)
1783c02db8c6SChris Wright 			err = ops->ndo_set_vf_mac(dev, ivm->vf,
1784c02db8c6SChris Wright 						  ivm->mac);
17854f7d2cdfSDaniel Borkmann 		if (err < 0)
17864f7d2cdfSDaniel Borkmann 			return err;
1787c02db8c6SChris Wright 	}
17884f7d2cdfSDaniel Borkmann 
17894f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_VLAN]) {
17904f7d2cdfSDaniel Borkmann 		struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
17914f7d2cdfSDaniel Borkmann 
1792c02db8c6SChris Wright 		err = -EOPNOTSUPP;
1793c02db8c6SChris Wright 		if (ops->ndo_set_vf_vlan)
17944f7d2cdfSDaniel Borkmann 			err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
179579aab093SMoshe Shemesh 						   ivv->qos,
179679aab093SMoshe Shemesh 						   htons(ETH_P_8021Q));
179779aab093SMoshe Shemesh 		if (err < 0)
179879aab093SMoshe Shemesh 			return err;
179979aab093SMoshe Shemesh 	}
180079aab093SMoshe Shemesh 
180179aab093SMoshe Shemesh 	if (tb[IFLA_VF_VLAN_LIST]) {
180279aab093SMoshe Shemesh 		struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
180379aab093SMoshe Shemesh 		struct nlattr *attr;
180479aab093SMoshe Shemesh 		int rem, len = 0;
180579aab093SMoshe Shemesh 
180679aab093SMoshe Shemesh 		err = -EOPNOTSUPP;
180779aab093SMoshe Shemesh 		if (!ops->ndo_set_vf_vlan)
180879aab093SMoshe Shemesh 			return err;
180979aab093SMoshe Shemesh 
181079aab093SMoshe Shemesh 		nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
181179aab093SMoshe Shemesh 			if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
181279aab093SMoshe Shemesh 			    nla_len(attr) < NLA_HDRLEN) {
181379aab093SMoshe Shemesh 				return -EINVAL;
181479aab093SMoshe Shemesh 			}
181579aab093SMoshe Shemesh 			if (len >= MAX_VLAN_LIST_LEN)
181679aab093SMoshe Shemesh 				return -EOPNOTSUPP;
181779aab093SMoshe Shemesh 			ivvl[len] = nla_data(attr);
181879aab093SMoshe Shemesh 
181979aab093SMoshe Shemesh 			len++;
182079aab093SMoshe Shemesh 		}
1821fa34cd94SArnd Bergmann 		if (len == 0)
1822fa34cd94SArnd Bergmann 			return -EINVAL;
1823fa34cd94SArnd Bergmann 
182479aab093SMoshe Shemesh 		err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
182579aab093SMoshe Shemesh 					   ivvl[0]->qos, ivvl[0]->vlan_proto);
18264f7d2cdfSDaniel Borkmann 		if (err < 0)
18274f7d2cdfSDaniel Borkmann 			return err;
1828c02db8c6SChris Wright 	}
18294f7d2cdfSDaniel Borkmann 
18304f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_TX_RATE]) {
18314f7d2cdfSDaniel Borkmann 		struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
1832ed616689SSucheta Chakraborty 		struct ifla_vf_info ivf;
18334f7d2cdfSDaniel Borkmann 
1834c02db8c6SChris Wright 		err = -EOPNOTSUPP;
1835ed616689SSucheta Chakraborty 		if (ops->ndo_get_vf_config)
18364f7d2cdfSDaniel Borkmann 			err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
18374f7d2cdfSDaniel Borkmann 		if (err < 0)
18384f7d2cdfSDaniel Borkmann 			return err;
18394f7d2cdfSDaniel Borkmann 
1840ed616689SSucheta Chakraborty 		err = -EOPNOTSUPP;
1841ed616689SSucheta Chakraborty 		if (ops->ndo_set_vf_rate)
1842ed616689SSucheta Chakraborty 			err = ops->ndo_set_vf_rate(dev, ivt->vf,
1843ed616689SSucheta Chakraborty 						   ivf.min_tx_rate,
1844c02db8c6SChris Wright 						   ivt->rate);
18454f7d2cdfSDaniel Borkmann 		if (err < 0)
18464f7d2cdfSDaniel Borkmann 			return err;
1847c02db8c6SChris Wright 	}
18484f7d2cdfSDaniel Borkmann 
18494f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_RATE]) {
18504f7d2cdfSDaniel Borkmann 		struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
18514f7d2cdfSDaniel Borkmann 
1852ed616689SSucheta Chakraborty 		err = -EOPNOTSUPP;
1853ed616689SSucheta Chakraborty 		if (ops->ndo_set_vf_rate)
1854ed616689SSucheta Chakraborty 			err = ops->ndo_set_vf_rate(dev, ivt->vf,
1855ed616689SSucheta Chakraborty 						   ivt->min_tx_rate,
1856ed616689SSucheta Chakraborty 						   ivt->max_tx_rate);
18574f7d2cdfSDaniel Borkmann 		if (err < 0)
18584f7d2cdfSDaniel Borkmann 			return err;
1859ed616689SSucheta Chakraborty 	}
18604f7d2cdfSDaniel Borkmann 
18614f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_SPOOFCHK]) {
18624f7d2cdfSDaniel Borkmann 		struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
18634f7d2cdfSDaniel Borkmann 
18645f8444a3SGreg Rose 		err = -EOPNOTSUPP;
18655f8444a3SGreg Rose 		if (ops->ndo_set_vf_spoofchk)
18665f8444a3SGreg Rose 			err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
18675f8444a3SGreg Rose 						       ivs->setting);
18684f7d2cdfSDaniel Borkmann 		if (err < 0)
18694f7d2cdfSDaniel Borkmann 			return err;
18705f8444a3SGreg Rose 	}
18714f7d2cdfSDaniel Borkmann 
18724f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_LINK_STATE]) {
18734f7d2cdfSDaniel Borkmann 		struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
18744f7d2cdfSDaniel Borkmann 
18751d8faf48SRony Efraim 		err = -EOPNOTSUPP;
18761d8faf48SRony Efraim 		if (ops->ndo_set_vf_link_state)
18771d8faf48SRony Efraim 			err = ops->ndo_set_vf_link_state(dev, ivl->vf,
18781d8faf48SRony Efraim 							 ivl->link_state);
18794f7d2cdfSDaniel Borkmann 		if (err < 0)
18804f7d2cdfSDaniel Borkmann 			return err;
18811d8faf48SRony Efraim 	}
18824f7d2cdfSDaniel Borkmann 
18834f7d2cdfSDaniel Borkmann 	if (tb[IFLA_VF_RSS_QUERY_EN]) {
188401a3d796SVlad Zolotarov 		struct ifla_vf_rss_query_en *ivrssq_en;
188501a3d796SVlad Zolotarov 
188601a3d796SVlad Zolotarov 		err = -EOPNOTSUPP;
18874f7d2cdfSDaniel Borkmann 		ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
188801a3d796SVlad Zolotarov 		if (ops->ndo_set_vf_rss_query_en)
18894f7d2cdfSDaniel Borkmann 			err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
189001a3d796SVlad Zolotarov 							   ivrssq_en->setting);
18914f7d2cdfSDaniel Borkmann 		if (err < 0)
18924f7d2cdfSDaniel Borkmann 			return err;
189301a3d796SVlad Zolotarov 	}
18944f7d2cdfSDaniel Borkmann 
1895dd461d6aSHiroshi Shimamoto 	if (tb[IFLA_VF_TRUST]) {
1896dd461d6aSHiroshi Shimamoto 		struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
1897dd461d6aSHiroshi Shimamoto 
1898dd461d6aSHiroshi Shimamoto 		err = -EOPNOTSUPP;
1899dd461d6aSHiroshi Shimamoto 		if (ops->ndo_set_vf_trust)
1900dd461d6aSHiroshi Shimamoto 			err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
1901dd461d6aSHiroshi Shimamoto 		if (err < 0)
1902dd461d6aSHiroshi Shimamoto 			return err;
1903dd461d6aSHiroshi Shimamoto 	}
1904dd461d6aSHiroshi Shimamoto 
1905cc8e27ccSEli Cohen 	if (tb[IFLA_VF_IB_NODE_GUID]) {
1906cc8e27ccSEli Cohen 		struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
1907cc8e27ccSEli Cohen 
1908cc8e27ccSEli Cohen 		if (!ops->ndo_set_vf_guid)
1909cc8e27ccSEli Cohen 			return -EOPNOTSUPP;
1910cc8e27ccSEli Cohen 
1911cc8e27ccSEli Cohen 		return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
1912cc8e27ccSEli Cohen 	}
1913cc8e27ccSEli Cohen 
1914cc8e27ccSEli Cohen 	if (tb[IFLA_VF_IB_PORT_GUID]) {
1915cc8e27ccSEli Cohen 		struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
1916cc8e27ccSEli Cohen 
1917cc8e27ccSEli Cohen 		if (!ops->ndo_set_vf_guid)
1918cc8e27ccSEli Cohen 			return -EOPNOTSUPP;
1919cc8e27ccSEli Cohen 
1920cc8e27ccSEli Cohen 		return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
1921cc8e27ccSEli Cohen 	}
1922cc8e27ccSEli Cohen 
1923c02db8c6SChris Wright 	return err;
1924c02db8c6SChris Wright }
1925c02db8c6SChris Wright 
1926fbaec0eaSJiri Pirko static int do_set_master(struct net_device *dev, int ifindex)
1927fbaec0eaSJiri Pirko {
1928898e5061SJiri Pirko 	struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
1929fbaec0eaSJiri Pirko 	const struct net_device_ops *ops;
1930fbaec0eaSJiri Pirko 	int err;
1931fbaec0eaSJiri Pirko 
1932898e5061SJiri Pirko 	if (upper_dev) {
1933898e5061SJiri Pirko 		if (upper_dev->ifindex == ifindex)
1934fbaec0eaSJiri Pirko 			return 0;
1935898e5061SJiri Pirko 		ops = upper_dev->netdev_ops;
1936fbaec0eaSJiri Pirko 		if (ops->ndo_del_slave) {
1937898e5061SJiri Pirko 			err = ops->ndo_del_slave(upper_dev, dev);
1938fbaec0eaSJiri Pirko 			if (err)
1939fbaec0eaSJiri Pirko 				return err;
1940fbaec0eaSJiri Pirko 		} else {
1941fbaec0eaSJiri Pirko 			return -EOPNOTSUPP;
1942fbaec0eaSJiri Pirko 		}
1943fbaec0eaSJiri Pirko 	}
1944fbaec0eaSJiri Pirko 
1945fbaec0eaSJiri Pirko 	if (ifindex) {
1946898e5061SJiri Pirko 		upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
1947898e5061SJiri Pirko 		if (!upper_dev)
1948fbaec0eaSJiri Pirko 			return -EINVAL;
1949898e5061SJiri Pirko 		ops = upper_dev->netdev_ops;
1950fbaec0eaSJiri Pirko 		if (ops->ndo_add_slave) {
1951898e5061SJiri Pirko 			err = ops->ndo_add_slave(upper_dev, dev);
1952fbaec0eaSJiri Pirko 			if (err)
1953fbaec0eaSJiri Pirko 				return err;
1954fbaec0eaSJiri Pirko 		} else {
1955fbaec0eaSJiri Pirko 			return -EOPNOTSUPP;
1956fbaec0eaSJiri Pirko 		}
1957fbaec0eaSJiri Pirko 	}
1958fbaec0eaSJiri Pirko 	return 0;
1959fbaec0eaSJiri Pirko }
1960fbaec0eaSJiri Pirko 
196190c325e3SNicolas Dichtel #define DO_SETLINK_MODIFIED	0x01
1962ba998906SNicolas Dichtel /* notify flag means notify + modified. */
1963ba998906SNicolas Dichtel #define DO_SETLINK_NOTIFY	0x03
196490f62cf3SEric W. Biederman static int do_setlink(const struct sk_buff *skb,
196590f62cf3SEric W. Biederman 		      struct net_device *dev, struct ifinfomsg *ifm,
1966ddf9f970SJakub Kicinski 		      struct netlink_ext_ack *extack,
196790c325e3SNicolas Dichtel 		      struct nlattr **tb, char *ifname, int status)
19680157f60cSPatrick McHardy {
1969d314774cSStephen Hemminger 	const struct net_device_ops *ops = dev->netdev_ops;
19700157f60cSPatrick McHardy 	int err;
19710157f60cSPatrick McHardy 
1972f0630529SEric W. Biederman 	if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
197381adee47SEric W. Biederman 		struct net *net = rtnl_link_get_net(dev_net(dev), tb);
1974d8a5ec67SEric W. Biederman 		if (IS_ERR(net)) {
1975d8a5ec67SEric W. Biederman 			err = PTR_ERR(net);
1976d8a5ec67SEric W. Biederman 			goto errout;
1977d8a5ec67SEric W. Biederman 		}
197890f62cf3SEric W. Biederman 		if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
1979e0ebde0eSNicolas Dichtel 			put_net(net);
1980b51642f6SEric W. Biederman 			err = -EPERM;
1981b51642f6SEric W. Biederman 			goto errout;
1982b51642f6SEric W. Biederman 		}
1983d8a5ec67SEric W. Biederman 		err = dev_change_net_namespace(dev, net, ifname);
1984d8a5ec67SEric W. Biederman 		put_net(net);
1985d8a5ec67SEric W. Biederman 		if (err)
1986d8a5ec67SEric W. Biederman 			goto errout;
198790c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
1988d8a5ec67SEric W. Biederman 	}
1989d8a5ec67SEric W. Biederman 
19900157f60cSPatrick McHardy 	if (tb[IFLA_MAP]) {
19910157f60cSPatrick McHardy 		struct rtnl_link_ifmap *u_map;
19920157f60cSPatrick McHardy 		struct ifmap k_map;
19930157f60cSPatrick McHardy 
1994d314774cSStephen Hemminger 		if (!ops->ndo_set_config) {
19950157f60cSPatrick McHardy 			err = -EOPNOTSUPP;
19960157f60cSPatrick McHardy 			goto errout;
19970157f60cSPatrick McHardy 		}
19980157f60cSPatrick McHardy 
19990157f60cSPatrick McHardy 		if (!netif_device_present(dev)) {
20000157f60cSPatrick McHardy 			err = -ENODEV;
20010157f60cSPatrick McHardy 			goto errout;
20020157f60cSPatrick McHardy 		}
20030157f60cSPatrick McHardy 
20040157f60cSPatrick McHardy 		u_map = nla_data(tb[IFLA_MAP]);
20050157f60cSPatrick McHardy 		k_map.mem_start = (unsigned long) u_map->mem_start;
20060157f60cSPatrick McHardy 		k_map.mem_end = (unsigned long) u_map->mem_end;
20070157f60cSPatrick McHardy 		k_map.base_addr = (unsigned short) u_map->base_addr;
20080157f60cSPatrick McHardy 		k_map.irq = (unsigned char) u_map->irq;
20090157f60cSPatrick McHardy 		k_map.dma = (unsigned char) u_map->dma;
20100157f60cSPatrick McHardy 		k_map.port = (unsigned char) u_map->port;
20110157f60cSPatrick McHardy 
2012d314774cSStephen Hemminger 		err = ops->ndo_set_config(dev, &k_map);
20130157f60cSPatrick McHardy 		if (err < 0)
20140157f60cSPatrick McHardy 			goto errout;
20150157f60cSPatrick McHardy 
2016ba998906SNicolas Dichtel 		status |= DO_SETLINK_NOTIFY;
20170157f60cSPatrick McHardy 	}
20180157f60cSPatrick McHardy 
20190157f60cSPatrick McHardy 	if (tb[IFLA_ADDRESS]) {
20200157f60cSPatrick McHardy 		struct sockaddr *sa;
20210157f60cSPatrick McHardy 		int len;
20220157f60cSPatrick McHardy 
2023153711f9SWANG Cong 		len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
2024153711f9SWANG Cong 						  sizeof(*sa));
20250157f60cSPatrick McHardy 		sa = kmalloc(len, GFP_KERNEL);
20260157f60cSPatrick McHardy 		if (!sa) {
20270157f60cSPatrick McHardy 			err = -ENOMEM;
20280157f60cSPatrick McHardy 			goto errout;
20290157f60cSPatrick McHardy 		}
20300157f60cSPatrick McHardy 		sa->sa_family = dev->type;
20310157f60cSPatrick McHardy 		memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
20320157f60cSPatrick McHardy 		       dev->addr_len);
2033e7c3273eSJiri Pirko 		err = dev_set_mac_address(dev, sa);
20340157f60cSPatrick McHardy 		kfree(sa);
20350157f60cSPatrick McHardy 		if (err)
20360157f60cSPatrick McHardy 			goto errout;
203790c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
20380157f60cSPatrick McHardy 	}
20390157f60cSPatrick McHardy 
20400157f60cSPatrick McHardy 	if (tb[IFLA_MTU]) {
20410157f60cSPatrick McHardy 		err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
20420157f60cSPatrick McHardy 		if (err < 0)
20430157f60cSPatrick McHardy 			goto errout;
204490c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
20450157f60cSPatrick McHardy 	}
20460157f60cSPatrick McHardy 
2047cbda10faSVlad Dogaru 	if (tb[IFLA_GROUP]) {
2048cbda10faSVlad Dogaru 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
2049ba998906SNicolas Dichtel 		status |= DO_SETLINK_NOTIFY;
2050cbda10faSVlad Dogaru 	}
2051cbda10faSVlad Dogaru 
20520157f60cSPatrick McHardy 	/*
20530157f60cSPatrick McHardy 	 * Interface selected by interface index but interface
20540157f60cSPatrick McHardy 	 * name provided implies that a name change has been
20550157f60cSPatrick McHardy 	 * requested.
20560157f60cSPatrick McHardy 	 */
20570157f60cSPatrick McHardy 	if (ifm->ifi_index > 0 && ifname[0]) {
20580157f60cSPatrick McHardy 		err = dev_change_name(dev, ifname);
20590157f60cSPatrick McHardy 		if (err < 0)
20600157f60cSPatrick McHardy 			goto errout;
206190c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
20620157f60cSPatrick McHardy 	}
20630157f60cSPatrick McHardy 
20640b815a1aSStephen Hemminger 	if (tb[IFLA_IFALIAS]) {
20650b815a1aSStephen Hemminger 		err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
20660b815a1aSStephen Hemminger 				    nla_len(tb[IFLA_IFALIAS]));
20670b815a1aSStephen Hemminger 		if (err < 0)
20680b815a1aSStephen Hemminger 			goto errout;
2069ba998906SNicolas Dichtel 		status |= DO_SETLINK_NOTIFY;
20700b815a1aSStephen Hemminger 	}
20710b815a1aSStephen Hemminger 
20720157f60cSPatrick McHardy 	if (tb[IFLA_BROADCAST]) {
20730157f60cSPatrick McHardy 		nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
2074e7c3273eSJiri Pirko 		call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
20750157f60cSPatrick McHardy 	}
20760157f60cSPatrick McHardy 
20770157f60cSPatrick McHardy 	if (ifm->ifi_flags || ifm->ifi_change) {
20783729d502SPatrick McHardy 		err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
20795f9021cfSJohannes Berg 		if (err < 0)
20805f9021cfSJohannes Berg 			goto errout;
20810157f60cSPatrick McHardy 	}
20820157f60cSPatrick McHardy 
2083fbaec0eaSJiri Pirko 	if (tb[IFLA_MASTER]) {
2084fbaec0eaSJiri Pirko 		err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
2085fbaec0eaSJiri Pirko 		if (err)
2086fbaec0eaSJiri Pirko 			goto errout;
208790c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
2088fbaec0eaSJiri Pirko 	}
2089fbaec0eaSJiri Pirko 
20909a57247fSJiri Pirko 	if (tb[IFLA_CARRIER]) {
20919a57247fSJiri Pirko 		err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
20929a57247fSJiri Pirko 		if (err)
20939a57247fSJiri Pirko 			goto errout;
209490c325e3SNicolas Dichtel 		status |= DO_SETLINK_MODIFIED;
20959a57247fSJiri Pirko 	}
20969a57247fSJiri Pirko 
20975d1180fcSNicolas Dichtel 	if (tb[IFLA_TXQLEN]) {
20980cd29503SAlexey Dobriyan 		unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
20990cd29503SAlexey Dobriyan 		unsigned int orig_len = dev->tx_queue_len;
21005d1180fcSNicolas Dichtel 
210108294a26SJason Wang 		if (dev->tx_queue_len ^ value) {
21025d1180fcSNicolas Dichtel 			dev->tx_queue_len = value;
210308294a26SJason Wang 			err = call_netdevice_notifiers(
210408294a26SJason Wang 			      NETDEV_CHANGE_TX_QUEUE_LEN, dev);
210508294a26SJason Wang 			err = notifier_to_errno(err);
210608294a26SJason Wang 			if (err) {
210708294a26SJason Wang 				dev->tx_queue_len = orig_len;
210808294a26SJason Wang 				goto errout;
210908294a26SJason Wang 			}
211008294a26SJason Wang 			status |= DO_SETLINK_NOTIFY;
211108294a26SJason Wang 		}
21125d1180fcSNicolas Dichtel 	}
21130157f60cSPatrick McHardy 
21140157f60cSPatrick McHardy 	if (tb[IFLA_OPERSTATE])
211593b2d4a2SDavid S. Miller 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
21160157f60cSPatrick McHardy 
21170157f60cSPatrick McHardy 	if (tb[IFLA_LINKMODE]) {
21181889b0e7SNicolas Dichtel 		unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
21191889b0e7SNicolas Dichtel 
21200157f60cSPatrick McHardy 		write_lock_bh(&dev_base_lock);
21211889b0e7SNicolas Dichtel 		if (dev->link_mode ^ value)
2122ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
21231889b0e7SNicolas Dichtel 		dev->link_mode = value;
212493b2d4a2SDavid S. Miller 		write_unlock_bh(&dev_base_lock);
21250157f60cSPatrick McHardy 	}
21260157f60cSPatrick McHardy 
2127c02db8c6SChris Wright 	if (tb[IFLA_VFINFO_LIST]) {
21284f7d2cdfSDaniel Borkmann 		struct nlattr *vfinfo[IFLA_VF_MAX + 1];
2129c02db8c6SChris Wright 		struct nlattr *attr;
2130c02db8c6SChris Wright 		int rem;
21314f7d2cdfSDaniel Borkmann 
2132c02db8c6SChris Wright 		nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
21334f7d2cdfSDaniel Borkmann 			if (nla_type(attr) != IFLA_VF_INFO ||
21344f7d2cdfSDaniel Borkmann 			    nla_len(attr) < NLA_HDRLEN) {
2135253683bbSDavid Howells 				err = -EINVAL;
2136c02db8c6SChris Wright 				goto errout;
2137253683bbSDavid Howells 			}
21384f7d2cdfSDaniel Borkmann 			err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr,
2139fceb6435SJohannes Berg 					       ifla_vf_policy, NULL);
21404f7d2cdfSDaniel Borkmann 			if (err < 0)
21414f7d2cdfSDaniel Borkmann 				goto errout;
21424f7d2cdfSDaniel Borkmann 			err = do_setvfinfo(dev, vfinfo);
2143ebc08a6fSWilliams, Mitch A 			if (err < 0)
2144ebc08a6fSWilliams, Mitch A 				goto errout;
2145ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
2146ebc08a6fSWilliams, Mitch A 		}
2147ebc08a6fSWilliams, Mitch A 	}
21480157f60cSPatrick McHardy 	err = 0;
21490157f60cSPatrick McHardy 
215057b61080SScott Feldman 	if (tb[IFLA_VF_PORTS]) {
215157b61080SScott Feldman 		struct nlattr *port[IFLA_PORT_MAX+1];
215257b61080SScott Feldman 		struct nlattr *attr;
215357b61080SScott Feldman 		int vf;
215457b61080SScott Feldman 		int rem;
215557b61080SScott Feldman 
215657b61080SScott Feldman 		err = -EOPNOTSUPP;
215757b61080SScott Feldman 		if (!ops->ndo_set_vf_port)
215857b61080SScott Feldman 			goto errout;
215957b61080SScott Feldman 
216057b61080SScott Feldman 		nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
2161035d210fSDaniel Borkmann 			if (nla_type(attr) != IFLA_VF_PORT ||
2162035d210fSDaniel Borkmann 			    nla_len(attr) < NLA_HDRLEN) {
2163035d210fSDaniel Borkmann 				err = -EINVAL;
2164035d210fSDaniel Borkmann 				goto errout;
2165035d210fSDaniel Borkmann 			}
2166035d210fSDaniel Borkmann 			err = nla_parse_nested(port, IFLA_PORT_MAX, attr,
2167fceb6435SJohannes Berg 					       ifla_port_policy, NULL);
216857b61080SScott Feldman 			if (err < 0)
216957b61080SScott Feldman 				goto errout;
217057b61080SScott Feldman 			if (!port[IFLA_PORT_VF]) {
217157b61080SScott Feldman 				err = -EOPNOTSUPP;
217257b61080SScott Feldman 				goto errout;
217357b61080SScott Feldman 			}
217457b61080SScott Feldman 			vf = nla_get_u32(port[IFLA_PORT_VF]);
217557b61080SScott Feldman 			err = ops->ndo_set_vf_port(dev, vf, port);
217657b61080SScott Feldman 			if (err < 0)
217757b61080SScott Feldman 				goto errout;
2178ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
217957b61080SScott Feldman 		}
218057b61080SScott Feldman 	}
218157b61080SScott Feldman 	err = 0;
218257b61080SScott Feldman 
218357b61080SScott Feldman 	if (tb[IFLA_PORT_SELF]) {
218457b61080SScott Feldman 		struct nlattr *port[IFLA_PORT_MAX+1];
218557b61080SScott Feldman 
218657b61080SScott Feldman 		err = nla_parse_nested(port, IFLA_PORT_MAX,
2187fceb6435SJohannes Berg 				       tb[IFLA_PORT_SELF], ifla_port_policy,
2188fceb6435SJohannes Berg 				       NULL);
218957b61080SScott Feldman 		if (err < 0)
219057b61080SScott Feldman 			goto errout;
219157b61080SScott Feldman 
219257b61080SScott Feldman 		err = -EOPNOTSUPP;
219357b61080SScott Feldman 		if (ops->ndo_set_vf_port)
219457b61080SScott Feldman 			err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
219557b61080SScott Feldman 		if (err < 0)
219657b61080SScott Feldman 			goto errout;
2197ba998906SNicolas Dichtel 		status |= DO_SETLINK_NOTIFY;
219857b61080SScott Feldman 	}
2199f8ff182cSThomas Graf 
2200f8ff182cSThomas Graf 	if (tb[IFLA_AF_SPEC]) {
2201f8ff182cSThomas Graf 		struct nlattr *af;
2202f8ff182cSThomas Graf 		int rem;
2203f8ff182cSThomas Graf 
2204f8ff182cSThomas Graf 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2205f8ff182cSThomas Graf 			const struct rtnl_af_ops *af_ops;
2206f8ff182cSThomas Graf 
2207f8ff182cSThomas Graf 			if (!(af_ops = rtnl_af_lookup(nla_type(af))))
2208cf7afbfeSThomas Graf 				BUG();
2209f8ff182cSThomas Graf 
2210cf7afbfeSThomas Graf 			err = af_ops->set_link_af(dev, af);
2211f8ff182cSThomas Graf 			if (err < 0)
2212f8ff182cSThomas Graf 				goto errout;
2213f8ff182cSThomas Graf 
2214ba998906SNicolas Dichtel 			status |= DO_SETLINK_NOTIFY;
2215f8ff182cSThomas Graf 		}
2216f8ff182cSThomas Graf 	}
221757b61080SScott Feldman 	err = 0;
221857b61080SScott Feldman 
221988d6378bSAnuradha Karuppiah 	if (tb[IFLA_PROTO_DOWN]) {
222088d6378bSAnuradha Karuppiah 		err = dev_change_proto_down(dev,
222188d6378bSAnuradha Karuppiah 					    nla_get_u8(tb[IFLA_PROTO_DOWN]));
222288d6378bSAnuradha Karuppiah 		if (err)
222388d6378bSAnuradha Karuppiah 			goto errout;
222488d6378bSAnuradha Karuppiah 		status |= DO_SETLINK_NOTIFY;
222588d6378bSAnuradha Karuppiah 	}
222688d6378bSAnuradha Karuppiah 
2227d1fdd913SBrenden Blanco 	if (tb[IFLA_XDP]) {
2228d1fdd913SBrenden Blanco 		struct nlattr *xdp[IFLA_XDP_MAX + 1];
222985de8576SDaniel Borkmann 		u32 xdp_flags = 0;
2230d1fdd913SBrenden Blanco 
2231d1fdd913SBrenden Blanco 		err = nla_parse_nested(xdp, IFLA_XDP_MAX, tb[IFLA_XDP],
2232fceb6435SJohannes Berg 				       ifla_xdp_policy, NULL);
2233d1fdd913SBrenden Blanco 		if (err < 0)
2234d1fdd913SBrenden Blanco 			goto errout;
2235d1fdd913SBrenden Blanco 
223658038695SMartin KaFai Lau 		if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
2237262d8625SBrenden Blanco 			err = -EINVAL;
2238262d8625SBrenden Blanco 			goto errout;
2239262d8625SBrenden Blanco 		}
224085de8576SDaniel Borkmann 
224185de8576SDaniel Borkmann 		if (xdp[IFLA_XDP_FLAGS]) {
224285de8576SDaniel Borkmann 			xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
224385de8576SDaniel Borkmann 			if (xdp_flags & ~XDP_FLAGS_MASK) {
224485de8576SDaniel Borkmann 				err = -EINVAL;
224585de8576SDaniel Borkmann 				goto errout;
224685de8576SDaniel Borkmann 			}
2247ee5d032fSJakub Kicinski 			if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) {
22480489df9aSDaniel Borkmann 				err = -EINVAL;
22490489df9aSDaniel Borkmann 				goto errout;
22500489df9aSDaniel Borkmann 			}
225185de8576SDaniel Borkmann 		}
225285de8576SDaniel Borkmann 
2253d1fdd913SBrenden Blanco 		if (xdp[IFLA_XDP_FD]) {
2254ddf9f970SJakub Kicinski 			err = dev_change_xdp_fd(dev, extack,
225585de8576SDaniel Borkmann 						nla_get_s32(xdp[IFLA_XDP_FD]),
225685de8576SDaniel Borkmann 						xdp_flags);
2257d1fdd913SBrenden Blanco 			if (err)
2258d1fdd913SBrenden Blanco 				goto errout;
2259d1fdd913SBrenden Blanco 			status |= DO_SETLINK_NOTIFY;
2260d1fdd913SBrenden Blanco 		}
2261d1fdd913SBrenden Blanco 	}
2262d1fdd913SBrenden Blanco 
22630157f60cSPatrick McHardy errout:
2264ba998906SNicolas Dichtel 	if (status & DO_SETLINK_MODIFIED) {
2265ba998906SNicolas Dichtel 		if (status & DO_SETLINK_NOTIFY)
2266ba998906SNicolas Dichtel 			netdev_state_change(dev);
2267ba998906SNicolas Dichtel 
2268ba998906SNicolas Dichtel 		if (err < 0)
2269e87cc472SJoe 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",
2270e87cc472SJoe Perches 					     dev->name);
2271ba998906SNicolas Dichtel 	}
22720157f60cSPatrick McHardy 
22730157f60cSPatrick McHardy 	return err;
22740157f60cSPatrick McHardy }
22750157f60cSPatrick McHardy 
2276c21ef3e3SDavid Ahern static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2277c21ef3e3SDavid Ahern 			struct netlink_ext_ack *extack)
2278da5e0494SThomas Graf {
22793b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
2280da5e0494SThomas Graf 	struct ifinfomsg *ifm;
2281da5e0494SThomas Graf 	struct net_device *dev;
22820157f60cSPatrick McHardy 	int err;
2283da5e0494SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
22841da177e4SLinus Torvalds 	char ifname[IFNAMSIZ];
22851da177e4SLinus Torvalds 
2286c21ef3e3SDavid Ahern 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy,
2287c21ef3e3SDavid Ahern 			  extack);
2288da5e0494SThomas Graf 	if (err < 0)
2289da5e0494SThomas Graf 		goto errout;
22901da177e4SLinus Torvalds 
22915176f91eSThomas Graf 	if (tb[IFLA_IFNAME])
22925176f91eSThomas Graf 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
229378e5b891SPatrick McHardy 	else
229478e5b891SPatrick McHardy 		ifname[0] = '\0';
22951da177e4SLinus Torvalds 
22961da177e4SLinus Torvalds 	err = -EINVAL;
2297da5e0494SThomas Graf 	ifm = nlmsg_data(nlh);
229851055be8SPatrick McHardy 	if (ifm->ifi_index > 0)
2299a3d12891SEric Dumazet 		dev = __dev_get_by_index(net, ifm->ifi_index);
2300da5e0494SThomas Graf 	else if (tb[IFLA_IFNAME])
2301a3d12891SEric Dumazet 		dev = __dev_get_by_name(net, ifname);
2302da5e0494SThomas Graf 	else
2303da5e0494SThomas Graf 		goto errout;
23041da177e4SLinus Torvalds 
2305da5e0494SThomas Graf 	if (dev == NULL) {
2306da5e0494SThomas Graf 		err = -ENODEV;
2307da5e0494SThomas Graf 		goto errout;
2308da5e0494SThomas Graf 	}
23091da177e4SLinus Torvalds 
2310e0d087afSEric Dumazet 	err = validate_linkmsg(dev, tb);
2311e0d087afSEric Dumazet 	if (err < 0)
2312a3d12891SEric Dumazet 		goto errout;
2313da5e0494SThomas Graf 
2314ddf9f970SJakub Kicinski 	err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0);
2315da5e0494SThomas Graf errout:
23161da177e4SLinus Torvalds 	return err;
23171da177e4SLinus Torvalds }
23181da177e4SLinus Torvalds 
231966400d54SWANG Cong static int rtnl_group_dellink(const struct net *net, int group)
232066400d54SWANG Cong {
232166400d54SWANG Cong 	struct net_device *dev, *aux;
232266400d54SWANG Cong 	LIST_HEAD(list_kill);
232366400d54SWANG Cong 	bool found = false;
232466400d54SWANG Cong 
232566400d54SWANG Cong 	if (!group)
232666400d54SWANG Cong 		return -EPERM;
232766400d54SWANG Cong 
232866400d54SWANG Cong 	for_each_netdev(net, dev) {
232966400d54SWANG Cong 		if (dev->group == group) {
233066400d54SWANG Cong 			const struct rtnl_link_ops *ops;
233166400d54SWANG Cong 
233266400d54SWANG Cong 			found = true;
233366400d54SWANG Cong 			ops = dev->rtnl_link_ops;
233466400d54SWANG Cong 			if (!ops || !ops->dellink)
233566400d54SWANG Cong 				return -EOPNOTSUPP;
233666400d54SWANG Cong 		}
233766400d54SWANG Cong 	}
233866400d54SWANG Cong 
233966400d54SWANG Cong 	if (!found)
234066400d54SWANG Cong 		return -ENODEV;
234166400d54SWANG Cong 
234266400d54SWANG Cong 	for_each_netdev_safe(net, dev, aux) {
234366400d54SWANG Cong 		if (dev->group == group) {
234466400d54SWANG Cong 			const struct rtnl_link_ops *ops;
234566400d54SWANG Cong 
234666400d54SWANG Cong 			ops = dev->rtnl_link_ops;
234766400d54SWANG Cong 			ops->dellink(dev, &list_kill);
234866400d54SWANG Cong 		}
234966400d54SWANG Cong 	}
235066400d54SWANG Cong 	unregister_netdevice_many(&list_kill);
235166400d54SWANG Cong 
235266400d54SWANG Cong 	return 0;
235366400d54SWANG Cong }
235466400d54SWANG Cong 
2355614732eaSThomas Graf int rtnl_delete_link(struct net_device *dev)
2356614732eaSThomas Graf {
2357614732eaSThomas Graf 	const struct rtnl_link_ops *ops;
2358614732eaSThomas Graf 	LIST_HEAD(list_kill);
2359614732eaSThomas Graf 
2360614732eaSThomas Graf 	ops = dev->rtnl_link_ops;
2361614732eaSThomas Graf 	if (!ops || !ops->dellink)
2362614732eaSThomas Graf 		return -EOPNOTSUPP;
2363614732eaSThomas Graf 
2364614732eaSThomas Graf 	ops->dellink(dev, &list_kill);
2365614732eaSThomas Graf 	unregister_netdevice_many(&list_kill);
2366614732eaSThomas Graf 
2367614732eaSThomas Graf 	return 0;
2368614732eaSThomas Graf }
2369614732eaSThomas Graf EXPORT_SYMBOL_GPL(rtnl_delete_link);
2370614732eaSThomas Graf 
2371c21ef3e3SDavid Ahern static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
2372c21ef3e3SDavid Ahern 			struct netlink_ext_ack *extack)
237338f7b870SPatrick McHardy {
23743b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
237538f7b870SPatrick McHardy 	struct net_device *dev;
237638f7b870SPatrick McHardy 	struct ifinfomsg *ifm;
237738f7b870SPatrick McHardy 	char ifname[IFNAMSIZ];
237838f7b870SPatrick McHardy 	struct nlattr *tb[IFLA_MAX+1];
237938f7b870SPatrick McHardy 	int err;
238038f7b870SPatrick McHardy 
2381c21ef3e3SDavid Ahern 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
238238f7b870SPatrick McHardy 	if (err < 0)
238338f7b870SPatrick McHardy 		return err;
238438f7b870SPatrick McHardy 
238538f7b870SPatrick McHardy 	if (tb[IFLA_IFNAME])
238638f7b870SPatrick McHardy 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
238738f7b870SPatrick McHardy 
238838f7b870SPatrick McHardy 	ifm = nlmsg_data(nlh);
238938f7b870SPatrick McHardy 	if (ifm->ifi_index > 0)
2390881d966bSEric W. Biederman 		dev = __dev_get_by_index(net, ifm->ifi_index);
239138f7b870SPatrick McHardy 	else if (tb[IFLA_IFNAME])
2392881d966bSEric W. Biederman 		dev = __dev_get_by_name(net, ifname);
239366400d54SWANG Cong 	else if (tb[IFLA_GROUP])
239466400d54SWANG Cong 		return rtnl_group_dellink(net, nla_get_u32(tb[IFLA_GROUP]));
239538f7b870SPatrick McHardy 	else
239638f7b870SPatrick McHardy 		return -EINVAL;
239738f7b870SPatrick McHardy 
239838f7b870SPatrick McHardy 	if (!dev)
239938f7b870SPatrick McHardy 		return -ENODEV;
240038f7b870SPatrick McHardy 
2401614732eaSThomas Graf 	return rtnl_delete_link(dev);
240238f7b870SPatrick McHardy }
240338f7b870SPatrick McHardy 
24043729d502SPatrick McHardy int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
24053729d502SPatrick McHardy {
24063729d502SPatrick McHardy 	unsigned int old_flags;
24073729d502SPatrick McHardy 	int err;
24083729d502SPatrick McHardy 
24093729d502SPatrick McHardy 	old_flags = dev->flags;
24103729d502SPatrick McHardy 	if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
24113729d502SPatrick McHardy 		err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
24123729d502SPatrick McHardy 		if (err < 0)
24133729d502SPatrick McHardy 			return err;
24143729d502SPatrick McHardy 	}
24153729d502SPatrick McHardy 
24163729d502SPatrick McHardy 	dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
24173729d502SPatrick McHardy 
2418a528c219SNicolas Dichtel 	__dev_notify_flags(dev, old_flags, ~0U);
24193729d502SPatrick McHardy 	return 0;
24203729d502SPatrick McHardy }
24213729d502SPatrick McHardy EXPORT_SYMBOL(rtnl_configure_link);
24223729d502SPatrick McHardy 
2423c0713563SRami Rosen struct net_device *rtnl_create_link(struct net *net,
242478ebb0d0SThomas Graf 	const char *ifname, unsigned char name_assign_type,
24255517750fSTom Gundersen 	const struct rtnl_link_ops *ops, struct nlattr *tb[])
2426e7199288SPavel Emelianov {
2427e7199288SPavel Emelianov 	struct net_device *dev;
2428d40156aaSJiri Pirko 	unsigned int num_tx_queues = 1;
2429d40156aaSJiri Pirko 	unsigned int num_rx_queues = 1;
2430e7199288SPavel Emelianov 
243176ff5cc9SJiri Pirko 	if (tb[IFLA_NUM_TX_QUEUES])
243276ff5cc9SJiri Pirko 		num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
243376ff5cc9SJiri Pirko 	else if (ops->get_num_tx_queues)
2434d40156aaSJiri Pirko 		num_tx_queues = ops->get_num_tx_queues();
243576ff5cc9SJiri Pirko 
243676ff5cc9SJiri Pirko 	if (tb[IFLA_NUM_RX_QUEUES])
243776ff5cc9SJiri Pirko 		num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
243876ff5cc9SJiri Pirko 	else if (ops->get_num_rx_queues)
2439d40156aaSJiri Pirko 		num_rx_queues = ops->get_num_rx_queues();
2440efacb309Sstephen hemminger 
24415517750fSTom Gundersen 	dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
2442c835a677STom Gundersen 			       ops->setup, num_tx_queues, num_rx_queues);
2443e7199288SPavel Emelianov 	if (!dev)
2444d1892e4eSTobias Klauser 		return ERR_PTR(-ENOMEM);
2445e7199288SPavel Emelianov 
244681adee47SEric W. Biederman 	dev_net_set(dev, net);
244781adee47SEric W. Biederman 	dev->rtnl_link_ops = ops;
24483729d502SPatrick McHardy 	dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
244981adee47SEric W. Biederman 
2450e7199288SPavel Emelianov 	if (tb[IFLA_MTU])
2451e7199288SPavel Emelianov 		dev->mtu = nla_get_u32(tb[IFLA_MTU]);
24522afb9b53SJiri Pirko 	if (tb[IFLA_ADDRESS]) {
2453e7199288SPavel Emelianov 		memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
2454e7199288SPavel Emelianov 				nla_len(tb[IFLA_ADDRESS]));
24552afb9b53SJiri Pirko 		dev->addr_assign_type = NET_ADDR_SET;
24562afb9b53SJiri Pirko 	}
2457e7199288SPavel Emelianov 	if (tb[IFLA_BROADCAST])
2458e7199288SPavel Emelianov 		memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
2459e7199288SPavel Emelianov 				nla_len(tb[IFLA_BROADCAST]));
2460e7199288SPavel Emelianov 	if (tb[IFLA_TXQLEN])
2461e7199288SPavel Emelianov 		dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
2462e7199288SPavel Emelianov 	if (tb[IFLA_OPERSTATE])
246393b2d4a2SDavid S. Miller 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
2464e7199288SPavel Emelianov 	if (tb[IFLA_LINKMODE])
2465e7199288SPavel Emelianov 		dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
2466ffa934f1SPatrick McHardy 	if (tb[IFLA_GROUP])
2467ffa934f1SPatrick McHardy 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
2468e7199288SPavel Emelianov 
2469e7199288SPavel Emelianov 	return dev;
2470e7199288SPavel Emelianov }
2471e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_create_link);
2472e7199288SPavel Emelianov 
247390f62cf3SEric W. Biederman static int rtnl_group_changelink(const struct sk_buff *skb,
247490f62cf3SEric W. Biederman 		struct net *net, int group,
2475e7ed828fSVlad Dogaru 		struct ifinfomsg *ifm,
2476ddf9f970SJakub Kicinski 		struct netlink_ext_ack *extack,
2477e7ed828fSVlad Dogaru 		struct nlattr **tb)
2478e7ed828fSVlad Dogaru {
2479d079535dSWANG Cong 	struct net_device *dev, *aux;
2480e7ed828fSVlad Dogaru 	int err;
2481e7ed828fSVlad Dogaru 
2482d079535dSWANG Cong 	for_each_netdev_safe(net, dev, aux) {
2483e7ed828fSVlad Dogaru 		if (dev->group == group) {
2484ddf9f970SJakub Kicinski 			err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0);
2485e7ed828fSVlad Dogaru 			if (err < 0)
2486e7ed828fSVlad Dogaru 				return err;
2487e7ed828fSVlad Dogaru 		}
2488e7ed828fSVlad Dogaru 	}
2489e7ed828fSVlad Dogaru 
2490e7ed828fSVlad Dogaru 	return 0;
2491e7ed828fSVlad Dogaru }
2492e7ed828fSVlad Dogaru 
2493c21ef3e3SDavid Ahern static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2494c21ef3e3SDavid Ahern 			struct netlink_ext_ack *extack)
249538f7b870SPatrick McHardy {
24963b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
249738f7b870SPatrick McHardy 	const struct rtnl_link_ops *ops;
2498ba7d49b1SJiri Pirko 	const struct rtnl_link_ops *m_ops = NULL;
249938f7b870SPatrick McHardy 	struct net_device *dev;
2500ba7d49b1SJiri Pirko 	struct net_device *master_dev = NULL;
250138f7b870SPatrick McHardy 	struct ifinfomsg *ifm;
250238f7b870SPatrick McHardy 	char kind[MODULE_NAME_LEN];
250338f7b870SPatrick McHardy 	char ifname[IFNAMSIZ];
250438f7b870SPatrick McHardy 	struct nlattr *tb[IFLA_MAX+1];
250538f7b870SPatrick McHardy 	struct nlattr *linkinfo[IFLA_INFO_MAX+1];
25065517750fSTom Gundersen 	unsigned char name_assign_type = NET_NAME_USER;
250738f7b870SPatrick McHardy 	int err;
250838f7b870SPatrick McHardy 
250995a5afcaSJohannes Berg #ifdef CONFIG_MODULES
251038f7b870SPatrick McHardy replay:
25118072f085SThomas Graf #endif
2512c21ef3e3SDavid Ahern 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
251338f7b870SPatrick McHardy 	if (err < 0)
251438f7b870SPatrick McHardy 		return err;
251538f7b870SPatrick McHardy 
251638f7b870SPatrick McHardy 	if (tb[IFLA_IFNAME])
251738f7b870SPatrick McHardy 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
251838f7b870SPatrick McHardy 	else
251938f7b870SPatrick McHardy 		ifname[0] = '\0';
252038f7b870SPatrick McHardy 
252138f7b870SPatrick McHardy 	ifm = nlmsg_data(nlh);
252238f7b870SPatrick McHardy 	if (ifm->ifi_index > 0)
2523881d966bSEric W. Biederman 		dev = __dev_get_by_index(net, ifm->ifi_index);
2524e7ed828fSVlad Dogaru 	else {
2525e7ed828fSVlad Dogaru 		if (ifname[0])
2526881d966bSEric W. Biederman 			dev = __dev_get_by_name(net, ifname);
252738f7b870SPatrick McHardy 		else
252838f7b870SPatrick McHardy 			dev = NULL;
2529e7ed828fSVlad Dogaru 	}
253038f7b870SPatrick McHardy 
2531ba7d49b1SJiri Pirko 	if (dev) {
2532ba7d49b1SJiri Pirko 		master_dev = netdev_master_upper_dev_get(dev);
2533ba7d49b1SJiri Pirko 		if (master_dev)
2534ba7d49b1SJiri Pirko 			m_ops = master_dev->rtnl_link_ops;
2535ba7d49b1SJiri Pirko 	}
2536ba7d49b1SJiri Pirko 
2537e0d087afSEric Dumazet 	err = validate_linkmsg(dev, tb);
2538e0d087afSEric Dumazet 	if (err < 0)
25391840bb13SThomas Graf 		return err;
25401840bb13SThomas Graf 
254138f7b870SPatrick McHardy 	if (tb[IFLA_LINKINFO]) {
254238f7b870SPatrick McHardy 		err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
2543fceb6435SJohannes Berg 				       tb[IFLA_LINKINFO], ifla_info_policy,
2544fceb6435SJohannes Berg 				       NULL);
254538f7b870SPatrick McHardy 		if (err < 0)
254638f7b870SPatrick McHardy 			return err;
254738f7b870SPatrick McHardy 	} else
254838f7b870SPatrick McHardy 		memset(linkinfo, 0, sizeof(linkinfo));
254938f7b870SPatrick McHardy 
255038f7b870SPatrick McHardy 	if (linkinfo[IFLA_INFO_KIND]) {
255138f7b870SPatrick McHardy 		nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
255238f7b870SPatrick McHardy 		ops = rtnl_link_ops_get(kind);
255338f7b870SPatrick McHardy 	} else {
255438f7b870SPatrick McHardy 		kind[0] = '\0';
255538f7b870SPatrick McHardy 		ops = NULL;
255638f7b870SPatrick McHardy 	}
255738f7b870SPatrick McHardy 
255838f7b870SPatrick McHardy 	if (1) {
25594e10fd5bSSasha Levin 		struct nlattr *attr[ops ? ops->maxtype + 1 : 1];
25604e10fd5bSSasha Levin 		struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 1];
2561ba7d49b1SJiri Pirko 		struct nlattr **data = NULL;
2562ba7d49b1SJiri Pirko 		struct nlattr **slave_data = NULL;
2563317f4810SNicolas Dichtel 		struct net *dest_net, *link_net = NULL;
256438f7b870SPatrick McHardy 
256538f7b870SPatrick McHardy 		if (ops) {
256638f7b870SPatrick McHardy 			if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
256738f7b870SPatrick McHardy 				err = nla_parse_nested(attr, ops->maxtype,
256838f7b870SPatrick McHardy 						       linkinfo[IFLA_INFO_DATA],
2569fceb6435SJohannes Berg 						       ops->policy, NULL);
257038f7b870SPatrick McHardy 				if (err < 0)
257138f7b870SPatrick McHardy 					return err;
257238f7b870SPatrick McHardy 				data = attr;
257338f7b870SPatrick McHardy 			}
257438f7b870SPatrick McHardy 			if (ops->validate) {
2575a8b8a889SMatthias Schiffer 				err = ops->validate(tb, data, extack);
257638f7b870SPatrick McHardy 				if (err < 0)
257738f7b870SPatrick McHardy 					return err;
257838f7b870SPatrick McHardy 			}
257938f7b870SPatrick McHardy 		}
258038f7b870SPatrick McHardy 
2581ba7d49b1SJiri Pirko 		if (m_ops) {
2582ba7d49b1SJiri Pirko 			if (m_ops->slave_maxtype &&
2583ba7d49b1SJiri Pirko 			    linkinfo[IFLA_INFO_SLAVE_DATA]) {
2584ba7d49b1SJiri Pirko 				err = nla_parse_nested(slave_attr,
2585ba7d49b1SJiri Pirko 						       m_ops->slave_maxtype,
2586ba7d49b1SJiri Pirko 						       linkinfo[IFLA_INFO_SLAVE_DATA],
2587fceb6435SJohannes Berg 						       m_ops->slave_policy,
2588fceb6435SJohannes Berg 						       NULL);
2589ba7d49b1SJiri Pirko 				if (err < 0)
2590ba7d49b1SJiri Pirko 					return err;
2591ba7d49b1SJiri Pirko 				slave_data = slave_attr;
2592ba7d49b1SJiri Pirko 			}
2593ba7d49b1SJiri Pirko 			if (m_ops->slave_validate) {
2594d116ffc7SMatthias Schiffer 				err = m_ops->slave_validate(tb, slave_data,
2595d116ffc7SMatthias Schiffer 							    extack);
2596ba7d49b1SJiri Pirko 				if (err < 0)
2597ba7d49b1SJiri Pirko 					return err;
2598ba7d49b1SJiri Pirko 			}
2599ba7d49b1SJiri Pirko 		}
2600ba7d49b1SJiri Pirko 
260138f7b870SPatrick McHardy 		if (dev) {
260290c325e3SNicolas Dichtel 			int status = 0;
260338f7b870SPatrick McHardy 
260438f7b870SPatrick McHardy 			if (nlh->nlmsg_flags & NLM_F_EXCL)
260538f7b870SPatrick McHardy 				return -EEXIST;
260638f7b870SPatrick McHardy 			if (nlh->nlmsg_flags & NLM_F_REPLACE)
260738f7b870SPatrick McHardy 				return -EOPNOTSUPP;
260838f7b870SPatrick McHardy 
260938f7b870SPatrick McHardy 			if (linkinfo[IFLA_INFO_DATA]) {
261038f7b870SPatrick McHardy 				if (!ops || ops != dev->rtnl_link_ops ||
261138f7b870SPatrick McHardy 				    !ops->changelink)
261238f7b870SPatrick McHardy 					return -EOPNOTSUPP;
261338f7b870SPatrick McHardy 
2614ad744b22SMatthias Schiffer 				err = ops->changelink(dev, tb, data, extack);
261538f7b870SPatrick McHardy 				if (err < 0)
261638f7b870SPatrick McHardy 					return err;
2617ba998906SNicolas Dichtel 				status |= DO_SETLINK_NOTIFY;
261838f7b870SPatrick McHardy 			}
261938f7b870SPatrick McHardy 
2620ba7d49b1SJiri Pirko 			if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
2621ba7d49b1SJiri Pirko 				if (!m_ops || !m_ops->slave_changelink)
2622ba7d49b1SJiri Pirko 					return -EOPNOTSUPP;
2623ba7d49b1SJiri Pirko 
2624ba7d49b1SJiri Pirko 				err = m_ops->slave_changelink(master_dev, dev,
262517dd0ec4SMatthias Schiffer 							      tb, slave_data,
262617dd0ec4SMatthias Schiffer 							      extack);
2627ba7d49b1SJiri Pirko 				if (err < 0)
2628ba7d49b1SJiri Pirko 					return err;
2629ba998906SNicolas Dichtel 				status |= DO_SETLINK_NOTIFY;
2630ba7d49b1SJiri Pirko 			}
2631ba7d49b1SJiri Pirko 
2632ddf9f970SJakub Kicinski 			return do_setlink(skb, dev, ifm, extack, tb, ifname,
2633ddf9f970SJakub Kicinski 					  status);
263438f7b870SPatrick McHardy 		}
263538f7b870SPatrick McHardy 
2636ffa934f1SPatrick McHardy 		if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
2637ffa934f1SPatrick McHardy 			if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
263890f62cf3SEric W. Biederman 				return rtnl_group_changelink(skb, net,
2639ffa934f1SPatrick McHardy 						nla_get_u32(tb[IFLA_GROUP]),
2640ddf9f970SJakub Kicinski 						ifm, extack, tb);
264138f7b870SPatrick McHardy 			return -ENODEV;
2642ffa934f1SPatrick McHardy 		}
264338f7b870SPatrick McHardy 
2644160ca014STheuns Verwoerd 		if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
264538f7b870SPatrick McHardy 			return -EOPNOTSUPP;
264638f7b870SPatrick McHardy 
264738f7b870SPatrick McHardy 		if (!ops) {
264895a5afcaSJohannes Berg #ifdef CONFIG_MODULES
264938f7b870SPatrick McHardy 			if (kind[0]) {
265038f7b870SPatrick McHardy 				__rtnl_unlock();
265138f7b870SPatrick McHardy 				request_module("rtnl-link-%s", kind);
265238f7b870SPatrick McHardy 				rtnl_lock();
265338f7b870SPatrick McHardy 				ops = rtnl_link_ops_get(kind);
265438f7b870SPatrick McHardy 				if (ops)
265538f7b870SPatrick McHardy 					goto replay;
265638f7b870SPatrick McHardy 			}
265738f7b870SPatrick McHardy #endif
265838f7b870SPatrick McHardy 			return -EOPNOTSUPP;
265938f7b870SPatrick McHardy 		}
266038f7b870SPatrick McHardy 
2661b0ab2fabSJiri Pirko 		if (!ops->setup)
2662b0ab2fabSJiri Pirko 			return -EOPNOTSUPP;
2663b0ab2fabSJiri Pirko 
26645517750fSTom Gundersen 		if (!ifname[0]) {
266538f7b870SPatrick McHardy 			snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
26665517750fSTom Gundersen 			name_assign_type = NET_NAME_ENUM;
26675517750fSTom Gundersen 		}
266838f7b870SPatrick McHardy 
266981adee47SEric W. Biederman 		dest_net = rtnl_link_get_net(net, tb);
267013ad1774SEric W. Biederman 		if (IS_ERR(dest_net))
267113ad1774SEric W. Biederman 			return PTR_ERR(dest_net);
267213ad1774SEric W. Biederman 
2673505ce415SEric W. Biederman 		err = -EPERM;
2674505ce415SEric W. Biederman 		if (!netlink_ns_capable(skb, dest_net->user_ns, CAP_NET_ADMIN))
2675505ce415SEric W. Biederman 			goto out;
2676505ce415SEric W. Biederman 
2677317f4810SNicolas Dichtel 		if (tb[IFLA_LINK_NETNSID]) {
2678317f4810SNicolas Dichtel 			int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
2679317f4810SNicolas Dichtel 
2680317f4810SNicolas Dichtel 			link_net = get_net_ns_by_id(dest_net, id);
2681317f4810SNicolas Dichtel 			if (!link_net) {
2682317f4810SNicolas Dichtel 				err =  -EINVAL;
2683317f4810SNicolas Dichtel 				goto out;
2684317f4810SNicolas Dichtel 			}
268506615bedSEric W. Biederman 			err = -EPERM;
268606615bedSEric W. Biederman 			if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
268706615bedSEric W. Biederman 				goto out;
2688317f4810SNicolas Dichtel 		}
2689317f4810SNicolas Dichtel 
2690317f4810SNicolas Dichtel 		dev = rtnl_create_link(link_net ? : dest_net, ifname,
2691317f4810SNicolas Dichtel 				       name_assign_type, ops, tb);
26929c7dafbfSPavel Emelyanov 		if (IS_ERR(dev)) {
2693e7199288SPavel Emelianov 			err = PTR_ERR(dev);
26949c7dafbfSPavel Emelyanov 			goto out;
26959c7dafbfSPavel Emelyanov 		}
26969c7dafbfSPavel Emelyanov 
26979c7dafbfSPavel Emelyanov 		dev->ifindex = ifm->ifi_index;
26989c7dafbfSPavel Emelyanov 
26990e0eee24SCong Wang 		if (ops->newlink) {
27007a3f4a18SMatthias Schiffer 			err = ops->newlink(link_net ? : net, dev, tb, data,
27017a3f4a18SMatthias Schiffer 					   extack);
27020e0eee24SCong Wang 			/* Drivers should call free_netdev() in ->destructor
2703e51fb152SCong Wang 			 * and unregister it on failure after registration
2704e51fb152SCong Wang 			 * so that device could be finally freed in rtnl_unlock.
27050e0eee24SCong Wang 			 */
2706e51fb152SCong Wang 			if (err < 0) {
2707e51fb152SCong Wang 				/* If device is not registered at all, free it now */
2708e51fb152SCong Wang 				if (dev->reg_state == NETREG_UNINITIALIZED)
2709e51fb152SCong Wang 					free_netdev(dev);
27100e0eee24SCong Wang 				goto out;
2711e51fb152SCong Wang 			}
27120e0eee24SCong Wang 		} else {
27132d85cba2SPatrick McHardy 			err = register_netdevice(dev);
2714fce9b9beSDan Carpenter 			if (err < 0) {
271538f7b870SPatrick McHardy 				free_netdev(dev);
27163729d502SPatrick McHardy 				goto out;
2717fce9b9beSDan Carpenter 			}
27180e0eee24SCong Wang 		}
27193729d502SPatrick McHardy 		err = rtnl_configure_link(dev, ifm);
272043638900SDavid S. Miller 		if (err < 0)
272143638900SDavid S. Miller 			goto out_unregister;
272243638900SDavid S. Miller 		if (link_net) {
272343638900SDavid S. Miller 			err = dev_change_net_namespace(dev, dest_net, ifname);
272443638900SDavid S. Miller 			if (err < 0)
272543638900SDavid S. Miller 				goto out_unregister;
272643638900SDavid S. Miller 		}
2727160ca014STheuns Verwoerd 		if (tb[IFLA_MASTER]) {
2728160ca014STheuns Verwoerd 			err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
2729160ca014STheuns Verwoerd 			if (err)
2730160ca014STheuns Verwoerd 				goto out_unregister;
2731160ca014STheuns Verwoerd 		}
273243638900SDavid S. Miller out:
273343638900SDavid S. Miller 		if (link_net)
273443638900SDavid S. Miller 			put_net(link_net);
273543638900SDavid S. Miller 		put_net(dest_net);
273643638900SDavid S. Miller 		return err;
273743638900SDavid S. Miller out_unregister:
27387afb8886SWANG Cong 		if (ops->newlink) {
27397afb8886SWANG Cong 			LIST_HEAD(list_kill);
27407afb8886SWANG Cong 
27417afb8886SWANG Cong 			ops->dellink(dev, &list_kill);
27427afb8886SWANG Cong 			unregister_netdevice_many(&list_kill);
27437afb8886SWANG Cong 		} else {
27443729d502SPatrick McHardy 			unregister_netdevice(dev);
27457afb8886SWANG Cong 		}
2746317f4810SNicolas Dichtel 		goto out;
2747317f4810SNicolas Dichtel 	}
274838f7b870SPatrick McHardy }
274938f7b870SPatrick McHardy 
2750c21ef3e3SDavid Ahern static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2751c21ef3e3SDavid Ahern 			struct netlink_ext_ack *extack)
2752711e2c33SJean Tourrilhes {
27533b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
2754b60c5115SThomas Graf 	struct ifinfomsg *ifm;
2755a3d12891SEric Dumazet 	char ifname[IFNAMSIZ];
2756b60c5115SThomas Graf 	struct nlattr *tb[IFLA_MAX+1];
2757b60c5115SThomas Graf 	struct net_device *dev = NULL;
2758b60c5115SThomas Graf 	struct sk_buff *nskb;
2759339bf98fSThomas Graf 	int err;
2760115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
2761711e2c33SJean Tourrilhes 
2762c21ef3e3SDavid Ahern 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
2763b60c5115SThomas Graf 	if (err < 0)
27649918f230SEric Sesterhenn 		return err;
2765b60c5115SThomas Graf 
2766a3d12891SEric Dumazet 	if (tb[IFLA_IFNAME])
2767a3d12891SEric Dumazet 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2768a3d12891SEric Dumazet 
2769115c9b81SGreg Rose 	if (tb[IFLA_EXT_MASK])
2770115c9b81SGreg Rose 		ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2771115c9b81SGreg Rose 
2772b60c5115SThomas Graf 	ifm = nlmsg_data(nlh);
2773a3d12891SEric Dumazet 	if (ifm->ifi_index > 0)
2774a3d12891SEric Dumazet 		dev = __dev_get_by_index(net, ifm->ifi_index);
2775a3d12891SEric Dumazet 	else if (tb[IFLA_IFNAME])
2776a3d12891SEric Dumazet 		dev = __dev_get_by_name(net, ifname);
2777a3d12891SEric Dumazet 	else
2778b60c5115SThomas Graf 		return -EINVAL;
2779b60c5115SThomas Graf 
2780a3d12891SEric Dumazet 	if (dev == NULL)
2781a3d12891SEric Dumazet 		return -ENODEV;
2782a3d12891SEric Dumazet 
2783115c9b81SGreg Rose 	nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
2784a3d12891SEric Dumazet 	if (nskb == NULL)
2785a3d12891SEric Dumazet 		return -ENOBUFS;
2786711e2c33SJean Tourrilhes 
278715e47304SEric W. Biederman 	err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid,
27883d3ea5afSVlad Yasevich 			       nlh->nlmsg_seq, 0, 0, ext_filter_mask, 0);
278926932566SPatrick McHardy 	if (err < 0) {
279026932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in if_nlmsg_size */
279126932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
279226932566SPatrick McHardy 		kfree_skb(nskb);
2793a3d12891SEric Dumazet 	} else
279415e47304SEric W. Biederman 		err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
2795b60c5115SThomas Graf 
2796711e2c33SJean Tourrilhes 	return err;
2797711e2c33SJean Tourrilhes }
2798711e2c33SJean Tourrilhes 
2799115c9b81SGreg Rose static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
2800c7ac8679SGreg Rose {
2801115c9b81SGreg Rose 	struct net *net = sock_net(skb->sk);
2802115c9b81SGreg Rose 	struct net_device *dev;
2803115c9b81SGreg Rose 	struct nlattr *tb[IFLA_MAX+1];
2804115c9b81SGreg Rose 	u32 ext_filter_mask = 0;
2805115c9b81SGreg Rose 	u16 min_ifinfo_dump_size = 0;
2806e5eca6d4SMichal Schmidt 	int hdrlen;
2807115c9b81SGreg Rose 
2808e5eca6d4SMichal Schmidt 	/* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
2809e5eca6d4SMichal Schmidt 	hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2810e5eca6d4SMichal Schmidt 		 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2811e5eca6d4SMichal Schmidt 
2812fceb6435SJohannes Berg 	if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
2813115c9b81SGreg Rose 		if (tb[IFLA_EXT_MASK])
2814115c9b81SGreg Rose 			ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2815a4b64fbeSEric Dumazet 	}
2816115c9b81SGreg Rose 
2817115c9b81SGreg Rose 	if (!ext_filter_mask)
2818115c9b81SGreg Rose 		return NLMSG_GOODSIZE;
2819115c9b81SGreg Rose 	/*
2820115c9b81SGreg Rose 	 * traverse the list of net devices and compute the minimum
2821115c9b81SGreg Rose 	 * buffer size based upon the filter mask.
2822115c9b81SGreg Rose 	 */
2823115c9b81SGreg Rose 	list_for_each_entry(dev, &net->dev_base_head, dev_list) {
2824115c9b81SGreg Rose 		min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
2825115c9b81SGreg Rose 					     if_nlmsg_size(dev,
2826115c9b81SGreg Rose 						           ext_filter_mask));
2827115c9b81SGreg Rose 	}
2828115c9b81SGreg Rose 
282993af2056SZhang Shengju 	return nlmsg_total_size(min_ifinfo_dump_size);
2830c7ac8679SGreg Rose }
2831c7ac8679SGreg Rose 
283242bad1daSAdrian Bunk static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
28331da177e4SLinus Torvalds {
28341da177e4SLinus Torvalds 	int idx;
28351da177e4SLinus Torvalds 	int s_idx = cb->family;
28361da177e4SLinus Torvalds 
28371da177e4SLinus Torvalds 	if (s_idx == 0)
28381da177e4SLinus Torvalds 		s_idx = 1;
283925239ceeSPatrick McHardy 	for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
28401da177e4SLinus Torvalds 		int type = cb->nlh->nlmsg_type-RTM_BASE;
28411da177e4SLinus Torvalds 		if (idx < s_idx || idx == PF_PACKET)
28421da177e4SLinus Torvalds 			continue;
2843e2849863SThomas Graf 		if (rtnl_msg_handlers[idx] == NULL ||
2844e2849863SThomas Graf 		    rtnl_msg_handlers[idx][type].dumpit == NULL)
28451da177e4SLinus Torvalds 			continue;
28460465277fSNicolas Dichtel 		if (idx > s_idx) {
28471da177e4SLinus Torvalds 			memset(&cb->args[0], 0, sizeof(cb->args));
28480465277fSNicolas Dichtel 			cb->prev_seq = 0;
28490465277fSNicolas Dichtel 			cb->seq = 0;
28500465277fSNicolas Dichtel 		}
2851e2849863SThomas Graf 		if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
28521da177e4SLinus Torvalds 			break;
28531da177e4SLinus Torvalds 	}
28541da177e4SLinus Torvalds 	cb->family = idx;
28551da177e4SLinus Torvalds 
28561da177e4SLinus Torvalds 	return skb->len;
28571da177e4SLinus Torvalds }
28581da177e4SLinus Torvalds 
2859395eea6cSMahesh Bandewar struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
28603d3ea5afSVlad Yasevich 				       unsigned int change,
28613d3ea5afSVlad Yasevich 				       u32 event, gfp_t flags)
28621da177e4SLinus Torvalds {
2863c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
28641da177e4SLinus Torvalds 	struct sk_buff *skb;
28650ec6d3f4SThomas Graf 	int err = -ENOBUFS;
2866c7ac8679SGreg Rose 	size_t if_info_size;
28671da177e4SLinus Torvalds 
28687f294054SAlexei Starovoitov 	skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
28690ec6d3f4SThomas Graf 	if (skb == NULL)
28700ec6d3f4SThomas Graf 		goto errout;
28711da177e4SLinus Torvalds 
28723d3ea5afSVlad Yasevich 	err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0, event);
287326932566SPatrick McHardy 	if (err < 0) {
287426932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in if_nlmsg_size() */
287526932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
287626932566SPatrick McHardy 		kfree_skb(skb);
287726932566SPatrick McHardy 		goto errout;
287826932566SPatrick McHardy 	}
2879395eea6cSMahesh Bandewar 	return skb;
28800ec6d3f4SThomas Graf errout:
28810ec6d3f4SThomas Graf 	if (err < 0)
28824b3da706SEric W. Biederman 		rtnl_set_sk_err(net, RTNLGRP_LINK, err);
2883395eea6cSMahesh Bandewar 	return NULL;
2884395eea6cSMahesh Bandewar }
2885395eea6cSMahesh Bandewar 
2886395eea6cSMahesh Bandewar void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
2887395eea6cSMahesh Bandewar {
2888395eea6cSMahesh Bandewar 	struct net *net = dev_net(dev);
2889395eea6cSMahesh Bandewar 
2890395eea6cSMahesh Bandewar 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
2891395eea6cSMahesh Bandewar }
2892395eea6cSMahesh Bandewar 
28933d3ea5afSVlad Yasevich static void rtmsg_ifinfo_event(int type, struct net_device *dev,
28943d3ea5afSVlad Yasevich 			       unsigned int change, u32 event,
2895395eea6cSMahesh Bandewar 			       gfp_t flags)
2896395eea6cSMahesh Bandewar {
2897395eea6cSMahesh Bandewar 	struct sk_buff *skb;
2898395eea6cSMahesh Bandewar 
2899ed2a80abSNicolas Dichtel 	if (dev->reg_state != NETREG_REGISTERED)
2900ed2a80abSNicolas Dichtel 		return;
2901ed2a80abSNicolas Dichtel 
29023d3ea5afSVlad Yasevich 	skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags);
2903395eea6cSMahesh Bandewar 	if (skb)
2904395eea6cSMahesh Bandewar 		rtmsg_ifinfo_send(skb, dev, flags);
29051da177e4SLinus Torvalds }
29063d3ea5afSVlad Yasevich 
29073d3ea5afSVlad Yasevich void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
29083d3ea5afSVlad Yasevich 		  gfp_t flags)
29093d3ea5afSVlad Yasevich {
29108c6c918dSVlad Yasevich 	rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags);
29113d3ea5afSVlad Yasevich }
2912471cb5a3SJiri Pirko EXPORT_SYMBOL(rtmsg_ifinfo);
29131da177e4SLinus Torvalds 
2914d83b0603SJohn Fastabend static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
2915d83b0603SJohn Fastabend 				   struct net_device *dev,
29161e53d5bbSHubert Sokolowski 				   u8 *addr, u16 vid, u32 pid, u32 seq,
29171c104a6bSNicolas Dichtel 				   int type, unsigned int flags,
2918b3379041SHubert Sokolowski 				   int nlflags, u16 ndm_state)
2919d83b0603SJohn Fastabend {
2920d83b0603SJohn Fastabend 	struct nlmsghdr *nlh;
2921d83b0603SJohn Fastabend 	struct ndmsg *ndm;
2922d83b0603SJohn Fastabend 
29231c104a6bSNicolas Dichtel 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
2924d83b0603SJohn Fastabend 	if (!nlh)
2925d83b0603SJohn Fastabend 		return -EMSGSIZE;
2926d83b0603SJohn Fastabend 
2927d83b0603SJohn Fastabend 	ndm = nlmsg_data(nlh);
2928d83b0603SJohn Fastabend 	ndm->ndm_family  = AF_BRIDGE;
2929d83b0603SJohn Fastabend 	ndm->ndm_pad1	 = 0;
2930d83b0603SJohn Fastabend 	ndm->ndm_pad2    = 0;
2931d83b0603SJohn Fastabend 	ndm->ndm_flags	 = flags;
2932d83b0603SJohn Fastabend 	ndm->ndm_type	 = 0;
2933d83b0603SJohn Fastabend 	ndm->ndm_ifindex = dev->ifindex;
2934b3379041SHubert Sokolowski 	ndm->ndm_state   = ndm_state;
2935d83b0603SJohn Fastabend 
2936d83b0603SJohn Fastabend 	if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
2937d83b0603SJohn Fastabend 		goto nla_put_failure;
29381e53d5bbSHubert Sokolowski 	if (vid)
29391e53d5bbSHubert Sokolowski 		if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
29401e53d5bbSHubert Sokolowski 			goto nla_put_failure;
2941d83b0603SJohn Fastabend 
2942053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
2943053c095aSJohannes Berg 	return 0;
2944d83b0603SJohn Fastabend 
2945d83b0603SJohn Fastabend nla_put_failure:
2946d83b0603SJohn Fastabend 	nlmsg_cancel(skb, nlh);
2947d83b0603SJohn Fastabend 	return -EMSGSIZE;
2948d83b0603SJohn Fastabend }
2949d83b0603SJohn Fastabend 
29503ff661c3SJohn Fastabend static inline size_t rtnl_fdb_nlmsg_size(void)
29513ff661c3SJohn Fastabend {
2952f82ef3e1SSabrina Dubroca 	return NLMSG_ALIGN(sizeof(struct ndmsg)) +
2953f82ef3e1SSabrina Dubroca 	       nla_total_size(ETH_ALEN) +	/* NDA_LLADDR */
2954f82ef3e1SSabrina Dubroca 	       nla_total_size(sizeof(u16)) +	/* NDA_VLAN */
2955f82ef3e1SSabrina Dubroca 	       0;
29563ff661c3SJohn Fastabend }
29573ff661c3SJohn Fastabend 
2958b3379041SHubert Sokolowski static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
2959b3379041SHubert Sokolowski 			    u16 ndm_state)
29603ff661c3SJohn Fastabend {
29613ff661c3SJohn Fastabend 	struct net *net = dev_net(dev);
29623ff661c3SJohn Fastabend 	struct sk_buff *skb;
29633ff661c3SJohn Fastabend 	int err = -ENOBUFS;
29643ff661c3SJohn Fastabend 
29653ff661c3SJohn Fastabend 	skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
29663ff661c3SJohn Fastabend 	if (!skb)
29673ff661c3SJohn Fastabend 		goto errout;
29683ff661c3SJohn Fastabend 
29691e53d5bbSHubert Sokolowski 	err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
2970b3379041SHubert Sokolowski 				      0, 0, type, NTF_SELF, 0, ndm_state);
29713ff661c3SJohn Fastabend 	if (err < 0) {
29723ff661c3SJohn Fastabend 		kfree_skb(skb);
29733ff661c3SJohn Fastabend 		goto errout;
29743ff661c3SJohn Fastabend 	}
29753ff661c3SJohn Fastabend 
29763ff661c3SJohn Fastabend 	rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
29773ff661c3SJohn Fastabend 	return;
29783ff661c3SJohn Fastabend errout:
29793ff661c3SJohn Fastabend 	rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
29803ff661c3SJohn Fastabend }
29813ff661c3SJohn Fastabend 
2982090096bfSVlad Yasevich /**
2983090096bfSVlad Yasevich  * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
2984090096bfSVlad Yasevich  */
2985090096bfSVlad Yasevich int ndo_dflt_fdb_add(struct ndmsg *ndm,
2986090096bfSVlad Yasevich 		     struct nlattr *tb[],
2987090096bfSVlad Yasevich 		     struct net_device *dev,
2988f6f6424bSJiri Pirko 		     const unsigned char *addr, u16 vid,
2989090096bfSVlad Yasevich 		     u16 flags)
2990090096bfSVlad Yasevich {
2991090096bfSVlad Yasevich 	int err = -EINVAL;
2992090096bfSVlad Yasevich 
2993090096bfSVlad Yasevich 	/* If aging addresses are supported device will need to
2994090096bfSVlad Yasevich 	 * implement its own handler for this.
2995090096bfSVlad Yasevich 	 */
2996090096bfSVlad Yasevich 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
2997090096bfSVlad Yasevich 		pr_info("%s: FDB only supports static addresses\n", dev->name);
2998090096bfSVlad Yasevich 		return err;
2999090096bfSVlad Yasevich 	}
3000090096bfSVlad Yasevich 
300165891feaSOr Gerlitz 	if (vid) {
300265891feaSOr Gerlitz 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
300365891feaSOr Gerlitz 		return err;
300465891feaSOr Gerlitz 	}
300565891feaSOr Gerlitz 
3006090096bfSVlad Yasevich 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3007090096bfSVlad Yasevich 		err = dev_uc_add_excl(dev, addr);
3008090096bfSVlad Yasevich 	else if (is_multicast_ether_addr(addr))
3009090096bfSVlad Yasevich 		err = dev_mc_add_excl(dev, addr);
3010090096bfSVlad Yasevich 
3011090096bfSVlad Yasevich 	/* Only return duplicate errors if NLM_F_EXCL is set */
3012090096bfSVlad Yasevich 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
3013090096bfSVlad Yasevich 		err = 0;
3014090096bfSVlad Yasevich 
3015090096bfSVlad Yasevich 	return err;
3016090096bfSVlad Yasevich }
3017090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_add);
3018090096bfSVlad Yasevich 
3019f6f6424bSJiri Pirko static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid)
3020f6f6424bSJiri Pirko {
3021f6f6424bSJiri Pirko 	u16 vid = 0;
3022f6f6424bSJiri Pirko 
3023f6f6424bSJiri Pirko 	if (vlan_attr) {
3024f6f6424bSJiri Pirko 		if (nla_len(vlan_attr) != sizeof(u16)) {
3025f6f6424bSJiri Pirko 			pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan\n");
3026f6f6424bSJiri Pirko 			return -EINVAL;
3027f6f6424bSJiri Pirko 		}
3028f6f6424bSJiri Pirko 
3029f6f6424bSJiri Pirko 		vid = nla_get_u16(vlan_attr);
3030f6f6424bSJiri Pirko 
3031f6f6424bSJiri Pirko 		if (!vid || vid >= VLAN_VID_MASK) {
3032f6f6424bSJiri Pirko 			pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan id %d\n",
3033f6f6424bSJiri Pirko 				vid);
3034f6f6424bSJiri Pirko 			return -EINVAL;
3035f6f6424bSJiri Pirko 		}
3036f6f6424bSJiri Pirko 	}
3037f6f6424bSJiri Pirko 	*p_vid = vid;
3038f6f6424bSJiri Pirko 	return 0;
3039f6f6424bSJiri Pirko }
3040f6f6424bSJiri Pirko 
3041c21ef3e3SDavid Ahern static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
3042c21ef3e3SDavid Ahern 			struct netlink_ext_ack *extack)
304377162022SJohn Fastabend {
304477162022SJohn Fastabend 	struct net *net = sock_net(skb->sk);
304577162022SJohn Fastabend 	struct ndmsg *ndm;
304677162022SJohn Fastabend 	struct nlattr *tb[NDA_MAX+1];
304777162022SJohn Fastabend 	struct net_device *dev;
304877162022SJohn Fastabend 	u8 *addr;
3049f6f6424bSJiri Pirko 	u16 vid;
305077162022SJohn Fastabend 	int err;
305177162022SJohn Fastabend 
3052c21ef3e3SDavid Ahern 	err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
305377162022SJohn Fastabend 	if (err < 0)
305477162022SJohn Fastabend 		return err;
305577162022SJohn Fastabend 
305677162022SJohn Fastabend 	ndm = nlmsg_data(nlh);
305777162022SJohn Fastabend 	if (ndm->ndm_ifindex == 0) {
305877162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
305977162022SJohn Fastabend 		return -EINVAL;
306077162022SJohn Fastabend 	}
306177162022SJohn Fastabend 
306277162022SJohn Fastabend 	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
306377162022SJohn Fastabend 	if (dev == NULL) {
306477162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
306577162022SJohn Fastabend 		return -ENODEV;
306677162022SJohn Fastabend 	}
306777162022SJohn Fastabend 
306877162022SJohn Fastabend 	if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
306977162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
307077162022SJohn Fastabend 		return -EINVAL;
307177162022SJohn Fastabend 	}
307277162022SJohn Fastabend 
307377162022SJohn Fastabend 	addr = nla_data(tb[NDA_LLADDR]);
307477162022SJohn Fastabend 
3075f6f6424bSJiri Pirko 	err = fdb_vid_parse(tb[NDA_VLAN], &vid);
3076f6f6424bSJiri Pirko 	if (err)
3077f6f6424bSJiri Pirko 		return err;
3078f6f6424bSJiri Pirko 
307977162022SJohn Fastabend 	err = -EOPNOTSUPP;
308077162022SJohn Fastabend 
308177162022SJohn Fastabend 	/* Support fdb on master device the net/bridge default case */
308277162022SJohn Fastabend 	if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
308377162022SJohn Fastabend 	    (dev->priv_flags & IFF_BRIDGE_PORT)) {
3084898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3085898e5061SJiri Pirko 		const struct net_device_ops *ops = br_dev->netdev_ops;
3086898e5061SJiri Pirko 
3087f6f6424bSJiri Pirko 		err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
3088f6f6424bSJiri Pirko 				       nlh->nlmsg_flags);
308977162022SJohn Fastabend 		if (err)
309077162022SJohn Fastabend 			goto out;
309177162022SJohn Fastabend 		else
309277162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_MASTER;
309377162022SJohn Fastabend 	}
309477162022SJohn Fastabend 
309577162022SJohn Fastabend 	/* Embedded bridge, macvlan, and any other device support */
3096090096bfSVlad Yasevich 	if ((ndm->ndm_flags & NTF_SELF)) {
3097090096bfSVlad Yasevich 		if (dev->netdev_ops->ndo_fdb_add)
3098090096bfSVlad Yasevich 			err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
3099f6f6424bSJiri Pirko 							   vid,
3100090096bfSVlad Yasevich 							   nlh->nlmsg_flags);
3101090096bfSVlad Yasevich 		else
3102f6f6424bSJiri Pirko 			err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
310377162022SJohn Fastabend 					       nlh->nlmsg_flags);
310477162022SJohn Fastabend 
31053ff661c3SJohn Fastabend 		if (!err) {
3106b3379041SHubert Sokolowski 			rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
3107b3379041SHubert Sokolowski 					ndm->ndm_state);
310877162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_SELF;
310977162022SJohn Fastabend 		}
31103ff661c3SJohn Fastabend 	}
311177162022SJohn Fastabend out:
311277162022SJohn Fastabend 	return err;
311377162022SJohn Fastabend }
311477162022SJohn Fastabend 
3115090096bfSVlad Yasevich /**
3116090096bfSVlad Yasevich  * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
3117090096bfSVlad Yasevich  */
3118090096bfSVlad Yasevich int ndo_dflt_fdb_del(struct ndmsg *ndm,
3119090096bfSVlad Yasevich 		     struct nlattr *tb[],
3120090096bfSVlad Yasevich 		     struct net_device *dev,
3121f6f6424bSJiri Pirko 		     const unsigned char *addr, u16 vid)
3122090096bfSVlad Yasevich {
3123c8a89c4aSAlexander Duyck 	int err = -EINVAL;
3124090096bfSVlad Yasevich 
3125090096bfSVlad Yasevich 	/* If aging addresses are supported device will need to
3126090096bfSVlad Yasevich 	 * implement its own handler for this.
3127090096bfSVlad Yasevich 	 */
312864535993SSridhar Samudrala 	if (!(ndm->ndm_state & NUD_PERMANENT)) {
3129090096bfSVlad Yasevich 		pr_info("%s: FDB only supports static addresses\n", dev->name);
3130c8a89c4aSAlexander Duyck 		return err;
3131090096bfSVlad Yasevich 	}
3132090096bfSVlad Yasevich 
3133090096bfSVlad Yasevich 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3134090096bfSVlad Yasevich 		err = dev_uc_del(dev, addr);
3135090096bfSVlad Yasevich 	else if (is_multicast_ether_addr(addr))
3136090096bfSVlad Yasevich 		err = dev_mc_del(dev, addr);
3137090096bfSVlad Yasevich 
3138090096bfSVlad Yasevich 	return err;
3139090096bfSVlad Yasevich }
3140090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_del);
3141090096bfSVlad Yasevich 
3142c21ef3e3SDavid Ahern static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
3143c21ef3e3SDavid Ahern 			struct netlink_ext_ack *extack)
314477162022SJohn Fastabend {
314577162022SJohn Fastabend 	struct net *net = sock_net(skb->sk);
314677162022SJohn Fastabend 	struct ndmsg *ndm;
31471690be63SVlad Yasevich 	struct nlattr *tb[NDA_MAX+1];
314877162022SJohn Fastabend 	struct net_device *dev;
314977162022SJohn Fastabend 	int err = -EINVAL;
315077162022SJohn Fastabend 	__u8 *addr;
3151f6f6424bSJiri Pirko 	u16 vid;
315277162022SJohn Fastabend 
315390f62cf3SEric W. Biederman 	if (!netlink_capable(skb, CAP_NET_ADMIN))
31541690be63SVlad Yasevich 		return -EPERM;
31551690be63SVlad Yasevich 
3156c21ef3e3SDavid Ahern 	err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
31571690be63SVlad Yasevich 	if (err < 0)
31581690be63SVlad Yasevich 		return err;
315977162022SJohn Fastabend 
316077162022SJohn Fastabend 	ndm = nlmsg_data(nlh);
316177162022SJohn Fastabend 	if (ndm->ndm_ifindex == 0) {
316277162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
316377162022SJohn Fastabend 		return -EINVAL;
316477162022SJohn Fastabend 	}
316577162022SJohn Fastabend 
316677162022SJohn Fastabend 	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
316777162022SJohn Fastabend 	if (dev == NULL) {
316877162022SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
316977162022SJohn Fastabend 		return -ENODEV;
317077162022SJohn Fastabend 	}
317177162022SJohn Fastabend 
31721690be63SVlad Yasevich 	if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
31731690be63SVlad Yasevich 		pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n");
317477162022SJohn Fastabend 		return -EINVAL;
317577162022SJohn Fastabend 	}
317677162022SJohn Fastabend 
31771690be63SVlad Yasevich 	addr = nla_data(tb[NDA_LLADDR]);
31781690be63SVlad Yasevich 
3179f6f6424bSJiri Pirko 	err = fdb_vid_parse(tb[NDA_VLAN], &vid);
3180f6f6424bSJiri Pirko 	if (err)
3181f6f6424bSJiri Pirko 		return err;
3182f6f6424bSJiri Pirko 
318377162022SJohn Fastabend 	err = -EOPNOTSUPP;
318477162022SJohn Fastabend 
318577162022SJohn Fastabend 	/* Support fdb on master device the net/bridge default case */
318677162022SJohn Fastabend 	if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
318777162022SJohn Fastabend 	    (dev->priv_flags & IFF_BRIDGE_PORT)) {
3188898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3189898e5061SJiri Pirko 		const struct net_device_ops *ops = br_dev->netdev_ops;
319077162022SJohn Fastabend 
3191898e5061SJiri Pirko 		if (ops->ndo_fdb_del)
3192f6f6424bSJiri Pirko 			err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
319377162022SJohn Fastabend 
319477162022SJohn Fastabend 		if (err)
319577162022SJohn Fastabend 			goto out;
319677162022SJohn Fastabend 		else
319777162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_MASTER;
319877162022SJohn Fastabend 	}
319977162022SJohn Fastabend 
320077162022SJohn Fastabend 	/* Embedded bridge, macvlan, and any other device support */
3201090096bfSVlad Yasevich 	if (ndm->ndm_flags & NTF_SELF) {
3202090096bfSVlad Yasevich 		if (dev->netdev_ops->ndo_fdb_del)
3203f6f6424bSJiri Pirko 			err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
3204f6f6424bSJiri Pirko 							   vid);
3205090096bfSVlad Yasevich 		else
3206f6f6424bSJiri Pirko 			err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
320777162022SJohn Fastabend 
32083ff661c3SJohn Fastabend 		if (!err) {
3209b3379041SHubert Sokolowski 			rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
3210b3379041SHubert Sokolowski 					ndm->ndm_state);
321177162022SJohn Fastabend 			ndm->ndm_flags &= ~NTF_SELF;
321277162022SJohn Fastabend 		}
32133ff661c3SJohn Fastabend 	}
321477162022SJohn Fastabend out:
321577162022SJohn Fastabend 	return err;
321677162022SJohn Fastabend }
321777162022SJohn Fastabend 
3218d83b0603SJohn Fastabend static int nlmsg_populate_fdb(struct sk_buff *skb,
3219d83b0603SJohn Fastabend 			      struct netlink_callback *cb,
3220d83b0603SJohn Fastabend 			      struct net_device *dev,
3221d83b0603SJohn Fastabend 			      int *idx,
3222d83b0603SJohn Fastabend 			      struct netdev_hw_addr_list *list)
3223d83b0603SJohn Fastabend {
3224d83b0603SJohn Fastabend 	struct netdev_hw_addr *ha;
3225d83b0603SJohn Fastabend 	int err;
322615e47304SEric W. Biederman 	u32 portid, seq;
3227d83b0603SJohn Fastabend 
322815e47304SEric W. Biederman 	portid = NETLINK_CB(cb->skb).portid;
3229d83b0603SJohn Fastabend 	seq = cb->nlh->nlmsg_seq;
3230d83b0603SJohn Fastabend 
3231d83b0603SJohn Fastabend 	list_for_each_entry(ha, &list->list, list) {
3232d297653dSRoopa Prabhu 		if (*idx < cb->args[2])
3233d83b0603SJohn Fastabend 			goto skip;
3234d83b0603SJohn Fastabend 
32351e53d5bbSHubert Sokolowski 		err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
3236a7a558feSJohn Fastabend 					      portid, seq,
32371c104a6bSNicolas Dichtel 					      RTM_NEWNEIGH, NTF_SELF,
3238b3379041SHubert Sokolowski 					      NLM_F_MULTI, NUD_PERMANENT);
3239d83b0603SJohn Fastabend 		if (err < 0)
3240d83b0603SJohn Fastabend 			return err;
3241d83b0603SJohn Fastabend skip:
3242d83b0603SJohn Fastabend 		*idx += 1;
3243d83b0603SJohn Fastabend 	}
3244d83b0603SJohn Fastabend 	return 0;
3245d83b0603SJohn Fastabend }
3246d83b0603SJohn Fastabend 
3247d83b0603SJohn Fastabend /**
32482c53040fSBen Hutchings  * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
3249d83b0603SJohn Fastabend  * @nlh: netlink message header
3250d83b0603SJohn Fastabend  * @dev: netdevice
3251d83b0603SJohn Fastabend  *
3252d83b0603SJohn Fastabend  * Default netdevice operation to dump the existing unicast address list.
325391f3e7b1SJohn Fastabend  * Returns number of addresses from list put in skb.
3254d83b0603SJohn Fastabend  */
3255d83b0603SJohn Fastabend int ndo_dflt_fdb_dump(struct sk_buff *skb,
3256d83b0603SJohn Fastabend 		      struct netlink_callback *cb,
3257d83b0603SJohn Fastabend 		      struct net_device *dev,
32585d5eacb3SJamal Hadi Salim 		      struct net_device *filter_dev,
3259d297653dSRoopa Prabhu 		      int *idx)
3260d83b0603SJohn Fastabend {
3261d83b0603SJohn Fastabend 	int err;
3262d83b0603SJohn Fastabend 
3263d83b0603SJohn Fastabend 	netif_addr_lock_bh(dev);
3264d297653dSRoopa Prabhu 	err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
3265d83b0603SJohn Fastabend 	if (err)
3266d83b0603SJohn Fastabend 		goto out;
32672934c9dbSZhang Shengju 	err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
3268d83b0603SJohn Fastabend out:
3269d83b0603SJohn Fastabend 	netif_addr_unlock_bh(dev);
3270d297653dSRoopa Prabhu 	return err;
3271d83b0603SJohn Fastabend }
3272d83b0603SJohn Fastabend EXPORT_SYMBOL(ndo_dflt_fdb_dump);
3273d83b0603SJohn Fastabend 
327477162022SJohn Fastabend static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
327577162022SJohn Fastabend {
327677162022SJohn Fastabend 	struct net_device *dev;
32775e6d2435SJamal Hadi Salim 	struct nlattr *tb[IFLA_MAX+1];
32785e6d2435SJamal Hadi Salim 	struct net_device *br_dev = NULL;
32795e6d2435SJamal Hadi Salim 	const struct net_device_ops *ops = NULL;
32805e6d2435SJamal Hadi Salim 	const struct net_device_ops *cops = NULL;
32815e6d2435SJamal Hadi Salim 	struct ifinfomsg *ifm = nlmsg_data(cb->nlh);
32825e6d2435SJamal Hadi Salim 	struct net *net = sock_net(skb->sk);
3283d297653dSRoopa Prabhu 	struct hlist_head *head;
32845e6d2435SJamal Hadi Salim 	int brport_idx = 0;
32855e6d2435SJamal Hadi Salim 	int br_idx = 0;
3286d297653dSRoopa Prabhu 	int h, s_h;
3287d297653dSRoopa Prabhu 	int idx = 0, s_idx;
3288d297653dSRoopa Prabhu 	int err = 0;
3289d297653dSRoopa Prabhu 	int fidx = 0;
329077162022SJohn Fastabend 
32910ff50e83SAlexander Potapenko 	err = nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb,
32920ff50e83SAlexander Potapenko 			  IFLA_MAX, ifla_policy, NULL);
32930ff50e83SAlexander Potapenko 	if (err < 0) {
32940ff50e83SAlexander Potapenko 		return -EINVAL;
32950ff50e83SAlexander Potapenko 	} else if (err == 0) {
32965e6d2435SJamal Hadi Salim 		if (tb[IFLA_MASTER])
32975e6d2435SJamal Hadi Salim 			br_idx = nla_get_u32(tb[IFLA_MASTER]);
32985e6d2435SJamal Hadi Salim 	}
329977162022SJohn Fastabend 
33005e6d2435SJamal Hadi Salim 	brport_idx = ifm->ifi_index;
33015e6d2435SJamal Hadi Salim 
33025e6d2435SJamal Hadi Salim 	if (br_idx) {
33035e6d2435SJamal Hadi Salim 		br_dev = __dev_get_by_index(net, br_idx);
33045e6d2435SJamal Hadi Salim 		if (!br_dev)
33055e6d2435SJamal Hadi Salim 			return -ENODEV;
33065e6d2435SJamal Hadi Salim 
3307898e5061SJiri Pirko 		ops = br_dev->netdev_ops;
33085e6d2435SJamal Hadi Salim 	}
33095e6d2435SJamal Hadi Salim 
3310d297653dSRoopa Prabhu 	s_h = cb->args[0];
3311d297653dSRoopa Prabhu 	s_idx = cb->args[1];
3312d297653dSRoopa Prabhu 
3313d297653dSRoopa Prabhu 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
3314d297653dSRoopa Prabhu 		idx = 0;
3315d297653dSRoopa Prabhu 		head = &net->dev_index_head[h];
3316d297653dSRoopa Prabhu 		hlist_for_each_entry(dev, head, index_hlist) {
3317d297653dSRoopa Prabhu 
33185e6d2435SJamal Hadi Salim 			if (brport_idx && (dev->ifindex != brport_idx))
33195e6d2435SJamal Hadi Salim 				continue;
33205e6d2435SJamal Hadi Salim 
33215e6d2435SJamal Hadi Salim 			if (!br_idx) { /* user did not specify a specific bridge */
33225e6d2435SJamal Hadi Salim 				if (dev->priv_flags & IFF_BRIDGE_PORT) {
33235e6d2435SJamal Hadi Salim 					br_dev = netdev_master_upper_dev_get(dev);
33245e6d2435SJamal Hadi Salim 					cops = br_dev->netdev_ops;
33255e6d2435SJamal Hadi Salim 				}
33265e6d2435SJamal Hadi Salim 			} else {
33275e6d2435SJamal Hadi Salim 				if (dev != br_dev &&
33285e6d2435SJamal Hadi Salim 				    !(dev->priv_flags & IFF_BRIDGE_PORT))
33295e6d2435SJamal Hadi Salim 					continue;
33305e6d2435SJamal Hadi Salim 
33315e6d2435SJamal Hadi Salim 				if (br_dev != netdev_master_upper_dev_get(dev) &&
33325e6d2435SJamal Hadi Salim 				    !(dev->priv_flags & IFF_EBRIDGE))
33335e6d2435SJamal Hadi Salim 					continue;
33345e6d2435SJamal Hadi Salim 				cops = ops;
33355e6d2435SJamal Hadi Salim 			}
33365e6d2435SJamal Hadi Salim 
3337d297653dSRoopa Prabhu 			if (idx < s_idx)
3338d297653dSRoopa Prabhu 				goto cont;
3339d297653dSRoopa Prabhu 
33405e6d2435SJamal Hadi Salim 			if (dev->priv_flags & IFF_BRIDGE_PORT) {
3341d297653dSRoopa Prabhu 				if (cops && cops->ndo_fdb_dump) {
3342d297653dSRoopa Prabhu 					err = cops->ndo_fdb_dump(skb, cb,
3343d297653dSRoopa Prabhu 								br_dev, dev,
3344d297653dSRoopa Prabhu 								&fidx);
3345d297653dSRoopa Prabhu 					if (err == -EMSGSIZE)
3346d297653dSRoopa Prabhu 						goto out;
334777162022SJohn Fastabend 				}
3348d297653dSRoopa Prabhu 			}
334977162022SJohn Fastabend 
33505e6d2435SJamal Hadi Salim 			if (dev->netdev_ops->ndo_fdb_dump)
3351d297653dSRoopa Prabhu 				err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
3352d297653dSRoopa Prabhu 								    dev, NULL,
3353d297653dSRoopa Prabhu 								    &fidx);
33546cb69742SHubert Sokolowski 			else
3355d297653dSRoopa Prabhu 				err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
3356d297653dSRoopa Prabhu 							&fidx);
3357d297653dSRoopa Prabhu 			if (err == -EMSGSIZE)
3358d297653dSRoopa Prabhu 				goto out;
33595e6d2435SJamal Hadi Salim 
33605e6d2435SJamal Hadi Salim 			cops = NULL;
3361d297653dSRoopa Prabhu 
3362d297653dSRoopa Prabhu 			/* reset fdb offset to 0 for rest of the interfaces */
3363d297653dSRoopa Prabhu 			cb->args[2] = 0;
3364d297653dSRoopa Prabhu 			fidx = 0;
3365d297653dSRoopa Prabhu cont:
3366d297653dSRoopa Prabhu 			idx++;
3367d297653dSRoopa Prabhu 		}
336877162022SJohn Fastabend 	}
336977162022SJohn Fastabend 
3370d297653dSRoopa Prabhu out:
3371d297653dSRoopa Prabhu 	cb->args[0] = h;
3372d297653dSRoopa Prabhu 	cb->args[1] = idx;
3373d297653dSRoopa Prabhu 	cb->args[2] = fidx;
3374d297653dSRoopa Prabhu 
337577162022SJohn Fastabend 	return skb->len;
337677162022SJohn Fastabend }
337777162022SJohn Fastabend 
33782c3c031cSScott Feldman static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
33792c3c031cSScott Feldman 			       unsigned int attrnum, unsigned int flag)
33802c3c031cSScott Feldman {
33812c3c031cSScott Feldman 	if (mask & flag)
33822c3c031cSScott Feldman 		return nla_put_u8(skb, attrnum, !!(flags & flag));
33832c3c031cSScott Feldman 	return 0;
33842c3c031cSScott Feldman }
33852c3c031cSScott Feldman 
3386815cccbfSJohn Fastabend int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
33872c3c031cSScott Feldman 			    struct net_device *dev, u16 mode,
33887d4f8d87SScott Feldman 			    u32 flags, u32 mask, int nlflags,
33897d4f8d87SScott Feldman 			    u32 filter_mask,
33907d4f8d87SScott Feldman 			    int (*vlan_fill)(struct sk_buff *skb,
33917d4f8d87SScott Feldman 					     struct net_device *dev,
33927d4f8d87SScott Feldman 					     u32 filter_mask))
3393815cccbfSJohn Fastabend {
3394815cccbfSJohn Fastabend 	struct nlmsghdr *nlh;
3395815cccbfSJohn Fastabend 	struct ifinfomsg *ifm;
3396815cccbfSJohn Fastabend 	struct nlattr *br_afspec;
33972c3c031cSScott Feldman 	struct nlattr *protinfo;
3398815cccbfSJohn Fastabend 	u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
3399898e5061SJiri Pirko 	struct net_device *br_dev = netdev_master_upper_dev_get(dev);
34007d4f8d87SScott Feldman 	int err = 0;
3401815cccbfSJohn Fastabend 
340246c264daSNicolas Dichtel 	nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
3403815cccbfSJohn Fastabend 	if (nlh == NULL)
3404815cccbfSJohn Fastabend 		return -EMSGSIZE;
3405815cccbfSJohn Fastabend 
3406815cccbfSJohn Fastabend 	ifm = nlmsg_data(nlh);
3407815cccbfSJohn Fastabend 	ifm->ifi_family = AF_BRIDGE;
3408815cccbfSJohn Fastabend 	ifm->__ifi_pad = 0;
3409815cccbfSJohn Fastabend 	ifm->ifi_type = dev->type;
3410815cccbfSJohn Fastabend 	ifm->ifi_index = dev->ifindex;
3411815cccbfSJohn Fastabend 	ifm->ifi_flags = dev_get_flags(dev);
3412815cccbfSJohn Fastabend 	ifm->ifi_change = 0;
3413815cccbfSJohn Fastabend 
3414815cccbfSJohn Fastabend 
3415815cccbfSJohn Fastabend 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
3416815cccbfSJohn Fastabend 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
3417815cccbfSJohn Fastabend 	    nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
3418898e5061SJiri Pirko 	    (br_dev &&
3419898e5061SJiri Pirko 	     nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
3420815cccbfSJohn Fastabend 	    (dev->addr_len &&
3421815cccbfSJohn Fastabend 	     nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
3422a54acb3aSNicolas Dichtel 	    (dev->ifindex != dev_get_iflink(dev) &&
3423a54acb3aSNicolas Dichtel 	     nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
3424815cccbfSJohn Fastabend 		goto nla_put_failure;
3425815cccbfSJohn Fastabend 
3426815cccbfSJohn Fastabend 	br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
3427815cccbfSJohn Fastabend 	if (!br_afspec)
3428815cccbfSJohn Fastabend 		goto nla_put_failure;
3429815cccbfSJohn Fastabend 
34301d460b98SRoopa Prabhu 	if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
3431815cccbfSJohn Fastabend 		nla_nest_cancel(skb, br_afspec);
3432815cccbfSJohn Fastabend 		goto nla_put_failure;
3433815cccbfSJohn Fastabend 	}
34341d460b98SRoopa Prabhu 
34351d460b98SRoopa Prabhu 	if (mode != BRIDGE_MODE_UNDEF) {
34361d460b98SRoopa Prabhu 		if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
34371d460b98SRoopa Prabhu 			nla_nest_cancel(skb, br_afspec);
34381d460b98SRoopa Prabhu 			goto nla_put_failure;
34391d460b98SRoopa Prabhu 		}
34401d460b98SRoopa Prabhu 	}
34417d4f8d87SScott Feldman 	if (vlan_fill) {
34427d4f8d87SScott Feldman 		err = vlan_fill(skb, dev, filter_mask);
34437d4f8d87SScott Feldman 		if (err) {
34447d4f8d87SScott Feldman 			nla_nest_cancel(skb, br_afspec);
34457d4f8d87SScott Feldman 			goto nla_put_failure;
34467d4f8d87SScott Feldman 		}
34477d4f8d87SScott Feldman 	}
3448815cccbfSJohn Fastabend 	nla_nest_end(skb, br_afspec);
3449815cccbfSJohn Fastabend 
34502c3c031cSScott Feldman 	protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
34512c3c031cSScott Feldman 	if (!protinfo)
34522c3c031cSScott Feldman 		goto nla_put_failure;
34532c3c031cSScott Feldman 
34542c3c031cSScott Feldman 	if (brport_nla_put_flag(skb, flags, mask,
34552c3c031cSScott Feldman 				IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
34562c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
34572c3c031cSScott Feldman 				IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
34582c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
34592c3c031cSScott Feldman 				IFLA_BRPORT_FAST_LEAVE,
34602c3c031cSScott Feldman 				BR_MULTICAST_FAST_LEAVE) ||
34612c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
34622c3c031cSScott Feldman 				IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
34632c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
34642c3c031cSScott Feldman 				IFLA_BRPORT_LEARNING, BR_LEARNING) ||
34652c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
34662c3c031cSScott Feldman 				IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
34672c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
34682c3c031cSScott Feldman 				IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
34692c3c031cSScott Feldman 	    brport_nla_put_flag(skb, flags, mask,
34702c3c031cSScott Feldman 				IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
34712c3c031cSScott Feldman 		nla_nest_cancel(skb, protinfo);
34722c3c031cSScott Feldman 		goto nla_put_failure;
34732c3c031cSScott Feldman 	}
34742c3c031cSScott Feldman 
34752c3c031cSScott Feldman 	nla_nest_end(skb, protinfo);
34762c3c031cSScott Feldman 
3477053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
3478053c095aSJohannes Berg 	return 0;
3479815cccbfSJohn Fastabend nla_put_failure:
3480815cccbfSJohn Fastabend 	nlmsg_cancel(skb, nlh);
34817d4f8d87SScott Feldman 	return err ? err : -EMSGSIZE;
3482815cccbfSJohn Fastabend }
34837d4f8d87SScott Feldman EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
3484815cccbfSJohn Fastabend 
3485e5a55a89SJohn Fastabend static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
3486e5a55a89SJohn Fastabend {
3487e5a55a89SJohn Fastabend 	struct net *net = sock_net(skb->sk);
3488e5a55a89SJohn Fastabend 	struct net_device *dev;
3489e5a55a89SJohn Fastabend 	int idx = 0;
3490e5a55a89SJohn Fastabend 	u32 portid = NETLINK_CB(cb->skb).portid;
3491e5a55a89SJohn Fastabend 	u32 seq = cb->nlh->nlmsg_seq;
34926cbdceebSVlad Yasevich 	u32 filter_mask = 0;
3493d64f69b0SRoopa Prabhu 	int err;
34946cbdceebSVlad Yasevich 
3495aa68c20fSThomas Graf 	if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) {
3496aa68c20fSThomas Graf 		struct nlattr *extfilt;
3497aa68c20fSThomas Graf 
34983e805ad2SAsbjoern Sloth Toennesen 		extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
34996cbdceebSVlad Yasevich 					  IFLA_EXT_MASK);
3500aa68c20fSThomas Graf 		if (extfilt) {
3501aa68c20fSThomas Graf 			if (nla_len(extfilt) < sizeof(filter_mask))
3502aa68c20fSThomas Graf 				return -EINVAL;
3503aa68c20fSThomas Graf 
35046cbdceebSVlad Yasevich 			filter_mask = nla_get_u32(extfilt);
3505aa68c20fSThomas Graf 		}
3506aa68c20fSThomas Graf 	}
3507e5a55a89SJohn Fastabend 
3508e5a55a89SJohn Fastabend 	rcu_read_lock();
3509e5a55a89SJohn Fastabend 	for_each_netdev_rcu(net, dev) {
3510e5a55a89SJohn Fastabend 		const struct net_device_ops *ops = dev->netdev_ops;
3511898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3512e5a55a89SJohn Fastabend 
3513898e5061SJiri Pirko 		if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
3514d64f69b0SRoopa Prabhu 			if (idx >= cb->args[0]) {
3515d64f69b0SRoopa Prabhu 				err = br_dev->netdev_ops->ndo_bridge_getlink(
3516d64f69b0SRoopa Prabhu 						skb, portid, seq, dev,
3517d64f69b0SRoopa Prabhu 						filter_mask, NLM_F_MULTI);
3518f6c5775fSDavid Ahern 				if (err < 0 && err != -EOPNOTSUPP) {
3519f6c5775fSDavid Ahern 					if (likely(skb->len))
3520e5a55a89SJohn Fastabend 						break;
3521f6c5775fSDavid Ahern 
3522f6c5775fSDavid Ahern 					goto out_err;
3523f6c5775fSDavid Ahern 				}
3524d64f69b0SRoopa Prabhu 			}
3525e5a55a89SJohn Fastabend 			idx++;
3526e5a55a89SJohn Fastabend 		}
3527e5a55a89SJohn Fastabend 
3528e5a55a89SJohn Fastabend 		if (ops->ndo_bridge_getlink) {
3529d64f69b0SRoopa Prabhu 			if (idx >= cb->args[0]) {
3530d64f69b0SRoopa Prabhu 				err = ops->ndo_bridge_getlink(skb, portid,
3531d64f69b0SRoopa Prabhu 							      seq, dev,
353246c264daSNicolas Dichtel 							      filter_mask,
3533d64f69b0SRoopa Prabhu 							      NLM_F_MULTI);
3534f6c5775fSDavid Ahern 				if (err < 0 && err != -EOPNOTSUPP) {
3535f6c5775fSDavid Ahern 					if (likely(skb->len))
3536e5a55a89SJohn Fastabend 						break;
3537f6c5775fSDavid Ahern 
3538f6c5775fSDavid Ahern 					goto out_err;
3539f6c5775fSDavid Ahern 				}
3540d64f69b0SRoopa Prabhu 			}
3541e5a55a89SJohn Fastabend 			idx++;
3542e5a55a89SJohn Fastabend 		}
3543e5a55a89SJohn Fastabend 	}
3544f6c5775fSDavid Ahern 	err = skb->len;
3545f6c5775fSDavid Ahern out_err:
3546e5a55a89SJohn Fastabend 	rcu_read_unlock();
3547e5a55a89SJohn Fastabend 	cb->args[0] = idx;
3548e5a55a89SJohn Fastabend 
3549f6c5775fSDavid Ahern 	return err;
3550e5a55a89SJohn Fastabend }
3551e5a55a89SJohn Fastabend 
35522469ffd7SJohn Fastabend static inline size_t bridge_nlmsg_size(void)
35532469ffd7SJohn Fastabend {
35542469ffd7SJohn Fastabend 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
35552469ffd7SJohn Fastabend 		+ nla_total_size(IFNAMSIZ)	/* IFLA_IFNAME */
35562469ffd7SJohn Fastabend 		+ nla_total_size(MAX_ADDR_LEN)	/* IFLA_ADDRESS */
35572469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_MASTER */
35582469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_MTU */
35592469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_LINK */
35602469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u32))	/* IFLA_OPERSTATE */
35612469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u8))	/* IFLA_PROTINFO */
35622469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(struct nlattr))	/* IFLA_AF_SPEC */
35632469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u16))	/* IFLA_BRIDGE_FLAGS */
35642469ffd7SJohn Fastabend 		+ nla_total_size(sizeof(u16));	/* IFLA_BRIDGE_MODE */
35652469ffd7SJohn Fastabend }
35662469ffd7SJohn Fastabend 
356702dba438SRoopa Prabhu static int rtnl_bridge_notify(struct net_device *dev)
35682469ffd7SJohn Fastabend {
35692469ffd7SJohn Fastabend 	struct net *net = dev_net(dev);
35702469ffd7SJohn Fastabend 	struct sk_buff *skb;
35712469ffd7SJohn Fastabend 	int err = -EOPNOTSUPP;
35722469ffd7SJohn Fastabend 
357302dba438SRoopa Prabhu 	if (!dev->netdev_ops->ndo_bridge_getlink)
357402dba438SRoopa Prabhu 		return 0;
357502dba438SRoopa Prabhu 
35762469ffd7SJohn Fastabend 	skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
35772469ffd7SJohn Fastabend 	if (!skb) {
35782469ffd7SJohn Fastabend 		err = -ENOMEM;
35792469ffd7SJohn Fastabend 		goto errout;
35802469ffd7SJohn Fastabend 	}
35812469ffd7SJohn Fastabend 
358246c264daSNicolas Dichtel 	err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
3583c38e01b8SJohn Fastabend 	if (err < 0)
3584c38e01b8SJohn Fastabend 		goto errout;
35852469ffd7SJohn Fastabend 
358659ccaaaaSRoopa Prabhu 	if (!skb->len)
358759ccaaaaSRoopa Prabhu 		goto errout;
358859ccaaaaSRoopa Prabhu 
35892469ffd7SJohn Fastabend 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
35902469ffd7SJohn Fastabend 	return 0;
35912469ffd7SJohn Fastabend errout:
35922469ffd7SJohn Fastabend 	WARN_ON(err == -EMSGSIZE);
35932469ffd7SJohn Fastabend 	kfree_skb(skb);
359459ccaaaaSRoopa Prabhu 	if (err)
35952469ffd7SJohn Fastabend 		rtnl_set_sk_err(net, RTNLGRP_LINK, err);
35962469ffd7SJohn Fastabend 	return err;
35972469ffd7SJohn Fastabend }
35982469ffd7SJohn Fastabend 
3599c21ef3e3SDavid Ahern static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3600c21ef3e3SDavid Ahern 			       struct netlink_ext_ack *extack)
3601e5a55a89SJohn Fastabend {
3602e5a55a89SJohn Fastabend 	struct net *net = sock_net(skb->sk);
3603e5a55a89SJohn Fastabend 	struct ifinfomsg *ifm;
3604e5a55a89SJohn Fastabend 	struct net_device *dev;
36052469ffd7SJohn Fastabend 	struct nlattr *br_spec, *attr = NULL;
36062469ffd7SJohn Fastabend 	int rem, err = -EOPNOTSUPP;
36074de8b413SRosen, Rami 	u16 flags = 0;
3608c38e01b8SJohn Fastabend 	bool have_flags = false;
3609e5a55a89SJohn Fastabend 
3610e5a55a89SJohn Fastabend 	if (nlmsg_len(nlh) < sizeof(*ifm))
3611e5a55a89SJohn Fastabend 		return -EINVAL;
3612e5a55a89SJohn Fastabend 
3613e5a55a89SJohn Fastabend 	ifm = nlmsg_data(nlh);
3614e5a55a89SJohn Fastabend 	if (ifm->ifi_family != AF_BRIDGE)
3615e5a55a89SJohn Fastabend 		return -EPFNOSUPPORT;
3616e5a55a89SJohn Fastabend 
3617e5a55a89SJohn Fastabend 	dev = __dev_get_by_index(net, ifm->ifi_index);
3618e5a55a89SJohn Fastabend 	if (!dev) {
3619e5a55a89SJohn Fastabend 		pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3620e5a55a89SJohn Fastabend 		return -ENODEV;
3621e5a55a89SJohn Fastabend 	}
3622e5a55a89SJohn Fastabend 
36232469ffd7SJohn Fastabend 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
36242469ffd7SJohn Fastabend 	if (br_spec) {
36252469ffd7SJohn Fastabend 		nla_for_each_nested(attr, br_spec, rem) {
36262469ffd7SJohn Fastabend 			if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
36276e8d1c55SThomas Graf 				if (nla_len(attr) < sizeof(flags))
36286e8d1c55SThomas Graf 					return -EINVAL;
36296e8d1c55SThomas Graf 
3630c38e01b8SJohn Fastabend 				have_flags = true;
36312469ffd7SJohn Fastabend 				flags = nla_get_u16(attr);
36322469ffd7SJohn Fastabend 				break;
36332469ffd7SJohn Fastabend 			}
36342469ffd7SJohn Fastabend 		}
36352469ffd7SJohn Fastabend 	}
36362469ffd7SJohn Fastabend 
36372469ffd7SJohn Fastabend 	if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3638898e5061SJiri Pirko 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3639898e5061SJiri Pirko 
3640898e5061SJiri Pirko 		if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
36412469ffd7SJohn Fastabend 			err = -EOPNOTSUPP;
3642e5a55a89SJohn Fastabend 			goto out;
3643e5a55a89SJohn Fastabend 		}
3644e5a55a89SJohn Fastabend 
3645add511b3SRoopa Prabhu 		err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags);
36462469ffd7SJohn Fastabend 		if (err)
36472469ffd7SJohn Fastabend 			goto out;
36482469ffd7SJohn Fastabend 
36492469ffd7SJohn Fastabend 		flags &= ~BRIDGE_FLAGS_MASTER;
36502469ffd7SJohn Fastabend 	}
36512469ffd7SJohn Fastabend 
36522469ffd7SJohn Fastabend 	if ((flags & BRIDGE_FLAGS_SELF)) {
36532469ffd7SJohn Fastabend 		if (!dev->netdev_ops->ndo_bridge_setlink)
36542469ffd7SJohn Fastabend 			err = -EOPNOTSUPP;
36552469ffd7SJohn Fastabend 		else
3656add511b3SRoopa Prabhu 			err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
3657add511b3SRoopa Prabhu 								  flags);
365802dba438SRoopa Prabhu 		if (!err) {
36592469ffd7SJohn Fastabend 			flags &= ~BRIDGE_FLAGS_SELF;
366002dba438SRoopa Prabhu 
366102dba438SRoopa Prabhu 			/* Generate event to notify upper layer of bridge
366202dba438SRoopa Prabhu 			 * change
366302dba438SRoopa Prabhu 			 */
366402dba438SRoopa Prabhu 			err = rtnl_bridge_notify(dev);
366502dba438SRoopa Prabhu 		}
36662469ffd7SJohn Fastabend 	}
36672469ffd7SJohn Fastabend 
3668c38e01b8SJohn Fastabend 	if (have_flags)
36692469ffd7SJohn Fastabend 		memcpy(nla_data(attr), &flags, sizeof(flags));
3670e5a55a89SJohn Fastabend out:
3671e5a55a89SJohn Fastabend 	return err;
3672e5a55a89SJohn Fastabend }
3673e5a55a89SJohn Fastabend 
3674c21ef3e3SDavid Ahern static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
3675c21ef3e3SDavid Ahern 			       struct netlink_ext_ack *extack)
3676407af329SVlad Yasevich {
3677407af329SVlad Yasevich 	struct net *net = sock_net(skb->sk);
3678407af329SVlad Yasevich 	struct ifinfomsg *ifm;
3679407af329SVlad Yasevich 	struct net_device *dev;
3680407af329SVlad Yasevich 	struct nlattr *br_spec, *attr = NULL;
3681407af329SVlad Yasevich 	int rem, err = -EOPNOTSUPP;
36824de8b413SRosen, Rami 	u16 flags = 0;
3683407af329SVlad Yasevich 	bool have_flags = false;
3684407af329SVlad Yasevich 
3685407af329SVlad Yasevich 	if (nlmsg_len(nlh) < sizeof(*ifm))
3686407af329SVlad Yasevich 		return -EINVAL;
3687407af329SVlad Yasevich 
3688407af329SVlad Yasevich 	ifm = nlmsg_data(nlh);
3689407af329SVlad Yasevich 	if (ifm->ifi_family != AF_BRIDGE)
3690407af329SVlad Yasevich 		return -EPFNOSUPPORT;
3691407af329SVlad Yasevich 
3692407af329SVlad Yasevich 	dev = __dev_get_by_index(net, ifm->ifi_index);
3693407af329SVlad Yasevich 	if (!dev) {
3694407af329SVlad Yasevich 		pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3695407af329SVlad Yasevich 		return -ENODEV;
3696407af329SVlad Yasevich 	}
3697407af329SVlad Yasevich 
3698407af329SVlad Yasevich 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3699407af329SVlad Yasevich 	if (br_spec) {
3700407af329SVlad Yasevich 		nla_for_each_nested(attr, br_spec, rem) {
3701407af329SVlad Yasevich 			if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
37026e8d1c55SThomas Graf 				if (nla_len(attr) < sizeof(flags))
37036e8d1c55SThomas Graf 					return -EINVAL;
37046e8d1c55SThomas Graf 
3705407af329SVlad Yasevich 				have_flags = true;
3706407af329SVlad Yasevich 				flags = nla_get_u16(attr);
3707407af329SVlad Yasevich 				break;
3708407af329SVlad Yasevich 			}
3709407af329SVlad Yasevich 		}
3710407af329SVlad Yasevich 	}
3711407af329SVlad Yasevich 
3712407af329SVlad Yasevich 	if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3713407af329SVlad Yasevich 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3714407af329SVlad Yasevich 
3715407af329SVlad Yasevich 		if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
3716407af329SVlad Yasevich 			err = -EOPNOTSUPP;
3717407af329SVlad Yasevich 			goto out;
3718407af329SVlad Yasevich 		}
3719407af329SVlad Yasevich 
3720add511b3SRoopa Prabhu 		err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
3721407af329SVlad Yasevich 		if (err)
3722407af329SVlad Yasevich 			goto out;
3723407af329SVlad Yasevich 
3724407af329SVlad Yasevich 		flags &= ~BRIDGE_FLAGS_MASTER;
3725407af329SVlad Yasevich 	}
3726407af329SVlad Yasevich 
3727407af329SVlad Yasevich 	if ((flags & BRIDGE_FLAGS_SELF)) {
3728407af329SVlad Yasevich 		if (!dev->netdev_ops->ndo_bridge_dellink)
3729407af329SVlad Yasevich 			err = -EOPNOTSUPP;
3730407af329SVlad Yasevich 		else
3731add511b3SRoopa Prabhu 			err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
3732add511b3SRoopa Prabhu 								  flags);
3733407af329SVlad Yasevich 
373402dba438SRoopa Prabhu 		if (!err) {
3735407af329SVlad Yasevich 			flags &= ~BRIDGE_FLAGS_SELF;
373602dba438SRoopa Prabhu 
373702dba438SRoopa Prabhu 			/* Generate event to notify upper layer of bridge
373802dba438SRoopa Prabhu 			 * change
373902dba438SRoopa Prabhu 			 */
374002dba438SRoopa Prabhu 			err = rtnl_bridge_notify(dev);
374102dba438SRoopa Prabhu 		}
3742407af329SVlad Yasevich 	}
3743407af329SVlad Yasevich 
3744407af329SVlad Yasevich 	if (have_flags)
3745407af329SVlad Yasevich 		memcpy(nla_data(attr), &flags, sizeof(flags));
3746407af329SVlad Yasevich out:
3747407af329SVlad Yasevich 	return err;
3748407af329SVlad Yasevich }
3749407af329SVlad Yasevich 
3750e8872a25SNikolay Aleksandrov static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
3751e8872a25SNikolay Aleksandrov {
3752e8872a25SNikolay Aleksandrov 	return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
3753e8872a25SNikolay Aleksandrov 	       (!idxattr || idxattr == attrid);
3754e8872a25SNikolay Aleksandrov }
3755e8872a25SNikolay Aleksandrov 
375669ae6ad2SNogah Frankel #define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
375769ae6ad2SNogah Frankel static int rtnl_get_offload_stats_attr_size(int attr_id)
375869ae6ad2SNogah Frankel {
375969ae6ad2SNogah Frankel 	switch (attr_id) {
376069ae6ad2SNogah Frankel 	case IFLA_OFFLOAD_XSTATS_CPU_HIT:
376169ae6ad2SNogah Frankel 		return sizeof(struct rtnl_link_stats64);
376269ae6ad2SNogah Frankel 	}
376369ae6ad2SNogah Frankel 
376469ae6ad2SNogah Frankel 	return 0;
376569ae6ad2SNogah Frankel }
376669ae6ad2SNogah Frankel 
376769ae6ad2SNogah Frankel static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev,
376869ae6ad2SNogah Frankel 				  int *prividx)
376969ae6ad2SNogah Frankel {
377069ae6ad2SNogah Frankel 	struct nlattr *attr = NULL;
377169ae6ad2SNogah Frankel 	int attr_id, size;
377269ae6ad2SNogah Frankel 	void *attr_data;
377369ae6ad2SNogah Frankel 	int err;
377469ae6ad2SNogah Frankel 
377569ae6ad2SNogah Frankel 	if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
377669ae6ad2SNogah Frankel 	      dev->netdev_ops->ndo_get_offload_stats))
377769ae6ad2SNogah Frankel 		return -ENODATA;
377869ae6ad2SNogah Frankel 
377969ae6ad2SNogah Frankel 	for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
378069ae6ad2SNogah Frankel 	     attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
378169ae6ad2SNogah Frankel 		if (attr_id < *prividx)
378269ae6ad2SNogah Frankel 			continue;
378369ae6ad2SNogah Frankel 
378469ae6ad2SNogah Frankel 		size = rtnl_get_offload_stats_attr_size(attr_id);
378569ae6ad2SNogah Frankel 		if (!size)
378669ae6ad2SNogah Frankel 			continue;
378769ae6ad2SNogah Frankel 
37883df5b3c6SOr Gerlitz 		if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
378969ae6ad2SNogah Frankel 			continue;
379069ae6ad2SNogah Frankel 
379169ae6ad2SNogah Frankel 		attr = nla_reserve_64bit(skb, attr_id, size,
379269ae6ad2SNogah Frankel 					 IFLA_OFFLOAD_XSTATS_UNSPEC);
379369ae6ad2SNogah Frankel 		if (!attr)
379469ae6ad2SNogah Frankel 			goto nla_put_failure;
379569ae6ad2SNogah Frankel 
379669ae6ad2SNogah Frankel 		attr_data = nla_data(attr);
379769ae6ad2SNogah Frankel 		memset(attr_data, 0, size);
379869ae6ad2SNogah Frankel 		err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev,
379969ae6ad2SNogah Frankel 							     attr_data);
380069ae6ad2SNogah Frankel 		if (err)
380169ae6ad2SNogah Frankel 			goto get_offload_stats_failure;
380269ae6ad2SNogah Frankel 	}
380369ae6ad2SNogah Frankel 
380469ae6ad2SNogah Frankel 	if (!attr)
380569ae6ad2SNogah Frankel 		return -ENODATA;
380669ae6ad2SNogah Frankel 
380769ae6ad2SNogah Frankel 	*prividx = 0;
380869ae6ad2SNogah Frankel 	return 0;
380969ae6ad2SNogah Frankel 
381069ae6ad2SNogah Frankel nla_put_failure:
381169ae6ad2SNogah Frankel 	err = -EMSGSIZE;
381269ae6ad2SNogah Frankel get_offload_stats_failure:
381369ae6ad2SNogah Frankel 	*prividx = attr_id;
381469ae6ad2SNogah Frankel 	return err;
381569ae6ad2SNogah Frankel }
381669ae6ad2SNogah Frankel 
381769ae6ad2SNogah Frankel static int rtnl_get_offload_stats_size(const struct net_device *dev)
381869ae6ad2SNogah Frankel {
381969ae6ad2SNogah Frankel 	int nla_size = 0;
382069ae6ad2SNogah Frankel 	int attr_id;
382169ae6ad2SNogah Frankel 	int size;
382269ae6ad2SNogah Frankel 
382369ae6ad2SNogah Frankel 	if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
382469ae6ad2SNogah Frankel 	      dev->netdev_ops->ndo_get_offload_stats))
382569ae6ad2SNogah Frankel 		return 0;
382669ae6ad2SNogah Frankel 
382769ae6ad2SNogah Frankel 	for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
382869ae6ad2SNogah Frankel 	     attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
38293df5b3c6SOr Gerlitz 		if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
383069ae6ad2SNogah Frankel 			continue;
383169ae6ad2SNogah Frankel 		size = rtnl_get_offload_stats_attr_size(attr_id);
383269ae6ad2SNogah Frankel 		nla_size += nla_total_size_64bit(size);
383369ae6ad2SNogah Frankel 	}
383469ae6ad2SNogah Frankel 
383569ae6ad2SNogah Frankel 	if (nla_size != 0)
383669ae6ad2SNogah Frankel 		nla_size += nla_total_size(0);
383769ae6ad2SNogah Frankel 
383869ae6ad2SNogah Frankel 	return nla_size;
383969ae6ad2SNogah Frankel }
384069ae6ad2SNogah Frankel 
384110c9ead9SRoopa Prabhu static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
384210c9ead9SRoopa Prabhu 			       int type, u32 pid, u32 seq, u32 change,
3843e8872a25SNikolay Aleksandrov 			       unsigned int flags, unsigned int filter_mask,
3844e8872a25SNikolay Aleksandrov 			       int *idxattr, int *prividx)
384510c9ead9SRoopa Prabhu {
384610c9ead9SRoopa Prabhu 	struct if_stats_msg *ifsm;
384710c9ead9SRoopa Prabhu 	struct nlmsghdr *nlh;
384810c9ead9SRoopa Prabhu 	struct nlattr *attr;
3849e8872a25SNikolay Aleksandrov 	int s_prividx = *prividx;
385069ae6ad2SNogah Frankel 	int err;
385110c9ead9SRoopa Prabhu 
385210c9ead9SRoopa Prabhu 	ASSERT_RTNL();
385310c9ead9SRoopa Prabhu 
385410c9ead9SRoopa Prabhu 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
385510c9ead9SRoopa Prabhu 	if (!nlh)
385610c9ead9SRoopa Prabhu 		return -EMSGSIZE;
385710c9ead9SRoopa Prabhu 
385810c9ead9SRoopa Prabhu 	ifsm = nlmsg_data(nlh);
385910c9ead9SRoopa Prabhu 	ifsm->ifindex = dev->ifindex;
386010c9ead9SRoopa Prabhu 	ifsm->filter_mask = filter_mask;
386110c9ead9SRoopa Prabhu 
3862e8872a25SNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
386310c9ead9SRoopa Prabhu 		struct rtnl_link_stats64 *sp;
386410c9ead9SRoopa Prabhu 
386558414d32SNicolas Dichtel 		attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
386658414d32SNicolas Dichtel 					 sizeof(struct rtnl_link_stats64),
386758414d32SNicolas Dichtel 					 IFLA_STATS_UNSPEC);
386810c9ead9SRoopa Prabhu 		if (!attr)
386910c9ead9SRoopa Prabhu 			goto nla_put_failure;
387010c9ead9SRoopa Prabhu 
387110c9ead9SRoopa Prabhu 		sp = nla_data(attr);
387210c9ead9SRoopa Prabhu 		dev_get_stats(dev, sp);
387310c9ead9SRoopa Prabhu 	}
387410c9ead9SRoopa Prabhu 
387597a47facSNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
387697a47facSNikolay Aleksandrov 		const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
387797a47facSNikolay Aleksandrov 
387897a47facSNikolay Aleksandrov 		if (ops && ops->fill_linkxstats) {
387997a47facSNikolay Aleksandrov 			*idxattr = IFLA_STATS_LINK_XSTATS;
388097a47facSNikolay Aleksandrov 			attr = nla_nest_start(skb,
388197a47facSNikolay Aleksandrov 					      IFLA_STATS_LINK_XSTATS);
388297a47facSNikolay Aleksandrov 			if (!attr)
388397a47facSNikolay Aleksandrov 				goto nla_put_failure;
388497a47facSNikolay Aleksandrov 
388580e73cc5SNikolay Aleksandrov 			err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
388680e73cc5SNikolay Aleksandrov 			nla_nest_end(skb, attr);
388780e73cc5SNikolay Aleksandrov 			if (err)
388880e73cc5SNikolay Aleksandrov 				goto nla_put_failure;
388980e73cc5SNikolay Aleksandrov 			*idxattr = 0;
389080e73cc5SNikolay Aleksandrov 		}
389180e73cc5SNikolay Aleksandrov 	}
389280e73cc5SNikolay Aleksandrov 
389380e73cc5SNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
389480e73cc5SNikolay Aleksandrov 			     *idxattr)) {
389580e73cc5SNikolay Aleksandrov 		const struct rtnl_link_ops *ops = NULL;
389680e73cc5SNikolay Aleksandrov 		const struct net_device *master;
389780e73cc5SNikolay Aleksandrov 
389880e73cc5SNikolay Aleksandrov 		master = netdev_master_upper_dev_get(dev);
389980e73cc5SNikolay Aleksandrov 		if (master)
390080e73cc5SNikolay Aleksandrov 			ops = master->rtnl_link_ops;
390180e73cc5SNikolay Aleksandrov 		if (ops && ops->fill_linkxstats) {
390280e73cc5SNikolay Aleksandrov 			*idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
390380e73cc5SNikolay Aleksandrov 			attr = nla_nest_start(skb,
390480e73cc5SNikolay Aleksandrov 					      IFLA_STATS_LINK_XSTATS_SLAVE);
390580e73cc5SNikolay Aleksandrov 			if (!attr)
390680e73cc5SNikolay Aleksandrov 				goto nla_put_failure;
390780e73cc5SNikolay Aleksandrov 
390880e73cc5SNikolay Aleksandrov 			err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
390997a47facSNikolay Aleksandrov 			nla_nest_end(skb, attr);
391097a47facSNikolay Aleksandrov 			if (err)
391197a47facSNikolay Aleksandrov 				goto nla_put_failure;
391297a47facSNikolay Aleksandrov 			*idxattr = 0;
391397a47facSNikolay Aleksandrov 		}
391497a47facSNikolay Aleksandrov 	}
391597a47facSNikolay Aleksandrov 
391669ae6ad2SNogah Frankel 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
391769ae6ad2SNogah Frankel 			     *idxattr)) {
391869ae6ad2SNogah Frankel 		*idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
391969ae6ad2SNogah Frankel 		attr = nla_nest_start(skb, IFLA_STATS_LINK_OFFLOAD_XSTATS);
392069ae6ad2SNogah Frankel 		if (!attr)
392169ae6ad2SNogah Frankel 			goto nla_put_failure;
392269ae6ad2SNogah Frankel 
392369ae6ad2SNogah Frankel 		err = rtnl_get_offload_stats(skb, dev, prividx);
392469ae6ad2SNogah Frankel 		if (err == -ENODATA)
392569ae6ad2SNogah Frankel 			nla_nest_cancel(skb, attr);
392669ae6ad2SNogah Frankel 		else
392769ae6ad2SNogah Frankel 			nla_nest_end(skb, attr);
392869ae6ad2SNogah Frankel 
392969ae6ad2SNogah Frankel 		if (err && err != -ENODATA)
393069ae6ad2SNogah Frankel 			goto nla_put_failure;
393169ae6ad2SNogah Frankel 		*idxattr = 0;
393269ae6ad2SNogah Frankel 	}
393369ae6ad2SNogah Frankel 
3934aefb4d4aSRobert Shearman 	if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
3935aefb4d4aSRobert Shearman 		struct rtnl_af_ops *af_ops;
3936aefb4d4aSRobert Shearman 
3937aefb4d4aSRobert Shearman 		*idxattr = IFLA_STATS_AF_SPEC;
3938aefb4d4aSRobert Shearman 		attr = nla_nest_start(skb, IFLA_STATS_AF_SPEC);
3939aefb4d4aSRobert Shearman 		if (!attr)
3940aefb4d4aSRobert Shearman 			goto nla_put_failure;
3941aefb4d4aSRobert Shearman 
3942aefb4d4aSRobert Shearman 		list_for_each_entry(af_ops, &rtnl_af_ops, list) {
3943aefb4d4aSRobert Shearman 			if (af_ops->fill_stats_af) {
3944aefb4d4aSRobert Shearman 				struct nlattr *af;
3945aefb4d4aSRobert Shearman 				int err;
3946aefb4d4aSRobert Shearman 
3947aefb4d4aSRobert Shearman 				af = nla_nest_start(skb, af_ops->family);
3948aefb4d4aSRobert Shearman 				if (!af)
3949aefb4d4aSRobert Shearman 					goto nla_put_failure;
3950aefb4d4aSRobert Shearman 
3951aefb4d4aSRobert Shearman 				err = af_ops->fill_stats_af(skb, dev);
3952aefb4d4aSRobert Shearman 
3953aefb4d4aSRobert Shearman 				if (err == -ENODATA)
3954aefb4d4aSRobert Shearman 					nla_nest_cancel(skb, af);
3955aefb4d4aSRobert Shearman 				else if (err < 0)
3956aefb4d4aSRobert Shearman 					goto nla_put_failure;
3957aefb4d4aSRobert Shearman 
3958aefb4d4aSRobert Shearman 				nla_nest_end(skb, af);
3959aefb4d4aSRobert Shearman 			}
3960aefb4d4aSRobert Shearman 		}
3961aefb4d4aSRobert Shearman 
3962aefb4d4aSRobert Shearman 		nla_nest_end(skb, attr);
3963aefb4d4aSRobert Shearman 
3964aefb4d4aSRobert Shearman 		*idxattr = 0;
3965aefb4d4aSRobert Shearman 	}
3966aefb4d4aSRobert Shearman 
396710c9ead9SRoopa Prabhu 	nlmsg_end(skb, nlh);
396810c9ead9SRoopa Prabhu 
396910c9ead9SRoopa Prabhu 	return 0;
397010c9ead9SRoopa Prabhu 
397110c9ead9SRoopa Prabhu nla_put_failure:
3972e8872a25SNikolay Aleksandrov 	/* not a multi message or no progress mean a real error */
3973e8872a25SNikolay Aleksandrov 	if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
397410c9ead9SRoopa Prabhu 		nlmsg_cancel(skb, nlh);
3975e8872a25SNikolay Aleksandrov 	else
3976e8872a25SNikolay Aleksandrov 		nlmsg_end(skb, nlh);
397710c9ead9SRoopa Prabhu 
397810c9ead9SRoopa Prabhu 	return -EMSGSIZE;
397910c9ead9SRoopa Prabhu }
398010c9ead9SRoopa Prabhu 
398110c9ead9SRoopa Prabhu static size_t if_nlmsg_stats_size(const struct net_device *dev,
398210c9ead9SRoopa Prabhu 				  u32 filter_mask)
398310c9ead9SRoopa Prabhu {
398410c9ead9SRoopa Prabhu 	size_t size = 0;
398510c9ead9SRoopa Prabhu 
3986e8872a25SNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
398710c9ead9SRoopa Prabhu 		size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
398810c9ead9SRoopa Prabhu 
398997a47facSNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
399097a47facSNikolay Aleksandrov 		const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
399180e73cc5SNikolay Aleksandrov 		int attr = IFLA_STATS_LINK_XSTATS;
399297a47facSNikolay Aleksandrov 
399397a47facSNikolay Aleksandrov 		if (ops && ops->get_linkxstats_size) {
399480e73cc5SNikolay Aleksandrov 			size += nla_total_size(ops->get_linkxstats_size(dev,
399580e73cc5SNikolay Aleksandrov 									attr));
399697a47facSNikolay Aleksandrov 			/* for IFLA_STATS_LINK_XSTATS */
399797a47facSNikolay Aleksandrov 			size += nla_total_size(0);
399897a47facSNikolay Aleksandrov 		}
399997a47facSNikolay Aleksandrov 	}
400097a47facSNikolay Aleksandrov 
400180e73cc5SNikolay Aleksandrov 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
400280e73cc5SNikolay Aleksandrov 		struct net_device *_dev = (struct net_device *)dev;
400380e73cc5SNikolay Aleksandrov 		const struct rtnl_link_ops *ops = NULL;
400480e73cc5SNikolay Aleksandrov 		const struct net_device *master;
400580e73cc5SNikolay Aleksandrov 
400680e73cc5SNikolay Aleksandrov 		/* netdev_master_upper_dev_get can't take const */
400780e73cc5SNikolay Aleksandrov 		master = netdev_master_upper_dev_get(_dev);
400880e73cc5SNikolay Aleksandrov 		if (master)
400980e73cc5SNikolay Aleksandrov 			ops = master->rtnl_link_ops;
401080e73cc5SNikolay Aleksandrov 		if (ops && ops->get_linkxstats_size) {
401180e73cc5SNikolay Aleksandrov 			int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
401280e73cc5SNikolay Aleksandrov 
401380e73cc5SNikolay Aleksandrov 			size += nla_total_size(ops->get_linkxstats_size(dev,
401480e73cc5SNikolay Aleksandrov 									attr));
401580e73cc5SNikolay Aleksandrov 			/* for IFLA_STATS_LINK_XSTATS_SLAVE */
401680e73cc5SNikolay Aleksandrov 			size += nla_total_size(0);
401780e73cc5SNikolay Aleksandrov 		}
401880e73cc5SNikolay Aleksandrov 	}
401980e73cc5SNikolay Aleksandrov 
402069ae6ad2SNogah Frankel 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0))
402169ae6ad2SNogah Frankel 		size += rtnl_get_offload_stats_size(dev);
402269ae6ad2SNogah Frankel 
4023aefb4d4aSRobert Shearman 	if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
4024aefb4d4aSRobert Shearman 		struct rtnl_af_ops *af_ops;
4025aefb4d4aSRobert Shearman 
4026aefb4d4aSRobert Shearman 		/* for IFLA_STATS_AF_SPEC */
4027aefb4d4aSRobert Shearman 		size += nla_total_size(0);
4028aefb4d4aSRobert Shearman 
4029aefb4d4aSRobert Shearman 		list_for_each_entry(af_ops, &rtnl_af_ops, list) {
4030aefb4d4aSRobert Shearman 			if (af_ops->get_stats_af_size) {
4031aefb4d4aSRobert Shearman 				size += nla_total_size(
4032aefb4d4aSRobert Shearman 					af_ops->get_stats_af_size(dev));
4033aefb4d4aSRobert Shearman 
4034aefb4d4aSRobert Shearman 				/* for AF_* */
4035aefb4d4aSRobert Shearman 				size += nla_total_size(0);
4036aefb4d4aSRobert Shearman 			}
4037aefb4d4aSRobert Shearman 		}
4038aefb4d4aSRobert Shearman 	}
4039aefb4d4aSRobert Shearman 
404010c9ead9SRoopa Prabhu 	return size;
404110c9ead9SRoopa Prabhu }
404210c9ead9SRoopa Prabhu 
4043c21ef3e3SDavid Ahern static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
4044c21ef3e3SDavid Ahern 			  struct netlink_ext_ack *extack)
404510c9ead9SRoopa Prabhu {
404610c9ead9SRoopa Prabhu 	struct net *net = sock_net(skb->sk);
404710c9ead9SRoopa Prabhu 	struct net_device *dev = NULL;
4048e8872a25SNikolay Aleksandrov 	int idxattr = 0, prividx = 0;
4049e8872a25SNikolay Aleksandrov 	struct if_stats_msg *ifsm;
405010c9ead9SRoopa Prabhu 	struct sk_buff *nskb;
405110c9ead9SRoopa Prabhu 	u32 filter_mask;
405210c9ead9SRoopa Prabhu 	int err;
405310c9ead9SRoopa Prabhu 
40544775cc1fSMathias Krause 	if (nlmsg_len(nlh) < sizeof(*ifsm))
40554775cc1fSMathias Krause 		return -EINVAL;
40564775cc1fSMathias Krause 
405710c9ead9SRoopa Prabhu 	ifsm = nlmsg_data(nlh);
405810c9ead9SRoopa Prabhu 	if (ifsm->ifindex > 0)
405910c9ead9SRoopa Prabhu 		dev = __dev_get_by_index(net, ifsm->ifindex);
406010c9ead9SRoopa Prabhu 	else
406110c9ead9SRoopa Prabhu 		return -EINVAL;
406210c9ead9SRoopa Prabhu 
406310c9ead9SRoopa Prabhu 	if (!dev)
406410c9ead9SRoopa Prabhu 		return -ENODEV;
406510c9ead9SRoopa Prabhu 
406610c9ead9SRoopa Prabhu 	filter_mask = ifsm->filter_mask;
406710c9ead9SRoopa Prabhu 	if (!filter_mask)
406810c9ead9SRoopa Prabhu 		return -EINVAL;
406910c9ead9SRoopa Prabhu 
407010c9ead9SRoopa Prabhu 	nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
407110c9ead9SRoopa Prabhu 	if (!nskb)
407210c9ead9SRoopa Prabhu 		return -ENOBUFS;
407310c9ead9SRoopa Prabhu 
407410c9ead9SRoopa Prabhu 	err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
407510c9ead9SRoopa Prabhu 				  NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
4076e8872a25SNikolay Aleksandrov 				  0, filter_mask, &idxattr, &prividx);
407710c9ead9SRoopa Prabhu 	if (err < 0) {
407810c9ead9SRoopa Prabhu 		/* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
407910c9ead9SRoopa Prabhu 		WARN_ON(err == -EMSGSIZE);
408010c9ead9SRoopa Prabhu 		kfree_skb(nskb);
408110c9ead9SRoopa Prabhu 	} else {
408210c9ead9SRoopa Prabhu 		err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
408310c9ead9SRoopa Prabhu 	}
408410c9ead9SRoopa Prabhu 
408510c9ead9SRoopa Prabhu 	return err;
408610c9ead9SRoopa Prabhu }
408710c9ead9SRoopa Prabhu 
408810c9ead9SRoopa Prabhu static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
408910c9ead9SRoopa Prabhu {
4090e8872a25SNikolay Aleksandrov 	int h, s_h, err, s_idx, s_idxattr, s_prividx;
409110c9ead9SRoopa Prabhu 	struct net *net = sock_net(skb->sk);
409210c9ead9SRoopa Prabhu 	unsigned int flags = NLM_F_MULTI;
4093e8872a25SNikolay Aleksandrov 	struct if_stats_msg *ifsm;
4094e8872a25SNikolay Aleksandrov 	struct hlist_head *head;
4095e8872a25SNikolay Aleksandrov 	struct net_device *dev;
409610c9ead9SRoopa Prabhu 	u32 filter_mask = 0;
4097e8872a25SNikolay Aleksandrov 	int idx = 0;
409810c9ead9SRoopa Prabhu 
409910c9ead9SRoopa Prabhu 	s_h = cb->args[0];
410010c9ead9SRoopa Prabhu 	s_idx = cb->args[1];
4101e8872a25SNikolay Aleksandrov 	s_idxattr = cb->args[2];
4102e8872a25SNikolay Aleksandrov 	s_prividx = cb->args[3];
410310c9ead9SRoopa Prabhu 
410410c9ead9SRoopa Prabhu 	cb->seq = net->dev_base_seq;
410510c9ead9SRoopa Prabhu 
41064775cc1fSMathias Krause 	if (nlmsg_len(cb->nlh) < sizeof(*ifsm))
41074775cc1fSMathias Krause 		return -EINVAL;
41084775cc1fSMathias Krause 
410910c9ead9SRoopa Prabhu 	ifsm = nlmsg_data(cb->nlh);
411010c9ead9SRoopa Prabhu 	filter_mask = ifsm->filter_mask;
411110c9ead9SRoopa Prabhu 	if (!filter_mask)
411210c9ead9SRoopa Prabhu 		return -EINVAL;
411310c9ead9SRoopa Prabhu 
411410c9ead9SRoopa Prabhu 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
411510c9ead9SRoopa Prabhu 		idx = 0;
411610c9ead9SRoopa Prabhu 		head = &net->dev_index_head[h];
411710c9ead9SRoopa Prabhu 		hlist_for_each_entry(dev, head, index_hlist) {
411810c9ead9SRoopa Prabhu 			if (idx < s_idx)
411910c9ead9SRoopa Prabhu 				goto cont;
412010c9ead9SRoopa Prabhu 			err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
412110c9ead9SRoopa Prabhu 						  NETLINK_CB(cb->skb).portid,
412210c9ead9SRoopa Prabhu 						  cb->nlh->nlmsg_seq, 0,
4123e8872a25SNikolay Aleksandrov 						  flags, filter_mask,
4124e8872a25SNikolay Aleksandrov 						  &s_idxattr, &s_prividx);
412510c9ead9SRoopa Prabhu 			/* If we ran out of room on the first message,
412610c9ead9SRoopa Prabhu 			 * we're in trouble
412710c9ead9SRoopa Prabhu 			 */
412810c9ead9SRoopa Prabhu 			WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
412910c9ead9SRoopa Prabhu 
413010c9ead9SRoopa Prabhu 			if (err < 0)
413110c9ead9SRoopa Prabhu 				goto out;
4132e8872a25SNikolay Aleksandrov 			s_prividx = 0;
4133e8872a25SNikolay Aleksandrov 			s_idxattr = 0;
413410c9ead9SRoopa Prabhu 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
413510c9ead9SRoopa Prabhu cont:
413610c9ead9SRoopa Prabhu 			idx++;
413710c9ead9SRoopa Prabhu 		}
413810c9ead9SRoopa Prabhu 	}
413910c9ead9SRoopa Prabhu out:
4140e8872a25SNikolay Aleksandrov 	cb->args[3] = s_prividx;
4141e8872a25SNikolay Aleksandrov 	cb->args[2] = s_idxattr;
414210c9ead9SRoopa Prabhu 	cb->args[1] = idx;
414310c9ead9SRoopa Prabhu 	cb->args[0] = h;
414410c9ead9SRoopa Prabhu 
414510c9ead9SRoopa Prabhu 	return skb->len;
414610c9ead9SRoopa Prabhu }
414710c9ead9SRoopa Prabhu 
41481da177e4SLinus Torvalds /* Process one rtnetlink message. */
41491da177e4SLinus Torvalds 
41502d4bc933SJohannes Berg static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
41512d4bc933SJohannes Berg 			     struct netlink_ext_ack *extack)
41521da177e4SLinus Torvalds {
41533b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
4154e2849863SThomas Graf 	rtnl_doit_func doit;
4155617cfc75SAlexander Kuleshov 	int kind;
41561da177e4SLinus Torvalds 	int family;
41571da177e4SLinus Torvalds 	int type;
41582907c35fSEric Dumazet 	int err;
41591da177e4SLinus Torvalds 
41601da177e4SLinus Torvalds 	type = nlh->nlmsg_type;
41611da177e4SLinus Torvalds 	if (type > RTM_MAX)
4162038890feSThomas Graf 		return -EOPNOTSUPP;
41631da177e4SLinus Torvalds 
41641da177e4SLinus Torvalds 	type -= RTM_BASE;
41651da177e4SLinus Torvalds 
41661da177e4SLinus Torvalds 	/* All the messages must have at least 1 byte length */
4167573ce260SHong zhi guo 	if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
41681da177e4SLinus Torvalds 		return 0;
41691da177e4SLinus Torvalds 
4170573ce260SHong zhi guo 	family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
41711da177e4SLinus Torvalds 	kind = type&3;
41721da177e4SLinus Torvalds 
417390f62cf3SEric W. Biederman 	if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
41741d00a4ebSThomas Graf 		return -EPERM;
41751da177e4SLinus Torvalds 
4176b8f3ab42SDavid S. Miller 	if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
417797c53cacSDenis V. Lunev 		struct sock *rtnl;
4178e2849863SThomas Graf 		rtnl_dumpit_func dumpit;
4179c7ac8679SGreg Rose 		u16 min_dump_alloc = 0;
41801da177e4SLinus Torvalds 
41810cc09020SFlorian Westphal 		rtnl_lock();
41820cc09020SFlorian Westphal 
4183e2849863SThomas Graf 		dumpit = rtnl_get_dumpit(family, type);
4184e2849863SThomas Graf 		if (dumpit == NULL)
41850cc09020SFlorian Westphal 			goto err_unlock;
4186e1fa6d21SFlorian Westphal 
4187019a3169SFlorian Westphal 		refcount_inc(&rtnl_msg_handlers_ref[family]);
4188019a3169SFlorian Westphal 
4189e1fa6d21SFlorian Westphal 		if (type == RTM_GETLINK)
4190e1fa6d21SFlorian Westphal 			min_dump_alloc = rtnl_calcit(skb, nlh);
41911da177e4SLinus Torvalds 
41922907c35fSEric Dumazet 		__rtnl_unlock();
419397c53cacSDenis V. Lunev 		rtnl = net->rtnl;
419480d326faSPablo Neira Ayuso 		{
419580d326faSPablo Neira Ayuso 			struct netlink_dump_control c = {
419680d326faSPablo Neira Ayuso 				.dump		= dumpit,
419780d326faSPablo Neira Ayuso 				.min_dump_alloc	= min_dump_alloc,
419880d326faSPablo Neira Ayuso 			};
419980d326faSPablo Neira Ayuso 			err = netlink_dump_start(rtnl, skb, nlh, &c);
420080d326faSPablo Neira Ayuso 		}
4201019a3169SFlorian Westphal 		refcount_dec(&rtnl_msg_handlers_ref[family]);
42022907c35fSEric Dumazet 		return err;
42031da177e4SLinus Torvalds 	}
42041da177e4SLinus Torvalds 
42050cc09020SFlorian Westphal 	rtnl_lock();
4206e2849863SThomas Graf 	doit = rtnl_get_doit(family, type);
4207e2849863SThomas Graf 	if (doit == NULL)
42080cc09020SFlorian Westphal 		goto err_unlock;
42091da177e4SLinus Torvalds 
42100cc09020SFlorian Westphal 	err = doit(skb, nlh, extack);
42110cc09020SFlorian Westphal 	rtnl_unlock();
42120cc09020SFlorian Westphal 
42130cc09020SFlorian Westphal 	return err;
42140cc09020SFlorian Westphal 
42150cc09020SFlorian Westphal err_unlock:
42160cc09020SFlorian Westphal 	rtnl_unlock();
42170cc09020SFlorian Westphal 	return -EOPNOTSUPP;
42181da177e4SLinus Torvalds }
42191da177e4SLinus Torvalds 
4220cd40b7d3SDenis V. Lunev static void rtnetlink_rcv(struct sk_buff *skb)
42211da177e4SLinus Torvalds {
4222cd40b7d3SDenis V. Lunev 	netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
42231da177e4SLinus Torvalds }
42241da177e4SLinus Torvalds 
42255f729eaaSJulien Gomes static int rtnetlink_bind(struct net *net, int group)
42265f729eaaSJulien Gomes {
42275f729eaaSJulien Gomes 	switch (group) {
42285f729eaaSJulien Gomes 	case RTNLGRP_IPV4_MROUTE_R:
42295f729eaaSJulien Gomes 	case RTNLGRP_IPV6_MROUTE_R:
42305f729eaaSJulien Gomes 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
42315f729eaaSJulien Gomes 			return -EPERM;
42325f729eaaSJulien Gomes 		break;
42335f729eaaSJulien Gomes 	}
42345f729eaaSJulien Gomes 	return 0;
42355f729eaaSJulien Gomes }
42365f729eaaSJulien Gomes 
42371da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
42381da177e4SLinus Torvalds {
4239351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
4240e9dc8653SEric W. Biederman 
42411da177e4SLinus Torvalds 	switch (event) {
42425138e86fSVlad Yasevich 	case NETDEV_REBOOT:
42433753654eSDavid Ahern 	case NETDEV_CHANGEADDR:
42445138e86fSVlad Yasevich 	case NETDEV_CHANGENAME:
42455138e86fSVlad Yasevich 	case NETDEV_FEAT_CHANGE:
42465138e86fSVlad Yasevich 	case NETDEV_BONDING_FAILOVER:
42475138e86fSVlad Yasevich 	case NETDEV_NOTIFY_PEERS:
42485138e86fSVlad Yasevich 	case NETDEV_RESEND_IGMP:
42495138e86fSVlad Yasevich 	case NETDEV_CHANGEINFODATA:
42503d3ea5afSVlad Yasevich 		rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
42513d3ea5afSVlad Yasevich 				   GFP_KERNEL);
42521da177e4SLinus Torvalds 		break;
42531da177e4SLinus Torvalds 	default:
42541da177e4SLinus Torvalds 		break;
42551da177e4SLinus Torvalds 	}
42561da177e4SLinus Torvalds 	return NOTIFY_DONE;
42571da177e4SLinus Torvalds }
42581da177e4SLinus Torvalds 
42591da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = {
42601da177e4SLinus Torvalds 	.notifier_call	= rtnetlink_event,
42611da177e4SLinus Torvalds };
42621da177e4SLinus Torvalds 
426397c53cacSDenis V. Lunev 
42642c8c1e72SAlexey Dobriyan static int __net_init rtnetlink_net_init(struct net *net)
426597c53cacSDenis V. Lunev {
426697c53cacSDenis V. Lunev 	struct sock *sk;
4267a31f2d17SPablo Neira Ayuso 	struct netlink_kernel_cfg cfg = {
4268a31f2d17SPablo Neira Ayuso 		.groups		= RTNLGRP_MAX,
4269a31f2d17SPablo Neira Ayuso 		.input		= rtnetlink_rcv,
4270a31f2d17SPablo Neira Ayuso 		.cb_mutex	= &rtnl_mutex,
42719785e10aSPablo Neira Ayuso 		.flags		= NL_CFG_F_NONROOT_RECV,
42725f729eaaSJulien Gomes 		.bind		= rtnetlink_bind,
4273a31f2d17SPablo Neira Ayuso 	};
4274a31f2d17SPablo Neira Ayuso 
42759f00d977SPablo Neira Ayuso 	sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
427697c53cacSDenis V. Lunev 	if (!sk)
427797c53cacSDenis V. Lunev 		return -ENOMEM;
427897c53cacSDenis V. Lunev 	net->rtnl = sk;
427997c53cacSDenis V. Lunev 	return 0;
428097c53cacSDenis V. Lunev }
428197c53cacSDenis V. Lunev 
42822c8c1e72SAlexey Dobriyan static void __net_exit rtnetlink_net_exit(struct net *net)
428397c53cacSDenis V. Lunev {
4284b7c6ba6eSDenis V. Lunev 	netlink_kernel_release(net->rtnl);
428597c53cacSDenis V. Lunev 	net->rtnl = NULL;
428697c53cacSDenis V. Lunev }
428797c53cacSDenis V. Lunev 
428897c53cacSDenis V. Lunev static struct pernet_operations rtnetlink_net_ops = {
428997c53cacSDenis V. Lunev 	.init = rtnetlink_net_init,
429097c53cacSDenis V. Lunev 	.exit = rtnetlink_net_exit,
429197c53cacSDenis V. Lunev };
429297c53cacSDenis V. Lunev 
42931da177e4SLinus Torvalds void __init rtnetlink_init(void)
42941da177e4SLinus Torvalds {
429597c53cacSDenis V. Lunev 	if (register_pernet_subsys(&rtnetlink_net_ops))
42961da177e4SLinus Torvalds 		panic("rtnetlink_init: cannot initialize rtnetlink\n");
429797c53cacSDenis V. Lunev 
42981da177e4SLinus Torvalds 	register_netdevice_notifier(&rtnetlink_dev_notifier);
4299340d17fcSThomas Graf 
4300c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
4301b97bac64SFlorian Westphal 		      rtnl_dump_ifinfo, 0);
4302b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0);
4303b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0);
4304b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0);
4305687ad8ccSThomas Graf 
4306b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0);
4307b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
4308b97bac64SFlorian Westphal 	rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
430977162022SJohn Fastabend 
4310b97bac64SFlorian Westphal 	rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
4311b97bac64SFlorian Westphal 	rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0);
4312b97bac64SFlorian Westphal 	rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, 0);
4313e5a55a89SJohn Fastabend 
4314b97bac64SFlorian Westphal 	rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
4315b97bac64SFlorian Westphal 	rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
4316b97bac64SFlorian Westphal 	rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0);
431710c9ead9SRoopa Prabhu 
431810c9ead9SRoopa Prabhu 	rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,
4319b97bac64SFlorian Westphal 		      0);
43201da177e4SLinus Torvalds }
4321