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> 571da177e4SLinus Torvalds 586264f58cSJakub Kicinski #include "dev.h" 596264f58cSJakub Kicinski 60a428afe8SNikolay Aleksandrov #define RTNL_MAX_TYPE 50 612dba407fSNikolay Aleksandrov #define RTNL_SLAVE_MAX_TYPE 40 62ccf8dbcdSKees Cook 63e0d087afSEric Dumazet struct rtnl_link { 64e2849863SThomas Graf rtnl_doit_func doit; 65e2849863SThomas Graf rtnl_dumpit_func dumpit; 66e4202511SFlorian Westphal struct module *owner; 6762256f98SFlorian Westphal unsigned int flags; 68addf9b90SFlorian Westphal struct rcu_head rcu; 69e2849863SThomas Graf }; 70e2849863SThomas Graf 716756ae4bSStephen Hemminger static DEFINE_MUTEX(rtnl_mutex); 721da177e4SLinus Torvalds 731da177e4SLinus Torvalds void rtnl_lock(void) 741da177e4SLinus Torvalds { 756756ae4bSStephen Hemminger mutex_lock(&rtnl_mutex); 761da177e4SLinus Torvalds } 77e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_lock); 781da177e4SLinus Torvalds 7979ffdfc6SKirill Tkhai int rtnl_lock_killable(void) 8079ffdfc6SKirill Tkhai { 8179ffdfc6SKirill Tkhai return mutex_lock_killable(&rtnl_mutex); 8279ffdfc6SKirill Tkhai } 8379ffdfc6SKirill Tkhai EXPORT_SYMBOL(rtnl_lock_killable); 8479ffdfc6SKirill Tkhai 851b5c5493SEric Dumazet static struct sk_buff *defer_kfree_skb_list; 861b5c5493SEric Dumazet void rtnl_kfree_skbs(struct sk_buff *head, struct sk_buff *tail) 871b5c5493SEric Dumazet { 881b5c5493SEric Dumazet if (head && tail) { 891b5c5493SEric Dumazet tail->next = defer_kfree_skb_list; 901b5c5493SEric Dumazet defer_kfree_skb_list = head; 911b5c5493SEric Dumazet } 921b5c5493SEric Dumazet } 931b5c5493SEric Dumazet EXPORT_SYMBOL(rtnl_kfree_skbs); 941b5c5493SEric Dumazet 956756ae4bSStephen Hemminger void __rtnl_unlock(void) 961da177e4SLinus Torvalds { 971b5c5493SEric Dumazet struct sk_buff *head = defer_kfree_skb_list; 981b5c5493SEric Dumazet 991b5c5493SEric Dumazet defer_kfree_skb_list = NULL; 1001b5c5493SEric Dumazet 1010b5c21bbSJohannes Berg /* Ensure that we didn't actually add any TODO item when __rtnl_unlock() 1020b5c21bbSJohannes Berg * is used. In some places, e.g. in cfg80211, we have code that will do 1030b5c21bbSJohannes Berg * something like 1040b5c21bbSJohannes Berg * rtnl_lock() 1050b5c21bbSJohannes Berg * wiphy_lock() 1060b5c21bbSJohannes Berg * ... 1070b5c21bbSJohannes Berg * rtnl_unlock() 1080b5c21bbSJohannes Berg * 1090b5c21bbSJohannes Berg * and because netdev_run_todo() acquires the RTNL for items on the list 1100b5c21bbSJohannes Berg * we could cause a situation such as this: 1110b5c21bbSJohannes Berg * Thread 1 Thread 2 1120b5c21bbSJohannes Berg * rtnl_lock() 1130b5c21bbSJohannes Berg * unregister_netdevice() 1140b5c21bbSJohannes Berg * __rtnl_unlock() 1150b5c21bbSJohannes Berg * rtnl_lock() 1160b5c21bbSJohannes Berg * wiphy_lock() 1170b5c21bbSJohannes Berg * rtnl_unlock() 1180b5c21bbSJohannes Berg * netdev_run_todo() 1190b5c21bbSJohannes Berg * __rtnl_unlock() 1200b5c21bbSJohannes Berg * 1210b5c21bbSJohannes Berg * // list not empty now 1220b5c21bbSJohannes Berg * // because of thread 2 1230b5c21bbSJohannes Berg * rtnl_lock() 1240b5c21bbSJohannes Berg * while (!list_empty(...)) 1250b5c21bbSJohannes Berg * rtnl_lock() 1260b5c21bbSJohannes Berg * wiphy_lock() 1270b5c21bbSJohannes Berg * **** DEADLOCK **** 1280b5c21bbSJohannes Berg * 1290b5c21bbSJohannes Berg * However, usage of __rtnl_unlock() is rare, and so we can ensure that 1300b5c21bbSJohannes Berg * it's not used in cases where something is added to do the list. 1310b5c21bbSJohannes Berg */ 1320b5c21bbSJohannes Berg WARN_ON(!list_empty(&net_todo_list)); 1330b5c21bbSJohannes Berg 1346756ae4bSStephen Hemminger mutex_unlock(&rtnl_mutex); 1351b5c5493SEric Dumazet 1361b5c5493SEric Dumazet while (head) { 1371b5c5493SEric Dumazet struct sk_buff *next = head->next; 1381b5c5493SEric Dumazet 1391b5c5493SEric Dumazet kfree_skb(head); 1401b5c5493SEric Dumazet cond_resched(); 1411b5c5493SEric Dumazet head = next; 1421b5c5493SEric Dumazet } 1431da177e4SLinus Torvalds } 1441da177e4SLinus Torvalds 1451da177e4SLinus Torvalds void rtnl_unlock(void) 1461da177e4SLinus Torvalds { 14758ec3b4dSHerbert Xu /* This fellow will unlock it for us. */ 1481da177e4SLinus Torvalds netdev_run_todo(); 1491da177e4SLinus Torvalds } 150e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unlock); 1511da177e4SLinus Torvalds 1526756ae4bSStephen Hemminger int rtnl_trylock(void) 1536756ae4bSStephen Hemminger { 1546756ae4bSStephen Hemminger return mutex_trylock(&rtnl_mutex); 1556756ae4bSStephen Hemminger } 156e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_trylock); 1576756ae4bSStephen Hemminger 158c9c1014bSPatrick McHardy int rtnl_is_locked(void) 159c9c1014bSPatrick McHardy { 160c9c1014bSPatrick McHardy return mutex_is_locked(&rtnl_mutex); 161c9c1014bSPatrick McHardy } 162e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_is_locked); 163c9c1014bSPatrick McHardy 1646f99528eSVlad Buslov bool refcount_dec_and_rtnl_lock(refcount_t *r) 1656f99528eSVlad Buslov { 1666f99528eSVlad Buslov return refcount_dec_and_mutex_lock(r, &rtnl_mutex); 1676f99528eSVlad Buslov } 1686f99528eSVlad Buslov EXPORT_SYMBOL(refcount_dec_and_rtnl_lock); 1696f99528eSVlad Buslov 170a898def2SPaul E. McKenney #ifdef CONFIG_PROVE_LOCKING 1710cbf3343SYaowei Bai bool lockdep_rtnl_is_held(void) 172a898def2SPaul E. McKenney { 173a898def2SPaul E. McKenney return lockdep_is_held(&rtnl_mutex); 174a898def2SPaul E. McKenney } 175a898def2SPaul E. McKenney EXPORT_SYMBOL(lockdep_rtnl_is_held); 176a898def2SPaul E. McKenney #endif /* #ifdef CONFIG_PROVE_LOCKING */ 177a898def2SPaul E. McKenney 17851e13685SJakub Kicinski static struct rtnl_link __rcu *__rcu *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1]; 179e2849863SThomas Graf 180e2849863SThomas Graf static inline int rtm_msgindex(int msgtype) 181e2849863SThomas Graf { 182e2849863SThomas Graf int msgindex = msgtype - RTM_BASE; 183e2849863SThomas Graf 184e2849863SThomas Graf /* 185e2849863SThomas Graf * msgindex < 0 implies someone tried to register a netlink 186e2849863SThomas Graf * control code. msgindex >= RTM_NR_MSGTYPES may indicate that 187e2849863SThomas Graf * the message type has not been added to linux/rtnetlink.h 188e2849863SThomas Graf */ 189e2849863SThomas Graf BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES); 190e2849863SThomas Graf 191e2849863SThomas Graf return msgindex; 192e2849863SThomas Graf } 193e2849863SThomas Graf 194addf9b90SFlorian Westphal static struct rtnl_link *rtnl_get_link(int protocol, int msgtype) 195addf9b90SFlorian Westphal { 19651e13685SJakub Kicinski struct rtnl_link __rcu **tab; 197addf9b90SFlorian Westphal 198addf9b90SFlorian Westphal if (protocol >= ARRAY_SIZE(rtnl_msg_handlers)) 199addf9b90SFlorian Westphal protocol = PF_UNSPEC; 200addf9b90SFlorian Westphal 201addf9b90SFlorian Westphal tab = rcu_dereference_rtnl(rtnl_msg_handlers[protocol]); 202addf9b90SFlorian Westphal if (!tab) 203addf9b90SFlorian Westphal tab = rcu_dereference_rtnl(rtnl_msg_handlers[PF_UNSPEC]); 204addf9b90SFlorian Westphal 20551e13685SJakub Kicinski return rcu_dereference_rtnl(tab[msgtype]); 206addf9b90SFlorian Westphal } 207addf9b90SFlorian Westphal 208e4202511SFlorian Westphal static int rtnl_register_internal(struct module *owner, 209e4202511SFlorian Westphal int protocol, int msgtype, 210c7ac8679SGreg Rose rtnl_doit_func doit, rtnl_dumpit_func dumpit, 211b97bac64SFlorian Westphal unsigned int flags) 212e2849863SThomas Graf { 213b0e9fe1bSFlorian Westphal struct rtnl_link *link, *old; 214b0e9fe1bSFlorian Westphal struct rtnl_link __rcu **tab; 215e2849863SThomas Graf int msgindex; 216addf9b90SFlorian Westphal int ret = -ENOBUFS; 217e2849863SThomas Graf 21825239ceeSPatrick McHardy BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); 219e2849863SThomas Graf msgindex = rtm_msgindex(msgtype); 220e2849863SThomas Graf 221addf9b90SFlorian Westphal rtnl_lock(); 22251e13685SJakub Kicinski tab = rtnl_dereference(rtnl_msg_handlers[protocol]); 223e2849863SThomas Graf if (tab == NULL) { 224addf9b90SFlorian Westphal tab = kcalloc(RTM_NR_MSGTYPES, sizeof(void *), GFP_KERNEL); 225addf9b90SFlorian Westphal if (!tab) 226addf9b90SFlorian Westphal goto unlock; 227e2849863SThomas Graf 228addf9b90SFlorian Westphal /* ensures we see the 0 stores */ 2296853dd48SFlorian Westphal rcu_assign_pointer(rtnl_msg_handlers[protocol], tab); 230e2849863SThomas Graf } 231e2849863SThomas Graf 232addf9b90SFlorian Westphal old = rtnl_dereference(tab[msgindex]); 233addf9b90SFlorian Westphal if (old) { 234addf9b90SFlorian Westphal link = kmemdup(old, sizeof(*old), GFP_KERNEL); 235addf9b90SFlorian Westphal if (!link) 236addf9b90SFlorian Westphal goto unlock; 237addf9b90SFlorian Westphal } else { 238addf9b90SFlorian Westphal link = kzalloc(sizeof(*link), GFP_KERNEL); 239addf9b90SFlorian Westphal if (!link) 240addf9b90SFlorian Westphal goto unlock; 241addf9b90SFlorian Westphal } 242e2849863SThomas Graf 243e4202511SFlorian Westphal WARN_ON(link->owner && link->owner != owner); 244e4202511SFlorian Westphal link->owner = owner; 245e4202511SFlorian Westphal 246addf9b90SFlorian Westphal WARN_ON(doit && link->doit && link->doit != doit); 247addf9b90SFlorian Westphal if (doit) 248addf9b90SFlorian Westphal link->doit = doit; 249addf9b90SFlorian Westphal WARN_ON(dumpit && link->dumpit && link->dumpit != dumpit); 250addf9b90SFlorian Westphal if (dumpit) 251addf9b90SFlorian Westphal link->dumpit = dumpit; 252addf9b90SFlorian Westphal 253a6cec0bcSNikolay Aleksandrov WARN_ON(rtnl_msgtype_kind(msgtype) != RTNL_KIND_DEL && 254a6cec0bcSNikolay Aleksandrov (flags & RTNL_FLAG_BULK_DEL_SUPPORTED)); 255addf9b90SFlorian Westphal link->flags |= flags; 256addf9b90SFlorian Westphal 257addf9b90SFlorian Westphal /* publish protocol:msgtype */ 258addf9b90SFlorian Westphal rcu_assign_pointer(tab[msgindex], link); 259addf9b90SFlorian Westphal ret = 0; 260addf9b90SFlorian Westphal if (old) 261addf9b90SFlorian Westphal kfree_rcu(old, rcu); 262addf9b90SFlorian Westphal unlock: 263addf9b90SFlorian Westphal rtnl_unlock(); 264addf9b90SFlorian Westphal return ret; 265e2849863SThomas Graf } 266e4202511SFlorian Westphal 267e4202511SFlorian Westphal /** 268e4202511SFlorian Westphal * rtnl_register_module - Register a rtnetlink message type 269e4202511SFlorian Westphal * 270e4202511SFlorian Westphal * @owner: module registering the hook (THIS_MODULE) 271e4202511SFlorian Westphal * @protocol: Protocol family or PF_UNSPEC 272e4202511SFlorian Westphal * @msgtype: rtnetlink message type 273e4202511SFlorian Westphal * @doit: Function pointer called for each request message 274e4202511SFlorian Westphal * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message 275d467d0bcSZheng Yongjun * @flags: rtnl_link_flags to modify behaviour of doit/dumpit functions 276e4202511SFlorian Westphal * 277e4202511SFlorian Westphal * Like rtnl_register, but for use by removable modules. 278e4202511SFlorian Westphal */ 279e4202511SFlorian Westphal int rtnl_register_module(struct module *owner, 280e4202511SFlorian Westphal int protocol, int msgtype, 281e4202511SFlorian Westphal rtnl_doit_func doit, rtnl_dumpit_func dumpit, 282e4202511SFlorian Westphal unsigned int flags) 283e4202511SFlorian Westphal { 284e4202511SFlorian Westphal return rtnl_register_internal(owner, protocol, msgtype, 285e4202511SFlorian Westphal doit, dumpit, flags); 286e4202511SFlorian Westphal } 287e4202511SFlorian Westphal EXPORT_SYMBOL_GPL(rtnl_register_module); 288e4202511SFlorian Westphal 289e4202511SFlorian Westphal /** 29016feebcfSFlorian Westphal * rtnl_register - Register a rtnetlink message type 291e4202511SFlorian Westphal * @protocol: Protocol family or PF_UNSPEC 292e4202511SFlorian Westphal * @msgtype: rtnetlink message type 293e4202511SFlorian Westphal * @doit: Function pointer called for each request message 294e4202511SFlorian Westphal * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message 295d467d0bcSZheng Yongjun * @flags: rtnl_link_flags to modify behaviour of doit/dumpit functions 296e4202511SFlorian Westphal * 297e4202511SFlorian Westphal * Registers the specified function pointers (at least one of them has 298e4202511SFlorian Westphal * to be non-NULL) to be called whenever a request message for the 299e4202511SFlorian Westphal * specified protocol family and message type is received. 300e4202511SFlorian Westphal * 301e4202511SFlorian Westphal * The special protocol family PF_UNSPEC may be used to define fallback 302e4202511SFlorian Westphal * function pointers for the case when no entry for the specific protocol 303e4202511SFlorian Westphal * family exists. 304e2849863SThomas Graf */ 305e2849863SThomas Graf void rtnl_register(int protocol, int msgtype, 306c7ac8679SGreg Rose rtnl_doit_func doit, rtnl_dumpit_func dumpit, 307b97bac64SFlorian Westphal unsigned int flags) 308e2849863SThomas Graf { 30916feebcfSFlorian Westphal int err; 31016feebcfSFlorian Westphal 31116feebcfSFlorian Westphal err = rtnl_register_internal(NULL, protocol, msgtype, doit, dumpit, 31216feebcfSFlorian Westphal flags); 31316feebcfSFlorian Westphal if (err) 31416feebcfSFlorian Westphal pr_err("Unable to register rtnetlink message handler, " 31516feebcfSFlorian Westphal "protocol = %d, message type = %d\n", protocol, msgtype); 316e2849863SThomas Graf } 317e2849863SThomas Graf 318e2849863SThomas Graf /** 319e2849863SThomas Graf * rtnl_unregister - Unregister a rtnetlink message type 320e2849863SThomas Graf * @protocol: Protocol family or PF_UNSPEC 321e2849863SThomas Graf * @msgtype: rtnetlink message type 322e2849863SThomas Graf * 323e2849863SThomas Graf * Returns 0 on success or a negative error code. 324e2849863SThomas Graf */ 325e2849863SThomas Graf int rtnl_unregister(int protocol, int msgtype) 326e2849863SThomas Graf { 32751e13685SJakub Kicinski struct rtnl_link __rcu **tab; 32851e13685SJakub Kicinski struct rtnl_link *link; 329e2849863SThomas Graf int msgindex; 330e2849863SThomas Graf 33125239ceeSPatrick McHardy BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); 332e2849863SThomas Graf msgindex = rtm_msgindex(msgtype); 333e2849863SThomas Graf 3346853dd48SFlorian Westphal rtnl_lock(); 335addf9b90SFlorian Westphal tab = rtnl_dereference(rtnl_msg_handlers[protocol]); 336addf9b90SFlorian Westphal if (!tab) { 3376853dd48SFlorian Westphal rtnl_unlock(); 338e2849863SThomas Graf return -ENOENT; 3396853dd48SFlorian Westphal } 340e2849863SThomas Graf 34151e13685SJakub Kicinski link = rtnl_dereference(tab[msgindex]); 3424fc29989SYajun Deng RCU_INIT_POINTER(tab[msgindex], NULL); 3436853dd48SFlorian Westphal rtnl_unlock(); 344e2849863SThomas Graf 345addf9b90SFlorian Westphal kfree_rcu(link, rcu); 346addf9b90SFlorian Westphal 347e2849863SThomas Graf return 0; 348e2849863SThomas Graf } 349e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister); 350e2849863SThomas Graf 351e2849863SThomas Graf /** 352e2849863SThomas Graf * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol 353e2849863SThomas Graf * @protocol : Protocol family or PF_UNSPEC 354e2849863SThomas Graf * 355e2849863SThomas Graf * Identical to calling rtnl_unregster() for all registered message types 356e2849863SThomas Graf * of a certain protocol family. 357e2849863SThomas Graf */ 358e2849863SThomas Graf void rtnl_unregister_all(int protocol) 359e2849863SThomas Graf { 36051e13685SJakub Kicinski struct rtnl_link __rcu **tab; 36151e13685SJakub Kicinski struct rtnl_link *link; 362addf9b90SFlorian Westphal int msgindex; 363019a3169SFlorian Westphal 36425239ceeSPatrick McHardy BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); 365e2849863SThomas Graf 366019a3169SFlorian Westphal rtnl_lock(); 36751e13685SJakub Kicinski tab = rtnl_dereference(rtnl_msg_handlers[protocol]); 368f707ef61SSabrina Dubroca if (!tab) { 369f707ef61SSabrina Dubroca rtnl_unlock(); 370f707ef61SSabrina Dubroca return; 371f707ef61SSabrina Dubroca } 3726853dd48SFlorian Westphal RCU_INIT_POINTER(rtnl_msg_handlers[protocol], NULL); 373addf9b90SFlorian Westphal for (msgindex = 0; msgindex < RTM_NR_MSGTYPES; msgindex++) { 37451e13685SJakub Kicinski link = rtnl_dereference(tab[msgindex]); 375addf9b90SFlorian Westphal if (!link) 376addf9b90SFlorian Westphal continue; 377addf9b90SFlorian Westphal 3784fc29989SYajun Deng RCU_INIT_POINTER(tab[msgindex], NULL); 379addf9b90SFlorian Westphal kfree_rcu(link, rcu); 380addf9b90SFlorian Westphal } 381019a3169SFlorian Westphal rtnl_unlock(); 382019a3169SFlorian Westphal 3836853dd48SFlorian Westphal synchronize_net(); 3846853dd48SFlorian Westphal 385addf9b90SFlorian Westphal kfree(tab); 386e2849863SThomas Graf } 387e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister_all); 3881da177e4SLinus Torvalds 38938f7b870SPatrick McHardy static LIST_HEAD(link_ops); 39038f7b870SPatrick McHardy 391c63044f0SEric Dumazet static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind) 392c63044f0SEric Dumazet { 393c63044f0SEric Dumazet const struct rtnl_link_ops *ops; 394c63044f0SEric Dumazet 395c63044f0SEric Dumazet list_for_each_entry(ops, &link_ops, list) { 396c63044f0SEric Dumazet if (!strcmp(ops->kind, kind)) 397c63044f0SEric Dumazet return ops; 398c63044f0SEric Dumazet } 399c63044f0SEric Dumazet return NULL; 400c63044f0SEric Dumazet } 401c63044f0SEric Dumazet 40238f7b870SPatrick McHardy /** 40338f7b870SPatrick McHardy * __rtnl_link_register - Register rtnl_link_ops with rtnetlink. 40438f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to register 40538f7b870SPatrick McHardy * 40638f7b870SPatrick McHardy * The caller must hold the rtnl_mutex. This function should be used 40738f7b870SPatrick McHardy * by drivers that create devices during module initialization. It 40838f7b870SPatrick McHardy * must be called before registering the devices. 40938f7b870SPatrick McHardy * 41038f7b870SPatrick McHardy * Returns 0 on success or a negative error code. 41138f7b870SPatrick McHardy */ 41238f7b870SPatrick McHardy int __rtnl_link_register(struct rtnl_link_ops *ops) 41338f7b870SPatrick McHardy { 414c63044f0SEric Dumazet if (rtnl_link_ops_get(ops->kind)) 415c63044f0SEric Dumazet return -EEXIST; 416c63044f0SEric Dumazet 4178c713dc9SJohannes Berg /* The check for alloc/setup is here because if ops 418b0ab2fabSJiri Pirko * does not have that filled up, it is not possible 419b0ab2fabSJiri Pirko * to use the ops for creating device. So do not 420b0ab2fabSJiri Pirko * fill up dellink as well. That disables rtnl_dellink. 421b0ab2fabSJiri Pirko */ 4228c713dc9SJohannes Berg if ((ops->alloc || ops->setup) && !ops->dellink) 42323289a37SEric Dumazet ops->dellink = unregister_netdevice_queue; 4242d85cba2SPatrick McHardy 42538f7b870SPatrick McHardy list_add_tail(&ops->list, &link_ops); 42638f7b870SPatrick McHardy return 0; 42738f7b870SPatrick McHardy } 42838f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_register); 42938f7b870SPatrick McHardy 43038f7b870SPatrick McHardy /** 43138f7b870SPatrick McHardy * rtnl_link_register - Register rtnl_link_ops with rtnetlink. 43238f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to register 43338f7b870SPatrick McHardy * 43438f7b870SPatrick McHardy * Returns 0 on success or a negative error code. 43538f7b870SPatrick McHardy */ 43638f7b870SPatrick McHardy int rtnl_link_register(struct rtnl_link_ops *ops) 43738f7b870SPatrick McHardy { 43838f7b870SPatrick McHardy int err; 43938f7b870SPatrick McHardy 440ccf8dbcdSKees Cook /* Sanity-check max sizes to avoid stack buffer overflow. */ 441ccf8dbcdSKees Cook if (WARN_ON(ops->maxtype > RTNL_MAX_TYPE || 442ccf8dbcdSKees Cook ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE)) 443ccf8dbcdSKees Cook return -EINVAL; 444ccf8dbcdSKees Cook 44538f7b870SPatrick McHardy rtnl_lock(); 44638f7b870SPatrick McHardy err = __rtnl_link_register(ops); 44738f7b870SPatrick McHardy rtnl_unlock(); 44838f7b870SPatrick McHardy return err; 44938f7b870SPatrick McHardy } 45038f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_register); 45138f7b870SPatrick McHardy 452669f87baSPavel Emelyanov static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops) 453669f87baSPavel Emelyanov { 454669f87baSPavel Emelyanov struct net_device *dev; 45523289a37SEric Dumazet LIST_HEAD(list_kill); 45623289a37SEric Dumazet 457669f87baSPavel Emelyanov for_each_netdev(net, dev) { 45823289a37SEric Dumazet if (dev->rtnl_link_ops == ops) 45923289a37SEric Dumazet ops->dellink(dev, &list_kill); 460669f87baSPavel Emelyanov } 46123289a37SEric Dumazet unregister_netdevice_many(&list_kill); 462669f87baSPavel Emelyanov } 463669f87baSPavel Emelyanov 46438f7b870SPatrick McHardy /** 46538f7b870SPatrick McHardy * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. 46638f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to unregister 46738f7b870SPatrick McHardy * 468554873e5SKirill Tkhai * The caller must hold the rtnl_mutex and guarantee net_namespace_list 469554873e5SKirill Tkhai * integrity (hold pernet_ops_rwsem for writing to close the race 470554873e5SKirill Tkhai * with setup_net() and cleanup_net()). 47138f7b870SPatrick McHardy */ 47238f7b870SPatrick McHardy void __rtnl_link_unregister(struct rtnl_link_ops *ops) 47338f7b870SPatrick McHardy { 474881d966bSEric W. Biederman struct net *net; 4752d85cba2SPatrick McHardy 476881d966bSEric W. Biederman for_each_net(net) { 477669f87baSPavel Emelyanov __rtnl_kill_links(net, ops); 478881d966bSEric W. Biederman } 47938f7b870SPatrick McHardy list_del(&ops->list); 48038f7b870SPatrick McHardy } 48138f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_unregister); 48238f7b870SPatrick McHardy 483200b916fSCong Wang /* Return with the rtnl_lock held when there are no network 484200b916fSCong Wang * devices unregistering in any network namespace. 485200b916fSCong Wang */ 486200b916fSCong Wang static void rtnl_lock_unregistering_all(void) 487200b916fSCong Wang { 488200b916fSCong Wang struct net *net; 489200b916fSCong Wang bool unregistering; 490ff960a73SPeter Zijlstra DEFINE_WAIT_FUNC(wait, woken_wake_function); 491200b916fSCong Wang 492ff960a73SPeter Zijlstra add_wait_queue(&netdev_unregistering_wq, &wait); 493200b916fSCong Wang for (;;) { 494200b916fSCong Wang unregistering = false; 495200b916fSCong Wang rtnl_lock(); 496f0b07bb1SKirill Tkhai /* We held write locked pernet_ops_rwsem, and parallel 497f0b07bb1SKirill Tkhai * setup_net() and cleanup_net() are not possible. 498f0b07bb1SKirill Tkhai */ 499200b916fSCong Wang for_each_net(net) { 500ede6c39cSEric Dumazet if (atomic_read(&net->dev_unreg_count) > 0) { 501200b916fSCong Wang unregistering = true; 502200b916fSCong Wang break; 503200b916fSCong Wang } 504200b916fSCong Wang } 505200b916fSCong Wang if (!unregistering) 506200b916fSCong Wang break; 507200b916fSCong Wang __rtnl_unlock(); 508ff960a73SPeter Zijlstra 509ff960a73SPeter Zijlstra wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); 510200b916fSCong Wang } 511ff960a73SPeter Zijlstra remove_wait_queue(&netdev_unregistering_wq, &wait); 512200b916fSCong Wang } 513200b916fSCong Wang 51438f7b870SPatrick McHardy /** 51538f7b870SPatrick McHardy * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. 51638f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to unregister 51738f7b870SPatrick McHardy */ 51838f7b870SPatrick McHardy void rtnl_link_unregister(struct rtnl_link_ops *ops) 51938f7b870SPatrick McHardy { 5208518e9bbSKirill Tkhai /* Close the race with setup_net() and cleanup_net() */ 5214420bf21SKirill Tkhai down_write(&pernet_ops_rwsem); 522200b916fSCong Wang rtnl_lock_unregistering_all(); 52338f7b870SPatrick McHardy __rtnl_link_unregister(ops); 52438f7b870SPatrick McHardy rtnl_unlock(); 5254420bf21SKirill Tkhai up_write(&pernet_ops_rwsem); 52638f7b870SPatrick McHardy } 52738f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_unregister); 52838f7b870SPatrick McHardy 529ba7d49b1SJiri Pirko static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev) 530ba7d49b1SJiri Pirko { 531ba7d49b1SJiri Pirko struct net_device *master_dev; 532ba7d49b1SJiri Pirko const struct rtnl_link_ops *ops; 5338515ae38SFlorian Westphal size_t size = 0; 534ba7d49b1SJiri Pirko 5358515ae38SFlorian Westphal rcu_read_lock(); 5368515ae38SFlorian Westphal 5378515ae38SFlorian Westphal master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev); 538ba7d49b1SJiri Pirko if (!master_dev) 5398515ae38SFlorian Westphal goto out; 5408515ae38SFlorian Westphal 541ba7d49b1SJiri Pirko ops = master_dev->rtnl_link_ops; 5426049f253SFernando Luis Vazquez Cao if (!ops || !ops->get_slave_size) 5438515ae38SFlorian Westphal goto out; 544ba7d49b1SJiri Pirko /* IFLA_INFO_SLAVE_DATA + nested data */ 5458515ae38SFlorian Westphal size = nla_total_size(sizeof(struct nlattr)) + 546ba7d49b1SJiri Pirko ops->get_slave_size(master_dev, dev); 5478515ae38SFlorian Westphal 5488515ae38SFlorian Westphal out: 5498515ae38SFlorian Westphal rcu_read_unlock(); 5508515ae38SFlorian Westphal return size; 551ba7d49b1SJiri Pirko } 552ba7d49b1SJiri Pirko 55338f7b870SPatrick McHardy static size_t rtnl_link_get_size(const struct net_device *dev) 55438f7b870SPatrick McHardy { 55538f7b870SPatrick McHardy const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 55638f7b870SPatrick McHardy size_t size; 55738f7b870SPatrick McHardy 55838f7b870SPatrick McHardy if (!ops) 55938f7b870SPatrick McHardy return 0; 56038f7b870SPatrick McHardy 561369cf77aSThomas Graf size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */ 562369cf77aSThomas Graf nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */ 56338f7b870SPatrick McHardy 56438f7b870SPatrick McHardy if (ops->get_size) 56538f7b870SPatrick McHardy /* IFLA_INFO_DATA + nested data */ 566369cf77aSThomas Graf size += nla_total_size(sizeof(struct nlattr)) + 56738f7b870SPatrick McHardy ops->get_size(dev); 56838f7b870SPatrick McHardy 56938f7b870SPatrick McHardy if (ops->get_xstats_size) 570369cf77aSThomas Graf /* IFLA_INFO_XSTATS */ 571369cf77aSThomas Graf size += nla_total_size(ops->get_xstats_size(dev)); 57238f7b870SPatrick McHardy 573ba7d49b1SJiri Pirko size += rtnl_link_get_slave_info_data_size(dev); 574ba7d49b1SJiri Pirko 57538f7b870SPatrick McHardy return size; 57638f7b870SPatrick McHardy } 57738f7b870SPatrick McHardy 578f8ff182cSThomas Graf static LIST_HEAD(rtnl_af_ops); 579f8ff182cSThomas Graf 580f8ff182cSThomas Graf static const struct rtnl_af_ops *rtnl_af_lookup(const int family) 581f8ff182cSThomas Graf { 582f8ff182cSThomas Graf const struct rtnl_af_ops *ops; 583f8ff182cSThomas Graf 584a100243dSCong Wang ASSERT_RTNL(); 585a100243dSCong Wang 586a100243dSCong Wang list_for_each_entry(ops, &rtnl_af_ops, list) { 587f8ff182cSThomas Graf if (ops->family == family) 588f8ff182cSThomas Graf return ops; 589f8ff182cSThomas Graf } 590f8ff182cSThomas Graf 591f8ff182cSThomas Graf return NULL; 592f8ff182cSThomas Graf } 593f8ff182cSThomas Graf 594f8ff182cSThomas Graf /** 595f8ff182cSThomas Graf * rtnl_af_register - Register rtnl_af_ops with rtnetlink. 596f8ff182cSThomas Graf * @ops: struct rtnl_af_ops * to register 597f8ff182cSThomas Graf * 598f8ff182cSThomas Graf * Returns 0 on success or a negative error code. 599f8ff182cSThomas Graf */ 6003678a9d8Sstephen hemminger void rtnl_af_register(struct rtnl_af_ops *ops) 601f8ff182cSThomas Graf { 602f8ff182cSThomas Graf rtnl_lock(); 6035fa85a09SFlorian Westphal list_add_tail_rcu(&ops->list, &rtnl_af_ops); 604f8ff182cSThomas Graf rtnl_unlock(); 605f8ff182cSThomas Graf } 606f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_register); 607f8ff182cSThomas Graf 608f8ff182cSThomas Graf /** 609f8ff182cSThomas Graf * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink. 610f8ff182cSThomas Graf * @ops: struct rtnl_af_ops * to unregister 611f8ff182cSThomas Graf */ 612f8ff182cSThomas Graf void rtnl_af_unregister(struct rtnl_af_ops *ops) 613f8ff182cSThomas Graf { 614f8ff182cSThomas Graf rtnl_lock(); 6155fa85a09SFlorian Westphal list_del_rcu(&ops->list); 616f8ff182cSThomas Graf rtnl_unlock(); 6175fa85a09SFlorian Westphal 6185fa85a09SFlorian Westphal synchronize_rcu(); 619f8ff182cSThomas Graf } 620f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_unregister); 621f8ff182cSThomas Graf 622b1974ed0SArad, Ronen static size_t rtnl_link_get_af_size(const struct net_device *dev, 623b1974ed0SArad, Ronen u32 ext_filter_mask) 624f8ff182cSThomas Graf { 625f8ff182cSThomas Graf struct rtnl_af_ops *af_ops; 626f8ff182cSThomas Graf size_t size; 627f8ff182cSThomas Graf 628f8ff182cSThomas Graf /* IFLA_AF_SPEC */ 629f8ff182cSThomas Graf size = nla_total_size(sizeof(struct nlattr)); 630f8ff182cSThomas Graf 6315fa85a09SFlorian Westphal rcu_read_lock(); 6325fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 633f8ff182cSThomas Graf if (af_ops->get_link_af_size) { 634f8ff182cSThomas Graf /* AF_* + nested data */ 635f8ff182cSThomas Graf size += nla_total_size(sizeof(struct nlattr)) + 636b1974ed0SArad, Ronen af_ops->get_link_af_size(dev, ext_filter_mask); 637f8ff182cSThomas Graf } 638f8ff182cSThomas Graf } 6395fa85a09SFlorian Westphal rcu_read_unlock(); 640f8ff182cSThomas Graf 641f8ff182cSThomas Graf return size; 642f8ff182cSThomas Graf } 643f8ff182cSThomas Graf 644ba7d49b1SJiri Pirko static bool rtnl_have_link_slave_info(const struct net_device *dev) 645ba7d49b1SJiri Pirko { 646ba7d49b1SJiri Pirko struct net_device *master_dev; 6474c82a95eSFlorian Westphal bool ret = false; 648ba7d49b1SJiri Pirko 6494c82a95eSFlorian Westphal rcu_read_lock(); 6504c82a95eSFlorian Westphal 6514c82a95eSFlorian Westphal master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev); 652813f020cSJiri Pirko if (master_dev && master_dev->rtnl_link_ops) 6534c82a95eSFlorian Westphal ret = true; 6544c82a95eSFlorian Westphal rcu_read_unlock(); 6554c82a95eSFlorian Westphal return ret; 656ba7d49b1SJiri Pirko } 657ba7d49b1SJiri Pirko 658ba7d49b1SJiri Pirko static int rtnl_link_slave_info_fill(struct sk_buff *skb, 659ba7d49b1SJiri Pirko const struct net_device *dev) 660ba7d49b1SJiri Pirko { 661ba7d49b1SJiri Pirko struct net_device *master_dev; 662ba7d49b1SJiri Pirko const struct rtnl_link_ops *ops; 663ba7d49b1SJiri Pirko struct nlattr *slave_data; 664ba7d49b1SJiri Pirko int err; 665ba7d49b1SJiri Pirko 666ba7d49b1SJiri Pirko master_dev = netdev_master_upper_dev_get((struct net_device *) dev); 667ba7d49b1SJiri Pirko if (!master_dev) 668ba7d49b1SJiri Pirko return 0; 669ba7d49b1SJiri Pirko ops = master_dev->rtnl_link_ops; 670ba7d49b1SJiri Pirko if (!ops) 671ba7d49b1SJiri Pirko return 0; 672ba7d49b1SJiri Pirko if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0) 673ba7d49b1SJiri Pirko return -EMSGSIZE; 674ba7d49b1SJiri Pirko if (ops->fill_slave_info) { 675ae0be8deSMichal Kubecek slave_data = nla_nest_start_noflag(skb, IFLA_INFO_SLAVE_DATA); 676ba7d49b1SJiri Pirko if (!slave_data) 677ba7d49b1SJiri Pirko return -EMSGSIZE; 678ba7d49b1SJiri Pirko err = ops->fill_slave_info(skb, master_dev, dev); 679ba7d49b1SJiri Pirko if (err < 0) 680ba7d49b1SJiri Pirko goto err_cancel_slave_data; 681ba7d49b1SJiri Pirko nla_nest_end(skb, slave_data); 682ba7d49b1SJiri Pirko } 683ba7d49b1SJiri Pirko return 0; 684ba7d49b1SJiri Pirko 685ba7d49b1SJiri Pirko err_cancel_slave_data: 686ba7d49b1SJiri Pirko nla_nest_cancel(skb, slave_data); 687ba7d49b1SJiri Pirko return err; 688ba7d49b1SJiri Pirko } 689ba7d49b1SJiri Pirko 690ba7d49b1SJiri Pirko static int rtnl_link_info_fill(struct sk_buff *skb, 691ba7d49b1SJiri Pirko const struct net_device *dev) 69238f7b870SPatrick McHardy { 69338f7b870SPatrick McHardy const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 694ba7d49b1SJiri Pirko struct nlattr *data; 695ba7d49b1SJiri Pirko int err; 696ba7d49b1SJiri Pirko 697ba7d49b1SJiri Pirko if (!ops) 698ba7d49b1SJiri Pirko return 0; 699ba7d49b1SJiri Pirko if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0) 700ba7d49b1SJiri Pirko return -EMSGSIZE; 701ba7d49b1SJiri Pirko if (ops->fill_xstats) { 702ba7d49b1SJiri Pirko err = ops->fill_xstats(skb, dev); 703ba7d49b1SJiri Pirko if (err < 0) 704ba7d49b1SJiri Pirko return err; 705ba7d49b1SJiri Pirko } 706ba7d49b1SJiri Pirko if (ops->fill_info) { 707ae0be8deSMichal Kubecek data = nla_nest_start_noflag(skb, IFLA_INFO_DATA); 708ba7d49b1SJiri Pirko if (data == NULL) 709ba7d49b1SJiri Pirko return -EMSGSIZE; 710ba7d49b1SJiri Pirko err = ops->fill_info(skb, dev); 711ba7d49b1SJiri Pirko if (err < 0) 712ba7d49b1SJiri Pirko goto err_cancel_data; 713ba7d49b1SJiri Pirko nla_nest_end(skb, data); 714ba7d49b1SJiri Pirko } 715ba7d49b1SJiri Pirko return 0; 716ba7d49b1SJiri Pirko 717ba7d49b1SJiri Pirko err_cancel_data: 718ba7d49b1SJiri Pirko nla_nest_cancel(skb, data); 719ba7d49b1SJiri Pirko return err; 720ba7d49b1SJiri Pirko } 721ba7d49b1SJiri Pirko 722ba7d49b1SJiri Pirko static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev) 723ba7d49b1SJiri Pirko { 724ba7d49b1SJiri Pirko struct nlattr *linkinfo; 72538f7b870SPatrick McHardy int err = -EMSGSIZE; 72638f7b870SPatrick McHardy 727ae0be8deSMichal Kubecek linkinfo = nla_nest_start_noflag(skb, IFLA_LINKINFO); 72838f7b870SPatrick McHardy if (linkinfo == NULL) 72938f7b870SPatrick McHardy goto out; 73038f7b870SPatrick McHardy 731ba7d49b1SJiri Pirko err = rtnl_link_info_fill(skb, dev); 73238f7b870SPatrick McHardy if (err < 0) 73338f7b870SPatrick McHardy goto err_cancel_link; 734ba7d49b1SJiri Pirko 735ba7d49b1SJiri Pirko err = rtnl_link_slave_info_fill(skb, dev); 73638f7b870SPatrick McHardy if (err < 0) 737ba7d49b1SJiri Pirko goto err_cancel_link; 73838f7b870SPatrick McHardy 73938f7b870SPatrick McHardy nla_nest_end(skb, linkinfo); 74038f7b870SPatrick McHardy return 0; 74138f7b870SPatrick McHardy 74238f7b870SPatrick McHardy err_cancel_link: 74338f7b870SPatrick McHardy nla_nest_cancel(skb, linkinfo); 74438f7b870SPatrick McHardy out: 74538f7b870SPatrick McHardy return err; 74638f7b870SPatrick McHardy } 74738f7b870SPatrick McHardy 74895c96174SEric Dumazet int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo) 7491da177e4SLinus Torvalds { 75097c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 7511da177e4SLinus Torvalds 752cfdf0d9aSYajun Deng return nlmsg_notify(rtnl, skb, pid, group, echo, GFP_KERNEL); 7531da177e4SLinus Torvalds } 7541da177e4SLinus Torvalds 75597c53cacSDenis V. Lunev int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid) 7562942e900SThomas Graf { 75797c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 75897c53cacSDenis V. Lunev 7592942e900SThomas Graf return nlmsg_unicast(rtnl, skb, pid); 7602942e900SThomas Graf } 761e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unicast); 7622942e900SThomas Graf 7631ce85fe4SPablo Neira Ayuso void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group, 7641d997f10SHangbin Liu const struct nlmsghdr *nlh, gfp_t flags) 76597676b6bSThomas Graf { 76697c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 76797676b6bSThomas Graf 768f9b282b3SYajun Deng nlmsg_notify(rtnl, skb, pid, group, nlmsg_report(nlh), flags); 76997676b6bSThomas Graf } 770e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_notify); 77197676b6bSThomas Graf 77297c53cacSDenis V. Lunev void rtnl_set_sk_err(struct net *net, u32 group, int error) 77397676b6bSThomas Graf { 77497c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 77597c53cacSDenis V. Lunev 77697676b6bSThomas Graf netlink_set_err(rtnl, 0, group, error); 77797676b6bSThomas Graf } 778e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_set_sk_err); 77997676b6bSThomas Graf 7801da177e4SLinus Torvalds int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics) 7811da177e4SLinus Torvalds { 7822d7202bfSThomas Graf struct nlattr *mx; 7832d7202bfSThomas Graf int i, valid = 0; 7841da177e4SLinus Torvalds 785c22a133aSDavid Ahern /* nothing is dumped for dst_default_metrics, so just skip the loop */ 786c22a133aSDavid Ahern if (metrics == dst_default_metrics.metrics) 787c22a133aSDavid Ahern return 0; 788c22a133aSDavid Ahern 789ae0be8deSMichal Kubecek mx = nla_nest_start_noflag(skb, RTA_METRICS); 7902d7202bfSThomas Graf if (mx == NULL) 7912d7202bfSThomas Graf return -ENOBUFS; 7922d7202bfSThomas Graf 7931da177e4SLinus Torvalds for (i = 0; i < RTAX_MAX; i++) { 7942d7202bfSThomas Graf if (metrics[i]) { 795ea697639SDaniel Borkmann if (i == RTAX_CC_ALGO - 1) { 796ea697639SDaniel Borkmann char tmp[TCP_CA_NAME_MAX], *name; 797ea697639SDaniel Borkmann 798ea697639SDaniel Borkmann name = tcp_ca_get_name_by_key(metrics[i], tmp); 799ea697639SDaniel Borkmann if (!name) 800ea697639SDaniel Borkmann continue; 801ea697639SDaniel Borkmann if (nla_put_string(skb, i + 1, name)) 802ea697639SDaniel Borkmann goto nla_put_failure; 803c3a8d947SDaniel Borkmann } else if (i == RTAX_FEATURES - 1) { 804c3a8d947SDaniel Borkmann u32 user_features = metrics[i] & RTAX_FEATURE_MASK; 805c3a8d947SDaniel Borkmann 806f8edcd12SPhil Sutter if (!user_features) 807f8edcd12SPhil Sutter continue; 808c3a8d947SDaniel Borkmann BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK); 809c3a8d947SDaniel Borkmann if (nla_put_u32(skb, i + 1, user_features)) 810c3a8d947SDaniel Borkmann goto nla_put_failure; 811ea697639SDaniel Borkmann } else { 812a6574349SDavid S. Miller if (nla_put_u32(skb, i + 1, metrics[i])) 813a6574349SDavid S. Miller goto nla_put_failure; 8141da177e4SLinus Torvalds } 815ea697639SDaniel Borkmann valid++; 816ea697639SDaniel Borkmann } 8172d7202bfSThomas Graf } 8181da177e4SLinus Torvalds 819a57d27fcSDavid S. Miller if (!valid) { 820a57d27fcSDavid S. Miller nla_nest_cancel(skb, mx); 821a57d27fcSDavid S. Miller return 0; 822a57d27fcSDavid S. Miller } 8232d7202bfSThomas Graf 8242d7202bfSThomas Graf return nla_nest_end(skb, mx); 8252d7202bfSThomas Graf 8262d7202bfSThomas Graf nla_put_failure: 827bc3ed28cSThomas Graf nla_nest_cancel(skb, mx); 828bc3ed28cSThomas Graf return -EMSGSIZE; 8291da177e4SLinus Torvalds } 830e0d087afSEric Dumazet EXPORT_SYMBOL(rtnetlink_put_metrics); 8311da177e4SLinus Torvalds 832e3703b3dSThomas Graf int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id, 83387a50699SDavid S. Miller long expires, u32 error) 834e3703b3dSThomas Graf { 835e3703b3dSThomas Graf struct rta_cacheinfo ci = { 836e3703b3dSThomas Graf .rta_error = error, 837e3703b3dSThomas Graf .rta_id = id, 838e3703b3dSThomas Graf }; 839e3703b3dSThomas Graf 8403940746dSDavid Ahern if (dst) { 8413940746dSDavid Ahern ci.rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse); 8423940746dSDavid Ahern ci.rta_used = dst->__use; 8433940746dSDavid Ahern ci.rta_clntref = atomic_read(&dst->__refcnt); 8443940746dSDavid Ahern } 8458253947eSLi Wei if (expires) { 8468253947eSLi Wei unsigned long clock; 847e3703b3dSThomas Graf 8488253947eSLi Wei clock = jiffies_to_clock_t(abs(expires)); 8498253947eSLi Wei clock = min_t(unsigned long, clock, INT_MAX); 8508253947eSLi Wei ci.rta_expires = (expires > 0) ? clock : -clock; 8518253947eSLi Wei } 852e3703b3dSThomas Graf return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci); 853e3703b3dSThomas Graf } 854e3703b3dSThomas Graf EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo); 8551da177e4SLinus Torvalds 85693b2d4a2SDavid S. Miller static void set_operstate(struct net_device *dev, unsigned char transition) 857b00055aaSStefan Rompf { 858b00055aaSStefan Rompf unsigned char operstate = dev->operstate; 859b00055aaSStefan Rompf 860b00055aaSStefan Rompf switch (transition) { 861b00055aaSStefan Rompf case IF_OPER_UP: 862b00055aaSStefan Rompf if ((operstate == IF_OPER_DORMANT || 863eec517cdSAndrew Lunn operstate == IF_OPER_TESTING || 864b00055aaSStefan Rompf operstate == IF_OPER_UNKNOWN) && 865eec517cdSAndrew Lunn !netif_dormant(dev) && !netif_testing(dev)) 866b00055aaSStefan Rompf operstate = IF_OPER_UP; 867b00055aaSStefan Rompf break; 868b00055aaSStefan Rompf 869eec517cdSAndrew Lunn case IF_OPER_TESTING: 870abbc7928SJuhee Kang if (netif_oper_up(dev)) 871eec517cdSAndrew Lunn operstate = IF_OPER_TESTING; 872eec517cdSAndrew Lunn break; 873eec517cdSAndrew Lunn 874b00055aaSStefan Rompf case IF_OPER_DORMANT: 875abbc7928SJuhee Kang if (netif_oper_up(dev)) 876b00055aaSStefan Rompf operstate = IF_OPER_DORMANT; 877b00055aaSStefan Rompf break; 8783ff50b79SStephen Hemminger } 879b00055aaSStefan Rompf 880b00055aaSStefan Rompf if (dev->operstate != operstate) { 881fd888e85SSebastian Andrzej Siewior write_lock(&dev_base_lock); 882b00055aaSStefan Rompf dev->operstate = operstate; 883fd888e85SSebastian Andrzej Siewior write_unlock(&dev_base_lock); 884b00055aaSStefan Rompf netdev_state_change(dev); 88593b2d4a2SDavid S. Miller } 886b00055aaSStefan Rompf } 887b00055aaSStefan Rompf 888b1beb681SJiri Benc static unsigned int rtnl_dev_get_flags(const struct net_device *dev) 889b1beb681SJiri Benc { 890b1beb681SJiri Benc return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) | 891b1beb681SJiri Benc (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI)); 892b1beb681SJiri Benc } 893b1beb681SJiri Benc 8943729d502SPatrick McHardy static unsigned int rtnl_dev_combine_flags(const struct net_device *dev, 8953729d502SPatrick McHardy const struct ifinfomsg *ifm) 8963729d502SPatrick McHardy { 8973729d502SPatrick McHardy unsigned int flags = ifm->ifi_flags; 8983729d502SPatrick McHardy 8993729d502SPatrick McHardy /* bugwards compatibility: ifi_change == 0 is treated as ~0 */ 9003729d502SPatrick McHardy if (ifm->ifi_change) 9013729d502SPatrick McHardy flags = (flags & ifm->ifi_change) | 902b1beb681SJiri Benc (rtnl_dev_get_flags(dev) & ~ifm->ifi_change); 9033729d502SPatrick McHardy 9043729d502SPatrick McHardy return flags; 9053729d502SPatrick McHardy } 9063729d502SPatrick McHardy 907b60c5115SThomas Graf static void copy_rtnl_link_stats(struct rtnl_link_stats *a, 908be1f3c2cSBen Hutchings const struct rtnl_link_stats64 *b) 9091da177e4SLinus Torvalds { 910b60c5115SThomas Graf a->rx_packets = b->rx_packets; 911b60c5115SThomas Graf a->tx_packets = b->tx_packets; 912b60c5115SThomas Graf a->rx_bytes = b->rx_bytes; 913b60c5115SThomas Graf a->tx_bytes = b->tx_bytes; 914b60c5115SThomas Graf a->rx_errors = b->rx_errors; 915b60c5115SThomas Graf a->tx_errors = b->tx_errors; 916b60c5115SThomas Graf a->rx_dropped = b->rx_dropped; 917b60c5115SThomas Graf a->tx_dropped = b->tx_dropped; 918b60c5115SThomas Graf 919b60c5115SThomas Graf a->multicast = b->multicast; 920b60c5115SThomas Graf a->collisions = b->collisions; 921b60c5115SThomas Graf 922b60c5115SThomas Graf a->rx_length_errors = b->rx_length_errors; 923b60c5115SThomas Graf a->rx_over_errors = b->rx_over_errors; 924b60c5115SThomas Graf a->rx_crc_errors = b->rx_crc_errors; 925b60c5115SThomas Graf a->rx_frame_errors = b->rx_frame_errors; 926b60c5115SThomas Graf a->rx_fifo_errors = b->rx_fifo_errors; 927b60c5115SThomas Graf a->rx_missed_errors = b->rx_missed_errors; 928b60c5115SThomas Graf 929b60c5115SThomas Graf a->tx_aborted_errors = b->tx_aborted_errors; 930b60c5115SThomas Graf a->tx_carrier_errors = b->tx_carrier_errors; 931b60c5115SThomas Graf a->tx_fifo_errors = b->tx_fifo_errors; 932b60c5115SThomas Graf a->tx_heartbeat_errors = b->tx_heartbeat_errors; 933b60c5115SThomas Graf a->tx_window_errors = b->tx_window_errors; 934b60c5115SThomas Graf 935b60c5115SThomas Graf a->rx_compressed = b->rx_compressed; 936b60c5115SThomas Graf a->tx_compressed = b->tx_compressed; 9376e7333d3SJarod Wilson 9386e7333d3SJarod Wilson a->rx_nohandler = b->rx_nohandler; 93910708f37SJan Engelhardt } 94010708f37SJan Engelhardt 941c02db8c6SChris Wright /* All VF info */ 942115c9b81SGreg Rose static inline int rtnl_vfinfo_size(const struct net_device *dev, 943115c9b81SGreg Rose u32 ext_filter_mask) 944ebc08a6fSWilliams, Mitch A { 9459af15c38SPhil Sutter if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) { 946c02db8c6SChris Wright int num_vfs = dev_num_vf(dev->dev.parent); 9477e75f74aSSabrina Dubroca size_t size = nla_total_size(0); 948045de01aSScott Feldman size += num_vfs * 9497e75f74aSSabrina Dubroca (nla_total_size(0) + 9507e75f74aSSabrina Dubroca nla_total_size(sizeof(struct ifla_vf_mac)) + 95175345f88SDenis Kirjanov nla_total_size(sizeof(struct ifla_vf_broadcast)) + 9527e75f74aSSabrina Dubroca nla_total_size(sizeof(struct ifla_vf_vlan)) + 9537e75f74aSSabrina Dubroca nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */ 95479aab093SMoshe Shemesh nla_total_size(MAX_VLAN_LIST_LEN * 95579aab093SMoshe Shemesh sizeof(struct ifla_vf_vlan_info)) + 956ed616689SSucheta Chakraborty nla_total_size(sizeof(struct ifla_vf_spoofchk)) + 9577e75f74aSSabrina Dubroca nla_total_size(sizeof(struct ifla_vf_tx_rate)) + 958945a3676SJiri Benc nla_total_size(sizeof(struct ifla_vf_rate)) + 95901a3d796SVlad Zolotarov nla_total_size(sizeof(struct ifla_vf_link_state)) + 9603b766cd8SEran Ben Elisha nla_total_size(sizeof(struct ifla_vf_rss_query_en)) + 9617e75f74aSSabrina Dubroca nla_total_size(0) + /* nest IFLA_VF_STATS */ 9623b766cd8SEran Ben Elisha /* IFLA_VF_STATS_RX_PACKETS */ 963343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 9643b766cd8SEran Ben Elisha /* IFLA_VF_STATS_TX_PACKETS */ 965343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 9663b766cd8SEran Ben Elisha /* IFLA_VF_STATS_RX_BYTES */ 967343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 9683b766cd8SEran Ben Elisha /* IFLA_VF_STATS_TX_BYTES */ 969343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 9703b766cd8SEran Ben Elisha /* IFLA_VF_STATS_BROADCAST */ 971343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 9723b766cd8SEran Ben Elisha /* IFLA_VF_STATS_MULTICAST */ 973343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 974c5a9f6f0SEugenia Emantayev /* IFLA_VF_STATS_RX_DROPPED */ 975c5a9f6f0SEugenia Emantayev nla_total_size_64bit(sizeof(__u64)) + 976c5a9f6f0SEugenia Emantayev /* IFLA_VF_STATS_TX_DROPPED */ 977c5a9f6f0SEugenia Emantayev nla_total_size_64bit(sizeof(__u64)) + 978dd461d6aSHiroshi Shimamoto nla_total_size(sizeof(struct ifla_vf_trust))); 979c02db8c6SChris Wright return size; 980c02db8c6SChris Wright } else 981ebc08a6fSWilliams, Mitch A return 0; 982ebc08a6fSWilliams, Mitch A } 983ebc08a6fSWilliams, Mitch A 984c53864fdSDavid Gibson static size_t rtnl_port_size(const struct net_device *dev, 985c53864fdSDavid Gibson u32 ext_filter_mask) 98657b61080SScott Feldman { 98757b61080SScott Feldman size_t port_size = nla_total_size(4) /* PORT_VF */ 98857b61080SScott Feldman + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */ 98957b61080SScott Feldman + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */ 99057b61080SScott Feldman + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */ 99157b61080SScott Feldman + nla_total_size(1) /* PROT_VDP_REQUEST */ 99257b61080SScott Feldman + nla_total_size(2); /* PORT_VDP_RESPONSE */ 99357b61080SScott Feldman size_t vf_ports_size = nla_total_size(sizeof(struct nlattr)); 99457b61080SScott Feldman size_t vf_port_size = nla_total_size(sizeof(struct nlattr)) 99557b61080SScott Feldman + port_size; 99657b61080SScott Feldman size_t port_self_size = nla_total_size(sizeof(struct nlattr)) 99757b61080SScott Feldman + port_size; 99857b61080SScott Feldman 999c53864fdSDavid Gibson if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent || 1000c53864fdSDavid Gibson !(ext_filter_mask & RTEXT_FILTER_VF)) 100157b61080SScott Feldman return 0; 100257b61080SScott Feldman if (dev_num_vf(dev->dev.parent)) 100357b61080SScott Feldman return port_self_size + vf_ports_size + 100457b61080SScott Feldman vf_port_size * dev_num_vf(dev->dev.parent); 100557b61080SScott Feldman else 100657b61080SScott Feldman return port_self_size; 100757b61080SScott Feldman } 100857b61080SScott Feldman 1009b5cdae32SDavid S. Miller static size_t rtnl_xdp_size(void) 1010d1fdd913SBrenden Blanco { 1011b3cfaa31SSabrina Dubroca size_t xdp_size = nla_total_size(0) + /* nest IFLA_XDP */ 101258038695SMartin KaFai Lau nla_total_size(1) + /* XDP_ATTACHED */ 1013a25717d2SJakub Kicinski nla_total_size(4) + /* XDP_PROG_ID (or 1st mode) */ 10144f91da26SJakub Kicinski nla_total_size(4); /* XDP_<mode>_PROG_ID */ 1015d1fdd913SBrenden Blanco 1016d1fdd913SBrenden Blanco return xdp_size; 1017d1fdd913SBrenden Blanco } 1018d1fdd913SBrenden Blanco 101988f4fb0cSJiri Pirko static size_t rtnl_prop_list_size(const struct net_device *dev) 102088f4fb0cSJiri Pirko { 102188f4fb0cSJiri Pirko struct netdev_name_node *name_node; 102288f4fb0cSJiri Pirko size_t size; 102388f4fb0cSJiri Pirko 102488f4fb0cSJiri Pirko if (list_empty(&dev->name_node->list)) 102588f4fb0cSJiri Pirko return 0; 102688f4fb0cSJiri Pirko size = nla_total_size(0); 102788f4fb0cSJiri Pirko list_for_each_entry(name_node, &dev->name_node->list, list) 102888f4fb0cSJiri Pirko size += nla_total_size(ALTIFNAMSIZ); 102988f4fb0cSJiri Pirko return size; 103088f4fb0cSJiri Pirko } 103188f4fb0cSJiri Pirko 1032829eb208SRoopa Prabhu static size_t rtnl_proto_down_size(const struct net_device *dev) 1033829eb208SRoopa Prabhu { 1034829eb208SRoopa Prabhu size_t size = nla_total_size(1); 1035829eb208SRoopa Prabhu 1036829eb208SRoopa Prabhu if (dev->proto_down_reason) 1037829eb208SRoopa Prabhu size += nla_total_size(0) + nla_total_size(4); 1038829eb208SRoopa Prabhu 1039829eb208SRoopa Prabhu return size; 1040829eb208SRoopa Prabhu } 1041829eb208SRoopa Prabhu 1042dca56c30SJiri Pirko static size_t rtnl_devlink_port_size(const struct net_device *dev) 1043dca56c30SJiri Pirko { 1044dca56c30SJiri Pirko size_t size = nla_total_size(0); /* nest IFLA_DEVLINK_PORT */ 1045dca56c30SJiri Pirko 1046dca56c30SJiri Pirko if (dev->devlink_port) 1047dca56c30SJiri Pirko size += devlink_nl_port_handle_size(dev->devlink_port); 1048dca56c30SJiri Pirko 1049dca56c30SJiri Pirko return size; 1050dca56c30SJiri Pirko } 1051dca56c30SJiri Pirko 1052115c9b81SGreg Rose static noinline size_t if_nlmsg_size(const struct net_device *dev, 1053115c9b81SGreg Rose u32 ext_filter_mask) 1054339bf98fSThomas Graf { 1055339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 1056339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 10570b815a1aSStephen Hemminger + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */ 1058339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */ 1059270cb4d0SNicolas Dichtel + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap)) 1060339bf98fSThomas Graf + nla_total_size(sizeof(struct rtnl_link_stats)) 106135c58459SDavid S. Miller + nla_total_size_64bit(sizeof(struct rtnl_link_stats64)) 1062339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 1063339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */ 1064339bf98fSThomas Graf + nla_total_size(4) /* IFLA_TXQLEN */ 1065339bf98fSThomas Graf + nla_total_size(4) /* IFLA_WEIGHT */ 1066339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MTU */ 1067339bf98fSThomas Graf + nla_total_size(4) /* IFLA_LINK */ 1068339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MASTER */ 10699a57247fSJiri Pirko + nla_total_size(1) /* IFLA_CARRIER */ 1070edbc0bb3SBen Greear + nla_total_size(4) /* IFLA_PROMISCUITY */ 10717e6e1b57SNicolas Dichtel + nla_total_size(4) /* IFLA_ALLMULTI */ 107276ff5cc9SJiri Pirko + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */ 107376ff5cc9SJiri Pirko + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */ 10746919756cSTobias Klauser + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */ 10756919756cSTobias Klauser + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */ 1076eac1b93cSCoco Li + nla_total_size(4) /* IFLA_GRO_MAX_SIZE */ 1077*9eefedd5SXin Long + nla_total_size(4) /* IFLA_GSO_IPV4_MAX_SIZE */ 1078*9eefedd5SXin Long + nla_total_size(4) /* IFLA_GRO_IPV4_MAX_SIZE */ 107989527be8SEric Dumazet + nla_total_size(4) /* IFLA_TSO_MAX_SIZE */ 108089527be8SEric Dumazet + nla_total_size(4) /* IFLA_TSO_MAX_SEGS */ 1081339bf98fSThomas Graf + nla_total_size(1) /* IFLA_OPERSTATE */ 108238f7b870SPatrick McHardy + nla_total_size(1) /* IFLA_LINKMODE */ 10832d3b479dSdavid decotigny + nla_total_size(4) /* IFLA_CARRIER_CHANGES */ 1084d37512a2SNicolas Dichtel + nla_total_size(4) /* IFLA_LINK_NETNSID */ 1085db833d40SSerhey Popovych + nla_total_size(4) /* IFLA_GROUP */ 1086115c9b81SGreg Rose + nla_total_size(ext_filter_mask 1087115c9b81SGreg Rose & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */ 1088115c9b81SGreg Rose + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */ 1089c53864fdSDavid Gibson + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */ 1090f8ff182cSThomas Graf + rtnl_link_get_size(dev) /* IFLA_LINKINFO */ 1091b1974ed0SArad, Ronen + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */ 109282f28412SJiri Pirko + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */ 109388d6378bSAnuradha Karuppiah + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */ 1094c57c7a95SNicolas Dichtel + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */ 1095b5cdae32SDavid S. Miller + rtnl_xdp_size() /* IFLA_XDP */ 10963d3ea5afSVlad Yasevich + nla_total_size(4) /* IFLA_EVENT */ 10976621dd29SNicolas Dichtel + nla_total_size(4) /* IFLA_NEW_NETNSID */ 109838e01b30SNicolas Dichtel + nla_total_size(4) /* IFLA_NEW_IFINDEX */ 1099829eb208SRoopa Prabhu + rtnl_proto_down_size(dev) /* proto down */ 11007e4a8d5aSChristian Brauner + nla_total_size(4) /* IFLA_TARGET_NETNSID */ 1101b2d3bcfaSDavid Decotigny + nla_total_size(4) /* IFLA_CARRIER_UP_COUNT */ 1102b2d3bcfaSDavid Decotigny + nla_total_size(4) /* IFLA_CARRIER_DOWN_COUNT */ 11033e7a50ceSStephen Hemminger + nla_total_size(4) /* IFLA_MIN_MTU */ 11043e7a50ceSStephen Hemminger + nla_total_size(4) /* IFLA_MAX_MTU */ 110588f4fb0cSJiri Pirko + rtnl_prop_list_size(dev) 1106f74877a5SMichal Kubecek + nla_total_size(MAX_ADDR_LEN) /* IFLA_PERM_ADDRESS */ 1107dca56c30SJiri Pirko + rtnl_devlink_port_size(dev) 110879e1ad14SJiri Benc + 0; 1109339bf98fSThomas Graf } 1110339bf98fSThomas Graf 111157b61080SScott Feldman static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev) 111257b61080SScott Feldman { 111357b61080SScott Feldman struct nlattr *vf_ports; 111457b61080SScott Feldman struct nlattr *vf_port; 111557b61080SScott Feldman int vf; 111657b61080SScott Feldman int err; 111757b61080SScott Feldman 1118ae0be8deSMichal Kubecek vf_ports = nla_nest_start_noflag(skb, IFLA_VF_PORTS); 111957b61080SScott Feldman if (!vf_ports) 112057b61080SScott Feldman return -EMSGSIZE; 112157b61080SScott Feldman 112257b61080SScott Feldman for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) { 1123ae0be8deSMichal Kubecek vf_port = nla_nest_start_noflag(skb, IFLA_VF_PORT); 11248ca94183SScott Feldman if (!vf_port) 11258ca94183SScott Feldman goto nla_put_failure; 1126a6574349SDavid S. Miller if (nla_put_u32(skb, IFLA_PORT_VF, vf)) 1127a6574349SDavid S. Miller goto nla_put_failure; 112857b61080SScott Feldman err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb); 11298ca94183SScott Feldman if (err == -EMSGSIZE) 11308ca94183SScott Feldman goto nla_put_failure; 113157b61080SScott Feldman if (err) { 113257b61080SScott Feldman nla_nest_cancel(skb, vf_port); 113357b61080SScott Feldman continue; 113457b61080SScott Feldman } 113557b61080SScott Feldman nla_nest_end(skb, vf_port); 113657b61080SScott Feldman } 113757b61080SScott Feldman 113857b61080SScott Feldman nla_nest_end(skb, vf_ports); 113957b61080SScott Feldman 114057b61080SScott Feldman return 0; 11418ca94183SScott Feldman 11428ca94183SScott Feldman nla_put_failure: 11438ca94183SScott Feldman nla_nest_cancel(skb, vf_ports); 11448ca94183SScott Feldman return -EMSGSIZE; 114557b61080SScott Feldman } 114657b61080SScott Feldman 114757b61080SScott Feldman static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev) 114857b61080SScott Feldman { 114957b61080SScott Feldman struct nlattr *port_self; 115057b61080SScott Feldman int err; 115157b61080SScott Feldman 1152ae0be8deSMichal Kubecek port_self = nla_nest_start_noflag(skb, IFLA_PORT_SELF); 115357b61080SScott Feldman if (!port_self) 115457b61080SScott Feldman return -EMSGSIZE; 115557b61080SScott Feldman 115657b61080SScott Feldman err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb); 115757b61080SScott Feldman if (err) { 115857b61080SScott Feldman nla_nest_cancel(skb, port_self); 11598ca94183SScott Feldman return (err == -EMSGSIZE) ? err : 0; 116057b61080SScott Feldman } 116157b61080SScott Feldman 116257b61080SScott Feldman nla_nest_end(skb, port_self); 116357b61080SScott Feldman 116457b61080SScott Feldman return 0; 116557b61080SScott Feldman } 116657b61080SScott Feldman 1167c53864fdSDavid Gibson static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev, 1168c53864fdSDavid Gibson u32 ext_filter_mask) 116957b61080SScott Feldman { 117057b61080SScott Feldman int err; 117157b61080SScott Feldman 1172c53864fdSDavid Gibson if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent || 1173c53864fdSDavid Gibson !(ext_filter_mask & RTEXT_FILTER_VF)) 117457b61080SScott Feldman return 0; 117557b61080SScott Feldman 117657b61080SScott Feldman err = rtnl_port_self_fill(skb, dev); 117757b61080SScott Feldman if (err) 117857b61080SScott Feldman return err; 117957b61080SScott Feldman 118057b61080SScott Feldman if (dev_num_vf(dev->dev.parent)) { 118157b61080SScott Feldman err = rtnl_vf_ports_fill(skb, dev); 118257b61080SScott Feldman if (err) 118357b61080SScott Feldman return err; 118457b61080SScott Feldman } 118557b61080SScott Feldman 118657b61080SScott Feldman return 0; 118757b61080SScott Feldman } 118857b61080SScott Feldman 118966cae9edSJiri Pirko static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev) 119066cae9edSJiri Pirko { 119166cae9edSJiri Pirko int err; 119202637fceSJiri Pirko struct netdev_phys_item_id ppid; 119366cae9edSJiri Pirko 119466cae9edSJiri Pirko err = dev_get_phys_port_id(dev, &ppid); 119566cae9edSJiri Pirko if (err) { 119666cae9edSJiri Pirko if (err == -EOPNOTSUPP) 119766cae9edSJiri Pirko return 0; 119866cae9edSJiri Pirko return err; 119966cae9edSJiri Pirko } 120066cae9edSJiri Pirko 120166cae9edSJiri Pirko if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id)) 120266cae9edSJiri Pirko return -EMSGSIZE; 120366cae9edSJiri Pirko 120466cae9edSJiri Pirko return 0; 120566cae9edSJiri Pirko } 120666cae9edSJiri Pirko 1207db24a904SDavid Ahern static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev) 1208db24a904SDavid Ahern { 1209db24a904SDavid Ahern char name[IFNAMSIZ]; 1210db24a904SDavid Ahern int err; 1211db24a904SDavid Ahern 1212db24a904SDavid Ahern err = dev_get_phys_port_name(dev, name, sizeof(name)); 1213db24a904SDavid Ahern if (err) { 1214db24a904SDavid Ahern if (err == -EOPNOTSUPP) 1215db24a904SDavid Ahern return 0; 1216db24a904SDavid Ahern return err; 1217db24a904SDavid Ahern } 1218db24a904SDavid Ahern 121977ef033bSMichal Schmidt if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name)) 1220db24a904SDavid Ahern return -EMSGSIZE; 1221db24a904SDavid Ahern 1222db24a904SDavid Ahern return 0; 1223db24a904SDavid Ahern } 1224db24a904SDavid Ahern 122582f28412SJiri Pirko static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev) 122682f28412SJiri Pirko { 1227bccb3025SFlorian Fainelli struct netdev_phys_item_id ppid = { }; 122882f28412SJiri Pirko int err; 122982f28412SJiri Pirko 1230bccb3025SFlorian Fainelli err = dev_get_port_parent_id(dev, &ppid, false); 123182f28412SJiri Pirko if (err) { 123282f28412SJiri Pirko if (err == -EOPNOTSUPP) 123382f28412SJiri Pirko return 0; 123482f28412SJiri Pirko return err; 123582f28412SJiri Pirko } 123682f28412SJiri Pirko 1237bccb3025SFlorian Fainelli if (nla_put(skb, IFLA_PHYS_SWITCH_ID, ppid.id_len, ppid.id)) 123882f28412SJiri Pirko return -EMSGSIZE; 123982f28412SJiri Pirko 124082f28412SJiri Pirko return 0; 124182f28412SJiri Pirko } 124282f28412SJiri Pirko 1243b22b941bSHannes Frederic Sowa static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb, 1244b22b941bSHannes Frederic Sowa struct net_device *dev) 1245b22b941bSHannes Frederic Sowa { 1246550bce59SRoopa Prabhu struct rtnl_link_stats64 *sp; 1247b22b941bSHannes Frederic Sowa struct nlattr *attr; 1248b22b941bSHannes Frederic Sowa 124958414d32SNicolas Dichtel attr = nla_reserve_64bit(skb, IFLA_STATS64, 125058414d32SNicolas Dichtel sizeof(struct rtnl_link_stats64), IFLA_PAD); 1251b22b941bSHannes Frederic Sowa if (!attr) 1252b22b941bSHannes Frederic Sowa return -EMSGSIZE; 1253b22b941bSHannes Frederic Sowa 1254550bce59SRoopa Prabhu sp = nla_data(attr); 1255550bce59SRoopa Prabhu dev_get_stats(dev, sp); 1256550bce59SRoopa Prabhu 1257550bce59SRoopa Prabhu attr = nla_reserve(skb, IFLA_STATS, 1258550bce59SRoopa Prabhu sizeof(struct rtnl_link_stats)); 1259550bce59SRoopa Prabhu if (!attr) 1260550bce59SRoopa Prabhu return -EMSGSIZE; 1261550bce59SRoopa Prabhu 1262550bce59SRoopa Prabhu copy_rtnl_link_stats(nla_data(attr), sp); 1263b22b941bSHannes Frederic Sowa 1264b22b941bSHannes Frederic Sowa return 0; 1265b22b941bSHannes Frederic Sowa } 1266b22b941bSHannes Frederic Sowa 1267b22b941bSHannes Frederic Sowa static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb, 1268b22b941bSHannes Frederic Sowa struct net_device *dev, 1269b22b941bSHannes Frederic Sowa int vfs_num, 1270b22b941bSHannes Frederic Sowa struct nlattr *vfinfo) 1271b22b941bSHannes Frederic Sowa { 1272b22b941bSHannes Frederic Sowa struct ifla_vf_rss_query_en vf_rss_query_en; 127379aab093SMoshe Shemesh struct nlattr *vf, *vfstats, *vfvlanlist; 1274b22b941bSHannes Frederic Sowa struct ifla_vf_link_state vf_linkstate; 127579aab093SMoshe Shemesh struct ifla_vf_vlan_info vf_vlan_info; 1276b22b941bSHannes Frederic Sowa struct ifla_vf_spoofchk vf_spoofchk; 1277b22b941bSHannes Frederic Sowa struct ifla_vf_tx_rate vf_tx_rate; 1278b22b941bSHannes Frederic Sowa struct ifla_vf_stats vf_stats; 1279b22b941bSHannes Frederic Sowa struct ifla_vf_trust vf_trust; 1280b22b941bSHannes Frederic Sowa struct ifla_vf_vlan vf_vlan; 1281b22b941bSHannes Frederic Sowa struct ifla_vf_rate vf_rate; 1282b22b941bSHannes Frederic Sowa struct ifla_vf_mac vf_mac; 128375345f88SDenis Kirjanov struct ifla_vf_broadcast vf_broadcast; 1284b22b941bSHannes Frederic Sowa struct ifla_vf_info ivi; 128530aad417SDanit Goldberg struct ifla_vf_guid node_guid; 128630aad417SDanit Goldberg struct ifla_vf_guid port_guid; 1287b22b941bSHannes Frederic Sowa 12880eed9cf5SMintz, Yuval memset(&ivi, 0, sizeof(ivi)); 12890eed9cf5SMintz, Yuval 1290b22b941bSHannes Frederic Sowa /* Not all SR-IOV capable drivers support the 1291b22b941bSHannes Frederic Sowa * spoofcheck and "RSS query enable" query. Preset to 1292b22b941bSHannes Frederic Sowa * -1 so the user space tool can detect that the driver 1293b22b941bSHannes Frederic Sowa * didn't report anything. 1294b22b941bSHannes Frederic Sowa */ 1295b22b941bSHannes Frederic Sowa ivi.spoofchk = -1; 1296b22b941bSHannes Frederic Sowa ivi.rss_query_en = -1; 1297b22b941bSHannes Frederic Sowa ivi.trusted = -1; 1298b22b941bSHannes Frederic Sowa /* The default value for VF link state is "auto" 1299b22b941bSHannes Frederic Sowa * IFLA_VF_LINK_STATE_AUTO which equals zero 1300b22b941bSHannes Frederic Sowa */ 1301b22b941bSHannes Frederic Sowa ivi.linkstate = 0; 130279aab093SMoshe Shemesh /* VLAN Protocol by default is 802.1Q */ 130379aab093SMoshe Shemesh ivi.vlan_proto = htons(ETH_P_8021Q); 1304b22b941bSHannes Frederic Sowa if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi)) 1305b22b941bSHannes Frederic Sowa return 0; 1306b22b941bSHannes Frederic Sowa 1307775f4f05SDan Carpenter memset(&vf_vlan_info, 0, sizeof(vf_vlan_info)); 13089fbf082fSLeon Romanovsky memset(&node_guid, 0, sizeof(node_guid)); 13099fbf082fSLeon Romanovsky memset(&port_guid, 0, sizeof(port_guid)); 1310775f4f05SDan Carpenter 1311b22b941bSHannes Frederic Sowa vf_mac.vf = 1312b22b941bSHannes Frederic Sowa vf_vlan.vf = 131379aab093SMoshe Shemesh vf_vlan_info.vf = 1314b22b941bSHannes Frederic Sowa vf_rate.vf = 1315b22b941bSHannes Frederic Sowa vf_tx_rate.vf = 1316b22b941bSHannes Frederic Sowa vf_spoofchk.vf = 1317b22b941bSHannes Frederic Sowa vf_linkstate.vf = 1318b22b941bSHannes Frederic Sowa vf_rss_query_en.vf = 13199aed6ae0SDanit Goldberg vf_trust.vf = 13209aed6ae0SDanit Goldberg node_guid.vf = 13219aed6ae0SDanit Goldberg port_guid.vf = ivi.vf; 1322b22b941bSHannes Frederic Sowa 1323b22b941bSHannes Frederic Sowa memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac)); 132475345f88SDenis Kirjanov memcpy(vf_broadcast.broadcast, dev->broadcast, dev->addr_len); 1325b22b941bSHannes Frederic Sowa vf_vlan.vlan = ivi.vlan; 1326b22b941bSHannes Frederic Sowa vf_vlan.qos = ivi.qos; 132779aab093SMoshe Shemesh vf_vlan_info.vlan = ivi.vlan; 132879aab093SMoshe Shemesh vf_vlan_info.qos = ivi.qos; 132979aab093SMoshe Shemesh vf_vlan_info.vlan_proto = ivi.vlan_proto; 1330b22b941bSHannes Frederic Sowa vf_tx_rate.rate = ivi.max_tx_rate; 1331b22b941bSHannes Frederic Sowa vf_rate.min_tx_rate = ivi.min_tx_rate; 1332b22b941bSHannes Frederic Sowa vf_rate.max_tx_rate = ivi.max_tx_rate; 1333b22b941bSHannes Frederic Sowa vf_spoofchk.setting = ivi.spoofchk; 1334b22b941bSHannes Frederic Sowa vf_linkstate.link_state = ivi.linkstate; 1335b22b941bSHannes Frederic Sowa vf_rss_query_en.setting = ivi.rss_query_en; 1336b22b941bSHannes Frederic Sowa vf_trust.setting = ivi.trusted; 1337ae0be8deSMichal Kubecek vf = nla_nest_start_noflag(skb, IFLA_VF_INFO); 133879aab093SMoshe Shemesh if (!vf) 133979aab093SMoshe Shemesh goto nla_put_vfinfo_failure; 1340b22b941bSHannes Frederic Sowa if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) || 134175345f88SDenis Kirjanov nla_put(skb, IFLA_VF_BROADCAST, sizeof(vf_broadcast), &vf_broadcast) || 1342b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) || 1343b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate), 1344b22b941bSHannes Frederic Sowa &vf_rate) || 1345b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate), 1346b22b941bSHannes Frederic Sowa &vf_tx_rate) || 1347b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk), 1348b22b941bSHannes Frederic Sowa &vf_spoofchk) || 1349b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate), 1350b22b941bSHannes Frederic Sowa &vf_linkstate) || 1351b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_RSS_QUERY_EN, 1352b22b941bSHannes Frederic Sowa sizeof(vf_rss_query_en), 1353b22b941bSHannes Frederic Sowa &vf_rss_query_en) || 1354b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_TRUST, 1355b22b941bSHannes Frederic Sowa sizeof(vf_trust), &vf_trust)) 135679aab093SMoshe Shemesh goto nla_put_vf_failure; 135730aad417SDanit Goldberg 135830aad417SDanit Goldberg if (dev->netdev_ops->ndo_get_vf_guid && 135930aad417SDanit Goldberg !dev->netdev_ops->ndo_get_vf_guid(dev, vfs_num, &node_guid, 136030aad417SDanit Goldberg &port_guid)) { 136130aad417SDanit Goldberg if (nla_put(skb, IFLA_VF_IB_NODE_GUID, sizeof(node_guid), 136230aad417SDanit Goldberg &node_guid) || 136330aad417SDanit Goldberg nla_put(skb, IFLA_VF_IB_PORT_GUID, sizeof(port_guid), 136430aad417SDanit Goldberg &port_guid)) 136530aad417SDanit Goldberg goto nla_put_vf_failure; 136630aad417SDanit Goldberg } 1367ae0be8deSMichal Kubecek vfvlanlist = nla_nest_start_noflag(skb, IFLA_VF_VLAN_LIST); 136879aab093SMoshe Shemesh if (!vfvlanlist) 136979aab093SMoshe Shemesh goto nla_put_vf_failure; 137079aab093SMoshe Shemesh if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info), 137179aab093SMoshe Shemesh &vf_vlan_info)) { 137279aab093SMoshe Shemesh nla_nest_cancel(skb, vfvlanlist); 137379aab093SMoshe Shemesh goto nla_put_vf_failure; 137479aab093SMoshe Shemesh } 137579aab093SMoshe Shemesh nla_nest_end(skb, vfvlanlist); 1376b22b941bSHannes Frederic Sowa memset(&vf_stats, 0, sizeof(vf_stats)); 1377b22b941bSHannes Frederic Sowa if (dev->netdev_ops->ndo_get_vf_stats) 1378b22b941bSHannes Frederic Sowa dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num, 1379b22b941bSHannes Frederic Sowa &vf_stats); 1380ae0be8deSMichal Kubecek vfstats = nla_nest_start_noflag(skb, IFLA_VF_STATS); 138179aab093SMoshe Shemesh if (!vfstats) 138279aab093SMoshe Shemesh goto nla_put_vf_failure; 1383343a6d8eSNicolas Dichtel if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS, 1384343a6d8eSNicolas Dichtel vf_stats.rx_packets, IFLA_VF_STATS_PAD) || 1385343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS, 1386343a6d8eSNicolas Dichtel vf_stats.tx_packets, IFLA_VF_STATS_PAD) || 1387343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES, 1388343a6d8eSNicolas Dichtel vf_stats.rx_bytes, IFLA_VF_STATS_PAD) || 1389343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES, 1390343a6d8eSNicolas Dichtel vf_stats.tx_bytes, IFLA_VF_STATS_PAD) || 1391343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST, 1392343a6d8eSNicolas Dichtel vf_stats.broadcast, IFLA_VF_STATS_PAD) || 1393343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST, 1394c5a9f6f0SEugenia Emantayev vf_stats.multicast, IFLA_VF_STATS_PAD) || 1395c5a9f6f0SEugenia Emantayev nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED, 1396c5a9f6f0SEugenia Emantayev vf_stats.rx_dropped, IFLA_VF_STATS_PAD) || 1397c5a9f6f0SEugenia Emantayev nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED, 1398c5a9f6f0SEugenia Emantayev vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) { 139979aab093SMoshe Shemesh nla_nest_cancel(skb, vfstats); 140079aab093SMoshe Shemesh goto nla_put_vf_failure; 140179aab093SMoshe Shemesh } 1402b22b941bSHannes Frederic Sowa nla_nest_end(skb, vfstats); 1403b22b941bSHannes Frederic Sowa nla_nest_end(skb, vf); 1404b22b941bSHannes Frederic Sowa return 0; 140579aab093SMoshe Shemesh 140679aab093SMoshe Shemesh nla_put_vf_failure: 140779aab093SMoshe Shemesh nla_nest_cancel(skb, vf); 140879aab093SMoshe Shemesh nla_put_vfinfo_failure: 140979aab093SMoshe Shemesh nla_nest_cancel(skb, vfinfo); 141079aab093SMoshe Shemesh return -EMSGSIZE; 1411b22b941bSHannes Frederic Sowa } 1412b22b941bSHannes Frederic Sowa 1413250fc3dfSFlorian Westphal static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb, 1414250fc3dfSFlorian Westphal struct net_device *dev, 1415250fc3dfSFlorian Westphal u32 ext_filter_mask) 1416250fc3dfSFlorian Westphal { 1417250fc3dfSFlorian Westphal struct nlattr *vfinfo; 1418250fc3dfSFlorian Westphal int i, num_vfs; 1419250fc3dfSFlorian Westphal 1420250fc3dfSFlorian Westphal if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0)) 1421250fc3dfSFlorian Westphal return 0; 1422250fc3dfSFlorian Westphal 1423250fc3dfSFlorian Westphal num_vfs = dev_num_vf(dev->dev.parent); 1424250fc3dfSFlorian Westphal if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs)) 1425250fc3dfSFlorian Westphal return -EMSGSIZE; 1426250fc3dfSFlorian Westphal 1427250fc3dfSFlorian Westphal if (!dev->netdev_ops->ndo_get_vf_config) 1428250fc3dfSFlorian Westphal return 0; 1429250fc3dfSFlorian Westphal 1430ae0be8deSMichal Kubecek vfinfo = nla_nest_start_noflag(skb, IFLA_VFINFO_LIST); 1431250fc3dfSFlorian Westphal if (!vfinfo) 1432250fc3dfSFlorian Westphal return -EMSGSIZE; 1433250fc3dfSFlorian Westphal 1434250fc3dfSFlorian Westphal for (i = 0; i < num_vfs; i++) { 1435250fc3dfSFlorian Westphal if (rtnl_fill_vfinfo(skb, dev, i, vfinfo)) 1436250fc3dfSFlorian Westphal return -EMSGSIZE; 1437250fc3dfSFlorian Westphal } 1438250fc3dfSFlorian Westphal 1439250fc3dfSFlorian Westphal nla_nest_end(skb, vfinfo); 1440250fc3dfSFlorian Westphal return 0; 1441250fc3dfSFlorian Westphal } 1442250fc3dfSFlorian Westphal 1443b22b941bSHannes Frederic Sowa static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev) 1444b22b941bSHannes Frederic Sowa { 14455f8e4474SKangjie Lu struct rtnl_link_ifmap map; 14465f8e4474SKangjie Lu 14475f8e4474SKangjie Lu memset(&map, 0, sizeof(map)); 14485f8e4474SKangjie Lu map.mem_start = dev->mem_start; 14495f8e4474SKangjie Lu map.mem_end = dev->mem_end; 14505f8e4474SKangjie Lu map.base_addr = dev->base_addr; 14515f8e4474SKangjie Lu map.irq = dev->irq; 14525f8e4474SKangjie Lu map.dma = dev->dma; 14535f8e4474SKangjie Lu map.port = dev->if_port; 14545f8e4474SKangjie Lu 1455270cb4d0SNicolas Dichtel if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD)) 1456b22b941bSHannes Frederic Sowa return -EMSGSIZE; 1457b22b941bSHannes Frederic Sowa 1458b22b941bSHannes Frederic Sowa return 0; 1459b22b941bSHannes Frederic Sowa } 1460b22b941bSHannes Frederic Sowa 1461a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_skb(struct net_device *dev) 1462d67b9cd2SDaniel Borkmann { 146358038695SMartin KaFai Lau const struct bpf_prog *generic_xdp_prog; 1464d67b9cd2SDaniel Borkmann 1465d67b9cd2SDaniel Borkmann ASSERT_RTNL(); 1466d67b9cd2SDaniel Borkmann 146758038695SMartin KaFai Lau generic_xdp_prog = rtnl_dereference(dev->xdp_prog); 1468a25717d2SJakub Kicinski if (!generic_xdp_prog) 1469a25717d2SJakub Kicinski return 0; 1470a25717d2SJakub Kicinski return generic_xdp_prog->aux->id; 147158038695SMartin KaFai Lau } 1472ce158e58SJakub Kicinski 1473a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_drv(struct net_device *dev) 1474a25717d2SJakub Kicinski { 14757f0a8382SAndrii Nakryiko return dev_xdp_prog_id(dev, XDP_MODE_DRV); 1476a25717d2SJakub Kicinski } 1477118b4aa2SJakub Kicinski 1478a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_hw(struct net_device *dev) 1479a25717d2SJakub Kicinski { 14807f0a8382SAndrii Nakryiko return dev_xdp_prog_id(dev, XDP_MODE_HW); 1481a25717d2SJakub Kicinski } 1482a25717d2SJakub Kicinski 1483a25717d2SJakub Kicinski static int rtnl_xdp_report_one(struct sk_buff *skb, struct net_device *dev, 1484a25717d2SJakub Kicinski u32 *prog_id, u8 *mode, u8 tgt_mode, u32 attr, 1485a25717d2SJakub Kicinski u32 (*get_prog_id)(struct net_device *dev)) 1486a25717d2SJakub Kicinski { 1487a25717d2SJakub Kicinski u32 curr_id; 1488a25717d2SJakub Kicinski int err; 1489a25717d2SJakub Kicinski 1490a25717d2SJakub Kicinski curr_id = get_prog_id(dev); 1491a25717d2SJakub Kicinski if (!curr_id) 1492a25717d2SJakub Kicinski return 0; 1493a25717d2SJakub Kicinski 1494a25717d2SJakub Kicinski *prog_id = curr_id; 1495a25717d2SJakub Kicinski err = nla_put_u32(skb, attr, curr_id); 1496a25717d2SJakub Kicinski if (err) 1497a25717d2SJakub Kicinski return err; 1498a25717d2SJakub Kicinski 1499a25717d2SJakub Kicinski if (*mode != XDP_ATTACHED_NONE) 1500a25717d2SJakub Kicinski *mode = XDP_ATTACHED_MULTI; 1501a25717d2SJakub Kicinski else 1502a25717d2SJakub Kicinski *mode = tgt_mode; 1503a25717d2SJakub Kicinski 1504a25717d2SJakub Kicinski return 0; 1505d67b9cd2SDaniel Borkmann } 1506d67b9cd2SDaniel Borkmann 1507d1fdd913SBrenden Blanco static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev) 1508d1fdd913SBrenden Blanco { 1509d1fdd913SBrenden Blanco struct nlattr *xdp; 151058038695SMartin KaFai Lau u32 prog_id; 1511d1fdd913SBrenden Blanco int err; 15124f91da26SJakub Kicinski u8 mode; 1513d1fdd913SBrenden Blanco 1514ae0be8deSMichal Kubecek xdp = nla_nest_start_noflag(skb, IFLA_XDP); 1515d1fdd913SBrenden Blanco if (!xdp) 1516d1fdd913SBrenden Blanco return -EMSGSIZE; 1517b5cdae32SDavid S. Miller 1518a25717d2SJakub Kicinski prog_id = 0; 1519a25717d2SJakub Kicinski mode = XDP_ATTACHED_NONE; 1520202aabe8SJakub Kicinski err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_SKB, 1521202aabe8SJakub Kicinski IFLA_XDP_SKB_PROG_ID, rtnl_xdp_prog_skb); 1522202aabe8SJakub Kicinski if (err) 1523a25717d2SJakub Kicinski goto err_cancel; 1524202aabe8SJakub Kicinski err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_DRV, 1525202aabe8SJakub Kicinski IFLA_XDP_DRV_PROG_ID, rtnl_xdp_prog_drv); 1526202aabe8SJakub Kicinski if (err) 1527a25717d2SJakub Kicinski goto err_cancel; 1528202aabe8SJakub Kicinski err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_HW, 1529202aabe8SJakub Kicinski IFLA_XDP_HW_PROG_ID, rtnl_xdp_prog_hw); 1530202aabe8SJakub Kicinski if (err) 1531a25717d2SJakub Kicinski goto err_cancel; 1532a25717d2SJakub Kicinski 15334f91da26SJakub Kicinski err = nla_put_u8(skb, IFLA_XDP_ATTACHED, mode); 1534d1fdd913SBrenden Blanco if (err) 1535d1fdd913SBrenden Blanco goto err_cancel; 1536d1fdd913SBrenden Blanco 1537a25717d2SJakub Kicinski if (prog_id && mode != XDP_ATTACHED_MULTI) { 153858038695SMartin KaFai Lau err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id); 153958038695SMartin KaFai Lau if (err) 154058038695SMartin KaFai Lau goto err_cancel; 154158038695SMartin KaFai Lau } 154258038695SMartin KaFai Lau 1543d1fdd913SBrenden Blanco nla_nest_end(skb, xdp); 1544d1fdd913SBrenden Blanco return 0; 1545d1fdd913SBrenden Blanco 1546d1fdd913SBrenden Blanco err_cancel: 1547d1fdd913SBrenden Blanco nla_nest_cancel(skb, xdp); 1548d1fdd913SBrenden Blanco return err; 1549d1fdd913SBrenden Blanco } 1550d1fdd913SBrenden Blanco 15513d3ea5afSVlad Yasevich static u32 rtnl_get_event(unsigned long event) 15523d3ea5afSVlad Yasevich { 15533d3ea5afSVlad Yasevich u32 rtnl_event_type = IFLA_EVENT_NONE; 15543d3ea5afSVlad Yasevich 15553d3ea5afSVlad Yasevich switch (event) { 15563d3ea5afSVlad Yasevich case NETDEV_REBOOT: 15573d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_REBOOT; 15583d3ea5afSVlad Yasevich break; 15593d3ea5afSVlad Yasevich case NETDEV_FEAT_CHANGE: 15603d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_FEATURES; 15613d3ea5afSVlad Yasevich break; 15623d3ea5afSVlad Yasevich case NETDEV_BONDING_FAILOVER: 15633d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER; 15643d3ea5afSVlad Yasevich break; 15653d3ea5afSVlad Yasevich case NETDEV_NOTIFY_PEERS: 15663d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS; 15673d3ea5afSVlad Yasevich break; 15683d3ea5afSVlad Yasevich case NETDEV_RESEND_IGMP: 15693d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_IGMP_RESEND; 15703d3ea5afSVlad Yasevich break; 15713d3ea5afSVlad Yasevich case NETDEV_CHANGEINFODATA: 15723d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS; 15733d3ea5afSVlad Yasevich break; 15743d3ea5afSVlad Yasevich default: 15753d3ea5afSVlad Yasevich break; 15763d3ea5afSVlad Yasevich } 15773d3ea5afSVlad Yasevich 15783d3ea5afSVlad Yasevich return rtnl_event_type; 15793d3ea5afSVlad Yasevich } 15803d3ea5afSVlad Yasevich 158179110a04SFlorian Westphal static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev) 158279110a04SFlorian Westphal { 158379110a04SFlorian Westphal const struct net_device *upper_dev; 158479110a04SFlorian Westphal int ret = 0; 158579110a04SFlorian Westphal 158679110a04SFlorian Westphal rcu_read_lock(); 158779110a04SFlorian Westphal 158879110a04SFlorian Westphal upper_dev = netdev_master_upper_dev_get_rcu(dev); 158979110a04SFlorian Westphal if (upper_dev) 159079110a04SFlorian Westphal ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex); 159179110a04SFlorian Westphal 159279110a04SFlorian Westphal rcu_read_unlock(); 159379110a04SFlorian Westphal return ret; 159479110a04SFlorian Westphal } 159579110a04SFlorian Westphal 1596feadc4b6SSabrina Dubroca static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev, 1597feadc4b6SSabrina Dubroca bool force) 159879110a04SFlorian Westphal { 159979110a04SFlorian Westphal int ifindex = dev_get_iflink(dev); 160079110a04SFlorian Westphal 1601feadc4b6SSabrina Dubroca if (force || dev->ifindex != ifindex) 160279110a04SFlorian Westphal return nla_put_u32(skb, IFLA_LINK, ifindex); 1603feadc4b6SSabrina Dubroca 1604feadc4b6SSabrina Dubroca return 0; 160579110a04SFlorian Westphal } 160679110a04SFlorian Westphal 16076c557001SFlorian Westphal static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb, 16086c557001SFlorian Westphal struct net_device *dev) 16096c557001SFlorian Westphal { 16106c557001SFlorian Westphal char buf[IFALIASZ]; 16116c557001SFlorian Westphal int ret; 16126c557001SFlorian Westphal 16136c557001SFlorian Westphal ret = dev_get_alias(dev, buf, sizeof(buf)); 16146c557001SFlorian Westphal return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0; 16156c557001SFlorian Westphal } 16166c557001SFlorian Westphal 1617b1e66b9aSFlorian Westphal static int rtnl_fill_link_netnsid(struct sk_buff *skb, 161879e1ad14SJiri Benc const struct net_device *dev, 1619d4e4fdf9SGuillaume Nault struct net *src_net, gfp_t gfp) 1620b1e66b9aSFlorian Westphal { 1621feadc4b6SSabrina Dubroca bool put_iflink = false; 1622feadc4b6SSabrina Dubroca 1623b1e66b9aSFlorian Westphal if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) { 1624b1e66b9aSFlorian Westphal struct net *link_net = dev->rtnl_link_ops->get_link_net(dev); 1625b1e66b9aSFlorian Westphal 1626b1e66b9aSFlorian Westphal if (!net_eq(dev_net(dev), link_net)) { 1627d4e4fdf9SGuillaume Nault int id = peernet2id_alloc(src_net, link_net, gfp); 1628b1e66b9aSFlorian Westphal 1629b1e66b9aSFlorian Westphal if (nla_put_s32(skb, IFLA_LINK_NETNSID, id)) 1630b1e66b9aSFlorian Westphal return -EMSGSIZE; 1631feadc4b6SSabrina Dubroca 1632feadc4b6SSabrina Dubroca put_iflink = true; 1633b1e66b9aSFlorian Westphal } 1634b1e66b9aSFlorian Westphal } 1635b1e66b9aSFlorian Westphal 1636feadc4b6SSabrina Dubroca return nla_put_iflink(skb, dev, put_iflink); 1637b1e66b9aSFlorian Westphal } 1638b1e66b9aSFlorian Westphal 1639070cbf5bSFlorian Westphal static int rtnl_fill_link_af(struct sk_buff *skb, 1640070cbf5bSFlorian Westphal const struct net_device *dev, 1641070cbf5bSFlorian Westphal u32 ext_filter_mask) 1642070cbf5bSFlorian Westphal { 1643070cbf5bSFlorian Westphal const struct rtnl_af_ops *af_ops; 1644070cbf5bSFlorian Westphal struct nlattr *af_spec; 1645070cbf5bSFlorian Westphal 1646ae0be8deSMichal Kubecek af_spec = nla_nest_start_noflag(skb, IFLA_AF_SPEC); 1647070cbf5bSFlorian Westphal if (!af_spec) 1648070cbf5bSFlorian Westphal return -EMSGSIZE; 1649070cbf5bSFlorian Westphal 16505fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 1651070cbf5bSFlorian Westphal struct nlattr *af; 1652070cbf5bSFlorian Westphal int err; 1653070cbf5bSFlorian Westphal 1654070cbf5bSFlorian Westphal if (!af_ops->fill_link_af) 1655070cbf5bSFlorian Westphal continue; 1656070cbf5bSFlorian Westphal 1657ae0be8deSMichal Kubecek af = nla_nest_start_noflag(skb, af_ops->family); 1658070cbf5bSFlorian Westphal if (!af) 1659070cbf5bSFlorian Westphal return -EMSGSIZE; 1660070cbf5bSFlorian Westphal 1661070cbf5bSFlorian Westphal err = af_ops->fill_link_af(skb, dev, ext_filter_mask); 1662070cbf5bSFlorian Westphal /* 1663070cbf5bSFlorian Westphal * Caller may return ENODATA to indicate that there 1664070cbf5bSFlorian Westphal * was no data to be dumped. This is not an error, it 1665070cbf5bSFlorian Westphal * means we should trim the attribute header and 1666070cbf5bSFlorian Westphal * continue. 1667070cbf5bSFlorian Westphal */ 1668070cbf5bSFlorian Westphal if (err == -ENODATA) 1669070cbf5bSFlorian Westphal nla_nest_cancel(skb, af); 1670070cbf5bSFlorian Westphal else if (err < 0) 1671070cbf5bSFlorian Westphal return -EMSGSIZE; 1672070cbf5bSFlorian Westphal 1673070cbf5bSFlorian Westphal nla_nest_end(skb, af); 1674070cbf5bSFlorian Westphal } 1675070cbf5bSFlorian Westphal 1676070cbf5bSFlorian Westphal nla_nest_end(skb, af_spec); 1677070cbf5bSFlorian Westphal return 0; 1678070cbf5bSFlorian Westphal } 1679070cbf5bSFlorian Westphal 168088f4fb0cSJiri Pirko static int rtnl_fill_alt_ifnames(struct sk_buff *skb, 168188f4fb0cSJiri Pirko const struct net_device *dev) 168288f4fb0cSJiri Pirko { 168388f4fb0cSJiri Pirko struct netdev_name_node *name_node; 168488f4fb0cSJiri Pirko int count = 0; 168588f4fb0cSJiri Pirko 168688f4fb0cSJiri Pirko list_for_each_entry(name_node, &dev->name_node->list, list) { 168788f4fb0cSJiri Pirko if (nla_put_string(skb, IFLA_ALT_IFNAME, name_node->name)) 168888f4fb0cSJiri Pirko return -EMSGSIZE; 168988f4fb0cSJiri Pirko count++; 169088f4fb0cSJiri Pirko } 169188f4fb0cSJiri Pirko return count; 169288f4fb0cSJiri Pirko } 169388f4fb0cSJiri Pirko 169488f4fb0cSJiri Pirko static int rtnl_fill_prop_list(struct sk_buff *skb, 169588f4fb0cSJiri Pirko const struct net_device *dev) 169688f4fb0cSJiri Pirko { 169788f4fb0cSJiri Pirko struct nlattr *prop_list; 169888f4fb0cSJiri Pirko int ret; 169988f4fb0cSJiri Pirko 170088f4fb0cSJiri Pirko prop_list = nla_nest_start(skb, IFLA_PROP_LIST); 170188f4fb0cSJiri Pirko if (!prop_list) 170288f4fb0cSJiri Pirko return -EMSGSIZE; 170388f4fb0cSJiri Pirko 170488f4fb0cSJiri Pirko ret = rtnl_fill_alt_ifnames(skb, dev); 170588f4fb0cSJiri Pirko if (ret <= 0) 170688f4fb0cSJiri Pirko goto nest_cancel; 170788f4fb0cSJiri Pirko 170888f4fb0cSJiri Pirko nla_nest_end(skb, prop_list); 170988f4fb0cSJiri Pirko return 0; 171088f4fb0cSJiri Pirko 171188f4fb0cSJiri Pirko nest_cancel: 171288f4fb0cSJiri Pirko nla_nest_cancel(skb, prop_list); 171388f4fb0cSJiri Pirko return ret; 171488f4fb0cSJiri Pirko } 171588f4fb0cSJiri Pirko 1716829eb208SRoopa Prabhu static int rtnl_fill_proto_down(struct sk_buff *skb, 1717829eb208SRoopa Prabhu const struct net_device *dev) 1718829eb208SRoopa Prabhu { 1719829eb208SRoopa Prabhu struct nlattr *pr; 1720829eb208SRoopa Prabhu u32 preason; 1721829eb208SRoopa Prabhu 1722829eb208SRoopa Prabhu if (nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down)) 1723829eb208SRoopa Prabhu goto nla_put_failure; 1724829eb208SRoopa Prabhu 1725829eb208SRoopa Prabhu preason = dev->proto_down_reason; 1726829eb208SRoopa Prabhu if (!preason) 1727829eb208SRoopa Prabhu return 0; 1728829eb208SRoopa Prabhu 1729829eb208SRoopa Prabhu pr = nla_nest_start(skb, IFLA_PROTO_DOWN_REASON); 1730829eb208SRoopa Prabhu if (!pr) 1731829eb208SRoopa Prabhu return -EMSGSIZE; 1732829eb208SRoopa Prabhu 1733829eb208SRoopa Prabhu if (nla_put_u32(skb, IFLA_PROTO_DOWN_REASON_VALUE, preason)) { 1734829eb208SRoopa Prabhu nla_nest_cancel(skb, pr); 1735829eb208SRoopa Prabhu goto nla_put_failure; 1736829eb208SRoopa Prabhu } 1737829eb208SRoopa Prabhu 1738829eb208SRoopa Prabhu nla_nest_end(skb, pr); 1739829eb208SRoopa Prabhu return 0; 1740829eb208SRoopa Prabhu 1741829eb208SRoopa Prabhu nla_put_failure: 1742829eb208SRoopa Prabhu return -EMSGSIZE; 1743829eb208SRoopa Prabhu } 1744829eb208SRoopa Prabhu 1745dca56c30SJiri Pirko static int rtnl_fill_devlink_port(struct sk_buff *skb, 1746dca56c30SJiri Pirko const struct net_device *dev) 1747dca56c30SJiri Pirko { 1748dca56c30SJiri Pirko struct nlattr *devlink_port_nest; 1749dca56c30SJiri Pirko int ret; 1750dca56c30SJiri Pirko 1751dca56c30SJiri Pirko devlink_port_nest = nla_nest_start(skb, IFLA_DEVLINK_PORT); 1752dca56c30SJiri Pirko if (!devlink_port_nest) 1753dca56c30SJiri Pirko return -EMSGSIZE; 1754dca56c30SJiri Pirko 1755dca56c30SJiri Pirko if (dev->devlink_port) { 1756dca56c30SJiri Pirko ret = devlink_nl_port_handle_fill(skb, dev->devlink_port); 1757dca56c30SJiri Pirko if (ret < 0) 1758dca56c30SJiri Pirko goto nest_cancel; 1759dca56c30SJiri Pirko } 1760dca56c30SJiri Pirko 1761dca56c30SJiri Pirko nla_nest_end(skb, devlink_port_nest); 1762dca56c30SJiri Pirko return 0; 1763dca56c30SJiri Pirko 1764dca56c30SJiri Pirko nest_cancel: 1765dca56c30SJiri Pirko nla_nest_cancel(skb, devlink_port_nest); 1766dca56c30SJiri Pirko return ret; 1767dca56c30SJiri Pirko } 1768dca56c30SJiri Pirko 176979e1ad14SJiri Benc static int rtnl_fill_ifinfo(struct sk_buff *skb, 177079e1ad14SJiri Benc struct net_device *dev, struct net *src_net, 1771575c3e2aSPatrick McHardy int type, u32 pid, u32 seq, u32 change, 17723d3ea5afSVlad Yasevich unsigned int flags, u32 ext_filter_mask, 177338e01b30SNicolas Dichtel u32 event, int *new_nsid, int new_ifindex, 1774d4e4fdf9SGuillaume Nault int tgt_netnsid, gfp_t gfp) 1775b60c5115SThomas Graf { 1776b60c5115SThomas Graf struct ifinfomsg *ifm; 17771da177e4SLinus Torvalds struct nlmsghdr *nlh; 17785891cd5eSEric Dumazet struct Qdisc *qdisc; 17791da177e4SLinus Torvalds 17802907c35fSEric Dumazet ASSERT_RTNL(); 1781b60c5115SThomas Graf nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags); 1782b60c5115SThomas Graf if (nlh == NULL) 178326932566SPatrick McHardy return -EMSGSIZE; 17841da177e4SLinus Torvalds 1785b60c5115SThomas Graf ifm = nlmsg_data(nlh); 1786b60c5115SThomas Graf ifm->ifi_family = AF_UNSPEC; 1787b60c5115SThomas Graf ifm->__ifi_pad = 0; 1788b60c5115SThomas Graf ifm->ifi_type = dev->type; 1789b60c5115SThomas Graf ifm->ifi_index = dev->ifindex; 1790b60c5115SThomas Graf ifm->ifi_flags = dev_get_flags(dev); 1791b60c5115SThomas Graf ifm->ifi_change = change; 17921da177e4SLinus Torvalds 17937e4a8d5aSChristian Brauner if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_TARGET_NETNSID, tgt_netnsid)) 179479e1ad14SJiri Benc goto nla_put_failure; 179579e1ad14SJiri Benc 17965891cd5eSEric Dumazet qdisc = rtnl_dereference(dev->qdisc); 1797a6574349SDavid S. Miller if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 1798a6574349SDavid S. Miller nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) || 1799a6574349SDavid S. Miller nla_put_u8(skb, IFLA_OPERSTATE, 1800a6574349SDavid S. Miller netif_running(dev) ? dev->operstate : IF_OPER_DOWN) || 1801a6574349SDavid S. Miller nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) || 1802a6574349SDavid S. Miller nla_put_u32(skb, IFLA_MTU, dev->mtu) || 18033e7a50ceSStephen Hemminger nla_put_u32(skb, IFLA_MIN_MTU, dev->min_mtu) || 18043e7a50ceSStephen Hemminger nla_put_u32(skb, IFLA_MAX_MTU, dev->max_mtu) || 1805a6574349SDavid S. Miller nla_put_u32(skb, IFLA_GROUP, dev->group) || 1806edbc0bb3SBen Greear nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) || 18077e6e1b57SNicolas Dichtel nla_put_u32(skb, IFLA_ALLMULTI, dev->allmulti) || 180876ff5cc9SJiri Pirko nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) || 1809c70ce028SEric Dumazet nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) || 1810c70ce028SEric Dumazet nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) || 1811eac1b93cSCoco Li nla_put_u32(skb, IFLA_GRO_MAX_SIZE, dev->gro_max_size) || 1812*9eefedd5SXin Long nla_put_u32(skb, IFLA_GSO_IPV4_MAX_SIZE, dev->gso_ipv4_max_size) || 1813*9eefedd5SXin Long nla_put_u32(skb, IFLA_GRO_IPV4_MAX_SIZE, dev->gro_ipv4_max_size) || 181489527be8SEric Dumazet nla_put_u32(skb, IFLA_TSO_MAX_SIZE, dev->tso_max_size) || 181589527be8SEric Dumazet nla_put_u32(skb, IFLA_TSO_MAX_SEGS, dev->tso_max_segs) || 18161d69c2b3SMark A. Greer #ifdef CONFIG_RPS 181776ff5cc9SJiri Pirko nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) || 18181d69c2b3SMark A. Greer #endif 181979110a04SFlorian Westphal put_master_ifindex(skb, dev) || 18209a57247fSJiri Pirko nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) || 18215891cd5eSEric Dumazet (qdisc && 18225891cd5eSEric Dumazet nla_put_string(skb, IFLA_QDISC, qdisc->ops->id)) || 18236c557001SFlorian Westphal nla_put_ifalias(skb, dev) || 18242d3b479dSdavid decotigny nla_put_u32(skb, IFLA_CARRIER_CHANGES, 1825b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_up_count) + 1826b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_down_count)) || 1827b2d3bcfaSDavid Decotigny nla_put_u32(skb, IFLA_CARRIER_UP_COUNT, 1828b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_up_count)) || 1829b2d3bcfaSDavid Decotigny nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT, 1830b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_down_count))) 1831a6574349SDavid S. Miller goto nla_put_failure; 18320b815a1aSStephen Hemminger 1833829eb208SRoopa Prabhu if (rtnl_fill_proto_down(skb, dev)) 1834829eb208SRoopa Prabhu goto nla_put_failure; 1835829eb208SRoopa Prabhu 18363d3ea5afSVlad Yasevich if (event != IFLA_EVENT_NONE) { 18373d3ea5afSVlad Yasevich if (nla_put_u32(skb, IFLA_EVENT, event)) 18383d3ea5afSVlad Yasevich goto nla_put_failure; 18393d3ea5afSVlad Yasevich } 18403d3ea5afSVlad Yasevich 1841b22b941bSHannes Frederic Sowa if (rtnl_fill_link_ifmap(skb, dev)) 1842a6574349SDavid S. Miller goto nla_put_failure; 18431da177e4SLinus Torvalds 18441da177e4SLinus Torvalds if (dev->addr_len) { 1845a6574349SDavid S. Miller if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) || 1846a6574349SDavid S. Miller nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast)) 1847a6574349SDavid S. Miller goto nla_put_failure; 18481da177e4SLinus Torvalds } 18491da177e4SLinus Torvalds 185066cae9edSJiri Pirko if (rtnl_phys_port_id_fill(skb, dev)) 185166cae9edSJiri Pirko goto nla_put_failure; 185266cae9edSJiri Pirko 1853db24a904SDavid Ahern if (rtnl_phys_port_name_fill(skb, dev)) 1854db24a904SDavid Ahern goto nla_put_failure; 1855db24a904SDavid Ahern 185682f28412SJiri Pirko if (rtnl_phys_switch_id_fill(skb, dev)) 185782f28412SJiri Pirko goto nla_put_failure; 185882f28412SJiri Pirko 1859b22b941bSHannes Frederic Sowa if (rtnl_fill_stats(skb, dev)) 1860b60c5115SThomas Graf goto nla_put_failure; 1861b60c5115SThomas Graf 1862250fc3dfSFlorian Westphal if (rtnl_fill_vf(skb, dev, ext_filter_mask)) 1863a6574349SDavid S. Miller goto nla_put_failure; 186457b61080SScott Feldman 1865c53864fdSDavid Gibson if (rtnl_port_fill(skb, dev, ext_filter_mask)) 186657b61080SScott Feldman goto nla_put_failure; 186757b61080SScott Feldman 1868d1fdd913SBrenden Blanco if (rtnl_xdp_fill(skb, dev)) 1869d1fdd913SBrenden Blanco goto nla_put_failure; 1870d1fdd913SBrenden Blanco 1871ba7d49b1SJiri Pirko if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) { 187238f7b870SPatrick McHardy if (rtnl_link_fill(skb, dev) < 0) 187338f7b870SPatrick McHardy goto nla_put_failure; 187438f7b870SPatrick McHardy } 187538f7b870SPatrick McHardy 1876d4e4fdf9SGuillaume Nault if (rtnl_fill_link_netnsid(skb, dev, src_net, gfp)) 1877d37512a2SNicolas Dichtel goto nla_put_failure; 1878d37512a2SNicolas Dichtel 18796621dd29SNicolas Dichtel if (new_nsid && 18806621dd29SNicolas Dichtel nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0) 18816621dd29SNicolas Dichtel goto nla_put_failure; 188238e01b30SNicolas Dichtel if (new_ifindex && 188338e01b30SNicolas Dichtel nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0) 188438e01b30SNicolas Dichtel goto nla_put_failure; 188538e01b30SNicolas Dichtel 1886f74877a5SMichal Kubecek if (memchr_inv(dev->perm_addr, '\0', dev->addr_len) && 1887f74877a5SMichal Kubecek nla_put(skb, IFLA_PERM_ADDRESS, dev->addr_len, dev->perm_addr)) 1888f74877a5SMichal Kubecek goto nla_put_failure; 18896621dd29SNicolas Dichtel 18905fa85a09SFlorian Westphal rcu_read_lock(); 1891070cbf5bSFlorian Westphal if (rtnl_fill_link_af(skb, dev, ext_filter_mask)) 18925fa85a09SFlorian Westphal goto nla_put_failure_rcu; 18935fa85a09SFlorian Westphal rcu_read_unlock(); 1894f8ff182cSThomas Graf 189588f4fb0cSJiri Pirko if (rtnl_fill_prop_list(skb, dev)) 189688f4fb0cSJiri Pirko goto nla_put_failure; 189788f4fb0cSJiri Pirko 189800e77ed8SJohannes Berg if (dev->dev.parent && 189900e77ed8SJohannes Berg nla_put_string(skb, IFLA_PARENT_DEV_NAME, 190000e77ed8SJohannes Berg dev_name(dev->dev.parent))) 190100e77ed8SJohannes Berg goto nla_put_failure; 190200e77ed8SJohannes Berg 190300e77ed8SJohannes Berg if (dev->dev.parent && dev->dev.parent->bus && 190400e77ed8SJohannes Berg nla_put_string(skb, IFLA_PARENT_DEV_BUS_NAME, 190500e77ed8SJohannes Berg dev->dev.parent->bus->name)) 190600e77ed8SJohannes Berg goto nla_put_failure; 190700e77ed8SJohannes Berg 1908dca56c30SJiri Pirko if (rtnl_fill_devlink_port(skb, dev)) 1909dca56c30SJiri Pirko goto nla_put_failure; 1910dca56c30SJiri Pirko 1911053c095aSJohannes Berg nlmsg_end(skb, nlh); 1912053c095aSJohannes Berg return 0; 1913b60c5115SThomas Graf 19145fa85a09SFlorian Westphal nla_put_failure_rcu: 19155fa85a09SFlorian Westphal rcu_read_unlock(); 1916b60c5115SThomas Graf nla_put_failure: 191726932566SPatrick McHardy nlmsg_cancel(skb, nlh); 191826932566SPatrick McHardy return -EMSGSIZE; 19191da177e4SLinus Torvalds } 19201da177e4SLinus Torvalds 1921f7b12606SJiri Pirko static const struct nla_policy ifla_policy[IFLA_MAX+1] = { 1922f7b12606SJiri Pirko [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 }, 1923f7b12606SJiri Pirko [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 1924f7b12606SJiri Pirko [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 1925f7b12606SJiri Pirko [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) }, 1926f7b12606SJiri Pirko [IFLA_MTU] = { .type = NLA_U32 }, 1927f7b12606SJiri Pirko [IFLA_LINK] = { .type = NLA_U32 }, 1928f7b12606SJiri Pirko [IFLA_MASTER] = { .type = NLA_U32 }, 1929f7b12606SJiri Pirko [IFLA_CARRIER] = { .type = NLA_U8 }, 1930f7b12606SJiri Pirko [IFLA_TXQLEN] = { .type = NLA_U32 }, 1931f7b12606SJiri Pirko [IFLA_WEIGHT] = { .type = NLA_U32 }, 1932f7b12606SJiri Pirko [IFLA_OPERSTATE] = { .type = NLA_U8 }, 1933f7b12606SJiri Pirko [IFLA_LINKMODE] = { .type = NLA_U8 }, 1934f7b12606SJiri Pirko [IFLA_LINKINFO] = { .type = NLA_NESTED }, 1935f7b12606SJiri Pirko [IFLA_NET_NS_PID] = { .type = NLA_U32 }, 1936f7b12606SJiri Pirko [IFLA_NET_NS_FD] = { .type = NLA_U32 }, 19372459b4c6SNicolas Dichtel /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to 19382459b4c6SNicolas Dichtel * allow 0-length string (needed to remove an alias). 19392459b4c6SNicolas Dichtel */ 19402459b4c6SNicolas Dichtel [IFLA_IFALIAS] = { .type = NLA_BINARY, .len = IFALIASZ - 1 }, 1941f7b12606SJiri Pirko [IFLA_VFINFO_LIST] = {. type = NLA_NESTED }, 1942f7b12606SJiri Pirko [IFLA_VF_PORTS] = { .type = NLA_NESTED }, 1943f7b12606SJiri Pirko [IFLA_PORT_SELF] = { .type = NLA_NESTED }, 1944f7b12606SJiri Pirko [IFLA_AF_SPEC] = { .type = NLA_NESTED }, 1945f7b12606SJiri Pirko [IFLA_EXT_MASK] = { .type = NLA_U32 }, 1946f7b12606SJiri Pirko [IFLA_PROMISCUITY] = { .type = NLA_U32 }, 1947f7b12606SJiri Pirko [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 }, 1948f7b12606SJiri Pirko [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 }, 194946e6b992SStephen Hemminger [IFLA_GSO_MAX_SEGS] = { .type = NLA_U32 }, 195046e6b992SStephen Hemminger [IFLA_GSO_MAX_SIZE] = { .type = NLA_U32 }, 195102637fceSJiri Pirko [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN }, 19522d3b479dSdavid decotigny [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */ 195382f28412SJiri Pirko [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN }, 1954317f4810SNicolas Dichtel [IFLA_LINK_NETNSID] = { .type = NLA_S32 }, 195588d6378bSAnuradha Karuppiah [IFLA_PROTO_DOWN] = { .type = NLA_U8 }, 1956d1fdd913SBrenden Blanco [IFLA_XDP] = { .type = NLA_NESTED }, 19573d3ea5afSVlad Yasevich [IFLA_EVENT] = { .type = NLA_U32 }, 1958db833d40SSerhey Popovych [IFLA_GROUP] = { .type = NLA_U32 }, 19597e4a8d5aSChristian Brauner [IFLA_TARGET_NETNSID] = { .type = NLA_S32 }, 1960b2d3bcfaSDavid Decotigny [IFLA_CARRIER_UP_COUNT] = { .type = NLA_U32 }, 1961b2d3bcfaSDavid Decotigny [IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 }, 19623e7a50ceSStephen Hemminger [IFLA_MIN_MTU] = { .type = NLA_U32 }, 19633e7a50ceSStephen Hemminger [IFLA_MAX_MTU] = { .type = NLA_U32 }, 196436fbf1e5SJiri Pirko [IFLA_PROP_LIST] = { .type = NLA_NESTED }, 196536fbf1e5SJiri Pirko [IFLA_ALT_IFNAME] = { .type = NLA_STRING, 196636fbf1e5SJiri Pirko .len = ALTIFNAMSIZ - 1 }, 1967f74877a5SMichal Kubecek [IFLA_PERM_ADDRESS] = { .type = NLA_REJECT }, 1968829eb208SRoopa Prabhu [IFLA_PROTO_DOWN_REASON] = { .type = NLA_NESTED }, 19697e4a5131SAndrei Vagin [IFLA_NEW_IFINDEX] = NLA_POLICY_MIN(NLA_S32, 1), 197088b71053SJohannes Berg [IFLA_PARENT_DEV_NAME] = { .type = NLA_NUL_STRING }, 1971eac1b93cSCoco Li [IFLA_GRO_MAX_SIZE] = { .type = NLA_U32 }, 197289527be8SEric Dumazet [IFLA_TSO_MAX_SIZE] = { .type = NLA_REJECT }, 197389527be8SEric Dumazet [IFLA_TSO_MAX_SEGS] = { .type = NLA_REJECT }, 19747e6e1b57SNicolas Dichtel [IFLA_ALLMULTI] = { .type = NLA_REJECT }, 1975*9eefedd5SXin Long [IFLA_GSO_IPV4_MAX_SIZE] = { .type = NLA_U32 }, 1976*9eefedd5SXin Long [IFLA_GRO_IPV4_MAX_SIZE] = { .type = NLA_U32 }, 1977f7b12606SJiri Pirko }; 1978f7b12606SJiri Pirko 1979f7b12606SJiri Pirko static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = { 1980f7b12606SJiri Pirko [IFLA_INFO_KIND] = { .type = NLA_STRING }, 1981f7b12606SJiri Pirko [IFLA_INFO_DATA] = { .type = NLA_NESTED }, 1982f7b12606SJiri Pirko [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING }, 1983f7b12606SJiri Pirko [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED }, 1984f7b12606SJiri Pirko }; 1985f7b12606SJiri Pirko 1986f7b12606SJiri Pirko static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = { 1987364d5716SDaniel Borkmann [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) }, 198875345f88SDenis Kirjanov [IFLA_VF_BROADCAST] = { .type = NLA_REJECT }, 1989364d5716SDaniel Borkmann [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) }, 199079aab093SMoshe Shemesh [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED }, 1991364d5716SDaniel Borkmann [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) }, 1992364d5716SDaniel Borkmann [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) }, 1993364d5716SDaniel Borkmann [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) }, 1994364d5716SDaniel Borkmann [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) }, 199501a3d796SVlad Zolotarov [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) }, 19963b766cd8SEran Ben Elisha [IFLA_VF_STATS] = { .type = NLA_NESTED }, 1997dd461d6aSHiroshi Shimamoto [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) }, 1998cc8e27ccSEli Cohen [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) }, 1999cc8e27ccSEli Cohen [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) }, 20003b766cd8SEran Ben Elisha }; 20013b766cd8SEran Ben Elisha 2002f7b12606SJiri Pirko static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = { 2003f7b12606SJiri Pirko [IFLA_PORT_VF] = { .type = NLA_U32 }, 2004f7b12606SJiri Pirko [IFLA_PORT_PROFILE] = { .type = NLA_STRING, 2005f7b12606SJiri Pirko .len = PORT_PROFILE_MAX }, 2006f7b12606SJiri Pirko [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY, 2007f7b12606SJiri Pirko .len = PORT_UUID_MAX }, 2008f7b12606SJiri Pirko [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING, 2009f7b12606SJiri Pirko .len = PORT_UUID_MAX }, 2010f7b12606SJiri Pirko [IFLA_PORT_REQUEST] = { .type = NLA_U8, }, 2011f7b12606SJiri Pirko [IFLA_PORT_RESPONSE] = { .type = NLA_U16, }, 2012025331dfSDaniel Borkmann 2013025331dfSDaniel Borkmann /* Unused, but we need to keep it here since user space could 2014025331dfSDaniel Borkmann * fill it. It's also broken with regard to NLA_BINARY use in 2015025331dfSDaniel Borkmann * combination with structs. 2016025331dfSDaniel Borkmann */ 2017025331dfSDaniel Borkmann [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY, 2018025331dfSDaniel Borkmann .len = sizeof(struct ifla_port_vsi) }, 2019f7b12606SJiri Pirko }; 2020f7b12606SJiri Pirko 2021d1fdd913SBrenden Blanco static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = { 202292234c8fSToke Høiland-Jørgensen [IFLA_XDP_UNSPEC] = { .strict_start_type = IFLA_XDP_EXPECTED_FD }, 2023d1fdd913SBrenden Blanco [IFLA_XDP_FD] = { .type = NLA_S32 }, 202492234c8fSToke Høiland-Jørgensen [IFLA_XDP_EXPECTED_FD] = { .type = NLA_S32 }, 2025d1fdd913SBrenden Blanco [IFLA_XDP_ATTACHED] = { .type = NLA_U8 }, 202685de8576SDaniel Borkmann [IFLA_XDP_FLAGS] = { .type = NLA_U32 }, 202758038695SMartin KaFai Lau [IFLA_XDP_PROG_ID] = { .type = NLA_U32 }, 2028d1fdd913SBrenden Blanco }; 2029d1fdd913SBrenden Blanco 2030dc599f76SDavid Ahern static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla) 2031dc599f76SDavid Ahern { 2032dc599f76SDavid Ahern const struct rtnl_link_ops *ops = NULL; 2033dc599f76SDavid Ahern struct nlattr *linfo[IFLA_INFO_MAX + 1]; 2034dc599f76SDavid Ahern 20358cb08174SJohannes Berg if (nla_parse_nested_deprecated(linfo, IFLA_INFO_MAX, nla, ifla_info_policy, NULL) < 0) 2036dc599f76SDavid Ahern return NULL; 2037dc599f76SDavid Ahern 2038dc599f76SDavid Ahern if (linfo[IFLA_INFO_KIND]) { 2039dc599f76SDavid Ahern char kind[MODULE_NAME_LEN]; 2040dc599f76SDavid Ahern 2041872f6903SFrancis Laniel nla_strscpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind)); 2042dc599f76SDavid Ahern ops = rtnl_link_ops_get(kind); 2043dc599f76SDavid Ahern } 2044dc599f76SDavid Ahern 2045dc599f76SDavid Ahern return ops; 2046dc599f76SDavid Ahern } 2047dc599f76SDavid Ahern 2048dc599f76SDavid Ahern static bool link_master_filtered(struct net_device *dev, int master_idx) 2049dc599f76SDavid Ahern { 2050dc599f76SDavid Ahern struct net_device *master; 2051dc599f76SDavid Ahern 2052dc599f76SDavid Ahern if (!master_idx) 2053dc599f76SDavid Ahern return false; 2054dc599f76SDavid Ahern 2055dc599f76SDavid Ahern master = netdev_master_upper_dev_get(dev); 2056d3432bf1SLahav Schlesinger 2057d3432bf1SLahav Schlesinger /* 0 is already used to denote IFLA_MASTER wasn't passed, therefore need 2058d3432bf1SLahav Schlesinger * another invalid value for ifindex to denote "no master". 2059d3432bf1SLahav Schlesinger */ 2060d3432bf1SLahav Schlesinger if (master_idx == -1) 2061d3432bf1SLahav Schlesinger return !!master; 2062d3432bf1SLahav Schlesinger 2063dc599f76SDavid Ahern if (!master || master->ifindex != master_idx) 2064dc599f76SDavid Ahern return true; 2065dc599f76SDavid Ahern 2066dc599f76SDavid Ahern return false; 2067dc599f76SDavid Ahern } 2068dc599f76SDavid Ahern 2069dc599f76SDavid Ahern static bool link_kind_filtered(const struct net_device *dev, 2070dc599f76SDavid Ahern const struct rtnl_link_ops *kind_ops) 2071dc599f76SDavid Ahern { 2072dc599f76SDavid Ahern if (kind_ops && dev->rtnl_link_ops != kind_ops) 2073dc599f76SDavid Ahern return true; 2074dc599f76SDavid Ahern 2075dc599f76SDavid Ahern return false; 2076dc599f76SDavid Ahern } 2077dc599f76SDavid Ahern 2078dc599f76SDavid Ahern static bool link_dump_filtered(struct net_device *dev, 2079dc599f76SDavid Ahern int master_idx, 2080dc599f76SDavid Ahern const struct rtnl_link_ops *kind_ops) 2081dc599f76SDavid Ahern { 2082dc599f76SDavid Ahern if (link_master_filtered(dev, master_idx) || 2083dc599f76SDavid Ahern link_kind_filtered(dev, kind_ops)) 2084dc599f76SDavid Ahern return true; 2085dc599f76SDavid Ahern 2086dc599f76SDavid Ahern return false; 2087dc599f76SDavid Ahern } 2088dc599f76SDavid Ahern 2089c383edc4SChristian Brauner /** 2090c383edc4SChristian Brauner * rtnl_get_net_ns_capable - Get netns if sufficiently privileged. 2091c383edc4SChristian Brauner * @sk: netlink socket 2092c383edc4SChristian Brauner * @netnsid: network namespace identifier 2093c383edc4SChristian Brauner * 2094c383edc4SChristian Brauner * Returns the network namespace identified by netnsid on success or an error 2095c383edc4SChristian Brauner * pointer on failure. 2096c383edc4SChristian Brauner */ 2097c383edc4SChristian Brauner struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid) 209879e1ad14SJiri Benc { 209979e1ad14SJiri Benc struct net *net; 210079e1ad14SJiri Benc 2101f428fe4aSAndrei Vagin net = get_net_ns_by_id(sock_net(sk), netnsid); 210279e1ad14SJiri Benc if (!net) 210379e1ad14SJiri Benc return ERR_PTR(-EINVAL); 210479e1ad14SJiri Benc 210579e1ad14SJiri Benc /* For now, the caller is required to have CAP_NET_ADMIN in 210679e1ad14SJiri Benc * the user namespace owning the target net ns. 210779e1ad14SJiri Benc */ 2108f428fe4aSAndrei Vagin if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) { 210979e1ad14SJiri Benc put_net(net); 211079e1ad14SJiri Benc return ERR_PTR(-EACCES); 211179e1ad14SJiri Benc } 211279e1ad14SJiri Benc return net; 211379e1ad14SJiri Benc } 2114c383edc4SChristian Brauner EXPORT_SYMBOL_GPL(rtnl_get_net_ns_capable); 211579e1ad14SJiri Benc 2116905cf0abSDavid Ahern static int rtnl_valid_dump_ifinfo_req(const struct nlmsghdr *nlh, 2117905cf0abSDavid Ahern bool strict_check, struct nlattr **tb, 2118905cf0abSDavid Ahern struct netlink_ext_ack *extack) 2119905cf0abSDavid Ahern { 2120905cf0abSDavid Ahern int hdrlen; 2121905cf0abSDavid Ahern 2122905cf0abSDavid Ahern if (strict_check) { 2123905cf0abSDavid Ahern struct ifinfomsg *ifm; 2124905cf0abSDavid Ahern 2125905cf0abSDavid Ahern if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { 2126905cf0abSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid header for link dump"); 2127905cf0abSDavid Ahern return -EINVAL; 2128905cf0abSDavid Ahern } 2129905cf0abSDavid Ahern 2130905cf0abSDavid Ahern ifm = nlmsg_data(nlh); 2131905cf0abSDavid Ahern if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags || 2132905cf0abSDavid Ahern ifm->ifi_change) { 2133905cf0abSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid values in header for link dump request"); 2134905cf0abSDavid Ahern return -EINVAL; 2135905cf0abSDavid Ahern } 2136905cf0abSDavid Ahern if (ifm->ifi_index) { 2137905cf0abSDavid Ahern NL_SET_ERR_MSG(extack, "Filter by device index not supported for link dumps"); 2138905cf0abSDavid Ahern return -EINVAL; 2139905cf0abSDavid Ahern } 2140905cf0abSDavid Ahern 21418cb08174SJohannes Berg return nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, 21428cb08174SJohannes Berg IFLA_MAX, ifla_policy, 21438cb08174SJohannes Berg extack); 2144905cf0abSDavid Ahern } 2145905cf0abSDavid Ahern 2146905cf0abSDavid Ahern /* A hack to preserve kernel<->userspace interface. 2147905cf0abSDavid Ahern * The correct header is ifinfomsg. It is consistent with rtnl_getlink. 2148905cf0abSDavid Ahern * However, before Linux v3.9 the code here assumed rtgenmsg and that's 2149905cf0abSDavid Ahern * what iproute2 < v3.9.0 used. 2150905cf0abSDavid Ahern * We can detect the old iproute2. Even including the IFLA_EXT_MASK 2151905cf0abSDavid Ahern * attribute, its netlink message is shorter than struct ifinfomsg. 2152905cf0abSDavid Ahern */ 2153905cf0abSDavid Ahern hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ? 2154905cf0abSDavid Ahern sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); 2155905cf0abSDavid Ahern 21568cb08174SJohannes Berg return nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, 21578cb08174SJohannes Berg extack); 2158905cf0abSDavid Ahern } 2159905cf0abSDavid Ahern 2160b60c5115SThomas Graf static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 21611da177e4SLinus Torvalds { 2162905cf0abSDavid Ahern struct netlink_ext_ack *extack = cb->extack; 2163905cf0abSDavid Ahern const struct nlmsghdr *nlh = cb->nlh; 21643b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 216579e1ad14SJiri Benc struct net *tgt_net = net; 21667c28bd0bSEric Dumazet int h, s_h; 21677c28bd0bSEric Dumazet int idx = 0, s_idx; 21681da177e4SLinus Torvalds struct net_device *dev; 21697c28bd0bSEric Dumazet struct hlist_head *head; 2170115c9b81SGreg Rose struct nlattr *tb[IFLA_MAX+1]; 2171115c9b81SGreg Rose u32 ext_filter_mask = 0; 2172dc599f76SDavid Ahern const struct rtnl_link_ops *kind_ops = NULL; 2173dc599f76SDavid Ahern unsigned int flags = NLM_F_MULTI; 2174dc599f76SDavid Ahern int master_idx = 0; 217579e1ad14SJiri Benc int netnsid = -1; 2176905cf0abSDavid Ahern int err, i; 21771da177e4SLinus Torvalds 21787c28bd0bSEric Dumazet s_h = cb->args[0]; 21797c28bd0bSEric Dumazet s_idx = cb->args[1]; 21807c28bd0bSEric Dumazet 2181905cf0abSDavid Ahern err = rtnl_valid_dump_ifinfo_req(nlh, cb->strict_check, tb, extack); 2182905cf0abSDavid Ahern if (err < 0) { 2183905cf0abSDavid Ahern if (cb->strict_check) 2184905cf0abSDavid Ahern return err; 2185e5eca6d4SMichal Schmidt 2186905cf0abSDavid Ahern goto walk_entries; 218779e1ad14SJiri Benc } 218879e1ad14SJiri Benc 2189905cf0abSDavid Ahern for (i = 0; i <= IFLA_MAX; ++i) { 2190905cf0abSDavid Ahern if (!tb[i]) 2191905cf0abSDavid Ahern continue; 2192dc599f76SDavid Ahern 2193905cf0abSDavid Ahern /* new attributes should only be added with strict checking */ 2194905cf0abSDavid Ahern switch (i) { 2195905cf0abSDavid Ahern case IFLA_TARGET_NETNSID: 2196905cf0abSDavid Ahern netnsid = nla_get_s32(tb[i]); 2197905cf0abSDavid Ahern tgt_net = rtnl_get_net_ns_capable(skb->sk, netnsid); 2198905cf0abSDavid Ahern if (IS_ERR(tgt_net)) { 2199905cf0abSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid target network namespace id"); 2200905cf0abSDavid Ahern return PTR_ERR(tgt_net); 2201905cf0abSDavid Ahern } 2202905cf0abSDavid Ahern break; 2203905cf0abSDavid Ahern case IFLA_EXT_MASK: 2204905cf0abSDavid Ahern ext_filter_mask = nla_get_u32(tb[i]); 2205905cf0abSDavid Ahern break; 2206905cf0abSDavid Ahern case IFLA_MASTER: 2207905cf0abSDavid Ahern master_idx = nla_get_u32(tb[i]); 2208905cf0abSDavid Ahern break; 2209905cf0abSDavid Ahern case IFLA_LINKINFO: 2210905cf0abSDavid Ahern kind_ops = linkinfo_to_kind_ops(tb[i]); 2211905cf0abSDavid Ahern break; 2212905cf0abSDavid Ahern default: 2213905cf0abSDavid Ahern if (cb->strict_check) { 2214905cf0abSDavid Ahern NL_SET_ERR_MSG(extack, "Unsupported attribute in link dump request"); 2215905cf0abSDavid Ahern return -EINVAL; 2216905cf0abSDavid Ahern } 2217905cf0abSDavid Ahern } 2218905cf0abSDavid Ahern } 2219dc599f76SDavid Ahern 2220dc599f76SDavid Ahern if (master_idx || kind_ops) 2221dc599f76SDavid Ahern flags |= NLM_F_DUMP_FILTERED; 2222115c9b81SGreg Rose 2223905cf0abSDavid Ahern walk_entries: 22247c28bd0bSEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 22257562f876SPavel Emelianov idx = 0; 222679e1ad14SJiri Benc head = &tgt_net->dev_index_head[h]; 2227cac5e65eSEric Dumazet hlist_for_each_entry(dev, head, index_hlist) { 2228dc599f76SDavid Ahern if (link_dump_filtered(dev, master_idx, kind_ops)) 22293f0ae05dSZhang Shengju goto cont; 22301da177e4SLinus Torvalds if (idx < s_idx) 22317562f876SPavel Emelianov goto cont; 223279e1ad14SJiri Benc err = rtnl_fill_ifinfo(skb, dev, net, 223379e1ad14SJiri Benc RTM_NEWLINK, 223415e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 2235905cf0abSDavid Ahern nlh->nlmsg_seq, 0, flags, 223638e01b30SNicolas Dichtel ext_filter_mask, 0, NULL, 0, 2237d4e4fdf9SGuillaume Nault netnsid, GFP_KERNEL); 2238973462bbSDavid Gibson 2239f6c5775fSDavid Ahern if (err < 0) { 2240f6c5775fSDavid Ahern if (likely(skb->len)) 22417c28bd0bSEric Dumazet goto out; 22424e985adaSThomas Graf 2243f6c5775fSDavid Ahern goto out_err; 2244f6c5775fSDavid Ahern } 22457562f876SPavel Emelianov cont: 22467562f876SPavel Emelianov idx++; 22471da177e4SLinus Torvalds } 22487c28bd0bSEric Dumazet } 22497c28bd0bSEric Dumazet out: 2250f6c5775fSDavid Ahern err = skb->len; 2251f6c5775fSDavid Ahern out_err: 22527c28bd0bSEric Dumazet cb->args[1] = idx; 22537c28bd0bSEric Dumazet cb->args[0] = h; 2254a9ecb0cbSzhang kai cb->seq = tgt_net->dev_base_seq; 2255d0225784SJakub Sitnicki nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 225679e1ad14SJiri Benc if (netnsid >= 0) 225779e1ad14SJiri Benc put_net(tgt_net); 22581da177e4SLinus Torvalds 2259f6c5775fSDavid Ahern return err; 22601da177e4SLinus Torvalds } 22611da177e4SLinus Torvalds 2262fceb6435SJohannes Berg int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len, 2263fceb6435SJohannes Berg struct netlink_ext_ack *exterr) 2264f7b12606SJiri Pirko { 22658cb08174SJohannes Berg return nla_parse_deprecated(tb, IFLA_MAX, head, len, ifla_policy, 22668cb08174SJohannes Berg exterr); 2267f7b12606SJiri Pirko } 2268f7b12606SJiri Pirko EXPORT_SYMBOL(rtnl_nla_parse_ifla); 226957b61080SScott Feldman 227081adee47SEric W. Biederman struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[]) 227181adee47SEric W. Biederman { 227281adee47SEric W. Biederman struct net *net; 227381adee47SEric W. Biederman /* Examine the link attributes and figure out which 227481adee47SEric W. Biederman * network namespace we are talking about. 227581adee47SEric W. Biederman */ 227681adee47SEric W. Biederman if (tb[IFLA_NET_NS_PID]) 227781adee47SEric W. Biederman net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID])); 2278f0630529SEric W. Biederman else if (tb[IFLA_NET_NS_FD]) 2279f0630529SEric W. Biederman net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD])); 228081adee47SEric W. Biederman else 228181adee47SEric W. Biederman net = get_net(src_net); 228281adee47SEric W. Biederman return net; 228381adee47SEric W. Biederman } 228481adee47SEric W. Biederman EXPORT_SYMBOL(rtnl_link_get_net); 228581adee47SEric W. Biederman 22867c4f63baSChristian Brauner /* Figure out which network namespace we are talking about by 22877c4f63baSChristian Brauner * examining the link attributes in the following order: 22887c4f63baSChristian Brauner * 22897c4f63baSChristian Brauner * 1. IFLA_NET_NS_PID 22907c4f63baSChristian Brauner * 2. IFLA_NET_NS_FD 22917e4a8d5aSChristian Brauner * 3. IFLA_TARGET_NETNSID 22927c4f63baSChristian Brauner */ 22937c4f63baSChristian Brauner static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net, 22947c4f63baSChristian Brauner struct nlattr *tb[]) 22957c4f63baSChristian Brauner { 22967c4f63baSChristian Brauner struct net *net; 22977c4f63baSChristian Brauner 22987c4f63baSChristian Brauner if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) 22997c4f63baSChristian Brauner return rtnl_link_get_net(src_net, tb); 23007c4f63baSChristian Brauner 23017e4a8d5aSChristian Brauner if (!tb[IFLA_TARGET_NETNSID]) 23027c4f63baSChristian Brauner return get_net(src_net); 23037c4f63baSChristian Brauner 23047e4a8d5aSChristian Brauner net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_TARGET_NETNSID])); 23057c4f63baSChristian Brauner if (!net) 23067c4f63baSChristian Brauner return ERR_PTR(-EINVAL); 23077c4f63baSChristian Brauner 23087c4f63baSChristian Brauner return net; 23097c4f63baSChristian Brauner } 23107c4f63baSChristian Brauner 23117c4f63baSChristian Brauner static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb, 23127c4f63baSChristian Brauner struct net *src_net, 23137c4f63baSChristian Brauner struct nlattr *tb[], int cap) 23147c4f63baSChristian Brauner { 23157c4f63baSChristian Brauner struct net *net; 23167c4f63baSChristian Brauner 23177c4f63baSChristian Brauner net = rtnl_link_get_net_by_nlattr(src_net, tb); 23187c4f63baSChristian Brauner if (IS_ERR(net)) 23197c4f63baSChristian Brauner return net; 23207c4f63baSChristian Brauner 23217c4f63baSChristian Brauner if (!netlink_ns_capable(skb, net->user_ns, cap)) { 23227c4f63baSChristian Brauner put_net(net); 23237c4f63baSChristian Brauner return ERR_PTR(-EPERM); 23247c4f63baSChristian Brauner } 23257c4f63baSChristian Brauner 23267c4f63baSChristian Brauner return net; 23277c4f63baSChristian Brauner } 23287c4f63baSChristian Brauner 23294ff66caeSChristian Brauner /* Verify that rtnetlink requests do not pass additional properties 23304ff66caeSChristian Brauner * potentially referring to different network namespaces. 23314ff66caeSChristian Brauner */ 23324ff66caeSChristian Brauner static int rtnl_ensure_unique_netns(struct nlattr *tb[], 23334ff66caeSChristian Brauner struct netlink_ext_ack *extack, 23344ff66caeSChristian Brauner bool netns_id_only) 23354ff66caeSChristian Brauner { 23364ff66caeSChristian Brauner 23374ff66caeSChristian Brauner if (netns_id_only) { 23384ff66caeSChristian Brauner if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD]) 23394ff66caeSChristian Brauner return 0; 23404ff66caeSChristian Brauner 23414ff66caeSChristian Brauner NL_SET_ERR_MSG(extack, "specified netns attribute not supported"); 23424ff66caeSChristian Brauner return -EOPNOTSUPP; 23434ff66caeSChristian Brauner } 23444ff66caeSChristian Brauner 23457e4a8d5aSChristian Brauner if (tb[IFLA_TARGET_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])) 23464ff66caeSChristian Brauner goto invalid_attr; 23474ff66caeSChristian Brauner 23487e4a8d5aSChristian Brauner if (tb[IFLA_NET_NS_PID] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_FD])) 23494ff66caeSChristian Brauner goto invalid_attr; 23504ff66caeSChristian Brauner 23517e4a8d5aSChristian Brauner if (tb[IFLA_NET_NS_FD] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_PID])) 23524ff66caeSChristian Brauner goto invalid_attr; 23534ff66caeSChristian Brauner 23544ff66caeSChristian Brauner return 0; 23554ff66caeSChristian Brauner 23564ff66caeSChristian Brauner invalid_attr: 23574ff66caeSChristian Brauner NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified"); 23584ff66caeSChristian Brauner return -EINVAL; 23594ff66caeSChristian Brauner } 23604ff66caeSChristian Brauner 2361a14857c2SBin Chen static int rtnl_set_vf_rate(struct net_device *dev, int vf, int min_tx_rate, 2362a14857c2SBin Chen int max_tx_rate) 2363a14857c2SBin Chen { 2364a14857c2SBin Chen const struct net_device_ops *ops = dev->netdev_ops; 2365a14857c2SBin Chen 2366a14857c2SBin Chen if (!ops->ndo_set_vf_rate) 2367a14857c2SBin Chen return -EOPNOTSUPP; 2368a14857c2SBin Chen if (max_tx_rate && max_tx_rate < min_tx_rate) 2369a14857c2SBin Chen return -EINVAL; 2370a14857c2SBin Chen 2371a14857c2SBin Chen return ops->ndo_set_vf_rate(dev, vf, min_tx_rate, max_tx_rate); 2372a14857c2SBin Chen } 2373a14857c2SBin Chen 23748679c31eSRocco Yue static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[], 23758679c31eSRocco Yue struct netlink_ext_ack *extack) 23761840bb13SThomas Graf { 23771840bb13SThomas Graf if (dev) { 23781840bb13SThomas Graf if (tb[IFLA_ADDRESS] && 23791840bb13SThomas Graf nla_len(tb[IFLA_ADDRESS]) < dev->addr_len) 23801840bb13SThomas Graf return -EINVAL; 23811840bb13SThomas Graf 23821840bb13SThomas Graf if (tb[IFLA_BROADCAST] && 23831840bb13SThomas Graf nla_len(tb[IFLA_BROADCAST]) < dev->addr_len) 23841840bb13SThomas Graf return -EINVAL; 23851840bb13SThomas Graf } 23861840bb13SThomas Graf 2387cf7afbfeSThomas Graf if (tb[IFLA_AF_SPEC]) { 2388cf7afbfeSThomas Graf struct nlattr *af; 2389cf7afbfeSThomas Graf int rem, err; 2390cf7afbfeSThomas Graf 2391cf7afbfeSThomas Graf nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { 2392cf7afbfeSThomas Graf const struct rtnl_af_ops *af_ops; 2393cf7afbfeSThomas Graf 23945fa85a09SFlorian Westphal af_ops = rtnl_af_lookup(nla_type(af)); 2395a100243dSCong Wang if (!af_ops) 2396cf7afbfeSThomas Graf return -EAFNOSUPPORT; 2397cf7afbfeSThomas Graf 2398a100243dSCong Wang if (!af_ops->set_link_af) 2399cf7afbfeSThomas Graf return -EOPNOTSUPP; 2400cf7afbfeSThomas Graf 2401cf7afbfeSThomas Graf if (af_ops->validate_link_af) { 24028679c31eSRocco Yue err = af_ops->validate_link_af(dev, af, extack); 2403a100243dSCong Wang if (err < 0) 2404cf7afbfeSThomas Graf return err; 2405cf7afbfeSThomas Graf } 2406cf7afbfeSThomas Graf } 2407cf7afbfeSThomas Graf } 2408cf7afbfeSThomas Graf 24091840bb13SThomas Graf return 0; 24101840bb13SThomas Graf } 24111840bb13SThomas Graf 2412cc8e27ccSEli Cohen static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt, 2413cc8e27ccSEli Cohen int guid_type) 2414cc8e27ccSEli Cohen { 2415cc8e27ccSEli Cohen const struct net_device_ops *ops = dev->netdev_ops; 2416cc8e27ccSEli Cohen 2417cc8e27ccSEli Cohen return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type); 2418cc8e27ccSEli Cohen } 2419cc8e27ccSEli Cohen 2420cc8e27ccSEli Cohen static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type) 2421cc8e27ccSEli Cohen { 2422cc8e27ccSEli Cohen if (dev->type != ARPHRD_INFINIBAND) 2423cc8e27ccSEli Cohen return -EOPNOTSUPP; 2424cc8e27ccSEli Cohen 2425cc8e27ccSEli Cohen return handle_infiniband_guid(dev, ivt, guid_type); 2426cc8e27ccSEli Cohen } 2427cc8e27ccSEli Cohen 24284f7d2cdfSDaniel Borkmann static int do_setvfinfo(struct net_device *dev, struct nlattr **tb) 2429c02db8c6SChris Wright { 2430c02db8c6SChris Wright const struct net_device_ops *ops = dev->netdev_ops; 24314f7d2cdfSDaniel Borkmann int err = -EINVAL; 2432c02db8c6SChris Wright 24334f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_MAC]) { 24344f7d2cdfSDaniel Borkmann struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]); 24354f7d2cdfSDaniel Borkmann 2436ff08ddbaSDan Carpenter if (ivm->vf >= INT_MAX) 2437ff08ddbaSDan Carpenter return -EINVAL; 2438c02db8c6SChris Wright err = -EOPNOTSUPP; 2439c02db8c6SChris Wright if (ops->ndo_set_vf_mac) 2440c02db8c6SChris Wright err = ops->ndo_set_vf_mac(dev, ivm->vf, 2441c02db8c6SChris Wright ivm->mac); 24424f7d2cdfSDaniel Borkmann if (err < 0) 24434f7d2cdfSDaniel Borkmann return err; 2444c02db8c6SChris Wright } 24454f7d2cdfSDaniel Borkmann 24464f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_VLAN]) { 24474f7d2cdfSDaniel Borkmann struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]); 24484f7d2cdfSDaniel Borkmann 2449ff08ddbaSDan Carpenter if (ivv->vf >= INT_MAX) 2450ff08ddbaSDan Carpenter return -EINVAL; 2451c02db8c6SChris Wright err = -EOPNOTSUPP; 2452c02db8c6SChris Wright if (ops->ndo_set_vf_vlan) 24534f7d2cdfSDaniel Borkmann err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan, 245479aab093SMoshe Shemesh ivv->qos, 245579aab093SMoshe Shemesh htons(ETH_P_8021Q)); 245679aab093SMoshe Shemesh if (err < 0) 245779aab093SMoshe Shemesh return err; 245879aab093SMoshe Shemesh } 245979aab093SMoshe Shemesh 246079aab093SMoshe Shemesh if (tb[IFLA_VF_VLAN_LIST]) { 246179aab093SMoshe Shemesh struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN]; 246279aab093SMoshe Shemesh struct nlattr *attr; 246379aab093SMoshe Shemesh int rem, len = 0; 246479aab093SMoshe Shemesh 246579aab093SMoshe Shemesh err = -EOPNOTSUPP; 246679aab093SMoshe Shemesh if (!ops->ndo_set_vf_vlan) 246779aab093SMoshe Shemesh return err; 246879aab093SMoshe Shemesh 246979aab093SMoshe Shemesh nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) { 247079aab093SMoshe Shemesh if (nla_type(attr) != IFLA_VF_VLAN_INFO || 247179aab093SMoshe Shemesh nla_len(attr) < NLA_HDRLEN) { 247279aab093SMoshe Shemesh return -EINVAL; 247379aab093SMoshe Shemesh } 247479aab093SMoshe Shemesh if (len >= MAX_VLAN_LIST_LEN) 247579aab093SMoshe Shemesh return -EOPNOTSUPP; 247679aab093SMoshe Shemesh ivvl[len] = nla_data(attr); 247779aab093SMoshe Shemesh 247879aab093SMoshe Shemesh len++; 247979aab093SMoshe Shemesh } 2480fa34cd94SArnd Bergmann if (len == 0) 2481fa34cd94SArnd Bergmann return -EINVAL; 2482fa34cd94SArnd Bergmann 2483ff08ddbaSDan Carpenter if (ivvl[0]->vf >= INT_MAX) 2484ff08ddbaSDan Carpenter return -EINVAL; 248579aab093SMoshe Shemesh err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan, 248679aab093SMoshe Shemesh ivvl[0]->qos, ivvl[0]->vlan_proto); 24874f7d2cdfSDaniel Borkmann if (err < 0) 24884f7d2cdfSDaniel Borkmann return err; 2489c02db8c6SChris Wright } 24904f7d2cdfSDaniel Borkmann 24914f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_TX_RATE]) { 24924f7d2cdfSDaniel Borkmann struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]); 2493ed616689SSucheta Chakraborty struct ifla_vf_info ivf; 24944f7d2cdfSDaniel Borkmann 2495ff08ddbaSDan Carpenter if (ivt->vf >= INT_MAX) 2496ff08ddbaSDan Carpenter return -EINVAL; 2497c02db8c6SChris Wright err = -EOPNOTSUPP; 2498ed616689SSucheta Chakraborty if (ops->ndo_get_vf_config) 24994f7d2cdfSDaniel Borkmann err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf); 25004f7d2cdfSDaniel Borkmann if (err < 0) 25014f7d2cdfSDaniel Borkmann return err; 25024f7d2cdfSDaniel Borkmann 2503a14857c2SBin Chen err = rtnl_set_vf_rate(dev, ivt->vf, 2504a14857c2SBin Chen ivf.min_tx_rate, ivt->rate); 25054f7d2cdfSDaniel Borkmann if (err < 0) 25064f7d2cdfSDaniel Borkmann return err; 2507c02db8c6SChris Wright } 25084f7d2cdfSDaniel Borkmann 25094f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_RATE]) { 25104f7d2cdfSDaniel Borkmann struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]); 25114f7d2cdfSDaniel Borkmann 2512ff08ddbaSDan Carpenter if (ivt->vf >= INT_MAX) 2513ff08ddbaSDan Carpenter return -EINVAL; 2514a14857c2SBin Chen 2515a14857c2SBin Chen err = rtnl_set_vf_rate(dev, ivt->vf, 2516a14857c2SBin Chen ivt->min_tx_rate, ivt->max_tx_rate); 25174f7d2cdfSDaniel Borkmann if (err < 0) 25184f7d2cdfSDaniel Borkmann return err; 2519ed616689SSucheta Chakraborty } 25204f7d2cdfSDaniel Borkmann 25214f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_SPOOFCHK]) { 25224f7d2cdfSDaniel Borkmann struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]); 25234f7d2cdfSDaniel Borkmann 2524ff08ddbaSDan Carpenter if (ivs->vf >= INT_MAX) 2525ff08ddbaSDan Carpenter return -EINVAL; 25265f8444a3SGreg Rose err = -EOPNOTSUPP; 25275f8444a3SGreg Rose if (ops->ndo_set_vf_spoofchk) 25285f8444a3SGreg Rose err = ops->ndo_set_vf_spoofchk(dev, ivs->vf, 25295f8444a3SGreg Rose ivs->setting); 25304f7d2cdfSDaniel Borkmann if (err < 0) 25314f7d2cdfSDaniel Borkmann return err; 25325f8444a3SGreg Rose } 25334f7d2cdfSDaniel Borkmann 25344f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_LINK_STATE]) { 25354f7d2cdfSDaniel Borkmann struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]); 25364f7d2cdfSDaniel Borkmann 2537ff08ddbaSDan Carpenter if (ivl->vf >= INT_MAX) 2538ff08ddbaSDan Carpenter return -EINVAL; 25391d8faf48SRony Efraim err = -EOPNOTSUPP; 25401d8faf48SRony Efraim if (ops->ndo_set_vf_link_state) 25411d8faf48SRony Efraim err = ops->ndo_set_vf_link_state(dev, ivl->vf, 25421d8faf48SRony Efraim ivl->link_state); 25434f7d2cdfSDaniel Borkmann if (err < 0) 25444f7d2cdfSDaniel Borkmann return err; 25451d8faf48SRony Efraim } 25464f7d2cdfSDaniel Borkmann 25474f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_RSS_QUERY_EN]) { 254801a3d796SVlad Zolotarov struct ifla_vf_rss_query_en *ivrssq_en; 254901a3d796SVlad Zolotarov 255001a3d796SVlad Zolotarov err = -EOPNOTSUPP; 25514f7d2cdfSDaniel Borkmann ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]); 2552ff08ddbaSDan Carpenter if (ivrssq_en->vf >= INT_MAX) 2553ff08ddbaSDan Carpenter return -EINVAL; 255401a3d796SVlad Zolotarov if (ops->ndo_set_vf_rss_query_en) 25554f7d2cdfSDaniel Borkmann err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf, 255601a3d796SVlad Zolotarov ivrssq_en->setting); 25574f7d2cdfSDaniel Borkmann if (err < 0) 25584f7d2cdfSDaniel Borkmann return err; 255901a3d796SVlad Zolotarov } 25604f7d2cdfSDaniel Borkmann 2561dd461d6aSHiroshi Shimamoto if (tb[IFLA_VF_TRUST]) { 2562dd461d6aSHiroshi Shimamoto struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]); 2563dd461d6aSHiroshi Shimamoto 2564ff08ddbaSDan Carpenter if (ivt->vf >= INT_MAX) 2565ff08ddbaSDan Carpenter return -EINVAL; 2566dd461d6aSHiroshi Shimamoto err = -EOPNOTSUPP; 2567dd461d6aSHiroshi Shimamoto if (ops->ndo_set_vf_trust) 2568dd461d6aSHiroshi Shimamoto err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting); 2569dd461d6aSHiroshi Shimamoto if (err < 0) 2570dd461d6aSHiroshi Shimamoto return err; 2571dd461d6aSHiroshi Shimamoto } 2572dd461d6aSHiroshi Shimamoto 2573cc8e27ccSEli Cohen if (tb[IFLA_VF_IB_NODE_GUID]) { 2574cc8e27ccSEli Cohen struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]); 2575cc8e27ccSEli Cohen 2576ff08ddbaSDan Carpenter if (ivt->vf >= INT_MAX) 2577ff08ddbaSDan Carpenter return -EINVAL; 2578cc8e27ccSEli Cohen if (!ops->ndo_set_vf_guid) 2579cc8e27ccSEli Cohen return -EOPNOTSUPP; 2580cc8e27ccSEli Cohen return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID); 2581cc8e27ccSEli Cohen } 2582cc8e27ccSEli Cohen 2583cc8e27ccSEli Cohen if (tb[IFLA_VF_IB_PORT_GUID]) { 2584cc8e27ccSEli Cohen struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]); 2585cc8e27ccSEli Cohen 2586ff08ddbaSDan Carpenter if (ivt->vf >= INT_MAX) 2587ff08ddbaSDan Carpenter return -EINVAL; 2588cc8e27ccSEli Cohen if (!ops->ndo_set_vf_guid) 2589cc8e27ccSEli Cohen return -EOPNOTSUPP; 2590cc8e27ccSEli Cohen 2591cc8e27ccSEli Cohen return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID); 2592cc8e27ccSEli Cohen } 2593cc8e27ccSEli Cohen 2594c02db8c6SChris Wright return err; 2595c02db8c6SChris Wright } 2596c02db8c6SChris Wright 259733eaf2a6SDavid Ahern static int do_set_master(struct net_device *dev, int ifindex, 259833eaf2a6SDavid Ahern struct netlink_ext_ack *extack) 2599fbaec0eaSJiri Pirko { 2600898e5061SJiri Pirko struct net_device *upper_dev = netdev_master_upper_dev_get(dev); 2601fbaec0eaSJiri Pirko const struct net_device_ops *ops; 2602fbaec0eaSJiri Pirko int err; 2603fbaec0eaSJiri Pirko 2604898e5061SJiri Pirko if (upper_dev) { 2605898e5061SJiri Pirko if (upper_dev->ifindex == ifindex) 2606fbaec0eaSJiri Pirko return 0; 2607898e5061SJiri Pirko ops = upper_dev->netdev_ops; 2608fbaec0eaSJiri Pirko if (ops->ndo_del_slave) { 2609898e5061SJiri Pirko err = ops->ndo_del_slave(upper_dev, dev); 2610fbaec0eaSJiri Pirko if (err) 2611fbaec0eaSJiri Pirko return err; 2612fbaec0eaSJiri Pirko } else { 2613fbaec0eaSJiri Pirko return -EOPNOTSUPP; 2614fbaec0eaSJiri Pirko } 2615fbaec0eaSJiri Pirko } 2616fbaec0eaSJiri Pirko 2617fbaec0eaSJiri Pirko if (ifindex) { 2618898e5061SJiri Pirko upper_dev = __dev_get_by_index(dev_net(dev), ifindex); 2619898e5061SJiri Pirko if (!upper_dev) 2620fbaec0eaSJiri Pirko return -EINVAL; 2621898e5061SJiri Pirko ops = upper_dev->netdev_ops; 2622fbaec0eaSJiri Pirko if (ops->ndo_add_slave) { 262333eaf2a6SDavid Ahern err = ops->ndo_add_slave(upper_dev, dev, extack); 2624fbaec0eaSJiri Pirko if (err) 2625fbaec0eaSJiri Pirko return err; 2626fbaec0eaSJiri Pirko } else { 2627fbaec0eaSJiri Pirko return -EOPNOTSUPP; 2628fbaec0eaSJiri Pirko } 2629fbaec0eaSJiri Pirko } 2630fbaec0eaSJiri Pirko return 0; 2631fbaec0eaSJiri Pirko } 2632fbaec0eaSJiri Pirko 2633829eb208SRoopa Prabhu static const struct nla_policy ifla_proto_down_reason_policy[IFLA_PROTO_DOWN_REASON_VALUE + 1] = { 2634829eb208SRoopa Prabhu [IFLA_PROTO_DOWN_REASON_MASK] = { .type = NLA_U32 }, 2635829eb208SRoopa Prabhu [IFLA_PROTO_DOWN_REASON_VALUE] = { .type = NLA_U32 }, 2636829eb208SRoopa Prabhu }; 2637829eb208SRoopa Prabhu 2638829eb208SRoopa Prabhu static int do_set_proto_down(struct net_device *dev, 2639829eb208SRoopa Prabhu struct nlattr *nl_proto_down, 2640829eb208SRoopa Prabhu struct nlattr *nl_proto_down_reason, 2641829eb208SRoopa Prabhu struct netlink_ext_ack *extack) 2642829eb208SRoopa Prabhu { 2643829eb208SRoopa Prabhu struct nlattr *pdreason[IFLA_PROTO_DOWN_REASON_MAX + 1]; 2644829eb208SRoopa Prabhu unsigned long mask = 0; 2645829eb208SRoopa Prabhu u32 value; 2646829eb208SRoopa Prabhu bool proto_down; 2647829eb208SRoopa Prabhu int err; 2648829eb208SRoopa Prabhu 26492106efdaSJakub Kicinski if (!(dev->priv_flags & IFF_CHANGE_PROTO_DOWN)) { 2650829eb208SRoopa Prabhu NL_SET_ERR_MSG(extack, "Protodown not supported by device"); 2651829eb208SRoopa Prabhu return -EOPNOTSUPP; 2652829eb208SRoopa Prabhu } 2653829eb208SRoopa Prabhu 2654829eb208SRoopa Prabhu if (nl_proto_down_reason) { 2655829eb208SRoopa Prabhu err = nla_parse_nested_deprecated(pdreason, 2656829eb208SRoopa Prabhu IFLA_PROTO_DOWN_REASON_MAX, 2657829eb208SRoopa Prabhu nl_proto_down_reason, 2658829eb208SRoopa Prabhu ifla_proto_down_reason_policy, 2659829eb208SRoopa Prabhu NULL); 2660829eb208SRoopa Prabhu if (err < 0) 2661829eb208SRoopa Prabhu return err; 2662829eb208SRoopa Prabhu 2663829eb208SRoopa Prabhu if (!pdreason[IFLA_PROTO_DOWN_REASON_VALUE]) { 2664829eb208SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid protodown reason value"); 2665829eb208SRoopa Prabhu return -EINVAL; 2666829eb208SRoopa Prabhu } 2667829eb208SRoopa Prabhu 2668829eb208SRoopa Prabhu value = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_VALUE]); 2669829eb208SRoopa Prabhu 2670829eb208SRoopa Prabhu if (pdreason[IFLA_PROTO_DOWN_REASON_MASK]) 2671829eb208SRoopa Prabhu mask = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_MASK]); 2672829eb208SRoopa Prabhu 2673829eb208SRoopa Prabhu dev_change_proto_down_reason(dev, mask, value); 2674829eb208SRoopa Prabhu } 2675829eb208SRoopa Prabhu 2676829eb208SRoopa Prabhu if (nl_proto_down) { 2677829eb208SRoopa Prabhu proto_down = nla_get_u8(nl_proto_down); 2678829eb208SRoopa Prabhu 2679d467d0bcSZheng Yongjun /* Don't turn off protodown if there are active reasons */ 2680829eb208SRoopa Prabhu if (!proto_down && dev->proto_down_reason) { 2681829eb208SRoopa Prabhu NL_SET_ERR_MSG(extack, "Cannot clear protodown, active reasons"); 2682829eb208SRoopa Prabhu return -EBUSY; 2683829eb208SRoopa Prabhu } 2684829eb208SRoopa Prabhu err = dev_change_proto_down(dev, 2685829eb208SRoopa Prabhu proto_down); 2686829eb208SRoopa Prabhu if (err) 2687829eb208SRoopa Prabhu return err; 2688829eb208SRoopa Prabhu } 2689829eb208SRoopa Prabhu 2690829eb208SRoopa Prabhu return 0; 2691829eb208SRoopa Prabhu } 2692829eb208SRoopa Prabhu 269390c325e3SNicolas Dichtel #define DO_SETLINK_MODIFIED 0x01 2694ba998906SNicolas Dichtel /* notify flag means notify + modified. */ 2695ba998906SNicolas Dichtel #define DO_SETLINK_NOTIFY 0x03 269690f62cf3SEric W. Biederman static int do_setlink(const struct sk_buff *skb, 269790f62cf3SEric W. Biederman struct net_device *dev, struct ifinfomsg *ifm, 2698ddf9f970SJakub Kicinski struct netlink_ext_ack *extack, 26995ea08b52SFlorent Fourcot struct nlattr **tb, int status) 27000157f60cSPatrick McHardy { 2701d314774cSStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops; 27025ea08b52SFlorent Fourcot char ifname[IFNAMSIZ]; 27030157f60cSPatrick McHardy int err; 27040157f60cSPatrick McHardy 27058679c31eSRocco Yue err = validate_linkmsg(dev, tb, extack); 2706644c7eebSEric Dumazet if (err < 0) 2707644c7eebSEric Dumazet return err; 2708644c7eebSEric Dumazet 27095ea08b52SFlorent Fourcot if (tb[IFLA_IFNAME]) 27105ea08b52SFlorent Fourcot nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 27115ea08b52SFlorent Fourcot else 27125ea08b52SFlorent Fourcot ifname[0] = '\0'; 27135ea08b52SFlorent Fourcot 27147e4a8d5aSChristian Brauner if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_TARGET_NETNSID]) { 27155ea08b52SFlorent Fourcot const char *pat = ifname[0] ? ifname : NULL; 2716eeb85a14SAndrei Vagin struct net *net; 2717eeb85a14SAndrei Vagin int new_ifindex; 2718eeb85a14SAndrei Vagin 2719eeb85a14SAndrei Vagin net = rtnl_link_get_net_capable(skb, dev_net(dev), 27207c4f63baSChristian Brauner tb, CAP_NET_ADMIN); 2721d8a5ec67SEric W. Biederman if (IS_ERR(net)) { 2722d8a5ec67SEric W. Biederman err = PTR_ERR(net); 2723d8a5ec67SEric W. Biederman goto errout; 2724d8a5ec67SEric W. Biederman } 27257c4f63baSChristian Brauner 2726eeb85a14SAndrei Vagin if (tb[IFLA_NEW_IFINDEX]) 2727eeb85a14SAndrei Vagin new_ifindex = nla_get_s32(tb[IFLA_NEW_IFINDEX]); 2728eeb85a14SAndrei Vagin else 2729eeb85a14SAndrei Vagin new_ifindex = 0; 2730eeb85a14SAndrei Vagin 273196a6b93bSAndrey Ignatov err = __dev_change_net_namespace(dev, net, pat, new_ifindex); 2732d8a5ec67SEric W. Biederman put_net(net); 2733d8a5ec67SEric W. Biederman if (err) 2734d8a5ec67SEric W. Biederman goto errout; 273590c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 2736d8a5ec67SEric W. Biederman } 2737d8a5ec67SEric W. Biederman 27380157f60cSPatrick McHardy if (tb[IFLA_MAP]) { 27390157f60cSPatrick McHardy struct rtnl_link_ifmap *u_map; 27400157f60cSPatrick McHardy struct ifmap k_map; 27410157f60cSPatrick McHardy 2742d314774cSStephen Hemminger if (!ops->ndo_set_config) { 27430157f60cSPatrick McHardy err = -EOPNOTSUPP; 27440157f60cSPatrick McHardy goto errout; 27450157f60cSPatrick McHardy } 27460157f60cSPatrick McHardy 27470157f60cSPatrick McHardy if (!netif_device_present(dev)) { 27480157f60cSPatrick McHardy err = -ENODEV; 27490157f60cSPatrick McHardy goto errout; 27500157f60cSPatrick McHardy } 27510157f60cSPatrick McHardy 27520157f60cSPatrick McHardy u_map = nla_data(tb[IFLA_MAP]); 27530157f60cSPatrick McHardy k_map.mem_start = (unsigned long) u_map->mem_start; 27540157f60cSPatrick McHardy k_map.mem_end = (unsigned long) u_map->mem_end; 27550157f60cSPatrick McHardy k_map.base_addr = (unsigned short) u_map->base_addr; 27560157f60cSPatrick McHardy k_map.irq = (unsigned char) u_map->irq; 27570157f60cSPatrick McHardy k_map.dma = (unsigned char) u_map->dma; 27580157f60cSPatrick McHardy k_map.port = (unsigned char) u_map->port; 27590157f60cSPatrick McHardy 2760d314774cSStephen Hemminger err = ops->ndo_set_config(dev, &k_map); 27610157f60cSPatrick McHardy if (err < 0) 27620157f60cSPatrick McHardy goto errout; 27630157f60cSPatrick McHardy 2764ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 27650157f60cSPatrick McHardy } 27660157f60cSPatrick McHardy 27670157f60cSPatrick McHardy if (tb[IFLA_ADDRESS]) { 27680157f60cSPatrick McHardy struct sockaddr *sa; 27690157f60cSPatrick McHardy int len; 27700157f60cSPatrick McHardy 2771153711f9SWANG Cong len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len, 2772153711f9SWANG Cong sizeof(*sa)); 27730157f60cSPatrick McHardy sa = kmalloc(len, GFP_KERNEL); 27740157f60cSPatrick McHardy if (!sa) { 27750157f60cSPatrick McHardy err = -ENOMEM; 27760157f60cSPatrick McHardy goto errout; 27770157f60cSPatrick McHardy } 27780157f60cSPatrick McHardy sa->sa_family = dev->type; 27790157f60cSPatrick McHardy memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]), 27800157f60cSPatrick McHardy dev->addr_len); 27813b23a32aSCong Wang err = dev_set_mac_address_user(dev, sa, extack); 27820157f60cSPatrick McHardy kfree(sa); 27830157f60cSPatrick McHardy if (err) 27840157f60cSPatrick McHardy goto errout; 278590c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 27860157f60cSPatrick McHardy } 27870157f60cSPatrick McHardy 27880157f60cSPatrick McHardy if (tb[IFLA_MTU]) { 27897a4c53beSStephen Hemminger err = dev_set_mtu_ext(dev, nla_get_u32(tb[IFLA_MTU]), extack); 27900157f60cSPatrick McHardy if (err < 0) 27910157f60cSPatrick McHardy goto errout; 279290c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 27930157f60cSPatrick McHardy } 27940157f60cSPatrick McHardy 2795cbda10faSVlad Dogaru if (tb[IFLA_GROUP]) { 2796cbda10faSVlad Dogaru dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); 2797ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 2798cbda10faSVlad Dogaru } 2799cbda10faSVlad Dogaru 28000157f60cSPatrick McHardy /* 28010157f60cSPatrick McHardy * Interface selected by interface index but interface 28020157f60cSPatrick McHardy * name provided implies that a name change has been 28030157f60cSPatrick McHardy * requested. 28040157f60cSPatrick McHardy */ 28050157f60cSPatrick McHardy if (ifm->ifi_index > 0 && ifname[0]) { 28060157f60cSPatrick McHardy err = dev_change_name(dev, ifname); 28070157f60cSPatrick McHardy if (err < 0) 28080157f60cSPatrick McHardy goto errout; 280990c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 28100157f60cSPatrick McHardy } 28110157f60cSPatrick McHardy 28120b815a1aSStephen Hemminger if (tb[IFLA_IFALIAS]) { 28130b815a1aSStephen Hemminger err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]), 28140b815a1aSStephen Hemminger nla_len(tb[IFLA_IFALIAS])); 28150b815a1aSStephen Hemminger if (err < 0) 28160b815a1aSStephen Hemminger goto errout; 2817ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 28180b815a1aSStephen Hemminger } 28190b815a1aSStephen Hemminger 28200157f60cSPatrick McHardy if (tb[IFLA_BROADCAST]) { 28210157f60cSPatrick McHardy nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len); 2822e7c3273eSJiri Pirko call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); 28230157f60cSPatrick McHardy } 28240157f60cSPatrick McHardy 2825fbaec0eaSJiri Pirko if (tb[IFLA_MASTER]) { 282633eaf2a6SDavid Ahern err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack); 2827fbaec0eaSJiri Pirko if (err) 2828fbaec0eaSJiri Pirko goto errout; 282990c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 2830fbaec0eaSJiri Pirko } 2831fbaec0eaSJiri Pirko 2832a4abfa62SPhil Sutter if (ifm->ifi_flags || ifm->ifi_change) { 2833a4abfa62SPhil Sutter err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm), 2834a4abfa62SPhil Sutter extack); 2835a4abfa62SPhil Sutter if (err < 0) 2836a4abfa62SPhil Sutter goto errout; 2837a4abfa62SPhil Sutter } 2838a4abfa62SPhil Sutter 28399a57247fSJiri Pirko if (tb[IFLA_CARRIER]) { 28409a57247fSJiri Pirko err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER])); 28419a57247fSJiri Pirko if (err) 28429a57247fSJiri Pirko goto errout; 284390c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 28449a57247fSJiri Pirko } 28459a57247fSJiri Pirko 28465d1180fcSNicolas Dichtel if (tb[IFLA_TXQLEN]) { 28470cd29503SAlexey Dobriyan unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]); 28485d1180fcSNicolas Dichtel 28496a643ddbSCong Wang err = dev_change_tx_queue_len(dev, value); 28506a643ddbSCong Wang if (err) 285108294a26SJason Wang goto errout; 28522d7f669bSXin Long status |= DO_SETLINK_MODIFIED; 285308294a26SJason Wang } 28540157f60cSPatrick McHardy 285546e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SIZE]) { 285646e6b992SStephen Hemminger u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]); 285746e6b992SStephen Hemminger 28587c4e983cSAlexander Duyck if (max_size > dev->tso_max_size) { 285946e6b992SStephen Hemminger err = -EINVAL; 286046e6b992SStephen Hemminger goto errout; 286146e6b992SStephen Hemminger } 286246e6b992SStephen Hemminger 286346e6b992SStephen Hemminger if (dev->gso_max_size ^ max_size) { 286446e6b992SStephen Hemminger netif_set_gso_max_size(dev, max_size); 286546e6b992SStephen Hemminger status |= DO_SETLINK_MODIFIED; 286646e6b992SStephen Hemminger } 286746e6b992SStephen Hemminger } 286846e6b992SStephen Hemminger 286946e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SEGS]) { 287046e6b992SStephen Hemminger u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]); 287146e6b992SStephen Hemminger 287214d7b812SJakub Kicinski if (max_segs > GSO_MAX_SEGS || max_segs > dev->tso_max_segs) { 287346e6b992SStephen Hemminger err = -EINVAL; 287446e6b992SStephen Hemminger goto errout; 287546e6b992SStephen Hemminger } 287646e6b992SStephen Hemminger 287746e6b992SStephen Hemminger if (dev->gso_max_segs ^ max_segs) { 28786d872df3SEric Dumazet netif_set_gso_max_segs(dev, max_segs); 287946e6b992SStephen Hemminger status |= DO_SETLINK_MODIFIED; 288046e6b992SStephen Hemminger } 288146e6b992SStephen Hemminger } 288246e6b992SStephen Hemminger 2883eac1b93cSCoco Li if (tb[IFLA_GRO_MAX_SIZE]) { 2884eac1b93cSCoco Li u32 gro_max_size = nla_get_u32(tb[IFLA_GRO_MAX_SIZE]); 2885eac1b93cSCoco Li 2886eac1b93cSCoco Li if (dev->gro_max_size ^ gro_max_size) { 2887eac1b93cSCoco Li netif_set_gro_max_size(dev, gro_max_size); 2888eac1b93cSCoco Li status |= DO_SETLINK_MODIFIED; 2889eac1b93cSCoco Li } 2890eac1b93cSCoco Li } 2891eac1b93cSCoco Li 2892*9eefedd5SXin Long if (tb[IFLA_GSO_IPV4_MAX_SIZE]) { 2893*9eefedd5SXin Long u32 max_size = nla_get_u32(tb[IFLA_GSO_IPV4_MAX_SIZE]); 2894*9eefedd5SXin Long 2895*9eefedd5SXin Long if (max_size > dev->tso_max_size) { 2896*9eefedd5SXin Long err = -EINVAL; 2897*9eefedd5SXin Long goto errout; 2898*9eefedd5SXin Long } 2899*9eefedd5SXin Long 2900*9eefedd5SXin Long if (dev->gso_ipv4_max_size ^ max_size) { 2901*9eefedd5SXin Long netif_set_gso_ipv4_max_size(dev, max_size); 2902*9eefedd5SXin Long status |= DO_SETLINK_MODIFIED; 2903*9eefedd5SXin Long } 2904*9eefedd5SXin Long } 2905*9eefedd5SXin Long 2906*9eefedd5SXin Long if (tb[IFLA_GRO_IPV4_MAX_SIZE]) { 2907*9eefedd5SXin Long u32 gro_max_size = nla_get_u32(tb[IFLA_GRO_IPV4_MAX_SIZE]); 2908*9eefedd5SXin Long 2909*9eefedd5SXin Long if (dev->gro_ipv4_max_size ^ gro_max_size) { 2910*9eefedd5SXin Long netif_set_gro_ipv4_max_size(dev, gro_max_size); 2911*9eefedd5SXin Long status |= DO_SETLINK_MODIFIED; 2912*9eefedd5SXin Long } 2913*9eefedd5SXin Long } 2914*9eefedd5SXin Long 29150157f60cSPatrick McHardy if (tb[IFLA_OPERSTATE]) 291693b2d4a2SDavid S. Miller set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 29170157f60cSPatrick McHardy 29180157f60cSPatrick McHardy if (tb[IFLA_LINKMODE]) { 29191889b0e7SNicolas Dichtel unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]); 29201889b0e7SNicolas Dichtel 2921fd888e85SSebastian Andrzej Siewior write_lock(&dev_base_lock); 29221889b0e7SNicolas Dichtel if (dev->link_mode ^ value) 2923ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 29241889b0e7SNicolas Dichtel dev->link_mode = value; 2925fd888e85SSebastian Andrzej Siewior write_unlock(&dev_base_lock); 29260157f60cSPatrick McHardy } 29270157f60cSPatrick McHardy 2928c02db8c6SChris Wright if (tb[IFLA_VFINFO_LIST]) { 29294f7d2cdfSDaniel Borkmann struct nlattr *vfinfo[IFLA_VF_MAX + 1]; 2930c02db8c6SChris Wright struct nlattr *attr; 2931c02db8c6SChris Wright int rem; 29324f7d2cdfSDaniel Borkmann 2933c02db8c6SChris Wright nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) { 29344f7d2cdfSDaniel Borkmann if (nla_type(attr) != IFLA_VF_INFO || 29354f7d2cdfSDaniel Borkmann nla_len(attr) < NLA_HDRLEN) { 2936253683bbSDavid Howells err = -EINVAL; 2937c02db8c6SChris Wright goto errout; 2938253683bbSDavid Howells } 29398cb08174SJohannes Berg err = nla_parse_nested_deprecated(vfinfo, IFLA_VF_MAX, 29408cb08174SJohannes Berg attr, 29418cb08174SJohannes Berg ifla_vf_policy, 29428cb08174SJohannes Berg NULL); 29434f7d2cdfSDaniel Borkmann if (err < 0) 29444f7d2cdfSDaniel Borkmann goto errout; 29454f7d2cdfSDaniel Borkmann err = do_setvfinfo(dev, vfinfo); 2946ebc08a6fSWilliams, Mitch A if (err < 0) 2947ebc08a6fSWilliams, Mitch A goto errout; 2948ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 2949ebc08a6fSWilliams, Mitch A } 2950ebc08a6fSWilliams, Mitch A } 29510157f60cSPatrick McHardy err = 0; 29520157f60cSPatrick McHardy 295357b61080SScott Feldman if (tb[IFLA_VF_PORTS]) { 295457b61080SScott Feldman struct nlattr *port[IFLA_PORT_MAX+1]; 295557b61080SScott Feldman struct nlattr *attr; 295657b61080SScott Feldman int vf; 295757b61080SScott Feldman int rem; 295857b61080SScott Feldman 295957b61080SScott Feldman err = -EOPNOTSUPP; 296057b61080SScott Feldman if (!ops->ndo_set_vf_port) 296157b61080SScott Feldman goto errout; 296257b61080SScott Feldman 296357b61080SScott Feldman nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) { 2964035d210fSDaniel Borkmann if (nla_type(attr) != IFLA_VF_PORT || 2965035d210fSDaniel Borkmann nla_len(attr) < NLA_HDRLEN) { 2966035d210fSDaniel Borkmann err = -EINVAL; 2967035d210fSDaniel Borkmann goto errout; 2968035d210fSDaniel Borkmann } 29698cb08174SJohannes Berg err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX, 29708cb08174SJohannes Berg attr, 29718cb08174SJohannes Berg ifla_port_policy, 29728cb08174SJohannes Berg NULL); 297357b61080SScott Feldman if (err < 0) 297457b61080SScott Feldman goto errout; 297557b61080SScott Feldman if (!port[IFLA_PORT_VF]) { 297657b61080SScott Feldman err = -EOPNOTSUPP; 297757b61080SScott Feldman goto errout; 297857b61080SScott Feldman } 297957b61080SScott Feldman vf = nla_get_u32(port[IFLA_PORT_VF]); 298057b61080SScott Feldman err = ops->ndo_set_vf_port(dev, vf, port); 298157b61080SScott Feldman if (err < 0) 298257b61080SScott Feldman goto errout; 2983ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 298457b61080SScott Feldman } 298557b61080SScott Feldman } 298657b61080SScott Feldman err = 0; 298757b61080SScott Feldman 298857b61080SScott Feldman if (tb[IFLA_PORT_SELF]) { 298957b61080SScott Feldman struct nlattr *port[IFLA_PORT_MAX+1]; 299057b61080SScott Feldman 29918cb08174SJohannes Berg err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX, 29928cb08174SJohannes Berg tb[IFLA_PORT_SELF], 29938cb08174SJohannes Berg ifla_port_policy, NULL); 299457b61080SScott Feldman if (err < 0) 299557b61080SScott Feldman goto errout; 299657b61080SScott Feldman 299757b61080SScott Feldman err = -EOPNOTSUPP; 299857b61080SScott Feldman if (ops->ndo_set_vf_port) 299957b61080SScott Feldman err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port); 300057b61080SScott Feldman if (err < 0) 300157b61080SScott Feldman goto errout; 3002ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 300357b61080SScott Feldman } 3004f8ff182cSThomas Graf 3005f8ff182cSThomas Graf if (tb[IFLA_AF_SPEC]) { 3006f8ff182cSThomas Graf struct nlattr *af; 3007f8ff182cSThomas Graf int rem; 3008f8ff182cSThomas Graf 3009f8ff182cSThomas Graf nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { 3010f8ff182cSThomas Graf const struct rtnl_af_ops *af_ops; 3011f8ff182cSThomas Graf 3012058c8d59SGustavo A. R. Silva BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af)))); 3013f8ff182cSThomas Graf 30143583a4e8SStephen Hemminger err = af_ops->set_link_af(dev, af, extack); 3015a100243dSCong Wang if (err < 0) 3016f8ff182cSThomas Graf goto errout; 3017f8ff182cSThomas Graf 3018ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 3019f8ff182cSThomas Graf } 3020f8ff182cSThomas Graf } 302157b61080SScott Feldman err = 0; 302257b61080SScott Feldman 3023829eb208SRoopa Prabhu if (tb[IFLA_PROTO_DOWN] || tb[IFLA_PROTO_DOWN_REASON]) { 3024829eb208SRoopa Prabhu err = do_set_proto_down(dev, tb[IFLA_PROTO_DOWN], 3025829eb208SRoopa Prabhu tb[IFLA_PROTO_DOWN_REASON], extack); 302688d6378bSAnuradha Karuppiah if (err) 302788d6378bSAnuradha Karuppiah goto errout; 302888d6378bSAnuradha Karuppiah status |= DO_SETLINK_NOTIFY; 302988d6378bSAnuradha Karuppiah } 303088d6378bSAnuradha Karuppiah 3031d1fdd913SBrenden Blanco if (tb[IFLA_XDP]) { 3032d1fdd913SBrenden Blanco struct nlattr *xdp[IFLA_XDP_MAX + 1]; 303385de8576SDaniel Borkmann u32 xdp_flags = 0; 3034d1fdd913SBrenden Blanco 30358cb08174SJohannes Berg err = nla_parse_nested_deprecated(xdp, IFLA_XDP_MAX, 30368cb08174SJohannes Berg tb[IFLA_XDP], 3037fceb6435SJohannes Berg ifla_xdp_policy, NULL); 3038d1fdd913SBrenden Blanco if (err < 0) 3039d1fdd913SBrenden Blanco goto errout; 3040d1fdd913SBrenden Blanco 304158038695SMartin KaFai Lau if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) { 3042262d8625SBrenden Blanco err = -EINVAL; 3043262d8625SBrenden Blanco goto errout; 3044262d8625SBrenden Blanco } 304585de8576SDaniel Borkmann 304685de8576SDaniel Borkmann if (xdp[IFLA_XDP_FLAGS]) { 304785de8576SDaniel Borkmann xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]); 304885de8576SDaniel Borkmann if (xdp_flags & ~XDP_FLAGS_MASK) { 304985de8576SDaniel Borkmann err = -EINVAL; 305085de8576SDaniel Borkmann goto errout; 305185de8576SDaniel Borkmann } 3052ee5d032fSJakub Kicinski if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) { 30530489df9aSDaniel Borkmann err = -EINVAL; 30540489df9aSDaniel Borkmann goto errout; 30550489df9aSDaniel Borkmann } 305685de8576SDaniel Borkmann } 305785de8576SDaniel Borkmann 3058d1fdd913SBrenden Blanco if (xdp[IFLA_XDP_FD]) { 305992234c8fSToke Høiland-Jørgensen int expected_fd = -1; 306092234c8fSToke Høiland-Jørgensen 306192234c8fSToke Høiland-Jørgensen if (xdp_flags & XDP_FLAGS_REPLACE) { 306292234c8fSToke Høiland-Jørgensen if (!xdp[IFLA_XDP_EXPECTED_FD]) { 306392234c8fSToke Høiland-Jørgensen err = -EINVAL; 306492234c8fSToke Høiland-Jørgensen goto errout; 306592234c8fSToke Høiland-Jørgensen } 306692234c8fSToke Høiland-Jørgensen expected_fd = 306792234c8fSToke Høiland-Jørgensen nla_get_s32(xdp[IFLA_XDP_EXPECTED_FD]); 306892234c8fSToke Høiland-Jørgensen } 306992234c8fSToke Høiland-Jørgensen 3070ddf9f970SJakub Kicinski err = dev_change_xdp_fd(dev, extack, 307185de8576SDaniel Borkmann nla_get_s32(xdp[IFLA_XDP_FD]), 307292234c8fSToke Høiland-Jørgensen expected_fd, 307385de8576SDaniel Borkmann xdp_flags); 3074d1fdd913SBrenden Blanco if (err) 3075d1fdd913SBrenden Blanco goto errout; 3076d1fdd913SBrenden Blanco status |= DO_SETLINK_NOTIFY; 3077d1fdd913SBrenden Blanco } 3078d1fdd913SBrenden Blanco } 3079d1fdd913SBrenden Blanco 30800157f60cSPatrick McHardy errout: 3081ba998906SNicolas Dichtel if (status & DO_SETLINK_MODIFIED) { 308264ff90ccSXin Long if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY) 3083ba998906SNicolas Dichtel netdev_state_change(dev); 3084ba998906SNicolas Dichtel 3085ba998906SNicolas Dichtel if (err < 0) 3086e87cc472SJoe 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", 3087e87cc472SJoe Perches dev->name); 3088ba998906SNicolas Dichtel } 30890157f60cSPatrick McHardy 30900157f60cSPatrick McHardy return err; 30910157f60cSPatrick McHardy } 30920157f60cSPatrick McHardy 3093cc6090e9SJiri Pirko static struct net_device *rtnl_dev_get(struct net *net, 30945ea08b52SFlorent Fourcot struct nlattr *tb[]) 3095cc6090e9SJiri Pirko { 30965ea08b52SFlorent Fourcot char ifname[ALTIFNAMSIZ]; 3097cc6090e9SJiri Pirko 30985ea08b52SFlorent Fourcot if (tb[IFLA_IFNAME]) 30995ea08b52SFlorent Fourcot nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 31005ea08b52SFlorent Fourcot else if (tb[IFLA_ALT_IFNAME]) 31015ea08b52SFlorent Fourcot nla_strscpy(ifname, tb[IFLA_ALT_IFNAME], ALTIFNAMSIZ); 3102cc6090e9SJiri Pirko else 3103cc6090e9SJiri Pirko return NULL; 3104cc6090e9SJiri Pirko 3105cc6090e9SJiri Pirko return __dev_get_by_name(net, ifname); 3106cc6090e9SJiri Pirko } 3107cc6090e9SJiri Pirko 3108c21ef3e3SDavid Ahern static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, 3109c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 3110da5e0494SThomas Graf { 31113b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 3112da5e0494SThomas Graf struct ifinfomsg *ifm; 3113da5e0494SThomas Graf struct net_device *dev; 31140157f60cSPatrick McHardy int err; 3115da5e0494SThomas Graf struct nlattr *tb[IFLA_MAX+1]; 31161da177e4SLinus Torvalds 31178cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX, 31188cb08174SJohannes Berg ifla_policy, extack); 3119da5e0494SThomas Graf if (err < 0) 3120da5e0494SThomas Graf goto errout; 31211da177e4SLinus Torvalds 31224ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, false); 31234ff66caeSChristian Brauner if (err < 0) 31244ff66caeSChristian Brauner goto errout; 31254ff66caeSChristian Brauner 31261da177e4SLinus Torvalds err = -EINVAL; 3127da5e0494SThomas Graf ifm = nlmsg_data(nlh); 312851055be8SPatrick McHardy if (ifm->ifi_index > 0) 3129a3d12891SEric Dumazet dev = __dev_get_by_index(net, ifm->ifi_index); 313076c9ac0eSJiri Pirko else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) 31315ea08b52SFlorent Fourcot dev = rtnl_dev_get(net, tb); 3132da5e0494SThomas Graf else 3133da5e0494SThomas Graf goto errout; 31341da177e4SLinus Torvalds 3135da5e0494SThomas Graf if (dev == NULL) { 3136da5e0494SThomas Graf err = -ENODEV; 3137da5e0494SThomas Graf goto errout; 3138da5e0494SThomas Graf } 31391da177e4SLinus Torvalds 31405ea08b52SFlorent Fourcot err = do_setlink(skb, dev, ifm, extack, tb, 0); 3141da5e0494SThomas Graf errout: 31421da177e4SLinus Torvalds return err; 31431da177e4SLinus Torvalds } 31441da177e4SLinus Torvalds 314566400d54SWANG Cong static int rtnl_group_dellink(const struct net *net, int group) 314666400d54SWANG Cong { 314766400d54SWANG Cong struct net_device *dev, *aux; 314866400d54SWANG Cong LIST_HEAD(list_kill); 314966400d54SWANG Cong bool found = false; 315066400d54SWANG Cong 315166400d54SWANG Cong if (!group) 315266400d54SWANG Cong return -EPERM; 315366400d54SWANG Cong 315466400d54SWANG Cong for_each_netdev(net, dev) { 315566400d54SWANG Cong if (dev->group == group) { 315666400d54SWANG Cong const struct rtnl_link_ops *ops; 315766400d54SWANG Cong 315866400d54SWANG Cong found = true; 315966400d54SWANG Cong ops = dev->rtnl_link_ops; 316066400d54SWANG Cong if (!ops || !ops->dellink) 316166400d54SWANG Cong return -EOPNOTSUPP; 316266400d54SWANG Cong } 316366400d54SWANG Cong } 316466400d54SWANG Cong 316566400d54SWANG Cong if (!found) 316666400d54SWANG Cong return -ENODEV; 316766400d54SWANG Cong 316866400d54SWANG Cong for_each_netdev_safe(net, dev, aux) { 316966400d54SWANG Cong if (dev->group == group) { 317066400d54SWANG Cong const struct rtnl_link_ops *ops; 317166400d54SWANG Cong 317266400d54SWANG Cong ops = dev->rtnl_link_ops; 317366400d54SWANG Cong ops->dellink(dev, &list_kill); 317466400d54SWANG Cong } 317566400d54SWANG Cong } 317666400d54SWANG Cong unregister_netdevice_many(&list_kill); 317766400d54SWANG Cong 317866400d54SWANG Cong return 0; 317966400d54SWANG Cong } 318066400d54SWANG Cong 3181f3a63cceSHangbin Liu int rtnl_delete_link(struct net_device *dev, u32 portid, const struct nlmsghdr *nlh) 3182614732eaSThomas Graf { 3183614732eaSThomas Graf const struct rtnl_link_ops *ops; 3184614732eaSThomas Graf LIST_HEAD(list_kill); 3185614732eaSThomas Graf 3186614732eaSThomas Graf ops = dev->rtnl_link_ops; 3187614732eaSThomas Graf if (!ops || !ops->dellink) 3188614732eaSThomas Graf return -EOPNOTSUPP; 3189614732eaSThomas Graf 3190614732eaSThomas Graf ops->dellink(dev, &list_kill); 3191f3a63cceSHangbin Liu unregister_netdevice_many_notify(&list_kill, portid, nlh); 3192614732eaSThomas Graf 3193614732eaSThomas Graf return 0; 3194614732eaSThomas Graf } 3195614732eaSThomas Graf EXPORT_SYMBOL_GPL(rtnl_delete_link); 3196614732eaSThomas Graf 3197c21ef3e3SDavid Ahern static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, 3198c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 319938f7b870SPatrick McHardy { 32003b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 3201f3a63cceSHangbin Liu u32 portid = NETLINK_CB(skb).portid; 3202b61ad68aSChristian Brauner struct net *tgt_net = net; 3203b61ad68aSChristian Brauner struct net_device *dev = NULL; 320438f7b870SPatrick McHardy struct ifinfomsg *ifm; 320538f7b870SPatrick McHardy struct nlattr *tb[IFLA_MAX+1]; 320638f7b870SPatrick McHardy int err; 3207b61ad68aSChristian Brauner int netnsid = -1; 320838f7b870SPatrick McHardy 32098cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX, 32108cb08174SJohannes Berg ifla_policy, extack); 321138f7b870SPatrick McHardy if (err < 0) 321238f7b870SPatrick McHardy return err; 321338f7b870SPatrick McHardy 32144ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, true); 32154ff66caeSChristian Brauner if (err < 0) 32164ff66caeSChristian Brauner return err; 32174ff66caeSChristian Brauner 32187e4a8d5aSChristian Brauner if (tb[IFLA_TARGET_NETNSID]) { 32197e4a8d5aSChristian Brauner netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]); 3220c383edc4SChristian Brauner tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid); 3221b61ad68aSChristian Brauner if (IS_ERR(tgt_net)) 3222b61ad68aSChristian Brauner return PTR_ERR(tgt_net); 3223b61ad68aSChristian Brauner } 3224b61ad68aSChristian Brauner 3225b61ad68aSChristian Brauner err = -EINVAL; 322638f7b870SPatrick McHardy ifm = nlmsg_data(nlh); 322738f7b870SPatrick McHardy if (ifm->ifi_index > 0) 3228b61ad68aSChristian Brauner dev = __dev_get_by_index(tgt_net, ifm->ifi_index); 322976c9ac0eSJiri Pirko else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) 32305ea08b52SFlorent Fourcot dev = rtnl_dev_get(net, tb); 323166400d54SWANG Cong else if (tb[IFLA_GROUP]) 3232b61ad68aSChristian Brauner err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP])); 323338f7b870SPatrick McHardy else 3234b61ad68aSChristian Brauner goto out; 323538f7b870SPatrick McHardy 3236b61ad68aSChristian Brauner if (!dev) { 3237dee04163SFlorent Fourcot if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME] || ifm->ifi_index > 0) 3238b61ad68aSChristian Brauner err = -ENODEV; 323938f7b870SPatrick McHardy 3240b61ad68aSChristian Brauner goto out; 3241b61ad68aSChristian Brauner } 3242b61ad68aSChristian Brauner 3243f3a63cceSHangbin Liu err = rtnl_delete_link(dev, portid, nlh); 3244b61ad68aSChristian Brauner 3245b61ad68aSChristian Brauner out: 3246b61ad68aSChristian Brauner if (netnsid >= 0) 3247b61ad68aSChristian Brauner put_net(tgt_net); 3248b61ad68aSChristian Brauner 3249b61ad68aSChristian Brauner return err; 325038f7b870SPatrick McHardy } 325138f7b870SPatrick McHardy 32521d997f10SHangbin Liu int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm, 32531d997f10SHangbin Liu u32 portid, const struct nlmsghdr *nlh) 32543729d502SPatrick McHardy { 32553729d502SPatrick McHardy unsigned int old_flags; 32563729d502SPatrick McHardy int err; 32573729d502SPatrick McHardy 32583729d502SPatrick McHardy old_flags = dev->flags; 32593729d502SPatrick McHardy if (ifm && (ifm->ifi_flags || ifm->ifi_change)) { 32606d040321SPetr Machata err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm), 32616d040321SPetr Machata NULL); 32623729d502SPatrick McHardy if (err < 0) 32633729d502SPatrick McHardy return err; 32643729d502SPatrick McHardy } 32653729d502SPatrick McHardy 32668d356b89SRoopa Prabhu if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) { 32671d997f10SHangbin Liu __dev_notify_flags(dev, old_flags, (old_flags ^ dev->flags), portid, nlh); 32688d356b89SRoopa Prabhu } else { 32693729d502SPatrick McHardy dev->rtnl_link_state = RTNL_LINK_INITIALIZED; 32701d997f10SHangbin Liu __dev_notify_flags(dev, old_flags, ~0U, portid, nlh); 32718d356b89SRoopa Prabhu } 32723729d502SPatrick McHardy return 0; 32733729d502SPatrick McHardy } 32743729d502SPatrick McHardy EXPORT_SYMBOL(rtnl_configure_link); 32753729d502SPatrick McHardy 3276d0522f1cSDavid Ahern struct net_device *rtnl_create_link(struct net *net, const char *ifname, 3277d0522f1cSDavid Ahern unsigned char name_assign_type, 3278d0522f1cSDavid Ahern const struct rtnl_link_ops *ops, 3279d0522f1cSDavid Ahern struct nlattr *tb[], 3280d0522f1cSDavid Ahern struct netlink_ext_ack *extack) 3281e7199288SPavel Emelianov { 3282e7199288SPavel Emelianov struct net_device *dev; 3283d40156aaSJiri Pirko unsigned int num_tx_queues = 1; 3284d40156aaSJiri Pirko unsigned int num_rx_queues = 1; 3285e7199288SPavel Emelianov 328676ff5cc9SJiri Pirko if (tb[IFLA_NUM_TX_QUEUES]) 328776ff5cc9SJiri Pirko num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]); 328876ff5cc9SJiri Pirko else if (ops->get_num_tx_queues) 3289d40156aaSJiri Pirko num_tx_queues = ops->get_num_tx_queues(); 329076ff5cc9SJiri Pirko 329176ff5cc9SJiri Pirko if (tb[IFLA_NUM_RX_QUEUES]) 329276ff5cc9SJiri Pirko num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]); 329376ff5cc9SJiri Pirko else if (ops->get_num_rx_queues) 3294d40156aaSJiri Pirko num_rx_queues = ops->get_num_rx_queues(); 3295efacb309Sstephen hemminger 3296d0522f1cSDavid Ahern if (num_tx_queues < 1 || num_tx_queues > 4096) { 3297d0522f1cSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid number of transmit queues"); 32980e1d6ecaSEric Dumazet return ERR_PTR(-EINVAL); 3299d0522f1cSDavid Ahern } 33000e1d6ecaSEric Dumazet 3301d0522f1cSDavid Ahern if (num_rx_queues < 1 || num_rx_queues > 4096) { 3302d0522f1cSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid number of receive queues"); 33030e1d6ecaSEric Dumazet return ERR_PTR(-EINVAL); 3304d0522f1cSDavid Ahern } 33050e1d6ecaSEric Dumazet 33068c713dc9SJohannes Berg if (ops->alloc) { 33078c713dc9SJohannes Berg dev = ops->alloc(tb, ifname, name_assign_type, 33088c713dc9SJohannes Berg num_tx_queues, num_rx_queues); 33098c713dc9SJohannes Berg if (IS_ERR(dev)) 33108c713dc9SJohannes Berg return dev; 33118c713dc9SJohannes Berg } else { 33128c713dc9SJohannes Berg dev = alloc_netdev_mqs(ops->priv_size, ifname, 33138c713dc9SJohannes Berg name_assign_type, ops->setup, 33148c713dc9SJohannes Berg num_tx_queues, num_rx_queues); 33158c713dc9SJohannes Berg } 33168c713dc9SJohannes Berg 3317e7199288SPavel Emelianov if (!dev) 3318d1892e4eSTobias Klauser return ERR_PTR(-ENOMEM); 3319e7199288SPavel Emelianov 332081adee47SEric W. Biederman dev_net_set(dev, net); 332181adee47SEric W. Biederman dev->rtnl_link_ops = ops; 33223729d502SPatrick McHardy dev->rtnl_link_state = RTNL_LINK_INITIALIZING; 332381adee47SEric W. Biederman 3324d836f5c6SEric Dumazet if (tb[IFLA_MTU]) { 3325d836f5c6SEric Dumazet u32 mtu = nla_get_u32(tb[IFLA_MTU]); 3326d836f5c6SEric Dumazet int err; 3327d836f5c6SEric Dumazet 3328d836f5c6SEric Dumazet err = dev_validate_mtu(dev, mtu, extack); 3329d836f5c6SEric Dumazet if (err) { 3330d836f5c6SEric Dumazet free_netdev(dev); 3331d836f5c6SEric Dumazet return ERR_PTR(err); 3332d836f5c6SEric Dumazet } 3333d836f5c6SEric Dumazet dev->mtu = mtu; 3334d836f5c6SEric Dumazet } 33352afb9b53SJiri Pirko if (tb[IFLA_ADDRESS]) { 3336efd38f75SJakub Kicinski __dev_addr_set(dev, nla_data(tb[IFLA_ADDRESS]), 3337e7199288SPavel Emelianov nla_len(tb[IFLA_ADDRESS])); 33382afb9b53SJiri Pirko dev->addr_assign_type = NET_ADDR_SET; 33392afb9b53SJiri Pirko } 3340e7199288SPavel Emelianov if (tb[IFLA_BROADCAST]) 3341e7199288SPavel Emelianov memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]), 3342e7199288SPavel Emelianov nla_len(tb[IFLA_BROADCAST])); 3343e7199288SPavel Emelianov if (tb[IFLA_TXQLEN]) 3344e7199288SPavel Emelianov dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); 3345e7199288SPavel Emelianov if (tb[IFLA_OPERSTATE]) 334693b2d4a2SDavid S. Miller set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 3347e7199288SPavel Emelianov if (tb[IFLA_LINKMODE]) 3348e7199288SPavel Emelianov dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); 3349ffa934f1SPatrick McHardy if (tb[IFLA_GROUP]) 3350ffa934f1SPatrick McHardy dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); 335146e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SIZE]) 335246e6b992SStephen Hemminger netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE])); 335346e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SEGS]) 33546d872df3SEric Dumazet netif_set_gso_max_segs(dev, nla_get_u32(tb[IFLA_GSO_MAX_SEGS])); 3355eac1b93cSCoco Li if (tb[IFLA_GRO_MAX_SIZE]) 3356eac1b93cSCoco Li netif_set_gro_max_size(dev, nla_get_u32(tb[IFLA_GRO_MAX_SIZE])); 3357*9eefedd5SXin Long if (tb[IFLA_GSO_IPV4_MAX_SIZE]) 3358*9eefedd5SXin Long netif_set_gso_ipv4_max_size(dev, nla_get_u32(tb[IFLA_GSO_IPV4_MAX_SIZE])); 3359*9eefedd5SXin Long if (tb[IFLA_GRO_IPV4_MAX_SIZE]) 3360*9eefedd5SXin Long netif_set_gro_ipv4_max_size(dev, nla_get_u32(tb[IFLA_GRO_IPV4_MAX_SIZE])); 3361e7199288SPavel Emelianov 3362e7199288SPavel Emelianov return dev; 3363e7199288SPavel Emelianov } 3364e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_create_link); 3365e7199288SPavel Emelianov 336690f62cf3SEric W. Biederman static int rtnl_group_changelink(const struct sk_buff *skb, 336790f62cf3SEric W. Biederman struct net *net, int group, 3368e7ed828fSVlad Dogaru struct ifinfomsg *ifm, 3369ddf9f970SJakub Kicinski struct netlink_ext_ack *extack, 3370e7ed828fSVlad Dogaru struct nlattr **tb) 3371e7ed828fSVlad Dogaru { 3372d079535dSWANG Cong struct net_device *dev, *aux; 3373e7ed828fSVlad Dogaru int err; 3374e7ed828fSVlad Dogaru 3375d079535dSWANG Cong for_each_netdev_safe(net, dev, aux) { 3376e7ed828fSVlad Dogaru if (dev->group == group) { 33775ea08b52SFlorent Fourcot err = do_setlink(skb, dev, ifm, extack, tb, 0); 3378e7ed828fSVlad Dogaru if (err < 0) 3379e7ed828fSVlad Dogaru return err; 3380e7ed828fSVlad Dogaru } 3381e7ed828fSVlad Dogaru } 3382e7ed828fSVlad Dogaru 3383e7ed828fSVlad Dogaru return 0; 3384e7ed828fSVlad Dogaru } 3385e7ed828fSVlad Dogaru 338663105e83SJakub Kicinski static int rtnl_newlink_create(struct sk_buff *skb, struct ifinfomsg *ifm, 338763105e83SJakub Kicinski const struct rtnl_link_ops *ops, 3388d88e136cSHangbin Liu const struct nlmsghdr *nlh, 338963105e83SJakub Kicinski struct nlattr **tb, struct nlattr **data, 339002839cc8SJakub Kicinski struct netlink_ext_ack *extack) 339102839cc8SJakub Kicinski { 339202839cc8SJakub Kicinski unsigned char name_assign_type = NET_NAME_USER; 339302839cc8SJakub Kicinski struct net *net = sock_net(skb->sk); 3394d88e136cSHangbin Liu u32 portid = NETLINK_CB(skb).portid; 339502839cc8SJakub Kicinski struct net *dest_net, *link_net; 339602839cc8SJakub Kicinski struct net_device *dev; 339702839cc8SJakub Kicinski char ifname[IFNAMSIZ]; 339802839cc8SJakub Kicinski int err; 339902839cc8SJakub Kicinski 340002839cc8SJakub Kicinski if (!ops->alloc && !ops->setup) 340102839cc8SJakub Kicinski return -EOPNOTSUPP; 340202839cc8SJakub Kicinski 340302839cc8SJakub Kicinski if (tb[IFLA_IFNAME]) { 340402839cc8SJakub Kicinski nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 340502839cc8SJakub Kicinski } else { 340602839cc8SJakub Kicinski snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind); 340702839cc8SJakub Kicinski name_assign_type = NET_NAME_ENUM; 340802839cc8SJakub Kicinski } 340902839cc8SJakub Kicinski 341002839cc8SJakub Kicinski dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN); 341102839cc8SJakub Kicinski if (IS_ERR(dest_net)) 341202839cc8SJakub Kicinski return PTR_ERR(dest_net); 341302839cc8SJakub Kicinski 341402839cc8SJakub Kicinski if (tb[IFLA_LINK_NETNSID]) { 341502839cc8SJakub Kicinski int id = nla_get_s32(tb[IFLA_LINK_NETNSID]); 341602839cc8SJakub Kicinski 341702839cc8SJakub Kicinski link_net = get_net_ns_by_id(dest_net, id); 341802839cc8SJakub Kicinski if (!link_net) { 341902839cc8SJakub Kicinski NL_SET_ERR_MSG(extack, "Unknown network namespace id"); 342002839cc8SJakub Kicinski err = -EINVAL; 342102839cc8SJakub Kicinski goto out; 342202839cc8SJakub Kicinski } 342302839cc8SJakub Kicinski err = -EPERM; 342402839cc8SJakub Kicinski if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN)) 342502839cc8SJakub Kicinski goto out; 342602839cc8SJakub Kicinski } else { 342702839cc8SJakub Kicinski link_net = NULL; 342802839cc8SJakub Kicinski } 342902839cc8SJakub Kicinski 343002839cc8SJakub Kicinski dev = rtnl_create_link(link_net ? : dest_net, ifname, 343102839cc8SJakub Kicinski name_assign_type, ops, tb, extack); 343202839cc8SJakub Kicinski if (IS_ERR(dev)) { 343302839cc8SJakub Kicinski err = PTR_ERR(dev); 343402839cc8SJakub Kicinski goto out; 343502839cc8SJakub Kicinski } 343602839cc8SJakub Kicinski 343702839cc8SJakub Kicinski dev->ifindex = ifm->ifi_index; 343802839cc8SJakub Kicinski 343902839cc8SJakub Kicinski if (ops->newlink) 344002839cc8SJakub Kicinski err = ops->newlink(link_net ? : net, dev, tb, data, extack); 344102839cc8SJakub Kicinski else 344202839cc8SJakub Kicinski err = register_netdevice(dev); 344302839cc8SJakub Kicinski if (err < 0) { 344402839cc8SJakub Kicinski free_netdev(dev); 344502839cc8SJakub Kicinski goto out; 344602839cc8SJakub Kicinski } 344702839cc8SJakub Kicinski 3448d88e136cSHangbin Liu err = rtnl_configure_link(dev, ifm, portid, nlh); 344902839cc8SJakub Kicinski if (err < 0) 345002839cc8SJakub Kicinski goto out_unregister; 345102839cc8SJakub Kicinski if (link_net) { 345202839cc8SJakub Kicinski err = dev_change_net_namespace(dev, dest_net, ifname); 345302839cc8SJakub Kicinski if (err < 0) 345402839cc8SJakub Kicinski goto out_unregister; 345502839cc8SJakub Kicinski } 345602839cc8SJakub Kicinski if (tb[IFLA_MASTER]) { 345702839cc8SJakub Kicinski err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack); 345802839cc8SJakub Kicinski if (err) 345902839cc8SJakub Kicinski goto out_unregister; 346002839cc8SJakub Kicinski } 346102839cc8SJakub Kicinski out: 346202839cc8SJakub Kicinski if (link_net) 346302839cc8SJakub Kicinski put_net(link_net); 346402839cc8SJakub Kicinski put_net(dest_net); 346502839cc8SJakub Kicinski return err; 346602839cc8SJakub Kicinski out_unregister: 346702839cc8SJakub Kicinski if (ops->newlink) { 346802839cc8SJakub Kicinski LIST_HEAD(list_kill); 346902839cc8SJakub Kicinski 347002839cc8SJakub Kicinski ops->dellink(dev, &list_kill); 347102839cc8SJakub Kicinski unregister_netdevice_many(&list_kill); 347202839cc8SJakub Kicinski } else { 347302839cc8SJakub Kicinski unregister_netdevice(dev); 347402839cc8SJakub Kicinski } 347502839cc8SJakub Kicinski goto out; 347602839cc8SJakub Kicinski } 347763105e83SJakub Kicinski 3478c92bf26cSJakub Kicinski struct rtnl_newlink_tbs { 3479c92bf26cSJakub Kicinski struct nlattr *tb[IFLA_MAX + 1]; 3480c92bf26cSJakub Kicinski struct nlattr *attr[RTNL_MAX_TYPE + 1]; 3481420d0318SJakub Kicinski struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1]; 3482c92bf26cSJakub Kicinski }; 3483c92bf26cSJakub Kicinski 3484c92bf26cSJakub Kicinski static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, 3485c92bf26cSJakub Kicinski struct rtnl_newlink_tbs *tbs, 3486c92bf26cSJakub Kicinski struct netlink_ext_ack *extack) 3487c92bf26cSJakub Kicinski { 3488420d0318SJakub Kicinski struct nlattr *linkinfo[IFLA_INFO_MAX + 1]; 3489c92bf26cSJakub Kicinski struct nlattr ** const tb = tbs->tb; 3490c6f6f244SEric Dumazet const struct rtnl_link_ops *m_ops; 3491c6f6f244SEric Dumazet struct net_device *master_dev; 34923b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 349338f7b870SPatrick McHardy const struct rtnl_link_ops *ops; 3494420d0318SJakub Kicinski struct nlattr **slave_data; 3495420d0318SJakub Kicinski char kind[MODULE_NAME_LEN]; 3496420d0318SJakub Kicinski struct net_device *dev; 3497420d0318SJakub Kicinski struct ifinfomsg *ifm; 3498420d0318SJakub Kicinski struct nlattr **data; 3499ef2a7c90SFlorent Fourcot bool link_specified; 350038f7b870SPatrick McHardy int err; 350138f7b870SPatrick McHardy 350295a5afcaSJohannes Berg #ifdef CONFIG_MODULES 350338f7b870SPatrick McHardy replay: 35048072f085SThomas Graf #endif 35058cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX, 35068cb08174SJohannes Berg ifla_policy, extack); 350738f7b870SPatrick McHardy if (err < 0) 350838f7b870SPatrick McHardy return err; 350938f7b870SPatrick McHardy 35104ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, false); 35114ff66caeSChristian Brauner if (err < 0) 35124ff66caeSChristian Brauner return err; 35134ff66caeSChristian Brauner 351438f7b870SPatrick McHardy ifm = nlmsg_data(nlh); 3515ef2a7c90SFlorent Fourcot if (ifm->ifi_index > 0) { 3516ef2a7c90SFlorent Fourcot link_specified = true; 3517881d966bSEric W. Biederman dev = __dev_get_by_index(net, ifm->ifi_index); 3518ef2a7c90SFlorent Fourcot } else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) { 3519ef2a7c90SFlorent Fourcot link_specified = true; 35205ea08b52SFlorent Fourcot dev = rtnl_dev_get(net, tb); 3521ef2a7c90SFlorent Fourcot } else { 3522ef2a7c90SFlorent Fourcot link_specified = false; 352338f7b870SPatrick McHardy dev = NULL; 3524ef2a7c90SFlorent Fourcot } 352538f7b870SPatrick McHardy 3526c6f6f244SEric Dumazet master_dev = NULL; 3527c6f6f244SEric Dumazet m_ops = NULL; 3528ba7d49b1SJiri Pirko if (dev) { 3529ba7d49b1SJiri Pirko master_dev = netdev_master_upper_dev_get(dev); 3530ba7d49b1SJiri Pirko if (master_dev) 3531ba7d49b1SJiri Pirko m_ops = master_dev->rtnl_link_ops; 3532ba7d49b1SJiri Pirko } 3533ba7d49b1SJiri Pirko 35348679c31eSRocco Yue err = validate_linkmsg(dev, tb, extack); 3535e0d087afSEric Dumazet if (err < 0) 35361840bb13SThomas Graf return err; 35371840bb13SThomas Graf 353838f7b870SPatrick McHardy if (tb[IFLA_LINKINFO]) { 35398cb08174SJohannes Berg err = nla_parse_nested_deprecated(linkinfo, IFLA_INFO_MAX, 35408cb08174SJohannes Berg tb[IFLA_LINKINFO], 35418cb08174SJohannes Berg ifla_info_policy, NULL); 354238f7b870SPatrick McHardy if (err < 0) 354338f7b870SPatrick McHardy return err; 354438f7b870SPatrick McHardy } else 354538f7b870SPatrick McHardy memset(linkinfo, 0, sizeof(linkinfo)); 354638f7b870SPatrick McHardy 354738f7b870SPatrick McHardy if (linkinfo[IFLA_INFO_KIND]) { 3548872f6903SFrancis Laniel nla_strscpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind)); 354938f7b870SPatrick McHardy ops = rtnl_link_ops_get(kind); 355038f7b870SPatrick McHardy } else { 355138f7b870SPatrick McHardy kind[0] = '\0'; 355238f7b870SPatrick McHardy ops = NULL; 355338f7b870SPatrick McHardy } 355438f7b870SPatrick McHardy 3555420d0318SJakub Kicinski data = NULL; 355638f7b870SPatrick McHardy if (ops) { 3557ccf8dbcdSKees Cook if (ops->maxtype > RTNL_MAX_TYPE) 3558ccf8dbcdSKees Cook return -EINVAL; 3559ccf8dbcdSKees Cook 356038f7b870SPatrick McHardy if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) { 3561c92bf26cSJakub Kicinski err = nla_parse_nested_deprecated(tbs->attr, ops->maxtype, 356238f7b870SPatrick McHardy linkinfo[IFLA_INFO_DATA], 356368d57f3bSDavid Ahern ops->policy, extack); 356438f7b870SPatrick McHardy if (err < 0) 356538f7b870SPatrick McHardy return err; 3566c92bf26cSJakub Kicinski data = tbs->attr; 356738f7b870SPatrick McHardy } 356838f7b870SPatrick McHardy if (ops->validate) { 3569a8b8a889SMatthias Schiffer err = ops->validate(tb, data, extack); 357038f7b870SPatrick McHardy if (err < 0) 357138f7b870SPatrick McHardy return err; 357238f7b870SPatrick McHardy } 357338f7b870SPatrick McHardy } 357438f7b870SPatrick McHardy 3575420d0318SJakub Kicinski slave_data = NULL; 3576ba7d49b1SJiri Pirko if (m_ops) { 3577af066ed3SPrashant Bhole if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE) 3578ccf8dbcdSKees Cook return -EINVAL; 3579ccf8dbcdSKees Cook 3580ba7d49b1SJiri Pirko if (m_ops->slave_maxtype && 3581ba7d49b1SJiri Pirko linkinfo[IFLA_INFO_SLAVE_DATA]) { 3582c92bf26cSJakub Kicinski err = nla_parse_nested_deprecated(tbs->slave_attr, 35838cb08174SJohannes Berg m_ops->slave_maxtype, 3584ba7d49b1SJiri Pirko linkinfo[IFLA_INFO_SLAVE_DATA], 35858cb08174SJohannes Berg m_ops->slave_policy, 35868cb08174SJohannes Berg extack); 3587ba7d49b1SJiri Pirko if (err < 0) 3588ba7d49b1SJiri Pirko return err; 3589c92bf26cSJakub Kicinski slave_data = tbs->slave_attr; 3590ba7d49b1SJiri Pirko } 3591ba7d49b1SJiri Pirko } 3592ba7d49b1SJiri Pirko 359338f7b870SPatrick McHardy if (dev) { 359490c325e3SNicolas Dichtel int status = 0; 359538f7b870SPatrick McHardy 359638f7b870SPatrick McHardy if (nlh->nlmsg_flags & NLM_F_EXCL) 359738f7b870SPatrick McHardy return -EEXIST; 359838f7b870SPatrick McHardy if (nlh->nlmsg_flags & NLM_F_REPLACE) 359938f7b870SPatrick McHardy return -EOPNOTSUPP; 360038f7b870SPatrick McHardy 360138f7b870SPatrick McHardy if (linkinfo[IFLA_INFO_DATA]) { 360238f7b870SPatrick McHardy if (!ops || ops != dev->rtnl_link_ops || 360338f7b870SPatrick McHardy !ops->changelink) 360438f7b870SPatrick McHardy return -EOPNOTSUPP; 360538f7b870SPatrick McHardy 3606ad744b22SMatthias Schiffer err = ops->changelink(dev, tb, data, extack); 360738f7b870SPatrick McHardy if (err < 0) 360838f7b870SPatrick McHardy return err; 3609ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 361038f7b870SPatrick McHardy } 361138f7b870SPatrick McHardy 3612ba7d49b1SJiri Pirko if (linkinfo[IFLA_INFO_SLAVE_DATA]) { 3613ba7d49b1SJiri Pirko if (!m_ops || !m_ops->slave_changelink) 3614ba7d49b1SJiri Pirko return -EOPNOTSUPP; 3615ba7d49b1SJiri Pirko 3616420d0318SJakub Kicinski err = m_ops->slave_changelink(master_dev, dev, tb, 3617420d0318SJakub Kicinski slave_data, extack); 3618ba7d49b1SJiri Pirko if (err < 0) 3619ba7d49b1SJiri Pirko return err; 3620ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 3621ba7d49b1SJiri Pirko } 3622ba7d49b1SJiri Pirko 36235ea08b52SFlorent Fourcot return do_setlink(skb, dev, ifm, extack, tb, status); 362438f7b870SPatrick McHardy } 362538f7b870SPatrick McHardy 3626ffa934f1SPatrick McHardy if (!(nlh->nlmsg_flags & NLM_F_CREATE)) { 3627ef2a7c90SFlorent Fourcot /* No dev found and NLM_F_CREATE not set. Requested dev does not exist, 3628ef2a7c90SFlorent Fourcot * or it's for a group 3629ef2a7c90SFlorent Fourcot */ 3630ef2a7c90SFlorent Fourcot if (link_specified) 3631ef2a7c90SFlorent Fourcot return -ENODEV; 3632ef2a7c90SFlorent Fourcot if (tb[IFLA_GROUP]) 363390f62cf3SEric W. Biederman return rtnl_group_changelink(skb, net, 3634ffa934f1SPatrick McHardy nla_get_u32(tb[IFLA_GROUP]), 3635ddf9f970SJakub Kicinski ifm, extack, tb); 36366f37c9f9SFlorent Fourcot return -ENODEV; 3637ffa934f1SPatrick McHardy } 363838f7b870SPatrick McHardy 3639160ca014STheuns Verwoerd if (tb[IFLA_MAP] || tb[IFLA_PROTINFO]) 364038f7b870SPatrick McHardy return -EOPNOTSUPP; 364138f7b870SPatrick McHardy 364238f7b870SPatrick McHardy if (!ops) { 364395a5afcaSJohannes Berg #ifdef CONFIG_MODULES 364438f7b870SPatrick McHardy if (kind[0]) { 364538f7b870SPatrick McHardy __rtnl_unlock(); 364638f7b870SPatrick McHardy request_module("rtnl-link-%s", kind); 364738f7b870SPatrick McHardy rtnl_lock(); 364838f7b870SPatrick McHardy ops = rtnl_link_ops_get(kind); 364938f7b870SPatrick McHardy if (ops) 365038f7b870SPatrick McHardy goto replay; 365138f7b870SPatrick McHardy } 365238f7b870SPatrick McHardy #endif 365368d57f3bSDavid Ahern NL_SET_ERR_MSG(extack, "Unknown device type"); 365438f7b870SPatrick McHardy return -EOPNOTSUPP; 365538f7b870SPatrick McHardy } 365638f7b870SPatrick McHardy 3657d88e136cSHangbin Liu return rtnl_newlink_create(skb, ifm, ops, nlh, tb, data, extack); 365863105e83SJakub Kicinski } 365963105e83SJakub Kicinski 3660a2939745SJakub Kicinski static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, 3661a2939745SJakub Kicinski struct netlink_ext_ack *extack) 3662a2939745SJakub Kicinski { 3663c92bf26cSJakub Kicinski struct rtnl_newlink_tbs *tbs; 3664a2939745SJakub Kicinski int ret; 3665a2939745SJakub Kicinski 3666c92bf26cSJakub Kicinski tbs = kmalloc(sizeof(*tbs), GFP_KERNEL); 3667c92bf26cSJakub Kicinski if (!tbs) 3668a2939745SJakub Kicinski return -ENOMEM; 3669a2939745SJakub Kicinski 3670c92bf26cSJakub Kicinski ret = __rtnl_newlink(skb, nlh, tbs, extack); 3671c92bf26cSJakub Kicinski kfree(tbs); 3672a2939745SJakub Kicinski return ret; 3673a2939745SJakub Kicinski } 3674a2939745SJakub Kicinski 36759b3757b0SJakub Kicinski static int rtnl_valid_getlink_req(struct sk_buff *skb, 36769b3757b0SJakub Kicinski const struct nlmsghdr *nlh, 36779b3757b0SJakub Kicinski struct nlattr **tb, 36789b3757b0SJakub Kicinski struct netlink_ext_ack *extack) 36799b3757b0SJakub Kicinski { 36809b3757b0SJakub Kicinski struct ifinfomsg *ifm; 36819b3757b0SJakub Kicinski int i, err; 36829b3757b0SJakub Kicinski 36839b3757b0SJakub Kicinski if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { 36849b3757b0SJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid header for get link"); 36859b3757b0SJakub Kicinski return -EINVAL; 36869b3757b0SJakub Kicinski } 36879b3757b0SJakub Kicinski 36889b3757b0SJakub Kicinski if (!netlink_strict_get_check(skb)) 36898cb08174SJohannes Berg return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX, 36908cb08174SJohannes Berg ifla_policy, extack); 36919b3757b0SJakub Kicinski 36929b3757b0SJakub Kicinski ifm = nlmsg_data(nlh); 36939b3757b0SJakub Kicinski if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags || 36949b3757b0SJakub Kicinski ifm->ifi_change) { 36959b3757b0SJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid values in header for get link request"); 36969b3757b0SJakub Kicinski return -EINVAL; 36979b3757b0SJakub Kicinski } 36989b3757b0SJakub Kicinski 36998cb08174SJohannes Berg err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFLA_MAX, 37008cb08174SJohannes Berg ifla_policy, extack); 37019b3757b0SJakub Kicinski if (err) 37029b3757b0SJakub Kicinski return err; 37039b3757b0SJakub Kicinski 37049b3757b0SJakub Kicinski for (i = 0; i <= IFLA_MAX; i++) { 37059b3757b0SJakub Kicinski if (!tb[i]) 37069b3757b0SJakub Kicinski continue; 37079b3757b0SJakub Kicinski 37089b3757b0SJakub Kicinski switch (i) { 37099b3757b0SJakub Kicinski case IFLA_IFNAME: 371076c9ac0eSJiri Pirko case IFLA_ALT_IFNAME: 37119b3757b0SJakub Kicinski case IFLA_EXT_MASK: 37129b3757b0SJakub Kicinski case IFLA_TARGET_NETNSID: 37139b3757b0SJakub Kicinski break; 37149b3757b0SJakub Kicinski default: 37159b3757b0SJakub Kicinski NL_SET_ERR_MSG(extack, "Unsupported attribute in get link request"); 37169b3757b0SJakub Kicinski return -EINVAL; 37179b3757b0SJakub Kicinski } 37189b3757b0SJakub Kicinski } 37199b3757b0SJakub Kicinski 37209b3757b0SJakub Kicinski return 0; 37219b3757b0SJakub Kicinski } 37229b3757b0SJakub Kicinski 3723c21ef3e3SDavid Ahern static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh, 3724c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 3725711e2c33SJean Tourrilhes { 37263b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 372779e1ad14SJiri Benc struct net *tgt_net = net; 3728b60c5115SThomas Graf struct ifinfomsg *ifm; 3729b60c5115SThomas Graf struct nlattr *tb[IFLA_MAX+1]; 3730b60c5115SThomas Graf struct net_device *dev = NULL; 3731b60c5115SThomas Graf struct sk_buff *nskb; 373279e1ad14SJiri Benc int netnsid = -1; 3733339bf98fSThomas Graf int err; 3734115c9b81SGreg Rose u32 ext_filter_mask = 0; 3735711e2c33SJean Tourrilhes 37369b3757b0SJakub Kicinski err = rtnl_valid_getlink_req(skb, nlh, tb, extack); 3737b60c5115SThomas Graf if (err < 0) 37389918f230SEric Sesterhenn return err; 3739b60c5115SThomas Graf 37404ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, true); 37414ff66caeSChristian Brauner if (err < 0) 37424ff66caeSChristian Brauner return err; 37434ff66caeSChristian Brauner 37447e4a8d5aSChristian Brauner if (tb[IFLA_TARGET_NETNSID]) { 37457e4a8d5aSChristian Brauner netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]); 3746c383edc4SChristian Brauner tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid); 374779e1ad14SJiri Benc if (IS_ERR(tgt_net)) 374879e1ad14SJiri Benc return PTR_ERR(tgt_net); 374979e1ad14SJiri Benc } 375079e1ad14SJiri Benc 3751115c9b81SGreg Rose if (tb[IFLA_EXT_MASK]) 3752115c9b81SGreg Rose ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 3753115c9b81SGreg Rose 375479e1ad14SJiri Benc err = -EINVAL; 3755b60c5115SThomas Graf ifm = nlmsg_data(nlh); 3756a3d12891SEric Dumazet if (ifm->ifi_index > 0) 375779e1ad14SJiri Benc dev = __dev_get_by_index(tgt_net, ifm->ifi_index); 375876c9ac0eSJiri Pirko else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) 37595ea08b52SFlorent Fourcot dev = rtnl_dev_get(tgt_net, tb); 3760a3d12891SEric Dumazet else 376179e1ad14SJiri Benc goto out; 3762b60c5115SThomas Graf 376379e1ad14SJiri Benc err = -ENODEV; 3764a3d12891SEric Dumazet if (dev == NULL) 376579e1ad14SJiri Benc goto out; 3766a3d12891SEric Dumazet 376779e1ad14SJiri Benc err = -ENOBUFS; 3768115c9b81SGreg Rose nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL); 3769a3d12891SEric Dumazet if (nskb == NULL) 377079e1ad14SJiri Benc goto out; 3771711e2c33SJean Tourrilhes 377279e1ad14SJiri Benc err = rtnl_fill_ifinfo(nskb, dev, net, 377379e1ad14SJiri Benc RTM_NEWLINK, NETLINK_CB(skb).portid, 377479e1ad14SJiri Benc nlh->nlmsg_seq, 0, 0, ext_filter_mask, 3775d4e4fdf9SGuillaume Nault 0, NULL, 0, netnsid, GFP_KERNEL); 377626932566SPatrick McHardy if (err < 0) { 377726932566SPatrick McHardy /* -EMSGSIZE implies BUG in if_nlmsg_size */ 377826932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 377926932566SPatrick McHardy kfree_skb(nskb); 3780a3d12891SEric Dumazet } else 378115e47304SEric W. Biederman err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); 378279e1ad14SJiri Benc out: 378379e1ad14SJiri Benc if (netnsid >= 0) 378479e1ad14SJiri Benc put_net(tgt_net); 3785b60c5115SThomas Graf 3786711e2c33SJean Tourrilhes return err; 3787711e2c33SJean Tourrilhes } 3788711e2c33SJean Tourrilhes 378936fbf1e5SJiri Pirko static int rtnl_alt_ifname(int cmd, struct net_device *dev, struct nlattr *attr, 379036fbf1e5SJiri Pirko bool *changed, struct netlink_ext_ack *extack) 379136fbf1e5SJiri Pirko { 379236fbf1e5SJiri Pirko char *alt_ifname; 3793155fb43bSJakub Kicinski size_t size; 379436fbf1e5SJiri Pirko int err; 379536fbf1e5SJiri Pirko 379636fbf1e5SJiri Pirko err = nla_validate(attr, attr->nla_len, IFLA_MAX, ifla_policy, extack); 379736fbf1e5SJiri Pirko if (err) 379836fbf1e5SJiri Pirko return err; 379936fbf1e5SJiri Pirko 3800155fb43bSJakub Kicinski if (cmd == RTM_NEWLINKPROP) { 3801155fb43bSJakub Kicinski size = rtnl_prop_list_size(dev); 3802155fb43bSJakub Kicinski size += nla_total_size(ALTIFNAMSIZ); 3803155fb43bSJakub Kicinski if (size >= U16_MAX) { 3804155fb43bSJakub Kicinski NL_SET_ERR_MSG(extack, 3805155fb43bSJakub Kicinski "effective property list too long"); 3806155fb43bSJakub Kicinski return -EINVAL; 3807155fb43bSJakub Kicinski } 3808155fb43bSJakub Kicinski } 3809155fb43bSJakub Kicinski 38105d26cff5SJakub Kicinski alt_ifname = nla_strdup(attr, GFP_KERNEL_ACCOUNT); 381136fbf1e5SJiri Pirko if (!alt_ifname) 381236fbf1e5SJiri Pirko return -ENOMEM; 381344bfa9c5SEric Dumazet 381444bfa9c5SEric Dumazet if (cmd == RTM_NEWLINKPROP) { 381536fbf1e5SJiri Pirko err = netdev_name_node_alt_create(dev, alt_ifname); 381644bfa9c5SEric Dumazet if (!err) 381744bfa9c5SEric Dumazet alt_ifname = NULL; 381836fbf1e5SJiri Pirko } else if (cmd == RTM_DELLINKPROP) { 381936fbf1e5SJiri Pirko err = netdev_name_node_alt_destroy(dev, alt_ifname); 382036fbf1e5SJiri Pirko } else { 382144bfa9c5SEric Dumazet WARN_ON_ONCE(1); 382244bfa9c5SEric Dumazet err = -EINVAL; 382336fbf1e5SJiri Pirko } 382436fbf1e5SJiri Pirko 382544bfa9c5SEric Dumazet kfree(alt_ifname); 382644bfa9c5SEric Dumazet if (!err) 382736fbf1e5SJiri Pirko *changed = true; 382844bfa9c5SEric Dumazet return err; 382936fbf1e5SJiri Pirko } 383036fbf1e5SJiri Pirko 383136fbf1e5SJiri Pirko static int rtnl_linkprop(int cmd, struct sk_buff *skb, struct nlmsghdr *nlh, 383236fbf1e5SJiri Pirko struct netlink_ext_ack *extack) 383336fbf1e5SJiri Pirko { 383436fbf1e5SJiri Pirko struct net *net = sock_net(skb->sk); 383536fbf1e5SJiri Pirko struct nlattr *tb[IFLA_MAX + 1]; 383636fbf1e5SJiri Pirko struct net_device *dev; 383736fbf1e5SJiri Pirko struct ifinfomsg *ifm; 383836fbf1e5SJiri Pirko bool changed = false; 383936fbf1e5SJiri Pirko struct nlattr *attr; 384036fbf1e5SJiri Pirko int err, rem; 384136fbf1e5SJiri Pirko 384236fbf1e5SJiri Pirko err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack); 384336fbf1e5SJiri Pirko if (err) 384436fbf1e5SJiri Pirko return err; 384536fbf1e5SJiri Pirko 384636fbf1e5SJiri Pirko err = rtnl_ensure_unique_netns(tb, extack, true); 384736fbf1e5SJiri Pirko if (err) 384836fbf1e5SJiri Pirko return err; 384936fbf1e5SJiri Pirko 385036fbf1e5SJiri Pirko ifm = nlmsg_data(nlh); 3851cc6090e9SJiri Pirko if (ifm->ifi_index > 0) 385236fbf1e5SJiri Pirko dev = __dev_get_by_index(net, ifm->ifi_index); 385376c9ac0eSJiri Pirko else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) 38545ea08b52SFlorent Fourcot dev = rtnl_dev_get(net, tb); 3855cc6090e9SJiri Pirko else 385636fbf1e5SJiri Pirko return -EINVAL; 385736fbf1e5SJiri Pirko 385836fbf1e5SJiri Pirko if (!dev) 385936fbf1e5SJiri Pirko return -ENODEV; 386036fbf1e5SJiri Pirko 386136fbf1e5SJiri Pirko if (!tb[IFLA_PROP_LIST]) 386236fbf1e5SJiri Pirko return 0; 386336fbf1e5SJiri Pirko 386436fbf1e5SJiri Pirko nla_for_each_nested(attr, tb[IFLA_PROP_LIST], rem) { 386536fbf1e5SJiri Pirko switch (nla_type(attr)) { 386636fbf1e5SJiri Pirko case IFLA_ALT_IFNAME: 386736fbf1e5SJiri Pirko err = rtnl_alt_ifname(cmd, dev, attr, &changed, extack); 386836fbf1e5SJiri Pirko if (err) 386936fbf1e5SJiri Pirko return err; 387036fbf1e5SJiri Pirko break; 387136fbf1e5SJiri Pirko } 387236fbf1e5SJiri Pirko } 387336fbf1e5SJiri Pirko 387436fbf1e5SJiri Pirko if (changed) 387536fbf1e5SJiri Pirko netdev_state_change(dev); 387636fbf1e5SJiri Pirko return 0; 387736fbf1e5SJiri Pirko } 387836fbf1e5SJiri Pirko 387936fbf1e5SJiri Pirko static int rtnl_newlinkprop(struct sk_buff *skb, struct nlmsghdr *nlh, 388036fbf1e5SJiri Pirko struct netlink_ext_ack *extack) 388136fbf1e5SJiri Pirko { 388236fbf1e5SJiri Pirko return rtnl_linkprop(RTM_NEWLINKPROP, skb, nlh, extack); 388336fbf1e5SJiri Pirko } 388436fbf1e5SJiri Pirko 388536fbf1e5SJiri Pirko static int rtnl_dellinkprop(struct sk_buff *skb, struct nlmsghdr *nlh, 388636fbf1e5SJiri Pirko struct netlink_ext_ack *extack) 388736fbf1e5SJiri Pirko { 388836fbf1e5SJiri Pirko return rtnl_linkprop(RTM_DELLINKPROP, skb, nlh, extack); 388936fbf1e5SJiri Pirko } 389036fbf1e5SJiri Pirko 3891ebfe3c51SDi Zhu static u32 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh) 3892c7ac8679SGreg Rose { 3893115c9b81SGreg Rose struct net *net = sock_net(skb->sk); 3894ebfe3c51SDi Zhu size_t min_ifinfo_dump_size = 0; 3895115c9b81SGreg Rose struct nlattr *tb[IFLA_MAX+1]; 3896115c9b81SGreg Rose u32 ext_filter_mask = 0; 3897ebfe3c51SDi Zhu struct net_device *dev; 3898e5eca6d4SMichal Schmidt int hdrlen; 3899115c9b81SGreg Rose 3900e5eca6d4SMichal Schmidt /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */ 3901e5eca6d4SMichal Schmidt hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ? 3902e5eca6d4SMichal Schmidt sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); 3903e5eca6d4SMichal Schmidt 39048cb08174SJohannes Berg if (nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) { 3905115c9b81SGreg Rose if (tb[IFLA_EXT_MASK]) 3906115c9b81SGreg Rose ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 3907a4b64fbeSEric Dumazet } 3908115c9b81SGreg Rose 3909115c9b81SGreg Rose if (!ext_filter_mask) 3910115c9b81SGreg Rose return NLMSG_GOODSIZE; 3911115c9b81SGreg Rose /* 3912115c9b81SGreg Rose * traverse the list of net devices and compute the minimum 3913115c9b81SGreg Rose * buffer size based upon the filter mask. 3914115c9b81SGreg Rose */ 39156853dd48SFlorian Westphal rcu_read_lock(); 39166853dd48SFlorian Westphal for_each_netdev_rcu(net, dev) { 3917ebfe3c51SDi Zhu min_ifinfo_dump_size = max(min_ifinfo_dump_size, 3918ebfe3c51SDi Zhu if_nlmsg_size(dev, ext_filter_mask)); 3919115c9b81SGreg Rose } 39206853dd48SFlorian Westphal rcu_read_unlock(); 3921115c9b81SGreg Rose 392293af2056SZhang Shengju return nlmsg_total_size(min_ifinfo_dump_size); 3923c7ac8679SGreg Rose } 3924c7ac8679SGreg Rose 392542bad1daSAdrian Bunk static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb) 39261da177e4SLinus Torvalds { 39271da177e4SLinus Torvalds int idx; 39281da177e4SLinus Torvalds int s_idx = cb->family; 392987ccbb1fSChristian Brauner int type = cb->nlh->nlmsg_type - RTM_BASE; 3930c63586dcSDavid Ahern int ret = 0; 39311da177e4SLinus Torvalds 39321da177e4SLinus Torvalds if (s_idx == 0) 39331da177e4SLinus Torvalds s_idx = 1; 39346853dd48SFlorian Westphal 393525239ceeSPatrick McHardy for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) { 393651e13685SJakub Kicinski struct rtnl_link __rcu **tab; 3937addf9b90SFlorian Westphal struct rtnl_link *link; 39386853dd48SFlorian Westphal rtnl_dumpit_func dumpit; 39396853dd48SFlorian Westphal 39401da177e4SLinus Torvalds if (idx < s_idx || idx == PF_PACKET) 39411da177e4SLinus Torvalds continue; 39426853dd48SFlorian Westphal 3943addf9b90SFlorian Westphal if (type < 0 || type >= RTM_NR_MSGTYPES) 39441da177e4SLinus Torvalds continue; 39456853dd48SFlorian Westphal 3946addf9b90SFlorian Westphal tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]); 3947addf9b90SFlorian Westphal if (!tab) 3948addf9b90SFlorian Westphal continue; 3949addf9b90SFlorian Westphal 395051e13685SJakub Kicinski link = rcu_dereference_rtnl(tab[type]); 3951addf9b90SFlorian Westphal if (!link) 3952addf9b90SFlorian Westphal continue; 3953addf9b90SFlorian Westphal 3954addf9b90SFlorian Westphal dumpit = link->dumpit; 39556853dd48SFlorian Westphal if (!dumpit) 39566853dd48SFlorian Westphal continue; 39576853dd48SFlorian Westphal 39580465277fSNicolas Dichtel if (idx > s_idx) { 39591da177e4SLinus Torvalds memset(&cb->args[0], 0, sizeof(cb->args)); 39600465277fSNicolas Dichtel cb->prev_seq = 0; 39610465277fSNicolas Dichtel cb->seq = 0; 39620465277fSNicolas Dichtel } 3963c63586dcSDavid Ahern ret = dumpit(skb, cb); 39645e1acb4aSAlexey Kodanev if (ret) 39651da177e4SLinus Torvalds break; 39661da177e4SLinus Torvalds } 39671da177e4SLinus Torvalds cb->family = idx; 39681da177e4SLinus Torvalds 3969c63586dcSDavid Ahern return skb->len ? : ret; 39701da177e4SLinus Torvalds } 39711da177e4SLinus Torvalds 3972395eea6cSMahesh Bandewar struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev, 39733d3ea5afSVlad Yasevich unsigned int change, 397438e01b30SNicolas Dichtel u32 event, gfp_t flags, int *new_nsid, 39751d997f10SHangbin Liu int new_ifindex, u32 portid, u32 seq) 39761da177e4SLinus Torvalds { 3977c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 39781da177e4SLinus Torvalds struct sk_buff *skb; 39790ec6d3f4SThomas Graf int err = -ENOBUFS; 39801da177e4SLinus Torvalds 398150af5969Sluo penghao skb = nlmsg_new(if_nlmsg_size(dev, 0), flags); 39820ec6d3f4SThomas Graf if (skb == NULL) 39830ec6d3f4SThomas Graf goto errout; 39841da177e4SLinus Torvalds 398579e1ad14SJiri Benc err = rtnl_fill_ifinfo(skb, dev, dev_net(dev), 39861d997f10SHangbin Liu type, portid, seq, change, 0, 0, event, 3987d4e4fdf9SGuillaume Nault new_nsid, new_ifindex, -1, flags); 398826932566SPatrick McHardy if (err < 0) { 398926932566SPatrick McHardy /* -EMSGSIZE implies BUG in if_nlmsg_size() */ 399026932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 399126932566SPatrick McHardy kfree_skb(skb); 399226932566SPatrick McHardy goto errout; 399326932566SPatrick McHardy } 3994395eea6cSMahesh Bandewar return skb; 39950ec6d3f4SThomas Graf errout: 39960ec6d3f4SThomas Graf if (err < 0) 39974b3da706SEric W. Biederman rtnl_set_sk_err(net, RTNLGRP_LINK, err); 3998395eea6cSMahesh Bandewar return NULL; 3999395eea6cSMahesh Bandewar } 4000395eea6cSMahesh Bandewar 40011d997f10SHangbin Liu void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags, 40021d997f10SHangbin Liu u32 portid, const struct nlmsghdr *nlh) 4003395eea6cSMahesh Bandewar { 4004395eea6cSMahesh Bandewar struct net *net = dev_net(dev); 4005395eea6cSMahesh Bandewar 40061d997f10SHangbin Liu rtnl_notify(skb, net, portid, RTNLGRP_LINK, nlh, flags); 4007395eea6cSMahesh Bandewar } 4008395eea6cSMahesh Bandewar 40093d3ea5afSVlad Yasevich static void rtmsg_ifinfo_event(int type, struct net_device *dev, 40103d3ea5afSVlad Yasevich unsigned int change, u32 event, 40111d997f10SHangbin Liu gfp_t flags, int *new_nsid, int new_ifindex, 40121d997f10SHangbin Liu u32 portid, const struct nlmsghdr *nlh) 4013395eea6cSMahesh Bandewar { 4014395eea6cSMahesh Bandewar struct sk_buff *skb; 4015395eea6cSMahesh Bandewar 4016ed2a80abSNicolas Dichtel if (dev->reg_state != NETREG_REGISTERED) 4017ed2a80abSNicolas Dichtel return; 4018ed2a80abSNicolas Dichtel 401938e01b30SNicolas Dichtel skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid, 40201d997f10SHangbin Liu new_ifindex, portid, nlmsg_seq(nlh)); 4021395eea6cSMahesh Bandewar if (skb) 40221d997f10SHangbin Liu rtmsg_ifinfo_send(skb, dev, flags, portid, nlh); 40231da177e4SLinus Torvalds } 40243d3ea5afSVlad Yasevich 40253d3ea5afSVlad Yasevich void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change, 40261d997f10SHangbin Liu gfp_t flags, u32 portid, const struct nlmsghdr *nlh) 40273d3ea5afSVlad Yasevich { 402838e01b30SNicolas Dichtel rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags, 40291d997f10SHangbin Liu NULL, 0, portid, nlh); 40303d3ea5afSVlad Yasevich } 40311da177e4SLinus Torvalds 40326621dd29SNicolas Dichtel void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change, 403338e01b30SNicolas Dichtel gfp_t flags, int *new_nsid, int new_ifindex) 40346621dd29SNicolas Dichtel { 40356621dd29SNicolas Dichtel rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags, 40361d997f10SHangbin Liu new_nsid, new_ifindex, 0, NULL); 40376621dd29SNicolas Dichtel } 40386621dd29SNicolas Dichtel 4039d83b0603SJohn Fastabend static int nlmsg_populate_fdb_fill(struct sk_buff *skb, 4040d83b0603SJohn Fastabend struct net_device *dev, 40411e53d5bbSHubert Sokolowski u8 *addr, u16 vid, u32 pid, u32 seq, 40421c104a6bSNicolas Dichtel int type, unsigned int flags, 4043b3379041SHubert Sokolowski int nlflags, u16 ndm_state) 4044d83b0603SJohn Fastabend { 4045d83b0603SJohn Fastabend struct nlmsghdr *nlh; 4046d83b0603SJohn Fastabend struct ndmsg *ndm; 4047d83b0603SJohn Fastabend 40481c104a6bSNicolas Dichtel nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags); 4049d83b0603SJohn Fastabend if (!nlh) 4050d83b0603SJohn Fastabend return -EMSGSIZE; 4051d83b0603SJohn Fastabend 4052d83b0603SJohn Fastabend ndm = nlmsg_data(nlh); 4053d83b0603SJohn Fastabend ndm->ndm_family = AF_BRIDGE; 4054d83b0603SJohn Fastabend ndm->ndm_pad1 = 0; 4055d83b0603SJohn Fastabend ndm->ndm_pad2 = 0; 4056d83b0603SJohn Fastabend ndm->ndm_flags = flags; 4057d83b0603SJohn Fastabend ndm->ndm_type = 0; 4058d83b0603SJohn Fastabend ndm->ndm_ifindex = dev->ifindex; 4059b3379041SHubert Sokolowski ndm->ndm_state = ndm_state; 4060d83b0603SJohn Fastabend 4061d83b0603SJohn Fastabend if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr)) 4062d83b0603SJohn Fastabend goto nla_put_failure; 40631e53d5bbSHubert Sokolowski if (vid) 40641e53d5bbSHubert Sokolowski if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid)) 40651e53d5bbSHubert Sokolowski goto nla_put_failure; 4066d83b0603SJohn Fastabend 4067053c095aSJohannes Berg nlmsg_end(skb, nlh); 4068053c095aSJohannes Berg return 0; 4069d83b0603SJohn Fastabend 4070d83b0603SJohn Fastabend nla_put_failure: 4071d83b0603SJohn Fastabend nlmsg_cancel(skb, nlh); 4072d83b0603SJohn Fastabend return -EMSGSIZE; 4073d83b0603SJohn Fastabend } 4074d83b0603SJohn Fastabend 40753ff661c3SJohn Fastabend static inline size_t rtnl_fdb_nlmsg_size(void) 40763ff661c3SJohn Fastabend { 4077f82ef3e1SSabrina Dubroca return NLMSG_ALIGN(sizeof(struct ndmsg)) + 4078f82ef3e1SSabrina Dubroca nla_total_size(ETH_ALEN) + /* NDA_LLADDR */ 4079f82ef3e1SSabrina Dubroca nla_total_size(sizeof(u16)) + /* NDA_VLAN */ 4080f82ef3e1SSabrina Dubroca 0; 40813ff661c3SJohn Fastabend } 40823ff661c3SJohn Fastabend 4083b3379041SHubert Sokolowski static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type, 4084b3379041SHubert Sokolowski u16 ndm_state) 40853ff661c3SJohn Fastabend { 40863ff661c3SJohn Fastabend struct net *net = dev_net(dev); 40873ff661c3SJohn Fastabend struct sk_buff *skb; 40883ff661c3SJohn Fastabend int err = -ENOBUFS; 40893ff661c3SJohn Fastabend 40903ff661c3SJohn Fastabend skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC); 40913ff661c3SJohn Fastabend if (!skb) 40923ff661c3SJohn Fastabend goto errout; 40933ff661c3SJohn Fastabend 40941e53d5bbSHubert Sokolowski err = nlmsg_populate_fdb_fill(skb, dev, addr, vid, 4095b3379041SHubert Sokolowski 0, 0, type, NTF_SELF, 0, ndm_state); 40963ff661c3SJohn Fastabend if (err < 0) { 40973ff661c3SJohn Fastabend kfree_skb(skb); 40983ff661c3SJohn Fastabend goto errout; 40993ff661c3SJohn Fastabend } 41003ff661c3SJohn Fastabend 41013ff661c3SJohn Fastabend rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); 41023ff661c3SJohn Fastabend return; 41033ff661c3SJohn Fastabend errout: 41043ff661c3SJohn Fastabend rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); 41053ff661c3SJohn Fastabend } 41063ff661c3SJohn Fastabend 4107a986967eSBart Van Assche /* 4108090096bfSVlad Yasevich * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry 4109090096bfSVlad Yasevich */ 4110090096bfSVlad Yasevich int ndo_dflt_fdb_add(struct ndmsg *ndm, 4111090096bfSVlad Yasevich struct nlattr *tb[], 4112090096bfSVlad Yasevich struct net_device *dev, 4113f6f6424bSJiri Pirko const unsigned char *addr, u16 vid, 4114090096bfSVlad Yasevich u16 flags) 4115090096bfSVlad Yasevich { 4116090096bfSVlad Yasevich int err = -EINVAL; 4117090096bfSVlad Yasevich 4118090096bfSVlad Yasevich /* If aging addresses are supported device will need to 4119090096bfSVlad Yasevich * implement its own handler for this. 4120090096bfSVlad Yasevich */ 4121090096bfSVlad Yasevich if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) { 412278ecc890SVladimir Oltean netdev_info(dev, "default FDB implementation only supports local addresses\n"); 4123090096bfSVlad Yasevich return err; 4124090096bfSVlad Yasevich } 4125090096bfSVlad Yasevich 4126a35ec8e3SHans J. Schultz if (tb[NDA_FLAGS_EXT]) { 4127a35ec8e3SHans J. Schultz netdev_info(dev, "invalid flags given to default FDB implementation\n"); 4128a35ec8e3SHans J. Schultz return err; 4129a35ec8e3SHans J. Schultz } 4130a35ec8e3SHans J. Schultz 413165891feaSOr Gerlitz if (vid) { 413223ac0b42SVladimir Oltean netdev_info(dev, "vlans aren't supported yet for dev_uc|mc_add()\n"); 413365891feaSOr Gerlitz return err; 413465891feaSOr Gerlitz } 413565891feaSOr Gerlitz 4136090096bfSVlad Yasevich if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) 4137090096bfSVlad Yasevich err = dev_uc_add_excl(dev, addr); 4138090096bfSVlad Yasevich else if (is_multicast_ether_addr(addr)) 4139090096bfSVlad Yasevich err = dev_mc_add_excl(dev, addr); 4140090096bfSVlad Yasevich 4141090096bfSVlad Yasevich /* Only return duplicate errors if NLM_F_EXCL is set */ 4142090096bfSVlad Yasevich if (err == -EEXIST && !(flags & NLM_F_EXCL)) 4143090096bfSVlad Yasevich err = 0; 4144090096bfSVlad Yasevich 4145090096bfSVlad Yasevich return err; 4146090096bfSVlad Yasevich } 4147090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_add); 4148090096bfSVlad Yasevich 4149b88d12e4SFlorian Westphal static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid, 4150b88d12e4SFlorian Westphal struct netlink_ext_ack *extack) 4151f6f6424bSJiri Pirko { 4152f6f6424bSJiri Pirko u16 vid = 0; 4153f6f6424bSJiri Pirko 4154f6f6424bSJiri Pirko if (vlan_attr) { 4155f6f6424bSJiri Pirko if (nla_len(vlan_attr) != sizeof(u16)) { 4156b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid vlan attribute size"); 4157f6f6424bSJiri Pirko return -EINVAL; 4158f6f6424bSJiri Pirko } 4159f6f6424bSJiri Pirko 4160f6f6424bSJiri Pirko vid = nla_get_u16(vlan_attr); 4161f6f6424bSJiri Pirko 4162f6f6424bSJiri Pirko if (!vid || vid >= VLAN_VID_MASK) { 4163b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid vlan id"); 4164f6f6424bSJiri Pirko return -EINVAL; 4165f6f6424bSJiri Pirko } 4166f6f6424bSJiri Pirko } 4167f6f6424bSJiri Pirko *p_vid = vid; 4168f6f6424bSJiri Pirko return 0; 4169f6f6424bSJiri Pirko } 4170f6f6424bSJiri Pirko 4171c21ef3e3SDavid Ahern static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh, 4172c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 417377162022SJohn Fastabend { 417477162022SJohn Fastabend struct net *net = sock_net(skb->sk); 417577162022SJohn Fastabend struct ndmsg *ndm; 417677162022SJohn Fastabend struct nlattr *tb[NDA_MAX+1]; 417777162022SJohn Fastabend struct net_device *dev; 417877162022SJohn Fastabend u8 *addr; 4179f6f6424bSJiri Pirko u16 vid; 418077162022SJohn Fastabend int err; 418177162022SJohn Fastabend 41828cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, 41838cb08174SJohannes Berg extack); 418477162022SJohn Fastabend if (err < 0) 418577162022SJohn Fastabend return err; 418677162022SJohn Fastabend 418777162022SJohn Fastabend ndm = nlmsg_data(nlh); 418877162022SJohn Fastabend if (ndm->ndm_ifindex == 0) { 4189b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid ifindex"); 419077162022SJohn Fastabend return -EINVAL; 419177162022SJohn Fastabend } 419277162022SJohn Fastabend 419377162022SJohn Fastabend dev = __dev_get_by_index(net, ndm->ndm_ifindex); 419477162022SJohn Fastabend if (dev == NULL) { 4195b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 419677162022SJohn Fastabend return -ENODEV; 419777162022SJohn Fastabend } 419877162022SJohn Fastabend 419977162022SJohn Fastabend if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { 4200b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid address"); 420177162022SJohn Fastabend return -EINVAL; 420277162022SJohn Fastabend } 420377162022SJohn Fastabend 4204da715775SIdo Schimmel if (dev->type != ARPHRD_ETHER) { 4205da715775SIdo Schimmel NL_SET_ERR_MSG(extack, "FDB add only supported for Ethernet devices"); 4206da715775SIdo Schimmel return -EINVAL; 4207da715775SIdo Schimmel } 4208da715775SIdo Schimmel 420977162022SJohn Fastabend addr = nla_data(tb[NDA_LLADDR]); 421077162022SJohn Fastabend 4211b88d12e4SFlorian Westphal err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack); 4212f6f6424bSJiri Pirko if (err) 4213f6f6424bSJiri Pirko return err; 4214f6f6424bSJiri Pirko 421577162022SJohn Fastabend err = -EOPNOTSUPP; 421677162022SJohn Fastabend 421777162022SJohn Fastabend /* Support fdb on master device the net/bridge default case */ 421877162022SJohn Fastabend if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && 42192e92a2d0SJulian Wiedmann netif_is_bridge_port(dev)) { 4220898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4221898e5061SJiri Pirko const struct net_device_ops *ops = br_dev->netdev_ops; 4222898e5061SJiri Pirko 4223f6f6424bSJiri Pirko err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid, 422487b0984eSPetr Machata nlh->nlmsg_flags, extack); 422577162022SJohn Fastabend if (err) 422677162022SJohn Fastabend goto out; 422777162022SJohn Fastabend else 422877162022SJohn Fastabend ndm->ndm_flags &= ~NTF_MASTER; 422977162022SJohn Fastabend } 423077162022SJohn Fastabend 423177162022SJohn Fastabend /* Embedded bridge, macvlan, and any other device support */ 4232090096bfSVlad Yasevich if ((ndm->ndm_flags & NTF_SELF)) { 4233090096bfSVlad Yasevich if (dev->netdev_ops->ndo_fdb_add) 4234090096bfSVlad Yasevich err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr, 4235f6f6424bSJiri Pirko vid, 423687b0984eSPetr Machata nlh->nlmsg_flags, 423787b0984eSPetr Machata extack); 4238090096bfSVlad Yasevich else 4239f6f6424bSJiri Pirko err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid, 424077162022SJohn Fastabend nlh->nlmsg_flags); 424177162022SJohn Fastabend 42423ff661c3SJohn Fastabend if (!err) { 4243b3379041SHubert Sokolowski rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH, 4244b3379041SHubert Sokolowski ndm->ndm_state); 424577162022SJohn Fastabend ndm->ndm_flags &= ~NTF_SELF; 424677162022SJohn Fastabend } 42473ff661c3SJohn Fastabend } 424877162022SJohn Fastabend out: 424977162022SJohn Fastabend return err; 425077162022SJohn Fastabend } 425177162022SJohn Fastabend 4252a986967eSBart Van Assche /* 4253090096bfSVlad Yasevich * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry 4254090096bfSVlad Yasevich */ 4255090096bfSVlad Yasevich int ndo_dflt_fdb_del(struct ndmsg *ndm, 4256090096bfSVlad Yasevich struct nlattr *tb[], 4257090096bfSVlad Yasevich struct net_device *dev, 4258f6f6424bSJiri Pirko const unsigned char *addr, u16 vid) 4259090096bfSVlad Yasevich { 4260c8a89c4aSAlexander Duyck int err = -EINVAL; 4261090096bfSVlad Yasevich 4262090096bfSVlad Yasevich /* If aging addresses are supported device will need to 4263090096bfSVlad Yasevich * implement its own handler for this. 4264090096bfSVlad Yasevich */ 426564535993SSridhar Samudrala if (!(ndm->ndm_state & NUD_PERMANENT)) { 426678ecc890SVladimir Oltean netdev_info(dev, "default FDB implementation only supports local addresses\n"); 4267c8a89c4aSAlexander Duyck return err; 4268090096bfSVlad Yasevich } 4269090096bfSVlad Yasevich 4270090096bfSVlad Yasevich if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) 4271090096bfSVlad Yasevich err = dev_uc_del(dev, addr); 4272090096bfSVlad Yasevich else if (is_multicast_ether_addr(addr)) 4273090096bfSVlad Yasevich err = dev_mc_del(dev, addr); 4274090096bfSVlad Yasevich 4275090096bfSVlad Yasevich return err; 4276090096bfSVlad Yasevich } 4277090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_del); 4278090096bfSVlad Yasevich 42799e834259SNikolay Aleksandrov static const struct nla_policy fdb_del_bulk_policy[NDA_MAX + 1] = { 42809e834259SNikolay Aleksandrov [NDA_VLAN] = { .type = NLA_U16 }, 42819e834259SNikolay Aleksandrov [NDA_IFINDEX] = NLA_POLICY_MIN(NLA_S32, 1), 4282ea2c0f9eSNikolay Aleksandrov [NDA_NDM_STATE_MASK] = { .type = NLA_U16 }, 4283ea2c0f9eSNikolay Aleksandrov [NDA_NDM_FLAGS_MASK] = { .type = NLA_U8 }, 42849e834259SNikolay Aleksandrov }; 42859e834259SNikolay Aleksandrov 4286c21ef3e3SDavid Ahern static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh, 4287c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 428877162022SJohn Fastabend { 42899e834259SNikolay Aleksandrov bool del_bulk = !!(nlh->nlmsg_flags & NLM_F_BULK); 429077162022SJohn Fastabend struct net *net = sock_net(skb->sk); 42919e834259SNikolay Aleksandrov const struct net_device_ops *ops; 429277162022SJohn Fastabend struct ndmsg *ndm; 42931690be63SVlad Yasevich struct nlattr *tb[NDA_MAX+1]; 429477162022SJohn Fastabend struct net_device *dev; 42959e834259SNikolay Aleksandrov __u8 *addr = NULL; 42967d311801SColin Ian King int err; 4297f6f6424bSJiri Pirko u16 vid; 429877162022SJohn Fastabend 429990f62cf3SEric W. Biederman if (!netlink_capable(skb, CAP_NET_ADMIN)) 43001690be63SVlad Yasevich return -EPERM; 43011690be63SVlad Yasevich 43029e834259SNikolay Aleksandrov if (!del_bulk) { 43039e834259SNikolay Aleksandrov err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, 43049e834259SNikolay Aleksandrov NULL, extack); 43059e834259SNikolay Aleksandrov } else { 43069e834259SNikolay Aleksandrov err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, 43079e834259SNikolay Aleksandrov fdb_del_bulk_policy, extack); 43089e834259SNikolay Aleksandrov } 43091690be63SVlad Yasevich if (err < 0) 43101690be63SVlad Yasevich return err; 431177162022SJohn Fastabend 431277162022SJohn Fastabend ndm = nlmsg_data(nlh); 431377162022SJohn Fastabend if (ndm->ndm_ifindex == 0) { 4314b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid ifindex"); 431577162022SJohn Fastabend return -EINVAL; 431677162022SJohn Fastabend } 431777162022SJohn Fastabend 431877162022SJohn Fastabend dev = __dev_get_by_index(net, ndm->ndm_ifindex); 431977162022SJohn Fastabend if (dev == NULL) { 4320b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 432177162022SJohn Fastabend return -ENODEV; 432277162022SJohn Fastabend } 432377162022SJohn Fastabend 43249e834259SNikolay Aleksandrov if (!del_bulk) { 43251690be63SVlad Yasevich if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { 4326b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid address"); 432777162022SJohn Fastabend return -EINVAL; 432877162022SJohn Fastabend } 43299e834259SNikolay Aleksandrov addr = nla_data(tb[NDA_LLADDR]); 43309e834259SNikolay Aleksandrov } 433177162022SJohn Fastabend 4332da715775SIdo Schimmel if (dev->type != ARPHRD_ETHER) { 4333da715775SIdo Schimmel NL_SET_ERR_MSG(extack, "FDB delete only supported for Ethernet devices"); 4334da715775SIdo Schimmel return -EINVAL; 4335da715775SIdo Schimmel } 4336da715775SIdo Schimmel 4337b88d12e4SFlorian Westphal err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack); 4338f6f6424bSJiri Pirko if (err) 4339f6f6424bSJiri Pirko return err; 4340f6f6424bSJiri Pirko 434177162022SJohn Fastabend err = -EOPNOTSUPP; 434277162022SJohn Fastabend 434377162022SJohn Fastabend /* Support fdb on master device the net/bridge default case */ 434477162022SJohn Fastabend if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && 43452e92a2d0SJulian Wiedmann netif_is_bridge_port(dev)) { 4346898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 434777162022SJohn Fastabend 43489e834259SNikolay Aleksandrov ops = br_dev->netdev_ops; 43499e834259SNikolay Aleksandrov if (!del_bulk) { 4350898e5061SJiri Pirko if (ops->ndo_fdb_del) 4351ca4567f1SAlaa Mohamed err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid, extack); 43529e834259SNikolay Aleksandrov } else { 43539e834259SNikolay Aleksandrov if (ops->ndo_fdb_del_bulk) 43549e834259SNikolay Aleksandrov err = ops->ndo_fdb_del_bulk(ndm, tb, dev, vid, 43559e834259SNikolay Aleksandrov extack); 43569e834259SNikolay Aleksandrov } 435777162022SJohn Fastabend 435877162022SJohn Fastabend if (err) 435977162022SJohn Fastabend goto out; 436077162022SJohn Fastabend else 436177162022SJohn Fastabend ndm->ndm_flags &= ~NTF_MASTER; 436277162022SJohn Fastabend } 436377162022SJohn Fastabend 436477162022SJohn Fastabend /* Embedded bridge, macvlan, and any other device support */ 4365090096bfSVlad Yasevich if (ndm->ndm_flags & NTF_SELF) { 43669e834259SNikolay Aleksandrov ops = dev->netdev_ops; 43679e834259SNikolay Aleksandrov if (!del_bulk) { 43689e834259SNikolay Aleksandrov if (ops->ndo_fdb_del) 4369ca4567f1SAlaa Mohamed err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid, extack); 4370090096bfSVlad Yasevich else 4371f6f6424bSJiri Pirko err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid); 43729e834259SNikolay Aleksandrov } else { 43739e834259SNikolay Aleksandrov /* in case err was cleared by NTF_MASTER call */ 43749e834259SNikolay Aleksandrov err = -EOPNOTSUPP; 43759e834259SNikolay Aleksandrov if (ops->ndo_fdb_del_bulk) 43769e834259SNikolay Aleksandrov err = ops->ndo_fdb_del_bulk(ndm, tb, dev, vid, 43779e834259SNikolay Aleksandrov extack); 43789e834259SNikolay Aleksandrov } 437977162022SJohn Fastabend 43803ff661c3SJohn Fastabend if (!err) { 43819e834259SNikolay Aleksandrov if (!del_bulk) 4382b3379041SHubert Sokolowski rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH, 4383b3379041SHubert Sokolowski ndm->ndm_state); 438477162022SJohn Fastabend ndm->ndm_flags &= ~NTF_SELF; 438577162022SJohn Fastabend } 43863ff661c3SJohn Fastabend } 438777162022SJohn Fastabend out: 438877162022SJohn Fastabend return err; 438977162022SJohn Fastabend } 439077162022SJohn Fastabend 4391d83b0603SJohn Fastabend static int nlmsg_populate_fdb(struct sk_buff *skb, 4392d83b0603SJohn Fastabend struct netlink_callback *cb, 4393d83b0603SJohn Fastabend struct net_device *dev, 4394d83b0603SJohn Fastabend int *idx, 4395d83b0603SJohn Fastabend struct netdev_hw_addr_list *list) 4396d83b0603SJohn Fastabend { 4397d83b0603SJohn Fastabend struct netdev_hw_addr *ha; 4398d83b0603SJohn Fastabend int err; 439915e47304SEric W. Biederman u32 portid, seq; 4400d83b0603SJohn Fastabend 440115e47304SEric W. Biederman portid = NETLINK_CB(cb->skb).portid; 4402d83b0603SJohn Fastabend seq = cb->nlh->nlmsg_seq; 4403d83b0603SJohn Fastabend 4404d83b0603SJohn Fastabend list_for_each_entry(ha, &list->list, list) { 4405d297653dSRoopa Prabhu if (*idx < cb->args[2]) 4406d83b0603SJohn Fastabend goto skip; 4407d83b0603SJohn Fastabend 44081e53d5bbSHubert Sokolowski err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0, 4409a7a558feSJohn Fastabend portid, seq, 44101c104a6bSNicolas Dichtel RTM_NEWNEIGH, NTF_SELF, 4411b3379041SHubert Sokolowski NLM_F_MULTI, NUD_PERMANENT); 4412d83b0603SJohn Fastabend if (err < 0) 4413d83b0603SJohn Fastabend return err; 4414d83b0603SJohn Fastabend skip: 4415d83b0603SJohn Fastabend *idx += 1; 4416d83b0603SJohn Fastabend } 4417d83b0603SJohn Fastabend return 0; 4418d83b0603SJohn Fastabend } 4419d83b0603SJohn Fastabend 4420d83b0603SJohn Fastabend /** 44212c53040fSBen Hutchings * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table. 4422a986967eSBart Van Assche * @skb: socket buffer to store message in 4423a986967eSBart Van Assche * @cb: netlink callback 4424d83b0603SJohn Fastabend * @dev: netdevice 4425a986967eSBart Van Assche * @filter_dev: ignored 4426a986967eSBart Van Assche * @idx: the number of FDB table entries dumped is added to *@idx 4427d83b0603SJohn Fastabend * 4428d83b0603SJohn Fastabend * Default netdevice operation to dump the existing unicast address list. 442991f3e7b1SJohn Fastabend * Returns number of addresses from list put in skb. 4430d83b0603SJohn Fastabend */ 4431d83b0603SJohn Fastabend int ndo_dflt_fdb_dump(struct sk_buff *skb, 4432d83b0603SJohn Fastabend struct netlink_callback *cb, 4433d83b0603SJohn Fastabend struct net_device *dev, 44345d5eacb3SJamal Hadi Salim struct net_device *filter_dev, 4435d297653dSRoopa Prabhu int *idx) 4436d83b0603SJohn Fastabend { 4437d83b0603SJohn Fastabend int err; 4438d83b0603SJohn Fastabend 443968883893SEric Dumazet if (dev->type != ARPHRD_ETHER) 444068883893SEric Dumazet return -EINVAL; 444168883893SEric Dumazet 4442d83b0603SJohn Fastabend netif_addr_lock_bh(dev); 4443d297653dSRoopa Prabhu err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc); 4444d83b0603SJohn Fastabend if (err) 4445d83b0603SJohn Fastabend goto out; 44462934c9dbSZhang Shengju err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc); 4447d83b0603SJohn Fastabend out: 4448d83b0603SJohn Fastabend netif_addr_unlock_bh(dev); 4449d297653dSRoopa Prabhu return err; 4450d83b0603SJohn Fastabend } 4451d83b0603SJohn Fastabend EXPORT_SYMBOL(ndo_dflt_fdb_dump); 4452d83b0603SJohn Fastabend 44538c6e137fSDavid Ahern static int valid_fdb_dump_strict(const struct nlmsghdr *nlh, 44548c6e137fSDavid Ahern int *br_idx, int *brport_idx, 44558c6e137fSDavid Ahern struct netlink_ext_ack *extack) 44568c6e137fSDavid Ahern { 44578c6e137fSDavid Ahern struct nlattr *tb[NDA_MAX + 1]; 44588c6e137fSDavid Ahern struct ndmsg *ndm; 44598c6e137fSDavid Ahern int err, i; 44608c6e137fSDavid Ahern 44618c6e137fSDavid Ahern if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) { 44628c6e137fSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid header for fdb dump request"); 44638c6e137fSDavid Ahern return -EINVAL; 44648c6e137fSDavid Ahern } 44658c6e137fSDavid Ahern 44668c6e137fSDavid Ahern ndm = nlmsg_data(nlh); 44678c6e137fSDavid Ahern if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state || 44688c6e137fSDavid Ahern ndm->ndm_flags || ndm->ndm_type) { 44698b73018fSNikolay Aleksandrov NL_SET_ERR_MSG(extack, "Invalid values in header for fdb dump request"); 44708c6e137fSDavid Ahern return -EINVAL; 44718c6e137fSDavid Ahern } 44728c6e137fSDavid Ahern 44738cb08174SJohannes Berg err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb, 44748cb08174SJohannes Berg NDA_MAX, NULL, extack); 44758c6e137fSDavid Ahern if (err < 0) 44768c6e137fSDavid Ahern return err; 44778c6e137fSDavid Ahern 44788c6e137fSDavid Ahern *brport_idx = ndm->ndm_ifindex; 44798c6e137fSDavid Ahern for (i = 0; i <= NDA_MAX; ++i) { 44808c6e137fSDavid Ahern if (!tb[i]) 44818c6e137fSDavid Ahern continue; 44828c6e137fSDavid Ahern 44838c6e137fSDavid Ahern switch (i) { 44848c6e137fSDavid Ahern case NDA_IFINDEX: 44858c6e137fSDavid Ahern if (nla_len(tb[i]) != sizeof(u32)) { 44868c6e137fSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid IFINDEX attribute in fdb dump request"); 44878c6e137fSDavid Ahern return -EINVAL; 44888c6e137fSDavid Ahern } 44898c6e137fSDavid Ahern *brport_idx = nla_get_u32(tb[NDA_IFINDEX]); 44908c6e137fSDavid Ahern break; 44918c6e137fSDavid Ahern case NDA_MASTER: 44928c6e137fSDavid Ahern if (nla_len(tb[i]) != sizeof(u32)) { 44938c6e137fSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid MASTER attribute in fdb dump request"); 44948c6e137fSDavid Ahern return -EINVAL; 44958c6e137fSDavid Ahern } 44968c6e137fSDavid Ahern *br_idx = nla_get_u32(tb[NDA_MASTER]); 44978c6e137fSDavid Ahern break; 44988c6e137fSDavid Ahern default: 44998c6e137fSDavid Ahern NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb dump request"); 45008c6e137fSDavid Ahern return -EINVAL; 45018c6e137fSDavid Ahern } 45028c6e137fSDavid Ahern } 45038c6e137fSDavid Ahern 45048c6e137fSDavid Ahern return 0; 45058c6e137fSDavid Ahern } 45068c6e137fSDavid Ahern 45078dfbda19SDavid Ahern static int valid_fdb_dump_legacy(const struct nlmsghdr *nlh, 45088dfbda19SDavid Ahern int *br_idx, int *brport_idx, 45098dfbda19SDavid Ahern struct netlink_ext_ack *extack) 45108dfbda19SDavid Ahern { 45118dfbda19SDavid Ahern struct nlattr *tb[IFLA_MAX+1]; 45128dfbda19SDavid Ahern int err; 45138dfbda19SDavid Ahern 45148dfbda19SDavid Ahern /* A hack to preserve kernel<->userspace interface. 45158dfbda19SDavid Ahern * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0. 45168dfbda19SDavid Ahern * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails. 45178dfbda19SDavid Ahern * So, check for ndmsg with an optional u32 attribute (not used here). 45188dfbda19SDavid Ahern * Fortunately these sizes don't conflict with the size of ifinfomsg 45198dfbda19SDavid Ahern * with an optional attribute. 45208dfbda19SDavid Ahern */ 45218dfbda19SDavid Ahern if (nlmsg_len(nlh) != sizeof(struct ndmsg) && 45228dfbda19SDavid Ahern (nlmsg_len(nlh) != sizeof(struct ndmsg) + 45238dfbda19SDavid Ahern nla_attr_size(sizeof(u32)))) { 45244565d7e5SDavid Ahern struct ifinfomsg *ifm; 45254565d7e5SDavid Ahern 45268cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg), 45278cb08174SJohannes Berg tb, IFLA_MAX, ifla_policy, 45288cb08174SJohannes Berg extack); 45298dfbda19SDavid Ahern if (err < 0) { 45308dfbda19SDavid Ahern return -EINVAL; 45318dfbda19SDavid Ahern } else if (err == 0) { 45328dfbda19SDavid Ahern if (tb[IFLA_MASTER]) 45338dfbda19SDavid Ahern *br_idx = nla_get_u32(tb[IFLA_MASTER]); 45348dfbda19SDavid Ahern } 45358dfbda19SDavid Ahern 45364565d7e5SDavid Ahern ifm = nlmsg_data(nlh); 45378dfbda19SDavid Ahern *brport_idx = ifm->ifi_index; 45388dfbda19SDavid Ahern } 45398dfbda19SDavid Ahern return 0; 45408dfbda19SDavid Ahern } 45418dfbda19SDavid Ahern 454277162022SJohn Fastabend static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb) 454377162022SJohn Fastabend { 454477162022SJohn Fastabend struct net_device *dev; 45455e6d2435SJamal Hadi Salim struct net_device *br_dev = NULL; 45465e6d2435SJamal Hadi Salim const struct net_device_ops *ops = NULL; 45475e6d2435SJamal Hadi Salim const struct net_device_ops *cops = NULL; 45485e6d2435SJamal Hadi Salim struct net *net = sock_net(skb->sk); 4549d297653dSRoopa Prabhu struct hlist_head *head; 45505e6d2435SJamal Hadi Salim int brport_idx = 0; 45515e6d2435SJamal Hadi Salim int br_idx = 0; 4552d297653dSRoopa Prabhu int h, s_h; 4553d297653dSRoopa Prabhu int idx = 0, s_idx; 4554d297653dSRoopa Prabhu int err = 0; 4555d297653dSRoopa Prabhu int fidx = 0; 455677162022SJohn Fastabend 45578c6e137fSDavid Ahern if (cb->strict_check) 45588c6e137fSDavid Ahern err = valid_fdb_dump_strict(cb->nlh, &br_idx, &brport_idx, 45598c6e137fSDavid Ahern cb->extack); 45608c6e137fSDavid Ahern else 45618dfbda19SDavid Ahern err = valid_fdb_dump_legacy(cb->nlh, &br_idx, &brport_idx, 45628dfbda19SDavid Ahern cb->extack); 45638dfbda19SDavid Ahern if (err < 0) 45648dfbda19SDavid Ahern return err; 45655e6d2435SJamal Hadi Salim 45665e6d2435SJamal Hadi Salim if (br_idx) { 45675e6d2435SJamal Hadi Salim br_dev = __dev_get_by_index(net, br_idx); 45685e6d2435SJamal Hadi Salim if (!br_dev) 45695e6d2435SJamal Hadi Salim return -ENODEV; 45705e6d2435SJamal Hadi Salim 4571898e5061SJiri Pirko ops = br_dev->netdev_ops; 45725e6d2435SJamal Hadi Salim } 45735e6d2435SJamal Hadi Salim 4574d297653dSRoopa Prabhu s_h = cb->args[0]; 4575d297653dSRoopa Prabhu s_idx = cb->args[1]; 4576d297653dSRoopa Prabhu 4577d297653dSRoopa Prabhu for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 4578d297653dSRoopa Prabhu idx = 0; 4579d297653dSRoopa Prabhu head = &net->dev_index_head[h]; 4580d297653dSRoopa Prabhu hlist_for_each_entry(dev, head, index_hlist) { 4581d297653dSRoopa Prabhu 45825e6d2435SJamal Hadi Salim if (brport_idx && (dev->ifindex != brport_idx)) 45835e6d2435SJamal Hadi Salim continue; 45845e6d2435SJamal Hadi Salim 45855e6d2435SJamal Hadi Salim if (!br_idx) { /* user did not specify a specific bridge */ 45862e92a2d0SJulian Wiedmann if (netif_is_bridge_port(dev)) { 45875e6d2435SJamal Hadi Salim br_dev = netdev_master_upper_dev_get(dev); 45885e6d2435SJamal Hadi Salim cops = br_dev->netdev_ops; 45895e6d2435SJamal Hadi Salim } 45905e6d2435SJamal Hadi Salim } else { 45915e6d2435SJamal Hadi Salim if (dev != br_dev && 45922e92a2d0SJulian Wiedmann !netif_is_bridge_port(dev)) 45935e6d2435SJamal Hadi Salim continue; 45945e6d2435SJamal Hadi Salim 45955e6d2435SJamal Hadi Salim if (br_dev != netdev_master_upper_dev_get(dev) && 4596254ec036SKyungrok Chung !netif_is_bridge_master(dev)) 45975e6d2435SJamal Hadi Salim continue; 45985e6d2435SJamal Hadi Salim cops = ops; 45995e6d2435SJamal Hadi Salim } 46005e6d2435SJamal Hadi Salim 4601d297653dSRoopa Prabhu if (idx < s_idx) 4602d297653dSRoopa Prabhu goto cont; 4603d297653dSRoopa Prabhu 46042e92a2d0SJulian Wiedmann if (netif_is_bridge_port(dev)) { 4605d297653dSRoopa Prabhu if (cops && cops->ndo_fdb_dump) { 4606d297653dSRoopa Prabhu err = cops->ndo_fdb_dump(skb, cb, 4607d297653dSRoopa Prabhu br_dev, dev, 4608d297653dSRoopa Prabhu &fidx); 4609d297653dSRoopa Prabhu if (err == -EMSGSIZE) 4610d297653dSRoopa Prabhu goto out; 461177162022SJohn Fastabend } 4612d297653dSRoopa Prabhu } 461377162022SJohn Fastabend 46145e6d2435SJamal Hadi Salim if (dev->netdev_ops->ndo_fdb_dump) 4615d297653dSRoopa Prabhu err = dev->netdev_ops->ndo_fdb_dump(skb, cb, 4616d297653dSRoopa Prabhu dev, NULL, 4617d297653dSRoopa Prabhu &fidx); 46186cb69742SHubert Sokolowski else 4619d297653dSRoopa Prabhu err = ndo_dflt_fdb_dump(skb, cb, dev, NULL, 4620d297653dSRoopa Prabhu &fidx); 4621d297653dSRoopa Prabhu if (err == -EMSGSIZE) 4622d297653dSRoopa Prabhu goto out; 46235e6d2435SJamal Hadi Salim 46245e6d2435SJamal Hadi Salim cops = NULL; 4625d297653dSRoopa Prabhu 4626d297653dSRoopa Prabhu /* reset fdb offset to 0 for rest of the interfaces */ 4627d297653dSRoopa Prabhu cb->args[2] = 0; 4628d297653dSRoopa Prabhu fidx = 0; 4629d297653dSRoopa Prabhu cont: 4630d297653dSRoopa Prabhu idx++; 4631d297653dSRoopa Prabhu } 463277162022SJohn Fastabend } 463377162022SJohn Fastabend 4634d297653dSRoopa Prabhu out: 4635d297653dSRoopa Prabhu cb->args[0] = h; 4636d297653dSRoopa Prabhu cb->args[1] = idx; 4637d297653dSRoopa Prabhu cb->args[2] = fidx; 4638d297653dSRoopa Prabhu 463977162022SJohn Fastabend return skb->len; 464077162022SJohn Fastabend } 464177162022SJohn Fastabend 46425b2f94b2SRoopa Prabhu static int valid_fdb_get_strict(const struct nlmsghdr *nlh, 46435b2f94b2SRoopa Prabhu struct nlattr **tb, u8 *ndm_flags, 46445b2f94b2SRoopa Prabhu int *br_idx, int *brport_idx, u8 **addr, 46455b2f94b2SRoopa Prabhu u16 *vid, struct netlink_ext_ack *extack) 46465b2f94b2SRoopa Prabhu { 46475b2f94b2SRoopa Prabhu struct ndmsg *ndm; 46485b2f94b2SRoopa Prabhu int err, i; 46495b2f94b2SRoopa Prabhu 46505b2f94b2SRoopa Prabhu if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) { 46515b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid header for fdb get request"); 46525b2f94b2SRoopa Prabhu return -EINVAL; 46535b2f94b2SRoopa Prabhu } 46545b2f94b2SRoopa Prabhu 46555b2f94b2SRoopa Prabhu ndm = nlmsg_data(nlh); 46565b2f94b2SRoopa Prabhu if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state || 46575b2f94b2SRoopa Prabhu ndm->ndm_type) { 46585b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid values in header for fdb get request"); 46595b2f94b2SRoopa Prabhu return -EINVAL; 46605b2f94b2SRoopa Prabhu } 46615b2f94b2SRoopa Prabhu 46625b2f94b2SRoopa Prabhu if (ndm->ndm_flags & ~(NTF_MASTER | NTF_SELF)) { 46635b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid flags in header for fdb get request"); 46645b2f94b2SRoopa Prabhu return -EINVAL; 46655b2f94b2SRoopa Prabhu } 46665b2f94b2SRoopa Prabhu 46678cb08174SJohannes Berg err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb, 46688cb08174SJohannes Berg NDA_MAX, nda_policy, extack); 46695b2f94b2SRoopa Prabhu if (err < 0) 46705b2f94b2SRoopa Prabhu return err; 46715b2f94b2SRoopa Prabhu 46725b2f94b2SRoopa Prabhu *ndm_flags = ndm->ndm_flags; 46735b2f94b2SRoopa Prabhu *brport_idx = ndm->ndm_ifindex; 46745b2f94b2SRoopa Prabhu for (i = 0; i <= NDA_MAX; ++i) { 46755b2f94b2SRoopa Prabhu if (!tb[i]) 46765b2f94b2SRoopa Prabhu continue; 46775b2f94b2SRoopa Prabhu 46785b2f94b2SRoopa Prabhu switch (i) { 46795b2f94b2SRoopa Prabhu case NDA_MASTER: 46805b2f94b2SRoopa Prabhu *br_idx = nla_get_u32(tb[i]); 46815b2f94b2SRoopa Prabhu break; 46825b2f94b2SRoopa Prabhu case NDA_LLADDR: 46835b2f94b2SRoopa Prabhu if (nla_len(tb[i]) != ETH_ALEN) { 46845b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid address in fdb get request"); 46855b2f94b2SRoopa Prabhu return -EINVAL; 46865b2f94b2SRoopa Prabhu } 46875b2f94b2SRoopa Prabhu *addr = nla_data(tb[i]); 46885b2f94b2SRoopa Prabhu break; 46895b2f94b2SRoopa Prabhu case NDA_VLAN: 46905b2f94b2SRoopa Prabhu err = fdb_vid_parse(tb[i], vid, extack); 46915b2f94b2SRoopa Prabhu if (err) 46925b2f94b2SRoopa Prabhu return err; 46935b2f94b2SRoopa Prabhu break; 46945b2f94b2SRoopa Prabhu case NDA_VNI: 46955b2f94b2SRoopa Prabhu break; 46965b2f94b2SRoopa Prabhu default: 46975b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb get request"); 46985b2f94b2SRoopa Prabhu return -EINVAL; 46995b2f94b2SRoopa Prabhu } 47005b2f94b2SRoopa Prabhu } 47015b2f94b2SRoopa Prabhu 47025b2f94b2SRoopa Prabhu return 0; 47035b2f94b2SRoopa Prabhu } 47045b2f94b2SRoopa Prabhu 47055b2f94b2SRoopa Prabhu static int rtnl_fdb_get(struct sk_buff *in_skb, struct nlmsghdr *nlh, 47065b2f94b2SRoopa Prabhu struct netlink_ext_ack *extack) 47075b2f94b2SRoopa Prabhu { 47085b2f94b2SRoopa Prabhu struct net_device *dev = NULL, *br_dev = NULL; 47095b2f94b2SRoopa Prabhu const struct net_device_ops *ops = NULL; 47105b2f94b2SRoopa Prabhu struct net *net = sock_net(in_skb->sk); 47115b2f94b2SRoopa Prabhu struct nlattr *tb[NDA_MAX + 1]; 47125b2f94b2SRoopa Prabhu struct sk_buff *skb; 47135b2f94b2SRoopa Prabhu int brport_idx = 0; 47145b2f94b2SRoopa Prabhu u8 ndm_flags = 0; 47155b2f94b2SRoopa Prabhu int br_idx = 0; 47165b2f94b2SRoopa Prabhu u8 *addr = NULL; 47175b2f94b2SRoopa Prabhu u16 vid = 0; 47185b2f94b2SRoopa Prabhu int err; 47195b2f94b2SRoopa Prabhu 47205b2f94b2SRoopa Prabhu err = valid_fdb_get_strict(nlh, tb, &ndm_flags, &br_idx, 47215b2f94b2SRoopa Prabhu &brport_idx, &addr, &vid, extack); 47225b2f94b2SRoopa Prabhu if (err < 0) 47235b2f94b2SRoopa Prabhu return err; 47245b2f94b2SRoopa Prabhu 4725f989d03eSNikolay Aleksandrov if (!addr) { 4726f989d03eSNikolay Aleksandrov NL_SET_ERR_MSG(extack, "Missing lookup address for fdb get request"); 4727f989d03eSNikolay Aleksandrov return -EINVAL; 4728f989d03eSNikolay Aleksandrov } 4729f989d03eSNikolay Aleksandrov 47305b2f94b2SRoopa Prabhu if (brport_idx) { 47315b2f94b2SRoopa Prabhu dev = __dev_get_by_index(net, brport_idx); 47325b2f94b2SRoopa Prabhu if (!dev) { 47335b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Unknown device ifindex"); 47345b2f94b2SRoopa Prabhu return -ENODEV; 47355b2f94b2SRoopa Prabhu } 47365b2f94b2SRoopa Prabhu } 47375b2f94b2SRoopa Prabhu 47385b2f94b2SRoopa Prabhu if (br_idx) { 47395b2f94b2SRoopa Prabhu if (dev) { 47405b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Master and device are mutually exclusive"); 47415b2f94b2SRoopa Prabhu return -EINVAL; 47425b2f94b2SRoopa Prabhu } 47435b2f94b2SRoopa Prabhu 47445b2f94b2SRoopa Prabhu br_dev = __dev_get_by_index(net, br_idx); 47455b2f94b2SRoopa Prabhu if (!br_dev) { 47465b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid master ifindex"); 47475b2f94b2SRoopa Prabhu return -EINVAL; 47485b2f94b2SRoopa Prabhu } 47495b2f94b2SRoopa Prabhu ops = br_dev->netdev_ops; 47505b2f94b2SRoopa Prabhu } 47515b2f94b2SRoopa Prabhu 47525b2f94b2SRoopa Prabhu if (dev) { 47535b2f94b2SRoopa Prabhu if (!ndm_flags || (ndm_flags & NTF_MASTER)) { 47542e92a2d0SJulian Wiedmann if (!netif_is_bridge_port(dev)) { 47555b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Device is not a bridge port"); 47565b2f94b2SRoopa Prabhu return -EINVAL; 47575b2f94b2SRoopa Prabhu } 47585b2f94b2SRoopa Prabhu br_dev = netdev_master_upper_dev_get(dev); 47595b2f94b2SRoopa Prabhu if (!br_dev) { 47605b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Master of device not found"); 47615b2f94b2SRoopa Prabhu return -EINVAL; 47625b2f94b2SRoopa Prabhu } 47635b2f94b2SRoopa Prabhu ops = br_dev->netdev_ops; 47645b2f94b2SRoopa Prabhu } else { 47655b2f94b2SRoopa Prabhu if (!(ndm_flags & NTF_SELF)) { 47665b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Missing NTF_SELF"); 47675b2f94b2SRoopa Prabhu return -EINVAL; 47685b2f94b2SRoopa Prabhu } 47695b2f94b2SRoopa Prabhu ops = dev->netdev_ops; 47705b2f94b2SRoopa Prabhu } 47715b2f94b2SRoopa Prabhu } 47725b2f94b2SRoopa Prabhu 47735b2f94b2SRoopa Prabhu if (!br_dev && !dev) { 47745b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "No device specified"); 47755b2f94b2SRoopa Prabhu return -ENODEV; 47765b2f94b2SRoopa Prabhu } 47775b2f94b2SRoopa Prabhu 47785b2f94b2SRoopa Prabhu if (!ops || !ops->ndo_fdb_get) { 47795b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Fdb get operation not supported by device"); 47805b2f94b2SRoopa Prabhu return -EOPNOTSUPP; 47815b2f94b2SRoopa Prabhu } 47825b2f94b2SRoopa Prabhu 47835b2f94b2SRoopa Prabhu skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); 47845b2f94b2SRoopa Prabhu if (!skb) 47855b2f94b2SRoopa Prabhu return -ENOBUFS; 47865b2f94b2SRoopa Prabhu 47875b2f94b2SRoopa Prabhu if (br_dev) 47885b2f94b2SRoopa Prabhu dev = br_dev; 47895b2f94b2SRoopa Prabhu err = ops->ndo_fdb_get(skb, tb, dev, addr, vid, 47905b2f94b2SRoopa Prabhu NETLINK_CB(in_skb).portid, 47915b2f94b2SRoopa Prabhu nlh->nlmsg_seq, extack); 47925b2f94b2SRoopa Prabhu if (err) 47935b2f94b2SRoopa Prabhu goto out; 47945b2f94b2SRoopa Prabhu 47955b2f94b2SRoopa Prabhu return rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 47965b2f94b2SRoopa Prabhu out: 47975b2f94b2SRoopa Prabhu kfree_skb(skb); 47985b2f94b2SRoopa Prabhu return err; 47995b2f94b2SRoopa Prabhu } 48005b2f94b2SRoopa Prabhu 48012c3c031cSScott Feldman static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask, 48022c3c031cSScott Feldman unsigned int attrnum, unsigned int flag) 48032c3c031cSScott Feldman { 48042c3c031cSScott Feldman if (mask & flag) 48052c3c031cSScott Feldman return nla_put_u8(skb, attrnum, !!(flags & flag)); 48062c3c031cSScott Feldman return 0; 48072c3c031cSScott Feldman } 48082c3c031cSScott Feldman 4809815cccbfSJohn Fastabend int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq, 48102c3c031cSScott Feldman struct net_device *dev, u16 mode, 48117d4f8d87SScott Feldman u32 flags, u32 mask, int nlflags, 48127d4f8d87SScott Feldman u32 filter_mask, 48137d4f8d87SScott Feldman int (*vlan_fill)(struct sk_buff *skb, 48147d4f8d87SScott Feldman struct net_device *dev, 48157d4f8d87SScott Feldman u32 filter_mask)) 4816815cccbfSJohn Fastabend { 4817815cccbfSJohn Fastabend struct nlmsghdr *nlh; 4818815cccbfSJohn Fastabend struct ifinfomsg *ifm; 4819815cccbfSJohn Fastabend struct nlattr *br_afspec; 48202c3c031cSScott Feldman struct nlattr *protinfo; 4821815cccbfSJohn Fastabend u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN; 4822898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 48237d4f8d87SScott Feldman int err = 0; 4824815cccbfSJohn Fastabend 482546c264daSNicolas Dichtel nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags); 4826815cccbfSJohn Fastabend if (nlh == NULL) 4827815cccbfSJohn Fastabend return -EMSGSIZE; 4828815cccbfSJohn Fastabend 4829815cccbfSJohn Fastabend ifm = nlmsg_data(nlh); 4830815cccbfSJohn Fastabend ifm->ifi_family = AF_BRIDGE; 4831815cccbfSJohn Fastabend ifm->__ifi_pad = 0; 4832815cccbfSJohn Fastabend ifm->ifi_type = dev->type; 4833815cccbfSJohn Fastabend ifm->ifi_index = dev->ifindex; 4834815cccbfSJohn Fastabend ifm->ifi_flags = dev_get_flags(dev); 4835815cccbfSJohn Fastabend ifm->ifi_change = 0; 4836815cccbfSJohn Fastabend 4837815cccbfSJohn Fastabend 4838815cccbfSJohn Fastabend if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 4839815cccbfSJohn Fastabend nla_put_u32(skb, IFLA_MTU, dev->mtu) || 4840815cccbfSJohn Fastabend nla_put_u8(skb, IFLA_OPERSTATE, operstate) || 4841898e5061SJiri Pirko (br_dev && 4842898e5061SJiri Pirko nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) || 4843815cccbfSJohn Fastabend (dev->addr_len && 4844815cccbfSJohn Fastabend nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || 4845a54acb3aSNicolas Dichtel (dev->ifindex != dev_get_iflink(dev) && 4846a54acb3aSNicolas Dichtel nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev)))) 4847815cccbfSJohn Fastabend goto nla_put_failure; 4848815cccbfSJohn Fastabend 4849ae0be8deSMichal Kubecek br_afspec = nla_nest_start_noflag(skb, IFLA_AF_SPEC); 4850815cccbfSJohn Fastabend if (!br_afspec) 4851815cccbfSJohn Fastabend goto nla_put_failure; 4852815cccbfSJohn Fastabend 48531d460b98SRoopa Prabhu if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) { 4854815cccbfSJohn Fastabend nla_nest_cancel(skb, br_afspec); 4855815cccbfSJohn Fastabend goto nla_put_failure; 4856815cccbfSJohn Fastabend } 48571d460b98SRoopa Prabhu 48581d460b98SRoopa Prabhu if (mode != BRIDGE_MODE_UNDEF) { 48591d460b98SRoopa Prabhu if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) { 48601d460b98SRoopa Prabhu nla_nest_cancel(skb, br_afspec); 48611d460b98SRoopa Prabhu goto nla_put_failure; 48621d460b98SRoopa Prabhu } 48631d460b98SRoopa Prabhu } 48647d4f8d87SScott Feldman if (vlan_fill) { 48657d4f8d87SScott Feldman err = vlan_fill(skb, dev, filter_mask); 48667d4f8d87SScott Feldman if (err) { 48677d4f8d87SScott Feldman nla_nest_cancel(skb, br_afspec); 48687d4f8d87SScott Feldman goto nla_put_failure; 48697d4f8d87SScott Feldman } 48707d4f8d87SScott Feldman } 4871815cccbfSJohn Fastabend nla_nest_end(skb, br_afspec); 4872815cccbfSJohn Fastabend 4873ae0be8deSMichal Kubecek protinfo = nla_nest_start(skb, IFLA_PROTINFO); 48742c3c031cSScott Feldman if (!protinfo) 48752c3c031cSScott Feldman goto nla_put_failure; 48762c3c031cSScott Feldman 48772c3c031cSScott Feldman if (brport_nla_put_flag(skb, flags, mask, 48782c3c031cSScott Feldman IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) || 48792c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 48802c3c031cSScott Feldman IFLA_BRPORT_GUARD, BR_BPDU_GUARD) || 48812c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 48822c3c031cSScott Feldman IFLA_BRPORT_FAST_LEAVE, 48832c3c031cSScott Feldman BR_MULTICAST_FAST_LEAVE) || 48842c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 48852c3c031cSScott Feldman IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) || 48862c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 48872c3c031cSScott Feldman IFLA_BRPORT_LEARNING, BR_LEARNING) || 48882c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 48892c3c031cSScott Feldman IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) || 48902c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 48912c3c031cSScott Feldman IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) || 48922c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 4893583cb0b4SJulian Wiedmann IFLA_BRPORT_PROXYARP, BR_PROXYARP) || 4894583cb0b4SJulian Wiedmann brport_nla_put_flag(skb, flags, mask, 4895583cb0b4SJulian Wiedmann IFLA_BRPORT_MCAST_FLOOD, BR_MCAST_FLOOD) || 4896583cb0b4SJulian Wiedmann brport_nla_put_flag(skb, flags, mask, 4897583cb0b4SJulian Wiedmann IFLA_BRPORT_BCAST_FLOOD, BR_BCAST_FLOOD)) { 48982c3c031cSScott Feldman nla_nest_cancel(skb, protinfo); 48992c3c031cSScott Feldman goto nla_put_failure; 49002c3c031cSScott Feldman } 49012c3c031cSScott Feldman 49022c3c031cSScott Feldman nla_nest_end(skb, protinfo); 49032c3c031cSScott Feldman 4904053c095aSJohannes Berg nlmsg_end(skb, nlh); 4905053c095aSJohannes Berg return 0; 4906815cccbfSJohn Fastabend nla_put_failure: 4907815cccbfSJohn Fastabend nlmsg_cancel(skb, nlh); 49087d4f8d87SScott Feldman return err ? err : -EMSGSIZE; 4909815cccbfSJohn Fastabend } 49107d4f8d87SScott Feldman EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink); 4911815cccbfSJohn Fastabend 49122d011be8SDavid Ahern static int valid_bridge_getlink_req(const struct nlmsghdr *nlh, 49132d011be8SDavid Ahern bool strict_check, u32 *filter_mask, 49142d011be8SDavid Ahern struct netlink_ext_ack *extack) 49152d011be8SDavid Ahern { 49162d011be8SDavid Ahern struct nlattr *tb[IFLA_MAX+1]; 49172d011be8SDavid Ahern int err, i; 49182d011be8SDavid Ahern 49192d011be8SDavid Ahern if (strict_check) { 49202d011be8SDavid Ahern struct ifinfomsg *ifm; 49212d011be8SDavid Ahern 49222d011be8SDavid Ahern if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { 49232d011be8SDavid Ahern NL_SET_ERR_MSG(extack, "Invalid header for bridge link dump"); 49242d011be8SDavid Ahern return -EINVAL; 49252d011be8SDavid Ahern } 49262d011be8SDavid Ahern 49272d011be8SDavid Ahern ifm = nlmsg_data(nlh); 49282d011be8SDavid Ahern if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags || 49292d011be8SDavid Ahern ifm->ifi_change || ifm->ifi_index) { 49302d011be8SDavid Ahern NL_SET_ERR_MSG(extack, "Invalid values in header for bridge link dump request"); 49312d011be8SDavid Ahern return -EINVAL; 49322d011be8SDavid Ahern } 49332d011be8SDavid Ahern 49348cb08174SJohannes Berg err = nlmsg_parse_deprecated_strict(nlh, 49358cb08174SJohannes Berg sizeof(struct ifinfomsg), 49368cb08174SJohannes Berg tb, IFLA_MAX, ifla_policy, 49378cb08174SJohannes Berg extack); 49382d011be8SDavid Ahern } else { 49398cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg), 49408cb08174SJohannes Berg tb, IFLA_MAX, ifla_policy, 49418cb08174SJohannes Berg extack); 49422d011be8SDavid Ahern } 49432d011be8SDavid Ahern if (err < 0) 49442d011be8SDavid Ahern return err; 49452d011be8SDavid Ahern 49462d011be8SDavid Ahern /* new attributes should only be added with strict checking */ 49472d011be8SDavid Ahern for (i = 0; i <= IFLA_MAX; ++i) { 49482d011be8SDavid Ahern if (!tb[i]) 49492d011be8SDavid Ahern continue; 49502d011be8SDavid Ahern 49512d011be8SDavid Ahern switch (i) { 49522d011be8SDavid Ahern case IFLA_EXT_MASK: 49532d011be8SDavid Ahern *filter_mask = nla_get_u32(tb[i]); 49542d011be8SDavid Ahern break; 49552d011be8SDavid Ahern default: 49562d011be8SDavid Ahern if (strict_check) { 49572d011be8SDavid Ahern NL_SET_ERR_MSG(extack, "Unsupported attribute in bridge link dump request"); 49582d011be8SDavid Ahern return -EINVAL; 49592d011be8SDavid Ahern } 49602d011be8SDavid Ahern } 49612d011be8SDavid Ahern } 49622d011be8SDavid Ahern 49632d011be8SDavid Ahern return 0; 49642d011be8SDavid Ahern } 49652d011be8SDavid Ahern 4966e5a55a89SJohn Fastabend static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb) 4967e5a55a89SJohn Fastabend { 49682d011be8SDavid Ahern const struct nlmsghdr *nlh = cb->nlh; 4969e5a55a89SJohn Fastabend struct net *net = sock_net(skb->sk); 4970e5a55a89SJohn Fastabend struct net_device *dev; 4971e5a55a89SJohn Fastabend int idx = 0; 4972e5a55a89SJohn Fastabend u32 portid = NETLINK_CB(cb->skb).portid; 49732d011be8SDavid Ahern u32 seq = nlh->nlmsg_seq; 49746cbdceebSVlad Yasevich u32 filter_mask = 0; 4975d64f69b0SRoopa Prabhu int err; 49766cbdceebSVlad Yasevich 49772d011be8SDavid Ahern err = valid_bridge_getlink_req(nlh, cb->strict_check, &filter_mask, 49782d011be8SDavid Ahern cb->extack); 49792d011be8SDavid Ahern if (err < 0 && cb->strict_check) 49802d011be8SDavid Ahern return err; 4981e5a55a89SJohn Fastabend 4982e5a55a89SJohn Fastabend rcu_read_lock(); 4983e5a55a89SJohn Fastabend for_each_netdev_rcu(net, dev) { 4984e5a55a89SJohn Fastabend const struct net_device_ops *ops = dev->netdev_ops; 4985898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4986e5a55a89SJohn Fastabend 4987898e5061SJiri Pirko if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) { 4988d64f69b0SRoopa Prabhu if (idx >= cb->args[0]) { 4989d64f69b0SRoopa Prabhu err = br_dev->netdev_ops->ndo_bridge_getlink( 4990d64f69b0SRoopa Prabhu skb, portid, seq, dev, 4991d64f69b0SRoopa Prabhu filter_mask, NLM_F_MULTI); 4992f6c5775fSDavid Ahern if (err < 0 && err != -EOPNOTSUPP) { 4993f6c5775fSDavid Ahern if (likely(skb->len)) 4994e5a55a89SJohn Fastabend break; 4995f6c5775fSDavid Ahern 4996f6c5775fSDavid Ahern goto out_err; 4997f6c5775fSDavid Ahern } 4998d64f69b0SRoopa Prabhu } 4999e5a55a89SJohn Fastabend idx++; 5000e5a55a89SJohn Fastabend } 5001e5a55a89SJohn Fastabend 5002e5a55a89SJohn Fastabend if (ops->ndo_bridge_getlink) { 5003d64f69b0SRoopa Prabhu if (idx >= cb->args[0]) { 5004d64f69b0SRoopa Prabhu err = ops->ndo_bridge_getlink(skb, portid, 5005d64f69b0SRoopa Prabhu seq, dev, 500646c264daSNicolas Dichtel filter_mask, 5007d64f69b0SRoopa Prabhu NLM_F_MULTI); 5008f6c5775fSDavid Ahern if (err < 0 && err != -EOPNOTSUPP) { 5009f6c5775fSDavid Ahern if (likely(skb->len)) 5010e5a55a89SJohn Fastabend break; 5011f6c5775fSDavid Ahern 5012f6c5775fSDavid Ahern goto out_err; 5013f6c5775fSDavid Ahern } 5014d64f69b0SRoopa Prabhu } 5015e5a55a89SJohn Fastabend idx++; 5016e5a55a89SJohn Fastabend } 5017e5a55a89SJohn Fastabend } 5018f6c5775fSDavid Ahern err = skb->len; 5019f6c5775fSDavid Ahern out_err: 5020e5a55a89SJohn Fastabend rcu_read_unlock(); 5021e5a55a89SJohn Fastabend cb->args[0] = idx; 5022e5a55a89SJohn Fastabend 5023f6c5775fSDavid Ahern return err; 5024e5a55a89SJohn Fastabend } 5025e5a55a89SJohn Fastabend 50262469ffd7SJohn Fastabend static inline size_t bridge_nlmsg_size(void) 50272469ffd7SJohn Fastabend { 50282469ffd7SJohn Fastabend return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 50292469ffd7SJohn Fastabend + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 50302469ffd7SJohn Fastabend + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 50312469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_MASTER */ 50322469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_MTU */ 50332469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_LINK */ 50342469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */ 50352469ffd7SJohn Fastabend + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */ 50362469ffd7SJohn Fastabend + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */ 50372469ffd7SJohn Fastabend + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */ 50382469ffd7SJohn Fastabend + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */ 50392469ffd7SJohn Fastabend } 50402469ffd7SJohn Fastabend 504102dba438SRoopa Prabhu static int rtnl_bridge_notify(struct net_device *dev) 50422469ffd7SJohn Fastabend { 50432469ffd7SJohn Fastabend struct net *net = dev_net(dev); 50442469ffd7SJohn Fastabend struct sk_buff *skb; 50452469ffd7SJohn Fastabend int err = -EOPNOTSUPP; 50462469ffd7SJohn Fastabend 504702dba438SRoopa Prabhu if (!dev->netdev_ops->ndo_bridge_getlink) 504802dba438SRoopa Prabhu return 0; 504902dba438SRoopa Prabhu 50502469ffd7SJohn Fastabend skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC); 50512469ffd7SJohn Fastabend if (!skb) { 50522469ffd7SJohn Fastabend err = -ENOMEM; 50532469ffd7SJohn Fastabend goto errout; 50542469ffd7SJohn Fastabend } 50552469ffd7SJohn Fastabend 505646c264daSNicolas Dichtel err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0); 5057c38e01b8SJohn Fastabend if (err < 0) 5058c38e01b8SJohn Fastabend goto errout; 50592469ffd7SJohn Fastabend 5060d2e381c4SIdo Schimmel /* Notification info is only filled for bridge ports, not the bridge 5061d2e381c4SIdo Schimmel * device itself. Therefore, a zero notification length is valid and 5062d2e381c4SIdo Schimmel * should not result in an error. 5063d2e381c4SIdo Schimmel */ 5064d2e381c4SIdo Schimmel if (!skb->len) 506559ccaaaaSRoopa Prabhu goto errout; 506659ccaaaaSRoopa Prabhu 50672469ffd7SJohn Fastabend rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC); 50682469ffd7SJohn Fastabend return 0; 50692469ffd7SJohn Fastabend errout: 50702469ffd7SJohn Fastabend WARN_ON(err == -EMSGSIZE); 50712469ffd7SJohn Fastabend kfree_skb(skb); 507259ccaaaaSRoopa Prabhu if (err) 50732469ffd7SJohn Fastabend rtnl_set_sk_err(net, RTNLGRP_LINK, err); 50742469ffd7SJohn Fastabend return err; 50752469ffd7SJohn Fastabend } 50762469ffd7SJohn Fastabend 5077c21ef3e3SDavid Ahern static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, 5078c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 5079e5a55a89SJohn Fastabend { 5080e5a55a89SJohn Fastabend struct net *net = sock_net(skb->sk); 5081e5a55a89SJohn Fastabend struct ifinfomsg *ifm; 5082e5a55a89SJohn Fastabend struct net_device *dev; 50832469ffd7SJohn Fastabend struct nlattr *br_spec, *attr = NULL; 50842469ffd7SJohn Fastabend int rem, err = -EOPNOTSUPP; 50854de8b413SRosen, Rami u16 flags = 0; 5086c38e01b8SJohn Fastabend bool have_flags = false; 5087e5a55a89SJohn Fastabend 5088e5a55a89SJohn Fastabend if (nlmsg_len(nlh) < sizeof(*ifm)) 5089e5a55a89SJohn Fastabend return -EINVAL; 5090e5a55a89SJohn Fastabend 5091e5a55a89SJohn Fastabend ifm = nlmsg_data(nlh); 5092e5a55a89SJohn Fastabend if (ifm->ifi_family != AF_BRIDGE) 5093e5a55a89SJohn Fastabend return -EPFNOSUPPORT; 5094e5a55a89SJohn Fastabend 5095e5a55a89SJohn Fastabend dev = __dev_get_by_index(net, ifm->ifi_index); 5096e5a55a89SJohn Fastabend if (!dev) { 5097b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 5098e5a55a89SJohn Fastabend return -ENODEV; 5099e5a55a89SJohn Fastabend } 5100e5a55a89SJohn Fastabend 51012469ffd7SJohn Fastabend br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 51022469ffd7SJohn Fastabend if (br_spec) { 51032469ffd7SJohn Fastabend nla_for_each_nested(attr, br_spec, rem) { 51042469ffd7SJohn Fastabend if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { 51056e8d1c55SThomas Graf if (nla_len(attr) < sizeof(flags)) 51066e8d1c55SThomas Graf return -EINVAL; 51076e8d1c55SThomas Graf 5108c38e01b8SJohn Fastabend have_flags = true; 51092469ffd7SJohn Fastabend flags = nla_get_u16(attr); 51102469ffd7SJohn Fastabend break; 51112469ffd7SJohn Fastabend } 51122469ffd7SJohn Fastabend } 51132469ffd7SJohn Fastabend } 51142469ffd7SJohn Fastabend 51152469ffd7SJohn Fastabend if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { 5116898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 5117898e5061SJiri Pirko 5118898e5061SJiri Pirko if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) { 51192469ffd7SJohn Fastabend err = -EOPNOTSUPP; 5120e5a55a89SJohn Fastabend goto out; 5121e5a55a89SJohn Fastabend } 5122e5a55a89SJohn Fastabend 51232fd527b7SPetr Machata err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags, 51242fd527b7SPetr Machata extack); 51252469ffd7SJohn Fastabend if (err) 51262469ffd7SJohn Fastabend goto out; 51272469ffd7SJohn Fastabend 51282469ffd7SJohn Fastabend flags &= ~BRIDGE_FLAGS_MASTER; 51292469ffd7SJohn Fastabend } 51302469ffd7SJohn Fastabend 51312469ffd7SJohn Fastabend if ((flags & BRIDGE_FLAGS_SELF)) { 51322469ffd7SJohn Fastabend if (!dev->netdev_ops->ndo_bridge_setlink) 51332469ffd7SJohn Fastabend err = -EOPNOTSUPP; 51342469ffd7SJohn Fastabend else 5135add511b3SRoopa Prabhu err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh, 51362fd527b7SPetr Machata flags, 51372fd527b7SPetr Machata extack); 513802dba438SRoopa Prabhu if (!err) { 51392469ffd7SJohn Fastabend flags &= ~BRIDGE_FLAGS_SELF; 514002dba438SRoopa Prabhu 514102dba438SRoopa Prabhu /* Generate event to notify upper layer of bridge 514202dba438SRoopa Prabhu * change 514302dba438SRoopa Prabhu */ 514402dba438SRoopa Prabhu err = rtnl_bridge_notify(dev); 514502dba438SRoopa Prabhu } 51462469ffd7SJohn Fastabend } 51472469ffd7SJohn Fastabend 5148c38e01b8SJohn Fastabend if (have_flags) 51492469ffd7SJohn Fastabend memcpy(nla_data(attr), &flags, sizeof(flags)); 5150e5a55a89SJohn Fastabend out: 5151e5a55a89SJohn Fastabend return err; 5152e5a55a89SJohn Fastabend } 5153e5a55a89SJohn Fastabend 5154c21ef3e3SDavid Ahern static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, 5155c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 5156407af329SVlad Yasevich { 5157407af329SVlad Yasevich struct net *net = sock_net(skb->sk); 5158407af329SVlad Yasevich struct ifinfomsg *ifm; 5159407af329SVlad Yasevich struct net_device *dev; 5160407af329SVlad Yasevich struct nlattr *br_spec, *attr = NULL; 5161407af329SVlad Yasevich int rem, err = -EOPNOTSUPP; 51624de8b413SRosen, Rami u16 flags = 0; 5163407af329SVlad Yasevich bool have_flags = false; 5164407af329SVlad Yasevich 5165407af329SVlad Yasevich if (nlmsg_len(nlh) < sizeof(*ifm)) 5166407af329SVlad Yasevich return -EINVAL; 5167407af329SVlad Yasevich 5168407af329SVlad Yasevich ifm = nlmsg_data(nlh); 5169407af329SVlad Yasevich if (ifm->ifi_family != AF_BRIDGE) 5170407af329SVlad Yasevich return -EPFNOSUPPORT; 5171407af329SVlad Yasevich 5172407af329SVlad Yasevich dev = __dev_get_by_index(net, ifm->ifi_index); 5173407af329SVlad Yasevich if (!dev) { 5174b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 5175407af329SVlad Yasevich return -ENODEV; 5176407af329SVlad Yasevich } 5177407af329SVlad Yasevich 5178407af329SVlad Yasevich br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 5179407af329SVlad Yasevich if (br_spec) { 5180407af329SVlad Yasevich nla_for_each_nested(attr, br_spec, rem) { 5181407af329SVlad Yasevich if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { 51826e8d1c55SThomas Graf if (nla_len(attr) < sizeof(flags)) 51836e8d1c55SThomas Graf return -EINVAL; 51846e8d1c55SThomas Graf 5185407af329SVlad Yasevich have_flags = true; 5186407af329SVlad Yasevich flags = nla_get_u16(attr); 5187407af329SVlad Yasevich break; 5188407af329SVlad Yasevich } 5189407af329SVlad Yasevich } 5190407af329SVlad Yasevich } 5191407af329SVlad Yasevich 5192407af329SVlad Yasevich if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { 5193407af329SVlad Yasevich struct net_device *br_dev = netdev_master_upper_dev_get(dev); 5194407af329SVlad Yasevich 5195407af329SVlad Yasevich if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) { 5196407af329SVlad Yasevich err = -EOPNOTSUPP; 5197407af329SVlad Yasevich goto out; 5198407af329SVlad Yasevich } 5199407af329SVlad Yasevich 5200add511b3SRoopa Prabhu err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags); 5201407af329SVlad Yasevich if (err) 5202407af329SVlad Yasevich goto out; 5203407af329SVlad Yasevich 5204407af329SVlad Yasevich flags &= ~BRIDGE_FLAGS_MASTER; 5205407af329SVlad Yasevich } 5206407af329SVlad Yasevich 5207407af329SVlad Yasevich if ((flags & BRIDGE_FLAGS_SELF)) { 5208407af329SVlad Yasevich if (!dev->netdev_ops->ndo_bridge_dellink) 5209407af329SVlad Yasevich err = -EOPNOTSUPP; 5210407af329SVlad Yasevich else 5211add511b3SRoopa Prabhu err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh, 5212add511b3SRoopa Prabhu flags); 5213407af329SVlad Yasevich 521402dba438SRoopa Prabhu if (!err) { 5215407af329SVlad Yasevich flags &= ~BRIDGE_FLAGS_SELF; 521602dba438SRoopa Prabhu 521702dba438SRoopa Prabhu /* Generate event to notify upper layer of bridge 521802dba438SRoopa Prabhu * change 521902dba438SRoopa Prabhu */ 522002dba438SRoopa Prabhu err = rtnl_bridge_notify(dev); 522102dba438SRoopa Prabhu } 5222407af329SVlad Yasevich } 5223407af329SVlad Yasevich 5224407af329SVlad Yasevich if (have_flags) 5225407af329SVlad Yasevich memcpy(nla_data(attr), &flags, sizeof(flags)); 5226407af329SVlad Yasevich out: 5227407af329SVlad Yasevich return err; 5228407af329SVlad Yasevich } 5229407af329SVlad Yasevich 5230e8872a25SNikolay Aleksandrov static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr) 5231e8872a25SNikolay Aleksandrov { 5232e8872a25SNikolay Aleksandrov return (mask & IFLA_STATS_FILTER_BIT(attrid)) && 5233e8872a25SNikolay Aleksandrov (!idxattr || idxattr == attrid); 5234e8872a25SNikolay Aleksandrov } 5235e8872a25SNikolay Aleksandrov 5236f6e0fb81SPetr Machata static bool 5237f6e0fb81SPetr Machata rtnl_offload_xstats_have_ndo(const struct net_device *dev, int attr_id) 523869ae6ad2SNogah Frankel { 5239f6e0fb81SPetr Machata return dev->netdev_ops && 5240f6e0fb81SPetr Machata dev->netdev_ops->ndo_has_offload_stats && 5241f6e0fb81SPetr Machata dev->netdev_ops->ndo_get_offload_stats && 5242f6e0fb81SPetr Machata dev->netdev_ops->ndo_has_offload_stats(dev, attr_id); 524369ae6ad2SNogah Frankel } 524469ae6ad2SNogah Frankel 5245f6e0fb81SPetr Machata static unsigned int 5246f6e0fb81SPetr Machata rtnl_offload_xstats_get_size_ndo(const struct net_device *dev, int attr_id) 5247f6e0fb81SPetr Machata { 5248f6e0fb81SPetr Machata return rtnl_offload_xstats_have_ndo(dev, attr_id) ? 5249f6e0fb81SPetr Machata sizeof(struct rtnl_link_stats64) : 0; 5250f6e0fb81SPetr Machata } 5251f6e0fb81SPetr Machata 5252f6e0fb81SPetr Machata static int 5253f6e0fb81SPetr Machata rtnl_offload_xstats_fill_ndo(struct net_device *dev, int attr_id, 5254f6e0fb81SPetr Machata struct sk_buff *skb) 5255f6e0fb81SPetr Machata { 5256f6e0fb81SPetr Machata unsigned int size = rtnl_offload_xstats_get_size_ndo(dev, attr_id); 5257f6e0fb81SPetr Machata struct nlattr *attr = NULL; 5258f6e0fb81SPetr Machata void *attr_data; 5259f6e0fb81SPetr Machata int err; 5260f6e0fb81SPetr Machata 5261f6e0fb81SPetr Machata if (!size) 5262f6e0fb81SPetr Machata return -ENODATA; 5263f6e0fb81SPetr Machata 5264f6e0fb81SPetr Machata attr = nla_reserve_64bit(skb, attr_id, size, 5265f6e0fb81SPetr Machata IFLA_OFFLOAD_XSTATS_UNSPEC); 5266f6e0fb81SPetr Machata if (!attr) 5267f6e0fb81SPetr Machata return -EMSGSIZE; 5268f6e0fb81SPetr Machata 5269f6e0fb81SPetr Machata attr_data = nla_data(attr); 5270f6e0fb81SPetr Machata memset(attr_data, 0, size); 5271f6e0fb81SPetr Machata 5272f6e0fb81SPetr Machata err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev, attr_data); 5273f6e0fb81SPetr Machata if (err) 5274f6e0fb81SPetr Machata return err; 5275f6e0fb81SPetr Machata 527669ae6ad2SNogah Frankel return 0; 527769ae6ad2SNogah Frankel } 527869ae6ad2SNogah Frankel 52790e7788fdSPetr Machata static unsigned int 52800e7788fdSPetr Machata rtnl_offload_xstats_get_size_stats(const struct net_device *dev, 52810e7788fdSPetr Machata enum netdev_offload_xstats_type type) 52820e7788fdSPetr Machata { 52830e7788fdSPetr Machata bool enabled = netdev_offload_xstats_enabled(dev, type); 52840e7788fdSPetr Machata 52850e7788fdSPetr Machata return enabled ? sizeof(struct rtnl_hw_stats64) : 0; 52860e7788fdSPetr Machata } 52870e7788fdSPetr Machata 52880e7788fdSPetr Machata struct rtnl_offload_xstats_request_used { 52890e7788fdSPetr Machata bool request; 52900e7788fdSPetr Machata bool used; 52910e7788fdSPetr Machata }; 52920e7788fdSPetr Machata 52930e7788fdSPetr Machata static int 52940e7788fdSPetr Machata rtnl_offload_xstats_get_stats(struct net_device *dev, 52950e7788fdSPetr Machata enum netdev_offload_xstats_type type, 52960e7788fdSPetr Machata struct rtnl_offload_xstats_request_used *ru, 52970e7788fdSPetr Machata struct rtnl_hw_stats64 *stats, 52980e7788fdSPetr Machata struct netlink_ext_ack *extack) 52990e7788fdSPetr Machata { 53000e7788fdSPetr Machata bool request; 53010e7788fdSPetr Machata bool used; 53020e7788fdSPetr Machata int err; 53030e7788fdSPetr Machata 53040e7788fdSPetr Machata request = netdev_offload_xstats_enabled(dev, type); 53050e7788fdSPetr Machata if (!request) { 53060e7788fdSPetr Machata used = false; 53070e7788fdSPetr Machata goto out; 53080e7788fdSPetr Machata } 53090e7788fdSPetr Machata 53100e7788fdSPetr Machata err = netdev_offload_xstats_get(dev, type, stats, &used, extack); 53110e7788fdSPetr Machata if (err) 53120e7788fdSPetr Machata return err; 53130e7788fdSPetr Machata 53140e7788fdSPetr Machata out: 53150e7788fdSPetr Machata if (ru) { 53160e7788fdSPetr Machata ru->request = request; 53170e7788fdSPetr Machata ru->used = used; 53180e7788fdSPetr Machata } 53190e7788fdSPetr Machata return 0; 53200e7788fdSPetr Machata } 53210e7788fdSPetr Machata 53220e7788fdSPetr Machata static int 53230e7788fdSPetr Machata rtnl_offload_xstats_fill_hw_s_info_one(struct sk_buff *skb, int attr_id, 53240e7788fdSPetr Machata struct rtnl_offload_xstats_request_used *ru) 53250e7788fdSPetr Machata { 53260e7788fdSPetr Machata struct nlattr *nest; 53270e7788fdSPetr Machata 53280e7788fdSPetr Machata nest = nla_nest_start(skb, attr_id); 53290e7788fdSPetr Machata if (!nest) 53300e7788fdSPetr Machata return -EMSGSIZE; 53310e7788fdSPetr Machata 53320e7788fdSPetr Machata if (nla_put_u8(skb, IFLA_OFFLOAD_XSTATS_HW_S_INFO_REQUEST, ru->request)) 53330e7788fdSPetr Machata goto nla_put_failure; 53340e7788fdSPetr Machata 53350e7788fdSPetr Machata if (nla_put_u8(skb, IFLA_OFFLOAD_XSTATS_HW_S_INFO_USED, ru->used)) 53360e7788fdSPetr Machata goto nla_put_failure; 53370e7788fdSPetr Machata 53380e7788fdSPetr Machata nla_nest_end(skb, nest); 53390e7788fdSPetr Machata return 0; 53400e7788fdSPetr Machata 53410e7788fdSPetr Machata nla_put_failure: 53420e7788fdSPetr Machata nla_nest_cancel(skb, nest); 53430e7788fdSPetr Machata return -EMSGSIZE; 53440e7788fdSPetr Machata } 53450e7788fdSPetr Machata 53460e7788fdSPetr Machata static int 53470e7788fdSPetr Machata rtnl_offload_xstats_fill_hw_s_info(struct sk_buff *skb, struct net_device *dev, 53480e7788fdSPetr Machata struct netlink_ext_ack *extack) 53490e7788fdSPetr Machata { 53500e7788fdSPetr Machata enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3; 53510e7788fdSPetr Machata struct rtnl_offload_xstats_request_used ru_l3; 53520e7788fdSPetr Machata struct nlattr *nest; 53530e7788fdSPetr Machata int err; 53540e7788fdSPetr Machata 53550e7788fdSPetr Machata err = rtnl_offload_xstats_get_stats(dev, t_l3, &ru_l3, NULL, extack); 53560e7788fdSPetr Machata if (err) 53570e7788fdSPetr Machata return err; 53580e7788fdSPetr Machata 53590e7788fdSPetr Machata nest = nla_nest_start(skb, IFLA_OFFLOAD_XSTATS_HW_S_INFO); 53600e7788fdSPetr Machata if (!nest) 53610e7788fdSPetr Machata return -EMSGSIZE; 53620e7788fdSPetr Machata 53630e7788fdSPetr Machata if (rtnl_offload_xstats_fill_hw_s_info_one(skb, 53640e7788fdSPetr Machata IFLA_OFFLOAD_XSTATS_L3_STATS, 53650e7788fdSPetr Machata &ru_l3)) 53660e7788fdSPetr Machata goto nla_put_failure; 53670e7788fdSPetr Machata 53680e7788fdSPetr Machata nla_nest_end(skb, nest); 53690e7788fdSPetr Machata return 0; 53700e7788fdSPetr Machata 53710e7788fdSPetr Machata nla_put_failure: 53720e7788fdSPetr Machata nla_nest_cancel(skb, nest); 53730e7788fdSPetr Machata return -EMSGSIZE; 53740e7788fdSPetr Machata } 53750e7788fdSPetr Machata 53766b524a1dSPetr Machata static int rtnl_offload_xstats_fill(struct sk_buff *skb, struct net_device *dev, 537705415bccSPetr Machata int *prividx, u32 off_filter_mask, 537805415bccSPetr Machata struct netlink_ext_ack *extack) 537969ae6ad2SNogah Frankel { 53800e7788fdSPetr Machata enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3; 53810e7788fdSPetr Machata int attr_id_hw_s_info = IFLA_OFFLOAD_XSTATS_HW_S_INFO; 53820e7788fdSPetr Machata int attr_id_l3_stats = IFLA_OFFLOAD_XSTATS_L3_STATS; 5383f6e0fb81SPetr Machata int attr_id_cpu_hit = IFLA_OFFLOAD_XSTATS_CPU_HIT; 5384f6e0fb81SPetr Machata bool have_data = false; 538569ae6ad2SNogah Frankel int err; 538669ae6ad2SNogah Frankel 538746efc97bSPetr Machata if (*prividx <= attr_id_cpu_hit && 538846efc97bSPetr Machata (off_filter_mask & 538946efc97bSPetr Machata IFLA_STATS_FILTER_BIT(attr_id_cpu_hit))) { 5390f6e0fb81SPetr Machata err = rtnl_offload_xstats_fill_ndo(dev, attr_id_cpu_hit, skb); 5391f6e0fb81SPetr Machata if (!err) { 5392f6e0fb81SPetr Machata have_data = true; 5393f6e0fb81SPetr Machata } else if (err != -ENODATA) { 5394f6e0fb81SPetr Machata *prividx = attr_id_cpu_hit; 5395f6e0fb81SPetr Machata return err; 5396f6e0fb81SPetr Machata } 539769ae6ad2SNogah Frankel } 539869ae6ad2SNogah Frankel 53990e7788fdSPetr Machata if (*prividx <= attr_id_hw_s_info && 54000e7788fdSPetr Machata (off_filter_mask & IFLA_STATS_FILTER_BIT(attr_id_hw_s_info))) { 54010e7788fdSPetr Machata *prividx = attr_id_hw_s_info; 54020e7788fdSPetr Machata 54030e7788fdSPetr Machata err = rtnl_offload_xstats_fill_hw_s_info(skb, dev, extack); 54040e7788fdSPetr Machata if (err) 54050e7788fdSPetr Machata return err; 54060e7788fdSPetr Machata 54070e7788fdSPetr Machata have_data = true; 54080e7788fdSPetr Machata *prividx = 0; 54090e7788fdSPetr Machata } 54100e7788fdSPetr Machata 54110e7788fdSPetr Machata if (*prividx <= attr_id_l3_stats && 54120e7788fdSPetr Machata (off_filter_mask & IFLA_STATS_FILTER_BIT(attr_id_l3_stats))) { 54130e7788fdSPetr Machata unsigned int size_l3; 54140e7788fdSPetr Machata struct nlattr *attr; 54150e7788fdSPetr Machata 54160e7788fdSPetr Machata *prividx = attr_id_l3_stats; 54170e7788fdSPetr Machata 54180e7788fdSPetr Machata size_l3 = rtnl_offload_xstats_get_size_stats(dev, t_l3); 541923cfe941SPetr Machata if (!size_l3) 542023cfe941SPetr Machata goto skip_l3_stats; 54210e7788fdSPetr Machata attr = nla_reserve_64bit(skb, attr_id_l3_stats, size_l3, 54220e7788fdSPetr Machata IFLA_OFFLOAD_XSTATS_UNSPEC); 54230e7788fdSPetr Machata if (!attr) 54240e7788fdSPetr Machata return -EMSGSIZE; 54250e7788fdSPetr Machata 54260e7788fdSPetr Machata err = rtnl_offload_xstats_get_stats(dev, t_l3, NULL, 54270e7788fdSPetr Machata nla_data(attr), extack); 54280e7788fdSPetr Machata if (err) 54290e7788fdSPetr Machata return err; 54300e7788fdSPetr Machata 54310e7788fdSPetr Machata have_data = true; 543223cfe941SPetr Machata skip_l3_stats: 54330e7788fdSPetr Machata *prividx = 0; 54340e7788fdSPetr Machata } 54350e7788fdSPetr Machata 5436f6e0fb81SPetr Machata if (!have_data) 543769ae6ad2SNogah Frankel return -ENODATA; 543869ae6ad2SNogah Frankel 543969ae6ad2SNogah Frankel *prividx = 0; 544069ae6ad2SNogah Frankel return 0; 544169ae6ad2SNogah Frankel } 544269ae6ad2SNogah Frankel 54430e7788fdSPetr Machata static unsigned int 54440e7788fdSPetr Machata rtnl_offload_xstats_get_size_hw_s_info_one(const struct net_device *dev, 54450e7788fdSPetr Machata enum netdev_offload_xstats_type type) 54460e7788fdSPetr Machata { 54470e7788fdSPetr Machata bool enabled = netdev_offload_xstats_enabled(dev, type); 54480e7788fdSPetr Machata 54490e7788fdSPetr Machata return nla_total_size(0) + 54500e7788fdSPetr Machata /* IFLA_OFFLOAD_XSTATS_HW_S_INFO_REQUEST */ 54510e7788fdSPetr Machata nla_total_size(sizeof(u8)) + 54520e7788fdSPetr Machata /* IFLA_OFFLOAD_XSTATS_HW_S_INFO_USED */ 54530e7788fdSPetr Machata (enabled ? nla_total_size(sizeof(u8)) : 0) + 54540e7788fdSPetr Machata 0; 54550e7788fdSPetr Machata } 54560e7788fdSPetr Machata 54570e7788fdSPetr Machata static unsigned int 54580e7788fdSPetr Machata rtnl_offload_xstats_get_size_hw_s_info(const struct net_device *dev) 54590e7788fdSPetr Machata { 54600e7788fdSPetr Machata enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3; 54610e7788fdSPetr Machata 54620e7788fdSPetr Machata return nla_total_size(0) + 54630e7788fdSPetr Machata /* IFLA_OFFLOAD_XSTATS_L3_STATS */ 54640e7788fdSPetr Machata rtnl_offload_xstats_get_size_hw_s_info_one(dev, t_l3) + 54650e7788fdSPetr Machata 0; 54660e7788fdSPetr Machata } 54670e7788fdSPetr Machata 546846efc97bSPetr Machata static int rtnl_offload_xstats_get_size(const struct net_device *dev, 546946efc97bSPetr Machata u32 off_filter_mask) 547069ae6ad2SNogah Frankel { 54710e7788fdSPetr Machata enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3; 5472f6e0fb81SPetr Machata int attr_id_cpu_hit = IFLA_OFFLOAD_XSTATS_CPU_HIT; 547369ae6ad2SNogah Frankel int nla_size = 0; 547469ae6ad2SNogah Frankel int size; 547569ae6ad2SNogah Frankel 547646efc97bSPetr Machata if (off_filter_mask & 547746efc97bSPetr Machata IFLA_STATS_FILTER_BIT(attr_id_cpu_hit)) { 5478f6e0fb81SPetr Machata size = rtnl_offload_xstats_get_size_ndo(dev, attr_id_cpu_hit); 547969ae6ad2SNogah Frankel nla_size += nla_total_size_64bit(size); 548046efc97bSPetr Machata } 548169ae6ad2SNogah Frankel 54820e7788fdSPetr Machata if (off_filter_mask & 54830e7788fdSPetr Machata IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_HW_S_INFO)) 54840e7788fdSPetr Machata nla_size += rtnl_offload_xstats_get_size_hw_s_info(dev); 54850e7788fdSPetr Machata 54860e7788fdSPetr Machata if (off_filter_mask & 54870e7788fdSPetr Machata IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_L3_STATS)) { 54880e7788fdSPetr Machata size = rtnl_offload_xstats_get_size_stats(dev, t_l3); 54890e7788fdSPetr Machata nla_size += nla_total_size_64bit(size); 54900e7788fdSPetr Machata } 54910e7788fdSPetr Machata 549269ae6ad2SNogah Frankel if (nla_size != 0) 549369ae6ad2SNogah Frankel nla_size += nla_total_size(0); 549469ae6ad2SNogah Frankel 549569ae6ad2SNogah Frankel return nla_size; 549669ae6ad2SNogah Frankel } 549769ae6ad2SNogah Frankel 549846efc97bSPetr Machata struct rtnl_stats_dump_filters { 549946efc97bSPetr Machata /* mask[0] filters outer attributes. Then individual nests have their 550046efc97bSPetr Machata * filtering mask at the index of the nested attribute. 550146efc97bSPetr Machata */ 550246efc97bSPetr Machata u32 mask[IFLA_STATS_MAX + 1]; 550346efc97bSPetr Machata }; 550446efc97bSPetr Machata 550510c9ead9SRoopa Prabhu static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev, 550610c9ead9SRoopa Prabhu int type, u32 pid, u32 seq, u32 change, 550746efc97bSPetr Machata unsigned int flags, 550846efc97bSPetr Machata const struct rtnl_stats_dump_filters *filters, 550905415bccSPetr Machata int *idxattr, int *prividx, 551005415bccSPetr Machata struct netlink_ext_ack *extack) 551110c9ead9SRoopa Prabhu { 551246efc97bSPetr Machata unsigned int filter_mask = filters->mask[0]; 551310c9ead9SRoopa Prabhu struct if_stats_msg *ifsm; 551410c9ead9SRoopa Prabhu struct nlmsghdr *nlh; 551510c9ead9SRoopa Prabhu struct nlattr *attr; 5516e8872a25SNikolay Aleksandrov int s_prividx = *prividx; 551769ae6ad2SNogah Frankel int err; 551810c9ead9SRoopa Prabhu 551910c9ead9SRoopa Prabhu ASSERT_RTNL(); 552010c9ead9SRoopa Prabhu 552110c9ead9SRoopa Prabhu nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags); 552210c9ead9SRoopa Prabhu if (!nlh) 552310c9ead9SRoopa Prabhu return -EMSGSIZE; 552410c9ead9SRoopa Prabhu 552510c9ead9SRoopa Prabhu ifsm = nlmsg_data(nlh); 5526ce024f42SNikolay Aleksandrov ifsm->family = PF_UNSPEC; 5527ce024f42SNikolay Aleksandrov ifsm->pad1 = 0; 5528ce024f42SNikolay Aleksandrov ifsm->pad2 = 0; 552910c9ead9SRoopa Prabhu ifsm->ifindex = dev->ifindex; 553010c9ead9SRoopa Prabhu ifsm->filter_mask = filter_mask; 553110c9ead9SRoopa Prabhu 5532e8872a25SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) { 553310c9ead9SRoopa Prabhu struct rtnl_link_stats64 *sp; 553410c9ead9SRoopa Prabhu 553558414d32SNicolas Dichtel attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64, 553658414d32SNicolas Dichtel sizeof(struct rtnl_link_stats64), 553758414d32SNicolas Dichtel IFLA_STATS_UNSPEC); 5538216e6906SPetr Machata if (!attr) { 5539216e6906SPetr Machata err = -EMSGSIZE; 554010c9ead9SRoopa Prabhu goto nla_put_failure; 5541216e6906SPetr Machata } 554210c9ead9SRoopa Prabhu 554310c9ead9SRoopa Prabhu sp = nla_data(attr); 554410c9ead9SRoopa Prabhu dev_get_stats(dev, sp); 554510c9ead9SRoopa Prabhu } 554610c9ead9SRoopa Prabhu 554797a47facSNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) { 554897a47facSNikolay Aleksandrov const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 554997a47facSNikolay Aleksandrov 555097a47facSNikolay Aleksandrov if (ops && ops->fill_linkxstats) { 555197a47facSNikolay Aleksandrov *idxattr = IFLA_STATS_LINK_XSTATS; 5552ae0be8deSMichal Kubecek attr = nla_nest_start_noflag(skb, 555397a47facSNikolay Aleksandrov IFLA_STATS_LINK_XSTATS); 5554216e6906SPetr Machata if (!attr) { 5555216e6906SPetr Machata err = -EMSGSIZE; 555697a47facSNikolay Aleksandrov goto nla_put_failure; 5557216e6906SPetr Machata } 555897a47facSNikolay Aleksandrov 555980e73cc5SNikolay Aleksandrov err = ops->fill_linkxstats(skb, dev, prividx, *idxattr); 556080e73cc5SNikolay Aleksandrov nla_nest_end(skb, attr); 556180e73cc5SNikolay Aleksandrov if (err) 556280e73cc5SNikolay Aleksandrov goto nla_put_failure; 556380e73cc5SNikolay Aleksandrov *idxattr = 0; 556480e73cc5SNikolay Aleksandrov } 556580e73cc5SNikolay Aleksandrov } 556680e73cc5SNikolay Aleksandrov 556780e73cc5SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 556880e73cc5SNikolay Aleksandrov *idxattr)) { 556980e73cc5SNikolay Aleksandrov const struct rtnl_link_ops *ops = NULL; 557080e73cc5SNikolay Aleksandrov const struct net_device *master; 557180e73cc5SNikolay Aleksandrov 557280e73cc5SNikolay Aleksandrov master = netdev_master_upper_dev_get(dev); 557380e73cc5SNikolay Aleksandrov if (master) 557480e73cc5SNikolay Aleksandrov ops = master->rtnl_link_ops; 557580e73cc5SNikolay Aleksandrov if (ops && ops->fill_linkxstats) { 557680e73cc5SNikolay Aleksandrov *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE; 5577ae0be8deSMichal Kubecek attr = nla_nest_start_noflag(skb, 557880e73cc5SNikolay Aleksandrov IFLA_STATS_LINK_XSTATS_SLAVE); 5579216e6906SPetr Machata if (!attr) { 5580216e6906SPetr Machata err = -EMSGSIZE; 558180e73cc5SNikolay Aleksandrov goto nla_put_failure; 5582216e6906SPetr Machata } 558380e73cc5SNikolay Aleksandrov 558480e73cc5SNikolay Aleksandrov err = ops->fill_linkxstats(skb, dev, prividx, *idxattr); 558597a47facSNikolay Aleksandrov nla_nest_end(skb, attr); 558697a47facSNikolay Aleksandrov if (err) 558797a47facSNikolay Aleksandrov goto nla_put_failure; 558897a47facSNikolay Aleksandrov *idxattr = 0; 558997a47facSNikolay Aleksandrov } 559097a47facSNikolay Aleksandrov } 559197a47facSNikolay Aleksandrov 559269ae6ad2SNogah Frankel if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 559369ae6ad2SNogah Frankel *idxattr)) { 559446efc97bSPetr Machata u32 off_filter_mask; 559546efc97bSPetr Machata 559646efc97bSPetr Machata off_filter_mask = filters->mask[IFLA_STATS_LINK_OFFLOAD_XSTATS]; 559769ae6ad2SNogah Frankel *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS; 5598ae0be8deSMichal Kubecek attr = nla_nest_start_noflag(skb, 5599ae0be8deSMichal Kubecek IFLA_STATS_LINK_OFFLOAD_XSTATS); 5600216e6906SPetr Machata if (!attr) { 5601216e6906SPetr Machata err = -EMSGSIZE; 560269ae6ad2SNogah Frankel goto nla_put_failure; 5603216e6906SPetr Machata } 560469ae6ad2SNogah Frankel 560546efc97bSPetr Machata err = rtnl_offload_xstats_fill(skb, dev, prividx, 560605415bccSPetr Machata off_filter_mask, extack); 560769ae6ad2SNogah Frankel if (err == -ENODATA) 560869ae6ad2SNogah Frankel nla_nest_cancel(skb, attr); 560969ae6ad2SNogah Frankel else 561069ae6ad2SNogah Frankel nla_nest_end(skb, attr); 561169ae6ad2SNogah Frankel 561269ae6ad2SNogah Frankel if (err && err != -ENODATA) 561369ae6ad2SNogah Frankel goto nla_put_failure; 561469ae6ad2SNogah Frankel *idxattr = 0; 561569ae6ad2SNogah Frankel } 561669ae6ad2SNogah Frankel 5617aefb4d4aSRobert Shearman if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) { 5618aefb4d4aSRobert Shearman struct rtnl_af_ops *af_ops; 5619aefb4d4aSRobert Shearman 5620aefb4d4aSRobert Shearman *idxattr = IFLA_STATS_AF_SPEC; 5621ae0be8deSMichal Kubecek attr = nla_nest_start_noflag(skb, IFLA_STATS_AF_SPEC); 5622216e6906SPetr Machata if (!attr) { 5623216e6906SPetr Machata err = -EMSGSIZE; 5624aefb4d4aSRobert Shearman goto nla_put_failure; 5625216e6906SPetr Machata } 5626aefb4d4aSRobert Shearman 56275fa85a09SFlorian Westphal rcu_read_lock(); 56285fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 5629aefb4d4aSRobert Shearman if (af_ops->fill_stats_af) { 5630aefb4d4aSRobert Shearman struct nlattr *af; 5631aefb4d4aSRobert Shearman 5632ae0be8deSMichal Kubecek af = nla_nest_start_noflag(skb, 5633ae0be8deSMichal Kubecek af_ops->family); 56345fa85a09SFlorian Westphal if (!af) { 56355fa85a09SFlorian Westphal rcu_read_unlock(); 563657d29a29STom Rix err = -EMSGSIZE; 5637aefb4d4aSRobert Shearman goto nla_put_failure; 56385fa85a09SFlorian Westphal } 5639aefb4d4aSRobert Shearman err = af_ops->fill_stats_af(skb, dev); 5640aefb4d4aSRobert Shearman 56415fa85a09SFlorian Westphal if (err == -ENODATA) { 5642aefb4d4aSRobert Shearman nla_nest_cancel(skb, af); 56435fa85a09SFlorian Westphal } else if (err < 0) { 56445fa85a09SFlorian Westphal rcu_read_unlock(); 5645aefb4d4aSRobert Shearman goto nla_put_failure; 56465fa85a09SFlorian Westphal } 5647aefb4d4aSRobert Shearman 5648aefb4d4aSRobert Shearman nla_nest_end(skb, af); 5649aefb4d4aSRobert Shearman } 5650aefb4d4aSRobert Shearman } 56515fa85a09SFlorian Westphal rcu_read_unlock(); 5652aefb4d4aSRobert Shearman 5653aefb4d4aSRobert Shearman nla_nest_end(skb, attr); 5654aefb4d4aSRobert Shearman 5655aefb4d4aSRobert Shearman *idxattr = 0; 5656aefb4d4aSRobert Shearman } 5657aefb4d4aSRobert Shearman 565810c9ead9SRoopa Prabhu nlmsg_end(skb, nlh); 565910c9ead9SRoopa Prabhu 566010c9ead9SRoopa Prabhu return 0; 566110c9ead9SRoopa Prabhu 566210c9ead9SRoopa Prabhu nla_put_failure: 5663e8872a25SNikolay Aleksandrov /* not a multi message or no progress mean a real error */ 5664e8872a25SNikolay Aleksandrov if (!(flags & NLM_F_MULTI) || s_prividx == *prividx) 566510c9ead9SRoopa Prabhu nlmsg_cancel(skb, nlh); 5666e8872a25SNikolay Aleksandrov else 5667e8872a25SNikolay Aleksandrov nlmsg_end(skb, nlh); 566810c9ead9SRoopa Prabhu 5669216e6906SPetr Machata return err; 567010c9ead9SRoopa Prabhu } 567110c9ead9SRoopa Prabhu 567210c9ead9SRoopa Prabhu static size_t if_nlmsg_stats_size(const struct net_device *dev, 567346efc97bSPetr Machata const struct rtnl_stats_dump_filters *filters) 567410c9ead9SRoopa Prabhu { 5675d3436799SEric Dumazet size_t size = NLMSG_ALIGN(sizeof(struct if_stats_msg)); 567646efc97bSPetr Machata unsigned int filter_mask = filters->mask[0]; 567710c9ead9SRoopa Prabhu 5678e8872a25SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0)) 567910c9ead9SRoopa Prabhu size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64)); 568010c9ead9SRoopa Prabhu 568197a47facSNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) { 568297a47facSNikolay Aleksandrov const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 568380e73cc5SNikolay Aleksandrov int attr = IFLA_STATS_LINK_XSTATS; 568497a47facSNikolay Aleksandrov 568597a47facSNikolay Aleksandrov if (ops && ops->get_linkxstats_size) { 568680e73cc5SNikolay Aleksandrov size += nla_total_size(ops->get_linkxstats_size(dev, 568780e73cc5SNikolay Aleksandrov attr)); 568897a47facSNikolay Aleksandrov /* for IFLA_STATS_LINK_XSTATS */ 568997a47facSNikolay Aleksandrov size += nla_total_size(0); 569097a47facSNikolay Aleksandrov } 569197a47facSNikolay Aleksandrov } 569297a47facSNikolay Aleksandrov 569380e73cc5SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) { 569480e73cc5SNikolay Aleksandrov struct net_device *_dev = (struct net_device *)dev; 569580e73cc5SNikolay Aleksandrov const struct rtnl_link_ops *ops = NULL; 569680e73cc5SNikolay Aleksandrov const struct net_device *master; 569780e73cc5SNikolay Aleksandrov 569880e73cc5SNikolay Aleksandrov /* netdev_master_upper_dev_get can't take const */ 569980e73cc5SNikolay Aleksandrov master = netdev_master_upper_dev_get(_dev); 570080e73cc5SNikolay Aleksandrov if (master) 570180e73cc5SNikolay Aleksandrov ops = master->rtnl_link_ops; 570280e73cc5SNikolay Aleksandrov if (ops && ops->get_linkxstats_size) { 570380e73cc5SNikolay Aleksandrov int attr = IFLA_STATS_LINK_XSTATS_SLAVE; 570480e73cc5SNikolay Aleksandrov 570580e73cc5SNikolay Aleksandrov size += nla_total_size(ops->get_linkxstats_size(dev, 570680e73cc5SNikolay Aleksandrov attr)); 570780e73cc5SNikolay Aleksandrov /* for IFLA_STATS_LINK_XSTATS_SLAVE */ 570880e73cc5SNikolay Aleksandrov size += nla_total_size(0); 570980e73cc5SNikolay Aleksandrov } 571080e73cc5SNikolay Aleksandrov } 571180e73cc5SNikolay Aleksandrov 571246efc97bSPetr Machata if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0)) { 571346efc97bSPetr Machata u32 off_filter_mask; 571446efc97bSPetr Machata 571546efc97bSPetr Machata off_filter_mask = filters->mask[IFLA_STATS_LINK_OFFLOAD_XSTATS]; 571646efc97bSPetr Machata size += rtnl_offload_xstats_get_size(dev, off_filter_mask); 571746efc97bSPetr Machata } 571869ae6ad2SNogah Frankel 5719aefb4d4aSRobert Shearman if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) { 5720aefb4d4aSRobert Shearman struct rtnl_af_ops *af_ops; 5721aefb4d4aSRobert Shearman 5722aefb4d4aSRobert Shearman /* for IFLA_STATS_AF_SPEC */ 5723aefb4d4aSRobert Shearman size += nla_total_size(0); 5724aefb4d4aSRobert Shearman 57255fa85a09SFlorian Westphal rcu_read_lock(); 57265fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 5727aefb4d4aSRobert Shearman if (af_ops->get_stats_af_size) { 5728aefb4d4aSRobert Shearman size += nla_total_size( 5729aefb4d4aSRobert Shearman af_ops->get_stats_af_size(dev)); 5730aefb4d4aSRobert Shearman 5731aefb4d4aSRobert Shearman /* for AF_* */ 5732aefb4d4aSRobert Shearman size += nla_total_size(0); 5733aefb4d4aSRobert Shearman } 5734aefb4d4aSRobert Shearman } 57355fa85a09SFlorian Westphal rcu_read_unlock(); 5736aefb4d4aSRobert Shearman } 5737aefb4d4aSRobert Shearman 573810c9ead9SRoopa Prabhu return size; 573910c9ead9SRoopa Prabhu } 574010c9ead9SRoopa Prabhu 574146efc97bSPetr Machata #define RTNL_STATS_OFFLOAD_XSTATS_VALID ((1 << __IFLA_OFFLOAD_XSTATS_MAX) - 1) 574246efc97bSPetr Machata 574346efc97bSPetr Machata static const struct nla_policy 574446efc97bSPetr Machata rtnl_stats_get_policy_filters[IFLA_STATS_MAX + 1] = { 574546efc97bSPetr Machata [IFLA_STATS_LINK_OFFLOAD_XSTATS] = 574646efc97bSPetr Machata NLA_POLICY_MASK(NLA_U32, RTNL_STATS_OFFLOAD_XSTATS_VALID), 574746efc97bSPetr Machata }; 574846efc97bSPetr Machata 574946efc97bSPetr Machata static const struct nla_policy 575046efc97bSPetr Machata rtnl_stats_get_policy[IFLA_STATS_GETSET_MAX + 1] = { 575146efc97bSPetr Machata [IFLA_STATS_GET_FILTERS] = 575246efc97bSPetr Machata NLA_POLICY_NESTED(rtnl_stats_get_policy_filters), 575346efc97bSPetr Machata }; 575446efc97bSPetr Machata 575503ba3566SPetr Machata static const struct nla_policy 575603ba3566SPetr Machata ifla_stats_set_policy[IFLA_STATS_GETSET_MAX + 1] = { 57575fd0b838SPetr Machata [IFLA_STATS_SET_OFFLOAD_XSTATS_L3_STATS] = NLA_POLICY_MAX(NLA_U8, 1), 575803ba3566SPetr Machata }; 575903ba3566SPetr Machata 576046efc97bSPetr Machata static int rtnl_stats_get_parse_filters(struct nlattr *ifla_filters, 576146efc97bSPetr Machata struct rtnl_stats_dump_filters *filters, 576246efc97bSPetr Machata struct netlink_ext_ack *extack) 576346efc97bSPetr Machata { 576446efc97bSPetr Machata struct nlattr *tb[IFLA_STATS_MAX + 1]; 576546efc97bSPetr Machata int err; 576646efc97bSPetr Machata int at; 576746efc97bSPetr Machata 576846efc97bSPetr Machata err = nla_parse_nested(tb, IFLA_STATS_MAX, ifla_filters, 576946efc97bSPetr Machata rtnl_stats_get_policy_filters, extack); 577046efc97bSPetr Machata if (err < 0) 577146efc97bSPetr Machata return err; 577246efc97bSPetr Machata 577346efc97bSPetr Machata for (at = 1; at <= IFLA_STATS_MAX; at++) { 577446efc97bSPetr Machata if (tb[at]) { 577546efc97bSPetr Machata if (!(filters->mask[0] & IFLA_STATS_FILTER_BIT(at))) { 577646efc97bSPetr Machata NL_SET_ERR_MSG(extack, "Filtered attribute not enabled in filter_mask"); 577746efc97bSPetr Machata return -EINVAL; 577846efc97bSPetr Machata } 577946efc97bSPetr Machata filters->mask[at] = nla_get_u32(tb[at]); 578046efc97bSPetr Machata } 578146efc97bSPetr Machata } 578246efc97bSPetr Machata 578346efc97bSPetr Machata return 0; 578446efc97bSPetr Machata } 578546efc97bSPetr Machata 578646efc97bSPetr Machata static int rtnl_stats_get_parse(const struct nlmsghdr *nlh, 578746efc97bSPetr Machata u32 filter_mask, 578846efc97bSPetr Machata struct rtnl_stats_dump_filters *filters, 578946efc97bSPetr Machata struct netlink_ext_ack *extack) 579046efc97bSPetr Machata { 579146efc97bSPetr Machata struct nlattr *tb[IFLA_STATS_GETSET_MAX + 1]; 579246efc97bSPetr Machata int err; 579346efc97bSPetr Machata int i; 579446efc97bSPetr Machata 579546efc97bSPetr Machata filters->mask[0] = filter_mask; 579646efc97bSPetr Machata for (i = 1; i < ARRAY_SIZE(filters->mask); i++) 579746efc97bSPetr Machata filters->mask[i] = -1U; 579846efc97bSPetr Machata 579946efc97bSPetr Machata err = nlmsg_parse(nlh, sizeof(struct if_stats_msg), tb, 580046efc97bSPetr Machata IFLA_STATS_GETSET_MAX, rtnl_stats_get_policy, extack); 580146efc97bSPetr Machata if (err < 0) 580246efc97bSPetr Machata return err; 580346efc97bSPetr Machata 580446efc97bSPetr Machata if (tb[IFLA_STATS_GET_FILTERS]) { 580546efc97bSPetr Machata err = rtnl_stats_get_parse_filters(tb[IFLA_STATS_GET_FILTERS], 580646efc97bSPetr Machata filters, extack); 580746efc97bSPetr Machata if (err) 580846efc97bSPetr Machata return err; 580946efc97bSPetr Machata } 581046efc97bSPetr Machata 581146efc97bSPetr Machata return 0; 581246efc97bSPetr Machata } 581346efc97bSPetr Machata 581451bc860dSJakub Kicinski static int rtnl_valid_stats_req(const struct nlmsghdr *nlh, bool strict_check, 581551bc860dSJakub Kicinski bool is_dump, struct netlink_ext_ack *extack) 581651bc860dSJakub Kicinski { 581751bc860dSJakub Kicinski struct if_stats_msg *ifsm; 581851bc860dSJakub Kicinski 581969f23a09SEric Dumazet if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifsm))) { 582051bc860dSJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid header for stats dump"); 582151bc860dSJakub Kicinski return -EINVAL; 582251bc860dSJakub Kicinski } 582351bc860dSJakub Kicinski 582451bc860dSJakub Kicinski if (!strict_check) 582551bc860dSJakub Kicinski return 0; 582651bc860dSJakub Kicinski 582751bc860dSJakub Kicinski ifsm = nlmsg_data(nlh); 582851bc860dSJakub Kicinski 582951bc860dSJakub Kicinski /* only requests using strict checks can pass data to influence 583051bc860dSJakub Kicinski * the dump. The legacy exception is filter_mask. 583151bc860dSJakub Kicinski */ 583251bc860dSJakub Kicinski if (ifsm->pad1 || ifsm->pad2 || (is_dump && ifsm->ifindex)) { 583351bc860dSJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid values in header for stats dump request"); 583451bc860dSJakub Kicinski return -EINVAL; 583551bc860dSJakub Kicinski } 58366300acb2SJakub Kicinski if (ifsm->filter_mask >= IFLA_STATS_FILTER_BIT(IFLA_STATS_MAX + 1)) { 58376300acb2SJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid stats requested through filter mask"); 58386300acb2SJakub Kicinski return -EINVAL; 58396300acb2SJakub Kicinski } 584051bc860dSJakub Kicinski 584151bc860dSJakub Kicinski return 0; 584251bc860dSJakub Kicinski } 584351bc860dSJakub Kicinski 5844c21ef3e3SDavid Ahern static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh, 5845c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 584610c9ead9SRoopa Prabhu { 584746efc97bSPetr Machata struct rtnl_stats_dump_filters filters; 584810c9ead9SRoopa Prabhu struct net *net = sock_net(skb->sk); 584910c9ead9SRoopa Prabhu struct net_device *dev = NULL; 5850e8872a25SNikolay Aleksandrov int idxattr = 0, prividx = 0; 5851e8872a25SNikolay Aleksandrov struct if_stats_msg *ifsm; 585210c9ead9SRoopa Prabhu struct sk_buff *nskb; 585310c9ead9SRoopa Prabhu int err; 585410c9ead9SRoopa Prabhu 585551bc860dSJakub Kicinski err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb), 585651bc860dSJakub Kicinski false, extack); 585751bc860dSJakub Kicinski if (err) 585851bc860dSJakub Kicinski return err; 58594775cc1fSMathias Krause 586010c9ead9SRoopa Prabhu ifsm = nlmsg_data(nlh); 586110c9ead9SRoopa Prabhu if (ifsm->ifindex > 0) 586210c9ead9SRoopa Prabhu dev = __dev_get_by_index(net, ifsm->ifindex); 586310c9ead9SRoopa Prabhu else 586410c9ead9SRoopa Prabhu return -EINVAL; 586510c9ead9SRoopa Prabhu 586610c9ead9SRoopa Prabhu if (!dev) 586710c9ead9SRoopa Prabhu return -ENODEV; 586810c9ead9SRoopa Prabhu 586946efc97bSPetr Machata if (!ifsm->filter_mask) { 587022b67d17SPetr Machata NL_SET_ERR_MSG(extack, "Filter mask must be set for stats get"); 587110c9ead9SRoopa Prabhu return -EINVAL; 587222b67d17SPetr Machata } 587310c9ead9SRoopa Prabhu 587446efc97bSPetr Machata err = rtnl_stats_get_parse(nlh, ifsm->filter_mask, &filters, extack); 587546efc97bSPetr Machata if (err) 587646efc97bSPetr Machata return err; 587746efc97bSPetr Machata 587846efc97bSPetr Machata nskb = nlmsg_new(if_nlmsg_stats_size(dev, &filters), GFP_KERNEL); 587910c9ead9SRoopa Prabhu if (!nskb) 588010c9ead9SRoopa Prabhu return -ENOBUFS; 588110c9ead9SRoopa Prabhu 588210c9ead9SRoopa Prabhu err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS, 588310c9ead9SRoopa Prabhu NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0, 588405415bccSPetr Machata 0, &filters, &idxattr, &prividx, extack); 588510c9ead9SRoopa Prabhu if (err < 0) { 588610c9ead9SRoopa Prabhu /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */ 588710c9ead9SRoopa Prabhu WARN_ON(err == -EMSGSIZE); 588810c9ead9SRoopa Prabhu kfree_skb(nskb); 588910c9ead9SRoopa Prabhu } else { 589010c9ead9SRoopa Prabhu err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); 589110c9ead9SRoopa Prabhu } 589210c9ead9SRoopa Prabhu 589310c9ead9SRoopa Prabhu return err; 589410c9ead9SRoopa Prabhu } 589510c9ead9SRoopa Prabhu 589610c9ead9SRoopa Prabhu static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb) 589710c9ead9SRoopa Prabhu { 5898841891ecSDavid Ahern struct netlink_ext_ack *extack = cb->extack; 5899e8872a25SNikolay Aleksandrov int h, s_h, err, s_idx, s_idxattr, s_prividx; 590046efc97bSPetr Machata struct rtnl_stats_dump_filters filters; 590110c9ead9SRoopa Prabhu struct net *net = sock_net(skb->sk); 590210c9ead9SRoopa Prabhu unsigned int flags = NLM_F_MULTI; 5903e8872a25SNikolay Aleksandrov struct if_stats_msg *ifsm; 5904e8872a25SNikolay Aleksandrov struct hlist_head *head; 5905e8872a25SNikolay Aleksandrov struct net_device *dev; 5906e8872a25SNikolay Aleksandrov int idx = 0; 590710c9ead9SRoopa Prabhu 590810c9ead9SRoopa Prabhu s_h = cb->args[0]; 590910c9ead9SRoopa Prabhu s_idx = cb->args[1]; 5910e8872a25SNikolay Aleksandrov s_idxattr = cb->args[2]; 5911e8872a25SNikolay Aleksandrov s_prividx = cb->args[3]; 591210c9ead9SRoopa Prabhu 591310c9ead9SRoopa Prabhu cb->seq = net->dev_base_seq; 591410c9ead9SRoopa Prabhu 591551bc860dSJakub Kicinski err = rtnl_valid_stats_req(cb->nlh, cb->strict_check, true, extack); 591651bc860dSJakub Kicinski if (err) 591751bc860dSJakub Kicinski return err; 59184775cc1fSMathias Krause 591910c9ead9SRoopa Prabhu ifsm = nlmsg_data(cb->nlh); 592046efc97bSPetr Machata if (!ifsm->filter_mask) { 5921841891ecSDavid Ahern NL_SET_ERR_MSG(extack, "Filter mask must be set for stats dump"); 5922841891ecSDavid Ahern return -EINVAL; 5923841891ecSDavid Ahern } 592410c9ead9SRoopa Prabhu 592546efc97bSPetr Machata err = rtnl_stats_get_parse(cb->nlh, ifsm->filter_mask, &filters, 592646efc97bSPetr Machata extack); 592746efc97bSPetr Machata if (err) 592846efc97bSPetr Machata return err; 592946efc97bSPetr Machata 593010c9ead9SRoopa Prabhu for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 593110c9ead9SRoopa Prabhu idx = 0; 593210c9ead9SRoopa Prabhu head = &net->dev_index_head[h]; 593310c9ead9SRoopa Prabhu hlist_for_each_entry(dev, head, index_hlist) { 593410c9ead9SRoopa Prabhu if (idx < s_idx) 593510c9ead9SRoopa Prabhu goto cont; 593610c9ead9SRoopa Prabhu err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS, 593710c9ead9SRoopa Prabhu NETLINK_CB(cb->skb).portid, 593810c9ead9SRoopa Prabhu cb->nlh->nlmsg_seq, 0, 593946efc97bSPetr Machata flags, &filters, 594005415bccSPetr Machata &s_idxattr, &s_prividx, 594105415bccSPetr Machata extack); 594210c9ead9SRoopa Prabhu /* If we ran out of room on the first message, 594310c9ead9SRoopa Prabhu * we're in trouble 594410c9ead9SRoopa Prabhu */ 594510c9ead9SRoopa Prabhu WARN_ON((err == -EMSGSIZE) && (skb->len == 0)); 594610c9ead9SRoopa Prabhu 594710c9ead9SRoopa Prabhu if (err < 0) 594810c9ead9SRoopa Prabhu goto out; 5949e8872a25SNikolay Aleksandrov s_prividx = 0; 5950e8872a25SNikolay Aleksandrov s_idxattr = 0; 595110c9ead9SRoopa Prabhu nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 595210c9ead9SRoopa Prabhu cont: 595310c9ead9SRoopa Prabhu idx++; 595410c9ead9SRoopa Prabhu } 595510c9ead9SRoopa Prabhu } 595610c9ead9SRoopa Prabhu out: 5957e8872a25SNikolay Aleksandrov cb->args[3] = s_prividx; 5958e8872a25SNikolay Aleksandrov cb->args[2] = s_idxattr; 595910c9ead9SRoopa Prabhu cb->args[1] = idx; 596010c9ead9SRoopa Prabhu cb->args[0] = h; 596110c9ead9SRoopa Prabhu 596210c9ead9SRoopa Prabhu return skb->len; 596310c9ead9SRoopa Prabhu } 596410c9ead9SRoopa Prabhu 59655fd0b838SPetr Machata void rtnl_offload_xstats_notify(struct net_device *dev) 59665fd0b838SPetr Machata { 59675fd0b838SPetr Machata struct rtnl_stats_dump_filters response_filters = {}; 59685fd0b838SPetr Machata struct net *net = dev_net(dev); 59695fd0b838SPetr Machata int idxattr = 0, prividx = 0; 59705fd0b838SPetr Machata struct sk_buff *skb; 59715fd0b838SPetr Machata int err = -ENOBUFS; 59725fd0b838SPetr Machata 59735fd0b838SPetr Machata ASSERT_RTNL(); 59745fd0b838SPetr Machata 59755fd0b838SPetr Machata response_filters.mask[0] |= 59765fd0b838SPetr Machata IFLA_STATS_FILTER_BIT(IFLA_STATS_LINK_OFFLOAD_XSTATS); 59775fd0b838SPetr Machata response_filters.mask[IFLA_STATS_LINK_OFFLOAD_XSTATS] |= 59785fd0b838SPetr Machata IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_HW_S_INFO); 59795fd0b838SPetr Machata 59805fd0b838SPetr Machata skb = nlmsg_new(if_nlmsg_stats_size(dev, &response_filters), 59815fd0b838SPetr Machata GFP_KERNEL); 59825fd0b838SPetr Machata if (!skb) 59835fd0b838SPetr Machata goto errout; 59845fd0b838SPetr Machata 59855fd0b838SPetr Machata err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS, 0, 0, 0, 0, 59865fd0b838SPetr Machata &response_filters, &idxattr, &prividx, NULL); 59875fd0b838SPetr Machata if (err < 0) { 59885fd0b838SPetr Machata kfree_skb(skb); 59895fd0b838SPetr Machata goto errout; 59905fd0b838SPetr Machata } 59915fd0b838SPetr Machata 59925fd0b838SPetr Machata rtnl_notify(skb, net, 0, RTNLGRP_STATS, NULL, GFP_KERNEL); 59935fd0b838SPetr Machata return; 59945fd0b838SPetr Machata 59955fd0b838SPetr Machata errout: 59965fd0b838SPetr Machata rtnl_set_sk_err(net, RTNLGRP_STATS, err); 59975fd0b838SPetr Machata } 59985fd0b838SPetr Machata EXPORT_SYMBOL(rtnl_offload_xstats_notify); 59995fd0b838SPetr Machata 600003ba3566SPetr Machata static int rtnl_stats_set(struct sk_buff *skb, struct nlmsghdr *nlh, 600103ba3566SPetr Machata struct netlink_ext_ack *extack) 600203ba3566SPetr Machata { 60035fd0b838SPetr Machata enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3; 600403ba3566SPetr Machata struct rtnl_stats_dump_filters response_filters = {}; 600503ba3566SPetr Machata struct nlattr *tb[IFLA_STATS_GETSET_MAX + 1]; 600603ba3566SPetr Machata struct net *net = sock_net(skb->sk); 600703ba3566SPetr Machata struct net_device *dev = NULL; 600803ba3566SPetr Machata struct if_stats_msg *ifsm; 60095fd0b838SPetr Machata bool notify = false; 601003ba3566SPetr Machata int err; 601103ba3566SPetr Machata 601203ba3566SPetr Machata err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb), 601303ba3566SPetr Machata false, extack); 601403ba3566SPetr Machata if (err) 601503ba3566SPetr Machata return err; 601603ba3566SPetr Machata 601703ba3566SPetr Machata ifsm = nlmsg_data(nlh); 601803ba3566SPetr Machata if (ifsm->family != AF_UNSPEC) { 601903ba3566SPetr Machata NL_SET_ERR_MSG(extack, "Address family should be AF_UNSPEC"); 602003ba3566SPetr Machata return -EINVAL; 602103ba3566SPetr Machata } 602203ba3566SPetr Machata 602303ba3566SPetr Machata if (ifsm->ifindex > 0) 602403ba3566SPetr Machata dev = __dev_get_by_index(net, ifsm->ifindex); 602503ba3566SPetr Machata else 602603ba3566SPetr Machata return -EINVAL; 602703ba3566SPetr Machata 602803ba3566SPetr Machata if (!dev) 602903ba3566SPetr Machata return -ENODEV; 603003ba3566SPetr Machata 603103ba3566SPetr Machata if (ifsm->filter_mask) { 603203ba3566SPetr Machata NL_SET_ERR_MSG(extack, "Filter mask must be 0 for stats set"); 603303ba3566SPetr Machata return -EINVAL; 603403ba3566SPetr Machata } 603503ba3566SPetr Machata 603603ba3566SPetr Machata err = nlmsg_parse(nlh, sizeof(*ifsm), tb, IFLA_STATS_GETSET_MAX, 603703ba3566SPetr Machata ifla_stats_set_policy, extack); 603803ba3566SPetr Machata if (err < 0) 603903ba3566SPetr Machata return err; 604003ba3566SPetr Machata 60415fd0b838SPetr Machata if (tb[IFLA_STATS_SET_OFFLOAD_XSTATS_L3_STATS]) { 60425fd0b838SPetr Machata u8 req = nla_get_u8(tb[IFLA_STATS_SET_OFFLOAD_XSTATS_L3_STATS]); 604303ba3566SPetr Machata 60445fd0b838SPetr Machata if (req) 60455fd0b838SPetr Machata err = netdev_offload_xstats_enable(dev, t_l3, extack); 60465fd0b838SPetr Machata else 60475fd0b838SPetr Machata err = netdev_offload_xstats_disable(dev, t_l3); 60485fd0b838SPetr Machata 60495fd0b838SPetr Machata if (!err) 60505fd0b838SPetr Machata notify = true; 60515fd0b838SPetr Machata else if (err != -EALREADY) 60525fd0b838SPetr Machata return err; 60535fd0b838SPetr Machata 60545fd0b838SPetr Machata response_filters.mask[0] |= 60555fd0b838SPetr Machata IFLA_STATS_FILTER_BIT(IFLA_STATS_LINK_OFFLOAD_XSTATS); 60565fd0b838SPetr Machata response_filters.mask[IFLA_STATS_LINK_OFFLOAD_XSTATS] |= 60575fd0b838SPetr Machata IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_HW_S_INFO); 605803ba3566SPetr Machata } 605903ba3566SPetr Machata 60605fd0b838SPetr Machata if (notify) 60615fd0b838SPetr Machata rtnl_offload_xstats_notify(dev); 60625fd0b838SPetr Machata 60635fd0b838SPetr Machata return 0; 606403ba3566SPetr Machata } 606503ba3566SPetr Machata 60661da177e4SLinus Torvalds /* Process one rtnetlink message. */ 60671da177e4SLinus Torvalds 60682d4bc933SJohannes Berg static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, 60692d4bc933SJohannes Berg struct netlink_ext_ack *extack) 60701da177e4SLinus Torvalds { 60713b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 6072addf9b90SFlorian Westphal struct rtnl_link *link; 607312dc5c2cSNikolay Aleksandrov enum rtnl_kinds kind; 6074e4202511SFlorian Westphal struct module *owner; 60756853dd48SFlorian Westphal int err = -EOPNOTSUPP; 6076e2849863SThomas Graf rtnl_doit_func doit; 607762256f98SFlorian Westphal unsigned int flags; 60781da177e4SLinus Torvalds int family; 60791da177e4SLinus Torvalds int type; 60801da177e4SLinus Torvalds 60811da177e4SLinus Torvalds type = nlh->nlmsg_type; 60821da177e4SLinus Torvalds if (type > RTM_MAX) 6083038890feSThomas Graf return -EOPNOTSUPP; 60841da177e4SLinus Torvalds 60851da177e4SLinus Torvalds type -= RTM_BASE; 60861da177e4SLinus Torvalds 60871da177e4SLinus Torvalds /* All the messages must have at least 1 byte length */ 6088573ce260SHong zhi guo if (nlmsg_len(nlh) < sizeof(struct rtgenmsg)) 60891da177e4SLinus Torvalds return 0; 60901da177e4SLinus Torvalds 6091573ce260SHong zhi guo family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family; 60922e9ea3e3SNikolay Aleksandrov kind = rtnl_msgtype_kind(type); 60931da177e4SLinus Torvalds 609412dc5c2cSNikolay Aleksandrov if (kind != RTNL_KIND_GET && !netlink_net_capable(skb, CAP_NET_ADMIN)) 60951d00a4ebSThomas Graf return -EPERM; 60961da177e4SLinus Torvalds 60976853dd48SFlorian Westphal rcu_read_lock(); 609812dc5c2cSNikolay Aleksandrov if (kind == RTNL_KIND_GET && (nlh->nlmsg_flags & NLM_F_DUMP)) { 609997c53cacSDenis V. Lunev struct sock *rtnl; 6100e2849863SThomas Graf rtnl_dumpit_func dumpit; 6101ebfe3c51SDi Zhu u32 min_dump_alloc = 0; 61021da177e4SLinus Torvalds 6103addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 6104addf9b90SFlorian Westphal if (!link || !link->dumpit) { 61056853dd48SFlorian Westphal family = PF_UNSPEC; 6106addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 6107addf9b90SFlorian Westphal if (!link || !link->dumpit) 61086853dd48SFlorian Westphal goto err_unlock; 61096853dd48SFlorian Westphal } 6110e4202511SFlorian Westphal owner = link->owner; 6111addf9b90SFlorian Westphal dumpit = link->dumpit; 61126853dd48SFlorian Westphal 61135c2bb9b6SFlorian Westphal if (type == RTM_GETLINK - RTM_BASE) 6114e1fa6d21SFlorian Westphal min_dump_alloc = rtnl_calcit(skb, nlh); 61151da177e4SLinus Torvalds 6116e4202511SFlorian Westphal err = 0; 6117e4202511SFlorian Westphal /* need to do this before rcu_read_unlock() */ 6118e4202511SFlorian Westphal if (!try_module_get(owner)) 6119e4202511SFlorian Westphal err = -EPROTONOSUPPORT; 6120e4202511SFlorian Westphal 61216853dd48SFlorian Westphal rcu_read_unlock(); 61226853dd48SFlorian Westphal 612397c53cacSDenis V. Lunev rtnl = net->rtnl; 6124e4202511SFlorian Westphal if (err == 0) { 612580d326faSPablo Neira Ayuso struct netlink_dump_control c = { 612680d326faSPablo Neira Ayuso .dump = dumpit, 612780d326faSPablo Neira Ayuso .min_dump_alloc = min_dump_alloc, 6128e4202511SFlorian Westphal .module = owner, 612980d326faSPablo Neira Ayuso }; 613080d326faSPablo Neira Ayuso err = netlink_dump_start(rtnl, skb, nlh, &c); 6131e4202511SFlorian Westphal /* netlink_dump_start() will keep a reference on 6132e4202511SFlorian Westphal * module if dump is still in progress. 6133e4202511SFlorian Westphal */ 6134e4202511SFlorian Westphal module_put(owner); 613580d326faSPablo Neira Ayuso } 61362907c35fSEric Dumazet return err; 61371da177e4SLinus Torvalds } 61381da177e4SLinus Torvalds 6139addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 6140addf9b90SFlorian Westphal if (!link || !link->doit) { 61418caa38b5SFlorian Westphal family = PF_UNSPEC; 6142addf9b90SFlorian Westphal link = rtnl_get_link(PF_UNSPEC, type); 6143addf9b90SFlorian Westphal if (!link || !link->doit) 6144addf9b90SFlorian Westphal goto out_unlock; 61458caa38b5SFlorian Westphal } 61468caa38b5SFlorian Westphal 6147e4202511SFlorian Westphal owner = link->owner; 6148e4202511SFlorian Westphal if (!try_module_get(owner)) { 6149e4202511SFlorian Westphal err = -EPROTONOSUPPORT; 6150e4202511SFlorian Westphal goto out_unlock; 6151e4202511SFlorian Westphal } 6152e4202511SFlorian Westphal 6153addf9b90SFlorian Westphal flags = link->flags; 6154a6cec0bcSNikolay Aleksandrov if (kind == RTNL_KIND_DEL && (nlh->nlmsg_flags & NLM_F_BULK) && 6155a6cec0bcSNikolay Aleksandrov !(flags & RTNL_FLAG_BULK_DEL_SUPPORTED)) { 6156a6cec0bcSNikolay Aleksandrov NL_SET_ERR_MSG(extack, "Bulk delete is not supported"); 61575b22f627SZhengchao Shao module_put(owner); 6158a6cec0bcSNikolay Aleksandrov goto err_unlock; 6159a6cec0bcSNikolay Aleksandrov } 6160a6cec0bcSNikolay Aleksandrov 616162256f98SFlorian Westphal if (flags & RTNL_FLAG_DOIT_UNLOCKED) { 6162addf9b90SFlorian Westphal doit = link->doit; 616362256f98SFlorian Westphal rcu_read_unlock(); 616462256f98SFlorian Westphal if (doit) 616562256f98SFlorian Westphal err = doit(skb, nlh, extack); 6166e4202511SFlorian Westphal module_put(owner); 616762256f98SFlorian Westphal return err; 616862256f98SFlorian Westphal } 61696853dd48SFlorian Westphal rcu_read_unlock(); 61706853dd48SFlorian Westphal 61710cc09020SFlorian Westphal rtnl_lock(); 6172addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 6173addf9b90SFlorian Westphal if (link && link->doit) 6174addf9b90SFlorian Westphal err = link->doit(skb, nlh, extack); 61750cc09020SFlorian Westphal rtnl_unlock(); 6176addf9b90SFlorian Westphal 6177e4202511SFlorian Westphal module_put(owner); 6178e4202511SFlorian Westphal 6179addf9b90SFlorian Westphal return err; 6180addf9b90SFlorian Westphal 6181addf9b90SFlorian Westphal out_unlock: 6182addf9b90SFlorian Westphal rcu_read_unlock(); 61830cc09020SFlorian Westphal return err; 61840cc09020SFlorian Westphal 61850cc09020SFlorian Westphal err_unlock: 61866853dd48SFlorian Westphal rcu_read_unlock(); 61870cc09020SFlorian Westphal return -EOPNOTSUPP; 61881da177e4SLinus Torvalds } 61891da177e4SLinus Torvalds 6190cd40b7d3SDenis V. Lunev static void rtnetlink_rcv(struct sk_buff *skb) 61911da177e4SLinus Torvalds { 6192cd40b7d3SDenis V. Lunev netlink_rcv_skb(skb, &rtnetlink_rcv_msg); 61931da177e4SLinus Torvalds } 61941da177e4SLinus Torvalds 61955f729eaaSJulien Gomes static int rtnetlink_bind(struct net *net, int group) 61965f729eaaSJulien Gomes { 61975f729eaaSJulien Gomes switch (group) { 61985f729eaaSJulien Gomes case RTNLGRP_IPV4_MROUTE_R: 61995f729eaaSJulien Gomes case RTNLGRP_IPV6_MROUTE_R: 62005f729eaaSJulien Gomes if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 62015f729eaaSJulien Gomes return -EPERM; 62025f729eaaSJulien Gomes break; 62035f729eaaSJulien Gomes } 62045f729eaaSJulien Gomes return 0; 62055f729eaaSJulien Gomes } 62065f729eaaSJulien Gomes 62071da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr) 62081da177e4SLinus Torvalds { 6209351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 6210e9dc8653SEric W. Biederman 62111da177e4SLinus Torvalds switch (event) { 62125138e86fSVlad Yasevich case NETDEV_REBOOT: 62138a212589SXin Long case NETDEV_CHANGEMTU: 62143753654eSDavid Ahern case NETDEV_CHANGEADDR: 62155138e86fSVlad Yasevich case NETDEV_CHANGENAME: 62165138e86fSVlad Yasevich case NETDEV_FEAT_CHANGE: 62175138e86fSVlad Yasevich case NETDEV_BONDING_FAILOVER: 6218e6e66594SXin Long case NETDEV_POST_TYPE_CHANGE: 62195138e86fSVlad Yasevich case NETDEV_NOTIFY_PEERS: 6220dc709f37SXin Long case NETDEV_CHANGEUPPER: 62215138e86fSVlad Yasevich case NETDEV_RESEND_IGMP: 62225138e86fSVlad Yasevich case NETDEV_CHANGEINFODATA: 6223eeda3fb9SXin Long case NETDEV_CHANGELOWERSTATE: 6224ebdcf045SXin Long case NETDEV_CHANGE_TX_QUEUE_LEN: 62253d3ea5afSVlad Yasevich rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event), 62261d997f10SHangbin Liu GFP_KERNEL, NULL, 0, 0, NULL); 62271da177e4SLinus Torvalds break; 62281da177e4SLinus Torvalds default: 62291da177e4SLinus Torvalds break; 62301da177e4SLinus Torvalds } 62311da177e4SLinus Torvalds return NOTIFY_DONE; 62321da177e4SLinus Torvalds } 62331da177e4SLinus Torvalds 62341da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = { 62351da177e4SLinus Torvalds .notifier_call = rtnetlink_event, 62361da177e4SLinus Torvalds }; 62371da177e4SLinus Torvalds 623897c53cacSDenis V. Lunev 62392c8c1e72SAlexey Dobriyan static int __net_init rtnetlink_net_init(struct net *net) 624097c53cacSDenis V. Lunev { 624197c53cacSDenis V. Lunev struct sock *sk; 6242a31f2d17SPablo Neira Ayuso struct netlink_kernel_cfg cfg = { 6243a31f2d17SPablo Neira Ayuso .groups = RTNLGRP_MAX, 6244a31f2d17SPablo Neira Ayuso .input = rtnetlink_rcv, 6245a31f2d17SPablo Neira Ayuso .cb_mutex = &rtnl_mutex, 62469785e10aSPablo Neira Ayuso .flags = NL_CFG_F_NONROOT_RECV, 62475f729eaaSJulien Gomes .bind = rtnetlink_bind, 6248a31f2d17SPablo Neira Ayuso }; 6249a31f2d17SPablo Neira Ayuso 62509f00d977SPablo Neira Ayuso sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg); 625197c53cacSDenis V. Lunev if (!sk) 625297c53cacSDenis V. Lunev return -ENOMEM; 625397c53cacSDenis V. Lunev net->rtnl = sk; 625497c53cacSDenis V. Lunev return 0; 625597c53cacSDenis V. Lunev } 625697c53cacSDenis V. Lunev 62572c8c1e72SAlexey Dobriyan static void __net_exit rtnetlink_net_exit(struct net *net) 625897c53cacSDenis V. Lunev { 6259b7c6ba6eSDenis V. Lunev netlink_kernel_release(net->rtnl); 626097c53cacSDenis V. Lunev net->rtnl = NULL; 626197c53cacSDenis V. Lunev } 626297c53cacSDenis V. Lunev 626397c53cacSDenis V. Lunev static struct pernet_operations rtnetlink_net_ops = { 626497c53cacSDenis V. Lunev .init = rtnetlink_net_init, 626597c53cacSDenis V. Lunev .exit = rtnetlink_net_exit, 626697c53cacSDenis V. Lunev }; 626797c53cacSDenis V. Lunev 62681da177e4SLinus Torvalds void __init rtnetlink_init(void) 62691da177e4SLinus Torvalds { 627097c53cacSDenis V. Lunev if (register_pernet_subsys(&rtnetlink_net_ops)) 62711da177e4SLinus Torvalds panic("rtnetlink_init: cannot initialize rtnetlink\n"); 627297c53cacSDenis V. Lunev 62731da177e4SLinus Torvalds register_netdevice_notifier(&rtnetlink_dev_notifier); 6274340d17fcSThomas Graf 6275c7ac8679SGreg Rose rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, 6276b97bac64SFlorian Westphal rtnl_dump_ifinfo, 0); 6277b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0); 6278b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0); 6279b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0); 6280687ad8ccSThomas Graf 6281b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0); 6282b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0); 6283b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0); 628477162022SJohn Fastabend 628536fbf1e5SJiri Pirko rtnl_register(PF_UNSPEC, RTM_NEWLINKPROP, rtnl_newlinkprop, NULL, 0); 628636fbf1e5SJiri Pirko rtnl_register(PF_UNSPEC, RTM_DELLINKPROP, rtnl_dellinkprop, NULL, 0); 628736fbf1e5SJiri Pirko 6288b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0); 62899e834259SNikolay Aleksandrov rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 62909e834259SNikolay Aleksandrov RTNL_FLAG_BULK_DEL_SUPPORTED); 62915b2f94b2SRoopa Prabhu rtnl_register(PF_BRIDGE, RTM_GETNEIGH, rtnl_fdb_get, rtnl_fdb_dump, 0); 6292e5a55a89SJohn Fastabend 6293b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0); 6294b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0); 6295b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0); 629610c9ead9SRoopa Prabhu 629710c9ead9SRoopa Prabhu rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump, 6298b97bac64SFlorian Westphal 0); 629903ba3566SPetr Machata rtnl_register(PF_UNSPEC, RTM_SETSTATS, rtnl_stats_set, NULL, 0); 63001da177e4SLinus Torvalds } 6301