12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * INET An implementation of the TCP/IP protocol suite for the LINUX
41da177e4SLinus Torvalds * operating system. INET is implemented using the BSD Socket
51da177e4SLinus Torvalds * interface as the means of communication with the user level.
61da177e4SLinus Torvalds *
71da177e4SLinus Torvalds * Routing netlink socket interface: protocol independent part.
81da177e4SLinus Torvalds *
91da177e4SLinus Torvalds * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
101da177e4SLinus Torvalds *
111da177e4SLinus Torvalds * Fixes:
12d467d0bcSZheng Yongjun * Vitaly E. Lavrov RTA_OK arithmetic was wrong.
131da177e4SLinus Torvalds */
141da177e4SLinus Torvalds
15ee5d032fSJakub Kicinski #include <linux/bitops.h>
161da177e4SLinus Torvalds #include <linux/errno.h>
171da177e4SLinus Torvalds #include <linux/module.h>
181da177e4SLinus Torvalds #include <linux/types.h>
191da177e4SLinus Torvalds #include <linux/socket.h>
201da177e4SLinus Torvalds #include <linux/kernel.h>
211da177e4SLinus Torvalds #include <linux/timer.h>
221da177e4SLinus Torvalds #include <linux/string.h>
231da177e4SLinus Torvalds #include <linux/sockios.h>
241da177e4SLinus Torvalds #include <linux/net.h>
251da177e4SLinus Torvalds #include <linux/fcntl.h>
261da177e4SLinus Torvalds #include <linux/mm.h>
271da177e4SLinus Torvalds #include <linux/slab.h>
281da177e4SLinus Torvalds #include <linux/interrupt.h>
291da177e4SLinus Torvalds #include <linux/capability.h>
301da177e4SLinus Torvalds #include <linux/skbuff.h>
311da177e4SLinus Torvalds #include <linux/init.h>
321da177e4SLinus Torvalds #include <linux/security.h>
336756ae4bSStephen Hemminger #include <linux/mutex.h>
341823730fSThomas Graf #include <linux/if_addr.h>
3577162022SJohn Fastabend #include <linux/if_bridge.h>
36f6f6424bSJiri Pirko #include <linux/if_vlan.h>
37ebc08a6fSWilliams, Mitch A #include <linux/pci.h>
3877162022SJohn Fastabend #include <linux/etherdevice.h>
3958038695SMartin KaFai Lau #include <linux/bpf.h>
401da177e4SLinus Torvalds
417c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
421da177e4SLinus Torvalds
431da177e4SLinus Torvalds #include <linux/inet.h>
441da177e4SLinus Torvalds #include <linux/netdevice.h>
451da177e4SLinus Torvalds #include <net/ip.h>
461da177e4SLinus Torvalds #include <net/protocol.h>
471da177e4SLinus Torvalds #include <net/arp.h>
481da177e4SLinus Torvalds #include <net/route.h>
491da177e4SLinus Torvalds #include <net/udp.h>
50ea697639SDaniel Borkmann #include <net/tcp.h>
511da177e4SLinus Torvalds #include <net/sock.h>
521da177e4SLinus Torvalds #include <net/pkt_sched.h>
5314c0b97dSThomas Graf #include <net/fib_rules.h>
54e2849863SThomas Graf #include <net/rtnetlink.h>
5530ffee84SJohannes Berg #include <net/net_namespace.h>
56dca56c30SJiri Pirko #include <net/devlink.h>
57cc7f5022SIdo Schimmel #if IS_ENABLED(CONFIG_IPV6)
58cc7f5022SIdo Schimmel #include <net/addrconf.h>
59cc7f5022SIdo Schimmel #endif
601da177e4SLinus Torvalds
616264f58cSJakub Kicinski #include "dev.h"
626264f58cSJakub Kicinski
63a428afe8SNikolay Aleksandrov #define RTNL_MAX_TYPE 50
6429cfb2aaSIdo Schimmel #define RTNL_SLAVE_MAX_TYPE 44
65ccf8dbcdSKees Cook
66e0d087afSEric Dumazet struct rtnl_link {
67e2849863SThomas Graf rtnl_doit_func doit;
68e2849863SThomas Graf rtnl_dumpit_func dumpit;
69e4202511SFlorian Westphal struct module *owner;
7062256f98SFlorian Westphal unsigned int flags;
71addf9b90SFlorian Westphal struct rcu_head rcu;
72e2849863SThomas Graf };
73e2849863SThomas Graf
746756ae4bSStephen Hemminger static DEFINE_MUTEX(rtnl_mutex);
751da177e4SLinus Torvalds
rtnl_lock(void)761da177e4SLinus Torvalds void rtnl_lock(void)
771da177e4SLinus Torvalds {
786756ae4bSStephen Hemminger mutex_lock(&rtnl_mutex);
791da177e4SLinus Torvalds }
80e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_lock);
811da177e4SLinus Torvalds
rtnl_lock_killable(void)8279ffdfc6SKirill Tkhai int rtnl_lock_killable(void)
8379ffdfc6SKirill Tkhai {
8479ffdfc6SKirill Tkhai return mutex_lock_killable(&rtnl_mutex);
8579ffdfc6SKirill Tkhai }
8679ffdfc6SKirill Tkhai EXPORT_SYMBOL(rtnl_lock_killable);
8779ffdfc6SKirill Tkhai
881b5c5493SEric Dumazet static struct sk_buff *defer_kfree_skb_list;
rtnl_kfree_skbs(struct sk_buff * head,struct sk_buff * tail)891b5c5493SEric Dumazet void rtnl_kfree_skbs(struct sk_buff *head, struct sk_buff *tail)
901b5c5493SEric Dumazet {
911b5c5493SEric Dumazet if (head && tail) {
921b5c5493SEric Dumazet tail->next = defer_kfree_skb_list;
931b5c5493SEric Dumazet defer_kfree_skb_list = head;
941b5c5493SEric Dumazet }
951b5c5493SEric Dumazet }
961b5c5493SEric Dumazet EXPORT_SYMBOL(rtnl_kfree_skbs);
971b5c5493SEric Dumazet
__rtnl_unlock(void)986756ae4bSStephen Hemminger void __rtnl_unlock(void)
991da177e4SLinus Torvalds {
1001b5c5493SEric Dumazet struct sk_buff *head = defer_kfree_skb_list;
1011b5c5493SEric Dumazet
1021b5c5493SEric Dumazet defer_kfree_skb_list = NULL;
1031b5c5493SEric Dumazet
1040b5c21bbSJohannes Berg /* Ensure that we didn't actually add any TODO item when __rtnl_unlock()
1050b5c21bbSJohannes Berg * is used. In some places, e.g. in cfg80211, we have code that will do
1060b5c21bbSJohannes Berg * something like
1070b5c21bbSJohannes Berg * rtnl_lock()
1080b5c21bbSJohannes Berg * wiphy_lock()
1090b5c21bbSJohannes Berg * ...
1100b5c21bbSJohannes Berg * rtnl_unlock()
1110b5c21bbSJohannes Berg *
1120b5c21bbSJohannes Berg * and because netdev_run_todo() acquires the RTNL for items on the list
1130b5c21bbSJohannes Berg * we could cause a situation such as this:
1140b5c21bbSJohannes Berg * Thread 1 Thread 2
1150b5c21bbSJohannes Berg * rtnl_lock()
1160b5c21bbSJohannes Berg * unregister_netdevice()
1170b5c21bbSJohannes Berg * __rtnl_unlock()
1180b5c21bbSJohannes Berg * rtnl_lock()
1190b5c21bbSJohannes Berg * wiphy_lock()
1200b5c21bbSJohannes Berg * rtnl_unlock()
1210b5c21bbSJohannes Berg * netdev_run_todo()
1220b5c21bbSJohannes Berg * __rtnl_unlock()
1230b5c21bbSJohannes Berg *
1240b5c21bbSJohannes Berg * // list not empty now
1250b5c21bbSJohannes Berg * // because of thread 2
1260b5c21bbSJohannes Berg * rtnl_lock()
1270b5c21bbSJohannes Berg * while (!list_empty(...))
1280b5c21bbSJohannes Berg * rtnl_lock()
1290b5c21bbSJohannes Berg * wiphy_lock()
1300b5c21bbSJohannes Berg * **** DEADLOCK ****
1310b5c21bbSJohannes Berg *
1320b5c21bbSJohannes Berg * However, usage of __rtnl_unlock() is rare, and so we can ensure that
1330b5c21bbSJohannes Berg * it's not used in cases where something is added to do the list.
1340b5c21bbSJohannes Berg */
1350b5c21bbSJohannes Berg WARN_ON(!list_empty(&net_todo_list));
1360b5c21bbSJohannes Berg
1376756ae4bSStephen Hemminger mutex_unlock(&rtnl_mutex);
1381b5c5493SEric Dumazet
1391b5c5493SEric Dumazet while (head) {
1401b5c5493SEric Dumazet struct sk_buff *next = head->next;
1411b5c5493SEric Dumazet
1421b5c5493SEric Dumazet kfree_skb(head);
1431b5c5493SEric Dumazet cond_resched();
1441b5c5493SEric Dumazet head = next;
1451b5c5493SEric Dumazet }
1461da177e4SLinus Torvalds }
1471da177e4SLinus Torvalds
rtnl_unlock(void)1481da177e4SLinus Torvalds void rtnl_unlock(void)
1491da177e4SLinus Torvalds {
15058ec3b4dSHerbert Xu /* This fellow will unlock it for us. */
1511da177e4SLinus Torvalds netdev_run_todo();
1521da177e4SLinus Torvalds }
153e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unlock);
1541da177e4SLinus Torvalds
rtnl_trylock(void)1556756ae4bSStephen Hemminger int rtnl_trylock(void)
1566756ae4bSStephen Hemminger {
1576756ae4bSStephen Hemminger return mutex_trylock(&rtnl_mutex);
1586756ae4bSStephen Hemminger }
159e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_trylock);
1606756ae4bSStephen Hemminger
rtnl_is_locked(void)161c9c1014bSPatrick McHardy int rtnl_is_locked(void)
162c9c1014bSPatrick McHardy {
163c9c1014bSPatrick McHardy return mutex_is_locked(&rtnl_mutex);
164c9c1014bSPatrick McHardy }
165e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_is_locked);
166c9c1014bSPatrick McHardy
refcount_dec_and_rtnl_lock(refcount_t * r)1676f99528eSVlad Buslov bool refcount_dec_and_rtnl_lock(refcount_t *r)
1686f99528eSVlad Buslov {
1696f99528eSVlad Buslov return refcount_dec_and_mutex_lock(r, &rtnl_mutex);
1706f99528eSVlad Buslov }
1716f99528eSVlad Buslov EXPORT_SYMBOL(refcount_dec_and_rtnl_lock);
1726f99528eSVlad Buslov
173a898def2SPaul E. McKenney #ifdef CONFIG_PROVE_LOCKING
lockdep_rtnl_is_held(void)1740cbf3343SYaowei Bai bool lockdep_rtnl_is_held(void)
175a898def2SPaul E. McKenney {
176a898def2SPaul E. McKenney return lockdep_is_held(&rtnl_mutex);
177a898def2SPaul E. McKenney }
178a898def2SPaul E. McKenney EXPORT_SYMBOL(lockdep_rtnl_is_held);
179a898def2SPaul E. McKenney #endif /* #ifdef CONFIG_PROVE_LOCKING */
180a898def2SPaul E. McKenney
18151e13685SJakub Kicinski static struct rtnl_link __rcu *__rcu *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
182e2849863SThomas Graf
rtm_msgindex(int msgtype)183e2849863SThomas Graf static inline int rtm_msgindex(int msgtype)
184e2849863SThomas Graf {
185e2849863SThomas Graf int msgindex = msgtype - RTM_BASE;
186e2849863SThomas Graf
187e2849863SThomas Graf /*
188e2849863SThomas Graf * msgindex < 0 implies someone tried to register a netlink
189e2849863SThomas Graf * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
190e2849863SThomas Graf * the message type has not been added to linux/rtnetlink.h
191e2849863SThomas Graf */
192e2849863SThomas Graf BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
193e2849863SThomas Graf
194e2849863SThomas Graf return msgindex;
195e2849863SThomas Graf }
196e2849863SThomas Graf
rtnl_get_link(int protocol,int msgtype)197addf9b90SFlorian Westphal static struct rtnl_link *rtnl_get_link(int protocol, int msgtype)
198addf9b90SFlorian Westphal {
19951e13685SJakub Kicinski struct rtnl_link __rcu **tab;
200addf9b90SFlorian Westphal
201addf9b90SFlorian Westphal if (protocol >= ARRAY_SIZE(rtnl_msg_handlers))
202addf9b90SFlorian Westphal protocol = PF_UNSPEC;
203addf9b90SFlorian Westphal
204addf9b90SFlorian Westphal tab = rcu_dereference_rtnl(rtnl_msg_handlers[protocol]);
205addf9b90SFlorian Westphal if (!tab)
206addf9b90SFlorian Westphal tab = rcu_dereference_rtnl(rtnl_msg_handlers[PF_UNSPEC]);
207addf9b90SFlorian Westphal
20851e13685SJakub Kicinski return rcu_dereference_rtnl(tab[msgtype]);
209addf9b90SFlorian Westphal }
210addf9b90SFlorian Westphal
rtnl_register_internal(struct module * owner,int protocol,int msgtype,rtnl_doit_func doit,rtnl_dumpit_func dumpit,unsigned int flags)211e4202511SFlorian Westphal static int rtnl_register_internal(struct module *owner,
212e4202511SFlorian Westphal int protocol, int msgtype,
213c7ac8679SGreg Rose rtnl_doit_func doit, rtnl_dumpit_func dumpit,
214b97bac64SFlorian Westphal unsigned int flags)
215e2849863SThomas Graf {
216b0e9fe1bSFlorian Westphal struct rtnl_link *link, *old;
217b0e9fe1bSFlorian Westphal struct rtnl_link __rcu **tab;
218e2849863SThomas Graf int msgindex;
219addf9b90SFlorian Westphal int ret = -ENOBUFS;
220e2849863SThomas Graf
22125239ceeSPatrick McHardy BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
222e2849863SThomas Graf msgindex = rtm_msgindex(msgtype);
223e2849863SThomas Graf
224addf9b90SFlorian Westphal rtnl_lock();
22551e13685SJakub Kicinski tab = rtnl_dereference(rtnl_msg_handlers[protocol]);
226e2849863SThomas Graf if (tab == NULL) {
227addf9b90SFlorian Westphal tab = kcalloc(RTM_NR_MSGTYPES, sizeof(void *), GFP_KERNEL);
228addf9b90SFlorian Westphal if (!tab)
229addf9b90SFlorian Westphal goto unlock;
230e2849863SThomas Graf
231addf9b90SFlorian Westphal /* ensures we see the 0 stores */
2326853dd48SFlorian Westphal rcu_assign_pointer(rtnl_msg_handlers[protocol], tab);
233e2849863SThomas Graf }
234e2849863SThomas Graf
235addf9b90SFlorian Westphal old = rtnl_dereference(tab[msgindex]);
236addf9b90SFlorian Westphal if (old) {
237addf9b90SFlorian Westphal link = kmemdup(old, sizeof(*old), GFP_KERNEL);
238addf9b90SFlorian Westphal if (!link)
239addf9b90SFlorian Westphal goto unlock;
240addf9b90SFlorian Westphal } else {
241addf9b90SFlorian Westphal link = kzalloc(sizeof(*link), GFP_KERNEL);
242addf9b90SFlorian Westphal if (!link)
243addf9b90SFlorian Westphal goto unlock;
244addf9b90SFlorian Westphal }
245e2849863SThomas Graf
246e4202511SFlorian Westphal WARN_ON(link->owner && link->owner != owner);
247e4202511SFlorian Westphal link->owner = owner;
248e4202511SFlorian Westphal
249addf9b90SFlorian Westphal WARN_ON(doit && link->doit && link->doit != doit);
250addf9b90SFlorian Westphal if (doit)
251addf9b90SFlorian Westphal link->doit = doit;
252addf9b90SFlorian Westphal WARN_ON(dumpit && link->dumpit && link->dumpit != dumpit);
253addf9b90SFlorian Westphal if (dumpit)
254addf9b90SFlorian Westphal link->dumpit = dumpit;
255addf9b90SFlorian Westphal
256a6cec0bcSNikolay Aleksandrov WARN_ON(rtnl_msgtype_kind(msgtype) != RTNL_KIND_DEL &&
257a6cec0bcSNikolay Aleksandrov (flags & RTNL_FLAG_BULK_DEL_SUPPORTED));
258addf9b90SFlorian Westphal link->flags |= flags;
259addf9b90SFlorian Westphal
260addf9b90SFlorian Westphal /* publish protocol:msgtype */
261addf9b90SFlorian Westphal rcu_assign_pointer(tab[msgindex], link);
262addf9b90SFlorian Westphal ret = 0;
263addf9b90SFlorian Westphal if (old)
264addf9b90SFlorian Westphal kfree_rcu(old, rcu);
265addf9b90SFlorian Westphal unlock:
266addf9b90SFlorian Westphal rtnl_unlock();
267addf9b90SFlorian Westphal return ret;
268e2849863SThomas Graf }
269e4202511SFlorian Westphal
270e4202511SFlorian Westphal /**
271e4202511SFlorian Westphal * rtnl_register_module - Register a rtnetlink message type
272e4202511SFlorian Westphal *
273e4202511SFlorian Westphal * @owner: module registering the hook (THIS_MODULE)
274e4202511SFlorian Westphal * @protocol: Protocol family or PF_UNSPEC
275e4202511SFlorian Westphal * @msgtype: rtnetlink message type
276e4202511SFlorian Westphal * @doit: Function pointer called for each request message
277e4202511SFlorian Westphal * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
278d467d0bcSZheng Yongjun * @flags: rtnl_link_flags to modify behaviour of doit/dumpit functions
279e4202511SFlorian Westphal *
280e4202511SFlorian Westphal * Like rtnl_register, but for use by removable modules.
281e4202511SFlorian Westphal */
rtnl_register_module(struct module * owner,int protocol,int msgtype,rtnl_doit_func doit,rtnl_dumpit_func dumpit,unsigned int flags)282e4202511SFlorian Westphal int rtnl_register_module(struct module *owner,
283e4202511SFlorian Westphal int protocol, int msgtype,
284e4202511SFlorian Westphal rtnl_doit_func doit, rtnl_dumpit_func dumpit,
285e4202511SFlorian Westphal unsigned int flags)
286e4202511SFlorian Westphal {
287e4202511SFlorian Westphal return rtnl_register_internal(owner, protocol, msgtype,
288e4202511SFlorian Westphal doit, dumpit, flags);
289e4202511SFlorian Westphal }
290e4202511SFlorian Westphal EXPORT_SYMBOL_GPL(rtnl_register_module);
291e4202511SFlorian Westphal
292e4202511SFlorian Westphal /**
29316feebcfSFlorian Westphal * rtnl_register - Register a rtnetlink message type
294e4202511SFlorian Westphal * @protocol: Protocol family or PF_UNSPEC
295e4202511SFlorian Westphal * @msgtype: rtnetlink message type
296e4202511SFlorian Westphal * @doit: Function pointer called for each request message
297e4202511SFlorian Westphal * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
298d467d0bcSZheng Yongjun * @flags: rtnl_link_flags to modify behaviour of doit/dumpit functions
299e4202511SFlorian Westphal *
300e4202511SFlorian Westphal * Registers the specified function pointers (at least one of them has
301e4202511SFlorian Westphal * to be non-NULL) to be called whenever a request message for the
302e4202511SFlorian Westphal * specified protocol family and message type is received.
303e4202511SFlorian Westphal *
304e4202511SFlorian Westphal * The special protocol family PF_UNSPEC may be used to define fallback
305e4202511SFlorian Westphal * function pointers for the case when no entry for the specific protocol
306e4202511SFlorian Westphal * family exists.
307e2849863SThomas Graf */
rtnl_register(int protocol,int msgtype,rtnl_doit_func doit,rtnl_dumpit_func dumpit,unsigned int flags)308e2849863SThomas Graf void rtnl_register(int protocol, int msgtype,
309c7ac8679SGreg Rose rtnl_doit_func doit, rtnl_dumpit_func dumpit,
310b97bac64SFlorian Westphal unsigned int flags)
311e2849863SThomas Graf {
31216feebcfSFlorian Westphal int err;
31316feebcfSFlorian Westphal
31416feebcfSFlorian Westphal err = rtnl_register_internal(NULL, protocol, msgtype, doit, dumpit,
31516feebcfSFlorian Westphal flags);
31616feebcfSFlorian Westphal if (err)
31716feebcfSFlorian Westphal pr_err("Unable to register rtnetlink message handler, "
31816feebcfSFlorian Westphal "protocol = %d, message type = %d\n", protocol, msgtype);
319e2849863SThomas Graf }
320e2849863SThomas Graf
321e2849863SThomas Graf /**
322e2849863SThomas Graf * rtnl_unregister - Unregister a rtnetlink message type
323e2849863SThomas Graf * @protocol: Protocol family or PF_UNSPEC
324e2849863SThomas Graf * @msgtype: rtnetlink message type
325e2849863SThomas Graf *
326e2849863SThomas Graf * Returns 0 on success or a negative error code.
327e2849863SThomas Graf */
rtnl_unregister(int protocol,int msgtype)328e2849863SThomas Graf int rtnl_unregister(int protocol, int msgtype)
329e2849863SThomas Graf {
33051e13685SJakub Kicinski struct rtnl_link __rcu **tab;
33151e13685SJakub Kicinski struct rtnl_link *link;
332e2849863SThomas Graf int msgindex;
333e2849863SThomas Graf
33425239ceeSPatrick McHardy BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
335e2849863SThomas Graf msgindex = rtm_msgindex(msgtype);
336e2849863SThomas Graf
3376853dd48SFlorian Westphal rtnl_lock();
338addf9b90SFlorian Westphal tab = rtnl_dereference(rtnl_msg_handlers[protocol]);
339addf9b90SFlorian Westphal if (!tab) {
3406853dd48SFlorian Westphal rtnl_unlock();
341e2849863SThomas Graf return -ENOENT;
3426853dd48SFlorian Westphal }
343e2849863SThomas Graf
34451e13685SJakub Kicinski link = rtnl_dereference(tab[msgindex]);
3454fc29989SYajun Deng RCU_INIT_POINTER(tab[msgindex], NULL);
3466853dd48SFlorian Westphal rtnl_unlock();
347e2849863SThomas Graf
348addf9b90SFlorian Westphal kfree_rcu(link, rcu);
349addf9b90SFlorian Westphal
350e2849863SThomas Graf return 0;
351e2849863SThomas Graf }
352e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister);
353e2849863SThomas Graf
354e2849863SThomas Graf /**
355e2849863SThomas Graf * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
356e2849863SThomas Graf * @protocol : Protocol family or PF_UNSPEC
357e2849863SThomas Graf *
358e2849863SThomas Graf * Identical to calling rtnl_unregster() for all registered message types
359e2849863SThomas Graf * of a certain protocol family.
360e2849863SThomas Graf */
rtnl_unregister_all(int protocol)361e2849863SThomas Graf void rtnl_unregister_all(int protocol)
362e2849863SThomas Graf {
36351e13685SJakub Kicinski struct rtnl_link __rcu **tab;
36451e13685SJakub Kicinski struct rtnl_link *link;
365addf9b90SFlorian Westphal int msgindex;
366019a3169SFlorian Westphal
36725239ceeSPatrick McHardy BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
368e2849863SThomas Graf
369019a3169SFlorian Westphal rtnl_lock();
37051e13685SJakub Kicinski tab = rtnl_dereference(rtnl_msg_handlers[protocol]);
371f707ef61SSabrina Dubroca if (!tab) {
372f707ef61SSabrina Dubroca rtnl_unlock();
373f707ef61SSabrina Dubroca return;
374f707ef61SSabrina Dubroca }
3756853dd48SFlorian Westphal RCU_INIT_POINTER(rtnl_msg_handlers[protocol], NULL);
376addf9b90SFlorian Westphal for (msgindex = 0; msgindex < RTM_NR_MSGTYPES; msgindex++) {
37751e13685SJakub Kicinski link = rtnl_dereference(tab[msgindex]);
378addf9b90SFlorian Westphal if (!link)
379addf9b90SFlorian Westphal continue;
380addf9b90SFlorian Westphal
3814fc29989SYajun Deng RCU_INIT_POINTER(tab[msgindex], NULL);
382addf9b90SFlorian Westphal kfree_rcu(link, rcu);
383addf9b90SFlorian Westphal }
384019a3169SFlorian Westphal rtnl_unlock();
385019a3169SFlorian Westphal
3866853dd48SFlorian Westphal synchronize_net();
3876853dd48SFlorian Westphal
388addf9b90SFlorian Westphal kfree(tab);
389e2849863SThomas Graf }
390e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister_all);
3911da177e4SLinus Torvalds
__rtnl_register_many(const struct rtnl_msg_handler * handlers,int n)39235b72beaSKuniyuki Iwashima int __rtnl_register_many(const struct rtnl_msg_handler *handlers, int n)
39335b72beaSKuniyuki Iwashima {
39435b72beaSKuniyuki Iwashima const struct rtnl_msg_handler *handler;
39535b72beaSKuniyuki Iwashima int i, err;
39635b72beaSKuniyuki Iwashima
39735b72beaSKuniyuki Iwashima for (i = 0, handler = handlers; i < n; i++, handler++) {
39835b72beaSKuniyuki Iwashima err = rtnl_register_internal(handler->owner, handler->protocol,
39935b72beaSKuniyuki Iwashima handler->msgtype, handler->doit,
40035b72beaSKuniyuki Iwashima handler->dumpit, handler->flags);
40135b72beaSKuniyuki Iwashima if (err) {
40235b72beaSKuniyuki Iwashima __rtnl_unregister_many(handlers, i);
40335b72beaSKuniyuki Iwashima break;
40435b72beaSKuniyuki Iwashima }
40535b72beaSKuniyuki Iwashima }
40635b72beaSKuniyuki Iwashima
40735b72beaSKuniyuki Iwashima return err;
40835b72beaSKuniyuki Iwashima }
40935b72beaSKuniyuki Iwashima EXPORT_SYMBOL_GPL(__rtnl_register_many);
41035b72beaSKuniyuki Iwashima
__rtnl_unregister_many(const struct rtnl_msg_handler * handlers,int n)41135b72beaSKuniyuki Iwashima void __rtnl_unregister_many(const struct rtnl_msg_handler *handlers, int n)
41235b72beaSKuniyuki Iwashima {
41335b72beaSKuniyuki Iwashima const struct rtnl_msg_handler *handler;
41435b72beaSKuniyuki Iwashima int i;
41535b72beaSKuniyuki Iwashima
41635b72beaSKuniyuki Iwashima for (i = n - 1, handler = handlers + n - 1; i >= 0; i--, handler--)
41735b72beaSKuniyuki Iwashima rtnl_unregister(handler->protocol, handler->msgtype);
41835b72beaSKuniyuki Iwashima }
41935b72beaSKuniyuki Iwashima EXPORT_SYMBOL_GPL(__rtnl_unregister_many);
42035b72beaSKuniyuki Iwashima
42138f7b870SPatrick McHardy static LIST_HEAD(link_ops);
42238f7b870SPatrick McHardy
rtnl_link_ops_get(const char * kind)423c63044f0SEric Dumazet static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
424c63044f0SEric Dumazet {
425c63044f0SEric Dumazet const struct rtnl_link_ops *ops;
426c63044f0SEric Dumazet
427c63044f0SEric Dumazet list_for_each_entry(ops, &link_ops, list) {
428c63044f0SEric Dumazet if (!strcmp(ops->kind, kind))
429c63044f0SEric Dumazet return ops;
430c63044f0SEric Dumazet }
431c63044f0SEric Dumazet return NULL;
432c63044f0SEric Dumazet }
433c63044f0SEric Dumazet
43438f7b870SPatrick McHardy /**
43538f7b870SPatrick McHardy * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
43638f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to register
43738f7b870SPatrick McHardy *
43838f7b870SPatrick McHardy * The caller must hold the rtnl_mutex. This function should be used
43938f7b870SPatrick McHardy * by drivers that create devices during module initialization. It
44038f7b870SPatrick McHardy * must be called before registering the devices.
44138f7b870SPatrick McHardy *
44238f7b870SPatrick McHardy * Returns 0 on success or a negative error code.
44338f7b870SPatrick McHardy */
__rtnl_link_register(struct rtnl_link_ops * ops)44438f7b870SPatrick McHardy int __rtnl_link_register(struct rtnl_link_ops *ops)
44538f7b870SPatrick McHardy {
446c63044f0SEric Dumazet if (rtnl_link_ops_get(ops->kind))
447c63044f0SEric Dumazet return -EEXIST;
448c63044f0SEric Dumazet
4498c713dc9SJohannes Berg /* The check for alloc/setup is here because if ops
450b0ab2fabSJiri Pirko * does not have that filled up, it is not possible
451b0ab2fabSJiri Pirko * to use the ops for creating device. So do not
452b0ab2fabSJiri Pirko * fill up dellink as well. That disables rtnl_dellink.
453b0ab2fabSJiri Pirko */
4548c713dc9SJohannes Berg if ((ops->alloc || ops->setup) && !ops->dellink)
45523289a37SEric Dumazet ops->dellink = unregister_netdevice_queue;
4562d85cba2SPatrick McHardy
45738f7b870SPatrick McHardy list_add_tail(&ops->list, &link_ops);
45838f7b870SPatrick McHardy return 0;
45938f7b870SPatrick McHardy }
46038f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_register);
46138f7b870SPatrick McHardy
46238f7b870SPatrick McHardy /**
46338f7b870SPatrick McHardy * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
46438f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to register
46538f7b870SPatrick McHardy *
46638f7b870SPatrick McHardy * Returns 0 on success or a negative error code.
46738f7b870SPatrick McHardy */
rtnl_link_register(struct rtnl_link_ops * ops)46838f7b870SPatrick McHardy int rtnl_link_register(struct rtnl_link_ops *ops)
46938f7b870SPatrick McHardy {
47038f7b870SPatrick McHardy int err;
47138f7b870SPatrick McHardy
472ccf8dbcdSKees Cook /* Sanity-check max sizes to avoid stack buffer overflow. */
473ccf8dbcdSKees Cook if (WARN_ON(ops->maxtype > RTNL_MAX_TYPE ||
474ccf8dbcdSKees Cook ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE))
475ccf8dbcdSKees Cook return -EINVAL;
476ccf8dbcdSKees Cook
47738f7b870SPatrick McHardy rtnl_lock();
47838f7b870SPatrick McHardy err = __rtnl_link_register(ops);
47938f7b870SPatrick McHardy rtnl_unlock();
48038f7b870SPatrick McHardy return err;
48138f7b870SPatrick McHardy }
48238f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_register);
48338f7b870SPatrick McHardy
__rtnl_kill_links(struct net * net,struct rtnl_link_ops * ops)484669f87baSPavel Emelyanov static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
485669f87baSPavel Emelyanov {
486669f87baSPavel Emelyanov struct net_device *dev;
48723289a37SEric Dumazet LIST_HEAD(list_kill);
48823289a37SEric Dumazet
489669f87baSPavel Emelyanov for_each_netdev(net, dev) {
49023289a37SEric Dumazet if (dev->rtnl_link_ops == ops)
49123289a37SEric Dumazet ops->dellink(dev, &list_kill);
492669f87baSPavel Emelyanov }
49323289a37SEric Dumazet unregister_netdevice_many(&list_kill);
494669f87baSPavel Emelyanov }
495669f87baSPavel Emelyanov
49638f7b870SPatrick McHardy /**
49738f7b870SPatrick McHardy * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
49838f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to unregister
49938f7b870SPatrick McHardy *
500554873e5SKirill Tkhai * The caller must hold the rtnl_mutex and guarantee net_namespace_list
501554873e5SKirill Tkhai * integrity (hold pernet_ops_rwsem for writing to close the race
502554873e5SKirill Tkhai * with setup_net() and cleanup_net()).
50338f7b870SPatrick McHardy */
__rtnl_link_unregister(struct rtnl_link_ops * ops)50438f7b870SPatrick McHardy void __rtnl_link_unregister(struct rtnl_link_ops *ops)
50538f7b870SPatrick McHardy {
506881d966bSEric W. Biederman struct net *net;
5072d85cba2SPatrick McHardy
508881d966bSEric W. Biederman for_each_net(net) {
509669f87baSPavel Emelyanov __rtnl_kill_links(net, ops);
510881d966bSEric W. Biederman }
51138f7b870SPatrick McHardy list_del(&ops->list);
51238f7b870SPatrick McHardy }
51338f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
51438f7b870SPatrick McHardy
515200b916fSCong Wang /* Return with the rtnl_lock held when there are no network
516200b916fSCong Wang * devices unregistering in any network namespace.
517200b916fSCong Wang */
rtnl_lock_unregistering_all(void)518200b916fSCong Wang static void rtnl_lock_unregistering_all(void)
519200b916fSCong Wang {
520200b916fSCong Wang struct net *net;
521200b916fSCong Wang bool unregistering;
522ff960a73SPeter Zijlstra DEFINE_WAIT_FUNC(wait, woken_wake_function);
523200b916fSCong Wang
524ff960a73SPeter Zijlstra add_wait_queue(&netdev_unregistering_wq, &wait);
525200b916fSCong Wang for (;;) {
526200b916fSCong Wang unregistering = false;
527200b916fSCong Wang rtnl_lock();
528f0b07bb1SKirill Tkhai /* We held write locked pernet_ops_rwsem, and parallel
529f0b07bb1SKirill Tkhai * setup_net() and cleanup_net() are not possible.
530f0b07bb1SKirill Tkhai */
531200b916fSCong Wang for_each_net(net) {
532ede6c39cSEric Dumazet if (atomic_read(&net->dev_unreg_count) > 0) {
533200b916fSCong Wang unregistering = true;
534200b916fSCong Wang break;
535200b916fSCong Wang }
536200b916fSCong Wang }
537200b916fSCong Wang if (!unregistering)
538200b916fSCong Wang break;
539200b916fSCong Wang __rtnl_unlock();
540ff960a73SPeter Zijlstra
541ff960a73SPeter Zijlstra wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
542200b916fSCong Wang }
543ff960a73SPeter Zijlstra remove_wait_queue(&netdev_unregistering_wq, &wait);
544200b916fSCong Wang }
545200b916fSCong Wang
54638f7b870SPatrick McHardy /**
54738f7b870SPatrick McHardy * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
54838f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to unregister
54938f7b870SPatrick McHardy */
rtnl_link_unregister(struct rtnl_link_ops * ops)55038f7b870SPatrick McHardy void rtnl_link_unregister(struct rtnl_link_ops *ops)
55138f7b870SPatrick McHardy {
5528518e9bbSKirill Tkhai /* Close the race with setup_net() and cleanup_net() */
5534420bf21SKirill Tkhai down_write(&pernet_ops_rwsem);
554200b916fSCong Wang rtnl_lock_unregistering_all();
55538f7b870SPatrick McHardy __rtnl_link_unregister(ops);
55638f7b870SPatrick McHardy rtnl_unlock();
5574420bf21SKirill Tkhai up_write(&pernet_ops_rwsem);
55838f7b870SPatrick McHardy }
55938f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_unregister);
56038f7b870SPatrick McHardy
rtnl_link_get_slave_info_data_size(const struct net_device * dev)561ba7d49b1SJiri Pirko static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
562ba7d49b1SJiri Pirko {
563ba7d49b1SJiri Pirko struct net_device *master_dev;
564ba7d49b1SJiri Pirko const struct rtnl_link_ops *ops;
5658515ae38SFlorian Westphal size_t size = 0;
566ba7d49b1SJiri Pirko
5678515ae38SFlorian Westphal rcu_read_lock();
5688515ae38SFlorian Westphal
5698515ae38SFlorian Westphal master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
570ba7d49b1SJiri Pirko if (!master_dev)
5718515ae38SFlorian Westphal goto out;
5728515ae38SFlorian Westphal
573ba7d49b1SJiri Pirko ops = master_dev->rtnl_link_ops;
5746049f253SFernando Luis Vazquez Cao if (!ops || !ops->get_slave_size)
5758515ae38SFlorian Westphal goto out;
576ba7d49b1SJiri Pirko /* IFLA_INFO_SLAVE_DATA + nested data */
5778515ae38SFlorian Westphal size = nla_total_size(sizeof(struct nlattr)) +
578ba7d49b1SJiri Pirko ops->get_slave_size(master_dev, dev);
5798515ae38SFlorian Westphal
5808515ae38SFlorian Westphal out:
5818515ae38SFlorian Westphal rcu_read_unlock();
5828515ae38SFlorian Westphal return size;
583ba7d49b1SJiri Pirko }
584ba7d49b1SJiri Pirko
rtnl_link_get_size(const struct net_device * dev)58538f7b870SPatrick McHardy static size_t rtnl_link_get_size(const struct net_device *dev)
58638f7b870SPatrick McHardy {
58738f7b870SPatrick McHardy const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
58838f7b870SPatrick McHardy size_t size;
58938f7b870SPatrick McHardy
59038f7b870SPatrick McHardy if (!ops)
59138f7b870SPatrick McHardy return 0;
59238f7b870SPatrick McHardy
593369cf77aSThomas Graf size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
594369cf77aSThomas Graf nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */
59538f7b870SPatrick McHardy
59638f7b870SPatrick McHardy if (ops->get_size)
59738f7b870SPatrick McHardy /* IFLA_INFO_DATA + nested data */
598369cf77aSThomas Graf size += nla_total_size(sizeof(struct nlattr)) +
59938f7b870SPatrick McHardy ops->get_size(dev);
60038f7b870SPatrick McHardy
60138f7b870SPatrick McHardy if (ops->get_xstats_size)
602369cf77aSThomas Graf /* IFLA_INFO_XSTATS */
603369cf77aSThomas Graf size += nla_total_size(ops->get_xstats_size(dev));
60438f7b870SPatrick McHardy
605ba7d49b1SJiri Pirko size += rtnl_link_get_slave_info_data_size(dev);
606ba7d49b1SJiri Pirko
60738f7b870SPatrick McHardy return size;
60838f7b870SPatrick McHardy }
60938f7b870SPatrick McHardy
610f8ff182cSThomas Graf static LIST_HEAD(rtnl_af_ops);
611f8ff182cSThomas Graf
rtnl_af_lookup(const int family)612f8ff182cSThomas Graf static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
613f8ff182cSThomas Graf {
614f8ff182cSThomas Graf const struct rtnl_af_ops *ops;
615f8ff182cSThomas Graf
616a100243dSCong Wang ASSERT_RTNL();
617a100243dSCong Wang
618a100243dSCong Wang list_for_each_entry(ops, &rtnl_af_ops, list) {
619f8ff182cSThomas Graf if (ops->family == family)
620f8ff182cSThomas Graf return ops;
621f8ff182cSThomas Graf }
622f8ff182cSThomas Graf
623f8ff182cSThomas Graf return NULL;
624f8ff182cSThomas Graf }
625f8ff182cSThomas Graf
626f8ff182cSThomas Graf /**
627f8ff182cSThomas Graf * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
628f8ff182cSThomas Graf * @ops: struct rtnl_af_ops * to register
629f8ff182cSThomas Graf *
630f8ff182cSThomas Graf * Returns 0 on success or a negative error code.
631f8ff182cSThomas Graf */
rtnl_af_register(struct rtnl_af_ops * ops)6323678a9d8Sstephen hemminger void rtnl_af_register(struct rtnl_af_ops *ops)
633f8ff182cSThomas Graf {
634f8ff182cSThomas Graf rtnl_lock();
6355fa85a09SFlorian Westphal list_add_tail_rcu(&ops->list, &rtnl_af_ops);
636f8ff182cSThomas Graf rtnl_unlock();
637f8ff182cSThomas Graf }
638f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_register);
639f8ff182cSThomas Graf
640f8ff182cSThomas Graf /**
641f8ff182cSThomas Graf * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
642f8ff182cSThomas Graf * @ops: struct rtnl_af_ops * to unregister
643f8ff182cSThomas Graf */
rtnl_af_unregister(struct rtnl_af_ops * ops)644f8ff182cSThomas Graf void rtnl_af_unregister(struct rtnl_af_ops *ops)
645f8ff182cSThomas Graf {
646f8ff182cSThomas Graf rtnl_lock();
6475fa85a09SFlorian Westphal list_del_rcu(&ops->list);
648f8ff182cSThomas Graf rtnl_unlock();
6495fa85a09SFlorian Westphal
6505fa85a09SFlorian Westphal synchronize_rcu();
651f8ff182cSThomas Graf }
652f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_unregister);
653f8ff182cSThomas Graf
rtnl_link_get_af_size(const struct net_device * dev,u32 ext_filter_mask)654b1974ed0SArad, Ronen static size_t rtnl_link_get_af_size(const struct net_device *dev,
655b1974ed0SArad, Ronen u32 ext_filter_mask)
656f8ff182cSThomas Graf {
657f8ff182cSThomas Graf struct rtnl_af_ops *af_ops;
658f8ff182cSThomas Graf size_t size;
659f8ff182cSThomas Graf
660f8ff182cSThomas Graf /* IFLA_AF_SPEC */
661f8ff182cSThomas Graf size = nla_total_size(sizeof(struct nlattr));
662f8ff182cSThomas Graf
6635fa85a09SFlorian Westphal rcu_read_lock();
6645fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
665f8ff182cSThomas Graf if (af_ops->get_link_af_size) {
666f8ff182cSThomas Graf /* AF_* + nested data */
667f8ff182cSThomas Graf size += nla_total_size(sizeof(struct nlattr)) +
668b1974ed0SArad, Ronen af_ops->get_link_af_size(dev, ext_filter_mask);
669f8ff182cSThomas Graf }
670f8ff182cSThomas Graf }
6715fa85a09SFlorian Westphal rcu_read_unlock();
672f8ff182cSThomas Graf
673f8ff182cSThomas Graf return size;
674f8ff182cSThomas Graf }
675f8ff182cSThomas Graf
rtnl_have_link_slave_info(const struct net_device * dev)676ba7d49b1SJiri Pirko static bool rtnl_have_link_slave_info(const struct net_device *dev)
677ba7d49b1SJiri Pirko {
678ba7d49b1SJiri Pirko struct net_device *master_dev;
6794c82a95eSFlorian Westphal bool ret = false;
680ba7d49b1SJiri Pirko
6814c82a95eSFlorian Westphal rcu_read_lock();
6824c82a95eSFlorian Westphal
6834c82a95eSFlorian Westphal master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
684813f020cSJiri Pirko if (master_dev && master_dev->rtnl_link_ops)
6854c82a95eSFlorian Westphal ret = true;
6864c82a95eSFlorian Westphal rcu_read_unlock();
6874c82a95eSFlorian Westphal return ret;
688ba7d49b1SJiri Pirko }
689ba7d49b1SJiri Pirko
rtnl_link_slave_info_fill(struct sk_buff * skb,const struct net_device * dev)690ba7d49b1SJiri Pirko static int rtnl_link_slave_info_fill(struct sk_buff *skb,
691ba7d49b1SJiri Pirko const struct net_device *dev)
692ba7d49b1SJiri Pirko {
693ba7d49b1SJiri Pirko struct net_device *master_dev;
694ba7d49b1SJiri Pirko const struct rtnl_link_ops *ops;
695ba7d49b1SJiri Pirko struct nlattr *slave_data;
696ba7d49b1SJiri Pirko int err;
697ba7d49b1SJiri Pirko
698ba7d49b1SJiri Pirko master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
699ba7d49b1SJiri Pirko if (!master_dev)
700ba7d49b1SJiri Pirko return 0;
701ba7d49b1SJiri Pirko ops = master_dev->rtnl_link_ops;
702ba7d49b1SJiri Pirko if (!ops)
703ba7d49b1SJiri Pirko return 0;
704ba7d49b1SJiri Pirko if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
705ba7d49b1SJiri Pirko return -EMSGSIZE;
706ba7d49b1SJiri Pirko if (ops->fill_slave_info) {
707ae0be8deSMichal Kubecek slave_data = nla_nest_start_noflag(skb, IFLA_INFO_SLAVE_DATA);
708ba7d49b1SJiri Pirko if (!slave_data)
709ba7d49b1SJiri Pirko return -EMSGSIZE;
710ba7d49b1SJiri Pirko err = ops->fill_slave_info(skb, master_dev, dev);
711ba7d49b1SJiri Pirko if (err < 0)
712ba7d49b1SJiri Pirko goto err_cancel_slave_data;
713ba7d49b1SJiri Pirko nla_nest_end(skb, slave_data);
714ba7d49b1SJiri Pirko }
715ba7d49b1SJiri Pirko return 0;
716ba7d49b1SJiri Pirko
717ba7d49b1SJiri Pirko err_cancel_slave_data:
718ba7d49b1SJiri Pirko nla_nest_cancel(skb, slave_data);
719ba7d49b1SJiri Pirko return err;
720ba7d49b1SJiri Pirko }
721ba7d49b1SJiri Pirko
rtnl_link_info_fill(struct sk_buff * skb,const struct net_device * dev)722ba7d49b1SJiri Pirko static int rtnl_link_info_fill(struct sk_buff *skb,
723ba7d49b1SJiri Pirko const struct net_device *dev)
72438f7b870SPatrick McHardy {
72538f7b870SPatrick McHardy const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
726ba7d49b1SJiri Pirko struct nlattr *data;
727ba7d49b1SJiri Pirko int err;
728ba7d49b1SJiri Pirko
729ba7d49b1SJiri Pirko if (!ops)
730ba7d49b1SJiri Pirko return 0;
731ba7d49b1SJiri Pirko if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
732ba7d49b1SJiri Pirko return -EMSGSIZE;
733ba7d49b1SJiri Pirko if (ops->fill_xstats) {
734ba7d49b1SJiri Pirko err = ops->fill_xstats(skb, dev);
735ba7d49b1SJiri Pirko if (err < 0)
736ba7d49b1SJiri Pirko return err;
737ba7d49b1SJiri Pirko }
738ba7d49b1SJiri Pirko if (ops->fill_info) {
739ae0be8deSMichal Kubecek data = nla_nest_start_noflag(skb, IFLA_INFO_DATA);
740ba7d49b1SJiri Pirko if (data == NULL)
741ba7d49b1SJiri Pirko return -EMSGSIZE;
742ba7d49b1SJiri Pirko err = ops->fill_info(skb, dev);
743ba7d49b1SJiri Pirko if (err < 0)
744ba7d49b1SJiri Pirko goto err_cancel_data;
745ba7d49b1SJiri Pirko nla_nest_end(skb, data);
746ba7d49b1SJiri Pirko }
747ba7d49b1SJiri Pirko return 0;
748ba7d49b1SJiri Pirko
749ba7d49b1SJiri Pirko err_cancel_data:
750ba7d49b1SJiri Pirko nla_nest_cancel(skb, data);
751ba7d49b1SJiri Pirko return err;
752ba7d49b1SJiri Pirko }
753ba7d49b1SJiri Pirko
rtnl_link_fill(struct sk_buff * skb,const struct net_device * dev)754ba7d49b1SJiri Pirko static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
755ba7d49b1SJiri Pirko {
756ba7d49b1SJiri Pirko struct nlattr *linkinfo;
75738f7b870SPatrick McHardy int err = -EMSGSIZE;
75838f7b870SPatrick McHardy
759ae0be8deSMichal Kubecek linkinfo = nla_nest_start_noflag(skb, IFLA_LINKINFO);
76038f7b870SPatrick McHardy if (linkinfo == NULL)
76138f7b870SPatrick McHardy goto out;
76238f7b870SPatrick McHardy
763ba7d49b1SJiri Pirko err = rtnl_link_info_fill(skb, dev);
76438f7b870SPatrick McHardy if (err < 0)
76538f7b870SPatrick McHardy goto err_cancel_link;
766ba7d49b1SJiri Pirko
767ba7d49b1SJiri Pirko err = rtnl_link_slave_info_fill(skb, dev);
76838f7b870SPatrick McHardy if (err < 0)
769ba7d49b1SJiri Pirko goto err_cancel_link;
77038f7b870SPatrick McHardy
77138f7b870SPatrick McHardy nla_nest_end(skb, linkinfo);
77238f7b870SPatrick McHardy return 0;
77338f7b870SPatrick McHardy
77438f7b870SPatrick McHardy err_cancel_link:
77538f7b870SPatrick McHardy nla_nest_cancel(skb, linkinfo);
77638f7b870SPatrick McHardy out:
77738f7b870SPatrick McHardy return err;
77838f7b870SPatrick McHardy }
77938f7b870SPatrick McHardy
rtnetlink_send(struct sk_buff * skb,struct net * net,u32 pid,unsigned int group,int echo)78095c96174SEric Dumazet int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
7811da177e4SLinus Torvalds {
78297c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl;
7831da177e4SLinus Torvalds
784cfdf0d9aSYajun Deng return nlmsg_notify(rtnl, skb, pid, group, echo, GFP_KERNEL);
7851da177e4SLinus Torvalds }
7861da177e4SLinus Torvalds
rtnl_unicast(struct sk_buff * skb,struct net * net,u32 pid)78797c53cacSDenis V. Lunev int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
7882942e900SThomas Graf {
78997c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl;
79097c53cacSDenis V. Lunev
7912942e900SThomas Graf return nlmsg_unicast(rtnl, skb, pid);
7922942e900SThomas Graf }
793e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unicast);
7942942e900SThomas Graf
rtnl_notify(struct sk_buff * skb,struct net * net,u32 pid,u32 group,const struct nlmsghdr * nlh,gfp_t flags)7951ce85fe4SPablo Neira Ayuso void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
7961d997f10SHangbin Liu const struct nlmsghdr *nlh, gfp_t flags)
79797676b6bSThomas Graf {
79897c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl;
79997676b6bSThomas Graf
800f9b282b3SYajun Deng nlmsg_notify(rtnl, skb, pid, group, nlmsg_report(nlh), flags);
80197676b6bSThomas Graf }
802e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_notify);
80397676b6bSThomas Graf
rtnl_set_sk_err(struct net * net,u32 group,int error)80497c53cacSDenis V. Lunev void rtnl_set_sk_err(struct net *net, u32 group, int error)
80597676b6bSThomas Graf {
80697c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl;
80797c53cacSDenis V. Lunev
80897676b6bSThomas Graf netlink_set_err(rtnl, 0, group, error);
80997676b6bSThomas Graf }
810e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_set_sk_err);
81197676b6bSThomas Graf
rtnetlink_put_metrics(struct sk_buff * skb,u32 * metrics)8121da177e4SLinus Torvalds int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
8131da177e4SLinus Torvalds {
8142d7202bfSThomas Graf struct nlattr *mx;
8152d7202bfSThomas Graf int i, valid = 0;
8161da177e4SLinus Torvalds
817c22a133aSDavid Ahern /* nothing is dumped for dst_default_metrics, so just skip the loop */
818c22a133aSDavid Ahern if (metrics == dst_default_metrics.metrics)
819c22a133aSDavid Ahern return 0;
820c22a133aSDavid Ahern
821ae0be8deSMichal Kubecek mx = nla_nest_start_noflag(skb, RTA_METRICS);
8222d7202bfSThomas Graf if (mx == NULL)
8232d7202bfSThomas Graf return -ENOBUFS;
8242d7202bfSThomas Graf
8251da177e4SLinus Torvalds for (i = 0; i < RTAX_MAX; i++) {
8262d7202bfSThomas Graf if (metrics[i]) {
827ea697639SDaniel Borkmann if (i == RTAX_CC_ALGO - 1) {
828ea697639SDaniel Borkmann char tmp[TCP_CA_NAME_MAX], *name;
829ea697639SDaniel Borkmann
830ea697639SDaniel Borkmann name = tcp_ca_get_name_by_key(metrics[i], tmp);
831ea697639SDaniel Borkmann if (!name)
832ea697639SDaniel Borkmann continue;
833ea697639SDaniel Borkmann if (nla_put_string(skb, i + 1, name))
834ea697639SDaniel Borkmann goto nla_put_failure;
835c3a8d947SDaniel Borkmann } else if (i == RTAX_FEATURES - 1) {
836c3a8d947SDaniel Borkmann u32 user_features = metrics[i] & RTAX_FEATURE_MASK;
837c3a8d947SDaniel Borkmann
838f8edcd12SPhil Sutter if (!user_features)
839f8edcd12SPhil Sutter continue;
840c3a8d947SDaniel Borkmann BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK);
841c3a8d947SDaniel Borkmann if (nla_put_u32(skb, i + 1, user_features))
842c3a8d947SDaniel Borkmann goto nla_put_failure;
843ea697639SDaniel Borkmann } else {
844a6574349SDavid S. Miller if (nla_put_u32(skb, i + 1, metrics[i]))
845a6574349SDavid S. Miller goto nla_put_failure;
8461da177e4SLinus Torvalds }
847ea697639SDaniel Borkmann valid++;
848ea697639SDaniel Borkmann }
8492d7202bfSThomas Graf }
8501da177e4SLinus Torvalds
851a57d27fcSDavid S. Miller if (!valid) {
852a57d27fcSDavid S. Miller nla_nest_cancel(skb, mx);
853a57d27fcSDavid S. Miller return 0;
854a57d27fcSDavid S. Miller }
8552d7202bfSThomas Graf
8562d7202bfSThomas Graf return nla_nest_end(skb, mx);
8572d7202bfSThomas Graf
8582d7202bfSThomas Graf nla_put_failure:
859bc3ed28cSThomas Graf nla_nest_cancel(skb, mx);
860bc3ed28cSThomas Graf return -EMSGSIZE;
8611da177e4SLinus Torvalds }
862e0d087afSEric Dumazet EXPORT_SYMBOL(rtnetlink_put_metrics);
8631da177e4SLinus Torvalds
rtnl_put_cacheinfo(struct sk_buff * skb,struct dst_entry * dst,u32 id,long expires,u32 error)864e3703b3dSThomas Graf int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
86587a50699SDavid S. Miller long expires, u32 error)
866e3703b3dSThomas Graf {
867e3703b3dSThomas Graf struct rta_cacheinfo ci = {
868e3703b3dSThomas Graf .rta_error = error,
869e3703b3dSThomas Graf .rta_id = id,
870e3703b3dSThomas Graf };
871e3703b3dSThomas Graf
8723940746dSDavid Ahern if (dst) {
8733940746dSDavid Ahern ci.rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse);
8743940746dSDavid Ahern ci.rta_used = dst->__use;
875bc9d3a9fSThomas Gleixner ci.rta_clntref = rcuref_read(&dst->__rcuref);
8763940746dSDavid Ahern }
8778253947eSLi Wei if (expires) {
8788253947eSLi Wei unsigned long clock;
879e3703b3dSThomas Graf
8808253947eSLi Wei clock = jiffies_to_clock_t(abs(expires));
8818253947eSLi Wei clock = min_t(unsigned long, clock, INT_MAX);
8828253947eSLi Wei ci.rta_expires = (expires > 0) ? clock : -clock;
8838253947eSLi Wei }
884e3703b3dSThomas Graf return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
885e3703b3dSThomas Graf }
886e3703b3dSThomas Graf EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
8871da177e4SLinus Torvalds
set_operstate(struct net_device * dev,unsigned char transition)88893b2d4a2SDavid S. Miller static void set_operstate(struct net_device *dev, unsigned char transition)
889b00055aaSStefan Rompf {
890b00055aaSStefan Rompf unsigned char operstate = dev->operstate;
891b00055aaSStefan Rompf
892b00055aaSStefan Rompf switch (transition) {
893b00055aaSStefan Rompf case IF_OPER_UP:
894b00055aaSStefan Rompf if ((operstate == IF_OPER_DORMANT ||
895eec517cdSAndrew Lunn operstate == IF_OPER_TESTING ||
896b00055aaSStefan Rompf operstate == IF_OPER_UNKNOWN) &&
897eec517cdSAndrew Lunn !netif_dormant(dev) && !netif_testing(dev))
898b00055aaSStefan Rompf operstate = IF_OPER_UP;
899b00055aaSStefan Rompf break;
900b00055aaSStefan Rompf
901eec517cdSAndrew Lunn case IF_OPER_TESTING:
902abbc7928SJuhee Kang if (netif_oper_up(dev))
903eec517cdSAndrew Lunn operstate = IF_OPER_TESTING;
904eec517cdSAndrew Lunn break;
905eec517cdSAndrew Lunn
906b00055aaSStefan Rompf case IF_OPER_DORMANT:
907abbc7928SJuhee Kang if (netif_oper_up(dev))
908b00055aaSStefan Rompf operstate = IF_OPER_DORMANT;
909b00055aaSStefan Rompf break;
9103ff50b79SStephen Hemminger }
911b00055aaSStefan Rompf
9121c2b0f08SEric Dumazet if (READ_ONCE(dev->operstate) != operstate) {
913fd888e85SSebastian Andrzej Siewior write_lock(&dev_base_lock);
9141c2b0f08SEric Dumazet WRITE_ONCE(dev->operstate, operstate);
915fd888e85SSebastian Andrzej Siewior write_unlock(&dev_base_lock);
916b00055aaSStefan Rompf netdev_state_change(dev);
91793b2d4a2SDavid S. Miller }
918b00055aaSStefan Rompf }
919b00055aaSStefan Rompf
rtnl_dev_get_flags(const struct net_device * dev)920b1beb681SJiri Benc static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
921b1beb681SJiri Benc {
922b1beb681SJiri Benc return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
923b1beb681SJiri Benc (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
924b1beb681SJiri Benc }
925b1beb681SJiri Benc
rtnl_dev_combine_flags(const struct net_device * dev,const struct ifinfomsg * ifm)9263729d502SPatrick McHardy static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
9273729d502SPatrick McHardy const struct ifinfomsg *ifm)
9283729d502SPatrick McHardy {
9293729d502SPatrick McHardy unsigned int flags = ifm->ifi_flags;
9303729d502SPatrick McHardy
9313729d502SPatrick McHardy /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
9323729d502SPatrick McHardy if (ifm->ifi_change)
9333729d502SPatrick McHardy flags = (flags & ifm->ifi_change) |
934b1beb681SJiri Benc (rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
9353729d502SPatrick McHardy
9363729d502SPatrick McHardy return flags;
9373729d502SPatrick McHardy }
9383729d502SPatrick McHardy
copy_rtnl_link_stats(struct rtnl_link_stats * a,const struct rtnl_link_stats64 * b)939b60c5115SThomas Graf static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
940be1f3c2cSBen Hutchings const struct rtnl_link_stats64 *b)
9411da177e4SLinus Torvalds {
942b60c5115SThomas Graf a->rx_packets = b->rx_packets;
943b60c5115SThomas Graf a->tx_packets = b->tx_packets;
944b60c5115SThomas Graf a->rx_bytes = b->rx_bytes;
945b60c5115SThomas Graf a->tx_bytes = b->tx_bytes;
946b60c5115SThomas Graf a->rx_errors = b->rx_errors;
947b60c5115SThomas Graf a->tx_errors = b->tx_errors;
948b60c5115SThomas Graf a->rx_dropped = b->rx_dropped;
949b60c5115SThomas Graf a->tx_dropped = b->tx_dropped;
950b60c5115SThomas Graf
951b60c5115SThomas Graf a->multicast = b->multicast;
952b60c5115SThomas Graf a->collisions = b->collisions;
953b60c5115SThomas Graf
954b60c5115SThomas Graf a->rx_length_errors = b->rx_length_errors;
955b60c5115SThomas Graf a->rx_over_errors = b->rx_over_errors;
956b60c5115SThomas Graf a->rx_crc_errors = b->rx_crc_errors;
957b60c5115SThomas Graf a->rx_frame_errors = b->rx_frame_errors;
958b60c5115SThomas Graf a->rx_fifo_errors = b->rx_fifo_errors;
959b60c5115SThomas Graf a->rx_missed_errors = b->rx_missed_errors;
960b60c5115SThomas Graf
961b60c5115SThomas Graf a->tx_aborted_errors = b->tx_aborted_errors;
962b60c5115SThomas Graf a->tx_carrier_errors = b->tx_carrier_errors;
963b60c5115SThomas Graf a->tx_fifo_errors = b->tx_fifo_errors;
964b60c5115SThomas Graf a->tx_heartbeat_errors = b->tx_heartbeat_errors;
965b60c5115SThomas Graf a->tx_window_errors = b->tx_window_errors;
966b60c5115SThomas Graf
967b60c5115SThomas Graf a->rx_compressed = b->rx_compressed;
968b60c5115SThomas Graf a->tx_compressed = b->tx_compressed;
9696e7333d3SJarod Wilson
9706e7333d3SJarod Wilson a->rx_nohandler = b->rx_nohandler;
97110708f37SJan Engelhardt }
97210708f37SJan Engelhardt
973c02db8c6SChris Wright /* All VF info */
rtnl_vfinfo_size(const struct net_device * dev,u32 ext_filter_mask)974115c9b81SGreg Rose static inline int rtnl_vfinfo_size(const struct net_device *dev,
975115c9b81SGreg Rose u32 ext_filter_mask)
976ebc08a6fSWilliams, Mitch A {
9779af15c38SPhil Sutter if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) {
978c02db8c6SChris Wright int num_vfs = dev_num_vf(dev->dev.parent);
9797e75f74aSSabrina Dubroca size_t size = nla_total_size(0);
980045de01aSScott Feldman size += num_vfs *
9817e75f74aSSabrina Dubroca (nla_total_size(0) +
9827e75f74aSSabrina Dubroca nla_total_size(sizeof(struct ifla_vf_mac)) +
98375345f88SDenis Kirjanov nla_total_size(sizeof(struct ifla_vf_broadcast)) +
9847e75f74aSSabrina Dubroca nla_total_size(sizeof(struct ifla_vf_vlan)) +
9857e75f74aSSabrina Dubroca nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
98679aab093SMoshe Shemesh nla_total_size(MAX_VLAN_LIST_LEN *
98779aab093SMoshe Shemesh sizeof(struct ifla_vf_vlan_info)) +
988ed616689SSucheta Chakraborty nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
9897e75f74aSSabrina Dubroca nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
990945a3676SJiri Benc nla_total_size(sizeof(struct ifla_vf_rate)) +
99101a3d796SVlad Zolotarov nla_total_size(sizeof(struct ifla_vf_link_state)) +
9923b766cd8SEran Ben Elisha nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
993fa0e21faSEdwin Peer nla_total_size(sizeof(struct ifla_vf_trust)));
994fa0e21faSEdwin Peer if (~ext_filter_mask & RTEXT_FILTER_SKIP_STATS) {
995fa0e21faSEdwin Peer size += num_vfs *
996fa0e21faSEdwin Peer (nla_total_size(0) + /* nest IFLA_VF_STATS */
9973b766cd8SEran Ben Elisha /* IFLA_VF_STATS_RX_PACKETS */
998343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) +
9993b766cd8SEran Ben Elisha /* IFLA_VF_STATS_TX_PACKETS */
1000343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) +
10013b766cd8SEran Ben Elisha /* IFLA_VF_STATS_RX_BYTES */
1002343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) +
10033b766cd8SEran Ben Elisha /* IFLA_VF_STATS_TX_BYTES */
1004343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) +
10053b766cd8SEran Ben Elisha /* IFLA_VF_STATS_BROADCAST */
1006343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) +
10073b766cd8SEran Ben Elisha /* IFLA_VF_STATS_MULTICAST */
1008343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) +
1009c5a9f6f0SEugenia Emantayev /* IFLA_VF_STATS_RX_DROPPED */
1010c5a9f6f0SEugenia Emantayev nla_total_size_64bit(sizeof(__u64)) +
1011c5a9f6f0SEugenia Emantayev /* IFLA_VF_STATS_TX_DROPPED */
1012fa0e21faSEdwin Peer nla_total_size_64bit(sizeof(__u64)));
1013fa0e21faSEdwin Peer }
1014c02db8c6SChris Wright return size;
1015c02db8c6SChris Wright } else
1016ebc08a6fSWilliams, Mitch A return 0;
1017ebc08a6fSWilliams, Mitch A }
1018ebc08a6fSWilliams, Mitch A
rtnl_port_size(const struct net_device * dev,u32 ext_filter_mask)1019c53864fdSDavid Gibson static size_t rtnl_port_size(const struct net_device *dev,
1020c53864fdSDavid Gibson u32 ext_filter_mask)
102157b61080SScott Feldman {
102257b61080SScott Feldman size_t port_size = nla_total_size(4) /* PORT_VF */
102357b61080SScott Feldman + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
102457b61080SScott Feldman + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
102557b61080SScott Feldman + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
102657b61080SScott Feldman + nla_total_size(1) /* PROT_VDP_REQUEST */
102757b61080SScott Feldman + nla_total_size(2); /* PORT_VDP_RESPONSE */
102857b61080SScott Feldman size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
102957b61080SScott Feldman size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
103057b61080SScott Feldman + port_size;
103157b61080SScott Feldman size_t port_self_size = nla_total_size(sizeof(struct nlattr))
103257b61080SScott Feldman + port_size;
103357b61080SScott Feldman
1034c53864fdSDavid Gibson if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
1035c53864fdSDavid Gibson !(ext_filter_mask & RTEXT_FILTER_VF))
103657b61080SScott Feldman return 0;
103757b61080SScott Feldman if (dev_num_vf(dev->dev.parent))
103857b61080SScott Feldman return port_self_size + vf_ports_size +
103957b61080SScott Feldman vf_port_size * dev_num_vf(dev->dev.parent);
104057b61080SScott Feldman else
104157b61080SScott Feldman return port_self_size;
104257b61080SScott Feldman }
104357b61080SScott Feldman
rtnl_xdp_size(void)1044b5cdae32SDavid S. Miller static size_t rtnl_xdp_size(void)
1045d1fdd913SBrenden Blanco {
1046b3cfaa31SSabrina Dubroca size_t xdp_size = nla_total_size(0) + /* nest IFLA_XDP */
104758038695SMartin KaFai Lau nla_total_size(1) + /* XDP_ATTACHED */
1048a25717d2SJakub Kicinski nla_total_size(4) + /* XDP_PROG_ID (or 1st mode) */
10494f91da26SJakub Kicinski nla_total_size(4); /* XDP_<mode>_PROG_ID */
1050d1fdd913SBrenden Blanco
1051d1fdd913SBrenden Blanco return xdp_size;
1052d1fdd913SBrenden Blanco }
1053d1fdd913SBrenden Blanco
rtnl_prop_list_size(const struct net_device * dev)105488f4fb0cSJiri Pirko static size_t rtnl_prop_list_size(const struct net_device *dev)
105588f4fb0cSJiri Pirko {
105688f4fb0cSJiri Pirko struct netdev_name_node *name_node;
105788f4fb0cSJiri Pirko size_t size;
105888f4fb0cSJiri Pirko
105988f4fb0cSJiri Pirko if (list_empty(&dev->name_node->list))
106088f4fb0cSJiri Pirko return 0;
106188f4fb0cSJiri Pirko size = nla_total_size(0);
106288f4fb0cSJiri Pirko list_for_each_entry(name_node, &dev->name_node->list, list)
106388f4fb0cSJiri Pirko size += nla_total_size(ALTIFNAMSIZ);
106488f4fb0cSJiri Pirko return size;
106588f4fb0cSJiri Pirko }
106688f4fb0cSJiri Pirko
rtnl_proto_down_size(const struct net_device * dev)1067829eb208SRoopa Prabhu static size_t rtnl_proto_down_size(const struct net_device *dev)
1068829eb208SRoopa Prabhu {
1069829eb208SRoopa Prabhu size_t size = nla_total_size(1);
1070829eb208SRoopa Prabhu
1071829eb208SRoopa Prabhu if (dev->proto_down_reason)
1072829eb208SRoopa Prabhu size += nla_total_size(0) + nla_total_size(4);
1073829eb208SRoopa Prabhu
1074829eb208SRoopa Prabhu return size;
1075829eb208SRoopa Prabhu }
1076829eb208SRoopa Prabhu
rtnl_devlink_port_size(const struct net_device * dev)1077dca56c30SJiri Pirko static size_t rtnl_devlink_port_size(const struct net_device *dev)
1078dca56c30SJiri Pirko {
1079dca56c30SJiri Pirko size_t size = nla_total_size(0); /* nest IFLA_DEVLINK_PORT */
1080dca56c30SJiri Pirko
1081dca56c30SJiri Pirko if (dev->devlink_port)
1082dca56c30SJiri Pirko size += devlink_nl_port_handle_size(dev->devlink_port);
1083dca56c30SJiri Pirko
1084dca56c30SJiri Pirko return size;
1085dca56c30SJiri Pirko }
1086dca56c30SJiri Pirko
if_nlmsg_size(const struct net_device * dev,u32 ext_filter_mask)1087115c9b81SGreg Rose static noinline size_t if_nlmsg_size(const struct net_device *dev,
1088115c9b81SGreg Rose u32 ext_filter_mask)
1089339bf98fSThomas Graf {
1090339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifinfomsg))
1091339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
10920b815a1aSStephen Hemminger + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
1093339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
1094270cb4d0SNicolas Dichtel + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
1095339bf98fSThomas Graf + nla_total_size(sizeof(struct rtnl_link_stats))
109635c58459SDavid S. Miller + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
1097339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
1098339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
1099339bf98fSThomas Graf + nla_total_size(4) /* IFLA_TXQLEN */
1100339bf98fSThomas Graf + nla_total_size(4) /* IFLA_WEIGHT */
1101339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MTU */
1102339bf98fSThomas Graf + nla_total_size(4) /* IFLA_LINK */
1103339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MASTER */
11049a57247fSJiri Pirko + nla_total_size(1) /* IFLA_CARRIER */
1105edbc0bb3SBen Greear + nla_total_size(4) /* IFLA_PROMISCUITY */
11067e6e1b57SNicolas Dichtel + nla_total_size(4) /* IFLA_ALLMULTI */
110776ff5cc9SJiri Pirko + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
110876ff5cc9SJiri Pirko + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
11096919756cSTobias Klauser + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
11106919756cSTobias Klauser + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
1111eac1b93cSCoco Li + nla_total_size(4) /* IFLA_GRO_MAX_SIZE */
11129eefedd5SXin Long + nla_total_size(4) /* IFLA_GSO_IPV4_MAX_SIZE */
11139eefedd5SXin Long + nla_total_size(4) /* IFLA_GRO_IPV4_MAX_SIZE */
111489527be8SEric Dumazet + nla_total_size(4) /* IFLA_TSO_MAX_SIZE */
111589527be8SEric Dumazet + nla_total_size(4) /* IFLA_TSO_MAX_SEGS */
1116339bf98fSThomas Graf + nla_total_size(1) /* IFLA_OPERSTATE */
111738f7b870SPatrick McHardy + nla_total_size(1) /* IFLA_LINKMODE */
11182d3b479dSdavid decotigny + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
1119d37512a2SNicolas Dichtel + nla_total_size(4) /* IFLA_LINK_NETNSID */
1120db833d40SSerhey Popovych + nla_total_size(4) /* IFLA_GROUP */
1121115c9b81SGreg Rose + nla_total_size(ext_filter_mask
1122115c9b81SGreg Rose & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
1123115c9b81SGreg Rose + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
1124c53864fdSDavid Gibson + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
1125f8ff182cSThomas Graf + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
1126b1974ed0SArad, Ronen + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
112782f28412SJiri Pirko + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
112888d6378bSAnuradha Karuppiah + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
1129c57c7a95SNicolas Dichtel + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
1130b5cdae32SDavid S. Miller + rtnl_xdp_size() /* IFLA_XDP */
11313d3ea5afSVlad Yasevich + nla_total_size(4) /* IFLA_EVENT */
11326621dd29SNicolas Dichtel + nla_total_size(4) /* IFLA_NEW_NETNSID */
113338e01b30SNicolas Dichtel + nla_total_size(4) /* IFLA_NEW_IFINDEX */
1134829eb208SRoopa Prabhu + rtnl_proto_down_size(dev) /* proto down */
11357e4a8d5aSChristian Brauner + nla_total_size(4) /* IFLA_TARGET_NETNSID */
1136b2d3bcfaSDavid Decotigny + nla_total_size(4) /* IFLA_CARRIER_UP_COUNT */
1137b2d3bcfaSDavid Decotigny + nla_total_size(4) /* IFLA_CARRIER_DOWN_COUNT */
11383e7a50ceSStephen Hemminger + nla_total_size(4) /* IFLA_MIN_MTU */
11393e7a50ceSStephen Hemminger + nla_total_size(4) /* IFLA_MAX_MTU */
114088f4fb0cSJiri Pirko + rtnl_prop_list_size(dev)
1141f74877a5SMichal Kubecek + nla_total_size(MAX_ADDR_LEN) /* IFLA_PERM_ADDRESS */
1142dca56c30SJiri Pirko + rtnl_devlink_port_size(dev)
114379e1ad14SJiri Benc + 0;
1144339bf98fSThomas Graf }
1145339bf98fSThomas Graf
rtnl_vf_ports_fill(struct sk_buff * skb,struct net_device * dev)114657b61080SScott Feldman static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
114757b61080SScott Feldman {
114857b61080SScott Feldman struct nlattr *vf_ports;
114957b61080SScott Feldman struct nlattr *vf_port;
115057b61080SScott Feldman int vf;
115157b61080SScott Feldman int err;
115257b61080SScott Feldman
1153ae0be8deSMichal Kubecek vf_ports = nla_nest_start_noflag(skb, IFLA_VF_PORTS);
115457b61080SScott Feldman if (!vf_ports)
115557b61080SScott Feldman return -EMSGSIZE;
115657b61080SScott Feldman
115757b61080SScott Feldman for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
1158ae0be8deSMichal Kubecek vf_port = nla_nest_start_noflag(skb, IFLA_VF_PORT);
11598ca94183SScott Feldman if (!vf_port)
11608ca94183SScott Feldman goto nla_put_failure;
1161a6574349SDavid S. Miller if (nla_put_u32(skb, IFLA_PORT_VF, vf))
1162a6574349SDavid S. Miller goto nla_put_failure;
116357b61080SScott Feldman err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
11648ca94183SScott Feldman if (err == -EMSGSIZE)
11658ca94183SScott Feldman goto nla_put_failure;
116657b61080SScott Feldman if (err) {
116757b61080SScott Feldman nla_nest_cancel(skb, vf_port);
116857b61080SScott Feldman continue;
116957b61080SScott Feldman }
117057b61080SScott Feldman nla_nest_end(skb, vf_port);
117157b61080SScott Feldman }
117257b61080SScott Feldman
117357b61080SScott Feldman nla_nest_end(skb, vf_ports);
117457b61080SScott Feldman
117557b61080SScott Feldman return 0;
11768ca94183SScott Feldman
11778ca94183SScott Feldman nla_put_failure:
11788ca94183SScott Feldman nla_nest_cancel(skb, vf_ports);
11798ca94183SScott Feldman return -EMSGSIZE;
118057b61080SScott Feldman }
118157b61080SScott Feldman
rtnl_port_self_fill(struct sk_buff * skb,struct net_device * dev)118257b61080SScott Feldman static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
118357b61080SScott Feldman {
118457b61080SScott Feldman struct nlattr *port_self;
118557b61080SScott Feldman int err;
118657b61080SScott Feldman
1187ae0be8deSMichal Kubecek port_self = nla_nest_start_noflag(skb, IFLA_PORT_SELF);
118857b61080SScott Feldman if (!port_self)
118957b61080SScott Feldman return -EMSGSIZE;
119057b61080SScott Feldman
119157b61080SScott Feldman err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
119257b61080SScott Feldman if (err) {
119357b61080SScott Feldman nla_nest_cancel(skb, port_self);
11948ca94183SScott Feldman return (err == -EMSGSIZE) ? err : 0;
119557b61080SScott Feldman }
119657b61080SScott Feldman
119757b61080SScott Feldman nla_nest_end(skb, port_self);
119857b61080SScott Feldman
119957b61080SScott Feldman return 0;
120057b61080SScott Feldman }
120157b61080SScott Feldman
rtnl_port_fill(struct sk_buff * skb,struct net_device * dev,u32 ext_filter_mask)1202c53864fdSDavid Gibson static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
1203c53864fdSDavid Gibson u32 ext_filter_mask)
120457b61080SScott Feldman {
120557b61080SScott Feldman int err;
120657b61080SScott Feldman
1207c53864fdSDavid Gibson if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
1208c53864fdSDavid Gibson !(ext_filter_mask & RTEXT_FILTER_VF))
120957b61080SScott Feldman return 0;
121057b61080SScott Feldman
121157b61080SScott Feldman err = rtnl_port_self_fill(skb, dev);
121257b61080SScott Feldman if (err)
121357b61080SScott Feldman return err;
121457b61080SScott Feldman
121557b61080SScott Feldman if (dev_num_vf(dev->dev.parent)) {
121657b61080SScott Feldman err = rtnl_vf_ports_fill(skb, dev);
121757b61080SScott Feldman if (err)
121857b61080SScott Feldman return err;
121957b61080SScott Feldman }
122057b61080SScott Feldman
122157b61080SScott Feldman return 0;
122257b61080SScott Feldman }
122357b61080SScott Feldman
rtnl_phys_port_id_fill(struct sk_buff * skb,struct net_device * dev)122466cae9edSJiri Pirko static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
122566cae9edSJiri Pirko {
122666cae9edSJiri Pirko int err;
122702637fceSJiri Pirko struct netdev_phys_item_id ppid;
122866cae9edSJiri Pirko
122966cae9edSJiri Pirko err = dev_get_phys_port_id(dev, &ppid);
123066cae9edSJiri Pirko if (err) {
123166cae9edSJiri Pirko if (err == -EOPNOTSUPP)
123266cae9edSJiri Pirko return 0;
123366cae9edSJiri Pirko return err;
123466cae9edSJiri Pirko }
123566cae9edSJiri Pirko
123666cae9edSJiri Pirko if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
123766cae9edSJiri Pirko return -EMSGSIZE;
123866cae9edSJiri Pirko
123966cae9edSJiri Pirko return 0;
124066cae9edSJiri Pirko }
124166cae9edSJiri Pirko
rtnl_phys_port_name_fill(struct sk_buff * skb,struct net_device * dev)1242db24a904SDavid Ahern static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1243db24a904SDavid Ahern {
1244db24a904SDavid Ahern char name[IFNAMSIZ];
1245db24a904SDavid Ahern int err;
1246db24a904SDavid Ahern
1247db24a904SDavid Ahern err = dev_get_phys_port_name(dev, name, sizeof(name));
1248db24a904SDavid Ahern if (err) {
1249db24a904SDavid Ahern if (err == -EOPNOTSUPP)
1250db24a904SDavid Ahern return 0;
1251db24a904SDavid Ahern return err;
1252db24a904SDavid Ahern }
1253db24a904SDavid Ahern
125477ef033bSMichal Schmidt if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
1255db24a904SDavid Ahern return -EMSGSIZE;
1256db24a904SDavid Ahern
1257db24a904SDavid Ahern return 0;
1258db24a904SDavid Ahern }
1259db24a904SDavid Ahern
rtnl_phys_switch_id_fill(struct sk_buff * skb,struct net_device * dev)126082f28412SJiri Pirko static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
126182f28412SJiri Pirko {
1262bccb3025SFlorian Fainelli struct netdev_phys_item_id ppid = { };
126382f28412SJiri Pirko int err;
126482f28412SJiri Pirko
1265bccb3025SFlorian Fainelli err = dev_get_port_parent_id(dev, &ppid, false);
126682f28412SJiri Pirko if (err) {
126782f28412SJiri Pirko if (err == -EOPNOTSUPP)
126882f28412SJiri Pirko return 0;
126982f28412SJiri Pirko return err;
127082f28412SJiri Pirko }
127182f28412SJiri Pirko
1272bccb3025SFlorian Fainelli if (nla_put(skb, IFLA_PHYS_SWITCH_ID, ppid.id_len, ppid.id))
127382f28412SJiri Pirko return -EMSGSIZE;
127482f28412SJiri Pirko
127582f28412SJiri Pirko return 0;
127682f28412SJiri Pirko }
127782f28412SJiri Pirko
rtnl_fill_stats(struct sk_buff * skb,struct net_device * dev)1278b22b941bSHannes Frederic Sowa static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1279b22b941bSHannes Frederic Sowa struct net_device *dev)
1280b22b941bSHannes Frederic Sowa {
1281550bce59SRoopa Prabhu struct rtnl_link_stats64 *sp;
1282b22b941bSHannes Frederic Sowa struct nlattr *attr;
1283b22b941bSHannes Frederic Sowa
128458414d32SNicolas Dichtel attr = nla_reserve_64bit(skb, IFLA_STATS64,
128558414d32SNicolas Dichtel sizeof(struct rtnl_link_stats64), IFLA_PAD);
1286b22b941bSHannes Frederic Sowa if (!attr)
1287b22b941bSHannes Frederic Sowa return -EMSGSIZE;
1288b22b941bSHannes Frederic Sowa
1289550bce59SRoopa Prabhu sp = nla_data(attr);
1290550bce59SRoopa Prabhu dev_get_stats(dev, sp);
1291550bce59SRoopa Prabhu
1292550bce59SRoopa Prabhu attr = nla_reserve(skb, IFLA_STATS,
1293550bce59SRoopa Prabhu sizeof(struct rtnl_link_stats));
1294550bce59SRoopa Prabhu if (!attr)
1295550bce59SRoopa Prabhu return -EMSGSIZE;
1296550bce59SRoopa Prabhu
1297550bce59SRoopa Prabhu copy_rtnl_link_stats(nla_data(attr), sp);
1298b22b941bSHannes Frederic Sowa
1299b22b941bSHannes Frederic Sowa return 0;
1300b22b941bSHannes Frederic Sowa }
1301b22b941bSHannes Frederic Sowa
rtnl_fill_vfinfo(struct sk_buff * skb,struct net_device * dev,int vfs_num,u32 ext_filter_mask)1302b22b941bSHannes Frederic Sowa static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1303b22b941bSHannes Frederic Sowa struct net_device *dev,
1304b22b941bSHannes Frederic Sowa int vfs_num,
1305fa0e21faSEdwin Peer u32 ext_filter_mask)
1306b22b941bSHannes Frederic Sowa {
1307b22b941bSHannes Frederic Sowa struct ifla_vf_rss_query_en vf_rss_query_en;
130879aab093SMoshe Shemesh struct nlattr *vf, *vfstats, *vfvlanlist;
1309b22b941bSHannes Frederic Sowa struct ifla_vf_link_state vf_linkstate;
131079aab093SMoshe Shemesh struct ifla_vf_vlan_info vf_vlan_info;
1311b22b941bSHannes Frederic Sowa struct ifla_vf_spoofchk vf_spoofchk;
1312b22b941bSHannes Frederic Sowa struct ifla_vf_tx_rate vf_tx_rate;
1313b22b941bSHannes Frederic Sowa struct ifla_vf_stats vf_stats;
1314b22b941bSHannes Frederic Sowa struct ifla_vf_trust vf_trust;
1315b22b941bSHannes Frederic Sowa struct ifla_vf_vlan vf_vlan;
1316b22b941bSHannes Frederic Sowa struct ifla_vf_rate vf_rate;
1317b22b941bSHannes Frederic Sowa struct ifla_vf_mac vf_mac;
131875345f88SDenis Kirjanov struct ifla_vf_broadcast vf_broadcast;
1319b22b941bSHannes Frederic Sowa struct ifla_vf_info ivi;
132030aad417SDanit Goldberg struct ifla_vf_guid node_guid;
132130aad417SDanit Goldberg struct ifla_vf_guid port_guid;
1322b22b941bSHannes Frederic Sowa
13230eed9cf5SMintz, Yuval memset(&ivi, 0, sizeof(ivi));
13240eed9cf5SMintz, Yuval
1325b22b941bSHannes Frederic Sowa /* Not all SR-IOV capable drivers support the
1326b22b941bSHannes Frederic Sowa * spoofcheck and "RSS query enable" query. Preset to
1327b22b941bSHannes Frederic Sowa * -1 so the user space tool can detect that the driver
1328b22b941bSHannes Frederic Sowa * didn't report anything.
1329b22b941bSHannes Frederic Sowa */
1330b22b941bSHannes Frederic Sowa ivi.spoofchk = -1;
1331b22b941bSHannes Frederic Sowa ivi.rss_query_en = -1;
1332b22b941bSHannes Frederic Sowa ivi.trusted = -1;
1333b22b941bSHannes Frederic Sowa /* The default value for VF link state is "auto"
1334b22b941bSHannes Frederic Sowa * IFLA_VF_LINK_STATE_AUTO which equals zero
1335b22b941bSHannes Frederic Sowa */
1336b22b941bSHannes Frederic Sowa ivi.linkstate = 0;
133779aab093SMoshe Shemesh /* VLAN Protocol by default is 802.1Q */
133879aab093SMoshe Shemesh ivi.vlan_proto = htons(ETH_P_8021Q);
1339b22b941bSHannes Frederic Sowa if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1340b22b941bSHannes Frederic Sowa return 0;
1341b22b941bSHannes Frederic Sowa
1342775f4f05SDan Carpenter memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
13439fbf082fSLeon Romanovsky memset(&node_guid, 0, sizeof(node_guid));
13449fbf082fSLeon Romanovsky memset(&port_guid, 0, sizeof(port_guid));
1345775f4f05SDan Carpenter
1346b22b941bSHannes Frederic Sowa vf_mac.vf =
1347b22b941bSHannes Frederic Sowa vf_vlan.vf =
134879aab093SMoshe Shemesh vf_vlan_info.vf =
1349b22b941bSHannes Frederic Sowa vf_rate.vf =
1350b22b941bSHannes Frederic Sowa vf_tx_rate.vf =
1351b22b941bSHannes Frederic Sowa vf_spoofchk.vf =
1352b22b941bSHannes Frederic Sowa vf_linkstate.vf =
1353b22b941bSHannes Frederic Sowa vf_rss_query_en.vf =
13549aed6ae0SDanit Goldberg vf_trust.vf =
13559aed6ae0SDanit Goldberg node_guid.vf =
13569aed6ae0SDanit Goldberg port_guid.vf = ivi.vf;
1357b22b941bSHannes Frederic Sowa
1358b22b941bSHannes Frederic Sowa memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
135975345f88SDenis Kirjanov memcpy(vf_broadcast.broadcast, dev->broadcast, dev->addr_len);
1360b22b941bSHannes Frederic Sowa vf_vlan.vlan = ivi.vlan;
1361b22b941bSHannes Frederic Sowa vf_vlan.qos = ivi.qos;
136279aab093SMoshe Shemesh vf_vlan_info.vlan = ivi.vlan;
136379aab093SMoshe Shemesh vf_vlan_info.qos = ivi.qos;
136479aab093SMoshe Shemesh vf_vlan_info.vlan_proto = ivi.vlan_proto;
1365b22b941bSHannes Frederic Sowa vf_tx_rate.rate = ivi.max_tx_rate;
1366b22b941bSHannes Frederic Sowa vf_rate.min_tx_rate = ivi.min_tx_rate;
1367b22b941bSHannes Frederic Sowa vf_rate.max_tx_rate = ivi.max_tx_rate;
1368b22b941bSHannes Frederic Sowa vf_spoofchk.setting = ivi.spoofchk;
1369b22b941bSHannes Frederic Sowa vf_linkstate.link_state = ivi.linkstate;
1370b22b941bSHannes Frederic Sowa vf_rss_query_en.setting = ivi.rss_query_en;
1371b22b941bSHannes Frederic Sowa vf_trust.setting = ivi.trusted;
1372ae0be8deSMichal Kubecek vf = nla_nest_start_noflag(skb, IFLA_VF_INFO);
137379aab093SMoshe Shemesh if (!vf)
13744a59cdfdSGal Pressman return -EMSGSIZE;
1375b22b941bSHannes Frederic Sowa if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
137675345f88SDenis Kirjanov nla_put(skb, IFLA_VF_BROADCAST, sizeof(vf_broadcast), &vf_broadcast) ||
1377b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1378b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1379b22b941bSHannes Frederic Sowa &vf_rate) ||
1380b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1381b22b941bSHannes Frederic Sowa &vf_tx_rate) ||
1382b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1383b22b941bSHannes Frederic Sowa &vf_spoofchk) ||
1384b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1385b22b941bSHannes Frederic Sowa &vf_linkstate) ||
1386b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1387b22b941bSHannes Frederic Sowa sizeof(vf_rss_query_en),
1388b22b941bSHannes Frederic Sowa &vf_rss_query_en) ||
1389b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_TRUST,
1390b22b941bSHannes Frederic Sowa sizeof(vf_trust), &vf_trust))
139179aab093SMoshe Shemesh goto nla_put_vf_failure;
139230aad417SDanit Goldberg
139330aad417SDanit Goldberg if (dev->netdev_ops->ndo_get_vf_guid &&
139430aad417SDanit Goldberg !dev->netdev_ops->ndo_get_vf_guid(dev, vfs_num, &node_guid,
139530aad417SDanit Goldberg &port_guid)) {
139630aad417SDanit Goldberg if (nla_put(skb, IFLA_VF_IB_NODE_GUID, sizeof(node_guid),
139730aad417SDanit Goldberg &node_guid) ||
139830aad417SDanit Goldberg nla_put(skb, IFLA_VF_IB_PORT_GUID, sizeof(port_guid),
139930aad417SDanit Goldberg &port_guid))
140030aad417SDanit Goldberg goto nla_put_vf_failure;
140130aad417SDanit Goldberg }
1402ae0be8deSMichal Kubecek vfvlanlist = nla_nest_start_noflag(skb, IFLA_VF_VLAN_LIST);
140379aab093SMoshe Shemesh if (!vfvlanlist)
140479aab093SMoshe Shemesh goto nla_put_vf_failure;
140579aab093SMoshe Shemesh if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
140679aab093SMoshe Shemesh &vf_vlan_info)) {
140779aab093SMoshe Shemesh nla_nest_cancel(skb, vfvlanlist);
140879aab093SMoshe Shemesh goto nla_put_vf_failure;
140979aab093SMoshe Shemesh }
141079aab093SMoshe Shemesh nla_nest_end(skb, vfvlanlist);
1411fa0e21faSEdwin Peer if (~ext_filter_mask & RTEXT_FILTER_SKIP_STATS) {
1412b22b941bSHannes Frederic Sowa memset(&vf_stats, 0, sizeof(vf_stats));
1413b22b941bSHannes Frederic Sowa if (dev->netdev_ops->ndo_get_vf_stats)
1414b22b941bSHannes Frederic Sowa dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1415b22b941bSHannes Frederic Sowa &vf_stats);
1416ae0be8deSMichal Kubecek vfstats = nla_nest_start_noflag(skb, IFLA_VF_STATS);
141779aab093SMoshe Shemesh if (!vfstats)
141879aab093SMoshe Shemesh goto nla_put_vf_failure;
1419343a6d8eSNicolas Dichtel if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1420343a6d8eSNicolas Dichtel vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1421343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1422343a6d8eSNicolas Dichtel vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1423343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1424343a6d8eSNicolas Dichtel vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1425343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1426343a6d8eSNicolas Dichtel vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1427343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1428343a6d8eSNicolas Dichtel vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1429343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
1430c5a9f6f0SEugenia Emantayev vf_stats.multicast, IFLA_VF_STATS_PAD) ||
1431c5a9f6f0SEugenia Emantayev nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED,
1432c5a9f6f0SEugenia Emantayev vf_stats.rx_dropped, IFLA_VF_STATS_PAD) ||
1433c5a9f6f0SEugenia Emantayev nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED,
1434c5a9f6f0SEugenia Emantayev vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) {
143579aab093SMoshe Shemesh nla_nest_cancel(skb, vfstats);
143679aab093SMoshe Shemesh goto nla_put_vf_failure;
143779aab093SMoshe Shemesh }
1438b22b941bSHannes Frederic Sowa nla_nest_end(skb, vfstats);
1439fa0e21faSEdwin Peer }
1440b22b941bSHannes Frederic Sowa nla_nest_end(skb, vf);
1441b22b941bSHannes Frederic Sowa return 0;
144279aab093SMoshe Shemesh
144379aab093SMoshe Shemesh nla_put_vf_failure:
144479aab093SMoshe Shemesh nla_nest_cancel(skb, vf);
144579aab093SMoshe Shemesh return -EMSGSIZE;
1446b22b941bSHannes Frederic Sowa }
1447b22b941bSHannes Frederic Sowa
rtnl_fill_vf(struct sk_buff * skb,struct net_device * dev,u32 ext_filter_mask)1448250fc3dfSFlorian Westphal static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb,
1449250fc3dfSFlorian Westphal struct net_device *dev,
1450250fc3dfSFlorian Westphal u32 ext_filter_mask)
1451250fc3dfSFlorian Westphal {
1452250fc3dfSFlorian Westphal struct nlattr *vfinfo;
1453250fc3dfSFlorian Westphal int i, num_vfs;
1454250fc3dfSFlorian Westphal
1455250fc3dfSFlorian Westphal if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0))
1456250fc3dfSFlorian Westphal return 0;
1457250fc3dfSFlorian Westphal
1458250fc3dfSFlorian Westphal num_vfs = dev_num_vf(dev->dev.parent);
1459250fc3dfSFlorian Westphal if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs))
1460250fc3dfSFlorian Westphal return -EMSGSIZE;
1461250fc3dfSFlorian Westphal
1462250fc3dfSFlorian Westphal if (!dev->netdev_ops->ndo_get_vf_config)
1463250fc3dfSFlorian Westphal return 0;
1464250fc3dfSFlorian Westphal
1465ae0be8deSMichal Kubecek vfinfo = nla_nest_start_noflag(skb, IFLA_VFINFO_LIST);
1466250fc3dfSFlorian Westphal if (!vfinfo)
1467250fc3dfSFlorian Westphal return -EMSGSIZE;
1468250fc3dfSFlorian Westphal
1469250fc3dfSFlorian Westphal for (i = 0; i < num_vfs; i++) {
14704a59cdfdSGal Pressman if (rtnl_fill_vfinfo(skb, dev, i, ext_filter_mask)) {
14714a59cdfdSGal Pressman nla_nest_cancel(skb, vfinfo);
1472250fc3dfSFlorian Westphal return -EMSGSIZE;
1473250fc3dfSFlorian Westphal }
14744a59cdfdSGal Pressman }
1475250fc3dfSFlorian Westphal
1476250fc3dfSFlorian Westphal nla_nest_end(skb, vfinfo);
1477250fc3dfSFlorian Westphal return 0;
1478250fc3dfSFlorian Westphal }
1479250fc3dfSFlorian Westphal
rtnl_fill_link_ifmap(struct sk_buff * skb,struct net_device * dev)1480b22b941bSHannes Frederic Sowa static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1481b22b941bSHannes Frederic Sowa {
14825f8e4474SKangjie Lu struct rtnl_link_ifmap map;
14835f8e4474SKangjie Lu
14845f8e4474SKangjie Lu memset(&map, 0, sizeof(map));
14855f8e4474SKangjie Lu map.mem_start = dev->mem_start;
14865f8e4474SKangjie Lu map.mem_end = dev->mem_end;
14875f8e4474SKangjie Lu map.base_addr = dev->base_addr;
14885f8e4474SKangjie Lu map.irq = dev->irq;
14895f8e4474SKangjie Lu map.dma = dev->dma;
14905f8e4474SKangjie Lu map.port = dev->if_port;
14915f8e4474SKangjie Lu
1492270cb4d0SNicolas Dichtel if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
1493b22b941bSHannes Frederic Sowa return -EMSGSIZE;
1494b22b941bSHannes Frederic Sowa
1495b22b941bSHannes Frederic Sowa return 0;
1496b22b941bSHannes Frederic Sowa }
1497b22b941bSHannes Frederic Sowa
rtnl_xdp_prog_skb(struct net_device * dev)1498a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_skb(struct net_device *dev)
1499d67b9cd2SDaniel Borkmann {
150058038695SMartin KaFai Lau const struct bpf_prog *generic_xdp_prog;
1501d67b9cd2SDaniel Borkmann
1502d67b9cd2SDaniel Borkmann ASSERT_RTNL();
1503d67b9cd2SDaniel Borkmann
150458038695SMartin KaFai Lau generic_xdp_prog = rtnl_dereference(dev->xdp_prog);
1505a25717d2SJakub Kicinski if (!generic_xdp_prog)
1506a25717d2SJakub Kicinski return 0;
1507a25717d2SJakub Kicinski return generic_xdp_prog->aux->id;
150858038695SMartin KaFai Lau }
1509ce158e58SJakub Kicinski
rtnl_xdp_prog_drv(struct net_device * dev)1510a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_drv(struct net_device *dev)
1511a25717d2SJakub Kicinski {
15127f0a8382SAndrii Nakryiko return dev_xdp_prog_id(dev, XDP_MODE_DRV);
1513a25717d2SJakub Kicinski }
1514118b4aa2SJakub Kicinski
rtnl_xdp_prog_hw(struct net_device * dev)1515a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_hw(struct net_device *dev)
1516a25717d2SJakub Kicinski {
15177f0a8382SAndrii Nakryiko return dev_xdp_prog_id(dev, XDP_MODE_HW);
1518a25717d2SJakub Kicinski }
1519a25717d2SJakub Kicinski
rtnl_xdp_report_one(struct sk_buff * skb,struct net_device * dev,u32 * prog_id,u8 * mode,u8 tgt_mode,u32 attr,u32 (* get_prog_id)(struct net_device * dev))1520a25717d2SJakub Kicinski static int rtnl_xdp_report_one(struct sk_buff *skb, struct net_device *dev,
1521a25717d2SJakub Kicinski u32 *prog_id, u8 *mode, u8 tgt_mode, u32 attr,
1522a25717d2SJakub Kicinski u32 (*get_prog_id)(struct net_device *dev))
1523a25717d2SJakub Kicinski {
1524a25717d2SJakub Kicinski u32 curr_id;
1525a25717d2SJakub Kicinski int err;
1526a25717d2SJakub Kicinski
1527a25717d2SJakub Kicinski curr_id = get_prog_id(dev);
1528a25717d2SJakub Kicinski if (!curr_id)
1529a25717d2SJakub Kicinski return 0;
1530a25717d2SJakub Kicinski
1531a25717d2SJakub Kicinski *prog_id = curr_id;
1532a25717d2SJakub Kicinski err = nla_put_u32(skb, attr, curr_id);
1533a25717d2SJakub Kicinski if (err)
1534a25717d2SJakub Kicinski return err;
1535a25717d2SJakub Kicinski
1536a25717d2SJakub Kicinski if (*mode != XDP_ATTACHED_NONE)
1537a25717d2SJakub Kicinski *mode = XDP_ATTACHED_MULTI;
1538a25717d2SJakub Kicinski else
1539a25717d2SJakub Kicinski *mode = tgt_mode;
1540a25717d2SJakub Kicinski
1541a25717d2SJakub Kicinski return 0;
1542d67b9cd2SDaniel Borkmann }
1543d67b9cd2SDaniel Borkmann
rtnl_xdp_fill(struct sk_buff * skb,struct net_device * dev)1544d1fdd913SBrenden Blanco static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1545d1fdd913SBrenden Blanco {
1546d1fdd913SBrenden Blanco struct nlattr *xdp;
154758038695SMartin KaFai Lau u32 prog_id;
1548d1fdd913SBrenden Blanco int err;
15494f91da26SJakub Kicinski u8 mode;
1550d1fdd913SBrenden Blanco
1551ae0be8deSMichal Kubecek xdp = nla_nest_start_noflag(skb, IFLA_XDP);
1552d1fdd913SBrenden Blanco if (!xdp)
1553d1fdd913SBrenden Blanco return -EMSGSIZE;
1554b5cdae32SDavid S. Miller
1555a25717d2SJakub Kicinski prog_id = 0;
1556a25717d2SJakub Kicinski mode = XDP_ATTACHED_NONE;
1557202aabe8SJakub Kicinski err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_SKB,
1558202aabe8SJakub Kicinski IFLA_XDP_SKB_PROG_ID, rtnl_xdp_prog_skb);
1559202aabe8SJakub Kicinski if (err)
1560a25717d2SJakub Kicinski goto err_cancel;
1561202aabe8SJakub Kicinski err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_DRV,
1562202aabe8SJakub Kicinski IFLA_XDP_DRV_PROG_ID, rtnl_xdp_prog_drv);
1563202aabe8SJakub Kicinski if (err)
1564a25717d2SJakub Kicinski goto err_cancel;
1565202aabe8SJakub Kicinski err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_HW,
1566202aabe8SJakub Kicinski IFLA_XDP_HW_PROG_ID, rtnl_xdp_prog_hw);
1567202aabe8SJakub Kicinski if (err)
1568a25717d2SJakub Kicinski goto err_cancel;
1569a25717d2SJakub Kicinski
15704f91da26SJakub Kicinski err = nla_put_u8(skb, IFLA_XDP_ATTACHED, mode);
1571d1fdd913SBrenden Blanco if (err)
1572d1fdd913SBrenden Blanco goto err_cancel;
1573d1fdd913SBrenden Blanco
1574a25717d2SJakub Kicinski if (prog_id && mode != XDP_ATTACHED_MULTI) {
157558038695SMartin KaFai Lau err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
157658038695SMartin KaFai Lau if (err)
157758038695SMartin KaFai Lau goto err_cancel;
157858038695SMartin KaFai Lau }
157958038695SMartin KaFai Lau
1580d1fdd913SBrenden Blanco nla_nest_end(skb, xdp);
1581d1fdd913SBrenden Blanco return 0;
1582d1fdd913SBrenden Blanco
1583d1fdd913SBrenden Blanco err_cancel:
1584d1fdd913SBrenden Blanco nla_nest_cancel(skb, xdp);
1585d1fdd913SBrenden Blanco return err;
1586d1fdd913SBrenden Blanco }
1587d1fdd913SBrenden Blanco
rtnl_get_event(unsigned long event)15883d3ea5afSVlad Yasevich static u32 rtnl_get_event(unsigned long event)
15893d3ea5afSVlad Yasevich {
15903d3ea5afSVlad Yasevich u32 rtnl_event_type = IFLA_EVENT_NONE;
15913d3ea5afSVlad Yasevich
15923d3ea5afSVlad Yasevich switch (event) {
15933d3ea5afSVlad Yasevich case NETDEV_REBOOT:
15943d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_REBOOT;
15953d3ea5afSVlad Yasevich break;
15963d3ea5afSVlad Yasevich case NETDEV_FEAT_CHANGE:
15973d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_FEATURES;
15983d3ea5afSVlad Yasevich break;
15993d3ea5afSVlad Yasevich case NETDEV_BONDING_FAILOVER:
16003d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER;
16013d3ea5afSVlad Yasevich break;
16023d3ea5afSVlad Yasevich case NETDEV_NOTIFY_PEERS:
16033d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS;
16043d3ea5afSVlad Yasevich break;
16053d3ea5afSVlad Yasevich case NETDEV_RESEND_IGMP:
16063d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_IGMP_RESEND;
16073d3ea5afSVlad Yasevich break;
16083d3ea5afSVlad Yasevich case NETDEV_CHANGEINFODATA:
16093d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS;
16103d3ea5afSVlad Yasevich break;
16113d3ea5afSVlad Yasevich default:
16123d3ea5afSVlad Yasevich break;
16133d3ea5afSVlad Yasevich }
16143d3ea5afSVlad Yasevich
16153d3ea5afSVlad Yasevich return rtnl_event_type;
16163d3ea5afSVlad Yasevich }
16173d3ea5afSVlad Yasevich
put_master_ifindex(struct sk_buff * skb,struct net_device * dev)161879110a04SFlorian Westphal static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev)
161979110a04SFlorian Westphal {
162079110a04SFlorian Westphal const struct net_device *upper_dev;
162179110a04SFlorian Westphal int ret = 0;
162279110a04SFlorian Westphal
162379110a04SFlorian Westphal rcu_read_lock();
162479110a04SFlorian Westphal
162579110a04SFlorian Westphal upper_dev = netdev_master_upper_dev_get_rcu(dev);
162679110a04SFlorian Westphal if (upper_dev)
162779110a04SFlorian Westphal ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex);
162879110a04SFlorian Westphal
162979110a04SFlorian Westphal rcu_read_unlock();
163079110a04SFlorian Westphal return ret;
163179110a04SFlorian Westphal }
163279110a04SFlorian Westphal
nla_put_iflink(struct sk_buff * skb,const struct net_device * dev,bool force)1633feadc4b6SSabrina Dubroca static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev,
1634feadc4b6SSabrina Dubroca bool force)
163579110a04SFlorian Westphal {
163679110a04SFlorian Westphal int ifindex = dev_get_iflink(dev);
163779110a04SFlorian Westphal
1638feadc4b6SSabrina Dubroca if (force || dev->ifindex != ifindex)
163979110a04SFlorian Westphal return nla_put_u32(skb, IFLA_LINK, ifindex);
1640feadc4b6SSabrina Dubroca
1641feadc4b6SSabrina Dubroca return 0;
164279110a04SFlorian Westphal }
164379110a04SFlorian Westphal
nla_put_ifalias(struct sk_buff * skb,struct net_device * dev)16446c557001SFlorian Westphal static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb,
16456c557001SFlorian Westphal struct net_device *dev)
16466c557001SFlorian Westphal {
16476c557001SFlorian Westphal char buf[IFALIASZ];
16486c557001SFlorian Westphal int ret;
16496c557001SFlorian Westphal
16506c557001SFlorian Westphal ret = dev_get_alias(dev, buf, sizeof(buf));
16516c557001SFlorian Westphal return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0;
16526c557001SFlorian Westphal }
16536c557001SFlorian Westphal
rtnl_fill_link_netnsid(struct sk_buff * skb,const struct net_device * dev,struct net * src_net,gfp_t gfp)1654b1e66b9aSFlorian Westphal static int rtnl_fill_link_netnsid(struct sk_buff *skb,
165579e1ad14SJiri Benc const struct net_device *dev,
1656d4e4fdf9SGuillaume Nault struct net *src_net, gfp_t gfp)
1657b1e66b9aSFlorian Westphal {
1658feadc4b6SSabrina Dubroca bool put_iflink = false;
1659feadc4b6SSabrina Dubroca
1660b1e66b9aSFlorian Westphal if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) {
1661b1e66b9aSFlorian Westphal struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1662b1e66b9aSFlorian Westphal
1663b1e66b9aSFlorian Westphal if (!net_eq(dev_net(dev), link_net)) {
1664d4e4fdf9SGuillaume Nault int id = peernet2id_alloc(src_net, link_net, gfp);
1665b1e66b9aSFlorian Westphal
1666b1e66b9aSFlorian Westphal if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1667b1e66b9aSFlorian Westphal return -EMSGSIZE;
1668feadc4b6SSabrina Dubroca
1669feadc4b6SSabrina Dubroca put_iflink = true;
1670b1e66b9aSFlorian Westphal }
1671b1e66b9aSFlorian Westphal }
1672b1e66b9aSFlorian Westphal
1673feadc4b6SSabrina Dubroca return nla_put_iflink(skb, dev, put_iflink);
1674b1e66b9aSFlorian Westphal }
1675b1e66b9aSFlorian Westphal
rtnl_fill_link_af(struct sk_buff * skb,const struct net_device * dev,u32 ext_filter_mask)1676070cbf5bSFlorian Westphal static int rtnl_fill_link_af(struct sk_buff *skb,
1677070cbf5bSFlorian Westphal const struct net_device *dev,
1678070cbf5bSFlorian Westphal u32 ext_filter_mask)
1679070cbf5bSFlorian Westphal {
1680070cbf5bSFlorian Westphal const struct rtnl_af_ops *af_ops;
1681070cbf5bSFlorian Westphal struct nlattr *af_spec;
1682070cbf5bSFlorian Westphal
1683ae0be8deSMichal Kubecek af_spec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
1684070cbf5bSFlorian Westphal if (!af_spec)
1685070cbf5bSFlorian Westphal return -EMSGSIZE;
1686070cbf5bSFlorian Westphal
16875fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
1688070cbf5bSFlorian Westphal struct nlattr *af;
1689070cbf5bSFlorian Westphal int err;
1690070cbf5bSFlorian Westphal
1691070cbf5bSFlorian Westphal if (!af_ops->fill_link_af)
1692070cbf5bSFlorian Westphal continue;
1693070cbf5bSFlorian Westphal
1694ae0be8deSMichal Kubecek af = nla_nest_start_noflag(skb, af_ops->family);
1695070cbf5bSFlorian Westphal if (!af)
1696070cbf5bSFlorian Westphal return -EMSGSIZE;
1697070cbf5bSFlorian Westphal
1698070cbf5bSFlorian Westphal err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1699070cbf5bSFlorian Westphal /*
1700070cbf5bSFlorian Westphal * Caller may return ENODATA to indicate that there
1701070cbf5bSFlorian Westphal * was no data to be dumped. This is not an error, it
1702070cbf5bSFlorian Westphal * means we should trim the attribute header and
1703070cbf5bSFlorian Westphal * continue.
1704070cbf5bSFlorian Westphal */
1705070cbf5bSFlorian Westphal if (err == -ENODATA)
1706070cbf5bSFlorian Westphal nla_nest_cancel(skb, af);
1707070cbf5bSFlorian Westphal else if (err < 0)
1708070cbf5bSFlorian Westphal return -EMSGSIZE;
1709070cbf5bSFlorian Westphal
1710070cbf5bSFlorian Westphal nla_nest_end(skb, af);
1711070cbf5bSFlorian Westphal }
1712070cbf5bSFlorian Westphal
1713070cbf5bSFlorian Westphal nla_nest_end(skb, af_spec);
1714070cbf5bSFlorian Westphal return 0;
1715070cbf5bSFlorian Westphal }
1716070cbf5bSFlorian Westphal
rtnl_fill_alt_ifnames(struct sk_buff * skb,const struct net_device * dev)171788f4fb0cSJiri Pirko static int rtnl_fill_alt_ifnames(struct sk_buff *skb,
171888f4fb0cSJiri Pirko const struct net_device *dev)
171988f4fb0cSJiri Pirko {
172088f4fb0cSJiri Pirko struct netdev_name_node *name_node;
172188f4fb0cSJiri Pirko int count = 0;
172288f4fb0cSJiri Pirko
172388f4fb0cSJiri Pirko list_for_each_entry(name_node, &dev->name_node->list, list) {
172488f4fb0cSJiri Pirko if (nla_put_string(skb, IFLA_ALT_IFNAME, name_node->name))
172588f4fb0cSJiri Pirko return -EMSGSIZE;
172688f4fb0cSJiri Pirko count++;
172788f4fb0cSJiri Pirko }
172888f4fb0cSJiri Pirko return count;
172988f4fb0cSJiri Pirko }
173088f4fb0cSJiri Pirko
rtnl_fill_prop_list(struct sk_buff * skb,const struct net_device * dev)173188f4fb0cSJiri Pirko static int rtnl_fill_prop_list(struct sk_buff *skb,
173288f4fb0cSJiri Pirko const struct net_device *dev)
173388f4fb0cSJiri Pirko {
173488f4fb0cSJiri Pirko struct nlattr *prop_list;
173588f4fb0cSJiri Pirko int ret;
173688f4fb0cSJiri Pirko
173788f4fb0cSJiri Pirko prop_list = nla_nest_start(skb, IFLA_PROP_LIST);
173888f4fb0cSJiri Pirko if (!prop_list)
173988f4fb0cSJiri Pirko return -EMSGSIZE;
174088f4fb0cSJiri Pirko
174188f4fb0cSJiri Pirko ret = rtnl_fill_alt_ifnames(skb, dev);
174288f4fb0cSJiri Pirko if (ret <= 0)
174388f4fb0cSJiri Pirko goto nest_cancel;
174488f4fb0cSJiri Pirko
174588f4fb0cSJiri Pirko nla_nest_end(skb, prop_list);
174688f4fb0cSJiri Pirko return 0;
174788f4fb0cSJiri Pirko
174888f4fb0cSJiri Pirko nest_cancel:
174988f4fb0cSJiri Pirko nla_nest_cancel(skb, prop_list);
175088f4fb0cSJiri Pirko return ret;
175188f4fb0cSJiri Pirko }
175288f4fb0cSJiri Pirko
rtnl_fill_proto_down(struct sk_buff * skb,const struct net_device * dev)1753829eb208SRoopa Prabhu static int rtnl_fill_proto_down(struct sk_buff *skb,
1754829eb208SRoopa Prabhu const struct net_device *dev)
1755829eb208SRoopa Prabhu {
1756829eb208SRoopa Prabhu struct nlattr *pr;
1757829eb208SRoopa Prabhu u32 preason;
1758829eb208SRoopa Prabhu
1759829eb208SRoopa Prabhu if (nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down))
1760829eb208SRoopa Prabhu goto nla_put_failure;
1761829eb208SRoopa Prabhu
1762829eb208SRoopa Prabhu preason = dev->proto_down_reason;
1763829eb208SRoopa Prabhu if (!preason)
1764829eb208SRoopa Prabhu return 0;
1765829eb208SRoopa Prabhu
1766829eb208SRoopa Prabhu pr = nla_nest_start(skb, IFLA_PROTO_DOWN_REASON);
1767829eb208SRoopa Prabhu if (!pr)
1768829eb208SRoopa Prabhu return -EMSGSIZE;
1769829eb208SRoopa Prabhu
1770829eb208SRoopa Prabhu if (nla_put_u32(skb, IFLA_PROTO_DOWN_REASON_VALUE, preason)) {
1771829eb208SRoopa Prabhu nla_nest_cancel(skb, pr);
1772829eb208SRoopa Prabhu goto nla_put_failure;
1773829eb208SRoopa Prabhu }
1774829eb208SRoopa Prabhu
1775829eb208SRoopa Prabhu nla_nest_end(skb, pr);
1776829eb208SRoopa Prabhu return 0;
1777829eb208SRoopa Prabhu
1778829eb208SRoopa Prabhu nla_put_failure:
1779829eb208SRoopa Prabhu return -EMSGSIZE;
1780829eb208SRoopa Prabhu }
1781829eb208SRoopa Prabhu
rtnl_fill_devlink_port(struct sk_buff * skb,const struct net_device * dev)1782dca56c30SJiri Pirko static int rtnl_fill_devlink_port(struct sk_buff *skb,
1783dca56c30SJiri Pirko const struct net_device *dev)
1784dca56c30SJiri Pirko {
1785dca56c30SJiri Pirko struct nlattr *devlink_port_nest;
1786dca56c30SJiri Pirko int ret;
1787dca56c30SJiri Pirko
1788dca56c30SJiri Pirko devlink_port_nest = nla_nest_start(skb, IFLA_DEVLINK_PORT);
1789dca56c30SJiri Pirko if (!devlink_port_nest)
1790dca56c30SJiri Pirko return -EMSGSIZE;
1791dca56c30SJiri Pirko
1792dca56c30SJiri Pirko if (dev->devlink_port) {
1793dca56c30SJiri Pirko ret = devlink_nl_port_handle_fill(skb, dev->devlink_port);
1794dca56c30SJiri Pirko if (ret < 0)
1795dca56c30SJiri Pirko goto nest_cancel;
1796dca56c30SJiri Pirko }
1797dca56c30SJiri Pirko
1798dca56c30SJiri Pirko nla_nest_end(skb, devlink_port_nest);
1799dca56c30SJiri Pirko return 0;
1800dca56c30SJiri Pirko
1801dca56c30SJiri Pirko nest_cancel:
1802dca56c30SJiri Pirko nla_nest_cancel(skb, devlink_port_nest);
1803dca56c30SJiri Pirko return ret;
1804dca56c30SJiri Pirko }
1805dca56c30SJiri Pirko
rtnl_fill_ifinfo(struct sk_buff * skb,struct net_device * dev,struct net * src_net,int type,u32 pid,u32 seq,u32 change,unsigned int flags,u32 ext_filter_mask,u32 event,int * new_nsid,int new_ifindex,int tgt_netnsid,gfp_t gfp)180679e1ad14SJiri Benc static int rtnl_fill_ifinfo(struct sk_buff *skb,
180779e1ad14SJiri Benc struct net_device *dev, struct net *src_net,
1808575c3e2aSPatrick McHardy int type, u32 pid, u32 seq, u32 change,
18093d3ea5afSVlad Yasevich unsigned int flags, u32 ext_filter_mask,
181038e01b30SNicolas Dichtel u32 event, int *new_nsid, int new_ifindex,
1811d4e4fdf9SGuillaume Nault int tgt_netnsid, gfp_t gfp)
1812b60c5115SThomas Graf {
1813b60c5115SThomas Graf struct ifinfomsg *ifm;
18141da177e4SLinus Torvalds struct nlmsghdr *nlh;
18155891cd5eSEric Dumazet struct Qdisc *qdisc;
18161da177e4SLinus Torvalds
18172907c35fSEric Dumazet ASSERT_RTNL();
1818b60c5115SThomas Graf nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1819b60c5115SThomas Graf if (nlh == NULL)
182026932566SPatrick McHardy return -EMSGSIZE;
18211da177e4SLinus Torvalds
1822b60c5115SThomas Graf ifm = nlmsg_data(nlh);
1823b60c5115SThomas Graf ifm->ifi_family = AF_UNSPEC;
1824b60c5115SThomas Graf ifm->__ifi_pad = 0;
1825b60c5115SThomas Graf ifm->ifi_type = dev->type;
1826b60c5115SThomas Graf ifm->ifi_index = dev->ifindex;
1827b60c5115SThomas Graf ifm->ifi_flags = dev_get_flags(dev);
1828b60c5115SThomas Graf ifm->ifi_change = change;
18291da177e4SLinus Torvalds
18307e4a8d5aSChristian Brauner if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_TARGET_NETNSID, tgt_netnsid))
183179e1ad14SJiri Benc goto nla_put_failure;
183279e1ad14SJiri Benc
18335891cd5eSEric Dumazet qdisc = rtnl_dereference(dev->qdisc);
1834a6574349SDavid S. Miller if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1835a6574349SDavid S. Miller nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1836a6574349SDavid S. Miller nla_put_u8(skb, IFLA_OPERSTATE,
1837a6574349SDavid S. Miller netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1838a6574349SDavid S. Miller nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1839a6574349SDavid S. Miller nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
18403e7a50ceSStephen Hemminger nla_put_u32(skb, IFLA_MIN_MTU, dev->min_mtu) ||
18413e7a50ceSStephen Hemminger nla_put_u32(skb, IFLA_MAX_MTU, dev->max_mtu) ||
1842a6574349SDavid S. Miller nla_put_u32(skb, IFLA_GROUP, dev->group) ||
1843edbc0bb3SBen Greear nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
18447e6e1b57SNicolas Dichtel nla_put_u32(skb, IFLA_ALLMULTI, dev->allmulti) ||
184576ff5cc9SJiri Pirko nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
1846c70ce028SEric Dumazet nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1847c70ce028SEric Dumazet nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
1848eac1b93cSCoco Li nla_put_u32(skb, IFLA_GRO_MAX_SIZE, dev->gro_max_size) ||
18499eefedd5SXin Long nla_put_u32(skb, IFLA_GSO_IPV4_MAX_SIZE, dev->gso_ipv4_max_size) ||
18509eefedd5SXin Long nla_put_u32(skb, IFLA_GRO_IPV4_MAX_SIZE, dev->gro_ipv4_max_size) ||
185189527be8SEric Dumazet nla_put_u32(skb, IFLA_TSO_MAX_SIZE, dev->tso_max_size) ||
185289527be8SEric Dumazet nla_put_u32(skb, IFLA_TSO_MAX_SEGS, dev->tso_max_segs) ||
18531d69c2b3SMark A. Greer #ifdef CONFIG_RPS
185476ff5cc9SJiri Pirko nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
18551d69c2b3SMark A. Greer #endif
185679110a04SFlorian Westphal put_master_ifindex(skb, dev) ||
18579a57247fSJiri Pirko nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
18585891cd5eSEric Dumazet (qdisc &&
18595891cd5eSEric Dumazet nla_put_string(skb, IFLA_QDISC, qdisc->ops->id)) ||
18606c557001SFlorian Westphal nla_put_ifalias(skb, dev) ||
18612d3b479dSdavid decotigny nla_put_u32(skb, IFLA_CARRIER_CHANGES,
1862b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_up_count) +
1863b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_down_count)) ||
1864b2d3bcfaSDavid Decotigny nla_put_u32(skb, IFLA_CARRIER_UP_COUNT,
1865b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_up_count)) ||
1866b2d3bcfaSDavid Decotigny nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT,
1867b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_down_count)))
1868a6574349SDavid S. Miller goto nla_put_failure;
18690b815a1aSStephen Hemminger
1870829eb208SRoopa Prabhu if (rtnl_fill_proto_down(skb, dev))
1871829eb208SRoopa Prabhu goto nla_put_failure;
1872829eb208SRoopa Prabhu
18733d3ea5afSVlad Yasevich if (event != IFLA_EVENT_NONE) {
18743d3ea5afSVlad Yasevich if (nla_put_u32(skb, IFLA_EVENT, event))
18753d3ea5afSVlad Yasevich goto nla_put_failure;
18763d3ea5afSVlad Yasevich }
18773d3ea5afSVlad Yasevich
1878b22b941bSHannes Frederic Sowa if (rtnl_fill_link_ifmap(skb, dev))
1879a6574349SDavid S. Miller goto nla_put_failure;
18801da177e4SLinus Torvalds
18811da177e4SLinus Torvalds if (dev->addr_len) {
1882a6574349SDavid S. Miller if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1883a6574349SDavid S. Miller nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1884a6574349SDavid S. Miller goto nla_put_failure;
18851da177e4SLinus Torvalds }
18861da177e4SLinus Torvalds
188766cae9edSJiri Pirko if (rtnl_phys_port_id_fill(skb, dev))
188866cae9edSJiri Pirko goto nla_put_failure;
188966cae9edSJiri Pirko
1890db24a904SDavid Ahern if (rtnl_phys_port_name_fill(skb, dev))
1891db24a904SDavid Ahern goto nla_put_failure;
1892db24a904SDavid Ahern
189382f28412SJiri Pirko if (rtnl_phys_switch_id_fill(skb, dev))
189482f28412SJiri Pirko goto nla_put_failure;
189582f28412SJiri Pirko
1896b22b941bSHannes Frederic Sowa if (rtnl_fill_stats(skb, dev))
1897b60c5115SThomas Graf goto nla_put_failure;
1898b60c5115SThomas Graf
1899250fc3dfSFlorian Westphal if (rtnl_fill_vf(skb, dev, ext_filter_mask))
1900a6574349SDavid S. Miller goto nla_put_failure;
190157b61080SScott Feldman
1902c53864fdSDavid Gibson if (rtnl_port_fill(skb, dev, ext_filter_mask))
190357b61080SScott Feldman goto nla_put_failure;
190457b61080SScott Feldman
1905d1fdd913SBrenden Blanco if (rtnl_xdp_fill(skb, dev))
1906d1fdd913SBrenden Blanco goto nla_put_failure;
1907d1fdd913SBrenden Blanco
1908ba7d49b1SJiri Pirko if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
190938f7b870SPatrick McHardy if (rtnl_link_fill(skb, dev) < 0)
191038f7b870SPatrick McHardy goto nla_put_failure;
191138f7b870SPatrick McHardy }
191238f7b870SPatrick McHardy
1913d4e4fdf9SGuillaume Nault if (rtnl_fill_link_netnsid(skb, dev, src_net, gfp))
1914d37512a2SNicolas Dichtel goto nla_put_failure;
1915d37512a2SNicolas Dichtel
19166621dd29SNicolas Dichtel if (new_nsid &&
19176621dd29SNicolas Dichtel nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0)
19186621dd29SNicolas Dichtel goto nla_put_failure;
191938e01b30SNicolas Dichtel if (new_ifindex &&
192038e01b30SNicolas Dichtel nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0)
192138e01b30SNicolas Dichtel goto nla_put_failure;
192238e01b30SNicolas Dichtel
1923f74877a5SMichal Kubecek if (memchr_inv(dev->perm_addr, '\0', dev->addr_len) &&
1924f74877a5SMichal Kubecek nla_put(skb, IFLA_PERM_ADDRESS, dev->addr_len, dev->perm_addr))
1925f74877a5SMichal Kubecek goto nla_put_failure;
19266621dd29SNicolas Dichtel
19275fa85a09SFlorian Westphal rcu_read_lock();
1928070cbf5bSFlorian Westphal if (rtnl_fill_link_af(skb, dev, ext_filter_mask))
19295fa85a09SFlorian Westphal goto nla_put_failure_rcu;
19305fa85a09SFlorian Westphal rcu_read_unlock();
1931f8ff182cSThomas Graf
193288f4fb0cSJiri Pirko if (rtnl_fill_prop_list(skb, dev))
193388f4fb0cSJiri Pirko goto nla_put_failure;
193488f4fb0cSJiri Pirko
193500e77ed8SJohannes Berg if (dev->dev.parent &&
193600e77ed8SJohannes Berg nla_put_string(skb, IFLA_PARENT_DEV_NAME,
193700e77ed8SJohannes Berg dev_name(dev->dev.parent)))
193800e77ed8SJohannes Berg goto nla_put_failure;
193900e77ed8SJohannes Berg
194000e77ed8SJohannes Berg if (dev->dev.parent && dev->dev.parent->bus &&
194100e77ed8SJohannes Berg nla_put_string(skb, IFLA_PARENT_DEV_BUS_NAME,
194200e77ed8SJohannes Berg dev->dev.parent->bus->name))
194300e77ed8SJohannes Berg goto nla_put_failure;
194400e77ed8SJohannes Berg
1945dca56c30SJiri Pirko if (rtnl_fill_devlink_port(skb, dev))
1946dca56c30SJiri Pirko goto nla_put_failure;
1947dca56c30SJiri Pirko
1948053c095aSJohannes Berg nlmsg_end(skb, nlh);
1949053c095aSJohannes Berg return 0;
1950b60c5115SThomas Graf
19515fa85a09SFlorian Westphal nla_put_failure_rcu:
19525fa85a09SFlorian Westphal rcu_read_unlock();
1953b60c5115SThomas Graf nla_put_failure:
195426932566SPatrick McHardy nlmsg_cancel(skb, nlh);
195526932566SPatrick McHardy return -EMSGSIZE;
19561da177e4SLinus Torvalds }
19571da177e4SLinus Torvalds
1958f7b12606SJiri Pirko static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1959f7b12606SJiri Pirko [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1960f7b12606SJiri Pirko [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1961f7b12606SJiri Pirko [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1962f7b12606SJiri Pirko [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1963f7b12606SJiri Pirko [IFLA_MTU] = { .type = NLA_U32 },
1964f7b12606SJiri Pirko [IFLA_LINK] = { .type = NLA_U32 },
1965f7b12606SJiri Pirko [IFLA_MASTER] = { .type = NLA_U32 },
1966f7b12606SJiri Pirko [IFLA_CARRIER] = { .type = NLA_U8 },
1967f7b12606SJiri Pirko [IFLA_TXQLEN] = { .type = NLA_U32 },
1968f7b12606SJiri Pirko [IFLA_WEIGHT] = { .type = NLA_U32 },
1969f7b12606SJiri Pirko [IFLA_OPERSTATE] = { .type = NLA_U8 },
1970f7b12606SJiri Pirko [IFLA_LINKMODE] = { .type = NLA_U8 },
1971f7b12606SJiri Pirko [IFLA_LINKINFO] = { .type = NLA_NESTED },
1972f7b12606SJiri Pirko [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1973f7b12606SJiri Pirko [IFLA_NET_NS_FD] = { .type = NLA_U32 },
19742459b4c6SNicolas Dichtel /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
19752459b4c6SNicolas Dichtel * allow 0-length string (needed to remove an alias).
19762459b4c6SNicolas Dichtel */
19772459b4c6SNicolas Dichtel [IFLA_IFALIAS] = { .type = NLA_BINARY, .len = IFALIASZ - 1 },
1978f7b12606SJiri Pirko [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1979f7b12606SJiri Pirko [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1980f7b12606SJiri Pirko [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1981f7b12606SJiri Pirko [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1982f7b12606SJiri Pirko [IFLA_EXT_MASK] = { .type = NLA_U32 },
1983f7b12606SJiri Pirko [IFLA_PROMISCUITY] = { .type = NLA_U32 },
1984f7b12606SJiri Pirko [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1985f7b12606SJiri Pirko [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
198646e6b992SStephen Hemminger [IFLA_GSO_MAX_SEGS] = { .type = NLA_U32 },
1987*ac597700SWang Liang [IFLA_GSO_MAX_SIZE] = NLA_POLICY_MIN(NLA_U32, MAX_TCP_HEADER + 1),
198802637fceSJiri Pirko [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
19892d3b479dSdavid decotigny [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
199082f28412SJiri Pirko [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1991317f4810SNicolas Dichtel [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
199288d6378bSAnuradha Karuppiah [IFLA_PROTO_DOWN] = { .type = NLA_U8 },
1993d1fdd913SBrenden Blanco [IFLA_XDP] = { .type = NLA_NESTED },
19943d3ea5afSVlad Yasevich [IFLA_EVENT] = { .type = NLA_U32 },
1995db833d40SSerhey Popovych [IFLA_GROUP] = { .type = NLA_U32 },
19967e4a8d5aSChristian Brauner [IFLA_TARGET_NETNSID] = { .type = NLA_S32 },
1997b2d3bcfaSDavid Decotigny [IFLA_CARRIER_UP_COUNT] = { .type = NLA_U32 },
1998b2d3bcfaSDavid Decotigny [IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 },
19993e7a50ceSStephen Hemminger [IFLA_MIN_MTU] = { .type = NLA_U32 },
20003e7a50ceSStephen Hemminger [IFLA_MAX_MTU] = { .type = NLA_U32 },
200136fbf1e5SJiri Pirko [IFLA_PROP_LIST] = { .type = NLA_NESTED },
200236fbf1e5SJiri Pirko [IFLA_ALT_IFNAME] = { .type = NLA_STRING,
200336fbf1e5SJiri Pirko .len = ALTIFNAMSIZ - 1 },
2004f74877a5SMichal Kubecek [IFLA_PERM_ADDRESS] = { .type = NLA_REJECT },
2005829eb208SRoopa Prabhu [IFLA_PROTO_DOWN_REASON] = { .type = NLA_NESTED },
20067e4a5131SAndrei Vagin [IFLA_NEW_IFINDEX] = NLA_POLICY_MIN(NLA_S32, 1),
200788b71053SJohannes Berg [IFLA_PARENT_DEV_NAME] = { .type = NLA_NUL_STRING },
2008eac1b93cSCoco Li [IFLA_GRO_MAX_SIZE] = { .type = NLA_U32 },
200989527be8SEric Dumazet [IFLA_TSO_MAX_SIZE] = { .type = NLA_REJECT },
201089527be8SEric Dumazet [IFLA_TSO_MAX_SEGS] = { .type = NLA_REJECT },
20117e6e1b57SNicolas Dichtel [IFLA_ALLMULTI] = { .type = NLA_REJECT },
2012*ac597700SWang Liang [IFLA_GSO_IPV4_MAX_SIZE] = NLA_POLICY_MIN(NLA_U32, MAX_TCP_HEADER + 1),
20139eefedd5SXin Long [IFLA_GRO_IPV4_MAX_SIZE] = { .type = NLA_U32 },
2014f7b12606SJiri Pirko };
2015f7b12606SJiri Pirko
2016f7b12606SJiri Pirko static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
2017f7b12606SJiri Pirko [IFLA_INFO_KIND] = { .type = NLA_STRING },
2018f7b12606SJiri Pirko [IFLA_INFO_DATA] = { .type = NLA_NESTED },
2019f7b12606SJiri Pirko [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
2020f7b12606SJiri Pirko [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
2021f7b12606SJiri Pirko };
2022f7b12606SJiri Pirko
2023f7b12606SJiri Pirko static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
2024364d5716SDaniel Borkmann [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
202575345f88SDenis Kirjanov [IFLA_VF_BROADCAST] = { .type = NLA_REJECT },
2026364d5716SDaniel Borkmann [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
202779aab093SMoshe Shemesh [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED },
2028364d5716SDaniel Borkmann [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
2029364d5716SDaniel Borkmann [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
2030364d5716SDaniel Borkmann [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
2031364d5716SDaniel Borkmann [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
203201a3d796SVlad Zolotarov [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
20333b766cd8SEran Ben Elisha [IFLA_VF_STATS] = { .type = NLA_NESTED },
2034dd461d6aSHiroshi Shimamoto [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) },
2035cc8e27ccSEli Cohen [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) },
2036cc8e27ccSEli Cohen [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) },
20373b766cd8SEran Ben Elisha };
20383b766cd8SEran Ben Elisha
2039f7b12606SJiri Pirko static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
2040f7b12606SJiri Pirko [IFLA_PORT_VF] = { .type = NLA_U32 },
2041f7b12606SJiri Pirko [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
2042f7b12606SJiri Pirko .len = PORT_PROFILE_MAX },
2043f7b12606SJiri Pirko [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
2044f7b12606SJiri Pirko .len = PORT_UUID_MAX },
2045f7b12606SJiri Pirko [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
2046f7b12606SJiri Pirko .len = PORT_UUID_MAX },
2047f7b12606SJiri Pirko [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
2048f7b12606SJiri Pirko [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
2049025331dfSDaniel Borkmann
2050025331dfSDaniel Borkmann /* Unused, but we need to keep it here since user space could
2051025331dfSDaniel Borkmann * fill it. It's also broken with regard to NLA_BINARY use in
2052025331dfSDaniel Borkmann * combination with structs.
2053025331dfSDaniel Borkmann */
2054025331dfSDaniel Borkmann [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
2055025331dfSDaniel Borkmann .len = sizeof(struct ifla_port_vsi) },
2056f7b12606SJiri Pirko };
2057f7b12606SJiri Pirko
2058d1fdd913SBrenden Blanco static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
205992234c8fSToke Høiland-Jørgensen [IFLA_XDP_UNSPEC] = { .strict_start_type = IFLA_XDP_EXPECTED_FD },
2060d1fdd913SBrenden Blanco [IFLA_XDP_FD] = { .type = NLA_S32 },
206192234c8fSToke Høiland-Jørgensen [IFLA_XDP_EXPECTED_FD] = { .type = NLA_S32 },
2062d1fdd913SBrenden Blanco [IFLA_XDP_ATTACHED] = { .type = NLA_U8 },
206385de8576SDaniel Borkmann [IFLA_XDP_FLAGS] = { .type = NLA_U32 },
206458038695SMartin KaFai Lau [IFLA_XDP_PROG_ID] = { .type = NLA_U32 },
2065d1fdd913SBrenden Blanco };
2066d1fdd913SBrenden Blanco
linkinfo_to_kind_ops(const struct nlattr * nla)2067dc599f76SDavid Ahern static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
2068dc599f76SDavid Ahern {
2069dc599f76SDavid Ahern const struct rtnl_link_ops *ops = NULL;
2070dc599f76SDavid Ahern struct nlattr *linfo[IFLA_INFO_MAX + 1];
2071dc599f76SDavid Ahern
20728cb08174SJohannes Berg if (nla_parse_nested_deprecated(linfo, IFLA_INFO_MAX, nla, ifla_info_policy, NULL) < 0)
2073dc599f76SDavid Ahern return NULL;
2074dc599f76SDavid Ahern
2075dc599f76SDavid Ahern if (linfo[IFLA_INFO_KIND]) {
2076dc599f76SDavid Ahern char kind[MODULE_NAME_LEN];
2077dc599f76SDavid Ahern
2078872f6903SFrancis Laniel nla_strscpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
2079dc599f76SDavid Ahern ops = rtnl_link_ops_get(kind);
2080dc599f76SDavid Ahern }
2081dc599f76SDavid Ahern
2082dc599f76SDavid Ahern return ops;
2083dc599f76SDavid Ahern }
2084dc599f76SDavid Ahern
link_master_filtered(struct net_device * dev,int master_idx)2085dc599f76SDavid Ahern static bool link_master_filtered(struct net_device *dev, int master_idx)
2086dc599f76SDavid Ahern {
2087dc599f76SDavid Ahern struct net_device *master;
2088dc599f76SDavid Ahern
2089dc599f76SDavid Ahern if (!master_idx)
2090dc599f76SDavid Ahern return false;
2091dc599f76SDavid Ahern
2092dc599f76SDavid Ahern master = netdev_master_upper_dev_get(dev);
2093d3432bf1SLahav Schlesinger
2094d3432bf1SLahav Schlesinger /* 0 is already used to denote IFLA_MASTER wasn't passed, therefore need
2095d3432bf1SLahav Schlesinger * another invalid value for ifindex to denote "no master".
2096d3432bf1SLahav Schlesinger */
2097d3432bf1SLahav Schlesinger if (master_idx == -1)
2098d3432bf1SLahav Schlesinger return !!master;
2099d3432bf1SLahav Schlesinger
2100dc599f76SDavid Ahern if (!master || master->ifindex != master_idx)
2101dc599f76SDavid Ahern return true;
2102dc599f76SDavid Ahern
2103dc599f76SDavid Ahern return false;
2104dc599f76SDavid Ahern }
2105dc599f76SDavid Ahern
link_kind_filtered(const struct net_device * dev,const struct rtnl_link_ops * kind_ops)2106dc599f76SDavid Ahern static bool link_kind_filtered(const struct net_device *dev,
2107dc599f76SDavid Ahern const struct rtnl_link_ops *kind_ops)
2108dc599f76SDavid Ahern {
2109dc599f76SDavid Ahern if (kind_ops && dev->rtnl_link_ops != kind_ops)
2110dc599f76SDavid Ahern return true;
2111dc599f76SDavid Ahern
2112dc599f76SDavid Ahern return false;
2113dc599f76SDavid Ahern }
2114dc599f76SDavid Ahern
link_dump_filtered(struct net_device * dev,int master_idx,const struct rtnl_link_ops * kind_ops)2115dc599f76SDavid Ahern static bool link_dump_filtered(struct net_device *dev,
2116dc599f76SDavid Ahern int master_idx,
2117dc599f76SDavid Ahern const struct rtnl_link_ops *kind_ops)
2118dc599f76SDavid Ahern {
2119dc599f76SDavid Ahern if (link_master_filtered(dev, master_idx) ||
2120dc599f76SDavid Ahern link_kind_filtered(dev, kind_ops))
2121dc599f76SDavid Ahern return true;
2122dc599f76SDavid Ahern
2123dc599f76SDavid Ahern return false;
2124dc599f76SDavid Ahern }
2125dc599f76SDavid Ahern
2126c383edc4SChristian Brauner /**
2127c383edc4SChristian Brauner * rtnl_get_net_ns_capable - Get netns if sufficiently privileged.
2128c383edc4SChristian Brauner * @sk: netlink socket
2129c383edc4SChristian Brauner * @netnsid: network namespace identifier
2130c383edc4SChristian Brauner *
2131c383edc4SChristian Brauner * Returns the network namespace identified by netnsid on success or an error
2132c383edc4SChristian Brauner * pointer on failure.
2133c383edc4SChristian Brauner */
rtnl_get_net_ns_capable(struct sock * sk,int netnsid)2134c383edc4SChristian Brauner struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid)
213579e1ad14SJiri Benc {
213679e1ad14SJiri Benc struct net *net;
213779e1ad14SJiri Benc
2138f428fe4aSAndrei Vagin net = get_net_ns_by_id(sock_net(sk), netnsid);
213979e1ad14SJiri Benc if (!net)
214079e1ad14SJiri Benc return ERR_PTR(-EINVAL);
214179e1ad14SJiri Benc
214279e1ad14SJiri Benc /* For now, the caller is required to have CAP_NET_ADMIN in
214379e1ad14SJiri Benc * the user namespace owning the target net ns.
214479e1ad14SJiri Benc */
2145f428fe4aSAndrei Vagin if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) {
214679e1ad14SJiri Benc put_net(net);
214779e1ad14SJiri Benc return ERR_PTR(-EACCES);
214879e1ad14SJiri Benc }
214979e1ad14SJiri Benc return net;
215079e1ad14SJiri Benc }
2151c383edc4SChristian Brauner EXPORT_SYMBOL_GPL(rtnl_get_net_ns_capable);
215279e1ad14SJiri Benc
rtnl_valid_dump_ifinfo_req(const struct nlmsghdr * nlh,bool strict_check,struct nlattr ** tb,struct netlink_ext_ack * extack)2153905cf0abSDavid Ahern static int rtnl_valid_dump_ifinfo_req(const struct nlmsghdr *nlh,
2154905cf0abSDavid Ahern bool strict_check, struct nlattr **tb,
2155905cf0abSDavid Ahern struct netlink_ext_ack *extack)
2156905cf0abSDavid Ahern {
2157905cf0abSDavid Ahern int hdrlen;
2158905cf0abSDavid Ahern
2159905cf0abSDavid Ahern if (strict_check) {
2160905cf0abSDavid Ahern struct ifinfomsg *ifm;
2161905cf0abSDavid Ahern
2162905cf0abSDavid Ahern if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
2163905cf0abSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid header for link dump");
2164905cf0abSDavid Ahern return -EINVAL;
2165905cf0abSDavid Ahern }
2166905cf0abSDavid Ahern
2167905cf0abSDavid Ahern ifm = nlmsg_data(nlh);
2168905cf0abSDavid Ahern if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
2169905cf0abSDavid Ahern ifm->ifi_change) {
2170905cf0abSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid values in header for link dump request");
2171905cf0abSDavid Ahern return -EINVAL;
2172905cf0abSDavid Ahern }
2173905cf0abSDavid Ahern if (ifm->ifi_index) {
2174905cf0abSDavid Ahern NL_SET_ERR_MSG(extack, "Filter by device index not supported for link dumps");
2175905cf0abSDavid Ahern return -EINVAL;
2176905cf0abSDavid Ahern }
2177905cf0abSDavid Ahern
21788cb08174SJohannes Berg return nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb,
21798cb08174SJohannes Berg IFLA_MAX, ifla_policy,
21808cb08174SJohannes Berg extack);
2181905cf0abSDavid Ahern }
2182905cf0abSDavid Ahern
2183905cf0abSDavid Ahern /* A hack to preserve kernel<->userspace interface.
2184905cf0abSDavid Ahern * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
2185905cf0abSDavid Ahern * However, before Linux v3.9 the code here assumed rtgenmsg and that's
2186905cf0abSDavid Ahern * what iproute2 < v3.9.0 used.
2187905cf0abSDavid Ahern * We can detect the old iproute2. Even including the IFLA_EXT_MASK
2188905cf0abSDavid Ahern * attribute, its netlink message is shorter than struct ifinfomsg.
2189905cf0abSDavid Ahern */
2190905cf0abSDavid Ahern hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2191905cf0abSDavid Ahern sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2192905cf0abSDavid Ahern
21938cb08174SJohannes Berg return nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy,
21948cb08174SJohannes Berg extack);
2195905cf0abSDavid Ahern }
2196905cf0abSDavid Ahern
rtnl_dump_ifinfo(struct sk_buff * skb,struct netlink_callback * cb)2197b60c5115SThomas Graf static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
21981da177e4SLinus Torvalds {
2199905cf0abSDavid Ahern struct netlink_ext_ack *extack = cb->extack;
2200905cf0abSDavid Ahern const struct nlmsghdr *nlh = cb->nlh;
22013b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk);
220279e1ad14SJiri Benc struct net *tgt_net = net;
22037c28bd0bSEric Dumazet int h, s_h;
22047c28bd0bSEric Dumazet int idx = 0, s_idx;
22051da177e4SLinus Torvalds struct net_device *dev;
22067c28bd0bSEric Dumazet struct hlist_head *head;
2207115c9b81SGreg Rose struct nlattr *tb[IFLA_MAX+1];
2208115c9b81SGreg Rose u32 ext_filter_mask = 0;
2209dc599f76SDavid Ahern const struct rtnl_link_ops *kind_ops = NULL;
2210dc599f76SDavid Ahern unsigned int flags = NLM_F_MULTI;
2211dc599f76SDavid Ahern int master_idx = 0;
221279e1ad14SJiri Benc int netnsid = -1;
2213905cf0abSDavid Ahern int err, i;
22141da177e4SLinus Torvalds
22157c28bd0bSEric Dumazet s_h = cb->args[0];
22167c28bd0bSEric Dumazet s_idx = cb->args[1];
22177c28bd0bSEric Dumazet
2218905cf0abSDavid Ahern err = rtnl_valid_dump_ifinfo_req(nlh, cb->strict_check, tb, extack);
2219905cf0abSDavid Ahern if (err < 0) {
2220905cf0abSDavid Ahern if (cb->strict_check)
2221905cf0abSDavid Ahern return err;
2222e5eca6d4SMichal Schmidt
2223905cf0abSDavid Ahern goto walk_entries;
222479e1ad14SJiri Benc }
222579e1ad14SJiri Benc
2226905cf0abSDavid Ahern for (i = 0; i <= IFLA_MAX; ++i) {
2227905cf0abSDavid Ahern if (!tb[i])
2228905cf0abSDavid Ahern continue;
2229dc599f76SDavid Ahern
2230905cf0abSDavid Ahern /* new attributes should only be added with strict checking */
2231905cf0abSDavid Ahern switch (i) {
2232905cf0abSDavid Ahern case IFLA_TARGET_NETNSID:
2233905cf0abSDavid Ahern netnsid = nla_get_s32(tb[i]);
2234905cf0abSDavid Ahern tgt_net = rtnl_get_net_ns_capable(skb->sk, netnsid);
2235905cf0abSDavid Ahern if (IS_ERR(tgt_net)) {
2236905cf0abSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid target network namespace id");
2237905cf0abSDavid Ahern return PTR_ERR(tgt_net);
2238905cf0abSDavid Ahern }
2239905cf0abSDavid Ahern break;
2240905cf0abSDavid Ahern case IFLA_EXT_MASK:
2241905cf0abSDavid Ahern ext_filter_mask = nla_get_u32(tb[i]);
2242905cf0abSDavid Ahern break;
2243905cf0abSDavid Ahern case IFLA_MASTER:
2244905cf0abSDavid Ahern master_idx = nla_get_u32(tb[i]);
2245905cf0abSDavid Ahern break;
2246905cf0abSDavid Ahern case IFLA_LINKINFO:
2247905cf0abSDavid Ahern kind_ops = linkinfo_to_kind_ops(tb[i]);
2248905cf0abSDavid Ahern break;
2249905cf0abSDavid Ahern default:
2250905cf0abSDavid Ahern if (cb->strict_check) {
2251905cf0abSDavid Ahern NL_SET_ERR_MSG(extack, "Unsupported attribute in link dump request");
2252905cf0abSDavid Ahern return -EINVAL;
2253905cf0abSDavid Ahern }
2254905cf0abSDavid Ahern }
2255905cf0abSDavid Ahern }
2256dc599f76SDavid Ahern
2257dc599f76SDavid Ahern if (master_idx || kind_ops)
2258dc599f76SDavid Ahern flags |= NLM_F_DUMP_FILTERED;
2259115c9b81SGreg Rose
2260905cf0abSDavid Ahern walk_entries:
22617c28bd0bSEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
22627562f876SPavel Emelianov idx = 0;
226379e1ad14SJiri Benc head = &tgt_net->dev_index_head[h];
2264cac5e65eSEric Dumazet hlist_for_each_entry(dev, head, index_hlist) {
2265dc599f76SDavid Ahern if (link_dump_filtered(dev, master_idx, kind_ops))
22663f0ae05dSZhang Shengju goto cont;
22671da177e4SLinus Torvalds if (idx < s_idx)
22687562f876SPavel Emelianov goto cont;
226979e1ad14SJiri Benc err = rtnl_fill_ifinfo(skb, dev, net,
227079e1ad14SJiri Benc RTM_NEWLINK,
227115e47304SEric W. Biederman NETLINK_CB(cb->skb).portid,
2272905cf0abSDavid Ahern nlh->nlmsg_seq, 0, flags,
227338e01b30SNicolas Dichtel ext_filter_mask, 0, NULL, 0,
2274d4e4fdf9SGuillaume Nault netnsid, GFP_KERNEL);
2275973462bbSDavid Gibson
2276f6c5775fSDavid Ahern if (err < 0) {
2277f6c5775fSDavid Ahern if (likely(skb->len))
22787c28bd0bSEric Dumazet goto out;
22794e985adaSThomas Graf
2280f6c5775fSDavid Ahern goto out_err;
2281f6c5775fSDavid Ahern }
22827562f876SPavel Emelianov cont:
22837562f876SPavel Emelianov idx++;
22841da177e4SLinus Torvalds }
22857c28bd0bSEric Dumazet }
22867c28bd0bSEric Dumazet out:
2287f6c5775fSDavid Ahern err = skb->len;
2288f6c5775fSDavid Ahern out_err:
22897c28bd0bSEric Dumazet cb->args[1] = idx;
22907c28bd0bSEric Dumazet cb->args[0] = h;
2291a9ecb0cbSzhang kai cb->seq = tgt_net->dev_base_seq;
2292d0225784SJakub Sitnicki nl_dump_check_consistent(cb, nlmsg_hdr(skb));
229379e1ad14SJiri Benc if (netnsid >= 0)
229479e1ad14SJiri Benc put_net(tgt_net);
22951da177e4SLinus Torvalds
2296f6c5775fSDavid Ahern return err;
22971da177e4SLinus Torvalds }
22981da177e4SLinus Torvalds
rtnl_nla_parse_ifinfomsg(struct nlattr ** tb,const struct nlattr * nla_peer,struct netlink_ext_ack * exterr)2299f534f658SJakub Kicinski int rtnl_nla_parse_ifinfomsg(struct nlattr **tb, const struct nlattr *nla_peer,
2300fceb6435SJohannes Berg struct netlink_ext_ack *exterr)
2301f7b12606SJiri Pirko {
2302f534f658SJakub Kicinski const struct ifinfomsg *ifmp;
2303f534f658SJakub Kicinski const struct nlattr *attrs;
2304f534f658SJakub Kicinski size_t len;
2305f534f658SJakub Kicinski
2306f534f658SJakub Kicinski ifmp = nla_data(nla_peer);
2307f534f658SJakub Kicinski attrs = nla_data(nla_peer) + sizeof(struct ifinfomsg);
2308f534f658SJakub Kicinski len = nla_len(nla_peer) - sizeof(struct ifinfomsg);
2309f534f658SJakub Kicinski
2310f534f658SJakub Kicinski if (ifmp->ifi_index < 0) {
2311f534f658SJakub Kicinski NL_SET_ERR_MSG_ATTR(exterr, nla_peer,
2312f534f658SJakub Kicinski "ifindex can't be negative");
2313f534f658SJakub Kicinski return -EINVAL;
2314f534f658SJakub Kicinski }
2315f534f658SJakub Kicinski
2316f534f658SJakub Kicinski return nla_parse_deprecated(tb, IFLA_MAX, attrs, len, ifla_policy,
23178cb08174SJohannes Berg exterr);
2318f7b12606SJiri Pirko }
2319f534f658SJakub Kicinski EXPORT_SYMBOL(rtnl_nla_parse_ifinfomsg);
232057b61080SScott Feldman
rtnl_link_get_net(struct net * src_net,struct nlattr * tb[])232181adee47SEric W. Biederman struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
232281adee47SEric W. Biederman {
232381adee47SEric W. Biederman struct net *net;
232481adee47SEric W. Biederman /* Examine the link attributes and figure out which
232581adee47SEric W. Biederman * network namespace we are talking about.
232681adee47SEric W. Biederman */
232781adee47SEric W. Biederman if (tb[IFLA_NET_NS_PID])
232881adee47SEric W. Biederman net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
2329f0630529SEric W. Biederman else if (tb[IFLA_NET_NS_FD])
2330f0630529SEric W. Biederman net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
233181adee47SEric W. Biederman else
233281adee47SEric W. Biederman net = get_net(src_net);
233381adee47SEric W. Biederman return net;
233481adee47SEric W. Biederman }
233581adee47SEric W. Biederman EXPORT_SYMBOL(rtnl_link_get_net);
233681adee47SEric W. Biederman
23377c4f63baSChristian Brauner /* Figure out which network namespace we are talking about by
23387c4f63baSChristian Brauner * examining the link attributes in the following order:
23397c4f63baSChristian Brauner *
23407c4f63baSChristian Brauner * 1. IFLA_NET_NS_PID
23417c4f63baSChristian Brauner * 2. IFLA_NET_NS_FD
23427e4a8d5aSChristian Brauner * 3. IFLA_TARGET_NETNSID
23437c4f63baSChristian Brauner */
rtnl_link_get_net_by_nlattr(struct net * src_net,struct nlattr * tb[])23447c4f63baSChristian Brauner static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net,
23457c4f63baSChristian Brauner struct nlattr *tb[])
23467c4f63baSChristian Brauner {
23477c4f63baSChristian Brauner struct net *net;
23487c4f63baSChristian Brauner
23497c4f63baSChristian Brauner if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])
23507c4f63baSChristian Brauner return rtnl_link_get_net(src_net, tb);
23517c4f63baSChristian Brauner
23527e4a8d5aSChristian Brauner if (!tb[IFLA_TARGET_NETNSID])
23537c4f63baSChristian Brauner return get_net(src_net);
23547c4f63baSChristian Brauner
23557e4a8d5aSChristian Brauner net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_TARGET_NETNSID]));
23567c4f63baSChristian Brauner if (!net)
23577c4f63baSChristian Brauner return ERR_PTR(-EINVAL);
23587c4f63baSChristian Brauner
23597c4f63baSChristian Brauner return net;
23607c4f63baSChristian Brauner }
23617c4f63baSChristian Brauner
rtnl_link_get_net_capable(const struct sk_buff * skb,struct net * src_net,struct nlattr * tb[],int cap)23627c4f63baSChristian Brauner static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb,
23637c4f63baSChristian Brauner struct net *src_net,
23647c4f63baSChristian Brauner struct nlattr *tb[], int cap)
23657c4f63baSChristian Brauner {
23667c4f63baSChristian Brauner struct net *net;
23677c4f63baSChristian Brauner
23687c4f63baSChristian Brauner net = rtnl_link_get_net_by_nlattr(src_net, tb);
23697c4f63baSChristian Brauner if (IS_ERR(net))
23707c4f63baSChristian Brauner return net;
23717c4f63baSChristian Brauner
23727c4f63baSChristian Brauner if (!netlink_ns_capable(skb, net->user_ns, cap)) {
23737c4f63baSChristian Brauner put_net(net);
23747c4f63baSChristian Brauner return ERR_PTR(-EPERM);
23757c4f63baSChristian Brauner }
23767c4f63baSChristian Brauner
23777c4f63baSChristian Brauner return net;
23787c4f63baSChristian Brauner }
23797c4f63baSChristian Brauner
23804ff66caeSChristian Brauner /* Verify that rtnetlink requests do not pass additional properties
23814ff66caeSChristian Brauner * potentially referring to different network namespaces.
23824ff66caeSChristian Brauner */
rtnl_ensure_unique_netns(struct nlattr * tb[],struct netlink_ext_ack * extack,bool netns_id_only)23834ff66caeSChristian Brauner static int rtnl_ensure_unique_netns(struct nlattr *tb[],
23844ff66caeSChristian Brauner struct netlink_ext_ack *extack,
23854ff66caeSChristian Brauner bool netns_id_only)
23864ff66caeSChristian Brauner {
23874ff66caeSChristian Brauner
23884ff66caeSChristian Brauner if (netns_id_only) {
23894ff66caeSChristian Brauner if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD])
23904ff66caeSChristian Brauner return 0;
23914ff66caeSChristian Brauner
23924ff66caeSChristian Brauner NL_SET_ERR_MSG(extack, "specified netns attribute not supported");
23934ff66caeSChristian Brauner return -EOPNOTSUPP;
23944ff66caeSChristian Brauner }
23954ff66caeSChristian Brauner
23967e4a8d5aSChristian Brauner if (tb[IFLA_TARGET_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]))
23974ff66caeSChristian Brauner goto invalid_attr;
23984ff66caeSChristian Brauner
23997e4a8d5aSChristian Brauner if (tb[IFLA_NET_NS_PID] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_FD]))
24004ff66caeSChristian Brauner goto invalid_attr;
24014ff66caeSChristian Brauner
24027e4a8d5aSChristian Brauner if (tb[IFLA_NET_NS_FD] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_PID]))
24034ff66caeSChristian Brauner goto invalid_attr;
24044ff66caeSChristian Brauner
24054ff66caeSChristian Brauner return 0;
24064ff66caeSChristian Brauner
24074ff66caeSChristian Brauner invalid_attr:
24084ff66caeSChristian Brauner NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified");
24094ff66caeSChristian Brauner return -EINVAL;
24104ff66caeSChristian Brauner }
24114ff66caeSChristian Brauner
rtnl_set_vf_rate(struct net_device * dev,int vf,int min_tx_rate,int max_tx_rate)2412a14857c2SBin Chen static int rtnl_set_vf_rate(struct net_device *dev, int vf, int min_tx_rate,
2413a14857c2SBin Chen int max_tx_rate)
2414a14857c2SBin Chen {
2415a14857c2SBin Chen const struct net_device_ops *ops = dev->netdev_ops;
2416a14857c2SBin Chen
2417a14857c2SBin Chen if (!ops->ndo_set_vf_rate)
2418a14857c2SBin Chen return -EOPNOTSUPP;
2419a14857c2SBin Chen if (max_tx_rate && max_tx_rate < min_tx_rate)
2420a14857c2SBin Chen return -EINVAL;
2421a14857c2SBin Chen
2422a14857c2SBin Chen return ops->ndo_set_vf_rate(dev, vf, min_tx_rate, max_tx_rate);
2423a14857c2SBin Chen }
2424a14857c2SBin Chen
validate_linkmsg(struct net_device * dev,struct nlattr * tb[],struct netlink_ext_ack * extack)24258679c31eSRocco Yue static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[],
24268679c31eSRocco Yue struct netlink_ext_ack *extack)
24271840bb13SThomas Graf {
24281840bb13SThomas Graf if (tb[IFLA_ADDRESS] &&
24291840bb13SThomas Graf nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
24301840bb13SThomas Graf return -EINVAL;
24311840bb13SThomas Graf
24321840bb13SThomas Graf if (tb[IFLA_BROADCAST] &&
24331840bb13SThomas Graf nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
24341840bb13SThomas Graf return -EINVAL;
2435fef5b228SXin Long
2436fef5b228SXin Long if (tb[IFLA_GSO_MAX_SIZE] &&
2437fef5b228SXin Long nla_get_u32(tb[IFLA_GSO_MAX_SIZE]) > dev->tso_max_size) {
2438fef5b228SXin Long NL_SET_ERR_MSG(extack, "too big gso_max_size");
2439fef5b228SXin Long return -EINVAL;
2440fef5b228SXin Long }
2441fef5b228SXin Long
2442fef5b228SXin Long if (tb[IFLA_GSO_MAX_SEGS] &&
2443fef5b228SXin Long (nla_get_u32(tb[IFLA_GSO_MAX_SEGS]) > GSO_MAX_SEGS ||
2444fef5b228SXin Long nla_get_u32(tb[IFLA_GSO_MAX_SEGS]) > dev->tso_max_segs)) {
2445fef5b228SXin Long NL_SET_ERR_MSG(extack, "too big gso_max_segs");
2446fef5b228SXin Long return -EINVAL;
2447fef5b228SXin Long }
2448fef5b228SXin Long
244965d6914eSXin Long if (tb[IFLA_GRO_MAX_SIZE] &&
245065d6914eSXin Long nla_get_u32(tb[IFLA_GRO_MAX_SIZE]) > GRO_MAX_SIZE) {
245165d6914eSXin Long NL_SET_ERR_MSG(extack, "too big gro_max_size");
245265d6914eSXin Long return -EINVAL;
245365d6914eSXin Long }
245465d6914eSXin Long
2455fef5b228SXin Long if (tb[IFLA_GSO_IPV4_MAX_SIZE] &&
2456fef5b228SXin Long nla_get_u32(tb[IFLA_GSO_IPV4_MAX_SIZE]) > dev->tso_max_size) {
2457fef5b228SXin Long NL_SET_ERR_MSG(extack, "too big gso_ipv4_max_size");
2458fef5b228SXin Long return -EINVAL;
2459fef5b228SXin Long }
246065d6914eSXin Long
246165d6914eSXin Long if (tb[IFLA_GRO_IPV4_MAX_SIZE] &&
246265d6914eSXin Long nla_get_u32(tb[IFLA_GRO_IPV4_MAX_SIZE]) > GRO_MAX_SIZE) {
246365d6914eSXin Long NL_SET_ERR_MSG(extack, "too big gro_ipv4_max_size");
246465d6914eSXin Long return -EINVAL;
246565d6914eSXin Long }
24661840bb13SThomas Graf
2467cf7afbfeSThomas Graf if (tb[IFLA_AF_SPEC]) {
2468cf7afbfeSThomas Graf struct nlattr *af;
2469cf7afbfeSThomas Graf int rem, err;
2470cf7afbfeSThomas Graf
2471cf7afbfeSThomas Graf nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2472cf7afbfeSThomas Graf const struct rtnl_af_ops *af_ops;
2473cf7afbfeSThomas Graf
24745fa85a09SFlorian Westphal af_ops = rtnl_af_lookup(nla_type(af));
2475a100243dSCong Wang if (!af_ops)
2476cf7afbfeSThomas Graf return -EAFNOSUPPORT;
2477cf7afbfeSThomas Graf
2478a100243dSCong Wang if (!af_ops->set_link_af)
2479cf7afbfeSThomas Graf return -EOPNOTSUPP;
2480cf7afbfeSThomas Graf
2481cf7afbfeSThomas Graf if (af_ops->validate_link_af) {
24828679c31eSRocco Yue err = af_ops->validate_link_af(dev, af, extack);
2483a100243dSCong Wang if (err < 0)
2484cf7afbfeSThomas Graf return err;
2485cf7afbfeSThomas Graf }
2486cf7afbfeSThomas Graf }
2487cf7afbfeSThomas Graf }
2488cf7afbfeSThomas Graf
24891840bb13SThomas Graf return 0;
24901840bb13SThomas Graf }
24911840bb13SThomas Graf
handle_infiniband_guid(struct net_device * dev,struct ifla_vf_guid * ivt,int guid_type)2492cc8e27ccSEli Cohen static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
2493cc8e27ccSEli Cohen int guid_type)
2494cc8e27ccSEli Cohen {
2495cc8e27ccSEli Cohen const struct net_device_ops *ops = dev->netdev_ops;
2496cc8e27ccSEli Cohen
2497cc8e27ccSEli Cohen return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
2498cc8e27ccSEli Cohen }
2499cc8e27ccSEli Cohen
handle_vf_guid(struct net_device * dev,struct ifla_vf_guid * ivt,int guid_type)2500cc8e27ccSEli Cohen static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
2501cc8e27ccSEli Cohen {
2502cc8e27ccSEli Cohen if (dev->type != ARPHRD_INFINIBAND)
2503cc8e27ccSEli Cohen return -EOPNOTSUPP;
2504cc8e27ccSEli Cohen
2505cc8e27ccSEli Cohen return handle_infiniband_guid(dev, ivt, guid_type);
2506cc8e27ccSEli Cohen }
2507cc8e27ccSEli Cohen
do_setvfinfo(struct net_device * dev,struct nlattr ** tb)25084f7d2cdfSDaniel Borkmann static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
2509c02db8c6SChris Wright {
2510c02db8c6SChris Wright const struct net_device_ops *ops = dev->netdev_ops;
25114f7d2cdfSDaniel Borkmann int err = -EINVAL;
2512c02db8c6SChris Wright
25134f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_MAC]) {
25144f7d2cdfSDaniel Borkmann struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
25154f7d2cdfSDaniel Borkmann
2516ff08ddbaSDan Carpenter if (ivm->vf >= INT_MAX)
2517ff08ddbaSDan Carpenter return -EINVAL;
2518c02db8c6SChris Wright err = -EOPNOTSUPP;
2519c02db8c6SChris Wright if (ops->ndo_set_vf_mac)
2520c02db8c6SChris Wright err = ops->ndo_set_vf_mac(dev, ivm->vf,
2521c02db8c6SChris Wright ivm->mac);
25224f7d2cdfSDaniel Borkmann if (err < 0)
25234f7d2cdfSDaniel Borkmann return err;
2524c02db8c6SChris Wright }
25254f7d2cdfSDaniel Borkmann
25264f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_VLAN]) {
25274f7d2cdfSDaniel Borkmann struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
25284f7d2cdfSDaniel Borkmann
2529ff08ddbaSDan Carpenter if (ivv->vf >= INT_MAX)
2530ff08ddbaSDan Carpenter return -EINVAL;
2531c02db8c6SChris Wright err = -EOPNOTSUPP;
2532c02db8c6SChris Wright if (ops->ndo_set_vf_vlan)
25334f7d2cdfSDaniel Borkmann err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
253479aab093SMoshe Shemesh ivv->qos,
253579aab093SMoshe Shemesh htons(ETH_P_8021Q));
253679aab093SMoshe Shemesh if (err < 0)
253779aab093SMoshe Shemesh return err;
253879aab093SMoshe Shemesh }
253979aab093SMoshe Shemesh
254079aab093SMoshe Shemesh if (tb[IFLA_VF_VLAN_LIST]) {
254179aab093SMoshe Shemesh struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
254279aab093SMoshe Shemesh struct nlattr *attr;
254379aab093SMoshe Shemesh int rem, len = 0;
254479aab093SMoshe Shemesh
254579aab093SMoshe Shemesh err = -EOPNOTSUPP;
254679aab093SMoshe Shemesh if (!ops->ndo_set_vf_vlan)
254779aab093SMoshe Shemesh return err;
254879aab093SMoshe Shemesh
254979aab093SMoshe Shemesh nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
255079aab093SMoshe Shemesh if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
2551206003c7SRoded Zats nla_len(attr) < sizeof(struct ifla_vf_vlan_info)) {
255279aab093SMoshe Shemesh return -EINVAL;
255379aab093SMoshe Shemesh }
255479aab093SMoshe Shemesh if (len >= MAX_VLAN_LIST_LEN)
255579aab093SMoshe Shemesh return -EOPNOTSUPP;
255679aab093SMoshe Shemesh ivvl[len] = nla_data(attr);
255779aab093SMoshe Shemesh
255879aab093SMoshe Shemesh len++;
255979aab093SMoshe Shemesh }
2560fa34cd94SArnd Bergmann if (len == 0)
2561fa34cd94SArnd Bergmann return -EINVAL;
2562fa34cd94SArnd Bergmann
2563ff08ddbaSDan Carpenter if (ivvl[0]->vf >= INT_MAX)
2564ff08ddbaSDan Carpenter return -EINVAL;
256579aab093SMoshe Shemesh err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
256679aab093SMoshe Shemesh ivvl[0]->qos, ivvl[0]->vlan_proto);
25674f7d2cdfSDaniel Borkmann if (err < 0)
25684f7d2cdfSDaniel Borkmann return err;
2569c02db8c6SChris Wright }
25704f7d2cdfSDaniel Borkmann
25714f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_TX_RATE]) {
25724f7d2cdfSDaniel Borkmann struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
2573ed616689SSucheta Chakraborty struct ifla_vf_info ivf;
25744f7d2cdfSDaniel Borkmann
2575ff08ddbaSDan Carpenter if (ivt->vf >= INT_MAX)
2576ff08ddbaSDan Carpenter return -EINVAL;
2577c02db8c6SChris Wright err = -EOPNOTSUPP;
2578ed616689SSucheta Chakraborty if (ops->ndo_get_vf_config)
25794f7d2cdfSDaniel Borkmann err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
25804f7d2cdfSDaniel Borkmann if (err < 0)
25814f7d2cdfSDaniel Borkmann return err;
25824f7d2cdfSDaniel Borkmann
2583a14857c2SBin Chen err = rtnl_set_vf_rate(dev, ivt->vf,
2584a14857c2SBin Chen ivf.min_tx_rate, ivt->rate);
25854f7d2cdfSDaniel Borkmann if (err < 0)
25864f7d2cdfSDaniel Borkmann return err;
2587c02db8c6SChris Wright }
25884f7d2cdfSDaniel Borkmann
25894f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_RATE]) {
25904f7d2cdfSDaniel Borkmann struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
25914f7d2cdfSDaniel Borkmann
2592ff08ddbaSDan Carpenter if (ivt->vf >= INT_MAX)
2593ff08ddbaSDan Carpenter return -EINVAL;
2594a14857c2SBin Chen
2595a14857c2SBin Chen err = rtnl_set_vf_rate(dev, ivt->vf,
2596a14857c2SBin Chen ivt->min_tx_rate, ivt->max_tx_rate);
25974f7d2cdfSDaniel Borkmann if (err < 0)
25984f7d2cdfSDaniel Borkmann return err;
2599ed616689SSucheta Chakraborty }
26004f7d2cdfSDaniel Borkmann
26014f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_SPOOFCHK]) {
26024f7d2cdfSDaniel Borkmann struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
26034f7d2cdfSDaniel Borkmann
2604ff08ddbaSDan Carpenter if (ivs->vf >= INT_MAX)
2605ff08ddbaSDan Carpenter return -EINVAL;
26065f8444a3SGreg Rose err = -EOPNOTSUPP;
26075f8444a3SGreg Rose if (ops->ndo_set_vf_spoofchk)
26085f8444a3SGreg Rose err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
26095f8444a3SGreg Rose ivs->setting);
26104f7d2cdfSDaniel Borkmann if (err < 0)
26114f7d2cdfSDaniel Borkmann return err;
26125f8444a3SGreg Rose }
26134f7d2cdfSDaniel Borkmann
26144f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_LINK_STATE]) {
26154f7d2cdfSDaniel Borkmann struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
26164f7d2cdfSDaniel Borkmann
2617ff08ddbaSDan Carpenter if (ivl->vf >= INT_MAX)
2618ff08ddbaSDan Carpenter return -EINVAL;
26191d8faf48SRony Efraim err = -EOPNOTSUPP;
26201d8faf48SRony Efraim if (ops->ndo_set_vf_link_state)
26211d8faf48SRony Efraim err = ops->ndo_set_vf_link_state(dev, ivl->vf,
26221d8faf48SRony Efraim ivl->link_state);
26234f7d2cdfSDaniel Borkmann if (err < 0)
26244f7d2cdfSDaniel Borkmann return err;
26251d8faf48SRony Efraim }
26264f7d2cdfSDaniel Borkmann
26274f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_RSS_QUERY_EN]) {
262801a3d796SVlad Zolotarov struct ifla_vf_rss_query_en *ivrssq_en;
262901a3d796SVlad Zolotarov
263001a3d796SVlad Zolotarov err = -EOPNOTSUPP;
26314f7d2cdfSDaniel Borkmann ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
2632ff08ddbaSDan Carpenter if (ivrssq_en->vf >= INT_MAX)
2633ff08ddbaSDan Carpenter return -EINVAL;
263401a3d796SVlad Zolotarov if (ops->ndo_set_vf_rss_query_en)
26354f7d2cdfSDaniel Borkmann err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
263601a3d796SVlad Zolotarov ivrssq_en->setting);
26374f7d2cdfSDaniel Borkmann if (err < 0)
26384f7d2cdfSDaniel Borkmann return err;
263901a3d796SVlad Zolotarov }
26404f7d2cdfSDaniel Borkmann
2641dd461d6aSHiroshi Shimamoto if (tb[IFLA_VF_TRUST]) {
2642dd461d6aSHiroshi Shimamoto struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
2643dd461d6aSHiroshi Shimamoto
2644ff08ddbaSDan Carpenter if (ivt->vf >= INT_MAX)
2645ff08ddbaSDan Carpenter return -EINVAL;
2646dd461d6aSHiroshi Shimamoto err = -EOPNOTSUPP;
2647dd461d6aSHiroshi Shimamoto if (ops->ndo_set_vf_trust)
2648dd461d6aSHiroshi Shimamoto err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
2649dd461d6aSHiroshi Shimamoto if (err < 0)
2650dd461d6aSHiroshi Shimamoto return err;
2651dd461d6aSHiroshi Shimamoto }
2652dd461d6aSHiroshi Shimamoto
2653cc8e27ccSEli Cohen if (tb[IFLA_VF_IB_NODE_GUID]) {
2654cc8e27ccSEli Cohen struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
2655cc8e27ccSEli Cohen
2656ff08ddbaSDan Carpenter if (ivt->vf >= INT_MAX)
2657ff08ddbaSDan Carpenter return -EINVAL;
2658cc8e27ccSEli Cohen if (!ops->ndo_set_vf_guid)
2659cc8e27ccSEli Cohen return -EOPNOTSUPP;
2660cc8e27ccSEli Cohen return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
2661cc8e27ccSEli Cohen }
2662cc8e27ccSEli Cohen
2663cc8e27ccSEli Cohen if (tb[IFLA_VF_IB_PORT_GUID]) {
2664cc8e27ccSEli Cohen struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
2665cc8e27ccSEli Cohen
2666ff08ddbaSDan Carpenter if (ivt->vf >= INT_MAX)
2667ff08ddbaSDan Carpenter return -EINVAL;
2668cc8e27ccSEli Cohen if (!ops->ndo_set_vf_guid)
2669cc8e27ccSEli Cohen return -EOPNOTSUPP;
2670cc8e27ccSEli Cohen
2671cc8e27ccSEli Cohen return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
2672cc8e27ccSEli Cohen }
2673cc8e27ccSEli Cohen
2674c02db8c6SChris Wright return err;
2675c02db8c6SChris Wright }
2676c02db8c6SChris Wright
do_set_master(struct net_device * dev,int ifindex,struct netlink_ext_ack * extack)267733eaf2a6SDavid Ahern static int do_set_master(struct net_device *dev, int ifindex,
267833eaf2a6SDavid Ahern struct netlink_ext_ack *extack)
2679fbaec0eaSJiri Pirko {
2680898e5061SJiri Pirko struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
2681fbaec0eaSJiri Pirko const struct net_device_ops *ops;
2682fbaec0eaSJiri Pirko int err;
2683fbaec0eaSJiri Pirko
2684898e5061SJiri Pirko if (upper_dev) {
2685898e5061SJiri Pirko if (upper_dev->ifindex == ifindex)
2686fbaec0eaSJiri Pirko return 0;
2687898e5061SJiri Pirko ops = upper_dev->netdev_ops;
2688fbaec0eaSJiri Pirko if (ops->ndo_del_slave) {
2689898e5061SJiri Pirko err = ops->ndo_del_slave(upper_dev, dev);
2690fbaec0eaSJiri Pirko if (err)
2691fbaec0eaSJiri Pirko return err;
2692fbaec0eaSJiri Pirko } else {
2693fbaec0eaSJiri Pirko return -EOPNOTSUPP;
2694fbaec0eaSJiri Pirko }
2695fbaec0eaSJiri Pirko }
2696fbaec0eaSJiri Pirko
2697fbaec0eaSJiri Pirko if (ifindex) {
2698898e5061SJiri Pirko upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
2699898e5061SJiri Pirko if (!upper_dev)
2700fbaec0eaSJiri Pirko return -EINVAL;
2701898e5061SJiri Pirko ops = upper_dev->netdev_ops;
2702fbaec0eaSJiri Pirko if (ops->ndo_add_slave) {
270333eaf2a6SDavid Ahern err = ops->ndo_add_slave(upper_dev, dev, extack);
2704fbaec0eaSJiri Pirko if (err)
2705fbaec0eaSJiri Pirko return err;
2706fbaec0eaSJiri Pirko } else {
2707fbaec0eaSJiri Pirko return -EOPNOTSUPP;
2708fbaec0eaSJiri Pirko }
2709fbaec0eaSJiri Pirko }
2710fbaec0eaSJiri Pirko return 0;
2711fbaec0eaSJiri Pirko }
2712fbaec0eaSJiri Pirko
2713829eb208SRoopa Prabhu static const struct nla_policy ifla_proto_down_reason_policy[IFLA_PROTO_DOWN_REASON_VALUE + 1] = {
2714829eb208SRoopa Prabhu [IFLA_PROTO_DOWN_REASON_MASK] = { .type = NLA_U32 },
2715829eb208SRoopa Prabhu [IFLA_PROTO_DOWN_REASON_VALUE] = { .type = NLA_U32 },
2716829eb208SRoopa Prabhu };
2717829eb208SRoopa Prabhu
do_set_proto_down(struct net_device * dev,struct nlattr * nl_proto_down,struct nlattr * nl_proto_down_reason,struct netlink_ext_ack * extack)2718829eb208SRoopa Prabhu static int do_set_proto_down(struct net_device *dev,
2719829eb208SRoopa Prabhu struct nlattr *nl_proto_down,
2720829eb208SRoopa Prabhu struct nlattr *nl_proto_down_reason,
2721829eb208SRoopa Prabhu struct netlink_ext_ack *extack)
2722829eb208SRoopa Prabhu {
2723829eb208SRoopa Prabhu struct nlattr *pdreason[IFLA_PROTO_DOWN_REASON_MAX + 1];
2724829eb208SRoopa Prabhu unsigned long mask = 0;
2725829eb208SRoopa Prabhu u32 value;
2726829eb208SRoopa Prabhu bool proto_down;
2727829eb208SRoopa Prabhu int err;
2728829eb208SRoopa Prabhu
27292106efdaSJakub Kicinski if (!(dev->priv_flags & IFF_CHANGE_PROTO_DOWN)) {
2730829eb208SRoopa Prabhu NL_SET_ERR_MSG(extack, "Protodown not supported by device");
2731829eb208SRoopa Prabhu return -EOPNOTSUPP;
2732829eb208SRoopa Prabhu }
2733829eb208SRoopa Prabhu
2734829eb208SRoopa Prabhu if (nl_proto_down_reason) {
2735829eb208SRoopa Prabhu err = nla_parse_nested_deprecated(pdreason,
2736829eb208SRoopa Prabhu IFLA_PROTO_DOWN_REASON_MAX,
2737829eb208SRoopa Prabhu nl_proto_down_reason,
2738829eb208SRoopa Prabhu ifla_proto_down_reason_policy,
2739829eb208SRoopa Prabhu NULL);
2740829eb208SRoopa Prabhu if (err < 0)
2741829eb208SRoopa Prabhu return err;
2742829eb208SRoopa Prabhu
2743829eb208SRoopa Prabhu if (!pdreason[IFLA_PROTO_DOWN_REASON_VALUE]) {
2744829eb208SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid protodown reason value");
2745829eb208SRoopa Prabhu return -EINVAL;
2746829eb208SRoopa Prabhu }
2747829eb208SRoopa Prabhu
2748829eb208SRoopa Prabhu value = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_VALUE]);
2749829eb208SRoopa Prabhu
2750829eb208SRoopa Prabhu if (pdreason[IFLA_PROTO_DOWN_REASON_MASK])
2751829eb208SRoopa Prabhu mask = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_MASK]);
2752829eb208SRoopa Prabhu
2753829eb208SRoopa Prabhu dev_change_proto_down_reason(dev, mask, value);
2754829eb208SRoopa Prabhu }
2755829eb208SRoopa Prabhu
2756829eb208SRoopa Prabhu if (nl_proto_down) {
2757829eb208SRoopa Prabhu proto_down = nla_get_u8(nl_proto_down);
2758829eb208SRoopa Prabhu
2759d467d0bcSZheng Yongjun /* Don't turn off protodown if there are active reasons */
2760829eb208SRoopa Prabhu if (!proto_down && dev->proto_down_reason) {
2761829eb208SRoopa Prabhu NL_SET_ERR_MSG(extack, "Cannot clear protodown, active reasons");
2762829eb208SRoopa Prabhu return -EBUSY;
2763829eb208SRoopa Prabhu }
2764829eb208SRoopa Prabhu err = dev_change_proto_down(dev,
2765829eb208SRoopa Prabhu proto_down);
2766829eb208SRoopa Prabhu if (err)
2767829eb208SRoopa Prabhu return err;
2768829eb208SRoopa Prabhu }
2769829eb208SRoopa Prabhu
2770829eb208SRoopa Prabhu return 0;
2771829eb208SRoopa Prabhu }
2772829eb208SRoopa Prabhu
277390c325e3SNicolas Dichtel #define DO_SETLINK_MODIFIED 0x01
2774ba998906SNicolas Dichtel /* notify flag means notify + modified. */
2775ba998906SNicolas Dichtel #define DO_SETLINK_NOTIFY 0x03
do_setlink(const struct sk_buff * skb,struct net_device * dev,struct ifinfomsg * ifm,struct netlink_ext_ack * extack,struct nlattr ** tb,int status)277690f62cf3SEric W. Biederman static int do_setlink(const struct sk_buff *skb,
277790f62cf3SEric W. Biederman struct net_device *dev, struct ifinfomsg *ifm,
2778ddf9f970SJakub Kicinski struct netlink_ext_ack *extack,
27795ea08b52SFlorent Fourcot struct nlattr **tb, int status)
27800157f60cSPatrick McHardy {
2781d314774cSStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops;
27825ea08b52SFlorent Fourcot char ifname[IFNAMSIZ];
27830157f60cSPatrick McHardy int err;
27840157f60cSPatrick McHardy
27855ea08b52SFlorent Fourcot if (tb[IFLA_IFNAME])
27865ea08b52SFlorent Fourcot nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
27875ea08b52SFlorent Fourcot else
27885ea08b52SFlorent Fourcot ifname[0] = '\0';
27895ea08b52SFlorent Fourcot
27907e4a8d5aSChristian Brauner if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_TARGET_NETNSID]) {
27915ea08b52SFlorent Fourcot const char *pat = ifname[0] ? ifname : NULL;
2792eeb85a14SAndrei Vagin struct net *net;
2793eeb85a14SAndrei Vagin int new_ifindex;
2794eeb85a14SAndrei Vagin
2795eeb85a14SAndrei Vagin net = rtnl_link_get_net_capable(skb, dev_net(dev),
27967c4f63baSChristian Brauner tb, CAP_NET_ADMIN);
2797d8a5ec67SEric W. Biederman if (IS_ERR(net)) {
2798d8a5ec67SEric W. Biederman err = PTR_ERR(net);
2799d8a5ec67SEric W. Biederman goto errout;
2800d8a5ec67SEric W. Biederman }
28017c4f63baSChristian Brauner
2802eeb85a14SAndrei Vagin if (tb[IFLA_NEW_IFINDEX])
2803eeb85a14SAndrei Vagin new_ifindex = nla_get_s32(tb[IFLA_NEW_IFINDEX]);
2804eeb85a14SAndrei Vagin else
2805eeb85a14SAndrei Vagin new_ifindex = 0;
2806eeb85a14SAndrei Vagin
280796a6b93bSAndrey Ignatov err = __dev_change_net_namespace(dev, net, pat, new_ifindex);
2808d8a5ec67SEric W. Biederman put_net(net);
2809d8a5ec67SEric W. Biederman if (err)
2810d8a5ec67SEric W. Biederman goto errout;
281190c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED;
2812d8a5ec67SEric W. Biederman }
2813d8a5ec67SEric W. Biederman
28140157f60cSPatrick McHardy if (tb[IFLA_MAP]) {
28150157f60cSPatrick McHardy struct rtnl_link_ifmap *u_map;
28160157f60cSPatrick McHardy struct ifmap k_map;
28170157f60cSPatrick McHardy
2818d314774cSStephen Hemminger if (!ops->ndo_set_config) {
28190157f60cSPatrick McHardy err = -EOPNOTSUPP;
28200157f60cSPatrick McHardy goto errout;
28210157f60cSPatrick McHardy }
28220157f60cSPatrick McHardy
28230157f60cSPatrick McHardy if (!netif_device_present(dev)) {
28240157f60cSPatrick McHardy err = -ENODEV;
28250157f60cSPatrick McHardy goto errout;
28260157f60cSPatrick McHardy }
28270157f60cSPatrick McHardy
28280157f60cSPatrick McHardy u_map = nla_data(tb[IFLA_MAP]);
28290157f60cSPatrick McHardy k_map.mem_start = (unsigned long) u_map->mem_start;
28300157f60cSPatrick McHardy k_map.mem_end = (unsigned long) u_map->mem_end;
28310157f60cSPatrick McHardy k_map.base_addr = (unsigned short) u_map->base_addr;
28320157f60cSPatrick McHardy k_map.irq = (unsigned char) u_map->irq;
28330157f60cSPatrick McHardy k_map.dma = (unsigned char) u_map->dma;
28340157f60cSPatrick McHardy k_map.port = (unsigned char) u_map->port;
28350157f60cSPatrick McHardy
2836d314774cSStephen Hemminger err = ops->ndo_set_config(dev, &k_map);
28370157f60cSPatrick McHardy if (err < 0)
28380157f60cSPatrick McHardy goto errout;
28390157f60cSPatrick McHardy
2840ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY;
28410157f60cSPatrick McHardy }
28420157f60cSPatrick McHardy
28430157f60cSPatrick McHardy if (tb[IFLA_ADDRESS]) {
28440157f60cSPatrick McHardy struct sockaddr *sa;
28450157f60cSPatrick McHardy int len;
28460157f60cSPatrick McHardy
2847153711f9SWANG Cong len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
2848153711f9SWANG Cong sizeof(*sa));
28490157f60cSPatrick McHardy sa = kmalloc(len, GFP_KERNEL);
28500157f60cSPatrick McHardy if (!sa) {
28510157f60cSPatrick McHardy err = -ENOMEM;
28520157f60cSPatrick McHardy goto errout;
28530157f60cSPatrick McHardy }
28540157f60cSPatrick McHardy sa->sa_family = dev->type;
28550157f60cSPatrick McHardy memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
28560157f60cSPatrick McHardy dev->addr_len);
28573b23a32aSCong Wang err = dev_set_mac_address_user(dev, sa, extack);
28580157f60cSPatrick McHardy kfree(sa);
28590157f60cSPatrick McHardy if (err)
28600157f60cSPatrick McHardy goto errout;
286190c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED;
28620157f60cSPatrick McHardy }
28630157f60cSPatrick McHardy
28640157f60cSPatrick McHardy if (tb[IFLA_MTU]) {
28657a4c53beSStephen Hemminger err = dev_set_mtu_ext(dev, nla_get_u32(tb[IFLA_MTU]), extack);
28660157f60cSPatrick McHardy if (err < 0)
28670157f60cSPatrick McHardy goto errout;
286890c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED;
28690157f60cSPatrick McHardy }
28700157f60cSPatrick McHardy
2871cbda10faSVlad Dogaru if (tb[IFLA_GROUP]) {
2872cbda10faSVlad Dogaru dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
2873ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY;
2874cbda10faSVlad Dogaru }
2875cbda10faSVlad Dogaru
28760157f60cSPatrick McHardy /*
28770157f60cSPatrick McHardy * Interface selected by interface index but interface
28780157f60cSPatrick McHardy * name provided implies that a name change has been
28790157f60cSPatrick McHardy * requested.
28800157f60cSPatrick McHardy */
28810157f60cSPatrick McHardy if (ifm->ifi_index > 0 && ifname[0]) {
28820157f60cSPatrick McHardy err = dev_change_name(dev, ifname);
28830157f60cSPatrick McHardy if (err < 0)
28840157f60cSPatrick McHardy goto errout;
288590c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED;
28860157f60cSPatrick McHardy }
28870157f60cSPatrick McHardy
28880b815a1aSStephen Hemminger if (tb[IFLA_IFALIAS]) {
28890b815a1aSStephen Hemminger err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
28900b815a1aSStephen Hemminger nla_len(tb[IFLA_IFALIAS]));
28910b815a1aSStephen Hemminger if (err < 0)
28920b815a1aSStephen Hemminger goto errout;
2893ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY;
28940b815a1aSStephen Hemminger }
28950b815a1aSStephen Hemminger
28960157f60cSPatrick McHardy if (tb[IFLA_BROADCAST]) {
28970157f60cSPatrick McHardy nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
2898e7c3273eSJiri Pirko call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
28990157f60cSPatrick McHardy }
29000157f60cSPatrick McHardy
2901a4abfa62SPhil Sutter if (ifm->ifi_flags || ifm->ifi_change) {
2902a4abfa62SPhil Sutter err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
2903a4abfa62SPhil Sutter extack);
2904a4abfa62SPhil Sutter if (err < 0)
2905a4abfa62SPhil Sutter goto errout;
2906a4abfa62SPhil Sutter }
2907a4abfa62SPhil Sutter
290868325c8cSNicolas Dichtel if (tb[IFLA_MASTER]) {
290968325c8cSNicolas Dichtel err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
291068325c8cSNicolas Dichtel if (err)
291168325c8cSNicolas Dichtel goto errout;
291268325c8cSNicolas Dichtel status |= DO_SETLINK_MODIFIED;
291368325c8cSNicolas Dichtel }
291468325c8cSNicolas Dichtel
29159a57247fSJiri Pirko if (tb[IFLA_CARRIER]) {
29169a57247fSJiri Pirko err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
29179a57247fSJiri Pirko if (err)
29189a57247fSJiri Pirko goto errout;
291990c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED;
29209a57247fSJiri Pirko }
29219a57247fSJiri Pirko
29225d1180fcSNicolas Dichtel if (tb[IFLA_TXQLEN]) {
29230cd29503SAlexey Dobriyan unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
29245d1180fcSNicolas Dichtel
29256a643ddbSCong Wang err = dev_change_tx_queue_len(dev, value);
29266a643ddbSCong Wang if (err)
292708294a26SJason Wang goto errout;
29282d7f669bSXin Long status |= DO_SETLINK_MODIFIED;
292908294a26SJason Wang }
29300157f60cSPatrick McHardy
293146e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SIZE]) {
293246e6b992SStephen Hemminger u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]);
293346e6b992SStephen Hemminger
293446e6b992SStephen Hemminger if (dev->gso_max_size ^ max_size) {
293546e6b992SStephen Hemminger netif_set_gso_max_size(dev, max_size);
293646e6b992SStephen Hemminger status |= DO_SETLINK_MODIFIED;
293746e6b992SStephen Hemminger }
293846e6b992SStephen Hemminger }
293946e6b992SStephen Hemminger
294046e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SEGS]) {
294146e6b992SStephen Hemminger u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
294246e6b992SStephen Hemminger
294346e6b992SStephen Hemminger if (dev->gso_max_segs ^ max_segs) {
29446d872df3SEric Dumazet netif_set_gso_max_segs(dev, max_segs);
294546e6b992SStephen Hemminger status |= DO_SETLINK_MODIFIED;
294646e6b992SStephen Hemminger }
294746e6b992SStephen Hemminger }
294846e6b992SStephen Hemminger
2949eac1b93cSCoco Li if (tb[IFLA_GRO_MAX_SIZE]) {
2950eac1b93cSCoco Li u32 gro_max_size = nla_get_u32(tb[IFLA_GRO_MAX_SIZE]);
2951eac1b93cSCoco Li
2952eac1b93cSCoco Li if (dev->gro_max_size ^ gro_max_size) {
2953eac1b93cSCoco Li netif_set_gro_max_size(dev, gro_max_size);
2954eac1b93cSCoco Li status |= DO_SETLINK_MODIFIED;
2955eac1b93cSCoco Li }
2956eac1b93cSCoco Li }
2957eac1b93cSCoco Li
29589eefedd5SXin Long if (tb[IFLA_GSO_IPV4_MAX_SIZE]) {
29599eefedd5SXin Long u32 max_size = nla_get_u32(tb[IFLA_GSO_IPV4_MAX_SIZE]);
29609eefedd5SXin Long
29619eefedd5SXin Long if (dev->gso_ipv4_max_size ^ max_size) {
29629eefedd5SXin Long netif_set_gso_ipv4_max_size(dev, max_size);
29639eefedd5SXin Long status |= DO_SETLINK_MODIFIED;
29649eefedd5SXin Long }
29659eefedd5SXin Long }
29669eefedd5SXin Long
29679eefedd5SXin Long if (tb[IFLA_GRO_IPV4_MAX_SIZE]) {
29689eefedd5SXin Long u32 gro_max_size = nla_get_u32(tb[IFLA_GRO_IPV4_MAX_SIZE]);
29699eefedd5SXin Long
29709eefedd5SXin Long if (dev->gro_ipv4_max_size ^ gro_max_size) {
29719eefedd5SXin Long netif_set_gro_ipv4_max_size(dev, gro_max_size);
29729eefedd5SXin Long status |= DO_SETLINK_MODIFIED;
29739eefedd5SXin Long }
29749eefedd5SXin Long }
29759eefedd5SXin Long
29760157f60cSPatrick McHardy if (tb[IFLA_OPERSTATE])
297793b2d4a2SDavid S. Miller set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
29780157f60cSPatrick McHardy
29790157f60cSPatrick McHardy if (tb[IFLA_LINKMODE]) {
29801889b0e7SNicolas Dichtel unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
29811889b0e7SNicolas Dichtel
2982fd888e85SSebastian Andrzej Siewior write_lock(&dev_base_lock);
29831889b0e7SNicolas Dichtel if (dev->link_mode ^ value)
2984ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY;
29851889b0e7SNicolas Dichtel dev->link_mode = value;
2986fd888e85SSebastian Andrzej Siewior write_unlock(&dev_base_lock);
29870157f60cSPatrick McHardy }
29880157f60cSPatrick McHardy
2989c02db8c6SChris Wright if (tb[IFLA_VFINFO_LIST]) {
29904f7d2cdfSDaniel Borkmann struct nlattr *vfinfo[IFLA_VF_MAX + 1];
2991c02db8c6SChris Wright struct nlattr *attr;
2992c02db8c6SChris Wright int rem;
29934f7d2cdfSDaniel Borkmann
2994c02db8c6SChris Wright nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
29954f7d2cdfSDaniel Borkmann if (nla_type(attr) != IFLA_VF_INFO ||
29964f7d2cdfSDaniel Borkmann nla_len(attr) < NLA_HDRLEN) {
2997253683bbSDavid Howells err = -EINVAL;
2998c02db8c6SChris Wright goto errout;
2999253683bbSDavid Howells }
30008cb08174SJohannes Berg err = nla_parse_nested_deprecated(vfinfo, IFLA_VF_MAX,
30018cb08174SJohannes Berg attr,
30028cb08174SJohannes Berg ifla_vf_policy,
30038cb08174SJohannes Berg NULL);
30044f7d2cdfSDaniel Borkmann if (err < 0)
30054f7d2cdfSDaniel Borkmann goto errout;
30064f7d2cdfSDaniel Borkmann err = do_setvfinfo(dev, vfinfo);
3007ebc08a6fSWilliams, Mitch A if (err < 0)
3008ebc08a6fSWilliams, Mitch A goto errout;
3009ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY;
3010ebc08a6fSWilliams, Mitch A }
3011ebc08a6fSWilliams, Mitch A }
30120157f60cSPatrick McHardy err = 0;
30130157f60cSPatrick McHardy
301457b61080SScott Feldman if (tb[IFLA_VF_PORTS]) {
301557b61080SScott Feldman struct nlattr *port[IFLA_PORT_MAX+1];
301657b61080SScott Feldman struct nlattr *attr;
301757b61080SScott Feldman int vf;
301857b61080SScott Feldman int rem;
301957b61080SScott Feldman
302057b61080SScott Feldman err = -EOPNOTSUPP;
302157b61080SScott Feldman if (!ops->ndo_set_vf_port)
302257b61080SScott Feldman goto errout;
302357b61080SScott Feldman
302457b61080SScott Feldman nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
3025035d210fSDaniel Borkmann if (nla_type(attr) != IFLA_VF_PORT ||
3026035d210fSDaniel Borkmann nla_len(attr) < NLA_HDRLEN) {
3027035d210fSDaniel Borkmann err = -EINVAL;
3028035d210fSDaniel Borkmann goto errout;
3029035d210fSDaniel Borkmann }
30308cb08174SJohannes Berg err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
30318cb08174SJohannes Berg attr,
30328cb08174SJohannes Berg ifla_port_policy,
30338cb08174SJohannes Berg NULL);
303457b61080SScott Feldman if (err < 0)
303557b61080SScott Feldman goto errout;
303657b61080SScott Feldman if (!port[IFLA_PORT_VF]) {
303757b61080SScott Feldman err = -EOPNOTSUPP;
303857b61080SScott Feldman goto errout;
303957b61080SScott Feldman }
304057b61080SScott Feldman vf = nla_get_u32(port[IFLA_PORT_VF]);
304157b61080SScott Feldman err = ops->ndo_set_vf_port(dev, vf, port);
304257b61080SScott Feldman if (err < 0)
304357b61080SScott Feldman goto errout;
3044ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY;
304557b61080SScott Feldman }
304657b61080SScott Feldman }
304757b61080SScott Feldman err = 0;
304857b61080SScott Feldman
304957b61080SScott Feldman if (tb[IFLA_PORT_SELF]) {
305057b61080SScott Feldman struct nlattr *port[IFLA_PORT_MAX+1];
305157b61080SScott Feldman
30528cb08174SJohannes Berg err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
30538cb08174SJohannes Berg tb[IFLA_PORT_SELF],
30548cb08174SJohannes Berg ifla_port_policy, NULL);
305557b61080SScott Feldman if (err < 0)
305657b61080SScott Feldman goto errout;
305757b61080SScott Feldman
305857b61080SScott Feldman err = -EOPNOTSUPP;
305957b61080SScott Feldman if (ops->ndo_set_vf_port)
306057b61080SScott Feldman err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
306157b61080SScott Feldman if (err < 0)
306257b61080SScott Feldman goto errout;
3063ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY;
306457b61080SScott Feldman }
3065f8ff182cSThomas Graf
3066f8ff182cSThomas Graf if (tb[IFLA_AF_SPEC]) {
3067f8ff182cSThomas Graf struct nlattr *af;
3068f8ff182cSThomas Graf int rem;
3069f8ff182cSThomas Graf
3070f8ff182cSThomas Graf nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
3071f8ff182cSThomas Graf const struct rtnl_af_ops *af_ops;
3072f8ff182cSThomas Graf
3073058c8d59SGustavo A. R. Silva BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af))));
3074f8ff182cSThomas Graf
30753583a4e8SStephen Hemminger err = af_ops->set_link_af(dev, af, extack);
3076a100243dSCong Wang if (err < 0)
3077f8ff182cSThomas Graf goto errout;
3078f8ff182cSThomas Graf
3079ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY;
3080f8ff182cSThomas Graf }
3081f8ff182cSThomas Graf }
308257b61080SScott Feldman err = 0;
308357b61080SScott Feldman
3084829eb208SRoopa Prabhu if (tb[IFLA_PROTO_DOWN] || tb[IFLA_PROTO_DOWN_REASON]) {
3085829eb208SRoopa Prabhu err = do_set_proto_down(dev, tb[IFLA_PROTO_DOWN],
3086829eb208SRoopa Prabhu tb[IFLA_PROTO_DOWN_REASON], extack);
308788d6378bSAnuradha Karuppiah if (err)
308888d6378bSAnuradha Karuppiah goto errout;
308988d6378bSAnuradha Karuppiah status |= DO_SETLINK_NOTIFY;
309088d6378bSAnuradha Karuppiah }
309188d6378bSAnuradha Karuppiah
3092d1fdd913SBrenden Blanco if (tb[IFLA_XDP]) {
3093d1fdd913SBrenden Blanco struct nlattr *xdp[IFLA_XDP_MAX + 1];
309485de8576SDaniel Borkmann u32 xdp_flags = 0;
3095d1fdd913SBrenden Blanco
30968cb08174SJohannes Berg err = nla_parse_nested_deprecated(xdp, IFLA_XDP_MAX,
30978cb08174SJohannes Berg tb[IFLA_XDP],
3098fceb6435SJohannes Berg ifla_xdp_policy, NULL);
3099d1fdd913SBrenden Blanco if (err < 0)
3100d1fdd913SBrenden Blanco goto errout;
3101d1fdd913SBrenden Blanco
310258038695SMartin KaFai Lau if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
3103262d8625SBrenden Blanco err = -EINVAL;
3104262d8625SBrenden Blanco goto errout;
3105262d8625SBrenden Blanco }
310685de8576SDaniel Borkmann
310785de8576SDaniel Borkmann if (xdp[IFLA_XDP_FLAGS]) {
310885de8576SDaniel Borkmann xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
310985de8576SDaniel Borkmann if (xdp_flags & ~XDP_FLAGS_MASK) {
311085de8576SDaniel Borkmann err = -EINVAL;
311185de8576SDaniel Borkmann goto errout;
311285de8576SDaniel Borkmann }
3113ee5d032fSJakub Kicinski if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) {
31140489df9aSDaniel Borkmann err = -EINVAL;
31150489df9aSDaniel Borkmann goto errout;
31160489df9aSDaniel Borkmann }
311785de8576SDaniel Borkmann }
311885de8576SDaniel Borkmann
3119d1fdd913SBrenden Blanco if (xdp[IFLA_XDP_FD]) {
312092234c8fSToke Høiland-Jørgensen int expected_fd = -1;
312192234c8fSToke Høiland-Jørgensen
312292234c8fSToke Høiland-Jørgensen if (xdp_flags & XDP_FLAGS_REPLACE) {
312392234c8fSToke Høiland-Jørgensen if (!xdp[IFLA_XDP_EXPECTED_FD]) {
312492234c8fSToke Høiland-Jørgensen err = -EINVAL;
312592234c8fSToke Høiland-Jørgensen goto errout;
312692234c8fSToke Høiland-Jørgensen }
312792234c8fSToke Høiland-Jørgensen expected_fd =
312892234c8fSToke Høiland-Jørgensen nla_get_s32(xdp[IFLA_XDP_EXPECTED_FD]);
312992234c8fSToke Høiland-Jørgensen }
313092234c8fSToke Høiland-Jørgensen
3131ddf9f970SJakub Kicinski err = dev_change_xdp_fd(dev, extack,
313285de8576SDaniel Borkmann nla_get_s32(xdp[IFLA_XDP_FD]),
313392234c8fSToke Høiland-Jørgensen expected_fd,
313485de8576SDaniel Borkmann xdp_flags);
3135d1fdd913SBrenden Blanco if (err)
3136d1fdd913SBrenden Blanco goto errout;
3137d1fdd913SBrenden Blanco status |= DO_SETLINK_NOTIFY;
3138d1fdd913SBrenden Blanco }
3139d1fdd913SBrenden Blanco }
3140d1fdd913SBrenden Blanco
31410157f60cSPatrick McHardy errout:
3142ba998906SNicolas Dichtel if (status & DO_SETLINK_MODIFIED) {
314364ff90ccSXin Long if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY)
3144ba998906SNicolas Dichtel netdev_state_change(dev);
3145ba998906SNicolas Dichtel
3146ba998906SNicolas Dichtel if (err < 0)
3147e87cc472SJoe 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",
3148e87cc472SJoe Perches dev->name);
3149ba998906SNicolas Dichtel }
31500157f60cSPatrick McHardy
31510157f60cSPatrick McHardy return err;
31520157f60cSPatrick McHardy }
31530157f60cSPatrick McHardy
rtnl_dev_get(struct net * net,struct nlattr * tb[])3154cc6090e9SJiri Pirko static struct net_device *rtnl_dev_get(struct net *net,
31555ea08b52SFlorent Fourcot struct nlattr *tb[])
3156cc6090e9SJiri Pirko {
31575ea08b52SFlorent Fourcot char ifname[ALTIFNAMSIZ];
3158cc6090e9SJiri Pirko
31595ea08b52SFlorent Fourcot if (tb[IFLA_IFNAME])
31605ea08b52SFlorent Fourcot nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
31615ea08b52SFlorent Fourcot else if (tb[IFLA_ALT_IFNAME])
31625ea08b52SFlorent Fourcot nla_strscpy(ifname, tb[IFLA_ALT_IFNAME], ALTIFNAMSIZ);
3163cc6090e9SJiri Pirko else
3164cc6090e9SJiri Pirko return NULL;
3165cc6090e9SJiri Pirko
3166cc6090e9SJiri Pirko return __dev_get_by_name(net, ifname);
3167cc6090e9SJiri Pirko }
3168cc6090e9SJiri Pirko
rtnl_setlink(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)3169c21ef3e3SDavid Ahern static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3170c21ef3e3SDavid Ahern struct netlink_ext_ack *extack)
3171da5e0494SThomas Graf {
31723b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk);
3173da5e0494SThomas Graf struct ifinfomsg *ifm;
3174da5e0494SThomas Graf struct net_device *dev;
31750157f60cSPatrick McHardy int err;
3176da5e0494SThomas Graf struct nlattr *tb[IFLA_MAX+1];
31771da177e4SLinus Torvalds
31788cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
31798cb08174SJohannes Berg ifla_policy, extack);
3180da5e0494SThomas Graf if (err < 0)
3181da5e0494SThomas Graf goto errout;
31821da177e4SLinus Torvalds
31834ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, false);
31844ff66caeSChristian Brauner if (err < 0)
31854ff66caeSChristian Brauner goto errout;
31864ff66caeSChristian Brauner
31871da177e4SLinus Torvalds err = -EINVAL;
3188da5e0494SThomas Graf ifm = nlmsg_data(nlh);
318951055be8SPatrick McHardy if (ifm->ifi_index > 0)
3190a3d12891SEric Dumazet dev = __dev_get_by_index(net, ifm->ifi_index);
319176c9ac0eSJiri Pirko else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
31925ea08b52SFlorent Fourcot dev = rtnl_dev_get(net, tb);
3193da5e0494SThomas Graf else
3194da5e0494SThomas Graf goto errout;
31951da177e4SLinus Torvalds
3196da5e0494SThomas Graf if (dev == NULL) {
3197da5e0494SThomas Graf err = -ENODEV;
3198da5e0494SThomas Graf goto errout;
3199da5e0494SThomas Graf }
32001da177e4SLinus Torvalds
320189da780aSXin Long err = validate_linkmsg(dev, tb, extack);
320289da780aSXin Long if (err < 0)
320389da780aSXin Long goto errout;
320489da780aSXin Long
32055ea08b52SFlorent Fourcot err = do_setlink(skb, dev, ifm, extack, tb, 0);
3206da5e0494SThomas Graf errout:
32071da177e4SLinus Torvalds return err;
32081da177e4SLinus Torvalds }
32091da177e4SLinus Torvalds
rtnl_group_dellink(const struct net * net,int group)321066400d54SWANG Cong static int rtnl_group_dellink(const struct net *net, int group)
321166400d54SWANG Cong {
321266400d54SWANG Cong struct net_device *dev, *aux;
321366400d54SWANG Cong LIST_HEAD(list_kill);
321466400d54SWANG Cong bool found = false;
321566400d54SWANG Cong
321666400d54SWANG Cong if (!group)
321766400d54SWANG Cong return -EPERM;
321866400d54SWANG Cong
321966400d54SWANG Cong for_each_netdev(net, dev) {
322066400d54SWANG Cong if (dev->group == group) {
322166400d54SWANG Cong const struct rtnl_link_ops *ops;
322266400d54SWANG Cong
322366400d54SWANG Cong found = true;
322466400d54SWANG Cong ops = dev->rtnl_link_ops;
322566400d54SWANG Cong if (!ops || !ops->dellink)
322666400d54SWANG Cong return -EOPNOTSUPP;
322766400d54SWANG Cong }
322866400d54SWANG Cong }
322966400d54SWANG Cong
323066400d54SWANG Cong if (!found)
323166400d54SWANG Cong return -ENODEV;
323266400d54SWANG Cong
323366400d54SWANG Cong for_each_netdev_safe(net, dev, aux) {
323466400d54SWANG Cong if (dev->group == group) {
323566400d54SWANG Cong const struct rtnl_link_ops *ops;
323666400d54SWANG Cong
323766400d54SWANG Cong ops = dev->rtnl_link_ops;
323866400d54SWANG Cong ops->dellink(dev, &list_kill);
323966400d54SWANG Cong }
324066400d54SWANG Cong }
324166400d54SWANG Cong unregister_netdevice_many(&list_kill);
324266400d54SWANG Cong
324366400d54SWANG Cong return 0;
324466400d54SWANG Cong }
324566400d54SWANG Cong
rtnl_delete_link(struct net_device * dev,u32 portid,const struct nlmsghdr * nlh)3246f3a63cceSHangbin Liu int rtnl_delete_link(struct net_device *dev, u32 portid, const struct nlmsghdr *nlh)
3247614732eaSThomas Graf {
3248614732eaSThomas Graf const struct rtnl_link_ops *ops;
3249614732eaSThomas Graf LIST_HEAD(list_kill);
3250614732eaSThomas Graf
3251614732eaSThomas Graf ops = dev->rtnl_link_ops;
3252614732eaSThomas Graf if (!ops || !ops->dellink)
3253614732eaSThomas Graf return -EOPNOTSUPP;
3254614732eaSThomas Graf
3255614732eaSThomas Graf ops->dellink(dev, &list_kill);
3256f3a63cceSHangbin Liu unregister_netdevice_many_notify(&list_kill, portid, nlh);
3257614732eaSThomas Graf
3258614732eaSThomas Graf return 0;
3259614732eaSThomas Graf }
3260614732eaSThomas Graf EXPORT_SYMBOL_GPL(rtnl_delete_link);
3261614732eaSThomas Graf
rtnl_dellink(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)3262c21ef3e3SDavid Ahern static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
3263c21ef3e3SDavid Ahern struct netlink_ext_ack *extack)
326438f7b870SPatrick McHardy {
32653b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk);
3266f3a63cceSHangbin Liu u32 portid = NETLINK_CB(skb).portid;
3267b61ad68aSChristian Brauner struct net *tgt_net = net;
3268b61ad68aSChristian Brauner struct net_device *dev = NULL;
326938f7b870SPatrick McHardy struct ifinfomsg *ifm;
327038f7b870SPatrick McHardy struct nlattr *tb[IFLA_MAX+1];
327138f7b870SPatrick McHardy int err;
3272b61ad68aSChristian Brauner int netnsid = -1;
327338f7b870SPatrick McHardy
32748cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
32758cb08174SJohannes Berg ifla_policy, extack);
327638f7b870SPatrick McHardy if (err < 0)
327738f7b870SPatrick McHardy return err;
327838f7b870SPatrick McHardy
32794ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, true);
32804ff66caeSChristian Brauner if (err < 0)
32814ff66caeSChristian Brauner return err;
32824ff66caeSChristian Brauner
32837e4a8d5aSChristian Brauner if (tb[IFLA_TARGET_NETNSID]) {
32847e4a8d5aSChristian Brauner netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
3285c383edc4SChristian Brauner tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
3286b61ad68aSChristian Brauner if (IS_ERR(tgt_net))
3287b61ad68aSChristian Brauner return PTR_ERR(tgt_net);
3288b61ad68aSChristian Brauner }
3289b61ad68aSChristian Brauner
3290b61ad68aSChristian Brauner err = -EINVAL;
329138f7b870SPatrick McHardy ifm = nlmsg_data(nlh);
329238f7b870SPatrick McHardy if (ifm->ifi_index > 0)
3293b61ad68aSChristian Brauner dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
329476c9ac0eSJiri Pirko else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
32950db00726SKuniyuki Iwashima dev = rtnl_dev_get(tgt_net, tb);
329666400d54SWANG Cong else if (tb[IFLA_GROUP])
3297b61ad68aSChristian Brauner err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP]));
329838f7b870SPatrick McHardy else
3299b61ad68aSChristian Brauner goto out;
330038f7b870SPatrick McHardy
3301b61ad68aSChristian Brauner if (!dev) {
3302dee04163SFlorent Fourcot if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME] || ifm->ifi_index > 0)
3303b61ad68aSChristian Brauner err = -ENODEV;
330438f7b870SPatrick McHardy
3305b61ad68aSChristian Brauner goto out;
3306b61ad68aSChristian Brauner }
3307b61ad68aSChristian Brauner
3308f3a63cceSHangbin Liu err = rtnl_delete_link(dev, portid, nlh);
3309b61ad68aSChristian Brauner
3310b61ad68aSChristian Brauner out:
3311b61ad68aSChristian Brauner if (netnsid >= 0)
3312b61ad68aSChristian Brauner put_net(tgt_net);
3313b61ad68aSChristian Brauner
3314b61ad68aSChristian Brauner return err;
331538f7b870SPatrick McHardy }
331638f7b870SPatrick McHardy
rtnl_configure_link(struct net_device * dev,const struct ifinfomsg * ifm,u32 portid,const struct nlmsghdr * nlh)33171d997f10SHangbin Liu int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm,
33181d997f10SHangbin Liu u32 portid, const struct nlmsghdr *nlh)
33193729d502SPatrick McHardy {
33203729d502SPatrick McHardy unsigned int old_flags;
33213729d502SPatrick McHardy int err;
33223729d502SPatrick McHardy
33233729d502SPatrick McHardy old_flags = dev->flags;
33243729d502SPatrick McHardy if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
33256d040321SPetr Machata err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
33266d040321SPetr Machata NULL);
33273729d502SPatrick McHardy if (err < 0)
33283729d502SPatrick McHardy return err;
33293729d502SPatrick McHardy }
33303729d502SPatrick McHardy
33318d356b89SRoopa Prabhu if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) {
33321d997f10SHangbin Liu __dev_notify_flags(dev, old_flags, (old_flags ^ dev->flags), portid, nlh);
33338d356b89SRoopa Prabhu } else {
33343729d502SPatrick McHardy dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
33351d997f10SHangbin Liu __dev_notify_flags(dev, old_flags, ~0U, portid, nlh);
33368d356b89SRoopa Prabhu }
33373729d502SPatrick McHardy return 0;
33383729d502SPatrick McHardy }
33393729d502SPatrick McHardy EXPORT_SYMBOL(rtnl_configure_link);
33403729d502SPatrick McHardy
rtnl_create_link(struct net * net,const char * ifname,unsigned char name_assign_type,const struct rtnl_link_ops * ops,struct nlattr * tb[],struct netlink_ext_ack * extack)3341d0522f1cSDavid Ahern struct net_device *rtnl_create_link(struct net *net, const char *ifname,
3342d0522f1cSDavid Ahern unsigned char name_assign_type,
3343d0522f1cSDavid Ahern const struct rtnl_link_ops *ops,
3344d0522f1cSDavid Ahern struct nlattr *tb[],
3345d0522f1cSDavid Ahern struct netlink_ext_ack *extack)
3346e7199288SPavel Emelianov {
3347e7199288SPavel Emelianov struct net_device *dev;
3348d40156aaSJiri Pirko unsigned int num_tx_queues = 1;
3349d40156aaSJiri Pirko unsigned int num_rx_queues = 1;
3350b0ad3c17SXin Long int err;
3351e7199288SPavel Emelianov
335276ff5cc9SJiri Pirko if (tb[IFLA_NUM_TX_QUEUES])
335376ff5cc9SJiri Pirko num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
335476ff5cc9SJiri Pirko else if (ops->get_num_tx_queues)
3355d40156aaSJiri Pirko num_tx_queues = ops->get_num_tx_queues();
335676ff5cc9SJiri Pirko
335776ff5cc9SJiri Pirko if (tb[IFLA_NUM_RX_QUEUES])
335876ff5cc9SJiri Pirko num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
335976ff5cc9SJiri Pirko else if (ops->get_num_rx_queues)
3360d40156aaSJiri Pirko num_rx_queues = ops->get_num_rx_queues();
3361efacb309Sstephen hemminger
3362d0522f1cSDavid Ahern if (num_tx_queues < 1 || num_tx_queues > 4096) {
3363d0522f1cSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid number of transmit queues");
33640e1d6ecaSEric Dumazet return ERR_PTR(-EINVAL);
3365d0522f1cSDavid Ahern }
33660e1d6ecaSEric Dumazet
3367d0522f1cSDavid Ahern if (num_rx_queues < 1 || num_rx_queues > 4096) {
3368d0522f1cSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid number of receive queues");
33690e1d6ecaSEric Dumazet return ERR_PTR(-EINVAL);
3370d0522f1cSDavid Ahern }
33710e1d6ecaSEric Dumazet
33728c713dc9SJohannes Berg if (ops->alloc) {
33738c713dc9SJohannes Berg dev = ops->alloc(tb, ifname, name_assign_type,
33748c713dc9SJohannes Berg num_tx_queues, num_rx_queues);
33758c713dc9SJohannes Berg if (IS_ERR(dev))
33768c713dc9SJohannes Berg return dev;
33778c713dc9SJohannes Berg } else {
33788c713dc9SJohannes Berg dev = alloc_netdev_mqs(ops->priv_size, ifname,
33798c713dc9SJohannes Berg name_assign_type, ops->setup,
33808c713dc9SJohannes Berg num_tx_queues, num_rx_queues);
33818c713dc9SJohannes Berg }
33828c713dc9SJohannes Berg
3383e7199288SPavel Emelianov if (!dev)
3384d1892e4eSTobias Klauser return ERR_PTR(-ENOMEM);
3385e7199288SPavel Emelianov
3386b0ad3c17SXin Long err = validate_linkmsg(dev, tb, extack);
3387b0ad3c17SXin Long if (err < 0) {
3388b0ad3c17SXin Long free_netdev(dev);
3389b0ad3c17SXin Long return ERR_PTR(err);
3390b0ad3c17SXin Long }
3391b0ad3c17SXin Long
339281adee47SEric W. Biederman dev_net_set(dev, net);
339381adee47SEric W. Biederman dev->rtnl_link_ops = ops;
33943729d502SPatrick McHardy dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
339581adee47SEric W. Biederman
3396d836f5c6SEric Dumazet if (tb[IFLA_MTU]) {
3397d836f5c6SEric Dumazet u32 mtu = nla_get_u32(tb[IFLA_MTU]);
3398d836f5c6SEric Dumazet
3399d836f5c6SEric Dumazet err = dev_validate_mtu(dev, mtu, extack);
3400d836f5c6SEric Dumazet if (err) {
3401d836f5c6SEric Dumazet free_netdev(dev);
3402d836f5c6SEric Dumazet return ERR_PTR(err);
3403d836f5c6SEric Dumazet }
3404d836f5c6SEric Dumazet dev->mtu = mtu;
3405d836f5c6SEric Dumazet }
34062afb9b53SJiri Pirko if (tb[IFLA_ADDRESS]) {
3407efd38f75SJakub Kicinski __dev_addr_set(dev, nla_data(tb[IFLA_ADDRESS]),
3408e7199288SPavel Emelianov nla_len(tb[IFLA_ADDRESS]));
34092afb9b53SJiri Pirko dev->addr_assign_type = NET_ADDR_SET;
34102afb9b53SJiri Pirko }
3411e7199288SPavel Emelianov if (tb[IFLA_BROADCAST])
3412e7199288SPavel Emelianov memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
3413e7199288SPavel Emelianov nla_len(tb[IFLA_BROADCAST]));
3414e7199288SPavel Emelianov if (tb[IFLA_TXQLEN])
3415e7199288SPavel Emelianov dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
3416e7199288SPavel Emelianov if (tb[IFLA_OPERSTATE])
341793b2d4a2SDavid S. Miller set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
3418e7199288SPavel Emelianov if (tb[IFLA_LINKMODE])
3419e7199288SPavel Emelianov dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
3420ffa934f1SPatrick McHardy if (tb[IFLA_GROUP])
3421ffa934f1SPatrick McHardy dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
342246e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SIZE])
342346e6b992SStephen Hemminger netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE]));
342446e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SEGS])
34256d872df3SEric Dumazet netif_set_gso_max_segs(dev, nla_get_u32(tb[IFLA_GSO_MAX_SEGS]));
3426eac1b93cSCoco Li if (tb[IFLA_GRO_MAX_SIZE])
3427eac1b93cSCoco Li netif_set_gro_max_size(dev, nla_get_u32(tb[IFLA_GRO_MAX_SIZE]));
34289eefedd5SXin Long if (tb[IFLA_GSO_IPV4_MAX_SIZE])
34299eefedd5SXin Long netif_set_gso_ipv4_max_size(dev, nla_get_u32(tb[IFLA_GSO_IPV4_MAX_SIZE]));
34309eefedd5SXin Long if (tb[IFLA_GRO_IPV4_MAX_SIZE])
34319eefedd5SXin Long netif_set_gro_ipv4_max_size(dev, nla_get_u32(tb[IFLA_GRO_IPV4_MAX_SIZE]));
3432e7199288SPavel Emelianov
3433e7199288SPavel Emelianov return dev;
3434e7199288SPavel Emelianov }
3435e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_create_link);
3436e7199288SPavel Emelianov
rtnl_group_changelink(const struct sk_buff * skb,struct net * net,int group,struct ifinfomsg * ifm,struct netlink_ext_ack * extack,struct nlattr ** tb)343790f62cf3SEric W. Biederman static int rtnl_group_changelink(const struct sk_buff *skb,
343890f62cf3SEric W. Biederman struct net *net, int group,
3439e7ed828fSVlad Dogaru struct ifinfomsg *ifm,
3440ddf9f970SJakub Kicinski struct netlink_ext_ack *extack,
3441e7ed828fSVlad Dogaru struct nlattr **tb)
3442e7ed828fSVlad Dogaru {
3443d079535dSWANG Cong struct net_device *dev, *aux;
3444e7ed828fSVlad Dogaru int err;
3445e7ed828fSVlad Dogaru
3446d079535dSWANG Cong for_each_netdev_safe(net, dev, aux) {
3447e7ed828fSVlad Dogaru if (dev->group == group) {
344889da780aSXin Long err = validate_linkmsg(dev, tb, extack);
344989da780aSXin Long if (err < 0)
345089da780aSXin Long return err;
34515ea08b52SFlorent Fourcot err = do_setlink(skb, dev, ifm, extack, tb, 0);
3452e7ed828fSVlad Dogaru if (err < 0)
3453e7ed828fSVlad Dogaru return err;
3454e7ed828fSVlad Dogaru }
3455e7ed828fSVlad Dogaru }
3456e7ed828fSVlad Dogaru
3457e7ed828fSVlad Dogaru return 0;
3458e7ed828fSVlad Dogaru }
3459e7ed828fSVlad Dogaru
rtnl_newlink_create(struct sk_buff * skb,struct ifinfomsg * ifm,const struct rtnl_link_ops * ops,const struct nlmsghdr * nlh,struct nlattr ** tb,struct nlattr ** data,struct netlink_ext_ack * extack)346063105e83SJakub Kicinski static int rtnl_newlink_create(struct sk_buff *skb, struct ifinfomsg *ifm,
346163105e83SJakub Kicinski const struct rtnl_link_ops *ops,
3462d88e136cSHangbin Liu const struct nlmsghdr *nlh,
346363105e83SJakub Kicinski struct nlattr **tb, struct nlattr **data,
346402839cc8SJakub Kicinski struct netlink_ext_ack *extack)
346502839cc8SJakub Kicinski {
346602839cc8SJakub Kicinski unsigned char name_assign_type = NET_NAME_USER;
346702839cc8SJakub Kicinski struct net *net = sock_net(skb->sk);
3468d88e136cSHangbin Liu u32 portid = NETLINK_CB(skb).portid;
346902839cc8SJakub Kicinski struct net *dest_net, *link_net;
347002839cc8SJakub Kicinski struct net_device *dev;
347102839cc8SJakub Kicinski char ifname[IFNAMSIZ];
347202839cc8SJakub Kicinski int err;
347302839cc8SJakub Kicinski
347402839cc8SJakub Kicinski if (!ops->alloc && !ops->setup)
347502839cc8SJakub Kicinski return -EOPNOTSUPP;
347602839cc8SJakub Kicinski
347702839cc8SJakub Kicinski if (tb[IFLA_IFNAME]) {
347802839cc8SJakub Kicinski nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
347902839cc8SJakub Kicinski } else {
348002839cc8SJakub Kicinski snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
348102839cc8SJakub Kicinski name_assign_type = NET_NAME_ENUM;
348202839cc8SJakub Kicinski }
348302839cc8SJakub Kicinski
348402839cc8SJakub Kicinski dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN);
348502839cc8SJakub Kicinski if (IS_ERR(dest_net))
348602839cc8SJakub Kicinski return PTR_ERR(dest_net);
348702839cc8SJakub Kicinski
348802839cc8SJakub Kicinski if (tb[IFLA_LINK_NETNSID]) {
348902839cc8SJakub Kicinski int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
349002839cc8SJakub Kicinski
349102839cc8SJakub Kicinski link_net = get_net_ns_by_id(dest_net, id);
349202839cc8SJakub Kicinski if (!link_net) {
349302839cc8SJakub Kicinski NL_SET_ERR_MSG(extack, "Unknown network namespace id");
349402839cc8SJakub Kicinski err = -EINVAL;
349502839cc8SJakub Kicinski goto out;
349602839cc8SJakub Kicinski }
349702839cc8SJakub Kicinski err = -EPERM;
349802839cc8SJakub Kicinski if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
349902839cc8SJakub Kicinski goto out;
350002839cc8SJakub Kicinski } else {
350102839cc8SJakub Kicinski link_net = NULL;
350202839cc8SJakub Kicinski }
350302839cc8SJakub Kicinski
350402839cc8SJakub Kicinski dev = rtnl_create_link(link_net ? : dest_net, ifname,
350502839cc8SJakub Kicinski name_assign_type, ops, tb, extack);
350602839cc8SJakub Kicinski if (IS_ERR(dev)) {
350702839cc8SJakub Kicinski err = PTR_ERR(dev);
350802839cc8SJakub Kicinski goto out;
350902839cc8SJakub Kicinski }
351002839cc8SJakub Kicinski
351102839cc8SJakub Kicinski dev->ifindex = ifm->ifi_index;
351202839cc8SJakub Kicinski
351302839cc8SJakub Kicinski if (ops->newlink)
351402839cc8SJakub Kicinski err = ops->newlink(link_net ? : net, dev, tb, data, extack);
351502839cc8SJakub Kicinski else
351602839cc8SJakub Kicinski err = register_netdevice(dev);
351702839cc8SJakub Kicinski if (err < 0) {
351802839cc8SJakub Kicinski free_netdev(dev);
351902839cc8SJakub Kicinski goto out;
352002839cc8SJakub Kicinski }
352102839cc8SJakub Kicinski
3522d88e136cSHangbin Liu err = rtnl_configure_link(dev, ifm, portid, nlh);
352302839cc8SJakub Kicinski if (err < 0)
352402839cc8SJakub Kicinski goto out_unregister;
352502839cc8SJakub Kicinski if (link_net) {
352602839cc8SJakub Kicinski err = dev_change_net_namespace(dev, dest_net, ifname);
352702839cc8SJakub Kicinski if (err < 0)
352802839cc8SJakub Kicinski goto out_unregister;
352902839cc8SJakub Kicinski }
353002839cc8SJakub Kicinski if (tb[IFLA_MASTER]) {
353102839cc8SJakub Kicinski err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
353202839cc8SJakub Kicinski if (err)
353302839cc8SJakub Kicinski goto out_unregister;
353402839cc8SJakub Kicinski }
353502839cc8SJakub Kicinski out:
353602839cc8SJakub Kicinski if (link_net)
353702839cc8SJakub Kicinski put_net(link_net);
353802839cc8SJakub Kicinski put_net(dest_net);
353902839cc8SJakub Kicinski return err;
354002839cc8SJakub Kicinski out_unregister:
354102839cc8SJakub Kicinski if (ops->newlink) {
354202839cc8SJakub Kicinski LIST_HEAD(list_kill);
354302839cc8SJakub Kicinski
354402839cc8SJakub Kicinski ops->dellink(dev, &list_kill);
354502839cc8SJakub Kicinski unregister_netdevice_many(&list_kill);
354602839cc8SJakub Kicinski } else {
354702839cc8SJakub Kicinski unregister_netdevice(dev);
354802839cc8SJakub Kicinski }
354902839cc8SJakub Kicinski goto out;
355002839cc8SJakub Kicinski }
355163105e83SJakub Kicinski
3552c92bf26cSJakub Kicinski struct rtnl_newlink_tbs {
3553c92bf26cSJakub Kicinski struct nlattr *tb[IFLA_MAX + 1];
3554c92bf26cSJakub Kicinski struct nlattr *attr[RTNL_MAX_TYPE + 1];
3555420d0318SJakub Kicinski struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1];
3556c92bf26cSJakub Kicinski };
3557c92bf26cSJakub Kicinski
__rtnl_newlink(struct sk_buff * skb,struct nlmsghdr * nlh,struct rtnl_newlink_tbs * tbs,struct netlink_ext_ack * extack)3558c92bf26cSJakub Kicinski static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3559c92bf26cSJakub Kicinski struct rtnl_newlink_tbs *tbs,
3560c92bf26cSJakub Kicinski struct netlink_ext_ack *extack)
3561c92bf26cSJakub Kicinski {
3562420d0318SJakub Kicinski struct nlattr *linkinfo[IFLA_INFO_MAX + 1];
3563c92bf26cSJakub Kicinski struct nlattr ** const tb = tbs->tb;
3564c6f6f244SEric Dumazet const struct rtnl_link_ops *m_ops;
3565c6f6f244SEric Dumazet struct net_device *master_dev;
35663b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk);
356738f7b870SPatrick McHardy const struct rtnl_link_ops *ops;
3568420d0318SJakub Kicinski struct nlattr **slave_data;
3569420d0318SJakub Kicinski char kind[MODULE_NAME_LEN];
3570420d0318SJakub Kicinski struct net_device *dev;
3571420d0318SJakub Kicinski struct ifinfomsg *ifm;
3572420d0318SJakub Kicinski struct nlattr **data;
3573ef2a7c90SFlorent Fourcot bool link_specified;
357438f7b870SPatrick McHardy int err;
357538f7b870SPatrick McHardy
357695a5afcaSJohannes Berg #ifdef CONFIG_MODULES
357738f7b870SPatrick McHardy replay:
35788072f085SThomas Graf #endif
35798cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
35808cb08174SJohannes Berg ifla_policy, extack);
358138f7b870SPatrick McHardy if (err < 0)
358238f7b870SPatrick McHardy return err;
358338f7b870SPatrick McHardy
35844ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, false);
35854ff66caeSChristian Brauner if (err < 0)
35864ff66caeSChristian Brauner return err;
35874ff66caeSChristian Brauner
358838f7b870SPatrick McHardy ifm = nlmsg_data(nlh);
3589ef2a7c90SFlorent Fourcot if (ifm->ifi_index > 0) {
3590ef2a7c90SFlorent Fourcot link_specified = true;
3591881d966bSEric W. Biederman dev = __dev_get_by_index(net, ifm->ifi_index);
359230188bd7SIdo Schimmel } else if (ifm->ifi_index < 0) {
359330188bd7SIdo Schimmel NL_SET_ERR_MSG(extack, "ifindex can't be negative");
359430188bd7SIdo Schimmel return -EINVAL;
3595ef2a7c90SFlorent Fourcot } else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) {
3596ef2a7c90SFlorent Fourcot link_specified = true;
35975ea08b52SFlorent Fourcot dev = rtnl_dev_get(net, tb);
3598ef2a7c90SFlorent Fourcot } else {
3599ef2a7c90SFlorent Fourcot link_specified = false;
360038f7b870SPatrick McHardy dev = NULL;
3601ef2a7c90SFlorent Fourcot }
360238f7b870SPatrick McHardy
3603c6f6f244SEric Dumazet master_dev = NULL;
3604c6f6f244SEric Dumazet m_ops = NULL;
3605ba7d49b1SJiri Pirko if (dev) {
3606ba7d49b1SJiri Pirko master_dev = netdev_master_upper_dev_get(dev);
3607ba7d49b1SJiri Pirko if (master_dev)
3608ba7d49b1SJiri Pirko m_ops = master_dev->rtnl_link_ops;
3609ba7d49b1SJiri Pirko }
3610ba7d49b1SJiri Pirko
361138f7b870SPatrick McHardy if (tb[IFLA_LINKINFO]) {
36128cb08174SJohannes Berg err = nla_parse_nested_deprecated(linkinfo, IFLA_INFO_MAX,
36138cb08174SJohannes Berg tb[IFLA_LINKINFO],
36148cb08174SJohannes Berg ifla_info_policy, NULL);
361538f7b870SPatrick McHardy if (err < 0)
361638f7b870SPatrick McHardy return err;
361738f7b870SPatrick McHardy } else
361838f7b870SPatrick McHardy memset(linkinfo, 0, sizeof(linkinfo));
361938f7b870SPatrick McHardy
362038f7b870SPatrick McHardy if (linkinfo[IFLA_INFO_KIND]) {
3621872f6903SFrancis Laniel nla_strscpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
362238f7b870SPatrick McHardy ops = rtnl_link_ops_get(kind);
362338f7b870SPatrick McHardy } else {
362438f7b870SPatrick McHardy kind[0] = '\0';
362538f7b870SPatrick McHardy ops = NULL;
362638f7b870SPatrick McHardy }
362738f7b870SPatrick McHardy
3628420d0318SJakub Kicinski data = NULL;
362938f7b870SPatrick McHardy if (ops) {
3630ccf8dbcdSKees Cook if (ops->maxtype > RTNL_MAX_TYPE)
3631ccf8dbcdSKees Cook return -EINVAL;
3632ccf8dbcdSKees Cook
363338f7b870SPatrick McHardy if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
3634c92bf26cSJakub Kicinski err = nla_parse_nested_deprecated(tbs->attr, ops->maxtype,
363538f7b870SPatrick McHardy linkinfo[IFLA_INFO_DATA],
363668d57f3bSDavid Ahern ops->policy, extack);
363738f7b870SPatrick McHardy if (err < 0)
363838f7b870SPatrick McHardy return err;
3639c92bf26cSJakub Kicinski data = tbs->attr;
364038f7b870SPatrick McHardy }
364138f7b870SPatrick McHardy if (ops->validate) {
3642a8b8a889SMatthias Schiffer err = ops->validate(tb, data, extack);
364338f7b870SPatrick McHardy if (err < 0)
364438f7b870SPatrick McHardy return err;
364538f7b870SPatrick McHardy }
364638f7b870SPatrick McHardy }
364738f7b870SPatrick McHardy
3648420d0318SJakub Kicinski slave_data = NULL;
3649ba7d49b1SJiri Pirko if (m_ops) {
3650af066ed3SPrashant Bhole if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE)
3651ccf8dbcdSKees Cook return -EINVAL;
3652ccf8dbcdSKees Cook
3653ba7d49b1SJiri Pirko if (m_ops->slave_maxtype &&
3654ba7d49b1SJiri Pirko linkinfo[IFLA_INFO_SLAVE_DATA]) {
3655c92bf26cSJakub Kicinski err = nla_parse_nested_deprecated(tbs->slave_attr,
36568cb08174SJohannes Berg m_ops->slave_maxtype,
3657ba7d49b1SJiri Pirko linkinfo[IFLA_INFO_SLAVE_DATA],
36588cb08174SJohannes Berg m_ops->slave_policy,
36598cb08174SJohannes Berg extack);
3660ba7d49b1SJiri Pirko if (err < 0)
3661ba7d49b1SJiri Pirko return err;
3662c92bf26cSJakub Kicinski slave_data = tbs->slave_attr;
3663ba7d49b1SJiri Pirko }
3664ba7d49b1SJiri Pirko }
3665ba7d49b1SJiri Pirko
366638f7b870SPatrick McHardy if (dev) {
366790c325e3SNicolas Dichtel int status = 0;
366838f7b870SPatrick McHardy
366938f7b870SPatrick McHardy if (nlh->nlmsg_flags & NLM_F_EXCL)
367038f7b870SPatrick McHardy return -EEXIST;
367138f7b870SPatrick McHardy if (nlh->nlmsg_flags & NLM_F_REPLACE)
367238f7b870SPatrick McHardy return -EOPNOTSUPP;
367338f7b870SPatrick McHardy
367489da780aSXin Long err = validate_linkmsg(dev, tb, extack);
367589da780aSXin Long if (err < 0)
367689da780aSXin Long return err;
367789da780aSXin Long
367838f7b870SPatrick McHardy if (linkinfo[IFLA_INFO_DATA]) {
367938f7b870SPatrick McHardy if (!ops || ops != dev->rtnl_link_ops ||
368038f7b870SPatrick McHardy !ops->changelink)
368138f7b870SPatrick McHardy return -EOPNOTSUPP;
368238f7b870SPatrick McHardy
3683ad744b22SMatthias Schiffer err = ops->changelink(dev, tb, data, extack);
368438f7b870SPatrick McHardy if (err < 0)
368538f7b870SPatrick McHardy return err;
3686ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY;
368738f7b870SPatrick McHardy }
368838f7b870SPatrick McHardy
3689ba7d49b1SJiri Pirko if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
3690ba7d49b1SJiri Pirko if (!m_ops || !m_ops->slave_changelink)
3691ba7d49b1SJiri Pirko return -EOPNOTSUPP;
3692ba7d49b1SJiri Pirko
3693420d0318SJakub Kicinski err = m_ops->slave_changelink(master_dev, dev, tb,
3694420d0318SJakub Kicinski slave_data, extack);
3695ba7d49b1SJiri Pirko if (err < 0)
3696ba7d49b1SJiri Pirko return err;
3697ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY;
3698ba7d49b1SJiri Pirko }
3699ba7d49b1SJiri Pirko
37005ea08b52SFlorent Fourcot return do_setlink(skb, dev, ifm, extack, tb, status);
370138f7b870SPatrick McHardy }
370238f7b870SPatrick McHardy
3703ffa934f1SPatrick McHardy if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
3704ef2a7c90SFlorent Fourcot /* No dev found and NLM_F_CREATE not set. Requested dev does not exist,
3705ef2a7c90SFlorent Fourcot * or it's for a group
3706ef2a7c90SFlorent Fourcot */
3707ef2a7c90SFlorent Fourcot if (link_specified)
3708ef2a7c90SFlorent Fourcot return -ENODEV;
3709ef2a7c90SFlorent Fourcot if (tb[IFLA_GROUP])
371090f62cf3SEric W. Biederman return rtnl_group_changelink(skb, net,
3711ffa934f1SPatrick McHardy nla_get_u32(tb[IFLA_GROUP]),
3712ddf9f970SJakub Kicinski ifm, extack, tb);
37136f37c9f9SFlorent Fourcot return -ENODEV;
3714ffa934f1SPatrick McHardy }
371538f7b870SPatrick McHardy
3716160ca014STheuns Verwoerd if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
371738f7b870SPatrick McHardy return -EOPNOTSUPP;
371838f7b870SPatrick McHardy
371938f7b870SPatrick McHardy if (!ops) {
372095a5afcaSJohannes Berg #ifdef CONFIG_MODULES
372138f7b870SPatrick McHardy if (kind[0]) {
372238f7b870SPatrick McHardy __rtnl_unlock();
372338f7b870SPatrick McHardy request_module("rtnl-link-%s", kind);
372438f7b870SPatrick McHardy rtnl_lock();
372538f7b870SPatrick McHardy ops = rtnl_link_ops_get(kind);
372638f7b870SPatrick McHardy if (ops)
372738f7b870SPatrick McHardy goto replay;
372838f7b870SPatrick McHardy }
372938f7b870SPatrick McHardy #endif
373068d57f3bSDavid Ahern NL_SET_ERR_MSG(extack, "Unknown device type");
373138f7b870SPatrick McHardy return -EOPNOTSUPP;
373238f7b870SPatrick McHardy }
373338f7b870SPatrick McHardy
3734d88e136cSHangbin Liu return rtnl_newlink_create(skb, ifm, ops, nlh, tb, data, extack);
373563105e83SJakub Kicinski }
373663105e83SJakub Kicinski
rtnl_newlink(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)3737a2939745SJakub Kicinski static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3738a2939745SJakub Kicinski struct netlink_ext_ack *extack)
3739a2939745SJakub Kicinski {
3740c92bf26cSJakub Kicinski struct rtnl_newlink_tbs *tbs;
3741a2939745SJakub Kicinski int ret;
3742a2939745SJakub Kicinski
3743c92bf26cSJakub Kicinski tbs = kmalloc(sizeof(*tbs), GFP_KERNEL);
3744c92bf26cSJakub Kicinski if (!tbs)
3745a2939745SJakub Kicinski return -ENOMEM;
3746a2939745SJakub Kicinski
3747c92bf26cSJakub Kicinski ret = __rtnl_newlink(skb, nlh, tbs, extack);
3748c92bf26cSJakub Kicinski kfree(tbs);
3749a2939745SJakub Kicinski return ret;
3750a2939745SJakub Kicinski }
3751a2939745SJakub Kicinski
rtnl_valid_getlink_req(struct sk_buff * skb,const struct nlmsghdr * nlh,struct nlattr ** tb,struct netlink_ext_ack * extack)37529b3757b0SJakub Kicinski static int rtnl_valid_getlink_req(struct sk_buff *skb,
37539b3757b0SJakub Kicinski const struct nlmsghdr *nlh,
37549b3757b0SJakub Kicinski struct nlattr **tb,
37559b3757b0SJakub Kicinski struct netlink_ext_ack *extack)
37569b3757b0SJakub Kicinski {
37579b3757b0SJakub Kicinski struct ifinfomsg *ifm;
37589b3757b0SJakub Kicinski int i, err;
37599b3757b0SJakub Kicinski
37609b3757b0SJakub Kicinski if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
37619b3757b0SJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid header for get link");
37629b3757b0SJakub Kicinski return -EINVAL;
37639b3757b0SJakub Kicinski }
37649b3757b0SJakub Kicinski
37659b3757b0SJakub Kicinski if (!netlink_strict_get_check(skb))
37668cb08174SJohannes Berg return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
37678cb08174SJohannes Berg ifla_policy, extack);
37689b3757b0SJakub Kicinski
37699b3757b0SJakub Kicinski ifm = nlmsg_data(nlh);
37709b3757b0SJakub Kicinski if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
37719b3757b0SJakub Kicinski ifm->ifi_change) {
37729b3757b0SJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid values in header for get link request");
37739b3757b0SJakub Kicinski return -EINVAL;
37749b3757b0SJakub Kicinski }
37759b3757b0SJakub Kicinski
37768cb08174SJohannes Berg err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFLA_MAX,
37778cb08174SJohannes Berg ifla_policy, extack);
37789b3757b0SJakub Kicinski if (err)
37799b3757b0SJakub Kicinski return err;
37809b3757b0SJakub Kicinski
37819b3757b0SJakub Kicinski for (i = 0; i <= IFLA_MAX; i++) {
37829b3757b0SJakub Kicinski if (!tb[i])
37839b3757b0SJakub Kicinski continue;
37849b3757b0SJakub Kicinski
37859b3757b0SJakub Kicinski switch (i) {
37869b3757b0SJakub Kicinski case IFLA_IFNAME:
378776c9ac0eSJiri Pirko case IFLA_ALT_IFNAME:
37889b3757b0SJakub Kicinski case IFLA_EXT_MASK:
37899b3757b0SJakub Kicinski case IFLA_TARGET_NETNSID:
37909b3757b0SJakub Kicinski break;
37919b3757b0SJakub Kicinski default:
37929b3757b0SJakub Kicinski NL_SET_ERR_MSG(extack, "Unsupported attribute in get link request");
37939b3757b0SJakub Kicinski return -EINVAL;
37949b3757b0SJakub Kicinski }
37959b3757b0SJakub Kicinski }
37969b3757b0SJakub Kicinski
37979b3757b0SJakub Kicinski return 0;
37989b3757b0SJakub Kicinski }
37999b3757b0SJakub Kicinski
rtnl_getlink(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)3800c21ef3e3SDavid Ahern static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3801c21ef3e3SDavid Ahern struct netlink_ext_ack *extack)
3802711e2c33SJean Tourrilhes {
38033b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk);
380479e1ad14SJiri Benc struct net *tgt_net = net;
3805b60c5115SThomas Graf struct ifinfomsg *ifm;
3806b60c5115SThomas Graf struct nlattr *tb[IFLA_MAX+1];
3807b60c5115SThomas Graf struct net_device *dev = NULL;
3808b60c5115SThomas Graf struct sk_buff *nskb;
380979e1ad14SJiri Benc int netnsid = -1;
3810339bf98fSThomas Graf int err;
3811115c9b81SGreg Rose u32 ext_filter_mask = 0;
3812711e2c33SJean Tourrilhes
38139b3757b0SJakub Kicinski err = rtnl_valid_getlink_req(skb, nlh, tb, extack);
3814b60c5115SThomas Graf if (err < 0)
38159918f230SEric Sesterhenn return err;
3816b60c5115SThomas Graf
38174ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, true);
38184ff66caeSChristian Brauner if (err < 0)
38194ff66caeSChristian Brauner return err;
38204ff66caeSChristian Brauner
38217e4a8d5aSChristian Brauner if (tb[IFLA_TARGET_NETNSID]) {
38227e4a8d5aSChristian Brauner netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
3823c383edc4SChristian Brauner tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
382479e1ad14SJiri Benc if (IS_ERR(tgt_net))
382579e1ad14SJiri Benc return PTR_ERR(tgt_net);
382679e1ad14SJiri Benc }
382779e1ad14SJiri Benc
3828115c9b81SGreg Rose if (tb[IFLA_EXT_MASK])
3829115c9b81SGreg Rose ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3830115c9b81SGreg Rose
383179e1ad14SJiri Benc err = -EINVAL;
3832b60c5115SThomas Graf ifm = nlmsg_data(nlh);
3833a3d12891SEric Dumazet if (ifm->ifi_index > 0)
383479e1ad14SJiri Benc dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
383576c9ac0eSJiri Pirko else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
38365ea08b52SFlorent Fourcot dev = rtnl_dev_get(tgt_net, tb);
3837a3d12891SEric Dumazet else
383879e1ad14SJiri Benc goto out;
3839b60c5115SThomas Graf
384079e1ad14SJiri Benc err = -ENODEV;
3841a3d12891SEric Dumazet if (dev == NULL)
384279e1ad14SJiri Benc goto out;
3843a3d12891SEric Dumazet
384479e1ad14SJiri Benc err = -ENOBUFS;
3845115c9b81SGreg Rose nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
3846a3d12891SEric Dumazet if (nskb == NULL)
384779e1ad14SJiri Benc goto out;
3848711e2c33SJean Tourrilhes
384979e1ad14SJiri Benc err = rtnl_fill_ifinfo(nskb, dev, net,
385079e1ad14SJiri Benc RTM_NEWLINK, NETLINK_CB(skb).portid,
385179e1ad14SJiri Benc nlh->nlmsg_seq, 0, 0, ext_filter_mask,
3852d4e4fdf9SGuillaume Nault 0, NULL, 0, netnsid, GFP_KERNEL);
385326932566SPatrick McHardy if (err < 0) {
385426932566SPatrick McHardy /* -EMSGSIZE implies BUG in if_nlmsg_size */
385526932566SPatrick McHardy WARN_ON(err == -EMSGSIZE);
385626932566SPatrick McHardy kfree_skb(nskb);
3857a3d12891SEric Dumazet } else
385815e47304SEric W. Biederman err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
385979e1ad14SJiri Benc out:
386079e1ad14SJiri Benc if (netnsid >= 0)
386179e1ad14SJiri Benc put_net(tgt_net);
3862b60c5115SThomas Graf
3863711e2c33SJean Tourrilhes return err;
3864711e2c33SJean Tourrilhes }
3865711e2c33SJean Tourrilhes
rtnl_alt_ifname(int cmd,struct net_device * dev,struct nlattr * attr,bool * changed,struct netlink_ext_ack * extack)386636fbf1e5SJiri Pirko static int rtnl_alt_ifname(int cmd, struct net_device *dev, struct nlattr *attr,
386736fbf1e5SJiri Pirko bool *changed, struct netlink_ext_ack *extack)
386836fbf1e5SJiri Pirko {
386936fbf1e5SJiri Pirko char *alt_ifname;
3870155fb43bSJakub Kicinski size_t size;
387136fbf1e5SJiri Pirko int err;
387236fbf1e5SJiri Pirko
387336fbf1e5SJiri Pirko err = nla_validate(attr, attr->nla_len, IFLA_MAX, ifla_policy, extack);
387436fbf1e5SJiri Pirko if (err)
387536fbf1e5SJiri Pirko return err;
387636fbf1e5SJiri Pirko
3877155fb43bSJakub Kicinski if (cmd == RTM_NEWLINKPROP) {
3878155fb43bSJakub Kicinski size = rtnl_prop_list_size(dev);
3879155fb43bSJakub Kicinski size += nla_total_size(ALTIFNAMSIZ);
3880155fb43bSJakub Kicinski if (size >= U16_MAX) {
3881155fb43bSJakub Kicinski NL_SET_ERR_MSG(extack,
3882155fb43bSJakub Kicinski "effective property list too long");
3883155fb43bSJakub Kicinski return -EINVAL;
3884155fb43bSJakub Kicinski }
3885155fb43bSJakub Kicinski }
3886155fb43bSJakub Kicinski
38875d26cff5SJakub Kicinski alt_ifname = nla_strdup(attr, GFP_KERNEL_ACCOUNT);
388836fbf1e5SJiri Pirko if (!alt_ifname)
388936fbf1e5SJiri Pirko return -ENOMEM;
389044bfa9c5SEric Dumazet
389144bfa9c5SEric Dumazet if (cmd == RTM_NEWLINKPROP) {
389236fbf1e5SJiri Pirko err = netdev_name_node_alt_create(dev, alt_ifname);
389344bfa9c5SEric Dumazet if (!err)
389444bfa9c5SEric Dumazet alt_ifname = NULL;
389536fbf1e5SJiri Pirko } else if (cmd == RTM_DELLINKPROP) {
389636fbf1e5SJiri Pirko err = netdev_name_node_alt_destroy(dev, alt_ifname);
389736fbf1e5SJiri Pirko } else {
389844bfa9c5SEric Dumazet WARN_ON_ONCE(1);
389944bfa9c5SEric Dumazet err = -EINVAL;
390036fbf1e5SJiri Pirko }
390136fbf1e5SJiri Pirko
390244bfa9c5SEric Dumazet kfree(alt_ifname);
390344bfa9c5SEric Dumazet if (!err)
390436fbf1e5SJiri Pirko *changed = true;
390544bfa9c5SEric Dumazet return err;
390636fbf1e5SJiri Pirko }
390736fbf1e5SJiri Pirko
rtnl_linkprop(int cmd,struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)390836fbf1e5SJiri Pirko static int rtnl_linkprop(int cmd, struct sk_buff *skb, struct nlmsghdr *nlh,
390936fbf1e5SJiri Pirko struct netlink_ext_ack *extack)
391036fbf1e5SJiri Pirko {
391136fbf1e5SJiri Pirko struct net *net = sock_net(skb->sk);
391236fbf1e5SJiri Pirko struct nlattr *tb[IFLA_MAX + 1];
391336fbf1e5SJiri Pirko struct net_device *dev;
391436fbf1e5SJiri Pirko struct ifinfomsg *ifm;
391536fbf1e5SJiri Pirko bool changed = false;
391636fbf1e5SJiri Pirko struct nlattr *attr;
391736fbf1e5SJiri Pirko int err, rem;
391836fbf1e5SJiri Pirko
391936fbf1e5SJiri Pirko err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
392036fbf1e5SJiri Pirko if (err)
392136fbf1e5SJiri Pirko return err;
392236fbf1e5SJiri Pirko
392336fbf1e5SJiri Pirko err = rtnl_ensure_unique_netns(tb, extack, true);
392436fbf1e5SJiri Pirko if (err)
392536fbf1e5SJiri Pirko return err;
392636fbf1e5SJiri Pirko
392736fbf1e5SJiri Pirko ifm = nlmsg_data(nlh);
3928cc6090e9SJiri Pirko if (ifm->ifi_index > 0)
392936fbf1e5SJiri Pirko dev = __dev_get_by_index(net, ifm->ifi_index);
393076c9ac0eSJiri Pirko else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
39315ea08b52SFlorent Fourcot dev = rtnl_dev_get(net, tb);
3932cc6090e9SJiri Pirko else
393336fbf1e5SJiri Pirko return -EINVAL;
393436fbf1e5SJiri Pirko
393536fbf1e5SJiri Pirko if (!dev)
393636fbf1e5SJiri Pirko return -ENODEV;
393736fbf1e5SJiri Pirko
393836fbf1e5SJiri Pirko if (!tb[IFLA_PROP_LIST])
393936fbf1e5SJiri Pirko return 0;
394036fbf1e5SJiri Pirko
394136fbf1e5SJiri Pirko nla_for_each_nested(attr, tb[IFLA_PROP_LIST], rem) {
394236fbf1e5SJiri Pirko switch (nla_type(attr)) {
394336fbf1e5SJiri Pirko case IFLA_ALT_IFNAME:
394436fbf1e5SJiri Pirko err = rtnl_alt_ifname(cmd, dev, attr, &changed, extack);
394536fbf1e5SJiri Pirko if (err)
394636fbf1e5SJiri Pirko return err;
394736fbf1e5SJiri Pirko break;
394836fbf1e5SJiri Pirko }
394936fbf1e5SJiri Pirko }
395036fbf1e5SJiri Pirko
395136fbf1e5SJiri Pirko if (changed)
395236fbf1e5SJiri Pirko netdev_state_change(dev);
395336fbf1e5SJiri Pirko return 0;
395436fbf1e5SJiri Pirko }
395536fbf1e5SJiri Pirko
rtnl_newlinkprop(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)395636fbf1e5SJiri Pirko static int rtnl_newlinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
395736fbf1e5SJiri Pirko struct netlink_ext_ack *extack)
395836fbf1e5SJiri Pirko {
395936fbf1e5SJiri Pirko return rtnl_linkprop(RTM_NEWLINKPROP, skb, nlh, extack);
396036fbf1e5SJiri Pirko }
396136fbf1e5SJiri Pirko
rtnl_dellinkprop(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)396236fbf1e5SJiri Pirko static int rtnl_dellinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
396336fbf1e5SJiri Pirko struct netlink_ext_ack *extack)
396436fbf1e5SJiri Pirko {
396536fbf1e5SJiri Pirko return rtnl_linkprop(RTM_DELLINKPROP, skb, nlh, extack);
396636fbf1e5SJiri Pirko }
396736fbf1e5SJiri Pirko
rtnl_calcit(struct sk_buff * skb,struct nlmsghdr * nlh)3968ebfe3c51SDi Zhu static u32 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
3969c7ac8679SGreg Rose {
3970115c9b81SGreg Rose struct net *net = sock_net(skb->sk);
3971ebfe3c51SDi Zhu size_t min_ifinfo_dump_size = 0;
3972115c9b81SGreg Rose struct nlattr *tb[IFLA_MAX+1];
3973115c9b81SGreg Rose u32 ext_filter_mask = 0;
3974ebfe3c51SDi Zhu struct net_device *dev;
3975e5eca6d4SMichal Schmidt int hdrlen;
3976115c9b81SGreg Rose
3977e5eca6d4SMichal Schmidt /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
3978e5eca6d4SMichal Schmidt hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
3979e5eca6d4SMichal Schmidt sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
3980e5eca6d4SMichal Schmidt
39818cb08174SJohannes Berg if (nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
3982115c9b81SGreg Rose if (tb[IFLA_EXT_MASK])
3983115c9b81SGreg Rose ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3984a4b64fbeSEric Dumazet }
3985115c9b81SGreg Rose
3986115c9b81SGreg Rose if (!ext_filter_mask)
3987115c9b81SGreg Rose return NLMSG_GOODSIZE;
3988115c9b81SGreg Rose /*
3989115c9b81SGreg Rose * traverse the list of net devices and compute the minimum
3990115c9b81SGreg Rose * buffer size based upon the filter mask.
3991115c9b81SGreg Rose */
39926853dd48SFlorian Westphal rcu_read_lock();
39936853dd48SFlorian Westphal for_each_netdev_rcu(net, dev) {
3994ebfe3c51SDi Zhu min_ifinfo_dump_size = max(min_ifinfo_dump_size,
3995ebfe3c51SDi Zhu if_nlmsg_size(dev, ext_filter_mask));
3996115c9b81SGreg Rose }
39976853dd48SFlorian Westphal rcu_read_unlock();
3998115c9b81SGreg Rose
399993af2056SZhang Shengju return nlmsg_total_size(min_ifinfo_dump_size);
4000c7ac8679SGreg Rose }
4001c7ac8679SGreg Rose
rtnl_dump_all(struct sk_buff * skb,struct netlink_callback * cb)400242bad1daSAdrian Bunk static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
40031da177e4SLinus Torvalds {
40041da177e4SLinus Torvalds int idx;
40051da177e4SLinus Torvalds int s_idx = cb->family;
400687ccbb1fSChristian Brauner int type = cb->nlh->nlmsg_type - RTM_BASE;
4007c63586dcSDavid Ahern int ret = 0;
40081da177e4SLinus Torvalds
40091da177e4SLinus Torvalds if (s_idx == 0)
40101da177e4SLinus Torvalds s_idx = 1;
40116853dd48SFlorian Westphal
401225239ceeSPatrick McHardy for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
401351e13685SJakub Kicinski struct rtnl_link __rcu **tab;
4014addf9b90SFlorian Westphal struct rtnl_link *link;
40156853dd48SFlorian Westphal rtnl_dumpit_func dumpit;
40166853dd48SFlorian Westphal
40171da177e4SLinus Torvalds if (idx < s_idx || idx == PF_PACKET)
40181da177e4SLinus Torvalds continue;
40196853dd48SFlorian Westphal
4020addf9b90SFlorian Westphal if (type < 0 || type >= RTM_NR_MSGTYPES)
40211da177e4SLinus Torvalds continue;
40226853dd48SFlorian Westphal
4023addf9b90SFlorian Westphal tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]);
4024addf9b90SFlorian Westphal if (!tab)
4025addf9b90SFlorian Westphal continue;
4026addf9b90SFlorian Westphal
402751e13685SJakub Kicinski link = rcu_dereference_rtnl(tab[type]);
4028addf9b90SFlorian Westphal if (!link)
4029addf9b90SFlorian Westphal continue;
4030addf9b90SFlorian Westphal
4031addf9b90SFlorian Westphal dumpit = link->dumpit;
40326853dd48SFlorian Westphal if (!dumpit)
40336853dd48SFlorian Westphal continue;
40346853dd48SFlorian Westphal
40350465277fSNicolas Dichtel if (idx > s_idx) {
40361da177e4SLinus Torvalds memset(&cb->args[0], 0, sizeof(cb->args));
40370465277fSNicolas Dichtel cb->prev_seq = 0;
40380465277fSNicolas Dichtel cb->seq = 0;
40390465277fSNicolas Dichtel }
4040c63586dcSDavid Ahern ret = dumpit(skb, cb);
40415e1acb4aSAlexey Kodanev if (ret)
40421da177e4SLinus Torvalds break;
40431da177e4SLinus Torvalds }
40441da177e4SLinus Torvalds cb->family = idx;
40451da177e4SLinus Torvalds
4046c63586dcSDavid Ahern return skb->len ? : ret;
40471da177e4SLinus Torvalds }
40481da177e4SLinus Torvalds
rtmsg_ifinfo_build_skb(int type,struct net_device * dev,unsigned int change,u32 event,gfp_t flags,int * new_nsid,int new_ifindex,u32 portid,const struct nlmsghdr * nlh)4049395eea6cSMahesh Bandewar struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
40503d3ea5afSVlad Yasevich unsigned int change,
405138e01b30SNicolas Dichtel u32 event, gfp_t flags, int *new_nsid,
405259d3efd2SMartin Willi int new_ifindex, u32 portid,
405359d3efd2SMartin Willi const struct nlmsghdr *nlh)
40541da177e4SLinus Torvalds {
4055c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev);
40561da177e4SLinus Torvalds struct sk_buff *skb;
40570ec6d3f4SThomas Graf int err = -ENOBUFS;
405859d3efd2SMartin Willi u32 seq = 0;
40591da177e4SLinus Torvalds
406050af5969Sluo penghao skb = nlmsg_new(if_nlmsg_size(dev, 0), flags);
40610ec6d3f4SThomas Graf if (skb == NULL)
40620ec6d3f4SThomas Graf goto errout;
40631da177e4SLinus Torvalds
406459d3efd2SMartin Willi if (nlmsg_report(nlh))
406559d3efd2SMartin Willi seq = nlmsg_seq(nlh);
406659d3efd2SMartin Willi else
406759d3efd2SMartin Willi portid = 0;
406859d3efd2SMartin Willi
406979e1ad14SJiri Benc err = rtnl_fill_ifinfo(skb, dev, dev_net(dev),
40701d997f10SHangbin Liu type, portid, seq, change, 0, 0, event,
4071d4e4fdf9SGuillaume Nault new_nsid, new_ifindex, -1, flags);
407226932566SPatrick McHardy if (err < 0) {
407326932566SPatrick McHardy /* -EMSGSIZE implies BUG in if_nlmsg_size() */
407426932566SPatrick McHardy WARN_ON(err == -EMSGSIZE);
407526932566SPatrick McHardy kfree_skb(skb);
407626932566SPatrick McHardy goto errout;
407726932566SPatrick McHardy }
4078395eea6cSMahesh Bandewar return skb;
40790ec6d3f4SThomas Graf errout:
40800ec6d3f4SThomas Graf if (err < 0)
40814b3da706SEric W. Biederman rtnl_set_sk_err(net, RTNLGRP_LINK, err);
4082395eea6cSMahesh Bandewar return NULL;
4083395eea6cSMahesh Bandewar }
4084395eea6cSMahesh Bandewar
rtmsg_ifinfo_send(struct sk_buff * skb,struct net_device * dev,gfp_t flags,u32 portid,const struct nlmsghdr * nlh)40851d997f10SHangbin Liu void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags,
40861d997f10SHangbin Liu u32 portid, const struct nlmsghdr *nlh)
4087395eea6cSMahesh Bandewar {
4088395eea6cSMahesh Bandewar struct net *net = dev_net(dev);
4089395eea6cSMahesh Bandewar
40901d997f10SHangbin Liu rtnl_notify(skb, net, portid, RTNLGRP_LINK, nlh, flags);
4091395eea6cSMahesh Bandewar }
4092395eea6cSMahesh Bandewar
rtmsg_ifinfo_event(int type,struct net_device * dev,unsigned int change,u32 event,gfp_t flags,int * new_nsid,int new_ifindex,u32 portid,const struct nlmsghdr * nlh)40933d3ea5afSVlad Yasevich static void rtmsg_ifinfo_event(int type, struct net_device *dev,
40943d3ea5afSVlad Yasevich unsigned int change, u32 event,
40951d997f10SHangbin Liu gfp_t flags, int *new_nsid, int new_ifindex,
40961d997f10SHangbin Liu u32 portid, const struct nlmsghdr *nlh)
4097395eea6cSMahesh Bandewar {
4098395eea6cSMahesh Bandewar struct sk_buff *skb;
4099395eea6cSMahesh Bandewar
4100ed2a80abSNicolas Dichtel if (dev->reg_state != NETREG_REGISTERED)
4101ed2a80abSNicolas Dichtel return;
4102ed2a80abSNicolas Dichtel
410338e01b30SNicolas Dichtel skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid,
410459d3efd2SMartin Willi new_ifindex, portid, nlh);
4105395eea6cSMahesh Bandewar if (skb)
41061d997f10SHangbin Liu rtmsg_ifinfo_send(skb, dev, flags, portid, nlh);
41071da177e4SLinus Torvalds }
41083d3ea5afSVlad Yasevich
rtmsg_ifinfo(int type,struct net_device * dev,unsigned int change,gfp_t flags,u32 portid,const struct nlmsghdr * nlh)41093d3ea5afSVlad Yasevich void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
41101d997f10SHangbin Liu gfp_t flags, u32 portid, const struct nlmsghdr *nlh)
41113d3ea5afSVlad Yasevich {
411238e01b30SNicolas Dichtel rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
41131d997f10SHangbin Liu NULL, 0, portid, nlh);
41143d3ea5afSVlad Yasevich }
41151da177e4SLinus Torvalds
rtmsg_ifinfo_newnet(int type,struct net_device * dev,unsigned int change,gfp_t flags,int * new_nsid,int new_ifindex)41166621dd29SNicolas Dichtel void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change,
411738e01b30SNicolas Dichtel gfp_t flags, int *new_nsid, int new_ifindex)
41186621dd29SNicolas Dichtel {
41196621dd29SNicolas Dichtel rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
41201d997f10SHangbin Liu new_nsid, new_ifindex, 0, NULL);
41216621dd29SNicolas Dichtel }
41226621dd29SNicolas Dichtel
nlmsg_populate_fdb_fill(struct sk_buff * skb,struct net_device * dev,u8 * addr,u16 vid,u32 pid,u32 seq,int type,unsigned int flags,int nlflags,u16 ndm_state)4123d83b0603SJohn Fastabend static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
4124d83b0603SJohn Fastabend struct net_device *dev,
41251e53d5bbSHubert Sokolowski u8 *addr, u16 vid, u32 pid, u32 seq,
41261c104a6bSNicolas Dichtel int type, unsigned int flags,
4127b3379041SHubert Sokolowski int nlflags, u16 ndm_state)
4128d83b0603SJohn Fastabend {
4129d83b0603SJohn Fastabend struct nlmsghdr *nlh;
4130d83b0603SJohn Fastabend struct ndmsg *ndm;
4131d83b0603SJohn Fastabend
41321c104a6bSNicolas Dichtel nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
4133d83b0603SJohn Fastabend if (!nlh)
4134d83b0603SJohn Fastabend return -EMSGSIZE;
4135d83b0603SJohn Fastabend
4136d83b0603SJohn Fastabend ndm = nlmsg_data(nlh);
4137d83b0603SJohn Fastabend ndm->ndm_family = AF_BRIDGE;
4138d83b0603SJohn Fastabend ndm->ndm_pad1 = 0;
4139d83b0603SJohn Fastabend ndm->ndm_pad2 = 0;
4140d83b0603SJohn Fastabend ndm->ndm_flags = flags;
4141d83b0603SJohn Fastabend ndm->ndm_type = 0;
4142d83b0603SJohn Fastabend ndm->ndm_ifindex = dev->ifindex;
4143b3379041SHubert Sokolowski ndm->ndm_state = ndm_state;
4144d83b0603SJohn Fastabend
4145aa540695SEric Dumazet if (nla_put(skb, NDA_LLADDR, dev->addr_len, addr))
4146d83b0603SJohn Fastabend goto nla_put_failure;
41471e53d5bbSHubert Sokolowski if (vid)
41481e53d5bbSHubert Sokolowski if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
41491e53d5bbSHubert Sokolowski goto nla_put_failure;
4150d83b0603SJohn Fastabend
4151053c095aSJohannes Berg nlmsg_end(skb, nlh);
4152053c095aSJohannes Berg return 0;
4153d83b0603SJohn Fastabend
4154d83b0603SJohn Fastabend nla_put_failure:
4155d83b0603SJohn Fastabend nlmsg_cancel(skb, nlh);
4156d83b0603SJohn Fastabend return -EMSGSIZE;
4157d83b0603SJohn Fastabend }
4158d83b0603SJohn Fastabend
rtnl_fdb_nlmsg_size(const struct net_device * dev)4159aa540695SEric Dumazet static inline size_t rtnl_fdb_nlmsg_size(const struct net_device *dev)
41603ff661c3SJohn Fastabend {
4161f82ef3e1SSabrina Dubroca return NLMSG_ALIGN(sizeof(struct ndmsg)) +
4162aa540695SEric Dumazet nla_total_size(dev->addr_len) + /* NDA_LLADDR */
4163f82ef3e1SSabrina Dubroca nla_total_size(sizeof(u16)) + /* NDA_VLAN */
4164f82ef3e1SSabrina Dubroca 0;
41653ff661c3SJohn Fastabend }
41663ff661c3SJohn Fastabend
rtnl_fdb_notify(struct net_device * dev,u8 * addr,u16 vid,int type,u16 ndm_state)4167b3379041SHubert Sokolowski static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
4168b3379041SHubert Sokolowski u16 ndm_state)
41693ff661c3SJohn Fastabend {
41703ff661c3SJohn Fastabend struct net *net = dev_net(dev);
41713ff661c3SJohn Fastabend struct sk_buff *skb;
41723ff661c3SJohn Fastabend int err = -ENOBUFS;
41733ff661c3SJohn Fastabend
4174aa540695SEric Dumazet skb = nlmsg_new(rtnl_fdb_nlmsg_size(dev), GFP_ATOMIC);
41753ff661c3SJohn Fastabend if (!skb)
41763ff661c3SJohn Fastabend goto errout;
41773ff661c3SJohn Fastabend
41781e53d5bbSHubert Sokolowski err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
4179b3379041SHubert Sokolowski 0, 0, type, NTF_SELF, 0, ndm_state);
41803ff661c3SJohn Fastabend if (err < 0) {
41813ff661c3SJohn Fastabend kfree_skb(skb);
41823ff661c3SJohn Fastabend goto errout;
41833ff661c3SJohn Fastabend }
41843ff661c3SJohn Fastabend
41853ff661c3SJohn Fastabend rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
41863ff661c3SJohn Fastabend return;
41873ff661c3SJohn Fastabend errout:
41883ff661c3SJohn Fastabend rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
41893ff661c3SJohn Fastabend }
41903ff661c3SJohn Fastabend
4191a986967eSBart Van Assche /*
4192090096bfSVlad Yasevich * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
4193090096bfSVlad Yasevich */
ndo_dflt_fdb_add(struct ndmsg * ndm,struct nlattr * tb[],struct net_device * dev,const unsigned char * addr,u16 vid,u16 flags)4194090096bfSVlad Yasevich int ndo_dflt_fdb_add(struct ndmsg *ndm,
4195090096bfSVlad Yasevich struct nlattr *tb[],
4196090096bfSVlad Yasevich struct net_device *dev,
4197f6f6424bSJiri Pirko const unsigned char *addr, u16 vid,
4198090096bfSVlad Yasevich u16 flags)
4199090096bfSVlad Yasevich {
4200090096bfSVlad Yasevich int err = -EINVAL;
4201090096bfSVlad Yasevich
4202090096bfSVlad Yasevich /* If aging addresses are supported device will need to
4203090096bfSVlad Yasevich * implement its own handler for this.
4204090096bfSVlad Yasevich */
4205090096bfSVlad Yasevich if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
420678ecc890SVladimir Oltean netdev_info(dev, "default FDB implementation only supports local addresses\n");
4207090096bfSVlad Yasevich return err;
4208090096bfSVlad Yasevich }
4209090096bfSVlad Yasevich
4210a35ec8e3SHans J. Schultz if (tb[NDA_FLAGS_EXT]) {
4211a35ec8e3SHans J. Schultz netdev_info(dev, "invalid flags given to default FDB implementation\n");
4212a35ec8e3SHans J. Schultz return err;
4213a35ec8e3SHans J. Schultz }
4214a35ec8e3SHans J. Schultz
421565891feaSOr Gerlitz if (vid) {
421623ac0b42SVladimir Oltean netdev_info(dev, "vlans aren't supported yet for dev_uc|mc_add()\n");
421765891feaSOr Gerlitz return err;
421865891feaSOr Gerlitz }
421965891feaSOr Gerlitz
4220090096bfSVlad Yasevich if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
4221090096bfSVlad Yasevich err = dev_uc_add_excl(dev, addr);
4222090096bfSVlad Yasevich else if (is_multicast_ether_addr(addr))
4223090096bfSVlad Yasevich err = dev_mc_add_excl(dev, addr);
4224090096bfSVlad Yasevich
4225090096bfSVlad Yasevich /* Only return duplicate errors if NLM_F_EXCL is set */
4226090096bfSVlad Yasevich if (err == -EEXIST && !(flags & NLM_F_EXCL))
4227090096bfSVlad Yasevich err = 0;
4228090096bfSVlad Yasevich
4229090096bfSVlad Yasevich return err;
4230090096bfSVlad Yasevich }
4231090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_add);
4232090096bfSVlad Yasevich
fdb_vid_parse(struct nlattr * vlan_attr,u16 * p_vid,struct netlink_ext_ack * extack)4233b88d12e4SFlorian Westphal static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid,
4234b88d12e4SFlorian Westphal struct netlink_ext_ack *extack)
4235f6f6424bSJiri Pirko {
4236f6f6424bSJiri Pirko u16 vid = 0;
4237f6f6424bSJiri Pirko
4238f6f6424bSJiri Pirko if (vlan_attr) {
4239f6f6424bSJiri Pirko if (nla_len(vlan_attr) != sizeof(u16)) {
4240b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid vlan attribute size");
4241f6f6424bSJiri Pirko return -EINVAL;
4242f6f6424bSJiri Pirko }
4243f6f6424bSJiri Pirko
4244f6f6424bSJiri Pirko vid = nla_get_u16(vlan_attr);
4245f6f6424bSJiri Pirko
4246f6f6424bSJiri Pirko if (!vid || vid >= VLAN_VID_MASK) {
4247b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid vlan id");
4248f6f6424bSJiri Pirko return -EINVAL;
4249f6f6424bSJiri Pirko }
4250f6f6424bSJiri Pirko }
4251f6f6424bSJiri Pirko *p_vid = vid;
4252f6f6424bSJiri Pirko return 0;
4253f6f6424bSJiri Pirko }
4254f6f6424bSJiri Pirko
rtnl_fdb_add(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)4255c21ef3e3SDavid Ahern static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
4256c21ef3e3SDavid Ahern struct netlink_ext_ack *extack)
425777162022SJohn Fastabend {
425877162022SJohn Fastabend struct net *net = sock_net(skb->sk);
425977162022SJohn Fastabend struct ndmsg *ndm;
426077162022SJohn Fastabend struct nlattr *tb[NDA_MAX+1];
426177162022SJohn Fastabend struct net_device *dev;
426277162022SJohn Fastabend u8 *addr;
4263f6f6424bSJiri Pirko u16 vid;
426477162022SJohn Fastabend int err;
426577162022SJohn Fastabend
42668cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL,
42678cb08174SJohannes Berg extack);
426877162022SJohn Fastabend if (err < 0)
426977162022SJohn Fastabend return err;
427077162022SJohn Fastabend
427177162022SJohn Fastabend ndm = nlmsg_data(nlh);
427277162022SJohn Fastabend if (ndm->ndm_ifindex == 0) {
4273b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid ifindex");
427477162022SJohn Fastabend return -EINVAL;
427577162022SJohn Fastabend }
427677162022SJohn Fastabend
427777162022SJohn Fastabend dev = __dev_get_by_index(net, ndm->ndm_ifindex);
427877162022SJohn Fastabend if (dev == NULL) {
4279b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex");
428077162022SJohn Fastabend return -ENODEV;
428177162022SJohn Fastabend }
428277162022SJohn Fastabend
428377162022SJohn Fastabend if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
4284b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid address");
428577162022SJohn Fastabend return -EINVAL;
428677162022SJohn Fastabend }
428777162022SJohn Fastabend
4288da715775SIdo Schimmel if (dev->type != ARPHRD_ETHER) {
4289da715775SIdo Schimmel NL_SET_ERR_MSG(extack, "FDB add only supported for Ethernet devices");
4290da715775SIdo Schimmel return -EINVAL;
4291da715775SIdo Schimmel }
4292da715775SIdo Schimmel
429377162022SJohn Fastabend addr = nla_data(tb[NDA_LLADDR]);
429477162022SJohn Fastabend
4295b88d12e4SFlorian Westphal err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
4296f6f6424bSJiri Pirko if (err)
4297f6f6424bSJiri Pirko return err;
4298f6f6424bSJiri Pirko
429977162022SJohn Fastabend err = -EOPNOTSUPP;
430077162022SJohn Fastabend
430177162022SJohn Fastabend /* Support fdb on master device the net/bridge default case */
430277162022SJohn Fastabend if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
43032e92a2d0SJulian Wiedmann netif_is_bridge_port(dev)) {
4304898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4305898e5061SJiri Pirko const struct net_device_ops *ops = br_dev->netdev_ops;
4306898e5061SJiri Pirko
4307f6f6424bSJiri Pirko err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
430887b0984eSPetr Machata nlh->nlmsg_flags, extack);
430977162022SJohn Fastabend if (err)
431077162022SJohn Fastabend goto out;
431177162022SJohn Fastabend else
431277162022SJohn Fastabend ndm->ndm_flags &= ~NTF_MASTER;
431377162022SJohn Fastabend }
431477162022SJohn Fastabend
431577162022SJohn Fastabend /* Embedded bridge, macvlan, and any other device support */
4316090096bfSVlad Yasevich if ((ndm->ndm_flags & NTF_SELF)) {
4317090096bfSVlad Yasevich if (dev->netdev_ops->ndo_fdb_add)
4318090096bfSVlad Yasevich err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
4319f6f6424bSJiri Pirko vid,
432087b0984eSPetr Machata nlh->nlmsg_flags,
432187b0984eSPetr Machata extack);
4322090096bfSVlad Yasevich else
4323f6f6424bSJiri Pirko err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
432477162022SJohn Fastabend nlh->nlmsg_flags);
432577162022SJohn Fastabend
43263ff661c3SJohn Fastabend if (!err) {
4327b3379041SHubert Sokolowski rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
4328b3379041SHubert Sokolowski ndm->ndm_state);
432977162022SJohn Fastabend ndm->ndm_flags &= ~NTF_SELF;
433077162022SJohn Fastabend }
43313ff661c3SJohn Fastabend }
433277162022SJohn Fastabend out:
433377162022SJohn Fastabend return err;
433477162022SJohn Fastabend }
433577162022SJohn Fastabend
4336a986967eSBart Van Assche /*
4337090096bfSVlad Yasevich * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
4338090096bfSVlad Yasevich */
ndo_dflt_fdb_del(struct ndmsg * ndm,struct nlattr * tb[],struct net_device * dev,const unsigned char * addr,u16 vid)4339090096bfSVlad Yasevich int ndo_dflt_fdb_del(struct ndmsg *ndm,
4340090096bfSVlad Yasevich struct nlattr *tb[],
4341090096bfSVlad Yasevich struct net_device *dev,
4342f6f6424bSJiri Pirko const unsigned char *addr, u16 vid)
4343090096bfSVlad Yasevich {
4344c8a89c4aSAlexander Duyck int err = -EINVAL;
4345090096bfSVlad Yasevich
4346090096bfSVlad Yasevich /* If aging addresses are supported device will need to
4347090096bfSVlad Yasevich * implement its own handler for this.
4348090096bfSVlad Yasevich */
434964535993SSridhar Samudrala if (!(ndm->ndm_state & NUD_PERMANENT)) {
435078ecc890SVladimir Oltean netdev_info(dev, "default FDB implementation only supports local addresses\n");
4351c8a89c4aSAlexander Duyck return err;
4352090096bfSVlad Yasevich }
4353090096bfSVlad Yasevich
4354090096bfSVlad Yasevich if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
4355090096bfSVlad Yasevich err = dev_uc_del(dev, addr);
4356090096bfSVlad Yasevich else if (is_multicast_ether_addr(addr))
4357090096bfSVlad Yasevich err = dev_mc_del(dev, addr);
4358090096bfSVlad Yasevich
4359090096bfSVlad Yasevich return err;
4360090096bfSVlad Yasevich }
4361090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_del);
4362090096bfSVlad Yasevich
43639e834259SNikolay Aleksandrov static const struct nla_policy fdb_del_bulk_policy[NDA_MAX + 1] = {
43649e834259SNikolay Aleksandrov [NDA_VLAN] = { .type = NLA_U16 },
43659e834259SNikolay Aleksandrov [NDA_IFINDEX] = NLA_POLICY_MIN(NLA_S32, 1),
4366ea2c0f9eSNikolay Aleksandrov [NDA_NDM_STATE_MASK] = { .type = NLA_U16 },
4367ea2c0f9eSNikolay Aleksandrov [NDA_NDM_FLAGS_MASK] = { .type = NLA_U8 },
43689e834259SNikolay Aleksandrov };
43699e834259SNikolay Aleksandrov
rtnl_fdb_del(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)4370c21ef3e3SDavid Ahern static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
4371c21ef3e3SDavid Ahern struct netlink_ext_ack *extack)
437277162022SJohn Fastabend {
43739e834259SNikolay Aleksandrov bool del_bulk = !!(nlh->nlmsg_flags & NLM_F_BULK);
437477162022SJohn Fastabend struct net *net = sock_net(skb->sk);
43759e834259SNikolay Aleksandrov const struct net_device_ops *ops;
437677162022SJohn Fastabend struct ndmsg *ndm;
43771690be63SVlad Yasevich struct nlattr *tb[NDA_MAX+1];
437877162022SJohn Fastabend struct net_device *dev;
43799e834259SNikolay Aleksandrov __u8 *addr = NULL;
43807d311801SColin Ian King int err;
4381f6f6424bSJiri Pirko u16 vid;
438277162022SJohn Fastabend
438390f62cf3SEric W. Biederman if (!netlink_capable(skb, CAP_NET_ADMIN))
43841690be63SVlad Yasevich return -EPERM;
43851690be63SVlad Yasevich
43869e834259SNikolay Aleksandrov if (!del_bulk) {
43879e834259SNikolay Aleksandrov err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX,
43889e834259SNikolay Aleksandrov NULL, extack);
43899e834259SNikolay Aleksandrov } else {
43909e834259SNikolay Aleksandrov err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX,
43919e834259SNikolay Aleksandrov fdb_del_bulk_policy, extack);
43929e834259SNikolay Aleksandrov }
43931690be63SVlad Yasevich if (err < 0)
43941690be63SVlad Yasevich return err;
439577162022SJohn Fastabend
439677162022SJohn Fastabend ndm = nlmsg_data(nlh);
439777162022SJohn Fastabend if (ndm->ndm_ifindex == 0) {
4398b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid ifindex");
439977162022SJohn Fastabend return -EINVAL;
440077162022SJohn Fastabend }
440177162022SJohn Fastabend
440277162022SJohn Fastabend dev = __dev_get_by_index(net, ndm->ndm_ifindex);
440377162022SJohn Fastabend if (dev == NULL) {
4404b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex");
440577162022SJohn Fastabend return -ENODEV;
440677162022SJohn Fastabend }
440777162022SJohn Fastabend
44089e834259SNikolay Aleksandrov if (!del_bulk) {
44091690be63SVlad Yasevich if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
4410b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid address");
441177162022SJohn Fastabend return -EINVAL;
441277162022SJohn Fastabend }
44139e834259SNikolay Aleksandrov addr = nla_data(tb[NDA_LLADDR]);
44149e834259SNikolay Aleksandrov }
441577162022SJohn Fastabend
4416da715775SIdo Schimmel if (dev->type != ARPHRD_ETHER) {
4417da715775SIdo Schimmel NL_SET_ERR_MSG(extack, "FDB delete only supported for Ethernet devices");
4418da715775SIdo Schimmel return -EINVAL;
4419da715775SIdo Schimmel }
4420da715775SIdo Schimmel
4421b88d12e4SFlorian Westphal err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
4422f6f6424bSJiri Pirko if (err)
4423f6f6424bSJiri Pirko return err;
4424f6f6424bSJiri Pirko
442577162022SJohn Fastabend err = -EOPNOTSUPP;
442677162022SJohn Fastabend
442777162022SJohn Fastabend /* Support fdb on master device the net/bridge default case */
442877162022SJohn Fastabend if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
44292e92a2d0SJulian Wiedmann netif_is_bridge_port(dev)) {
4430898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev);
443177162022SJohn Fastabend
44329e834259SNikolay Aleksandrov ops = br_dev->netdev_ops;
44339e834259SNikolay Aleksandrov if (!del_bulk) {
4434898e5061SJiri Pirko if (ops->ndo_fdb_del)
4435ca4567f1SAlaa Mohamed err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid, extack);
44369e834259SNikolay Aleksandrov } else {
44379e834259SNikolay Aleksandrov if (ops->ndo_fdb_del_bulk)
44389e834259SNikolay Aleksandrov err = ops->ndo_fdb_del_bulk(ndm, tb, dev, vid,
44399e834259SNikolay Aleksandrov extack);
44409e834259SNikolay Aleksandrov }
444177162022SJohn Fastabend
444277162022SJohn Fastabend if (err)
444377162022SJohn Fastabend goto out;
444477162022SJohn Fastabend else
444577162022SJohn Fastabend ndm->ndm_flags &= ~NTF_MASTER;
444677162022SJohn Fastabend }
444777162022SJohn Fastabend
444877162022SJohn Fastabend /* Embedded bridge, macvlan, and any other device support */
4449090096bfSVlad Yasevich if (ndm->ndm_flags & NTF_SELF) {
44509e834259SNikolay Aleksandrov ops = dev->netdev_ops;
44519e834259SNikolay Aleksandrov if (!del_bulk) {
44529e834259SNikolay Aleksandrov if (ops->ndo_fdb_del)
4453ca4567f1SAlaa Mohamed err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid, extack);
4454090096bfSVlad Yasevich else
4455f6f6424bSJiri Pirko err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
44569e834259SNikolay Aleksandrov } else {
44579e834259SNikolay Aleksandrov /* in case err was cleared by NTF_MASTER call */
44589e834259SNikolay Aleksandrov err = -EOPNOTSUPP;
44599e834259SNikolay Aleksandrov if (ops->ndo_fdb_del_bulk)
44609e834259SNikolay Aleksandrov err = ops->ndo_fdb_del_bulk(ndm, tb, dev, vid,
44619e834259SNikolay Aleksandrov extack);
44629e834259SNikolay Aleksandrov }
446377162022SJohn Fastabend
44643ff661c3SJohn Fastabend if (!err) {
44659e834259SNikolay Aleksandrov if (!del_bulk)
4466b3379041SHubert Sokolowski rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
4467b3379041SHubert Sokolowski ndm->ndm_state);
446877162022SJohn Fastabend ndm->ndm_flags &= ~NTF_SELF;
446977162022SJohn Fastabend }
44703ff661c3SJohn Fastabend }
447177162022SJohn Fastabend out:
447277162022SJohn Fastabend return err;
447377162022SJohn Fastabend }
447477162022SJohn Fastabend
nlmsg_populate_fdb(struct sk_buff * skb,struct netlink_callback * cb,struct net_device * dev,int * idx,struct netdev_hw_addr_list * list)4475d83b0603SJohn Fastabend static int nlmsg_populate_fdb(struct sk_buff *skb,
4476d83b0603SJohn Fastabend struct netlink_callback *cb,
4477d83b0603SJohn Fastabend struct net_device *dev,
4478d83b0603SJohn Fastabend int *idx,
4479d83b0603SJohn Fastabend struct netdev_hw_addr_list *list)
4480d83b0603SJohn Fastabend {
4481d83b0603SJohn Fastabend struct netdev_hw_addr *ha;
4482d83b0603SJohn Fastabend int err;
448315e47304SEric W. Biederman u32 portid, seq;
4484d83b0603SJohn Fastabend
448515e47304SEric W. Biederman portid = NETLINK_CB(cb->skb).portid;
4486d83b0603SJohn Fastabend seq = cb->nlh->nlmsg_seq;
4487d83b0603SJohn Fastabend
4488d83b0603SJohn Fastabend list_for_each_entry(ha, &list->list, list) {
4489d297653dSRoopa Prabhu if (*idx < cb->args[2])
4490d83b0603SJohn Fastabend goto skip;
4491d83b0603SJohn Fastabend
44921e53d5bbSHubert Sokolowski err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
4493a7a558feSJohn Fastabend portid, seq,
44941c104a6bSNicolas Dichtel RTM_NEWNEIGH, NTF_SELF,
4495b3379041SHubert Sokolowski NLM_F_MULTI, NUD_PERMANENT);
4496d83b0603SJohn Fastabend if (err < 0)
4497d83b0603SJohn Fastabend return err;
4498d83b0603SJohn Fastabend skip:
4499d83b0603SJohn Fastabend *idx += 1;
4500d83b0603SJohn Fastabend }
4501d83b0603SJohn Fastabend return 0;
4502d83b0603SJohn Fastabend }
4503d83b0603SJohn Fastabend
4504d83b0603SJohn Fastabend /**
45052c53040fSBen Hutchings * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
4506a986967eSBart Van Assche * @skb: socket buffer to store message in
4507a986967eSBart Van Assche * @cb: netlink callback
4508d83b0603SJohn Fastabend * @dev: netdevice
4509a986967eSBart Van Assche * @filter_dev: ignored
4510a986967eSBart Van Assche * @idx: the number of FDB table entries dumped is added to *@idx
4511d83b0603SJohn Fastabend *
4512d83b0603SJohn Fastabend * Default netdevice operation to dump the existing unicast address list.
451391f3e7b1SJohn Fastabend * Returns number of addresses from list put in skb.
4514d83b0603SJohn Fastabend */
ndo_dflt_fdb_dump(struct sk_buff * skb,struct netlink_callback * cb,struct net_device * dev,struct net_device * filter_dev,int * idx)4515d83b0603SJohn Fastabend int ndo_dflt_fdb_dump(struct sk_buff *skb,
4516d83b0603SJohn Fastabend struct netlink_callback *cb,
4517d83b0603SJohn Fastabend struct net_device *dev,
45185d5eacb3SJamal Hadi Salim struct net_device *filter_dev,
4519d297653dSRoopa Prabhu int *idx)
4520d83b0603SJohn Fastabend {
4521d83b0603SJohn Fastabend int err;
4522d83b0603SJohn Fastabend
452368883893SEric Dumazet if (dev->type != ARPHRD_ETHER)
452468883893SEric Dumazet return -EINVAL;
452568883893SEric Dumazet
4526d83b0603SJohn Fastabend netif_addr_lock_bh(dev);
4527d297653dSRoopa Prabhu err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
4528d83b0603SJohn Fastabend if (err)
4529d83b0603SJohn Fastabend goto out;
45302934c9dbSZhang Shengju err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
4531d83b0603SJohn Fastabend out:
4532d83b0603SJohn Fastabend netif_addr_unlock_bh(dev);
4533d297653dSRoopa Prabhu return err;
4534d83b0603SJohn Fastabend }
4535d83b0603SJohn Fastabend EXPORT_SYMBOL(ndo_dflt_fdb_dump);
4536d83b0603SJohn Fastabend
valid_fdb_dump_strict(const struct nlmsghdr * nlh,int * br_idx,int * brport_idx,struct netlink_ext_ack * extack)45378c6e137fSDavid Ahern static int valid_fdb_dump_strict(const struct nlmsghdr *nlh,
45388c6e137fSDavid Ahern int *br_idx, int *brport_idx,
45398c6e137fSDavid Ahern struct netlink_ext_ack *extack)
45408c6e137fSDavid Ahern {
45418c6e137fSDavid Ahern struct nlattr *tb[NDA_MAX + 1];
45428c6e137fSDavid Ahern struct ndmsg *ndm;
45438c6e137fSDavid Ahern int err, i;
45448c6e137fSDavid Ahern
45458c6e137fSDavid Ahern if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
45468c6e137fSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid header for fdb dump request");
45478c6e137fSDavid Ahern return -EINVAL;
45488c6e137fSDavid Ahern }
45498c6e137fSDavid Ahern
45508c6e137fSDavid Ahern ndm = nlmsg_data(nlh);
45518c6e137fSDavid Ahern if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state ||
45528c6e137fSDavid Ahern ndm->ndm_flags || ndm->ndm_type) {
45538b73018fSNikolay Aleksandrov NL_SET_ERR_MSG(extack, "Invalid values in header for fdb dump request");
45548c6e137fSDavid Ahern return -EINVAL;
45558c6e137fSDavid Ahern }
45568c6e137fSDavid Ahern
45578cb08174SJohannes Berg err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
45588cb08174SJohannes Berg NDA_MAX, NULL, extack);
45598c6e137fSDavid Ahern if (err < 0)
45608c6e137fSDavid Ahern return err;
45618c6e137fSDavid Ahern
45628c6e137fSDavid Ahern *brport_idx = ndm->ndm_ifindex;
45638c6e137fSDavid Ahern for (i = 0; i <= NDA_MAX; ++i) {
45648c6e137fSDavid Ahern if (!tb[i])
45658c6e137fSDavid Ahern continue;
45668c6e137fSDavid Ahern
45678c6e137fSDavid Ahern switch (i) {
45688c6e137fSDavid Ahern case NDA_IFINDEX:
45698c6e137fSDavid Ahern if (nla_len(tb[i]) != sizeof(u32)) {
45708c6e137fSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid IFINDEX attribute in fdb dump request");
45718c6e137fSDavid Ahern return -EINVAL;
45728c6e137fSDavid Ahern }
45738c6e137fSDavid Ahern *brport_idx = nla_get_u32(tb[NDA_IFINDEX]);
45748c6e137fSDavid Ahern break;
45758c6e137fSDavid Ahern case NDA_MASTER:
45768c6e137fSDavid Ahern if (nla_len(tb[i]) != sizeof(u32)) {
45778c6e137fSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid MASTER attribute in fdb dump request");
45788c6e137fSDavid Ahern return -EINVAL;
45798c6e137fSDavid Ahern }
45808c6e137fSDavid Ahern *br_idx = nla_get_u32(tb[NDA_MASTER]);
45818c6e137fSDavid Ahern break;
45828c6e137fSDavid Ahern default:
45838c6e137fSDavid Ahern NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb dump request");
45848c6e137fSDavid Ahern return -EINVAL;
45858c6e137fSDavid Ahern }
45868c6e137fSDavid Ahern }
45878c6e137fSDavid Ahern
45888c6e137fSDavid Ahern return 0;
45898c6e137fSDavid Ahern }
45908c6e137fSDavid Ahern
valid_fdb_dump_legacy(const struct nlmsghdr * nlh,int * br_idx,int * brport_idx,struct netlink_ext_ack * extack)45918dfbda19SDavid Ahern static int valid_fdb_dump_legacy(const struct nlmsghdr *nlh,
45928dfbda19SDavid Ahern int *br_idx, int *brport_idx,
45938dfbda19SDavid Ahern struct netlink_ext_ack *extack)
45948dfbda19SDavid Ahern {
45958dfbda19SDavid Ahern struct nlattr *tb[IFLA_MAX+1];
45968dfbda19SDavid Ahern int err;
45978dfbda19SDavid Ahern
45988dfbda19SDavid Ahern /* A hack to preserve kernel<->userspace interface.
45998dfbda19SDavid Ahern * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0.
46008dfbda19SDavid Ahern * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails.
46018dfbda19SDavid Ahern * So, check for ndmsg with an optional u32 attribute (not used here).
46028dfbda19SDavid Ahern * Fortunately these sizes don't conflict with the size of ifinfomsg
46038dfbda19SDavid Ahern * with an optional attribute.
46048dfbda19SDavid Ahern */
46058dfbda19SDavid Ahern if (nlmsg_len(nlh) != sizeof(struct ndmsg) &&
46068dfbda19SDavid Ahern (nlmsg_len(nlh) != sizeof(struct ndmsg) +
46078dfbda19SDavid Ahern nla_attr_size(sizeof(u32)))) {
46084565d7e5SDavid Ahern struct ifinfomsg *ifm;
46094565d7e5SDavid Ahern
46108cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
46118cb08174SJohannes Berg tb, IFLA_MAX, ifla_policy,
46128cb08174SJohannes Berg extack);
46138dfbda19SDavid Ahern if (err < 0) {
46148dfbda19SDavid Ahern return -EINVAL;
46158dfbda19SDavid Ahern } else if (err == 0) {
46168dfbda19SDavid Ahern if (tb[IFLA_MASTER])
46178dfbda19SDavid Ahern *br_idx = nla_get_u32(tb[IFLA_MASTER]);
46188dfbda19SDavid Ahern }
46198dfbda19SDavid Ahern
46204565d7e5SDavid Ahern ifm = nlmsg_data(nlh);
46218dfbda19SDavid Ahern *brport_idx = ifm->ifi_index;
46228dfbda19SDavid Ahern }
46238dfbda19SDavid Ahern return 0;
46248dfbda19SDavid Ahern }
46258dfbda19SDavid Ahern
rtnl_fdb_dump(struct sk_buff * skb,struct netlink_callback * cb)462677162022SJohn Fastabend static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
462777162022SJohn Fastabend {
462877162022SJohn Fastabend struct net_device *dev;
46295e6d2435SJamal Hadi Salim struct net_device *br_dev = NULL;
46305e6d2435SJamal Hadi Salim const struct net_device_ops *ops = NULL;
46315e6d2435SJamal Hadi Salim const struct net_device_ops *cops = NULL;
46325e6d2435SJamal Hadi Salim struct net *net = sock_net(skb->sk);
4633d297653dSRoopa Prabhu struct hlist_head *head;
46345e6d2435SJamal Hadi Salim int brport_idx = 0;
46355e6d2435SJamal Hadi Salim int br_idx = 0;
4636d297653dSRoopa Prabhu int h, s_h;
4637d297653dSRoopa Prabhu int idx = 0, s_idx;
4638d297653dSRoopa Prabhu int err = 0;
4639d297653dSRoopa Prabhu int fidx = 0;
464077162022SJohn Fastabend
46418c6e137fSDavid Ahern if (cb->strict_check)
46428c6e137fSDavid Ahern err = valid_fdb_dump_strict(cb->nlh, &br_idx, &brport_idx,
46438c6e137fSDavid Ahern cb->extack);
46448c6e137fSDavid Ahern else
46458dfbda19SDavid Ahern err = valid_fdb_dump_legacy(cb->nlh, &br_idx, &brport_idx,
46468dfbda19SDavid Ahern cb->extack);
46478dfbda19SDavid Ahern if (err < 0)
46488dfbda19SDavid Ahern return err;
46495e6d2435SJamal Hadi Salim
46505e6d2435SJamal Hadi Salim if (br_idx) {
46515e6d2435SJamal Hadi Salim br_dev = __dev_get_by_index(net, br_idx);
46525e6d2435SJamal Hadi Salim if (!br_dev)
46535e6d2435SJamal Hadi Salim return -ENODEV;
46545e6d2435SJamal Hadi Salim
4655898e5061SJiri Pirko ops = br_dev->netdev_ops;
46565e6d2435SJamal Hadi Salim }
46575e6d2435SJamal Hadi Salim
4658d297653dSRoopa Prabhu s_h = cb->args[0];
4659d297653dSRoopa Prabhu s_idx = cb->args[1];
4660d297653dSRoopa Prabhu
4661d297653dSRoopa Prabhu for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4662d297653dSRoopa Prabhu idx = 0;
4663d297653dSRoopa Prabhu head = &net->dev_index_head[h];
4664d297653dSRoopa Prabhu hlist_for_each_entry(dev, head, index_hlist) {
4665d297653dSRoopa Prabhu
46665e6d2435SJamal Hadi Salim if (brport_idx && (dev->ifindex != brport_idx))
46675e6d2435SJamal Hadi Salim continue;
46685e6d2435SJamal Hadi Salim
46695e6d2435SJamal Hadi Salim if (!br_idx) { /* user did not specify a specific bridge */
46702e92a2d0SJulian Wiedmann if (netif_is_bridge_port(dev)) {
46715e6d2435SJamal Hadi Salim br_dev = netdev_master_upper_dev_get(dev);
46725e6d2435SJamal Hadi Salim cops = br_dev->netdev_ops;
46735e6d2435SJamal Hadi Salim }
46745e6d2435SJamal Hadi Salim } else {
46755e6d2435SJamal Hadi Salim if (dev != br_dev &&
46762e92a2d0SJulian Wiedmann !netif_is_bridge_port(dev))
46775e6d2435SJamal Hadi Salim continue;
46785e6d2435SJamal Hadi Salim
46795e6d2435SJamal Hadi Salim if (br_dev != netdev_master_upper_dev_get(dev) &&
4680254ec036SKyungrok Chung !netif_is_bridge_master(dev))
46815e6d2435SJamal Hadi Salim continue;
46825e6d2435SJamal Hadi Salim cops = ops;
46835e6d2435SJamal Hadi Salim }
46845e6d2435SJamal Hadi Salim
4685d297653dSRoopa Prabhu if (idx < s_idx)
4686d297653dSRoopa Prabhu goto cont;
4687d297653dSRoopa Prabhu
46882e92a2d0SJulian Wiedmann if (netif_is_bridge_port(dev)) {
4689d297653dSRoopa Prabhu if (cops && cops->ndo_fdb_dump) {
4690d297653dSRoopa Prabhu err = cops->ndo_fdb_dump(skb, cb,
4691d297653dSRoopa Prabhu br_dev, dev,
4692d297653dSRoopa Prabhu &fidx);
4693d297653dSRoopa Prabhu if (err == -EMSGSIZE)
4694d297653dSRoopa Prabhu goto out;
469577162022SJohn Fastabend }
4696d297653dSRoopa Prabhu }
469777162022SJohn Fastabend
46985e6d2435SJamal Hadi Salim if (dev->netdev_ops->ndo_fdb_dump)
4699d297653dSRoopa Prabhu err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
4700d297653dSRoopa Prabhu dev, NULL,
4701d297653dSRoopa Prabhu &fidx);
47026cb69742SHubert Sokolowski else
4703d297653dSRoopa Prabhu err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
4704d297653dSRoopa Prabhu &fidx);
4705d297653dSRoopa Prabhu if (err == -EMSGSIZE)
4706d297653dSRoopa Prabhu goto out;
47075e6d2435SJamal Hadi Salim
47085e6d2435SJamal Hadi Salim cops = NULL;
4709d297653dSRoopa Prabhu
4710d297653dSRoopa Prabhu /* reset fdb offset to 0 for rest of the interfaces */
4711d297653dSRoopa Prabhu cb->args[2] = 0;
4712d297653dSRoopa Prabhu fidx = 0;
4713d297653dSRoopa Prabhu cont:
4714d297653dSRoopa Prabhu idx++;
4715d297653dSRoopa Prabhu }
471677162022SJohn Fastabend }
471777162022SJohn Fastabend
4718d297653dSRoopa Prabhu out:
4719d297653dSRoopa Prabhu cb->args[0] = h;
4720d297653dSRoopa Prabhu cb->args[1] = idx;
4721d297653dSRoopa Prabhu cb->args[2] = fidx;
4722d297653dSRoopa Prabhu
472377162022SJohn Fastabend return skb->len;
472477162022SJohn Fastabend }
472577162022SJohn Fastabend
valid_fdb_get_strict(const struct nlmsghdr * nlh,struct nlattr ** tb,u8 * ndm_flags,int * br_idx,int * brport_idx,u8 ** addr,u16 * vid,struct netlink_ext_ack * extack)47265b2f94b2SRoopa Prabhu static int valid_fdb_get_strict(const struct nlmsghdr *nlh,
47275b2f94b2SRoopa Prabhu struct nlattr **tb, u8 *ndm_flags,
47285b2f94b2SRoopa Prabhu int *br_idx, int *brport_idx, u8 **addr,
47295b2f94b2SRoopa Prabhu u16 *vid, struct netlink_ext_ack *extack)
47305b2f94b2SRoopa Prabhu {
47315b2f94b2SRoopa Prabhu struct ndmsg *ndm;
47325b2f94b2SRoopa Prabhu int err, i;
47335b2f94b2SRoopa Prabhu
47345b2f94b2SRoopa Prabhu if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
47355b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid header for fdb get request");
47365b2f94b2SRoopa Prabhu return -EINVAL;
47375b2f94b2SRoopa Prabhu }
47385b2f94b2SRoopa Prabhu
47395b2f94b2SRoopa Prabhu ndm = nlmsg_data(nlh);
47405b2f94b2SRoopa Prabhu if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state ||
47415b2f94b2SRoopa Prabhu ndm->ndm_type) {
47425b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid values in header for fdb get request");
47435b2f94b2SRoopa Prabhu return -EINVAL;
47445b2f94b2SRoopa Prabhu }
47455b2f94b2SRoopa Prabhu
47465b2f94b2SRoopa Prabhu if (ndm->ndm_flags & ~(NTF_MASTER | NTF_SELF)) {
47475b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid flags in header for fdb get request");
47485b2f94b2SRoopa Prabhu return -EINVAL;
47495b2f94b2SRoopa Prabhu }
47505b2f94b2SRoopa Prabhu
47518cb08174SJohannes Berg err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
47528cb08174SJohannes Berg NDA_MAX, nda_policy, extack);
47535b2f94b2SRoopa Prabhu if (err < 0)
47545b2f94b2SRoopa Prabhu return err;
47555b2f94b2SRoopa Prabhu
47565b2f94b2SRoopa Prabhu *ndm_flags = ndm->ndm_flags;
47575b2f94b2SRoopa Prabhu *brport_idx = ndm->ndm_ifindex;
47585b2f94b2SRoopa Prabhu for (i = 0; i <= NDA_MAX; ++i) {
47595b2f94b2SRoopa Prabhu if (!tb[i])
47605b2f94b2SRoopa Prabhu continue;
47615b2f94b2SRoopa Prabhu
47625b2f94b2SRoopa Prabhu switch (i) {
47635b2f94b2SRoopa Prabhu case NDA_MASTER:
47645b2f94b2SRoopa Prabhu *br_idx = nla_get_u32(tb[i]);
47655b2f94b2SRoopa Prabhu break;
47665b2f94b2SRoopa Prabhu case NDA_LLADDR:
47675b2f94b2SRoopa Prabhu if (nla_len(tb[i]) != ETH_ALEN) {
47685b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid address in fdb get request");
47695b2f94b2SRoopa Prabhu return -EINVAL;
47705b2f94b2SRoopa Prabhu }
47715b2f94b2SRoopa Prabhu *addr = nla_data(tb[i]);
47725b2f94b2SRoopa Prabhu break;
47735b2f94b2SRoopa Prabhu case NDA_VLAN:
47745b2f94b2SRoopa Prabhu err = fdb_vid_parse(tb[i], vid, extack);
47755b2f94b2SRoopa Prabhu if (err)
47765b2f94b2SRoopa Prabhu return err;
47775b2f94b2SRoopa Prabhu break;
47785b2f94b2SRoopa Prabhu case NDA_VNI:
47795b2f94b2SRoopa Prabhu break;
47805b2f94b2SRoopa Prabhu default:
47815b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb get request");
47825b2f94b2SRoopa Prabhu return -EINVAL;
47835b2f94b2SRoopa Prabhu }
47845b2f94b2SRoopa Prabhu }
47855b2f94b2SRoopa Prabhu
47865b2f94b2SRoopa Prabhu return 0;
47875b2f94b2SRoopa Prabhu }
47885b2f94b2SRoopa Prabhu
rtnl_fdb_get(struct sk_buff * in_skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)47895b2f94b2SRoopa Prabhu static int rtnl_fdb_get(struct sk_buff *in_skb, struct nlmsghdr *nlh,
47905b2f94b2SRoopa Prabhu struct netlink_ext_ack *extack)
47915b2f94b2SRoopa Prabhu {
47925b2f94b2SRoopa Prabhu struct net_device *dev = NULL, *br_dev = NULL;
47935b2f94b2SRoopa Prabhu const struct net_device_ops *ops = NULL;
47945b2f94b2SRoopa Prabhu struct net *net = sock_net(in_skb->sk);
47955b2f94b2SRoopa Prabhu struct nlattr *tb[NDA_MAX + 1];
47965b2f94b2SRoopa Prabhu struct sk_buff *skb;
47975b2f94b2SRoopa Prabhu int brport_idx = 0;
47985b2f94b2SRoopa Prabhu u8 ndm_flags = 0;
47995b2f94b2SRoopa Prabhu int br_idx = 0;
48005b2f94b2SRoopa Prabhu u8 *addr = NULL;
48015b2f94b2SRoopa Prabhu u16 vid = 0;
48025b2f94b2SRoopa Prabhu int err;
48035b2f94b2SRoopa Prabhu
48045b2f94b2SRoopa Prabhu err = valid_fdb_get_strict(nlh, tb, &ndm_flags, &br_idx,
48055b2f94b2SRoopa Prabhu &brport_idx, &addr, &vid, extack);
48065b2f94b2SRoopa Prabhu if (err < 0)
48075b2f94b2SRoopa Prabhu return err;
48085b2f94b2SRoopa Prabhu
4809f989d03eSNikolay Aleksandrov if (!addr) {
4810f989d03eSNikolay Aleksandrov NL_SET_ERR_MSG(extack, "Missing lookup address for fdb get request");
4811f989d03eSNikolay Aleksandrov return -EINVAL;
4812f989d03eSNikolay Aleksandrov }
4813f989d03eSNikolay Aleksandrov
48145b2f94b2SRoopa Prabhu if (brport_idx) {
48155b2f94b2SRoopa Prabhu dev = __dev_get_by_index(net, brport_idx);
48165b2f94b2SRoopa Prabhu if (!dev) {
48175b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Unknown device ifindex");
48185b2f94b2SRoopa Prabhu return -ENODEV;
48195b2f94b2SRoopa Prabhu }
48205b2f94b2SRoopa Prabhu }
48215b2f94b2SRoopa Prabhu
48225b2f94b2SRoopa Prabhu if (br_idx) {
48235b2f94b2SRoopa Prabhu if (dev) {
48245b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Master and device are mutually exclusive");
48255b2f94b2SRoopa Prabhu return -EINVAL;
48265b2f94b2SRoopa Prabhu }
48275b2f94b2SRoopa Prabhu
48285b2f94b2SRoopa Prabhu br_dev = __dev_get_by_index(net, br_idx);
48295b2f94b2SRoopa Prabhu if (!br_dev) {
48305b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid master ifindex");
48315b2f94b2SRoopa Prabhu return -EINVAL;
48325b2f94b2SRoopa Prabhu }
48335b2f94b2SRoopa Prabhu ops = br_dev->netdev_ops;
48345b2f94b2SRoopa Prabhu }
48355b2f94b2SRoopa Prabhu
48365b2f94b2SRoopa Prabhu if (dev) {
48375b2f94b2SRoopa Prabhu if (!ndm_flags || (ndm_flags & NTF_MASTER)) {
48382e92a2d0SJulian Wiedmann if (!netif_is_bridge_port(dev)) {
48395b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Device is not a bridge port");
48405b2f94b2SRoopa Prabhu return -EINVAL;
48415b2f94b2SRoopa Prabhu }
48425b2f94b2SRoopa Prabhu br_dev = netdev_master_upper_dev_get(dev);
48435b2f94b2SRoopa Prabhu if (!br_dev) {
48445b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Master of device not found");
48455b2f94b2SRoopa Prabhu return -EINVAL;
48465b2f94b2SRoopa Prabhu }
48475b2f94b2SRoopa Prabhu ops = br_dev->netdev_ops;
48485b2f94b2SRoopa Prabhu } else {
48495b2f94b2SRoopa Prabhu if (!(ndm_flags & NTF_SELF)) {
48505b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Missing NTF_SELF");
48515b2f94b2SRoopa Prabhu return -EINVAL;
48525b2f94b2SRoopa Prabhu }
48535b2f94b2SRoopa Prabhu ops = dev->netdev_ops;
48545b2f94b2SRoopa Prabhu }
48555b2f94b2SRoopa Prabhu }
48565b2f94b2SRoopa Prabhu
48575b2f94b2SRoopa Prabhu if (!br_dev && !dev) {
48585b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "No device specified");
48595b2f94b2SRoopa Prabhu return -ENODEV;
48605b2f94b2SRoopa Prabhu }
48615b2f94b2SRoopa Prabhu
48625b2f94b2SRoopa Prabhu if (!ops || !ops->ndo_fdb_get) {
48635b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Fdb get operation not supported by device");
48645b2f94b2SRoopa Prabhu return -EOPNOTSUPP;
48655b2f94b2SRoopa Prabhu }
48665b2f94b2SRoopa Prabhu
48675b2f94b2SRoopa Prabhu skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
48685b2f94b2SRoopa Prabhu if (!skb)
48695b2f94b2SRoopa Prabhu return -ENOBUFS;
48705b2f94b2SRoopa Prabhu
48715b2f94b2SRoopa Prabhu if (br_dev)
48725b2f94b2SRoopa Prabhu dev = br_dev;
48735b2f94b2SRoopa Prabhu err = ops->ndo_fdb_get(skb, tb, dev, addr, vid,
48745b2f94b2SRoopa Prabhu NETLINK_CB(in_skb).portid,
48755b2f94b2SRoopa Prabhu nlh->nlmsg_seq, extack);
48765b2f94b2SRoopa Prabhu if (err)
48775b2f94b2SRoopa Prabhu goto out;
48785b2f94b2SRoopa Prabhu
48795b2f94b2SRoopa Prabhu return rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
48805b2f94b2SRoopa Prabhu out:
48815b2f94b2SRoopa Prabhu kfree_skb(skb);
48825b2f94b2SRoopa Prabhu return err;
48835b2f94b2SRoopa Prabhu }
48845b2f94b2SRoopa Prabhu
brport_nla_put_flag(struct sk_buff * skb,u32 flags,u32 mask,unsigned int attrnum,unsigned int flag)48852c3c031cSScott Feldman static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
48862c3c031cSScott Feldman unsigned int attrnum, unsigned int flag)
48872c3c031cSScott Feldman {
48882c3c031cSScott Feldman if (mask & flag)
48892c3c031cSScott Feldman return nla_put_u8(skb, attrnum, !!(flags & flag));
48902c3c031cSScott Feldman return 0;
48912c3c031cSScott Feldman }
48922c3c031cSScott Feldman
ndo_dflt_bridge_getlink(struct sk_buff * skb,u32 pid,u32 seq,struct net_device * dev,u16 mode,u32 flags,u32 mask,int nlflags,u32 filter_mask,int (* vlan_fill)(struct sk_buff * skb,struct net_device * dev,u32 filter_mask))4893815cccbfSJohn Fastabend int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
48942c3c031cSScott Feldman struct net_device *dev, u16 mode,
48957d4f8d87SScott Feldman u32 flags, u32 mask, int nlflags,
48967d4f8d87SScott Feldman u32 filter_mask,
48977d4f8d87SScott Feldman int (*vlan_fill)(struct sk_buff *skb,
48987d4f8d87SScott Feldman struct net_device *dev,
48997d4f8d87SScott Feldman u32 filter_mask))
4900815cccbfSJohn Fastabend {
4901815cccbfSJohn Fastabend struct nlmsghdr *nlh;
4902815cccbfSJohn Fastabend struct ifinfomsg *ifm;
4903815cccbfSJohn Fastabend struct nlattr *br_afspec;
49042c3c031cSScott Feldman struct nlattr *protinfo;
4905815cccbfSJohn Fastabend u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
4906898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev);
49077d4f8d87SScott Feldman int err = 0;
4908815cccbfSJohn Fastabend
490946c264daSNicolas Dichtel nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
4910815cccbfSJohn Fastabend if (nlh == NULL)
4911815cccbfSJohn Fastabend return -EMSGSIZE;
4912815cccbfSJohn Fastabend
4913815cccbfSJohn Fastabend ifm = nlmsg_data(nlh);
4914815cccbfSJohn Fastabend ifm->ifi_family = AF_BRIDGE;
4915815cccbfSJohn Fastabend ifm->__ifi_pad = 0;
4916815cccbfSJohn Fastabend ifm->ifi_type = dev->type;
4917815cccbfSJohn Fastabend ifm->ifi_index = dev->ifindex;
4918815cccbfSJohn Fastabend ifm->ifi_flags = dev_get_flags(dev);
4919815cccbfSJohn Fastabend ifm->ifi_change = 0;
4920815cccbfSJohn Fastabend
4921815cccbfSJohn Fastabend
4922815cccbfSJohn Fastabend if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
4923815cccbfSJohn Fastabend nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
4924815cccbfSJohn Fastabend nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
4925898e5061SJiri Pirko (br_dev &&
4926898e5061SJiri Pirko nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
4927815cccbfSJohn Fastabend (dev->addr_len &&
4928815cccbfSJohn Fastabend nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
4929a54acb3aSNicolas Dichtel (dev->ifindex != dev_get_iflink(dev) &&
4930a54acb3aSNicolas Dichtel nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
4931815cccbfSJohn Fastabend goto nla_put_failure;
4932815cccbfSJohn Fastabend
4933ae0be8deSMichal Kubecek br_afspec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
4934815cccbfSJohn Fastabend if (!br_afspec)
4935815cccbfSJohn Fastabend goto nla_put_failure;
4936815cccbfSJohn Fastabend
49371d460b98SRoopa Prabhu if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
4938815cccbfSJohn Fastabend nla_nest_cancel(skb, br_afspec);
4939815cccbfSJohn Fastabend goto nla_put_failure;
4940815cccbfSJohn Fastabend }
49411d460b98SRoopa Prabhu
49421d460b98SRoopa Prabhu if (mode != BRIDGE_MODE_UNDEF) {
49431d460b98SRoopa Prabhu if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
49441d460b98SRoopa Prabhu nla_nest_cancel(skb, br_afspec);
49451d460b98SRoopa Prabhu goto nla_put_failure;
49461d460b98SRoopa Prabhu }
49471d460b98SRoopa Prabhu }
49487d4f8d87SScott Feldman if (vlan_fill) {
49497d4f8d87SScott Feldman err = vlan_fill(skb, dev, filter_mask);
49507d4f8d87SScott Feldman if (err) {
49517d4f8d87SScott Feldman nla_nest_cancel(skb, br_afspec);
49527d4f8d87SScott Feldman goto nla_put_failure;
49537d4f8d87SScott Feldman }
49547d4f8d87SScott Feldman }
4955815cccbfSJohn Fastabend nla_nest_end(skb, br_afspec);
4956815cccbfSJohn Fastabend
4957ae0be8deSMichal Kubecek protinfo = nla_nest_start(skb, IFLA_PROTINFO);
49582c3c031cSScott Feldman if (!protinfo)
49592c3c031cSScott Feldman goto nla_put_failure;
49602c3c031cSScott Feldman
49612c3c031cSScott Feldman if (brport_nla_put_flag(skb, flags, mask,
49622c3c031cSScott Feldman IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
49632c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask,
49642c3c031cSScott Feldman IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
49652c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask,
49662c3c031cSScott Feldman IFLA_BRPORT_FAST_LEAVE,
49672c3c031cSScott Feldman BR_MULTICAST_FAST_LEAVE) ||
49682c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask,
49692c3c031cSScott Feldman IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
49702c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask,
49712c3c031cSScott Feldman IFLA_BRPORT_LEARNING, BR_LEARNING) ||
49722c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask,
49732c3c031cSScott Feldman IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
49742c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask,
49752c3c031cSScott Feldman IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
49762c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask,
4977583cb0b4SJulian Wiedmann IFLA_BRPORT_PROXYARP, BR_PROXYARP) ||
4978583cb0b4SJulian Wiedmann brport_nla_put_flag(skb, flags, mask,
4979583cb0b4SJulian Wiedmann IFLA_BRPORT_MCAST_FLOOD, BR_MCAST_FLOOD) ||
4980583cb0b4SJulian Wiedmann brport_nla_put_flag(skb, flags, mask,
4981583cb0b4SJulian Wiedmann IFLA_BRPORT_BCAST_FLOOD, BR_BCAST_FLOOD)) {
49822c3c031cSScott Feldman nla_nest_cancel(skb, protinfo);
49832c3c031cSScott Feldman goto nla_put_failure;
49842c3c031cSScott Feldman }
49852c3c031cSScott Feldman
49862c3c031cSScott Feldman nla_nest_end(skb, protinfo);
49872c3c031cSScott Feldman
4988053c095aSJohannes Berg nlmsg_end(skb, nlh);
4989053c095aSJohannes Berg return 0;
4990815cccbfSJohn Fastabend nla_put_failure:
4991815cccbfSJohn Fastabend nlmsg_cancel(skb, nlh);
49927d4f8d87SScott Feldman return err ? err : -EMSGSIZE;
4993815cccbfSJohn Fastabend }
49947d4f8d87SScott Feldman EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
4995815cccbfSJohn Fastabend
valid_bridge_getlink_req(const struct nlmsghdr * nlh,bool strict_check,u32 * filter_mask,struct netlink_ext_ack * extack)49962d011be8SDavid Ahern static int valid_bridge_getlink_req(const struct nlmsghdr *nlh,
49972d011be8SDavid Ahern bool strict_check, u32 *filter_mask,
49982d011be8SDavid Ahern struct netlink_ext_ack *extack)
49992d011be8SDavid Ahern {
50002d011be8SDavid Ahern struct nlattr *tb[IFLA_MAX+1];
50012d011be8SDavid Ahern int err, i;
50022d011be8SDavid Ahern
50032d011be8SDavid Ahern if (strict_check) {
50042d011be8SDavid Ahern struct ifinfomsg *ifm;
50052d011be8SDavid Ahern
50062d011be8SDavid Ahern if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
50072d011be8SDavid Ahern NL_SET_ERR_MSG(extack, "Invalid header for bridge link dump");
50082d011be8SDavid Ahern return -EINVAL;
50092d011be8SDavid Ahern }
50102d011be8SDavid Ahern
50112d011be8SDavid Ahern ifm = nlmsg_data(nlh);
50122d011be8SDavid Ahern if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
50132d011be8SDavid Ahern ifm->ifi_change || ifm->ifi_index) {
50142d011be8SDavid Ahern NL_SET_ERR_MSG(extack, "Invalid values in header for bridge link dump request");
50152d011be8SDavid Ahern return -EINVAL;
50162d011be8SDavid Ahern }
50172d011be8SDavid Ahern
50188cb08174SJohannes Berg err = nlmsg_parse_deprecated_strict(nlh,
50198cb08174SJohannes Berg sizeof(struct ifinfomsg),
50208cb08174SJohannes Berg tb, IFLA_MAX, ifla_policy,
50218cb08174SJohannes Berg extack);
50222d011be8SDavid Ahern } else {
50238cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
50248cb08174SJohannes Berg tb, IFLA_MAX, ifla_policy,
50258cb08174SJohannes Berg extack);
50262d011be8SDavid Ahern }
50272d011be8SDavid Ahern if (err < 0)
50282d011be8SDavid Ahern return err;
50292d011be8SDavid Ahern
50302d011be8SDavid Ahern /* new attributes should only be added with strict checking */
50312d011be8SDavid Ahern for (i = 0; i <= IFLA_MAX; ++i) {
50322d011be8SDavid Ahern if (!tb[i])
50332d011be8SDavid Ahern continue;
50342d011be8SDavid Ahern
50352d011be8SDavid Ahern switch (i) {
50362d011be8SDavid Ahern case IFLA_EXT_MASK:
50372d011be8SDavid Ahern *filter_mask = nla_get_u32(tb[i]);
50382d011be8SDavid Ahern break;
50392d011be8SDavid Ahern default:
50402d011be8SDavid Ahern if (strict_check) {
50412d011be8SDavid Ahern NL_SET_ERR_MSG(extack, "Unsupported attribute in bridge link dump request");
50422d011be8SDavid Ahern return -EINVAL;
50432d011be8SDavid Ahern }
50442d011be8SDavid Ahern }
50452d011be8SDavid Ahern }
50462d011be8SDavid Ahern
50472d011be8SDavid Ahern return 0;
50482d011be8SDavid Ahern }
50492d011be8SDavid Ahern
rtnl_bridge_getlink(struct sk_buff * skb,struct netlink_callback * cb)5050e5a55a89SJohn Fastabend static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
5051e5a55a89SJohn Fastabend {
50522d011be8SDavid Ahern const struct nlmsghdr *nlh = cb->nlh;
5053e5a55a89SJohn Fastabend struct net *net = sock_net(skb->sk);
5054e5a55a89SJohn Fastabend struct net_device *dev;
5055e5a55a89SJohn Fastabend int idx = 0;
5056e5a55a89SJohn Fastabend u32 portid = NETLINK_CB(cb->skb).portid;
50572d011be8SDavid Ahern u32 seq = nlh->nlmsg_seq;
50586cbdceebSVlad Yasevich u32 filter_mask = 0;
5059d64f69b0SRoopa Prabhu int err;
50606cbdceebSVlad Yasevich
50612d011be8SDavid Ahern err = valid_bridge_getlink_req(nlh, cb->strict_check, &filter_mask,
50622d011be8SDavid Ahern cb->extack);
50632d011be8SDavid Ahern if (err < 0 && cb->strict_check)
50642d011be8SDavid Ahern return err;
5065e5a55a89SJohn Fastabend
5066e5a55a89SJohn Fastabend rcu_read_lock();
5067e5a55a89SJohn Fastabend for_each_netdev_rcu(net, dev) {
5068e5a55a89SJohn Fastabend const struct net_device_ops *ops = dev->netdev_ops;
5069898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev);
5070e5a55a89SJohn Fastabend
5071898e5061SJiri Pirko if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
5072d64f69b0SRoopa Prabhu if (idx >= cb->args[0]) {
5073d64f69b0SRoopa Prabhu err = br_dev->netdev_ops->ndo_bridge_getlink(
5074d64f69b0SRoopa Prabhu skb, portid, seq, dev,
5075d64f69b0SRoopa Prabhu filter_mask, NLM_F_MULTI);
5076f6c5775fSDavid Ahern if (err < 0 && err != -EOPNOTSUPP) {
5077f6c5775fSDavid Ahern if (likely(skb->len))
5078e5a55a89SJohn Fastabend break;
5079f6c5775fSDavid Ahern
5080f6c5775fSDavid Ahern goto out_err;
5081f6c5775fSDavid Ahern }
5082d64f69b0SRoopa Prabhu }
5083e5a55a89SJohn Fastabend idx++;
5084e5a55a89SJohn Fastabend }
5085e5a55a89SJohn Fastabend
5086e5a55a89SJohn Fastabend if (ops->ndo_bridge_getlink) {
5087d64f69b0SRoopa Prabhu if (idx >= cb->args[0]) {
5088d64f69b0SRoopa Prabhu err = ops->ndo_bridge_getlink(skb, portid,
5089d64f69b0SRoopa Prabhu seq, dev,
509046c264daSNicolas Dichtel filter_mask,
5091d64f69b0SRoopa Prabhu NLM_F_MULTI);
5092f6c5775fSDavid Ahern if (err < 0 && err != -EOPNOTSUPP) {
5093f6c5775fSDavid Ahern if (likely(skb->len))
5094e5a55a89SJohn Fastabend break;
5095f6c5775fSDavid Ahern
5096f6c5775fSDavid Ahern goto out_err;
5097f6c5775fSDavid Ahern }
5098d64f69b0SRoopa Prabhu }
5099e5a55a89SJohn Fastabend idx++;
5100e5a55a89SJohn Fastabend }
5101e5a55a89SJohn Fastabend }
5102f6c5775fSDavid Ahern err = skb->len;
5103f6c5775fSDavid Ahern out_err:
5104e5a55a89SJohn Fastabend rcu_read_unlock();
5105e5a55a89SJohn Fastabend cb->args[0] = idx;
5106e5a55a89SJohn Fastabend
5107f6c5775fSDavid Ahern return err;
5108e5a55a89SJohn Fastabend }
5109e5a55a89SJohn Fastabend
bridge_nlmsg_size(void)51102469ffd7SJohn Fastabend static inline size_t bridge_nlmsg_size(void)
51112469ffd7SJohn Fastabend {
51122469ffd7SJohn Fastabend return NLMSG_ALIGN(sizeof(struct ifinfomsg))
51132469ffd7SJohn Fastabend + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
51142469ffd7SJohn Fastabend + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
51152469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
51162469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_MTU */
51172469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_LINK */
51182469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
51192469ffd7SJohn Fastabend + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
51202469ffd7SJohn Fastabend + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
51212469ffd7SJohn Fastabend + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
51222469ffd7SJohn Fastabend + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
51232469ffd7SJohn Fastabend }
51242469ffd7SJohn Fastabend
rtnl_bridge_notify(struct net_device * dev)512502dba438SRoopa Prabhu static int rtnl_bridge_notify(struct net_device *dev)
51262469ffd7SJohn Fastabend {
51272469ffd7SJohn Fastabend struct net *net = dev_net(dev);
51282469ffd7SJohn Fastabend struct sk_buff *skb;
51292469ffd7SJohn Fastabend int err = -EOPNOTSUPP;
51302469ffd7SJohn Fastabend
513102dba438SRoopa Prabhu if (!dev->netdev_ops->ndo_bridge_getlink)
513202dba438SRoopa Prabhu return 0;
513302dba438SRoopa Prabhu
51342469ffd7SJohn Fastabend skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
51352469ffd7SJohn Fastabend if (!skb) {
51362469ffd7SJohn Fastabend err = -ENOMEM;
51372469ffd7SJohn Fastabend goto errout;
51382469ffd7SJohn Fastabend }
51392469ffd7SJohn Fastabend
514046c264daSNicolas Dichtel err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
5141c38e01b8SJohn Fastabend if (err < 0)
5142c38e01b8SJohn Fastabend goto errout;
51432469ffd7SJohn Fastabend
5144d2e381c4SIdo Schimmel /* Notification info is only filled for bridge ports, not the bridge
5145d2e381c4SIdo Schimmel * device itself. Therefore, a zero notification length is valid and
5146d2e381c4SIdo Schimmel * should not result in an error.
5147d2e381c4SIdo Schimmel */
5148d2e381c4SIdo Schimmel if (!skb->len)
514959ccaaaaSRoopa Prabhu goto errout;
515059ccaaaaSRoopa Prabhu
51512469ffd7SJohn Fastabend rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
51522469ffd7SJohn Fastabend return 0;
51532469ffd7SJohn Fastabend errout:
51542469ffd7SJohn Fastabend WARN_ON(err == -EMSGSIZE);
51552469ffd7SJohn Fastabend kfree_skb(skb);
515659ccaaaaSRoopa Prabhu if (err)
51572469ffd7SJohn Fastabend rtnl_set_sk_err(net, RTNLGRP_LINK, err);
51582469ffd7SJohn Fastabend return err;
51592469ffd7SJohn Fastabend }
51602469ffd7SJohn Fastabend
rtnl_bridge_setlink(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)5161c21ef3e3SDavid Ahern static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
5162c21ef3e3SDavid Ahern struct netlink_ext_ack *extack)
5163e5a55a89SJohn Fastabend {
5164e5a55a89SJohn Fastabend struct net *net = sock_net(skb->sk);
5165e5a55a89SJohn Fastabend struct ifinfomsg *ifm;
5166e5a55a89SJohn Fastabend struct net_device *dev;
5167167d8642SLin Ma struct nlattr *br_spec, *attr, *br_flags_attr = NULL;
51682469ffd7SJohn Fastabend int rem, err = -EOPNOTSUPP;
51694de8b413SRosen, Rami u16 flags = 0;
5170e5a55a89SJohn Fastabend
5171e5a55a89SJohn Fastabend if (nlmsg_len(nlh) < sizeof(*ifm))
5172e5a55a89SJohn Fastabend return -EINVAL;
5173e5a55a89SJohn Fastabend
5174e5a55a89SJohn Fastabend ifm = nlmsg_data(nlh);
5175e5a55a89SJohn Fastabend if (ifm->ifi_family != AF_BRIDGE)
5176e5a55a89SJohn Fastabend return -EPFNOSUPPORT;
5177e5a55a89SJohn Fastabend
5178e5a55a89SJohn Fastabend dev = __dev_get_by_index(net, ifm->ifi_index);
5179e5a55a89SJohn Fastabend if (!dev) {
5180b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex");
5181e5a55a89SJohn Fastabend return -ENODEV;
5182e5a55a89SJohn Fastabend }
5183e5a55a89SJohn Fastabend
51842469ffd7SJohn Fastabend br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
51852469ffd7SJohn Fastabend if (br_spec) {
51862469ffd7SJohn Fastabend nla_for_each_nested(attr, br_spec, rem) {
5187167d8642SLin Ma if (nla_type(attr) == IFLA_BRIDGE_FLAGS && !br_flags_attr) {
51886e8d1c55SThomas Graf if (nla_len(attr) < sizeof(flags))
51896e8d1c55SThomas Graf return -EINVAL;
51906e8d1c55SThomas Graf
5191167d8642SLin Ma br_flags_attr = attr;
51922469ffd7SJohn Fastabend flags = nla_get_u16(attr);
5193d73ef2d6SLin Ma }
5194d73ef2d6SLin Ma
5195d73ef2d6SLin Ma if (nla_type(attr) == IFLA_BRIDGE_MODE) {
5196d73ef2d6SLin Ma if (nla_len(attr) < sizeof(u16))
5197d73ef2d6SLin Ma return -EINVAL;
51982469ffd7SJohn Fastabend }
51992469ffd7SJohn Fastabend }
52002469ffd7SJohn Fastabend }
52012469ffd7SJohn Fastabend
52022469ffd7SJohn Fastabend if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
5203898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev);
5204898e5061SJiri Pirko
5205898e5061SJiri Pirko if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
52062469ffd7SJohn Fastabend err = -EOPNOTSUPP;
5207e5a55a89SJohn Fastabend goto out;
5208e5a55a89SJohn Fastabend }
5209e5a55a89SJohn Fastabend
52102fd527b7SPetr Machata err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags,
52112fd527b7SPetr Machata extack);
52122469ffd7SJohn Fastabend if (err)
52132469ffd7SJohn Fastabend goto out;
52142469ffd7SJohn Fastabend
52152469ffd7SJohn Fastabend flags &= ~BRIDGE_FLAGS_MASTER;
52162469ffd7SJohn Fastabend }
52172469ffd7SJohn Fastabend
52182469ffd7SJohn Fastabend if ((flags & BRIDGE_FLAGS_SELF)) {
52192469ffd7SJohn Fastabend if (!dev->netdev_ops->ndo_bridge_setlink)
52202469ffd7SJohn Fastabend err = -EOPNOTSUPP;
52212469ffd7SJohn Fastabend else
5222add511b3SRoopa Prabhu err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
52232fd527b7SPetr Machata flags,
52242fd527b7SPetr Machata extack);
522502dba438SRoopa Prabhu if (!err) {
52262469ffd7SJohn Fastabend flags &= ~BRIDGE_FLAGS_SELF;
522702dba438SRoopa Prabhu
522802dba438SRoopa Prabhu /* Generate event to notify upper layer of bridge
522902dba438SRoopa Prabhu * change
523002dba438SRoopa Prabhu */
523102dba438SRoopa Prabhu err = rtnl_bridge_notify(dev);
523202dba438SRoopa Prabhu }
52332469ffd7SJohn Fastabend }
52342469ffd7SJohn Fastabend
5235167d8642SLin Ma if (br_flags_attr)
5236167d8642SLin Ma memcpy(nla_data(br_flags_attr), &flags, sizeof(flags));
5237e5a55a89SJohn Fastabend out:
5238e5a55a89SJohn Fastabend return err;
5239e5a55a89SJohn Fastabend }
5240e5a55a89SJohn Fastabend
rtnl_bridge_dellink(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)5241c21ef3e3SDavid Ahern static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
5242c21ef3e3SDavid Ahern struct netlink_ext_ack *extack)
5243407af329SVlad Yasevich {
5244407af329SVlad Yasevich struct net *net = sock_net(skb->sk);
5245407af329SVlad Yasevich struct ifinfomsg *ifm;
5246407af329SVlad Yasevich struct net_device *dev;
5247407af329SVlad Yasevich struct nlattr *br_spec, *attr = NULL;
5248407af329SVlad Yasevich int rem, err = -EOPNOTSUPP;
52494de8b413SRosen, Rami u16 flags = 0;
5250407af329SVlad Yasevich bool have_flags = false;
5251407af329SVlad Yasevich
5252407af329SVlad Yasevich if (nlmsg_len(nlh) < sizeof(*ifm))
5253407af329SVlad Yasevich return -EINVAL;
5254407af329SVlad Yasevich
5255407af329SVlad Yasevich ifm = nlmsg_data(nlh);
5256407af329SVlad Yasevich if (ifm->ifi_family != AF_BRIDGE)
5257407af329SVlad Yasevich return -EPFNOSUPPORT;
5258407af329SVlad Yasevich
5259407af329SVlad Yasevich dev = __dev_get_by_index(net, ifm->ifi_index);
5260407af329SVlad Yasevich if (!dev) {
5261b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex");
5262407af329SVlad Yasevich return -ENODEV;
5263407af329SVlad Yasevich }
5264407af329SVlad Yasevich
5265407af329SVlad Yasevich br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
5266407af329SVlad Yasevich if (br_spec) {
5267407af329SVlad Yasevich nla_for_each_nested(attr, br_spec, rem) {
5268407af329SVlad Yasevich if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
52696e8d1c55SThomas Graf if (nla_len(attr) < sizeof(flags))
52706e8d1c55SThomas Graf return -EINVAL;
52716e8d1c55SThomas Graf
5272407af329SVlad Yasevich have_flags = true;
5273407af329SVlad Yasevich flags = nla_get_u16(attr);
5274407af329SVlad Yasevich break;
5275407af329SVlad Yasevich }
5276407af329SVlad Yasevich }
5277407af329SVlad Yasevich }
5278407af329SVlad Yasevich
5279407af329SVlad Yasevich if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
5280407af329SVlad Yasevich struct net_device *br_dev = netdev_master_upper_dev_get(dev);
5281407af329SVlad Yasevich
5282407af329SVlad Yasevich if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
5283407af329SVlad Yasevich err = -EOPNOTSUPP;
5284407af329SVlad Yasevich goto out;
5285407af329SVlad Yasevich }
5286407af329SVlad Yasevich
5287add511b3SRoopa Prabhu err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
5288407af329SVlad Yasevich if (err)
5289407af329SVlad Yasevich goto out;
5290407af329SVlad Yasevich
5291407af329SVlad Yasevich flags &= ~BRIDGE_FLAGS_MASTER;
5292407af329SVlad Yasevich }
5293407af329SVlad Yasevich
5294407af329SVlad Yasevich if ((flags & BRIDGE_FLAGS_SELF)) {
5295407af329SVlad Yasevich if (!dev->netdev_ops->ndo_bridge_dellink)
5296407af329SVlad Yasevich err = -EOPNOTSUPP;
5297407af329SVlad Yasevich else
5298add511b3SRoopa Prabhu err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
5299add511b3SRoopa Prabhu flags);
5300407af329SVlad Yasevich
530102dba438SRoopa Prabhu if (!err) {
5302407af329SVlad Yasevich flags &= ~BRIDGE_FLAGS_SELF;
530302dba438SRoopa Prabhu
530402dba438SRoopa Prabhu /* Generate event to notify upper layer of bridge
530502dba438SRoopa Prabhu * change
530602dba438SRoopa Prabhu */
530702dba438SRoopa Prabhu err = rtnl_bridge_notify(dev);
530802dba438SRoopa Prabhu }
5309407af329SVlad Yasevich }
5310407af329SVlad Yasevich
5311407af329SVlad Yasevich if (have_flags)
5312407af329SVlad Yasevich memcpy(nla_data(attr), &flags, sizeof(flags));
5313407af329SVlad Yasevich out:
5314407af329SVlad Yasevich return err;
5315407af329SVlad Yasevich }
5316407af329SVlad Yasevich
stats_attr_valid(unsigned int mask,int attrid,int idxattr)5317e8872a25SNikolay Aleksandrov static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
5318e8872a25SNikolay Aleksandrov {
5319e8872a25SNikolay Aleksandrov return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
5320e8872a25SNikolay Aleksandrov (!idxattr || idxattr == attrid);
5321e8872a25SNikolay Aleksandrov }
5322e8872a25SNikolay Aleksandrov
5323f6e0fb81SPetr Machata static bool
rtnl_offload_xstats_have_ndo(const struct net_device * dev,int attr_id)5324f6e0fb81SPetr Machata rtnl_offload_xstats_have_ndo(const struct net_device *dev, int attr_id)
532569ae6ad2SNogah Frankel {
5326f6e0fb81SPetr Machata return dev->netdev_ops &&
5327f6e0fb81SPetr Machata dev->netdev_ops->ndo_has_offload_stats &&
5328f6e0fb81SPetr Machata dev->netdev_ops->ndo_get_offload_stats &&
5329f6e0fb81SPetr Machata dev->netdev_ops->ndo_has_offload_stats(dev, attr_id);
533069ae6ad2SNogah Frankel }
533169ae6ad2SNogah Frankel
5332f6e0fb81SPetr Machata static unsigned int
rtnl_offload_xstats_get_size_ndo(const struct net_device * dev,int attr_id)5333f6e0fb81SPetr Machata rtnl_offload_xstats_get_size_ndo(const struct net_device *dev, int attr_id)
5334f6e0fb81SPetr Machata {
5335f6e0fb81SPetr Machata return rtnl_offload_xstats_have_ndo(dev, attr_id) ?
5336f6e0fb81SPetr Machata sizeof(struct rtnl_link_stats64) : 0;
5337f6e0fb81SPetr Machata }
5338f6e0fb81SPetr Machata
5339f6e0fb81SPetr Machata static int
rtnl_offload_xstats_fill_ndo(struct net_device * dev,int attr_id,struct sk_buff * skb)5340f6e0fb81SPetr Machata rtnl_offload_xstats_fill_ndo(struct net_device *dev, int attr_id,
5341f6e0fb81SPetr Machata struct sk_buff *skb)
5342f6e0fb81SPetr Machata {
5343f6e0fb81SPetr Machata unsigned int size = rtnl_offload_xstats_get_size_ndo(dev, attr_id);
5344f6e0fb81SPetr Machata struct nlattr *attr = NULL;
5345f6e0fb81SPetr Machata void *attr_data;
5346f6e0fb81SPetr Machata int err;
5347f6e0fb81SPetr Machata
5348f6e0fb81SPetr Machata if (!size)
5349f6e0fb81SPetr Machata return -ENODATA;
5350f6e0fb81SPetr Machata
5351f6e0fb81SPetr Machata attr = nla_reserve_64bit(skb, attr_id, size,
5352f6e0fb81SPetr Machata IFLA_OFFLOAD_XSTATS_UNSPEC);
5353f6e0fb81SPetr Machata if (!attr)
5354f6e0fb81SPetr Machata return -EMSGSIZE;
5355f6e0fb81SPetr Machata
5356f6e0fb81SPetr Machata attr_data = nla_data(attr);
5357f6e0fb81SPetr Machata memset(attr_data, 0, size);
5358f6e0fb81SPetr Machata
5359f6e0fb81SPetr Machata err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev, attr_data);
5360f6e0fb81SPetr Machata if (err)
5361f6e0fb81SPetr Machata return err;
5362f6e0fb81SPetr Machata
536369ae6ad2SNogah Frankel return 0;
536469ae6ad2SNogah Frankel }
536569ae6ad2SNogah Frankel
53660e7788fdSPetr Machata static unsigned int
rtnl_offload_xstats_get_size_stats(const struct net_device * dev,enum netdev_offload_xstats_type type)53670e7788fdSPetr Machata rtnl_offload_xstats_get_size_stats(const struct net_device *dev,
53680e7788fdSPetr Machata enum netdev_offload_xstats_type type)
53690e7788fdSPetr Machata {
53700e7788fdSPetr Machata bool enabled = netdev_offload_xstats_enabled(dev, type);
53710e7788fdSPetr Machata
53720e7788fdSPetr Machata return enabled ? sizeof(struct rtnl_hw_stats64) : 0;
53730e7788fdSPetr Machata }
53740e7788fdSPetr Machata
53750e7788fdSPetr Machata struct rtnl_offload_xstats_request_used {
53760e7788fdSPetr Machata bool request;
53770e7788fdSPetr Machata bool used;
53780e7788fdSPetr Machata };
53790e7788fdSPetr Machata
53800e7788fdSPetr Machata static int
rtnl_offload_xstats_get_stats(struct net_device * dev,enum netdev_offload_xstats_type type,struct rtnl_offload_xstats_request_used * ru,struct rtnl_hw_stats64 * stats,struct netlink_ext_ack * extack)53810e7788fdSPetr Machata rtnl_offload_xstats_get_stats(struct net_device *dev,
53820e7788fdSPetr Machata enum netdev_offload_xstats_type type,
53830e7788fdSPetr Machata struct rtnl_offload_xstats_request_used *ru,
53840e7788fdSPetr Machata struct rtnl_hw_stats64 *stats,
53850e7788fdSPetr Machata struct netlink_ext_ack *extack)
53860e7788fdSPetr Machata {
53870e7788fdSPetr Machata bool request;
53880e7788fdSPetr Machata bool used;
53890e7788fdSPetr Machata int err;
53900e7788fdSPetr Machata
53910e7788fdSPetr Machata request = netdev_offload_xstats_enabled(dev, type);
53920e7788fdSPetr Machata if (!request) {
53930e7788fdSPetr Machata used = false;
53940e7788fdSPetr Machata goto out;
53950e7788fdSPetr Machata }
53960e7788fdSPetr Machata
53970e7788fdSPetr Machata err = netdev_offload_xstats_get(dev, type, stats, &used, extack);
53980e7788fdSPetr Machata if (err)
53990e7788fdSPetr Machata return err;
54000e7788fdSPetr Machata
54010e7788fdSPetr Machata out:
54020e7788fdSPetr Machata if (ru) {
54030e7788fdSPetr Machata ru->request = request;
54040e7788fdSPetr Machata ru->used = used;
54050e7788fdSPetr Machata }
54060e7788fdSPetr Machata return 0;
54070e7788fdSPetr Machata }
54080e7788fdSPetr Machata
54090e7788fdSPetr Machata static int
rtnl_offload_xstats_fill_hw_s_info_one(struct sk_buff * skb,int attr_id,struct rtnl_offload_xstats_request_used * ru)54100e7788fdSPetr Machata rtnl_offload_xstats_fill_hw_s_info_one(struct sk_buff *skb, int attr_id,
54110e7788fdSPetr Machata struct rtnl_offload_xstats_request_used *ru)
54120e7788fdSPetr Machata {
54130e7788fdSPetr Machata struct nlattr *nest;
54140e7788fdSPetr Machata
54150e7788fdSPetr Machata nest = nla_nest_start(skb, attr_id);
54160e7788fdSPetr Machata if (!nest)
54170e7788fdSPetr Machata return -EMSGSIZE;
54180e7788fdSPetr Machata
54190e7788fdSPetr Machata if (nla_put_u8(skb, IFLA_OFFLOAD_XSTATS_HW_S_INFO_REQUEST, ru->request))
54200e7788fdSPetr Machata goto nla_put_failure;
54210e7788fdSPetr Machata
54220e7788fdSPetr Machata if (nla_put_u8(skb, IFLA_OFFLOAD_XSTATS_HW_S_INFO_USED, ru->used))
54230e7788fdSPetr Machata goto nla_put_failure;
54240e7788fdSPetr Machata
54250e7788fdSPetr Machata nla_nest_end(skb, nest);
54260e7788fdSPetr Machata return 0;
54270e7788fdSPetr Machata
54280e7788fdSPetr Machata nla_put_failure:
54290e7788fdSPetr Machata nla_nest_cancel(skb, nest);
54300e7788fdSPetr Machata return -EMSGSIZE;
54310e7788fdSPetr Machata }
54320e7788fdSPetr Machata
54330e7788fdSPetr Machata static int
rtnl_offload_xstats_fill_hw_s_info(struct sk_buff * skb,struct net_device * dev,struct netlink_ext_ack * extack)54340e7788fdSPetr Machata rtnl_offload_xstats_fill_hw_s_info(struct sk_buff *skb, struct net_device *dev,
54350e7788fdSPetr Machata struct netlink_ext_ack *extack)
54360e7788fdSPetr Machata {
54370e7788fdSPetr Machata enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3;
54380e7788fdSPetr Machata struct rtnl_offload_xstats_request_used ru_l3;
54390e7788fdSPetr Machata struct nlattr *nest;
54400e7788fdSPetr Machata int err;
54410e7788fdSPetr Machata
54420e7788fdSPetr Machata err = rtnl_offload_xstats_get_stats(dev, t_l3, &ru_l3, NULL, extack);
54430e7788fdSPetr Machata if (err)
54440e7788fdSPetr Machata return err;
54450e7788fdSPetr Machata
54460e7788fdSPetr Machata nest = nla_nest_start(skb, IFLA_OFFLOAD_XSTATS_HW_S_INFO);
54470e7788fdSPetr Machata if (!nest)
54480e7788fdSPetr Machata return -EMSGSIZE;
54490e7788fdSPetr Machata
54500e7788fdSPetr Machata if (rtnl_offload_xstats_fill_hw_s_info_one(skb,
54510e7788fdSPetr Machata IFLA_OFFLOAD_XSTATS_L3_STATS,
54520e7788fdSPetr Machata &ru_l3))
54530e7788fdSPetr Machata goto nla_put_failure;
54540e7788fdSPetr Machata
54550e7788fdSPetr Machata nla_nest_end(skb, nest);
54560e7788fdSPetr Machata return 0;
54570e7788fdSPetr Machata
54580e7788fdSPetr Machata nla_put_failure:
54590e7788fdSPetr Machata nla_nest_cancel(skb, nest);
54600e7788fdSPetr Machata return -EMSGSIZE;
54610e7788fdSPetr Machata }
54620e7788fdSPetr Machata
rtnl_offload_xstats_fill(struct sk_buff * skb,struct net_device * dev,int * prividx,u32 off_filter_mask,struct netlink_ext_ack * extack)54636b524a1dSPetr Machata static int rtnl_offload_xstats_fill(struct sk_buff *skb, struct net_device *dev,
546405415bccSPetr Machata int *prividx, u32 off_filter_mask,
546505415bccSPetr Machata struct netlink_ext_ack *extack)
546669ae6ad2SNogah Frankel {
54670e7788fdSPetr Machata enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3;
54680e7788fdSPetr Machata int attr_id_hw_s_info = IFLA_OFFLOAD_XSTATS_HW_S_INFO;
54690e7788fdSPetr Machata int attr_id_l3_stats = IFLA_OFFLOAD_XSTATS_L3_STATS;
5470f6e0fb81SPetr Machata int attr_id_cpu_hit = IFLA_OFFLOAD_XSTATS_CPU_HIT;
5471f6e0fb81SPetr Machata bool have_data = false;
547269ae6ad2SNogah Frankel int err;
547369ae6ad2SNogah Frankel
547446efc97bSPetr Machata if (*prividx <= attr_id_cpu_hit &&
547546efc97bSPetr Machata (off_filter_mask &
547646efc97bSPetr Machata IFLA_STATS_FILTER_BIT(attr_id_cpu_hit))) {
5477f6e0fb81SPetr Machata err = rtnl_offload_xstats_fill_ndo(dev, attr_id_cpu_hit, skb);
5478f6e0fb81SPetr Machata if (!err) {
5479f6e0fb81SPetr Machata have_data = true;
5480f6e0fb81SPetr Machata } else if (err != -ENODATA) {
5481f6e0fb81SPetr Machata *prividx = attr_id_cpu_hit;
5482f6e0fb81SPetr Machata return err;
5483f6e0fb81SPetr Machata }
548469ae6ad2SNogah Frankel }
548569ae6ad2SNogah Frankel
54860e7788fdSPetr Machata if (*prividx <= attr_id_hw_s_info &&
54870e7788fdSPetr Machata (off_filter_mask & IFLA_STATS_FILTER_BIT(attr_id_hw_s_info))) {
54880e7788fdSPetr Machata *prividx = attr_id_hw_s_info;
54890e7788fdSPetr Machata
54900e7788fdSPetr Machata err = rtnl_offload_xstats_fill_hw_s_info(skb, dev, extack);
54910e7788fdSPetr Machata if (err)
54920e7788fdSPetr Machata return err;
54930e7788fdSPetr Machata
54940e7788fdSPetr Machata have_data = true;
54950e7788fdSPetr Machata *prividx = 0;
54960e7788fdSPetr Machata }
54970e7788fdSPetr Machata
54980e7788fdSPetr Machata if (*prividx <= attr_id_l3_stats &&
54990e7788fdSPetr Machata (off_filter_mask & IFLA_STATS_FILTER_BIT(attr_id_l3_stats))) {
55000e7788fdSPetr Machata unsigned int size_l3;
55010e7788fdSPetr Machata struct nlattr *attr;
55020e7788fdSPetr Machata
55030e7788fdSPetr Machata *prividx = attr_id_l3_stats;
55040e7788fdSPetr Machata
55050e7788fdSPetr Machata size_l3 = rtnl_offload_xstats_get_size_stats(dev, t_l3);
550623cfe941SPetr Machata if (!size_l3)
550723cfe941SPetr Machata goto skip_l3_stats;
55080e7788fdSPetr Machata attr = nla_reserve_64bit(skb, attr_id_l3_stats, size_l3,
55090e7788fdSPetr Machata IFLA_OFFLOAD_XSTATS_UNSPEC);
55100e7788fdSPetr Machata if (!attr)
55110e7788fdSPetr Machata return -EMSGSIZE;
55120e7788fdSPetr Machata
55130e7788fdSPetr Machata err = rtnl_offload_xstats_get_stats(dev, t_l3, NULL,
55140e7788fdSPetr Machata nla_data(attr), extack);
55150e7788fdSPetr Machata if (err)
55160e7788fdSPetr Machata return err;
55170e7788fdSPetr Machata
55180e7788fdSPetr Machata have_data = true;
551923cfe941SPetr Machata skip_l3_stats:
55200e7788fdSPetr Machata *prividx = 0;
55210e7788fdSPetr Machata }
55220e7788fdSPetr Machata
5523f6e0fb81SPetr Machata if (!have_data)
552469ae6ad2SNogah Frankel return -ENODATA;
552569ae6ad2SNogah Frankel
552669ae6ad2SNogah Frankel *prividx = 0;
552769ae6ad2SNogah Frankel return 0;
552869ae6ad2SNogah Frankel }
552969ae6ad2SNogah Frankel
55300e7788fdSPetr Machata static unsigned int
rtnl_offload_xstats_get_size_hw_s_info_one(const struct net_device * dev,enum netdev_offload_xstats_type type)55310e7788fdSPetr Machata rtnl_offload_xstats_get_size_hw_s_info_one(const struct net_device *dev,
55320e7788fdSPetr Machata enum netdev_offload_xstats_type type)
55330e7788fdSPetr Machata {
55340e7788fdSPetr Machata return nla_total_size(0) +
55350e7788fdSPetr Machata /* IFLA_OFFLOAD_XSTATS_HW_S_INFO_REQUEST */
55360e7788fdSPetr Machata nla_total_size(sizeof(u8)) +
55370e7788fdSPetr Machata /* IFLA_OFFLOAD_XSTATS_HW_S_INFO_USED */
5538503930f8SChristoph Paasch nla_total_size(sizeof(u8)) +
55390e7788fdSPetr Machata 0;
55400e7788fdSPetr Machata }
55410e7788fdSPetr Machata
55420e7788fdSPetr Machata static unsigned int
rtnl_offload_xstats_get_size_hw_s_info(const struct net_device * dev)55430e7788fdSPetr Machata rtnl_offload_xstats_get_size_hw_s_info(const struct net_device *dev)
55440e7788fdSPetr Machata {
55450e7788fdSPetr Machata enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3;
55460e7788fdSPetr Machata
55470e7788fdSPetr Machata return nla_total_size(0) +
55480e7788fdSPetr Machata /* IFLA_OFFLOAD_XSTATS_L3_STATS */
55490e7788fdSPetr Machata rtnl_offload_xstats_get_size_hw_s_info_one(dev, t_l3) +
55500e7788fdSPetr Machata 0;
55510e7788fdSPetr Machata }
55520e7788fdSPetr Machata
rtnl_offload_xstats_get_size(const struct net_device * dev,u32 off_filter_mask)555346efc97bSPetr Machata static int rtnl_offload_xstats_get_size(const struct net_device *dev,
555446efc97bSPetr Machata u32 off_filter_mask)
555569ae6ad2SNogah Frankel {
55560e7788fdSPetr Machata enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3;
5557f6e0fb81SPetr Machata int attr_id_cpu_hit = IFLA_OFFLOAD_XSTATS_CPU_HIT;
555869ae6ad2SNogah Frankel int nla_size = 0;
555969ae6ad2SNogah Frankel int size;
556069ae6ad2SNogah Frankel
556146efc97bSPetr Machata if (off_filter_mask &
556246efc97bSPetr Machata IFLA_STATS_FILTER_BIT(attr_id_cpu_hit)) {
5563f6e0fb81SPetr Machata size = rtnl_offload_xstats_get_size_ndo(dev, attr_id_cpu_hit);
556469ae6ad2SNogah Frankel nla_size += nla_total_size_64bit(size);
556546efc97bSPetr Machata }
556669ae6ad2SNogah Frankel
55670e7788fdSPetr Machata if (off_filter_mask &
55680e7788fdSPetr Machata IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_HW_S_INFO))
55690e7788fdSPetr Machata nla_size += rtnl_offload_xstats_get_size_hw_s_info(dev);
55700e7788fdSPetr Machata
55710e7788fdSPetr Machata if (off_filter_mask &
55720e7788fdSPetr Machata IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_L3_STATS)) {
55730e7788fdSPetr Machata size = rtnl_offload_xstats_get_size_stats(dev, t_l3);
55740e7788fdSPetr Machata nla_size += nla_total_size_64bit(size);
55750e7788fdSPetr Machata }
55760e7788fdSPetr Machata
557769ae6ad2SNogah Frankel if (nla_size != 0)
557869ae6ad2SNogah Frankel nla_size += nla_total_size(0);
557969ae6ad2SNogah Frankel
558069ae6ad2SNogah Frankel return nla_size;
558169ae6ad2SNogah Frankel }
558269ae6ad2SNogah Frankel
558346efc97bSPetr Machata struct rtnl_stats_dump_filters {
558446efc97bSPetr Machata /* mask[0] filters outer attributes. Then individual nests have their
558546efc97bSPetr Machata * filtering mask at the index of the nested attribute.
558646efc97bSPetr Machata */
558746efc97bSPetr Machata u32 mask[IFLA_STATS_MAX + 1];
558846efc97bSPetr Machata };
558946efc97bSPetr Machata
rtnl_fill_statsinfo(struct sk_buff * skb,struct net_device * dev,int type,u32 pid,u32 seq,u32 change,unsigned int flags,const struct rtnl_stats_dump_filters * filters,int * idxattr,int * prividx,struct netlink_ext_ack * extack)559010c9ead9SRoopa Prabhu static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
559110c9ead9SRoopa Prabhu int type, u32 pid, u32 seq, u32 change,
559246efc97bSPetr Machata unsigned int flags,
559346efc97bSPetr Machata const struct rtnl_stats_dump_filters *filters,
559405415bccSPetr Machata int *idxattr, int *prividx,
559505415bccSPetr Machata struct netlink_ext_ack *extack)
559610c9ead9SRoopa Prabhu {
559746efc97bSPetr Machata unsigned int filter_mask = filters->mask[0];
559810c9ead9SRoopa Prabhu struct if_stats_msg *ifsm;
559910c9ead9SRoopa Prabhu struct nlmsghdr *nlh;
560010c9ead9SRoopa Prabhu struct nlattr *attr;
5601e8872a25SNikolay Aleksandrov int s_prividx = *prividx;
560269ae6ad2SNogah Frankel int err;
560310c9ead9SRoopa Prabhu
560410c9ead9SRoopa Prabhu ASSERT_RTNL();
560510c9ead9SRoopa Prabhu
560610c9ead9SRoopa Prabhu nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
560710c9ead9SRoopa Prabhu if (!nlh)
560810c9ead9SRoopa Prabhu return -EMSGSIZE;
560910c9ead9SRoopa Prabhu
561010c9ead9SRoopa Prabhu ifsm = nlmsg_data(nlh);
5611ce024f42SNikolay Aleksandrov ifsm->family = PF_UNSPEC;
5612ce024f42SNikolay Aleksandrov ifsm->pad1 = 0;
5613ce024f42SNikolay Aleksandrov ifsm->pad2 = 0;
561410c9ead9SRoopa Prabhu ifsm->ifindex = dev->ifindex;
561510c9ead9SRoopa Prabhu ifsm->filter_mask = filter_mask;
561610c9ead9SRoopa Prabhu
5617e8872a25SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
561810c9ead9SRoopa Prabhu struct rtnl_link_stats64 *sp;
561910c9ead9SRoopa Prabhu
562058414d32SNicolas Dichtel attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
562158414d32SNicolas Dichtel sizeof(struct rtnl_link_stats64),
562258414d32SNicolas Dichtel IFLA_STATS_UNSPEC);
5623216e6906SPetr Machata if (!attr) {
5624216e6906SPetr Machata err = -EMSGSIZE;
562510c9ead9SRoopa Prabhu goto nla_put_failure;
5626216e6906SPetr Machata }
562710c9ead9SRoopa Prabhu
562810c9ead9SRoopa Prabhu sp = nla_data(attr);
562910c9ead9SRoopa Prabhu dev_get_stats(dev, sp);
563010c9ead9SRoopa Prabhu }
563110c9ead9SRoopa Prabhu
563297a47facSNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
563397a47facSNikolay Aleksandrov const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
563497a47facSNikolay Aleksandrov
563597a47facSNikolay Aleksandrov if (ops && ops->fill_linkxstats) {
563697a47facSNikolay Aleksandrov *idxattr = IFLA_STATS_LINK_XSTATS;
5637ae0be8deSMichal Kubecek attr = nla_nest_start_noflag(skb,
563897a47facSNikolay Aleksandrov IFLA_STATS_LINK_XSTATS);
5639216e6906SPetr Machata if (!attr) {
5640216e6906SPetr Machata err = -EMSGSIZE;
564197a47facSNikolay Aleksandrov goto nla_put_failure;
5642216e6906SPetr Machata }
564397a47facSNikolay Aleksandrov
564480e73cc5SNikolay Aleksandrov err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
564580e73cc5SNikolay Aleksandrov nla_nest_end(skb, attr);
564680e73cc5SNikolay Aleksandrov if (err)
564780e73cc5SNikolay Aleksandrov goto nla_put_failure;
564880e73cc5SNikolay Aleksandrov *idxattr = 0;
564980e73cc5SNikolay Aleksandrov }
565080e73cc5SNikolay Aleksandrov }
565180e73cc5SNikolay Aleksandrov
565280e73cc5SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
565380e73cc5SNikolay Aleksandrov *idxattr)) {
565480e73cc5SNikolay Aleksandrov const struct rtnl_link_ops *ops = NULL;
565580e73cc5SNikolay Aleksandrov const struct net_device *master;
565680e73cc5SNikolay Aleksandrov
565780e73cc5SNikolay Aleksandrov master = netdev_master_upper_dev_get(dev);
565880e73cc5SNikolay Aleksandrov if (master)
565980e73cc5SNikolay Aleksandrov ops = master->rtnl_link_ops;
566080e73cc5SNikolay Aleksandrov if (ops && ops->fill_linkxstats) {
566180e73cc5SNikolay Aleksandrov *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
5662ae0be8deSMichal Kubecek attr = nla_nest_start_noflag(skb,
566380e73cc5SNikolay Aleksandrov IFLA_STATS_LINK_XSTATS_SLAVE);
5664216e6906SPetr Machata if (!attr) {
5665216e6906SPetr Machata err = -EMSGSIZE;
566680e73cc5SNikolay Aleksandrov goto nla_put_failure;
5667216e6906SPetr Machata }
566880e73cc5SNikolay Aleksandrov
566980e73cc5SNikolay Aleksandrov err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
567097a47facSNikolay Aleksandrov nla_nest_end(skb, attr);
567197a47facSNikolay Aleksandrov if (err)
567297a47facSNikolay Aleksandrov goto nla_put_failure;
567397a47facSNikolay Aleksandrov *idxattr = 0;
567497a47facSNikolay Aleksandrov }
567597a47facSNikolay Aleksandrov }
567697a47facSNikolay Aleksandrov
567769ae6ad2SNogah Frankel if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
567869ae6ad2SNogah Frankel *idxattr)) {
567946efc97bSPetr Machata u32 off_filter_mask;
568046efc97bSPetr Machata
568146efc97bSPetr Machata off_filter_mask = filters->mask[IFLA_STATS_LINK_OFFLOAD_XSTATS];
568269ae6ad2SNogah Frankel *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
5683ae0be8deSMichal Kubecek attr = nla_nest_start_noflag(skb,
5684ae0be8deSMichal Kubecek IFLA_STATS_LINK_OFFLOAD_XSTATS);
5685216e6906SPetr Machata if (!attr) {
5686216e6906SPetr Machata err = -EMSGSIZE;
568769ae6ad2SNogah Frankel goto nla_put_failure;
5688216e6906SPetr Machata }
568969ae6ad2SNogah Frankel
569046efc97bSPetr Machata err = rtnl_offload_xstats_fill(skb, dev, prividx,
569105415bccSPetr Machata off_filter_mask, extack);
569269ae6ad2SNogah Frankel if (err == -ENODATA)
569369ae6ad2SNogah Frankel nla_nest_cancel(skb, attr);
569469ae6ad2SNogah Frankel else
569569ae6ad2SNogah Frankel nla_nest_end(skb, attr);
569669ae6ad2SNogah Frankel
569769ae6ad2SNogah Frankel if (err && err != -ENODATA)
569869ae6ad2SNogah Frankel goto nla_put_failure;
569969ae6ad2SNogah Frankel *idxattr = 0;
570069ae6ad2SNogah Frankel }
570169ae6ad2SNogah Frankel
5702aefb4d4aSRobert Shearman if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
5703aefb4d4aSRobert Shearman struct rtnl_af_ops *af_ops;
5704aefb4d4aSRobert Shearman
5705aefb4d4aSRobert Shearman *idxattr = IFLA_STATS_AF_SPEC;
5706ae0be8deSMichal Kubecek attr = nla_nest_start_noflag(skb, IFLA_STATS_AF_SPEC);
5707216e6906SPetr Machata if (!attr) {
5708216e6906SPetr Machata err = -EMSGSIZE;
5709aefb4d4aSRobert Shearman goto nla_put_failure;
5710216e6906SPetr Machata }
5711aefb4d4aSRobert Shearman
57125fa85a09SFlorian Westphal rcu_read_lock();
57135fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
5714aefb4d4aSRobert Shearman if (af_ops->fill_stats_af) {
5715aefb4d4aSRobert Shearman struct nlattr *af;
5716aefb4d4aSRobert Shearman
5717ae0be8deSMichal Kubecek af = nla_nest_start_noflag(skb,
5718ae0be8deSMichal Kubecek af_ops->family);
57195fa85a09SFlorian Westphal if (!af) {
57205fa85a09SFlorian Westphal rcu_read_unlock();
572157d29a29STom Rix err = -EMSGSIZE;
5722aefb4d4aSRobert Shearman goto nla_put_failure;
57235fa85a09SFlorian Westphal }
5724aefb4d4aSRobert Shearman err = af_ops->fill_stats_af(skb, dev);
5725aefb4d4aSRobert Shearman
57265fa85a09SFlorian Westphal if (err == -ENODATA) {
5727aefb4d4aSRobert Shearman nla_nest_cancel(skb, af);
57285fa85a09SFlorian Westphal } else if (err < 0) {
57295fa85a09SFlorian Westphal rcu_read_unlock();
5730aefb4d4aSRobert Shearman goto nla_put_failure;
57315fa85a09SFlorian Westphal }
5732aefb4d4aSRobert Shearman
5733aefb4d4aSRobert Shearman nla_nest_end(skb, af);
5734aefb4d4aSRobert Shearman }
5735aefb4d4aSRobert Shearman }
57365fa85a09SFlorian Westphal rcu_read_unlock();
5737aefb4d4aSRobert Shearman
5738aefb4d4aSRobert Shearman nla_nest_end(skb, attr);
5739aefb4d4aSRobert Shearman
5740aefb4d4aSRobert Shearman *idxattr = 0;
5741aefb4d4aSRobert Shearman }
5742aefb4d4aSRobert Shearman
574310c9ead9SRoopa Prabhu nlmsg_end(skb, nlh);
574410c9ead9SRoopa Prabhu
574510c9ead9SRoopa Prabhu return 0;
574610c9ead9SRoopa Prabhu
574710c9ead9SRoopa Prabhu nla_put_failure:
5748e8872a25SNikolay Aleksandrov /* not a multi message or no progress mean a real error */
5749e8872a25SNikolay Aleksandrov if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
575010c9ead9SRoopa Prabhu nlmsg_cancel(skb, nlh);
5751e8872a25SNikolay Aleksandrov else
5752e8872a25SNikolay Aleksandrov nlmsg_end(skb, nlh);
575310c9ead9SRoopa Prabhu
5754216e6906SPetr Machata return err;
575510c9ead9SRoopa Prabhu }
575610c9ead9SRoopa Prabhu
if_nlmsg_stats_size(const struct net_device * dev,const struct rtnl_stats_dump_filters * filters)575710c9ead9SRoopa Prabhu static size_t if_nlmsg_stats_size(const struct net_device *dev,
575846efc97bSPetr Machata const struct rtnl_stats_dump_filters *filters)
575910c9ead9SRoopa Prabhu {
5760d3436799SEric Dumazet size_t size = NLMSG_ALIGN(sizeof(struct if_stats_msg));
576146efc97bSPetr Machata unsigned int filter_mask = filters->mask[0];
576210c9ead9SRoopa Prabhu
5763e8872a25SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
576410c9ead9SRoopa Prabhu size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
576510c9ead9SRoopa Prabhu
576697a47facSNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
576797a47facSNikolay Aleksandrov const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
576880e73cc5SNikolay Aleksandrov int attr = IFLA_STATS_LINK_XSTATS;
576997a47facSNikolay Aleksandrov
577097a47facSNikolay Aleksandrov if (ops && ops->get_linkxstats_size) {
577180e73cc5SNikolay Aleksandrov size += nla_total_size(ops->get_linkxstats_size(dev,
577280e73cc5SNikolay Aleksandrov attr));
577397a47facSNikolay Aleksandrov /* for IFLA_STATS_LINK_XSTATS */
577497a47facSNikolay Aleksandrov size += nla_total_size(0);
577597a47facSNikolay Aleksandrov }
577697a47facSNikolay Aleksandrov }
577797a47facSNikolay Aleksandrov
577880e73cc5SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
577980e73cc5SNikolay Aleksandrov struct net_device *_dev = (struct net_device *)dev;
578080e73cc5SNikolay Aleksandrov const struct rtnl_link_ops *ops = NULL;
578180e73cc5SNikolay Aleksandrov const struct net_device *master;
578280e73cc5SNikolay Aleksandrov
578380e73cc5SNikolay Aleksandrov /* netdev_master_upper_dev_get can't take const */
578480e73cc5SNikolay Aleksandrov master = netdev_master_upper_dev_get(_dev);
578580e73cc5SNikolay Aleksandrov if (master)
578680e73cc5SNikolay Aleksandrov ops = master->rtnl_link_ops;
578780e73cc5SNikolay Aleksandrov if (ops && ops->get_linkxstats_size) {
578880e73cc5SNikolay Aleksandrov int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
578980e73cc5SNikolay Aleksandrov
579080e73cc5SNikolay Aleksandrov size += nla_total_size(ops->get_linkxstats_size(dev,
579180e73cc5SNikolay Aleksandrov attr));
579280e73cc5SNikolay Aleksandrov /* for IFLA_STATS_LINK_XSTATS_SLAVE */
579380e73cc5SNikolay Aleksandrov size += nla_total_size(0);
579480e73cc5SNikolay Aleksandrov }
579580e73cc5SNikolay Aleksandrov }
579680e73cc5SNikolay Aleksandrov
579746efc97bSPetr Machata if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0)) {
579846efc97bSPetr Machata u32 off_filter_mask;
579946efc97bSPetr Machata
580046efc97bSPetr Machata off_filter_mask = filters->mask[IFLA_STATS_LINK_OFFLOAD_XSTATS];
580146efc97bSPetr Machata size += rtnl_offload_xstats_get_size(dev, off_filter_mask);
580246efc97bSPetr Machata }
580369ae6ad2SNogah Frankel
5804aefb4d4aSRobert Shearman if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
5805aefb4d4aSRobert Shearman struct rtnl_af_ops *af_ops;
5806aefb4d4aSRobert Shearman
5807aefb4d4aSRobert Shearman /* for IFLA_STATS_AF_SPEC */
5808aefb4d4aSRobert Shearman size += nla_total_size(0);
5809aefb4d4aSRobert Shearman
58105fa85a09SFlorian Westphal rcu_read_lock();
58115fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
5812aefb4d4aSRobert Shearman if (af_ops->get_stats_af_size) {
5813aefb4d4aSRobert Shearman size += nla_total_size(
5814aefb4d4aSRobert Shearman af_ops->get_stats_af_size(dev));
5815aefb4d4aSRobert Shearman
5816aefb4d4aSRobert Shearman /* for AF_* */
5817aefb4d4aSRobert Shearman size += nla_total_size(0);
5818aefb4d4aSRobert Shearman }
5819aefb4d4aSRobert Shearman }
58205fa85a09SFlorian Westphal rcu_read_unlock();
5821aefb4d4aSRobert Shearman }
5822aefb4d4aSRobert Shearman
582310c9ead9SRoopa Prabhu return size;
582410c9ead9SRoopa Prabhu }
582510c9ead9SRoopa Prabhu
582646efc97bSPetr Machata #define RTNL_STATS_OFFLOAD_XSTATS_VALID ((1 << __IFLA_OFFLOAD_XSTATS_MAX) - 1)
582746efc97bSPetr Machata
582846efc97bSPetr Machata static const struct nla_policy
582946efc97bSPetr Machata rtnl_stats_get_policy_filters[IFLA_STATS_MAX + 1] = {
583046efc97bSPetr Machata [IFLA_STATS_LINK_OFFLOAD_XSTATS] =
583146efc97bSPetr Machata NLA_POLICY_MASK(NLA_U32, RTNL_STATS_OFFLOAD_XSTATS_VALID),
583246efc97bSPetr Machata };
583346efc97bSPetr Machata
583446efc97bSPetr Machata static const struct nla_policy
583546efc97bSPetr Machata rtnl_stats_get_policy[IFLA_STATS_GETSET_MAX + 1] = {
583646efc97bSPetr Machata [IFLA_STATS_GET_FILTERS] =
583746efc97bSPetr Machata NLA_POLICY_NESTED(rtnl_stats_get_policy_filters),
583846efc97bSPetr Machata };
583946efc97bSPetr Machata
584003ba3566SPetr Machata static const struct nla_policy
584103ba3566SPetr Machata ifla_stats_set_policy[IFLA_STATS_GETSET_MAX + 1] = {
58425fd0b838SPetr Machata [IFLA_STATS_SET_OFFLOAD_XSTATS_L3_STATS] = NLA_POLICY_MAX(NLA_U8, 1),
584303ba3566SPetr Machata };
584403ba3566SPetr Machata
rtnl_stats_get_parse_filters(struct nlattr * ifla_filters,struct rtnl_stats_dump_filters * filters,struct netlink_ext_ack * extack)584546efc97bSPetr Machata static int rtnl_stats_get_parse_filters(struct nlattr *ifla_filters,
584646efc97bSPetr Machata struct rtnl_stats_dump_filters *filters,
584746efc97bSPetr Machata struct netlink_ext_ack *extack)
584846efc97bSPetr Machata {
584946efc97bSPetr Machata struct nlattr *tb[IFLA_STATS_MAX + 1];
585046efc97bSPetr Machata int err;
585146efc97bSPetr Machata int at;
585246efc97bSPetr Machata
585346efc97bSPetr Machata err = nla_parse_nested(tb, IFLA_STATS_MAX, ifla_filters,
585446efc97bSPetr Machata rtnl_stats_get_policy_filters, extack);
585546efc97bSPetr Machata if (err < 0)
585646efc97bSPetr Machata return err;
585746efc97bSPetr Machata
585846efc97bSPetr Machata for (at = 1; at <= IFLA_STATS_MAX; at++) {
585946efc97bSPetr Machata if (tb[at]) {
586046efc97bSPetr Machata if (!(filters->mask[0] & IFLA_STATS_FILTER_BIT(at))) {
586146efc97bSPetr Machata NL_SET_ERR_MSG(extack, "Filtered attribute not enabled in filter_mask");
586246efc97bSPetr Machata return -EINVAL;
586346efc97bSPetr Machata }
586446efc97bSPetr Machata filters->mask[at] = nla_get_u32(tb[at]);
586546efc97bSPetr Machata }
586646efc97bSPetr Machata }
586746efc97bSPetr Machata
586846efc97bSPetr Machata return 0;
586946efc97bSPetr Machata }
587046efc97bSPetr Machata
rtnl_stats_get_parse(const struct nlmsghdr * nlh,u32 filter_mask,struct rtnl_stats_dump_filters * filters,struct netlink_ext_ack * extack)587146efc97bSPetr Machata static int rtnl_stats_get_parse(const struct nlmsghdr *nlh,
587246efc97bSPetr Machata u32 filter_mask,
587346efc97bSPetr Machata struct rtnl_stats_dump_filters *filters,
587446efc97bSPetr Machata struct netlink_ext_ack *extack)
587546efc97bSPetr Machata {
587646efc97bSPetr Machata struct nlattr *tb[IFLA_STATS_GETSET_MAX + 1];
587746efc97bSPetr Machata int err;
587846efc97bSPetr Machata int i;
587946efc97bSPetr Machata
588046efc97bSPetr Machata filters->mask[0] = filter_mask;
588146efc97bSPetr Machata for (i = 1; i < ARRAY_SIZE(filters->mask); i++)
588246efc97bSPetr Machata filters->mask[i] = -1U;
588346efc97bSPetr Machata
588446efc97bSPetr Machata err = nlmsg_parse(nlh, sizeof(struct if_stats_msg), tb,
588546efc97bSPetr Machata IFLA_STATS_GETSET_MAX, rtnl_stats_get_policy, extack);
588646efc97bSPetr Machata if (err < 0)
588746efc97bSPetr Machata return err;
588846efc97bSPetr Machata
588946efc97bSPetr Machata if (tb[IFLA_STATS_GET_FILTERS]) {
589046efc97bSPetr Machata err = rtnl_stats_get_parse_filters(tb[IFLA_STATS_GET_FILTERS],
589146efc97bSPetr Machata filters, extack);
589246efc97bSPetr Machata if (err)
589346efc97bSPetr Machata return err;
589446efc97bSPetr Machata }
589546efc97bSPetr Machata
589646efc97bSPetr Machata return 0;
589746efc97bSPetr Machata }
589846efc97bSPetr Machata
rtnl_valid_stats_req(const struct nlmsghdr * nlh,bool strict_check,bool is_dump,struct netlink_ext_ack * extack)589951bc860dSJakub Kicinski static int rtnl_valid_stats_req(const struct nlmsghdr *nlh, bool strict_check,
590051bc860dSJakub Kicinski bool is_dump, struct netlink_ext_ack *extack)
590151bc860dSJakub Kicinski {
590251bc860dSJakub Kicinski struct if_stats_msg *ifsm;
590351bc860dSJakub Kicinski
590469f23a09SEric Dumazet if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifsm))) {
590551bc860dSJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid header for stats dump");
590651bc860dSJakub Kicinski return -EINVAL;
590751bc860dSJakub Kicinski }
590851bc860dSJakub Kicinski
590951bc860dSJakub Kicinski if (!strict_check)
591051bc860dSJakub Kicinski return 0;
591151bc860dSJakub Kicinski
591251bc860dSJakub Kicinski ifsm = nlmsg_data(nlh);
591351bc860dSJakub Kicinski
591451bc860dSJakub Kicinski /* only requests using strict checks can pass data to influence
591551bc860dSJakub Kicinski * the dump. The legacy exception is filter_mask.
591651bc860dSJakub Kicinski */
591751bc860dSJakub Kicinski if (ifsm->pad1 || ifsm->pad2 || (is_dump && ifsm->ifindex)) {
591851bc860dSJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid values in header for stats dump request");
591951bc860dSJakub Kicinski return -EINVAL;
592051bc860dSJakub Kicinski }
59216300acb2SJakub Kicinski if (ifsm->filter_mask >= IFLA_STATS_FILTER_BIT(IFLA_STATS_MAX + 1)) {
59226300acb2SJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid stats requested through filter mask");
59236300acb2SJakub Kicinski return -EINVAL;
59246300acb2SJakub Kicinski }
592551bc860dSJakub Kicinski
592651bc860dSJakub Kicinski return 0;
592751bc860dSJakub Kicinski }
592851bc860dSJakub Kicinski
rtnl_stats_get(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)5929c21ef3e3SDavid Ahern static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
5930c21ef3e3SDavid Ahern struct netlink_ext_ack *extack)
593110c9ead9SRoopa Prabhu {
593246efc97bSPetr Machata struct rtnl_stats_dump_filters filters;
593310c9ead9SRoopa Prabhu struct net *net = sock_net(skb->sk);
593410c9ead9SRoopa Prabhu struct net_device *dev = NULL;
5935e8872a25SNikolay Aleksandrov int idxattr = 0, prividx = 0;
5936e8872a25SNikolay Aleksandrov struct if_stats_msg *ifsm;
593710c9ead9SRoopa Prabhu struct sk_buff *nskb;
593810c9ead9SRoopa Prabhu int err;
593910c9ead9SRoopa Prabhu
594051bc860dSJakub Kicinski err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb),
594151bc860dSJakub Kicinski false, extack);
594251bc860dSJakub Kicinski if (err)
594351bc860dSJakub Kicinski return err;
59444775cc1fSMathias Krause
594510c9ead9SRoopa Prabhu ifsm = nlmsg_data(nlh);
594610c9ead9SRoopa Prabhu if (ifsm->ifindex > 0)
594710c9ead9SRoopa Prabhu dev = __dev_get_by_index(net, ifsm->ifindex);
594810c9ead9SRoopa Prabhu else
594910c9ead9SRoopa Prabhu return -EINVAL;
595010c9ead9SRoopa Prabhu
595110c9ead9SRoopa Prabhu if (!dev)
595210c9ead9SRoopa Prabhu return -ENODEV;
595310c9ead9SRoopa Prabhu
595446efc97bSPetr Machata if (!ifsm->filter_mask) {
595522b67d17SPetr Machata NL_SET_ERR_MSG(extack, "Filter mask must be set for stats get");
595610c9ead9SRoopa Prabhu return -EINVAL;
595722b67d17SPetr Machata }
595810c9ead9SRoopa Prabhu
595946efc97bSPetr Machata err = rtnl_stats_get_parse(nlh, ifsm->filter_mask, &filters, extack);
596046efc97bSPetr Machata if (err)
596146efc97bSPetr Machata return err;
596246efc97bSPetr Machata
596346efc97bSPetr Machata nskb = nlmsg_new(if_nlmsg_stats_size(dev, &filters), GFP_KERNEL);
596410c9ead9SRoopa Prabhu if (!nskb)
596510c9ead9SRoopa Prabhu return -ENOBUFS;
596610c9ead9SRoopa Prabhu
596710c9ead9SRoopa Prabhu err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
596810c9ead9SRoopa Prabhu NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
596905415bccSPetr Machata 0, &filters, &idxattr, &prividx, extack);
597010c9ead9SRoopa Prabhu if (err < 0) {
597110c9ead9SRoopa Prabhu /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
597210c9ead9SRoopa Prabhu WARN_ON(err == -EMSGSIZE);
597310c9ead9SRoopa Prabhu kfree_skb(nskb);
597410c9ead9SRoopa Prabhu } else {
597510c9ead9SRoopa Prabhu err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
597610c9ead9SRoopa Prabhu }
597710c9ead9SRoopa Prabhu
597810c9ead9SRoopa Prabhu return err;
597910c9ead9SRoopa Prabhu }
598010c9ead9SRoopa Prabhu
rtnl_stats_dump(struct sk_buff * skb,struct netlink_callback * cb)598110c9ead9SRoopa Prabhu static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
598210c9ead9SRoopa Prabhu {
5983841891ecSDavid Ahern struct netlink_ext_ack *extack = cb->extack;
5984e8872a25SNikolay Aleksandrov int h, s_h, err, s_idx, s_idxattr, s_prividx;
598546efc97bSPetr Machata struct rtnl_stats_dump_filters filters;
598610c9ead9SRoopa Prabhu struct net *net = sock_net(skb->sk);
598710c9ead9SRoopa Prabhu unsigned int flags = NLM_F_MULTI;
5988e8872a25SNikolay Aleksandrov struct if_stats_msg *ifsm;
5989e8872a25SNikolay Aleksandrov struct hlist_head *head;
5990e8872a25SNikolay Aleksandrov struct net_device *dev;
5991e8872a25SNikolay Aleksandrov int idx = 0;
599210c9ead9SRoopa Prabhu
599310c9ead9SRoopa Prabhu s_h = cb->args[0];
599410c9ead9SRoopa Prabhu s_idx = cb->args[1];
5995e8872a25SNikolay Aleksandrov s_idxattr = cb->args[2];
5996e8872a25SNikolay Aleksandrov s_prividx = cb->args[3];
599710c9ead9SRoopa Prabhu
599810c9ead9SRoopa Prabhu cb->seq = net->dev_base_seq;
599910c9ead9SRoopa Prabhu
600051bc860dSJakub Kicinski err = rtnl_valid_stats_req(cb->nlh, cb->strict_check, true, extack);
600151bc860dSJakub Kicinski if (err)
600251bc860dSJakub Kicinski return err;
60034775cc1fSMathias Krause
600410c9ead9SRoopa Prabhu ifsm = nlmsg_data(cb->nlh);
600546efc97bSPetr Machata if (!ifsm->filter_mask) {
6006841891ecSDavid Ahern NL_SET_ERR_MSG(extack, "Filter mask must be set for stats dump");
6007841891ecSDavid Ahern return -EINVAL;
6008841891ecSDavid Ahern }
600910c9ead9SRoopa Prabhu
601046efc97bSPetr Machata err = rtnl_stats_get_parse(cb->nlh, ifsm->filter_mask, &filters,
601146efc97bSPetr Machata extack);
601246efc97bSPetr Machata if (err)
601346efc97bSPetr Machata return err;
601446efc97bSPetr Machata
601510c9ead9SRoopa Prabhu for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
601610c9ead9SRoopa Prabhu idx = 0;
601710c9ead9SRoopa Prabhu head = &net->dev_index_head[h];
601810c9ead9SRoopa Prabhu hlist_for_each_entry(dev, head, index_hlist) {
601910c9ead9SRoopa Prabhu if (idx < s_idx)
602010c9ead9SRoopa Prabhu goto cont;
602110c9ead9SRoopa Prabhu err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
602210c9ead9SRoopa Prabhu NETLINK_CB(cb->skb).portid,
602310c9ead9SRoopa Prabhu cb->nlh->nlmsg_seq, 0,
602446efc97bSPetr Machata flags, &filters,
602505415bccSPetr Machata &s_idxattr, &s_prividx,
602605415bccSPetr Machata extack);
602710c9ead9SRoopa Prabhu /* If we ran out of room on the first message,
602810c9ead9SRoopa Prabhu * we're in trouble
602910c9ead9SRoopa Prabhu */
603010c9ead9SRoopa Prabhu WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
603110c9ead9SRoopa Prabhu
603210c9ead9SRoopa Prabhu if (err < 0)
603310c9ead9SRoopa Prabhu goto out;
6034e8872a25SNikolay Aleksandrov s_prividx = 0;
6035e8872a25SNikolay Aleksandrov s_idxattr = 0;
603610c9ead9SRoopa Prabhu nl_dump_check_consistent(cb, nlmsg_hdr(skb));
603710c9ead9SRoopa Prabhu cont:
603810c9ead9SRoopa Prabhu idx++;
603910c9ead9SRoopa Prabhu }
604010c9ead9SRoopa Prabhu }
604110c9ead9SRoopa Prabhu out:
6042e8872a25SNikolay Aleksandrov cb->args[3] = s_prividx;
6043e8872a25SNikolay Aleksandrov cb->args[2] = s_idxattr;
604410c9ead9SRoopa Prabhu cb->args[1] = idx;
604510c9ead9SRoopa Prabhu cb->args[0] = h;
604610c9ead9SRoopa Prabhu
604710c9ead9SRoopa Prabhu return skb->len;
604810c9ead9SRoopa Prabhu }
604910c9ead9SRoopa Prabhu
rtnl_offload_xstats_notify(struct net_device * dev)60505fd0b838SPetr Machata void rtnl_offload_xstats_notify(struct net_device *dev)
60515fd0b838SPetr Machata {
60525fd0b838SPetr Machata struct rtnl_stats_dump_filters response_filters = {};
60535fd0b838SPetr Machata struct net *net = dev_net(dev);
60545fd0b838SPetr Machata int idxattr = 0, prividx = 0;
60555fd0b838SPetr Machata struct sk_buff *skb;
60565fd0b838SPetr Machata int err = -ENOBUFS;
60575fd0b838SPetr Machata
60585fd0b838SPetr Machata ASSERT_RTNL();
60595fd0b838SPetr Machata
60605fd0b838SPetr Machata response_filters.mask[0] |=
60615fd0b838SPetr Machata IFLA_STATS_FILTER_BIT(IFLA_STATS_LINK_OFFLOAD_XSTATS);
60625fd0b838SPetr Machata response_filters.mask[IFLA_STATS_LINK_OFFLOAD_XSTATS] |=
60635fd0b838SPetr Machata IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_HW_S_INFO);
60645fd0b838SPetr Machata
60655fd0b838SPetr Machata skb = nlmsg_new(if_nlmsg_stats_size(dev, &response_filters),
60665fd0b838SPetr Machata GFP_KERNEL);
60675fd0b838SPetr Machata if (!skb)
60685fd0b838SPetr Machata goto errout;
60695fd0b838SPetr Machata
60705fd0b838SPetr Machata err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS, 0, 0, 0, 0,
60715fd0b838SPetr Machata &response_filters, &idxattr, &prividx, NULL);
60725fd0b838SPetr Machata if (err < 0) {
60735fd0b838SPetr Machata kfree_skb(skb);
60745fd0b838SPetr Machata goto errout;
60755fd0b838SPetr Machata }
60765fd0b838SPetr Machata
60775fd0b838SPetr Machata rtnl_notify(skb, net, 0, RTNLGRP_STATS, NULL, GFP_KERNEL);
60785fd0b838SPetr Machata return;
60795fd0b838SPetr Machata
60805fd0b838SPetr Machata errout:
60815fd0b838SPetr Machata rtnl_set_sk_err(net, RTNLGRP_STATS, err);
60825fd0b838SPetr Machata }
60835fd0b838SPetr Machata EXPORT_SYMBOL(rtnl_offload_xstats_notify);
60845fd0b838SPetr Machata
rtnl_stats_set(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)608503ba3566SPetr Machata static int rtnl_stats_set(struct sk_buff *skb, struct nlmsghdr *nlh,
608603ba3566SPetr Machata struct netlink_ext_ack *extack)
608703ba3566SPetr Machata {
60885fd0b838SPetr Machata enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3;
608903ba3566SPetr Machata struct rtnl_stats_dump_filters response_filters = {};
609003ba3566SPetr Machata struct nlattr *tb[IFLA_STATS_GETSET_MAX + 1];
609103ba3566SPetr Machata struct net *net = sock_net(skb->sk);
609203ba3566SPetr Machata struct net_device *dev = NULL;
609303ba3566SPetr Machata struct if_stats_msg *ifsm;
60945fd0b838SPetr Machata bool notify = false;
609503ba3566SPetr Machata int err;
609603ba3566SPetr Machata
609703ba3566SPetr Machata err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb),
609803ba3566SPetr Machata false, extack);
609903ba3566SPetr Machata if (err)
610003ba3566SPetr Machata return err;
610103ba3566SPetr Machata
610203ba3566SPetr Machata ifsm = nlmsg_data(nlh);
610303ba3566SPetr Machata if (ifsm->family != AF_UNSPEC) {
610403ba3566SPetr Machata NL_SET_ERR_MSG(extack, "Address family should be AF_UNSPEC");
610503ba3566SPetr Machata return -EINVAL;
610603ba3566SPetr Machata }
610703ba3566SPetr Machata
610803ba3566SPetr Machata if (ifsm->ifindex > 0)
610903ba3566SPetr Machata dev = __dev_get_by_index(net, ifsm->ifindex);
611003ba3566SPetr Machata else
611103ba3566SPetr Machata return -EINVAL;
611203ba3566SPetr Machata
611303ba3566SPetr Machata if (!dev)
611403ba3566SPetr Machata return -ENODEV;
611503ba3566SPetr Machata
611603ba3566SPetr Machata if (ifsm->filter_mask) {
611703ba3566SPetr Machata NL_SET_ERR_MSG(extack, "Filter mask must be 0 for stats set");
611803ba3566SPetr Machata return -EINVAL;
611903ba3566SPetr Machata }
612003ba3566SPetr Machata
612103ba3566SPetr Machata err = nlmsg_parse(nlh, sizeof(*ifsm), tb, IFLA_STATS_GETSET_MAX,
612203ba3566SPetr Machata ifla_stats_set_policy, extack);
612303ba3566SPetr Machata if (err < 0)
612403ba3566SPetr Machata return err;
612503ba3566SPetr Machata
61265fd0b838SPetr Machata if (tb[IFLA_STATS_SET_OFFLOAD_XSTATS_L3_STATS]) {
61275fd0b838SPetr Machata u8 req = nla_get_u8(tb[IFLA_STATS_SET_OFFLOAD_XSTATS_L3_STATS]);
612803ba3566SPetr Machata
61295fd0b838SPetr Machata if (req)
61305fd0b838SPetr Machata err = netdev_offload_xstats_enable(dev, t_l3, extack);
61315fd0b838SPetr Machata else
61325fd0b838SPetr Machata err = netdev_offload_xstats_disable(dev, t_l3);
61335fd0b838SPetr Machata
61345fd0b838SPetr Machata if (!err)
61355fd0b838SPetr Machata notify = true;
61365fd0b838SPetr Machata else if (err != -EALREADY)
61375fd0b838SPetr Machata return err;
61385fd0b838SPetr Machata
61395fd0b838SPetr Machata response_filters.mask[0] |=
61405fd0b838SPetr Machata IFLA_STATS_FILTER_BIT(IFLA_STATS_LINK_OFFLOAD_XSTATS);
61415fd0b838SPetr Machata response_filters.mask[IFLA_STATS_LINK_OFFLOAD_XSTATS] |=
61425fd0b838SPetr Machata IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_HW_S_INFO);
614303ba3566SPetr Machata }
614403ba3566SPetr Machata
61455fd0b838SPetr Machata if (notify)
61465fd0b838SPetr Machata rtnl_offload_xstats_notify(dev);
61475fd0b838SPetr Machata
61485fd0b838SPetr Machata return 0;
614903ba3566SPetr Machata }
615003ba3566SPetr Machata
rtnl_mdb_valid_dump_req(const struct nlmsghdr * nlh,struct netlink_ext_ack * extack)6151cc7f5022SIdo Schimmel static int rtnl_mdb_valid_dump_req(const struct nlmsghdr *nlh,
6152cc7f5022SIdo Schimmel struct netlink_ext_ack *extack)
6153cc7f5022SIdo Schimmel {
6154cc7f5022SIdo Schimmel struct br_port_msg *bpm;
6155cc7f5022SIdo Schimmel
6156cc7f5022SIdo Schimmel if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*bpm))) {
6157cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Invalid header for mdb dump request");
6158cc7f5022SIdo Schimmel return -EINVAL;
6159cc7f5022SIdo Schimmel }
6160cc7f5022SIdo Schimmel
6161cc7f5022SIdo Schimmel bpm = nlmsg_data(nlh);
6162cc7f5022SIdo Schimmel if (bpm->ifindex) {
6163cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Filtering by device index is not supported for mdb dump request");
6164cc7f5022SIdo Schimmel return -EINVAL;
6165cc7f5022SIdo Schimmel }
6166cc7f5022SIdo Schimmel if (nlmsg_attrlen(nlh, sizeof(*bpm))) {
6167cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Invalid data after header in mdb dump request");
6168cc7f5022SIdo Schimmel return -EINVAL;
6169cc7f5022SIdo Schimmel }
6170cc7f5022SIdo Schimmel
6171cc7f5022SIdo Schimmel return 0;
6172cc7f5022SIdo Schimmel }
6173cc7f5022SIdo Schimmel
6174cc7f5022SIdo Schimmel struct rtnl_mdb_dump_ctx {
6175cc7f5022SIdo Schimmel long idx;
6176cc7f5022SIdo Schimmel };
6177cc7f5022SIdo Schimmel
rtnl_mdb_dump(struct sk_buff * skb,struct netlink_callback * cb)6178cc7f5022SIdo Schimmel static int rtnl_mdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
6179cc7f5022SIdo Schimmel {
6180cc7f5022SIdo Schimmel struct rtnl_mdb_dump_ctx *ctx = (void *)cb->ctx;
6181cc7f5022SIdo Schimmel struct net *net = sock_net(skb->sk);
6182cc7f5022SIdo Schimmel struct net_device *dev;
6183cc7f5022SIdo Schimmel int idx, s_idx;
6184cc7f5022SIdo Schimmel int err;
6185cc7f5022SIdo Schimmel
6186cc7f5022SIdo Schimmel NL_ASSERT_DUMP_CTX_FITS(struct rtnl_mdb_dump_ctx);
6187cc7f5022SIdo Schimmel
6188cc7f5022SIdo Schimmel if (cb->strict_check) {
6189cc7f5022SIdo Schimmel err = rtnl_mdb_valid_dump_req(cb->nlh, cb->extack);
6190cc7f5022SIdo Schimmel if (err)
6191cc7f5022SIdo Schimmel return err;
6192cc7f5022SIdo Schimmel }
6193cc7f5022SIdo Schimmel
6194cc7f5022SIdo Schimmel s_idx = ctx->idx;
6195cc7f5022SIdo Schimmel idx = 0;
6196cc7f5022SIdo Schimmel
6197cc7f5022SIdo Schimmel for_each_netdev(net, dev) {
6198cc7f5022SIdo Schimmel if (idx < s_idx)
6199cc7f5022SIdo Schimmel goto skip;
6200cc7f5022SIdo Schimmel if (!dev->netdev_ops->ndo_mdb_dump)
6201cc7f5022SIdo Schimmel goto skip;
6202cc7f5022SIdo Schimmel
6203cc7f5022SIdo Schimmel err = dev->netdev_ops->ndo_mdb_dump(dev, skb, cb);
6204cc7f5022SIdo Schimmel if (err == -EMSGSIZE)
6205cc7f5022SIdo Schimmel goto out;
6206cc7f5022SIdo Schimmel /* Moving on to next device, reset markers and sequence
6207cc7f5022SIdo Schimmel * counters since they are all maintained per-device.
6208cc7f5022SIdo Schimmel */
6209cc7f5022SIdo Schimmel memset(cb->ctx, 0, sizeof(cb->ctx));
6210cc7f5022SIdo Schimmel cb->prev_seq = 0;
6211cc7f5022SIdo Schimmel cb->seq = 0;
6212cc7f5022SIdo Schimmel skip:
6213cc7f5022SIdo Schimmel idx++;
6214cc7f5022SIdo Schimmel }
6215cc7f5022SIdo Schimmel
6216cc7f5022SIdo Schimmel out:
6217cc7f5022SIdo Schimmel ctx->idx = idx;
6218cc7f5022SIdo Schimmel return skb->len;
6219cc7f5022SIdo Schimmel }
6220cc7f5022SIdo Schimmel
rtnl_validate_mdb_entry(const struct nlattr * attr,struct netlink_ext_ack * extack)6221cc7f5022SIdo Schimmel static int rtnl_validate_mdb_entry(const struct nlattr *attr,
6222cc7f5022SIdo Schimmel struct netlink_ext_ack *extack)
6223cc7f5022SIdo Schimmel {
6224cc7f5022SIdo Schimmel struct br_mdb_entry *entry = nla_data(attr);
6225cc7f5022SIdo Schimmel
6226cc7f5022SIdo Schimmel if (nla_len(attr) != sizeof(struct br_mdb_entry)) {
6227cc7f5022SIdo Schimmel NL_SET_ERR_MSG_ATTR(extack, attr, "Invalid attribute length");
6228cc7f5022SIdo Schimmel return -EINVAL;
6229cc7f5022SIdo Schimmel }
6230cc7f5022SIdo Schimmel
6231cc7f5022SIdo Schimmel if (entry->ifindex == 0) {
6232cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Zero entry ifindex is not allowed");
6233cc7f5022SIdo Schimmel return -EINVAL;
6234cc7f5022SIdo Schimmel }
6235cc7f5022SIdo Schimmel
6236cc7f5022SIdo Schimmel if (entry->addr.proto == htons(ETH_P_IP)) {
6237da654c80SIdo Schimmel if (!ipv4_is_multicast(entry->addr.u.ip4) &&
6238da654c80SIdo Schimmel !ipv4_is_zeronet(entry->addr.u.ip4)) {
6239da654c80SIdo Schimmel NL_SET_ERR_MSG(extack, "IPv4 entry group address is not multicast or 0.0.0.0");
6240cc7f5022SIdo Schimmel return -EINVAL;
6241cc7f5022SIdo Schimmel }
6242cc7f5022SIdo Schimmel if (ipv4_is_local_multicast(entry->addr.u.ip4)) {
6243cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "IPv4 entry group address is local multicast");
6244cc7f5022SIdo Schimmel return -EINVAL;
6245cc7f5022SIdo Schimmel }
6246cc7f5022SIdo Schimmel #if IS_ENABLED(CONFIG_IPV6)
6247cc7f5022SIdo Schimmel } else if (entry->addr.proto == htons(ETH_P_IPV6)) {
6248cc7f5022SIdo Schimmel if (ipv6_addr_is_ll_all_nodes(&entry->addr.u.ip6)) {
6249cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "IPv6 entry group address is link-local all nodes");
6250cc7f5022SIdo Schimmel return -EINVAL;
6251cc7f5022SIdo Schimmel }
6252cc7f5022SIdo Schimmel #endif
6253cc7f5022SIdo Schimmel } else if (entry->addr.proto == 0) {
6254cc7f5022SIdo Schimmel /* L2 mdb */
6255cc7f5022SIdo Schimmel if (!is_multicast_ether_addr(entry->addr.u.mac_addr)) {
6256cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "L2 entry group is not multicast");
6257cc7f5022SIdo Schimmel return -EINVAL;
6258cc7f5022SIdo Schimmel }
6259cc7f5022SIdo Schimmel } else {
6260cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Unknown entry protocol");
6261cc7f5022SIdo Schimmel return -EINVAL;
6262cc7f5022SIdo Schimmel }
6263cc7f5022SIdo Schimmel
6264cc7f5022SIdo Schimmel if (entry->state != MDB_PERMANENT && entry->state != MDB_TEMPORARY) {
6265cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Unknown entry state");
6266cc7f5022SIdo Schimmel return -EINVAL;
6267cc7f5022SIdo Schimmel }
6268cc7f5022SIdo Schimmel if (entry->vid >= VLAN_VID_MASK) {
6269cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Invalid entry VLAN id");
6270cc7f5022SIdo Schimmel return -EINVAL;
6271cc7f5022SIdo Schimmel }
6272cc7f5022SIdo Schimmel
6273cc7f5022SIdo Schimmel return 0;
6274cc7f5022SIdo Schimmel }
6275cc7f5022SIdo Schimmel
6276cc7f5022SIdo Schimmel static const struct nla_policy mdba_policy[MDBA_SET_ENTRY_MAX + 1] = {
6277cc7f5022SIdo Schimmel [MDBA_SET_ENTRY_UNSPEC] = { .strict_start_type = MDBA_SET_ENTRY_ATTRS + 1 },
6278cc7f5022SIdo Schimmel [MDBA_SET_ENTRY] = NLA_POLICY_VALIDATE_FN(NLA_BINARY,
6279cc7f5022SIdo Schimmel rtnl_validate_mdb_entry,
6280cc7f5022SIdo Schimmel sizeof(struct br_mdb_entry)),
6281cc7f5022SIdo Schimmel [MDBA_SET_ENTRY_ATTRS] = { .type = NLA_NESTED },
6282cc7f5022SIdo Schimmel };
6283cc7f5022SIdo Schimmel
rtnl_mdb_add(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)6284cc7f5022SIdo Schimmel static int rtnl_mdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
6285cc7f5022SIdo Schimmel struct netlink_ext_ack *extack)
6286cc7f5022SIdo Schimmel {
6287cc7f5022SIdo Schimmel struct nlattr *tb[MDBA_SET_ENTRY_MAX + 1];
6288cc7f5022SIdo Schimmel struct net *net = sock_net(skb->sk);
6289cc7f5022SIdo Schimmel struct br_port_msg *bpm;
6290cc7f5022SIdo Schimmel struct net_device *dev;
6291cc7f5022SIdo Schimmel int err;
6292cc7f5022SIdo Schimmel
6293cc7f5022SIdo Schimmel err = nlmsg_parse_deprecated(nlh, sizeof(*bpm), tb,
6294cc7f5022SIdo Schimmel MDBA_SET_ENTRY_MAX, mdba_policy, extack);
6295cc7f5022SIdo Schimmel if (err)
6296cc7f5022SIdo Schimmel return err;
6297cc7f5022SIdo Schimmel
6298cc7f5022SIdo Schimmel bpm = nlmsg_data(nlh);
6299cc7f5022SIdo Schimmel if (!bpm->ifindex) {
6300cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Invalid ifindex");
6301cc7f5022SIdo Schimmel return -EINVAL;
6302cc7f5022SIdo Schimmel }
6303cc7f5022SIdo Schimmel
6304cc7f5022SIdo Schimmel dev = __dev_get_by_index(net, bpm->ifindex);
6305cc7f5022SIdo Schimmel if (!dev) {
6306cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Device doesn't exist");
6307cc7f5022SIdo Schimmel return -ENODEV;
6308cc7f5022SIdo Schimmel }
6309cc7f5022SIdo Schimmel
6310cc7f5022SIdo Schimmel if (NL_REQ_ATTR_CHECK(extack, NULL, tb, MDBA_SET_ENTRY)) {
6311cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Missing MDBA_SET_ENTRY attribute");
6312cc7f5022SIdo Schimmel return -EINVAL;
6313cc7f5022SIdo Schimmel }
6314cc7f5022SIdo Schimmel
6315cc7f5022SIdo Schimmel if (!dev->netdev_ops->ndo_mdb_add) {
6316cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Device does not support MDB operations");
6317cc7f5022SIdo Schimmel return -EOPNOTSUPP;
6318cc7f5022SIdo Schimmel }
6319cc7f5022SIdo Schimmel
6320cc7f5022SIdo Schimmel return dev->netdev_ops->ndo_mdb_add(dev, tb, nlh->nlmsg_flags, extack);
6321cc7f5022SIdo Schimmel }
6322cc7f5022SIdo Schimmel
rtnl_mdb_del(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)6323cc7f5022SIdo Schimmel static int rtnl_mdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
6324cc7f5022SIdo Schimmel struct netlink_ext_ack *extack)
6325cc7f5022SIdo Schimmel {
6326cc7f5022SIdo Schimmel struct nlattr *tb[MDBA_SET_ENTRY_MAX + 1];
6327cc7f5022SIdo Schimmel struct net *net = sock_net(skb->sk);
6328cc7f5022SIdo Schimmel struct br_port_msg *bpm;
6329cc7f5022SIdo Schimmel struct net_device *dev;
6330cc7f5022SIdo Schimmel int err;
6331cc7f5022SIdo Schimmel
6332cc7f5022SIdo Schimmel err = nlmsg_parse_deprecated(nlh, sizeof(*bpm), tb,
6333cc7f5022SIdo Schimmel MDBA_SET_ENTRY_MAX, mdba_policy, extack);
6334cc7f5022SIdo Schimmel if (err)
6335cc7f5022SIdo Schimmel return err;
6336cc7f5022SIdo Schimmel
6337cc7f5022SIdo Schimmel bpm = nlmsg_data(nlh);
6338cc7f5022SIdo Schimmel if (!bpm->ifindex) {
6339cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Invalid ifindex");
6340cc7f5022SIdo Schimmel return -EINVAL;
6341cc7f5022SIdo Schimmel }
6342cc7f5022SIdo Schimmel
6343cc7f5022SIdo Schimmel dev = __dev_get_by_index(net, bpm->ifindex);
6344cc7f5022SIdo Schimmel if (!dev) {
6345cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Device doesn't exist");
6346cc7f5022SIdo Schimmel return -ENODEV;
6347cc7f5022SIdo Schimmel }
6348cc7f5022SIdo Schimmel
6349cc7f5022SIdo Schimmel if (NL_REQ_ATTR_CHECK(extack, NULL, tb, MDBA_SET_ENTRY)) {
6350cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Missing MDBA_SET_ENTRY attribute");
6351cc7f5022SIdo Schimmel return -EINVAL;
6352cc7f5022SIdo Schimmel }
6353cc7f5022SIdo Schimmel
6354cc7f5022SIdo Schimmel if (!dev->netdev_ops->ndo_mdb_del) {
6355cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Device does not support MDB operations");
6356cc7f5022SIdo Schimmel return -EOPNOTSUPP;
6357cc7f5022SIdo Schimmel }
6358cc7f5022SIdo Schimmel
6359cc7f5022SIdo Schimmel return dev->netdev_ops->ndo_mdb_del(dev, tb, extack);
6360cc7f5022SIdo Schimmel }
6361cc7f5022SIdo Schimmel
63621da177e4SLinus Torvalds /* Process one rtnetlink message. */
63631da177e4SLinus Torvalds
rtnetlink_rcv_msg(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)63642d4bc933SJohannes Berg static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
63652d4bc933SJohannes Berg struct netlink_ext_ack *extack)
63661da177e4SLinus Torvalds {
63673b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk);
6368addf9b90SFlorian Westphal struct rtnl_link *link;
636912dc5c2cSNikolay Aleksandrov enum rtnl_kinds kind;
6370e4202511SFlorian Westphal struct module *owner;
63716853dd48SFlorian Westphal int err = -EOPNOTSUPP;
6372e2849863SThomas Graf rtnl_doit_func doit;
637362256f98SFlorian Westphal unsigned int flags;
63741da177e4SLinus Torvalds int family;
63751da177e4SLinus Torvalds int type;
63761da177e4SLinus Torvalds
63771da177e4SLinus Torvalds type = nlh->nlmsg_type;
63781da177e4SLinus Torvalds if (type > RTM_MAX)
6379038890feSThomas Graf return -EOPNOTSUPP;
63801da177e4SLinus Torvalds
63811da177e4SLinus Torvalds type -= RTM_BASE;
63821da177e4SLinus Torvalds
63831da177e4SLinus Torvalds /* All the messages must have at least 1 byte length */
6384573ce260SHong zhi guo if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
63851da177e4SLinus Torvalds return 0;
63861da177e4SLinus Torvalds
6387573ce260SHong zhi guo family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
63882e9ea3e3SNikolay Aleksandrov kind = rtnl_msgtype_kind(type);
63891da177e4SLinus Torvalds
639012dc5c2cSNikolay Aleksandrov if (kind != RTNL_KIND_GET && !netlink_net_capable(skb, CAP_NET_ADMIN))
63911d00a4ebSThomas Graf return -EPERM;
63921da177e4SLinus Torvalds
63936853dd48SFlorian Westphal rcu_read_lock();
639412dc5c2cSNikolay Aleksandrov if (kind == RTNL_KIND_GET && (nlh->nlmsg_flags & NLM_F_DUMP)) {
639597c53cacSDenis V. Lunev struct sock *rtnl;
6396e2849863SThomas Graf rtnl_dumpit_func dumpit;
6397ebfe3c51SDi Zhu u32 min_dump_alloc = 0;
63981da177e4SLinus Torvalds
6399addf9b90SFlorian Westphal link = rtnl_get_link(family, type);
6400addf9b90SFlorian Westphal if (!link || !link->dumpit) {
64016853dd48SFlorian Westphal family = PF_UNSPEC;
6402addf9b90SFlorian Westphal link = rtnl_get_link(family, type);
6403addf9b90SFlorian Westphal if (!link || !link->dumpit)
64046853dd48SFlorian Westphal goto err_unlock;
64056853dd48SFlorian Westphal }
6406e4202511SFlorian Westphal owner = link->owner;
6407addf9b90SFlorian Westphal dumpit = link->dumpit;
6408ba5366b8SEric Dumazet flags = link->flags;
64096853dd48SFlorian Westphal
64105c2bb9b6SFlorian Westphal if (type == RTM_GETLINK - RTM_BASE)
6411e1fa6d21SFlorian Westphal min_dump_alloc = rtnl_calcit(skb, nlh);
64121da177e4SLinus Torvalds
6413e4202511SFlorian Westphal err = 0;
6414e4202511SFlorian Westphal /* need to do this before rcu_read_unlock() */
6415e4202511SFlorian Westphal if (!try_module_get(owner))
6416e4202511SFlorian Westphal err = -EPROTONOSUPPORT;
6417e4202511SFlorian Westphal
64186853dd48SFlorian Westphal rcu_read_unlock();
64196853dd48SFlorian Westphal
642097c53cacSDenis V. Lunev rtnl = net->rtnl;
6421e4202511SFlorian Westphal if (err == 0) {
642280d326faSPablo Neira Ayuso struct netlink_dump_control c = {
642380d326faSPablo Neira Ayuso .dump = dumpit,
642480d326faSPablo Neira Ayuso .min_dump_alloc = min_dump_alloc,
6425e4202511SFlorian Westphal .module = owner,
6426ba5366b8SEric Dumazet .flags = flags,
642780d326faSPablo Neira Ayuso };
642880d326faSPablo Neira Ayuso err = netlink_dump_start(rtnl, skb, nlh, &c);
6429e4202511SFlorian Westphal /* netlink_dump_start() will keep a reference on
6430e4202511SFlorian Westphal * module if dump is still in progress.
6431e4202511SFlorian Westphal */
6432e4202511SFlorian Westphal module_put(owner);
643380d326faSPablo Neira Ayuso }
64342907c35fSEric Dumazet return err;
64351da177e4SLinus Torvalds }
64361da177e4SLinus Torvalds
6437addf9b90SFlorian Westphal link = rtnl_get_link(family, type);
6438addf9b90SFlorian Westphal if (!link || !link->doit) {
64398caa38b5SFlorian Westphal family = PF_UNSPEC;
6440addf9b90SFlorian Westphal link = rtnl_get_link(PF_UNSPEC, type);
6441addf9b90SFlorian Westphal if (!link || !link->doit)
6442addf9b90SFlorian Westphal goto out_unlock;
64438caa38b5SFlorian Westphal }
64448caa38b5SFlorian Westphal
6445e4202511SFlorian Westphal owner = link->owner;
6446e4202511SFlorian Westphal if (!try_module_get(owner)) {
6447e4202511SFlorian Westphal err = -EPROTONOSUPPORT;
6448e4202511SFlorian Westphal goto out_unlock;
6449e4202511SFlorian Westphal }
6450e4202511SFlorian Westphal
6451addf9b90SFlorian Westphal flags = link->flags;
6452a6cec0bcSNikolay Aleksandrov if (kind == RTNL_KIND_DEL && (nlh->nlmsg_flags & NLM_F_BULK) &&
6453a6cec0bcSNikolay Aleksandrov !(flags & RTNL_FLAG_BULK_DEL_SUPPORTED)) {
6454a6cec0bcSNikolay Aleksandrov NL_SET_ERR_MSG(extack, "Bulk delete is not supported");
64555b22f627SZhengchao Shao module_put(owner);
6456a6cec0bcSNikolay Aleksandrov goto err_unlock;
6457a6cec0bcSNikolay Aleksandrov }
6458a6cec0bcSNikolay Aleksandrov
645962256f98SFlorian Westphal if (flags & RTNL_FLAG_DOIT_UNLOCKED) {
6460addf9b90SFlorian Westphal doit = link->doit;
646162256f98SFlorian Westphal rcu_read_unlock();
646262256f98SFlorian Westphal if (doit)
646362256f98SFlorian Westphal err = doit(skb, nlh, extack);
6464e4202511SFlorian Westphal module_put(owner);
646562256f98SFlorian Westphal return err;
646662256f98SFlorian Westphal }
64676853dd48SFlorian Westphal rcu_read_unlock();
64686853dd48SFlorian Westphal
64690cc09020SFlorian Westphal rtnl_lock();
6470addf9b90SFlorian Westphal link = rtnl_get_link(family, type);
6471addf9b90SFlorian Westphal if (link && link->doit)
6472addf9b90SFlorian Westphal err = link->doit(skb, nlh, extack);
64730cc09020SFlorian Westphal rtnl_unlock();
6474addf9b90SFlorian Westphal
6475e4202511SFlorian Westphal module_put(owner);
6476e4202511SFlorian Westphal
6477addf9b90SFlorian Westphal return err;
6478addf9b90SFlorian Westphal
6479addf9b90SFlorian Westphal out_unlock:
6480addf9b90SFlorian Westphal rcu_read_unlock();
64810cc09020SFlorian Westphal return err;
64820cc09020SFlorian Westphal
64830cc09020SFlorian Westphal err_unlock:
64846853dd48SFlorian Westphal rcu_read_unlock();
64850cc09020SFlorian Westphal return -EOPNOTSUPP;
64861da177e4SLinus Torvalds }
64871da177e4SLinus Torvalds
rtnetlink_rcv(struct sk_buff * skb)6488cd40b7d3SDenis V. Lunev static void rtnetlink_rcv(struct sk_buff *skb)
64891da177e4SLinus Torvalds {
6490cd40b7d3SDenis V. Lunev netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
64911da177e4SLinus Torvalds }
64921da177e4SLinus Torvalds
rtnetlink_bind(struct net * net,int group)64935f729eaaSJulien Gomes static int rtnetlink_bind(struct net *net, int group)
64945f729eaaSJulien Gomes {
64955f729eaaSJulien Gomes switch (group) {
64965f729eaaSJulien Gomes case RTNLGRP_IPV4_MROUTE_R:
64975f729eaaSJulien Gomes case RTNLGRP_IPV6_MROUTE_R:
64985f729eaaSJulien Gomes if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
64995f729eaaSJulien Gomes return -EPERM;
65005f729eaaSJulien Gomes break;
65015f729eaaSJulien Gomes }
65025f729eaaSJulien Gomes return 0;
65035f729eaaSJulien Gomes }
65045f729eaaSJulien Gomes
rtnetlink_event(struct notifier_block * this,unsigned long event,void * ptr)65051da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
65061da177e4SLinus Torvalds {
6507351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr);
6508e9dc8653SEric W. Biederman
65091da177e4SLinus Torvalds switch (event) {
65105138e86fSVlad Yasevich case NETDEV_REBOOT:
65118a212589SXin Long case NETDEV_CHANGEMTU:
65123753654eSDavid Ahern case NETDEV_CHANGEADDR:
65135138e86fSVlad Yasevich case NETDEV_CHANGENAME:
65145138e86fSVlad Yasevich case NETDEV_FEAT_CHANGE:
65155138e86fSVlad Yasevich case NETDEV_BONDING_FAILOVER:
6516e6e66594SXin Long case NETDEV_POST_TYPE_CHANGE:
65175138e86fSVlad Yasevich case NETDEV_NOTIFY_PEERS:
6518dc709f37SXin Long case NETDEV_CHANGEUPPER:
65195138e86fSVlad Yasevich case NETDEV_RESEND_IGMP:
65205138e86fSVlad Yasevich case NETDEV_CHANGEINFODATA:
6521eeda3fb9SXin Long case NETDEV_CHANGELOWERSTATE:
6522ebdcf045SXin Long case NETDEV_CHANGE_TX_QUEUE_LEN:
65233d3ea5afSVlad Yasevich rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
65241d997f10SHangbin Liu GFP_KERNEL, NULL, 0, 0, NULL);
65251da177e4SLinus Torvalds break;
65261da177e4SLinus Torvalds default:
65271da177e4SLinus Torvalds break;
65281da177e4SLinus Torvalds }
65291da177e4SLinus Torvalds return NOTIFY_DONE;
65301da177e4SLinus Torvalds }
65311da177e4SLinus Torvalds
65321da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = {
65331da177e4SLinus Torvalds .notifier_call = rtnetlink_event,
65341da177e4SLinus Torvalds };
65351da177e4SLinus Torvalds
653697c53cacSDenis V. Lunev
rtnetlink_net_init(struct net * net)65372c8c1e72SAlexey Dobriyan static int __net_init rtnetlink_net_init(struct net *net)
653897c53cacSDenis V. Lunev {
653997c53cacSDenis V. Lunev struct sock *sk;
6540a31f2d17SPablo Neira Ayuso struct netlink_kernel_cfg cfg = {
6541a31f2d17SPablo Neira Ayuso .groups = RTNLGRP_MAX,
6542a31f2d17SPablo Neira Ayuso .input = rtnetlink_rcv,
6543a31f2d17SPablo Neira Ayuso .cb_mutex = &rtnl_mutex,
65449785e10aSPablo Neira Ayuso .flags = NL_CFG_F_NONROOT_RECV,
65455f729eaaSJulien Gomes .bind = rtnetlink_bind,
6546a31f2d17SPablo Neira Ayuso };
6547a31f2d17SPablo Neira Ayuso
65489f00d977SPablo Neira Ayuso sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
654997c53cacSDenis V. Lunev if (!sk)
655097c53cacSDenis V. Lunev return -ENOMEM;
655197c53cacSDenis V. Lunev net->rtnl = sk;
655297c53cacSDenis V. Lunev return 0;
655397c53cacSDenis V. Lunev }
655497c53cacSDenis V. Lunev
rtnetlink_net_exit(struct net * net)65552c8c1e72SAlexey Dobriyan static void __net_exit rtnetlink_net_exit(struct net *net)
655697c53cacSDenis V. Lunev {
6557b7c6ba6eSDenis V. Lunev netlink_kernel_release(net->rtnl);
655897c53cacSDenis V. Lunev net->rtnl = NULL;
655997c53cacSDenis V. Lunev }
656097c53cacSDenis V. Lunev
656197c53cacSDenis V. Lunev static struct pernet_operations rtnetlink_net_ops = {
656297c53cacSDenis V. Lunev .init = rtnetlink_net_init,
656397c53cacSDenis V. Lunev .exit = rtnetlink_net_exit,
656497c53cacSDenis V. Lunev };
656597c53cacSDenis V. Lunev
rtnetlink_init(void)65661da177e4SLinus Torvalds void __init rtnetlink_init(void)
65671da177e4SLinus Torvalds {
656897c53cacSDenis V. Lunev if (register_pernet_subsys(&rtnetlink_net_ops))
65691da177e4SLinus Torvalds panic("rtnetlink_init: cannot initialize rtnetlink\n");
657097c53cacSDenis V. Lunev
65711da177e4SLinus Torvalds register_netdevice_notifier(&rtnetlink_dev_notifier);
6572340d17fcSThomas Graf
6573c7ac8679SGreg Rose rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
6574b97bac64SFlorian Westphal rtnl_dump_ifinfo, 0);
6575b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0);
6576b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0);
6577b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0);
6578687ad8ccSThomas Graf
6579b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0);
6580b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
6581b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
658277162022SJohn Fastabend
658336fbf1e5SJiri Pirko rtnl_register(PF_UNSPEC, RTM_NEWLINKPROP, rtnl_newlinkprop, NULL, 0);
658436fbf1e5SJiri Pirko rtnl_register(PF_UNSPEC, RTM_DELLINKPROP, rtnl_dellinkprop, NULL, 0);
658536fbf1e5SJiri Pirko
6586b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
65879e834259SNikolay Aleksandrov rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL,
65889e834259SNikolay Aleksandrov RTNL_FLAG_BULK_DEL_SUPPORTED);
65895b2f94b2SRoopa Prabhu rtnl_register(PF_BRIDGE, RTM_GETNEIGH, rtnl_fdb_get, rtnl_fdb_dump, 0);
6590e5a55a89SJohn Fastabend
6591b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
6592b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
6593b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0);
659410c9ead9SRoopa Prabhu
659510c9ead9SRoopa Prabhu rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,
6596b97bac64SFlorian Westphal 0);
659703ba3566SPetr Machata rtnl_register(PF_UNSPEC, RTM_SETSTATS, rtnl_stats_set, NULL, 0);
6598cc7f5022SIdo Schimmel
6599cc7f5022SIdo Schimmel rtnl_register(PF_BRIDGE, RTM_GETMDB, NULL, rtnl_mdb_dump, 0);
6600cc7f5022SIdo Schimmel rtnl_register(PF_BRIDGE, RTM_NEWMDB, rtnl_mdb_add, NULL, 0);
6601cc7f5022SIdo Schimmel rtnl_register(PF_BRIDGE, RTM_DELMDB, rtnl_mdb_del, NULL, 0);
66021da177e4SLinus Torvalds }
6603