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> 57*cc7f5022SIdo Schimmel #if IS_ENABLED(CONFIG_IPV6) 58*cc7f5022SIdo Schimmel #include <net/addrconf.h> 59*cc7f5022SIdo Schimmel #endif 601da177e4SLinus Torvalds 616264f58cSJakub Kicinski #include "dev.h" 626264f58cSJakub Kicinski 63a428afe8SNikolay Aleksandrov #define RTNL_MAX_TYPE 50 64a1aee20dSPetr Machata #define RTNL_SLAVE_MAX_TYPE 42 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 761da177e4SLinus Torvalds void rtnl_lock(void) 771da177e4SLinus Torvalds { 786756ae4bSStephen Hemminger mutex_lock(&rtnl_mutex); 791da177e4SLinus Torvalds } 80e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_lock); 811da177e4SLinus Torvalds 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; 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 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 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 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 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 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 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 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 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 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 */ 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 */ 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 */ 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 */ 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 39238f7b870SPatrick McHardy static LIST_HEAD(link_ops); 39338f7b870SPatrick McHardy 394c63044f0SEric Dumazet static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind) 395c63044f0SEric Dumazet { 396c63044f0SEric Dumazet const struct rtnl_link_ops *ops; 397c63044f0SEric Dumazet 398c63044f0SEric Dumazet list_for_each_entry(ops, &link_ops, list) { 399c63044f0SEric Dumazet if (!strcmp(ops->kind, kind)) 400c63044f0SEric Dumazet return ops; 401c63044f0SEric Dumazet } 402c63044f0SEric Dumazet return NULL; 403c63044f0SEric Dumazet } 404c63044f0SEric Dumazet 40538f7b870SPatrick McHardy /** 40638f7b870SPatrick McHardy * __rtnl_link_register - Register rtnl_link_ops with rtnetlink. 40738f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to register 40838f7b870SPatrick McHardy * 40938f7b870SPatrick McHardy * The caller must hold the rtnl_mutex. This function should be used 41038f7b870SPatrick McHardy * by drivers that create devices during module initialization. It 41138f7b870SPatrick McHardy * must be called before registering the devices. 41238f7b870SPatrick McHardy * 41338f7b870SPatrick McHardy * Returns 0 on success or a negative error code. 41438f7b870SPatrick McHardy */ 41538f7b870SPatrick McHardy int __rtnl_link_register(struct rtnl_link_ops *ops) 41638f7b870SPatrick McHardy { 417c63044f0SEric Dumazet if (rtnl_link_ops_get(ops->kind)) 418c63044f0SEric Dumazet return -EEXIST; 419c63044f0SEric Dumazet 4208c713dc9SJohannes Berg /* The check for alloc/setup is here because if ops 421b0ab2fabSJiri Pirko * does not have that filled up, it is not possible 422b0ab2fabSJiri Pirko * to use the ops for creating device. So do not 423b0ab2fabSJiri Pirko * fill up dellink as well. That disables rtnl_dellink. 424b0ab2fabSJiri Pirko */ 4258c713dc9SJohannes Berg if ((ops->alloc || ops->setup) && !ops->dellink) 42623289a37SEric Dumazet ops->dellink = unregister_netdevice_queue; 4272d85cba2SPatrick McHardy 42838f7b870SPatrick McHardy list_add_tail(&ops->list, &link_ops); 42938f7b870SPatrick McHardy return 0; 43038f7b870SPatrick McHardy } 43138f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_register); 43238f7b870SPatrick McHardy 43338f7b870SPatrick McHardy /** 43438f7b870SPatrick McHardy * rtnl_link_register - Register rtnl_link_ops with rtnetlink. 43538f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to register 43638f7b870SPatrick McHardy * 43738f7b870SPatrick McHardy * Returns 0 on success or a negative error code. 43838f7b870SPatrick McHardy */ 43938f7b870SPatrick McHardy int rtnl_link_register(struct rtnl_link_ops *ops) 44038f7b870SPatrick McHardy { 44138f7b870SPatrick McHardy int err; 44238f7b870SPatrick McHardy 443ccf8dbcdSKees Cook /* Sanity-check max sizes to avoid stack buffer overflow. */ 444ccf8dbcdSKees Cook if (WARN_ON(ops->maxtype > RTNL_MAX_TYPE || 445ccf8dbcdSKees Cook ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE)) 446ccf8dbcdSKees Cook return -EINVAL; 447ccf8dbcdSKees Cook 44838f7b870SPatrick McHardy rtnl_lock(); 44938f7b870SPatrick McHardy err = __rtnl_link_register(ops); 45038f7b870SPatrick McHardy rtnl_unlock(); 45138f7b870SPatrick McHardy return err; 45238f7b870SPatrick McHardy } 45338f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_register); 45438f7b870SPatrick McHardy 455669f87baSPavel Emelyanov static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops) 456669f87baSPavel Emelyanov { 457669f87baSPavel Emelyanov struct net_device *dev; 45823289a37SEric Dumazet LIST_HEAD(list_kill); 45923289a37SEric Dumazet 460669f87baSPavel Emelyanov for_each_netdev(net, dev) { 46123289a37SEric Dumazet if (dev->rtnl_link_ops == ops) 46223289a37SEric Dumazet ops->dellink(dev, &list_kill); 463669f87baSPavel Emelyanov } 46423289a37SEric Dumazet unregister_netdevice_many(&list_kill); 465669f87baSPavel Emelyanov } 466669f87baSPavel Emelyanov 46738f7b870SPatrick McHardy /** 46838f7b870SPatrick McHardy * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. 46938f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to unregister 47038f7b870SPatrick McHardy * 471554873e5SKirill Tkhai * The caller must hold the rtnl_mutex and guarantee net_namespace_list 472554873e5SKirill Tkhai * integrity (hold pernet_ops_rwsem for writing to close the race 473554873e5SKirill Tkhai * with setup_net() and cleanup_net()). 47438f7b870SPatrick McHardy */ 47538f7b870SPatrick McHardy void __rtnl_link_unregister(struct rtnl_link_ops *ops) 47638f7b870SPatrick McHardy { 477881d966bSEric W. Biederman struct net *net; 4782d85cba2SPatrick McHardy 479881d966bSEric W. Biederman for_each_net(net) { 480669f87baSPavel Emelyanov __rtnl_kill_links(net, ops); 481881d966bSEric W. Biederman } 48238f7b870SPatrick McHardy list_del(&ops->list); 48338f7b870SPatrick McHardy } 48438f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_unregister); 48538f7b870SPatrick McHardy 486200b916fSCong Wang /* Return with the rtnl_lock held when there are no network 487200b916fSCong Wang * devices unregistering in any network namespace. 488200b916fSCong Wang */ 489200b916fSCong Wang static void rtnl_lock_unregistering_all(void) 490200b916fSCong Wang { 491200b916fSCong Wang struct net *net; 492200b916fSCong Wang bool unregistering; 493ff960a73SPeter Zijlstra DEFINE_WAIT_FUNC(wait, woken_wake_function); 494200b916fSCong Wang 495ff960a73SPeter Zijlstra add_wait_queue(&netdev_unregistering_wq, &wait); 496200b916fSCong Wang for (;;) { 497200b916fSCong Wang unregistering = false; 498200b916fSCong Wang rtnl_lock(); 499f0b07bb1SKirill Tkhai /* We held write locked pernet_ops_rwsem, and parallel 500f0b07bb1SKirill Tkhai * setup_net() and cleanup_net() are not possible. 501f0b07bb1SKirill Tkhai */ 502200b916fSCong Wang for_each_net(net) { 503ede6c39cSEric Dumazet if (atomic_read(&net->dev_unreg_count) > 0) { 504200b916fSCong Wang unregistering = true; 505200b916fSCong Wang break; 506200b916fSCong Wang } 507200b916fSCong Wang } 508200b916fSCong Wang if (!unregistering) 509200b916fSCong Wang break; 510200b916fSCong Wang __rtnl_unlock(); 511ff960a73SPeter Zijlstra 512ff960a73SPeter Zijlstra wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); 513200b916fSCong Wang } 514ff960a73SPeter Zijlstra remove_wait_queue(&netdev_unregistering_wq, &wait); 515200b916fSCong Wang } 516200b916fSCong Wang 51738f7b870SPatrick McHardy /** 51838f7b870SPatrick McHardy * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. 51938f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to unregister 52038f7b870SPatrick McHardy */ 52138f7b870SPatrick McHardy void rtnl_link_unregister(struct rtnl_link_ops *ops) 52238f7b870SPatrick McHardy { 5238518e9bbSKirill Tkhai /* Close the race with setup_net() and cleanup_net() */ 5244420bf21SKirill Tkhai down_write(&pernet_ops_rwsem); 525200b916fSCong Wang rtnl_lock_unregistering_all(); 52638f7b870SPatrick McHardy __rtnl_link_unregister(ops); 52738f7b870SPatrick McHardy rtnl_unlock(); 5284420bf21SKirill Tkhai up_write(&pernet_ops_rwsem); 52938f7b870SPatrick McHardy } 53038f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_unregister); 53138f7b870SPatrick McHardy 532ba7d49b1SJiri Pirko static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev) 533ba7d49b1SJiri Pirko { 534ba7d49b1SJiri Pirko struct net_device *master_dev; 535ba7d49b1SJiri Pirko const struct rtnl_link_ops *ops; 5368515ae38SFlorian Westphal size_t size = 0; 537ba7d49b1SJiri Pirko 5388515ae38SFlorian Westphal rcu_read_lock(); 5398515ae38SFlorian Westphal 5408515ae38SFlorian Westphal master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev); 541ba7d49b1SJiri Pirko if (!master_dev) 5428515ae38SFlorian Westphal goto out; 5438515ae38SFlorian Westphal 544ba7d49b1SJiri Pirko ops = master_dev->rtnl_link_ops; 5456049f253SFernando Luis Vazquez Cao if (!ops || !ops->get_slave_size) 5468515ae38SFlorian Westphal goto out; 547ba7d49b1SJiri Pirko /* IFLA_INFO_SLAVE_DATA + nested data */ 5488515ae38SFlorian Westphal size = nla_total_size(sizeof(struct nlattr)) + 549ba7d49b1SJiri Pirko ops->get_slave_size(master_dev, dev); 5508515ae38SFlorian Westphal 5518515ae38SFlorian Westphal out: 5528515ae38SFlorian Westphal rcu_read_unlock(); 5538515ae38SFlorian Westphal return size; 554ba7d49b1SJiri Pirko } 555ba7d49b1SJiri Pirko 55638f7b870SPatrick McHardy static size_t rtnl_link_get_size(const struct net_device *dev) 55738f7b870SPatrick McHardy { 55838f7b870SPatrick McHardy const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 55938f7b870SPatrick McHardy size_t size; 56038f7b870SPatrick McHardy 56138f7b870SPatrick McHardy if (!ops) 56238f7b870SPatrick McHardy return 0; 56338f7b870SPatrick McHardy 564369cf77aSThomas Graf size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */ 565369cf77aSThomas Graf nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */ 56638f7b870SPatrick McHardy 56738f7b870SPatrick McHardy if (ops->get_size) 56838f7b870SPatrick McHardy /* IFLA_INFO_DATA + nested data */ 569369cf77aSThomas Graf size += nla_total_size(sizeof(struct nlattr)) + 57038f7b870SPatrick McHardy ops->get_size(dev); 57138f7b870SPatrick McHardy 57238f7b870SPatrick McHardy if (ops->get_xstats_size) 573369cf77aSThomas Graf /* IFLA_INFO_XSTATS */ 574369cf77aSThomas Graf size += nla_total_size(ops->get_xstats_size(dev)); 57538f7b870SPatrick McHardy 576ba7d49b1SJiri Pirko size += rtnl_link_get_slave_info_data_size(dev); 577ba7d49b1SJiri Pirko 57838f7b870SPatrick McHardy return size; 57938f7b870SPatrick McHardy } 58038f7b870SPatrick McHardy 581f8ff182cSThomas Graf static LIST_HEAD(rtnl_af_ops); 582f8ff182cSThomas Graf 583f8ff182cSThomas Graf static const struct rtnl_af_ops *rtnl_af_lookup(const int family) 584f8ff182cSThomas Graf { 585f8ff182cSThomas Graf const struct rtnl_af_ops *ops; 586f8ff182cSThomas Graf 587a100243dSCong Wang ASSERT_RTNL(); 588a100243dSCong Wang 589a100243dSCong Wang list_for_each_entry(ops, &rtnl_af_ops, list) { 590f8ff182cSThomas Graf if (ops->family == family) 591f8ff182cSThomas Graf return ops; 592f8ff182cSThomas Graf } 593f8ff182cSThomas Graf 594f8ff182cSThomas Graf return NULL; 595f8ff182cSThomas Graf } 596f8ff182cSThomas Graf 597f8ff182cSThomas Graf /** 598f8ff182cSThomas Graf * rtnl_af_register - Register rtnl_af_ops with rtnetlink. 599f8ff182cSThomas Graf * @ops: struct rtnl_af_ops * to register 600f8ff182cSThomas Graf * 601f8ff182cSThomas Graf * Returns 0 on success or a negative error code. 602f8ff182cSThomas Graf */ 6033678a9d8Sstephen hemminger void rtnl_af_register(struct rtnl_af_ops *ops) 604f8ff182cSThomas Graf { 605f8ff182cSThomas Graf rtnl_lock(); 6065fa85a09SFlorian Westphal list_add_tail_rcu(&ops->list, &rtnl_af_ops); 607f8ff182cSThomas Graf rtnl_unlock(); 608f8ff182cSThomas Graf } 609f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_register); 610f8ff182cSThomas Graf 611f8ff182cSThomas Graf /** 612f8ff182cSThomas Graf * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink. 613f8ff182cSThomas Graf * @ops: struct rtnl_af_ops * to unregister 614f8ff182cSThomas Graf */ 615f8ff182cSThomas Graf void rtnl_af_unregister(struct rtnl_af_ops *ops) 616f8ff182cSThomas Graf { 617f8ff182cSThomas Graf rtnl_lock(); 6185fa85a09SFlorian Westphal list_del_rcu(&ops->list); 619f8ff182cSThomas Graf rtnl_unlock(); 6205fa85a09SFlorian Westphal 6215fa85a09SFlorian Westphal synchronize_rcu(); 622f8ff182cSThomas Graf } 623f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_unregister); 624f8ff182cSThomas Graf 625b1974ed0SArad, Ronen static size_t rtnl_link_get_af_size(const struct net_device *dev, 626b1974ed0SArad, Ronen u32 ext_filter_mask) 627f8ff182cSThomas Graf { 628f8ff182cSThomas Graf struct rtnl_af_ops *af_ops; 629f8ff182cSThomas Graf size_t size; 630f8ff182cSThomas Graf 631f8ff182cSThomas Graf /* IFLA_AF_SPEC */ 632f8ff182cSThomas Graf size = nla_total_size(sizeof(struct nlattr)); 633f8ff182cSThomas Graf 6345fa85a09SFlorian Westphal rcu_read_lock(); 6355fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 636f8ff182cSThomas Graf if (af_ops->get_link_af_size) { 637f8ff182cSThomas Graf /* AF_* + nested data */ 638f8ff182cSThomas Graf size += nla_total_size(sizeof(struct nlattr)) + 639b1974ed0SArad, Ronen af_ops->get_link_af_size(dev, ext_filter_mask); 640f8ff182cSThomas Graf } 641f8ff182cSThomas Graf } 6425fa85a09SFlorian Westphal rcu_read_unlock(); 643f8ff182cSThomas Graf 644f8ff182cSThomas Graf return size; 645f8ff182cSThomas Graf } 646f8ff182cSThomas Graf 647ba7d49b1SJiri Pirko static bool rtnl_have_link_slave_info(const struct net_device *dev) 648ba7d49b1SJiri Pirko { 649ba7d49b1SJiri Pirko struct net_device *master_dev; 6504c82a95eSFlorian Westphal bool ret = false; 651ba7d49b1SJiri Pirko 6524c82a95eSFlorian Westphal rcu_read_lock(); 6534c82a95eSFlorian Westphal 6544c82a95eSFlorian Westphal master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev); 655813f020cSJiri Pirko if (master_dev && master_dev->rtnl_link_ops) 6564c82a95eSFlorian Westphal ret = true; 6574c82a95eSFlorian Westphal rcu_read_unlock(); 6584c82a95eSFlorian Westphal return ret; 659ba7d49b1SJiri Pirko } 660ba7d49b1SJiri Pirko 661ba7d49b1SJiri Pirko static int rtnl_link_slave_info_fill(struct sk_buff *skb, 662ba7d49b1SJiri Pirko const struct net_device *dev) 663ba7d49b1SJiri Pirko { 664ba7d49b1SJiri Pirko struct net_device *master_dev; 665ba7d49b1SJiri Pirko const struct rtnl_link_ops *ops; 666ba7d49b1SJiri Pirko struct nlattr *slave_data; 667ba7d49b1SJiri Pirko int err; 668ba7d49b1SJiri Pirko 669ba7d49b1SJiri Pirko master_dev = netdev_master_upper_dev_get((struct net_device *) dev); 670ba7d49b1SJiri Pirko if (!master_dev) 671ba7d49b1SJiri Pirko return 0; 672ba7d49b1SJiri Pirko ops = master_dev->rtnl_link_ops; 673ba7d49b1SJiri Pirko if (!ops) 674ba7d49b1SJiri Pirko return 0; 675ba7d49b1SJiri Pirko if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0) 676ba7d49b1SJiri Pirko return -EMSGSIZE; 677ba7d49b1SJiri Pirko if (ops->fill_slave_info) { 678ae0be8deSMichal Kubecek slave_data = nla_nest_start_noflag(skb, IFLA_INFO_SLAVE_DATA); 679ba7d49b1SJiri Pirko if (!slave_data) 680ba7d49b1SJiri Pirko return -EMSGSIZE; 681ba7d49b1SJiri Pirko err = ops->fill_slave_info(skb, master_dev, dev); 682ba7d49b1SJiri Pirko if (err < 0) 683ba7d49b1SJiri Pirko goto err_cancel_slave_data; 684ba7d49b1SJiri Pirko nla_nest_end(skb, slave_data); 685ba7d49b1SJiri Pirko } 686ba7d49b1SJiri Pirko return 0; 687ba7d49b1SJiri Pirko 688ba7d49b1SJiri Pirko err_cancel_slave_data: 689ba7d49b1SJiri Pirko nla_nest_cancel(skb, slave_data); 690ba7d49b1SJiri Pirko return err; 691ba7d49b1SJiri Pirko } 692ba7d49b1SJiri Pirko 693ba7d49b1SJiri Pirko static int rtnl_link_info_fill(struct sk_buff *skb, 694ba7d49b1SJiri Pirko const struct net_device *dev) 69538f7b870SPatrick McHardy { 69638f7b870SPatrick McHardy const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 697ba7d49b1SJiri Pirko struct nlattr *data; 698ba7d49b1SJiri Pirko int err; 699ba7d49b1SJiri Pirko 700ba7d49b1SJiri Pirko if (!ops) 701ba7d49b1SJiri Pirko return 0; 702ba7d49b1SJiri Pirko if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0) 703ba7d49b1SJiri Pirko return -EMSGSIZE; 704ba7d49b1SJiri Pirko if (ops->fill_xstats) { 705ba7d49b1SJiri Pirko err = ops->fill_xstats(skb, dev); 706ba7d49b1SJiri Pirko if (err < 0) 707ba7d49b1SJiri Pirko return err; 708ba7d49b1SJiri Pirko } 709ba7d49b1SJiri Pirko if (ops->fill_info) { 710ae0be8deSMichal Kubecek data = nla_nest_start_noflag(skb, IFLA_INFO_DATA); 711ba7d49b1SJiri Pirko if (data == NULL) 712ba7d49b1SJiri Pirko return -EMSGSIZE; 713ba7d49b1SJiri Pirko err = ops->fill_info(skb, dev); 714ba7d49b1SJiri Pirko if (err < 0) 715ba7d49b1SJiri Pirko goto err_cancel_data; 716ba7d49b1SJiri Pirko nla_nest_end(skb, data); 717ba7d49b1SJiri Pirko } 718ba7d49b1SJiri Pirko return 0; 719ba7d49b1SJiri Pirko 720ba7d49b1SJiri Pirko err_cancel_data: 721ba7d49b1SJiri Pirko nla_nest_cancel(skb, data); 722ba7d49b1SJiri Pirko return err; 723ba7d49b1SJiri Pirko } 724ba7d49b1SJiri Pirko 725ba7d49b1SJiri Pirko static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev) 726ba7d49b1SJiri Pirko { 727ba7d49b1SJiri Pirko struct nlattr *linkinfo; 72838f7b870SPatrick McHardy int err = -EMSGSIZE; 72938f7b870SPatrick McHardy 730ae0be8deSMichal Kubecek linkinfo = nla_nest_start_noflag(skb, IFLA_LINKINFO); 73138f7b870SPatrick McHardy if (linkinfo == NULL) 73238f7b870SPatrick McHardy goto out; 73338f7b870SPatrick McHardy 734ba7d49b1SJiri Pirko err = rtnl_link_info_fill(skb, dev); 73538f7b870SPatrick McHardy if (err < 0) 73638f7b870SPatrick McHardy goto err_cancel_link; 737ba7d49b1SJiri Pirko 738ba7d49b1SJiri Pirko err = rtnl_link_slave_info_fill(skb, dev); 73938f7b870SPatrick McHardy if (err < 0) 740ba7d49b1SJiri Pirko goto err_cancel_link; 74138f7b870SPatrick McHardy 74238f7b870SPatrick McHardy nla_nest_end(skb, linkinfo); 74338f7b870SPatrick McHardy return 0; 74438f7b870SPatrick McHardy 74538f7b870SPatrick McHardy err_cancel_link: 74638f7b870SPatrick McHardy nla_nest_cancel(skb, linkinfo); 74738f7b870SPatrick McHardy out: 74838f7b870SPatrick McHardy return err; 74938f7b870SPatrick McHardy } 75038f7b870SPatrick McHardy 75195c96174SEric Dumazet int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo) 7521da177e4SLinus Torvalds { 75397c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 7541da177e4SLinus Torvalds 755cfdf0d9aSYajun Deng return nlmsg_notify(rtnl, skb, pid, group, echo, GFP_KERNEL); 7561da177e4SLinus Torvalds } 7571da177e4SLinus Torvalds 75897c53cacSDenis V. Lunev int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid) 7592942e900SThomas Graf { 76097c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 76197c53cacSDenis V. Lunev 7622942e900SThomas Graf return nlmsg_unicast(rtnl, skb, pid); 7632942e900SThomas Graf } 764e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unicast); 7652942e900SThomas Graf 7661ce85fe4SPablo Neira Ayuso void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group, 7671d997f10SHangbin Liu const struct nlmsghdr *nlh, gfp_t flags) 76897676b6bSThomas Graf { 76997c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 77097676b6bSThomas Graf 771f9b282b3SYajun Deng nlmsg_notify(rtnl, skb, pid, group, nlmsg_report(nlh), flags); 77297676b6bSThomas Graf } 773e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_notify); 77497676b6bSThomas Graf 77597c53cacSDenis V. Lunev void rtnl_set_sk_err(struct net *net, u32 group, int error) 77697676b6bSThomas Graf { 77797c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 77897c53cacSDenis V. Lunev 77997676b6bSThomas Graf netlink_set_err(rtnl, 0, group, error); 78097676b6bSThomas Graf } 781e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_set_sk_err); 78297676b6bSThomas Graf 7831da177e4SLinus Torvalds int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics) 7841da177e4SLinus Torvalds { 7852d7202bfSThomas Graf struct nlattr *mx; 7862d7202bfSThomas Graf int i, valid = 0; 7871da177e4SLinus Torvalds 788c22a133aSDavid Ahern /* nothing is dumped for dst_default_metrics, so just skip the loop */ 789c22a133aSDavid Ahern if (metrics == dst_default_metrics.metrics) 790c22a133aSDavid Ahern return 0; 791c22a133aSDavid Ahern 792ae0be8deSMichal Kubecek mx = nla_nest_start_noflag(skb, RTA_METRICS); 7932d7202bfSThomas Graf if (mx == NULL) 7942d7202bfSThomas Graf return -ENOBUFS; 7952d7202bfSThomas Graf 7961da177e4SLinus Torvalds for (i = 0; i < RTAX_MAX; i++) { 7972d7202bfSThomas Graf if (metrics[i]) { 798ea697639SDaniel Borkmann if (i == RTAX_CC_ALGO - 1) { 799ea697639SDaniel Borkmann char tmp[TCP_CA_NAME_MAX], *name; 800ea697639SDaniel Borkmann 801ea697639SDaniel Borkmann name = tcp_ca_get_name_by_key(metrics[i], tmp); 802ea697639SDaniel Borkmann if (!name) 803ea697639SDaniel Borkmann continue; 804ea697639SDaniel Borkmann if (nla_put_string(skb, i + 1, name)) 805ea697639SDaniel Borkmann goto nla_put_failure; 806c3a8d947SDaniel Borkmann } else if (i == RTAX_FEATURES - 1) { 807c3a8d947SDaniel Borkmann u32 user_features = metrics[i] & RTAX_FEATURE_MASK; 808c3a8d947SDaniel Borkmann 809f8edcd12SPhil Sutter if (!user_features) 810f8edcd12SPhil Sutter continue; 811c3a8d947SDaniel Borkmann BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK); 812c3a8d947SDaniel Borkmann if (nla_put_u32(skb, i + 1, user_features)) 813c3a8d947SDaniel Borkmann goto nla_put_failure; 814ea697639SDaniel Borkmann } else { 815a6574349SDavid S. Miller if (nla_put_u32(skb, i + 1, metrics[i])) 816a6574349SDavid S. Miller goto nla_put_failure; 8171da177e4SLinus Torvalds } 818ea697639SDaniel Borkmann valid++; 819ea697639SDaniel Borkmann } 8202d7202bfSThomas Graf } 8211da177e4SLinus Torvalds 822a57d27fcSDavid S. Miller if (!valid) { 823a57d27fcSDavid S. Miller nla_nest_cancel(skb, mx); 824a57d27fcSDavid S. Miller return 0; 825a57d27fcSDavid S. Miller } 8262d7202bfSThomas Graf 8272d7202bfSThomas Graf return nla_nest_end(skb, mx); 8282d7202bfSThomas Graf 8292d7202bfSThomas Graf nla_put_failure: 830bc3ed28cSThomas Graf nla_nest_cancel(skb, mx); 831bc3ed28cSThomas Graf return -EMSGSIZE; 8321da177e4SLinus Torvalds } 833e0d087afSEric Dumazet EXPORT_SYMBOL(rtnetlink_put_metrics); 8341da177e4SLinus Torvalds 835e3703b3dSThomas Graf int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id, 83687a50699SDavid S. Miller long expires, u32 error) 837e3703b3dSThomas Graf { 838e3703b3dSThomas Graf struct rta_cacheinfo ci = { 839e3703b3dSThomas Graf .rta_error = error, 840e3703b3dSThomas Graf .rta_id = id, 841e3703b3dSThomas Graf }; 842e3703b3dSThomas Graf 8433940746dSDavid Ahern if (dst) { 8443940746dSDavid Ahern ci.rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse); 8453940746dSDavid Ahern ci.rta_used = dst->__use; 8463940746dSDavid Ahern ci.rta_clntref = atomic_read(&dst->__refcnt); 8473940746dSDavid Ahern } 8488253947eSLi Wei if (expires) { 8498253947eSLi Wei unsigned long clock; 850e3703b3dSThomas Graf 8518253947eSLi Wei clock = jiffies_to_clock_t(abs(expires)); 8528253947eSLi Wei clock = min_t(unsigned long, clock, INT_MAX); 8538253947eSLi Wei ci.rta_expires = (expires > 0) ? clock : -clock; 8548253947eSLi Wei } 855e3703b3dSThomas Graf return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci); 856e3703b3dSThomas Graf } 857e3703b3dSThomas Graf EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo); 8581da177e4SLinus Torvalds 85993b2d4a2SDavid S. Miller static void set_operstate(struct net_device *dev, unsigned char transition) 860b00055aaSStefan Rompf { 861b00055aaSStefan Rompf unsigned char operstate = dev->operstate; 862b00055aaSStefan Rompf 863b00055aaSStefan Rompf switch (transition) { 864b00055aaSStefan Rompf case IF_OPER_UP: 865b00055aaSStefan Rompf if ((operstate == IF_OPER_DORMANT || 866eec517cdSAndrew Lunn operstate == IF_OPER_TESTING || 867b00055aaSStefan Rompf operstate == IF_OPER_UNKNOWN) && 868eec517cdSAndrew Lunn !netif_dormant(dev) && !netif_testing(dev)) 869b00055aaSStefan Rompf operstate = IF_OPER_UP; 870b00055aaSStefan Rompf break; 871b00055aaSStefan Rompf 872eec517cdSAndrew Lunn case IF_OPER_TESTING: 873abbc7928SJuhee Kang if (netif_oper_up(dev)) 874eec517cdSAndrew Lunn operstate = IF_OPER_TESTING; 875eec517cdSAndrew Lunn break; 876eec517cdSAndrew Lunn 877b00055aaSStefan Rompf case IF_OPER_DORMANT: 878abbc7928SJuhee Kang if (netif_oper_up(dev)) 879b00055aaSStefan Rompf operstate = IF_OPER_DORMANT; 880b00055aaSStefan Rompf break; 8813ff50b79SStephen Hemminger } 882b00055aaSStefan Rompf 883b00055aaSStefan Rompf if (dev->operstate != operstate) { 884fd888e85SSebastian Andrzej Siewior write_lock(&dev_base_lock); 885b00055aaSStefan Rompf dev->operstate = operstate; 886fd888e85SSebastian Andrzej Siewior write_unlock(&dev_base_lock); 887b00055aaSStefan Rompf netdev_state_change(dev); 88893b2d4a2SDavid S. Miller } 889b00055aaSStefan Rompf } 890b00055aaSStefan Rompf 891b1beb681SJiri Benc static unsigned int rtnl_dev_get_flags(const struct net_device *dev) 892b1beb681SJiri Benc { 893b1beb681SJiri Benc return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) | 894b1beb681SJiri Benc (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI)); 895b1beb681SJiri Benc } 896b1beb681SJiri Benc 8973729d502SPatrick McHardy static unsigned int rtnl_dev_combine_flags(const struct net_device *dev, 8983729d502SPatrick McHardy const struct ifinfomsg *ifm) 8993729d502SPatrick McHardy { 9003729d502SPatrick McHardy unsigned int flags = ifm->ifi_flags; 9013729d502SPatrick McHardy 9023729d502SPatrick McHardy /* bugwards compatibility: ifi_change == 0 is treated as ~0 */ 9033729d502SPatrick McHardy if (ifm->ifi_change) 9043729d502SPatrick McHardy flags = (flags & ifm->ifi_change) | 905b1beb681SJiri Benc (rtnl_dev_get_flags(dev) & ~ifm->ifi_change); 9063729d502SPatrick McHardy 9073729d502SPatrick McHardy return flags; 9083729d502SPatrick McHardy } 9093729d502SPatrick McHardy 910b60c5115SThomas Graf static void copy_rtnl_link_stats(struct rtnl_link_stats *a, 911be1f3c2cSBen Hutchings const struct rtnl_link_stats64 *b) 9121da177e4SLinus Torvalds { 913b60c5115SThomas Graf a->rx_packets = b->rx_packets; 914b60c5115SThomas Graf a->tx_packets = b->tx_packets; 915b60c5115SThomas Graf a->rx_bytes = b->rx_bytes; 916b60c5115SThomas Graf a->tx_bytes = b->tx_bytes; 917b60c5115SThomas Graf a->rx_errors = b->rx_errors; 918b60c5115SThomas Graf a->tx_errors = b->tx_errors; 919b60c5115SThomas Graf a->rx_dropped = b->rx_dropped; 920b60c5115SThomas Graf a->tx_dropped = b->tx_dropped; 921b60c5115SThomas Graf 922b60c5115SThomas Graf a->multicast = b->multicast; 923b60c5115SThomas Graf a->collisions = b->collisions; 924b60c5115SThomas Graf 925b60c5115SThomas Graf a->rx_length_errors = b->rx_length_errors; 926b60c5115SThomas Graf a->rx_over_errors = b->rx_over_errors; 927b60c5115SThomas Graf a->rx_crc_errors = b->rx_crc_errors; 928b60c5115SThomas Graf a->rx_frame_errors = b->rx_frame_errors; 929b60c5115SThomas Graf a->rx_fifo_errors = b->rx_fifo_errors; 930b60c5115SThomas Graf a->rx_missed_errors = b->rx_missed_errors; 931b60c5115SThomas Graf 932b60c5115SThomas Graf a->tx_aborted_errors = b->tx_aborted_errors; 933b60c5115SThomas Graf a->tx_carrier_errors = b->tx_carrier_errors; 934b60c5115SThomas Graf a->tx_fifo_errors = b->tx_fifo_errors; 935b60c5115SThomas Graf a->tx_heartbeat_errors = b->tx_heartbeat_errors; 936b60c5115SThomas Graf a->tx_window_errors = b->tx_window_errors; 937b60c5115SThomas Graf 938b60c5115SThomas Graf a->rx_compressed = b->rx_compressed; 939b60c5115SThomas Graf a->tx_compressed = b->tx_compressed; 9406e7333d3SJarod Wilson 9416e7333d3SJarod Wilson a->rx_nohandler = b->rx_nohandler; 94210708f37SJan Engelhardt } 94310708f37SJan Engelhardt 944c02db8c6SChris Wright /* All VF info */ 945115c9b81SGreg Rose static inline int rtnl_vfinfo_size(const struct net_device *dev, 946115c9b81SGreg Rose u32 ext_filter_mask) 947ebc08a6fSWilliams, Mitch A { 9489af15c38SPhil Sutter if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) { 949c02db8c6SChris Wright int num_vfs = dev_num_vf(dev->dev.parent); 9507e75f74aSSabrina Dubroca size_t size = nla_total_size(0); 951045de01aSScott Feldman size += num_vfs * 9527e75f74aSSabrina Dubroca (nla_total_size(0) + 9537e75f74aSSabrina Dubroca nla_total_size(sizeof(struct ifla_vf_mac)) + 95475345f88SDenis Kirjanov nla_total_size(sizeof(struct ifla_vf_broadcast)) + 9557e75f74aSSabrina Dubroca nla_total_size(sizeof(struct ifla_vf_vlan)) + 9567e75f74aSSabrina Dubroca nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */ 95779aab093SMoshe Shemesh nla_total_size(MAX_VLAN_LIST_LEN * 95879aab093SMoshe Shemesh sizeof(struct ifla_vf_vlan_info)) + 959ed616689SSucheta Chakraborty nla_total_size(sizeof(struct ifla_vf_spoofchk)) + 9607e75f74aSSabrina Dubroca nla_total_size(sizeof(struct ifla_vf_tx_rate)) + 961945a3676SJiri Benc nla_total_size(sizeof(struct ifla_vf_rate)) + 96201a3d796SVlad Zolotarov nla_total_size(sizeof(struct ifla_vf_link_state)) + 9633b766cd8SEran Ben Elisha nla_total_size(sizeof(struct ifla_vf_rss_query_en)) + 9647e75f74aSSabrina Dubroca nla_total_size(0) + /* nest IFLA_VF_STATS */ 9653b766cd8SEran Ben Elisha /* IFLA_VF_STATS_RX_PACKETS */ 966343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 9673b766cd8SEran Ben Elisha /* IFLA_VF_STATS_TX_PACKETS */ 968343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 9693b766cd8SEran Ben Elisha /* IFLA_VF_STATS_RX_BYTES */ 970343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 9713b766cd8SEran Ben Elisha /* IFLA_VF_STATS_TX_BYTES */ 972343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 9733b766cd8SEran Ben Elisha /* IFLA_VF_STATS_BROADCAST */ 974343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 9753b766cd8SEran Ben Elisha /* IFLA_VF_STATS_MULTICAST */ 976343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 977c5a9f6f0SEugenia Emantayev /* IFLA_VF_STATS_RX_DROPPED */ 978c5a9f6f0SEugenia Emantayev nla_total_size_64bit(sizeof(__u64)) + 979c5a9f6f0SEugenia Emantayev /* IFLA_VF_STATS_TX_DROPPED */ 980c5a9f6f0SEugenia Emantayev nla_total_size_64bit(sizeof(__u64)) + 981dd461d6aSHiroshi Shimamoto nla_total_size(sizeof(struct ifla_vf_trust))); 982c02db8c6SChris Wright return size; 983c02db8c6SChris Wright } else 984ebc08a6fSWilliams, Mitch A return 0; 985ebc08a6fSWilliams, Mitch A } 986ebc08a6fSWilliams, Mitch A 987c53864fdSDavid Gibson static size_t rtnl_port_size(const struct net_device *dev, 988c53864fdSDavid Gibson u32 ext_filter_mask) 98957b61080SScott Feldman { 99057b61080SScott Feldman size_t port_size = nla_total_size(4) /* PORT_VF */ 99157b61080SScott Feldman + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */ 99257b61080SScott Feldman + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */ 99357b61080SScott Feldman + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */ 99457b61080SScott Feldman + nla_total_size(1) /* PROT_VDP_REQUEST */ 99557b61080SScott Feldman + nla_total_size(2); /* PORT_VDP_RESPONSE */ 99657b61080SScott Feldman size_t vf_ports_size = nla_total_size(sizeof(struct nlattr)); 99757b61080SScott Feldman size_t vf_port_size = nla_total_size(sizeof(struct nlattr)) 99857b61080SScott Feldman + port_size; 99957b61080SScott Feldman size_t port_self_size = nla_total_size(sizeof(struct nlattr)) 100057b61080SScott Feldman + port_size; 100157b61080SScott Feldman 1002c53864fdSDavid Gibson if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent || 1003c53864fdSDavid Gibson !(ext_filter_mask & RTEXT_FILTER_VF)) 100457b61080SScott Feldman return 0; 100557b61080SScott Feldman if (dev_num_vf(dev->dev.parent)) 100657b61080SScott Feldman return port_self_size + vf_ports_size + 100757b61080SScott Feldman vf_port_size * dev_num_vf(dev->dev.parent); 100857b61080SScott Feldman else 100957b61080SScott Feldman return port_self_size; 101057b61080SScott Feldman } 101157b61080SScott Feldman 1012b5cdae32SDavid S. Miller static size_t rtnl_xdp_size(void) 1013d1fdd913SBrenden Blanco { 1014b3cfaa31SSabrina Dubroca size_t xdp_size = nla_total_size(0) + /* nest IFLA_XDP */ 101558038695SMartin KaFai Lau nla_total_size(1) + /* XDP_ATTACHED */ 1016a25717d2SJakub Kicinski nla_total_size(4) + /* XDP_PROG_ID (or 1st mode) */ 10174f91da26SJakub Kicinski nla_total_size(4); /* XDP_<mode>_PROG_ID */ 1018d1fdd913SBrenden Blanco 1019d1fdd913SBrenden Blanco return xdp_size; 1020d1fdd913SBrenden Blanco } 1021d1fdd913SBrenden Blanco 102288f4fb0cSJiri Pirko static size_t rtnl_prop_list_size(const struct net_device *dev) 102388f4fb0cSJiri Pirko { 102488f4fb0cSJiri Pirko struct netdev_name_node *name_node; 102588f4fb0cSJiri Pirko size_t size; 102688f4fb0cSJiri Pirko 102788f4fb0cSJiri Pirko if (list_empty(&dev->name_node->list)) 102888f4fb0cSJiri Pirko return 0; 102988f4fb0cSJiri Pirko size = nla_total_size(0); 103088f4fb0cSJiri Pirko list_for_each_entry(name_node, &dev->name_node->list, list) 103188f4fb0cSJiri Pirko size += nla_total_size(ALTIFNAMSIZ); 103288f4fb0cSJiri Pirko return size; 103388f4fb0cSJiri Pirko } 103488f4fb0cSJiri Pirko 1035829eb208SRoopa Prabhu static size_t rtnl_proto_down_size(const struct net_device *dev) 1036829eb208SRoopa Prabhu { 1037829eb208SRoopa Prabhu size_t size = nla_total_size(1); 1038829eb208SRoopa Prabhu 1039829eb208SRoopa Prabhu if (dev->proto_down_reason) 1040829eb208SRoopa Prabhu size += nla_total_size(0) + nla_total_size(4); 1041829eb208SRoopa Prabhu 1042829eb208SRoopa Prabhu return size; 1043829eb208SRoopa Prabhu } 1044829eb208SRoopa Prabhu 1045dca56c30SJiri Pirko static size_t rtnl_devlink_port_size(const struct net_device *dev) 1046dca56c30SJiri Pirko { 1047dca56c30SJiri Pirko size_t size = nla_total_size(0); /* nest IFLA_DEVLINK_PORT */ 1048dca56c30SJiri Pirko 1049dca56c30SJiri Pirko if (dev->devlink_port) 1050dca56c30SJiri Pirko size += devlink_nl_port_handle_size(dev->devlink_port); 1051dca56c30SJiri Pirko 1052dca56c30SJiri Pirko return size; 1053dca56c30SJiri Pirko } 1054dca56c30SJiri Pirko 1055115c9b81SGreg Rose static noinline size_t if_nlmsg_size(const struct net_device *dev, 1056115c9b81SGreg Rose u32 ext_filter_mask) 1057339bf98fSThomas Graf { 1058339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 1059339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 10600b815a1aSStephen Hemminger + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */ 1061339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */ 1062270cb4d0SNicolas Dichtel + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap)) 1063339bf98fSThomas Graf + nla_total_size(sizeof(struct rtnl_link_stats)) 106435c58459SDavid S. Miller + nla_total_size_64bit(sizeof(struct rtnl_link_stats64)) 1065339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 1066339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */ 1067339bf98fSThomas Graf + nla_total_size(4) /* IFLA_TXQLEN */ 1068339bf98fSThomas Graf + nla_total_size(4) /* IFLA_WEIGHT */ 1069339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MTU */ 1070339bf98fSThomas Graf + nla_total_size(4) /* IFLA_LINK */ 1071339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MASTER */ 10729a57247fSJiri Pirko + nla_total_size(1) /* IFLA_CARRIER */ 1073edbc0bb3SBen Greear + nla_total_size(4) /* IFLA_PROMISCUITY */ 10747e6e1b57SNicolas Dichtel + nla_total_size(4) /* IFLA_ALLMULTI */ 107576ff5cc9SJiri Pirko + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */ 107676ff5cc9SJiri Pirko + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */ 10776919756cSTobias Klauser + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */ 10786919756cSTobias Klauser + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */ 1079eac1b93cSCoco Li + nla_total_size(4) /* IFLA_GRO_MAX_SIZE */ 10809eefedd5SXin Long + nla_total_size(4) /* IFLA_GSO_IPV4_MAX_SIZE */ 10819eefedd5SXin Long + nla_total_size(4) /* IFLA_GRO_IPV4_MAX_SIZE */ 108289527be8SEric Dumazet + nla_total_size(4) /* IFLA_TSO_MAX_SIZE */ 108389527be8SEric Dumazet + nla_total_size(4) /* IFLA_TSO_MAX_SEGS */ 1084339bf98fSThomas Graf + nla_total_size(1) /* IFLA_OPERSTATE */ 108538f7b870SPatrick McHardy + nla_total_size(1) /* IFLA_LINKMODE */ 10862d3b479dSdavid decotigny + nla_total_size(4) /* IFLA_CARRIER_CHANGES */ 1087d37512a2SNicolas Dichtel + nla_total_size(4) /* IFLA_LINK_NETNSID */ 1088db833d40SSerhey Popovych + nla_total_size(4) /* IFLA_GROUP */ 1089115c9b81SGreg Rose + nla_total_size(ext_filter_mask 1090115c9b81SGreg Rose & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */ 1091115c9b81SGreg Rose + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */ 1092c53864fdSDavid Gibson + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */ 1093f8ff182cSThomas Graf + rtnl_link_get_size(dev) /* IFLA_LINKINFO */ 1094b1974ed0SArad, Ronen + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */ 109582f28412SJiri Pirko + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */ 109688d6378bSAnuradha Karuppiah + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */ 1097c57c7a95SNicolas Dichtel + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */ 1098b5cdae32SDavid S. Miller + rtnl_xdp_size() /* IFLA_XDP */ 10993d3ea5afSVlad Yasevich + nla_total_size(4) /* IFLA_EVENT */ 11006621dd29SNicolas Dichtel + nla_total_size(4) /* IFLA_NEW_NETNSID */ 110138e01b30SNicolas Dichtel + nla_total_size(4) /* IFLA_NEW_IFINDEX */ 1102829eb208SRoopa Prabhu + rtnl_proto_down_size(dev) /* proto down */ 11037e4a8d5aSChristian Brauner + nla_total_size(4) /* IFLA_TARGET_NETNSID */ 1104b2d3bcfaSDavid Decotigny + nla_total_size(4) /* IFLA_CARRIER_UP_COUNT */ 1105b2d3bcfaSDavid Decotigny + nla_total_size(4) /* IFLA_CARRIER_DOWN_COUNT */ 11063e7a50ceSStephen Hemminger + nla_total_size(4) /* IFLA_MIN_MTU */ 11073e7a50ceSStephen Hemminger + nla_total_size(4) /* IFLA_MAX_MTU */ 110888f4fb0cSJiri Pirko + rtnl_prop_list_size(dev) 1109f74877a5SMichal Kubecek + nla_total_size(MAX_ADDR_LEN) /* IFLA_PERM_ADDRESS */ 1110dca56c30SJiri Pirko + rtnl_devlink_port_size(dev) 111179e1ad14SJiri Benc + 0; 1112339bf98fSThomas Graf } 1113339bf98fSThomas Graf 111457b61080SScott Feldman static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev) 111557b61080SScott Feldman { 111657b61080SScott Feldman struct nlattr *vf_ports; 111757b61080SScott Feldman struct nlattr *vf_port; 111857b61080SScott Feldman int vf; 111957b61080SScott Feldman int err; 112057b61080SScott Feldman 1121ae0be8deSMichal Kubecek vf_ports = nla_nest_start_noflag(skb, IFLA_VF_PORTS); 112257b61080SScott Feldman if (!vf_ports) 112357b61080SScott Feldman return -EMSGSIZE; 112457b61080SScott Feldman 112557b61080SScott Feldman for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) { 1126ae0be8deSMichal Kubecek vf_port = nla_nest_start_noflag(skb, IFLA_VF_PORT); 11278ca94183SScott Feldman if (!vf_port) 11288ca94183SScott Feldman goto nla_put_failure; 1129a6574349SDavid S. Miller if (nla_put_u32(skb, IFLA_PORT_VF, vf)) 1130a6574349SDavid S. Miller goto nla_put_failure; 113157b61080SScott Feldman err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb); 11328ca94183SScott Feldman if (err == -EMSGSIZE) 11338ca94183SScott Feldman goto nla_put_failure; 113457b61080SScott Feldman if (err) { 113557b61080SScott Feldman nla_nest_cancel(skb, vf_port); 113657b61080SScott Feldman continue; 113757b61080SScott Feldman } 113857b61080SScott Feldman nla_nest_end(skb, vf_port); 113957b61080SScott Feldman } 114057b61080SScott Feldman 114157b61080SScott Feldman nla_nest_end(skb, vf_ports); 114257b61080SScott Feldman 114357b61080SScott Feldman return 0; 11448ca94183SScott Feldman 11458ca94183SScott Feldman nla_put_failure: 11468ca94183SScott Feldman nla_nest_cancel(skb, vf_ports); 11478ca94183SScott Feldman return -EMSGSIZE; 114857b61080SScott Feldman } 114957b61080SScott Feldman 115057b61080SScott Feldman static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev) 115157b61080SScott Feldman { 115257b61080SScott Feldman struct nlattr *port_self; 115357b61080SScott Feldman int err; 115457b61080SScott Feldman 1155ae0be8deSMichal Kubecek port_self = nla_nest_start_noflag(skb, IFLA_PORT_SELF); 115657b61080SScott Feldman if (!port_self) 115757b61080SScott Feldman return -EMSGSIZE; 115857b61080SScott Feldman 115957b61080SScott Feldman err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb); 116057b61080SScott Feldman if (err) { 116157b61080SScott Feldman nla_nest_cancel(skb, port_self); 11628ca94183SScott Feldman return (err == -EMSGSIZE) ? err : 0; 116357b61080SScott Feldman } 116457b61080SScott Feldman 116557b61080SScott Feldman nla_nest_end(skb, port_self); 116657b61080SScott Feldman 116757b61080SScott Feldman return 0; 116857b61080SScott Feldman } 116957b61080SScott Feldman 1170c53864fdSDavid Gibson static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev, 1171c53864fdSDavid Gibson u32 ext_filter_mask) 117257b61080SScott Feldman { 117357b61080SScott Feldman int err; 117457b61080SScott Feldman 1175c53864fdSDavid Gibson if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent || 1176c53864fdSDavid Gibson !(ext_filter_mask & RTEXT_FILTER_VF)) 117757b61080SScott Feldman return 0; 117857b61080SScott Feldman 117957b61080SScott Feldman err = rtnl_port_self_fill(skb, dev); 118057b61080SScott Feldman if (err) 118157b61080SScott Feldman return err; 118257b61080SScott Feldman 118357b61080SScott Feldman if (dev_num_vf(dev->dev.parent)) { 118457b61080SScott Feldman err = rtnl_vf_ports_fill(skb, dev); 118557b61080SScott Feldman if (err) 118657b61080SScott Feldman return err; 118757b61080SScott Feldman } 118857b61080SScott Feldman 118957b61080SScott Feldman return 0; 119057b61080SScott Feldman } 119157b61080SScott Feldman 119266cae9edSJiri Pirko static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev) 119366cae9edSJiri Pirko { 119466cae9edSJiri Pirko int err; 119502637fceSJiri Pirko struct netdev_phys_item_id ppid; 119666cae9edSJiri Pirko 119766cae9edSJiri Pirko err = dev_get_phys_port_id(dev, &ppid); 119866cae9edSJiri Pirko if (err) { 119966cae9edSJiri Pirko if (err == -EOPNOTSUPP) 120066cae9edSJiri Pirko return 0; 120166cae9edSJiri Pirko return err; 120266cae9edSJiri Pirko } 120366cae9edSJiri Pirko 120466cae9edSJiri Pirko if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id)) 120566cae9edSJiri Pirko return -EMSGSIZE; 120666cae9edSJiri Pirko 120766cae9edSJiri Pirko return 0; 120866cae9edSJiri Pirko } 120966cae9edSJiri Pirko 1210db24a904SDavid Ahern static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev) 1211db24a904SDavid Ahern { 1212db24a904SDavid Ahern char name[IFNAMSIZ]; 1213db24a904SDavid Ahern int err; 1214db24a904SDavid Ahern 1215db24a904SDavid Ahern err = dev_get_phys_port_name(dev, name, sizeof(name)); 1216db24a904SDavid Ahern if (err) { 1217db24a904SDavid Ahern if (err == -EOPNOTSUPP) 1218db24a904SDavid Ahern return 0; 1219db24a904SDavid Ahern return err; 1220db24a904SDavid Ahern } 1221db24a904SDavid Ahern 122277ef033bSMichal Schmidt if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name)) 1223db24a904SDavid Ahern return -EMSGSIZE; 1224db24a904SDavid Ahern 1225db24a904SDavid Ahern return 0; 1226db24a904SDavid Ahern } 1227db24a904SDavid Ahern 122882f28412SJiri Pirko static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev) 122982f28412SJiri Pirko { 1230bccb3025SFlorian Fainelli struct netdev_phys_item_id ppid = { }; 123182f28412SJiri Pirko int err; 123282f28412SJiri Pirko 1233bccb3025SFlorian Fainelli err = dev_get_port_parent_id(dev, &ppid, false); 123482f28412SJiri Pirko if (err) { 123582f28412SJiri Pirko if (err == -EOPNOTSUPP) 123682f28412SJiri Pirko return 0; 123782f28412SJiri Pirko return err; 123882f28412SJiri Pirko } 123982f28412SJiri Pirko 1240bccb3025SFlorian Fainelli if (nla_put(skb, IFLA_PHYS_SWITCH_ID, ppid.id_len, ppid.id)) 124182f28412SJiri Pirko return -EMSGSIZE; 124282f28412SJiri Pirko 124382f28412SJiri Pirko return 0; 124482f28412SJiri Pirko } 124582f28412SJiri Pirko 1246b22b941bSHannes Frederic Sowa static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb, 1247b22b941bSHannes Frederic Sowa struct net_device *dev) 1248b22b941bSHannes Frederic Sowa { 1249550bce59SRoopa Prabhu struct rtnl_link_stats64 *sp; 1250b22b941bSHannes Frederic Sowa struct nlattr *attr; 1251b22b941bSHannes Frederic Sowa 125258414d32SNicolas Dichtel attr = nla_reserve_64bit(skb, IFLA_STATS64, 125358414d32SNicolas Dichtel sizeof(struct rtnl_link_stats64), IFLA_PAD); 1254b22b941bSHannes Frederic Sowa if (!attr) 1255b22b941bSHannes Frederic Sowa return -EMSGSIZE; 1256b22b941bSHannes Frederic Sowa 1257550bce59SRoopa Prabhu sp = nla_data(attr); 1258550bce59SRoopa Prabhu dev_get_stats(dev, sp); 1259550bce59SRoopa Prabhu 1260550bce59SRoopa Prabhu attr = nla_reserve(skb, IFLA_STATS, 1261550bce59SRoopa Prabhu sizeof(struct rtnl_link_stats)); 1262550bce59SRoopa Prabhu if (!attr) 1263550bce59SRoopa Prabhu return -EMSGSIZE; 1264550bce59SRoopa Prabhu 1265550bce59SRoopa Prabhu copy_rtnl_link_stats(nla_data(attr), sp); 1266b22b941bSHannes Frederic Sowa 1267b22b941bSHannes Frederic Sowa return 0; 1268b22b941bSHannes Frederic Sowa } 1269b22b941bSHannes Frederic Sowa 1270b22b941bSHannes Frederic Sowa static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb, 1271b22b941bSHannes Frederic Sowa struct net_device *dev, 1272b22b941bSHannes Frederic Sowa int vfs_num, 1273b22b941bSHannes Frederic Sowa struct nlattr *vfinfo) 1274b22b941bSHannes Frederic Sowa { 1275b22b941bSHannes Frederic Sowa struct ifla_vf_rss_query_en vf_rss_query_en; 127679aab093SMoshe Shemesh struct nlattr *vf, *vfstats, *vfvlanlist; 1277b22b941bSHannes Frederic Sowa struct ifla_vf_link_state vf_linkstate; 127879aab093SMoshe Shemesh struct ifla_vf_vlan_info vf_vlan_info; 1279b22b941bSHannes Frederic Sowa struct ifla_vf_spoofchk vf_spoofchk; 1280b22b941bSHannes Frederic Sowa struct ifla_vf_tx_rate vf_tx_rate; 1281b22b941bSHannes Frederic Sowa struct ifla_vf_stats vf_stats; 1282b22b941bSHannes Frederic Sowa struct ifla_vf_trust vf_trust; 1283b22b941bSHannes Frederic Sowa struct ifla_vf_vlan vf_vlan; 1284b22b941bSHannes Frederic Sowa struct ifla_vf_rate vf_rate; 1285b22b941bSHannes Frederic Sowa struct ifla_vf_mac vf_mac; 128675345f88SDenis Kirjanov struct ifla_vf_broadcast vf_broadcast; 1287b22b941bSHannes Frederic Sowa struct ifla_vf_info ivi; 128830aad417SDanit Goldberg struct ifla_vf_guid node_guid; 128930aad417SDanit Goldberg struct ifla_vf_guid port_guid; 1290b22b941bSHannes Frederic Sowa 12910eed9cf5SMintz, Yuval memset(&ivi, 0, sizeof(ivi)); 12920eed9cf5SMintz, Yuval 1293b22b941bSHannes Frederic Sowa /* Not all SR-IOV capable drivers support the 1294b22b941bSHannes Frederic Sowa * spoofcheck and "RSS query enable" query. Preset to 1295b22b941bSHannes Frederic Sowa * -1 so the user space tool can detect that the driver 1296b22b941bSHannes Frederic Sowa * didn't report anything. 1297b22b941bSHannes Frederic Sowa */ 1298b22b941bSHannes Frederic Sowa ivi.spoofchk = -1; 1299b22b941bSHannes Frederic Sowa ivi.rss_query_en = -1; 1300b22b941bSHannes Frederic Sowa ivi.trusted = -1; 1301b22b941bSHannes Frederic Sowa /* The default value for VF link state is "auto" 1302b22b941bSHannes Frederic Sowa * IFLA_VF_LINK_STATE_AUTO which equals zero 1303b22b941bSHannes Frederic Sowa */ 1304b22b941bSHannes Frederic Sowa ivi.linkstate = 0; 130579aab093SMoshe Shemesh /* VLAN Protocol by default is 802.1Q */ 130679aab093SMoshe Shemesh ivi.vlan_proto = htons(ETH_P_8021Q); 1307b22b941bSHannes Frederic Sowa if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi)) 1308b22b941bSHannes Frederic Sowa return 0; 1309b22b941bSHannes Frederic Sowa 1310775f4f05SDan Carpenter memset(&vf_vlan_info, 0, sizeof(vf_vlan_info)); 13119fbf082fSLeon Romanovsky memset(&node_guid, 0, sizeof(node_guid)); 13129fbf082fSLeon Romanovsky memset(&port_guid, 0, sizeof(port_guid)); 1313775f4f05SDan Carpenter 1314b22b941bSHannes Frederic Sowa vf_mac.vf = 1315b22b941bSHannes Frederic Sowa vf_vlan.vf = 131679aab093SMoshe Shemesh vf_vlan_info.vf = 1317b22b941bSHannes Frederic Sowa vf_rate.vf = 1318b22b941bSHannes Frederic Sowa vf_tx_rate.vf = 1319b22b941bSHannes Frederic Sowa vf_spoofchk.vf = 1320b22b941bSHannes Frederic Sowa vf_linkstate.vf = 1321b22b941bSHannes Frederic Sowa vf_rss_query_en.vf = 13229aed6ae0SDanit Goldberg vf_trust.vf = 13239aed6ae0SDanit Goldberg node_guid.vf = 13249aed6ae0SDanit Goldberg port_guid.vf = ivi.vf; 1325b22b941bSHannes Frederic Sowa 1326b22b941bSHannes Frederic Sowa memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac)); 132775345f88SDenis Kirjanov memcpy(vf_broadcast.broadcast, dev->broadcast, dev->addr_len); 1328b22b941bSHannes Frederic Sowa vf_vlan.vlan = ivi.vlan; 1329b22b941bSHannes Frederic Sowa vf_vlan.qos = ivi.qos; 133079aab093SMoshe Shemesh vf_vlan_info.vlan = ivi.vlan; 133179aab093SMoshe Shemesh vf_vlan_info.qos = ivi.qos; 133279aab093SMoshe Shemesh vf_vlan_info.vlan_proto = ivi.vlan_proto; 1333b22b941bSHannes Frederic Sowa vf_tx_rate.rate = ivi.max_tx_rate; 1334b22b941bSHannes Frederic Sowa vf_rate.min_tx_rate = ivi.min_tx_rate; 1335b22b941bSHannes Frederic Sowa vf_rate.max_tx_rate = ivi.max_tx_rate; 1336b22b941bSHannes Frederic Sowa vf_spoofchk.setting = ivi.spoofchk; 1337b22b941bSHannes Frederic Sowa vf_linkstate.link_state = ivi.linkstate; 1338b22b941bSHannes Frederic Sowa vf_rss_query_en.setting = ivi.rss_query_en; 1339b22b941bSHannes Frederic Sowa vf_trust.setting = ivi.trusted; 1340ae0be8deSMichal Kubecek vf = nla_nest_start_noflag(skb, IFLA_VF_INFO); 134179aab093SMoshe Shemesh if (!vf) 134279aab093SMoshe Shemesh goto nla_put_vfinfo_failure; 1343b22b941bSHannes Frederic Sowa if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) || 134475345f88SDenis Kirjanov nla_put(skb, IFLA_VF_BROADCAST, sizeof(vf_broadcast), &vf_broadcast) || 1345b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) || 1346b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate), 1347b22b941bSHannes Frederic Sowa &vf_rate) || 1348b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate), 1349b22b941bSHannes Frederic Sowa &vf_tx_rate) || 1350b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk), 1351b22b941bSHannes Frederic Sowa &vf_spoofchk) || 1352b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate), 1353b22b941bSHannes Frederic Sowa &vf_linkstate) || 1354b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_RSS_QUERY_EN, 1355b22b941bSHannes Frederic Sowa sizeof(vf_rss_query_en), 1356b22b941bSHannes Frederic Sowa &vf_rss_query_en) || 1357b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_TRUST, 1358b22b941bSHannes Frederic Sowa sizeof(vf_trust), &vf_trust)) 135979aab093SMoshe Shemesh goto nla_put_vf_failure; 136030aad417SDanit Goldberg 136130aad417SDanit Goldberg if (dev->netdev_ops->ndo_get_vf_guid && 136230aad417SDanit Goldberg !dev->netdev_ops->ndo_get_vf_guid(dev, vfs_num, &node_guid, 136330aad417SDanit Goldberg &port_guid)) { 136430aad417SDanit Goldberg if (nla_put(skb, IFLA_VF_IB_NODE_GUID, sizeof(node_guid), 136530aad417SDanit Goldberg &node_guid) || 136630aad417SDanit Goldberg nla_put(skb, IFLA_VF_IB_PORT_GUID, sizeof(port_guid), 136730aad417SDanit Goldberg &port_guid)) 136830aad417SDanit Goldberg goto nla_put_vf_failure; 136930aad417SDanit Goldberg } 1370ae0be8deSMichal Kubecek vfvlanlist = nla_nest_start_noflag(skb, IFLA_VF_VLAN_LIST); 137179aab093SMoshe Shemesh if (!vfvlanlist) 137279aab093SMoshe Shemesh goto nla_put_vf_failure; 137379aab093SMoshe Shemesh if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info), 137479aab093SMoshe Shemesh &vf_vlan_info)) { 137579aab093SMoshe Shemesh nla_nest_cancel(skb, vfvlanlist); 137679aab093SMoshe Shemesh goto nla_put_vf_failure; 137779aab093SMoshe Shemesh } 137879aab093SMoshe Shemesh nla_nest_end(skb, vfvlanlist); 1379b22b941bSHannes Frederic Sowa memset(&vf_stats, 0, sizeof(vf_stats)); 1380b22b941bSHannes Frederic Sowa if (dev->netdev_ops->ndo_get_vf_stats) 1381b22b941bSHannes Frederic Sowa dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num, 1382b22b941bSHannes Frederic Sowa &vf_stats); 1383ae0be8deSMichal Kubecek vfstats = nla_nest_start_noflag(skb, IFLA_VF_STATS); 138479aab093SMoshe Shemesh if (!vfstats) 138579aab093SMoshe Shemesh goto nla_put_vf_failure; 1386343a6d8eSNicolas Dichtel if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS, 1387343a6d8eSNicolas Dichtel vf_stats.rx_packets, IFLA_VF_STATS_PAD) || 1388343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS, 1389343a6d8eSNicolas Dichtel vf_stats.tx_packets, IFLA_VF_STATS_PAD) || 1390343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES, 1391343a6d8eSNicolas Dichtel vf_stats.rx_bytes, IFLA_VF_STATS_PAD) || 1392343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES, 1393343a6d8eSNicolas Dichtel vf_stats.tx_bytes, IFLA_VF_STATS_PAD) || 1394343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST, 1395343a6d8eSNicolas Dichtel vf_stats.broadcast, IFLA_VF_STATS_PAD) || 1396343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST, 1397c5a9f6f0SEugenia Emantayev vf_stats.multicast, IFLA_VF_STATS_PAD) || 1398c5a9f6f0SEugenia Emantayev nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED, 1399c5a9f6f0SEugenia Emantayev vf_stats.rx_dropped, IFLA_VF_STATS_PAD) || 1400c5a9f6f0SEugenia Emantayev nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED, 1401c5a9f6f0SEugenia Emantayev vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) { 140279aab093SMoshe Shemesh nla_nest_cancel(skb, vfstats); 140379aab093SMoshe Shemesh goto nla_put_vf_failure; 140479aab093SMoshe Shemesh } 1405b22b941bSHannes Frederic Sowa nla_nest_end(skb, vfstats); 1406b22b941bSHannes Frederic Sowa nla_nest_end(skb, vf); 1407b22b941bSHannes Frederic Sowa return 0; 140879aab093SMoshe Shemesh 140979aab093SMoshe Shemesh nla_put_vf_failure: 141079aab093SMoshe Shemesh nla_nest_cancel(skb, vf); 141179aab093SMoshe Shemesh nla_put_vfinfo_failure: 141279aab093SMoshe Shemesh nla_nest_cancel(skb, vfinfo); 141379aab093SMoshe Shemesh return -EMSGSIZE; 1414b22b941bSHannes Frederic Sowa } 1415b22b941bSHannes Frederic Sowa 1416250fc3dfSFlorian Westphal static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb, 1417250fc3dfSFlorian Westphal struct net_device *dev, 1418250fc3dfSFlorian Westphal u32 ext_filter_mask) 1419250fc3dfSFlorian Westphal { 1420250fc3dfSFlorian Westphal struct nlattr *vfinfo; 1421250fc3dfSFlorian Westphal int i, num_vfs; 1422250fc3dfSFlorian Westphal 1423250fc3dfSFlorian Westphal if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0)) 1424250fc3dfSFlorian Westphal return 0; 1425250fc3dfSFlorian Westphal 1426250fc3dfSFlorian Westphal num_vfs = dev_num_vf(dev->dev.parent); 1427250fc3dfSFlorian Westphal if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs)) 1428250fc3dfSFlorian Westphal return -EMSGSIZE; 1429250fc3dfSFlorian Westphal 1430250fc3dfSFlorian Westphal if (!dev->netdev_ops->ndo_get_vf_config) 1431250fc3dfSFlorian Westphal return 0; 1432250fc3dfSFlorian Westphal 1433ae0be8deSMichal Kubecek vfinfo = nla_nest_start_noflag(skb, IFLA_VFINFO_LIST); 1434250fc3dfSFlorian Westphal if (!vfinfo) 1435250fc3dfSFlorian Westphal return -EMSGSIZE; 1436250fc3dfSFlorian Westphal 1437250fc3dfSFlorian Westphal for (i = 0; i < num_vfs; i++) { 1438250fc3dfSFlorian Westphal if (rtnl_fill_vfinfo(skb, dev, i, vfinfo)) 1439250fc3dfSFlorian Westphal return -EMSGSIZE; 1440250fc3dfSFlorian Westphal } 1441250fc3dfSFlorian Westphal 1442250fc3dfSFlorian Westphal nla_nest_end(skb, vfinfo); 1443250fc3dfSFlorian Westphal return 0; 1444250fc3dfSFlorian Westphal } 1445250fc3dfSFlorian Westphal 1446b22b941bSHannes Frederic Sowa static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev) 1447b22b941bSHannes Frederic Sowa { 14485f8e4474SKangjie Lu struct rtnl_link_ifmap map; 14495f8e4474SKangjie Lu 14505f8e4474SKangjie Lu memset(&map, 0, sizeof(map)); 14515f8e4474SKangjie Lu map.mem_start = dev->mem_start; 14525f8e4474SKangjie Lu map.mem_end = dev->mem_end; 14535f8e4474SKangjie Lu map.base_addr = dev->base_addr; 14545f8e4474SKangjie Lu map.irq = dev->irq; 14555f8e4474SKangjie Lu map.dma = dev->dma; 14565f8e4474SKangjie Lu map.port = dev->if_port; 14575f8e4474SKangjie Lu 1458270cb4d0SNicolas Dichtel if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD)) 1459b22b941bSHannes Frederic Sowa return -EMSGSIZE; 1460b22b941bSHannes Frederic Sowa 1461b22b941bSHannes Frederic Sowa return 0; 1462b22b941bSHannes Frederic Sowa } 1463b22b941bSHannes Frederic Sowa 1464a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_skb(struct net_device *dev) 1465d67b9cd2SDaniel Borkmann { 146658038695SMartin KaFai Lau const struct bpf_prog *generic_xdp_prog; 1467d67b9cd2SDaniel Borkmann 1468d67b9cd2SDaniel Borkmann ASSERT_RTNL(); 1469d67b9cd2SDaniel Borkmann 147058038695SMartin KaFai Lau generic_xdp_prog = rtnl_dereference(dev->xdp_prog); 1471a25717d2SJakub Kicinski if (!generic_xdp_prog) 1472a25717d2SJakub Kicinski return 0; 1473a25717d2SJakub Kicinski return generic_xdp_prog->aux->id; 147458038695SMartin KaFai Lau } 1475ce158e58SJakub Kicinski 1476a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_drv(struct net_device *dev) 1477a25717d2SJakub Kicinski { 14787f0a8382SAndrii Nakryiko return dev_xdp_prog_id(dev, XDP_MODE_DRV); 1479a25717d2SJakub Kicinski } 1480118b4aa2SJakub Kicinski 1481a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_hw(struct net_device *dev) 1482a25717d2SJakub Kicinski { 14837f0a8382SAndrii Nakryiko return dev_xdp_prog_id(dev, XDP_MODE_HW); 1484a25717d2SJakub Kicinski } 1485a25717d2SJakub Kicinski 1486a25717d2SJakub Kicinski static int rtnl_xdp_report_one(struct sk_buff *skb, struct net_device *dev, 1487a25717d2SJakub Kicinski u32 *prog_id, u8 *mode, u8 tgt_mode, u32 attr, 1488a25717d2SJakub Kicinski u32 (*get_prog_id)(struct net_device *dev)) 1489a25717d2SJakub Kicinski { 1490a25717d2SJakub Kicinski u32 curr_id; 1491a25717d2SJakub Kicinski int err; 1492a25717d2SJakub Kicinski 1493a25717d2SJakub Kicinski curr_id = get_prog_id(dev); 1494a25717d2SJakub Kicinski if (!curr_id) 1495a25717d2SJakub Kicinski return 0; 1496a25717d2SJakub Kicinski 1497a25717d2SJakub Kicinski *prog_id = curr_id; 1498a25717d2SJakub Kicinski err = nla_put_u32(skb, attr, curr_id); 1499a25717d2SJakub Kicinski if (err) 1500a25717d2SJakub Kicinski return err; 1501a25717d2SJakub Kicinski 1502a25717d2SJakub Kicinski if (*mode != XDP_ATTACHED_NONE) 1503a25717d2SJakub Kicinski *mode = XDP_ATTACHED_MULTI; 1504a25717d2SJakub Kicinski else 1505a25717d2SJakub Kicinski *mode = tgt_mode; 1506a25717d2SJakub Kicinski 1507a25717d2SJakub Kicinski return 0; 1508d67b9cd2SDaniel Borkmann } 1509d67b9cd2SDaniel Borkmann 1510d1fdd913SBrenden Blanco static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev) 1511d1fdd913SBrenden Blanco { 1512d1fdd913SBrenden Blanco struct nlattr *xdp; 151358038695SMartin KaFai Lau u32 prog_id; 1514d1fdd913SBrenden Blanco int err; 15154f91da26SJakub Kicinski u8 mode; 1516d1fdd913SBrenden Blanco 1517ae0be8deSMichal Kubecek xdp = nla_nest_start_noflag(skb, IFLA_XDP); 1518d1fdd913SBrenden Blanco if (!xdp) 1519d1fdd913SBrenden Blanco return -EMSGSIZE; 1520b5cdae32SDavid S. Miller 1521a25717d2SJakub Kicinski prog_id = 0; 1522a25717d2SJakub Kicinski mode = XDP_ATTACHED_NONE; 1523202aabe8SJakub Kicinski err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_SKB, 1524202aabe8SJakub Kicinski IFLA_XDP_SKB_PROG_ID, rtnl_xdp_prog_skb); 1525202aabe8SJakub Kicinski if (err) 1526a25717d2SJakub Kicinski goto err_cancel; 1527202aabe8SJakub Kicinski err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_DRV, 1528202aabe8SJakub Kicinski IFLA_XDP_DRV_PROG_ID, rtnl_xdp_prog_drv); 1529202aabe8SJakub Kicinski if (err) 1530a25717d2SJakub Kicinski goto err_cancel; 1531202aabe8SJakub Kicinski err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_HW, 1532202aabe8SJakub Kicinski IFLA_XDP_HW_PROG_ID, rtnl_xdp_prog_hw); 1533202aabe8SJakub Kicinski if (err) 1534a25717d2SJakub Kicinski goto err_cancel; 1535a25717d2SJakub Kicinski 15364f91da26SJakub Kicinski err = nla_put_u8(skb, IFLA_XDP_ATTACHED, mode); 1537d1fdd913SBrenden Blanco if (err) 1538d1fdd913SBrenden Blanco goto err_cancel; 1539d1fdd913SBrenden Blanco 1540a25717d2SJakub Kicinski if (prog_id && mode != XDP_ATTACHED_MULTI) { 154158038695SMartin KaFai Lau err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id); 154258038695SMartin KaFai Lau if (err) 154358038695SMartin KaFai Lau goto err_cancel; 154458038695SMartin KaFai Lau } 154558038695SMartin KaFai Lau 1546d1fdd913SBrenden Blanco nla_nest_end(skb, xdp); 1547d1fdd913SBrenden Blanco return 0; 1548d1fdd913SBrenden Blanco 1549d1fdd913SBrenden Blanco err_cancel: 1550d1fdd913SBrenden Blanco nla_nest_cancel(skb, xdp); 1551d1fdd913SBrenden Blanco return err; 1552d1fdd913SBrenden Blanco } 1553d1fdd913SBrenden Blanco 15543d3ea5afSVlad Yasevich static u32 rtnl_get_event(unsigned long event) 15553d3ea5afSVlad Yasevich { 15563d3ea5afSVlad Yasevich u32 rtnl_event_type = IFLA_EVENT_NONE; 15573d3ea5afSVlad Yasevich 15583d3ea5afSVlad Yasevich switch (event) { 15593d3ea5afSVlad Yasevich case NETDEV_REBOOT: 15603d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_REBOOT; 15613d3ea5afSVlad Yasevich break; 15623d3ea5afSVlad Yasevich case NETDEV_FEAT_CHANGE: 15633d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_FEATURES; 15643d3ea5afSVlad Yasevich break; 15653d3ea5afSVlad Yasevich case NETDEV_BONDING_FAILOVER: 15663d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER; 15673d3ea5afSVlad Yasevich break; 15683d3ea5afSVlad Yasevich case NETDEV_NOTIFY_PEERS: 15693d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS; 15703d3ea5afSVlad Yasevich break; 15713d3ea5afSVlad Yasevich case NETDEV_RESEND_IGMP: 15723d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_IGMP_RESEND; 15733d3ea5afSVlad Yasevich break; 15743d3ea5afSVlad Yasevich case NETDEV_CHANGEINFODATA: 15753d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS; 15763d3ea5afSVlad Yasevich break; 15773d3ea5afSVlad Yasevich default: 15783d3ea5afSVlad Yasevich break; 15793d3ea5afSVlad Yasevich } 15803d3ea5afSVlad Yasevich 15813d3ea5afSVlad Yasevich return rtnl_event_type; 15823d3ea5afSVlad Yasevich } 15833d3ea5afSVlad Yasevich 158479110a04SFlorian Westphal static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev) 158579110a04SFlorian Westphal { 158679110a04SFlorian Westphal const struct net_device *upper_dev; 158779110a04SFlorian Westphal int ret = 0; 158879110a04SFlorian Westphal 158979110a04SFlorian Westphal rcu_read_lock(); 159079110a04SFlorian Westphal 159179110a04SFlorian Westphal upper_dev = netdev_master_upper_dev_get_rcu(dev); 159279110a04SFlorian Westphal if (upper_dev) 159379110a04SFlorian Westphal ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex); 159479110a04SFlorian Westphal 159579110a04SFlorian Westphal rcu_read_unlock(); 159679110a04SFlorian Westphal return ret; 159779110a04SFlorian Westphal } 159879110a04SFlorian Westphal 1599feadc4b6SSabrina Dubroca static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev, 1600feadc4b6SSabrina Dubroca bool force) 160179110a04SFlorian Westphal { 160279110a04SFlorian Westphal int ifindex = dev_get_iflink(dev); 160379110a04SFlorian Westphal 1604feadc4b6SSabrina Dubroca if (force || dev->ifindex != ifindex) 160579110a04SFlorian Westphal return nla_put_u32(skb, IFLA_LINK, ifindex); 1606feadc4b6SSabrina Dubroca 1607feadc4b6SSabrina Dubroca return 0; 160879110a04SFlorian Westphal } 160979110a04SFlorian Westphal 16106c557001SFlorian Westphal static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb, 16116c557001SFlorian Westphal struct net_device *dev) 16126c557001SFlorian Westphal { 16136c557001SFlorian Westphal char buf[IFALIASZ]; 16146c557001SFlorian Westphal int ret; 16156c557001SFlorian Westphal 16166c557001SFlorian Westphal ret = dev_get_alias(dev, buf, sizeof(buf)); 16176c557001SFlorian Westphal return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0; 16186c557001SFlorian Westphal } 16196c557001SFlorian Westphal 1620b1e66b9aSFlorian Westphal static int rtnl_fill_link_netnsid(struct sk_buff *skb, 162179e1ad14SJiri Benc const struct net_device *dev, 1622d4e4fdf9SGuillaume Nault struct net *src_net, gfp_t gfp) 1623b1e66b9aSFlorian Westphal { 1624feadc4b6SSabrina Dubroca bool put_iflink = false; 1625feadc4b6SSabrina Dubroca 1626b1e66b9aSFlorian Westphal if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) { 1627b1e66b9aSFlorian Westphal struct net *link_net = dev->rtnl_link_ops->get_link_net(dev); 1628b1e66b9aSFlorian Westphal 1629b1e66b9aSFlorian Westphal if (!net_eq(dev_net(dev), link_net)) { 1630d4e4fdf9SGuillaume Nault int id = peernet2id_alloc(src_net, link_net, gfp); 1631b1e66b9aSFlorian Westphal 1632b1e66b9aSFlorian Westphal if (nla_put_s32(skb, IFLA_LINK_NETNSID, id)) 1633b1e66b9aSFlorian Westphal return -EMSGSIZE; 1634feadc4b6SSabrina Dubroca 1635feadc4b6SSabrina Dubroca put_iflink = true; 1636b1e66b9aSFlorian Westphal } 1637b1e66b9aSFlorian Westphal } 1638b1e66b9aSFlorian Westphal 1639feadc4b6SSabrina Dubroca return nla_put_iflink(skb, dev, put_iflink); 1640b1e66b9aSFlorian Westphal } 1641b1e66b9aSFlorian Westphal 1642070cbf5bSFlorian Westphal static int rtnl_fill_link_af(struct sk_buff *skb, 1643070cbf5bSFlorian Westphal const struct net_device *dev, 1644070cbf5bSFlorian Westphal u32 ext_filter_mask) 1645070cbf5bSFlorian Westphal { 1646070cbf5bSFlorian Westphal const struct rtnl_af_ops *af_ops; 1647070cbf5bSFlorian Westphal struct nlattr *af_spec; 1648070cbf5bSFlorian Westphal 1649ae0be8deSMichal Kubecek af_spec = nla_nest_start_noflag(skb, IFLA_AF_SPEC); 1650070cbf5bSFlorian Westphal if (!af_spec) 1651070cbf5bSFlorian Westphal return -EMSGSIZE; 1652070cbf5bSFlorian Westphal 16535fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 1654070cbf5bSFlorian Westphal struct nlattr *af; 1655070cbf5bSFlorian Westphal int err; 1656070cbf5bSFlorian Westphal 1657070cbf5bSFlorian Westphal if (!af_ops->fill_link_af) 1658070cbf5bSFlorian Westphal continue; 1659070cbf5bSFlorian Westphal 1660ae0be8deSMichal Kubecek af = nla_nest_start_noflag(skb, af_ops->family); 1661070cbf5bSFlorian Westphal if (!af) 1662070cbf5bSFlorian Westphal return -EMSGSIZE; 1663070cbf5bSFlorian Westphal 1664070cbf5bSFlorian Westphal err = af_ops->fill_link_af(skb, dev, ext_filter_mask); 1665070cbf5bSFlorian Westphal /* 1666070cbf5bSFlorian Westphal * Caller may return ENODATA to indicate that there 1667070cbf5bSFlorian Westphal * was no data to be dumped. This is not an error, it 1668070cbf5bSFlorian Westphal * means we should trim the attribute header and 1669070cbf5bSFlorian Westphal * continue. 1670070cbf5bSFlorian Westphal */ 1671070cbf5bSFlorian Westphal if (err == -ENODATA) 1672070cbf5bSFlorian Westphal nla_nest_cancel(skb, af); 1673070cbf5bSFlorian Westphal else if (err < 0) 1674070cbf5bSFlorian Westphal return -EMSGSIZE; 1675070cbf5bSFlorian Westphal 1676070cbf5bSFlorian Westphal nla_nest_end(skb, af); 1677070cbf5bSFlorian Westphal } 1678070cbf5bSFlorian Westphal 1679070cbf5bSFlorian Westphal nla_nest_end(skb, af_spec); 1680070cbf5bSFlorian Westphal return 0; 1681070cbf5bSFlorian Westphal } 1682070cbf5bSFlorian Westphal 168388f4fb0cSJiri Pirko static int rtnl_fill_alt_ifnames(struct sk_buff *skb, 168488f4fb0cSJiri Pirko const struct net_device *dev) 168588f4fb0cSJiri Pirko { 168688f4fb0cSJiri Pirko struct netdev_name_node *name_node; 168788f4fb0cSJiri Pirko int count = 0; 168888f4fb0cSJiri Pirko 168988f4fb0cSJiri Pirko list_for_each_entry(name_node, &dev->name_node->list, list) { 169088f4fb0cSJiri Pirko if (nla_put_string(skb, IFLA_ALT_IFNAME, name_node->name)) 169188f4fb0cSJiri Pirko return -EMSGSIZE; 169288f4fb0cSJiri Pirko count++; 169388f4fb0cSJiri Pirko } 169488f4fb0cSJiri Pirko return count; 169588f4fb0cSJiri Pirko } 169688f4fb0cSJiri Pirko 169788f4fb0cSJiri Pirko static int rtnl_fill_prop_list(struct sk_buff *skb, 169888f4fb0cSJiri Pirko const struct net_device *dev) 169988f4fb0cSJiri Pirko { 170088f4fb0cSJiri Pirko struct nlattr *prop_list; 170188f4fb0cSJiri Pirko int ret; 170288f4fb0cSJiri Pirko 170388f4fb0cSJiri Pirko prop_list = nla_nest_start(skb, IFLA_PROP_LIST); 170488f4fb0cSJiri Pirko if (!prop_list) 170588f4fb0cSJiri Pirko return -EMSGSIZE; 170688f4fb0cSJiri Pirko 170788f4fb0cSJiri Pirko ret = rtnl_fill_alt_ifnames(skb, dev); 170888f4fb0cSJiri Pirko if (ret <= 0) 170988f4fb0cSJiri Pirko goto nest_cancel; 171088f4fb0cSJiri Pirko 171188f4fb0cSJiri Pirko nla_nest_end(skb, prop_list); 171288f4fb0cSJiri Pirko return 0; 171388f4fb0cSJiri Pirko 171488f4fb0cSJiri Pirko nest_cancel: 171588f4fb0cSJiri Pirko nla_nest_cancel(skb, prop_list); 171688f4fb0cSJiri Pirko return ret; 171788f4fb0cSJiri Pirko } 171888f4fb0cSJiri Pirko 1719829eb208SRoopa Prabhu static int rtnl_fill_proto_down(struct sk_buff *skb, 1720829eb208SRoopa Prabhu const struct net_device *dev) 1721829eb208SRoopa Prabhu { 1722829eb208SRoopa Prabhu struct nlattr *pr; 1723829eb208SRoopa Prabhu u32 preason; 1724829eb208SRoopa Prabhu 1725829eb208SRoopa Prabhu if (nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down)) 1726829eb208SRoopa Prabhu goto nla_put_failure; 1727829eb208SRoopa Prabhu 1728829eb208SRoopa Prabhu preason = dev->proto_down_reason; 1729829eb208SRoopa Prabhu if (!preason) 1730829eb208SRoopa Prabhu return 0; 1731829eb208SRoopa Prabhu 1732829eb208SRoopa Prabhu pr = nla_nest_start(skb, IFLA_PROTO_DOWN_REASON); 1733829eb208SRoopa Prabhu if (!pr) 1734829eb208SRoopa Prabhu return -EMSGSIZE; 1735829eb208SRoopa Prabhu 1736829eb208SRoopa Prabhu if (nla_put_u32(skb, IFLA_PROTO_DOWN_REASON_VALUE, preason)) { 1737829eb208SRoopa Prabhu nla_nest_cancel(skb, pr); 1738829eb208SRoopa Prabhu goto nla_put_failure; 1739829eb208SRoopa Prabhu } 1740829eb208SRoopa Prabhu 1741829eb208SRoopa Prabhu nla_nest_end(skb, pr); 1742829eb208SRoopa Prabhu return 0; 1743829eb208SRoopa Prabhu 1744829eb208SRoopa Prabhu nla_put_failure: 1745829eb208SRoopa Prabhu return -EMSGSIZE; 1746829eb208SRoopa Prabhu } 1747829eb208SRoopa Prabhu 1748dca56c30SJiri Pirko static int rtnl_fill_devlink_port(struct sk_buff *skb, 1749dca56c30SJiri Pirko const struct net_device *dev) 1750dca56c30SJiri Pirko { 1751dca56c30SJiri Pirko struct nlattr *devlink_port_nest; 1752dca56c30SJiri Pirko int ret; 1753dca56c30SJiri Pirko 1754dca56c30SJiri Pirko devlink_port_nest = nla_nest_start(skb, IFLA_DEVLINK_PORT); 1755dca56c30SJiri Pirko if (!devlink_port_nest) 1756dca56c30SJiri Pirko return -EMSGSIZE; 1757dca56c30SJiri Pirko 1758dca56c30SJiri Pirko if (dev->devlink_port) { 1759dca56c30SJiri Pirko ret = devlink_nl_port_handle_fill(skb, dev->devlink_port); 1760dca56c30SJiri Pirko if (ret < 0) 1761dca56c30SJiri Pirko goto nest_cancel; 1762dca56c30SJiri Pirko } 1763dca56c30SJiri Pirko 1764dca56c30SJiri Pirko nla_nest_end(skb, devlink_port_nest); 1765dca56c30SJiri Pirko return 0; 1766dca56c30SJiri Pirko 1767dca56c30SJiri Pirko nest_cancel: 1768dca56c30SJiri Pirko nla_nest_cancel(skb, devlink_port_nest); 1769dca56c30SJiri Pirko return ret; 1770dca56c30SJiri Pirko } 1771dca56c30SJiri Pirko 177279e1ad14SJiri Benc static int rtnl_fill_ifinfo(struct sk_buff *skb, 177379e1ad14SJiri Benc struct net_device *dev, struct net *src_net, 1774575c3e2aSPatrick McHardy int type, u32 pid, u32 seq, u32 change, 17753d3ea5afSVlad Yasevich unsigned int flags, u32 ext_filter_mask, 177638e01b30SNicolas Dichtel u32 event, int *new_nsid, int new_ifindex, 1777d4e4fdf9SGuillaume Nault int tgt_netnsid, gfp_t gfp) 1778b60c5115SThomas Graf { 1779b60c5115SThomas Graf struct ifinfomsg *ifm; 17801da177e4SLinus Torvalds struct nlmsghdr *nlh; 17815891cd5eSEric Dumazet struct Qdisc *qdisc; 17821da177e4SLinus Torvalds 17832907c35fSEric Dumazet ASSERT_RTNL(); 1784b60c5115SThomas Graf nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags); 1785b60c5115SThomas Graf if (nlh == NULL) 178626932566SPatrick McHardy return -EMSGSIZE; 17871da177e4SLinus Torvalds 1788b60c5115SThomas Graf ifm = nlmsg_data(nlh); 1789b60c5115SThomas Graf ifm->ifi_family = AF_UNSPEC; 1790b60c5115SThomas Graf ifm->__ifi_pad = 0; 1791b60c5115SThomas Graf ifm->ifi_type = dev->type; 1792b60c5115SThomas Graf ifm->ifi_index = dev->ifindex; 1793b60c5115SThomas Graf ifm->ifi_flags = dev_get_flags(dev); 1794b60c5115SThomas Graf ifm->ifi_change = change; 17951da177e4SLinus Torvalds 17967e4a8d5aSChristian Brauner if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_TARGET_NETNSID, tgt_netnsid)) 179779e1ad14SJiri Benc goto nla_put_failure; 179879e1ad14SJiri Benc 17995891cd5eSEric Dumazet qdisc = rtnl_dereference(dev->qdisc); 1800a6574349SDavid S. Miller if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 1801a6574349SDavid S. Miller nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) || 1802a6574349SDavid S. Miller nla_put_u8(skb, IFLA_OPERSTATE, 1803a6574349SDavid S. Miller netif_running(dev) ? dev->operstate : IF_OPER_DOWN) || 1804a6574349SDavid S. Miller nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) || 1805a6574349SDavid S. Miller nla_put_u32(skb, IFLA_MTU, dev->mtu) || 18063e7a50ceSStephen Hemminger nla_put_u32(skb, IFLA_MIN_MTU, dev->min_mtu) || 18073e7a50ceSStephen Hemminger nla_put_u32(skb, IFLA_MAX_MTU, dev->max_mtu) || 1808a6574349SDavid S. Miller nla_put_u32(skb, IFLA_GROUP, dev->group) || 1809edbc0bb3SBen Greear nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) || 18107e6e1b57SNicolas Dichtel nla_put_u32(skb, IFLA_ALLMULTI, dev->allmulti) || 181176ff5cc9SJiri Pirko nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) || 1812c70ce028SEric Dumazet nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) || 1813c70ce028SEric Dumazet nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) || 1814eac1b93cSCoco Li nla_put_u32(skb, IFLA_GRO_MAX_SIZE, dev->gro_max_size) || 18159eefedd5SXin Long nla_put_u32(skb, IFLA_GSO_IPV4_MAX_SIZE, dev->gso_ipv4_max_size) || 18169eefedd5SXin Long nla_put_u32(skb, IFLA_GRO_IPV4_MAX_SIZE, dev->gro_ipv4_max_size) || 181789527be8SEric Dumazet nla_put_u32(skb, IFLA_TSO_MAX_SIZE, dev->tso_max_size) || 181889527be8SEric Dumazet nla_put_u32(skb, IFLA_TSO_MAX_SEGS, dev->tso_max_segs) || 18191d69c2b3SMark A. Greer #ifdef CONFIG_RPS 182076ff5cc9SJiri Pirko nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) || 18211d69c2b3SMark A. Greer #endif 182279110a04SFlorian Westphal put_master_ifindex(skb, dev) || 18239a57247fSJiri Pirko nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) || 18245891cd5eSEric Dumazet (qdisc && 18255891cd5eSEric Dumazet nla_put_string(skb, IFLA_QDISC, qdisc->ops->id)) || 18266c557001SFlorian Westphal nla_put_ifalias(skb, dev) || 18272d3b479dSdavid decotigny nla_put_u32(skb, IFLA_CARRIER_CHANGES, 1828b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_up_count) + 1829b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_down_count)) || 1830b2d3bcfaSDavid Decotigny nla_put_u32(skb, IFLA_CARRIER_UP_COUNT, 1831b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_up_count)) || 1832b2d3bcfaSDavid Decotigny nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT, 1833b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_down_count))) 1834a6574349SDavid S. Miller goto nla_put_failure; 18350b815a1aSStephen Hemminger 1836829eb208SRoopa Prabhu if (rtnl_fill_proto_down(skb, dev)) 1837829eb208SRoopa Prabhu goto nla_put_failure; 1838829eb208SRoopa Prabhu 18393d3ea5afSVlad Yasevich if (event != IFLA_EVENT_NONE) { 18403d3ea5afSVlad Yasevich if (nla_put_u32(skb, IFLA_EVENT, event)) 18413d3ea5afSVlad Yasevich goto nla_put_failure; 18423d3ea5afSVlad Yasevich } 18433d3ea5afSVlad Yasevich 1844b22b941bSHannes Frederic Sowa if (rtnl_fill_link_ifmap(skb, dev)) 1845a6574349SDavid S. Miller goto nla_put_failure; 18461da177e4SLinus Torvalds 18471da177e4SLinus Torvalds if (dev->addr_len) { 1848a6574349SDavid S. Miller if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) || 1849a6574349SDavid S. Miller nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast)) 1850a6574349SDavid S. Miller goto nla_put_failure; 18511da177e4SLinus Torvalds } 18521da177e4SLinus Torvalds 185366cae9edSJiri Pirko if (rtnl_phys_port_id_fill(skb, dev)) 185466cae9edSJiri Pirko goto nla_put_failure; 185566cae9edSJiri Pirko 1856db24a904SDavid Ahern if (rtnl_phys_port_name_fill(skb, dev)) 1857db24a904SDavid Ahern goto nla_put_failure; 1858db24a904SDavid Ahern 185982f28412SJiri Pirko if (rtnl_phys_switch_id_fill(skb, dev)) 186082f28412SJiri Pirko goto nla_put_failure; 186182f28412SJiri Pirko 1862b22b941bSHannes Frederic Sowa if (rtnl_fill_stats(skb, dev)) 1863b60c5115SThomas Graf goto nla_put_failure; 1864b60c5115SThomas Graf 1865250fc3dfSFlorian Westphal if (rtnl_fill_vf(skb, dev, ext_filter_mask)) 1866a6574349SDavid S. Miller goto nla_put_failure; 186757b61080SScott Feldman 1868c53864fdSDavid Gibson if (rtnl_port_fill(skb, dev, ext_filter_mask)) 186957b61080SScott Feldman goto nla_put_failure; 187057b61080SScott Feldman 1871d1fdd913SBrenden Blanco if (rtnl_xdp_fill(skb, dev)) 1872d1fdd913SBrenden Blanco goto nla_put_failure; 1873d1fdd913SBrenden Blanco 1874ba7d49b1SJiri Pirko if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) { 187538f7b870SPatrick McHardy if (rtnl_link_fill(skb, dev) < 0) 187638f7b870SPatrick McHardy goto nla_put_failure; 187738f7b870SPatrick McHardy } 187838f7b870SPatrick McHardy 1879d4e4fdf9SGuillaume Nault if (rtnl_fill_link_netnsid(skb, dev, src_net, gfp)) 1880d37512a2SNicolas Dichtel goto nla_put_failure; 1881d37512a2SNicolas Dichtel 18826621dd29SNicolas Dichtel if (new_nsid && 18836621dd29SNicolas Dichtel nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0) 18846621dd29SNicolas Dichtel goto nla_put_failure; 188538e01b30SNicolas Dichtel if (new_ifindex && 188638e01b30SNicolas Dichtel nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0) 188738e01b30SNicolas Dichtel goto nla_put_failure; 188838e01b30SNicolas Dichtel 1889f74877a5SMichal Kubecek if (memchr_inv(dev->perm_addr, '\0', dev->addr_len) && 1890f74877a5SMichal Kubecek nla_put(skb, IFLA_PERM_ADDRESS, dev->addr_len, dev->perm_addr)) 1891f74877a5SMichal Kubecek goto nla_put_failure; 18926621dd29SNicolas Dichtel 18935fa85a09SFlorian Westphal rcu_read_lock(); 1894070cbf5bSFlorian Westphal if (rtnl_fill_link_af(skb, dev, ext_filter_mask)) 18955fa85a09SFlorian Westphal goto nla_put_failure_rcu; 18965fa85a09SFlorian Westphal rcu_read_unlock(); 1897f8ff182cSThomas Graf 189888f4fb0cSJiri Pirko if (rtnl_fill_prop_list(skb, dev)) 189988f4fb0cSJiri Pirko goto nla_put_failure; 190088f4fb0cSJiri Pirko 190100e77ed8SJohannes Berg if (dev->dev.parent && 190200e77ed8SJohannes Berg nla_put_string(skb, IFLA_PARENT_DEV_NAME, 190300e77ed8SJohannes Berg dev_name(dev->dev.parent))) 190400e77ed8SJohannes Berg goto nla_put_failure; 190500e77ed8SJohannes Berg 190600e77ed8SJohannes Berg if (dev->dev.parent && dev->dev.parent->bus && 190700e77ed8SJohannes Berg nla_put_string(skb, IFLA_PARENT_DEV_BUS_NAME, 190800e77ed8SJohannes Berg dev->dev.parent->bus->name)) 190900e77ed8SJohannes Berg goto nla_put_failure; 191000e77ed8SJohannes Berg 1911dca56c30SJiri Pirko if (rtnl_fill_devlink_port(skb, dev)) 1912dca56c30SJiri Pirko goto nla_put_failure; 1913dca56c30SJiri Pirko 1914053c095aSJohannes Berg nlmsg_end(skb, nlh); 1915053c095aSJohannes Berg return 0; 1916b60c5115SThomas Graf 19175fa85a09SFlorian Westphal nla_put_failure_rcu: 19185fa85a09SFlorian Westphal rcu_read_unlock(); 1919b60c5115SThomas Graf nla_put_failure: 192026932566SPatrick McHardy nlmsg_cancel(skb, nlh); 192126932566SPatrick McHardy return -EMSGSIZE; 19221da177e4SLinus Torvalds } 19231da177e4SLinus Torvalds 1924f7b12606SJiri Pirko static const struct nla_policy ifla_policy[IFLA_MAX+1] = { 1925f7b12606SJiri Pirko [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 }, 1926f7b12606SJiri Pirko [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 1927f7b12606SJiri Pirko [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 1928f7b12606SJiri Pirko [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) }, 1929f7b12606SJiri Pirko [IFLA_MTU] = { .type = NLA_U32 }, 1930f7b12606SJiri Pirko [IFLA_LINK] = { .type = NLA_U32 }, 1931f7b12606SJiri Pirko [IFLA_MASTER] = { .type = NLA_U32 }, 1932f7b12606SJiri Pirko [IFLA_CARRIER] = { .type = NLA_U8 }, 1933f7b12606SJiri Pirko [IFLA_TXQLEN] = { .type = NLA_U32 }, 1934f7b12606SJiri Pirko [IFLA_WEIGHT] = { .type = NLA_U32 }, 1935f7b12606SJiri Pirko [IFLA_OPERSTATE] = { .type = NLA_U8 }, 1936f7b12606SJiri Pirko [IFLA_LINKMODE] = { .type = NLA_U8 }, 1937f7b12606SJiri Pirko [IFLA_LINKINFO] = { .type = NLA_NESTED }, 1938f7b12606SJiri Pirko [IFLA_NET_NS_PID] = { .type = NLA_U32 }, 1939f7b12606SJiri Pirko [IFLA_NET_NS_FD] = { .type = NLA_U32 }, 19402459b4c6SNicolas Dichtel /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to 19412459b4c6SNicolas Dichtel * allow 0-length string (needed to remove an alias). 19422459b4c6SNicolas Dichtel */ 19432459b4c6SNicolas Dichtel [IFLA_IFALIAS] = { .type = NLA_BINARY, .len = IFALIASZ - 1 }, 1944f7b12606SJiri Pirko [IFLA_VFINFO_LIST] = {. type = NLA_NESTED }, 1945f7b12606SJiri Pirko [IFLA_VF_PORTS] = { .type = NLA_NESTED }, 1946f7b12606SJiri Pirko [IFLA_PORT_SELF] = { .type = NLA_NESTED }, 1947f7b12606SJiri Pirko [IFLA_AF_SPEC] = { .type = NLA_NESTED }, 1948f7b12606SJiri Pirko [IFLA_EXT_MASK] = { .type = NLA_U32 }, 1949f7b12606SJiri Pirko [IFLA_PROMISCUITY] = { .type = NLA_U32 }, 1950f7b12606SJiri Pirko [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 }, 1951f7b12606SJiri Pirko [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 }, 195246e6b992SStephen Hemminger [IFLA_GSO_MAX_SEGS] = { .type = NLA_U32 }, 195346e6b992SStephen Hemminger [IFLA_GSO_MAX_SIZE] = { .type = NLA_U32 }, 195402637fceSJiri Pirko [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN }, 19552d3b479dSdavid decotigny [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */ 195682f28412SJiri Pirko [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN }, 1957317f4810SNicolas Dichtel [IFLA_LINK_NETNSID] = { .type = NLA_S32 }, 195888d6378bSAnuradha Karuppiah [IFLA_PROTO_DOWN] = { .type = NLA_U8 }, 1959d1fdd913SBrenden Blanco [IFLA_XDP] = { .type = NLA_NESTED }, 19603d3ea5afSVlad Yasevich [IFLA_EVENT] = { .type = NLA_U32 }, 1961db833d40SSerhey Popovych [IFLA_GROUP] = { .type = NLA_U32 }, 19627e4a8d5aSChristian Brauner [IFLA_TARGET_NETNSID] = { .type = NLA_S32 }, 1963b2d3bcfaSDavid Decotigny [IFLA_CARRIER_UP_COUNT] = { .type = NLA_U32 }, 1964b2d3bcfaSDavid Decotigny [IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 }, 19653e7a50ceSStephen Hemminger [IFLA_MIN_MTU] = { .type = NLA_U32 }, 19663e7a50ceSStephen Hemminger [IFLA_MAX_MTU] = { .type = NLA_U32 }, 196736fbf1e5SJiri Pirko [IFLA_PROP_LIST] = { .type = NLA_NESTED }, 196836fbf1e5SJiri Pirko [IFLA_ALT_IFNAME] = { .type = NLA_STRING, 196936fbf1e5SJiri Pirko .len = ALTIFNAMSIZ - 1 }, 1970f74877a5SMichal Kubecek [IFLA_PERM_ADDRESS] = { .type = NLA_REJECT }, 1971829eb208SRoopa Prabhu [IFLA_PROTO_DOWN_REASON] = { .type = NLA_NESTED }, 19727e4a5131SAndrei Vagin [IFLA_NEW_IFINDEX] = NLA_POLICY_MIN(NLA_S32, 1), 197388b71053SJohannes Berg [IFLA_PARENT_DEV_NAME] = { .type = NLA_NUL_STRING }, 1974eac1b93cSCoco Li [IFLA_GRO_MAX_SIZE] = { .type = NLA_U32 }, 197589527be8SEric Dumazet [IFLA_TSO_MAX_SIZE] = { .type = NLA_REJECT }, 197689527be8SEric Dumazet [IFLA_TSO_MAX_SEGS] = { .type = NLA_REJECT }, 19777e6e1b57SNicolas Dichtel [IFLA_ALLMULTI] = { .type = NLA_REJECT }, 19789eefedd5SXin Long [IFLA_GSO_IPV4_MAX_SIZE] = { .type = NLA_U32 }, 19799eefedd5SXin Long [IFLA_GRO_IPV4_MAX_SIZE] = { .type = NLA_U32 }, 1980f7b12606SJiri Pirko }; 1981f7b12606SJiri Pirko 1982f7b12606SJiri Pirko static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = { 1983f7b12606SJiri Pirko [IFLA_INFO_KIND] = { .type = NLA_STRING }, 1984f7b12606SJiri Pirko [IFLA_INFO_DATA] = { .type = NLA_NESTED }, 1985f7b12606SJiri Pirko [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING }, 1986f7b12606SJiri Pirko [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED }, 1987f7b12606SJiri Pirko }; 1988f7b12606SJiri Pirko 1989f7b12606SJiri Pirko static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = { 1990364d5716SDaniel Borkmann [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) }, 199175345f88SDenis Kirjanov [IFLA_VF_BROADCAST] = { .type = NLA_REJECT }, 1992364d5716SDaniel Borkmann [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) }, 199379aab093SMoshe Shemesh [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED }, 1994364d5716SDaniel Borkmann [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) }, 1995364d5716SDaniel Borkmann [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) }, 1996364d5716SDaniel Borkmann [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) }, 1997364d5716SDaniel Borkmann [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) }, 199801a3d796SVlad Zolotarov [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) }, 19993b766cd8SEran Ben Elisha [IFLA_VF_STATS] = { .type = NLA_NESTED }, 2000dd461d6aSHiroshi Shimamoto [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) }, 2001cc8e27ccSEli Cohen [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) }, 2002cc8e27ccSEli Cohen [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) }, 20033b766cd8SEran Ben Elisha }; 20043b766cd8SEran Ben Elisha 2005f7b12606SJiri Pirko static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = { 2006f7b12606SJiri Pirko [IFLA_PORT_VF] = { .type = NLA_U32 }, 2007f7b12606SJiri Pirko [IFLA_PORT_PROFILE] = { .type = NLA_STRING, 2008f7b12606SJiri Pirko .len = PORT_PROFILE_MAX }, 2009f7b12606SJiri Pirko [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY, 2010f7b12606SJiri Pirko .len = PORT_UUID_MAX }, 2011f7b12606SJiri Pirko [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING, 2012f7b12606SJiri Pirko .len = PORT_UUID_MAX }, 2013f7b12606SJiri Pirko [IFLA_PORT_REQUEST] = { .type = NLA_U8, }, 2014f7b12606SJiri Pirko [IFLA_PORT_RESPONSE] = { .type = NLA_U16, }, 2015025331dfSDaniel Borkmann 2016025331dfSDaniel Borkmann /* Unused, but we need to keep it here since user space could 2017025331dfSDaniel Borkmann * fill it. It's also broken with regard to NLA_BINARY use in 2018025331dfSDaniel Borkmann * combination with structs. 2019025331dfSDaniel Borkmann */ 2020025331dfSDaniel Borkmann [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY, 2021025331dfSDaniel Borkmann .len = sizeof(struct ifla_port_vsi) }, 2022f7b12606SJiri Pirko }; 2023f7b12606SJiri Pirko 2024d1fdd913SBrenden Blanco static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = { 202592234c8fSToke Høiland-Jørgensen [IFLA_XDP_UNSPEC] = { .strict_start_type = IFLA_XDP_EXPECTED_FD }, 2026d1fdd913SBrenden Blanco [IFLA_XDP_FD] = { .type = NLA_S32 }, 202792234c8fSToke Høiland-Jørgensen [IFLA_XDP_EXPECTED_FD] = { .type = NLA_S32 }, 2028d1fdd913SBrenden Blanco [IFLA_XDP_ATTACHED] = { .type = NLA_U8 }, 202985de8576SDaniel Borkmann [IFLA_XDP_FLAGS] = { .type = NLA_U32 }, 203058038695SMartin KaFai Lau [IFLA_XDP_PROG_ID] = { .type = NLA_U32 }, 2031d1fdd913SBrenden Blanco }; 2032d1fdd913SBrenden Blanco 2033dc599f76SDavid Ahern static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla) 2034dc599f76SDavid Ahern { 2035dc599f76SDavid Ahern const struct rtnl_link_ops *ops = NULL; 2036dc599f76SDavid Ahern struct nlattr *linfo[IFLA_INFO_MAX + 1]; 2037dc599f76SDavid Ahern 20388cb08174SJohannes Berg if (nla_parse_nested_deprecated(linfo, IFLA_INFO_MAX, nla, ifla_info_policy, NULL) < 0) 2039dc599f76SDavid Ahern return NULL; 2040dc599f76SDavid Ahern 2041dc599f76SDavid Ahern if (linfo[IFLA_INFO_KIND]) { 2042dc599f76SDavid Ahern char kind[MODULE_NAME_LEN]; 2043dc599f76SDavid Ahern 2044872f6903SFrancis Laniel nla_strscpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind)); 2045dc599f76SDavid Ahern ops = rtnl_link_ops_get(kind); 2046dc599f76SDavid Ahern } 2047dc599f76SDavid Ahern 2048dc599f76SDavid Ahern return ops; 2049dc599f76SDavid Ahern } 2050dc599f76SDavid Ahern 2051dc599f76SDavid Ahern static bool link_master_filtered(struct net_device *dev, int master_idx) 2052dc599f76SDavid Ahern { 2053dc599f76SDavid Ahern struct net_device *master; 2054dc599f76SDavid Ahern 2055dc599f76SDavid Ahern if (!master_idx) 2056dc599f76SDavid Ahern return false; 2057dc599f76SDavid Ahern 2058dc599f76SDavid Ahern master = netdev_master_upper_dev_get(dev); 2059d3432bf1SLahav Schlesinger 2060d3432bf1SLahav Schlesinger /* 0 is already used to denote IFLA_MASTER wasn't passed, therefore need 2061d3432bf1SLahav Schlesinger * another invalid value for ifindex to denote "no master". 2062d3432bf1SLahav Schlesinger */ 2063d3432bf1SLahav Schlesinger if (master_idx == -1) 2064d3432bf1SLahav Schlesinger return !!master; 2065d3432bf1SLahav Schlesinger 2066dc599f76SDavid Ahern if (!master || master->ifindex != master_idx) 2067dc599f76SDavid Ahern return true; 2068dc599f76SDavid Ahern 2069dc599f76SDavid Ahern return false; 2070dc599f76SDavid Ahern } 2071dc599f76SDavid Ahern 2072dc599f76SDavid Ahern static bool link_kind_filtered(const struct net_device *dev, 2073dc599f76SDavid Ahern const struct rtnl_link_ops *kind_ops) 2074dc599f76SDavid Ahern { 2075dc599f76SDavid Ahern if (kind_ops && dev->rtnl_link_ops != kind_ops) 2076dc599f76SDavid Ahern return true; 2077dc599f76SDavid Ahern 2078dc599f76SDavid Ahern return false; 2079dc599f76SDavid Ahern } 2080dc599f76SDavid Ahern 2081dc599f76SDavid Ahern static bool link_dump_filtered(struct net_device *dev, 2082dc599f76SDavid Ahern int master_idx, 2083dc599f76SDavid Ahern const struct rtnl_link_ops *kind_ops) 2084dc599f76SDavid Ahern { 2085dc599f76SDavid Ahern if (link_master_filtered(dev, master_idx) || 2086dc599f76SDavid Ahern link_kind_filtered(dev, kind_ops)) 2087dc599f76SDavid Ahern return true; 2088dc599f76SDavid Ahern 2089dc599f76SDavid Ahern return false; 2090dc599f76SDavid Ahern } 2091dc599f76SDavid Ahern 2092c383edc4SChristian Brauner /** 2093c383edc4SChristian Brauner * rtnl_get_net_ns_capable - Get netns if sufficiently privileged. 2094c383edc4SChristian Brauner * @sk: netlink socket 2095c383edc4SChristian Brauner * @netnsid: network namespace identifier 2096c383edc4SChristian Brauner * 2097c383edc4SChristian Brauner * Returns the network namespace identified by netnsid on success or an error 2098c383edc4SChristian Brauner * pointer on failure. 2099c383edc4SChristian Brauner */ 2100c383edc4SChristian Brauner struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid) 210179e1ad14SJiri Benc { 210279e1ad14SJiri Benc struct net *net; 210379e1ad14SJiri Benc 2104f428fe4aSAndrei Vagin net = get_net_ns_by_id(sock_net(sk), netnsid); 210579e1ad14SJiri Benc if (!net) 210679e1ad14SJiri Benc return ERR_PTR(-EINVAL); 210779e1ad14SJiri Benc 210879e1ad14SJiri Benc /* For now, the caller is required to have CAP_NET_ADMIN in 210979e1ad14SJiri Benc * the user namespace owning the target net ns. 211079e1ad14SJiri Benc */ 2111f428fe4aSAndrei Vagin if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) { 211279e1ad14SJiri Benc put_net(net); 211379e1ad14SJiri Benc return ERR_PTR(-EACCES); 211479e1ad14SJiri Benc } 211579e1ad14SJiri Benc return net; 211679e1ad14SJiri Benc } 2117c383edc4SChristian Brauner EXPORT_SYMBOL_GPL(rtnl_get_net_ns_capable); 211879e1ad14SJiri Benc 2119905cf0abSDavid Ahern static int rtnl_valid_dump_ifinfo_req(const struct nlmsghdr *nlh, 2120905cf0abSDavid Ahern bool strict_check, struct nlattr **tb, 2121905cf0abSDavid Ahern struct netlink_ext_ack *extack) 2122905cf0abSDavid Ahern { 2123905cf0abSDavid Ahern int hdrlen; 2124905cf0abSDavid Ahern 2125905cf0abSDavid Ahern if (strict_check) { 2126905cf0abSDavid Ahern struct ifinfomsg *ifm; 2127905cf0abSDavid Ahern 2128905cf0abSDavid Ahern if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { 2129905cf0abSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid header for link dump"); 2130905cf0abSDavid Ahern return -EINVAL; 2131905cf0abSDavid Ahern } 2132905cf0abSDavid Ahern 2133905cf0abSDavid Ahern ifm = nlmsg_data(nlh); 2134905cf0abSDavid Ahern if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags || 2135905cf0abSDavid Ahern ifm->ifi_change) { 2136905cf0abSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid values in header for link dump request"); 2137905cf0abSDavid Ahern return -EINVAL; 2138905cf0abSDavid Ahern } 2139905cf0abSDavid Ahern if (ifm->ifi_index) { 2140905cf0abSDavid Ahern NL_SET_ERR_MSG(extack, "Filter by device index not supported for link dumps"); 2141905cf0abSDavid Ahern return -EINVAL; 2142905cf0abSDavid Ahern } 2143905cf0abSDavid Ahern 21448cb08174SJohannes Berg return nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, 21458cb08174SJohannes Berg IFLA_MAX, ifla_policy, 21468cb08174SJohannes Berg extack); 2147905cf0abSDavid Ahern } 2148905cf0abSDavid Ahern 2149905cf0abSDavid Ahern /* A hack to preserve kernel<->userspace interface. 2150905cf0abSDavid Ahern * The correct header is ifinfomsg. It is consistent with rtnl_getlink. 2151905cf0abSDavid Ahern * However, before Linux v3.9 the code here assumed rtgenmsg and that's 2152905cf0abSDavid Ahern * what iproute2 < v3.9.0 used. 2153905cf0abSDavid Ahern * We can detect the old iproute2. Even including the IFLA_EXT_MASK 2154905cf0abSDavid Ahern * attribute, its netlink message is shorter than struct ifinfomsg. 2155905cf0abSDavid Ahern */ 2156905cf0abSDavid Ahern hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ? 2157905cf0abSDavid Ahern sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); 2158905cf0abSDavid Ahern 21598cb08174SJohannes Berg return nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, 21608cb08174SJohannes Berg extack); 2161905cf0abSDavid Ahern } 2162905cf0abSDavid Ahern 2163b60c5115SThomas Graf static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 21641da177e4SLinus Torvalds { 2165905cf0abSDavid Ahern struct netlink_ext_ack *extack = cb->extack; 2166905cf0abSDavid Ahern const struct nlmsghdr *nlh = cb->nlh; 21673b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 216879e1ad14SJiri Benc struct net *tgt_net = net; 21697c28bd0bSEric Dumazet int h, s_h; 21707c28bd0bSEric Dumazet int idx = 0, s_idx; 21711da177e4SLinus Torvalds struct net_device *dev; 21727c28bd0bSEric Dumazet struct hlist_head *head; 2173115c9b81SGreg Rose struct nlattr *tb[IFLA_MAX+1]; 2174115c9b81SGreg Rose u32 ext_filter_mask = 0; 2175dc599f76SDavid Ahern const struct rtnl_link_ops *kind_ops = NULL; 2176dc599f76SDavid Ahern unsigned int flags = NLM_F_MULTI; 2177dc599f76SDavid Ahern int master_idx = 0; 217879e1ad14SJiri Benc int netnsid = -1; 2179905cf0abSDavid Ahern int err, i; 21801da177e4SLinus Torvalds 21817c28bd0bSEric Dumazet s_h = cb->args[0]; 21827c28bd0bSEric Dumazet s_idx = cb->args[1]; 21837c28bd0bSEric Dumazet 2184905cf0abSDavid Ahern err = rtnl_valid_dump_ifinfo_req(nlh, cb->strict_check, tb, extack); 2185905cf0abSDavid Ahern if (err < 0) { 2186905cf0abSDavid Ahern if (cb->strict_check) 2187905cf0abSDavid Ahern return err; 2188e5eca6d4SMichal Schmidt 2189905cf0abSDavid Ahern goto walk_entries; 219079e1ad14SJiri Benc } 219179e1ad14SJiri Benc 2192905cf0abSDavid Ahern for (i = 0; i <= IFLA_MAX; ++i) { 2193905cf0abSDavid Ahern if (!tb[i]) 2194905cf0abSDavid Ahern continue; 2195dc599f76SDavid Ahern 2196905cf0abSDavid Ahern /* new attributes should only be added with strict checking */ 2197905cf0abSDavid Ahern switch (i) { 2198905cf0abSDavid Ahern case IFLA_TARGET_NETNSID: 2199905cf0abSDavid Ahern netnsid = nla_get_s32(tb[i]); 2200905cf0abSDavid Ahern tgt_net = rtnl_get_net_ns_capable(skb->sk, netnsid); 2201905cf0abSDavid Ahern if (IS_ERR(tgt_net)) { 2202905cf0abSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid target network namespace id"); 2203905cf0abSDavid Ahern return PTR_ERR(tgt_net); 2204905cf0abSDavid Ahern } 2205905cf0abSDavid Ahern break; 2206905cf0abSDavid Ahern case IFLA_EXT_MASK: 2207905cf0abSDavid Ahern ext_filter_mask = nla_get_u32(tb[i]); 2208905cf0abSDavid Ahern break; 2209905cf0abSDavid Ahern case IFLA_MASTER: 2210905cf0abSDavid Ahern master_idx = nla_get_u32(tb[i]); 2211905cf0abSDavid Ahern break; 2212905cf0abSDavid Ahern case IFLA_LINKINFO: 2213905cf0abSDavid Ahern kind_ops = linkinfo_to_kind_ops(tb[i]); 2214905cf0abSDavid Ahern break; 2215905cf0abSDavid Ahern default: 2216905cf0abSDavid Ahern if (cb->strict_check) { 2217905cf0abSDavid Ahern NL_SET_ERR_MSG(extack, "Unsupported attribute in link dump request"); 2218905cf0abSDavid Ahern return -EINVAL; 2219905cf0abSDavid Ahern } 2220905cf0abSDavid Ahern } 2221905cf0abSDavid Ahern } 2222dc599f76SDavid Ahern 2223dc599f76SDavid Ahern if (master_idx || kind_ops) 2224dc599f76SDavid Ahern flags |= NLM_F_DUMP_FILTERED; 2225115c9b81SGreg Rose 2226905cf0abSDavid Ahern walk_entries: 22277c28bd0bSEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 22287562f876SPavel Emelianov idx = 0; 222979e1ad14SJiri Benc head = &tgt_net->dev_index_head[h]; 2230cac5e65eSEric Dumazet hlist_for_each_entry(dev, head, index_hlist) { 2231dc599f76SDavid Ahern if (link_dump_filtered(dev, master_idx, kind_ops)) 22323f0ae05dSZhang Shengju goto cont; 22331da177e4SLinus Torvalds if (idx < s_idx) 22347562f876SPavel Emelianov goto cont; 223579e1ad14SJiri Benc err = rtnl_fill_ifinfo(skb, dev, net, 223679e1ad14SJiri Benc RTM_NEWLINK, 223715e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 2238905cf0abSDavid Ahern nlh->nlmsg_seq, 0, flags, 223938e01b30SNicolas Dichtel ext_filter_mask, 0, NULL, 0, 2240d4e4fdf9SGuillaume Nault netnsid, GFP_KERNEL); 2241973462bbSDavid Gibson 2242f6c5775fSDavid Ahern if (err < 0) { 2243f6c5775fSDavid Ahern if (likely(skb->len)) 22447c28bd0bSEric Dumazet goto out; 22454e985adaSThomas Graf 2246f6c5775fSDavid Ahern goto out_err; 2247f6c5775fSDavid Ahern } 22487562f876SPavel Emelianov cont: 22497562f876SPavel Emelianov idx++; 22501da177e4SLinus Torvalds } 22517c28bd0bSEric Dumazet } 22527c28bd0bSEric Dumazet out: 2253f6c5775fSDavid Ahern err = skb->len; 2254f6c5775fSDavid Ahern out_err: 22557c28bd0bSEric Dumazet cb->args[1] = idx; 22567c28bd0bSEric Dumazet cb->args[0] = h; 2257a9ecb0cbSzhang kai cb->seq = tgt_net->dev_base_seq; 2258d0225784SJakub Sitnicki nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 225979e1ad14SJiri Benc if (netnsid >= 0) 226079e1ad14SJiri Benc put_net(tgt_net); 22611da177e4SLinus Torvalds 2262f6c5775fSDavid Ahern return err; 22631da177e4SLinus Torvalds } 22641da177e4SLinus Torvalds 2265fceb6435SJohannes Berg int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len, 2266fceb6435SJohannes Berg struct netlink_ext_ack *exterr) 2267f7b12606SJiri Pirko { 22688cb08174SJohannes Berg return nla_parse_deprecated(tb, IFLA_MAX, head, len, ifla_policy, 22698cb08174SJohannes Berg exterr); 2270f7b12606SJiri Pirko } 2271f7b12606SJiri Pirko EXPORT_SYMBOL(rtnl_nla_parse_ifla); 227257b61080SScott Feldman 227381adee47SEric W. Biederman struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[]) 227481adee47SEric W. Biederman { 227581adee47SEric W. Biederman struct net *net; 227681adee47SEric W. Biederman /* Examine the link attributes and figure out which 227781adee47SEric W. Biederman * network namespace we are talking about. 227881adee47SEric W. Biederman */ 227981adee47SEric W. Biederman if (tb[IFLA_NET_NS_PID]) 228081adee47SEric W. Biederman net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID])); 2281f0630529SEric W. Biederman else if (tb[IFLA_NET_NS_FD]) 2282f0630529SEric W. Biederman net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD])); 228381adee47SEric W. Biederman else 228481adee47SEric W. Biederman net = get_net(src_net); 228581adee47SEric W. Biederman return net; 228681adee47SEric W. Biederman } 228781adee47SEric W. Biederman EXPORT_SYMBOL(rtnl_link_get_net); 228881adee47SEric W. Biederman 22897c4f63baSChristian Brauner /* Figure out which network namespace we are talking about by 22907c4f63baSChristian Brauner * examining the link attributes in the following order: 22917c4f63baSChristian Brauner * 22927c4f63baSChristian Brauner * 1. IFLA_NET_NS_PID 22937c4f63baSChristian Brauner * 2. IFLA_NET_NS_FD 22947e4a8d5aSChristian Brauner * 3. IFLA_TARGET_NETNSID 22957c4f63baSChristian Brauner */ 22967c4f63baSChristian Brauner static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net, 22977c4f63baSChristian Brauner struct nlattr *tb[]) 22987c4f63baSChristian Brauner { 22997c4f63baSChristian Brauner struct net *net; 23007c4f63baSChristian Brauner 23017c4f63baSChristian Brauner if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) 23027c4f63baSChristian Brauner return rtnl_link_get_net(src_net, tb); 23037c4f63baSChristian Brauner 23047e4a8d5aSChristian Brauner if (!tb[IFLA_TARGET_NETNSID]) 23057c4f63baSChristian Brauner return get_net(src_net); 23067c4f63baSChristian Brauner 23077e4a8d5aSChristian Brauner net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_TARGET_NETNSID])); 23087c4f63baSChristian Brauner if (!net) 23097c4f63baSChristian Brauner return ERR_PTR(-EINVAL); 23107c4f63baSChristian Brauner 23117c4f63baSChristian Brauner return net; 23127c4f63baSChristian Brauner } 23137c4f63baSChristian Brauner 23147c4f63baSChristian Brauner static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb, 23157c4f63baSChristian Brauner struct net *src_net, 23167c4f63baSChristian Brauner struct nlattr *tb[], int cap) 23177c4f63baSChristian Brauner { 23187c4f63baSChristian Brauner struct net *net; 23197c4f63baSChristian Brauner 23207c4f63baSChristian Brauner net = rtnl_link_get_net_by_nlattr(src_net, tb); 23217c4f63baSChristian Brauner if (IS_ERR(net)) 23227c4f63baSChristian Brauner return net; 23237c4f63baSChristian Brauner 23247c4f63baSChristian Brauner if (!netlink_ns_capable(skb, net->user_ns, cap)) { 23257c4f63baSChristian Brauner put_net(net); 23267c4f63baSChristian Brauner return ERR_PTR(-EPERM); 23277c4f63baSChristian Brauner } 23287c4f63baSChristian Brauner 23297c4f63baSChristian Brauner return net; 23307c4f63baSChristian Brauner } 23317c4f63baSChristian Brauner 23324ff66caeSChristian Brauner /* Verify that rtnetlink requests do not pass additional properties 23334ff66caeSChristian Brauner * potentially referring to different network namespaces. 23344ff66caeSChristian Brauner */ 23354ff66caeSChristian Brauner static int rtnl_ensure_unique_netns(struct nlattr *tb[], 23364ff66caeSChristian Brauner struct netlink_ext_ack *extack, 23374ff66caeSChristian Brauner bool netns_id_only) 23384ff66caeSChristian Brauner { 23394ff66caeSChristian Brauner 23404ff66caeSChristian Brauner if (netns_id_only) { 23414ff66caeSChristian Brauner if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD]) 23424ff66caeSChristian Brauner return 0; 23434ff66caeSChristian Brauner 23444ff66caeSChristian Brauner NL_SET_ERR_MSG(extack, "specified netns attribute not supported"); 23454ff66caeSChristian Brauner return -EOPNOTSUPP; 23464ff66caeSChristian Brauner } 23474ff66caeSChristian Brauner 23487e4a8d5aSChristian Brauner if (tb[IFLA_TARGET_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])) 23494ff66caeSChristian Brauner goto invalid_attr; 23504ff66caeSChristian Brauner 23517e4a8d5aSChristian Brauner if (tb[IFLA_NET_NS_PID] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_FD])) 23524ff66caeSChristian Brauner goto invalid_attr; 23534ff66caeSChristian Brauner 23547e4a8d5aSChristian Brauner if (tb[IFLA_NET_NS_FD] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_PID])) 23554ff66caeSChristian Brauner goto invalid_attr; 23564ff66caeSChristian Brauner 23574ff66caeSChristian Brauner return 0; 23584ff66caeSChristian Brauner 23594ff66caeSChristian Brauner invalid_attr: 23604ff66caeSChristian Brauner NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified"); 23614ff66caeSChristian Brauner return -EINVAL; 23624ff66caeSChristian Brauner } 23634ff66caeSChristian Brauner 2364a14857c2SBin Chen static int rtnl_set_vf_rate(struct net_device *dev, int vf, int min_tx_rate, 2365a14857c2SBin Chen int max_tx_rate) 2366a14857c2SBin Chen { 2367a14857c2SBin Chen const struct net_device_ops *ops = dev->netdev_ops; 2368a14857c2SBin Chen 2369a14857c2SBin Chen if (!ops->ndo_set_vf_rate) 2370a14857c2SBin Chen return -EOPNOTSUPP; 2371a14857c2SBin Chen if (max_tx_rate && max_tx_rate < min_tx_rate) 2372a14857c2SBin Chen return -EINVAL; 2373a14857c2SBin Chen 2374a14857c2SBin Chen return ops->ndo_set_vf_rate(dev, vf, min_tx_rate, max_tx_rate); 2375a14857c2SBin Chen } 2376a14857c2SBin Chen 23778679c31eSRocco Yue static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[], 23788679c31eSRocco Yue struct netlink_ext_ack *extack) 23791840bb13SThomas Graf { 23801840bb13SThomas Graf if (dev) { 23811840bb13SThomas Graf if (tb[IFLA_ADDRESS] && 23821840bb13SThomas Graf nla_len(tb[IFLA_ADDRESS]) < dev->addr_len) 23831840bb13SThomas Graf return -EINVAL; 23841840bb13SThomas Graf 23851840bb13SThomas Graf if (tb[IFLA_BROADCAST] && 23861840bb13SThomas Graf nla_len(tb[IFLA_BROADCAST]) < dev->addr_len) 23871840bb13SThomas Graf return -EINVAL; 23881840bb13SThomas Graf } 23891840bb13SThomas Graf 2390cf7afbfeSThomas Graf if (tb[IFLA_AF_SPEC]) { 2391cf7afbfeSThomas Graf struct nlattr *af; 2392cf7afbfeSThomas Graf int rem, err; 2393cf7afbfeSThomas Graf 2394cf7afbfeSThomas Graf nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { 2395cf7afbfeSThomas Graf const struct rtnl_af_ops *af_ops; 2396cf7afbfeSThomas Graf 23975fa85a09SFlorian Westphal af_ops = rtnl_af_lookup(nla_type(af)); 2398a100243dSCong Wang if (!af_ops) 2399cf7afbfeSThomas Graf return -EAFNOSUPPORT; 2400cf7afbfeSThomas Graf 2401a100243dSCong Wang if (!af_ops->set_link_af) 2402cf7afbfeSThomas Graf return -EOPNOTSUPP; 2403cf7afbfeSThomas Graf 2404cf7afbfeSThomas Graf if (af_ops->validate_link_af) { 24058679c31eSRocco Yue err = af_ops->validate_link_af(dev, af, extack); 2406a100243dSCong Wang if (err < 0) 2407cf7afbfeSThomas Graf return err; 2408cf7afbfeSThomas Graf } 2409cf7afbfeSThomas Graf } 2410cf7afbfeSThomas Graf } 2411cf7afbfeSThomas Graf 24121840bb13SThomas Graf return 0; 24131840bb13SThomas Graf } 24141840bb13SThomas Graf 2415cc8e27ccSEli Cohen static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt, 2416cc8e27ccSEli Cohen int guid_type) 2417cc8e27ccSEli Cohen { 2418cc8e27ccSEli Cohen const struct net_device_ops *ops = dev->netdev_ops; 2419cc8e27ccSEli Cohen 2420cc8e27ccSEli Cohen return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type); 2421cc8e27ccSEli Cohen } 2422cc8e27ccSEli Cohen 2423cc8e27ccSEli Cohen static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type) 2424cc8e27ccSEli Cohen { 2425cc8e27ccSEli Cohen if (dev->type != ARPHRD_INFINIBAND) 2426cc8e27ccSEli Cohen return -EOPNOTSUPP; 2427cc8e27ccSEli Cohen 2428cc8e27ccSEli Cohen return handle_infiniband_guid(dev, ivt, guid_type); 2429cc8e27ccSEli Cohen } 2430cc8e27ccSEli Cohen 24314f7d2cdfSDaniel Borkmann static int do_setvfinfo(struct net_device *dev, struct nlattr **tb) 2432c02db8c6SChris Wright { 2433c02db8c6SChris Wright const struct net_device_ops *ops = dev->netdev_ops; 24344f7d2cdfSDaniel Borkmann int err = -EINVAL; 2435c02db8c6SChris Wright 24364f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_MAC]) { 24374f7d2cdfSDaniel Borkmann struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]); 24384f7d2cdfSDaniel Borkmann 2439ff08ddbaSDan Carpenter if (ivm->vf >= INT_MAX) 2440ff08ddbaSDan Carpenter return -EINVAL; 2441c02db8c6SChris Wright err = -EOPNOTSUPP; 2442c02db8c6SChris Wright if (ops->ndo_set_vf_mac) 2443c02db8c6SChris Wright err = ops->ndo_set_vf_mac(dev, ivm->vf, 2444c02db8c6SChris Wright ivm->mac); 24454f7d2cdfSDaniel Borkmann if (err < 0) 24464f7d2cdfSDaniel Borkmann return err; 2447c02db8c6SChris Wright } 24484f7d2cdfSDaniel Borkmann 24494f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_VLAN]) { 24504f7d2cdfSDaniel Borkmann struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]); 24514f7d2cdfSDaniel Borkmann 2452ff08ddbaSDan Carpenter if (ivv->vf >= INT_MAX) 2453ff08ddbaSDan Carpenter return -EINVAL; 2454c02db8c6SChris Wright err = -EOPNOTSUPP; 2455c02db8c6SChris Wright if (ops->ndo_set_vf_vlan) 24564f7d2cdfSDaniel Borkmann err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan, 245779aab093SMoshe Shemesh ivv->qos, 245879aab093SMoshe Shemesh htons(ETH_P_8021Q)); 245979aab093SMoshe Shemesh if (err < 0) 246079aab093SMoshe Shemesh return err; 246179aab093SMoshe Shemesh } 246279aab093SMoshe Shemesh 246379aab093SMoshe Shemesh if (tb[IFLA_VF_VLAN_LIST]) { 246479aab093SMoshe Shemesh struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN]; 246579aab093SMoshe Shemesh struct nlattr *attr; 246679aab093SMoshe Shemesh int rem, len = 0; 246779aab093SMoshe Shemesh 246879aab093SMoshe Shemesh err = -EOPNOTSUPP; 246979aab093SMoshe Shemesh if (!ops->ndo_set_vf_vlan) 247079aab093SMoshe Shemesh return err; 247179aab093SMoshe Shemesh 247279aab093SMoshe Shemesh nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) { 247379aab093SMoshe Shemesh if (nla_type(attr) != IFLA_VF_VLAN_INFO || 247479aab093SMoshe Shemesh nla_len(attr) < NLA_HDRLEN) { 247579aab093SMoshe Shemesh return -EINVAL; 247679aab093SMoshe Shemesh } 247779aab093SMoshe Shemesh if (len >= MAX_VLAN_LIST_LEN) 247879aab093SMoshe Shemesh return -EOPNOTSUPP; 247979aab093SMoshe Shemesh ivvl[len] = nla_data(attr); 248079aab093SMoshe Shemesh 248179aab093SMoshe Shemesh len++; 248279aab093SMoshe Shemesh } 2483fa34cd94SArnd Bergmann if (len == 0) 2484fa34cd94SArnd Bergmann return -EINVAL; 2485fa34cd94SArnd Bergmann 2486ff08ddbaSDan Carpenter if (ivvl[0]->vf >= INT_MAX) 2487ff08ddbaSDan Carpenter return -EINVAL; 248879aab093SMoshe Shemesh err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan, 248979aab093SMoshe Shemesh ivvl[0]->qos, ivvl[0]->vlan_proto); 24904f7d2cdfSDaniel Borkmann if (err < 0) 24914f7d2cdfSDaniel Borkmann return err; 2492c02db8c6SChris Wright } 24934f7d2cdfSDaniel Borkmann 24944f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_TX_RATE]) { 24954f7d2cdfSDaniel Borkmann struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]); 2496ed616689SSucheta Chakraborty struct ifla_vf_info ivf; 24974f7d2cdfSDaniel Borkmann 2498ff08ddbaSDan Carpenter if (ivt->vf >= INT_MAX) 2499ff08ddbaSDan Carpenter return -EINVAL; 2500c02db8c6SChris Wright err = -EOPNOTSUPP; 2501ed616689SSucheta Chakraborty if (ops->ndo_get_vf_config) 25024f7d2cdfSDaniel Borkmann err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf); 25034f7d2cdfSDaniel Borkmann if (err < 0) 25044f7d2cdfSDaniel Borkmann return err; 25054f7d2cdfSDaniel Borkmann 2506a14857c2SBin Chen err = rtnl_set_vf_rate(dev, ivt->vf, 2507a14857c2SBin Chen ivf.min_tx_rate, ivt->rate); 25084f7d2cdfSDaniel Borkmann if (err < 0) 25094f7d2cdfSDaniel Borkmann return err; 2510c02db8c6SChris Wright } 25114f7d2cdfSDaniel Borkmann 25124f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_RATE]) { 25134f7d2cdfSDaniel Borkmann struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]); 25144f7d2cdfSDaniel Borkmann 2515ff08ddbaSDan Carpenter if (ivt->vf >= INT_MAX) 2516ff08ddbaSDan Carpenter return -EINVAL; 2517a14857c2SBin Chen 2518a14857c2SBin Chen err = rtnl_set_vf_rate(dev, ivt->vf, 2519a14857c2SBin Chen ivt->min_tx_rate, ivt->max_tx_rate); 25204f7d2cdfSDaniel Borkmann if (err < 0) 25214f7d2cdfSDaniel Borkmann return err; 2522ed616689SSucheta Chakraborty } 25234f7d2cdfSDaniel Borkmann 25244f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_SPOOFCHK]) { 25254f7d2cdfSDaniel Borkmann struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]); 25264f7d2cdfSDaniel Borkmann 2527ff08ddbaSDan Carpenter if (ivs->vf >= INT_MAX) 2528ff08ddbaSDan Carpenter return -EINVAL; 25295f8444a3SGreg Rose err = -EOPNOTSUPP; 25305f8444a3SGreg Rose if (ops->ndo_set_vf_spoofchk) 25315f8444a3SGreg Rose err = ops->ndo_set_vf_spoofchk(dev, ivs->vf, 25325f8444a3SGreg Rose ivs->setting); 25334f7d2cdfSDaniel Borkmann if (err < 0) 25344f7d2cdfSDaniel Borkmann return err; 25355f8444a3SGreg Rose } 25364f7d2cdfSDaniel Borkmann 25374f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_LINK_STATE]) { 25384f7d2cdfSDaniel Borkmann struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]); 25394f7d2cdfSDaniel Borkmann 2540ff08ddbaSDan Carpenter if (ivl->vf >= INT_MAX) 2541ff08ddbaSDan Carpenter return -EINVAL; 25421d8faf48SRony Efraim err = -EOPNOTSUPP; 25431d8faf48SRony Efraim if (ops->ndo_set_vf_link_state) 25441d8faf48SRony Efraim err = ops->ndo_set_vf_link_state(dev, ivl->vf, 25451d8faf48SRony Efraim ivl->link_state); 25464f7d2cdfSDaniel Borkmann if (err < 0) 25474f7d2cdfSDaniel Borkmann return err; 25481d8faf48SRony Efraim } 25494f7d2cdfSDaniel Borkmann 25504f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_RSS_QUERY_EN]) { 255101a3d796SVlad Zolotarov struct ifla_vf_rss_query_en *ivrssq_en; 255201a3d796SVlad Zolotarov 255301a3d796SVlad Zolotarov err = -EOPNOTSUPP; 25544f7d2cdfSDaniel Borkmann ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]); 2555ff08ddbaSDan Carpenter if (ivrssq_en->vf >= INT_MAX) 2556ff08ddbaSDan Carpenter return -EINVAL; 255701a3d796SVlad Zolotarov if (ops->ndo_set_vf_rss_query_en) 25584f7d2cdfSDaniel Borkmann err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf, 255901a3d796SVlad Zolotarov ivrssq_en->setting); 25604f7d2cdfSDaniel Borkmann if (err < 0) 25614f7d2cdfSDaniel Borkmann return err; 256201a3d796SVlad Zolotarov } 25634f7d2cdfSDaniel Borkmann 2564dd461d6aSHiroshi Shimamoto if (tb[IFLA_VF_TRUST]) { 2565dd461d6aSHiroshi Shimamoto struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]); 2566dd461d6aSHiroshi Shimamoto 2567ff08ddbaSDan Carpenter if (ivt->vf >= INT_MAX) 2568ff08ddbaSDan Carpenter return -EINVAL; 2569dd461d6aSHiroshi Shimamoto err = -EOPNOTSUPP; 2570dd461d6aSHiroshi Shimamoto if (ops->ndo_set_vf_trust) 2571dd461d6aSHiroshi Shimamoto err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting); 2572dd461d6aSHiroshi Shimamoto if (err < 0) 2573dd461d6aSHiroshi Shimamoto return err; 2574dd461d6aSHiroshi Shimamoto } 2575dd461d6aSHiroshi Shimamoto 2576cc8e27ccSEli Cohen if (tb[IFLA_VF_IB_NODE_GUID]) { 2577cc8e27ccSEli Cohen struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]); 2578cc8e27ccSEli Cohen 2579ff08ddbaSDan Carpenter if (ivt->vf >= INT_MAX) 2580ff08ddbaSDan Carpenter return -EINVAL; 2581cc8e27ccSEli Cohen if (!ops->ndo_set_vf_guid) 2582cc8e27ccSEli Cohen return -EOPNOTSUPP; 2583cc8e27ccSEli Cohen return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID); 2584cc8e27ccSEli Cohen } 2585cc8e27ccSEli Cohen 2586cc8e27ccSEli Cohen if (tb[IFLA_VF_IB_PORT_GUID]) { 2587cc8e27ccSEli Cohen struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]); 2588cc8e27ccSEli Cohen 2589ff08ddbaSDan Carpenter if (ivt->vf >= INT_MAX) 2590ff08ddbaSDan Carpenter return -EINVAL; 2591cc8e27ccSEli Cohen if (!ops->ndo_set_vf_guid) 2592cc8e27ccSEli Cohen return -EOPNOTSUPP; 2593cc8e27ccSEli Cohen 2594cc8e27ccSEli Cohen return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID); 2595cc8e27ccSEli Cohen } 2596cc8e27ccSEli Cohen 2597c02db8c6SChris Wright return err; 2598c02db8c6SChris Wright } 2599c02db8c6SChris Wright 260033eaf2a6SDavid Ahern static int do_set_master(struct net_device *dev, int ifindex, 260133eaf2a6SDavid Ahern struct netlink_ext_ack *extack) 2602fbaec0eaSJiri Pirko { 2603898e5061SJiri Pirko struct net_device *upper_dev = netdev_master_upper_dev_get(dev); 2604fbaec0eaSJiri Pirko const struct net_device_ops *ops; 2605fbaec0eaSJiri Pirko int err; 2606fbaec0eaSJiri Pirko 2607898e5061SJiri Pirko if (upper_dev) { 2608898e5061SJiri Pirko if (upper_dev->ifindex == ifindex) 2609fbaec0eaSJiri Pirko return 0; 2610898e5061SJiri Pirko ops = upper_dev->netdev_ops; 2611fbaec0eaSJiri Pirko if (ops->ndo_del_slave) { 2612898e5061SJiri Pirko err = ops->ndo_del_slave(upper_dev, dev); 2613fbaec0eaSJiri Pirko if (err) 2614fbaec0eaSJiri Pirko return err; 2615fbaec0eaSJiri Pirko } else { 2616fbaec0eaSJiri Pirko return -EOPNOTSUPP; 2617fbaec0eaSJiri Pirko } 2618fbaec0eaSJiri Pirko } 2619fbaec0eaSJiri Pirko 2620fbaec0eaSJiri Pirko if (ifindex) { 2621898e5061SJiri Pirko upper_dev = __dev_get_by_index(dev_net(dev), ifindex); 2622898e5061SJiri Pirko if (!upper_dev) 2623fbaec0eaSJiri Pirko return -EINVAL; 2624898e5061SJiri Pirko ops = upper_dev->netdev_ops; 2625fbaec0eaSJiri Pirko if (ops->ndo_add_slave) { 262633eaf2a6SDavid Ahern err = ops->ndo_add_slave(upper_dev, dev, extack); 2627fbaec0eaSJiri Pirko if (err) 2628fbaec0eaSJiri Pirko return err; 2629fbaec0eaSJiri Pirko } else { 2630fbaec0eaSJiri Pirko return -EOPNOTSUPP; 2631fbaec0eaSJiri Pirko } 2632fbaec0eaSJiri Pirko } 2633fbaec0eaSJiri Pirko return 0; 2634fbaec0eaSJiri Pirko } 2635fbaec0eaSJiri Pirko 2636829eb208SRoopa Prabhu static const struct nla_policy ifla_proto_down_reason_policy[IFLA_PROTO_DOWN_REASON_VALUE + 1] = { 2637829eb208SRoopa Prabhu [IFLA_PROTO_DOWN_REASON_MASK] = { .type = NLA_U32 }, 2638829eb208SRoopa Prabhu [IFLA_PROTO_DOWN_REASON_VALUE] = { .type = NLA_U32 }, 2639829eb208SRoopa Prabhu }; 2640829eb208SRoopa Prabhu 2641829eb208SRoopa Prabhu static int do_set_proto_down(struct net_device *dev, 2642829eb208SRoopa Prabhu struct nlattr *nl_proto_down, 2643829eb208SRoopa Prabhu struct nlattr *nl_proto_down_reason, 2644829eb208SRoopa Prabhu struct netlink_ext_ack *extack) 2645829eb208SRoopa Prabhu { 2646829eb208SRoopa Prabhu struct nlattr *pdreason[IFLA_PROTO_DOWN_REASON_MAX + 1]; 2647829eb208SRoopa Prabhu unsigned long mask = 0; 2648829eb208SRoopa Prabhu u32 value; 2649829eb208SRoopa Prabhu bool proto_down; 2650829eb208SRoopa Prabhu int err; 2651829eb208SRoopa Prabhu 26522106efdaSJakub Kicinski if (!(dev->priv_flags & IFF_CHANGE_PROTO_DOWN)) { 2653829eb208SRoopa Prabhu NL_SET_ERR_MSG(extack, "Protodown not supported by device"); 2654829eb208SRoopa Prabhu return -EOPNOTSUPP; 2655829eb208SRoopa Prabhu } 2656829eb208SRoopa Prabhu 2657829eb208SRoopa Prabhu if (nl_proto_down_reason) { 2658829eb208SRoopa Prabhu err = nla_parse_nested_deprecated(pdreason, 2659829eb208SRoopa Prabhu IFLA_PROTO_DOWN_REASON_MAX, 2660829eb208SRoopa Prabhu nl_proto_down_reason, 2661829eb208SRoopa Prabhu ifla_proto_down_reason_policy, 2662829eb208SRoopa Prabhu NULL); 2663829eb208SRoopa Prabhu if (err < 0) 2664829eb208SRoopa Prabhu return err; 2665829eb208SRoopa Prabhu 2666829eb208SRoopa Prabhu if (!pdreason[IFLA_PROTO_DOWN_REASON_VALUE]) { 2667829eb208SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid protodown reason value"); 2668829eb208SRoopa Prabhu return -EINVAL; 2669829eb208SRoopa Prabhu } 2670829eb208SRoopa Prabhu 2671829eb208SRoopa Prabhu value = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_VALUE]); 2672829eb208SRoopa Prabhu 2673829eb208SRoopa Prabhu if (pdreason[IFLA_PROTO_DOWN_REASON_MASK]) 2674829eb208SRoopa Prabhu mask = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_MASK]); 2675829eb208SRoopa Prabhu 2676829eb208SRoopa Prabhu dev_change_proto_down_reason(dev, mask, value); 2677829eb208SRoopa Prabhu } 2678829eb208SRoopa Prabhu 2679829eb208SRoopa Prabhu if (nl_proto_down) { 2680829eb208SRoopa Prabhu proto_down = nla_get_u8(nl_proto_down); 2681829eb208SRoopa Prabhu 2682d467d0bcSZheng Yongjun /* Don't turn off protodown if there are active reasons */ 2683829eb208SRoopa Prabhu if (!proto_down && dev->proto_down_reason) { 2684829eb208SRoopa Prabhu NL_SET_ERR_MSG(extack, "Cannot clear protodown, active reasons"); 2685829eb208SRoopa Prabhu return -EBUSY; 2686829eb208SRoopa Prabhu } 2687829eb208SRoopa Prabhu err = dev_change_proto_down(dev, 2688829eb208SRoopa Prabhu proto_down); 2689829eb208SRoopa Prabhu if (err) 2690829eb208SRoopa Prabhu return err; 2691829eb208SRoopa Prabhu } 2692829eb208SRoopa Prabhu 2693829eb208SRoopa Prabhu return 0; 2694829eb208SRoopa Prabhu } 2695829eb208SRoopa Prabhu 269690c325e3SNicolas Dichtel #define DO_SETLINK_MODIFIED 0x01 2697ba998906SNicolas Dichtel /* notify flag means notify + modified. */ 2698ba998906SNicolas Dichtel #define DO_SETLINK_NOTIFY 0x03 269990f62cf3SEric W. Biederman static int do_setlink(const struct sk_buff *skb, 270090f62cf3SEric W. Biederman struct net_device *dev, struct ifinfomsg *ifm, 2701ddf9f970SJakub Kicinski struct netlink_ext_ack *extack, 27025ea08b52SFlorent Fourcot struct nlattr **tb, int status) 27030157f60cSPatrick McHardy { 2704d314774cSStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops; 27055ea08b52SFlorent Fourcot char ifname[IFNAMSIZ]; 27060157f60cSPatrick McHardy int err; 27070157f60cSPatrick McHardy 27088679c31eSRocco Yue err = validate_linkmsg(dev, tb, extack); 2709644c7eebSEric Dumazet if (err < 0) 2710644c7eebSEric Dumazet return err; 2711644c7eebSEric Dumazet 27125ea08b52SFlorent Fourcot if (tb[IFLA_IFNAME]) 27135ea08b52SFlorent Fourcot nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 27145ea08b52SFlorent Fourcot else 27155ea08b52SFlorent Fourcot ifname[0] = '\0'; 27165ea08b52SFlorent Fourcot 27177e4a8d5aSChristian Brauner if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_TARGET_NETNSID]) { 27185ea08b52SFlorent Fourcot const char *pat = ifname[0] ? ifname : NULL; 2719eeb85a14SAndrei Vagin struct net *net; 2720eeb85a14SAndrei Vagin int new_ifindex; 2721eeb85a14SAndrei Vagin 2722eeb85a14SAndrei Vagin net = rtnl_link_get_net_capable(skb, dev_net(dev), 27237c4f63baSChristian Brauner tb, CAP_NET_ADMIN); 2724d8a5ec67SEric W. Biederman if (IS_ERR(net)) { 2725d8a5ec67SEric W. Biederman err = PTR_ERR(net); 2726d8a5ec67SEric W. Biederman goto errout; 2727d8a5ec67SEric W. Biederman } 27287c4f63baSChristian Brauner 2729eeb85a14SAndrei Vagin if (tb[IFLA_NEW_IFINDEX]) 2730eeb85a14SAndrei Vagin new_ifindex = nla_get_s32(tb[IFLA_NEW_IFINDEX]); 2731eeb85a14SAndrei Vagin else 2732eeb85a14SAndrei Vagin new_ifindex = 0; 2733eeb85a14SAndrei Vagin 273496a6b93bSAndrey Ignatov err = __dev_change_net_namespace(dev, net, pat, new_ifindex); 2735d8a5ec67SEric W. Biederman put_net(net); 2736d8a5ec67SEric W. Biederman if (err) 2737d8a5ec67SEric W. Biederman goto errout; 273890c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 2739d8a5ec67SEric W. Biederman } 2740d8a5ec67SEric W. Biederman 27410157f60cSPatrick McHardy if (tb[IFLA_MAP]) { 27420157f60cSPatrick McHardy struct rtnl_link_ifmap *u_map; 27430157f60cSPatrick McHardy struct ifmap k_map; 27440157f60cSPatrick McHardy 2745d314774cSStephen Hemminger if (!ops->ndo_set_config) { 27460157f60cSPatrick McHardy err = -EOPNOTSUPP; 27470157f60cSPatrick McHardy goto errout; 27480157f60cSPatrick McHardy } 27490157f60cSPatrick McHardy 27500157f60cSPatrick McHardy if (!netif_device_present(dev)) { 27510157f60cSPatrick McHardy err = -ENODEV; 27520157f60cSPatrick McHardy goto errout; 27530157f60cSPatrick McHardy } 27540157f60cSPatrick McHardy 27550157f60cSPatrick McHardy u_map = nla_data(tb[IFLA_MAP]); 27560157f60cSPatrick McHardy k_map.mem_start = (unsigned long) u_map->mem_start; 27570157f60cSPatrick McHardy k_map.mem_end = (unsigned long) u_map->mem_end; 27580157f60cSPatrick McHardy k_map.base_addr = (unsigned short) u_map->base_addr; 27590157f60cSPatrick McHardy k_map.irq = (unsigned char) u_map->irq; 27600157f60cSPatrick McHardy k_map.dma = (unsigned char) u_map->dma; 27610157f60cSPatrick McHardy k_map.port = (unsigned char) u_map->port; 27620157f60cSPatrick McHardy 2763d314774cSStephen Hemminger err = ops->ndo_set_config(dev, &k_map); 27640157f60cSPatrick McHardy if (err < 0) 27650157f60cSPatrick McHardy goto errout; 27660157f60cSPatrick McHardy 2767ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 27680157f60cSPatrick McHardy } 27690157f60cSPatrick McHardy 27700157f60cSPatrick McHardy if (tb[IFLA_ADDRESS]) { 27710157f60cSPatrick McHardy struct sockaddr *sa; 27720157f60cSPatrick McHardy int len; 27730157f60cSPatrick McHardy 2774153711f9SWANG Cong len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len, 2775153711f9SWANG Cong sizeof(*sa)); 27760157f60cSPatrick McHardy sa = kmalloc(len, GFP_KERNEL); 27770157f60cSPatrick McHardy if (!sa) { 27780157f60cSPatrick McHardy err = -ENOMEM; 27790157f60cSPatrick McHardy goto errout; 27800157f60cSPatrick McHardy } 27810157f60cSPatrick McHardy sa->sa_family = dev->type; 27820157f60cSPatrick McHardy memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]), 27830157f60cSPatrick McHardy dev->addr_len); 27843b23a32aSCong Wang err = dev_set_mac_address_user(dev, sa, extack); 27850157f60cSPatrick McHardy kfree(sa); 27860157f60cSPatrick McHardy if (err) 27870157f60cSPatrick McHardy goto errout; 278890c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 27890157f60cSPatrick McHardy } 27900157f60cSPatrick McHardy 27910157f60cSPatrick McHardy if (tb[IFLA_MTU]) { 27927a4c53beSStephen Hemminger err = dev_set_mtu_ext(dev, nla_get_u32(tb[IFLA_MTU]), extack); 27930157f60cSPatrick McHardy if (err < 0) 27940157f60cSPatrick McHardy goto errout; 279590c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 27960157f60cSPatrick McHardy } 27970157f60cSPatrick McHardy 2798cbda10faSVlad Dogaru if (tb[IFLA_GROUP]) { 2799cbda10faSVlad Dogaru dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); 2800ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 2801cbda10faSVlad Dogaru } 2802cbda10faSVlad Dogaru 28030157f60cSPatrick McHardy /* 28040157f60cSPatrick McHardy * Interface selected by interface index but interface 28050157f60cSPatrick McHardy * name provided implies that a name change has been 28060157f60cSPatrick McHardy * requested. 28070157f60cSPatrick McHardy */ 28080157f60cSPatrick McHardy if (ifm->ifi_index > 0 && ifname[0]) { 28090157f60cSPatrick McHardy err = dev_change_name(dev, ifname); 28100157f60cSPatrick McHardy if (err < 0) 28110157f60cSPatrick McHardy goto errout; 281290c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 28130157f60cSPatrick McHardy } 28140157f60cSPatrick McHardy 28150b815a1aSStephen Hemminger if (tb[IFLA_IFALIAS]) { 28160b815a1aSStephen Hemminger err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]), 28170b815a1aSStephen Hemminger nla_len(tb[IFLA_IFALIAS])); 28180b815a1aSStephen Hemminger if (err < 0) 28190b815a1aSStephen Hemminger goto errout; 2820ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 28210b815a1aSStephen Hemminger } 28220b815a1aSStephen Hemminger 28230157f60cSPatrick McHardy if (tb[IFLA_BROADCAST]) { 28240157f60cSPatrick McHardy nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len); 2825e7c3273eSJiri Pirko call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); 28260157f60cSPatrick McHardy } 28270157f60cSPatrick McHardy 2828fbaec0eaSJiri Pirko if (tb[IFLA_MASTER]) { 282933eaf2a6SDavid Ahern err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack); 2830fbaec0eaSJiri Pirko if (err) 2831fbaec0eaSJiri Pirko goto errout; 283290c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 2833fbaec0eaSJiri Pirko } 2834fbaec0eaSJiri Pirko 2835a4abfa62SPhil Sutter if (ifm->ifi_flags || ifm->ifi_change) { 2836a4abfa62SPhil Sutter err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm), 2837a4abfa62SPhil Sutter extack); 2838a4abfa62SPhil Sutter if (err < 0) 2839a4abfa62SPhil Sutter goto errout; 2840a4abfa62SPhil Sutter } 2841a4abfa62SPhil Sutter 28429a57247fSJiri Pirko if (tb[IFLA_CARRIER]) { 28439a57247fSJiri Pirko err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER])); 28449a57247fSJiri Pirko if (err) 28459a57247fSJiri Pirko goto errout; 284690c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 28479a57247fSJiri Pirko } 28489a57247fSJiri Pirko 28495d1180fcSNicolas Dichtel if (tb[IFLA_TXQLEN]) { 28500cd29503SAlexey Dobriyan unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]); 28515d1180fcSNicolas Dichtel 28526a643ddbSCong Wang err = dev_change_tx_queue_len(dev, value); 28536a643ddbSCong Wang if (err) 285408294a26SJason Wang goto errout; 28552d7f669bSXin Long status |= DO_SETLINK_MODIFIED; 285608294a26SJason Wang } 28570157f60cSPatrick McHardy 285846e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SIZE]) { 285946e6b992SStephen Hemminger u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]); 286046e6b992SStephen Hemminger 28617c4e983cSAlexander Duyck if (max_size > dev->tso_max_size) { 286246e6b992SStephen Hemminger err = -EINVAL; 286346e6b992SStephen Hemminger goto errout; 286446e6b992SStephen Hemminger } 286546e6b992SStephen Hemminger 286646e6b992SStephen Hemminger if (dev->gso_max_size ^ max_size) { 286746e6b992SStephen Hemminger netif_set_gso_max_size(dev, max_size); 286846e6b992SStephen Hemminger status |= DO_SETLINK_MODIFIED; 286946e6b992SStephen Hemminger } 287046e6b992SStephen Hemminger } 287146e6b992SStephen Hemminger 287246e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SEGS]) { 287346e6b992SStephen Hemminger u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]); 287446e6b992SStephen Hemminger 287514d7b812SJakub Kicinski if (max_segs > GSO_MAX_SEGS || max_segs > dev->tso_max_segs) { 287646e6b992SStephen Hemminger err = -EINVAL; 287746e6b992SStephen Hemminger goto errout; 287846e6b992SStephen Hemminger } 287946e6b992SStephen Hemminger 288046e6b992SStephen Hemminger if (dev->gso_max_segs ^ max_segs) { 28816d872df3SEric Dumazet netif_set_gso_max_segs(dev, max_segs); 288246e6b992SStephen Hemminger status |= DO_SETLINK_MODIFIED; 288346e6b992SStephen Hemminger } 288446e6b992SStephen Hemminger } 288546e6b992SStephen Hemminger 2886eac1b93cSCoco Li if (tb[IFLA_GRO_MAX_SIZE]) { 2887eac1b93cSCoco Li u32 gro_max_size = nla_get_u32(tb[IFLA_GRO_MAX_SIZE]); 2888eac1b93cSCoco Li 2889eac1b93cSCoco Li if (dev->gro_max_size ^ gro_max_size) { 2890eac1b93cSCoco Li netif_set_gro_max_size(dev, gro_max_size); 2891eac1b93cSCoco Li status |= DO_SETLINK_MODIFIED; 2892eac1b93cSCoco Li } 2893eac1b93cSCoco Li } 2894eac1b93cSCoco Li 28959eefedd5SXin Long if (tb[IFLA_GSO_IPV4_MAX_SIZE]) { 28969eefedd5SXin Long u32 max_size = nla_get_u32(tb[IFLA_GSO_IPV4_MAX_SIZE]); 28979eefedd5SXin Long 28989eefedd5SXin Long if (max_size > dev->tso_max_size) { 28999eefedd5SXin Long err = -EINVAL; 29009eefedd5SXin Long goto errout; 29019eefedd5SXin Long } 29029eefedd5SXin Long 29039eefedd5SXin Long if (dev->gso_ipv4_max_size ^ max_size) { 29049eefedd5SXin Long netif_set_gso_ipv4_max_size(dev, max_size); 29059eefedd5SXin Long status |= DO_SETLINK_MODIFIED; 29069eefedd5SXin Long } 29079eefedd5SXin Long } 29089eefedd5SXin Long 29099eefedd5SXin Long if (tb[IFLA_GRO_IPV4_MAX_SIZE]) { 29109eefedd5SXin Long u32 gro_max_size = nla_get_u32(tb[IFLA_GRO_IPV4_MAX_SIZE]); 29119eefedd5SXin Long 29129eefedd5SXin Long if (dev->gro_ipv4_max_size ^ gro_max_size) { 29139eefedd5SXin Long netif_set_gro_ipv4_max_size(dev, gro_max_size); 29149eefedd5SXin Long status |= DO_SETLINK_MODIFIED; 29159eefedd5SXin Long } 29169eefedd5SXin Long } 29179eefedd5SXin Long 29180157f60cSPatrick McHardy if (tb[IFLA_OPERSTATE]) 291993b2d4a2SDavid S. Miller set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 29200157f60cSPatrick McHardy 29210157f60cSPatrick McHardy if (tb[IFLA_LINKMODE]) { 29221889b0e7SNicolas Dichtel unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]); 29231889b0e7SNicolas Dichtel 2924fd888e85SSebastian Andrzej Siewior write_lock(&dev_base_lock); 29251889b0e7SNicolas Dichtel if (dev->link_mode ^ value) 2926ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 29271889b0e7SNicolas Dichtel dev->link_mode = value; 2928fd888e85SSebastian Andrzej Siewior write_unlock(&dev_base_lock); 29290157f60cSPatrick McHardy } 29300157f60cSPatrick McHardy 2931c02db8c6SChris Wright if (tb[IFLA_VFINFO_LIST]) { 29324f7d2cdfSDaniel Borkmann struct nlattr *vfinfo[IFLA_VF_MAX + 1]; 2933c02db8c6SChris Wright struct nlattr *attr; 2934c02db8c6SChris Wright int rem; 29354f7d2cdfSDaniel Borkmann 2936c02db8c6SChris Wright nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) { 29374f7d2cdfSDaniel Borkmann if (nla_type(attr) != IFLA_VF_INFO || 29384f7d2cdfSDaniel Borkmann nla_len(attr) < NLA_HDRLEN) { 2939253683bbSDavid Howells err = -EINVAL; 2940c02db8c6SChris Wright goto errout; 2941253683bbSDavid Howells } 29428cb08174SJohannes Berg err = nla_parse_nested_deprecated(vfinfo, IFLA_VF_MAX, 29438cb08174SJohannes Berg attr, 29448cb08174SJohannes Berg ifla_vf_policy, 29458cb08174SJohannes Berg NULL); 29464f7d2cdfSDaniel Borkmann if (err < 0) 29474f7d2cdfSDaniel Borkmann goto errout; 29484f7d2cdfSDaniel Borkmann err = do_setvfinfo(dev, vfinfo); 2949ebc08a6fSWilliams, Mitch A if (err < 0) 2950ebc08a6fSWilliams, Mitch A goto errout; 2951ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 2952ebc08a6fSWilliams, Mitch A } 2953ebc08a6fSWilliams, Mitch A } 29540157f60cSPatrick McHardy err = 0; 29550157f60cSPatrick McHardy 295657b61080SScott Feldman if (tb[IFLA_VF_PORTS]) { 295757b61080SScott Feldman struct nlattr *port[IFLA_PORT_MAX+1]; 295857b61080SScott Feldman struct nlattr *attr; 295957b61080SScott Feldman int vf; 296057b61080SScott Feldman int rem; 296157b61080SScott Feldman 296257b61080SScott Feldman err = -EOPNOTSUPP; 296357b61080SScott Feldman if (!ops->ndo_set_vf_port) 296457b61080SScott Feldman goto errout; 296557b61080SScott Feldman 296657b61080SScott Feldman nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) { 2967035d210fSDaniel Borkmann if (nla_type(attr) != IFLA_VF_PORT || 2968035d210fSDaniel Borkmann nla_len(attr) < NLA_HDRLEN) { 2969035d210fSDaniel Borkmann err = -EINVAL; 2970035d210fSDaniel Borkmann goto errout; 2971035d210fSDaniel Borkmann } 29728cb08174SJohannes Berg err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX, 29738cb08174SJohannes Berg attr, 29748cb08174SJohannes Berg ifla_port_policy, 29758cb08174SJohannes Berg NULL); 297657b61080SScott Feldman if (err < 0) 297757b61080SScott Feldman goto errout; 297857b61080SScott Feldman if (!port[IFLA_PORT_VF]) { 297957b61080SScott Feldman err = -EOPNOTSUPP; 298057b61080SScott Feldman goto errout; 298157b61080SScott Feldman } 298257b61080SScott Feldman vf = nla_get_u32(port[IFLA_PORT_VF]); 298357b61080SScott Feldman err = ops->ndo_set_vf_port(dev, vf, port); 298457b61080SScott Feldman if (err < 0) 298557b61080SScott Feldman goto errout; 2986ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 298757b61080SScott Feldman } 298857b61080SScott Feldman } 298957b61080SScott Feldman err = 0; 299057b61080SScott Feldman 299157b61080SScott Feldman if (tb[IFLA_PORT_SELF]) { 299257b61080SScott Feldman struct nlattr *port[IFLA_PORT_MAX+1]; 299357b61080SScott Feldman 29948cb08174SJohannes Berg err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX, 29958cb08174SJohannes Berg tb[IFLA_PORT_SELF], 29968cb08174SJohannes Berg ifla_port_policy, NULL); 299757b61080SScott Feldman if (err < 0) 299857b61080SScott Feldman goto errout; 299957b61080SScott Feldman 300057b61080SScott Feldman err = -EOPNOTSUPP; 300157b61080SScott Feldman if (ops->ndo_set_vf_port) 300257b61080SScott Feldman err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port); 300357b61080SScott Feldman if (err < 0) 300457b61080SScott Feldman goto errout; 3005ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 300657b61080SScott Feldman } 3007f8ff182cSThomas Graf 3008f8ff182cSThomas Graf if (tb[IFLA_AF_SPEC]) { 3009f8ff182cSThomas Graf struct nlattr *af; 3010f8ff182cSThomas Graf int rem; 3011f8ff182cSThomas Graf 3012f8ff182cSThomas Graf nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { 3013f8ff182cSThomas Graf const struct rtnl_af_ops *af_ops; 3014f8ff182cSThomas Graf 3015058c8d59SGustavo A. R. Silva BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af)))); 3016f8ff182cSThomas Graf 30173583a4e8SStephen Hemminger err = af_ops->set_link_af(dev, af, extack); 3018a100243dSCong Wang if (err < 0) 3019f8ff182cSThomas Graf goto errout; 3020f8ff182cSThomas Graf 3021ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 3022f8ff182cSThomas Graf } 3023f8ff182cSThomas Graf } 302457b61080SScott Feldman err = 0; 302557b61080SScott Feldman 3026829eb208SRoopa Prabhu if (tb[IFLA_PROTO_DOWN] || tb[IFLA_PROTO_DOWN_REASON]) { 3027829eb208SRoopa Prabhu err = do_set_proto_down(dev, tb[IFLA_PROTO_DOWN], 3028829eb208SRoopa Prabhu tb[IFLA_PROTO_DOWN_REASON], extack); 302988d6378bSAnuradha Karuppiah if (err) 303088d6378bSAnuradha Karuppiah goto errout; 303188d6378bSAnuradha Karuppiah status |= DO_SETLINK_NOTIFY; 303288d6378bSAnuradha Karuppiah } 303388d6378bSAnuradha Karuppiah 3034d1fdd913SBrenden Blanco if (tb[IFLA_XDP]) { 3035d1fdd913SBrenden Blanco struct nlattr *xdp[IFLA_XDP_MAX + 1]; 303685de8576SDaniel Borkmann u32 xdp_flags = 0; 3037d1fdd913SBrenden Blanco 30388cb08174SJohannes Berg err = nla_parse_nested_deprecated(xdp, IFLA_XDP_MAX, 30398cb08174SJohannes Berg tb[IFLA_XDP], 3040fceb6435SJohannes Berg ifla_xdp_policy, NULL); 3041d1fdd913SBrenden Blanco if (err < 0) 3042d1fdd913SBrenden Blanco goto errout; 3043d1fdd913SBrenden Blanco 304458038695SMartin KaFai Lau if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) { 3045262d8625SBrenden Blanco err = -EINVAL; 3046262d8625SBrenden Blanco goto errout; 3047262d8625SBrenden Blanco } 304885de8576SDaniel Borkmann 304985de8576SDaniel Borkmann if (xdp[IFLA_XDP_FLAGS]) { 305085de8576SDaniel Borkmann xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]); 305185de8576SDaniel Borkmann if (xdp_flags & ~XDP_FLAGS_MASK) { 305285de8576SDaniel Borkmann err = -EINVAL; 305385de8576SDaniel Borkmann goto errout; 305485de8576SDaniel Borkmann } 3055ee5d032fSJakub Kicinski if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) { 30560489df9aSDaniel Borkmann err = -EINVAL; 30570489df9aSDaniel Borkmann goto errout; 30580489df9aSDaniel Borkmann } 305985de8576SDaniel Borkmann } 306085de8576SDaniel Borkmann 3061d1fdd913SBrenden Blanco if (xdp[IFLA_XDP_FD]) { 306292234c8fSToke Høiland-Jørgensen int expected_fd = -1; 306392234c8fSToke Høiland-Jørgensen 306492234c8fSToke Høiland-Jørgensen if (xdp_flags & XDP_FLAGS_REPLACE) { 306592234c8fSToke Høiland-Jørgensen if (!xdp[IFLA_XDP_EXPECTED_FD]) { 306692234c8fSToke Høiland-Jørgensen err = -EINVAL; 306792234c8fSToke Høiland-Jørgensen goto errout; 306892234c8fSToke Høiland-Jørgensen } 306992234c8fSToke Høiland-Jørgensen expected_fd = 307092234c8fSToke Høiland-Jørgensen nla_get_s32(xdp[IFLA_XDP_EXPECTED_FD]); 307192234c8fSToke Høiland-Jørgensen } 307292234c8fSToke Høiland-Jørgensen 3073ddf9f970SJakub Kicinski err = dev_change_xdp_fd(dev, extack, 307485de8576SDaniel Borkmann nla_get_s32(xdp[IFLA_XDP_FD]), 307592234c8fSToke Høiland-Jørgensen expected_fd, 307685de8576SDaniel Borkmann xdp_flags); 3077d1fdd913SBrenden Blanco if (err) 3078d1fdd913SBrenden Blanco goto errout; 3079d1fdd913SBrenden Blanco status |= DO_SETLINK_NOTIFY; 3080d1fdd913SBrenden Blanco } 3081d1fdd913SBrenden Blanco } 3082d1fdd913SBrenden Blanco 30830157f60cSPatrick McHardy errout: 3084ba998906SNicolas Dichtel if (status & DO_SETLINK_MODIFIED) { 308564ff90ccSXin Long if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY) 3086ba998906SNicolas Dichtel netdev_state_change(dev); 3087ba998906SNicolas Dichtel 3088ba998906SNicolas Dichtel if (err < 0) 3089e87cc472SJoe 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", 3090e87cc472SJoe Perches dev->name); 3091ba998906SNicolas Dichtel } 30920157f60cSPatrick McHardy 30930157f60cSPatrick McHardy return err; 30940157f60cSPatrick McHardy } 30950157f60cSPatrick McHardy 3096cc6090e9SJiri Pirko static struct net_device *rtnl_dev_get(struct net *net, 30975ea08b52SFlorent Fourcot struct nlattr *tb[]) 3098cc6090e9SJiri Pirko { 30995ea08b52SFlorent Fourcot char ifname[ALTIFNAMSIZ]; 3100cc6090e9SJiri Pirko 31015ea08b52SFlorent Fourcot if (tb[IFLA_IFNAME]) 31025ea08b52SFlorent Fourcot nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 31035ea08b52SFlorent Fourcot else if (tb[IFLA_ALT_IFNAME]) 31045ea08b52SFlorent Fourcot nla_strscpy(ifname, tb[IFLA_ALT_IFNAME], ALTIFNAMSIZ); 3105cc6090e9SJiri Pirko else 3106cc6090e9SJiri Pirko return NULL; 3107cc6090e9SJiri Pirko 3108cc6090e9SJiri Pirko return __dev_get_by_name(net, ifname); 3109cc6090e9SJiri Pirko } 3110cc6090e9SJiri Pirko 3111c21ef3e3SDavid Ahern static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, 3112c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 3113da5e0494SThomas Graf { 31143b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 3115da5e0494SThomas Graf struct ifinfomsg *ifm; 3116da5e0494SThomas Graf struct net_device *dev; 31170157f60cSPatrick McHardy int err; 3118da5e0494SThomas Graf struct nlattr *tb[IFLA_MAX+1]; 31191da177e4SLinus Torvalds 31208cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX, 31218cb08174SJohannes Berg ifla_policy, extack); 3122da5e0494SThomas Graf if (err < 0) 3123da5e0494SThomas Graf goto errout; 31241da177e4SLinus Torvalds 31254ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, false); 31264ff66caeSChristian Brauner if (err < 0) 31274ff66caeSChristian Brauner goto errout; 31284ff66caeSChristian Brauner 31291da177e4SLinus Torvalds err = -EINVAL; 3130da5e0494SThomas Graf ifm = nlmsg_data(nlh); 313151055be8SPatrick McHardy if (ifm->ifi_index > 0) 3132a3d12891SEric Dumazet dev = __dev_get_by_index(net, ifm->ifi_index); 313376c9ac0eSJiri Pirko else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) 31345ea08b52SFlorent Fourcot dev = rtnl_dev_get(net, tb); 3135da5e0494SThomas Graf else 3136da5e0494SThomas Graf goto errout; 31371da177e4SLinus Torvalds 3138da5e0494SThomas Graf if (dev == NULL) { 3139da5e0494SThomas Graf err = -ENODEV; 3140da5e0494SThomas Graf goto errout; 3141da5e0494SThomas Graf } 31421da177e4SLinus Torvalds 31435ea08b52SFlorent Fourcot err = do_setlink(skb, dev, ifm, extack, tb, 0); 3144da5e0494SThomas Graf errout: 31451da177e4SLinus Torvalds return err; 31461da177e4SLinus Torvalds } 31471da177e4SLinus Torvalds 314866400d54SWANG Cong static int rtnl_group_dellink(const struct net *net, int group) 314966400d54SWANG Cong { 315066400d54SWANG Cong struct net_device *dev, *aux; 315166400d54SWANG Cong LIST_HEAD(list_kill); 315266400d54SWANG Cong bool found = false; 315366400d54SWANG Cong 315466400d54SWANG Cong if (!group) 315566400d54SWANG Cong return -EPERM; 315666400d54SWANG Cong 315766400d54SWANG Cong for_each_netdev(net, dev) { 315866400d54SWANG Cong if (dev->group == group) { 315966400d54SWANG Cong const struct rtnl_link_ops *ops; 316066400d54SWANG Cong 316166400d54SWANG Cong found = true; 316266400d54SWANG Cong ops = dev->rtnl_link_ops; 316366400d54SWANG Cong if (!ops || !ops->dellink) 316466400d54SWANG Cong return -EOPNOTSUPP; 316566400d54SWANG Cong } 316666400d54SWANG Cong } 316766400d54SWANG Cong 316866400d54SWANG Cong if (!found) 316966400d54SWANG Cong return -ENODEV; 317066400d54SWANG Cong 317166400d54SWANG Cong for_each_netdev_safe(net, dev, aux) { 317266400d54SWANG Cong if (dev->group == group) { 317366400d54SWANG Cong const struct rtnl_link_ops *ops; 317466400d54SWANG Cong 317566400d54SWANG Cong ops = dev->rtnl_link_ops; 317666400d54SWANG Cong ops->dellink(dev, &list_kill); 317766400d54SWANG Cong } 317866400d54SWANG Cong } 317966400d54SWANG Cong unregister_netdevice_many(&list_kill); 318066400d54SWANG Cong 318166400d54SWANG Cong return 0; 318266400d54SWANG Cong } 318366400d54SWANG Cong 3184f3a63cceSHangbin Liu int rtnl_delete_link(struct net_device *dev, u32 portid, const struct nlmsghdr *nlh) 3185614732eaSThomas Graf { 3186614732eaSThomas Graf const struct rtnl_link_ops *ops; 3187614732eaSThomas Graf LIST_HEAD(list_kill); 3188614732eaSThomas Graf 3189614732eaSThomas Graf ops = dev->rtnl_link_ops; 3190614732eaSThomas Graf if (!ops || !ops->dellink) 3191614732eaSThomas Graf return -EOPNOTSUPP; 3192614732eaSThomas Graf 3193614732eaSThomas Graf ops->dellink(dev, &list_kill); 3194f3a63cceSHangbin Liu unregister_netdevice_many_notify(&list_kill, portid, nlh); 3195614732eaSThomas Graf 3196614732eaSThomas Graf return 0; 3197614732eaSThomas Graf } 3198614732eaSThomas Graf EXPORT_SYMBOL_GPL(rtnl_delete_link); 3199614732eaSThomas Graf 3200c21ef3e3SDavid Ahern static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, 3201c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 320238f7b870SPatrick McHardy { 32033b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 3204f3a63cceSHangbin Liu u32 portid = NETLINK_CB(skb).portid; 3205b61ad68aSChristian Brauner struct net *tgt_net = net; 3206b61ad68aSChristian Brauner struct net_device *dev = NULL; 320738f7b870SPatrick McHardy struct ifinfomsg *ifm; 320838f7b870SPatrick McHardy struct nlattr *tb[IFLA_MAX+1]; 320938f7b870SPatrick McHardy int err; 3210b61ad68aSChristian Brauner int netnsid = -1; 321138f7b870SPatrick McHardy 32128cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX, 32138cb08174SJohannes Berg ifla_policy, extack); 321438f7b870SPatrick McHardy if (err < 0) 321538f7b870SPatrick McHardy return err; 321638f7b870SPatrick McHardy 32174ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, true); 32184ff66caeSChristian Brauner if (err < 0) 32194ff66caeSChristian Brauner return err; 32204ff66caeSChristian Brauner 32217e4a8d5aSChristian Brauner if (tb[IFLA_TARGET_NETNSID]) { 32227e4a8d5aSChristian Brauner netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]); 3223c383edc4SChristian Brauner tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid); 3224b61ad68aSChristian Brauner if (IS_ERR(tgt_net)) 3225b61ad68aSChristian Brauner return PTR_ERR(tgt_net); 3226b61ad68aSChristian Brauner } 3227b61ad68aSChristian Brauner 3228b61ad68aSChristian Brauner err = -EINVAL; 322938f7b870SPatrick McHardy ifm = nlmsg_data(nlh); 323038f7b870SPatrick McHardy if (ifm->ifi_index > 0) 3231b61ad68aSChristian Brauner dev = __dev_get_by_index(tgt_net, ifm->ifi_index); 323276c9ac0eSJiri Pirko else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) 32335ea08b52SFlorent Fourcot dev = rtnl_dev_get(net, tb); 323466400d54SWANG Cong else if (tb[IFLA_GROUP]) 3235b61ad68aSChristian Brauner err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP])); 323638f7b870SPatrick McHardy else 3237b61ad68aSChristian Brauner goto out; 323838f7b870SPatrick McHardy 3239b61ad68aSChristian Brauner if (!dev) { 3240dee04163SFlorent Fourcot if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME] || ifm->ifi_index > 0) 3241b61ad68aSChristian Brauner err = -ENODEV; 324238f7b870SPatrick McHardy 3243b61ad68aSChristian Brauner goto out; 3244b61ad68aSChristian Brauner } 3245b61ad68aSChristian Brauner 3246f3a63cceSHangbin Liu err = rtnl_delete_link(dev, portid, nlh); 3247b61ad68aSChristian Brauner 3248b61ad68aSChristian Brauner out: 3249b61ad68aSChristian Brauner if (netnsid >= 0) 3250b61ad68aSChristian Brauner put_net(tgt_net); 3251b61ad68aSChristian Brauner 3252b61ad68aSChristian Brauner return err; 325338f7b870SPatrick McHardy } 325438f7b870SPatrick McHardy 32551d997f10SHangbin Liu int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm, 32561d997f10SHangbin Liu u32 portid, const struct nlmsghdr *nlh) 32573729d502SPatrick McHardy { 32583729d502SPatrick McHardy unsigned int old_flags; 32593729d502SPatrick McHardy int err; 32603729d502SPatrick McHardy 32613729d502SPatrick McHardy old_flags = dev->flags; 32623729d502SPatrick McHardy if (ifm && (ifm->ifi_flags || ifm->ifi_change)) { 32636d040321SPetr Machata err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm), 32646d040321SPetr Machata NULL); 32653729d502SPatrick McHardy if (err < 0) 32663729d502SPatrick McHardy return err; 32673729d502SPatrick McHardy } 32683729d502SPatrick McHardy 32698d356b89SRoopa Prabhu if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) { 32701d997f10SHangbin Liu __dev_notify_flags(dev, old_flags, (old_flags ^ dev->flags), portid, nlh); 32718d356b89SRoopa Prabhu } else { 32723729d502SPatrick McHardy dev->rtnl_link_state = RTNL_LINK_INITIALIZED; 32731d997f10SHangbin Liu __dev_notify_flags(dev, old_flags, ~0U, portid, nlh); 32748d356b89SRoopa Prabhu } 32753729d502SPatrick McHardy return 0; 32763729d502SPatrick McHardy } 32773729d502SPatrick McHardy EXPORT_SYMBOL(rtnl_configure_link); 32783729d502SPatrick McHardy 3279d0522f1cSDavid Ahern struct net_device *rtnl_create_link(struct net *net, const char *ifname, 3280d0522f1cSDavid Ahern unsigned char name_assign_type, 3281d0522f1cSDavid Ahern const struct rtnl_link_ops *ops, 3282d0522f1cSDavid Ahern struct nlattr *tb[], 3283d0522f1cSDavid Ahern struct netlink_ext_ack *extack) 3284e7199288SPavel Emelianov { 3285e7199288SPavel Emelianov struct net_device *dev; 3286d40156aaSJiri Pirko unsigned int num_tx_queues = 1; 3287d40156aaSJiri Pirko unsigned int num_rx_queues = 1; 3288e7199288SPavel Emelianov 328976ff5cc9SJiri Pirko if (tb[IFLA_NUM_TX_QUEUES]) 329076ff5cc9SJiri Pirko num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]); 329176ff5cc9SJiri Pirko else if (ops->get_num_tx_queues) 3292d40156aaSJiri Pirko num_tx_queues = ops->get_num_tx_queues(); 329376ff5cc9SJiri Pirko 329476ff5cc9SJiri Pirko if (tb[IFLA_NUM_RX_QUEUES]) 329576ff5cc9SJiri Pirko num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]); 329676ff5cc9SJiri Pirko else if (ops->get_num_rx_queues) 3297d40156aaSJiri Pirko num_rx_queues = ops->get_num_rx_queues(); 3298efacb309Sstephen hemminger 3299d0522f1cSDavid Ahern if (num_tx_queues < 1 || num_tx_queues > 4096) { 3300d0522f1cSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid number of transmit queues"); 33010e1d6ecaSEric Dumazet return ERR_PTR(-EINVAL); 3302d0522f1cSDavid Ahern } 33030e1d6ecaSEric Dumazet 3304d0522f1cSDavid Ahern if (num_rx_queues < 1 || num_rx_queues > 4096) { 3305d0522f1cSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid number of receive queues"); 33060e1d6ecaSEric Dumazet return ERR_PTR(-EINVAL); 3307d0522f1cSDavid Ahern } 33080e1d6ecaSEric Dumazet 33098c713dc9SJohannes Berg if (ops->alloc) { 33108c713dc9SJohannes Berg dev = ops->alloc(tb, ifname, name_assign_type, 33118c713dc9SJohannes Berg num_tx_queues, num_rx_queues); 33128c713dc9SJohannes Berg if (IS_ERR(dev)) 33138c713dc9SJohannes Berg return dev; 33148c713dc9SJohannes Berg } else { 33158c713dc9SJohannes Berg dev = alloc_netdev_mqs(ops->priv_size, ifname, 33168c713dc9SJohannes Berg name_assign_type, ops->setup, 33178c713dc9SJohannes Berg num_tx_queues, num_rx_queues); 33188c713dc9SJohannes Berg } 33198c713dc9SJohannes Berg 3320e7199288SPavel Emelianov if (!dev) 3321d1892e4eSTobias Klauser return ERR_PTR(-ENOMEM); 3322e7199288SPavel Emelianov 332381adee47SEric W. Biederman dev_net_set(dev, net); 332481adee47SEric W. Biederman dev->rtnl_link_ops = ops; 33253729d502SPatrick McHardy dev->rtnl_link_state = RTNL_LINK_INITIALIZING; 332681adee47SEric W. Biederman 3327d836f5c6SEric Dumazet if (tb[IFLA_MTU]) { 3328d836f5c6SEric Dumazet u32 mtu = nla_get_u32(tb[IFLA_MTU]); 3329d836f5c6SEric Dumazet int err; 3330d836f5c6SEric Dumazet 3331d836f5c6SEric Dumazet err = dev_validate_mtu(dev, mtu, extack); 3332d836f5c6SEric Dumazet if (err) { 3333d836f5c6SEric Dumazet free_netdev(dev); 3334d836f5c6SEric Dumazet return ERR_PTR(err); 3335d836f5c6SEric Dumazet } 3336d836f5c6SEric Dumazet dev->mtu = mtu; 3337d836f5c6SEric Dumazet } 33382afb9b53SJiri Pirko if (tb[IFLA_ADDRESS]) { 3339efd38f75SJakub Kicinski __dev_addr_set(dev, nla_data(tb[IFLA_ADDRESS]), 3340e7199288SPavel Emelianov nla_len(tb[IFLA_ADDRESS])); 33412afb9b53SJiri Pirko dev->addr_assign_type = NET_ADDR_SET; 33422afb9b53SJiri Pirko } 3343e7199288SPavel Emelianov if (tb[IFLA_BROADCAST]) 3344e7199288SPavel Emelianov memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]), 3345e7199288SPavel Emelianov nla_len(tb[IFLA_BROADCAST])); 3346e7199288SPavel Emelianov if (tb[IFLA_TXQLEN]) 3347e7199288SPavel Emelianov dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); 3348e7199288SPavel Emelianov if (tb[IFLA_OPERSTATE]) 334993b2d4a2SDavid S. Miller set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 3350e7199288SPavel Emelianov if (tb[IFLA_LINKMODE]) 3351e7199288SPavel Emelianov dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); 3352ffa934f1SPatrick McHardy if (tb[IFLA_GROUP]) 3353ffa934f1SPatrick McHardy dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); 335446e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SIZE]) 335546e6b992SStephen Hemminger netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE])); 335646e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SEGS]) 33576d872df3SEric Dumazet netif_set_gso_max_segs(dev, nla_get_u32(tb[IFLA_GSO_MAX_SEGS])); 3358eac1b93cSCoco Li if (tb[IFLA_GRO_MAX_SIZE]) 3359eac1b93cSCoco Li netif_set_gro_max_size(dev, nla_get_u32(tb[IFLA_GRO_MAX_SIZE])); 33609eefedd5SXin Long if (tb[IFLA_GSO_IPV4_MAX_SIZE]) 33619eefedd5SXin Long netif_set_gso_ipv4_max_size(dev, nla_get_u32(tb[IFLA_GSO_IPV4_MAX_SIZE])); 33629eefedd5SXin Long if (tb[IFLA_GRO_IPV4_MAX_SIZE]) 33639eefedd5SXin Long netif_set_gro_ipv4_max_size(dev, nla_get_u32(tb[IFLA_GRO_IPV4_MAX_SIZE])); 3364e7199288SPavel Emelianov 3365e7199288SPavel Emelianov return dev; 3366e7199288SPavel Emelianov } 3367e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_create_link); 3368e7199288SPavel Emelianov 336990f62cf3SEric W. Biederman static int rtnl_group_changelink(const struct sk_buff *skb, 337090f62cf3SEric W. Biederman struct net *net, int group, 3371e7ed828fSVlad Dogaru struct ifinfomsg *ifm, 3372ddf9f970SJakub Kicinski struct netlink_ext_ack *extack, 3373e7ed828fSVlad Dogaru struct nlattr **tb) 3374e7ed828fSVlad Dogaru { 3375d079535dSWANG Cong struct net_device *dev, *aux; 3376e7ed828fSVlad Dogaru int err; 3377e7ed828fSVlad Dogaru 3378d079535dSWANG Cong for_each_netdev_safe(net, dev, aux) { 3379e7ed828fSVlad Dogaru if (dev->group == group) { 33805ea08b52SFlorent Fourcot err = do_setlink(skb, dev, ifm, extack, tb, 0); 3381e7ed828fSVlad Dogaru if (err < 0) 3382e7ed828fSVlad Dogaru return err; 3383e7ed828fSVlad Dogaru } 3384e7ed828fSVlad Dogaru } 3385e7ed828fSVlad Dogaru 3386e7ed828fSVlad Dogaru return 0; 3387e7ed828fSVlad Dogaru } 3388e7ed828fSVlad Dogaru 338963105e83SJakub Kicinski static int rtnl_newlink_create(struct sk_buff *skb, struct ifinfomsg *ifm, 339063105e83SJakub Kicinski const struct rtnl_link_ops *ops, 3391d88e136cSHangbin Liu const struct nlmsghdr *nlh, 339263105e83SJakub Kicinski struct nlattr **tb, struct nlattr **data, 339302839cc8SJakub Kicinski struct netlink_ext_ack *extack) 339402839cc8SJakub Kicinski { 339502839cc8SJakub Kicinski unsigned char name_assign_type = NET_NAME_USER; 339602839cc8SJakub Kicinski struct net *net = sock_net(skb->sk); 3397d88e136cSHangbin Liu u32 portid = NETLINK_CB(skb).portid; 339802839cc8SJakub Kicinski struct net *dest_net, *link_net; 339902839cc8SJakub Kicinski struct net_device *dev; 340002839cc8SJakub Kicinski char ifname[IFNAMSIZ]; 340102839cc8SJakub Kicinski int err; 340202839cc8SJakub Kicinski 340302839cc8SJakub Kicinski if (!ops->alloc && !ops->setup) 340402839cc8SJakub Kicinski return -EOPNOTSUPP; 340502839cc8SJakub Kicinski 340602839cc8SJakub Kicinski if (tb[IFLA_IFNAME]) { 340702839cc8SJakub Kicinski nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 340802839cc8SJakub Kicinski } else { 340902839cc8SJakub Kicinski snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind); 341002839cc8SJakub Kicinski name_assign_type = NET_NAME_ENUM; 341102839cc8SJakub Kicinski } 341202839cc8SJakub Kicinski 341302839cc8SJakub Kicinski dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN); 341402839cc8SJakub Kicinski if (IS_ERR(dest_net)) 341502839cc8SJakub Kicinski return PTR_ERR(dest_net); 341602839cc8SJakub Kicinski 341702839cc8SJakub Kicinski if (tb[IFLA_LINK_NETNSID]) { 341802839cc8SJakub Kicinski int id = nla_get_s32(tb[IFLA_LINK_NETNSID]); 341902839cc8SJakub Kicinski 342002839cc8SJakub Kicinski link_net = get_net_ns_by_id(dest_net, id); 342102839cc8SJakub Kicinski if (!link_net) { 342202839cc8SJakub Kicinski NL_SET_ERR_MSG(extack, "Unknown network namespace id"); 342302839cc8SJakub Kicinski err = -EINVAL; 342402839cc8SJakub Kicinski goto out; 342502839cc8SJakub Kicinski } 342602839cc8SJakub Kicinski err = -EPERM; 342702839cc8SJakub Kicinski if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN)) 342802839cc8SJakub Kicinski goto out; 342902839cc8SJakub Kicinski } else { 343002839cc8SJakub Kicinski link_net = NULL; 343102839cc8SJakub Kicinski } 343202839cc8SJakub Kicinski 343302839cc8SJakub Kicinski dev = rtnl_create_link(link_net ? : dest_net, ifname, 343402839cc8SJakub Kicinski name_assign_type, ops, tb, extack); 343502839cc8SJakub Kicinski if (IS_ERR(dev)) { 343602839cc8SJakub Kicinski err = PTR_ERR(dev); 343702839cc8SJakub Kicinski goto out; 343802839cc8SJakub Kicinski } 343902839cc8SJakub Kicinski 344002839cc8SJakub Kicinski dev->ifindex = ifm->ifi_index; 344102839cc8SJakub Kicinski 344202839cc8SJakub Kicinski if (ops->newlink) 344302839cc8SJakub Kicinski err = ops->newlink(link_net ? : net, dev, tb, data, extack); 344402839cc8SJakub Kicinski else 344502839cc8SJakub Kicinski err = register_netdevice(dev); 344602839cc8SJakub Kicinski if (err < 0) { 344702839cc8SJakub Kicinski free_netdev(dev); 344802839cc8SJakub Kicinski goto out; 344902839cc8SJakub Kicinski } 345002839cc8SJakub Kicinski 3451d88e136cSHangbin Liu err = rtnl_configure_link(dev, ifm, portid, nlh); 345202839cc8SJakub Kicinski if (err < 0) 345302839cc8SJakub Kicinski goto out_unregister; 345402839cc8SJakub Kicinski if (link_net) { 345502839cc8SJakub Kicinski err = dev_change_net_namespace(dev, dest_net, ifname); 345602839cc8SJakub Kicinski if (err < 0) 345702839cc8SJakub Kicinski goto out_unregister; 345802839cc8SJakub Kicinski } 345902839cc8SJakub Kicinski if (tb[IFLA_MASTER]) { 346002839cc8SJakub Kicinski err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack); 346102839cc8SJakub Kicinski if (err) 346202839cc8SJakub Kicinski goto out_unregister; 346302839cc8SJakub Kicinski } 346402839cc8SJakub Kicinski out: 346502839cc8SJakub Kicinski if (link_net) 346602839cc8SJakub Kicinski put_net(link_net); 346702839cc8SJakub Kicinski put_net(dest_net); 346802839cc8SJakub Kicinski return err; 346902839cc8SJakub Kicinski out_unregister: 347002839cc8SJakub Kicinski if (ops->newlink) { 347102839cc8SJakub Kicinski LIST_HEAD(list_kill); 347202839cc8SJakub Kicinski 347302839cc8SJakub Kicinski ops->dellink(dev, &list_kill); 347402839cc8SJakub Kicinski unregister_netdevice_many(&list_kill); 347502839cc8SJakub Kicinski } else { 347602839cc8SJakub Kicinski unregister_netdevice(dev); 347702839cc8SJakub Kicinski } 347802839cc8SJakub Kicinski goto out; 347902839cc8SJakub Kicinski } 348063105e83SJakub Kicinski 3481c92bf26cSJakub Kicinski struct rtnl_newlink_tbs { 3482c92bf26cSJakub Kicinski struct nlattr *tb[IFLA_MAX + 1]; 3483c92bf26cSJakub Kicinski struct nlattr *attr[RTNL_MAX_TYPE + 1]; 3484420d0318SJakub Kicinski struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1]; 3485c92bf26cSJakub Kicinski }; 3486c92bf26cSJakub Kicinski 3487c92bf26cSJakub Kicinski static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, 3488c92bf26cSJakub Kicinski struct rtnl_newlink_tbs *tbs, 3489c92bf26cSJakub Kicinski struct netlink_ext_ack *extack) 3490c92bf26cSJakub Kicinski { 3491420d0318SJakub Kicinski struct nlattr *linkinfo[IFLA_INFO_MAX + 1]; 3492c92bf26cSJakub Kicinski struct nlattr ** const tb = tbs->tb; 3493c6f6f244SEric Dumazet const struct rtnl_link_ops *m_ops; 3494c6f6f244SEric Dumazet struct net_device *master_dev; 34953b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 349638f7b870SPatrick McHardy const struct rtnl_link_ops *ops; 3497420d0318SJakub Kicinski struct nlattr **slave_data; 3498420d0318SJakub Kicinski char kind[MODULE_NAME_LEN]; 3499420d0318SJakub Kicinski struct net_device *dev; 3500420d0318SJakub Kicinski struct ifinfomsg *ifm; 3501420d0318SJakub Kicinski struct nlattr **data; 3502ef2a7c90SFlorent Fourcot bool link_specified; 350338f7b870SPatrick McHardy int err; 350438f7b870SPatrick McHardy 350595a5afcaSJohannes Berg #ifdef CONFIG_MODULES 350638f7b870SPatrick McHardy replay: 35078072f085SThomas Graf #endif 35088cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX, 35098cb08174SJohannes Berg ifla_policy, extack); 351038f7b870SPatrick McHardy if (err < 0) 351138f7b870SPatrick McHardy return err; 351238f7b870SPatrick McHardy 35134ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, false); 35144ff66caeSChristian Brauner if (err < 0) 35154ff66caeSChristian Brauner return err; 35164ff66caeSChristian Brauner 351738f7b870SPatrick McHardy ifm = nlmsg_data(nlh); 3518ef2a7c90SFlorent Fourcot if (ifm->ifi_index > 0) { 3519ef2a7c90SFlorent Fourcot link_specified = true; 3520881d966bSEric W. Biederman dev = __dev_get_by_index(net, ifm->ifi_index); 3521ef2a7c90SFlorent Fourcot } else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) { 3522ef2a7c90SFlorent Fourcot link_specified = true; 35235ea08b52SFlorent Fourcot dev = rtnl_dev_get(net, tb); 3524ef2a7c90SFlorent Fourcot } else { 3525ef2a7c90SFlorent Fourcot link_specified = false; 352638f7b870SPatrick McHardy dev = NULL; 3527ef2a7c90SFlorent Fourcot } 352838f7b870SPatrick McHardy 3529c6f6f244SEric Dumazet master_dev = NULL; 3530c6f6f244SEric Dumazet m_ops = NULL; 3531ba7d49b1SJiri Pirko if (dev) { 3532ba7d49b1SJiri Pirko master_dev = netdev_master_upper_dev_get(dev); 3533ba7d49b1SJiri Pirko if (master_dev) 3534ba7d49b1SJiri Pirko m_ops = master_dev->rtnl_link_ops; 3535ba7d49b1SJiri Pirko } 3536ba7d49b1SJiri Pirko 35378679c31eSRocco Yue err = validate_linkmsg(dev, tb, extack); 3538e0d087afSEric Dumazet if (err < 0) 35391840bb13SThomas Graf return err; 35401840bb13SThomas Graf 354138f7b870SPatrick McHardy if (tb[IFLA_LINKINFO]) { 35428cb08174SJohannes Berg err = nla_parse_nested_deprecated(linkinfo, IFLA_INFO_MAX, 35438cb08174SJohannes Berg tb[IFLA_LINKINFO], 35448cb08174SJohannes Berg ifla_info_policy, NULL); 354538f7b870SPatrick McHardy if (err < 0) 354638f7b870SPatrick McHardy return err; 354738f7b870SPatrick McHardy } else 354838f7b870SPatrick McHardy memset(linkinfo, 0, sizeof(linkinfo)); 354938f7b870SPatrick McHardy 355038f7b870SPatrick McHardy if (linkinfo[IFLA_INFO_KIND]) { 3551872f6903SFrancis Laniel nla_strscpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind)); 355238f7b870SPatrick McHardy ops = rtnl_link_ops_get(kind); 355338f7b870SPatrick McHardy } else { 355438f7b870SPatrick McHardy kind[0] = '\0'; 355538f7b870SPatrick McHardy ops = NULL; 355638f7b870SPatrick McHardy } 355738f7b870SPatrick McHardy 3558420d0318SJakub Kicinski data = NULL; 355938f7b870SPatrick McHardy if (ops) { 3560ccf8dbcdSKees Cook if (ops->maxtype > RTNL_MAX_TYPE) 3561ccf8dbcdSKees Cook return -EINVAL; 3562ccf8dbcdSKees Cook 356338f7b870SPatrick McHardy if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) { 3564c92bf26cSJakub Kicinski err = nla_parse_nested_deprecated(tbs->attr, ops->maxtype, 356538f7b870SPatrick McHardy linkinfo[IFLA_INFO_DATA], 356668d57f3bSDavid Ahern ops->policy, extack); 356738f7b870SPatrick McHardy if (err < 0) 356838f7b870SPatrick McHardy return err; 3569c92bf26cSJakub Kicinski data = tbs->attr; 357038f7b870SPatrick McHardy } 357138f7b870SPatrick McHardy if (ops->validate) { 3572a8b8a889SMatthias Schiffer err = ops->validate(tb, data, extack); 357338f7b870SPatrick McHardy if (err < 0) 357438f7b870SPatrick McHardy return err; 357538f7b870SPatrick McHardy } 357638f7b870SPatrick McHardy } 357738f7b870SPatrick McHardy 3578420d0318SJakub Kicinski slave_data = NULL; 3579ba7d49b1SJiri Pirko if (m_ops) { 3580af066ed3SPrashant Bhole if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE) 3581ccf8dbcdSKees Cook return -EINVAL; 3582ccf8dbcdSKees Cook 3583ba7d49b1SJiri Pirko if (m_ops->slave_maxtype && 3584ba7d49b1SJiri Pirko linkinfo[IFLA_INFO_SLAVE_DATA]) { 3585c92bf26cSJakub Kicinski err = nla_parse_nested_deprecated(tbs->slave_attr, 35868cb08174SJohannes Berg m_ops->slave_maxtype, 3587ba7d49b1SJiri Pirko linkinfo[IFLA_INFO_SLAVE_DATA], 35888cb08174SJohannes Berg m_ops->slave_policy, 35898cb08174SJohannes Berg extack); 3590ba7d49b1SJiri Pirko if (err < 0) 3591ba7d49b1SJiri Pirko return err; 3592c92bf26cSJakub Kicinski slave_data = tbs->slave_attr; 3593ba7d49b1SJiri Pirko } 3594ba7d49b1SJiri Pirko } 3595ba7d49b1SJiri Pirko 359638f7b870SPatrick McHardy if (dev) { 359790c325e3SNicolas Dichtel int status = 0; 359838f7b870SPatrick McHardy 359938f7b870SPatrick McHardy if (nlh->nlmsg_flags & NLM_F_EXCL) 360038f7b870SPatrick McHardy return -EEXIST; 360138f7b870SPatrick McHardy if (nlh->nlmsg_flags & NLM_F_REPLACE) 360238f7b870SPatrick McHardy return -EOPNOTSUPP; 360338f7b870SPatrick McHardy 360438f7b870SPatrick McHardy if (linkinfo[IFLA_INFO_DATA]) { 360538f7b870SPatrick McHardy if (!ops || ops != dev->rtnl_link_ops || 360638f7b870SPatrick McHardy !ops->changelink) 360738f7b870SPatrick McHardy return -EOPNOTSUPP; 360838f7b870SPatrick McHardy 3609ad744b22SMatthias Schiffer err = ops->changelink(dev, tb, data, extack); 361038f7b870SPatrick McHardy if (err < 0) 361138f7b870SPatrick McHardy return err; 3612ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 361338f7b870SPatrick McHardy } 361438f7b870SPatrick McHardy 3615ba7d49b1SJiri Pirko if (linkinfo[IFLA_INFO_SLAVE_DATA]) { 3616ba7d49b1SJiri Pirko if (!m_ops || !m_ops->slave_changelink) 3617ba7d49b1SJiri Pirko return -EOPNOTSUPP; 3618ba7d49b1SJiri Pirko 3619420d0318SJakub Kicinski err = m_ops->slave_changelink(master_dev, dev, tb, 3620420d0318SJakub Kicinski slave_data, extack); 3621ba7d49b1SJiri Pirko if (err < 0) 3622ba7d49b1SJiri Pirko return err; 3623ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 3624ba7d49b1SJiri Pirko } 3625ba7d49b1SJiri Pirko 36265ea08b52SFlorent Fourcot return do_setlink(skb, dev, ifm, extack, tb, status); 362738f7b870SPatrick McHardy } 362838f7b870SPatrick McHardy 3629ffa934f1SPatrick McHardy if (!(nlh->nlmsg_flags & NLM_F_CREATE)) { 3630ef2a7c90SFlorent Fourcot /* No dev found and NLM_F_CREATE not set. Requested dev does not exist, 3631ef2a7c90SFlorent Fourcot * or it's for a group 3632ef2a7c90SFlorent Fourcot */ 3633ef2a7c90SFlorent Fourcot if (link_specified) 3634ef2a7c90SFlorent Fourcot return -ENODEV; 3635ef2a7c90SFlorent Fourcot if (tb[IFLA_GROUP]) 363690f62cf3SEric W. Biederman return rtnl_group_changelink(skb, net, 3637ffa934f1SPatrick McHardy nla_get_u32(tb[IFLA_GROUP]), 3638ddf9f970SJakub Kicinski ifm, extack, tb); 36396f37c9f9SFlorent Fourcot return -ENODEV; 3640ffa934f1SPatrick McHardy } 364138f7b870SPatrick McHardy 3642160ca014STheuns Verwoerd if (tb[IFLA_MAP] || tb[IFLA_PROTINFO]) 364338f7b870SPatrick McHardy return -EOPNOTSUPP; 364438f7b870SPatrick McHardy 364538f7b870SPatrick McHardy if (!ops) { 364695a5afcaSJohannes Berg #ifdef CONFIG_MODULES 364738f7b870SPatrick McHardy if (kind[0]) { 364838f7b870SPatrick McHardy __rtnl_unlock(); 364938f7b870SPatrick McHardy request_module("rtnl-link-%s", kind); 365038f7b870SPatrick McHardy rtnl_lock(); 365138f7b870SPatrick McHardy ops = rtnl_link_ops_get(kind); 365238f7b870SPatrick McHardy if (ops) 365338f7b870SPatrick McHardy goto replay; 365438f7b870SPatrick McHardy } 365538f7b870SPatrick McHardy #endif 365668d57f3bSDavid Ahern NL_SET_ERR_MSG(extack, "Unknown device type"); 365738f7b870SPatrick McHardy return -EOPNOTSUPP; 365838f7b870SPatrick McHardy } 365938f7b870SPatrick McHardy 3660d88e136cSHangbin Liu return rtnl_newlink_create(skb, ifm, ops, nlh, tb, data, extack); 366163105e83SJakub Kicinski } 366263105e83SJakub Kicinski 3663a2939745SJakub Kicinski static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, 3664a2939745SJakub Kicinski struct netlink_ext_ack *extack) 3665a2939745SJakub Kicinski { 3666c92bf26cSJakub Kicinski struct rtnl_newlink_tbs *tbs; 3667a2939745SJakub Kicinski int ret; 3668a2939745SJakub Kicinski 3669c92bf26cSJakub Kicinski tbs = kmalloc(sizeof(*tbs), GFP_KERNEL); 3670c92bf26cSJakub Kicinski if (!tbs) 3671a2939745SJakub Kicinski return -ENOMEM; 3672a2939745SJakub Kicinski 3673c92bf26cSJakub Kicinski ret = __rtnl_newlink(skb, nlh, tbs, extack); 3674c92bf26cSJakub Kicinski kfree(tbs); 3675a2939745SJakub Kicinski return ret; 3676a2939745SJakub Kicinski } 3677a2939745SJakub Kicinski 36789b3757b0SJakub Kicinski static int rtnl_valid_getlink_req(struct sk_buff *skb, 36799b3757b0SJakub Kicinski const struct nlmsghdr *nlh, 36809b3757b0SJakub Kicinski struct nlattr **tb, 36819b3757b0SJakub Kicinski struct netlink_ext_ack *extack) 36829b3757b0SJakub Kicinski { 36839b3757b0SJakub Kicinski struct ifinfomsg *ifm; 36849b3757b0SJakub Kicinski int i, err; 36859b3757b0SJakub Kicinski 36869b3757b0SJakub Kicinski if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { 36879b3757b0SJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid header for get link"); 36889b3757b0SJakub Kicinski return -EINVAL; 36899b3757b0SJakub Kicinski } 36909b3757b0SJakub Kicinski 36919b3757b0SJakub Kicinski if (!netlink_strict_get_check(skb)) 36928cb08174SJohannes Berg return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX, 36938cb08174SJohannes Berg ifla_policy, extack); 36949b3757b0SJakub Kicinski 36959b3757b0SJakub Kicinski ifm = nlmsg_data(nlh); 36969b3757b0SJakub Kicinski if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags || 36979b3757b0SJakub Kicinski ifm->ifi_change) { 36989b3757b0SJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid values in header for get link request"); 36999b3757b0SJakub Kicinski return -EINVAL; 37009b3757b0SJakub Kicinski } 37019b3757b0SJakub Kicinski 37028cb08174SJohannes Berg err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFLA_MAX, 37038cb08174SJohannes Berg ifla_policy, extack); 37049b3757b0SJakub Kicinski if (err) 37059b3757b0SJakub Kicinski return err; 37069b3757b0SJakub Kicinski 37079b3757b0SJakub Kicinski for (i = 0; i <= IFLA_MAX; i++) { 37089b3757b0SJakub Kicinski if (!tb[i]) 37099b3757b0SJakub Kicinski continue; 37109b3757b0SJakub Kicinski 37119b3757b0SJakub Kicinski switch (i) { 37129b3757b0SJakub Kicinski case IFLA_IFNAME: 371376c9ac0eSJiri Pirko case IFLA_ALT_IFNAME: 37149b3757b0SJakub Kicinski case IFLA_EXT_MASK: 37159b3757b0SJakub Kicinski case IFLA_TARGET_NETNSID: 37169b3757b0SJakub Kicinski break; 37179b3757b0SJakub Kicinski default: 37189b3757b0SJakub Kicinski NL_SET_ERR_MSG(extack, "Unsupported attribute in get link request"); 37199b3757b0SJakub Kicinski return -EINVAL; 37209b3757b0SJakub Kicinski } 37219b3757b0SJakub Kicinski } 37229b3757b0SJakub Kicinski 37239b3757b0SJakub Kicinski return 0; 37249b3757b0SJakub Kicinski } 37259b3757b0SJakub Kicinski 3726c21ef3e3SDavid Ahern static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh, 3727c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 3728711e2c33SJean Tourrilhes { 37293b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 373079e1ad14SJiri Benc struct net *tgt_net = net; 3731b60c5115SThomas Graf struct ifinfomsg *ifm; 3732b60c5115SThomas Graf struct nlattr *tb[IFLA_MAX+1]; 3733b60c5115SThomas Graf struct net_device *dev = NULL; 3734b60c5115SThomas Graf struct sk_buff *nskb; 373579e1ad14SJiri Benc int netnsid = -1; 3736339bf98fSThomas Graf int err; 3737115c9b81SGreg Rose u32 ext_filter_mask = 0; 3738711e2c33SJean Tourrilhes 37399b3757b0SJakub Kicinski err = rtnl_valid_getlink_req(skb, nlh, tb, extack); 3740b60c5115SThomas Graf if (err < 0) 37419918f230SEric Sesterhenn return err; 3742b60c5115SThomas Graf 37434ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, true); 37444ff66caeSChristian Brauner if (err < 0) 37454ff66caeSChristian Brauner return err; 37464ff66caeSChristian Brauner 37477e4a8d5aSChristian Brauner if (tb[IFLA_TARGET_NETNSID]) { 37487e4a8d5aSChristian Brauner netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]); 3749c383edc4SChristian Brauner tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid); 375079e1ad14SJiri Benc if (IS_ERR(tgt_net)) 375179e1ad14SJiri Benc return PTR_ERR(tgt_net); 375279e1ad14SJiri Benc } 375379e1ad14SJiri Benc 3754115c9b81SGreg Rose if (tb[IFLA_EXT_MASK]) 3755115c9b81SGreg Rose ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 3756115c9b81SGreg Rose 375779e1ad14SJiri Benc err = -EINVAL; 3758b60c5115SThomas Graf ifm = nlmsg_data(nlh); 3759a3d12891SEric Dumazet if (ifm->ifi_index > 0) 376079e1ad14SJiri Benc dev = __dev_get_by_index(tgt_net, ifm->ifi_index); 376176c9ac0eSJiri Pirko else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) 37625ea08b52SFlorent Fourcot dev = rtnl_dev_get(tgt_net, tb); 3763a3d12891SEric Dumazet else 376479e1ad14SJiri Benc goto out; 3765b60c5115SThomas Graf 376679e1ad14SJiri Benc err = -ENODEV; 3767a3d12891SEric Dumazet if (dev == NULL) 376879e1ad14SJiri Benc goto out; 3769a3d12891SEric Dumazet 377079e1ad14SJiri Benc err = -ENOBUFS; 3771115c9b81SGreg Rose nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL); 3772a3d12891SEric Dumazet if (nskb == NULL) 377379e1ad14SJiri Benc goto out; 3774711e2c33SJean Tourrilhes 377579e1ad14SJiri Benc err = rtnl_fill_ifinfo(nskb, dev, net, 377679e1ad14SJiri Benc RTM_NEWLINK, NETLINK_CB(skb).portid, 377779e1ad14SJiri Benc nlh->nlmsg_seq, 0, 0, ext_filter_mask, 3778d4e4fdf9SGuillaume Nault 0, NULL, 0, netnsid, GFP_KERNEL); 377926932566SPatrick McHardy if (err < 0) { 378026932566SPatrick McHardy /* -EMSGSIZE implies BUG in if_nlmsg_size */ 378126932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 378226932566SPatrick McHardy kfree_skb(nskb); 3783a3d12891SEric Dumazet } else 378415e47304SEric W. Biederman err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); 378579e1ad14SJiri Benc out: 378679e1ad14SJiri Benc if (netnsid >= 0) 378779e1ad14SJiri Benc put_net(tgt_net); 3788b60c5115SThomas Graf 3789711e2c33SJean Tourrilhes return err; 3790711e2c33SJean Tourrilhes } 3791711e2c33SJean Tourrilhes 379236fbf1e5SJiri Pirko static int rtnl_alt_ifname(int cmd, struct net_device *dev, struct nlattr *attr, 379336fbf1e5SJiri Pirko bool *changed, struct netlink_ext_ack *extack) 379436fbf1e5SJiri Pirko { 379536fbf1e5SJiri Pirko char *alt_ifname; 3796155fb43bSJakub Kicinski size_t size; 379736fbf1e5SJiri Pirko int err; 379836fbf1e5SJiri Pirko 379936fbf1e5SJiri Pirko err = nla_validate(attr, attr->nla_len, IFLA_MAX, ifla_policy, extack); 380036fbf1e5SJiri Pirko if (err) 380136fbf1e5SJiri Pirko return err; 380236fbf1e5SJiri Pirko 3803155fb43bSJakub Kicinski if (cmd == RTM_NEWLINKPROP) { 3804155fb43bSJakub Kicinski size = rtnl_prop_list_size(dev); 3805155fb43bSJakub Kicinski size += nla_total_size(ALTIFNAMSIZ); 3806155fb43bSJakub Kicinski if (size >= U16_MAX) { 3807155fb43bSJakub Kicinski NL_SET_ERR_MSG(extack, 3808155fb43bSJakub Kicinski "effective property list too long"); 3809155fb43bSJakub Kicinski return -EINVAL; 3810155fb43bSJakub Kicinski } 3811155fb43bSJakub Kicinski } 3812155fb43bSJakub Kicinski 38135d26cff5SJakub Kicinski alt_ifname = nla_strdup(attr, GFP_KERNEL_ACCOUNT); 381436fbf1e5SJiri Pirko if (!alt_ifname) 381536fbf1e5SJiri Pirko return -ENOMEM; 381644bfa9c5SEric Dumazet 381744bfa9c5SEric Dumazet if (cmd == RTM_NEWLINKPROP) { 381836fbf1e5SJiri Pirko err = netdev_name_node_alt_create(dev, alt_ifname); 381944bfa9c5SEric Dumazet if (!err) 382044bfa9c5SEric Dumazet alt_ifname = NULL; 382136fbf1e5SJiri Pirko } else if (cmd == RTM_DELLINKPROP) { 382236fbf1e5SJiri Pirko err = netdev_name_node_alt_destroy(dev, alt_ifname); 382336fbf1e5SJiri Pirko } else { 382444bfa9c5SEric Dumazet WARN_ON_ONCE(1); 382544bfa9c5SEric Dumazet err = -EINVAL; 382636fbf1e5SJiri Pirko } 382736fbf1e5SJiri Pirko 382844bfa9c5SEric Dumazet kfree(alt_ifname); 382944bfa9c5SEric Dumazet if (!err) 383036fbf1e5SJiri Pirko *changed = true; 383144bfa9c5SEric Dumazet return err; 383236fbf1e5SJiri Pirko } 383336fbf1e5SJiri Pirko 383436fbf1e5SJiri Pirko static int rtnl_linkprop(int cmd, struct sk_buff *skb, struct nlmsghdr *nlh, 383536fbf1e5SJiri Pirko struct netlink_ext_ack *extack) 383636fbf1e5SJiri Pirko { 383736fbf1e5SJiri Pirko struct net *net = sock_net(skb->sk); 383836fbf1e5SJiri Pirko struct nlattr *tb[IFLA_MAX + 1]; 383936fbf1e5SJiri Pirko struct net_device *dev; 384036fbf1e5SJiri Pirko struct ifinfomsg *ifm; 384136fbf1e5SJiri Pirko bool changed = false; 384236fbf1e5SJiri Pirko struct nlattr *attr; 384336fbf1e5SJiri Pirko int err, rem; 384436fbf1e5SJiri Pirko 384536fbf1e5SJiri Pirko err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack); 384636fbf1e5SJiri Pirko if (err) 384736fbf1e5SJiri Pirko return err; 384836fbf1e5SJiri Pirko 384936fbf1e5SJiri Pirko err = rtnl_ensure_unique_netns(tb, extack, true); 385036fbf1e5SJiri Pirko if (err) 385136fbf1e5SJiri Pirko return err; 385236fbf1e5SJiri Pirko 385336fbf1e5SJiri Pirko ifm = nlmsg_data(nlh); 3854cc6090e9SJiri Pirko if (ifm->ifi_index > 0) 385536fbf1e5SJiri Pirko dev = __dev_get_by_index(net, ifm->ifi_index); 385676c9ac0eSJiri Pirko else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) 38575ea08b52SFlorent Fourcot dev = rtnl_dev_get(net, tb); 3858cc6090e9SJiri Pirko else 385936fbf1e5SJiri Pirko return -EINVAL; 386036fbf1e5SJiri Pirko 386136fbf1e5SJiri Pirko if (!dev) 386236fbf1e5SJiri Pirko return -ENODEV; 386336fbf1e5SJiri Pirko 386436fbf1e5SJiri Pirko if (!tb[IFLA_PROP_LIST]) 386536fbf1e5SJiri Pirko return 0; 386636fbf1e5SJiri Pirko 386736fbf1e5SJiri Pirko nla_for_each_nested(attr, tb[IFLA_PROP_LIST], rem) { 386836fbf1e5SJiri Pirko switch (nla_type(attr)) { 386936fbf1e5SJiri Pirko case IFLA_ALT_IFNAME: 387036fbf1e5SJiri Pirko err = rtnl_alt_ifname(cmd, dev, attr, &changed, extack); 387136fbf1e5SJiri Pirko if (err) 387236fbf1e5SJiri Pirko return err; 387336fbf1e5SJiri Pirko break; 387436fbf1e5SJiri Pirko } 387536fbf1e5SJiri Pirko } 387636fbf1e5SJiri Pirko 387736fbf1e5SJiri Pirko if (changed) 387836fbf1e5SJiri Pirko netdev_state_change(dev); 387936fbf1e5SJiri Pirko return 0; 388036fbf1e5SJiri Pirko } 388136fbf1e5SJiri Pirko 388236fbf1e5SJiri Pirko static int rtnl_newlinkprop(struct sk_buff *skb, struct nlmsghdr *nlh, 388336fbf1e5SJiri Pirko struct netlink_ext_ack *extack) 388436fbf1e5SJiri Pirko { 388536fbf1e5SJiri Pirko return rtnl_linkprop(RTM_NEWLINKPROP, skb, nlh, extack); 388636fbf1e5SJiri Pirko } 388736fbf1e5SJiri Pirko 388836fbf1e5SJiri Pirko static int rtnl_dellinkprop(struct sk_buff *skb, struct nlmsghdr *nlh, 388936fbf1e5SJiri Pirko struct netlink_ext_ack *extack) 389036fbf1e5SJiri Pirko { 389136fbf1e5SJiri Pirko return rtnl_linkprop(RTM_DELLINKPROP, skb, nlh, extack); 389236fbf1e5SJiri Pirko } 389336fbf1e5SJiri Pirko 3894ebfe3c51SDi Zhu static u32 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh) 3895c7ac8679SGreg Rose { 3896115c9b81SGreg Rose struct net *net = sock_net(skb->sk); 3897ebfe3c51SDi Zhu size_t min_ifinfo_dump_size = 0; 3898115c9b81SGreg Rose struct nlattr *tb[IFLA_MAX+1]; 3899115c9b81SGreg Rose u32 ext_filter_mask = 0; 3900ebfe3c51SDi Zhu struct net_device *dev; 3901e5eca6d4SMichal Schmidt int hdrlen; 3902115c9b81SGreg Rose 3903e5eca6d4SMichal Schmidt /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */ 3904e5eca6d4SMichal Schmidt hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ? 3905e5eca6d4SMichal Schmidt sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); 3906e5eca6d4SMichal Schmidt 39078cb08174SJohannes Berg if (nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) { 3908115c9b81SGreg Rose if (tb[IFLA_EXT_MASK]) 3909115c9b81SGreg Rose ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 3910a4b64fbeSEric Dumazet } 3911115c9b81SGreg Rose 3912115c9b81SGreg Rose if (!ext_filter_mask) 3913115c9b81SGreg Rose return NLMSG_GOODSIZE; 3914115c9b81SGreg Rose /* 3915115c9b81SGreg Rose * traverse the list of net devices and compute the minimum 3916115c9b81SGreg Rose * buffer size based upon the filter mask. 3917115c9b81SGreg Rose */ 39186853dd48SFlorian Westphal rcu_read_lock(); 39196853dd48SFlorian Westphal for_each_netdev_rcu(net, dev) { 3920ebfe3c51SDi Zhu min_ifinfo_dump_size = max(min_ifinfo_dump_size, 3921ebfe3c51SDi Zhu if_nlmsg_size(dev, ext_filter_mask)); 3922115c9b81SGreg Rose } 39236853dd48SFlorian Westphal rcu_read_unlock(); 3924115c9b81SGreg Rose 392593af2056SZhang Shengju return nlmsg_total_size(min_ifinfo_dump_size); 3926c7ac8679SGreg Rose } 3927c7ac8679SGreg Rose 392842bad1daSAdrian Bunk static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb) 39291da177e4SLinus Torvalds { 39301da177e4SLinus Torvalds int idx; 39311da177e4SLinus Torvalds int s_idx = cb->family; 393287ccbb1fSChristian Brauner int type = cb->nlh->nlmsg_type - RTM_BASE; 3933c63586dcSDavid Ahern int ret = 0; 39341da177e4SLinus Torvalds 39351da177e4SLinus Torvalds if (s_idx == 0) 39361da177e4SLinus Torvalds s_idx = 1; 39376853dd48SFlorian Westphal 393825239ceeSPatrick McHardy for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) { 393951e13685SJakub Kicinski struct rtnl_link __rcu **tab; 3940addf9b90SFlorian Westphal struct rtnl_link *link; 39416853dd48SFlorian Westphal rtnl_dumpit_func dumpit; 39426853dd48SFlorian Westphal 39431da177e4SLinus Torvalds if (idx < s_idx || idx == PF_PACKET) 39441da177e4SLinus Torvalds continue; 39456853dd48SFlorian Westphal 3946addf9b90SFlorian Westphal if (type < 0 || type >= RTM_NR_MSGTYPES) 39471da177e4SLinus Torvalds continue; 39486853dd48SFlorian Westphal 3949addf9b90SFlorian Westphal tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]); 3950addf9b90SFlorian Westphal if (!tab) 3951addf9b90SFlorian Westphal continue; 3952addf9b90SFlorian Westphal 395351e13685SJakub Kicinski link = rcu_dereference_rtnl(tab[type]); 3954addf9b90SFlorian Westphal if (!link) 3955addf9b90SFlorian Westphal continue; 3956addf9b90SFlorian Westphal 3957addf9b90SFlorian Westphal dumpit = link->dumpit; 39586853dd48SFlorian Westphal if (!dumpit) 39596853dd48SFlorian Westphal continue; 39606853dd48SFlorian Westphal 39610465277fSNicolas Dichtel if (idx > s_idx) { 39621da177e4SLinus Torvalds memset(&cb->args[0], 0, sizeof(cb->args)); 39630465277fSNicolas Dichtel cb->prev_seq = 0; 39640465277fSNicolas Dichtel cb->seq = 0; 39650465277fSNicolas Dichtel } 3966c63586dcSDavid Ahern ret = dumpit(skb, cb); 39675e1acb4aSAlexey Kodanev if (ret) 39681da177e4SLinus Torvalds break; 39691da177e4SLinus Torvalds } 39701da177e4SLinus Torvalds cb->family = idx; 39711da177e4SLinus Torvalds 3972c63586dcSDavid Ahern return skb->len ? : ret; 39731da177e4SLinus Torvalds } 39741da177e4SLinus Torvalds 3975395eea6cSMahesh Bandewar struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev, 39763d3ea5afSVlad Yasevich unsigned int change, 397738e01b30SNicolas Dichtel u32 event, gfp_t flags, int *new_nsid, 39781d997f10SHangbin Liu int new_ifindex, u32 portid, u32 seq) 39791da177e4SLinus Torvalds { 3980c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 39811da177e4SLinus Torvalds struct sk_buff *skb; 39820ec6d3f4SThomas Graf int err = -ENOBUFS; 39831da177e4SLinus Torvalds 398450af5969Sluo penghao skb = nlmsg_new(if_nlmsg_size(dev, 0), flags); 39850ec6d3f4SThomas Graf if (skb == NULL) 39860ec6d3f4SThomas Graf goto errout; 39871da177e4SLinus Torvalds 398879e1ad14SJiri Benc err = rtnl_fill_ifinfo(skb, dev, dev_net(dev), 39891d997f10SHangbin Liu type, portid, seq, change, 0, 0, event, 3990d4e4fdf9SGuillaume Nault new_nsid, new_ifindex, -1, flags); 399126932566SPatrick McHardy if (err < 0) { 399226932566SPatrick McHardy /* -EMSGSIZE implies BUG in if_nlmsg_size() */ 399326932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 399426932566SPatrick McHardy kfree_skb(skb); 399526932566SPatrick McHardy goto errout; 399626932566SPatrick McHardy } 3997395eea6cSMahesh Bandewar return skb; 39980ec6d3f4SThomas Graf errout: 39990ec6d3f4SThomas Graf if (err < 0) 40004b3da706SEric W. Biederman rtnl_set_sk_err(net, RTNLGRP_LINK, err); 4001395eea6cSMahesh Bandewar return NULL; 4002395eea6cSMahesh Bandewar } 4003395eea6cSMahesh Bandewar 40041d997f10SHangbin Liu void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags, 40051d997f10SHangbin Liu u32 portid, const struct nlmsghdr *nlh) 4006395eea6cSMahesh Bandewar { 4007395eea6cSMahesh Bandewar struct net *net = dev_net(dev); 4008395eea6cSMahesh Bandewar 40091d997f10SHangbin Liu rtnl_notify(skb, net, portid, RTNLGRP_LINK, nlh, flags); 4010395eea6cSMahesh Bandewar } 4011395eea6cSMahesh Bandewar 40123d3ea5afSVlad Yasevich static void rtmsg_ifinfo_event(int type, struct net_device *dev, 40133d3ea5afSVlad Yasevich unsigned int change, u32 event, 40141d997f10SHangbin Liu gfp_t flags, int *new_nsid, int new_ifindex, 40151d997f10SHangbin Liu u32 portid, const struct nlmsghdr *nlh) 4016395eea6cSMahesh Bandewar { 4017395eea6cSMahesh Bandewar struct sk_buff *skb; 4018395eea6cSMahesh Bandewar 4019ed2a80abSNicolas Dichtel if (dev->reg_state != NETREG_REGISTERED) 4020ed2a80abSNicolas Dichtel return; 4021ed2a80abSNicolas Dichtel 402238e01b30SNicolas Dichtel skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid, 40231d997f10SHangbin Liu new_ifindex, portid, nlmsg_seq(nlh)); 4024395eea6cSMahesh Bandewar if (skb) 40251d997f10SHangbin Liu rtmsg_ifinfo_send(skb, dev, flags, portid, nlh); 40261da177e4SLinus Torvalds } 40273d3ea5afSVlad Yasevich 40283d3ea5afSVlad Yasevich void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change, 40291d997f10SHangbin Liu gfp_t flags, u32 portid, const struct nlmsghdr *nlh) 40303d3ea5afSVlad Yasevich { 403138e01b30SNicolas Dichtel rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags, 40321d997f10SHangbin Liu NULL, 0, portid, nlh); 40333d3ea5afSVlad Yasevich } 40341da177e4SLinus Torvalds 40356621dd29SNicolas Dichtel void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change, 403638e01b30SNicolas Dichtel gfp_t flags, int *new_nsid, int new_ifindex) 40376621dd29SNicolas Dichtel { 40386621dd29SNicolas Dichtel rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags, 40391d997f10SHangbin Liu new_nsid, new_ifindex, 0, NULL); 40406621dd29SNicolas Dichtel } 40416621dd29SNicolas Dichtel 4042d83b0603SJohn Fastabend static int nlmsg_populate_fdb_fill(struct sk_buff *skb, 4043d83b0603SJohn Fastabend struct net_device *dev, 40441e53d5bbSHubert Sokolowski u8 *addr, u16 vid, u32 pid, u32 seq, 40451c104a6bSNicolas Dichtel int type, unsigned int flags, 4046b3379041SHubert Sokolowski int nlflags, u16 ndm_state) 4047d83b0603SJohn Fastabend { 4048d83b0603SJohn Fastabend struct nlmsghdr *nlh; 4049d83b0603SJohn Fastabend struct ndmsg *ndm; 4050d83b0603SJohn Fastabend 40511c104a6bSNicolas Dichtel nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags); 4052d83b0603SJohn Fastabend if (!nlh) 4053d83b0603SJohn Fastabend return -EMSGSIZE; 4054d83b0603SJohn Fastabend 4055d83b0603SJohn Fastabend ndm = nlmsg_data(nlh); 4056d83b0603SJohn Fastabend ndm->ndm_family = AF_BRIDGE; 4057d83b0603SJohn Fastabend ndm->ndm_pad1 = 0; 4058d83b0603SJohn Fastabend ndm->ndm_pad2 = 0; 4059d83b0603SJohn Fastabend ndm->ndm_flags = flags; 4060d83b0603SJohn Fastabend ndm->ndm_type = 0; 4061d83b0603SJohn Fastabend ndm->ndm_ifindex = dev->ifindex; 4062b3379041SHubert Sokolowski ndm->ndm_state = ndm_state; 4063d83b0603SJohn Fastabend 4064d83b0603SJohn Fastabend if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr)) 4065d83b0603SJohn Fastabend goto nla_put_failure; 40661e53d5bbSHubert Sokolowski if (vid) 40671e53d5bbSHubert Sokolowski if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid)) 40681e53d5bbSHubert Sokolowski goto nla_put_failure; 4069d83b0603SJohn Fastabend 4070053c095aSJohannes Berg nlmsg_end(skb, nlh); 4071053c095aSJohannes Berg return 0; 4072d83b0603SJohn Fastabend 4073d83b0603SJohn Fastabend nla_put_failure: 4074d83b0603SJohn Fastabend nlmsg_cancel(skb, nlh); 4075d83b0603SJohn Fastabend return -EMSGSIZE; 4076d83b0603SJohn Fastabend } 4077d83b0603SJohn Fastabend 40783ff661c3SJohn Fastabend static inline size_t rtnl_fdb_nlmsg_size(void) 40793ff661c3SJohn Fastabend { 4080f82ef3e1SSabrina Dubroca return NLMSG_ALIGN(sizeof(struct ndmsg)) + 4081f82ef3e1SSabrina Dubroca nla_total_size(ETH_ALEN) + /* NDA_LLADDR */ 4082f82ef3e1SSabrina Dubroca nla_total_size(sizeof(u16)) + /* NDA_VLAN */ 4083f82ef3e1SSabrina Dubroca 0; 40843ff661c3SJohn Fastabend } 40853ff661c3SJohn Fastabend 4086b3379041SHubert Sokolowski static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type, 4087b3379041SHubert Sokolowski u16 ndm_state) 40883ff661c3SJohn Fastabend { 40893ff661c3SJohn Fastabend struct net *net = dev_net(dev); 40903ff661c3SJohn Fastabend struct sk_buff *skb; 40913ff661c3SJohn Fastabend int err = -ENOBUFS; 40923ff661c3SJohn Fastabend 40933ff661c3SJohn Fastabend skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC); 40943ff661c3SJohn Fastabend if (!skb) 40953ff661c3SJohn Fastabend goto errout; 40963ff661c3SJohn Fastabend 40971e53d5bbSHubert Sokolowski err = nlmsg_populate_fdb_fill(skb, dev, addr, vid, 4098b3379041SHubert Sokolowski 0, 0, type, NTF_SELF, 0, ndm_state); 40993ff661c3SJohn Fastabend if (err < 0) { 41003ff661c3SJohn Fastabend kfree_skb(skb); 41013ff661c3SJohn Fastabend goto errout; 41023ff661c3SJohn Fastabend } 41033ff661c3SJohn Fastabend 41043ff661c3SJohn Fastabend rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); 41053ff661c3SJohn Fastabend return; 41063ff661c3SJohn Fastabend errout: 41073ff661c3SJohn Fastabend rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); 41083ff661c3SJohn Fastabend } 41093ff661c3SJohn Fastabend 4110a986967eSBart Van Assche /* 4111090096bfSVlad Yasevich * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry 4112090096bfSVlad Yasevich */ 4113090096bfSVlad Yasevich int ndo_dflt_fdb_add(struct ndmsg *ndm, 4114090096bfSVlad Yasevich struct nlattr *tb[], 4115090096bfSVlad Yasevich struct net_device *dev, 4116f6f6424bSJiri Pirko const unsigned char *addr, u16 vid, 4117090096bfSVlad Yasevich u16 flags) 4118090096bfSVlad Yasevich { 4119090096bfSVlad Yasevich int err = -EINVAL; 4120090096bfSVlad Yasevich 4121090096bfSVlad Yasevich /* If aging addresses are supported device will need to 4122090096bfSVlad Yasevich * implement its own handler for this. 4123090096bfSVlad Yasevich */ 4124090096bfSVlad Yasevich if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) { 412578ecc890SVladimir Oltean netdev_info(dev, "default FDB implementation only supports local addresses\n"); 4126090096bfSVlad Yasevich return err; 4127090096bfSVlad Yasevich } 4128090096bfSVlad Yasevich 4129a35ec8e3SHans J. Schultz if (tb[NDA_FLAGS_EXT]) { 4130a35ec8e3SHans J. Schultz netdev_info(dev, "invalid flags given to default FDB implementation\n"); 4131a35ec8e3SHans J. Schultz return err; 4132a35ec8e3SHans J. Schultz } 4133a35ec8e3SHans J. Schultz 413465891feaSOr Gerlitz if (vid) { 413523ac0b42SVladimir Oltean netdev_info(dev, "vlans aren't supported yet for dev_uc|mc_add()\n"); 413665891feaSOr Gerlitz return err; 413765891feaSOr Gerlitz } 413865891feaSOr Gerlitz 4139090096bfSVlad Yasevich if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) 4140090096bfSVlad Yasevich err = dev_uc_add_excl(dev, addr); 4141090096bfSVlad Yasevich else if (is_multicast_ether_addr(addr)) 4142090096bfSVlad Yasevich err = dev_mc_add_excl(dev, addr); 4143090096bfSVlad Yasevich 4144090096bfSVlad Yasevich /* Only return duplicate errors if NLM_F_EXCL is set */ 4145090096bfSVlad Yasevich if (err == -EEXIST && !(flags & NLM_F_EXCL)) 4146090096bfSVlad Yasevich err = 0; 4147090096bfSVlad Yasevich 4148090096bfSVlad Yasevich return err; 4149090096bfSVlad Yasevich } 4150090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_add); 4151090096bfSVlad Yasevich 4152b88d12e4SFlorian Westphal static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid, 4153b88d12e4SFlorian Westphal struct netlink_ext_ack *extack) 4154f6f6424bSJiri Pirko { 4155f6f6424bSJiri Pirko u16 vid = 0; 4156f6f6424bSJiri Pirko 4157f6f6424bSJiri Pirko if (vlan_attr) { 4158f6f6424bSJiri Pirko if (nla_len(vlan_attr) != sizeof(u16)) { 4159b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid vlan attribute size"); 4160f6f6424bSJiri Pirko return -EINVAL; 4161f6f6424bSJiri Pirko } 4162f6f6424bSJiri Pirko 4163f6f6424bSJiri Pirko vid = nla_get_u16(vlan_attr); 4164f6f6424bSJiri Pirko 4165f6f6424bSJiri Pirko if (!vid || vid >= VLAN_VID_MASK) { 4166b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid vlan id"); 4167f6f6424bSJiri Pirko return -EINVAL; 4168f6f6424bSJiri Pirko } 4169f6f6424bSJiri Pirko } 4170f6f6424bSJiri Pirko *p_vid = vid; 4171f6f6424bSJiri Pirko return 0; 4172f6f6424bSJiri Pirko } 4173f6f6424bSJiri Pirko 4174c21ef3e3SDavid Ahern static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh, 4175c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 417677162022SJohn Fastabend { 417777162022SJohn Fastabend struct net *net = sock_net(skb->sk); 417877162022SJohn Fastabend struct ndmsg *ndm; 417977162022SJohn Fastabend struct nlattr *tb[NDA_MAX+1]; 418077162022SJohn Fastabend struct net_device *dev; 418177162022SJohn Fastabend u8 *addr; 4182f6f6424bSJiri Pirko u16 vid; 418377162022SJohn Fastabend int err; 418477162022SJohn Fastabend 41858cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, 41868cb08174SJohannes Berg extack); 418777162022SJohn Fastabend if (err < 0) 418877162022SJohn Fastabend return err; 418977162022SJohn Fastabend 419077162022SJohn Fastabend ndm = nlmsg_data(nlh); 419177162022SJohn Fastabend if (ndm->ndm_ifindex == 0) { 4192b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid ifindex"); 419377162022SJohn Fastabend return -EINVAL; 419477162022SJohn Fastabend } 419577162022SJohn Fastabend 419677162022SJohn Fastabend dev = __dev_get_by_index(net, ndm->ndm_ifindex); 419777162022SJohn Fastabend if (dev == NULL) { 4198b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 419977162022SJohn Fastabend return -ENODEV; 420077162022SJohn Fastabend } 420177162022SJohn Fastabend 420277162022SJohn Fastabend if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { 4203b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid address"); 420477162022SJohn Fastabend return -EINVAL; 420577162022SJohn Fastabend } 420677162022SJohn Fastabend 4207da715775SIdo Schimmel if (dev->type != ARPHRD_ETHER) { 4208da715775SIdo Schimmel NL_SET_ERR_MSG(extack, "FDB add only supported for Ethernet devices"); 4209da715775SIdo Schimmel return -EINVAL; 4210da715775SIdo Schimmel } 4211da715775SIdo Schimmel 421277162022SJohn Fastabend addr = nla_data(tb[NDA_LLADDR]); 421377162022SJohn Fastabend 4214b88d12e4SFlorian Westphal err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack); 4215f6f6424bSJiri Pirko if (err) 4216f6f6424bSJiri Pirko return err; 4217f6f6424bSJiri Pirko 421877162022SJohn Fastabend err = -EOPNOTSUPP; 421977162022SJohn Fastabend 422077162022SJohn Fastabend /* Support fdb on master device the net/bridge default case */ 422177162022SJohn Fastabend if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && 42222e92a2d0SJulian Wiedmann netif_is_bridge_port(dev)) { 4223898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4224898e5061SJiri Pirko const struct net_device_ops *ops = br_dev->netdev_ops; 4225898e5061SJiri Pirko 4226f6f6424bSJiri Pirko err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid, 422787b0984eSPetr Machata nlh->nlmsg_flags, extack); 422877162022SJohn Fastabend if (err) 422977162022SJohn Fastabend goto out; 423077162022SJohn Fastabend else 423177162022SJohn Fastabend ndm->ndm_flags &= ~NTF_MASTER; 423277162022SJohn Fastabend } 423377162022SJohn Fastabend 423477162022SJohn Fastabend /* Embedded bridge, macvlan, and any other device support */ 4235090096bfSVlad Yasevich if ((ndm->ndm_flags & NTF_SELF)) { 4236090096bfSVlad Yasevich if (dev->netdev_ops->ndo_fdb_add) 4237090096bfSVlad Yasevich err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr, 4238f6f6424bSJiri Pirko vid, 423987b0984eSPetr Machata nlh->nlmsg_flags, 424087b0984eSPetr Machata extack); 4241090096bfSVlad Yasevich else 4242f6f6424bSJiri Pirko err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid, 424377162022SJohn Fastabend nlh->nlmsg_flags); 424477162022SJohn Fastabend 42453ff661c3SJohn Fastabend if (!err) { 4246b3379041SHubert Sokolowski rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH, 4247b3379041SHubert Sokolowski ndm->ndm_state); 424877162022SJohn Fastabend ndm->ndm_flags &= ~NTF_SELF; 424977162022SJohn Fastabend } 42503ff661c3SJohn Fastabend } 425177162022SJohn Fastabend out: 425277162022SJohn Fastabend return err; 425377162022SJohn Fastabend } 425477162022SJohn Fastabend 4255a986967eSBart Van Assche /* 4256090096bfSVlad Yasevich * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry 4257090096bfSVlad Yasevich */ 4258090096bfSVlad Yasevich int ndo_dflt_fdb_del(struct ndmsg *ndm, 4259090096bfSVlad Yasevich struct nlattr *tb[], 4260090096bfSVlad Yasevich struct net_device *dev, 4261f6f6424bSJiri Pirko const unsigned char *addr, u16 vid) 4262090096bfSVlad Yasevich { 4263c8a89c4aSAlexander Duyck int err = -EINVAL; 4264090096bfSVlad Yasevich 4265090096bfSVlad Yasevich /* If aging addresses are supported device will need to 4266090096bfSVlad Yasevich * implement its own handler for this. 4267090096bfSVlad Yasevich */ 426864535993SSridhar Samudrala if (!(ndm->ndm_state & NUD_PERMANENT)) { 426978ecc890SVladimir Oltean netdev_info(dev, "default FDB implementation only supports local addresses\n"); 4270c8a89c4aSAlexander Duyck return err; 4271090096bfSVlad Yasevich } 4272090096bfSVlad Yasevich 4273090096bfSVlad Yasevich if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) 4274090096bfSVlad Yasevich err = dev_uc_del(dev, addr); 4275090096bfSVlad Yasevich else if (is_multicast_ether_addr(addr)) 4276090096bfSVlad Yasevich err = dev_mc_del(dev, addr); 4277090096bfSVlad Yasevich 4278090096bfSVlad Yasevich return err; 4279090096bfSVlad Yasevich } 4280090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_del); 4281090096bfSVlad Yasevich 42829e834259SNikolay Aleksandrov static const struct nla_policy fdb_del_bulk_policy[NDA_MAX + 1] = { 42839e834259SNikolay Aleksandrov [NDA_VLAN] = { .type = NLA_U16 }, 42849e834259SNikolay Aleksandrov [NDA_IFINDEX] = NLA_POLICY_MIN(NLA_S32, 1), 4285ea2c0f9eSNikolay Aleksandrov [NDA_NDM_STATE_MASK] = { .type = NLA_U16 }, 4286ea2c0f9eSNikolay Aleksandrov [NDA_NDM_FLAGS_MASK] = { .type = NLA_U8 }, 42879e834259SNikolay Aleksandrov }; 42889e834259SNikolay Aleksandrov 4289c21ef3e3SDavid Ahern static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh, 4290c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 429177162022SJohn Fastabend { 42929e834259SNikolay Aleksandrov bool del_bulk = !!(nlh->nlmsg_flags & NLM_F_BULK); 429377162022SJohn Fastabend struct net *net = sock_net(skb->sk); 42949e834259SNikolay Aleksandrov const struct net_device_ops *ops; 429577162022SJohn Fastabend struct ndmsg *ndm; 42961690be63SVlad Yasevich struct nlattr *tb[NDA_MAX+1]; 429777162022SJohn Fastabend struct net_device *dev; 42989e834259SNikolay Aleksandrov __u8 *addr = NULL; 42997d311801SColin Ian King int err; 4300f6f6424bSJiri Pirko u16 vid; 430177162022SJohn Fastabend 430290f62cf3SEric W. Biederman if (!netlink_capable(skb, CAP_NET_ADMIN)) 43031690be63SVlad Yasevich return -EPERM; 43041690be63SVlad Yasevich 43059e834259SNikolay Aleksandrov if (!del_bulk) { 43069e834259SNikolay Aleksandrov err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, 43079e834259SNikolay Aleksandrov NULL, extack); 43089e834259SNikolay Aleksandrov } else { 43099e834259SNikolay Aleksandrov err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, 43109e834259SNikolay Aleksandrov fdb_del_bulk_policy, extack); 43119e834259SNikolay Aleksandrov } 43121690be63SVlad Yasevich if (err < 0) 43131690be63SVlad Yasevich return err; 431477162022SJohn Fastabend 431577162022SJohn Fastabend ndm = nlmsg_data(nlh); 431677162022SJohn Fastabend if (ndm->ndm_ifindex == 0) { 4317b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid ifindex"); 431877162022SJohn Fastabend return -EINVAL; 431977162022SJohn Fastabend } 432077162022SJohn Fastabend 432177162022SJohn Fastabend dev = __dev_get_by_index(net, ndm->ndm_ifindex); 432277162022SJohn Fastabend if (dev == NULL) { 4323b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 432477162022SJohn Fastabend return -ENODEV; 432577162022SJohn Fastabend } 432677162022SJohn Fastabend 43279e834259SNikolay Aleksandrov if (!del_bulk) { 43281690be63SVlad Yasevich if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { 4329b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid address"); 433077162022SJohn Fastabend return -EINVAL; 433177162022SJohn Fastabend } 43329e834259SNikolay Aleksandrov addr = nla_data(tb[NDA_LLADDR]); 43339e834259SNikolay Aleksandrov } 433477162022SJohn Fastabend 4335da715775SIdo Schimmel if (dev->type != ARPHRD_ETHER) { 4336da715775SIdo Schimmel NL_SET_ERR_MSG(extack, "FDB delete only supported for Ethernet devices"); 4337da715775SIdo Schimmel return -EINVAL; 4338da715775SIdo Schimmel } 4339da715775SIdo Schimmel 4340b88d12e4SFlorian Westphal err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack); 4341f6f6424bSJiri Pirko if (err) 4342f6f6424bSJiri Pirko return err; 4343f6f6424bSJiri Pirko 434477162022SJohn Fastabend err = -EOPNOTSUPP; 434577162022SJohn Fastabend 434677162022SJohn Fastabend /* Support fdb on master device the net/bridge default case */ 434777162022SJohn Fastabend if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && 43482e92a2d0SJulian Wiedmann netif_is_bridge_port(dev)) { 4349898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 435077162022SJohn Fastabend 43519e834259SNikolay Aleksandrov ops = br_dev->netdev_ops; 43529e834259SNikolay Aleksandrov if (!del_bulk) { 4353898e5061SJiri Pirko if (ops->ndo_fdb_del) 4354ca4567f1SAlaa Mohamed err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid, extack); 43559e834259SNikolay Aleksandrov } else { 43569e834259SNikolay Aleksandrov if (ops->ndo_fdb_del_bulk) 43579e834259SNikolay Aleksandrov err = ops->ndo_fdb_del_bulk(ndm, tb, dev, vid, 43589e834259SNikolay Aleksandrov extack); 43599e834259SNikolay Aleksandrov } 436077162022SJohn Fastabend 436177162022SJohn Fastabend if (err) 436277162022SJohn Fastabend goto out; 436377162022SJohn Fastabend else 436477162022SJohn Fastabend ndm->ndm_flags &= ~NTF_MASTER; 436577162022SJohn Fastabend } 436677162022SJohn Fastabend 436777162022SJohn Fastabend /* Embedded bridge, macvlan, and any other device support */ 4368090096bfSVlad Yasevich if (ndm->ndm_flags & NTF_SELF) { 43699e834259SNikolay Aleksandrov ops = dev->netdev_ops; 43709e834259SNikolay Aleksandrov if (!del_bulk) { 43719e834259SNikolay Aleksandrov if (ops->ndo_fdb_del) 4372ca4567f1SAlaa Mohamed err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid, extack); 4373090096bfSVlad Yasevich else 4374f6f6424bSJiri Pirko err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid); 43759e834259SNikolay Aleksandrov } else { 43769e834259SNikolay Aleksandrov /* in case err was cleared by NTF_MASTER call */ 43779e834259SNikolay Aleksandrov err = -EOPNOTSUPP; 43789e834259SNikolay Aleksandrov if (ops->ndo_fdb_del_bulk) 43799e834259SNikolay Aleksandrov err = ops->ndo_fdb_del_bulk(ndm, tb, dev, vid, 43809e834259SNikolay Aleksandrov extack); 43819e834259SNikolay Aleksandrov } 438277162022SJohn Fastabend 43833ff661c3SJohn Fastabend if (!err) { 43849e834259SNikolay Aleksandrov if (!del_bulk) 4385b3379041SHubert Sokolowski rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH, 4386b3379041SHubert Sokolowski ndm->ndm_state); 438777162022SJohn Fastabend ndm->ndm_flags &= ~NTF_SELF; 438877162022SJohn Fastabend } 43893ff661c3SJohn Fastabend } 439077162022SJohn Fastabend out: 439177162022SJohn Fastabend return err; 439277162022SJohn Fastabend } 439377162022SJohn Fastabend 4394d83b0603SJohn Fastabend static int nlmsg_populate_fdb(struct sk_buff *skb, 4395d83b0603SJohn Fastabend struct netlink_callback *cb, 4396d83b0603SJohn Fastabend struct net_device *dev, 4397d83b0603SJohn Fastabend int *idx, 4398d83b0603SJohn Fastabend struct netdev_hw_addr_list *list) 4399d83b0603SJohn Fastabend { 4400d83b0603SJohn Fastabend struct netdev_hw_addr *ha; 4401d83b0603SJohn Fastabend int err; 440215e47304SEric W. Biederman u32 portid, seq; 4403d83b0603SJohn Fastabend 440415e47304SEric W. Biederman portid = NETLINK_CB(cb->skb).portid; 4405d83b0603SJohn Fastabend seq = cb->nlh->nlmsg_seq; 4406d83b0603SJohn Fastabend 4407d83b0603SJohn Fastabend list_for_each_entry(ha, &list->list, list) { 4408d297653dSRoopa Prabhu if (*idx < cb->args[2]) 4409d83b0603SJohn Fastabend goto skip; 4410d83b0603SJohn Fastabend 44111e53d5bbSHubert Sokolowski err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0, 4412a7a558feSJohn Fastabend portid, seq, 44131c104a6bSNicolas Dichtel RTM_NEWNEIGH, NTF_SELF, 4414b3379041SHubert Sokolowski NLM_F_MULTI, NUD_PERMANENT); 4415d83b0603SJohn Fastabend if (err < 0) 4416d83b0603SJohn Fastabend return err; 4417d83b0603SJohn Fastabend skip: 4418d83b0603SJohn Fastabend *idx += 1; 4419d83b0603SJohn Fastabend } 4420d83b0603SJohn Fastabend return 0; 4421d83b0603SJohn Fastabend } 4422d83b0603SJohn Fastabend 4423d83b0603SJohn Fastabend /** 44242c53040fSBen Hutchings * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table. 4425a986967eSBart Van Assche * @skb: socket buffer to store message in 4426a986967eSBart Van Assche * @cb: netlink callback 4427d83b0603SJohn Fastabend * @dev: netdevice 4428a986967eSBart Van Assche * @filter_dev: ignored 4429a986967eSBart Van Assche * @idx: the number of FDB table entries dumped is added to *@idx 4430d83b0603SJohn Fastabend * 4431d83b0603SJohn Fastabend * Default netdevice operation to dump the existing unicast address list. 443291f3e7b1SJohn Fastabend * Returns number of addresses from list put in skb. 4433d83b0603SJohn Fastabend */ 4434d83b0603SJohn Fastabend int ndo_dflt_fdb_dump(struct sk_buff *skb, 4435d83b0603SJohn Fastabend struct netlink_callback *cb, 4436d83b0603SJohn Fastabend struct net_device *dev, 44375d5eacb3SJamal Hadi Salim struct net_device *filter_dev, 4438d297653dSRoopa Prabhu int *idx) 4439d83b0603SJohn Fastabend { 4440d83b0603SJohn Fastabend int err; 4441d83b0603SJohn Fastabend 444268883893SEric Dumazet if (dev->type != ARPHRD_ETHER) 444368883893SEric Dumazet return -EINVAL; 444468883893SEric Dumazet 4445d83b0603SJohn Fastabend netif_addr_lock_bh(dev); 4446d297653dSRoopa Prabhu err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc); 4447d83b0603SJohn Fastabend if (err) 4448d83b0603SJohn Fastabend goto out; 44492934c9dbSZhang Shengju err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc); 4450d83b0603SJohn Fastabend out: 4451d83b0603SJohn Fastabend netif_addr_unlock_bh(dev); 4452d297653dSRoopa Prabhu return err; 4453d83b0603SJohn Fastabend } 4454d83b0603SJohn Fastabend EXPORT_SYMBOL(ndo_dflt_fdb_dump); 4455d83b0603SJohn Fastabend 44568c6e137fSDavid Ahern static int valid_fdb_dump_strict(const struct nlmsghdr *nlh, 44578c6e137fSDavid Ahern int *br_idx, int *brport_idx, 44588c6e137fSDavid Ahern struct netlink_ext_ack *extack) 44598c6e137fSDavid Ahern { 44608c6e137fSDavid Ahern struct nlattr *tb[NDA_MAX + 1]; 44618c6e137fSDavid Ahern struct ndmsg *ndm; 44628c6e137fSDavid Ahern int err, i; 44638c6e137fSDavid Ahern 44648c6e137fSDavid Ahern if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) { 44658c6e137fSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid header for fdb dump request"); 44668c6e137fSDavid Ahern return -EINVAL; 44678c6e137fSDavid Ahern } 44688c6e137fSDavid Ahern 44698c6e137fSDavid Ahern ndm = nlmsg_data(nlh); 44708c6e137fSDavid Ahern if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state || 44718c6e137fSDavid Ahern ndm->ndm_flags || ndm->ndm_type) { 44728b73018fSNikolay Aleksandrov NL_SET_ERR_MSG(extack, "Invalid values in header for fdb dump request"); 44738c6e137fSDavid Ahern return -EINVAL; 44748c6e137fSDavid Ahern } 44758c6e137fSDavid Ahern 44768cb08174SJohannes Berg err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb, 44778cb08174SJohannes Berg NDA_MAX, NULL, extack); 44788c6e137fSDavid Ahern if (err < 0) 44798c6e137fSDavid Ahern return err; 44808c6e137fSDavid Ahern 44818c6e137fSDavid Ahern *brport_idx = ndm->ndm_ifindex; 44828c6e137fSDavid Ahern for (i = 0; i <= NDA_MAX; ++i) { 44838c6e137fSDavid Ahern if (!tb[i]) 44848c6e137fSDavid Ahern continue; 44858c6e137fSDavid Ahern 44868c6e137fSDavid Ahern switch (i) { 44878c6e137fSDavid Ahern case NDA_IFINDEX: 44888c6e137fSDavid Ahern if (nla_len(tb[i]) != sizeof(u32)) { 44898c6e137fSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid IFINDEX attribute in fdb dump request"); 44908c6e137fSDavid Ahern return -EINVAL; 44918c6e137fSDavid Ahern } 44928c6e137fSDavid Ahern *brport_idx = nla_get_u32(tb[NDA_IFINDEX]); 44938c6e137fSDavid Ahern break; 44948c6e137fSDavid Ahern case NDA_MASTER: 44958c6e137fSDavid Ahern if (nla_len(tb[i]) != sizeof(u32)) { 44968c6e137fSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid MASTER attribute in fdb dump request"); 44978c6e137fSDavid Ahern return -EINVAL; 44988c6e137fSDavid Ahern } 44998c6e137fSDavid Ahern *br_idx = nla_get_u32(tb[NDA_MASTER]); 45008c6e137fSDavid Ahern break; 45018c6e137fSDavid Ahern default: 45028c6e137fSDavid Ahern NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb dump request"); 45038c6e137fSDavid Ahern return -EINVAL; 45048c6e137fSDavid Ahern } 45058c6e137fSDavid Ahern } 45068c6e137fSDavid Ahern 45078c6e137fSDavid Ahern return 0; 45088c6e137fSDavid Ahern } 45098c6e137fSDavid Ahern 45108dfbda19SDavid Ahern static int valid_fdb_dump_legacy(const struct nlmsghdr *nlh, 45118dfbda19SDavid Ahern int *br_idx, int *brport_idx, 45128dfbda19SDavid Ahern struct netlink_ext_ack *extack) 45138dfbda19SDavid Ahern { 45148dfbda19SDavid Ahern struct nlattr *tb[IFLA_MAX+1]; 45158dfbda19SDavid Ahern int err; 45168dfbda19SDavid Ahern 45178dfbda19SDavid Ahern /* A hack to preserve kernel<->userspace interface. 45188dfbda19SDavid Ahern * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0. 45198dfbda19SDavid Ahern * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails. 45208dfbda19SDavid Ahern * So, check for ndmsg with an optional u32 attribute (not used here). 45218dfbda19SDavid Ahern * Fortunately these sizes don't conflict with the size of ifinfomsg 45228dfbda19SDavid Ahern * with an optional attribute. 45238dfbda19SDavid Ahern */ 45248dfbda19SDavid Ahern if (nlmsg_len(nlh) != sizeof(struct ndmsg) && 45258dfbda19SDavid Ahern (nlmsg_len(nlh) != sizeof(struct ndmsg) + 45268dfbda19SDavid Ahern nla_attr_size(sizeof(u32)))) { 45274565d7e5SDavid Ahern struct ifinfomsg *ifm; 45284565d7e5SDavid Ahern 45298cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg), 45308cb08174SJohannes Berg tb, IFLA_MAX, ifla_policy, 45318cb08174SJohannes Berg extack); 45328dfbda19SDavid Ahern if (err < 0) { 45338dfbda19SDavid Ahern return -EINVAL; 45348dfbda19SDavid Ahern } else if (err == 0) { 45358dfbda19SDavid Ahern if (tb[IFLA_MASTER]) 45368dfbda19SDavid Ahern *br_idx = nla_get_u32(tb[IFLA_MASTER]); 45378dfbda19SDavid Ahern } 45388dfbda19SDavid Ahern 45394565d7e5SDavid Ahern ifm = nlmsg_data(nlh); 45408dfbda19SDavid Ahern *brport_idx = ifm->ifi_index; 45418dfbda19SDavid Ahern } 45428dfbda19SDavid Ahern return 0; 45438dfbda19SDavid Ahern } 45448dfbda19SDavid Ahern 454577162022SJohn Fastabend static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb) 454677162022SJohn Fastabend { 454777162022SJohn Fastabend struct net_device *dev; 45485e6d2435SJamal Hadi Salim struct net_device *br_dev = NULL; 45495e6d2435SJamal Hadi Salim const struct net_device_ops *ops = NULL; 45505e6d2435SJamal Hadi Salim const struct net_device_ops *cops = NULL; 45515e6d2435SJamal Hadi Salim struct net *net = sock_net(skb->sk); 4552d297653dSRoopa Prabhu struct hlist_head *head; 45535e6d2435SJamal Hadi Salim int brport_idx = 0; 45545e6d2435SJamal Hadi Salim int br_idx = 0; 4555d297653dSRoopa Prabhu int h, s_h; 4556d297653dSRoopa Prabhu int idx = 0, s_idx; 4557d297653dSRoopa Prabhu int err = 0; 4558d297653dSRoopa Prabhu int fidx = 0; 455977162022SJohn Fastabend 45608c6e137fSDavid Ahern if (cb->strict_check) 45618c6e137fSDavid Ahern err = valid_fdb_dump_strict(cb->nlh, &br_idx, &brport_idx, 45628c6e137fSDavid Ahern cb->extack); 45638c6e137fSDavid Ahern else 45648dfbda19SDavid Ahern err = valid_fdb_dump_legacy(cb->nlh, &br_idx, &brport_idx, 45658dfbda19SDavid Ahern cb->extack); 45668dfbda19SDavid Ahern if (err < 0) 45678dfbda19SDavid Ahern return err; 45685e6d2435SJamal Hadi Salim 45695e6d2435SJamal Hadi Salim if (br_idx) { 45705e6d2435SJamal Hadi Salim br_dev = __dev_get_by_index(net, br_idx); 45715e6d2435SJamal Hadi Salim if (!br_dev) 45725e6d2435SJamal Hadi Salim return -ENODEV; 45735e6d2435SJamal Hadi Salim 4574898e5061SJiri Pirko ops = br_dev->netdev_ops; 45755e6d2435SJamal Hadi Salim } 45765e6d2435SJamal Hadi Salim 4577d297653dSRoopa Prabhu s_h = cb->args[0]; 4578d297653dSRoopa Prabhu s_idx = cb->args[1]; 4579d297653dSRoopa Prabhu 4580d297653dSRoopa Prabhu for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 4581d297653dSRoopa Prabhu idx = 0; 4582d297653dSRoopa Prabhu head = &net->dev_index_head[h]; 4583d297653dSRoopa Prabhu hlist_for_each_entry(dev, head, index_hlist) { 4584d297653dSRoopa Prabhu 45855e6d2435SJamal Hadi Salim if (brport_idx && (dev->ifindex != brport_idx)) 45865e6d2435SJamal Hadi Salim continue; 45875e6d2435SJamal Hadi Salim 45885e6d2435SJamal Hadi Salim if (!br_idx) { /* user did not specify a specific bridge */ 45892e92a2d0SJulian Wiedmann if (netif_is_bridge_port(dev)) { 45905e6d2435SJamal Hadi Salim br_dev = netdev_master_upper_dev_get(dev); 45915e6d2435SJamal Hadi Salim cops = br_dev->netdev_ops; 45925e6d2435SJamal Hadi Salim } 45935e6d2435SJamal Hadi Salim } else { 45945e6d2435SJamal Hadi Salim if (dev != br_dev && 45952e92a2d0SJulian Wiedmann !netif_is_bridge_port(dev)) 45965e6d2435SJamal Hadi Salim continue; 45975e6d2435SJamal Hadi Salim 45985e6d2435SJamal Hadi Salim if (br_dev != netdev_master_upper_dev_get(dev) && 4599254ec036SKyungrok Chung !netif_is_bridge_master(dev)) 46005e6d2435SJamal Hadi Salim continue; 46015e6d2435SJamal Hadi Salim cops = ops; 46025e6d2435SJamal Hadi Salim } 46035e6d2435SJamal Hadi Salim 4604d297653dSRoopa Prabhu if (idx < s_idx) 4605d297653dSRoopa Prabhu goto cont; 4606d297653dSRoopa Prabhu 46072e92a2d0SJulian Wiedmann if (netif_is_bridge_port(dev)) { 4608d297653dSRoopa Prabhu if (cops && cops->ndo_fdb_dump) { 4609d297653dSRoopa Prabhu err = cops->ndo_fdb_dump(skb, cb, 4610d297653dSRoopa Prabhu br_dev, dev, 4611d297653dSRoopa Prabhu &fidx); 4612d297653dSRoopa Prabhu if (err == -EMSGSIZE) 4613d297653dSRoopa Prabhu goto out; 461477162022SJohn Fastabend } 4615d297653dSRoopa Prabhu } 461677162022SJohn Fastabend 46175e6d2435SJamal Hadi Salim if (dev->netdev_ops->ndo_fdb_dump) 4618d297653dSRoopa Prabhu err = dev->netdev_ops->ndo_fdb_dump(skb, cb, 4619d297653dSRoopa Prabhu dev, NULL, 4620d297653dSRoopa Prabhu &fidx); 46216cb69742SHubert Sokolowski else 4622d297653dSRoopa Prabhu err = ndo_dflt_fdb_dump(skb, cb, dev, NULL, 4623d297653dSRoopa Prabhu &fidx); 4624d297653dSRoopa Prabhu if (err == -EMSGSIZE) 4625d297653dSRoopa Prabhu goto out; 46265e6d2435SJamal Hadi Salim 46275e6d2435SJamal Hadi Salim cops = NULL; 4628d297653dSRoopa Prabhu 4629d297653dSRoopa Prabhu /* reset fdb offset to 0 for rest of the interfaces */ 4630d297653dSRoopa Prabhu cb->args[2] = 0; 4631d297653dSRoopa Prabhu fidx = 0; 4632d297653dSRoopa Prabhu cont: 4633d297653dSRoopa Prabhu idx++; 4634d297653dSRoopa Prabhu } 463577162022SJohn Fastabend } 463677162022SJohn Fastabend 4637d297653dSRoopa Prabhu out: 4638d297653dSRoopa Prabhu cb->args[0] = h; 4639d297653dSRoopa Prabhu cb->args[1] = idx; 4640d297653dSRoopa Prabhu cb->args[2] = fidx; 4641d297653dSRoopa Prabhu 464277162022SJohn Fastabend return skb->len; 464377162022SJohn Fastabend } 464477162022SJohn Fastabend 46455b2f94b2SRoopa Prabhu static int valid_fdb_get_strict(const struct nlmsghdr *nlh, 46465b2f94b2SRoopa Prabhu struct nlattr **tb, u8 *ndm_flags, 46475b2f94b2SRoopa Prabhu int *br_idx, int *brport_idx, u8 **addr, 46485b2f94b2SRoopa Prabhu u16 *vid, struct netlink_ext_ack *extack) 46495b2f94b2SRoopa Prabhu { 46505b2f94b2SRoopa Prabhu struct ndmsg *ndm; 46515b2f94b2SRoopa Prabhu int err, i; 46525b2f94b2SRoopa Prabhu 46535b2f94b2SRoopa Prabhu if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) { 46545b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid header for fdb get request"); 46555b2f94b2SRoopa Prabhu return -EINVAL; 46565b2f94b2SRoopa Prabhu } 46575b2f94b2SRoopa Prabhu 46585b2f94b2SRoopa Prabhu ndm = nlmsg_data(nlh); 46595b2f94b2SRoopa Prabhu if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state || 46605b2f94b2SRoopa Prabhu ndm->ndm_type) { 46615b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid values in header for fdb get request"); 46625b2f94b2SRoopa Prabhu return -EINVAL; 46635b2f94b2SRoopa Prabhu } 46645b2f94b2SRoopa Prabhu 46655b2f94b2SRoopa Prabhu if (ndm->ndm_flags & ~(NTF_MASTER | NTF_SELF)) { 46665b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid flags in header for fdb get request"); 46675b2f94b2SRoopa Prabhu return -EINVAL; 46685b2f94b2SRoopa Prabhu } 46695b2f94b2SRoopa Prabhu 46708cb08174SJohannes Berg err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb, 46718cb08174SJohannes Berg NDA_MAX, nda_policy, extack); 46725b2f94b2SRoopa Prabhu if (err < 0) 46735b2f94b2SRoopa Prabhu return err; 46745b2f94b2SRoopa Prabhu 46755b2f94b2SRoopa Prabhu *ndm_flags = ndm->ndm_flags; 46765b2f94b2SRoopa Prabhu *brport_idx = ndm->ndm_ifindex; 46775b2f94b2SRoopa Prabhu for (i = 0; i <= NDA_MAX; ++i) { 46785b2f94b2SRoopa Prabhu if (!tb[i]) 46795b2f94b2SRoopa Prabhu continue; 46805b2f94b2SRoopa Prabhu 46815b2f94b2SRoopa Prabhu switch (i) { 46825b2f94b2SRoopa Prabhu case NDA_MASTER: 46835b2f94b2SRoopa Prabhu *br_idx = nla_get_u32(tb[i]); 46845b2f94b2SRoopa Prabhu break; 46855b2f94b2SRoopa Prabhu case NDA_LLADDR: 46865b2f94b2SRoopa Prabhu if (nla_len(tb[i]) != ETH_ALEN) { 46875b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid address in fdb get request"); 46885b2f94b2SRoopa Prabhu return -EINVAL; 46895b2f94b2SRoopa Prabhu } 46905b2f94b2SRoopa Prabhu *addr = nla_data(tb[i]); 46915b2f94b2SRoopa Prabhu break; 46925b2f94b2SRoopa Prabhu case NDA_VLAN: 46935b2f94b2SRoopa Prabhu err = fdb_vid_parse(tb[i], vid, extack); 46945b2f94b2SRoopa Prabhu if (err) 46955b2f94b2SRoopa Prabhu return err; 46965b2f94b2SRoopa Prabhu break; 46975b2f94b2SRoopa Prabhu case NDA_VNI: 46985b2f94b2SRoopa Prabhu break; 46995b2f94b2SRoopa Prabhu default: 47005b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb get request"); 47015b2f94b2SRoopa Prabhu return -EINVAL; 47025b2f94b2SRoopa Prabhu } 47035b2f94b2SRoopa Prabhu } 47045b2f94b2SRoopa Prabhu 47055b2f94b2SRoopa Prabhu return 0; 47065b2f94b2SRoopa Prabhu } 47075b2f94b2SRoopa Prabhu 47085b2f94b2SRoopa Prabhu static int rtnl_fdb_get(struct sk_buff *in_skb, struct nlmsghdr *nlh, 47095b2f94b2SRoopa Prabhu struct netlink_ext_ack *extack) 47105b2f94b2SRoopa Prabhu { 47115b2f94b2SRoopa Prabhu struct net_device *dev = NULL, *br_dev = NULL; 47125b2f94b2SRoopa Prabhu const struct net_device_ops *ops = NULL; 47135b2f94b2SRoopa Prabhu struct net *net = sock_net(in_skb->sk); 47145b2f94b2SRoopa Prabhu struct nlattr *tb[NDA_MAX + 1]; 47155b2f94b2SRoopa Prabhu struct sk_buff *skb; 47165b2f94b2SRoopa Prabhu int brport_idx = 0; 47175b2f94b2SRoopa Prabhu u8 ndm_flags = 0; 47185b2f94b2SRoopa Prabhu int br_idx = 0; 47195b2f94b2SRoopa Prabhu u8 *addr = NULL; 47205b2f94b2SRoopa Prabhu u16 vid = 0; 47215b2f94b2SRoopa Prabhu int err; 47225b2f94b2SRoopa Prabhu 47235b2f94b2SRoopa Prabhu err = valid_fdb_get_strict(nlh, tb, &ndm_flags, &br_idx, 47245b2f94b2SRoopa Prabhu &brport_idx, &addr, &vid, extack); 47255b2f94b2SRoopa Prabhu if (err < 0) 47265b2f94b2SRoopa Prabhu return err; 47275b2f94b2SRoopa Prabhu 4728f989d03eSNikolay Aleksandrov if (!addr) { 4729f989d03eSNikolay Aleksandrov NL_SET_ERR_MSG(extack, "Missing lookup address for fdb get request"); 4730f989d03eSNikolay Aleksandrov return -EINVAL; 4731f989d03eSNikolay Aleksandrov } 4732f989d03eSNikolay Aleksandrov 47335b2f94b2SRoopa Prabhu if (brport_idx) { 47345b2f94b2SRoopa Prabhu dev = __dev_get_by_index(net, brport_idx); 47355b2f94b2SRoopa Prabhu if (!dev) { 47365b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Unknown device ifindex"); 47375b2f94b2SRoopa Prabhu return -ENODEV; 47385b2f94b2SRoopa Prabhu } 47395b2f94b2SRoopa Prabhu } 47405b2f94b2SRoopa Prabhu 47415b2f94b2SRoopa Prabhu if (br_idx) { 47425b2f94b2SRoopa Prabhu if (dev) { 47435b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Master and device are mutually exclusive"); 47445b2f94b2SRoopa Prabhu return -EINVAL; 47455b2f94b2SRoopa Prabhu } 47465b2f94b2SRoopa Prabhu 47475b2f94b2SRoopa Prabhu br_dev = __dev_get_by_index(net, br_idx); 47485b2f94b2SRoopa Prabhu if (!br_dev) { 47495b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid master ifindex"); 47505b2f94b2SRoopa Prabhu return -EINVAL; 47515b2f94b2SRoopa Prabhu } 47525b2f94b2SRoopa Prabhu ops = br_dev->netdev_ops; 47535b2f94b2SRoopa Prabhu } 47545b2f94b2SRoopa Prabhu 47555b2f94b2SRoopa Prabhu if (dev) { 47565b2f94b2SRoopa Prabhu if (!ndm_flags || (ndm_flags & NTF_MASTER)) { 47572e92a2d0SJulian Wiedmann if (!netif_is_bridge_port(dev)) { 47585b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Device is not a bridge port"); 47595b2f94b2SRoopa Prabhu return -EINVAL; 47605b2f94b2SRoopa Prabhu } 47615b2f94b2SRoopa Prabhu br_dev = netdev_master_upper_dev_get(dev); 47625b2f94b2SRoopa Prabhu if (!br_dev) { 47635b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Master of device not found"); 47645b2f94b2SRoopa Prabhu return -EINVAL; 47655b2f94b2SRoopa Prabhu } 47665b2f94b2SRoopa Prabhu ops = br_dev->netdev_ops; 47675b2f94b2SRoopa Prabhu } else { 47685b2f94b2SRoopa Prabhu if (!(ndm_flags & NTF_SELF)) { 47695b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Missing NTF_SELF"); 47705b2f94b2SRoopa Prabhu return -EINVAL; 47715b2f94b2SRoopa Prabhu } 47725b2f94b2SRoopa Prabhu ops = dev->netdev_ops; 47735b2f94b2SRoopa Prabhu } 47745b2f94b2SRoopa Prabhu } 47755b2f94b2SRoopa Prabhu 47765b2f94b2SRoopa Prabhu if (!br_dev && !dev) { 47775b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "No device specified"); 47785b2f94b2SRoopa Prabhu return -ENODEV; 47795b2f94b2SRoopa Prabhu } 47805b2f94b2SRoopa Prabhu 47815b2f94b2SRoopa Prabhu if (!ops || !ops->ndo_fdb_get) { 47825b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Fdb get operation not supported by device"); 47835b2f94b2SRoopa Prabhu return -EOPNOTSUPP; 47845b2f94b2SRoopa Prabhu } 47855b2f94b2SRoopa Prabhu 47865b2f94b2SRoopa Prabhu skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); 47875b2f94b2SRoopa Prabhu if (!skb) 47885b2f94b2SRoopa Prabhu return -ENOBUFS; 47895b2f94b2SRoopa Prabhu 47905b2f94b2SRoopa Prabhu if (br_dev) 47915b2f94b2SRoopa Prabhu dev = br_dev; 47925b2f94b2SRoopa Prabhu err = ops->ndo_fdb_get(skb, tb, dev, addr, vid, 47935b2f94b2SRoopa Prabhu NETLINK_CB(in_skb).portid, 47945b2f94b2SRoopa Prabhu nlh->nlmsg_seq, extack); 47955b2f94b2SRoopa Prabhu if (err) 47965b2f94b2SRoopa Prabhu goto out; 47975b2f94b2SRoopa Prabhu 47985b2f94b2SRoopa Prabhu return rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 47995b2f94b2SRoopa Prabhu out: 48005b2f94b2SRoopa Prabhu kfree_skb(skb); 48015b2f94b2SRoopa Prabhu return err; 48025b2f94b2SRoopa Prabhu } 48035b2f94b2SRoopa Prabhu 48042c3c031cSScott Feldman static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask, 48052c3c031cSScott Feldman unsigned int attrnum, unsigned int flag) 48062c3c031cSScott Feldman { 48072c3c031cSScott Feldman if (mask & flag) 48082c3c031cSScott Feldman return nla_put_u8(skb, attrnum, !!(flags & flag)); 48092c3c031cSScott Feldman return 0; 48102c3c031cSScott Feldman } 48112c3c031cSScott Feldman 4812815cccbfSJohn Fastabend int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq, 48132c3c031cSScott Feldman struct net_device *dev, u16 mode, 48147d4f8d87SScott Feldman u32 flags, u32 mask, int nlflags, 48157d4f8d87SScott Feldman u32 filter_mask, 48167d4f8d87SScott Feldman int (*vlan_fill)(struct sk_buff *skb, 48177d4f8d87SScott Feldman struct net_device *dev, 48187d4f8d87SScott Feldman u32 filter_mask)) 4819815cccbfSJohn Fastabend { 4820815cccbfSJohn Fastabend struct nlmsghdr *nlh; 4821815cccbfSJohn Fastabend struct ifinfomsg *ifm; 4822815cccbfSJohn Fastabend struct nlattr *br_afspec; 48232c3c031cSScott Feldman struct nlattr *protinfo; 4824815cccbfSJohn Fastabend u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN; 4825898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 48267d4f8d87SScott Feldman int err = 0; 4827815cccbfSJohn Fastabend 482846c264daSNicolas Dichtel nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags); 4829815cccbfSJohn Fastabend if (nlh == NULL) 4830815cccbfSJohn Fastabend return -EMSGSIZE; 4831815cccbfSJohn Fastabend 4832815cccbfSJohn Fastabend ifm = nlmsg_data(nlh); 4833815cccbfSJohn Fastabend ifm->ifi_family = AF_BRIDGE; 4834815cccbfSJohn Fastabend ifm->__ifi_pad = 0; 4835815cccbfSJohn Fastabend ifm->ifi_type = dev->type; 4836815cccbfSJohn Fastabend ifm->ifi_index = dev->ifindex; 4837815cccbfSJohn Fastabend ifm->ifi_flags = dev_get_flags(dev); 4838815cccbfSJohn Fastabend ifm->ifi_change = 0; 4839815cccbfSJohn Fastabend 4840815cccbfSJohn Fastabend 4841815cccbfSJohn Fastabend if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 4842815cccbfSJohn Fastabend nla_put_u32(skb, IFLA_MTU, dev->mtu) || 4843815cccbfSJohn Fastabend nla_put_u8(skb, IFLA_OPERSTATE, operstate) || 4844898e5061SJiri Pirko (br_dev && 4845898e5061SJiri Pirko nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) || 4846815cccbfSJohn Fastabend (dev->addr_len && 4847815cccbfSJohn Fastabend nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || 4848a54acb3aSNicolas Dichtel (dev->ifindex != dev_get_iflink(dev) && 4849a54acb3aSNicolas Dichtel nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev)))) 4850815cccbfSJohn Fastabend goto nla_put_failure; 4851815cccbfSJohn Fastabend 4852ae0be8deSMichal Kubecek br_afspec = nla_nest_start_noflag(skb, IFLA_AF_SPEC); 4853815cccbfSJohn Fastabend if (!br_afspec) 4854815cccbfSJohn Fastabend goto nla_put_failure; 4855815cccbfSJohn Fastabend 48561d460b98SRoopa Prabhu if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) { 4857815cccbfSJohn Fastabend nla_nest_cancel(skb, br_afspec); 4858815cccbfSJohn Fastabend goto nla_put_failure; 4859815cccbfSJohn Fastabend } 48601d460b98SRoopa Prabhu 48611d460b98SRoopa Prabhu if (mode != BRIDGE_MODE_UNDEF) { 48621d460b98SRoopa Prabhu if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) { 48631d460b98SRoopa Prabhu nla_nest_cancel(skb, br_afspec); 48641d460b98SRoopa Prabhu goto nla_put_failure; 48651d460b98SRoopa Prabhu } 48661d460b98SRoopa Prabhu } 48677d4f8d87SScott Feldman if (vlan_fill) { 48687d4f8d87SScott Feldman err = vlan_fill(skb, dev, filter_mask); 48697d4f8d87SScott Feldman if (err) { 48707d4f8d87SScott Feldman nla_nest_cancel(skb, br_afspec); 48717d4f8d87SScott Feldman goto nla_put_failure; 48727d4f8d87SScott Feldman } 48737d4f8d87SScott Feldman } 4874815cccbfSJohn Fastabend nla_nest_end(skb, br_afspec); 4875815cccbfSJohn Fastabend 4876ae0be8deSMichal Kubecek protinfo = nla_nest_start(skb, IFLA_PROTINFO); 48772c3c031cSScott Feldman if (!protinfo) 48782c3c031cSScott Feldman goto nla_put_failure; 48792c3c031cSScott Feldman 48802c3c031cSScott Feldman if (brport_nla_put_flag(skb, flags, mask, 48812c3c031cSScott Feldman IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) || 48822c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 48832c3c031cSScott Feldman IFLA_BRPORT_GUARD, BR_BPDU_GUARD) || 48842c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 48852c3c031cSScott Feldman IFLA_BRPORT_FAST_LEAVE, 48862c3c031cSScott Feldman BR_MULTICAST_FAST_LEAVE) || 48872c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 48882c3c031cSScott Feldman IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) || 48892c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 48902c3c031cSScott Feldman IFLA_BRPORT_LEARNING, BR_LEARNING) || 48912c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 48922c3c031cSScott Feldman IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) || 48932c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 48942c3c031cSScott Feldman IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) || 48952c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 4896583cb0b4SJulian Wiedmann IFLA_BRPORT_PROXYARP, BR_PROXYARP) || 4897583cb0b4SJulian Wiedmann brport_nla_put_flag(skb, flags, mask, 4898583cb0b4SJulian Wiedmann IFLA_BRPORT_MCAST_FLOOD, BR_MCAST_FLOOD) || 4899583cb0b4SJulian Wiedmann brport_nla_put_flag(skb, flags, mask, 4900583cb0b4SJulian Wiedmann IFLA_BRPORT_BCAST_FLOOD, BR_BCAST_FLOOD)) { 49012c3c031cSScott Feldman nla_nest_cancel(skb, protinfo); 49022c3c031cSScott Feldman goto nla_put_failure; 49032c3c031cSScott Feldman } 49042c3c031cSScott Feldman 49052c3c031cSScott Feldman nla_nest_end(skb, protinfo); 49062c3c031cSScott Feldman 4907053c095aSJohannes Berg nlmsg_end(skb, nlh); 4908053c095aSJohannes Berg return 0; 4909815cccbfSJohn Fastabend nla_put_failure: 4910815cccbfSJohn Fastabend nlmsg_cancel(skb, nlh); 49117d4f8d87SScott Feldman return err ? err : -EMSGSIZE; 4912815cccbfSJohn Fastabend } 49137d4f8d87SScott Feldman EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink); 4914815cccbfSJohn Fastabend 49152d011be8SDavid Ahern static int valid_bridge_getlink_req(const struct nlmsghdr *nlh, 49162d011be8SDavid Ahern bool strict_check, u32 *filter_mask, 49172d011be8SDavid Ahern struct netlink_ext_ack *extack) 49182d011be8SDavid Ahern { 49192d011be8SDavid Ahern struct nlattr *tb[IFLA_MAX+1]; 49202d011be8SDavid Ahern int err, i; 49212d011be8SDavid Ahern 49222d011be8SDavid Ahern if (strict_check) { 49232d011be8SDavid Ahern struct ifinfomsg *ifm; 49242d011be8SDavid Ahern 49252d011be8SDavid Ahern if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { 49262d011be8SDavid Ahern NL_SET_ERR_MSG(extack, "Invalid header for bridge link dump"); 49272d011be8SDavid Ahern return -EINVAL; 49282d011be8SDavid Ahern } 49292d011be8SDavid Ahern 49302d011be8SDavid Ahern ifm = nlmsg_data(nlh); 49312d011be8SDavid Ahern if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags || 49322d011be8SDavid Ahern ifm->ifi_change || ifm->ifi_index) { 49332d011be8SDavid Ahern NL_SET_ERR_MSG(extack, "Invalid values in header for bridge link dump request"); 49342d011be8SDavid Ahern return -EINVAL; 49352d011be8SDavid Ahern } 49362d011be8SDavid Ahern 49378cb08174SJohannes Berg err = nlmsg_parse_deprecated_strict(nlh, 49388cb08174SJohannes Berg sizeof(struct ifinfomsg), 49398cb08174SJohannes Berg tb, IFLA_MAX, ifla_policy, 49408cb08174SJohannes Berg extack); 49412d011be8SDavid Ahern } else { 49428cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg), 49438cb08174SJohannes Berg tb, IFLA_MAX, ifla_policy, 49448cb08174SJohannes Berg extack); 49452d011be8SDavid Ahern } 49462d011be8SDavid Ahern if (err < 0) 49472d011be8SDavid Ahern return err; 49482d011be8SDavid Ahern 49492d011be8SDavid Ahern /* new attributes should only be added with strict checking */ 49502d011be8SDavid Ahern for (i = 0; i <= IFLA_MAX; ++i) { 49512d011be8SDavid Ahern if (!tb[i]) 49522d011be8SDavid Ahern continue; 49532d011be8SDavid Ahern 49542d011be8SDavid Ahern switch (i) { 49552d011be8SDavid Ahern case IFLA_EXT_MASK: 49562d011be8SDavid Ahern *filter_mask = nla_get_u32(tb[i]); 49572d011be8SDavid Ahern break; 49582d011be8SDavid Ahern default: 49592d011be8SDavid Ahern if (strict_check) { 49602d011be8SDavid Ahern NL_SET_ERR_MSG(extack, "Unsupported attribute in bridge link dump request"); 49612d011be8SDavid Ahern return -EINVAL; 49622d011be8SDavid Ahern } 49632d011be8SDavid Ahern } 49642d011be8SDavid Ahern } 49652d011be8SDavid Ahern 49662d011be8SDavid Ahern return 0; 49672d011be8SDavid Ahern } 49682d011be8SDavid Ahern 4969e5a55a89SJohn Fastabend static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb) 4970e5a55a89SJohn Fastabend { 49712d011be8SDavid Ahern const struct nlmsghdr *nlh = cb->nlh; 4972e5a55a89SJohn Fastabend struct net *net = sock_net(skb->sk); 4973e5a55a89SJohn Fastabend struct net_device *dev; 4974e5a55a89SJohn Fastabend int idx = 0; 4975e5a55a89SJohn Fastabend u32 portid = NETLINK_CB(cb->skb).portid; 49762d011be8SDavid Ahern u32 seq = nlh->nlmsg_seq; 49776cbdceebSVlad Yasevich u32 filter_mask = 0; 4978d64f69b0SRoopa Prabhu int err; 49796cbdceebSVlad Yasevich 49802d011be8SDavid Ahern err = valid_bridge_getlink_req(nlh, cb->strict_check, &filter_mask, 49812d011be8SDavid Ahern cb->extack); 49822d011be8SDavid Ahern if (err < 0 && cb->strict_check) 49832d011be8SDavid Ahern return err; 4984e5a55a89SJohn Fastabend 4985e5a55a89SJohn Fastabend rcu_read_lock(); 4986e5a55a89SJohn Fastabend for_each_netdev_rcu(net, dev) { 4987e5a55a89SJohn Fastabend const struct net_device_ops *ops = dev->netdev_ops; 4988898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4989e5a55a89SJohn Fastabend 4990898e5061SJiri Pirko if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) { 4991d64f69b0SRoopa Prabhu if (idx >= cb->args[0]) { 4992d64f69b0SRoopa Prabhu err = br_dev->netdev_ops->ndo_bridge_getlink( 4993d64f69b0SRoopa Prabhu skb, portid, seq, dev, 4994d64f69b0SRoopa Prabhu filter_mask, NLM_F_MULTI); 4995f6c5775fSDavid Ahern if (err < 0 && err != -EOPNOTSUPP) { 4996f6c5775fSDavid Ahern if (likely(skb->len)) 4997e5a55a89SJohn Fastabend break; 4998f6c5775fSDavid Ahern 4999f6c5775fSDavid Ahern goto out_err; 5000f6c5775fSDavid Ahern } 5001d64f69b0SRoopa Prabhu } 5002e5a55a89SJohn Fastabend idx++; 5003e5a55a89SJohn Fastabend } 5004e5a55a89SJohn Fastabend 5005e5a55a89SJohn Fastabend if (ops->ndo_bridge_getlink) { 5006d64f69b0SRoopa Prabhu if (idx >= cb->args[0]) { 5007d64f69b0SRoopa Prabhu err = ops->ndo_bridge_getlink(skb, portid, 5008d64f69b0SRoopa Prabhu seq, dev, 500946c264daSNicolas Dichtel filter_mask, 5010d64f69b0SRoopa Prabhu NLM_F_MULTI); 5011f6c5775fSDavid Ahern if (err < 0 && err != -EOPNOTSUPP) { 5012f6c5775fSDavid Ahern if (likely(skb->len)) 5013e5a55a89SJohn Fastabend break; 5014f6c5775fSDavid Ahern 5015f6c5775fSDavid Ahern goto out_err; 5016f6c5775fSDavid Ahern } 5017d64f69b0SRoopa Prabhu } 5018e5a55a89SJohn Fastabend idx++; 5019e5a55a89SJohn Fastabend } 5020e5a55a89SJohn Fastabend } 5021f6c5775fSDavid Ahern err = skb->len; 5022f6c5775fSDavid Ahern out_err: 5023e5a55a89SJohn Fastabend rcu_read_unlock(); 5024e5a55a89SJohn Fastabend cb->args[0] = idx; 5025e5a55a89SJohn Fastabend 5026f6c5775fSDavid Ahern return err; 5027e5a55a89SJohn Fastabend } 5028e5a55a89SJohn Fastabend 50292469ffd7SJohn Fastabend static inline size_t bridge_nlmsg_size(void) 50302469ffd7SJohn Fastabend { 50312469ffd7SJohn Fastabend return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 50322469ffd7SJohn Fastabend + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 50332469ffd7SJohn Fastabend + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 50342469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_MASTER */ 50352469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_MTU */ 50362469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_LINK */ 50372469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */ 50382469ffd7SJohn Fastabend + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */ 50392469ffd7SJohn Fastabend + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */ 50402469ffd7SJohn Fastabend + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */ 50412469ffd7SJohn Fastabend + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */ 50422469ffd7SJohn Fastabend } 50432469ffd7SJohn Fastabend 504402dba438SRoopa Prabhu static int rtnl_bridge_notify(struct net_device *dev) 50452469ffd7SJohn Fastabend { 50462469ffd7SJohn Fastabend struct net *net = dev_net(dev); 50472469ffd7SJohn Fastabend struct sk_buff *skb; 50482469ffd7SJohn Fastabend int err = -EOPNOTSUPP; 50492469ffd7SJohn Fastabend 505002dba438SRoopa Prabhu if (!dev->netdev_ops->ndo_bridge_getlink) 505102dba438SRoopa Prabhu return 0; 505202dba438SRoopa Prabhu 50532469ffd7SJohn Fastabend skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC); 50542469ffd7SJohn Fastabend if (!skb) { 50552469ffd7SJohn Fastabend err = -ENOMEM; 50562469ffd7SJohn Fastabend goto errout; 50572469ffd7SJohn Fastabend } 50582469ffd7SJohn Fastabend 505946c264daSNicolas Dichtel err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0); 5060c38e01b8SJohn Fastabend if (err < 0) 5061c38e01b8SJohn Fastabend goto errout; 50622469ffd7SJohn Fastabend 5063d2e381c4SIdo Schimmel /* Notification info is only filled for bridge ports, not the bridge 5064d2e381c4SIdo Schimmel * device itself. Therefore, a zero notification length is valid and 5065d2e381c4SIdo Schimmel * should not result in an error. 5066d2e381c4SIdo Schimmel */ 5067d2e381c4SIdo Schimmel if (!skb->len) 506859ccaaaaSRoopa Prabhu goto errout; 506959ccaaaaSRoopa Prabhu 50702469ffd7SJohn Fastabend rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC); 50712469ffd7SJohn Fastabend return 0; 50722469ffd7SJohn Fastabend errout: 50732469ffd7SJohn Fastabend WARN_ON(err == -EMSGSIZE); 50742469ffd7SJohn Fastabend kfree_skb(skb); 507559ccaaaaSRoopa Prabhu if (err) 50762469ffd7SJohn Fastabend rtnl_set_sk_err(net, RTNLGRP_LINK, err); 50772469ffd7SJohn Fastabend return err; 50782469ffd7SJohn Fastabend } 50792469ffd7SJohn Fastabend 5080c21ef3e3SDavid Ahern static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, 5081c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 5082e5a55a89SJohn Fastabend { 5083e5a55a89SJohn Fastabend struct net *net = sock_net(skb->sk); 5084e5a55a89SJohn Fastabend struct ifinfomsg *ifm; 5085e5a55a89SJohn Fastabend struct net_device *dev; 50862469ffd7SJohn Fastabend struct nlattr *br_spec, *attr = NULL; 50872469ffd7SJohn Fastabend int rem, err = -EOPNOTSUPP; 50884de8b413SRosen, Rami u16 flags = 0; 5089c38e01b8SJohn Fastabend bool have_flags = false; 5090e5a55a89SJohn Fastabend 5091e5a55a89SJohn Fastabend if (nlmsg_len(nlh) < sizeof(*ifm)) 5092e5a55a89SJohn Fastabend return -EINVAL; 5093e5a55a89SJohn Fastabend 5094e5a55a89SJohn Fastabend ifm = nlmsg_data(nlh); 5095e5a55a89SJohn Fastabend if (ifm->ifi_family != AF_BRIDGE) 5096e5a55a89SJohn Fastabend return -EPFNOSUPPORT; 5097e5a55a89SJohn Fastabend 5098e5a55a89SJohn Fastabend dev = __dev_get_by_index(net, ifm->ifi_index); 5099e5a55a89SJohn Fastabend if (!dev) { 5100b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 5101e5a55a89SJohn Fastabend return -ENODEV; 5102e5a55a89SJohn Fastabend } 5103e5a55a89SJohn Fastabend 51042469ffd7SJohn Fastabend br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 51052469ffd7SJohn Fastabend if (br_spec) { 51062469ffd7SJohn Fastabend nla_for_each_nested(attr, br_spec, rem) { 51072469ffd7SJohn Fastabend if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { 51086e8d1c55SThomas Graf if (nla_len(attr) < sizeof(flags)) 51096e8d1c55SThomas Graf return -EINVAL; 51106e8d1c55SThomas Graf 5111c38e01b8SJohn Fastabend have_flags = true; 51122469ffd7SJohn Fastabend flags = nla_get_u16(attr); 51132469ffd7SJohn Fastabend break; 51142469ffd7SJohn Fastabend } 51152469ffd7SJohn Fastabend } 51162469ffd7SJohn Fastabend } 51172469ffd7SJohn Fastabend 51182469ffd7SJohn Fastabend if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { 5119898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 5120898e5061SJiri Pirko 5121898e5061SJiri Pirko if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) { 51222469ffd7SJohn Fastabend err = -EOPNOTSUPP; 5123e5a55a89SJohn Fastabend goto out; 5124e5a55a89SJohn Fastabend } 5125e5a55a89SJohn Fastabend 51262fd527b7SPetr Machata err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags, 51272fd527b7SPetr Machata extack); 51282469ffd7SJohn Fastabend if (err) 51292469ffd7SJohn Fastabend goto out; 51302469ffd7SJohn Fastabend 51312469ffd7SJohn Fastabend flags &= ~BRIDGE_FLAGS_MASTER; 51322469ffd7SJohn Fastabend } 51332469ffd7SJohn Fastabend 51342469ffd7SJohn Fastabend if ((flags & BRIDGE_FLAGS_SELF)) { 51352469ffd7SJohn Fastabend if (!dev->netdev_ops->ndo_bridge_setlink) 51362469ffd7SJohn Fastabend err = -EOPNOTSUPP; 51372469ffd7SJohn Fastabend else 5138add511b3SRoopa Prabhu err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh, 51392fd527b7SPetr Machata flags, 51402fd527b7SPetr Machata extack); 514102dba438SRoopa Prabhu if (!err) { 51422469ffd7SJohn Fastabend flags &= ~BRIDGE_FLAGS_SELF; 514302dba438SRoopa Prabhu 514402dba438SRoopa Prabhu /* Generate event to notify upper layer of bridge 514502dba438SRoopa Prabhu * change 514602dba438SRoopa Prabhu */ 514702dba438SRoopa Prabhu err = rtnl_bridge_notify(dev); 514802dba438SRoopa Prabhu } 51492469ffd7SJohn Fastabend } 51502469ffd7SJohn Fastabend 5151c38e01b8SJohn Fastabend if (have_flags) 51522469ffd7SJohn Fastabend memcpy(nla_data(attr), &flags, sizeof(flags)); 5153e5a55a89SJohn Fastabend out: 5154e5a55a89SJohn Fastabend return err; 5155e5a55a89SJohn Fastabend } 5156e5a55a89SJohn Fastabend 5157c21ef3e3SDavid Ahern static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, 5158c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 5159407af329SVlad Yasevich { 5160407af329SVlad Yasevich struct net *net = sock_net(skb->sk); 5161407af329SVlad Yasevich struct ifinfomsg *ifm; 5162407af329SVlad Yasevich struct net_device *dev; 5163407af329SVlad Yasevich struct nlattr *br_spec, *attr = NULL; 5164407af329SVlad Yasevich int rem, err = -EOPNOTSUPP; 51654de8b413SRosen, Rami u16 flags = 0; 5166407af329SVlad Yasevich bool have_flags = false; 5167407af329SVlad Yasevich 5168407af329SVlad Yasevich if (nlmsg_len(nlh) < sizeof(*ifm)) 5169407af329SVlad Yasevich return -EINVAL; 5170407af329SVlad Yasevich 5171407af329SVlad Yasevich ifm = nlmsg_data(nlh); 5172407af329SVlad Yasevich if (ifm->ifi_family != AF_BRIDGE) 5173407af329SVlad Yasevich return -EPFNOSUPPORT; 5174407af329SVlad Yasevich 5175407af329SVlad Yasevich dev = __dev_get_by_index(net, ifm->ifi_index); 5176407af329SVlad Yasevich if (!dev) { 5177b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 5178407af329SVlad Yasevich return -ENODEV; 5179407af329SVlad Yasevich } 5180407af329SVlad Yasevich 5181407af329SVlad Yasevich br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 5182407af329SVlad Yasevich if (br_spec) { 5183407af329SVlad Yasevich nla_for_each_nested(attr, br_spec, rem) { 5184407af329SVlad Yasevich if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { 51856e8d1c55SThomas Graf if (nla_len(attr) < sizeof(flags)) 51866e8d1c55SThomas Graf return -EINVAL; 51876e8d1c55SThomas Graf 5188407af329SVlad Yasevich have_flags = true; 5189407af329SVlad Yasevich flags = nla_get_u16(attr); 5190407af329SVlad Yasevich break; 5191407af329SVlad Yasevich } 5192407af329SVlad Yasevich } 5193407af329SVlad Yasevich } 5194407af329SVlad Yasevich 5195407af329SVlad Yasevich if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { 5196407af329SVlad Yasevich struct net_device *br_dev = netdev_master_upper_dev_get(dev); 5197407af329SVlad Yasevich 5198407af329SVlad Yasevich if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) { 5199407af329SVlad Yasevich err = -EOPNOTSUPP; 5200407af329SVlad Yasevich goto out; 5201407af329SVlad Yasevich } 5202407af329SVlad Yasevich 5203add511b3SRoopa Prabhu err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags); 5204407af329SVlad Yasevich if (err) 5205407af329SVlad Yasevich goto out; 5206407af329SVlad Yasevich 5207407af329SVlad Yasevich flags &= ~BRIDGE_FLAGS_MASTER; 5208407af329SVlad Yasevich } 5209407af329SVlad Yasevich 5210407af329SVlad Yasevich if ((flags & BRIDGE_FLAGS_SELF)) { 5211407af329SVlad Yasevich if (!dev->netdev_ops->ndo_bridge_dellink) 5212407af329SVlad Yasevich err = -EOPNOTSUPP; 5213407af329SVlad Yasevich else 5214add511b3SRoopa Prabhu err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh, 5215add511b3SRoopa Prabhu flags); 5216407af329SVlad Yasevich 521702dba438SRoopa Prabhu if (!err) { 5218407af329SVlad Yasevich flags &= ~BRIDGE_FLAGS_SELF; 521902dba438SRoopa Prabhu 522002dba438SRoopa Prabhu /* Generate event to notify upper layer of bridge 522102dba438SRoopa Prabhu * change 522202dba438SRoopa Prabhu */ 522302dba438SRoopa Prabhu err = rtnl_bridge_notify(dev); 522402dba438SRoopa Prabhu } 5225407af329SVlad Yasevich } 5226407af329SVlad Yasevich 5227407af329SVlad Yasevich if (have_flags) 5228407af329SVlad Yasevich memcpy(nla_data(attr), &flags, sizeof(flags)); 5229407af329SVlad Yasevich out: 5230407af329SVlad Yasevich return err; 5231407af329SVlad Yasevich } 5232407af329SVlad Yasevich 5233e8872a25SNikolay Aleksandrov static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr) 5234e8872a25SNikolay Aleksandrov { 5235e8872a25SNikolay Aleksandrov return (mask & IFLA_STATS_FILTER_BIT(attrid)) && 5236e8872a25SNikolay Aleksandrov (!idxattr || idxattr == attrid); 5237e8872a25SNikolay Aleksandrov } 5238e8872a25SNikolay Aleksandrov 5239f6e0fb81SPetr Machata static bool 5240f6e0fb81SPetr Machata rtnl_offload_xstats_have_ndo(const struct net_device *dev, int attr_id) 524169ae6ad2SNogah Frankel { 5242f6e0fb81SPetr Machata return dev->netdev_ops && 5243f6e0fb81SPetr Machata dev->netdev_ops->ndo_has_offload_stats && 5244f6e0fb81SPetr Machata dev->netdev_ops->ndo_get_offload_stats && 5245f6e0fb81SPetr Machata dev->netdev_ops->ndo_has_offload_stats(dev, attr_id); 524669ae6ad2SNogah Frankel } 524769ae6ad2SNogah Frankel 5248f6e0fb81SPetr Machata static unsigned int 5249f6e0fb81SPetr Machata rtnl_offload_xstats_get_size_ndo(const struct net_device *dev, int attr_id) 5250f6e0fb81SPetr Machata { 5251f6e0fb81SPetr Machata return rtnl_offload_xstats_have_ndo(dev, attr_id) ? 5252f6e0fb81SPetr Machata sizeof(struct rtnl_link_stats64) : 0; 5253f6e0fb81SPetr Machata } 5254f6e0fb81SPetr Machata 5255f6e0fb81SPetr Machata static int 5256f6e0fb81SPetr Machata rtnl_offload_xstats_fill_ndo(struct net_device *dev, int attr_id, 5257f6e0fb81SPetr Machata struct sk_buff *skb) 5258f6e0fb81SPetr Machata { 5259f6e0fb81SPetr Machata unsigned int size = rtnl_offload_xstats_get_size_ndo(dev, attr_id); 5260f6e0fb81SPetr Machata struct nlattr *attr = NULL; 5261f6e0fb81SPetr Machata void *attr_data; 5262f6e0fb81SPetr Machata int err; 5263f6e0fb81SPetr Machata 5264f6e0fb81SPetr Machata if (!size) 5265f6e0fb81SPetr Machata return -ENODATA; 5266f6e0fb81SPetr Machata 5267f6e0fb81SPetr Machata attr = nla_reserve_64bit(skb, attr_id, size, 5268f6e0fb81SPetr Machata IFLA_OFFLOAD_XSTATS_UNSPEC); 5269f6e0fb81SPetr Machata if (!attr) 5270f6e0fb81SPetr Machata return -EMSGSIZE; 5271f6e0fb81SPetr Machata 5272f6e0fb81SPetr Machata attr_data = nla_data(attr); 5273f6e0fb81SPetr Machata memset(attr_data, 0, size); 5274f6e0fb81SPetr Machata 5275f6e0fb81SPetr Machata err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev, attr_data); 5276f6e0fb81SPetr Machata if (err) 5277f6e0fb81SPetr Machata return err; 5278f6e0fb81SPetr Machata 527969ae6ad2SNogah Frankel return 0; 528069ae6ad2SNogah Frankel } 528169ae6ad2SNogah Frankel 52820e7788fdSPetr Machata static unsigned int 52830e7788fdSPetr Machata rtnl_offload_xstats_get_size_stats(const struct net_device *dev, 52840e7788fdSPetr Machata enum netdev_offload_xstats_type type) 52850e7788fdSPetr Machata { 52860e7788fdSPetr Machata bool enabled = netdev_offload_xstats_enabled(dev, type); 52870e7788fdSPetr Machata 52880e7788fdSPetr Machata return enabled ? sizeof(struct rtnl_hw_stats64) : 0; 52890e7788fdSPetr Machata } 52900e7788fdSPetr Machata 52910e7788fdSPetr Machata struct rtnl_offload_xstats_request_used { 52920e7788fdSPetr Machata bool request; 52930e7788fdSPetr Machata bool used; 52940e7788fdSPetr Machata }; 52950e7788fdSPetr Machata 52960e7788fdSPetr Machata static int 52970e7788fdSPetr Machata rtnl_offload_xstats_get_stats(struct net_device *dev, 52980e7788fdSPetr Machata enum netdev_offload_xstats_type type, 52990e7788fdSPetr Machata struct rtnl_offload_xstats_request_used *ru, 53000e7788fdSPetr Machata struct rtnl_hw_stats64 *stats, 53010e7788fdSPetr Machata struct netlink_ext_ack *extack) 53020e7788fdSPetr Machata { 53030e7788fdSPetr Machata bool request; 53040e7788fdSPetr Machata bool used; 53050e7788fdSPetr Machata int err; 53060e7788fdSPetr Machata 53070e7788fdSPetr Machata request = netdev_offload_xstats_enabled(dev, type); 53080e7788fdSPetr Machata if (!request) { 53090e7788fdSPetr Machata used = false; 53100e7788fdSPetr Machata goto out; 53110e7788fdSPetr Machata } 53120e7788fdSPetr Machata 53130e7788fdSPetr Machata err = netdev_offload_xstats_get(dev, type, stats, &used, extack); 53140e7788fdSPetr Machata if (err) 53150e7788fdSPetr Machata return err; 53160e7788fdSPetr Machata 53170e7788fdSPetr Machata out: 53180e7788fdSPetr Machata if (ru) { 53190e7788fdSPetr Machata ru->request = request; 53200e7788fdSPetr Machata ru->used = used; 53210e7788fdSPetr Machata } 53220e7788fdSPetr Machata return 0; 53230e7788fdSPetr Machata } 53240e7788fdSPetr Machata 53250e7788fdSPetr Machata static int 53260e7788fdSPetr Machata rtnl_offload_xstats_fill_hw_s_info_one(struct sk_buff *skb, int attr_id, 53270e7788fdSPetr Machata struct rtnl_offload_xstats_request_used *ru) 53280e7788fdSPetr Machata { 53290e7788fdSPetr Machata struct nlattr *nest; 53300e7788fdSPetr Machata 53310e7788fdSPetr Machata nest = nla_nest_start(skb, attr_id); 53320e7788fdSPetr Machata if (!nest) 53330e7788fdSPetr Machata return -EMSGSIZE; 53340e7788fdSPetr Machata 53350e7788fdSPetr Machata if (nla_put_u8(skb, IFLA_OFFLOAD_XSTATS_HW_S_INFO_REQUEST, ru->request)) 53360e7788fdSPetr Machata goto nla_put_failure; 53370e7788fdSPetr Machata 53380e7788fdSPetr Machata if (nla_put_u8(skb, IFLA_OFFLOAD_XSTATS_HW_S_INFO_USED, ru->used)) 53390e7788fdSPetr Machata goto nla_put_failure; 53400e7788fdSPetr Machata 53410e7788fdSPetr Machata nla_nest_end(skb, nest); 53420e7788fdSPetr Machata return 0; 53430e7788fdSPetr Machata 53440e7788fdSPetr Machata nla_put_failure: 53450e7788fdSPetr Machata nla_nest_cancel(skb, nest); 53460e7788fdSPetr Machata return -EMSGSIZE; 53470e7788fdSPetr Machata } 53480e7788fdSPetr Machata 53490e7788fdSPetr Machata static int 53500e7788fdSPetr Machata rtnl_offload_xstats_fill_hw_s_info(struct sk_buff *skb, struct net_device *dev, 53510e7788fdSPetr Machata struct netlink_ext_ack *extack) 53520e7788fdSPetr Machata { 53530e7788fdSPetr Machata enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3; 53540e7788fdSPetr Machata struct rtnl_offload_xstats_request_used ru_l3; 53550e7788fdSPetr Machata struct nlattr *nest; 53560e7788fdSPetr Machata int err; 53570e7788fdSPetr Machata 53580e7788fdSPetr Machata err = rtnl_offload_xstats_get_stats(dev, t_l3, &ru_l3, NULL, extack); 53590e7788fdSPetr Machata if (err) 53600e7788fdSPetr Machata return err; 53610e7788fdSPetr Machata 53620e7788fdSPetr Machata nest = nla_nest_start(skb, IFLA_OFFLOAD_XSTATS_HW_S_INFO); 53630e7788fdSPetr Machata if (!nest) 53640e7788fdSPetr Machata return -EMSGSIZE; 53650e7788fdSPetr Machata 53660e7788fdSPetr Machata if (rtnl_offload_xstats_fill_hw_s_info_one(skb, 53670e7788fdSPetr Machata IFLA_OFFLOAD_XSTATS_L3_STATS, 53680e7788fdSPetr Machata &ru_l3)) 53690e7788fdSPetr Machata goto nla_put_failure; 53700e7788fdSPetr Machata 53710e7788fdSPetr Machata nla_nest_end(skb, nest); 53720e7788fdSPetr Machata return 0; 53730e7788fdSPetr Machata 53740e7788fdSPetr Machata nla_put_failure: 53750e7788fdSPetr Machata nla_nest_cancel(skb, nest); 53760e7788fdSPetr Machata return -EMSGSIZE; 53770e7788fdSPetr Machata } 53780e7788fdSPetr Machata 53796b524a1dSPetr Machata static int rtnl_offload_xstats_fill(struct sk_buff *skb, struct net_device *dev, 538005415bccSPetr Machata int *prividx, u32 off_filter_mask, 538105415bccSPetr Machata struct netlink_ext_ack *extack) 538269ae6ad2SNogah Frankel { 53830e7788fdSPetr Machata enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3; 53840e7788fdSPetr Machata int attr_id_hw_s_info = IFLA_OFFLOAD_XSTATS_HW_S_INFO; 53850e7788fdSPetr Machata int attr_id_l3_stats = IFLA_OFFLOAD_XSTATS_L3_STATS; 5386f6e0fb81SPetr Machata int attr_id_cpu_hit = IFLA_OFFLOAD_XSTATS_CPU_HIT; 5387f6e0fb81SPetr Machata bool have_data = false; 538869ae6ad2SNogah Frankel int err; 538969ae6ad2SNogah Frankel 539046efc97bSPetr Machata if (*prividx <= attr_id_cpu_hit && 539146efc97bSPetr Machata (off_filter_mask & 539246efc97bSPetr Machata IFLA_STATS_FILTER_BIT(attr_id_cpu_hit))) { 5393f6e0fb81SPetr Machata err = rtnl_offload_xstats_fill_ndo(dev, attr_id_cpu_hit, skb); 5394f6e0fb81SPetr Machata if (!err) { 5395f6e0fb81SPetr Machata have_data = true; 5396f6e0fb81SPetr Machata } else if (err != -ENODATA) { 5397f6e0fb81SPetr Machata *prividx = attr_id_cpu_hit; 5398f6e0fb81SPetr Machata return err; 5399f6e0fb81SPetr Machata } 540069ae6ad2SNogah Frankel } 540169ae6ad2SNogah Frankel 54020e7788fdSPetr Machata if (*prividx <= attr_id_hw_s_info && 54030e7788fdSPetr Machata (off_filter_mask & IFLA_STATS_FILTER_BIT(attr_id_hw_s_info))) { 54040e7788fdSPetr Machata *prividx = attr_id_hw_s_info; 54050e7788fdSPetr Machata 54060e7788fdSPetr Machata err = rtnl_offload_xstats_fill_hw_s_info(skb, dev, extack); 54070e7788fdSPetr Machata if (err) 54080e7788fdSPetr Machata return err; 54090e7788fdSPetr Machata 54100e7788fdSPetr Machata have_data = true; 54110e7788fdSPetr Machata *prividx = 0; 54120e7788fdSPetr Machata } 54130e7788fdSPetr Machata 54140e7788fdSPetr Machata if (*prividx <= attr_id_l3_stats && 54150e7788fdSPetr Machata (off_filter_mask & IFLA_STATS_FILTER_BIT(attr_id_l3_stats))) { 54160e7788fdSPetr Machata unsigned int size_l3; 54170e7788fdSPetr Machata struct nlattr *attr; 54180e7788fdSPetr Machata 54190e7788fdSPetr Machata *prividx = attr_id_l3_stats; 54200e7788fdSPetr Machata 54210e7788fdSPetr Machata size_l3 = rtnl_offload_xstats_get_size_stats(dev, t_l3); 542223cfe941SPetr Machata if (!size_l3) 542323cfe941SPetr Machata goto skip_l3_stats; 54240e7788fdSPetr Machata attr = nla_reserve_64bit(skb, attr_id_l3_stats, size_l3, 54250e7788fdSPetr Machata IFLA_OFFLOAD_XSTATS_UNSPEC); 54260e7788fdSPetr Machata if (!attr) 54270e7788fdSPetr Machata return -EMSGSIZE; 54280e7788fdSPetr Machata 54290e7788fdSPetr Machata err = rtnl_offload_xstats_get_stats(dev, t_l3, NULL, 54300e7788fdSPetr Machata nla_data(attr), extack); 54310e7788fdSPetr Machata if (err) 54320e7788fdSPetr Machata return err; 54330e7788fdSPetr Machata 54340e7788fdSPetr Machata have_data = true; 543523cfe941SPetr Machata skip_l3_stats: 54360e7788fdSPetr Machata *prividx = 0; 54370e7788fdSPetr Machata } 54380e7788fdSPetr Machata 5439f6e0fb81SPetr Machata if (!have_data) 544069ae6ad2SNogah Frankel return -ENODATA; 544169ae6ad2SNogah Frankel 544269ae6ad2SNogah Frankel *prividx = 0; 544369ae6ad2SNogah Frankel return 0; 544469ae6ad2SNogah Frankel } 544569ae6ad2SNogah Frankel 54460e7788fdSPetr Machata static unsigned int 54470e7788fdSPetr Machata rtnl_offload_xstats_get_size_hw_s_info_one(const struct net_device *dev, 54480e7788fdSPetr Machata enum netdev_offload_xstats_type type) 54490e7788fdSPetr Machata { 54500e7788fdSPetr Machata bool enabled = netdev_offload_xstats_enabled(dev, type); 54510e7788fdSPetr Machata 54520e7788fdSPetr Machata return nla_total_size(0) + 54530e7788fdSPetr Machata /* IFLA_OFFLOAD_XSTATS_HW_S_INFO_REQUEST */ 54540e7788fdSPetr Machata nla_total_size(sizeof(u8)) + 54550e7788fdSPetr Machata /* IFLA_OFFLOAD_XSTATS_HW_S_INFO_USED */ 54560e7788fdSPetr Machata (enabled ? nla_total_size(sizeof(u8)) : 0) + 54570e7788fdSPetr Machata 0; 54580e7788fdSPetr Machata } 54590e7788fdSPetr Machata 54600e7788fdSPetr Machata static unsigned int 54610e7788fdSPetr Machata rtnl_offload_xstats_get_size_hw_s_info(const struct net_device *dev) 54620e7788fdSPetr Machata { 54630e7788fdSPetr Machata enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3; 54640e7788fdSPetr Machata 54650e7788fdSPetr Machata return nla_total_size(0) + 54660e7788fdSPetr Machata /* IFLA_OFFLOAD_XSTATS_L3_STATS */ 54670e7788fdSPetr Machata rtnl_offload_xstats_get_size_hw_s_info_one(dev, t_l3) + 54680e7788fdSPetr Machata 0; 54690e7788fdSPetr Machata } 54700e7788fdSPetr Machata 547146efc97bSPetr Machata static int rtnl_offload_xstats_get_size(const struct net_device *dev, 547246efc97bSPetr Machata u32 off_filter_mask) 547369ae6ad2SNogah Frankel { 54740e7788fdSPetr Machata enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3; 5475f6e0fb81SPetr Machata int attr_id_cpu_hit = IFLA_OFFLOAD_XSTATS_CPU_HIT; 547669ae6ad2SNogah Frankel int nla_size = 0; 547769ae6ad2SNogah Frankel int size; 547869ae6ad2SNogah Frankel 547946efc97bSPetr Machata if (off_filter_mask & 548046efc97bSPetr Machata IFLA_STATS_FILTER_BIT(attr_id_cpu_hit)) { 5481f6e0fb81SPetr Machata size = rtnl_offload_xstats_get_size_ndo(dev, attr_id_cpu_hit); 548269ae6ad2SNogah Frankel nla_size += nla_total_size_64bit(size); 548346efc97bSPetr Machata } 548469ae6ad2SNogah Frankel 54850e7788fdSPetr Machata if (off_filter_mask & 54860e7788fdSPetr Machata IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_HW_S_INFO)) 54870e7788fdSPetr Machata nla_size += rtnl_offload_xstats_get_size_hw_s_info(dev); 54880e7788fdSPetr Machata 54890e7788fdSPetr Machata if (off_filter_mask & 54900e7788fdSPetr Machata IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_L3_STATS)) { 54910e7788fdSPetr Machata size = rtnl_offload_xstats_get_size_stats(dev, t_l3); 54920e7788fdSPetr Machata nla_size += nla_total_size_64bit(size); 54930e7788fdSPetr Machata } 54940e7788fdSPetr Machata 549569ae6ad2SNogah Frankel if (nla_size != 0) 549669ae6ad2SNogah Frankel nla_size += nla_total_size(0); 549769ae6ad2SNogah Frankel 549869ae6ad2SNogah Frankel return nla_size; 549969ae6ad2SNogah Frankel } 550069ae6ad2SNogah Frankel 550146efc97bSPetr Machata struct rtnl_stats_dump_filters { 550246efc97bSPetr Machata /* mask[0] filters outer attributes. Then individual nests have their 550346efc97bSPetr Machata * filtering mask at the index of the nested attribute. 550446efc97bSPetr Machata */ 550546efc97bSPetr Machata u32 mask[IFLA_STATS_MAX + 1]; 550646efc97bSPetr Machata }; 550746efc97bSPetr Machata 550810c9ead9SRoopa Prabhu static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev, 550910c9ead9SRoopa Prabhu int type, u32 pid, u32 seq, u32 change, 551046efc97bSPetr Machata unsigned int flags, 551146efc97bSPetr Machata const struct rtnl_stats_dump_filters *filters, 551205415bccSPetr Machata int *idxattr, int *prividx, 551305415bccSPetr Machata struct netlink_ext_ack *extack) 551410c9ead9SRoopa Prabhu { 551546efc97bSPetr Machata unsigned int filter_mask = filters->mask[0]; 551610c9ead9SRoopa Prabhu struct if_stats_msg *ifsm; 551710c9ead9SRoopa Prabhu struct nlmsghdr *nlh; 551810c9ead9SRoopa Prabhu struct nlattr *attr; 5519e8872a25SNikolay Aleksandrov int s_prividx = *prividx; 552069ae6ad2SNogah Frankel int err; 552110c9ead9SRoopa Prabhu 552210c9ead9SRoopa Prabhu ASSERT_RTNL(); 552310c9ead9SRoopa Prabhu 552410c9ead9SRoopa Prabhu nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags); 552510c9ead9SRoopa Prabhu if (!nlh) 552610c9ead9SRoopa Prabhu return -EMSGSIZE; 552710c9ead9SRoopa Prabhu 552810c9ead9SRoopa Prabhu ifsm = nlmsg_data(nlh); 5529ce024f42SNikolay Aleksandrov ifsm->family = PF_UNSPEC; 5530ce024f42SNikolay Aleksandrov ifsm->pad1 = 0; 5531ce024f42SNikolay Aleksandrov ifsm->pad2 = 0; 553210c9ead9SRoopa Prabhu ifsm->ifindex = dev->ifindex; 553310c9ead9SRoopa Prabhu ifsm->filter_mask = filter_mask; 553410c9ead9SRoopa Prabhu 5535e8872a25SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) { 553610c9ead9SRoopa Prabhu struct rtnl_link_stats64 *sp; 553710c9ead9SRoopa Prabhu 553858414d32SNicolas Dichtel attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64, 553958414d32SNicolas Dichtel sizeof(struct rtnl_link_stats64), 554058414d32SNicolas Dichtel IFLA_STATS_UNSPEC); 5541216e6906SPetr Machata if (!attr) { 5542216e6906SPetr Machata err = -EMSGSIZE; 554310c9ead9SRoopa Prabhu goto nla_put_failure; 5544216e6906SPetr Machata } 554510c9ead9SRoopa Prabhu 554610c9ead9SRoopa Prabhu sp = nla_data(attr); 554710c9ead9SRoopa Prabhu dev_get_stats(dev, sp); 554810c9ead9SRoopa Prabhu } 554910c9ead9SRoopa Prabhu 555097a47facSNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) { 555197a47facSNikolay Aleksandrov const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 555297a47facSNikolay Aleksandrov 555397a47facSNikolay Aleksandrov if (ops && ops->fill_linkxstats) { 555497a47facSNikolay Aleksandrov *idxattr = IFLA_STATS_LINK_XSTATS; 5555ae0be8deSMichal Kubecek attr = nla_nest_start_noflag(skb, 555697a47facSNikolay Aleksandrov IFLA_STATS_LINK_XSTATS); 5557216e6906SPetr Machata if (!attr) { 5558216e6906SPetr Machata err = -EMSGSIZE; 555997a47facSNikolay Aleksandrov goto nla_put_failure; 5560216e6906SPetr Machata } 556197a47facSNikolay Aleksandrov 556280e73cc5SNikolay Aleksandrov err = ops->fill_linkxstats(skb, dev, prividx, *idxattr); 556380e73cc5SNikolay Aleksandrov nla_nest_end(skb, attr); 556480e73cc5SNikolay Aleksandrov if (err) 556580e73cc5SNikolay Aleksandrov goto nla_put_failure; 556680e73cc5SNikolay Aleksandrov *idxattr = 0; 556780e73cc5SNikolay Aleksandrov } 556880e73cc5SNikolay Aleksandrov } 556980e73cc5SNikolay Aleksandrov 557080e73cc5SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 557180e73cc5SNikolay Aleksandrov *idxattr)) { 557280e73cc5SNikolay Aleksandrov const struct rtnl_link_ops *ops = NULL; 557380e73cc5SNikolay Aleksandrov const struct net_device *master; 557480e73cc5SNikolay Aleksandrov 557580e73cc5SNikolay Aleksandrov master = netdev_master_upper_dev_get(dev); 557680e73cc5SNikolay Aleksandrov if (master) 557780e73cc5SNikolay Aleksandrov ops = master->rtnl_link_ops; 557880e73cc5SNikolay Aleksandrov if (ops && ops->fill_linkxstats) { 557980e73cc5SNikolay Aleksandrov *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE; 5580ae0be8deSMichal Kubecek attr = nla_nest_start_noflag(skb, 558180e73cc5SNikolay Aleksandrov IFLA_STATS_LINK_XSTATS_SLAVE); 5582216e6906SPetr Machata if (!attr) { 5583216e6906SPetr Machata err = -EMSGSIZE; 558480e73cc5SNikolay Aleksandrov goto nla_put_failure; 5585216e6906SPetr Machata } 558680e73cc5SNikolay Aleksandrov 558780e73cc5SNikolay Aleksandrov err = ops->fill_linkxstats(skb, dev, prividx, *idxattr); 558897a47facSNikolay Aleksandrov nla_nest_end(skb, attr); 558997a47facSNikolay Aleksandrov if (err) 559097a47facSNikolay Aleksandrov goto nla_put_failure; 559197a47facSNikolay Aleksandrov *idxattr = 0; 559297a47facSNikolay Aleksandrov } 559397a47facSNikolay Aleksandrov } 559497a47facSNikolay Aleksandrov 559569ae6ad2SNogah Frankel if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 559669ae6ad2SNogah Frankel *idxattr)) { 559746efc97bSPetr Machata u32 off_filter_mask; 559846efc97bSPetr Machata 559946efc97bSPetr Machata off_filter_mask = filters->mask[IFLA_STATS_LINK_OFFLOAD_XSTATS]; 560069ae6ad2SNogah Frankel *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS; 5601ae0be8deSMichal Kubecek attr = nla_nest_start_noflag(skb, 5602ae0be8deSMichal Kubecek IFLA_STATS_LINK_OFFLOAD_XSTATS); 5603216e6906SPetr Machata if (!attr) { 5604216e6906SPetr Machata err = -EMSGSIZE; 560569ae6ad2SNogah Frankel goto nla_put_failure; 5606216e6906SPetr Machata } 560769ae6ad2SNogah Frankel 560846efc97bSPetr Machata err = rtnl_offload_xstats_fill(skb, dev, prividx, 560905415bccSPetr Machata off_filter_mask, extack); 561069ae6ad2SNogah Frankel if (err == -ENODATA) 561169ae6ad2SNogah Frankel nla_nest_cancel(skb, attr); 561269ae6ad2SNogah Frankel else 561369ae6ad2SNogah Frankel nla_nest_end(skb, attr); 561469ae6ad2SNogah Frankel 561569ae6ad2SNogah Frankel if (err && err != -ENODATA) 561669ae6ad2SNogah Frankel goto nla_put_failure; 561769ae6ad2SNogah Frankel *idxattr = 0; 561869ae6ad2SNogah Frankel } 561969ae6ad2SNogah Frankel 5620aefb4d4aSRobert Shearman if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) { 5621aefb4d4aSRobert Shearman struct rtnl_af_ops *af_ops; 5622aefb4d4aSRobert Shearman 5623aefb4d4aSRobert Shearman *idxattr = IFLA_STATS_AF_SPEC; 5624ae0be8deSMichal Kubecek attr = nla_nest_start_noflag(skb, IFLA_STATS_AF_SPEC); 5625216e6906SPetr Machata if (!attr) { 5626216e6906SPetr Machata err = -EMSGSIZE; 5627aefb4d4aSRobert Shearman goto nla_put_failure; 5628216e6906SPetr Machata } 5629aefb4d4aSRobert Shearman 56305fa85a09SFlorian Westphal rcu_read_lock(); 56315fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 5632aefb4d4aSRobert Shearman if (af_ops->fill_stats_af) { 5633aefb4d4aSRobert Shearman struct nlattr *af; 5634aefb4d4aSRobert Shearman 5635ae0be8deSMichal Kubecek af = nla_nest_start_noflag(skb, 5636ae0be8deSMichal Kubecek af_ops->family); 56375fa85a09SFlorian Westphal if (!af) { 56385fa85a09SFlorian Westphal rcu_read_unlock(); 563957d29a29STom Rix err = -EMSGSIZE; 5640aefb4d4aSRobert Shearman goto nla_put_failure; 56415fa85a09SFlorian Westphal } 5642aefb4d4aSRobert Shearman err = af_ops->fill_stats_af(skb, dev); 5643aefb4d4aSRobert Shearman 56445fa85a09SFlorian Westphal if (err == -ENODATA) { 5645aefb4d4aSRobert Shearman nla_nest_cancel(skb, af); 56465fa85a09SFlorian Westphal } else if (err < 0) { 56475fa85a09SFlorian Westphal rcu_read_unlock(); 5648aefb4d4aSRobert Shearman goto nla_put_failure; 56495fa85a09SFlorian Westphal } 5650aefb4d4aSRobert Shearman 5651aefb4d4aSRobert Shearman nla_nest_end(skb, af); 5652aefb4d4aSRobert Shearman } 5653aefb4d4aSRobert Shearman } 56545fa85a09SFlorian Westphal rcu_read_unlock(); 5655aefb4d4aSRobert Shearman 5656aefb4d4aSRobert Shearman nla_nest_end(skb, attr); 5657aefb4d4aSRobert Shearman 5658aefb4d4aSRobert Shearman *idxattr = 0; 5659aefb4d4aSRobert Shearman } 5660aefb4d4aSRobert Shearman 566110c9ead9SRoopa Prabhu nlmsg_end(skb, nlh); 566210c9ead9SRoopa Prabhu 566310c9ead9SRoopa Prabhu return 0; 566410c9ead9SRoopa Prabhu 566510c9ead9SRoopa Prabhu nla_put_failure: 5666e8872a25SNikolay Aleksandrov /* not a multi message or no progress mean a real error */ 5667e8872a25SNikolay Aleksandrov if (!(flags & NLM_F_MULTI) || s_prividx == *prividx) 566810c9ead9SRoopa Prabhu nlmsg_cancel(skb, nlh); 5669e8872a25SNikolay Aleksandrov else 5670e8872a25SNikolay Aleksandrov nlmsg_end(skb, nlh); 567110c9ead9SRoopa Prabhu 5672216e6906SPetr Machata return err; 567310c9ead9SRoopa Prabhu } 567410c9ead9SRoopa Prabhu 567510c9ead9SRoopa Prabhu static size_t if_nlmsg_stats_size(const struct net_device *dev, 567646efc97bSPetr Machata const struct rtnl_stats_dump_filters *filters) 567710c9ead9SRoopa Prabhu { 5678d3436799SEric Dumazet size_t size = NLMSG_ALIGN(sizeof(struct if_stats_msg)); 567946efc97bSPetr Machata unsigned int filter_mask = filters->mask[0]; 568010c9ead9SRoopa Prabhu 5681e8872a25SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0)) 568210c9ead9SRoopa Prabhu size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64)); 568310c9ead9SRoopa Prabhu 568497a47facSNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) { 568597a47facSNikolay Aleksandrov const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 568680e73cc5SNikolay Aleksandrov int attr = IFLA_STATS_LINK_XSTATS; 568797a47facSNikolay Aleksandrov 568897a47facSNikolay Aleksandrov if (ops && ops->get_linkxstats_size) { 568980e73cc5SNikolay Aleksandrov size += nla_total_size(ops->get_linkxstats_size(dev, 569080e73cc5SNikolay Aleksandrov attr)); 569197a47facSNikolay Aleksandrov /* for IFLA_STATS_LINK_XSTATS */ 569297a47facSNikolay Aleksandrov size += nla_total_size(0); 569397a47facSNikolay Aleksandrov } 569497a47facSNikolay Aleksandrov } 569597a47facSNikolay Aleksandrov 569680e73cc5SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) { 569780e73cc5SNikolay Aleksandrov struct net_device *_dev = (struct net_device *)dev; 569880e73cc5SNikolay Aleksandrov const struct rtnl_link_ops *ops = NULL; 569980e73cc5SNikolay Aleksandrov const struct net_device *master; 570080e73cc5SNikolay Aleksandrov 570180e73cc5SNikolay Aleksandrov /* netdev_master_upper_dev_get can't take const */ 570280e73cc5SNikolay Aleksandrov master = netdev_master_upper_dev_get(_dev); 570380e73cc5SNikolay Aleksandrov if (master) 570480e73cc5SNikolay Aleksandrov ops = master->rtnl_link_ops; 570580e73cc5SNikolay Aleksandrov if (ops && ops->get_linkxstats_size) { 570680e73cc5SNikolay Aleksandrov int attr = IFLA_STATS_LINK_XSTATS_SLAVE; 570780e73cc5SNikolay Aleksandrov 570880e73cc5SNikolay Aleksandrov size += nla_total_size(ops->get_linkxstats_size(dev, 570980e73cc5SNikolay Aleksandrov attr)); 571080e73cc5SNikolay Aleksandrov /* for IFLA_STATS_LINK_XSTATS_SLAVE */ 571180e73cc5SNikolay Aleksandrov size += nla_total_size(0); 571280e73cc5SNikolay Aleksandrov } 571380e73cc5SNikolay Aleksandrov } 571480e73cc5SNikolay Aleksandrov 571546efc97bSPetr Machata if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0)) { 571646efc97bSPetr Machata u32 off_filter_mask; 571746efc97bSPetr Machata 571846efc97bSPetr Machata off_filter_mask = filters->mask[IFLA_STATS_LINK_OFFLOAD_XSTATS]; 571946efc97bSPetr Machata size += rtnl_offload_xstats_get_size(dev, off_filter_mask); 572046efc97bSPetr Machata } 572169ae6ad2SNogah Frankel 5722aefb4d4aSRobert Shearman if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) { 5723aefb4d4aSRobert Shearman struct rtnl_af_ops *af_ops; 5724aefb4d4aSRobert Shearman 5725aefb4d4aSRobert Shearman /* for IFLA_STATS_AF_SPEC */ 5726aefb4d4aSRobert Shearman size += nla_total_size(0); 5727aefb4d4aSRobert Shearman 57285fa85a09SFlorian Westphal rcu_read_lock(); 57295fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 5730aefb4d4aSRobert Shearman if (af_ops->get_stats_af_size) { 5731aefb4d4aSRobert Shearman size += nla_total_size( 5732aefb4d4aSRobert Shearman af_ops->get_stats_af_size(dev)); 5733aefb4d4aSRobert Shearman 5734aefb4d4aSRobert Shearman /* for AF_* */ 5735aefb4d4aSRobert Shearman size += nla_total_size(0); 5736aefb4d4aSRobert Shearman } 5737aefb4d4aSRobert Shearman } 57385fa85a09SFlorian Westphal rcu_read_unlock(); 5739aefb4d4aSRobert Shearman } 5740aefb4d4aSRobert Shearman 574110c9ead9SRoopa Prabhu return size; 574210c9ead9SRoopa Prabhu } 574310c9ead9SRoopa Prabhu 574446efc97bSPetr Machata #define RTNL_STATS_OFFLOAD_XSTATS_VALID ((1 << __IFLA_OFFLOAD_XSTATS_MAX) - 1) 574546efc97bSPetr Machata 574646efc97bSPetr Machata static const struct nla_policy 574746efc97bSPetr Machata rtnl_stats_get_policy_filters[IFLA_STATS_MAX + 1] = { 574846efc97bSPetr Machata [IFLA_STATS_LINK_OFFLOAD_XSTATS] = 574946efc97bSPetr Machata NLA_POLICY_MASK(NLA_U32, RTNL_STATS_OFFLOAD_XSTATS_VALID), 575046efc97bSPetr Machata }; 575146efc97bSPetr Machata 575246efc97bSPetr Machata static const struct nla_policy 575346efc97bSPetr Machata rtnl_stats_get_policy[IFLA_STATS_GETSET_MAX + 1] = { 575446efc97bSPetr Machata [IFLA_STATS_GET_FILTERS] = 575546efc97bSPetr Machata NLA_POLICY_NESTED(rtnl_stats_get_policy_filters), 575646efc97bSPetr Machata }; 575746efc97bSPetr Machata 575803ba3566SPetr Machata static const struct nla_policy 575903ba3566SPetr Machata ifla_stats_set_policy[IFLA_STATS_GETSET_MAX + 1] = { 57605fd0b838SPetr Machata [IFLA_STATS_SET_OFFLOAD_XSTATS_L3_STATS] = NLA_POLICY_MAX(NLA_U8, 1), 576103ba3566SPetr Machata }; 576203ba3566SPetr Machata 576346efc97bSPetr Machata static int rtnl_stats_get_parse_filters(struct nlattr *ifla_filters, 576446efc97bSPetr Machata struct rtnl_stats_dump_filters *filters, 576546efc97bSPetr Machata struct netlink_ext_ack *extack) 576646efc97bSPetr Machata { 576746efc97bSPetr Machata struct nlattr *tb[IFLA_STATS_MAX + 1]; 576846efc97bSPetr Machata int err; 576946efc97bSPetr Machata int at; 577046efc97bSPetr Machata 577146efc97bSPetr Machata err = nla_parse_nested(tb, IFLA_STATS_MAX, ifla_filters, 577246efc97bSPetr Machata rtnl_stats_get_policy_filters, extack); 577346efc97bSPetr Machata if (err < 0) 577446efc97bSPetr Machata return err; 577546efc97bSPetr Machata 577646efc97bSPetr Machata for (at = 1; at <= IFLA_STATS_MAX; at++) { 577746efc97bSPetr Machata if (tb[at]) { 577846efc97bSPetr Machata if (!(filters->mask[0] & IFLA_STATS_FILTER_BIT(at))) { 577946efc97bSPetr Machata NL_SET_ERR_MSG(extack, "Filtered attribute not enabled in filter_mask"); 578046efc97bSPetr Machata return -EINVAL; 578146efc97bSPetr Machata } 578246efc97bSPetr Machata filters->mask[at] = nla_get_u32(tb[at]); 578346efc97bSPetr Machata } 578446efc97bSPetr Machata } 578546efc97bSPetr Machata 578646efc97bSPetr Machata return 0; 578746efc97bSPetr Machata } 578846efc97bSPetr Machata 578946efc97bSPetr Machata static int rtnl_stats_get_parse(const struct nlmsghdr *nlh, 579046efc97bSPetr Machata u32 filter_mask, 579146efc97bSPetr Machata struct rtnl_stats_dump_filters *filters, 579246efc97bSPetr Machata struct netlink_ext_ack *extack) 579346efc97bSPetr Machata { 579446efc97bSPetr Machata struct nlattr *tb[IFLA_STATS_GETSET_MAX + 1]; 579546efc97bSPetr Machata int err; 579646efc97bSPetr Machata int i; 579746efc97bSPetr Machata 579846efc97bSPetr Machata filters->mask[0] = filter_mask; 579946efc97bSPetr Machata for (i = 1; i < ARRAY_SIZE(filters->mask); i++) 580046efc97bSPetr Machata filters->mask[i] = -1U; 580146efc97bSPetr Machata 580246efc97bSPetr Machata err = nlmsg_parse(nlh, sizeof(struct if_stats_msg), tb, 580346efc97bSPetr Machata IFLA_STATS_GETSET_MAX, rtnl_stats_get_policy, extack); 580446efc97bSPetr Machata if (err < 0) 580546efc97bSPetr Machata return err; 580646efc97bSPetr Machata 580746efc97bSPetr Machata if (tb[IFLA_STATS_GET_FILTERS]) { 580846efc97bSPetr Machata err = rtnl_stats_get_parse_filters(tb[IFLA_STATS_GET_FILTERS], 580946efc97bSPetr Machata filters, extack); 581046efc97bSPetr Machata if (err) 581146efc97bSPetr Machata return err; 581246efc97bSPetr Machata } 581346efc97bSPetr Machata 581446efc97bSPetr Machata return 0; 581546efc97bSPetr Machata } 581646efc97bSPetr Machata 581751bc860dSJakub Kicinski static int rtnl_valid_stats_req(const struct nlmsghdr *nlh, bool strict_check, 581851bc860dSJakub Kicinski bool is_dump, struct netlink_ext_ack *extack) 581951bc860dSJakub Kicinski { 582051bc860dSJakub Kicinski struct if_stats_msg *ifsm; 582151bc860dSJakub Kicinski 582269f23a09SEric Dumazet if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifsm))) { 582351bc860dSJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid header for stats dump"); 582451bc860dSJakub Kicinski return -EINVAL; 582551bc860dSJakub Kicinski } 582651bc860dSJakub Kicinski 582751bc860dSJakub Kicinski if (!strict_check) 582851bc860dSJakub Kicinski return 0; 582951bc860dSJakub Kicinski 583051bc860dSJakub Kicinski ifsm = nlmsg_data(nlh); 583151bc860dSJakub Kicinski 583251bc860dSJakub Kicinski /* only requests using strict checks can pass data to influence 583351bc860dSJakub Kicinski * the dump. The legacy exception is filter_mask. 583451bc860dSJakub Kicinski */ 583551bc860dSJakub Kicinski if (ifsm->pad1 || ifsm->pad2 || (is_dump && ifsm->ifindex)) { 583651bc860dSJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid values in header for stats dump request"); 583751bc860dSJakub Kicinski return -EINVAL; 583851bc860dSJakub Kicinski } 58396300acb2SJakub Kicinski if (ifsm->filter_mask >= IFLA_STATS_FILTER_BIT(IFLA_STATS_MAX + 1)) { 58406300acb2SJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid stats requested through filter mask"); 58416300acb2SJakub Kicinski return -EINVAL; 58426300acb2SJakub Kicinski } 584351bc860dSJakub Kicinski 584451bc860dSJakub Kicinski return 0; 584551bc860dSJakub Kicinski } 584651bc860dSJakub Kicinski 5847c21ef3e3SDavid Ahern static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh, 5848c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 584910c9ead9SRoopa Prabhu { 585046efc97bSPetr Machata struct rtnl_stats_dump_filters filters; 585110c9ead9SRoopa Prabhu struct net *net = sock_net(skb->sk); 585210c9ead9SRoopa Prabhu struct net_device *dev = NULL; 5853e8872a25SNikolay Aleksandrov int idxattr = 0, prividx = 0; 5854e8872a25SNikolay Aleksandrov struct if_stats_msg *ifsm; 585510c9ead9SRoopa Prabhu struct sk_buff *nskb; 585610c9ead9SRoopa Prabhu int err; 585710c9ead9SRoopa Prabhu 585851bc860dSJakub Kicinski err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb), 585951bc860dSJakub Kicinski false, extack); 586051bc860dSJakub Kicinski if (err) 586151bc860dSJakub Kicinski return err; 58624775cc1fSMathias Krause 586310c9ead9SRoopa Prabhu ifsm = nlmsg_data(nlh); 586410c9ead9SRoopa Prabhu if (ifsm->ifindex > 0) 586510c9ead9SRoopa Prabhu dev = __dev_get_by_index(net, ifsm->ifindex); 586610c9ead9SRoopa Prabhu else 586710c9ead9SRoopa Prabhu return -EINVAL; 586810c9ead9SRoopa Prabhu 586910c9ead9SRoopa Prabhu if (!dev) 587010c9ead9SRoopa Prabhu return -ENODEV; 587110c9ead9SRoopa Prabhu 587246efc97bSPetr Machata if (!ifsm->filter_mask) { 587322b67d17SPetr Machata NL_SET_ERR_MSG(extack, "Filter mask must be set for stats get"); 587410c9ead9SRoopa Prabhu return -EINVAL; 587522b67d17SPetr Machata } 587610c9ead9SRoopa Prabhu 587746efc97bSPetr Machata err = rtnl_stats_get_parse(nlh, ifsm->filter_mask, &filters, extack); 587846efc97bSPetr Machata if (err) 587946efc97bSPetr Machata return err; 588046efc97bSPetr Machata 588146efc97bSPetr Machata nskb = nlmsg_new(if_nlmsg_stats_size(dev, &filters), GFP_KERNEL); 588210c9ead9SRoopa Prabhu if (!nskb) 588310c9ead9SRoopa Prabhu return -ENOBUFS; 588410c9ead9SRoopa Prabhu 588510c9ead9SRoopa Prabhu err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS, 588610c9ead9SRoopa Prabhu NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0, 588705415bccSPetr Machata 0, &filters, &idxattr, &prividx, extack); 588810c9ead9SRoopa Prabhu if (err < 0) { 588910c9ead9SRoopa Prabhu /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */ 589010c9ead9SRoopa Prabhu WARN_ON(err == -EMSGSIZE); 589110c9ead9SRoopa Prabhu kfree_skb(nskb); 589210c9ead9SRoopa Prabhu } else { 589310c9ead9SRoopa Prabhu err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); 589410c9ead9SRoopa Prabhu } 589510c9ead9SRoopa Prabhu 589610c9ead9SRoopa Prabhu return err; 589710c9ead9SRoopa Prabhu } 589810c9ead9SRoopa Prabhu 589910c9ead9SRoopa Prabhu static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb) 590010c9ead9SRoopa Prabhu { 5901841891ecSDavid Ahern struct netlink_ext_ack *extack = cb->extack; 5902e8872a25SNikolay Aleksandrov int h, s_h, err, s_idx, s_idxattr, s_prividx; 590346efc97bSPetr Machata struct rtnl_stats_dump_filters filters; 590410c9ead9SRoopa Prabhu struct net *net = sock_net(skb->sk); 590510c9ead9SRoopa Prabhu unsigned int flags = NLM_F_MULTI; 5906e8872a25SNikolay Aleksandrov struct if_stats_msg *ifsm; 5907e8872a25SNikolay Aleksandrov struct hlist_head *head; 5908e8872a25SNikolay Aleksandrov struct net_device *dev; 5909e8872a25SNikolay Aleksandrov int idx = 0; 591010c9ead9SRoopa Prabhu 591110c9ead9SRoopa Prabhu s_h = cb->args[0]; 591210c9ead9SRoopa Prabhu s_idx = cb->args[1]; 5913e8872a25SNikolay Aleksandrov s_idxattr = cb->args[2]; 5914e8872a25SNikolay Aleksandrov s_prividx = cb->args[3]; 591510c9ead9SRoopa Prabhu 591610c9ead9SRoopa Prabhu cb->seq = net->dev_base_seq; 591710c9ead9SRoopa Prabhu 591851bc860dSJakub Kicinski err = rtnl_valid_stats_req(cb->nlh, cb->strict_check, true, extack); 591951bc860dSJakub Kicinski if (err) 592051bc860dSJakub Kicinski return err; 59214775cc1fSMathias Krause 592210c9ead9SRoopa Prabhu ifsm = nlmsg_data(cb->nlh); 592346efc97bSPetr Machata if (!ifsm->filter_mask) { 5924841891ecSDavid Ahern NL_SET_ERR_MSG(extack, "Filter mask must be set for stats dump"); 5925841891ecSDavid Ahern return -EINVAL; 5926841891ecSDavid Ahern } 592710c9ead9SRoopa Prabhu 592846efc97bSPetr Machata err = rtnl_stats_get_parse(cb->nlh, ifsm->filter_mask, &filters, 592946efc97bSPetr Machata extack); 593046efc97bSPetr Machata if (err) 593146efc97bSPetr Machata return err; 593246efc97bSPetr Machata 593310c9ead9SRoopa Prabhu for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 593410c9ead9SRoopa Prabhu idx = 0; 593510c9ead9SRoopa Prabhu head = &net->dev_index_head[h]; 593610c9ead9SRoopa Prabhu hlist_for_each_entry(dev, head, index_hlist) { 593710c9ead9SRoopa Prabhu if (idx < s_idx) 593810c9ead9SRoopa Prabhu goto cont; 593910c9ead9SRoopa Prabhu err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS, 594010c9ead9SRoopa Prabhu NETLINK_CB(cb->skb).portid, 594110c9ead9SRoopa Prabhu cb->nlh->nlmsg_seq, 0, 594246efc97bSPetr Machata flags, &filters, 594305415bccSPetr Machata &s_idxattr, &s_prividx, 594405415bccSPetr Machata extack); 594510c9ead9SRoopa Prabhu /* If we ran out of room on the first message, 594610c9ead9SRoopa Prabhu * we're in trouble 594710c9ead9SRoopa Prabhu */ 594810c9ead9SRoopa Prabhu WARN_ON((err == -EMSGSIZE) && (skb->len == 0)); 594910c9ead9SRoopa Prabhu 595010c9ead9SRoopa Prabhu if (err < 0) 595110c9ead9SRoopa Prabhu goto out; 5952e8872a25SNikolay Aleksandrov s_prividx = 0; 5953e8872a25SNikolay Aleksandrov s_idxattr = 0; 595410c9ead9SRoopa Prabhu nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 595510c9ead9SRoopa Prabhu cont: 595610c9ead9SRoopa Prabhu idx++; 595710c9ead9SRoopa Prabhu } 595810c9ead9SRoopa Prabhu } 595910c9ead9SRoopa Prabhu out: 5960e8872a25SNikolay Aleksandrov cb->args[3] = s_prividx; 5961e8872a25SNikolay Aleksandrov cb->args[2] = s_idxattr; 596210c9ead9SRoopa Prabhu cb->args[1] = idx; 596310c9ead9SRoopa Prabhu cb->args[0] = h; 596410c9ead9SRoopa Prabhu 596510c9ead9SRoopa Prabhu return skb->len; 596610c9ead9SRoopa Prabhu } 596710c9ead9SRoopa Prabhu 59685fd0b838SPetr Machata void rtnl_offload_xstats_notify(struct net_device *dev) 59695fd0b838SPetr Machata { 59705fd0b838SPetr Machata struct rtnl_stats_dump_filters response_filters = {}; 59715fd0b838SPetr Machata struct net *net = dev_net(dev); 59725fd0b838SPetr Machata int idxattr = 0, prividx = 0; 59735fd0b838SPetr Machata struct sk_buff *skb; 59745fd0b838SPetr Machata int err = -ENOBUFS; 59755fd0b838SPetr Machata 59765fd0b838SPetr Machata ASSERT_RTNL(); 59775fd0b838SPetr Machata 59785fd0b838SPetr Machata response_filters.mask[0] |= 59795fd0b838SPetr Machata IFLA_STATS_FILTER_BIT(IFLA_STATS_LINK_OFFLOAD_XSTATS); 59805fd0b838SPetr Machata response_filters.mask[IFLA_STATS_LINK_OFFLOAD_XSTATS] |= 59815fd0b838SPetr Machata IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_HW_S_INFO); 59825fd0b838SPetr Machata 59835fd0b838SPetr Machata skb = nlmsg_new(if_nlmsg_stats_size(dev, &response_filters), 59845fd0b838SPetr Machata GFP_KERNEL); 59855fd0b838SPetr Machata if (!skb) 59865fd0b838SPetr Machata goto errout; 59875fd0b838SPetr Machata 59885fd0b838SPetr Machata err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS, 0, 0, 0, 0, 59895fd0b838SPetr Machata &response_filters, &idxattr, &prividx, NULL); 59905fd0b838SPetr Machata if (err < 0) { 59915fd0b838SPetr Machata kfree_skb(skb); 59925fd0b838SPetr Machata goto errout; 59935fd0b838SPetr Machata } 59945fd0b838SPetr Machata 59955fd0b838SPetr Machata rtnl_notify(skb, net, 0, RTNLGRP_STATS, NULL, GFP_KERNEL); 59965fd0b838SPetr Machata return; 59975fd0b838SPetr Machata 59985fd0b838SPetr Machata errout: 59995fd0b838SPetr Machata rtnl_set_sk_err(net, RTNLGRP_STATS, err); 60005fd0b838SPetr Machata } 60015fd0b838SPetr Machata EXPORT_SYMBOL(rtnl_offload_xstats_notify); 60025fd0b838SPetr Machata 600303ba3566SPetr Machata static int rtnl_stats_set(struct sk_buff *skb, struct nlmsghdr *nlh, 600403ba3566SPetr Machata struct netlink_ext_ack *extack) 600503ba3566SPetr Machata { 60065fd0b838SPetr Machata enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3; 600703ba3566SPetr Machata struct rtnl_stats_dump_filters response_filters = {}; 600803ba3566SPetr Machata struct nlattr *tb[IFLA_STATS_GETSET_MAX + 1]; 600903ba3566SPetr Machata struct net *net = sock_net(skb->sk); 601003ba3566SPetr Machata struct net_device *dev = NULL; 601103ba3566SPetr Machata struct if_stats_msg *ifsm; 60125fd0b838SPetr Machata bool notify = false; 601303ba3566SPetr Machata int err; 601403ba3566SPetr Machata 601503ba3566SPetr Machata err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb), 601603ba3566SPetr Machata false, extack); 601703ba3566SPetr Machata if (err) 601803ba3566SPetr Machata return err; 601903ba3566SPetr Machata 602003ba3566SPetr Machata ifsm = nlmsg_data(nlh); 602103ba3566SPetr Machata if (ifsm->family != AF_UNSPEC) { 602203ba3566SPetr Machata NL_SET_ERR_MSG(extack, "Address family should be AF_UNSPEC"); 602303ba3566SPetr Machata return -EINVAL; 602403ba3566SPetr Machata } 602503ba3566SPetr Machata 602603ba3566SPetr Machata if (ifsm->ifindex > 0) 602703ba3566SPetr Machata dev = __dev_get_by_index(net, ifsm->ifindex); 602803ba3566SPetr Machata else 602903ba3566SPetr Machata return -EINVAL; 603003ba3566SPetr Machata 603103ba3566SPetr Machata if (!dev) 603203ba3566SPetr Machata return -ENODEV; 603303ba3566SPetr Machata 603403ba3566SPetr Machata if (ifsm->filter_mask) { 603503ba3566SPetr Machata NL_SET_ERR_MSG(extack, "Filter mask must be 0 for stats set"); 603603ba3566SPetr Machata return -EINVAL; 603703ba3566SPetr Machata } 603803ba3566SPetr Machata 603903ba3566SPetr Machata err = nlmsg_parse(nlh, sizeof(*ifsm), tb, IFLA_STATS_GETSET_MAX, 604003ba3566SPetr Machata ifla_stats_set_policy, extack); 604103ba3566SPetr Machata if (err < 0) 604203ba3566SPetr Machata return err; 604303ba3566SPetr Machata 60445fd0b838SPetr Machata if (tb[IFLA_STATS_SET_OFFLOAD_XSTATS_L3_STATS]) { 60455fd0b838SPetr Machata u8 req = nla_get_u8(tb[IFLA_STATS_SET_OFFLOAD_XSTATS_L3_STATS]); 604603ba3566SPetr Machata 60475fd0b838SPetr Machata if (req) 60485fd0b838SPetr Machata err = netdev_offload_xstats_enable(dev, t_l3, extack); 60495fd0b838SPetr Machata else 60505fd0b838SPetr Machata err = netdev_offload_xstats_disable(dev, t_l3); 60515fd0b838SPetr Machata 60525fd0b838SPetr Machata if (!err) 60535fd0b838SPetr Machata notify = true; 60545fd0b838SPetr Machata else if (err != -EALREADY) 60555fd0b838SPetr Machata return err; 60565fd0b838SPetr Machata 60575fd0b838SPetr Machata response_filters.mask[0] |= 60585fd0b838SPetr Machata IFLA_STATS_FILTER_BIT(IFLA_STATS_LINK_OFFLOAD_XSTATS); 60595fd0b838SPetr Machata response_filters.mask[IFLA_STATS_LINK_OFFLOAD_XSTATS] |= 60605fd0b838SPetr Machata IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_HW_S_INFO); 606103ba3566SPetr Machata } 606203ba3566SPetr Machata 60635fd0b838SPetr Machata if (notify) 60645fd0b838SPetr Machata rtnl_offload_xstats_notify(dev); 60655fd0b838SPetr Machata 60665fd0b838SPetr Machata return 0; 606703ba3566SPetr Machata } 606803ba3566SPetr Machata 6069*cc7f5022SIdo Schimmel static int rtnl_mdb_valid_dump_req(const struct nlmsghdr *nlh, 6070*cc7f5022SIdo Schimmel struct netlink_ext_ack *extack) 6071*cc7f5022SIdo Schimmel { 6072*cc7f5022SIdo Schimmel struct br_port_msg *bpm; 6073*cc7f5022SIdo Schimmel 6074*cc7f5022SIdo Schimmel if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*bpm))) { 6075*cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Invalid header for mdb dump request"); 6076*cc7f5022SIdo Schimmel return -EINVAL; 6077*cc7f5022SIdo Schimmel } 6078*cc7f5022SIdo Schimmel 6079*cc7f5022SIdo Schimmel bpm = nlmsg_data(nlh); 6080*cc7f5022SIdo Schimmel if (bpm->ifindex) { 6081*cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Filtering by device index is not supported for mdb dump request"); 6082*cc7f5022SIdo Schimmel return -EINVAL; 6083*cc7f5022SIdo Schimmel } 6084*cc7f5022SIdo Schimmel if (nlmsg_attrlen(nlh, sizeof(*bpm))) { 6085*cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Invalid data after header in mdb dump request"); 6086*cc7f5022SIdo Schimmel return -EINVAL; 6087*cc7f5022SIdo Schimmel } 6088*cc7f5022SIdo Schimmel 6089*cc7f5022SIdo Schimmel return 0; 6090*cc7f5022SIdo Schimmel } 6091*cc7f5022SIdo Schimmel 6092*cc7f5022SIdo Schimmel struct rtnl_mdb_dump_ctx { 6093*cc7f5022SIdo Schimmel long idx; 6094*cc7f5022SIdo Schimmel }; 6095*cc7f5022SIdo Schimmel 6096*cc7f5022SIdo Schimmel static int rtnl_mdb_dump(struct sk_buff *skb, struct netlink_callback *cb) 6097*cc7f5022SIdo Schimmel { 6098*cc7f5022SIdo Schimmel struct rtnl_mdb_dump_ctx *ctx = (void *)cb->ctx; 6099*cc7f5022SIdo Schimmel struct net *net = sock_net(skb->sk); 6100*cc7f5022SIdo Schimmel struct net_device *dev; 6101*cc7f5022SIdo Schimmel int idx, s_idx; 6102*cc7f5022SIdo Schimmel int err; 6103*cc7f5022SIdo Schimmel 6104*cc7f5022SIdo Schimmel NL_ASSERT_DUMP_CTX_FITS(struct rtnl_mdb_dump_ctx); 6105*cc7f5022SIdo Schimmel 6106*cc7f5022SIdo Schimmel if (cb->strict_check) { 6107*cc7f5022SIdo Schimmel err = rtnl_mdb_valid_dump_req(cb->nlh, cb->extack); 6108*cc7f5022SIdo Schimmel if (err) 6109*cc7f5022SIdo Schimmel return err; 6110*cc7f5022SIdo Schimmel } 6111*cc7f5022SIdo Schimmel 6112*cc7f5022SIdo Schimmel s_idx = ctx->idx; 6113*cc7f5022SIdo Schimmel idx = 0; 6114*cc7f5022SIdo Schimmel 6115*cc7f5022SIdo Schimmel for_each_netdev(net, dev) { 6116*cc7f5022SIdo Schimmel if (idx < s_idx) 6117*cc7f5022SIdo Schimmel goto skip; 6118*cc7f5022SIdo Schimmel if (!dev->netdev_ops->ndo_mdb_dump) 6119*cc7f5022SIdo Schimmel goto skip; 6120*cc7f5022SIdo Schimmel 6121*cc7f5022SIdo Schimmel err = dev->netdev_ops->ndo_mdb_dump(dev, skb, cb); 6122*cc7f5022SIdo Schimmel if (err == -EMSGSIZE) 6123*cc7f5022SIdo Schimmel goto out; 6124*cc7f5022SIdo Schimmel /* Moving on to next device, reset markers and sequence 6125*cc7f5022SIdo Schimmel * counters since they are all maintained per-device. 6126*cc7f5022SIdo Schimmel */ 6127*cc7f5022SIdo Schimmel memset(cb->ctx, 0, sizeof(cb->ctx)); 6128*cc7f5022SIdo Schimmel cb->prev_seq = 0; 6129*cc7f5022SIdo Schimmel cb->seq = 0; 6130*cc7f5022SIdo Schimmel skip: 6131*cc7f5022SIdo Schimmel idx++; 6132*cc7f5022SIdo Schimmel } 6133*cc7f5022SIdo Schimmel 6134*cc7f5022SIdo Schimmel out: 6135*cc7f5022SIdo Schimmel ctx->idx = idx; 6136*cc7f5022SIdo Schimmel return skb->len; 6137*cc7f5022SIdo Schimmel } 6138*cc7f5022SIdo Schimmel 6139*cc7f5022SIdo Schimmel static int rtnl_validate_mdb_entry(const struct nlattr *attr, 6140*cc7f5022SIdo Schimmel struct netlink_ext_ack *extack) 6141*cc7f5022SIdo Schimmel { 6142*cc7f5022SIdo Schimmel struct br_mdb_entry *entry = nla_data(attr); 6143*cc7f5022SIdo Schimmel 6144*cc7f5022SIdo Schimmel if (nla_len(attr) != sizeof(struct br_mdb_entry)) { 6145*cc7f5022SIdo Schimmel NL_SET_ERR_MSG_ATTR(extack, attr, "Invalid attribute length"); 6146*cc7f5022SIdo Schimmel return -EINVAL; 6147*cc7f5022SIdo Schimmel } 6148*cc7f5022SIdo Schimmel 6149*cc7f5022SIdo Schimmel if (entry->ifindex == 0) { 6150*cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Zero entry ifindex is not allowed"); 6151*cc7f5022SIdo Schimmel return -EINVAL; 6152*cc7f5022SIdo Schimmel } 6153*cc7f5022SIdo Schimmel 6154*cc7f5022SIdo Schimmel if (entry->addr.proto == htons(ETH_P_IP)) { 6155*cc7f5022SIdo Schimmel if (!ipv4_is_multicast(entry->addr.u.ip4)) { 6156*cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "IPv4 entry group address is not multicast"); 6157*cc7f5022SIdo Schimmel return -EINVAL; 6158*cc7f5022SIdo Schimmel } 6159*cc7f5022SIdo Schimmel if (ipv4_is_local_multicast(entry->addr.u.ip4)) { 6160*cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "IPv4 entry group address is local multicast"); 6161*cc7f5022SIdo Schimmel return -EINVAL; 6162*cc7f5022SIdo Schimmel } 6163*cc7f5022SIdo Schimmel #if IS_ENABLED(CONFIG_IPV6) 6164*cc7f5022SIdo Schimmel } else if (entry->addr.proto == htons(ETH_P_IPV6)) { 6165*cc7f5022SIdo Schimmel if (ipv6_addr_is_ll_all_nodes(&entry->addr.u.ip6)) { 6166*cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "IPv6 entry group address is link-local all nodes"); 6167*cc7f5022SIdo Schimmel return -EINVAL; 6168*cc7f5022SIdo Schimmel } 6169*cc7f5022SIdo Schimmel #endif 6170*cc7f5022SIdo Schimmel } else if (entry->addr.proto == 0) { 6171*cc7f5022SIdo Schimmel /* L2 mdb */ 6172*cc7f5022SIdo Schimmel if (!is_multicast_ether_addr(entry->addr.u.mac_addr)) { 6173*cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "L2 entry group is not multicast"); 6174*cc7f5022SIdo Schimmel return -EINVAL; 6175*cc7f5022SIdo Schimmel } 6176*cc7f5022SIdo Schimmel } else { 6177*cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Unknown entry protocol"); 6178*cc7f5022SIdo Schimmel return -EINVAL; 6179*cc7f5022SIdo Schimmel } 6180*cc7f5022SIdo Schimmel 6181*cc7f5022SIdo Schimmel if (entry->state != MDB_PERMANENT && entry->state != MDB_TEMPORARY) { 6182*cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Unknown entry state"); 6183*cc7f5022SIdo Schimmel return -EINVAL; 6184*cc7f5022SIdo Schimmel } 6185*cc7f5022SIdo Schimmel if (entry->vid >= VLAN_VID_MASK) { 6186*cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Invalid entry VLAN id"); 6187*cc7f5022SIdo Schimmel return -EINVAL; 6188*cc7f5022SIdo Schimmel } 6189*cc7f5022SIdo Schimmel 6190*cc7f5022SIdo Schimmel return 0; 6191*cc7f5022SIdo Schimmel } 6192*cc7f5022SIdo Schimmel 6193*cc7f5022SIdo Schimmel static const struct nla_policy mdba_policy[MDBA_SET_ENTRY_MAX + 1] = { 6194*cc7f5022SIdo Schimmel [MDBA_SET_ENTRY_UNSPEC] = { .strict_start_type = MDBA_SET_ENTRY_ATTRS + 1 }, 6195*cc7f5022SIdo Schimmel [MDBA_SET_ENTRY] = NLA_POLICY_VALIDATE_FN(NLA_BINARY, 6196*cc7f5022SIdo Schimmel rtnl_validate_mdb_entry, 6197*cc7f5022SIdo Schimmel sizeof(struct br_mdb_entry)), 6198*cc7f5022SIdo Schimmel [MDBA_SET_ENTRY_ATTRS] = { .type = NLA_NESTED }, 6199*cc7f5022SIdo Schimmel }; 6200*cc7f5022SIdo Schimmel 6201*cc7f5022SIdo Schimmel static int rtnl_mdb_add(struct sk_buff *skb, struct nlmsghdr *nlh, 6202*cc7f5022SIdo Schimmel struct netlink_ext_ack *extack) 6203*cc7f5022SIdo Schimmel { 6204*cc7f5022SIdo Schimmel struct nlattr *tb[MDBA_SET_ENTRY_MAX + 1]; 6205*cc7f5022SIdo Schimmel struct net *net = sock_net(skb->sk); 6206*cc7f5022SIdo Schimmel struct br_port_msg *bpm; 6207*cc7f5022SIdo Schimmel struct net_device *dev; 6208*cc7f5022SIdo Schimmel int err; 6209*cc7f5022SIdo Schimmel 6210*cc7f5022SIdo Schimmel err = nlmsg_parse_deprecated(nlh, sizeof(*bpm), tb, 6211*cc7f5022SIdo Schimmel MDBA_SET_ENTRY_MAX, mdba_policy, extack); 6212*cc7f5022SIdo Schimmel if (err) 6213*cc7f5022SIdo Schimmel return err; 6214*cc7f5022SIdo Schimmel 6215*cc7f5022SIdo Schimmel bpm = nlmsg_data(nlh); 6216*cc7f5022SIdo Schimmel if (!bpm->ifindex) { 6217*cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Invalid ifindex"); 6218*cc7f5022SIdo Schimmel return -EINVAL; 6219*cc7f5022SIdo Schimmel } 6220*cc7f5022SIdo Schimmel 6221*cc7f5022SIdo Schimmel dev = __dev_get_by_index(net, bpm->ifindex); 6222*cc7f5022SIdo Schimmel if (!dev) { 6223*cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Device doesn't exist"); 6224*cc7f5022SIdo Schimmel return -ENODEV; 6225*cc7f5022SIdo Schimmel } 6226*cc7f5022SIdo Schimmel 6227*cc7f5022SIdo Schimmel if (NL_REQ_ATTR_CHECK(extack, NULL, tb, MDBA_SET_ENTRY)) { 6228*cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Missing MDBA_SET_ENTRY attribute"); 6229*cc7f5022SIdo Schimmel return -EINVAL; 6230*cc7f5022SIdo Schimmel } 6231*cc7f5022SIdo Schimmel 6232*cc7f5022SIdo Schimmel if (!dev->netdev_ops->ndo_mdb_add) { 6233*cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Device does not support MDB operations"); 6234*cc7f5022SIdo Schimmel return -EOPNOTSUPP; 6235*cc7f5022SIdo Schimmel } 6236*cc7f5022SIdo Schimmel 6237*cc7f5022SIdo Schimmel return dev->netdev_ops->ndo_mdb_add(dev, tb, nlh->nlmsg_flags, extack); 6238*cc7f5022SIdo Schimmel } 6239*cc7f5022SIdo Schimmel 6240*cc7f5022SIdo Schimmel static int rtnl_mdb_del(struct sk_buff *skb, struct nlmsghdr *nlh, 6241*cc7f5022SIdo Schimmel struct netlink_ext_ack *extack) 6242*cc7f5022SIdo Schimmel { 6243*cc7f5022SIdo Schimmel struct nlattr *tb[MDBA_SET_ENTRY_MAX + 1]; 6244*cc7f5022SIdo Schimmel struct net *net = sock_net(skb->sk); 6245*cc7f5022SIdo Schimmel struct br_port_msg *bpm; 6246*cc7f5022SIdo Schimmel struct net_device *dev; 6247*cc7f5022SIdo Schimmel int err; 6248*cc7f5022SIdo Schimmel 6249*cc7f5022SIdo Schimmel err = nlmsg_parse_deprecated(nlh, sizeof(*bpm), tb, 6250*cc7f5022SIdo Schimmel MDBA_SET_ENTRY_MAX, mdba_policy, extack); 6251*cc7f5022SIdo Schimmel if (err) 6252*cc7f5022SIdo Schimmel return err; 6253*cc7f5022SIdo Schimmel 6254*cc7f5022SIdo Schimmel bpm = nlmsg_data(nlh); 6255*cc7f5022SIdo Schimmel if (!bpm->ifindex) { 6256*cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Invalid ifindex"); 6257*cc7f5022SIdo Schimmel return -EINVAL; 6258*cc7f5022SIdo Schimmel } 6259*cc7f5022SIdo Schimmel 6260*cc7f5022SIdo Schimmel dev = __dev_get_by_index(net, bpm->ifindex); 6261*cc7f5022SIdo Schimmel if (!dev) { 6262*cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Device doesn't exist"); 6263*cc7f5022SIdo Schimmel return -ENODEV; 6264*cc7f5022SIdo Schimmel } 6265*cc7f5022SIdo Schimmel 6266*cc7f5022SIdo Schimmel if (NL_REQ_ATTR_CHECK(extack, NULL, tb, MDBA_SET_ENTRY)) { 6267*cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Missing MDBA_SET_ENTRY attribute"); 6268*cc7f5022SIdo Schimmel return -EINVAL; 6269*cc7f5022SIdo Schimmel } 6270*cc7f5022SIdo Schimmel 6271*cc7f5022SIdo Schimmel if (!dev->netdev_ops->ndo_mdb_del) { 6272*cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Device does not support MDB operations"); 6273*cc7f5022SIdo Schimmel return -EOPNOTSUPP; 6274*cc7f5022SIdo Schimmel } 6275*cc7f5022SIdo Schimmel 6276*cc7f5022SIdo Schimmel return dev->netdev_ops->ndo_mdb_del(dev, tb, extack); 6277*cc7f5022SIdo Schimmel } 6278*cc7f5022SIdo Schimmel 62791da177e4SLinus Torvalds /* Process one rtnetlink message. */ 62801da177e4SLinus Torvalds 62812d4bc933SJohannes Berg static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, 62822d4bc933SJohannes Berg struct netlink_ext_ack *extack) 62831da177e4SLinus Torvalds { 62843b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 6285addf9b90SFlorian Westphal struct rtnl_link *link; 628612dc5c2cSNikolay Aleksandrov enum rtnl_kinds kind; 6287e4202511SFlorian Westphal struct module *owner; 62886853dd48SFlorian Westphal int err = -EOPNOTSUPP; 6289e2849863SThomas Graf rtnl_doit_func doit; 629062256f98SFlorian Westphal unsigned int flags; 62911da177e4SLinus Torvalds int family; 62921da177e4SLinus Torvalds int type; 62931da177e4SLinus Torvalds 62941da177e4SLinus Torvalds type = nlh->nlmsg_type; 62951da177e4SLinus Torvalds if (type > RTM_MAX) 6296038890feSThomas Graf return -EOPNOTSUPP; 62971da177e4SLinus Torvalds 62981da177e4SLinus Torvalds type -= RTM_BASE; 62991da177e4SLinus Torvalds 63001da177e4SLinus Torvalds /* All the messages must have at least 1 byte length */ 6301573ce260SHong zhi guo if (nlmsg_len(nlh) < sizeof(struct rtgenmsg)) 63021da177e4SLinus Torvalds return 0; 63031da177e4SLinus Torvalds 6304573ce260SHong zhi guo family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family; 63052e9ea3e3SNikolay Aleksandrov kind = rtnl_msgtype_kind(type); 63061da177e4SLinus Torvalds 630712dc5c2cSNikolay Aleksandrov if (kind != RTNL_KIND_GET && !netlink_net_capable(skb, CAP_NET_ADMIN)) 63081d00a4ebSThomas Graf return -EPERM; 63091da177e4SLinus Torvalds 63106853dd48SFlorian Westphal rcu_read_lock(); 631112dc5c2cSNikolay Aleksandrov if (kind == RTNL_KIND_GET && (nlh->nlmsg_flags & NLM_F_DUMP)) { 631297c53cacSDenis V. Lunev struct sock *rtnl; 6313e2849863SThomas Graf rtnl_dumpit_func dumpit; 6314ebfe3c51SDi Zhu u32 min_dump_alloc = 0; 63151da177e4SLinus Torvalds 6316addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 6317addf9b90SFlorian Westphal if (!link || !link->dumpit) { 63186853dd48SFlorian Westphal family = PF_UNSPEC; 6319addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 6320addf9b90SFlorian Westphal if (!link || !link->dumpit) 63216853dd48SFlorian Westphal goto err_unlock; 63226853dd48SFlorian Westphal } 6323e4202511SFlorian Westphal owner = link->owner; 6324addf9b90SFlorian Westphal dumpit = link->dumpit; 63256853dd48SFlorian Westphal 63265c2bb9b6SFlorian Westphal if (type == RTM_GETLINK - RTM_BASE) 6327e1fa6d21SFlorian Westphal min_dump_alloc = rtnl_calcit(skb, nlh); 63281da177e4SLinus Torvalds 6329e4202511SFlorian Westphal err = 0; 6330e4202511SFlorian Westphal /* need to do this before rcu_read_unlock() */ 6331e4202511SFlorian Westphal if (!try_module_get(owner)) 6332e4202511SFlorian Westphal err = -EPROTONOSUPPORT; 6333e4202511SFlorian Westphal 63346853dd48SFlorian Westphal rcu_read_unlock(); 63356853dd48SFlorian Westphal 633697c53cacSDenis V. Lunev rtnl = net->rtnl; 6337e4202511SFlorian Westphal if (err == 0) { 633880d326faSPablo Neira Ayuso struct netlink_dump_control c = { 633980d326faSPablo Neira Ayuso .dump = dumpit, 634080d326faSPablo Neira Ayuso .min_dump_alloc = min_dump_alloc, 6341e4202511SFlorian Westphal .module = owner, 634280d326faSPablo Neira Ayuso }; 634380d326faSPablo Neira Ayuso err = netlink_dump_start(rtnl, skb, nlh, &c); 6344e4202511SFlorian Westphal /* netlink_dump_start() will keep a reference on 6345e4202511SFlorian Westphal * module if dump is still in progress. 6346e4202511SFlorian Westphal */ 6347e4202511SFlorian Westphal module_put(owner); 634880d326faSPablo Neira Ayuso } 63492907c35fSEric Dumazet return err; 63501da177e4SLinus Torvalds } 63511da177e4SLinus Torvalds 6352addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 6353addf9b90SFlorian Westphal if (!link || !link->doit) { 63548caa38b5SFlorian Westphal family = PF_UNSPEC; 6355addf9b90SFlorian Westphal link = rtnl_get_link(PF_UNSPEC, type); 6356addf9b90SFlorian Westphal if (!link || !link->doit) 6357addf9b90SFlorian Westphal goto out_unlock; 63588caa38b5SFlorian Westphal } 63598caa38b5SFlorian Westphal 6360e4202511SFlorian Westphal owner = link->owner; 6361e4202511SFlorian Westphal if (!try_module_get(owner)) { 6362e4202511SFlorian Westphal err = -EPROTONOSUPPORT; 6363e4202511SFlorian Westphal goto out_unlock; 6364e4202511SFlorian Westphal } 6365e4202511SFlorian Westphal 6366addf9b90SFlorian Westphal flags = link->flags; 6367a6cec0bcSNikolay Aleksandrov if (kind == RTNL_KIND_DEL && (nlh->nlmsg_flags & NLM_F_BULK) && 6368a6cec0bcSNikolay Aleksandrov !(flags & RTNL_FLAG_BULK_DEL_SUPPORTED)) { 6369a6cec0bcSNikolay Aleksandrov NL_SET_ERR_MSG(extack, "Bulk delete is not supported"); 63705b22f627SZhengchao Shao module_put(owner); 6371a6cec0bcSNikolay Aleksandrov goto err_unlock; 6372a6cec0bcSNikolay Aleksandrov } 6373a6cec0bcSNikolay Aleksandrov 637462256f98SFlorian Westphal if (flags & RTNL_FLAG_DOIT_UNLOCKED) { 6375addf9b90SFlorian Westphal doit = link->doit; 637662256f98SFlorian Westphal rcu_read_unlock(); 637762256f98SFlorian Westphal if (doit) 637862256f98SFlorian Westphal err = doit(skb, nlh, extack); 6379e4202511SFlorian Westphal module_put(owner); 638062256f98SFlorian Westphal return err; 638162256f98SFlorian Westphal } 63826853dd48SFlorian Westphal rcu_read_unlock(); 63836853dd48SFlorian Westphal 63840cc09020SFlorian Westphal rtnl_lock(); 6385addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 6386addf9b90SFlorian Westphal if (link && link->doit) 6387addf9b90SFlorian Westphal err = link->doit(skb, nlh, extack); 63880cc09020SFlorian Westphal rtnl_unlock(); 6389addf9b90SFlorian Westphal 6390e4202511SFlorian Westphal module_put(owner); 6391e4202511SFlorian Westphal 6392addf9b90SFlorian Westphal return err; 6393addf9b90SFlorian Westphal 6394addf9b90SFlorian Westphal out_unlock: 6395addf9b90SFlorian Westphal rcu_read_unlock(); 63960cc09020SFlorian Westphal return err; 63970cc09020SFlorian Westphal 63980cc09020SFlorian Westphal err_unlock: 63996853dd48SFlorian Westphal rcu_read_unlock(); 64000cc09020SFlorian Westphal return -EOPNOTSUPP; 64011da177e4SLinus Torvalds } 64021da177e4SLinus Torvalds 6403cd40b7d3SDenis V. Lunev static void rtnetlink_rcv(struct sk_buff *skb) 64041da177e4SLinus Torvalds { 6405cd40b7d3SDenis V. Lunev netlink_rcv_skb(skb, &rtnetlink_rcv_msg); 64061da177e4SLinus Torvalds } 64071da177e4SLinus Torvalds 64085f729eaaSJulien Gomes static int rtnetlink_bind(struct net *net, int group) 64095f729eaaSJulien Gomes { 64105f729eaaSJulien Gomes switch (group) { 64115f729eaaSJulien Gomes case RTNLGRP_IPV4_MROUTE_R: 64125f729eaaSJulien Gomes case RTNLGRP_IPV6_MROUTE_R: 64135f729eaaSJulien Gomes if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 64145f729eaaSJulien Gomes return -EPERM; 64155f729eaaSJulien Gomes break; 64165f729eaaSJulien Gomes } 64175f729eaaSJulien Gomes return 0; 64185f729eaaSJulien Gomes } 64195f729eaaSJulien Gomes 64201da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr) 64211da177e4SLinus Torvalds { 6422351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 6423e9dc8653SEric W. Biederman 64241da177e4SLinus Torvalds switch (event) { 64255138e86fSVlad Yasevich case NETDEV_REBOOT: 64268a212589SXin Long case NETDEV_CHANGEMTU: 64273753654eSDavid Ahern case NETDEV_CHANGEADDR: 64285138e86fSVlad Yasevich case NETDEV_CHANGENAME: 64295138e86fSVlad Yasevich case NETDEV_FEAT_CHANGE: 64305138e86fSVlad Yasevich case NETDEV_BONDING_FAILOVER: 6431e6e66594SXin Long case NETDEV_POST_TYPE_CHANGE: 64325138e86fSVlad Yasevich case NETDEV_NOTIFY_PEERS: 6433dc709f37SXin Long case NETDEV_CHANGEUPPER: 64345138e86fSVlad Yasevich case NETDEV_RESEND_IGMP: 64355138e86fSVlad Yasevich case NETDEV_CHANGEINFODATA: 6436eeda3fb9SXin Long case NETDEV_CHANGELOWERSTATE: 6437ebdcf045SXin Long case NETDEV_CHANGE_TX_QUEUE_LEN: 64383d3ea5afSVlad Yasevich rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event), 64391d997f10SHangbin Liu GFP_KERNEL, NULL, 0, 0, NULL); 64401da177e4SLinus Torvalds break; 64411da177e4SLinus Torvalds default: 64421da177e4SLinus Torvalds break; 64431da177e4SLinus Torvalds } 64441da177e4SLinus Torvalds return NOTIFY_DONE; 64451da177e4SLinus Torvalds } 64461da177e4SLinus Torvalds 64471da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = { 64481da177e4SLinus Torvalds .notifier_call = rtnetlink_event, 64491da177e4SLinus Torvalds }; 64501da177e4SLinus Torvalds 645197c53cacSDenis V. Lunev 64522c8c1e72SAlexey Dobriyan static int __net_init rtnetlink_net_init(struct net *net) 645397c53cacSDenis V. Lunev { 645497c53cacSDenis V. Lunev struct sock *sk; 6455a31f2d17SPablo Neira Ayuso struct netlink_kernel_cfg cfg = { 6456a31f2d17SPablo Neira Ayuso .groups = RTNLGRP_MAX, 6457a31f2d17SPablo Neira Ayuso .input = rtnetlink_rcv, 6458a31f2d17SPablo Neira Ayuso .cb_mutex = &rtnl_mutex, 64599785e10aSPablo Neira Ayuso .flags = NL_CFG_F_NONROOT_RECV, 64605f729eaaSJulien Gomes .bind = rtnetlink_bind, 6461a31f2d17SPablo Neira Ayuso }; 6462a31f2d17SPablo Neira Ayuso 64639f00d977SPablo Neira Ayuso sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg); 646497c53cacSDenis V. Lunev if (!sk) 646597c53cacSDenis V. Lunev return -ENOMEM; 646697c53cacSDenis V. Lunev net->rtnl = sk; 646797c53cacSDenis V. Lunev return 0; 646897c53cacSDenis V. Lunev } 646997c53cacSDenis V. Lunev 64702c8c1e72SAlexey Dobriyan static void __net_exit rtnetlink_net_exit(struct net *net) 647197c53cacSDenis V. Lunev { 6472b7c6ba6eSDenis V. Lunev netlink_kernel_release(net->rtnl); 647397c53cacSDenis V. Lunev net->rtnl = NULL; 647497c53cacSDenis V. Lunev } 647597c53cacSDenis V. Lunev 647697c53cacSDenis V. Lunev static struct pernet_operations rtnetlink_net_ops = { 647797c53cacSDenis V. Lunev .init = rtnetlink_net_init, 647897c53cacSDenis V. Lunev .exit = rtnetlink_net_exit, 647997c53cacSDenis V. Lunev }; 648097c53cacSDenis V. Lunev 64811da177e4SLinus Torvalds void __init rtnetlink_init(void) 64821da177e4SLinus Torvalds { 648397c53cacSDenis V. Lunev if (register_pernet_subsys(&rtnetlink_net_ops)) 64841da177e4SLinus Torvalds panic("rtnetlink_init: cannot initialize rtnetlink\n"); 648597c53cacSDenis V. Lunev 64861da177e4SLinus Torvalds register_netdevice_notifier(&rtnetlink_dev_notifier); 6487340d17fcSThomas Graf 6488c7ac8679SGreg Rose rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, 6489b97bac64SFlorian Westphal rtnl_dump_ifinfo, 0); 6490b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0); 6491b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0); 6492b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0); 6493687ad8ccSThomas Graf 6494b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0); 6495b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0); 6496b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0); 649777162022SJohn Fastabend 649836fbf1e5SJiri Pirko rtnl_register(PF_UNSPEC, RTM_NEWLINKPROP, rtnl_newlinkprop, NULL, 0); 649936fbf1e5SJiri Pirko rtnl_register(PF_UNSPEC, RTM_DELLINKPROP, rtnl_dellinkprop, NULL, 0); 650036fbf1e5SJiri Pirko 6501b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0); 65029e834259SNikolay Aleksandrov rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 65039e834259SNikolay Aleksandrov RTNL_FLAG_BULK_DEL_SUPPORTED); 65045b2f94b2SRoopa Prabhu rtnl_register(PF_BRIDGE, RTM_GETNEIGH, rtnl_fdb_get, rtnl_fdb_dump, 0); 6505e5a55a89SJohn Fastabend 6506b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0); 6507b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0); 6508b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0); 650910c9ead9SRoopa Prabhu 651010c9ead9SRoopa Prabhu rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump, 6511b97bac64SFlorian Westphal 0); 651203ba3566SPetr Machata rtnl_register(PF_UNSPEC, RTM_SETSTATS, rtnl_stats_set, NULL, 0); 6513*cc7f5022SIdo Schimmel 6514*cc7f5022SIdo Schimmel rtnl_register(PF_BRIDGE, RTM_GETMDB, NULL, rtnl_mdb_dump, 0); 6515*cc7f5022SIdo Schimmel rtnl_register(PF_BRIDGE, RTM_NEWMDB, rtnl_mdb_add, NULL, 0); 6516*cc7f5022SIdo Schimmel rtnl_register(PF_BRIDGE, RTM_DELMDB, rtnl_mdb_del, NULL, 0); 65171da177e4SLinus Torvalds } 6518