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> 561da177e4SLinus Torvalds 57*6264f58cSJakub Kicinski #include "dev.h" 58*6264f58cSJakub Kicinski 59a428afe8SNikolay Aleksandrov #define RTNL_MAX_TYPE 50 602dba407fSNikolay Aleksandrov #define RTNL_SLAVE_MAX_TYPE 40 61ccf8dbcdSKees Cook 62e0d087afSEric Dumazet struct rtnl_link { 63e2849863SThomas Graf rtnl_doit_func doit; 64e2849863SThomas Graf rtnl_dumpit_func dumpit; 65e4202511SFlorian Westphal struct module *owner; 6662256f98SFlorian Westphal unsigned int flags; 67addf9b90SFlorian Westphal struct rcu_head rcu; 68e2849863SThomas Graf }; 69e2849863SThomas Graf 706756ae4bSStephen Hemminger static DEFINE_MUTEX(rtnl_mutex); 711da177e4SLinus Torvalds 721da177e4SLinus Torvalds void rtnl_lock(void) 731da177e4SLinus Torvalds { 746756ae4bSStephen Hemminger mutex_lock(&rtnl_mutex); 751da177e4SLinus Torvalds } 76e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_lock); 771da177e4SLinus Torvalds 7879ffdfc6SKirill Tkhai int rtnl_lock_killable(void) 7979ffdfc6SKirill Tkhai { 8079ffdfc6SKirill Tkhai return mutex_lock_killable(&rtnl_mutex); 8179ffdfc6SKirill Tkhai } 8279ffdfc6SKirill Tkhai EXPORT_SYMBOL(rtnl_lock_killable); 8379ffdfc6SKirill Tkhai 841b5c5493SEric Dumazet static struct sk_buff *defer_kfree_skb_list; 851b5c5493SEric Dumazet void rtnl_kfree_skbs(struct sk_buff *head, struct sk_buff *tail) 861b5c5493SEric Dumazet { 871b5c5493SEric Dumazet if (head && tail) { 881b5c5493SEric Dumazet tail->next = defer_kfree_skb_list; 891b5c5493SEric Dumazet defer_kfree_skb_list = head; 901b5c5493SEric Dumazet } 911b5c5493SEric Dumazet } 921b5c5493SEric Dumazet EXPORT_SYMBOL(rtnl_kfree_skbs); 931b5c5493SEric Dumazet 946756ae4bSStephen Hemminger void __rtnl_unlock(void) 951da177e4SLinus Torvalds { 961b5c5493SEric Dumazet struct sk_buff *head = defer_kfree_skb_list; 971b5c5493SEric Dumazet 981b5c5493SEric Dumazet defer_kfree_skb_list = NULL; 991b5c5493SEric Dumazet 1000b5c21bbSJohannes Berg /* Ensure that we didn't actually add any TODO item when __rtnl_unlock() 1010b5c21bbSJohannes Berg * is used. In some places, e.g. in cfg80211, we have code that will do 1020b5c21bbSJohannes Berg * something like 1030b5c21bbSJohannes Berg * rtnl_lock() 1040b5c21bbSJohannes Berg * wiphy_lock() 1050b5c21bbSJohannes Berg * ... 1060b5c21bbSJohannes Berg * rtnl_unlock() 1070b5c21bbSJohannes Berg * 1080b5c21bbSJohannes Berg * and because netdev_run_todo() acquires the RTNL for items on the list 1090b5c21bbSJohannes Berg * we could cause a situation such as this: 1100b5c21bbSJohannes Berg * Thread 1 Thread 2 1110b5c21bbSJohannes Berg * rtnl_lock() 1120b5c21bbSJohannes Berg * unregister_netdevice() 1130b5c21bbSJohannes Berg * __rtnl_unlock() 1140b5c21bbSJohannes Berg * rtnl_lock() 1150b5c21bbSJohannes Berg * wiphy_lock() 1160b5c21bbSJohannes Berg * rtnl_unlock() 1170b5c21bbSJohannes Berg * netdev_run_todo() 1180b5c21bbSJohannes Berg * __rtnl_unlock() 1190b5c21bbSJohannes Berg * 1200b5c21bbSJohannes Berg * // list not empty now 1210b5c21bbSJohannes Berg * // because of thread 2 1220b5c21bbSJohannes Berg * rtnl_lock() 1230b5c21bbSJohannes Berg * while (!list_empty(...)) 1240b5c21bbSJohannes Berg * rtnl_lock() 1250b5c21bbSJohannes Berg * wiphy_lock() 1260b5c21bbSJohannes Berg * **** DEADLOCK **** 1270b5c21bbSJohannes Berg * 1280b5c21bbSJohannes Berg * However, usage of __rtnl_unlock() is rare, and so we can ensure that 1290b5c21bbSJohannes Berg * it's not used in cases where something is added to do the list. 1300b5c21bbSJohannes Berg */ 1310b5c21bbSJohannes Berg WARN_ON(!list_empty(&net_todo_list)); 1320b5c21bbSJohannes Berg 1336756ae4bSStephen Hemminger mutex_unlock(&rtnl_mutex); 1341b5c5493SEric Dumazet 1351b5c5493SEric Dumazet while (head) { 1361b5c5493SEric Dumazet struct sk_buff *next = head->next; 1371b5c5493SEric Dumazet 1381b5c5493SEric Dumazet kfree_skb(head); 1391b5c5493SEric Dumazet cond_resched(); 1401b5c5493SEric Dumazet head = next; 1411b5c5493SEric Dumazet } 1421da177e4SLinus Torvalds } 1431da177e4SLinus Torvalds 1441da177e4SLinus Torvalds void rtnl_unlock(void) 1451da177e4SLinus Torvalds { 14658ec3b4dSHerbert Xu /* This fellow will unlock it for us. */ 1471da177e4SLinus Torvalds netdev_run_todo(); 1481da177e4SLinus Torvalds } 149e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unlock); 1501da177e4SLinus Torvalds 1516756ae4bSStephen Hemminger int rtnl_trylock(void) 1526756ae4bSStephen Hemminger { 1536756ae4bSStephen Hemminger return mutex_trylock(&rtnl_mutex); 1546756ae4bSStephen Hemminger } 155e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_trylock); 1566756ae4bSStephen Hemminger 157c9c1014bSPatrick McHardy int rtnl_is_locked(void) 158c9c1014bSPatrick McHardy { 159c9c1014bSPatrick McHardy return mutex_is_locked(&rtnl_mutex); 160c9c1014bSPatrick McHardy } 161e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_is_locked); 162c9c1014bSPatrick McHardy 1636f99528eSVlad Buslov bool refcount_dec_and_rtnl_lock(refcount_t *r) 1646f99528eSVlad Buslov { 1656f99528eSVlad Buslov return refcount_dec_and_mutex_lock(r, &rtnl_mutex); 1666f99528eSVlad Buslov } 1676f99528eSVlad Buslov EXPORT_SYMBOL(refcount_dec_and_rtnl_lock); 1686f99528eSVlad Buslov 169a898def2SPaul E. McKenney #ifdef CONFIG_PROVE_LOCKING 1700cbf3343SYaowei Bai bool lockdep_rtnl_is_held(void) 171a898def2SPaul E. McKenney { 172a898def2SPaul E. McKenney return lockdep_is_held(&rtnl_mutex); 173a898def2SPaul E. McKenney } 174a898def2SPaul E. McKenney EXPORT_SYMBOL(lockdep_rtnl_is_held); 175a898def2SPaul E. McKenney #endif /* #ifdef CONFIG_PROVE_LOCKING */ 176a898def2SPaul E. McKenney 17751e13685SJakub Kicinski static struct rtnl_link __rcu *__rcu *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1]; 178e2849863SThomas Graf 179e2849863SThomas Graf static inline int rtm_msgindex(int msgtype) 180e2849863SThomas Graf { 181e2849863SThomas Graf int msgindex = msgtype - RTM_BASE; 182e2849863SThomas Graf 183e2849863SThomas Graf /* 184e2849863SThomas Graf * msgindex < 0 implies someone tried to register a netlink 185e2849863SThomas Graf * control code. msgindex >= RTM_NR_MSGTYPES may indicate that 186e2849863SThomas Graf * the message type has not been added to linux/rtnetlink.h 187e2849863SThomas Graf */ 188e2849863SThomas Graf BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES); 189e2849863SThomas Graf 190e2849863SThomas Graf return msgindex; 191e2849863SThomas Graf } 192e2849863SThomas Graf 193addf9b90SFlorian Westphal static struct rtnl_link *rtnl_get_link(int protocol, int msgtype) 194addf9b90SFlorian Westphal { 19551e13685SJakub Kicinski struct rtnl_link __rcu **tab; 196addf9b90SFlorian Westphal 197addf9b90SFlorian Westphal if (protocol >= ARRAY_SIZE(rtnl_msg_handlers)) 198addf9b90SFlorian Westphal protocol = PF_UNSPEC; 199addf9b90SFlorian Westphal 200addf9b90SFlorian Westphal tab = rcu_dereference_rtnl(rtnl_msg_handlers[protocol]); 201addf9b90SFlorian Westphal if (!tab) 202addf9b90SFlorian Westphal tab = rcu_dereference_rtnl(rtnl_msg_handlers[PF_UNSPEC]); 203addf9b90SFlorian Westphal 20451e13685SJakub Kicinski return rcu_dereference_rtnl(tab[msgtype]); 205addf9b90SFlorian Westphal } 206addf9b90SFlorian Westphal 207e4202511SFlorian Westphal static int rtnl_register_internal(struct module *owner, 208e4202511SFlorian Westphal int protocol, int msgtype, 209c7ac8679SGreg Rose rtnl_doit_func doit, rtnl_dumpit_func dumpit, 210b97bac64SFlorian Westphal unsigned int flags) 211e2849863SThomas Graf { 212b0e9fe1bSFlorian Westphal struct rtnl_link *link, *old; 213b0e9fe1bSFlorian Westphal struct rtnl_link __rcu **tab; 214e2849863SThomas Graf int msgindex; 215addf9b90SFlorian Westphal int ret = -ENOBUFS; 216e2849863SThomas Graf 21725239ceeSPatrick McHardy BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); 218e2849863SThomas Graf msgindex = rtm_msgindex(msgtype); 219e2849863SThomas Graf 220addf9b90SFlorian Westphal rtnl_lock(); 22151e13685SJakub Kicinski tab = rtnl_dereference(rtnl_msg_handlers[protocol]); 222e2849863SThomas Graf if (tab == NULL) { 223addf9b90SFlorian Westphal tab = kcalloc(RTM_NR_MSGTYPES, sizeof(void *), GFP_KERNEL); 224addf9b90SFlorian Westphal if (!tab) 225addf9b90SFlorian Westphal goto unlock; 226e2849863SThomas Graf 227addf9b90SFlorian Westphal /* ensures we see the 0 stores */ 2286853dd48SFlorian Westphal rcu_assign_pointer(rtnl_msg_handlers[protocol], tab); 229e2849863SThomas Graf } 230e2849863SThomas Graf 231addf9b90SFlorian Westphal old = rtnl_dereference(tab[msgindex]); 232addf9b90SFlorian Westphal if (old) { 233addf9b90SFlorian Westphal link = kmemdup(old, sizeof(*old), GFP_KERNEL); 234addf9b90SFlorian Westphal if (!link) 235addf9b90SFlorian Westphal goto unlock; 236addf9b90SFlorian Westphal } else { 237addf9b90SFlorian Westphal link = kzalloc(sizeof(*link), GFP_KERNEL); 238addf9b90SFlorian Westphal if (!link) 239addf9b90SFlorian Westphal goto unlock; 240addf9b90SFlorian Westphal } 241e2849863SThomas Graf 242e4202511SFlorian Westphal WARN_ON(link->owner && link->owner != owner); 243e4202511SFlorian Westphal link->owner = owner; 244e4202511SFlorian Westphal 245addf9b90SFlorian Westphal WARN_ON(doit && link->doit && link->doit != doit); 246addf9b90SFlorian Westphal if (doit) 247addf9b90SFlorian Westphal link->doit = doit; 248addf9b90SFlorian Westphal WARN_ON(dumpit && link->dumpit && link->dumpit != dumpit); 249addf9b90SFlorian Westphal if (dumpit) 250addf9b90SFlorian Westphal link->dumpit = dumpit; 251addf9b90SFlorian Westphal 252addf9b90SFlorian Westphal link->flags |= flags; 253addf9b90SFlorian Westphal 254addf9b90SFlorian Westphal /* publish protocol:msgtype */ 255addf9b90SFlorian Westphal rcu_assign_pointer(tab[msgindex], link); 256addf9b90SFlorian Westphal ret = 0; 257addf9b90SFlorian Westphal if (old) 258addf9b90SFlorian Westphal kfree_rcu(old, rcu); 259addf9b90SFlorian Westphal unlock: 260addf9b90SFlorian Westphal rtnl_unlock(); 261addf9b90SFlorian Westphal return ret; 262e2849863SThomas Graf } 263e4202511SFlorian Westphal 264e4202511SFlorian Westphal /** 265e4202511SFlorian Westphal * rtnl_register_module - Register a rtnetlink message type 266e4202511SFlorian Westphal * 267e4202511SFlorian Westphal * @owner: module registering the hook (THIS_MODULE) 268e4202511SFlorian Westphal * @protocol: Protocol family or PF_UNSPEC 269e4202511SFlorian Westphal * @msgtype: rtnetlink message type 270e4202511SFlorian Westphal * @doit: Function pointer called for each request message 271e4202511SFlorian Westphal * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message 272d467d0bcSZheng Yongjun * @flags: rtnl_link_flags to modify behaviour of doit/dumpit functions 273e4202511SFlorian Westphal * 274e4202511SFlorian Westphal * Like rtnl_register, but for use by removable modules. 275e4202511SFlorian Westphal */ 276e4202511SFlorian Westphal int rtnl_register_module(struct module *owner, 277e4202511SFlorian Westphal int protocol, int msgtype, 278e4202511SFlorian Westphal rtnl_doit_func doit, rtnl_dumpit_func dumpit, 279e4202511SFlorian Westphal unsigned int flags) 280e4202511SFlorian Westphal { 281e4202511SFlorian Westphal return rtnl_register_internal(owner, protocol, msgtype, 282e4202511SFlorian Westphal doit, dumpit, flags); 283e4202511SFlorian Westphal } 284e4202511SFlorian Westphal EXPORT_SYMBOL_GPL(rtnl_register_module); 285e4202511SFlorian Westphal 286e4202511SFlorian Westphal /** 28716feebcfSFlorian Westphal * rtnl_register - Register a rtnetlink message type 288e4202511SFlorian Westphal * @protocol: Protocol family or PF_UNSPEC 289e4202511SFlorian Westphal * @msgtype: rtnetlink message type 290e4202511SFlorian Westphal * @doit: Function pointer called for each request message 291e4202511SFlorian Westphal * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message 292d467d0bcSZheng Yongjun * @flags: rtnl_link_flags to modify behaviour of doit/dumpit functions 293e4202511SFlorian Westphal * 294e4202511SFlorian Westphal * Registers the specified function pointers (at least one of them has 295e4202511SFlorian Westphal * to be non-NULL) to be called whenever a request message for the 296e4202511SFlorian Westphal * specified protocol family and message type is received. 297e4202511SFlorian Westphal * 298e4202511SFlorian Westphal * The special protocol family PF_UNSPEC may be used to define fallback 299e4202511SFlorian Westphal * function pointers for the case when no entry for the specific protocol 300e4202511SFlorian Westphal * family exists. 301e2849863SThomas Graf */ 302e2849863SThomas Graf void rtnl_register(int protocol, int msgtype, 303c7ac8679SGreg Rose rtnl_doit_func doit, rtnl_dumpit_func dumpit, 304b97bac64SFlorian Westphal unsigned int flags) 305e2849863SThomas Graf { 30616feebcfSFlorian Westphal int err; 30716feebcfSFlorian Westphal 30816feebcfSFlorian Westphal err = rtnl_register_internal(NULL, protocol, msgtype, doit, dumpit, 30916feebcfSFlorian Westphal flags); 31016feebcfSFlorian Westphal if (err) 31116feebcfSFlorian Westphal pr_err("Unable to register rtnetlink message handler, " 31216feebcfSFlorian Westphal "protocol = %d, message type = %d\n", protocol, msgtype); 313e2849863SThomas Graf } 314e2849863SThomas Graf 315e2849863SThomas Graf /** 316e2849863SThomas Graf * rtnl_unregister - Unregister a rtnetlink message type 317e2849863SThomas Graf * @protocol: Protocol family or PF_UNSPEC 318e2849863SThomas Graf * @msgtype: rtnetlink message type 319e2849863SThomas Graf * 320e2849863SThomas Graf * Returns 0 on success or a negative error code. 321e2849863SThomas Graf */ 322e2849863SThomas Graf int rtnl_unregister(int protocol, int msgtype) 323e2849863SThomas Graf { 32451e13685SJakub Kicinski struct rtnl_link __rcu **tab; 32551e13685SJakub Kicinski struct rtnl_link *link; 326e2849863SThomas Graf int msgindex; 327e2849863SThomas Graf 32825239ceeSPatrick McHardy BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); 329e2849863SThomas Graf msgindex = rtm_msgindex(msgtype); 330e2849863SThomas Graf 3316853dd48SFlorian Westphal rtnl_lock(); 332addf9b90SFlorian Westphal tab = rtnl_dereference(rtnl_msg_handlers[protocol]); 333addf9b90SFlorian Westphal if (!tab) { 3346853dd48SFlorian Westphal rtnl_unlock(); 335e2849863SThomas Graf return -ENOENT; 3366853dd48SFlorian Westphal } 337e2849863SThomas Graf 33851e13685SJakub Kicinski link = rtnl_dereference(tab[msgindex]); 3394fc29989SYajun Deng RCU_INIT_POINTER(tab[msgindex], NULL); 3406853dd48SFlorian Westphal rtnl_unlock(); 341e2849863SThomas Graf 342addf9b90SFlorian Westphal kfree_rcu(link, rcu); 343addf9b90SFlorian Westphal 344e2849863SThomas Graf return 0; 345e2849863SThomas Graf } 346e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister); 347e2849863SThomas Graf 348e2849863SThomas Graf /** 349e2849863SThomas Graf * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol 350e2849863SThomas Graf * @protocol : Protocol family or PF_UNSPEC 351e2849863SThomas Graf * 352e2849863SThomas Graf * Identical to calling rtnl_unregster() for all registered message types 353e2849863SThomas Graf * of a certain protocol family. 354e2849863SThomas Graf */ 355e2849863SThomas Graf void rtnl_unregister_all(int protocol) 356e2849863SThomas Graf { 35751e13685SJakub Kicinski struct rtnl_link __rcu **tab; 35851e13685SJakub Kicinski struct rtnl_link *link; 359addf9b90SFlorian Westphal int msgindex; 360019a3169SFlorian Westphal 36125239ceeSPatrick McHardy BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); 362e2849863SThomas Graf 363019a3169SFlorian Westphal rtnl_lock(); 36451e13685SJakub Kicinski tab = rtnl_dereference(rtnl_msg_handlers[protocol]); 365f707ef61SSabrina Dubroca if (!tab) { 366f707ef61SSabrina Dubroca rtnl_unlock(); 367f707ef61SSabrina Dubroca return; 368f707ef61SSabrina Dubroca } 3696853dd48SFlorian Westphal RCU_INIT_POINTER(rtnl_msg_handlers[protocol], NULL); 370addf9b90SFlorian Westphal for (msgindex = 0; msgindex < RTM_NR_MSGTYPES; msgindex++) { 37151e13685SJakub Kicinski link = rtnl_dereference(tab[msgindex]); 372addf9b90SFlorian Westphal if (!link) 373addf9b90SFlorian Westphal continue; 374addf9b90SFlorian Westphal 3754fc29989SYajun Deng RCU_INIT_POINTER(tab[msgindex], NULL); 376addf9b90SFlorian Westphal kfree_rcu(link, rcu); 377addf9b90SFlorian Westphal } 378019a3169SFlorian Westphal rtnl_unlock(); 379019a3169SFlorian Westphal 3806853dd48SFlorian Westphal synchronize_net(); 3816853dd48SFlorian Westphal 382addf9b90SFlorian Westphal kfree(tab); 383e2849863SThomas Graf } 384e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister_all); 3851da177e4SLinus Torvalds 38638f7b870SPatrick McHardy static LIST_HEAD(link_ops); 38738f7b870SPatrick McHardy 388c63044f0SEric Dumazet static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind) 389c63044f0SEric Dumazet { 390c63044f0SEric Dumazet const struct rtnl_link_ops *ops; 391c63044f0SEric Dumazet 392c63044f0SEric Dumazet list_for_each_entry(ops, &link_ops, list) { 393c63044f0SEric Dumazet if (!strcmp(ops->kind, kind)) 394c63044f0SEric Dumazet return ops; 395c63044f0SEric Dumazet } 396c63044f0SEric Dumazet return NULL; 397c63044f0SEric Dumazet } 398c63044f0SEric Dumazet 39938f7b870SPatrick McHardy /** 40038f7b870SPatrick McHardy * __rtnl_link_register - Register rtnl_link_ops with rtnetlink. 40138f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to register 40238f7b870SPatrick McHardy * 40338f7b870SPatrick McHardy * The caller must hold the rtnl_mutex. This function should be used 40438f7b870SPatrick McHardy * by drivers that create devices during module initialization. It 40538f7b870SPatrick McHardy * must be called before registering the devices. 40638f7b870SPatrick McHardy * 40738f7b870SPatrick McHardy * Returns 0 on success or a negative error code. 40838f7b870SPatrick McHardy */ 40938f7b870SPatrick McHardy int __rtnl_link_register(struct rtnl_link_ops *ops) 41038f7b870SPatrick McHardy { 411c63044f0SEric Dumazet if (rtnl_link_ops_get(ops->kind)) 412c63044f0SEric Dumazet return -EEXIST; 413c63044f0SEric Dumazet 4148c713dc9SJohannes Berg /* The check for alloc/setup is here because if ops 415b0ab2fabSJiri Pirko * does not have that filled up, it is not possible 416b0ab2fabSJiri Pirko * to use the ops for creating device. So do not 417b0ab2fabSJiri Pirko * fill up dellink as well. That disables rtnl_dellink. 418b0ab2fabSJiri Pirko */ 4198c713dc9SJohannes Berg if ((ops->alloc || ops->setup) && !ops->dellink) 42023289a37SEric Dumazet ops->dellink = unregister_netdevice_queue; 4212d85cba2SPatrick McHardy 42238f7b870SPatrick McHardy list_add_tail(&ops->list, &link_ops); 42338f7b870SPatrick McHardy return 0; 42438f7b870SPatrick McHardy } 42538f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_register); 42638f7b870SPatrick McHardy 42738f7b870SPatrick McHardy /** 42838f7b870SPatrick McHardy * rtnl_link_register - Register rtnl_link_ops with rtnetlink. 42938f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to register 43038f7b870SPatrick McHardy * 43138f7b870SPatrick McHardy * Returns 0 on success or a negative error code. 43238f7b870SPatrick McHardy */ 43338f7b870SPatrick McHardy int rtnl_link_register(struct rtnl_link_ops *ops) 43438f7b870SPatrick McHardy { 43538f7b870SPatrick McHardy int err; 43638f7b870SPatrick McHardy 437ccf8dbcdSKees Cook /* Sanity-check max sizes to avoid stack buffer overflow. */ 438ccf8dbcdSKees Cook if (WARN_ON(ops->maxtype > RTNL_MAX_TYPE || 439ccf8dbcdSKees Cook ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE)) 440ccf8dbcdSKees Cook return -EINVAL; 441ccf8dbcdSKees Cook 44238f7b870SPatrick McHardy rtnl_lock(); 44338f7b870SPatrick McHardy err = __rtnl_link_register(ops); 44438f7b870SPatrick McHardy rtnl_unlock(); 44538f7b870SPatrick McHardy return err; 44638f7b870SPatrick McHardy } 44738f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_register); 44838f7b870SPatrick McHardy 449669f87baSPavel Emelyanov static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops) 450669f87baSPavel Emelyanov { 451669f87baSPavel Emelyanov struct net_device *dev; 45223289a37SEric Dumazet LIST_HEAD(list_kill); 45323289a37SEric Dumazet 454669f87baSPavel Emelyanov for_each_netdev(net, dev) { 45523289a37SEric Dumazet if (dev->rtnl_link_ops == ops) 45623289a37SEric Dumazet ops->dellink(dev, &list_kill); 457669f87baSPavel Emelyanov } 45823289a37SEric Dumazet unregister_netdevice_many(&list_kill); 459669f87baSPavel Emelyanov } 460669f87baSPavel Emelyanov 46138f7b870SPatrick McHardy /** 46238f7b870SPatrick McHardy * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. 46338f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to unregister 46438f7b870SPatrick McHardy * 465554873e5SKirill Tkhai * The caller must hold the rtnl_mutex and guarantee net_namespace_list 466554873e5SKirill Tkhai * integrity (hold pernet_ops_rwsem for writing to close the race 467554873e5SKirill Tkhai * with setup_net() and cleanup_net()). 46838f7b870SPatrick McHardy */ 46938f7b870SPatrick McHardy void __rtnl_link_unregister(struct rtnl_link_ops *ops) 47038f7b870SPatrick McHardy { 471881d966bSEric W. Biederman struct net *net; 4722d85cba2SPatrick McHardy 473881d966bSEric W. Biederman for_each_net(net) { 474669f87baSPavel Emelyanov __rtnl_kill_links(net, ops); 475881d966bSEric W. Biederman } 47638f7b870SPatrick McHardy list_del(&ops->list); 47738f7b870SPatrick McHardy } 47838f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_unregister); 47938f7b870SPatrick McHardy 480200b916fSCong Wang /* Return with the rtnl_lock held when there are no network 481200b916fSCong Wang * devices unregistering in any network namespace. 482200b916fSCong Wang */ 483200b916fSCong Wang static void rtnl_lock_unregistering_all(void) 484200b916fSCong Wang { 485200b916fSCong Wang struct net *net; 486200b916fSCong Wang bool unregistering; 487ff960a73SPeter Zijlstra DEFINE_WAIT_FUNC(wait, woken_wake_function); 488200b916fSCong Wang 489ff960a73SPeter Zijlstra add_wait_queue(&netdev_unregistering_wq, &wait); 490200b916fSCong Wang for (;;) { 491200b916fSCong Wang unregistering = false; 492200b916fSCong Wang rtnl_lock(); 493f0b07bb1SKirill Tkhai /* We held write locked pernet_ops_rwsem, and parallel 494f0b07bb1SKirill Tkhai * setup_net() and cleanup_net() are not possible. 495f0b07bb1SKirill Tkhai */ 496200b916fSCong Wang for_each_net(net) { 497ede6c39cSEric Dumazet if (atomic_read(&net->dev_unreg_count) > 0) { 498200b916fSCong Wang unregistering = true; 499200b916fSCong Wang break; 500200b916fSCong Wang } 501200b916fSCong Wang } 502200b916fSCong Wang if (!unregistering) 503200b916fSCong Wang break; 504200b916fSCong Wang __rtnl_unlock(); 505ff960a73SPeter Zijlstra 506ff960a73SPeter Zijlstra wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); 507200b916fSCong Wang } 508ff960a73SPeter Zijlstra remove_wait_queue(&netdev_unregistering_wq, &wait); 509200b916fSCong Wang } 510200b916fSCong Wang 51138f7b870SPatrick McHardy /** 51238f7b870SPatrick McHardy * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. 51338f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to unregister 51438f7b870SPatrick McHardy */ 51538f7b870SPatrick McHardy void rtnl_link_unregister(struct rtnl_link_ops *ops) 51638f7b870SPatrick McHardy { 5178518e9bbSKirill Tkhai /* Close the race with setup_net() and cleanup_net() */ 5184420bf21SKirill Tkhai down_write(&pernet_ops_rwsem); 519200b916fSCong Wang rtnl_lock_unregistering_all(); 52038f7b870SPatrick McHardy __rtnl_link_unregister(ops); 52138f7b870SPatrick McHardy rtnl_unlock(); 5224420bf21SKirill Tkhai up_write(&pernet_ops_rwsem); 52338f7b870SPatrick McHardy } 52438f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_unregister); 52538f7b870SPatrick McHardy 526ba7d49b1SJiri Pirko static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev) 527ba7d49b1SJiri Pirko { 528ba7d49b1SJiri Pirko struct net_device *master_dev; 529ba7d49b1SJiri Pirko const struct rtnl_link_ops *ops; 5308515ae38SFlorian Westphal size_t size = 0; 531ba7d49b1SJiri Pirko 5328515ae38SFlorian Westphal rcu_read_lock(); 5338515ae38SFlorian Westphal 5348515ae38SFlorian Westphal master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev); 535ba7d49b1SJiri Pirko if (!master_dev) 5368515ae38SFlorian Westphal goto out; 5378515ae38SFlorian Westphal 538ba7d49b1SJiri Pirko ops = master_dev->rtnl_link_ops; 5396049f253SFernando Luis Vazquez Cao if (!ops || !ops->get_slave_size) 5408515ae38SFlorian Westphal goto out; 541ba7d49b1SJiri Pirko /* IFLA_INFO_SLAVE_DATA + nested data */ 5428515ae38SFlorian Westphal size = nla_total_size(sizeof(struct nlattr)) + 543ba7d49b1SJiri Pirko ops->get_slave_size(master_dev, dev); 5448515ae38SFlorian Westphal 5458515ae38SFlorian Westphal out: 5468515ae38SFlorian Westphal rcu_read_unlock(); 5478515ae38SFlorian Westphal return size; 548ba7d49b1SJiri Pirko } 549ba7d49b1SJiri Pirko 55038f7b870SPatrick McHardy static size_t rtnl_link_get_size(const struct net_device *dev) 55138f7b870SPatrick McHardy { 55238f7b870SPatrick McHardy const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 55338f7b870SPatrick McHardy size_t size; 55438f7b870SPatrick McHardy 55538f7b870SPatrick McHardy if (!ops) 55638f7b870SPatrick McHardy return 0; 55738f7b870SPatrick McHardy 558369cf77aSThomas Graf size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */ 559369cf77aSThomas Graf nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */ 56038f7b870SPatrick McHardy 56138f7b870SPatrick McHardy if (ops->get_size) 56238f7b870SPatrick McHardy /* IFLA_INFO_DATA + nested data */ 563369cf77aSThomas Graf size += nla_total_size(sizeof(struct nlattr)) + 56438f7b870SPatrick McHardy ops->get_size(dev); 56538f7b870SPatrick McHardy 56638f7b870SPatrick McHardy if (ops->get_xstats_size) 567369cf77aSThomas Graf /* IFLA_INFO_XSTATS */ 568369cf77aSThomas Graf size += nla_total_size(ops->get_xstats_size(dev)); 56938f7b870SPatrick McHardy 570ba7d49b1SJiri Pirko size += rtnl_link_get_slave_info_data_size(dev); 571ba7d49b1SJiri Pirko 57238f7b870SPatrick McHardy return size; 57338f7b870SPatrick McHardy } 57438f7b870SPatrick McHardy 575f8ff182cSThomas Graf static LIST_HEAD(rtnl_af_ops); 576f8ff182cSThomas Graf 577f8ff182cSThomas Graf static const struct rtnl_af_ops *rtnl_af_lookup(const int family) 578f8ff182cSThomas Graf { 579f8ff182cSThomas Graf const struct rtnl_af_ops *ops; 580f8ff182cSThomas Graf 581a100243dSCong Wang ASSERT_RTNL(); 582a100243dSCong Wang 583a100243dSCong Wang list_for_each_entry(ops, &rtnl_af_ops, list) { 584f8ff182cSThomas Graf if (ops->family == family) 585f8ff182cSThomas Graf return ops; 586f8ff182cSThomas Graf } 587f8ff182cSThomas Graf 588f8ff182cSThomas Graf return NULL; 589f8ff182cSThomas Graf } 590f8ff182cSThomas Graf 591f8ff182cSThomas Graf /** 592f8ff182cSThomas Graf * rtnl_af_register - Register rtnl_af_ops with rtnetlink. 593f8ff182cSThomas Graf * @ops: struct rtnl_af_ops * to register 594f8ff182cSThomas Graf * 595f8ff182cSThomas Graf * Returns 0 on success or a negative error code. 596f8ff182cSThomas Graf */ 5973678a9d8Sstephen hemminger void rtnl_af_register(struct rtnl_af_ops *ops) 598f8ff182cSThomas Graf { 599f8ff182cSThomas Graf rtnl_lock(); 6005fa85a09SFlorian Westphal list_add_tail_rcu(&ops->list, &rtnl_af_ops); 601f8ff182cSThomas Graf rtnl_unlock(); 602f8ff182cSThomas Graf } 603f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_register); 604f8ff182cSThomas Graf 605f8ff182cSThomas Graf /** 606f8ff182cSThomas Graf * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink. 607f8ff182cSThomas Graf * @ops: struct rtnl_af_ops * to unregister 608f8ff182cSThomas Graf */ 609f8ff182cSThomas Graf void rtnl_af_unregister(struct rtnl_af_ops *ops) 610f8ff182cSThomas Graf { 611f8ff182cSThomas Graf rtnl_lock(); 6125fa85a09SFlorian Westphal list_del_rcu(&ops->list); 613f8ff182cSThomas Graf rtnl_unlock(); 6145fa85a09SFlorian Westphal 6155fa85a09SFlorian Westphal synchronize_rcu(); 616f8ff182cSThomas Graf } 617f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_unregister); 618f8ff182cSThomas Graf 619b1974ed0SArad, Ronen static size_t rtnl_link_get_af_size(const struct net_device *dev, 620b1974ed0SArad, Ronen u32 ext_filter_mask) 621f8ff182cSThomas Graf { 622f8ff182cSThomas Graf struct rtnl_af_ops *af_ops; 623f8ff182cSThomas Graf size_t size; 624f8ff182cSThomas Graf 625f8ff182cSThomas Graf /* IFLA_AF_SPEC */ 626f8ff182cSThomas Graf size = nla_total_size(sizeof(struct nlattr)); 627f8ff182cSThomas Graf 6285fa85a09SFlorian Westphal rcu_read_lock(); 6295fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 630f8ff182cSThomas Graf if (af_ops->get_link_af_size) { 631f8ff182cSThomas Graf /* AF_* + nested data */ 632f8ff182cSThomas Graf size += nla_total_size(sizeof(struct nlattr)) + 633b1974ed0SArad, Ronen af_ops->get_link_af_size(dev, ext_filter_mask); 634f8ff182cSThomas Graf } 635f8ff182cSThomas Graf } 6365fa85a09SFlorian Westphal rcu_read_unlock(); 637f8ff182cSThomas Graf 638f8ff182cSThomas Graf return size; 639f8ff182cSThomas Graf } 640f8ff182cSThomas Graf 641ba7d49b1SJiri Pirko static bool rtnl_have_link_slave_info(const struct net_device *dev) 642ba7d49b1SJiri Pirko { 643ba7d49b1SJiri Pirko struct net_device *master_dev; 6444c82a95eSFlorian Westphal bool ret = false; 645ba7d49b1SJiri Pirko 6464c82a95eSFlorian Westphal rcu_read_lock(); 6474c82a95eSFlorian Westphal 6484c82a95eSFlorian Westphal master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev); 649813f020cSJiri Pirko if (master_dev && master_dev->rtnl_link_ops) 6504c82a95eSFlorian Westphal ret = true; 6514c82a95eSFlorian Westphal rcu_read_unlock(); 6524c82a95eSFlorian Westphal return ret; 653ba7d49b1SJiri Pirko } 654ba7d49b1SJiri Pirko 655ba7d49b1SJiri Pirko static int rtnl_link_slave_info_fill(struct sk_buff *skb, 656ba7d49b1SJiri Pirko const struct net_device *dev) 657ba7d49b1SJiri Pirko { 658ba7d49b1SJiri Pirko struct net_device *master_dev; 659ba7d49b1SJiri Pirko const struct rtnl_link_ops *ops; 660ba7d49b1SJiri Pirko struct nlattr *slave_data; 661ba7d49b1SJiri Pirko int err; 662ba7d49b1SJiri Pirko 663ba7d49b1SJiri Pirko master_dev = netdev_master_upper_dev_get((struct net_device *) dev); 664ba7d49b1SJiri Pirko if (!master_dev) 665ba7d49b1SJiri Pirko return 0; 666ba7d49b1SJiri Pirko ops = master_dev->rtnl_link_ops; 667ba7d49b1SJiri Pirko if (!ops) 668ba7d49b1SJiri Pirko return 0; 669ba7d49b1SJiri Pirko if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0) 670ba7d49b1SJiri Pirko return -EMSGSIZE; 671ba7d49b1SJiri Pirko if (ops->fill_slave_info) { 672ae0be8deSMichal Kubecek slave_data = nla_nest_start_noflag(skb, IFLA_INFO_SLAVE_DATA); 673ba7d49b1SJiri Pirko if (!slave_data) 674ba7d49b1SJiri Pirko return -EMSGSIZE; 675ba7d49b1SJiri Pirko err = ops->fill_slave_info(skb, master_dev, dev); 676ba7d49b1SJiri Pirko if (err < 0) 677ba7d49b1SJiri Pirko goto err_cancel_slave_data; 678ba7d49b1SJiri Pirko nla_nest_end(skb, slave_data); 679ba7d49b1SJiri Pirko } 680ba7d49b1SJiri Pirko return 0; 681ba7d49b1SJiri Pirko 682ba7d49b1SJiri Pirko err_cancel_slave_data: 683ba7d49b1SJiri Pirko nla_nest_cancel(skb, slave_data); 684ba7d49b1SJiri Pirko return err; 685ba7d49b1SJiri Pirko } 686ba7d49b1SJiri Pirko 687ba7d49b1SJiri Pirko static int rtnl_link_info_fill(struct sk_buff *skb, 688ba7d49b1SJiri Pirko const struct net_device *dev) 68938f7b870SPatrick McHardy { 69038f7b870SPatrick McHardy const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 691ba7d49b1SJiri Pirko struct nlattr *data; 692ba7d49b1SJiri Pirko int err; 693ba7d49b1SJiri Pirko 694ba7d49b1SJiri Pirko if (!ops) 695ba7d49b1SJiri Pirko return 0; 696ba7d49b1SJiri Pirko if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0) 697ba7d49b1SJiri Pirko return -EMSGSIZE; 698ba7d49b1SJiri Pirko if (ops->fill_xstats) { 699ba7d49b1SJiri Pirko err = ops->fill_xstats(skb, dev); 700ba7d49b1SJiri Pirko if (err < 0) 701ba7d49b1SJiri Pirko return err; 702ba7d49b1SJiri Pirko } 703ba7d49b1SJiri Pirko if (ops->fill_info) { 704ae0be8deSMichal Kubecek data = nla_nest_start_noflag(skb, IFLA_INFO_DATA); 705ba7d49b1SJiri Pirko if (data == NULL) 706ba7d49b1SJiri Pirko return -EMSGSIZE; 707ba7d49b1SJiri Pirko err = ops->fill_info(skb, dev); 708ba7d49b1SJiri Pirko if (err < 0) 709ba7d49b1SJiri Pirko goto err_cancel_data; 710ba7d49b1SJiri Pirko nla_nest_end(skb, data); 711ba7d49b1SJiri Pirko } 712ba7d49b1SJiri Pirko return 0; 713ba7d49b1SJiri Pirko 714ba7d49b1SJiri Pirko err_cancel_data: 715ba7d49b1SJiri Pirko nla_nest_cancel(skb, data); 716ba7d49b1SJiri Pirko return err; 717ba7d49b1SJiri Pirko } 718ba7d49b1SJiri Pirko 719ba7d49b1SJiri Pirko static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev) 720ba7d49b1SJiri Pirko { 721ba7d49b1SJiri Pirko struct nlattr *linkinfo; 72238f7b870SPatrick McHardy int err = -EMSGSIZE; 72338f7b870SPatrick McHardy 724ae0be8deSMichal Kubecek linkinfo = nla_nest_start_noflag(skb, IFLA_LINKINFO); 72538f7b870SPatrick McHardy if (linkinfo == NULL) 72638f7b870SPatrick McHardy goto out; 72738f7b870SPatrick McHardy 728ba7d49b1SJiri Pirko err = rtnl_link_info_fill(skb, dev); 72938f7b870SPatrick McHardy if (err < 0) 73038f7b870SPatrick McHardy goto err_cancel_link; 731ba7d49b1SJiri Pirko 732ba7d49b1SJiri Pirko err = rtnl_link_slave_info_fill(skb, dev); 73338f7b870SPatrick McHardy if (err < 0) 734ba7d49b1SJiri Pirko goto err_cancel_link; 73538f7b870SPatrick McHardy 73638f7b870SPatrick McHardy nla_nest_end(skb, linkinfo); 73738f7b870SPatrick McHardy return 0; 73838f7b870SPatrick McHardy 73938f7b870SPatrick McHardy err_cancel_link: 74038f7b870SPatrick McHardy nla_nest_cancel(skb, linkinfo); 74138f7b870SPatrick McHardy out: 74238f7b870SPatrick McHardy return err; 74338f7b870SPatrick McHardy } 74438f7b870SPatrick McHardy 74595c96174SEric Dumazet int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo) 7461da177e4SLinus Torvalds { 74797c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 7481da177e4SLinus Torvalds 749cfdf0d9aSYajun Deng return nlmsg_notify(rtnl, skb, pid, group, echo, GFP_KERNEL); 7501da177e4SLinus Torvalds } 7511da177e4SLinus Torvalds 75297c53cacSDenis V. Lunev int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid) 7532942e900SThomas Graf { 75497c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 75597c53cacSDenis V. Lunev 7562942e900SThomas Graf return nlmsg_unicast(rtnl, skb, pid); 7572942e900SThomas Graf } 758e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unicast); 7592942e900SThomas Graf 7601ce85fe4SPablo Neira Ayuso void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group, 76197676b6bSThomas Graf struct nlmsghdr *nlh, gfp_t flags) 76297676b6bSThomas Graf { 76397c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 76497676b6bSThomas Graf 765f9b282b3SYajun Deng nlmsg_notify(rtnl, skb, pid, group, nlmsg_report(nlh), flags); 76697676b6bSThomas Graf } 767e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_notify); 76897676b6bSThomas Graf 76997c53cacSDenis V. Lunev void rtnl_set_sk_err(struct net *net, u32 group, int error) 77097676b6bSThomas Graf { 77197c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 77297c53cacSDenis V. Lunev 77397676b6bSThomas Graf netlink_set_err(rtnl, 0, group, error); 77497676b6bSThomas Graf } 775e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_set_sk_err); 77697676b6bSThomas Graf 7771da177e4SLinus Torvalds int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics) 7781da177e4SLinus Torvalds { 7792d7202bfSThomas Graf struct nlattr *mx; 7802d7202bfSThomas Graf int i, valid = 0; 7811da177e4SLinus Torvalds 782c22a133aSDavid Ahern /* nothing is dumped for dst_default_metrics, so just skip the loop */ 783c22a133aSDavid Ahern if (metrics == dst_default_metrics.metrics) 784c22a133aSDavid Ahern return 0; 785c22a133aSDavid Ahern 786ae0be8deSMichal Kubecek mx = nla_nest_start_noflag(skb, RTA_METRICS); 7872d7202bfSThomas Graf if (mx == NULL) 7882d7202bfSThomas Graf return -ENOBUFS; 7892d7202bfSThomas Graf 7901da177e4SLinus Torvalds for (i = 0; i < RTAX_MAX; i++) { 7912d7202bfSThomas Graf if (metrics[i]) { 792ea697639SDaniel Borkmann if (i == RTAX_CC_ALGO - 1) { 793ea697639SDaniel Borkmann char tmp[TCP_CA_NAME_MAX], *name; 794ea697639SDaniel Borkmann 795ea697639SDaniel Borkmann name = tcp_ca_get_name_by_key(metrics[i], tmp); 796ea697639SDaniel Borkmann if (!name) 797ea697639SDaniel Borkmann continue; 798ea697639SDaniel Borkmann if (nla_put_string(skb, i + 1, name)) 799ea697639SDaniel Borkmann goto nla_put_failure; 800c3a8d947SDaniel Borkmann } else if (i == RTAX_FEATURES - 1) { 801c3a8d947SDaniel Borkmann u32 user_features = metrics[i] & RTAX_FEATURE_MASK; 802c3a8d947SDaniel Borkmann 803f8edcd12SPhil Sutter if (!user_features) 804f8edcd12SPhil Sutter continue; 805c3a8d947SDaniel Borkmann BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK); 806c3a8d947SDaniel Borkmann if (nla_put_u32(skb, i + 1, user_features)) 807c3a8d947SDaniel Borkmann goto nla_put_failure; 808ea697639SDaniel Borkmann } else { 809a6574349SDavid S. Miller if (nla_put_u32(skb, i + 1, metrics[i])) 810a6574349SDavid S. Miller goto nla_put_failure; 8111da177e4SLinus Torvalds } 812ea697639SDaniel Borkmann valid++; 813ea697639SDaniel Borkmann } 8142d7202bfSThomas Graf } 8151da177e4SLinus Torvalds 816a57d27fcSDavid S. Miller if (!valid) { 817a57d27fcSDavid S. Miller nla_nest_cancel(skb, mx); 818a57d27fcSDavid S. Miller return 0; 819a57d27fcSDavid S. Miller } 8202d7202bfSThomas Graf 8212d7202bfSThomas Graf return nla_nest_end(skb, mx); 8222d7202bfSThomas Graf 8232d7202bfSThomas Graf nla_put_failure: 824bc3ed28cSThomas Graf nla_nest_cancel(skb, mx); 825bc3ed28cSThomas Graf return -EMSGSIZE; 8261da177e4SLinus Torvalds } 827e0d087afSEric Dumazet EXPORT_SYMBOL(rtnetlink_put_metrics); 8281da177e4SLinus Torvalds 829e3703b3dSThomas Graf int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id, 83087a50699SDavid S. Miller long expires, u32 error) 831e3703b3dSThomas Graf { 832e3703b3dSThomas Graf struct rta_cacheinfo ci = { 833e3703b3dSThomas Graf .rta_error = error, 834e3703b3dSThomas Graf .rta_id = id, 835e3703b3dSThomas Graf }; 836e3703b3dSThomas Graf 8373940746dSDavid Ahern if (dst) { 8383940746dSDavid Ahern ci.rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse); 8393940746dSDavid Ahern ci.rta_used = dst->__use; 8403940746dSDavid Ahern ci.rta_clntref = atomic_read(&dst->__refcnt); 8413940746dSDavid Ahern } 8428253947eSLi Wei if (expires) { 8438253947eSLi Wei unsigned long clock; 844e3703b3dSThomas Graf 8458253947eSLi Wei clock = jiffies_to_clock_t(abs(expires)); 8468253947eSLi Wei clock = min_t(unsigned long, clock, INT_MAX); 8478253947eSLi Wei ci.rta_expires = (expires > 0) ? clock : -clock; 8488253947eSLi Wei } 849e3703b3dSThomas Graf return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci); 850e3703b3dSThomas Graf } 851e3703b3dSThomas Graf EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo); 8521da177e4SLinus Torvalds 85393b2d4a2SDavid S. Miller static void set_operstate(struct net_device *dev, unsigned char transition) 854b00055aaSStefan Rompf { 855b00055aaSStefan Rompf unsigned char operstate = dev->operstate; 856b00055aaSStefan Rompf 857b00055aaSStefan Rompf switch (transition) { 858b00055aaSStefan Rompf case IF_OPER_UP: 859b00055aaSStefan Rompf if ((operstate == IF_OPER_DORMANT || 860eec517cdSAndrew Lunn operstate == IF_OPER_TESTING || 861b00055aaSStefan Rompf operstate == IF_OPER_UNKNOWN) && 862eec517cdSAndrew Lunn !netif_dormant(dev) && !netif_testing(dev)) 863b00055aaSStefan Rompf operstate = IF_OPER_UP; 864b00055aaSStefan Rompf break; 865b00055aaSStefan Rompf 866eec517cdSAndrew Lunn case IF_OPER_TESTING: 867eec517cdSAndrew Lunn if (operstate == IF_OPER_UP || 868eec517cdSAndrew Lunn operstate == IF_OPER_UNKNOWN) 869eec517cdSAndrew Lunn operstate = IF_OPER_TESTING; 870eec517cdSAndrew Lunn break; 871eec517cdSAndrew Lunn 872b00055aaSStefan Rompf case IF_OPER_DORMANT: 873b00055aaSStefan Rompf if (operstate == IF_OPER_UP || 874b00055aaSStefan Rompf operstate == IF_OPER_UNKNOWN) 875b00055aaSStefan Rompf operstate = IF_OPER_DORMANT; 876b00055aaSStefan Rompf break; 8773ff50b79SStephen Hemminger } 878b00055aaSStefan Rompf 879b00055aaSStefan Rompf if (dev->operstate != operstate) { 880fd888e85SSebastian Andrzej Siewior write_lock(&dev_base_lock); 881b00055aaSStefan Rompf dev->operstate = operstate; 882fd888e85SSebastian Andrzej Siewior write_unlock(&dev_base_lock); 883b00055aaSStefan Rompf netdev_state_change(dev); 88493b2d4a2SDavid S. Miller } 885b00055aaSStefan Rompf } 886b00055aaSStefan Rompf 887b1beb681SJiri Benc static unsigned int rtnl_dev_get_flags(const struct net_device *dev) 888b1beb681SJiri Benc { 889b1beb681SJiri Benc return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) | 890b1beb681SJiri Benc (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI)); 891b1beb681SJiri Benc } 892b1beb681SJiri Benc 8933729d502SPatrick McHardy static unsigned int rtnl_dev_combine_flags(const struct net_device *dev, 8943729d502SPatrick McHardy const struct ifinfomsg *ifm) 8953729d502SPatrick McHardy { 8963729d502SPatrick McHardy unsigned int flags = ifm->ifi_flags; 8973729d502SPatrick McHardy 8983729d502SPatrick McHardy /* bugwards compatibility: ifi_change == 0 is treated as ~0 */ 8993729d502SPatrick McHardy if (ifm->ifi_change) 9003729d502SPatrick McHardy flags = (flags & ifm->ifi_change) | 901b1beb681SJiri Benc (rtnl_dev_get_flags(dev) & ~ifm->ifi_change); 9023729d502SPatrick McHardy 9033729d502SPatrick McHardy return flags; 9043729d502SPatrick McHardy } 9053729d502SPatrick McHardy 906b60c5115SThomas Graf static void copy_rtnl_link_stats(struct rtnl_link_stats *a, 907be1f3c2cSBen Hutchings const struct rtnl_link_stats64 *b) 9081da177e4SLinus Torvalds { 909b60c5115SThomas Graf a->rx_packets = b->rx_packets; 910b60c5115SThomas Graf a->tx_packets = b->tx_packets; 911b60c5115SThomas Graf a->rx_bytes = b->rx_bytes; 912b60c5115SThomas Graf a->tx_bytes = b->tx_bytes; 913b60c5115SThomas Graf a->rx_errors = b->rx_errors; 914b60c5115SThomas Graf a->tx_errors = b->tx_errors; 915b60c5115SThomas Graf a->rx_dropped = b->rx_dropped; 916b60c5115SThomas Graf a->tx_dropped = b->tx_dropped; 917b60c5115SThomas Graf 918b60c5115SThomas Graf a->multicast = b->multicast; 919b60c5115SThomas Graf a->collisions = b->collisions; 920b60c5115SThomas Graf 921b60c5115SThomas Graf a->rx_length_errors = b->rx_length_errors; 922b60c5115SThomas Graf a->rx_over_errors = b->rx_over_errors; 923b60c5115SThomas Graf a->rx_crc_errors = b->rx_crc_errors; 924b60c5115SThomas Graf a->rx_frame_errors = b->rx_frame_errors; 925b60c5115SThomas Graf a->rx_fifo_errors = b->rx_fifo_errors; 926b60c5115SThomas Graf a->rx_missed_errors = b->rx_missed_errors; 927b60c5115SThomas Graf 928b60c5115SThomas Graf a->tx_aborted_errors = b->tx_aborted_errors; 929b60c5115SThomas Graf a->tx_carrier_errors = b->tx_carrier_errors; 930b60c5115SThomas Graf a->tx_fifo_errors = b->tx_fifo_errors; 931b60c5115SThomas Graf a->tx_heartbeat_errors = b->tx_heartbeat_errors; 932b60c5115SThomas Graf a->tx_window_errors = b->tx_window_errors; 933b60c5115SThomas Graf 934b60c5115SThomas Graf a->rx_compressed = b->rx_compressed; 935b60c5115SThomas Graf a->tx_compressed = b->tx_compressed; 9366e7333d3SJarod Wilson 9376e7333d3SJarod Wilson a->rx_nohandler = b->rx_nohandler; 93810708f37SJan Engelhardt } 93910708f37SJan Engelhardt 940c02db8c6SChris Wright /* All VF info */ 941115c9b81SGreg Rose static inline int rtnl_vfinfo_size(const struct net_device *dev, 942115c9b81SGreg Rose u32 ext_filter_mask) 943ebc08a6fSWilliams, Mitch A { 9449af15c38SPhil Sutter if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) { 945c02db8c6SChris Wright int num_vfs = dev_num_vf(dev->dev.parent); 9467e75f74aSSabrina Dubroca size_t size = nla_total_size(0); 947045de01aSScott Feldman size += num_vfs * 9487e75f74aSSabrina Dubroca (nla_total_size(0) + 9497e75f74aSSabrina Dubroca nla_total_size(sizeof(struct ifla_vf_mac)) + 95075345f88SDenis Kirjanov nla_total_size(sizeof(struct ifla_vf_broadcast)) + 9517e75f74aSSabrina Dubroca nla_total_size(sizeof(struct ifla_vf_vlan)) + 9527e75f74aSSabrina Dubroca nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */ 95379aab093SMoshe Shemesh nla_total_size(MAX_VLAN_LIST_LEN * 95479aab093SMoshe Shemesh sizeof(struct ifla_vf_vlan_info)) + 955ed616689SSucheta Chakraborty nla_total_size(sizeof(struct ifla_vf_spoofchk)) + 9567e75f74aSSabrina Dubroca nla_total_size(sizeof(struct ifla_vf_tx_rate)) + 957945a3676SJiri Benc nla_total_size(sizeof(struct ifla_vf_rate)) + 95801a3d796SVlad Zolotarov nla_total_size(sizeof(struct ifla_vf_link_state)) + 9593b766cd8SEran Ben Elisha nla_total_size(sizeof(struct ifla_vf_rss_query_en)) + 9607e75f74aSSabrina Dubroca nla_total_size(0) + /* nest IFLA_VF_STATS */ 9613b766cd8SEran Ben Elisha /* IFLA_VF_STATS_RX_PACKETS */ 962343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 9633b766cd8SEran Ben Elisha /* IFLA_VF_STATS_TX_PACKETS */ 964343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 9653b766cd8SEran Ben Elisha /* IFLA_VF_STATS_RX_BYTES */ 966343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 9673b766cd8SEran Ben Elisha /* IFLA_VF_STATS_TX_BYTES */ 968343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 9693b766cd8SEran Ben Elisha /* IFLA_VF_STATS_BROADCAST */ 970343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 9713b766cd8SEran Ben Elisha /* IFLA_VF_STATS_MULTICAST */ 972343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 973c5a9f6f0SEugenia Emantayev /* IFLA_VF_STATS_RX_DROPPED */ 974c5a9f6f0SEugenia Emantayev nla_total_size_64bit(sizeof(__u64)) + 975c5a9f6f0SEugenia Emantayev /* IFLA_VF_STATS_TX_DROPPED */ 976c5a9f6f0SEugenia Emantayev nla_total_size_64bit(sizeof(__u64)) + 977dd461d6aSHiroshi Shimamoto nla_total_size(sizeof(struct ifla_vf_trust))); 978c02db8c6SChris Wright return size; 979c02db8c6SChris Wright } else 980ebc08a6fSWilliams, Mitch A return 0; 981ebc08a6fSWilliams, Mitch A } 982ebc08a6fSWilliams, Mitch A 983c53864fdSDavid Gibson static size_t rtnl_port_size(const struct net_device *dev, 984c53864fdSDavid Gibson u32 ext_filter_mask) 98557b61080SScott Feldman { 98657b61080SScott Feldman size_t port_size = nla_total_size(4) /* PORT_VF */ 98757b61080SScott Feldman + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */ 98857b61080SScott Feldman + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */ 98957b61080SScott Feldman + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */ 99057b61080SScott Feldman + nla_total_size(1) /* PROT_VDP_REQUEST */ 99157b61080SScott Feldman + nla_total_size(2); /* PORT_VDP_RESPONSE */ 99257b61080SScott Feldman size_t vf_ports_size = nla_total_size(sizeof(struct nlattr)); 99357b61080SScott Feldman size_t vf_port_size = nla_total_size(sizeof(struct nlattr)) 99457b61080SScott Feldman + port_size; 99557b61080SScott Feldman size_t port_self_size = nla_total_size(sizeof(struct nlattr)) 99657b61080SScott Feldman + port_size; 99757b61080SScott Feldman 998c53864fdSDavid Gibson if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent || 999c53864fdSDavid Gibson !(ext_filter_mask & RTEXT_FILTER_VF)) 100057b61080SScott Feldman return 0; 100157b61080SScott Feldman if (dev_num_vf(dev->dev.parent)) 100257b61080SScott Feldman return port_self_size + vf_ports_size + 100357b61080SScott Feldman vf_port_size * dev_num_vf(dev->dev.parent); 100457b61080SScott Feldman else 100557b61080SScott Feldman return port_self_size; 100657b61080SScott Feldman } 100757b61080SScott Feldman 1008b5cdae32SDavid S. Miller static size_t rtnl_xdp_size(void) 1009d1fdd913SBrenden Blanco { 1010b3cfaa31SSabrina Dubroca size_t xdp_size = nla_total_size(0) + /* nest IFLA_XDP */ 101158038695SMartin KaFai Lau nla_total_size(1) + /* XDP_ATTACHED */ 1012a25717d2SJakub Kicinski nla_total_size(4) + /* XDP_PROG_ID (or 1st mode) */ 10134f91da26SJakub Kicinski nla_total_size(4); /* XDP_<mode>_PROG_ID */ 1014d1fdd913SBrenden Blanco 1015d1fdd913SBrenden Blanco return xdp_size; 1016d1fdd913SBrenden Blanco } 1017d1fdd913SBrenden Blanco 101888f4fb0cSJiri Pirko static size_t rtnl_prop_list_size(const struct net_device *dev) 101988f4fb0cSJiri Pirko { 102088f4fb0cSJiri Pirko struct netdev_name_node *name_node; 102188f4fb0cSJiri Pirko size_t size; 102288f4fb0cSJiri Pirko 102388f4fb0cSJiri Pirko if (list_empty(&dev->name_node->list)) 102488f4fb0cSJiri Pirko return 0; 102588f4fb0cSJiri Pirko size = nla_total_size(0); 102688f4fb0cSJiri Pirko list_for_each_entry(name_node, &dev->name_node->list, list) 102788f4fb0cSJiri Pirko size += nla_total_size(ALTIFNAMSIZ); 102888f4fb0cSJiri Pirko return size; 102988f4fb0cSJiri Pirko } 103088f4fb0cSJiri Pirko 1031829eb208SRoopa Prabhu static size_t rtnl_proto_down_size(const struct net_device *dev) 1032829eb208SRoopa Prabhu { 1033829eb208SRoopa Prabhu size_t size = nla_total_size(1); 1034829eb208SRoopa Prabhu 1035829eb208SRoopa Prabhu if (dev->proto_down_reason) 1036829eb208SRoopa Prabhu size += nla_total_size(0) + nla_total_size(4); 1037829eb208SRoopa Prabhu 1038829eb208SRoopa Prabhu return size; 1039829eb208SRoopa Prabhu } 1040829eb208SRoopa Prabhu 1041115c9b81SGreg Rose static noinline size_t if_nlmsg_size(const struct net_device *dev, 1042115c9b81SGreg Rose u32 ext_filter_mask) 1043339bf98fSThomas Graf { 1044339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 1045339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 10460b815a1aSStephen Hemminger + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */ 1047339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */ 1048270cb4d0SNicolas Dichtel + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap)) 1049339bf98fSThomas Graf + nla_total_size(sizeof(struct rtnl_link_stats)) 105035c58459SDavid S. Miller + nla_total_size_64bit(sizeof(struct rtnl_link_stats64)) 1051339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 1052339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */ 1053339bf98fSThomas Graf + nla_total_size(4) /* IFLA_TXQLEN */ 1054339bf98fSThomas Graf + nla_total_size(4) /* IFLA_WEIGHT */ 1055339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MTU */ 1056339bf98fSThomas Graf + nla_total_size(4) /* IFLA_LINK */ 1057339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MASTER */ 10589a57247fSJiri Pirko + nla_total_size(1) /* IFLA_CARRIER */ 1059edbc0bb3SBen Greear + nla_total_size(4) /* IFLA_PROMISCUITY */ 106076ff5cc9SJiri Pirko + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */ 106176ff5cc9SJiri Pirko + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */ 10626919756cSTobias Klauser + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */ 10636919756cSTobias Klauser + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */ 1064eac1b93cSCoco Li + nla_total_size(4) /* IFLA_GRO_MAX_SIZE */ 1065339bf98fSThomas Graf + nla_total_size(1) /* IFLA_OPERSTATE */ 106638f7b870SPatrick McHardy + nla_total_size(1) /* IFLA_LINKMODE */ 10672d3b479dSdavid decotigny + nla_total_size(4) /* IFLA_CARRIER_CHANGES */ 1068d37512a2SNicolas Dichtel + nla_total_size(4) /* IFLA_LINK_NETNSID */ 1069db833d40SSerhey Popovych + nla_total_size(4) /* IFLA_GROUP */ 1070115c9b81SGreg Rose + nla_total_size(ext_filter_mask 1071115c9b81SGreg Rose & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */ 1072115c9b81SGreg Rose + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */ 1073c53864fdSDavid Gibson + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */ 1074f8ff182cSThomas Graf + rtnl_link_get_size(dev) /* IFLA_LINKINFO */ 1075b1974ed0SArad, Ronen + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */ 107682f28412SJiri Pirko + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */ 107788d6378bSAnuradha Karuppiah + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */ 1078c57c7a95SNicolas Dichtel + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */ 1079b5cdae32SDavid S. Miller + rtnl_xdp_size() /* IFLA_XDP */ 10803d3ea5afSVlad Yasevich + nla_total_size(4) /* IFLA_EVENT */ 10816621dd29SNicolas Dichtel + nla_total_size(4) /* IFLA_NEW_NETNSID */ 108238e01b30SNicolas Dichtel + nla_total_size(4) /* IFLA_NEW_IFINDEX */ 1083829eb208SRoopa Prabhu + rtnl_proto_down_size(dev) /* proto down */ 10847e4a8d5aSChristian Brauner + nla_total_size(4) /* IFLA_TARGET_NETNSID */ 1085b2d3bcfaSDavid Decotigny + nla_total_size(4) /* IFLA_CARRIER_UP_COUNT */ 1086b2d3bcfaSDavid Decotigny + nla_total_size(4) /* IFLA_CARRIER_DOWN_COUNT */ 10873e7a50ceSStephen Hemminger + nla_total_size(4) /* IFLA_MIN_MTU */ 10883e7a50ceSStephen Hemminger + nla_total_size(4) /* IFLA_MAX_MTU */ 108988f4fb0cSJiri Pirko + rtnl_prop_list_size(dev) 1090f74877a5SMichal Kubecek + nla_total_size(MAX_ADDR_LEN) /* IFLA_PERM_ADDRESS */ 109179e1ad14SJiri Benc + 0; 1092339bf98fSThomas Graf } 1093339bf98fSThomas Graf 109457b61080SScott Feldman static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev) 109557b61080SScott Feldman { 109657b61080SScott Feldman struct nlattr *vf_ports; 109757b61080SScott Feldman struct nlattr *vf_port; 109857b61080SScott Feldman int vf; 109957b61080SScott Feldman int err; 110057b61080SScott Feldman 1101ae0be8deSMichal Kubecek vf_ports = nla_nest_start_noflag(skb, IFLA_VF_PORTS); 110257b61080SScott Feldman if (!vf_ports) 110357b61080SScott Feldman return -EMSGSIZE; 110457b61080SScott Feldman 110557b61080SScott Feldman for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) { 1106ae0be8deSMichal Kubecek vf_port = nla_nest_start_noflag(skb, IFLA_VF_PORT); 11078ca94183SScott Feldman if (!vf_port) 11088ca94183SScott Feldman goto nla_put_failure; 1109a6574349SDavid S. Miller if (nla_put_u32(skb, IFLA_PORT_VF, vf)) 1110a6574349SDavid S. Miller goto nla_put_failure; 111157b61080SScott Feldman err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb); 11128ca94183SScott Feldman if (err == -EMSGSIZE) 11138ca94183SScott Feldman goto nla_put_failure; 111457b61080SScott Feldman if (err) { 111557b61080SScott Feldman nla_nest_cancel(skb, vf_port); 111657b61080SScott Feldman continue; 111757b61080SScott Feldman } 111857b61080SScott Feldman nla_nest_end(skb, vf_port); 111957b61080SScott Feldman } 112057b61080SScott Feldman 112157b61080SScott Feldman nla_nest_end(skb, vf_ports); 112257b61080SScott Feldman 112357b61080SScott Feldman return 0; 11248ca94183SScott Feldman 11258ca94183SScott Feldman nla_put_failure: 11268ca94183SScott Feldman nla_nest_cancel(skb, vf_ports); 11278ca94183SScott Feldman return -EMSGSIZE; 112857b61080SScott Feldman } 112957b61080SScott Feldman 113057b61080SScott Feldman static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev) 113157b61080SScott Feldman { 113257b61080SScott Feldman struct nlattr *port_self; 113357b61080SScott Feldman int err; 113457b61080SScott Feldman 1135ae0be8deSMichal Kubecek port_self = nla_nest_start_noflag(skb, IFLA_PORT_SELF); 113657b61080SScott Feldman if (!port_self) 113757b61080SScott Feldman return -EMSGSIZE; 113857b61080SScott Feldman 113957b61080SScott Feldman err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb); 114057b61080SScott Feldman if (err) { 114157b61080SScott Feldman nla_nest_cancel(skb, port_self); 11428ca94183SScott Feldman return (err == -EMSGSIZE) ? err : 0; 114357b61080SScott Feldman } 114457b61080SScott Feldman 114557b61080SScott Feldman nla_nest_end(skb, port_self); 114657b61080SScott Feldman 114757b61080SScott Feldman return 0; 114857b61080SScott Feldman } 114957b61080SScott Feldman 1150c53864fdSDavid Gibson static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev, 1151c53864fdSDavid Gibson u32 ext_filter_mask) 115257b61080SScott Feldman { 115357b61080SScott Feldman int err; 115457b61080SScott Feldman 1155c53864fdSDavid Gibson if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent || 1156c53864fdSDavid Gibson !(ext_filter_mask & RTEXT_FILTER_VF)) 115757b61080SScott Feldman return 0; 115857b61080SScott Feldman 115957b61080SScott Feldman err = rtnl_port_self_fill(skb, dev); 116057b61080SScott Feldman if (err) 116157b61080SScott Feldman return err; 116257b61080SScott Feldman 116357b61080SScott Feldman if (dev_num_vf(dev->dev.parent)) { 116457b61080SScott Feldman err = rtnl_vf_ports_fill(skb, dev); 116557b61080SScott Feldman if (err) 116657b61080SScott Feldman return err; 116757b61080SScott Feldman } 116857b61080SScott Feldman 116957b61080SScott Feldman return 0; 117057b61080SScott Feldman } 117157b61080SScott Feldman 117266cae9edSJiri Pirko static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev) 117366cae9edSJiri Pirko { 117466cae9edSJiri Pirko int err; 117502637fceSJiri Pirko struct netdev_phys_item_id ppid; 117666cae9edSJiri Pirko 117766cae9edSJiri Pirko err = dev_get_phys_port_id(dev, &ppid); 117866cae9edSJiri Pirko if (err) { 117966cae9edSJiri Pirko if (err == -EOPNOTSUPP) 118066cae9edSJiri Pirko return 0; 118166cae9edSJiri Pirko return err; 118266cae9edSJiri Pirko } 118366cae9edSJiri Pirko 118466cae9edSJiri Pirko if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id)) 118566cae9edSJiri Pirko return -EMSGSIZE; 118666cae9edSJiri Pirko 118766cae9edSJiri Pirko return 0; 118866cae9edSJiri Pirko } 118966cae9edSJiri Pirko 1190db24a904SDavid Ahern static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev) 1191db24a904SDavid Ahern { 1192db24a904SDavid Ahern char name[IFNAMSIZ]; 1193db24a904SDavid Ahern int err; 1194db24a904SDavid Ahern 1195db24a904SDavid Ahern err = dev_get_phys_port_name(dev, name, sizeof(name)); 1196db24a904SDavid Ahern if (err) { 1197db24a904SDavid Ahern if (err == -EOPNOTSUPP) 1198db24a904SDavid Ahern return 0; 1199db24a904SDavid Ahern return err; 1200db24a904SDavid Ahern } 1201db24a904SDavid Ahern 120277ef033bSMichal Schmidt if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name)) 1203db24a904SDavid Ahern return -EMSGSIZE; 1204db24a904SDavid Ahern 1205db24a904SDavid Ahern return 0; 1206db24a904SDavid Ahern } 1207db24a904SDavid Ahern 120882f28412SJiri Pirko static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev) 120982f28412SJiri Pirko { 1210bccb3025SFlorian Fainelli struct netdev_phys_item_id ppid = { }; 121182f28412SJiri Pirko int err; 121282f28412SJiri Pirko 1213bccb3025SFlorian Fainelli err = dev_get_port_parent_id(dev, &ppid, false); 121482f28412SJiri Pirko if (err) { 121582f28412SJiri Pirko if (err == -EOPNOTSUPP) 121682f28412SJiri Pirko return 0; 121782f28412SJiri Pirko return err; 121882f28412SJiri Pirko } 121982f28412SJiri Pirko 1220bccb3025SFlorian Fainelli if (nla_put(skb, IFLA_PHYS_SWITCH_ID, ppid.id_len, ppid.id)) 122182f28412SJiri Pirko return -EMSGSIZE; 122282f28412SJiri Pirko 122382f28412SJiri Pirko return 0; 122482f28412SJiri Pirko } 122582f28412SJiri Pirko 1226b22b941bSHannes Frederic Sowa static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb, 1227b22b941bSHannes Frederic Sowa struct net_device *dev) 1228b22b941bSHannes Frederic Sowa { 1229550bce59SRoopa Prabhu struct rtnl_link_stats64 *sp; 1230b22b941bSHannes Frederic Sowa struct nlattr *attr; 1231b22b941bSHannes Frederic Sowa 123258414d32SNicolas Dichtel attr = nla_reserve_64bit(skb, IFLA_STATS64, 123358414d32SNicolas Dichtel sizeof(struct rtnl_link_stats64), IFLA_PAD); 1234b22b941bSHannes Frederic Sowa if (!attr) 1235b22b941bSHannes Frederic Sowa return -EMSGSIZE; 1236b22b941bSHannes Frederic Sowa 1237550bce59SRoopa Prabhu sp = nla_data(attr); 1238550bce59SRoopa Prabhu dev_get_stats(dev, sp); 1239550bce59SRoopa Prabhu 1240550bce59SRoopa Prabhu attr = nla_reserve(skb, IFLA_STATS, 1241550bce59SRoopa Prabhu sizeof(struct rtnl_link_stats)); 1242550bce59SRoopa Prabhu if (!attr) 1243550bce59SRoopa Prabhu return -EMSGSIZE; 1244550bce59SRoopa Prabhu 1245550bce59SRoopa Prabhu copy_rtnl_link_stats(nla_data(attr), sp); 1246b22b941bSHannes Frederic Sowa 1247b22b941bSHannes Frederic Sowa return 0; 1248b22b941bSHannes Frederic Sowa } 1249b22b941bSHannes Frederic Sowa 1250b22b941bSHannes Frederic Sowa static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb, 1251b22b941bSHannes Frederic Sowa struct net_device *dev, 1252b22b941bSHannes Frederic Sowa int vfs_num, 1253b22b941bSHannes Frederic Sowa struct nlattr *vfinfo) 1254b22b941bSHannes Frederic Sowa { 1255b22b941bSHannes Frederic Sowa struct ifla_vf_rss_query_en vf_rss_query_en; 125679aab093SMoshe Shemesh struct nlattr *vf, *vfstats, *vfvlanlist; 1257b22b941bSHannes Frederic Sowa struct ifla_vf_link_state vf_linkstate; 125879aab093SMoshe Shemesh struct ifla_vf_vlan_info vf_vlan_info; 1259b22b941bSHannes Frederic Sowa struct ifla_vf_spoofchk vf_spoofchk; 1260b22b941bSHannes Frederic Sowa struct ifla_vf_tx_rate vf_tx_rate; 1261b22b941bSHannes Frederic Sowa struct ifla_vf_stats vf_stats; 1262b22b941bSHannes Frederic Sowa struct ifla_vf_trust vf_trust; 1263b22b941bSHannes Frederic Sowa struct ifla_vf_vlan vf_vlan; 1264b22b941bSHannes Frederic Sowa struct ifla_vf_rate vf_rate; 1265b22b941bSHannes Frederic Sowa struct ifla_vf_mac vf_mac; 126675345f88SDenis Kirjanov struct ifla_vf_broadcast vf_broadcast; 1267b22b941bSHannes Frederic Sowa struct ifla_vf_info ivi; 126830aad417SDanit Goldberg struct ifla_vf_guid node_guid; 126930aad417SDanit Goldberg struct ifla_vf_guid port_guid; 1270b22b941bSHannes Frederic Sowa 12710eed9cf5SMintz, Yuval memset(&ivi, 0, sizeof(ivi)); 12720eed9cf5SMintz, Yuval 1273b22b941bSHannes Frederic Sowa /* Not all SR-IOV capable drivers support the 1274b22b941bSHannes Frederic Sowa * spoofcheck and "RSS query enable" query. Preset to 1275b22b941bSHannes Frederic Sowa * -1 so the user space tool can detect that the driver 1276b22b941bSHannes Frederic Sowa * didn't report anything. 1277b22b941bSHannes Frederic Sowa */ 1278b22b941bSHannes Frederic Sowa ivi.spoofchk = -1; 1279b22b941bSHannes Frederic Sowa ivi.rss_query_en = -1; 1280b22b941bSHannes Frederic Sowa ivi.trusted = -1; 1281b22b941bSHannes Frederic Sowa /* The default value for VF link state is "auto" 1282b22b941bSHannes Frederic Sowa * IFLA_VF_LINK_STATE_AUTO which equals zero 1283b22b941bSHannes Frederic Sowa */ 1284b22b941bSHannes Frederic Sowa ivi.linkstate = 0; 128579aab093SMoshe Shemesh /* VLAN Protocol by default is 802.1Q */ 128679aab093SMoshe Shemesh ivi.vlan_proto = htons(ETH_P_8021Q); 1287b22b941bSHannes Frederic Sowa if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi)) 1288b22b941bSHannes Frederic Sowa return 0; 1289b22b941bSHannes Frederic Sowa 1290775f4f05SDan Carpenter memset(&vf_vlan_info, 0, sizeof(vf_vlan_info)); 12919fbf082fSLeon Romanovsky memset(&node_guid, 0, sizeof(node_guid)); 12929fbf082fSLeon Romanovsky memset(&port_guid, 0, sizeof(port_guid)); 1293775f4f05SDan Carpenter 1294b22b941bSHannes Frederic Sowa vf_mac.vf = 1295b22b941bSHannes Frederic Sowa vf_vlan.vf = 129679aab093SMoshe Shemesh vf_vlan_info.vf = 1297b22b941bSHannes Frederic Sowa vf_rate.vf = 1298b22b941bSHannes Frederic Sowa vf_tx_rate.vf = 1299b22b941bSHannes Frederic Sowa vf_spoofchk.vf = 1300b22b941bSHannes Frederic Sowa vf_linkstate.vf = 1301b22b941bSHannes Frederic Sowa vf_rss_query_en.vf = 13029aed6ae0SDanit Goldberg vf_trust.vf = 13039aed6ae0SDanit Goldberg node_guid.vf = 13049aed6ae0SDanit Goldberg port_guid.vf = ivi.vf; 1305b22b941bSHannes Frederic Sowa 1306b22b941bSHannes Frederic Sowa memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac)); 130775345f88SDenis Kirjanov memcpy(vf_broadcast.broadcast, dev->broadcast, dev->addr_len); 1308b22b941bSHannes Frederic Sowa vf_vlan.vlan = ivi.vlan; 1309b22b941bSHannes Frederic Sowa vf_vlan.qos = ivi.qos; 131079aab093SMoshe Shemesh vf_vlan_info.vlan = ivi.vlan; 131179aab093SMoshe Shemesh vf_vlan_info.qos = ivi.qos; 131279aab093SMoshe Shemesh vf_vlan_info.vlan_proto = ivi.vlan_proto; 1313b22b941bSHannes Frederic Sowa vf_tx_rate.rate = ivi.max_tx_rate; 1314b22b941bSHannes Frederic Sowa vf_rate.min_tx_rate = ivi.min_tx_rate; 1315b22b941bSHannes Frederic Sowa vf_rate.max_tx_rate = ivi.max_tx_rate; 1316b22b941bSHannes Frederic Sowa vf_spoofchk.setting = ivi.spoofchk; 1317b22b941bSHannes Frederic Sowa vf_linkstate.link_state = ivi.linkstate; 1318b22b941bSHannes Frederic Sowa vf_rss_query_en.setting = ivi.rss_query_en; 1319b22b941bSHannes Frederic Sowa vf_trust.setting = ivi.trusted; 1320ae0be8deSMichal Kubecek vf = nla_nest_start_noflag(skb, IFLA_VF_INFO); 132179aab093SMoshe Shemesh if (!vf) 132279aab093SMoshe Shemesh goto nla_put_vfinfo_failure; 1323b22b941bSHannes Frederic Sowa if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) || 132475345f88SDenis Kirjanov nla_put(skb, IFLA_VF_BROADCAST, sizeof(vf_broadcast), &vf_broadcast) || 1325b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) || 1326b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate), 1327b22b941bSHannes Frederic Sowa &vf_rate) || 1328b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate), 1329b22b941bSHannes Frederic Sowa &vf_tx_rate) || 1330b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk), 1331b22b941bSHannes Frederic Sowa &vf_spoofchk) || 1332b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate), 1333b22b941bSHannes Frederic Sowa &vf_linkstate) || 1334b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_RSS_QUERY_EN, 1335b22b941bSHannes Frederic Sowa sizeof(vf_rss_query_en), 1336b22b941bSHannes Frederic Sowa &vf_rss_query_en) || 1337b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_TRUST, 1338b22b941bSHannes Frederic Sowa sizeof(vf_trust), &vf_trust)) 133979aab093SMoshe Shemesh goto nla_put_vf_failure; 134030aad417SDanit Goldberg 134130aad417SDanit Goldberg if (dev->netdev_ops->ndo_get_vf_guid && 134230aad417SDanit Goldberg !dev->netdev_ops->ndo_get_vf_guid(dev, vfs_num, &node_guid, 134330aad417SDanit Goldberg &port_guid)) { 134430aad417SDanit Goldberg if (nla_put(skb, IFLA_VF_IB_NODE_GUID, sizeof(node_guid), 134530aad417SDanit Goldberg &node_guid) || 134630aad417SDanit Goldberg nla_put(skb, IFLA_VF_IB_PORT_GUID, sizeof(port_guid), 134730aad417SDanit Goldberg &port_guid)) 134830aad417SDanit Goldberg goto nla_put_vf_failure; 134930aad417SDanit Goldberg } 1350ae0be8deSMichal Kubecek vfvlanlist = nla_nest_start_noflag(skb, IFLA_VF_VLAN_LIST); 135179aab093SMoshe Shemesh if (!vfvlanlist) 135279aab093SMoshe Shemesh goto nla_put_vf_failure; 135379aab093SMoshe Shemesh if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info), 135479aab093SMoshe Shemesh &vf_vlan_info)) { 135579aab093SMoshe Shemesh nla_nest_cancel(skb, vfvlanlist); 135679aab093SMoshe Shemesh goto nla_put_vf_failure; 135779aab093SMoshe Shemesh } 135879aab093SMoshe Shemesh nla_nest_end(skb, vfvlanlist); 1359b22b941bSHannes Frederic Sowa memset(&vf_stats, 0, sizeof(vf_stats)); 1360b22b941bSHannes Frederic Sowa if (dev->netdev_ops->ndo_get_vf_stats) 1361b22b941bSHannes Frederic Sowa dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num, 1362b22b941bSHannes Frederic Sowa &vf_stats); 1363ae0be8deSMichal Kubecek vfstats = nla_nest_start_noflag(skb, IFLA_VF_STATS); 136479aab093SMoshe Shemesh if (!vfstats) 136579aab093SMoshe Shemesh goto nla_put_vf_failure; 1366343a6d8eSNicolas Dichtel if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS, 1367343a6d8eSNicolas Dichtel vf_stats.rx_packets, IFLA_VF_STATS_PAD) || 1368343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS, 1369343a6d8eSNicolas Dichtel vf_stats.tx_packets, IFLA_VF_STATS_PAD) || 1370343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES, 1371343a6d8eSNicolas Dichtel vf_stats.rx_bytes, IFLA_VF_STATS_PAD) || 1372343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES, 1373343a6d8eSNicolas Dichtel vf_stats.tx_bytes, IFLA_VF_STATS_PAD) || 1374343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST, 1375343a6d8eSNicolas Dichtel vf_stats.broadcast, IFLA_VF_STATS_PAD) || 1376343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST, 1377c5a9f6f0SEugenia Emantayev vf_stats.multicast, IFLA_VF_STATS_PAD) || 1378c5a9f6f0SEugenia Emantayev nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED, 1379c5a9f6f0SEugenia Emantayev vf_stats.rx_dropped, IFLA_VF_STATS_PAD) || 1380c5a9f6f0SEugenia Emantayev nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED, 1381c5a9f6f0SEugenia Emantayev vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) { 138279aab093SMoshe Shemesh nla_nest_cancel(skb, vfstats); 138379aab093SMoshe Shemesh goto nla_put_vf_failure; 138479aab093SMoshe Shemesh } 1385b22b941bSHannes Frederic Sowa nla_nest_end(skb, vfstats); 1386b22b941bSHannes Frederic Sowa nla_nest_end(skb, vf); 1387b22b941bSHannes Frederic Sowa return 0; 138879aab093SMoshe Shemesh 138979aab093SMoshe Shemesh nla_put_vf_failure: 139079aab093SMoshe Shemesh nla_nest_cancel(skb, vf); 139179aab093SMoshe Shemesh nla_put_vfinfo_failure: 139279aab093SMoshe Shemesh nla_nest_cancel(skb, vfinfo); 139379aab093SMoshe Shemesh return -EMSGSIZE; 1394b22b941bSHannes Frederic Sowa } 1395b22b941bSHannes Frederic Sowa 1396250fc3dfSFlorian Westphal static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb, 1397250fc3dfSFlorian Westphal struct net_device *dev, 1398250fc3dfSFlorian Westphal u32 ext_filter_mask) 1399250fc3dfSFlorian Westphal { 1400250fc3dfSFlorian Westphal struct nlattr *vfinfo; 1401250fc3dfSFlorian Westphal int i, num_vfs; 1402250fc3dfSFlorian Westphal 1403250fc3dfSFlorian Westphal if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0)) 1404250fc3dfSFlorian Westphal return 0; 1405250fc3dfSFlorian Westphal 1406250fc3dfSFlorian Westphal num_vfs = dev_num_vf(dev->dev.parent); 1407250fc3dfSFlorian Westphal if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs)) 1408250fc3dfSFlorian Westphal return -EMSGSIZE; 1409250fc3dfSFlorian Westphal 1410250fc3dfSFlorian Westphal if (!dev->netdev_ops->ndo_get_vf_config) 1411250fc3dfSFlorian Westphal return 0; 1412250fc3dfSFlorian Westphal 1413ae0be8deSMichal Kubecek vfinfo = nla_nest_start_noflag(skb, IFLA_VFINFO_LIST); 1414250fc3dfSFlorian Westphal if (!vfinfo) 1415250fc3dfSFlorian Westphal return -EMSGSIZE; 1416250fc3dfSFlorian Westphal 1417250fc3dfSFlorian Westphal for (i = 0; i < num_vfs; i++) { 1418250fc3dfSFlorian Westphal if (rtnl_fill_vfinfo(skb, dev, i, vfinfo)) 1419250fc3dfSFlorian Westphal return -EMSGSIZE; 1420250fc3dfSFlorian Westphal } 1421250fc3dfSFlorian Westphal 1422250fc3dfSFlorian Westphal nla_nest_end(skb, vfinfo); 1423250fc3dfSFlorian Westphal return 0; 1424250fc3dfSFlorian Westphal } 1425250fc3dfSFlorian Westphal 1426b22b941bSHannes Frederic Sowa static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev) 1427b22b941bSHannes Frederic Sowa { 14285f8e4474SKangjie Lu struct rtnl_link_ifmap map; 14295f8e4474SKangjie Lu 14305f8e4474SKangjie Lu memset(&map, 0, sizeof(map)); 14315f8e4474SKangjie Lu map.mem_start = dev->mem_start; 14325f8e4474SKangjie Lu map.mem_end = dev->mem_end; 14335f8e4474SKangjie Lu map.base_addr = dev->base_addr; 14345f8e4474SKangjie Lu map.irq = dev->irq; 14355f8e4474SKangjie Lu map.dma = dev->dma; 14365f8e4474SKangjie Lu map.port = dev->if_port; 14375f8e4474SKangjie Lu 1438270cb4d0SNicolas Dichtel if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD)) 1439b22b941bSHannes Frederic Sowa return -EMSGSIZE; 1440b22b941bSHannes Frederic Sowa 1441b22b941bSHannes Frederic Sowa return 0; 1442b22b941bSHannes Frederic Sowa } 1443b22b941bSHannes Frederic Sowa 1444a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_skb(struct net_device *dev) 1445d67b9cd2SDaniel Borkmann { 144658038695SMartin KaFai Lau const struct bpf_prog *generic_xdp_prog; 1447d67b9cd2SDaniel Borkmann 1448d67b9cd2SDaniel Borkmann ASSERT_RTNL(); 1449d67b9cd2SDaniel Borkmann 145058038695SMartin KaFai Lau generic_xdp_prog = rtnl_dereference(dev->xdp_prog); 1451a25717d2SJakub Kicinski if (!generic_xdp_prog) 1452a25717d2SJakub Kicinski return 0; 1453a25717d2SJakub Kicinski return generic_xdp_prog->aux->id; 145458038695SMartin KaFai Lau } 1455ce158e58SJakub Kicinski 1456a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_drv(struct net_device *dev) 1457a25717d2SJakub Kicinski { 14587f0a8382SAndrii Nakryiko return dev_xdp_prog_id(dev, XDP_MODE_DRV); 1459a25717d2SJakub Kicinski } 1460118b4aa2SJakub Kicinski 1461a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_hw(struct net_device *dev) 1462a25717d2SJakub Kicinski { 14637f0a8382SAndrii Nakryiko return dev_xdp_prog_id(dev, XDP_MODE_HW); 1464a25717d2SJakub Kicinski } 1465a25717d2SJakub Kicinski 1466a25717d2SJakub Kicinski static int rtnl_xdp_report_one(struct sk_buff *skb, struct net_device *dev, 1467a25717d2SJakub Kicinski u32 *prog_id, u8 *mode, u8 tgt_mode, u32 attr, 1468a25717d2SJakub Kicinski u32 (*get_prog_id)(struct net_device *dev)) 1469a25717d2SJakub Kicinski { 1470a25717d2SJakub Kicinski u32 curr_id; 1471a25717d2SJakub Kicinski int err; 1472a25717d2SJakub Kicinski 1473a25717d2SJakub Kicinski curr_id = get_prog_id(dev); 1474a25717d2SJakub Kicinski if (!curr_id) 1475a25717d2SJakub Kicinski return 0; 1476a25717d2SJakub Kicinski 1477a25717d2SJakub Kicinski *prog_id = curr_id; 1478a25717d2SJakub Kicinski err = nla_put_u32(skb, attr, curr_id); 1479a25717d2SJakub Kicinski if (err) 1480a25717d2SJakub Kicinski return err; 1481a25717d2SJakub Kicinski 1482a25717d2SJakub Kicinski if (*mode != XDP_ATTACHED_NONE) 1483a25717d2SJakub Kicinski *mode = XDP_ATTACHED_MULTI; 1484a25717d2SJakub Kicinski else 1485a25717d2SJakub Kicinski *mode = tgt_mode; 1486a25717d2SJakub Kicinski 1487a25717d2SJakub Kicinski return 0; 1488d67b9cd2SDaniel Borkmann } 1489d67b9cd2SDaniel Borkmann 1490d1fdd913SBrenden Blanco static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev) 1491d1fdd913SBrenden Blanco { 1492d1fdd913SBrenden Blanco struct nlattr *xdp; 149358038695SMartin KaFai Lau u32 prog_id; 1494d1fdd913SBrenden Blanco int err; 14954f91da26SJakub Kicinski u8 mode; 1496d1fdd913SBrenden Blanco 1497ae0be8deSMichal Kubecek xdp = nla_nest_start_noflag(skb, IFLA_XDP); 1498d1fdd913SBrenden Blanco if (!xdp) 1499d1fdd913SBrenden Blanco return -EMSGSIZE; 1500b5cdae32SDavid S. Miller 1501a25717d2SJakub Kicinski prog_id = 0; 1502a25717d2SJakub Kicinski mode = XDP_ATTACHED_NONE; 1503202aabe8SJakub Kicinski err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_SKB, 1504202aabe8SJakub Kicinski IFLA_XDP_SKB_PROG_ID, rtnl_xdp_prog_skb); 1505202aabe8SJakub Kicinski if (err) 1506a25717d2SJakub Kicinski goto err_cancel; 1507202aabe8SJakub Kicinski err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_DRV, 1508202aabe8SJakub Kicinski IFLA_XDP_DRV_PROG_ID, rtnl_xdp_prog_drv); 1509202aabe8SJakub Kicinski if (err) 1510a25717d2SJakub Kicinski goto err_cancel; 1511202aabe8SJakub Kicinski err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_HW, 1512202aabe8SJakub Kicinski IFLA_XDP_HW_PROG_ID, rtnl_xdp_prog_hw); 1513202aabe8SJakub Kicinski if (err) 1514a25717d2SJakub Kicinski goto err_cancel; 1515a25717d2SJakub Kicinski 15164f91da26SJakub Kicinski err = nla_put_u8(skb, IFLA_XDP_ATTACHED, mode); 1517d1fdd913SBrenden Blanco if (err) 1518d1fdd913SBrenden Blanco goto err_cancel; 1519d1fdd913SBrenden Blanco 1520a25717d2SJakub Kicinski if (prog_id && mode != XDP_ATTACHED_MULTI) { 152158038695SMartin KaFai Lau err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id); 152258038695SMartin KaFai Lau if (err) 152358038695SMartin KaFai Lau goto err_cancel; 152458038695SMartin KaFai Lau } 152558038695SMartin KaFai Lau 1526d1fdd913SBrenden Blanco nla_nest_end(skb, xdp); 1527d1fdd913SBrenden Blanco return 0; 1528d1fdd913SBrenden Blanco 1529d1fdd913SBrenden Blanco err_cancel: 1530d1fdd913SBrenden Blanco nla_nest_cancel(skb, xdp); 1531d1fdd913SBrenden Blanco return err; 1532d1fdd913SBrenden Blanco } 1533d1fdd913SBrenden Blanco 15343d3ea5afSVlad Yasevich static u32 rtnl_get_event(unsigned long event) 15353d3ea5afSVlad Yasevich { 15363d3ea5afSVlad Yasevich u32 rtnl_event_type = IFLA_EVENT_NONE; 15373d3ea5afSVlad Yasevich 15383d3ea5afSVlad Yasevich switch (event) { 15393d3ea5afSVlad Yasevich case NETDEV_REBOOT: 15403d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_REBOOT; 15413d3ea5afSVlad Yasevich break; 15423d3ea5afSVlad Yasevich case NETDEV_FEAT_CHANGE: 15433d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_FEATURES; 15443d3ea5afSVlad Yasevich break; 15453d3ea5afSVlad Yasevich case NETDEV_BONDING_FAILOVER: 15463d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER; 15473d3ea5afSVlad Yasevich break; 15483d3ea5afSVlad Yasevich case NETDEV_NOTIFY_PEERS: 15493d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS; 15503d3ea5afSVlad Yasevich break; 15513d3ea5afSVlad Yasevich case NETDEV_RESEND_IGMP: 15523d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_IGMP_RESEND; 15533d3ea5afSVlad Yasevich break; 15543d3ea5afSVlad Yasevich case NETDEV_CHANGEINFODATA: 15553d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS; 15563d3ea5afSVlad Yasevich break; 15573d3ea5afSVlad Yasevich default: 15583d3ea5afSVlad Yasevich break; 15593d3ea5afSVlad Yasevich } 15603d3ea5afSVlad Yasevich 15613d3ea5afSVlad Yasevich return rtnl_event_type; 15623d3ea5afSVlad Yasevich } 15633d3ea5afSVlad Yasevich 156479110a04SFlorian Westphal static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev) 156579110a04SFlorian Westphal { 156679110a04SFlorian Westphal const struct net_device *upper_dev; 156779110a04SFlorian Westphal int ret = 0; 156879110a04SFlorian Westphal 156979110a04SFlorian Westphal rcu_read_lock(); 157079110a04SFlorian Westphal 157179110a04SFlorian Westphal upper_dev = netdev_master_upper_dev_get_rcu(dev); 157279110a04SFlorian Westphal if (upper_dev) 157379110a04SFlorian Westphal ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex); 157479110a04SFlorian Westphal 157579110a04SFlorian Westphal rcu_read_unlock(); 157679110a04SFlorian Westphal return ret; 157779110a04SFlorian Westphal } 157879110a04SFlorian Westphal 1579feadc4b6SSabrina Dubroca static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev, 1580feadc4b6SSabrina Dubroca bool force) 158179110a04SFlorian Westphal { 158279110a04SFlorian Westphal int ifindex = dev_get_iflink(dev); 158379110a04SFlorian Westphal 1584feadc4b6SSabrina Dubroca if (force || dev->ifindex != ifindex) 158579110a04SFlorian Westphal return nla_put_u32(skb, IFLA_LINK, ifindex); 1586feadc4b6SSabrina Dubroca 1587feadc4b6SSabrina Dubroca return 0; 158879110a04SFlorian Westphal } 158979110a04SFlorian Westphal 15906c557001SFlorian Westphal static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb, 15916c557001SFlorian Westphal struct net_device *dev) 15926c557001SFlorian Westphal { 15936c557001SFlorian Westphal char buf[IFALIASZ]; 15946c557001SFlorian Westphal int ret; 15956c557001SFlorian Westphal 15966c557001SFlorian Westphal ret = dev_get_alias(dev, buf, sizeof(buf)); 15976c557001SFlorian Westphal return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0; 15986c557001SFlorian Westphal } 15996c557001SFlorian Westphal 1600b1e66b9aSFlorian Westphal static int rtnl_fill_link_netnsid(struct sk_buff *skb, 160179e1ad14SJiri Benc const struct net_device *dev, 1602d4e4fdf9SGuillaume Nault struct net *src_net, gfp_t gfp) 1603b1e66b9aSFlorian Westphal { 1604feadc4b6SSabrina Dubroca bool put_iflink = false; 1605feadc4b6SSabrina Dubroca 1606b1e66b9aSFlorian Westphal if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) { 1607b1e66b9aSFlorian Westphal struct net *link_net = dev->rtnl_link_ops->get_link_net(dev); 1608b1e66b9aSFlorian Westphal 1609b1e66b9aSFlorian Westphal if (!net_eq(dev_net(dev), link_net)) { 1610d4e4fdf9SGuillaume Nault int id = peernet2id_alloc(src_net, link_net, gfp); 1611b1e66b9aSFlorian Westphal 1612b1e66b9aSFlorian Westphal if (nla_put_s32(skb, IFLA_LINK_NETNSID, id)) 1613b1e66b9aSFlorian Westphal return -EMSGSIZE; 1614feadc4b6SSabrina Dubroca 1615feadc4b6SSabrina Dubroca put_iflink = true; 1616b1e66b9aSFlorian Westphal } 1617b1e66b9aSFlorian Westphal } 1618b1e66b9aSFlorian Westphal 1619feadc4b6SSabrina Dubroca return nla_put_iflink(skb, dev, put_iflink); 1620b1e66b9aSFlorian Westphal } 1621b1e66b9aSFlorian Westphal 1622070cbf5bSFlorian Westphal static int rtnl_fill_link_af(struct sk_buff *skb, 1623070cbf5bSFlorian Westphal const struct net_device *dev, 1624070cbf5bSFlorian Westphal u32 ext_filter_mask) 1625070cbf5bSFlorian Westphal { 1626070cbf5bSFlorian Westphal const struct rtnl_af_ops *af_ops; 1627070cbf5bSFlorian Westphal struct nlattr *af_spec; 1628070cbf5bSFlorian Westphal 1629ae0be8deSMichal Kubecek af_spec = nla_nest_start_noflag(skb, IFLA_AF_SPEC); 1630070cbf5bSFlorian Westphal if (!af_spec) 1631070cbf5bSFlorian Westphal return -EMSGSIZE; 1632070cbf5bSFlorian Westphal 16335fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 1634070cbf5bSFlorian Westphal struct nlattr *af; 1635070cbf5bSFlorian Westphal int err; 1636070cbf5bSFlorian Westphal 1637070cbf5bSFlorian Westphal if (!af_ops->fill_link_af) 1638070cbf5bSFlorian Westphal continue; 1639070cbf5bSFlorian Westphal 1640ae0be8deSMichal Kubecek af = nla_nest_start_noflag(skb, af_ops->family); 1641070cbf5bSFlorian Westphal if (!af) 1642070cbf5bSFlorian Westphal return -EMSGSIZE; 1643070cbf5bSFlorian Westphal 1644070cbf5bSFlorian Westphal err = af_ops->fill_link_af(skb, dev, ext_filter_mask); 1645070cbf5bSFlorian Westphal /* 1646070cbf5bSFlorian Westphal * Caller may return ENODATA to indicate that there 1647070cbf5bSFlorian Westphal * was no data to be dumped. This is not an error, it 1648070cbf5bSFlorian Westphal * means we should trim the attribute header and 1649070cbf5bSFlorian Westphal * continue. 1650070cbf5bSFlorian Westphal */ 1651070cbf5bSFlorian Westphal if (err == -ENODATA) 1652070cbf5bSFlorian Westphal nla_nest_cancel(skb, af); 1653070cbf5bSFlorian Westphal else if (err < 0) 1654070cbf5bSFlorian Westphal return -EMSGSIZE; 1655070cbf5bSFlorian Westphal 1656070cbf5bSFlorian Westphal nla_nest_end(skb, af); 1657070cbf5bSFlorian Westphal } 1658070cbf5bSFlorian Westphal 1659070cbf5bSFlorian Westphal nla_nest_end(skb, af_spec); 1660070cbf5bSFlorian Westphal return 0; 1661070cbf5bSFlorian Westphal } 1662070cbf5bSFlorian Westphal 166388f4fb0cSJiri Pirko static int rtnl_fill_alt_ifnames(struct sk_buff *skb, 166488f4fb0cSJiri Pirko const struct net_device *dev) 166588f4fb0cSJiri Pirko { 166688f4fb0cSJiri Pirko struct netdev_name_node *name_node; 166788f4fb0cSJiri Pirko int count = 0; 166888f4fb0cSJiri Pirko 166988f4fb0cSJiri Pirko list_for_each_entry(name_node, &dev->name_node->list, list) { 167088f4fb0cSJiri Pirko if (nla_put_string(skb, IFLA_ALT_IFNAME, name_node->name)) 167188f4fb0cSJiri Pirko return -EMSGSIZE; 167288f4fb0cSJiri Pirko count++; 167388f4fb0cSJiri Pirko } 167488f4fb0cSJiri Pirko return count; 167588f4fb0cSJiri Pirko } 167688f4fb0cSJiri Pirko 167788f4fb0cSJiri Pirko static int rtnl_fill_prop_list(struct sk_buff *skb, 167888f4fb0cSJiri Pirko const struct net_device *dev) 167988f4fb0cSJiri Pirko { 168088f4fb0cSJiri Pirko struct nlattr *prop_list; 168188f4fb0cSJiri Pirko int ret; 168288f4fb0cSJiri Pirko 168388f4fb0cSJiri Pirko prop_list = nla_nest_start(skb, IFLA_PROP_LIST); 168488f4fb0cSJiri Pirko if (!prop_list) 168588f4fb0cSJiri Pirko return -EMSGSIZE; 168688f4fb0cSJiri Pirko 168788f4fb0cSJiri Pirko ret = rtnl_fill_alt_ifnames(skb, dev); 168888f4fb0cSJiri Pirko if (ret <= 0) 168988f4fb0cSJiri Pirko goto nest_cancel; 169088f4fb0cSJiri Pirko 169188f4fb0cSJiri Pirko nla_nest_end(skb, prop_list); 169288f4fb0cSJiri Pirko return 0; 169388f4fb0cSJiri Pirko 169488f4fb0cSJiri Pirko nest_cancel: 169588f4fb0cSJiri Pirko nla_nest_cancel(skb, prop_list); 169688f4fb0cSJiri Pirko return ret; 169788f4fb0cSJiri Pirko } 169888f4fb0cSJiri Pirko 1699829eb208SRoopa Prabhu static int rtnl_fill_proto_down(struct sk_buff *skb, 1700829eb208SRoopa Prabhu const struct net_device *dev) 1701829eb208SRoopa Prabhu { 1702829eb208SRoopa Prabhu struct nlattr *pr; 1703829eb208SRoopa Prabhu u32 preason; 1704829eb208SRoopa Prabhu 1705829eb208SRoopa Prabhu if (nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down)) 1706829eb208SRoopa Prabhu goto nla_put_failure; 1707829eb208SRoopa Prabhu 1708829eb208SRoopa Prabhu preason = dev->proto_down_reason; 1709829eb208SRoopa Prabhu if (!preason) 1710829eb208SRoopa Prabhu return 0; 1711829eb208SRoopa Prabhu 1712829eb208SRoopa Prabhu pr = nla_nest_start(skb, IFLA_PROTO_DOWN_REASON); 1713829eb208SRoopa Prabhu if (!pr) 1714829eb208SRoopa Prabhu return -EMSGSIZE; 1715829eb208SRoopa Prabhu 1716829eb208SRoopa Prabhu if (nla_put_u32(skb, IFLA_PROTO_DOWN_REASON_VALUE, preason)) { 1717829eb208SRoopa Prabhu nla_nest_cancel(skb, pr); 1718829eb208SRoopa Prabhu goto nla_put_failure; 1719829eb208SRoopa Prabhu } 1720829eb208SRoopa Prabhu 1721829eb208SRoopa Prabhu nla_nest_end(skb, pr); 1722829eb208SRoopa Prabhu return 0; 1723829eb208SRoopa Prabhu 1724829eb208SRoopa Prabhu nla_put_failure: 1725829eb208SRoopa Prabhu return -EMSGSIZE; 1726829eb208SRoopa Prabhu } 1727829eb208SRoopa Prabhu 172879e1ad14SJiri Benc static int rtnl_fill_ifinfo(struct sk_buff *skb, 172979e1ad14SJiri Benc struct net_device *dev, struct net *src_net, 1730575c3e2aSPatrick McHardy int type, u32 pid, u32 seq, u32 change, 17313d3ea5afSVlad Yasevich unsigned int flags, u32 ext_filter_mask, 173238e01b30SNicolas Dichtel u32 event, int *new_nsid, int new_ifindex, 1733d4e4fdf9SGuillaume Nault int tgt_netnsid, gfp_t gfp) 1734b60c5115SThomas Graf { 1735b60c5115SThomas Graf struct ifinfomsg *ifm; 17361da177e4SLinus Torvalds struct nlmsghdr *nlh; 17375891cd5eSEric Dumazet struct Qdisc *qdisc; 17381da177e4SLinus Torvalds 17392907c35fSEric Dumazet ASSERT_RTNL(); 1740b60c5115SThomas Graf nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags); 1741b60c5115SThomas Graf if (nlh == NULL) 174226932566SPatrick McHardy return -EMSGSIZE; 17431da177e4SLinus Torvalds 1744b60c5115SThomas Graf ifm = nlmsg_data(nlh); 1745b60c5115SThomas Graf ifm->ifi_family = AF_UNSPEC; 1746b60c5115SThomas Graf ifm->__ifi_pad = 0; 1747b60c5115SThomas Graf ifm->ifi_type = dev->type; 1748b60c5115SThomas Graf ifm->ifi_index = dev->ifindex; 1749b60c5115SThomas Graf ifm->ifi_flags = dev_get_flags(dev); 1750b60c5115SThomas Graf ifm->ifi_change = change; 17511da177e4SLinus Torvalds 17527e4a8d5aSChristian Brauner if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_TARGET_NETNSID, tgt_netnsid)) 175379e1ad14SJiri Benc goto nla_put_failure; 175479e1ad14SJiri Benc 17555891cd5eSEric Dumazet qdisc = rtnl_dereference(dev->qdisc); 1756a6574349SDavid S. Miller if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 1757a6574349SDavid S. Miller nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) || 1758a6574349SDavid S. Miller nla_put_u8(skb, IFLA_OPERSTATE, 1759a6574349SDavid S. Miller netif_running(dev) ? dev->operstate : IF_OPER_DOWN) || 1760a6574349SDavid S. Miller nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) || 1761a6574349SDavid S. Miller nla_put_u32(skb, IFLA_MTU, dev->mtu) || 17623e7a50ceSStephen Hemminger nla_put_u32(skb, IFLA_MIN_MTU, dev->min_mtu) || 17633e7a50ceSStephen Hemminger nla_put_u32(skb, IFLA_MAX_MTU, dev->max_mtu) || 1764a6574349SDavid S. Miller nla_put_u32(skb, IFLA_GROUP, dev->group) || 1765edbc0bb3SBen Greear nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) || 176676ff5cc9SJiri Pirko nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) || 1767c70ce028SEric Dumazet nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) || 1768c70ce028SEric Dumazet nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) || 1769eac1b93cSCoco Li nla_put_u32(skb, IFLA_GRO_MAX_SIZE, dev->gro_max_size) || 17701d69c2b3SMark A. Greer #ifdef CONFIG_RPS 177176ff5cc9SJiri Pirko nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) || 17721d69c2b3SMark A. Greer #endif 177379110a04SFlorian Westphal put_master_ifindex(skb, dev) || 17749a57247fSJiri Pirko nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) || 17755891cd5eSEric Dumazet (qdisc && 17765891cd5eSEric Dumazet nla_put_string(skb, IFLA_QDISC, qdisc->ops->id)) || 17776c557001SFlorian Westphal nla_put_ifalias(skb, dev) || 17782d3b479dSdavid decotigny nla_put_u32(skb, IFLA_CARRIER_CHANGES, 1779b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_up_count) + 1780b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_down_count)) || 1781b2d3bcfaSDavid Decotigny nla_put_u32(skb, IFLA_CARRIER_UP_COUNT, 1782b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_up_count)) || 1783b2d3bcfaSDavid Decotigny nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT, 1784b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_down_count))) 1785a6574349SDavid S. Miller goto nla_put_failure; 17860b815a1aSStephen Hemminger 1787829eb208SRoopa Prabhu if (rtnl_fill_proto_down(skb, dev)) 1788829eb208SRoopa Prabhu goto nla_put_failure; 1789829eb208SRoopa Prabhu 17903d3ea5afSVlad Yasevich if (event != IFLA_EVENT_NONE) { 17913d3ea5afSVlad Yasevich if (nla_put_u32(skb, IFLA_EVENT, event)) 17923d3ea5afSVlad Yasevich goto nla_put_failure; 17933d3ea5afSVlad Yasevich } 17943d3ea5afSVlad Yasevich 1795b22b941bSHannes Frederic Sowa if (rtnl_fill_link_ifmap(skb, dev)) 1796a6574349SDavid S. Miller goto nla_put_failure; 17971da177e4SLinus Torvalds 17981da177e4SLinus Torvalds if (dev->addr_len) { 1799a6574349SDavid S. Miller if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) || 1800a6574349SDavid S. Miller nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast)) 1801a6574349SDavid S. Miller goto nla_put_failure; 18021da177e4SLinus Torvalds } 18031da177e4SLinus Torvalds 180466cae9edSJiri Pirko if (rtnl_phys_port_id_fill(skb, dev)) 180566cae9edSJiri Pirko goto nla_put_failure; 180666cae9edSJiri Pirko 1807db24a904SDavid Ahern if (rtnl_phys_port_name_fill(skb, dev)) 1808db24a904SDavid Ahern goto nla_put_failure; 1809db24a904SDavid Ahern 181082f28412SJiri Pirko if (rtnl_phys_switch_id_fill(skb, dev)) 181182f28412SJiri Pirko goto nla_put_failure; 181282f28412SJiri Pirko 1813b22b941bSHannes Frederic Sowa if (rtnl_fill_stats(skb, dev)) 1814b60c5115SThomas Graf goto nla_put_failure; 1815b60c5115SThomas Graf 1816250fc3dfSFlorian Westphal if (rtnl_fill_vf(skb, dev, ext_filter_mask)) 1817a6574349SDavid S. Miller goto nla_put_failure; 181857b61080SScott Feldman 1819c53864fdSDavid Gibson if (rtnl_port_fill(skb, dev, ext_filter_mask)) 182057b61080SScott Feldman goto nla_put_failure; 182157b61080SScott Feldman 1822d1fdd913SBrenden Blanco if (rtnl_xdp_fill(skb, dev)) 1823d1fdd913SBrenden Blanco goto nla_put_failure; 1824d1fdd913SBrenden Blanco 1825ba7d49b1SJiri Pirko if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) { 182638f7b870SPatrick McHardy if (rtnl_link_fill(skb, dev) < 0) 182738f7b870SPatrick McHardy goto nla_put_failure; 182838f7b870SPatrick McHardy } 182938f7b870SPatrick McHardy 1830d4e4fdf9SGuillaume Nault if (rtnl_fill_link_netnsid(skb, dev, src_net, gfp)) 1831d37512a2SNicolas Dichtel goto nla_put_failure; 1832d37512a2SNicolas Dichtel 18336621dd29SNicolas Dichtel if (new_nsid && 18346621dd29SNicolas Dichtel nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0) 18356621dd29SNicolas Dichtel goto nla_put_failure; 183638e01b30SNicolas Dichtel if (new_ifindex && 183738e01b30SNicolas Dichtel nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0) 183838e01b30SNicolas Dichtel goto nla_put_failure; 183938e01b30SNicolas Dichtel 1840f74877a5SMichal Kubecek if (memchr_inv(dev->perm_addr, '\0', dev->addr_len) && 1841f74877a5SMichal Kubecek nla_put(skb, IFLA_PERM_ADDRESS, dev->addr_len, dev->perm_addr)) 1842f74877a5SMichal Kubecek goto nla_put_failure; 18436621dd29SNicolas Dichtel 18445fa85a09SFlorian Westphal rcu_read_lock(); 1845070cbf5bSFlorian Westphal if (rtnl_fill_link_af(skb, dev, ext_filter_mask)) 18465fa85a09SFlorian Westphal goto nla_put_failure_rcu; 18475fa85a09SFlorian Westphal rcu_read_unlock(); 1848f8ff182cSThomas Graf 184988f4fb0cSJiri Pirko if (rtnl_fill_prop_list(skb, dev)) 185088f4fb0cSJiri Pirko goto nla_put_failure; 185188f4fb0cSJiri Pirko 185200e77ed8SJohannes Berg if (dev->dev.parent && 185300e77ed8SJohannes Berg nla_put_string(skb, IFLA_PARENT_DEV_NAME, 185400e77ed8SJohannes Berg dev_name(dev->dev.parent))) 185500e77ed8SJohannes Berg goto nla_put_failure; 185600e77ed8SJohannes Berg 185700e77ed8SJohannes Berg if (dev->dev.parent && dev->dev.parent->bus && 185800e77ed8SJohannes Berg nla_put_string(skb, IFLA_PARENT_DEV_BUS_NAME, 185900e77ed8SJohannes Berg dev->dev.parent->bus->name)) 186000e77ed8SJohannes Berg goto nla_put_failure; 186100e77ed8SJohannes Berg 1862053c095aSJohannes Berg nlmsg_end(skb, nlh); 1863053c095aSJohannes Berg return 0; 1864b60c5115SThomas Graf 18655fa85a09SFlorian Westphal nla_put_failure_rcu: 18665fa85a09SFlorian Westphal rcu_read_unlock(); 1867b60c5115SThomas Graf nla_put_failure: 186826932566SPatrick McHardy nlmsg_cancel(skb, nlh); 186926932566SPatrick McHardy return -EMSGSIZE; 18701da177e4SLinus Torvalds } 18711da177e4SLinus Torvalds 1872f7b12606SJiri Pirko static const struct nla_policy ifla_policy[IFLA_MAX+1] = { 1873f7b12606SJiri Pirko [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 }, 1874f7b12606SJiri Pirko [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 1875f7b12606SJiri Pirko [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 1876f7b12606SJiri Pirko [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) }, 1877f7b12606SJiri Pirko [IFLA_MTU] = { .type = NLA_U32 }, 1878f7b12606SJiri Pirko [IFLA_LINK] = { .type = NLA_U32 }, 1879f7b12606SJiri Pirko [IFLA_MASTER] = { .type = NLA_U32 }, 1880f7b12606SJiri Pirko [IFLA_CARRIER] = { .type = NLA_U8 }, 1881f7b12606SJiri Pirko [IFLA_TXQLEN] = { .type = NLA_U32 }, 1882f7b12606SJiri Pirko [IFLA_WEIGHT] = { .type = NLA_U32 }, 1883f7b12606SJiri Pirko [IFLA_OPERSTATE] = { .type = NLA_U8 }, 1884f7b12606SJiri Pirko [IFLA_LINKMODE] = { .type = NLA_U8 }, 1885f7b12606SJiri Pirko [IFLA_LINKINFO] = { .type = NLA_NESTED }, 1886f7b12606SJiri Pirko [IFLA_NET_NS_PID] = { .type = NLA_U32 }, 1887f7b12606SJiri Pirko [IFLA_NET_NS_FD] = { .type = NLA_U32 }, 18882459b4c6SNicolas Dichtel /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to 18892459b4c6SNicolas Dichtel * allow 0-length string (needed to remove an alias). 18902459b4c6SNicolas Dichtel */ 18912459b4c6SNicolas Dichtel [IFLA_IFALIAS] = { .type = NLA_BINARY, .len = IFALIASZ - 1 }, 1892f7b12606SJiri Pirko [IFLA_VFINFO_LIST] = {. type = NLA_NESTED }, 1893f7b12606SJiri Pirko [IFLA_VF_PORTS] = { .type = NLA_NESTED }, 1894f7b12606SJiri Pirko [IFLA_PORT_SELF] = { .type = NLA_NESTED }, 1895f7b12606SJiri Pirko [IFLA_AF_SPEC] = { .type = NLA_NESTED }, 1896f7b12606SJiri Pirko [IFLA_EXT_MASK] = { .type = NLA_U32 }, 1897f7b12606SJiri Pirko [IFLA_PROMISCUITY] = { .type = NLA_U32 }, 1898f7b12606SJiri Pirko [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 }, 1899f7b12606SJiri Pirko [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 }, 190046e6b992SStephen Hemminger [IFLA_GSO_MAX_SEGS] = { .type = NLA_U32 }, 190146e6b992SStephen Hemminger [IFLA_GSO_MAX_SIZE] = { .type = NLA_U32 }, 190202637fceSJiri Pirko [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN }, 19032d3b479dSdavid decotigny [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */ 190482f28412SJiri Pirko [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN }, 1905317f4810SNicolas Dichtel [IFLA_LINK_NETNSID] = { .type = NLA_S32 }, 190688d6378bSAnuradha Karuppiah [IFLA_PROTO_DOWN] = { .type = NLA_U8 }, 1907d1fdd913SBrenden Blanco [IFLA_XDP] = { .type = NLA_NESTED }, 19083d3ea5afSVlad Yasevich [IFLA_EVENT] = { .type = NLA_U32 }, 1909db833d40SSerhey Popovych [IFLA_GROUP] = { .type = NLA_U32 }, 19107e4a8d5aSChristian Brauner [IFLA_TARGET_NETNSID] = { .type = NLA_S32 }, 1911b2d3bcfaSDavid Decotigny [IFLA_CARRIER_UP_COUNT] = { .type = NLA_U32 }, 1912b2d3bcfaSDavid Decotigny [IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 }, 19133e7a50ceSStephen Hemminger [IFLA_MIN_MTU] = { .type = NLA_U32 }, 19143e7a50ceSStephen Hemminger [IFLA_MAX_MTU] = { .type = NLA_U32 }, 191536fbf1e5SJiri Pirko [IFLA_PROP_LIST] = { .type = NLA_NESTED }, 191636fbf1e5SJiri Pirko [IFLA_ALT_IFNAME] = { .type = NLA_STRING, 191736fbf1e5SJiri Pirko .len = ALTIFNAMSIZ - 1 }, 1918f74877a5SMichal Kubecek [IFLA_PERM_ADDRESS] = { .type = NLA_REJECT }, 1919829eb208SRoopa Prabhu [IFLA_PROTO_DOWN_REASON] = { .type = NLA_NESTED }, 19207e4a5131SAndrei Vagin [IFLA_NEW_IFINDEX] = NLA_POLICY_MIN(NLA_S32, 1), 192188b71053SJohannes Berg [IFLA_PARENT_DEV_NAME] = { .type = NLA_NUL_STRING }, 1922eac1b93cSCoco Li [IFLA_GRO_MAX_SIZE] = { .type = NLA_U32 }, 1923f7b12606SJiri Pirko }; 1924f7b12606SJiri Pirko 1925f7b12606SJiri Pirko static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = { 1926f7b12606SJiri Pirko [IFLA_INFO_KIND] = { .type = NLA_STRING }, 1927f7b12606SJiri Pirko [IFLA_INFO_DATA] = { .type = NLA_NESTED }, 1928f7b12606SJiri Pirko [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING }, 1929f7b12606SJiri Pirko [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED }, 1930f7b12606SJiri Pirko }; 1931f7b12606SJiri Pirko 1932f7b12606SJiri Pirko static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = { 1933364d5716SDaniel Borkmann [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) }, 193475345f88SDenis Kirjanov [IFLA_VF_BROADCAST] = { .type = NLA_REJECT }, 1935364d5716SDaniel Borkmann [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) }, 193679aab093SMoshe Shemesh [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED }, 1937364d5716SDaniel Borkmann [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) }, 1938364d5716SDaniel Borkmann [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) }, 1939364d5716SDaniel Borkmann [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) }, 1940364d5716SDaniel Borkmann [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) }, 194101a3d796SVlad Zolotarov [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) }, 19423b766cd8SEran Ben Elisha [IFLA_VF_STATS] = { .type = NLA_NESTED }, 1943dd461d6aSHiroshi Shimamoto [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) }, 1944cc8e27ccSEli Cohen [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) }, 1945cc8e27ccSEli Cohen [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) }, 19463b766cd8SEran Ben Elisha }; 19473b766cd8SEran Ben Elisha 1948f7b12606SJiri Pirko static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = { 1949f7b12606SJiri Pirko [IFLA_PORT_VF] = { .type = NLA_U32 }, 1950f7b12606SJiri Pirko [IFLA_PORT_PROFILE] = { .type = NLA_STRING, 1951f7b12606SJiri Pirko .len = PORT_PROFILE_MAX }, 1952f7b12606SJiri Pirko [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY, 1953f7b12606SJiri Pirko .len = PORT_UUID_MAX }, 1954f7b12606SJiri Pirko [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING, 1955f7b12606SJiri Pirko .len = PORT_UUID_MAX }, 1956f7b12606SJiri Pirko [IFLA_PORT_REQUEST] = { .type = NLA_U8, }, 1957f7b12606SJiri Pirko [IFLA_PORT_RESPONSE] = { .type = NLA_U16, }, 1958025331dfSDaniel Borkmann 1959025331dfSDaniel Borkmann /* Unused, but we need to keep it here since user space could 1960025331dfSDaniel Borkmann * fill it. It's also broken with regard to NLA_BINARY use in 1961025331dfSDaniel Borkmann * combination with structs. 1962025331dfSDaniel Borkmann */ 1963025331dfSDaniel Borkmann [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY, 1964025331dfSDaniel Borkmann .len = sizeof(struct ifla_port_vsi) }, 1965f7b12606SJiri Pirko }; 1966f7b12606SJiri Pirko 1967d1fdd913SBrenden Blanco static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = { 196892234c8fSToke Høiland-Jørgensen [IFLA_XDP_UNSPEC] = { .strict_start_type = IFLA_XDP_EXPECTED_FD }, 1969d1fdd913SBrenden Blanco [IFLA_XDP_FD] = { .type = NLA_S32 }, 197092234c8fSToke Høiland-Jørgensen [IFLA_XDP_EXPECTED_FD] = { .type = NLA_S32 }, 1971d1fdd913SBrenden Blanco [IFLA_XDP_ATTACHED] = { .type = NLA_U8 }, 197285de8576SDaniel Borkmann [IFLA_XDP_FLAGS] = { .type = NLA_U32 }, 197358038695SMartin KaFai Lau [IFLA_XDP_PROG_ID] = { .type = NLA_U32 }, 1974d1fdd913SBrenden Blanco }; 1975d1fdd913SBrenden Blanco 1976dc599f76SDavid Ahern static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla) 1977dc599f76SDavid Ahern { 1978dc599f76SDavid Ahern const struct rtnl_link_ops *ops = NULL; 1979dc599f76SDavid Ahern struct nlattr *linfo[IFLA_INFO_MAX + 1]; 1980dc599f76SDavid Ahern 19818cb08174SJohannes Berg if (nla_parse_nested_deprecated(linfo, IFLA_INFO_MAX, nla, ifla_info_policy, NULL) < 0) 1982dc599f76SDavid Ahern return NULL; 1983dc599f76SDavid Ahern 1984dc599f76SDavid Ahern if (linfo[IFLA_INFO_KIND]) { 1985dc599f76SDavid Ahern char kind[MODULE_NAME_LEN]; 1986dc599f76SDavid Ahern 1987872f6903SFrancis Laniel nla_strscpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind)); 1988dc599f76SDavid Ahern ops = rtnl_link_ops_get(kind); 1989dc599f76SDavid Ahern } 1990dc599f76SDavid Ahern 1991dc599f76SDavid Ahern return ops; 1992dc599f76SDavid Ahern } 1993dc599f76SDavid Ahern 1994dc599f76SDavid Ahern static bool link_master_filtered(struct net_device *dev, int master_idx) 1995dc599f76SDavid Ahern { 1996dc599f76SDavid Ahern struct net_device *master; 1997dc599f76SDavid Ahern 1998dc599f76SDavid Ahern if (!master_idx) 1999dc599f76SDavid Ahern return false; 2000dc599f76SDavid Ahern 2001dc599f76SDavid Ahern master = netdev_master_upper_dev_get(dev); 2002d3432bf1SLahav Schlesinger 2003d3432bf1SLahav Schlesinger /* 0 is already used to denote IFLA_MASTER wasn't passed, therefore need 2004d3432bf1SLahav Schlesinger * another invalid value for ifindex to denote "no master". 2005d3432bf1SLahav Schlesinger */ 2006d3432bf1SLahav Schlesinger if (master_idx == -1) 2007d3432bf1SLahav Schlesinger return !!master; 2008d3432bf1SLahav Schlesinger 2009dc599f76SDavid Ahern if (!master || master->ifindex != master_idx) 2010dc599f76SDavid Ahern return true; 2011dc599f76SDavid Ahern 2012dc599f76SDavid Ahern return false; 2013dc599f76SDavid Ahern } 2014dc599f76SDavid Ahern 2015dc599f76SDavid Ahern static bool link_kind_filtered(const struct net_device *dev, 2016dc599f76SDavid Ahern const struct rtnl_link_ops *kind_ops) 2017dc599f76SDavid Ahern { 2018dc599f76SDavid Ahern if (kind_ops && dev->rtnl_link_ops != kind_ops) 2019dc599f76SDavid Ahern return true; 2020dc599f76SDavid Ahern 2021dc599f76SDavid Ahern return false; 2022dc599f76SDavid Ahern } 2023dc599f76SDavid Ahern 2024dc599f76SDavid Ahern static bool link_dump_filtered(struct net_device *dev, 2025dc599f76SDavid Ahern int master_idx, 2026dc599f76SDavid Ahern const struct rtnl_link_ops *kind_ops) 2027dc599f76SDavid Ahern { 2028dc599f76SDavid Ahern if (link_master_filtered(dev, master_idx) || 2029dc599f76SDavid Ahern link_kind_filtered(dev, kind_ops)) 2030dc599f76SDavid Ahern return true; 2031dc599f76SDavid Ahern 2032dc599f76SDavid Ahern return false; 2033dc599f76SDavid Ahern } 2034dc599f76SDavid Ahern 2035c383edc4SChristian Brauner /** 2036c383edc4SChristian Brauner * rtnl_get_net_ns_capable - Get netns if sufficiently privileged. 2037c383edc4SChristian Brauner * @sk: netlink socket 2038c383edc4SChristian Brauner * @netnsid: network namespace identifier 2039c383edc4SChristian Brauner * 2040c383edc4SChristian Brauner * Returns the network namespace identified by netnsid on success or an error 2041c383edc4SChristian Brauner * pointer on failure. 2042c383edc4SChristian Brauner */ 2043c383edc4SChristian Brauner struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid) 204479e1ad14SJiri Benc { 204579e1ad14SJiri Benc struct net *net; 204679e1ad14SJiri Benc 2047f428fe4aSAndrei Vagin net = get_net_ns_by_id(sock_net(sk), netnsid); 204879e1ad14SJiri Benc if (!net) 204979e1ad14SJiri Benc return ERR_PTR(-EINVAL); 205079e1ad14SJiri Benc 205179e1ad14SJiri Benc /* For now, the caller is required to have CAP_NET_ADMIN in 205279e1ad14SJiri Benc * the user namespace owning the target net ns. 205379e1ad14SJiri Benc */ 2054f428fe4aSAndrei Vagin if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) { 205579e1ad14SJiri Benc put_net(net); 205679e1ad14SJiri Benc return ERR_PTR(-EACCES); 205779e1ad14SJiri Benc } 205879e1ad14SJiri Benc return net; 205979e1ad14SJiri Benc } 2060c383edc4SChristian Brauner EXPORT_SYMBOL_GPL(rtnl_get_net_ns_capable); 206179e1ad14SJiri Benc 2062905cf0abSDavid Ahern static int rtnl_valid_dump_ifinfo_req(const struct nlmsghdr *nlh, 2063905cf0abSDavid Ahern bool strict_check, struct nlattr **tb, 2064905cf0abSDavid Ahern struct netlink_ext_ack *extack) 2065905cf0abSDavid Ahern { 2066905cf0abSDavid Ahern int hdrlen; 2067905cf0abSDavid Ahern 2068905cf0abSDavid Ahern if (strict_check) { 2069905cf0abSDavid Ahern struct ifinfomsg *ifm; 2070905cf0abSDavid Ahern 2071905cf0abSDavid Ahern if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { 2072905cf0abSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid header for link dump"); 2073905cf0abSDavid Ahern return -EINVAL; 2074905cf0abSDavid Ahern } 2075905cf0abSDavid Ahern 2076905cf0abSDavid Ahern ifm = nlmsg_data(nlh); 2077905cf0abSDavid Ahern if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags || 2078905cf0abSDavid Ahern ifm->ifi_change) { 2079905cf0abSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid values in header for link dump request"); 2080905cf0abSDavid Ahern return -EINVAL; 2081905cf0abSDavid Ahern } 2082905cf0abSDavid Ahern if (ifm->ifi_index) { 2083905cf0abSDavid Ahern NL_SET_ERR_MSG(extack, "Filter by device index not supported for link dumps"); 2084905cf0abSDavid Ahern return -EINVAL; 2085905cf0abSDavid Ahern } 2086905cf0abSDavid Ahern 20878cb08174SJohannes Berg return nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, 20888cb08174SJohannes Berg IFLA_MAX, ifla_policy, 20898cb08174SJohannes Berg extack); 2090905cf0abSDavid Ahern } 2091905cf0abSDavid Ahern 2092905cf0abSDavid Ahern /* A hack to preserve kernel<->userspace interface. 2093905cf0abSDavid Ahern * The correct header is ifinfomsg. It is consistent with rtnl_getlink. 2094905cf0abSDavid Ahern * However, before Linux v3.9 the code here assumed rtgenmsg and that's 2095905cf0abSDavid Ahern * what iproute2 < v3.9.0 used. 2096905cf0abSDavid Ahern * We can detect the old iproute2. Even including the IFLA_EXT_MASK 2097905cf0abSDavid Ahern * attribute, its netlink message is shorter than struct ifinfomsg. 2098905cf0abSDavid Ahern */ 2099905cf0abSDavid Ahern hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ? 2100905cf0abSDavid Ahern sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); 2101905cf0abSDavid Ahern 21028cb08174SJohannes Berg return nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, 21038cb08174SJohannes Berg extack); 2104905cf0abSDavid Ahern } 2105905cf0abSDavid Ahern 2106b60c5115SThomas Graf static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 21071da177e4SLinus Torvalds { 2108905cf0abSDavid Ahern struct netlink_ext_ack *extack = cb->extack; 2109905cf0abSDavid Ahern const struct nlmsghdr *nlh = cb->nlh; 21103b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 211179e1ad14SJiri Benc struct net *tgt_net = net; 21127c28bd0bSEric Dumazet int h, s_h; 21137c28bd0bSEric Dumazet int idx = 0, s_idx; 21141da177e4SLinus Torvalds struct net_device *dev; 21157c28bd0bSEric Dumazet struct hlist_head *head; 2116115c9b81SGreg Rose struct nlattr *tb[IFLA_MAX+1]; 2117115c9b81SGreg Rose u32 ext_filter_mask = 0; 2118dc599f76SDavid Ahern const struct rtnl_link_ops *kind_ops = NULL; 2119dc599f76SDavid Ahern unsigned int flags = NLM_F_MULTI; 2120dc599f76SDavid Ahern int master_idx = 0; 212179e1ad14SJiri Benc int netnsid = -1; 2122905cf0abSDavid Ahern int err, i; 21231da177e4SLinus Torvalds 21247c28bd0bSEric Dumazet s_h = cb->args[0]; 21257c28bd0bSEric Dumazet s_idx = cb->args[1]; 21267c28bd0bSEric Dumazet 2127905cf0abSDavid Ahern err = rtnl_valid_dump_ifinfo_req(nlh, cb->strict_check, tb, extack); 2128905cf0abSDavid Ahern if (err < 0) { 2129905cf0abSDavid Ahern if (cb->strict_check) 2130905cf0abSDavid Ahern return err; 2131e5eca6d4SMichal Schmidt 2132905cf0abSDavid Ahern goto walk_entries; 213379e1ad14SJiri Benc } 213479e1ad14SJiri Benc 2135905cf0abSDavid Ahern for (i = 0; i <= IFLA_MAX; ++i) { 2136905cf0abSDavid Ahern if (!tb[i]) 2137905cf0abSDavid Ahern continue; 2138dc599f76SDavid Ahern 2139905cf0abSDavid Ahern /* new attributes should only be added with strict checking */ 2140905cf0abSDavid Ahern switch (i) { 2141905cf0abSDavid Ahern case IFLA_TARGET_NETNSID: 2142905cf0abSDavid Ahern netnsid = nla_get_s32(tb[i]); 2143905cf0abSDavid Ahern tgt_net = rtnl_get_net_ns_capable(skb->sk, netnsid); 2144905cf0abSDavid Ahern if (IS_ERR(tgt_net)) { 2145905cf0abSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid target network namespace id"); 2146905cf0abSDavid Ahern return PTR_ERR(tgt_net); 2147905cf0abSDavid Ahern } 2148905cf0abSDavid Ahern break; 2149905cf0abSDavid Ahern case IFLA_EXT_MASK: 2150905cf0abSDavid Ahern ext_filter_mask = nla_get_u32(tb[i]); 2151905cf0abSDavid Ahern break; 2152905cf0abSDavid Ahern case IFLA_MASTER: 2153905cf0abSDavid Ahern master_idx = nla_get_u32(tb[i]); 2154905cf0abSDavid Ahern break; 2155905cf0abSDavid Ahern case IFLA_LINKINFO: 2156905cf0abSDavid Ahern kind_ops = linkinfo_to_kind_ops(tb[i]); 2157905cf0abSDavid Ahern break; 2158905cf0abSDavid Ahern default: 2159905cf0abSDavid Ahern if (cb->strict_check) { 2160905cf0abSDavid Ahern NL_SET_ERR_MSG(extack, "Unsupported attribute in link dump request"); 2161905cf0abSDavid Ahern return -EINVAL; 2162905cf0abSDavid Ahern } 2163905cf0abSDavid Ahern } 2164905cf0abSDavid Ahern } 2165dc599f76SDavid Ahern 2166dc599f76SDavid Ahern if (master_idx || kind_ops) 2167dc599f76SDavid Ahern flags |= NLM_F_DUMP_FILTERED; 2168115c9b81SGreg Rose 2169905cf0abSDavid Ahern walk_entries: 21707c28bd0bSEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 21717562f876SPavel Emelianov idx = 0; 217279e1ad14SJiri Benc head = &tgt_net->dev_index_head[h]; 2173cac5e65eSEric Dumazet hlist_for_each_entry(dev, head, index_hlist) { 2174dc599f76SDavid Ahern if (link_dump_filtered(dev, master_idx, kind_ops)) 21753f0ae05dSZhang Shengju goto cont; 21761da177e4SLinus Torvalds if (idx < s_idx) 21777562f876SPavel Emelianov goto cont; 217879e1ad14SJiri Benc err = rtnl_fill_ifinfo(skb, dev, net, 217979e1ad14SJiri Benc RTM_NEWLINK, 218015e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 2181905cf0abSDavid Ahern nlh->nlmsg_seq, 0, flags, 218238e01b30SNicolas Dichtel ext_filter_mask, 0, NULL, 0, 2183d4e4fdf9SGuillaume Nault netnsid, GFP_KERNEL); 2184973462bbSDavid Gibson 2185f6c5775fSDavid Ahern if (err < 0) { 2186f6c5775fSDavid Ahern if (likely(skb->len)) 21877c28bd0bSEric Dumazet goto out; 21884e985adaSThomas Graf 2189f6c5775fSDavid Ahern goto out_err; 2190f6c5775fSDavid Ahern } 21917562f876SPavel Emelianov cont: 21927562f876SPavel Emelianov idx++; 21931da177e4SLinus Torvalds } 21947c28bd0bSEric Dumazet } 21957c28bd0bSEric Dumazet out: 2196f6c5775fSDavid Ahern err = skb->len; 2197f6c5775fSDavid Ahern out_err: 21987c28bd0bSEric Dumazet cb->args[1] = idx; 21997c28bd0bSEric Dumazet cb->args[0] = h; 2200a9ecb0cbSzhang kai cb->seq = tgt_net->dev_base_seq; 2201d0225784SJakub Sitnicki nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 220279e1ad14SJiri Benc if (netnsid >= 0) 220379e1ad14SJiri Benc put_net(tgt_net); 22041da177e4SLinus Torvalds 2205f6c5775fSDavid Ahern return err; 22061da177e4SLinus Torvalds } 22071da177e4SLinus Torvalds 2208fceb6435SJohannes Berg int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len, 2209fceb6435SJohannes Berg struct netlink_ext_ack *exterr) 2210f7b12606SJiri Pirko { 22118cb08174SJohannes Berg return nla_parse_deprecated(tb, IFLA_MAX, head, len, ifla_policy, 22128cb08174SJohannes Berg exterr); 2213f7b12606SJiri Pirko } 2214f7b12606SJiri Pirko EXPORT_SYMBOL(rtnl_nla_parse_ifla); 221557b61080SScott Feldman 221681adee47SEric W. Biederman struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[]) 221781adee47SEric W. Biederman { 221881adee47SEric W. Biederman struct net *net; 221981adee47SEric W. Biederman /* Examine the link attributes and figure out which 222081adee47SEric W. Biederman * network namespace we are talking about. 222181adee47SEric W. Biederman */ 222281adee47SEric W. Biederman if (tb[IFLA_NET_NS_PID]) 222381adee47SEric W. Biederman net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID])); 2224f0630529SEric W. Biederman else if (tb[IFLA_NET_NS_FD]) 2225f0630529SEric W. Biederman net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD])); 222681adee47SEric W. Biederman else 222781adee47SEric W. Biederman net = get_net(src_net); 222881adee47SEric W. Biederman return net; 222981adee47SEric W. Biederman } 223081adee47SEric W. Biederman EXPORT_SYMBOL(rtnl_link_get_net); 223181adee47SEric W. Biederman 22327c4f63baSChristian Brauner /* Figure out which network namespace we are talking about by 22337c4f63baSChristian Brauner * examining the link attributes in the following order: 22347c4f63baSChristian Brauner * 22357c4f63baSChristian Brauner * 1. IFLA_NET_NS_PID 22367c4f63baSChristian Brauner * 2. IFLA_NET_NS_FD 22377e4a8d5aSChristian Brauner * 3. IFLA_TARGET_NETNSID 22387c4f63baSChristian Brauner */ 22397c4f63baSChristian Brauner static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net, 22407c4f63baSChristian Brauner struct nlattr *tb[]) 22417c4f63baSChristian Brauner { 22427c4f63baSChristian Brauner struct net *net; 22437c4f63baSChristian Brauner 22447c4f63baSChristian Brauner if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) 22457c4f63baSChristian Brauner return rtnl_link_get_net(src_net, tb); 22467c4f63baSChristian Brauner 22477e4a8d5aSChristian Brauner if (!tb[IFLA_TARGET_NETNSID]) 22487c4f63baSChristian Brauner return get_net(src_net); 22497c4f63baSChristian Brauner 22507e4a8d5aSChristian Brauner net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_TARGET_NETNSID])); 22517c4f63baSChristian Brauner if (!net) 22527c4f63baSChristian Brauner return ERR_PTR(-EINVAL); 22537c4f63baSChristian Brauner 22547c4f63baSChristian Brauner return net; 22557c4f63baSChristian Brauner } 22567c4f63baSChristian Brauner 22577c4f63baSChristian Brauner static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb, 22587c4f63baSChristian Brauner struct net *src_net, 22597c4f63baSChristian Brauner struct nlattr *tb[], int cap) 22607c4f63baSChristian Brauner { 22617c4f63baSChristian Brauner struct net *net; 22627c4f63baSChristian Brauner 22637c4f63baSChristian Brauner net = rtnl_link_get_net_by_nlattr(src_net, tb); 22647c4f63baSChristian Brauner if (IS_ERR(net)) 22657c4f63baSChristian Brauner return net; 22667c4f63baSChristian Brauner 22677c4f63baSChristian Brauner if (!netlink_ns_capable(skb, net->user_ns, cap)) { 22687c4f63baSChristian Brauner put_net(net); 22697c4f63baSChristian Brauner return ERR_PTR(-EPERM); 22707c4f63baSChristian Brauner } 22717c4f63baSChristian Brauner 22727c4f63baSChristian Brauner return net; 22737c4f63baSChristian Brauner } 22747c4f63baSChristian Brauner 22754ff66caeSChristian Brauner /* Verify that rtnetlink requests do not pass additional properties 22764ff66caeSChristian Brauner * potentially referring to different network namespaces. 22774ff66caeSChristian Brauner */ 22784ff66caeSChristian Brauner static int rtnl_ensure_unique_netns(struct nlattr *tb[], 22794ff66caeSChristian Brauner struct netlink_ext_ack *extack, 22804ff66caeSChristian Brauner bool netns_id_only) 22814ff66caeSChristian Brauner { 22824ff66caeSChristian Brauner 22834ff66caeSChristian Brauner if (netns_id_only) { 22844ff66caeSChristian Brauner if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD]) 22854ff66caeSChristian Brauner return 0; 22864ff66caeSChristian Brauner 22874ff66caeSChristian Brauner NL_SET_ERR_MSG(extack, "specified netns attribute not supported"); 22884ff66caeSChristian Brauner return -EOPNOTSUPP; 22894ff66caeSChristian Brauner } 22904ff66caeSChristian Brauner 22917e4a8d5aSChristian Brauner if (tb[IFLA_TARGET_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])) 22924ff66caeSChristian Brauner goto invalid_attr; 22934ff66caeSChristian Brauner 22947e4a8d5aSChristian Brauner if (tb[IFLA_NET_NS_PID] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_FD])) 22954ff66caeSChristian Brauner goto invalid_attr; 22964ff66caeSChristian Brauner 22977e4a8d5aSChristian Brauner if (tb[IFLA_NET_NS_FD] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_PID])) 22984ff66caeSChristian Brauner goto invalid_attr; 22994ff66caeSChristian Brauner 23004ff66caeSChristian Brauner return 0; 23014ff66caeSChristian Brauner 23024ff66caeSChristian Brauner invalid_attr: 23034ff66caeSChristian Brauner NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified"); 23044ff66caeSChristian Brauner return -EINVAL; 23054ff66caeSChristian Brauner } 23064ff66caeSChristian Brauner 23078679c31eSRocco Yue static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[], 23088679c31eSRocco Yue struct netlink_ext_ack *extack) 23091840bb13SThomas Graf { 23101840bb13SThomas Graf if (dev) { 23111840bb13SThomas Graf if (tb[IFLA_ADDRESS] && 23121840bb13SThomas Graf nla_len(tb[IFLA_ADDRESS]) < dev->addr_len) 23131840bb13SThomas Graf return -EINVAL; 23141840bb13SThomas Graf 23151840bb13SThomas Graf if (tb[IFLA_BROADCAST] && 23161840bb13SThomas Graf nla_len(tb[IFLA_BROADCAST]) < dev->addr_len) 23171840bb13SThomas Graf return -EINVAL; 23181840bb13SThomas Graf } 23191840bb13SThomas Graf 2320cf7afbfeSThomas Graf if (tb[IFLA_AF_SPEC]) { 2321cf7afbfeSThomas Graf struct nlattr *af; 2322cf7afbfeSThomas Graf int rem, err; 2323cf7afbfeSThomas Graf 2324cf7afbfeSThomas Graf nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { 2325cf7afbfeSThomas Graf const struct rtnl_af_ops *af_ops; 2326cf7afbfeSThomas Graf 23275fa85a09SFlorian Westphal af_ops = rtnl_af_lookup(nla_type(af)); 2328a100243dSCong Wang if (!af_ops) 2329cf7afbfeSThomas Graf return -EAFNOSUPPORT; 2330cf7afbfeSThomas Graf 2331a100243dSCong Wang if (!af_ops->set_link_af) 2332cf7afbfeSThomas Graf return -EOPNOTSUPP; 2333cf7afbfeSThomas Graf 2334cf7afbfeSThomas Graf if (af_ops->validate_link_af) { 23358679c31eSRocco Yue err = af_ops->validate_link_af(dev, af, extack); 2336a100243dSCong Wang if (err < 0) 2337cf7afbfeSThomas Graf return err; 2338cf7afbfeSThomas Graf } 2339cf7afbfeSThomas Graf } 2340cf7afbfeSThomas Graf } 2341cf7afbfeSThomas Graf 2342eac1b93cSCoco Li if (tb[IFLA_GRO_MAX_SIZE]) { 2343eac1b93cSCoco Li u32 gro_max_size = nla_get_u32(tb[IFLA_GRO_MAX_SIZE]); 2344eac1b93cSCoco Li 2345eac1b93cSCoco Li if (gro_max_size > GRO_MAX_SIZE) { 2346eac1b93cSCoco Li NL_SET_ERR_MSG(extack, "too big gro_max_size"); 2347eac1b93cSCoco Li return -EINVAL; 2348eac1b93cSCoco Li } 2349eac1b93cSCoco Li } 23501840bb13SThomas Graf return 0; 23511840bb13SThomas Graf } 23521840bb13SThomas Graf 2353cc8e27ccSEli Cohen static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt, 2354cc8e27ccSEli Cohen int guid_type) 2355cc8e27ccSEli Cohen { 2356cc8e27ccSEli Cohen const struct net_device_ops *ops = dev->netdev_ops; 2357cc8e27ccSEli Cohen 2358cc8e27ccSEli Cohen return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type); 2359cc8e27ccSEli Cohen } 2360cc8e27ccSEli Cohen 2361cc8e27ccSEli Cohen static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type) 2362cc8e27ccSEli Cohen { 2363cc8e27ccSEli Cohen if (dev->type != ARPHRD_INFINIBAND) 2364cc8e27ccSEli Cohen return -EOPNOTSUPP; 2365cc8e27ccSEli Cohen 2366cc8e27ccSEli Cohen return handle_infiniband_guid(dev, ivt, guid_type); 2367cc8e27ccSEli Cohen } 2368cc8e27ccSEli Cohen 23694f7d2cdfSDaniel Borkmann static int do_setvfinfo(struct net_device *dev, struct nlattr **tb) 2370c02db8c6SChris Wright { 2371c02db8c6SChris Wright const struct net_device_ops *ops = dev->netdev_ops; 23724f7d2cdfSDaniel Borkmann int err = -EINVAL; 2373c02db8c6SChris Wright 23744f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_MAC]) { 23754f7d2cdfSDaniel Borkmann struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]); 23764f7d2cdfSDaniel Borkmann 2377ff08ddbaSDan Carpenter if (ivm->vf >= INT_MAX) 2378ff08ddbaSDan Carpenter return -EINVAL; 2379c02db8c6SChris Wright err = -EOPNOTSUPP; 2380c02db8c6SChris Wright if (ops->ndo_set_vf_mac) 2381c02db8c6SChris Wright err = ops->ndo_set_vf_mac(dev, ivm->vf, 2382c02db8c6SChris Wright ivm->mac); 23834f7d2cdfSDaniel Borkmann if (err < 0) 23844f7d2cdfSDaniel Borkmann return err; 2385c02db8c6SChris Wright } 23864f7d2cdfSDaniel Borkmann 23874f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_VLAN]) { 23884f7d2cdfSDaniel Borkmann struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]); 23894f7d2cdfSDaniel Borkmann 2390ff08ddbaSDan Carpenter if (ivv->vf >= INT_MAX) 2391ff08ddbaSDan Carpenter return -EINVAL; 2392c02db8c6SChris Wright err = -EOPNOTSUPP; 2393c02db8c6SChris Wright if (ops->ndo_set_vf_vlan) 23944f7d2cdfSDaniel Borkmann err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan, 239579aab093SMoshe Shemesh ivv->qos, 239679aab093SMoshe Shemesh htons(ETH_P_8021Q)); 239779aab093SMoshe Shemesh if (err < 0) 239879aab093SMoshe Shemesh return err; 239979aab093SMoshe Shemesh } 240079aab093SMoshe Shemesh 240179aab093SMoshe Shemesh if (tb[IFLA_VF_VLAN_LIST]) { 240279aab093SMoshe Shemesh struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN]; 240379aab093SMoshe Shemesh struct nlattr *attr; 240479aab093SMoshe Shemesh int rem, len = 0; 240579aab093SMoshe Shemesh 240679aab093SMoshe Shemesh err = -EOPNOTSUPP; 240779aab093SMoshe Shemesh if (!ops->ndo_set_vf_vlan) 240879aab093SMoshe Shemesh return err; 240979aab093SMoshe Shemesh 241079aab093SMoshe Shemesh nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) { 241179aab093SMoshe Shemesh if (nla_type(attr) != IFLA_VF_VLAN_INFO || 241279aab093SMoshe Shemesh nla_len(attr) < NLA_HDRLEN) { 241379aab093SMoshe Shemesh return -EINVAL; 241479aab093SMoshe Shemesh } 241579aab093SMoshe Shemesh if (len >= MAX_VLAN_LIST_LEN) 241679aab093SMoshe Shemesh return -EOPNOTSUPP; 241779aab093SMoshe Shemesh ivvl[len] = nla_data(attr); 241879aab093SMoshe Shemesh 241979aab093SMoshe Shemesh len++; 242079aab093SMoshe Shemesh } 2421fa34cd94SArnd Bergmann if (len == 0) 2422fa34cd94SArnd Bergmann return -EINVAL; 2423fa34cd94SArnd Bergmann 2424ff08ddbaSDan Carpenter if (ivvl[0]->vf >= INT_MAX) 2425ff08ddbaSDan Carpenter return -EINVAL; 242679aab093SMoshe Shemesh err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan, 242779aab093SMoshe Shemesh ivvl[0]->qos, ivvl[0]->vlan_proto); 24284f7d2cdfSDaniel Borkmann if (err < 0) 24294f7d2cdfSDaniel Borkmann return err; 2430c02db8c6SChris Wright } 24314f7d2cdfSDaniel Borkmann 24324f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_TX_RATE]) { 24334f7d2cdfSDaniel Borkmann struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]); 2434ed616689SSucheta Chakraborty struct ifla_vf_info ivf; 24354f7d2cdfSDaniel Borkmann 2436ff08ddbaSDan Carpenter if (ivt->vf >= INT_MAX) 2437ff08ddbaSDan Carpenter return -EINVAL; 2438c02db8c6SChris Wright err = -EOPNOTSUPP; 2439ed616689SSucheta Chakraborty if (ops->ndo_get_vf_config) 24404f7d2cdfSDaniel Borkmann err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf); 24414f7d2cdfSDaniel Borkmann if (err < 0) 24424f7d2cdfSDaniel Borkmann return err; 24434f7d2cdfSDaniel Borkmann 2444ed616689SSucheta Chakraborty err = -EOPNOTSUPP; 2445ed616689SSucheta Chakraborty if (ops->ndo_set_vf_rate) 2446ed616689SSucheta Chakraborty err = ops->ndo_set_vf_rate(dev, ivt->vf, 2447ed616689SSucheta Chakraborty ivf.min_tx_rate, 2448c02db8c6SChris Wright ivt->rate); 24494f7d2cdfSDaniel Borkmann if (err < 0) 24504f7d2cdfSDaniel Borkmann return err; 2451c02db8c6SChris Wright } 24524f7d2cdfSDaniel Borkmann 24534f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_RATE]) { 24544f7d2cdfSDaniel Borkmann struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]); 24554f7d2cdfSDaniel Borkmann 2456ff08ddbaSDan Carpenter if (ivt->vf >= INT_MAX) 2457ff08ddbaSDan Carpenter return -EINVAL; 2458ed616689SSucheta Chakraborty err = -EOPNOTSUPP; 2459ed616689SSucheta Chakraborty if (ops->ndo_set_vf_rate) 2460ed616689SSucheta Chakraborty err = ops->ndo_set_vf_rate(dev, ivt->vf, 2461ed616689SSucheta Chakraborty ivt->min_tx_rate, 2462ed616689SSucheta Chakraborty ivt->max_tx_rate); 24634f7d2cdfSDaniel Borkmann if (err < 0) 24644f7d2cdfSDaniel Borkmann return err; 2465ed616689SSucheta Chakraborty } 24664f7d2cdfSDaniel Borkmann 24674f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_SPOOFCHK]) { 24684f7d2cdfSDaniel Borkmann struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]); 24694f7d2cdfSDaniel Borkmann 2470ff08ddbaSDan Carpenter if (ivs->vf >= INT_MAX) 2471ff08ddbaSDan Carpenter return -EINVAL; 24725f8444a3SGreg Rose err = -EOPNOTSUPP; 24735f8444a3SGreg Rose if (ops->ndo_set_vf_spoofchk) 24745f8444a3SGreg Rose err = ops->ndo_set_vf_spoofchk(dev, ivs->vf, 24755f8444a3SGreg Rose ivs->setting); 24764f7d2cdfSDaniel Borkmann if (err < 0) 24774f7d2cdfSDaniel Borkmann return err; 24785f8444a3SGreg Rose } 24794f7d2cdfSDaniel Borkmann 24804f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_LINK_STATE]) { 24814f7d2cdfSDaniel Borkmann struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]); 24824f7d2cdfSDaniel Borkmann 2483ff08ddbaSDan Carpenter if (ivl->vf >= INT_MAX) 2484ff08ddbaSDan Carpenter return -EINVAL; 24851d8faf48SRony Efraim err = -EOPNOTSUPP; 24861d8faf48SRony Efraim if (ops->ndo_set_vf_link_state) 24871d8faf48SRony Efraim err = ops->ndo_set_vf_link_state(dev, ivl->vf, 24881d8faf48SRony Efraim ivl->link_state); 24894f7d2cdfSDaniel Borkmann if (err < 0) 24904f7d2cdfSDaniel Borkmann return err; 24911d8faf48SRony Efraim } 24924f7d2cdfSDaniel Borkmann 24934f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_RSS_QUERY_EN]) { 249401a3d796SVlad Zolotarov struct ifla_vf_rss_query_en *ivrssq_en; 249501a3d796SVlad Zolotarov 249601a3d796SVlad Zolotarov err = -EOPNOTSUPP; 24974f7d2cdfSDaniel Borkmann ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]); 2498ff08ddbaSDan Carpenter if (ivrssq_en->vf >= INT_MAX) 2499ff08ddbaSDan Carpenter return -EINVAL; 250001a3d796SVlad Zolotarov if (ops->ndo_set_vf_rss_query_en) 25014f7d2cdfSDaniel Borkmann err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf, 250201a3d796SVlad Zolotarov ivrssq_en->setting); 25034f7d2cdfSDaniel Borkmann if (err < 0) 25044f7d2cdfSDaniel Borkmann return err; 250501a3d796SVlad Zolotarov } 25064f7d2cdfSDaniel Borkmann 2507dd461d6aSHiroshi Shimamoto if (tb[IFLA_VF_TRUST]) { 2508dd461d6aSHiroshi Shimamoto struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]); 2509dd461d6aSHiroshi Shimamoto 2510ff08ddbaSDan Carpenter if (ivt->vf >= INT_MAX) 2511ff08ddbaSDan Carpenter return -EINVAL; 2512dd461d6aSHiroshi Shimamoto err = -EOPNOTSUPP; 2513dd461d6aSHiroshi Shimamoto if (ops->ndo_set_vf_trust) 2514dd461d6aSHiroshi Shimamoto err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting); 2515dd461d6aSHiroshi Shimamoto if (err < 0) 2516dd461d6aSHiroshi Shimamoto return err; 2517dd461d6aSHiroshi Shimamoto } 2518dd461d6aSHiroshi Shimamoto 2519cc8e27ccSEli Cohen if (tb[IFLA_VF_IB_NODE_GUID]) { 2520cc8e27ccSEli Cohen struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]); 2521cc8e27ccSEli Cohen 2522ff08ddbaSDan Carpenter if (ivt->vf >= INT_MAX) 2523ff08ddbaSDan Carpenter return -EINVAL; 2524cc8e27ccSEli Cohen if (!ops->ndo_set_vf_guid) 2525cc8e27ccSEli Cohen return -EOPNOTSUPP; 2526cc8e27ccSEli Cohen return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID); 2527cc8e27ccSEli Cohen } 2528cc8e27ccSEli Cohen 2529cc8e27ccSEli Cohen if (tb[IFLA_VF_IB_PORT_GUID]) { 2530cc8e27ccSEli Cohen struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]); 2531cc8e27ccSEli Cohen 2532ff08ddbaSDan Carpenter if (ivt->vf >= INT_MAX) 2533ff08ddbaSDan Carpenter return -EINVAL; 2534cc8e27ccSEli Cohen if (!ops->ndo_set_vf_guid) 2535cc8e27ccSEli Cohen return -EOPNOTSUPP; 2536cc8e27ccSEli Cohen 2537cc8e27ccSEli Cohen return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID); 2538cc8e27ccSEli Cohen } 2539cc8e27ccSEli Cohen 2540c02db8c6SChris Wright return err; 2541c02db8c6SChris Wright } 2542c02db8c6SChris Wright 254333eaf2a6SDavid Ahern static int do_set_master(struct net_device *dev, int ifindex, 254433eaf2a6SDavid Ahern struct netlink_ext_ack *extack) 2545fbaec0eaSJiri Pirko { 2546898e5061SJiri Pirko struct net_device *upper_dev = netdev_master_upper_dev_get(dev); 2547fbaec0eaSJiri Pirko const struct net_device_ops *ops; 2548fbaec0eaSJiri Pirko int err; 2549fbaec0eaSJiri Pirko 2550898e5061SJiri Pirko if (upper_dev) { 2551898e5061SJiri Pirko if (upper_dev->ifindex == ifindex) 2552fbaec0eaSJiri Pirko return 0; 2553898e5061SJiri Pirko ops = upper_dev->netdev_ops; 2554fbaec0eaSJiri Pirko if (ops->ndo_del_slave) { 2555898e5061SJiri Pirko err = ops->ndo_del_slave(upper_dev, dev); 2556fbaec0eaSJiri Pirko if (err) 2557fbaec0eaSJiri Pirko return err; 2558fbaec0eaSJiri Pirko } else { 2559fbaec0eaSJiri Pirko return -EOPNOTSUPP; 2560fbaec0eaSJiri Pirko } 2561fbaec0eaSJiri Pirko } 2562fbaec0eaSJiri Pirko 2563fbaec0eaSJiri Pirko if (ifindex) { 2564898e5061SJiri Pirko upper_dev = __dev_get_by_index(dev_net(dev), ifindex); 2565898e5061SJiri Pirko if (!upper_dev) 2566fbaec0eaSJiri Pirko return -EINVAL; 2567898e5061SJiri Pirko ops = upper_dev->netdev_ops; 2568fbaec0eaSJiri Pirko if (ops->ndo_add_slave) { 256933eaf2a6SDavid Ahern err = ops->ndo_add_slave(upper_dev, dev, extack); 2570fbaec0eaSJiri Pirko if (err) 2571fbaec0eaSJiri Pirko return err; 2572fbaec0eaSJiri Pirko } else { 2573fbaec0eaSJiri Pirko return -EOPNOTSUPP; 2574fbaec0eaSJiri Pirko } 2575fbaec0eaSJiri Pirko } 2576fbaec0eaSJiri Pirko return 0; 2577fbaec0eaSJiri Pirko } 2578fbaec0eaSJiri Pirko 2579829eb208SRoopa Prabhu static const struct nla_policy ifla_proto_down_reason_policy[IFLA_PROTO_DOWN_REASON_VALUE + 1] = { 2580829eb208SRoopa Prabhu [IFLA_PROTO_DOWN_REASON_MASK] = { .type = NLA_U32 }, 2581829eb208SRoopa Prabhu [IFLA_PROTO_DOWN_REASON_VALUE] = { .type = NLA_U32 }, 2582829eb208SRoopa Prabhu }; 2583829eb208SRoopa Prabhu 2584829eb208SRoopa Prabhu static int do_set_proto_down(struct net_device *dev, 2585829eb208SRoopa Prabhu struct nlattr *nl_proto_down, 2586829eb208SRoopa Prabhu struct nlattr *nl_proto_down_reason, 2587829eb208SRoopa Prabhu struct netlink_ext_ack *extack) 2588829eb208SRoopa Prabhu { 2589829eb208SRoopa Prabhu struct nlattr *pdreason[IFLA_PROTO_DOWN_REASON_MAX + 1]; 2590829eb208SRoopa Prabhu unsigned long mask = 0; 2591829eb208SRoopa Prabhu u32 value; 2592829eb208SRoopa Prabhu bool proto_down; 2593829eb208SRoopa Prabhu int err; 2594829eb208SRoopa Prabhu 25952106efdaSJakub Kicinski if (!(dev->priv_flags & IFF_CHANGE_PROTO_DOWN)) { 2596829eb208SRoopa Prabhu NL_SET_ERR_MSG(extack, "Protodown not supported by device"); 2597829eb208SRoopa Prabhu return -EOPNOTSUPP; 2598829eb208SRoopa Prabhu } 2599829eb208SRoopa Prabhu 2600829eb208SRoopa Prabhu if (nl_proto_down_reason) { 2601829eb208SRoopa Prabhu err = nla_parse_nested_deprecated(pdreason, 2602829eb208SRoopa Prabhu IFLA_PROTO_DOWN_REASON_MAX, 2603829eb208SRoopa Prabhu nl_proto_down_reason, 2604829eb208SRoopa Prabhu ifla_proto_down_reason_policy, 2605829eb208SRoopa Prabhu NULL); 2606829eb208SRoopa Prabhu if (err < 0) 2607829eb208SRoopa Prabhu return err; 2608829eb208SRoopa Prabhu 2609829eb208SRoopa Prabhu if (!pdreason[IFLA_PROTO_DOWN_REASON_VALUE]) { 2610829eb208SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid protodown reason value"); 2611829eb208SRoopa Prabhu return -EINVAL; 2612829eb208SRoopa Prabhu } 2613829eb208SRoopa Prabhu 2614829eb208SRoopa Prabhu value = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_VALUE]); 2615829eb208SRoopa Prabhu 2616829eb208SRoopa Prabhu if (pdreason[IFLA_PROTO_DOWN_REASON_MASK]) 2617829eb208SRoopa Prabhu mask = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_MASK]); 2618829eb208SRoopa Prabhu 2619829eb208SRoopa Prabhu dev_change_proto_down_reason(dev, mask, value); 2620829eb208SRoopa Prabhu } 2621829eb208SRoopa Prabhu 2622829eb208SRoopa Prabhu if (nl_proto_down) { 2623829eb208SRoopa Prabhu proto_down = nla_get_u8(nl_proto_down); 2624829eb208SRoopa Prabhu 2625d467d0bcSZheng Yongjun /* Don't turn off protodown if there are active reasons */ 2626829eb208SRoopa Prabhu if (!proto_down && dev->proto_down_reason) { 2627829eb208SRoopa Prabhu NL_SET_ERR_MSG(extack, "Cannot clear protodown, active reasons"); 2628829eb208SRoopa Prabhu return -EBUSY; 2629829eb208SRoopa Prabhu } 2630829eb208SRoopa Prabhu err = dev_change_proto_down(dev, 2631829eb208SRoopa Prabhu proto_down); 2632829eb208SRoopa Prabhu if (err) 2633829eb208SRoopa Prabhu return err; 2634829eb208SRoopa Prabhu } 2635829eb208SRoopa Prabhu 2636829eb208SRoopa Prabhu return 0; 2637829eb208SRoopa Prabhu } 2638829eb208SRoopa Prabhu 263990c325e3SNicolas Dichtel #define DO_SETLINK_MODIFIED 0x01 2640ba998906SNicolas Dichtel /* notify flag means notify + modified. */ 2641ba998906SNicolas Dichtel #define DO_SETLINK_NOTIFY 0x03 264290f62cf3SEric W. Biederman static int do_setlink(const struct sk_buff *skb, 264390f62cf3SEric W. Biederman struct net_device *dev, struct ifinfomsg *ifm, 2644ddf9f970SJakub Kicinski struct netlink_ext_ack *extack, 264590c325e3SNicolas Dichtel struct nlattr **tb, char *ifname, int status) 26460157f60cSPatrick McHardy { 2647d314774cSStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops; 26480157f60cSPatrick McHardy int err; 26490157f60cSPatrick McHardy 26508679c31eSRocco Yue err = validate_linkmsg(dev, tb, extack); 2651644c7eebSEric Dumazet if (err < 0) 2652644c7eebSEric Dumazet return err; 2653644c7eebSEric Dumazet 26547e4a8d5aSChristian Brauner if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_TARGET_NETNSID]) { 265596a6b93bSAndrey Ignatov const char *pat = ifname && ifname[0] ? ifname : NULL; 2656eeb85a14SAndrei Vagin struct net *net; 2657eeb85a14SAndrei Vagin int new_ifindex; 2658eeb85a14SAndrei Vagin 2659eeb85a14SAndrei Vagin net = rtnl_link_get_net_capable(skb, dev_net(dev), 26607c4f63baSChristian Brauner tb, CAP_NET_ADMIN); 2661d8a5ec67SEric W. Biederman if (IS_ERR(net)) { 2662d8a5ec67SEric W. Biederman err = PTR_ERR(net); 2663d8a5ec67SEric W. Biederman goto errout; 2664d8a5ec67SEric W. Biederman } 26657c4f63baSChristian Brauner 2666eeb85a14SAndrei Vagin if (tb[IFLA_NEW_IFINDEX]) 2667eeb85a14SAndrei Vagin new_ifindex = nla_get_s32(tb[IFLA_NEW_IFINDEX]); 2668eeb85a14SAndrei Vagin else 2669eeb85a14SAndrei Vagin new_ifindex = 0; 2670eeb85a14SAndrei Vagin 267196a6b93bSAndrey Ignatov err = __dev_change_net_namespace(dev, net, pat, new_ifindex); 2672d8a5ec67SEric W. Biederman put_net(net); 2673d8a5ec67SEric W. Biederman if (err) 2674d8a5ec67SEric W. Biederman goto errout; 267590c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 2676d8a5ec67SEric W. Biederman } 2677d8a5ec67SEric W. Biederman 26780157f60cSPatrick McHardy if (tb[IFLA_MAP]) { 26790157f60cSPatrick McHardy struct rtnl_link_ifmap *u_map; 26800157f60cSPatrick McHardy struct ifmap k_map; 26810157f60cSPatrick McHardy 2682d314774cSStephen Hemminger if (!ops->ndo_set_config) { 26830157f60cSPatrick McHardy err = -EOPNOTSUPP; 26840157f60cSPatrick McHardy goto errout; 26850157f60cSPatrick McHardy } 26860157f60cSPatrick McHardy 26870157f60cSPatrick McHardy if (!netif_device_present(dev)) { 26880157f60cSPatrick McHardy err = -ENODEV; 26890157f60cSPatrick McHardy goto errout; 26900157f60cSPatrick McHardy } 26910157f60cSPatrick McHardy 26920157f60cSPatrick McHardy u_map = nla_data(tb[IFLA_MAP]); 26930157f60cSPatrick McHardy k_map.mem_start = (unsigned long) u_map->mem_start; 26940157f60cSPatrick McHardy k_map.mem_end = (unsigned long) u_map->mem_end; 26950157f60cSPatrick McHardy k_map.base_addr = (unsigned short) u_map->base_addr; 26960157f60cSPatrick McHardy k_map.irq = (unsigned char) u_map->irq; 26970157f60cSPatrick McHardy k_map.dma = (unsigned char) u_map->dma; 26980157f60cSPatrick McHardy k_map.port = (unsigned char) u_map->port; 26990157f60cSPatrick McHardy 2700d314774cSStephen Hemminger err = ops->ndo_set_config(dev, &k_map); 27010157f60cSPatrick McHardy if (err < 0) 27020157f60cSPatrick McHardy goto errout; 27030157f60cSPatrick McHardy 2704ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 27050157f60cSPatrick McHardy } 27060157f60cSPatrick McHardy 27070157f60cSPatrick McHardy if (tb[IFLA_ADDRESS]) { 27080157f60cSPatrick McHardy struct sockaddr *sa; 27090157f60cSPatrick McHardy int len; 27100157f60cSPatrick McHardy 2711153711f9SWANG Cong len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len, 2712153711f9SWANG Cong sizeof(*sa)); 27130157f60cSPatrick McHardy sa = kmalloc(len, GFP_KERNEL); 27140157f60cSPatrick McHardy if (!sa) { 27150157f60cSPatrick McHardy err = -ENOMEM; 27160157f60cSPatrick McHardy goto errout; 27170157f60cSPatrick McHardy } 27180157f60cSPatrick McHardy sa->sa_family = dev->type; 27190157f60cSPatrick McHardy memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]), 27200157f60cSPatrick McHardy dev->addr_len); 27213b23a32aSCong Wang err = dev_set_mac_address_user(dev, sa, extack); 27220157f60cSPatrick McHardy kfree(sa); 27230157f60cSPatrick McHardy if (err) 27240157f60cSPatrick McHardy goto errout; 272590c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 27260157f60cSPatrick McHardy } 27270157f60cSPatrick McHardy 27280157f60cSPatrick McHardy if (tb[IFLA_MTU]) { 27297a4c53beSStephen Hemminger err = dev_set_mtu_ext(dev, nla_get_u32(tb[IFLA_MTU]), extack); 27300157f60cSPatrick McHardy if (err < 0) 27310157f60cSPatrick McHardy goto errout; 273290c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 27330157f60cSPatrick McHardy } 27340157f60cSPatrick McHardy 2735cbda10faSVlad Dogaru if (tb[IFLA_GROUP]) { 2736cbda10faSVlad Dogaru dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); 2737ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 2738cbda10faSVlad Dogaru } 2739cbda10faSVlad Dogaru 27400157f60cSPatrick McHardy /* 27410157f60cSPatrick McHardy * Interface selected by interface index but interface 27420157f60cSPatrick McHardy * name provided implies that a name change has been 27430157f60cSPatrick McHardy * requested. 27440157f60cSPatrick McHardy */ 27450157f60cSPatrick McHardy if (ifm->ifi_index > 0 && ifname[0]) { 27460157f60cSPatrick McHardy err = dev_change_name(dev, ifname); 27470157f60cSPatrick McHardy if (err < 0) 27480157f60cSPatrick McHardy goto errout; 274990c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 27500157f60cSPatrick McHardy } 27510157f60cSPatrick McHardy 27520b815a1aSStephen Hemminger if (tb[IFLA_IFALIAS]) { 27530b815a1aSStephen Hemminger err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]), 27540b815a1aSStephen Hemminger nla_len(tb[IFLA_IFALIAS])); 27550b815a1aSStephen Hemminger if (err < 0) 27560b815a1aSStephen Hemminger goto errout; 2757ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 27580b815a1aSStephen Hemminger } 27590b815a1aSStephen Hemminger 27600157f60cSPatrick McHardy if (tb[IFLA_BROADCAST]) { 27610157f60cSPatrick McHardy nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len); 2762e7c3273eSJiri Pirko call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); 27630157f60cSPatrick McHardy } 27640157f60cSPatrick McHardy 27650157f60cSPatrick McHardy if (ifm->ifi_flags || ifm->ifi_change) { 2766567c5e13SPetr Machata err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm), 2767567c5e13SPetr Machata extack); 27685f9021cfSJohannes Berg if (err < 0) 27695f9021cfSJohannes Berg goto errout; 27700157f60cSPatrick McHardy } 27710157f60cSPatrick McHardy 2772fbaec0eaSJiri Pirko if (tb[IFLA_MASTER]) { 277333eaf2a6SDavid Ahern err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack); 2774fbaec0eaSJiri Pirko if (err) 2775fbaec0eaSJiri Pirko goto errout; 277690c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 2777fbaec0eaSJiri Pirko } 2778fbaec0eaSJiri Pirko 27799a57247fSJiri Pirko if (tb[IFLA_CARRIER]) { 27809a57247fSJiri Pirko err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER])); 27819a57247fSJiri Pirko if (err) 27829a57247fSJiri Pirko goto errout; 278390c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 27849a57247fSJiri Pirko } 27859a57247fSJiri Pirko 27865d1180fcSNicolas Dichtel if (tb[IFLA_TXQLEN]) { 27870cd29503SAlexey Dobriyan unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]); 27885d1180fcSNicolas Dichtel 27896a643ddbSCong Wang err = dev_change_tx_queue_len(dev, value); 27906a643ddbSCong Wang if (err) 279108294a26SJason Wang goto errout; 27922d7f669bSXin Long status |= DO_SETLINK_MODIFIED; 279308294a26SJason Wang } 27940157f60cSPatrick McHardy 279546e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SIZE]) { 279646e6b992SStephen Hemminger u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]); 279746e6b992SStephen Hemminger 279846e6b992SStephen Hemminger if (max_size > GSO_MAX_SIZE) { 279946e6b992SStephen Hemminger err = -EINVAL; 280046e6b992SStephen Hemminger goto errout; 280146e6b992SStephen Hemminger } 280246e6b992SStephen Hemminger 280346e6b992SStephen Hemminger if (dev->gso_max_size ^ max_size) { 280446e6b992SStephen Hemminger netif_set_gso_max_size(dev, max_size); 280546e6b992SStephen Hemminger status |= DO_SETLINK_MODIFIED; 280646e6b992SStephen Hemminger } 280746e6b992SStephen Hemminger } 280846e6b992SStephen Hemminger 280946e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SEGS]) { 281046e6b992SStephen Hemminger u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]); 281146e6b992SStephen Hemminger 281246e6b992SStephen Hemminger if (max_segs > GSO_MAX_SEGS) { 281346e6b992SStephen Hemminger err = -EINVAL; 281446e6b992SStephen Hemminger goto errout; 281546e6b992SStephen Hemminger } 281646e6b992SStephen Hemminger 281746e6b992SStephen Hemminger if (dev->gso_max_segs ^ max_segs) { 28186d872df3SEric Dumazet netif_set_gso_max_segs(dev, max_segs); 281946e6b992SStephen Hemminger status |= DO_SETLINK_MODIFIED; 282046e6b992SStephen Hemminger } 282146e6b992SStephen Hemminger } 282246e6b992SStephen Hemminger 2823eac1b93cSCoco Li if (tb[IFLA_GRO_MAX_SIZE]) { 2824eac1b93cSCoco Li u32 gro_max_size = nla_get_u32(tb[IFLA_GRO_MAX_SIZE]); 2825eac1b93cSCoco Li 2826eac1b93cSCoco Li if (dev->gro_max_size ^ gro_max_size) { 2827eac1b93cSCoco Li netif_set_gro_max_size(dev, gro_max_size); 2828eac1b93cSCoco Li status |= DO_SETLINK_MODIFIED; 2829eac1b93cSCoco Li } 2830eac1b93cSCoco Li } 2831eac1b93cSCoco Li 28320157f60cSPatrick McHardy if (tb[IFLA_OPERSTATE]) 283393b2d4a2SDavid S. Miller set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 28340157f60cSPatrick McHardy 28350157f60cSPatrick McHardy if (tb[IFLA_LINKMODE]) { 28361889b0e7SNicolas Dichtel unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]); 28371889b0e7SNicolas Dichtel 2838fd888e85SSebastian Andrzej Siewior write_lock(&dev_base_lock); 28391889b0e7SNicolas Dichtel if (dev->link_mode ^ value) 2840ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 28411889b0e7SNicolas Dichtel dev->link_mode = value; 2842fd888e85SSebastian Andrzej Siewior write_unlock(&dev_base_lock); 28430157f60cSPatrick McHardy } 28440157f60cSPatrick McHardy 2845c02db8c6SChris Wright if (tb[IFLA_VFINFO_LIST]) { 28464f7d2cdfSDaniel Borkmann struct nlattr *vfinfo[IFLA_VF_MAX + 1]; 2847c02db8c6SChris Wright struct nlattr *attr; 2848c02db8c6SChris Wright int rem; 28494f7d2cdfSDaniel Borkmann 2850c02db8c6SChris Wright nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) { 28514f7d2cdfSDaniel Borkmann if (nla_type(attr) != IFLA_VF_INFO || 28524f7d2cdfSDaniel Borkmann nla_len(attr) < NLA_HDRLEN) { 2853253683bbSDavid Howells err = -EINVAL; 2854c02db8c6SChris Wright goto errout; 2855253683bbSDavid Howells } 28568cb08174SJohannes Berg err = nla_parse_nested_deprecated(vfinfo, IFLA_VF_MAX, 28578cb08174SJohannes Berg attr, 28588cb08174SJohannes Berg ifla_vf_policy, 28598cb08174SJohannes Berg NULL); 28604f7d2cdfSDaniel Borkmann if (err < 0) 28614f7d2cdfSDaniel Borkmann goto errout; 28624f7d2cdfSDaniel Borkmann err = do_setvfinfo(dev, vfinfo); 2863ebc08a6fSWilliams, Mitch A if (err < 0) 2864ebc08a6fSWilliams, Mitch A goto errout; 2865ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 2866ebc08a6fSWilliams, Mitch A } 2867ebc08a6fSWilliams, Mitch A } 28680157f60cSPatrick McHardy err = 0; 28690157f60cSPatrick McHardy 287057b61080SScott Feldman if (tb[IFLA_VF_PORTS]) { 287157b61080SScott Feldman struct nlattr *port[IFLA_PORT_MAX+1]; 287257b61080SScott Feldman struct nlattr *attr; 287357b61080SScott Feldman int vf; 287457b61080SScott Feldman int rem; 287557b61080SScott Feldman 287657b61080SScott Feldman err = -EOPNOTSUPP; 287757b61080SScott Feldman if (!ops->ndo_set_vf_port) 287857b61080SScott Feldman goto errout; 287957b61080SScott Feldman 288057b61080SScott Feldman nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) { 2881035d210fSDaniel Borkmann if (nla_type(attr) != IFLA_VF_PORT || 2882035d210fSDaniel Borkmann nla_len(attr) < NLA_HDRLEN) { 2883035d210fSDaniel Borkmann err = -EINVAL; 2884035d210fSDaniel Borkmann goto errout; 2885035d210fSDaniel Borkmann } 28868cb08174SJohannes Berg err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX, 28878cb08174SJohannes Berg attr, 28888cb08174SJohannes Berg ifla_port_policy, 28898cb08174SJohannes Berg NULL); 289057b61080SScott Feldman if (err < 0) 289157b61080SScott Feldman goto errout; 289257b61080SScott Feldman if (!port[IFLA_PORT_VF]) { 289357b61080SScott Feldman err = -EOPNOTSUPP; 289457b61080SScott Feldman goto errout; 289557b61080SScott Feldman } 289657b61080SScott Feldman vf = nla_get_u32(port[IFLA_PORT_VF]); 289757b61080SScott Feldman err = ops->ndo_set_vf_port(dev, vf, port); 289857b61080SScott Feldman if (err < 0) 289957b61080SScott Feldman goto errout; 2900ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 290157b61080SScott Feldman } 290257b61080SScott Feldman } 290357b61080SScott Feldman err = 0; 290457b61080SScott Feldman 290557b61080SScott Feldman if (tb[IFLA_PORT_SELF]) { 290657b61080SScott Feldman struct nlattr *port[IFLA_PORT_MAX+1]; 290757b61080SScott Feldman 29088cb08174SJohannes Berg err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX, 29098cb08174SJohannes Berg tb[IFLA_PORT_SELF], 29108cb08174SJohannes Berg ifla_port_policy, NULL); 291157b61080SScott Feldman if (err < 0) 291257b61080SScott Feldman goto errout; 291357b61080SScott Feldman 291457b61080SScott Feldman err = -EOPNOTSUPP; 291557b61080SScott Feldman if (ops->ndo_set_vf_port) 291657b61080SScott Feldman err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port); 291757b61080SScott Feldman if (err < 0) 291857b61080SScott Feldman goto errout; 2919ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 292057b61080SScott Feldman } 2921f8ff182cSThomas Graf 2922f8ff182cSThomas Graf if (tb[IFLA_AF_SPEC]) { 2923f8ff182cSThomas Graf struct nlattr *af; 2924f8ff182cSThomas Graf int rem; 2925f8ff182cSThomas Graf 2926f8ff182cSThomas Graf nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { 2927f8ff182cSThomas Graf const struct rtnl_af_ops *af_ops; 2928f8ff182cSThomas Graf 2929058c8d59SGustavo A. R. Silva BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af)))); 2930f8ff182cSThomas Graf 29313583a4e8SStephen Hemminger err = af_ops->set_link_af(dev, af, extack); 2932a100243dSCong Wang if (err < 0) 2933f8ff182cSThomas Graf goto errout; 2934f8ff182cSThomas Graf 2935ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 2936f8ff182cSThomas Graf } 2937f8ff182cSThomas Graf } 293857b61080SScott Feldman err = 0; 293957b61080SScott Feldman 2940829eb208SRoopa Prabhu if (tb[IFLA_PROTO_DOWN] || tb[IFLA_PROTO_DOWN_REASON]) { 2941829eb208SRoopa Prabhu err = do_set_proto_down(dev, tb[IFLA_PROTO_DOWN], 2942829eb208SRoopa Prabhu tb[IFLA_PROTO_DOWN_REASON], extack); 294388d6378bSAnuradha Karuppiah if (err) 294488d6378bSAnuradha Karuppiah goto errout; 294588d6378bSAnuradha Karuppiah status |= DO_SETLINK_NOTIFY; 294688d6378bSAnuradha Karuppiah } 294788d6378bSAnuradha Karuppiah 2948d1fdd913SBrenden Blanco if (tb[IFLA_XDP]) { 2949d1fdd913SBrenden Blanco struct nlattr *xdp[IFLA_XDP_MAX + 1]; 295085de8576SDaniel Borkmann u32 xdp_flags = 0; 2951d1fdd913SBrenden Blanco 29528cb08174SJohannes Berg err = nla_parse_nested_deprecated(xdp, IFLA_XDP_MAX, 29538cb08174SJohannes Berg tb[IFLA_XDP], 2954fceb6435SJohannes Berg ifla_xdp_policy, NULL); 2955d1fdd913SBrenden Blanco if (err < 0) 2956d1fdd913SBrenden Blanco goto errout; 2957d1fdd913SBrenden Blanco 295858038695SMartin KaFai Lau if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) { 2959262d8625SBrenden Blanco err = -EINVAL; 2960262d8625SBrenden Blanco goto errout; 2961262d8625SBrenden Blanco } 296285de8576SDaniel Borkmann 296385de8576SDaniel Borkmann if (xdp[IFLA_XDP_FLAGS]) { 296485de8576SDaniel Borkmann xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]); 296585de8576SDaniel Borkmann if (xdp_flags & ~XDP_FLAGS_MASK) { 296685de8576SDaniel Borkmann err = -EINVAL; 296785de8576SDaniel Borkmann goto errout; 296885de8576SDaniel Borkmann } 2969ee5d032fSJakub Kicinski if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) { 29700489df9aSDaniel Borkmann err = -EINVAL; 29710489df9aSDaniel Borkmann goto errout; 29720489df9aSDaniel Borkmann } 297385de8576SDaniel Borkmann } 297485de8576SDaniel Borkmann 2975d1fdd913SBrenden Blanco if (xdp[IFLA_XDP_FD]) { 297692234c8fSToke Høiland-Jørgensen int expected_fd = -1; 297792234c8fSToke Høiland-Jørgensen 297892234c8fSToke Høiland-Jørgensen if (xdp_flags & XDP_FLAGS_REPLACE) { 297992234c8fSToke Høiland-Jørgensen if (!xdp[IFLA_XDP_EXPECTED_FD]) { 298092234c8fSToke Høiland-Jørgensen err = -EINVAL; 298192234c8fSToke Høiland-Jørgensen goto errout; 298292234c8fSToke Høiland-Jørgensen } 298392234c8fSToke Høiland-Jørgensen expected_fd = 298492234c8fSToke Høiland-Jørgensen nla_get_s32(xdp[IFLA_XDP_EXPECTED_FD]); 298592234c8fSToke Høiland-Jørgensen } 298692234c8fSToke Høiland-Jørgensen 2987ddf9f970SJakub Kicinski err = dev_change_xdp_fd(dev, extack, 298885de8576SDaniel Borkmann nla_get_s32(xdp[IFLA_XDP_FD]), 298992234c8fSToke Høiland-Jørgensen expected_fd, 299085de8576SDaniel Borkmann xdp_flags); 2991d1fdd913SBrenden Blanco if (err) 2992d1fdd913SBrenden Blanco goto errout; 2993d1fdd913SBrenden Blanco status |= DO_SETLINK_NOTIFY; 2994d1fdd913SBrenden Blanco } 2995d1fdd913SBrenden Blanco } 2996d1fdd913SBrenden Blanco 29970157f60cSPatrick McHardy errout: 2998ba998906SNicolas Dichtel if (status & DO_SETLINK_MODIFIED) { 299964ff90ccSXin Long if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY) 3000ba998906SNicolas Dichtel netdev_state_change(dev); 3001ba998906SNicolas Dichtel 3002ba998906SNicolas Dichtel if (err < 0) 3003e87cc472SJoe 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", 3004e87cc472SJoe Perches dev->name); 3005ba998906SNicolas Dichtel } 30060157f60cSPatrick McHardy 30070157f60cSPatrick McHardy return err; 30080157f60cSPatrick McHardy } 30090157f60cSPatrick McHardy 3010cc6090e9SJiri Pirko static struct net_device *rtnl_dev_get(struct net *net, 3011cc6090e9SJiri Pirko struct nlattr *ifname_attr, 301276c9ac0eSJiri Pirko struct nlattr *altifname_attr, 3013cc6090e9SJiri Pirko char *ifname) 3014cc6090e9SJiri Pirko { 301576c9ac0eSJiri Pirko char buffer[ALTIFNAMSIZ]; 3016cc6090e9SJiri Pirko 3017cc6090e9SJiri Pirko if (!ifname) { 3018cc6090e9SJiri Pirko ifname = buffer; 3019cc6090e9SJiri Pirko if (ifname_attr) 3020872f6903SFrancis Laniel nla_strscpy(ifname, ifname_attr, IFNAMSIZ); 302176c9ac0eSJiri Pirko else if (altifname_attr) 3022872f6903SFrancis Laniel nla_strscpy(ifname, altifname_attr, ALTIFNAMSIZ); 3023cc6090e9SJiri Pirko else 3024cc6090e9SJiri Pirko return NULL; 3025cc6090e9SJiri Pirko } 3026cc6090e9SJiri Pirko 3027cc6090e9SJiri Pirko return __dev_get_by_name(net, ifname); 3028cc6090e9SJiri Pirko } 3029cc6090e9SJiri Pirko 3030c21ef3e3SDavid Ahern static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, 3031c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 3032da5e0494SThomas Graf { 30333b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 3034da5e0494SThomas Graf struct ifinfomsg *ifm; 3035da5e0494SThomas Graf struct net_device *dev; 30360157f60cSPatrick McHardy int err; 3037da5e0494SThomas Graf struct nlattr *tb[IFLA_MAX+1]; 30381da177e4SLinus Torvalds char ifname[IFNAMSIZ]; 30391da177e4SLinus Torvalds 30408cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX, 30418cb08174SJohannes Berg ifla_policy, extack); 3042da5e0494SThomas Graf if (err < 0) 3043da5e0494SThomas Graf goto errout; 30441da177e4SLinus Torvalds 30454ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, false); 30464ff66caeSChristian Brauner if (err < 0) 30474ff66caeSChristian Brauner goto errout; 30484ff66caeSChristian Brauner 30495176f91eSThomas Graf if (tb[IFLA_IFNAME]) 3050872f6903SFrancis Laniel nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 305178e5b891SPatrick McHardy else 305278e5b891SPatrick McHardy ifname[0] = '\0'; 30531da177e4SLinus Torvalds 30541da177e4SLinus Torvalds err = -EINVAL; 3055da5e0494SThomas Graf ifm = nlmsg_data(nlh); 305651055be8SPatrick McHardy if (ifm->ifi_index > 0) 3057a3d12891SEric Dumazet dev = __dev_get_by_index(net, ifm->ifi_index); 305876c9ac0eSJiri Pirko else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) 305976c9ac0eSJiri Pirko dev = rtnl_dev_get(net, NULL, tb[IFLA_ALT_IFNAME], ifname); 3060da5e0494SThomas Graf else 3061da5e0494SThomas Graf goto errout; 30621da177e4SLinus Torvalds 3063da5e0494SThomas Graf if (dev == NULL) { 3064da5e0494SThomas Graf err = -ENODEV; 3065da5e0494SThomas Graf goto errout; 3066da5e0494SThomas Graf } 30671da177e4SLinus Torvalds 3068ddf9f970SJakub Kicinski err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0); 3069da5e0494SThomas Graf errout: 30701da177e4SLinus Torvalds return err; 30711da177e4SLinus Torvalds } 30721da177e4SLinus Torvalds 307366400d54SWANG Cong static int rtnl_group_dellink(const struct net *net, int group) 307466400d54SWANG Cong { 307566400d54SWANG Cong struct net_device *dev, *aux; 307666400d54SWANG Cong LIST_HEAD(list_kill); 307766400d54SWANG Cong bool found = false; 307866400d54SWANG Cong 307966400d54SWANG Cong if (!group) 308066400d54SWANG Cong return -EPERM; 308166400d54SWANG Cong 308266400d54SWANG Cong for_each_netdev(net, dev) { 308366400d54SWANG Cong if (dev->group == group) { 308466400d54SWANG Cong const struct rtnl_link_ops *ops; 308566400d54SWANG Cong 308666400d54SWANG Cong found = true; 308766400d54SWANG Cong ops = dev->rtnl_link_ops; 308866400d54SWANG Cong if (!ops || !ops->dellink) 308966400d54SWANG Cong return -EOPNOTSUPP; 309066400d54SWANG Cong } 309166400d54SWANG Cong } 309266400d54SWANG Cong 309366400d54SWANG Cong if (!found) 309466400d54SWANG Cong return -ENODEV; 309566400d54SWANG Cong 309666400d54SWANG Cong for_each_netdev_safe(net, dev, aux) { 309766400d54SWANG Cong if (dev->group == group) { 309866400d54SWANG Cong const struct rtnl_link_ops *ops; 309966400d54SWANG Cong 310066400d54SWANG Cong ops = dev->rtnl_link_ops; 310166400d54SWANG Cong ops->dellink(dev, &list_kill); 310266400d54SWANG Cong } 310366400d54SWANG Cong } 310466400d54SWANG Cong unregister_netdevice_many(&list_kill); 310566400d54SWANG Cong 310666400d54SWANG Cong return 0; 310766400d54SWANG Cong } 310866400d54SWANG Cong 3109614732eaSThomas Graf int rtnl_delete_link(struct net_device *dev) 3110614732eaSThomas Graf { 3111614732eaSThomas Graf const struct rtnl_link_ops *ops; 3112614732eaSThomas Graf LIST_HEAD(list_kill); 3113614732eaSThomas Graf 3114614732eaSThomas Graf ops = dev->rtnl_link_ops; 3115614732eaSThomas Graf if (!ops || !ops->dellink) 3116614732eaSThomas Graf return -EOPNOTSUPP; 3117614732eaSThomas Graf 3118614732eaSThomas Graf ops->dellink(dev, &list_kill); 3119614732eaSThomas Graf unregister_netdevice_many(&list_kill); 3120614732eaSThomas Graf 3121614732eaSThomas Graf return 0; 3122614732eaSThomas Graf } 3123614732eaSThomas Graf EXPORT_SYMBOL_GPL(rtnl_delete_link); 3124614732eaSThomas Graf 3125c21ef3e3SDavid Ahern static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, 3126c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 312738f7b870SPatrick McHardy { 31283b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 3129b61ad68aSChristian Brauner struct net *tgt_net = net; 3130b61ad68aSChristian Brauner struct net_device *dev = NULL; 313138f7b870SPatrick McHardy struct ifinfomsg *ifm; 313238f7b870SPatrick McHardy struct nlattr *tb[IFLA_MAX+1]; 313338f7b870SPatrick McHardy int err; 3134b61ad68aSChristian Brauner int netnsid = -1; 313538f7b870SPatrick McHardy 31368cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX, 31378cb08174SJohannes Berg ifla_policy, extack); 313838f7b870SPatrick McHardy if (err < 0) 313938f7b870SPatrick McHardy return err; 314038f7b870SPatrick McHardy 31414ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, true); 31424ff66caeSChristian Brauner if (err < 0) 31434ff66caeSChristian Brauner return err; 31444ff66caeSChristian Brauner 31457e4a8d5aSChristian Brauner if (tb[IFLA_TARGET_NETNSID]) { 31467e4a8d5aSChristian Brauner netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]); 3147c383edc4SChristian Brauner tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid); 3148b61ad68aSChristian Brauner if (IS_ERR(tgt_net)) 3149b61ad68aSChristian Brauner return PTR_ERR(tgt_net); 3150b61ad68aSChristian Brauner } 3151b61ad68aSChristian Brauner 3152b61ad68aSChristian Brauner err = -EINVAL; 315338f7b870SPatrick McHardy ifm = nlmsg_data(nlh); 315438f7b870SPatrick McHardy if (ifm->ifi_index > 0) 3155b61ad68aSChristian Brauner dev = __dev_get_by_index(tgt_net, ifm->ifi_index); 315676c9ac0eSJiri Pirko else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) 315776c9ac0eSJiri Pirko dev = rtnl_dev_get(net, tb[IFLA_IFNAME], 315876c9ac0eSJiri Pirko tb[IFLA_ALT_IFNAME], NULL); 315966400d54SWANG Cong else if (tb[IFLA_GROUP]) 3160b61ad68aSChristian Brauner err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP])); 316138f7b870SPatrick McHardy else 3162b61ad68aSChristian Brauner goto out; 316338f7b870SPatrick McHardy 3164b61ad68aSChristian Brauner if (!dev) { 3165b61ad68aSChristian Brauner if (tb[IFLA_IFNAME] || ifm->ifi_index > 0) 3166b61ad68aSChristian Brauner err = -ENODEV; 316738f7b870SPatrick McHardy 3168b61ad68aSChristian Brauner goto out; 3169b61ad68aSChristian Brauner } 3170b61ad68aSChristian Brauner 3171b61ad68aSChristian Brauner err = rtnl_delete_link(dev); 3172b61ad68aSChristian Brauner 3173b61ad68aSChristian Brauner out: 3174b61ad68aSChristian Brauner if (netnsid >= 0) 3175b61ad68aSChristian Brauner put_net(tgt_net); 3176b61ad68aSChristian Brauner 3177b61ad68aSChristian Brauner return err; 317838f7b870SPatrick McHardy } 317938f7b870SPatrick McHardy 31803729d502SPatrick McHardy int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm) 31813729d502SPatrick McHardy { 31823729d502SPatrick McHardy unsigned int old_flags; 31833729d502SPatrick McHardy int err; 31843729d502SPatrick McHardy 31853729d502SPatrick McHardy old_flags = dev->flags; 31863729d502SPatrick McHardy if (ifm && (ifm->ifi_flags || ifm->ifi_change)) { 31876d040321SPetr Machata err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm), 31886d040321SPetr Machata NULL); 31893729d502SPatrick McHardy if (err < 0) 31903729d502SPatrick McHardy return err; 31913729d502SPatrick McHardy } 31923729d502SPatrick McHardy 31938d356b89SRoopa Prabhu if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) { 319456a49d70SRoopa Prabhu __dev_notify_flags(dev, old_flags, (old_flags ^ dev->flags)); 31958d356b89SRoopa Prabhu } else { 31963729d502SPatrick McHardy dev->rtnl_link_state = RTNL_LINK_INITIALIZED; 3197a528c219SNicolas Dichtel __dev_notify_flags(dev, old_flags, ~0U); 31988d356b89SRoopa Prabhu } 31993729d502SPatrick McHardy return 0; 32003729d502SPatrick McHardy } 32013729d502SPatrick McHardy EXPORT_SYMBOL(rtnl_configure_link); 32023729d502SPatrick McHardy 3203d0522f1cSDavid Ahern struct net_device *rtnl_create_link(struct net *net, const char *ifname, 3204d0522f1cSDavid Ahern unsigned char name_assign_type, 3205d0522f1cSDavid Ahern const struct rtnl_link_ops *ops, 3206d0522f1cSDavid Ahern struct nlattr *tb[], 3207d0522f1cSDavid Ahern struct netlink_ext_ack *extack) 3208e7199288SPavel Emelianov { 3209e7199288SPavel Emelianov struct net_device *dev; 3210d40156aaSJiri Pirko unsigned int num_tx_queues = 1; 3211d40156aaSJiri Pirko unsigned int num_rx_queues = 1; 3212e7199288SPavel Emelianov 321376ff5cc9SJiri Pirko if (tb[IFLA_NUM_TX_QUEUES]) 321476ff5cc9SJiri Pirko num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]); 321576ff5cc9SJiri Pirko else if (ops->get_num_tx_queues) 3216d40156aaSJiri Pirko num_tx_queues = ops->get_num_tx_queues(); 321776ff5cc9SJiri Pirko 321876ff5cc9SJiri Pirko if (tb[IFLA_NUM_RX_QUEUES]) 321976ff5cc9SJiri Pirko num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]); 322076ff5cc9SJiri Pirko else if (ops->get_num_rx_queues) 3221d40156aaSJiri Pirko num_rx_queues = ops->get_num_rx_queues(); 3222efacb309Sstephen hemminger 3223d0522f1cSDavid Ahern if (num_tx_queues < 1 || num_tx_queues > 4096) { 3224d0522f1cSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid number of transmit queues"); 32250e1d6ecaSEric Dumazet return ERR_PTR(-EINVAL); 3226d0522f1cSDavid Ahern } 32270e1d6ecaSEric Dumazet 3228d0522f1cSDavid Ahern if (num_rx_queues < 1 || num_rx_queues > 4096) { 3229d0522f1cSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid number of receive queues"); 32300e1d6ecaSEric Dumazet return ERR_PTR(-EINVAL); 3231d0522f1cSDavid Ahern } 32320e1d6ecaSEric Dumazet 32338c713dc9SJohannes Berg if (ops->alloc) { 32348c713dc9SJohannes Berg dev = ops->alloc(tb, ifname, name_assign_type, 32358c713dc9SJohannes Berg num_tx_queues, num_rx_queues); 32368c713dc9SJohannes Berg if (IS_ERR(dev)) 32378c713dc9SJohannes Berg return dev; 32388c713dc9SJohannes Berg } else { 32398c713dc9SJohannes Berg dev = alloc_netdev_mqs(ops->priv_size, ifname, 32408c713dc9SJohannes Berg name_assign_type, ops->setup, 32418c713dc9SJohannes Berg num_tx_queues, num_rx_queues); 32428c713dc9SJohannes Berg } 32438c713dc9SJohannes Berg 3244e7199288SPavel Emelianov if (!dev) 3245d1892e4eSTobias Klauser return ERR_PTR(-ENOMEM); 3246e7199288SPavel Emelianov 324781adee47SEric W. Biederman dev_net_set(dev, net); 324881adee47SEric W. Biederman dev->rtnl_link_ops = ops; 32493729d502SPatrick McHardy dev->rtnl_link_state = RTNL_LINK_INITIALIZING; 325081adee47SEric W. Biederman 3251d836f5c6SEric Dumazet if (tb[IFLA_MTU]) { 3252d836f5c6SEric Dumazet u32 mtu = nla_get_u32(tb[IFLA_MTU]); 3253d836f5c6SEric Dumazet int err; 3254d836f5c6SEric Dumazet 3255d836f5c6SEric Dumazet err = dev_validate_mtu(dev, mtu, extack); 3256d836f5c6SEric Dumazet if (err) { 3257d836f5c6SEric Dumazet free_netdev(dev); 3258d836f5c6SEric Dumazet return ERR_PTR(err); 3259d836f5c6SEric Dumazet } 3260d836f5c6SEric Dumazet dev->mtu = mtu; 3261d836f5c6SEric Dumazet } 32622afb9b53SJiri Pirko if (tb[IFLA_ADDRESS]) { 3263efd38f75SJakub Kicinski __dev_addr_set(dev, nla_data(tb[IFLA_ADDRESS]), 3264e7199288SPavel Emelianov nla_len(tb[IFLA_ADDRESS])); 32652afb9b53SJiri Pirko dev->addr_assign_type = NET_ADDR_SET; 32662afb9b53SJiri Pirko } 3267e7199288SPavel Emelianov if (tb[IFLA_BROADCAST]) 3268e7199288SPavel Emelianov memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]), 3269e7199288SPavel Emelianov nla_len(tb[IFLA_BROADCAST])); 3270e7199288SPavel Emelianov if (tb[IFLA_TXQLEN]) 3271e7199288SPavel Emelianov dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); 3272e7199288SPavel Emelianov if (tb[IFLA_OPERSTATE]) 327393b2d4a2SDavid S. Miller set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 3274e7199288SPavel Emelianov if (tb[IFLA_LINKMODE]) 3275e7199288SPavel Emelianov dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); 3276ffa934f1SPatrick McHardy if (tb[IFLA_GROUP]) 3277ffa934f1SPatrick McHardy dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); 327846e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SIZE]) 327946e6b992SStephen Hemminger netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE])); 328046e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SEGS]) 32816d872df3SEric Dumazet netif_set_gso_max_segs(dev, nla_get_u32(tb[IFLA_GSO_MAX_SEGS])); 3282eac1b93cSCoco Li if (tb[IFLA_GRO_MAX_SIZE]) 3283eac1b93cSCoco Li netif_set_gro_max_size(dev, nla_get_u32(tb[IFLA_GRO_MAX_SIZE])); 3284e7199288SPavel Emelianov 3285e7199288SPavel Emelianov return dev; 3286e7199288SPavel Emelianov } 3287e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_create_link); 3288e7199288SPavel Emelianov 328990f62cf3SEric W. Biederman static int rtnl_group_changelink(const struct sk_buff *skb, 329090f62cf3SEric W. Biederman struct net *net, int group, 3291e7ed828fSVlad Dogaru struct ifinfomsg *ifm, 3292ddf9f970SJakub Kicinski struct netlink_ext_ack *extack, 3293e7ed828fSVlad Dogaru struct nlattr **tb) 3294e7ed828fSVlad Dogaru { 3295d079535dSWANG Cong struct net_device *dev, *aux; 3296e7ed828fSVlad Dogaru int err; 3297e7ed828fSVlad Dogaru 3298d079535dSWANG Cong for_each_netdev_safe(net, dev, aux) { 3299e7ed828fSVlad Dogaru if (dev->group == group) { 3300ddf9f970SJakub Kicinski err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0); 3301e7ed828fSVlad Dogaru if (err < 0) 3302e7ed828fSVlad Dogaru return err; 3303e7ed828fSVlad Dogaru } 3304e7ed828fSVlad Dogaru } 3305e7ed828fSVlad Dogaru 3306e7ed828fSVlad Dogaru return 0; 3307e7ed828fSVlad Dogaru } 3308e7ed828fSVlad Dogaru 3309a2939745SJakub Kicinski static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, 3310a2939745SJakub Kicinski struct nlattr **attr, struct netlink_ext_ack *extack) 331138f7b870SPatrick McHardy { 3312420d0318SJakub Kicinski struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1]; 3313420d0318SJakub Kicinski unsigned char name_assign_type = NET_NAME_USER; 3314420d0318SJakub Kicinski struct nlattr *linkinfo[IFLA_INFO_MAX + 1]; 3315c6f6f244SEric Dumazet const struct rtnl_link_ops *m_ops; 3316c6f6f244SEric Dumazet struct net_device *master_dev; 33173b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 331838f7b870SPatrick McHardy const struct rtnl_link_ops *ops; 331938f7b870SPatrick McHardy struct nlattr *tb[IFLA_MAX + 1]; 3320420d0318SJakub Kicinski struct net *dest_net, *link_net; 3321420d0318SJakub Kicinski struct nlattr **slave_data; 3322420d0318SJakub Kicinski char kind[MODULE_NAME_LEN]; 3323420d0318SJakub Kicinski struct net_device *dev; 3324420d0318SJakub Kicinski struct ifinfomsg *ifm; 3325420d0318SJakub Kicinski char ifname[IFNAMSIZ]; 3326420d0318SJakub Kicinski struct nlattr **data; 332738f7b870SPatrick McHardy int err; 332838f7b870SPatrick McHardy 332995a5afcaSJohannes Berg #ifdef CONFIG_MODULES 333038f7b870SPatrick McHardy replay: 33318072f085SThomas Graf #endif 33328cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX, 33338cb08174SJohannes Berg ifla_policy, extack); 333438f7b870SPatrick McHardy if (err < 0) 333538f7b870SPatrick McHardy return err; 333638f7b870SPatrick McHardy 33374ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, false); 33384ff66caeSChristian Brauner if (err < 0) 33394ff66caeSChristian Brauner return err; 33404ff66caeSChristian Brauner 334138f7b870SPatrick McHardy if (tb[IFLA_IFNAME]) 3342872f6903SFrancis Laniel nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 334338f7b870SPatrick McHardy else 334438f7b870SPatrick McHardy ifname[0] = '\0'; 334538f7b870SPatrick McHardy 334638f7b870SPatrick McHardy ifm = nlmsg_data(nlh); 334738f7b870SPatrick McHardy if (ifm->ifi_index > 0) 3348881d966bSEric W. Biederman dev = __dev_get_by_index(net, ifm->ifi_index); 334976c9ac0eSJiri Pirko else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) 335076c9ac0eSJiri Pirko dev = rtnl_dev_get(net, NULL, tb[IFLA_ALT_IFNAME], ifname); 335138f7b870SPatrick McHardy else 335238f7b870SPatrick McHardy dev = NULL; 335338f7b870SPatrick McHardy 3354c6f6f244SEric Dumazet master_dev = NULL; 3355c6f6f244SEric Dumazet m_ops = NULL; 3356ba7d49b1SJiri Pirko if (dev) { 3357ba7d49b1SJiri Pirko master_dev = netdev_master_upper_dev_get(dev); 3358ba7d49b1SJiri Pirko if (master_dev) 3359ba7d49b1SJiri Pirko m_ops = master_dev->rtnl_link_ops; 3360ba7d49b1SJiri Pirko } 3361ba7d49b1SJiri Pirko 33628679c31eSRocco Yue err = validate_linkmsg(dev, tb, extack); 3363e0d087afSEric Dumazet if (err < 0) 33641840bb13SThomas Graf return err; 33651840bb13SThomas Graf 336638f7b870SPatrick McHardy if (tb[IFLA_LINKINFO]) { 33678cb08174SJohannes Berg err = nla_parse_nested_deprecated(linkinfo, IFLA_INFO_MAX, 33688cb08174SJohannes Berg tb[IFLA_LINKINFO], 33698cb08174SJohannes Berg ifla_info_policy, NULL); 337038f7b870SPatrick McHardy if (err < 0) 337138f7b870SPatrick McHardy return err; 337238f7b870SPatrick McHardy } else 337338f7b870SPatrick McHardy memset(linkinfo, 0, sizeof(linkinfo)); 337438f7b870SPatrick McHardy 337538f7b870SPatrick McHardy if (linkinfo[IFLA_INFO_KIND]) { 3376872f6903SFrancis Laniel nla_strscpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind)); 337738f7b870SPatrick McHardy ops = rtnl_link_ops_get(kind); 337838f7b870SPatrick McHardy } else { 337938f7b870SPatrick McHardy kind[0] = '\0'; 338038f7b870SPatrick McHardy ops = NULL; 338138f7b870SPatrick McHardy } 338238f7b870SPatrick McHardy 3383420d0318SJakub Kicinski data = NULL; 338438f7b870SPatrick McHardy if (ops) { 3385ccf8dbcdSKees Cook if (ops->maxtype > RTNL_MAX_TYPE) 3386ccf8dbcdSKees Cook return -EINVAL; 3387ccf8dbcdSKees Cook 338838f7b870SPatrick McHardy if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) { 33898cb08174SJohannes Berg err = nla_parse_nested_deprecated(attr, ops->maxtype, 339038f7b870SPatrick McHardy linkinfo[IFLA_INFO_DATA], 339168d57f3bSDavid Ahern ops->policy, extack); 339238f7b870SPatrick McHardy if (err < 0) 339338f7b870SPatrick McHardy return err; 339438f7b870SPatrick McHardy data = attr; 339538f7b870SPatrick McHardy } 339638f7b870SPatrick McHardy if (ops->validate) { 3397a8b8a889SMatthias Schiffer err = ops->validate(tb, data, extack); 339838f7b870SPatrick McHardy if (err < 0) 339938f7b870SPatrick McHardy return err; 340038f7b870SPatrick McHardy } 340138f7b870SPatrick McHardy } 340238f7b870SPatrick McHardy 3403420d0318SJakub Kicinski slave_data = NULL; 3404ba7d49b1SJiri Pirko if (m_ops) { 3405af066ed3SPrashant Bhole if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE) 3406ccf8dbcdSKees Cook return -EINVAL; 3407ccf8dbcdSKees Cook 3408ba7d49b1SJiri Pirko if (m_ops->slave_maxtype && 3409ba7d49b1SJiri Pirko linkinfo[IFLA_INFO_SLAVE_DATA]) { 34108cb08174SJohannes Berg err = nla_parse_nested_deprecated(slave_attr, 34118cb08174SJohannes Berg m_ops->slave_maxtype, 3412ba7d49b1SJiri Pirko linkinfo[IFLA_INFO_SLAVE_DATA], 34138cb08174SJohannes Berg m_ops->slave_policy, 34148cb08174SJohannes Berg extack); 3415ba7d49b1SJiri Pirko if (err < 0) 3416ba7d49b1SJiri Pirko return err; 3417ba7d49b1SJiri Pirko slave_data = slave_attr; 3418ba7d49b1SJiri Pirko } 3419ba7d49b1SJiri Pirko } 3420ba7d49b1SJiri Pirko 342138f7b870SPatrick McHardy if (dev) { 342290c325e3SNicolas Dichtel int status = 0; 342338f7b870SPatrick McHardy 342438f7b870SPatrick McHardy if (nlh->nlmsg_flags & NLM_F_EXCL) 342538f7b870SPatrick McHardy return -EEXIST; 342638f7b870SPatrick McHardy if (nlh->nlmsg_flags & NLM_F_REPLACE) 342738f7b870SPatrick McHardy return -EOPNOTSUPP; 342838f7b870SPatrick McHardy 342938f7b870SPatrick McHardy if (linkinfo[IFLA_INFO_DATA]) { 343038f7b870SPatrick McHardy if (!ops || ops != dev->rtnl_link_ops || 343138f7b870SPatrick McHardy !ops->changelink) 343238f7b870SPatrick McHardy return -EOPNOTSUPP; 343338f7b870SPatrick McHardy 3434ad744b22SMatthias Schiffer err = ops->changelink(dev, tb, data, extack); 343538f7b870SPatrick McHardy if (err < 0) 343638f7b870SPatrick McHardy return err; 3437ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 343838f7b870SPatrick McHardy } 343938f7b870SPatrick McHardy 3440ba7d49b1SJiri Pirko if (linkinfo[IFLA_INFO_SLAVE_DATA]) { 3441ba7d49b1SJiri Pirko if (!m_ops || !m_ops->slave_changelink) 3442ba7d49b1SJiri Pirko return -EOPNOTSUPP; 3443ba7d49b1SJiri Pirko 3444420d0318SJakub Kicinski err = m_ops->slave_changelink(master_dev, dev, tb, 3445420d0318SJakub Kicinski slave_data, extack); 3446ba7d49b1SJiri Pirko if (err < 0) 3447ba7d49b1SJiri Pirko return err; 3448ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 3449ba7d49b1SJiri Pirko } 3450ba7d49b1SJiri Pirko 3451420d0318SJakub Kicinski return do_setlink(skb, dev, ifm, extack, tb, ifname, status); 345238f7b870SPatrick McHardy } 345338f7b870SPatrick McHardy 3454ffa934f1SPatrick McHardy if (!(nlh->nlmsg_flags & NLM_F_CREATE)) { 3455ffa934f1SPatrick McHardy if (ifm->ifi_index == 0 && tb[IFLA_GROUP]) 345690f62cf3SEric W. Biederman return rtnl_group_changelink(skb, net, 3457ffa934f1SPatrick McHardy nla_get_u32(tb[IFLA_GROUP]), 3458ddf9f970SJakub Kicinski ifm, extack, tb); 345938f7b870SPatrick McHardy return -ENODEV; 3460ffa934f1SPatrick McHardy } 346138f7b870SPatrick McHardy 3462160ca014STheuns Verwoerd if (tb[IFLA_MAP] || tb[IFLA_PROTINFO]) 346338f7b870SPatrick McHardy return -EOPNOTSUPP; 346438f7b870SPatrick McHardy 346538f7b870SPatrick McHardy if (!ops) { 346695a5afcaSJohannes Berg #ifdef CONFIG_MODULES 346738f7b870SPatrick McHardy if (kind[0]) { 346838f7b870SPatrick McHardy __rtnl_unlock(); 346938f7b870SPatrick McHardy request_module("rtnl-link-%s", kind); 347038f7b870SPatrick McHardy rtnl_lock(); 347138f7b870SPatrick McHardy ops = rtnl_link_ops_get(kind); 347238f7b870SPatrick McHardy if (ops) 347338f7b870SPatrick McHardy goto replay; 347438f7b870SPatrick McHardy } 347538f7b870SPatrick McHardy #endif 347668d57f3bSDavid Ahern NL_SET_ERR_MSG(extack, "Unknown device type"); 347738f7b870SPatrick McHardy return -EOPNOTSUPP; 347838f7b870SPatrick McHardy } 347938f7b870SPatrick McHardy 34808c713dc9SJohannes Berg if (!ops->alloc && !ops->setup) 3481b0ab2fabSJiri Pirko return -EOPNOTSUPP; 3482b0ab2fabSJiri Pirko 34835517750fSTom Gundersen if (!ifname[0]) { 348438f7b870SPatrick McHardy snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind); 34855517750fSTom Gundersen name_assign_type = NET_NAME_ENUM; 34865517750fSTom Gundersen } 348738f7b870SPatrick McHardy 34885bb8ed07SChristian Brauner dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN); 348913ad1774SEric W. Biederman if (IS_ERR(dest_net)) 349013ad1774SEric W. Biederman return PTR_ERR(dest_net); 349113ad1774SEric W. Biederman 3492317f4810SNicolas Dichtel if (tb[IFLA_LINK_NETNSID]) { 3493317f4810SNicolas Dichtel int id = nla_get_s32(tb[IFLA_LINK_NETNSID]); 3494317f4810SNicolas Dichtel 3495317f4810SNicolas Dichtel link_net = get_net_ns_by_id(dest_net, id); 3496317f4810SNicolas Dichtel if (!link_net) { 349768d57f3bSDavid Ahern NL_SET_ERR_MSG(extack, "Unknown network namespace id"); 3498317f4810SNicolas Dichtel err = -EINVAL; 3499317f4810SNicolas Dichtel goto out; 3500317f4810SNicolas Dichtel } 350106615bedSEric W. Biederman err = -EPERM; 350206615bedSEric W. Biederman if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN)) 350306615bedSEric W. Biederman goto out; 3504420d0318SJakub Kicinski } else { 3505420d0318SJakub Kicinski link_net = NULL; 3506317f4810SNicolas Dichtel } 3507317f4810SNicolas Dichtel 3508317f4810SNicolas Dichtel dev = rtnl_create_link(link_net ? : dest_net, ifname, 3509d0522f1cSDavid Ahern name_assign_type, ops, tb, extack); 35109c7dafbfSPavel Emelyanov if (IS_ERR(dev)) { 3511e7199288SPavel Emelianov err = PTR_ERR(dev); 35129c7dafbfSPavel Emelyanov goto out; 35139c7dafbfSPavel Emelyanov } 35149c7dafbfSPavel Emelyanov 35159c7dafbfSPavel Emelyanov dev->ifindex = ifm->ifi_index; 35169c7dafbfSPavel Emelyanov 3517c269a24cSJakub Kicinski if (ops->newlink) 3518420d0318SJakub Kicinski err = ops->newlink(link_net ? : net, dev, tb, data, extack); 3519c269a24cSJakub Kicinski else 35202d85cba2SPatrick McHardy err = register_netdevice(dev); 3521fce9b9beSDan Carpenter if (err < 0) { 352238f7b870SPatrick McHardy free_netdev(dev); 35233729d502SPatrick McHardy goto out; 3524fce9b9beSDan Carpenter } 3525c269a24cSJakub Kicinski 35263729d502SPatrick McHardy err = rtnl_configure_link(dev, ifm); 352743638900SDavid S. Miller if (err < 0) 352843638900SDavid S. Miller goto out_unregister; 352943638900SDavid S. Miller if (link_net) { 35300854fa82SAndrei Vagin err = dev_change_net_namespace(dev, dest_net, ifname); 353143638900SDavid S. Miller if (err < 0) 353243638900SDavid S. Miller goto out_unregister; 353343638900SDavid S. Miller } 3534160ca014STheuns Verwoerd if (tb[IFLA_MASTER]) { 3535420d0318SJakub Kicinski err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack); 3536160ca014STheuns Verwoerd if (err) 3537160ca014STheuns Verwoerd goto out_unregister; 3538160ca014STheuns Verwoerd } 353943638900SDavid S. Miller out: 354043638900SDavid S. Miller if (link_net) 354143638900SDavid S. Miller put_net(link_net); 354243638900SDavid S. Miller put_net(dest_net); 354343638900SDavid S. Miller return err; 354443638900SDavid S. Miller out_unregister: 35457afb8886SWANG Cong if (ops->newlink) { 35467afb8886SWANG Cong LIST_HEAD(list_kill); 35477afb8886SWANG Cong 35487afb8886SWANG Cong ops->dellink(dev, &list_kill); 35497afb8886SWANG Cong unregister_netdevice_many(&list_kill); 35507afb8886SWANG Cong } else { 35513729d502SPatrick McHardy unregister_netdevice(dev); 35527afb8886SWANG Cong } 3553317f4810SNicolas Dichtel goto out; 3554317f4810SNicolas Dichtel } 355538f7b870SPatrick McHardy 3556a2939745SJakub Kicinski static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, 3557a2939745SJakub Kicinski struct netlink_ext_ack *extack) 3558a2939745SJakub Kicinski { 3559a2939745SJakub Kicinski struct nlattr **attr; 3560a2939745SJakub Kicinski int ret; 3561a2939745SJakub Kicinski 3562a2939745SJakub Kicinski attr = kmalloc_array(RTNL_MAX_TYPE + 1, sizeof(*attr), GFP_KERNEL); 3563a2939745SJakub Kicinski if (!attr) 3564a2939745SJakub Kicinski return -ENOMEM; 3565a2939745SJakub Kicinski 3566a2939745SJakub Kicinski ret = __rtnl_newlink(skb, nlh, attr, extack); 3567a2939745SJakub Kicinski kfree(attr); 3568a2939745SJakub Kicinski return ret; 3569a2939745SJakub Kicinski } 3570a2939745SJakub Kicinski 35719b3757b0SJakub Kicinski static int rtnl_valid_getlink_req(struct sk_buff *skb, 35729b3757b0SJakub Kicinski const struct nlmsghdr *nlh, 35739b3757b0SJakub Kicinski struct nlattr **tb, 35749b3757b0SJakub Kicinski struct netlink_ext_ack *extack) 35759b3757b0SJakub Kicinski { 35769b3757b0SJakub Kicinski struct ifinfomsg *ifm; 35779b3757b0SJakub Kicinski int i, err; 35789b3757b0SJakub Kicinski 35799b3757b0SJakub Kicinski if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { 35809b3757b0SJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid header for get link"); 35819b3757b0SJakub Kicinski return -EINVAL; 35829b3757b0SJakub Kicinski } 35839b3757b0SJakub Kicinski 35849b3757b0SJakub Kicinski if (!netlink_strict_get_check(skb)) 35858cb08174SJohannes Berg return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX, 35868cb08174SJohannes Berg ifla_policy, extack); 35879b3757b0SJakub Kicinski 35889b3757b0SJakub Kicinski ifm = nlmsg_data(nlh); 35899b3757b0SJakub Kicinski if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags || 35909b3757b0SJakub Kicinski ifm->ifi_change) { 35919b3757b0SJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid values in header for get link request"); 35929b3757b0SJakub Kicinski return -EINVAL; 35939b3757b0SJakub Kicinski } 35949b3757b0SJakub Kicinski 35958cb08174SJohannes Berg err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFLA_MAX, 35968cb08174SJohannes Berg ifla_policy, extack); 35979b3757b0SJakub Kicinski if (err) 35989b3757b0SJakub Kicinski return err; 35999b3757b0SJakub Kicinski 36009b3757b0SJakub Kicinski for (i = 0; i <= IFLA_MAX; i++) { 36019b3757b0SJakub Kicinski if (!tb[i]) 36029b3757b0SJakub Kicinski continue; 36039b3757b0SJakub Kicinski 36049b3757b0SJakub Kicinski switch (i) { 36059b3757b0SJakub Kicinski case IFLA_IFNAME: 360676c9ac0eSJiri Pirko case IFLA_ALT_IFNAME: 36079b3757b0SJakub Kicinski case IFLA_EXT_MASK: 36089b3757b0SJakub Kicinski case IFLA_TARGET_NETNSID: 36099b3757b0SJakub Kicinski break; 36109b3757b0SJakub Kicinski default: 36119b3757b0SJakub Kicinski NL_SET_ERR_MSG(extack, "Unsupported attribute in get link request"); 36129b3757b0SJakub Kicinski return -EINVAL; 36139b3757b0SJakub Kicinski } 36149b3757b0SJakub Kicinski } 36159b3757b0SJakub Kicinski 36169b3757b0SJakub Kicinski return 0; 36179b3757b0SJakub Kicinski } 36189b3757b0SJakub Kicinski 3619c21ef3e3SDavid Ahern static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh, 3620c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 3621711e2c33SJean Tourrilhes { 36223b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 362379e1ad14SJiri Benc struct net *tgt_net = net; 3624b60c5115SThomas Graf struct ifinfomsg *ifm; 3625b60c5115SThomas Graf struct nlattr *tb[IFLA_MAX+1]; 3626b60c5115SThomas Graf struct net_device *dev = NULL; 3627b60c5115SThomas Graf struct sk_buff *nskb; 362879e1ad14SJiri Benc int netnsid = -1; 3629339bf98fSThomas Graf int err; 3630115c9b81SGreg Rose u32 ext_filter_mask = 0; 3631711e2c33SJean Tourrilhes 36329b3757b0SJakub Kicinski err = rtnl_valid_getlink_req(skb, nlh, tb, extack); 3633b60c5115SThomas Graf if (err < 0) 36349918f230SEric Sesterhenn return err; 3635b60c5115SThomas Graf 36364ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, true); 36374ff66caeSChristian Brauner if (err < 0) 36384ff66caeSChristian Brauner return err; 36394ff66caeSChristian Brauner 36407e4a8d5aSChristian Brauner if (tb[IFLA_TARGET_NETNSID]) { 36417e4a8d5aSChristian Brauner netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]); 3642c383edc4SChristian Brauner tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid); 364379e1ad14SJiri Benc if (IS_ERR(tgt_net)) 364479e1ad14SJiri Benc return PTR_ERR(tgt_net); 364579e1ad14SJiri Benc } 364679e1ad14SJiri Benc 3647115c9b81SGreg Rose if (tb[IFLA_EXT_MASK]) 3648115c9b81SGreg Rose ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 3649115c9b81SGreg Rose 365079e1ad14SJiri Benc err = -EINVAL; 3651b60c5115SThomas Graf ifm = nlmsg_data(nlh); 3652a3d12891SEric Dumazet if (ifm->ifi_index > 0) 365379e1ad14SJiri Benc dev = __dev_get_by_index(tgt_net, ifm->ifi_index); 365476c9ac0eSJiri Pirko else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) 365576c9ac0eSJiri Pirko dev = rtnl_dev_get(tgt_net, tb[IFLA_IFNAME], 365676c9ac0eSJiri Pirko tb[IFLA_ALT_IFNAME], NULL); 3657a3d12891SEric Dumazet else 365879e1ad14SJiri Benc goto out; 3659b60c5115SThomas Graf 366079e1ad14SJiri Benc err = -ENODEV; 3661a3d12891SEric Dumazet if (dev == NULL) 366279e1ad14SJiri Benc goto out; 3663a3d12891SEric Dumazet 366479e1ad14SJiri Benc err = -ENOBUFS; 3665115c9b81SGreg Rose nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL); 3666a3d12891SEric Dumazet if (nskb == NULL) 366779e1ad14SJiri Benc goto out; 3668711e2c33SJean Tourrilhes 366979e1ad14SJiri Benc err = rtnl_fill_ifinfo(nskb, dev, net, 367079e1ad14SJiri Benc RTM_NEWLINK, NETLINK_CB(skb).portid, 367179e1ad14SJiri Benc nlh->nlmsg_seq, 0, 0, ext_filter_mask, 3672d4e4fdf9SGuillaume Nault 0, NULL, 0, netnsid, GFP_KERNEL); 367326932566SPatrick McHardy if (err < 0) { 367426932566SPatrick McHardy /* -EMSGSIZE implies BUG in if_nlmsg_size */ 367526932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 367626932566SPatrick McHardy kfree_skb(nskb); 3677a3d12891SEric Dumazet } else 367815e47304SEric W. Biederman err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); 367979e1ad14SJiri Benc out: 368079e1ad14SJiri Benc if (netnsid >= 0) 368179e1ad14SJiri Benc put_net(tgt_net); 3682b60c5115SThomas Graf 3683711e2c33SJean Tourrilhes return err; 3684711e2c33SJean Tourrilhes } 3685711e2c33SJean Tourrilhes 368636fbf1e5SJiri Pirko static int rtnl_alt_ifname(int cmd, struct net_device *dev, struct nlattr *attr, 368736fbf1e5SJiri Pirko bool *changed, struct netlink_ext_ack *extack) 368836fbf1e5SJiri Pirko { 368936fbf1e5SJiri Pirko char *alt_ifname; 3690155fb43bSJakub Kicinski size_t size; 369136fbf1e5SJiri Pirko int err; 369236fbf1e5SJiri Pirko 369336fbf1e5SJiri Pirko err = nla_validate(attr, attr->nla_len, IFLA_MAX, ifla_policy, extack); 369436fbf1e5SJiri Pirko if (err) 369536fbf1e5SJiri Pirko return err; 369636fbf1e5SJiri Pirko 3697155fb43bSJakub Kicinski if (cmd == RTM_NEWLINKPROP) { 3698155fb43bSJakub Kicinski size = rtnl_prop_list_size(dev); 3699155fb43bSJakub Kicinski size += nla_total_size(ALTIFNAMSIZ); 3700155fb43bSJakub Kicinski if (size >= U16_MAX) { 3701155fb43bSJakub Kicinski NL_SET_ERR_MSG(extack, 3702155fb43bSJakub Kicinski "effective property list too long"); 3703155fb43bSJakub Kicinski return -EINVAL; 3704155fb43bSJakub Kicinski } 3705155fb43bSJakub Kicinski } 3706155fb43bSJakub Kicinski 37075d26cff5SJakub Kicinski alt_ifname = nla_strdup(attr, GFP_KERNEL_ACCOUNT); 370836fbf1e5SJiri Pirko if (!alt_ifname) 370936fbf1e5SJiri Pirko return -ENOMEM; 371044bfa9c5SEric Dumazet 371144bfa9c5SEric Dumazet if (cmd == RTM_NEWLINKPROP) { 371236fbf1e5SJiri Pirko err = netdev_name_node_alt_create(dev, alt_ifname); 371344bfa9c5SEric Dumazet if (!err) 371444bfa9c5SEric Dumazet alt_ifname = NULL; 371536fbf1e5SJiri Pirko } else if (cmd == RTM_DELLINKPROP) { 371636fbf1e5SJiri Pirko err = netdev_name_node_alt_destroy(dev, alt_ifname); 371736fbf1e5SJiri Pirko } else { 371844bfa9c5SEric Dumazet WARN_ON_ONCE(1); 371944bfa9c5SEric Dumazet err = -EINVAL; 372036fbf1e5SJiri Pirko } 372136fbf1e5SJiri Pirko 372244bfa9c5SEric Dumazet kfree(alt_ifname); 372344bfa9c5SEric Dumazet if (!err) 372436fbf1e5SJiri Pirko *changed = true; 372544bfa9c5SEric Dumazet return err; 372636fbf1e5SJiri Pirko } 372736fbf1e5SJiri Pirko 372836fbf1e5SJiri Pirko static int rtnl_linkprop(int cmd, struct sk_buff *skb, struct nlmsghdr *nlh, 372936fbf1e5SJiri Pirko struct netlink_ext_ack *extack) 373036fbf1e5SJiri Pirko { 373136fbf1e5SJiri Pirko struct net *net = sock_net(skb->sk); 373236fbf1e5SJiri Pirko struct nlattr *tb[IFLA_MAX + 1]; 373336fbf1e5SJiri Pirko struct net_device *dev; 373436fbf1e5SJiri Pirko struct ifinfomsg *ifm; 373536fbf1e5SJiri Pirko bool changed = false; 373636fbf1e5SJiri Pirko struct nlattr *attr; 373736fbf1e5SJiri Pirko int err, rem; 373836fbf1e5SJiri Pirko 373936fbf1e5SJiri Pirko err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack); 374036fbf1e5SJiri Pirko if (err) 374136fbf1e5SJiri Pirko return err; 374236fbf1e5SJiri Pirko 374336fbf1e5SJiri Pirko err = rtnl_ensure_unique_netns(tb, extack, true); 374436fbf1e5SJiri Pirko if (err) 374536fbf1e5SJiri Pirko return err; 374636fbf1e5SJiri Pirko 374736fbf1e5SJiri Pirko ifm = nlmsg_data(nlh); 3748cc6090e9SJiri Pirko if (ifm->ifi_index > 0) 374936fbf1e5SJiri Pirko dev = __dev_get_by_index(net, ifm->ifi_index); 375076c9ac0eSJiri Pirko else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) 375176c9ac0eSJiri Pirko dev = rtnl_dev_get(net, tb[IFLA_IFNAME], 375276c9ac0eSJiri Pirko tb[IFLA_ALT_IFNAME], NULL); 3753cc6090e9SJiri Pirko else 375436fbf1e5SJiri Pirko return -EINVAL; 375536fbf1e5SJiri Pirko 375636fbf1e5SJiri Pirko if (!dev) 375736fbf1e5SJiri Pirko return -ENODEV; 375836fbf1e5SJiri Pirko 375936fbf1e5SJiri Pirko if (!tb[IFLA_PROP_LIST]) 376036fbf1e5SJiri Pirko return 0; 376136fbf1e5SJiri Pirko 376236fbf1e5SJiri Pirko nla_for_each_nested(attr, tb[IFLA_PROP_LIST], rem) { 376336fbf1e5SJiri Pirko switch (nla_type(attr)) { 376436fbf1e5SJiri Pirko case IFLA_ALT_IFNAME: 376536fbf1e5SJiri Pirko err = rtnl_alt_ifname(cmd, dev, attr, &changed, extack); 376636fbf1e5SJiri Pirko if (err) 376736fbf1e5SJiri Pirko return err; 376836fbf1e5SJiri Pirko break; 376936fbf1e5SJiri Pirko } 377036fbf1e5SJiri Pirko } 377136fbf1e5SJiri Pirko 377236fbf1e5SJiri Pirko if (changed) 377336fbf1e5SJiri Pirko netdev_state_change(dev); 377436fbf1e5SJiri Pirko return 0; 377536fbf1e5SJiri Pirko } 377636fbf1e5SJiri Pirko 377736fbf1e5SJiri Pirko static int rtnl_newlinkprop(struct sk_buff *skb, struct nlmsghdr *nlh, 377836fbf1e5SJiri Pirko struct netlink_ext_ack *extack) 377936fbf1e5SJiri Pirko { 378036fbf1e5SJiri Pirko return rtnl_linkprop(RTM_NEWLINKPROP, skb, nlh, extack); 378136fbf1e5SJiri Pirko } 378236fbf1e5SJiri Pirko 378336fbf1e5SJiri Pirko static int rtnl_dellinkprop(struct sk_buff *skb, struct nlmsghdr *nlh, 378436fbf1e5SJiri Pirko struct netlink_ext_ack *extack) 378536fbf1e5SJiri Pirko { 378636fbf1e5SJiri Pirko return rtnl_linkprop(RTM_DELLINKPROP, skb, nlh, extack); 378736fbf1e5SJiri Pirko } 378836fbf1e5SJiri Pirko 3789ebfe3c51SDi Zhu static u32 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh) 3790c7ac8679SGreg Rose { 3791115c9b81SGreg Rose struct net *net = sock_net(skb->sk); 3792ebfe3c51SDi Zhu size_t min_ifinfo_dump_size = 0; 3793115c9b81SGreg Rose struct nlattr *tb[IFLA_MAX+1]; 3794115c9b81SGreg Rose u32 ext_filter_mask = 0; 3795ebfe3c51SDi Zhu struct net_device *dev; 3796e5eca6d4SMichal Schmidt int hdrlen; 3797115c9b81SGreg Rose 3798e5eca6d4SMichal Schmidt /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */ 3799e5eca6d4SMichal Schmidt hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ? 3800e5eca6d4SMichal Schmidt sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); 3801e5eca6d4SMichal Schmidt 38028cb08174SJohannes Berg if (nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) { 3803115c9b81SGreg Rose if (tb[IFLA_EXT_MASK]) 3804115c9b81SGreg Rose ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 3805a4b64fbeSEric Dumazet } 3806115c9b81SGreg Rose 3807115c9b81SGreg Rose if (!ext_filter_mask) 3808115c9b81SGreg Rose return NLMSG_GOODSIZE; 3809115c9b81SGreg Rose /* 3810115c9b81SGreg Rose * traverse the list of net devices and compute the minimum 3811115c9b81SGreg Rose * buffer size based upon the filter mask. 3812115c9b81SGreg Rose */ 38136853dd48SFlorian Westphal rcu_read_lock(); 38146853dd48SFlorian Westphal for_each_netdev_rcu(net, dev) { 3815ebfe3c51SDi Zhu min_ifinfo_dump_size = max(min_ifinfo_dump_size, 3816ebfe3c51SDi Zhu if_nlmsg_size(dev, ext_filter_mask)); 3817115c9b81SGreg Rose } 38186853dd48SFlorian Westphal rcu_read_unlock(); 3819115c9b81SGreg Rose 382093af2056SZhang Shengju return nlmsg_total_size(min_ifinfo_dump_size); 3821c7ac8679SGreg Rose } 3822c7ac8679SGreg Rose 382342bad1daSAdrian Bunk static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb) 38241da177e4SLinus Torvalds { 38251da177e4SLinus Torvalds int idx; 38261da177e4SLinus Torvalds int s_idx = cb->family; 382787ccbb1fSChristian Brauner int type = cb->nlh->nlmsg_type - RTM_BASE; 3828c63586dcSDavid Ahern int ret = 0; 38291da177e4SLinus Torvalds 38301da177e4SLinus Torvalds if (s_idx == 0) 38311da177e4SLinus Torvalds s_idx = 1; 38326853dd48SFlorian Westphal 383325239ceeSPatrick McHardy for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) { 383451e13685SJakub Kicinski struct rtnl_link __rcu **tab; 3835addf9b90SFlorian Westphal struct rtnl_link *link; 38366853dd48SFlorian Westphal rtnl_dumpit_func dumpit; 38376853dd48SFlorian Westphal 38381da177e4SLinus Torvalds if (idx < s_idx || idx == PF_PACKET) 38391da177e4SLinus Torvalds continue; 38406853dd48SFlorian Westphal 3841addf9b90SFlorian Westphal if (type < 0 || type >= RTM_NR_MSGTYPES) 38421da177e4SLinus Torvalds continue; 38436853dd48SFlorian Westphal 3844addf9b90SFlorian Westphal tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]); 3845addf9b90SFlorian Westphal if (!tab) 3846addf9b90SFlorian Westphal continue; 3847addf9b90SFlorian Westphal 384851e13685SJakub Kicinski link = rcu_dereference_rtnl(tab[type]); 3849addf9b90SFlorian Westphal if (!link) 3850addf9b90SFlorian Westphal continue; 3851addf9b90SFlorian Westphal 3852addf9b90SFlorian Westphal dumpit = link->dumpit; 38536853dd48SFlorian Westphal if (!dumpit) 38546853dd48SFlorian Westphal continue; 38556853dd48SFlorian Westphal 38560465277fSNicolas Dichtel if (idx > s_idx) { 38571da177e4SLinus Torvalds memset(&cb->args[0], 0, sizeof(cb->args)); 38580465277fSNicolas Dichtel cb->prev_seq = 0; 38590465277fSNicolas Dichtel cb->seq = 0; 38600465277fSNicolas Dichtel } 3861c63586dcSDavid Ahern ret = dumpit(skb, cb); 38625e1acb4aSAlexey Kodanev if (ret) 38631da177e4SLinus Torvalds break; 38641da177e4SLinus Torvalds } 38651da177e4SLinus Torvalds cb->family = idx; 38661da177e4SLinus Torvalds 3867c63586dcSDavid Ahern return skb->len ? : ret; 38681da177e4SLinus Torvalds } 38691da177e4SLinus Torvalds 3870395eea6cSMahesh Bandewar struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev, 38713d3ea5afSVlad Yasevich unsigned int change, 387238e01b30SNicolas Dichtel u32 event, gfp_t flags, int *new_nsid, 387338e01b30SNicolas Dichtel int new_ifindex) 38741da177e4SLinus Torvalds { 3875c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 38761da177e4SLinus Torvalds struct sk_buff *skb; 38770ec6d3f4SThomas Graf int err = -ENOBUFS; 38781da177e4SLinus Torvalds 387950af5969Sluo penghao skb = nlmsg_new(if_nlmsg_size(dev, 0), flags); 38800ec6d3f4SThomas Graf if (skb == NULL) 38810ec6d3f4SThomas Graf goto errout; 38821da177e4SLinus Torvalds 388379e1ad14SJiri Benc err = rtnl_fill_ifinfo(skb, dev, dev_net(dev), 388479e1ad14SJiri Benc type, 0, 0, change, 0, 0, event, 3885d4e4fdf9SGuillaume Nault new_nsid, new_ifindex, -1, flags); 388626932566SPatrick McHardy if (err < 0) { 388726932566SPatrick McHardy /* -EMSGSIZE implies BUG in if_nlmsg_size() */ 388826932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 388926932566SPatrick McHardy kfree_skb(skb); 389026932566SPatrick McHardy goto errout; 389126932566SPatrick McHardy } 3892395eea6cSMahesh Bandewar return skb; 38930ec6d3f4SThomas Graf errout: 38940ec6d3f4SThomas Graf if (err < 0) 38954b3da706SEric W. Biederman rtnl_set_sk_err(net, RTNLGRP_LINK, err); 3896395eea6cSMahesh Bandewar return NULL; 3897395eea6cSMahesh Bandewar } 3898395eea6cSMahesh Bandewar 3899395eea6cSMahesh Bandewar void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags) 3900395eea6cSMahesh Bandewar { 3901395eea6cSMahesh Bandewar struct net *net = dev_net(dev); 3902395eea6cSMahesh Bandewar 3903395eea6cSMahesh Bandewar rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags); 3904395eea6cSMahesh Bandewar } 3905395eea6cSMahesh Bandewar 39063d3ea5afSVlad Yasevich static void rtmsg_ifinfo_event(int type, struct net_device *dev, 39073d3ea5afSVlad Yasevich unsigned int change, u32 event, 390838e01b30SNicolas Dichtel gfp_t flags, int *new_nsid, int new_ifindex) 3909395eea6cSMahesh Bandewar { 3910395eea6cSMahesh Bandewar struct sk_buff *skb; 3911395eea6cSMahesh Bandewar 3912ed2a80abSNicolas Dichtel if (dev->reg_state != NETREG_REGISTERED) 3913ed2a80abSNicolas Dichtel return; 3914ed2a80abSNicolas Dichtel 391538e01b30SNicolas Dichtel skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid, 391638e01b30SNicolas Dichtel new_ifindex); 3917395eea6cSMahesh Bandewar if (skb) 3918395eea6cSMahesh Bandewar rtmsg_ifinfo_send(skb, dev, flags); 39191da177e4SLinus Torvalds } 39203d3ea5afSVlad Yasevich 39213d3ea5afSVlad Yasevich void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change, 39223d3ea5afSVlad Yasevich gfp_t flags) 39233d3ea5afSVlad Yasevich { 392438e01b30SNicolas Dichtel rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags, 392538e01b30SNicolas Dichtel NULL, 0); 39263d3ea5afSVlad Yasevich } 39271da177e4SLinus Torvalds 39286621dd29SNicolas Dichtel void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change, 392938e01b30SNicolas Dichtel gfp_t flags, int *new_nsid, int new_ifindex) 39306621dd29SNicolas Dichtel { 39316621dd29SNicolas Dichtel rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags, 393238e01b30SNicolas Dichtel new_nsid, new_ifindex); 39336621dd29SNicolas Dichtel } 39346621dd29SNicolas Dichtel 3935d83b0603SJohn Fastabend static int nlmsg_populate_fdb_fill(struct sk_buff *skb, 3936d83b0603SJohn Fastabend struct net_device *dev, 39371e53d5bbSHubert Sokolowski u8 *addr, u16 vid, u32 pid, u32 seq, 39381c104a6bSNicolas Dichtel int type, unsigned int flags, 3939b3379041SHubert Sokolowski int nlflags, u16 ndm_state) 3940d83b0603SJohn Fastabend { 3941d83b0603SJohn Fastabend struct nlmsghdr *nlh; 3942d83b0603SJohn Fastabend struct ndmsg *ndm; 3943d83b0603SJohn Fastabend 39441c104a6bSNicolas Dichtel nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags); 3945d83b0603SJohn Fastabend if (!nlh) 3946d83b0603SJohn Fastabend return -EMSGSIZE; 3947d83b0603SJohn Fastabend 3948d83b0603SJohn Fastabend ndm = nlmsg_data(nlh); 3949d83b0603SJohn Fastabend ndm->ndm_family = AF_BRIDGE; 3950d83b0603SJohn Fastabend ndm->ndm_pad1 = 0; 3951d83b0603SJohn Fastabend ndm->ndm_pad2 = 0; 3952d83b0603SJohn Fastabend ndm->ndm_flags = flags; 3953d83b0603SJohn Fastabend ndm->ndm_type = 0; 3954d83b0603SJohn Fastabend ndm->ndm_ifindex = dev->ifindex; 3955b3379041SHubert Sokolowski ndm->ndm_state = ndm_state; 3956d83b0603SJohn Fastabend 3957d83b0603SJohn Fastabend if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr)) 3958d83b0603SJohn Fastabend goto nla_put_failure; 39591e53d5bbSHubert Sokolowski if (vid) 39601e53d5bbSHubert Sokolowski if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid)) 39611e53d5bbSHubert Sokolowski goto nla_put_failure; 3962d83b0603SJohn Fastabend 3963053c095aSJohannes Berg nlmsg_end(skb, nlh); 3964053c095aSJohannes Berg return 0; 3965d83b0603SJohn Fastabend 3966d83b0603SJohn Fastabend nla_put_failure: 3967d83b0603SJohn Fastabend nlmsg_cancel(skb, nlh); 3968d83b0603SJohn Fastabend return -EMSGSIZE; 3969d83b0603SJohn Fastabend } 3970d83b0603SJohn Fastabend 39713ff661c3SJohn Fastabend static inline size_t rtnl_fdb_nlmsg_size(void) 39723ff661c3SJohn Fastabend { 3973f82ef3e1SSabrina Dubroca return NLMSG_ALIGN(sizeof(struct ndmsg)) + 3974f82ef3e1SSabrina Dubroca nla_total_size(ETH_ALEN) + /* NDA_LLADDR */ 3975f82ef3e1SSabrina Dubroca nla_total_size(sizeof(u16)) + /* NDA_VLAN */ 3976f82ef3e1SSabrina Dubroca 0; 39773ff661c3SJohn Fastabend } 39783ff661c3SJohn Fastabend 3979b3379041SHubert Sokolowski static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type, 3980b3379041SHubert Sokolowski u16 ndm_state) 39813ff661c3SJohn Fastabend { 39823ff661c3SJohn Fastabend struct net *net = dev_net(dev); 39833ff661c3SJohn Fastabend struct sk_buff *skb; 39843ff661c3SJohn Fastabend int err = -ENOBUFS; 39853ff661c3SJohn Fastabend 39863ff661c3SJohn Fastabend skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC); 39873ff661c3SJohn Fastabend if (!skb) 39883ff661c3SJohn Fastabend goto errout; 39893ff661c3SJohn Fastabend 39901e53d5bbSHubert Sokolowski err = nlmsg_populate_fdb_fill(skb, dev, addr, vid, 3991b3379041SHubert Sokolowski 0, 0, type, NTF_SELF, 0, ndm_state); 39923ff661c3SJohn Fastabend if (err < 0) { 39933ff661c3SJohn Fastabend kfree_skb(skb); 39943ff661c3SJohn Fastabend goto errout; 39953ff661c3SJohn Fastabend } 39963ff661c3SJohn Fastabend 39973ff661c3SJohn Fastabend rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); 39983ff661c3SJohn Fastabend return; 39993ff661c3SJohn Fastabend errout: 40003ff661c3SJohn Fastabend rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); 40013ff661c3SJohn Fastabend } 40023ff661c3SJohn Fastabend 4003a986967eSBart Van Assche /* 4004090096bfSVlad Yasevich * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry 4005090096bfSVlad Yasevich */ 4006090096bfSVlad Yasevich int ndo_dflt_fdb_add(struct ndmsg *ndm, 4007090096bfSVlad Yasevich struct nlattr *tb[], 4008090096bfSVlad Yasevich struct net_device *dev, 4009f6f6424bSJiri Pirko const unsigned char *addr, u16 vid, 4010090096bfSVlad Yasevich u16 flags) 4011090096bfSVlad Yasevich { 4012090096bfSVlad Yasevich int err = -EINVAL; 4013090096bfSVlad Yasevich 4014090096bfSVlad Yasevich /* If aging addresses are supported device will need to 4015090096bfSVlad Yasevich * implement its own handler for this. 4016090096bfSVlad Yasevich */ 4017090096bfSVlad Yasevich if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) { 401878ecc890SVladimir Oltean netdev_info(dev, "default FDB implementation only supports local addresses\n"); 4019090096bfSVlad Yasevich return err; 4020090096bfSVlad Yasevich } 4021090096bfSVlad Yasevich 402265891feaSOr Gerlitz if (vid) { 402323ac0b42SVladimir Oltean netdev_info(dev, "vlans aren't supported yet for dev_uc|mc_add()\n"); 402465891feaSOr Gerlitz return err; 402565891feaSOr Gerlitz } 402665891feaSOr Gerlitz 4027090096bfSVlad Yasevich if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) 4028090096bfSVlad Yasevich err = dev_uc_add_excl(dev, addr); 4029090096bfSVlad Yasevich else if (is_multicast_ether_addr(addr)) 4030090096bfSVlad Yasevich err = dev_mc_add_excl(dev, addr); 4031090096bfSVlad Yasevich 4032090096bfSVlad Yasevich /* Only return duplicate errors if NLM_F_EXCL is set */ 4033090096bfSVlad Yasevich if (err == -EEXIST && !(flags & NLM_F_EXCL)) 4034090096bfSVlad Yasevich err = 0; 4035090096bfSVlad Yasevich 4036090096bfSVlad Yasevich return err; 4037090096bfSVlad Yasevich } 4038090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_add); 4039090096bfSVlad Yasevich 4040b88d12e4SFlorian Westphal static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid, 4041b88d12e4SFlorian Westphal struct netlink_ext_ack *extack) 4042f6f6424bSJiri Pirko { 4043f6f6424bSJiri Pirko u16 vid = 0; 4044f6f6424bSJiri Pirko 4045f6f6424bSJiri Pirko if (vlan_attr) { 4046f6f6424bSJiri Pirko if (nla_len(vlan_attr) != sizeof(u16)) { 4047b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid vlan attribute size"); 4048f6f6424bSJiri Pirko return -EINVAL; 4049f6f6424bSJiri Pirko } 4050f6f6424bSJiri Pirko 4051f6f6424bSJiri Pirko vid = nla_get_u16(vlan_attr); 4052f6f6424bSJiri Pirko 4053f6f6424bSJiri Pirko if (!vid || vid >= VLAN_VID_MASK) { 4054b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid vlan id"); 4055f6f6424bSJiri Pirko return -EINVAL; 4056f6f6424bSJiri Pirko } 4057f6f6424bSJiri Pirko } 4058f6f6424bSJiri Pirko *p_vid = vid; 4059f6f6424bSJiri Pirko return 0; 4060f6f6424bSJiri Pirko } 4061f6f6424bSJiri Pirko 4062c21ef3e3SDavid Ahern static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh, 4063c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 406477162022SJohn Fastabend { 406577162022SJohn Fastabend struct net *net = sock_net(skb->sk); 406677162022SJohn Fastabend struct ndmsg *ndm; 406777162022SJohn Fastabend struct nlattr *tb[NDA_MAX+1]; 406877162022SJohn Fastabend struct net_device *dev; 406977162022SJohn Fastabend u8 *addr; 4070f6f6424bSJiri Pirko u16 vid; 407177162022SJohn Fastabend int err; 407277162022SJohn Fastabend 40738cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, 40748cb08174SJohannes Berg extack); 407577162022SJohn Fastabend if (err < 0) 407677162022SJohn Fastabend return err; 407777162022SJohn Fastabend 407877162022SJohn Fastabend ndm = nlmsg_data(nlh); 407977162022SJohn Fastabend if (ndm->ndm_ifindex == 0) { 4080b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid ifindex"); 408177162022SJohn Fastabend return -EINVAL; 408277162022SJohn Fastabend } 408377162022SJohn Fastabend 408477162022SJohn Fastabend dev = __dev_get_by_index(net, ndm->ndm_ifindex); 408577162022SJohn Fastabend if (dev == NULL) { 4086b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 408777162022SJohn Fastabend return -ENODEV; 408877162022SJohn Fastabend } 408977162022SJohn Fastabend 409077162022SJohn Fastabend if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { 4091b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid address"); 409277162022SJohn Fastabend return -EINVAL; 409377162022SJohn Fastabend } 409477162022SJohn Fastabend 4095da715775SIdo Schimmel if (dev->type != ARPHRD_ETHER) { 4096da715775SIdo Schimmel NL_SET_ERR_MSG(extack, "FDB add only supported for Ethernet devices"); 4097da715775SIdo Schimmel return -EINVAL; 4098da715775SIdo Schimmel } 4099da715775SIdo Schimmel 410077162022SJohn Fastabend addr = nla_data(tb[NDA_LLADDR]); 410177162022SJohn Fastabend 4102b88d12e4SFlorian Westphal err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack); 4103f6f6424bSJiri Pirko if (err) 4104f6f6424bSJiri Pirko return err; 4105f6f6424bSJiri Pirko 410677162022SJohn Fastabend err = -EOPNOTSUPP; 410777162022SJohn Fastabend 410877162022SJohn Fastabend /* Support fdb on master device the net/bridge default case */ 410977162022SJohn Fastabend if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && 41102e92a2d0SJulian Wiedmann netif_is_bridge_port(dev)) { 4111898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4112898e5061SJiri Pirko const struct net_device_ops *ops = br_dev->netdev_ops; 4113898e5061SJiri Pirko 4114f6f6424bSJiri Pirko err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid, 411587b0984eSPetr Machata nlh->nlmsg_flags, extack); 411677162022SJohn Fastabend if (err) 411777162022SJohn Fastabend goto out; 411877162022SJohn Fastabend else 411977162022SJohn Fastabend ndm->ndm_flags &= ~NTF_MASTER; 412077162022SJohn Fastabend } 412177162022SJohn Fastabend 412277162022SJohn Fastabend /* Embedded bridge, macvlan, and any other device support */ 4123090096bfSVlad Yasevich if ((ndm->ndm_flags & NTF_SELF)) { 4124090096bfSVlad Yasevich if (dev->netdev_ops->ndo_fdb_add) 4125090096bfSVlad Yasevich err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr, 4126f6f6424bSJiri Pirko vid, 412787b0984eSPetr Machata nlh->nlmsg_flags, 412887b0984eSPetr Machata extack); 4129090096bfSVlad Yasevich else 4130f6f6424bSJiri Pirko err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid, 413177162022SJohn Fastabend nlh->nlmsg_flags); 413277162022SJohn Fastabend 41333ff661c3SJohn Fastabend if (!err) { 4134b3379041SHubert Sokolowski rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH, 4135b3379041SHubert Sokolowski ndm->ndm_state); 413677162022SJohn Fastabend ndm->ndm_flags &= ~NTF_SELF; 413777162022SJohn Fastabend } 41383ff661c3SJohn Fastabend } 413977162022SJohn Fastabend out: 414077162022SJohn Fastabend return err; 414177162022SJohn Fastabend } 414277162022SJohn Fastabend 4143a986967eSBart Van Assche /* 4144090096bfSVlad Yasevich * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry 4145090096bfSVlad Yasevich */ 4146090096bfSVlad Yasevich int ndo_dflt_fdb_del(struct ndmsg *ndm, 4147090096bfSVlad Yasevich struct nlattr *tb[], 4148090096bfSVlad Yasevich struct net_device *dev, 4149f6f6424bSJiri Pirko const unsigned char *addr, u16 vid) 4150090096bfSVlad Yasevich { 4151c8a89c4aSAlexander Duyck int err = -EINVAL; 4152090096bfSVlad Yasevich 4153090096bfSVlad Yasevich /* If aging addresses are supported device will need to 4154090096bfSVlad Yasevich * implement its own handler for this. 4155090096bfSVlad Yasevich */ 415664535993SSridhar Samudrala if (!(ndm->ndm_state & NUD_PERMANENT)) { 415778ecc890SVladimir Oltean netdev_info(dev, "default FDB implementation only supports local addresses\n"); 4158c8a89c4aSAlexander Duyck return err; 4159090096bfSVlad Yasevich } 4160090096bfSVlad Yasevich 4161090096bfSVlad Yasevich if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) 4162090096bfSVlad Yasevich err = dev_uc_del(dev, addr); 4163090096bfSVlad Yasevich else if (is_multicast_ether_addr(addr)) 4164090096bfSVlad Yasevich err = dev_mc_del(dev, addr); 4165090096bfSVlad Yasevich 4166090096bfSVlad Yasevich return err; 4167090096bfSVlad Yasevich } 4168090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_del); 4169090096bfSVlad Yasevich 4170c21ef3e3SDavid Ahern static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh, 4171c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 417277162022SJohn Fastabend { 417377162022SJohn Fastabend struct net *net = sock_net(skb->sk); 417477162022SJohn Fastabend struct ndmsg *ndm; 41751690be63SVlad Yasevich struct nlattr *tb[NDA_MAX+1]; 417677162022SJohn Fastabend struct net_device *dev; 417777162022SJohn Fastabend __u8 *addr; 41787d311801SColin Ian King int err; 4179f6f6424bSJiri Pirko u16 vid; 418077162022SJohn Fastabend 418190f62cf3SEric W. Biederman if (!netlink_capable(skb, CAP_NET_ADMIN)) 41821690be63SVlad Yasevich return -EPERM; 41831690be63SVlad Yasevich 41848cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, 41858cb08174SJohannes Berg extack); 41861690be63SVlad Yasevich if (err < 0) 41871690be63SVlad Yasevich return err; 418877162022SJohn Fastabend 418977162022SJohn Fastabend ndm = nlmsg_data(nlh); 419077162022SJohn Fastabend if (ndm->ndm_ifindex == 0) { 4191b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid ifindex"); 419277162022SJohn Fastabend return -EINVAL; 419377162022SJohn Fastabend } 419477162022SJohn Fastabend 419577162022SJohn Fastabend dev = __dev_get_by_index(net, ndm->ndm_ifindex); 419677162022SJohn Fastabend if (dev == NULL) { 4197b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 419877162022SJohn Fastabend return -ENODEV; 419977162022SJohn Fastabend } 420077162022SJohn Fastabend 42011690be63SVlad Yasevich if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { 4202b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid address"); 420377162022SJohn Fastabend return -EINVAL; 420477162022SJohn Fastabend } 420577162022SJohn Fastabend 4206da715775SIdo Schimmel if (dev->type != ARPHRD_ETHER) { 4207da715775SIdo Schimmel NL_SET_ERR_MSG(extack, "FDB delete only supported for Ethernet devices"); 4208da715775SIdo Schimmel return -EINVAL; 4209da715775SIdo Schimmel } 4210da715775SIdo Schimmel 42111690be63SVlad Yasevich addr = nla_data(tb[NDA_LLADDR]); 42121690be63SVlad Yasevich 4213b88d12e4SFlorian Westphal err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack); 4214f6f6424bSJiri Pirko if (err) 4215f6f6424bSJiri Pirko return err; 4216f6f6424bSJiri Pirko 421777162022SJohn Fastabend err = -EOPNOTSUPP; 421877162022SJohn Fastabend 421977162022SJohn Fastabend /* Support fdb on master device the net/bridge default case */ 422077162022SJohn Fastabend if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && 42212e92a2d0SJulian Wiedmann netif_is_bridge_port(dev)) { 4222898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4223898e5061SJiri Pirko const struct net_device_ops *ops = br_dev->netdev_ops; 422477162022SJohn Fastabend 4225898e5061SJiri Pirko if (ops->ndo_fdb_del) 4226f6f6424bSJiri Pirko err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid); 422777162022SJohn Fastabend 422877162022SJohn Fastabend if (err) 422977162022SJohn Fastabend goto out; 423077162022SJohn Fastabend else 423177162022SJohn Fastabend ndm->ndm_flags &= ~NTF_MASTER; 423277162022SJohn Fastabend } 423377162022SJohn Fastabend 423477162022SJohn Fastabend /* Embedded bridge, macvlan, and any other device support */ 4235090096bfSVlad Yasevich if (ndm->ndm_flags & NTF_SELF) { 4236090096bfSVlad Yasevich if (dev->netdev_ops->ndo_fdb_del) 4237f6f6424bSJiri Pirko err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr, 4238f6f6424bSJiri Pirko vid); 4239090096bfSVlad Yasevich else 4240f6f6424bSJiri Pirko err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid); 424177162022SJohn Fastabend 42423ff661c3SJohn Fastabend if (!err) { 4243b3379041SHubert Sokolowski rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH, 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 4252d83b0603SJohn Fastabend static int nlmsg_populate_fdb(struct sk_buff *skb, 4253d83b0603SJohn Fastabend struct netlink_callback *cb, 4254d83b0603SJohn Fastabend struct net_device *dev, 4255d83b0603SJohn Fastabend int *idx, 4256d83b0603SJohn Fastabend struct netdev_hw_addr_list *list) 4257d83b0603SJohn Fastabend { 4258d83b0603SJohn Fastabend struct netdev_hw_addr *ha; 4259d83b0603SJohn Fastabend int err; 426015e47304SEric W. Biederman u32 portid, seq; 4261d83b0603SJohn Fastabend 426215e47304SEric W. Biederman portid = NETLINK_CB(cb->skb).portid; 4263d83b0603SJohn Fastabend seq = cb->nlh->nlmsg_seq; 4264d83b0603SJohn Fastabend 4265d83b0603SJohn Fastabend list_for_each_entry(ha, &list->list, list) { 4266d297653dSRoopa Prabhu if (*idx < cb->args[2]) 4267d83b0603SJohn Fastabend goto skip; 4268d83b0603SJohn Fastabend 42691e53d5bbSHubert Sokolowski err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0, 4270a7a558feSJohn Fastabend portid, seq, 42711c104a6bSNicolas Dichtel RTM_NEWNEIGH, NTF_SELF, 4272b3379041SHubert Sokolowski NLM_F_MULTI, NUD_PERMANENT); 4273d83b0603SJohn Fastabend if (err < 0) 4274d83b0603SJohn Fastabend return err; 4275d83b0603SJohn Fastabend skip: 4276d83b0603SJohn Fastabend *idx += 1; 4277d83b0603SJohn Fastabend } 4278d83b0603SJohn Fastabend return 0; 4279d83b0603SJohn Fastabend } 4280d83b0603SJohn Fastabend 4281d83b0603SJohn Fastabend /** 42822c53040fSBen Hutchings * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table. 4283a986967eSBart Van Assche * @skb: socket buffer to store message in 4284a986967eSBart Van Assche * @cb: netlink callback 4285d83b0603SJohn Fastabend * @dev: netdevice 4286a986967eSBart Van Assche * @filter_dev: ignored 4287a986967eSBart Van Assche * @idx: the number of FDB table entries dumped is added to *@idx 4288d83b0603SJohn Fastabend * 4289d83b0603SJohn Fastabend * Default netdevice operation to dump the existing unicast address list. 429091f3e7b1SJohn Fastabend * Returns number of addresses from list put in skb. 4291d83b0603SJohn Fastabend */ 4292d83b0603SJohn Fastabend int ndo_dflt_fdb_dump(struct sk_buff *skb, 4293d83b0603SJohn Fastabend struct netlink_callback *cb, 4294d83b0603SJohn Fastabend struct net_device *dev, 42955d5eacb3SJamal Hadi Salim struct net_device *filter_dev, 4296d297653dSRoopa Prabhu int *idx) 4297d83b0603SJohn Fastabend { 4298d83b0603SJohn Fastabend int err; 4299d83b0603SJohn Fastabend 430068883893SEric Dumazet if (dev->type != ARPHRD_ETHER) 430168883893SEric Dumazet return -EINVAL; 430268883893SEric Dumazet 4303d83b0603SJohn Fastabend netif_addr_lock_bh(dev); 4304d297653dSRoopa Prabhu err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc); 4305d83b0603SJohn Fastabend if (err) 4306d83b0603SJohn Fastabend goto out; 43072934c9dbSZhang Shengju err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc); 4308d83b0603SJohn Fastabend out: 4309d83b0603SJohn Fastabend netif_addr_unlock_bh(dev); 4310d297653dSRoopa Prabhu return err; 4311d83b0603SJohn Fastabend } 4312d83b0603SJohn Fastabend EXPORT_SYMBOL(ndo_dflt_fdb_dump); 4313d83b0603SJohn Fastabend 43148c6e137fSDavid Ahern static int valid_fdb_dump_strict(const struct nlmsghdr *nlh, 43158c6e137fSDavid Ahern int *br_idx, int *brport_idx, 43168c6e137fSDavid Ahern struct netlink_ext_ack *extack) 43178c6e137fSDavid Ahern { 43188c6e137fSDavid Ahern struct nlattr *tb[NDA_MAX + 1]; 43198c6e137fSDavid Ahern struct ndmsg *ndm; 43208c6e137fSDavid Ahern int err, i; 43218c6e137fSDavid Ahern 43228c6e137fSDavid Ahern if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) { 43238c6e137fSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid header for fdb dump request"); 43248c6e137fSDavid Ahern return -EINVAL; 43258c6e137fSDavid Ahern } 43268c6e137fSDavid Ahern 43278c6e137fSDavid Ahern ndm = nlmsg_data(nlh); 43288c6e137fSDavid Ahern if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state || 43298c6e137fSDavid Ahern ndm->ndm_flags || ndm->ndm_type) { 43308b73018fSNikolay Aleksandrov NL_SET_ERR_MSG(extack, "Invalid values in header for fdb dump request"); 43318c6e137fSDavid Ahern return -EINVAL; 43328c6e137fSDavid Ahern } 43338c6e137fSDavid Ahern 43348cb08174SJohannes Berg err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb, 43358cb08174SJohannes Berg NDA_MAX, NULL, extack); 43368c6e137fSDavid Ahern if (err < 0) 43378c6e137fSDavid Ahern return err; 43388c6e137fSDavid Ahern 43398c6e137fSDavid Ahern *brport_idx = ndm->ndm_ifindex; 43408c6e137fSDavid Ahern for (i = 0; i <= NDA_MAX; ++i) { 43418c6e137fSDavid Ahern if (!tb[i]) 43428c6e137fSDavid Ahern continue; 43438c6e137fSDavid Ahern 43448c6e137fSDavid Ahern switch (i) { 43458c6e137fSDavid Ahern case NDA_IFINDEX: 43468c6e137fSDavid Ahern if (nla_len(tb[i]) != sizeof(u32)) { 43478c6e137fSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid IFINDEX attribute in fdb dump request"); 43488c6e137fSDavid Ahern return -EINVAL; 43498c6e137fSDavid Ahern } 43508c6e137fSDavid Ahern *brport_idx = nla_get_u32(tb[NDA_IFINDEX]); 43518c6e137fSDavid Ahern break; 43528c6e137fSDavid Ahern case NDA_MASTER: 43538c6e137fSDavid Ahern if (nla_len(tb[i]) != sizeof(u32)) { 43548c6e137fSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid MASTER attribute in fdb dump request"); 43558c6e137fSDavid Ahern return -EINVAL; 43568c6e137fSDavid Ahern } 43578c6e137fSDavid Ahern *br_idx = nla_get_u32(tb[NDA_MASTER]); 43588c6e137fSDavid Ahern break; 43598c6e137fSDavid Ahern default: 43608c6e137fSDavid Ahern NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb dump request"); 43618c6e137fSDavid Ahern return -EINVAL; 43628c6e137fSDavid Ahern } 43638c6e137fSDavid Ahern } 43648c6e137fSDavid Ahern 43658c6e137fSDavid Ahern return 0; 43668c6e137fSDavid Ahern } 43678c6e137fSDavid Ahern 43688dfbda19SDavid Ahern static int valid_fdb_dump_legacy(const struct nlmsghdr *nlh, 43698dfbda19SDavid Ahern int *br_idx, int *brport_idx, 43708dfbda19SDavid Ahern struct netlink_ext_ack *extack) 43718dfbda19SDavid Ahern { 43728dfbda19SDavid Ahern struct nlattr *tb[IFLA_MAX+1]; 43738dfbda19SDavid Ahern int err; 43748dfbda19SDavid Ahern 43758dfbda19SDavid Ahern /* A hack to preserve kernel<->userspace interface. 43768dfbda19SDavid Ahern * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0. 43778dfbda19SDavid Ahern * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails. 43788dfbda19SDavid Ahern * So, check for ndmsg with an optional u32 attribute (not used here). 43798dfbda19SDavid Ahern * Fortunately these sizes don't conflict with the size of ifinfomsg 43808dfbda19SDavid Ahern * with an optional attribute. 43818dfbda19SDavid Ahern */ 43828dfbda19SDavid Ahern if (nlmsg_len(nlh) != sizeof(struct ndmsg) && 43838dfbda19SDavid Ahern (nlmsg_len(nlh) != sizeof(struct ndmsg) + 43848dfbda19SDavid Ahern nla_attr_size(sizeof(u32)))) { 43854565d7e5SDavid Ahern struct ifinfomsg *ifm; 43864565d7e5SDavid Ahern 43878cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg), 43888cb08174SJohannes Berg tb, IFLA_MAX, ifla_policy, 43898cb08174SJohannes Berg extack); 43908dfbda19SDavid Ahern if (err < 0) { 43918dfbda19SDavid Ahern return -EINVAL; 43928dfbda19SDavid Ahern } else if (err == 0) { 43938dfbda19SDavid Ahern if (tb[IFLA_MASTER]) 43948dfbda19SDavid Ahern *br_idx = nla_get_u32(tb[IFLA_MASTER]); 43958dfbda19SDavid Ahern } 43968dfbda19SDavid Ahern 43974565d7e5SDavid Ahern ifm = nlmsg_data(nlh); 43988dfbda19SDavid Ahern *brport_idx = ifm->ifi_index; 43998dfbda19SDavid Ahern } 44008dfbda19SDavid Ahern return 0; 44018dfbda19SDavid Ahern } 44028dfbda19SDavid Ahern 440377162022SJohn Fastabend static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb) 440477162022SJohn Fastabend { 440577162022SJohn Fastabend struct net_device *dev; 44065e6d2435SJamal Hadi Salim struct net_device *br_dev = NULL; 44075e6d2435SJamal Hadi Salim const struct net_device_ops *ops = NULL; 44085e6d2435SJamal Hadi Salim const struct net_device_ops *cops = NULL; 44095e6d2435SJamal Hadi Salim struct net *net = sock_net(skb->sk); 4410d297653dSRoopa Prabhu struct hlist_head *head; 44115e6d2435SJamal Hadi Salim int brport_idx = 0; 44125e6d2435SJamal Hadi Salim int br_idx = 0; 4413d297653dSRoopa Prabhu int h, s_h; 4414d297653dSRoopa Prabhu int idx = 0, s_idx; 4415d297653dSRoopa Prabhu int err = 0; 4416d297653dSRoopa Prabhu int fidx = 0; 441777162022SJohn Fastabend 44188c6e137fSDavid Ahern if (cb->strict_check) 44198c6e137fSDavid Ahern err = valid_fdb_dump_strict(cb->nlh, &br_idx, &brport_idx, 44208c6e137fSDavid Ahern cb->extack); 44218c6e137fSDavid Ahern else 44228dfbda19SDavid Ahern err = valid_fdb_dump_legacy(cb->nlh, &br_idx, &brport_idx, 44238dfbda19SDavid Ahern cb->extack); 44248dfbda19SDavid Ahern if (err < 0) 44258dfbda19SDavid Ahern return err; 44265e6d2435SJamal Hadi Salim 44275e6d2435SJamal Hadi Salim if (br_idx) { 44285e6d2435SJamal Hadi Salim br_dev = __dev_get_by_index(net, br_idx); 44295e6d2435SJamal Hadi Salim if (!br_dev) 44305e6d2435SJamal Hadi Salim return -ENODEV; 44315e6d2435SJamal Hadi Salim 4432898e5061SJiri Pirko ops = br_dev->netdev_ops; 44335e6d2435SJamal Hadi Salim } 44345e6d2435SJamal Hadi Salim 4435d297653dSRoopa Prabhu s_h = cb->args[0]; 4436d297653dSRoopa Prabhu s_idx = cb->args[1]; 4437d297653dSRoopa Prabhu 4438d297653dSRoopa Prabhu for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 4439d297653dSRoopa Prabhu idx = 0; 4440d297653dSRoopa Prabhu head = &net->dev_index_head[h]; 4441d297653dSRoopa Prabhu hlist_for_each_entry(dev, head, index_hlist) { 4442d297653dSRoopa Prabhu 44435e6d2435SJamal Hadi Salim if (brport_idx && (dev->ifindex != brport_idx)) 44445e6d2435SJamal Hadi Salim continue; 44455e6d2435SJamal Hadi Salim 44465e6d2435SJamal Hadi Salim if (!br_idx) { /* user did not specify a specific bridge */ 44472e92a2d0SJulian Wiedmann if (netif_is_bridge_port(dev)) { 44485e6d2435SJamal Hadi Salim br_dev = netdev_master_upper_dev_get(dev); 44495e6d2435SJamal Hadi Salim cops = br_dev->netdev_ops; 44505e6d2435SJamal Hadi Salim } 44515e6d2435SJamal Hadi Salim } else { 44525e6d2435SJamal Hadi Salim if (dev != br_dev && 44532e92a2d0SJulian Wiedmann !netif_is_bridge_port(dev)) 44545e6d2435SJamal Hadi Salim continue; 44555e6d2435SJamal Hadi Salim 44565e6d2435SJamal Hadi Salim if (br_dev != netdev_master_upper_dev_get(dev) && 4457254ec036SKyungrok Chung !netif_is_bridge_master(dev)) 44585e6d2435SJamal Hadi Salim continue; 44595e6d2435SJamal Hadi Salim cops = ops; 44605e6d2435SJamal Hadi Salim } 44615e6d2435SJamal Hadi Salim 4462d297653dSRoopa Prabhu if (idx < s_idx) 4463d297653dSRoopa Prabhu goto cont; 4464d297653dSRoopa Prabhu 44652e92a2d0SJulian Wiedmann if (netif_is_bridge_port(dev)) { 4466d297653dSRoopa Prabhu if (cops && cops->ndo_fdb_dump) { 4467d297653dSRoopa Prabhu err = cops->ndo_fdb_dump(skb, cb, 4468d297653dSRoopa Prabhu br_dev, dev, 4469d297653dSRoopa Prabhu &fidx); 4470d297653dSRoopa Prabhu if (err == -EMSGSIZE) 4471d297653dSRoopa Prabhu goto out; 447277162022SJohn Fastabend } 4473d297653dSRoopa Prabhu } 447477162022SJohn Fastabend 44755e6d2435SJamal Hadi Salim if (dev->netdev_ops->ndo_fdb_dump) 4476d297653dSRoopa Prabhu err = dev->netdev_ops->ndo_fdb_dump(skb, cb, 4477d297653dSRoopa Prabhu dev, NULL, 4478d297653dSRoopa Prabhu &fidx); 44796cb69742SHubert Sokolowski else 4480d297653dSRoopa Prabhu err = ndo_dflt_fdb_dump(skb, cb, dev, NULL, 4481d297653dSRoopa Prabhu &fidx); 4482d297653dSRoopa Prabhu if (err == -EMSGSIZE) 4483d297653dSRoopa Prabhu goto out; 44845e6d2435SJamal Hadi Salim 44855e6d2435SJamal Hadi Salim cops = NULL; 4486d297653dSRoopa Prabhu 4487d297653dSRoopa Prabhu /* reset fdb offset to 0 for rest of the interfaces */ 4488d297653dSRoopa Prabhu cb->args[2] = 0; 4489d297653dSRoopa Prabhu fidx = 0; 4490d297653dSRoopa Prabhu cont: 4491d297653dSRoopa Prabhu idx++; 4492d297653dSRoopa Prabhu } 449377162022SJohn Fastabend } 449477162022SJohn Fastabend 4495d297653dSRoopa Prabhu out: 4496d297653dSRoopa Prabhu cb->args[0] = h; 4497d297653dSRoopa Prabhu cb->args[1] = idx; 4498d297653dSRoopa Prabhu cb->args[2] = fidx; 4499d297653dSRoopa Prabhu 450077162022SJohn Fastabend return skb->len; 450177162022SJohn Fastabend } 450277162022SJohn Fastabend 45035b2f94b2SRoopa Prabhu static int valid_fdb_get_strict(const struct nlmsghdr *nlh, 45045b2f94b2SRoopa Prabhu struct nlattr **tb, u8 *ndm_flags, 45055b2f94b2SRoopa Prabhu int *br_idx, int *brport_idx, u8 **addr, 45065b2f94b2SRoopa Prabhu u16 *vid, struct netlink_ext_ack *extack) 45075b2f94b2SRoopa Prabhu { 45085b2f94b2SRoopa Prabhu struct ndmsg *ndm; 45095b2f94b2SRoopa Prabhu int err, i; 45105b2f94b2SRoopa Prabhu 45115b2f94b2SRoopa Prabhu if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) { 45125b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid header for fdb get request"); 45135b2f94b2SRoopa Prabhu return -EINVAL; 45145b2f94b2SRoopa Prabhu } 45155b2f94b2SRoopa Prabhu 45165b2f94b2SRoopa Prabhu ndm = nlmsg_data(nlh); 45175b2f94b2SRoopa Prabhu if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state || 45185b2f94b2SRoopa Prabhu ndm->ndm_type) { 45195b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid values in header for fdb get request"); 45205b2f94b2SRoopa Prabhu return -EINVAL; 45215b2f94b2SRoopa Prabhu } 45225b2f94b2SRoopa Prabhu 45235b2f94b2SRoopa Prabhu if (ndm->ndm_flags & ~(NTF_MASTER | NTF_SELF)) { 45245b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid flags in header for fdb get request"); 45255b2f94b2SRoopa Prabhu return -EINVAL; 45265b2f94b2SRoopa Prabhu } 45275b2f94b2SRoopa Prabhu 45288cb08174SJohannes Berg err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb, 45298cb08174SJohannes Berg NDA_MAX, nda_policy, extack); 45305b2f94b2SRoopa Prabhu if (err < 0) 45315b2f94b2SRoopa Prabhu return err; 45325b2f94b2SRoopa Prabhu 45335b2f94b2SRoopa Prabhu *ndm_flags = ndm->ndm_flags; 45345b2f94b2SRoopa Prabhu *brport_idx = ndm->ndm_ifindex; 45355b2f94b2SRoopa Prabhu for (i = 0; i <= NDA_MAX; ++i) { 45365b2f94b2SRoopa Prabhu if (!tb[i]) 45375b2f94b2SRoopa Prabhu continue; 45385b2f94b2SRoopa Prabhu 45395b2f94b2SRoopa Prabhu switch (i) { 45405b2f94b2SRoopa Prabhu case NDA_MASTER: 45415b2f94b2SRoopa Prabhu *br_idx = nla_get_u32(tb[i]); 45425b2f94b2SRoopa Prabhu break; 45435b2f94b2SRoopa Prabhu case NDA_LLADDR: 45445b2f94b2SRoopa Prabhu if (nla_len(tb[i]) != ETH_ALEN) { 45455b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid address in fdb get request"); 45465b2f94b2SRoopa Prabhu return -EINVAL; 45475b2f94b2SRoopa Prabhu } 45485b2f94b2SRoopa Prabhu *addr = nla_data(tb[i]); 45495b2f94b2SRoopa Prabhu break; 45505b2f94b2SRoopa Prabhu case NDA_VLAN: 45515b2f94b2SRoopa Prabhu err = fdb_vid_parse(tb[i], vid, extack); 45525b2f94b2SRoopa Prabhu if (err) 45535b2f94b2SRoopa Prabhu return err; 45545b2f94b2SRoopa Prabhu break; 45555b2f94b2SRoopa Prabhu case NDA_VNI: 45565b2f94b2SRoopa Prabhu break; 45575b2f94b2SRoopa Prabhu default: 45585b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb get request"); 45595b2f94b2SRoopa Prabhu return -EINVAL; 45605b2f94b2SRoopa Prabhu } 45615b2f94b2SRoopa Prabhu } 45625b2f94b2SRoopa Prabhu 45635b2f94b2SRoopa Prabhu return 0; 45645b2f94b2SRoopa Prabhu } 45655b2f94b2SRoopa Prabhu 45665b2f94b2SRoopa Prabhu static int rtnl_fdb_get(struct sk_buff *in_skb, struct nlmsghdr *nlh, 45675b2f94b2SRoopa Prabhu struct netlink_ext_ack *extack) 45685b2f94b2SRoopa Prabhu { 45695b2f94b2SRoopa Prabhu struct net_device *dev = NULL, *br_dev = NULL; 45705b2f94b2SRoopa Prabhu const struct net_device_ops *ops = NULL; 45715b2f94b2SRoopa Prabhu struct net *net = sock_net(in_skb->sk); 45725b2f94b2SRoopa Prabhu struct nlattr *tb[NDA_MAX + 1]; 45735b2f94b2SRoopa Prabhu struct sk_buff *skb; 45745b2f94b2SRoopa Prabhu int brport_idx = 0; 45755b2f94b2SRoopa Prabhu u8 ndm_flags = 0; 45765b2f94b2SRoopa Prabhu int br_idx = 0; 45775b2f94b2SRoopa Prabhu u8 *addr = NULL; 45785b2f94b2SRoopa Prabhu u16 vid = 0; 45795b2f94b2SRoopa Prabhu int err; 45805b2f94b2SRoopa Prabhu 45815b2f94b2SRoopa Prabhu err = valid_fdb_get_strict(nlh, tb, &ndm_flags, &br_idx, 45825b2f94b2SRoopa Prabhu &brport_idx, &addr, &vid, extack); 45835b2f94b2SRoopa Prabhu if (err < 0) 45845b2f94b2SRoopa Prabhu return err; 45855b2f94b2SRoopa Prabhu 4586f989d03eSNikolay Aleksandrov if (!addr) { 4587f989d03eSNikolay Aleksandrov NL_SET_ERR_MSG(extack, "Missing lookup address for fdb get request"); 4588f989d03eSNikolay Aleksandrov return -EINVAL; 4589f989d03eSNikolay Aleksandrov } 4590f989d03eSNikolay Aleksandrov 45915b2f94b2SRoopa Prabhu if (brport_idx) { 45925b2f94b2SRoopa Prabhu dev = __dev_get_by_index(net, brport_idx); 45935b2f94b2SRoopa Prabhu if (!dev) { 45945b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Unknown device ifindex"); 45955b2f94b2SRoopa Prabhu return -ENODEV; 45965b2f94b2SRoopa Prabhu } 45975b2f94b2SRoopa Prabhu } 45985b2f94b2SRoopa Prabhu 45995b2f94b2SRoopa Prabhu if (br_idx) { 46005b2f94b2SRoopa Prabhu if (dev) { 46015b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Master and device are mutually exclusive"); 46025b2f94b2SRoopa Prabhu return -EINVAL; 46035b2f94b2SRoopa Prabhu } 46045b2f94b2SRoopa Prabhu 46055b2f94b2SRoopa Prabhu br_dev = __dev_get_by_index(net, br_idx); 46065b2f94b2SRoopa Prabhu if (!br_dev) { 46075b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid master ifindex"); 46085b2f94b2SRoopa Prabhu return -EINVAL; 46095b2f94b2SRoopa Prabhu } 46105b2f94b2SRoopa Prabhu ops = br_dev->netdev_ops; 46115b2f94b2SRoopa Prabhu } 46125b2f94b2SRoopa Prabhu 46135b2f94b2SRoopa Prabhu if (dev) { 46145b2f94b2SRoopa Prabhu if (!ndm_flags || (ndm_flags & NTF_MASTER)) { 46152e92a2d0SJulian Wiedmann if (!netif_is_bridge_port(dev)) { 46165b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Device is not a bridge port"); 46175b2f94b2SRoopa Prabhu return -EINVAL; 46185b2f94b2SRoopa Prabhu } 46195b2f94b2SRoopa Prabhu br_dev = netdev_master_upper_dev_get(dev); 46205b2f94b2SRoopa Prabhu if (!br_dev) { 46215b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Master of device not found"); 46225b2f94b2SRoopa Prabhu return -EINVAL; 46235b2f94b2SRoopa Prabhu } 46245b2f94b2SRoopa Prabhu ops = br_dev->netdev_ops; 46255b2f94b2SRoopa Prabhu } else { 46265b2f94b2SRoopa Prabhu if (!(ndm_flags & NTF_SELF)) { 46275b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Missing NTF_SELF"); 46285b2f94b2SRoopa Prabhu return -EINVAL; 46295b2f94b2SRoopa Prabhu } 46305b2f94b2SRoopa Prabhu ops = dev->netdev_ops; 46315b2f94b2SRoopa Prabhu } 46325b2f94b2SRoopa Prabhu } 46335b2f94b2SRoopa Prabhu 46345b2f94b2SRoopa Prabhu if (!br_dev && !dev) { 46355b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "No device specified"); 46365b2f94b2SRoopa Prabhu return -ENODEV; 46375b2f94b2SRoopa Prabhu } 46385b2f94b2SRoopa Prabhu 46395b2f94b2SRoopa Prabhu if (!ops || !ops->ndo_fdb_get) { 46405b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Fdb get operation not supported by device"); 46415b2f94b2SRoopa Prabhu return -EOPNOTSUPP; 46425b2f94b2SRoopa Prabhu } 46435b2f94b2SRoopa Prabhu 46445b2f94b2SRoopa Prabhu skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); 46455b2f94b2SRoopa Prabhu if (!skb) 46465b2f94b2SRoopa Prabhu return -ENOBUFS; 46475b2f94b2SRoopa Prabhu 46485b2f94b2SRoopa Prabhu if (br_dev) 46495b2f94b2SRoopa Prabhu dev = br_dev; 46505b2f94b2SRoopa Prabhu err = ops->ndo_fdb_get(skb, tb, dev, addr, vid, 46515b2f94b2SRoopa Prabhu NETLINK_CB(in_skb).portid, 46525b2f94b2SRoopa Prabhu nlh->nlmsg_seq, extack); 46535b2f94b2SRoopa Prabhu if (err) 46545b2f94b2SRoopa Prabhu goto out; 46555b2f94b2SRoopa Prabhu 46565b2f94b2SRoopa Prabhu return rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 46575b2f94b2SRoopa Prabhu out: 46585b2f94b2SRoopa Prabhu kfree_skb(skb); 46595b2f94b2SRoopa Prabhu return err; 46605b2f94b2SRoopa Prabhu } 46615b2f94b2SRoopa Prabhu 46622c3c031cSScott Feldman static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask, 46632c3c031cSScott Feldman unsigned int attrnum, unsigned int flag) 46642c3c031cSScott Feldman { 46652c3c031cSScott Feldman if (mask & flag) 46662c3c031cSScott Feldman return nla_put_u8(skb, attrnum, !!(flags & flag)); 46672c3c031cSScott Feldman return 0; 46682c3c031cSScott Feldman } 46692c3c031cSScott Feldman 4670815cccbfSJohn Fastabend int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq, 46712c3c031cSScott Feldman struct net_device *dev, u16 mode, 46727d4f8d87SScott Feldman u32 flags, u32 mask, int nlflags, 46737d4f8d87SScott Feldman u32 filter_mask, 46747d4f8d87SScott Feldman int (*vlan_fill)(struct sk_buff *skb, 46757d4f8d87SScott Feldman struct net_device *dev, 46767d4f8d87SScott Feldman u32 filter_mask)) 4677815cccbfSJohn Fastabend { 4678815cccbfSJohn Fastabend struct nlmsghdr *nlh; 4679815cccbfSJohn Fastabend struct ifinfomsg *ifm; 4680815cccbfSJohn Fastabend struct nlattr *br_afspec; 46812c3c031cSScott Feldman struct nlattr *protinfo; 4682815cccbfSJohn Fastabend u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN; 4683898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 46847d4f8d87SScott Feldman int err = 0; 4685815cccbfSJohn Fastabend 468646c264daSNicolas Dichtel nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags); 4687815cccbfSJohn Fastabend if (nlh == NULL) 4688815cccbfSJohn Fastabend return -EMSGSIZE; 4689815cccbfSJohn Fastabend 4690815cccbfSJohn Fastabend ifm = nlmsg_data(nlh); 4691815cccbfSJohn Fastabend ifm->ifi_family = AF_BRIDGE; 4692815cccbfSJohn Fastabend ifm->__ifi_pad = 0; 4693815cccbfSJohn Fastabend ifm->ifi_type = dev->type; 4694815cccbfSJohn Fastabend ifm->ifi_index = dev->ifindex; 4695815cccbfSJohn Fastabend ifm->ifi_flags = dev_get_flags(dev); 4696815cccbfSJohn Fastabend ifm->ifi_change = 0; 4697815cccbfSJohn Fastabend 4698815cccbfSJohn Fastabend 4699815cccbfSJohn Fastabend if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 4700815cccbfSJohn Fastabend nla_put_u32(skb, IFLA_MTU, dev->mtu) || 4701815cccbfSJohn Fastabend nla_put_u8(skb, IFLA_OPERSTATE, operstate) || 4702898e5061SJiri Pirko (br_dev && 4703898e5061SJiri Pirko nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) || 4704815cccbfSJohn Fastabend (dev->addr_len && 4705815cccbfSJohn Fastabend nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || 4706a54acb3aSNicolas Dichtel (dev->ifindex != dev_get_iflink(dev) && 4707a54acb3aSNicolas Dichtel nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev)))) 4708815cccbfSJohn Fastabend goto nla_put_failure; 4709815cccbfSJohn Fastabend 4710ae0be8deSMichal Kubecek br_afspec = nla_nest_start_noflag(skb, IFLA_AF_SPEC); 4711815cccbfSJohn Fastabend if (!br_afspec) 4712815cccbfSJohn Fastabend goto nla_put_failure; 4713815cccbfSJohn Fastabend 47141d460b98SRoopa Prabhu if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) { 4715815cccbfSJohn Fastabend nla_nest_cancel(skb, br_afspec); 4716815cccbfSJohn Fastabend goto nla_put_failure; 4717815cccbfSJohn Fastabend } 47181d460b98SRoopa Prabhu 47191d460b98SRoopa Prabhu if (mode != BRIDGE_MODE_UNDEF) { 47201d460b98SRoopa Prabhu if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) { 47211d460b98SRoopa Prabhu nla_nest_cancel(skb, br_afspec); 47221d460b98SRoopa Prabhu goto nla_put_failure; 47231d460b98SRoopa Prabhu } 47241d460b98SRoopa Prabhu } 47257d4f8d87SScott Feldman if (vlan_fill) { 47267d4f8d87SScott Feldman err = vlan_fill(skb, dev, filter_mask); 47277d4f8d87SScott Feldman if (err) { 47287d4f8d87SScott Feldman nla_nest_cancel(skb, br_afspec); 47297d4f8d87SScott Feldman goto nla_put_failure; 47307d4f8d87SScott Feldman } 47317d4f8d87SScott Feldman } 4732815cccbfSJohn Fastabend nla_nest_end(skb, br_afspec); 4733815cccbfSJohn Fastabend 4734ae0be8deSMichal Kubecek protinfo = nla_nest_start(skb, IFLA_PROTINFO); 47352c3c031cSScott Feldman if (!protinfo) 47362c3c031cSScott Feldman goto nla_put_failure; 47372c3c031cSScott Feldman 47382c3c031cSScott Feldman if (brport_nla_put_flag(skb, flags, mask, 47392c3c031cSScott Feldman IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) || 47402c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 47412c3c031cSScott Feldman IFLA_BRPORT_GUARD, BR_BPDU_GUARD) || 47422c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 47432c3c031cSScott Feldman IFLA_BRPORT_FAST_LEAVE, 47442c3c031cSScott Feldman BR_MULTICAST_FAST_LEAVE) || 47452c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 47462c3c031cSScott Feldman IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) || 47472c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 47482c3c031cSScott Feldman IFLA_BRPORT_LEARNING, BR_LEARNING) || 47492c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 47502c3c031cSScott Feldman IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) || 47512c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 47522c3c031cSScott Feldman IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) || 47532c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 4754583cb0b4SJulian Wiedmann IFLA_BRPORT_PROXYARP, BR_PROXYARP) || 4755583cb0b4SJulian Wiedmann brport_nla_put_flag(skb, flags, mask, 4756583cb0b4SJulian Wiedmann IFLA_BRPORT_MCAST_FLOOD, BR_MCAST_FLOOD) || 4757583cb0b4SJulian Wiedmann brport_nla_put_flag(skb, flags, mask, 4758583cb0b4SJulian Wiedmann IFLA_BRPORT_BCAST_FLOOD, BR_BCAST_FLOOD)) { 47592c3c031cSScott Feldman nla_nest_cancel(skb, protinfo); 47602c3c031cSScott Feldman goto nla_put_failure; 47612c3c031cSScott Feldman } 47622c3c031cSScott Feldman 47632c3c031cSScott Feldman nla_nest_end(skb, protinfo); 47642c3c031cSScott Feldman 4765053c095aSJohannes Berg nlmsg_end(skb, nlh); 4766053c095aSJohannes Berg return 0; 4767815cccbfSJohn Fastabend nla_put_failure: 4768815cccbfSJohn Fastabend nlmsg_cancel(skb, nlh); 47697d4f8d87SScott Feldman return err ? err : -EMSGSIZE; 4770815cccbfSJohn Fastabend } 47717d4f8d87SScott Feldman EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink); 4772815cccbfSJohn Fastabend 47732d011be8SDavid Ahern static int valid_bridge_getlink_req(const struct nlmsghdr *nlh, 47742d011be8SDavid Ahern bool strict_check, u32 *filter_mask, 47752d011be8SDavid Ahern struct netlink_ext_ack *extack) 47762d011be8SDavid Ahern { 47772d011be8SDavid Ahern struct nlattr *tb[IFLA_MAX+1]; 47782d011be8SDavid Ahern int err, i; 47792d011be8SDavid Ahern 47802d011be8SDavid Ahern if (strict_check) { 47812d011be8SDavid Ahern struct ifinfomsg *ifm; 47822d011be8SDavid Ahern 47832d011be8SDavid Ahern if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { 47842d011be8SDavid Ahern NL_SET_ERR_MSG(extack, "Invalid header for bridge link dump"); 47852d011be8SDavid Ahern return -EINVAL; 47862d011be8SDavid Ahern } 47872d011be8SDavid Ahern 47882d011be8SDavid Ahern ifm = nlmsg_data(nlh); 47892d011be8SDavid Ahern if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags || 47902d011be8SDavid Ahern ifm->ifi_change || ifm->ifi_index) { 47912d011be8SDavid Ahern NL_SET_ERR_MSG(extack, "Invalid values in header for bridge link dump request"); 47922d011be8SDavid Ahern return -EINVAL; 47932d011be8SDavid Ahern } 47942d011be8SDavid Ahern 47958cb08174SJohannes Berg err = nlmsg_parse_deprecated_strict(nlh, 47968cb08174SJohannes Berg sizeof(struct ifinfomsg), 47978cb08174SJohannes Berg tb, IFLA_MAX, ifla_policy, 47988cb08174SJohannes Berg extack); 47992d011be8SDavid Ahern } else { 48008cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg), 48018cb08174SJohannes Berg tb, IFLA_MAX, ifla_policy, 48028cb08174SJohannes Berg extack); 48032d011be8SDavid Ahern } 48042d011be8SDavid Ahern if (err < 0) 48052d011be8SDavid Ahern return err; 48062d011be8SDavid Ahern 48072d011be8SDavid Ahern /* new attributes should only be added with strict checking */ 48082d011be8SDavid Ahern for (i = 0; i <= IFLA_MAX; ++i) { 48092d011be8SDavid Ahern if (!tb[i]) 48102d011be8SDavid Ahern continue; 48112d011be8SDavid Ahern 48122d011be8SDavid Ahern switch (i) { 48132d011be8SDavid Ahern case IFLA_EXT_MASK: 48142d011be8SDavid Ahern *filter_mask = nla_get_u32(tb[i]); 48152d011be8SDavid Ahern break; 48162d011be8SDavid Ahern default: 48172d011be8SDavid Ahern if (strict_check) { 48182d011be8SDavid Ahern NL_SET_ERR_MSG(extack, "Unsupported attribute in bridge link dump request"); 48192d011be8SDavid Ahern return -EINVAL; 48202d011be8SDavid Ahern } 48212d011be8SDavid Ahern } 48222d011be8SDavid Ahern } 48232d011be8SDavid Ahern 48242d011be8SDavid Ahern return 0; 48252d011be8SDavid Ahern } 48262d011be8SDavid Ahern 4827e5a55a89SJohn Fastabend static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb) 4828e5a55a89SJohn Fastabend { 48292d011be8SDavid Ahern const struct nlmsghdr *nlh = cb->nlh; 4830e5a55a89SJohn Fastabend struct net *net = sock_net(skb->sk); 4831e5a55a89SJohn Fastabend struct net_device *dev; 4832e5a55a89SJohn Fastabend int idx = 0; 4833e5a55a89SJohn Fastabend u32 portid = NETLINK_CB(cb->skb).portid; 48342d011be8SDavid Ahern u32 seq = nlh->nlmsg_seq; 48356cbdceebSVlad Yasevich u32 filter_mask = 0; 4836d64f69b0SRoopa Prabhu int err; 48376cbdceebSVlad Yasevich 48382d011be8SDavid Ahern err = valid_bridge_getlink_req(nlh, cb->strict_check, &filter_mask, 48392d011be8SDavid Ahern cb->extack); 48402d011be8SDavid Ahern if (err < 0 && cb->strict_check) 48412d011be8SDavid Ahern return err; 4842e5a55a89SJohn Fastabend 4843e5a55a89SJohn Fastabend rcu_read_lock(); 4844e5a55a89SJohn Fastabend for_each_netdev_rcu(net, dev) { 4845e5a55a89SJohn Fastabend const struct net_device_ops *ops = dev->netdev_ops; 4846898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4847e5a55a89SJohn Fastabend 4848898e5061SJiri Pirko if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) { 4849d64f69b0SRoopa Prabhu if (idx >= cb->args[0]) { 4850d64f69b0SRoopa Prabhu err = br_dev->netdev_ops->ndo_bridge_getlink( 4851d64f69b0SRoopa Prabhu skb, portid, seq, dev, 4852d64f69b0SRoopa Prabhu filter_mask, NLM_F_MULTI); 4853f6c5775fSDavid Ahern if (err < 0 && err != -EOPNOTSUPP) { 4854f6c5775fSDavid Ahern if (likely(skb->len)) 4855e5a55a89SJohn Fastabend break; 4856f6c5775fSDavid Ahern 4857f6c5775fSDavid Ahern goto out_err; 4858f6c5775fSDavid Ahern } 4859d64f69b0SRoopa Prabhu } 4860e5a55a89SJohn Fastabend idx++; 4861e5a55a89SJohn Fastabend } 4862e5a55a89SJohn Fastabend 4863e5a55a89SJohn Fastabend if (ops->ndo_bridge_getlink) { 4864d64f69b0SRoopa Prabhu if (idx >= cb->args[0]) { 4865d64f69b0SRoopa Prabhu err = ops->ndo_bridge_getlink(skb, portid, 4866d64f69b0SRoopa Prabhu seq, dev, 486746c264daSNicolas Dichtel filter_mask, 4868d64f69b0SRoopa Prabhu NLM_F_MULTI); 4869f6c5775fSDavid Ahern if (err < 0 && err != -EOPNOTSUPP) { 4870f6c5775fSDavid Ahern if (likely(skb->len)) 4871e5a55a89SJohn Fastabend break; 4872f6c5775fSDavid Ahern 4873f6c5775fSDavid Ahern goto out_err; 4874f6c5775fSDavid Ahern } 4875d64f69b0SRoopa Prabhu } 4876e5a55a89SJohn Fastabend idx++; 4877e5a55a89SJohn Fastabend } 4878e5a55a89SJohn Fastabend } 4879f6c5775fSDavid Ahern err = skb->len; 4880f6c5775fSDavid Ahern out_err: 4881e5a55a89SJohn Fastabend rcu_read_unlock(); 4882e5a55a89SJohn Fastabend cb->args[0] = idx; 4883e5a55a89SJohn Fastabend 4884f6c5775fSDavid Ahern return err; 4885e5a55a89SJohn Fastabend } 4886e5a55a89SJohn Fastabend 48872469ffd7SJohn Fastabend static inline size_t bridge_nlmsg_size(void) 48882469ffd7SJohn Fastabend { 48892469ffd7SJohn Fastabend return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 48902469ffd7SJohn Fastabend + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 48912469ffd7SJohn Fastabend + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 48922469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_MASTER */ 48932469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_MTU */ 48942469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_LINK */ 48952469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */ 48962469ffd7SJohn Fastabend + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */ 48972469ffd7SJohn Fastabend + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */ 48982469ffd7SJohn Fastabend + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */ 48992469ffd7SJohn Fastabend + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */ 49002469ffd7SJohn Fastabend } 49012469ffd7SJohn Fastabend 490202dba438SRoopa Prabhu static int rtnl_bridge_notify(struct net_device *dev) 49032469ffd7SJohn Fastabend { 49042469ffd7SJohn Fastabend struct net *net = dev_net(dev); 49052469ffd7SJohn Fastabend struct sk_buff *skb; 49062469ffd7SJohn Fastabend int err = -EOPNOTSUPP; 49072469ffd7SJohn Fastabend 490802dba438SRoopa Prabhu if (!dev->netdev_ops->ndo_bridge_getlink) 490902dba438SRoopa Prabhu return 0; 491002dba438SRoopa Prabhu 49112469ffd7SJohn Fastabend skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC); 49122469ffd7SJohn Fastabend if (!skb) { 49132469ffd7SJohn Fastabend err = -ENOMEM; 49142469ffd7SJohn Fastabend goto errout; 49152469ffd7SJohn Fastabend } 49162469ffd7SJohn Fastabend 491746c264daSNicolas Dichtel err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0); 4918c38e01b8SJohn Fastabend if (err < 0) 4919c38e01b8SJohn Fastabend goto errout; 49202469ffd7SJohn Fastabend 4921d2e381c4SIdo Schimmel /* Notification info is only filled for bridge ports, not the bridge 4922d2e381c4SIdo Schimmel * device itself. Therefore, a zero notification length is valid and 4923d2e381c4SIdo Schimmel * should not result in an error. 4924d2e381c4SIdo Schimmel */ 4925d2e381c4SIdo Schimmel if (!skb->len) 492659ccaaaaSRoopa Prabhu goto errout; 492759ccaaaaSRoopa Prabhu 49282469ffd7SJohn Fastabend rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC); 49292469ffd7SJohn Fastabend return 0; 49302469ffd7SJohn Fastabend errout: 49312469ffd7SJohn Fastabend WARN_ON(err == -EMSGSIZE); 49322469ffd7SJohn Fastabend kfree_skb(skb); 493359ccaaaaSRoopa Prabhu if (err) 49342469ffd7SJohn Fastabend rtnl_set_sk_err(net, RTNLGRP_LINK, err); 49352469ffd7SJohn Fastabend return err; 49362469ffd7SJohn Fastabend } 49372469ffd7SJohn Fastabend 4938c21ef3e3SDavid Ahern static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, 4939c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 4940e5a55a89SJohn Fastabend { 4941e5a55a89SJohn Fastabend struct net *net = sock_net(skb->sk); 4942e5a55a89SJohn Fastabend struct ifinfomsg *ifm; 4943e5a55a89SJohn Fastabend struct net_device *dev; 49442469ffd7SJohn Fastabend struct nlattr *br_spec, *attr = NULL; 49452469ffd7SJohn Fastabend int rem, err = -EOPNOTSUPP; 49464de8b413SRosen, Rami u16 flags = 0; 4947c38e01b8SJohn Fastabend bool have_flags = false; 4948e5a55a89SJohn Fastabend 4949e5a55a89SJohn Fastabend if (nlmsg_len(nlh) < sizeof(*ifm)) 4950e5a55a89SJohn Fastabend return -EINVAL; 4951e5a55a89SJohn Fastabend 4952e5a55a89SJohn Fastabend ifm = nlmsg_data(nlh); 4953e5a55a89SJohn Fastabend if (ifm->ifi_family != AF_BRIDGE) 4954e5a55a89SJohn Fastabend return -EPFNOSUPPORT; 4955e5a55a89SJohn Fastabend 4956e5a55a89SJohn Fastabend dev = __dev_get_by_index(net, ifm->ifi_index); 4957e5a55a89SJohn Fastabend if (!dev) { 4958b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 4959e5a55a89SJohn Fastabend return -ENODEV; 4960e5a55a89SJohn Fastabend } 4961e5a55a89SJohn Fastabend 49622469ffd7SJohn Fastabend br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 49632469ffd7SJohn Fastabend if (br_spec) { 49642469ffd7SJohn Fastabend nla_for_each_nested(attr, br_spec, rem) { 49652469ffd7SJohn Fastabend if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { 49666e8d1c55SThomas Graf if (nla_len(attr) < sizeof(flags)) 49676e8d1c55SThomas Graf return -EINVAL; 49686e8d1c55SThomas Graf 4969c38e01b8SJohn Fastabend have_flags = true; 49702469ffd7SJohn Fastabend flags = nla_get_u16(attr); 49712469ffd7SJohn Fastabend break; 49722469ffd7SJohn Fastabend } 49732469ffd7SJohn Fastabend } 49742469ffd7SJohn Fastabend } 49752469ffd7SJohn Fastabend 49762469ffd7SJohn Fastabend if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { 4977898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4978898e5061SJiri Pirko 4979898e5061SJiri Pirko if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) { 49802469ffd7SJohn Fastabend err = -EOPNOTSUPP; 4981e5a55a89SJohn Fastabend goto out; 4982e5a55a89SJohn Fastabend } 4983e5a55a89SJohn Fastabend 49842fd527b7SPetr Machata err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags, 49852fd527b7SPetr Machata extack); 49862469ffd7SJohn Fastabend if (err) 49872469ffd7SJohn Fastabend goto out; 49882469ffd7SJohn Fastabend 49892469ffd7SJohn Fastabend flags &= ~BRIDGE_FLAGS_MASTER; 49902469ffd7SJohn Fastabend } 49912469ffd7SJohn Fastabend 49922469ffd7SJohn Fastabend if ((flags & BRIDGE_FLAGS_SELF)) { 49932469ffd7SJohn Fastabend if (!dev->netdev_ops->ndo_bridge_setlink) 49942469ffd7SJohn Fastabend err = -EOPNOTSUPP; 49952469ffd7SJohn Fastabend else 4996add511b3SRoopa Prabhu err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh, 49972fd527b7SPetr Machata flags, 49982fd527b7SPetr Machata extack); 499902dba438SRoopa Prabhu if (!err) { 50002469ffd7SJohn Fastabend flags &= ~BRIDGE_FLAGS_SELF; 500102dba438SRoopa Prabhu 500202dba438SRoopa Prabhu /* Generate event to notify upper layer of bridge 500302dba438SRoopa Prabhu * change 500402dba438SRoopa Prabhu */ 500502dba438SRoopa Prabhu err = rtnl_bridge_notify(dev); 500602dba438SRoopa Prabhu } 50072469ffd7SJohn Fastabend } 50082469ffd7SJohn Fastabend 5009c38e01b8SJohn Fastabend if (have_flags) 50102469ffd7SJohn Fastabend memcpy(nla_data(attr), &flags, sizeof(flags)); 5011e5a55a89SJohn Fastabend out: 5012e5a55a89SJohn Fastabend return err; 5013e5a55a89SJohn Fastabend } 5014e5a55a89SJohn Fastabend 5015c21ef3e3SDavid Ahern static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, 5016c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 5017407af329SVlad Yasevich { 5018407af329SVlad Yasevich struct net *net = sock_net(skb->sk); 5019407af329SVlad Yasevich struct ifinfomsg *ifm; 5020407af329SVlad Yasevich struct net_device *dev; 5021407af329SVlad Yasevich struct nlattr *br_spec, *attr = NULL; 5022407af329SVlad Yasevich int rem, err = -EOPNOTSUPP; 50234de8b413SRosen, Rami u16 flags = 0; 5024407af329SVlad Yasevich bool have_flags = false; 5025407af329SVlad Yasevich 5026407af329SVlad Yasevich if (nlmsg_len(nlh) < sizeof(*ifm)) 5027407af329SVlad Yasevich return -EINVAL; 5028407af329SVlad Yasevich 5029407af329SVlad Yasevich ifm = nlmsg_data(nlh); 5030407af329SVlad Yasevich if (ifm->ifi_family != AF_BRIDGE) 5031407af329SVlad Yasevich return -EPFNOSUPPORT; 5032407af329SVlad Yasevich 5033407af329SVlad Yasevich dev = __dev_get_by_index(net, ifm->ifi_index); 5034407af329SVlad Yasevich if (!dev) { 5035b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 5036407af329SVlad Yasevich return -ENODEV; 5037407af329SVlad Yasevich } 5038407af329SVlad Yasevich 5039407af329SVlad Yasevich br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 5040407af329SVlad Yasevich if (br_spec) { 5041407af329SVlad Yasevich nla_for_each_nested(attr, br_spec, rem) { 5042407af329SVlad Yasevich if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { 50436e8d1c55SThomas Graf if (nla_len(attr) < sizeof(flags)) 50446e8d1c55SThomas Graf return -EINVAL; 50456e8d1c55SThomas Graf 5046407af329SVlad Yasevich have_flags = true; 5047407af329SVlad Yasevich flags = nla_get_u16(attr); 5048407af329SVlad Yasevich break; 5049407af329SVlad Yasevich } 5050407af329SVlad Yasevich } 5051407af329SVlad Yasevich } 5052407af329SVlad Yasevich 5053407af329SVlad Yasevich if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { 5054407af329SVlad Yasevich struct net_device *br_dev = netdev_master_upper_dev_get(dev); 5055407af329SVlad Yasevich 5056407af329SVlad Yasevich if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) { 5057407af329SVlad Yasevich err = -EOPNOTSUPP; 5058407af329SVlad Yasevich goto out; 5059407af329SVlad Yasevich } 5060407af329SVlad Yasevich 5061add511b3SRoopa Prabhu err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags); 5062407af329SVlad Yasevich if (err) 5063407af329SVlad Yasevich goto out; 5064407af329SVlad Yasevich 5065407af329SVlad Yasevich flags &= ~BRIDGE_FLAGS_MASTER; 5066407af329SVlad Yasevich } 5067407af329SVlad Yasevich 5068407af329SVlad Yasevich if ((flags & BRIDGE_FLAGS_SELF)) { 5069407af329SVlad Yasevich if (!dev->netdev_ops->ndo_bridge_dellink) 5070407af329SVlad Yasevich err = -EOPNOTSUPP; 5071407af329SVlad Yasevich else 5072add511b3SRoopa Prabhu err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh, 5073add511b3SRoopa Prabhu flags); 5074407af329SVlad Yasevich 507502dba438SRoopa Prabhu if (!err) { 5076407af329SVlad Yasevich flags &= ~BRIDGE_FLAGS_SELF; 507702dba438SRoopa Prabhu 507802dba438SRoopa Prabhu /* Generate event to notify upper layer of bridge 507902dba438SRoopa Prabhu * change 508002dba438SRoopa Prabhu */ 508102dba438SRoopa Prabhu err = rtnl_bridge_notify(dev); 508202dba438SRoopa Prabhu } 5083407af329SVlad Yasevich } 5084407af329SVlad Yasevich 5085407af329SVlad Yasevich if (have_flags) 5086407af329SVlad Yasevich memcpy(nla_data(attr), &flags, sizeof(flags)); 5087407af329SVlad Yasevich out: 5088407af329SVlad Yasevich return err; 5089407af329SVlad Yasevich } 5090407af329SVlad Yasevich 5091e8872a25SNikolay Aleksandrov static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr) 5092e8872a25SNikolay Aleksandrov { 5093e8872a25SNikolay Aleksandrov return (mask & IFLA_STATS_FILTER_BIT(attrid)) && 5094e8872a25SNikolay Aleksandrov (!idxattr || idxattr == attrid); 5095e8872a25SNikolay Aleksandrov } 5096e8872a25SNikolay Aleksandrov 5097f6e0fb81SPetr Machata static bool 5098f6e0fb81SPetr Machata rtnl_offload_xstats_have_ndo(const struct net_device *dev, int attr_id) 509969ae6ad2SNogah Frankel { 5100f6e0fb81SPetr Machata return dev->netdev_ops && 5101f6e0fb81SPetr Machata dev->netdev_ops->ndo_has_offload_stats && 5102f6e0fb81SPetr Machata dev->netdev_ops->ndo_get_offload_stats && 5103f6e0fb81SPetr Machata dev->netdev_ops->ndo_has_offload_stats(dev, attr_id); 510469ae6ad2SNogah Frankel } 510569ae6ad2SNogah Frankel 5106f6e0fb81SPetr Machata static unsigned int 5107f6e0fb81SPetr Machata rtnl_offload_xstats_get_size_ndo(const struct net_device *dev, int attr_id) 5108f6e0fb81SPetr Machata { 5109f6e0fb81SPetr Machata return rtnl_offload_xstats_have_ndo(dev, attr_id) ? 5110f6e0fb81SPetr Machata sizeof(struct rtnl_link_stats64) : 0; 5111f6e0fb81SPetr Machata } 5112f6e0fb81SPetr Machata 5113f6e0fb81SPetr Machata static int 5114f6e0fb81SPetr Machata rtnl_offload_xstats_fill_ndo(struct net_device *dev, int attr_id, 5115f6e0fb81SPetr Machata struct sk_buff *skb) 5116f6e0fb81SPetr Machata { 5117f6e0fb81SPetr Machata unsigned int size = rtnl_offload_xstats_get_size_ndo(dev, attr_id); 5118f6e0fb81SPetr Machata struct nlattr *attr = NULL; 5119f6e0fb81SPetr Machata void *attr_data; 5120f6e0fb81SPetr Machata int err; 5121f6e0fb81SPetr Machata 5122f6e0fb81SPetr Machata if (!size) 5123f6e0fb81SPetr Machata return -ENODATA; 5124f6e0fb81SPetr Machata 5125f6e0fb81SPetr Machata attr = nla_reserve_64bit(skb, attr_id, size, 5126f6e0fb81SPetr Machata IFLA_OFFLOAD_XSTATS_UNSPEC); 5127f6e0fb81SPetr Machata if (!attr) 5128f6e0fb81SPetr Machata return -EMSGSIZE; 5129f6e0fb81SPetr Machata 5130f6e0fb81SPetr Machata attr_data = nla_data(attr); 5131f6e0fb81SPetr Machata memset(attr_data, 0, size); 5132f6e0fb81SPetr Machata 5133f6e0fb81SPetr Machata err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev, attr_data); 5134f6e0fb81SPetr Machata if (err) 5135f6e0fb81SPetr Machata return err; 5136f6e0fb81SPetr Machata 513769ae6ad2SNogah Frankel return 0; 513869ae6ad2SNogah Frankel } 513969ae6ad2SNogah Frankel 51400e7788fdSPetr Machata static unsigned int 51410e7788fdSPetr Machata rtnl_offload_xstats_get_size_stats(const struct net_device *dev, 51420e7788fdSPetr Machata enum netdev_offload_xstats_type type) 51430e7788fdSPetr Machata { 51440e7788fdSPetr Machata bool enabled = netdev_offload_xstats_enabled(dev, type); 51450e7788fdSPetr Machata 51460e7788fdSPetr Machata return enabled ? sizeof(struct rtnl_hw_stats64) : 0; 51470e7788fdSPetr Machata } 51480e7788fdSPetr Machata 51490e7788fdSPetr Machata struct rtnl_offload_xstats_request_used { 51500e7788fdSPetr Machata bool request; 51510e7788fdSPetr Machata bool used; 51520e7788fdSPetr Machata }; 51530e7788fdSPetr Machata 51540e7788fdSPetr Machata static int 51550e7788fdSPetr Machata rtnl_offload_xstats_get_stats(struct net_device *dev, 51560e7788fdSPetr Machata enum netdev_offload_xstats_type type, 51570e7788fdSPetr Machata struct rtnl_offload_xstats_request_used *ru, 51580e7788fdSPetr Machata struct rtnl_hw_stats64 *stats, 51590e7788fdSPetr Machata struct netlink_ext_ack *extack) 51600e7788fdSPetr Machata { 51610e7788fdSPetr Machata bool request; 51620e7788fdSPetr Machata bool used; 51630e7788fdSPetr Machata int err; 51640e7788fdSPetr Machata 51650e7788fdSPetr Machata request = netdev_offload_xstats_enabled(dev, type); 51660e7788fdSPetr Machata if (!request) { 51670e7788fdSPetr Machata used = false; 51680e7788fdSPetr Machata goto out; 51690e7788fdSPetr Machata } 51700e7788fdSPetr Machata 51710e7788fdSPetr Machata err = netdev_offload_xstats_get(dev, type, stats, &used, extack); 51720e7788fdSPetr Machata if (err) 51730e7788fdSPetr Machata return err; 51740e7788fdSPetr Machata 51750e7788fdSPetr Machata out: 51760e7788fdSPetr Machata if (ru) { 51770e7788fdSPetr Machata ru->request = request; 51780e7788fdSPetr Machata ru->used = used; 51790e7788fdSPetr Machata } 51800e7788fdSPetr Machata return 0; 51810e7788fdSPetr Machata } 51820e7788fdSPetr Machata 51830e7788fdSPetr Machata static int 51840e7788fdSPetr Machata rtnl_offload_xstats_fill_hw_s_info_one(struct sk_buff *skb, int attr_id, 51850e7788fdSPetr Machata struct rtnl_offload_xstats_request_used *ru) 51860e7788fdSPetr Machata { 51870e7788fdSPetr Machata struct nlattr *nest; 51880e7788fdSPetr Machata 51890e7788fdSPetr Machata nest = nla_nest_start(skb, attr_id); 51900e7788fdSPetr Machata if (!nest) 51910e7788fdSPetr Machata return -EMSGSIZE; 51920e7788fdSPetr Machata 51930e7788fdSPetr Machata if (nla_put_u8(skb, IFLA_OFFLOAD_XSTATS_HW_S_INFO_REQUEST, ru->request)) 51940e7788fdSPetr Machata goto nla_put_failure; 51950e7788fdSPetr Machata 51960e7788fdSPetr Machata if (nla_put_u8(skb, IFLA_OFFLOAD_XSTATS_HW_S_INFO_USED, ru->used)) 51970e7788fdSPetr Machata goto nla_put_failure; 51980e7788fdSPetr Machata 51990e7788fdSPetr Machata nla_nest_end(skb, nest); 52000e7788fdSPetr Machata return 0; 52010e7788fdSPetr Machata 52020e7788fdSPetr Machata nla_put_failure: 52030e7788fdSPetr Machata nla_nest_cancel(skb, nest); 52040e7788fdSPetr Machata return -EMSGSIZE; 52050e7788fdSPetr Machata } 52060e7788fdSPetr Machata 52070e7788fdSPetr Machata static int 52080e7788fdSPetr Machata rtnl_offload_xstats_fill_hw_s_info(struct sk_buff *skb, struct net_device *dev, 52090e7788fdSPetr Machata struct netlink_ext_ack *extack) 52100e7788fdSPetr Machata { 52110e7788fdSPetr Machata enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3; 52120e7788fdSPetr Machata struct rtnl_offload_xstats_request_used ru_l3; 52130e7788fdSPetr Machata struct nlattr *nest; 52140e7788fdSPetr Machata int err; 52150e7788fdSPetr Machata 52160e7788fdSPetr Machata err = rtnl_offload_xstats_get_stats(dev, t_l3, &ru_l3, NULL, extack); 52170e7788fdSPetr Machata if (err) 52180e7788fdSPetr Machata return err; 52190e7788fdSPetr Machata 52200e7788fdSPetr Machata nest = nla_nest_start(skb, IFLA_OFFLOAD_XSTATS_HW_S_INFO); 52210e7788fdSPetr Machata if (!nest) 52220e7788fdSPetr Machata return -EMSGSIZE; 52230e7788fdSPetr Machata 52240e7788fdSPetr Machata if (rtnl_offload_xstats_fill_hw_s_info_one(skb, 52250e7788fdSPetr Machata IFLA_OFFLOAD_XSTATS_L3_STATS, 52260e7788fdSPetr Machata &ru_l3)) 52270e7788fdSPetr Machata goto nla_put_failure; 52280e7788fdSPetr Machata 52290e7788fdSPetr Machata nla_nest_end(skb, nest); 52300e7788fdSPetr Machata return 0; 52310e7788fdSPetr Machata 52320e7788fdSPetr Machata nla_put_failure: 52330e7788fdSPetr Machata nla_nest_cancel(skb, nest); 52340e7788fdSPetr Machata return -EMSGSIZE; 52350e7788fdSPetr Machata } 52360e7788fdSPetr Machata 52376b524a1dSPetr Machata static int rtnl_offload_xstats_fill(struct sk_buff *skb, struct net_device *dev, 523805415bccSPetr Machata int *prividx, u32 off_filter_mask, 523905415bccSPetr Machata struct netlink_ext_ack *extack) 524069ae6ad2SNogah Frankel { 52410e7788fdSPetr Machata enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3; 52420e7788fdSPetr Machata int attr_id_hw_s_info = IFLA_OFFLOAD_XSTATS_HW_S_INFO; 52430e7788fdSPetr Machata int attr_id_l3_stats = IFLA_OFFLOAD_XSTATS_L3_STATS; 5244f6e0fb81SPetr Machata int attr_id_cpu_hit = IFLA_OFFLOAD_XSTATS_CPU_HIT; 5245f6e0fb81SPetr Machata bool have_data = false; 524669ae6ad2SNogah Frankel int err; 524769ae6ad2SNogah Frankel 524846efc97bSPetr Machata if (*prividx <= attr_id_cpu_hit && 524946efc97bSPetr Machata (off_filter_mask & 525046efc97bSPetr Machata IFLA_STATS_FILTER_BIT(attr_id_cpu_hit))) { 5251f6e0fb81SPetr Machata err = rtnl_offload_xstats_fill_ndo(dev, attr_id_cpu_hit, skb); 5252f6e0fb81SPetr Machata if (!err) { 5253f6e0fb81SPetr Machata have_data = true; 5254f6e0fb81SPetr Machata } else if (err != -ENODATA) { 5255f6e0fb81SPetr Machata *prividx = attr_id_cpu_hit; 5256f6e0fb81SPetr Machata return err; 5257f6e0fb81SPetr Machata } 525869ae6ad2SNogah Frankel } 525969ae6ad2SNogah Frankel 52600e7788fdSPetr Machata if (*prividx <= attr_id_hw_s_info && 52610e7788fdSPetr Machata (off_filter_mask & IFLA_STATS_FILTER_BIT(attr_id_hw_s_info))) { 52620e7788fdSPetr Machata *prividx = attr_id_hw_s_info; 52630e7788fdSPetr Machata 52640e7788fdSPetr Machata err = rtnl_offload_xstats_fill_hw_s_info(skb, dev, extack); 52650e7788fdSPetr Machata if (err) 52660e7788fdSPetr Machata return err; 52670e7788fdSPetr Machata 52680e7788fdSPetr Machata have_data = true; 52690e7788fdSPetr Machata *prividx = 0; 52700e7788fdSPetr Machata } 52710e7788fdSPetr Machata 52720e7788fdSPetr Machata if (*prividx <= attr_id_l3_stats && 52730e7788fdSPetr Machata (off_filter_mask & IFLA_STATS_FILTER_BIT(attr_id_l3_stats))) { 52740e7788fdSPetr Machata unsigned int size_l3; 52750e7788fdSPetr Machata struct nlattr *attr; 52760e7788fdSPetr Machata 52770e7788fdSPetr Machata *prividx = attr_id_l3_stats; 52780e7788fdSPetr Machata 52790e7788fdSPetr Machata size_l3 = rtnl_offload_xstats_get_size_stats(dev, t_l3); 52800e7788fdSPetr Machata attr = nla_reserve_64bit(skb, attr_id_l3_stats, size_l3, 52810e7788fdSPetr Machata IFLA_OFFLOAD_XSTATS_UNSPEC); 52820e7788fdSPetr Machata if (!attr) 52830e7788fdSPetr Machata return -EMSGSIZE; 52840e7788fdSPetr Machata 52850e7788fdSPetr Machata err = rtnl_offload_xstats_get_stats(dev, t_l3, NULL, 52860e7788fdSPetr Machata nla_data(attr), extack); 52870e7788fdSPetr Machata if (err) 52880e7788fdSPetr Machata return err; 52890e7788fdSPetr Machata 52900e7788fdSPetr Machata have_data = true; 52910e7788fdSPetr Machata *prividx = 0; 52920e7788fdSPetr Machata } 52930e7788fdSPetr Machata 5294f6e0fb81SPetr Machata if (!have_data) 529569ae6ad2SNogah Frankel return -ENODATA; 529669ae6ad2SNogah Frankel 529769ae6ad2SNogah Frankel *prividx = 0; 529869ae6ad2SNogah Frankel return 0; 529969ae6ad2SNogah Frankel } 530069ae6ad2SNogah Frankel 53010e7788fdSPetr Machata static unsigned int 53020e7788fdSPetr Machata rtnl_offload_xstats_get_size_hw_s_info_one(const struct net_device *dev, 53030e7788fdSPetr Machata enum netdev_offload_xstats_type type) 53040e7788fdSPetr Machata { 53050e7788fdSPetr Machata bool enabled = netdev_offload_xstats_enabled(dev, type); 53060e7788fdSPetr Machata 53070e7788fdSPetr Machata return nla_total_size(0) + 53080e7788fdSPetr Machata /* IFLA_OFFLOAD_XSTATS_HW_S_INFO_REQUEST */ 53090e7788fdSPetr Machata nla_total_size(sizeof(u8)) + 53100e7788fdSPetr Machata /* IFLA_OFFLOAD_XSTATS_HW_S_INFO_USED */ 53110e7788fdSPetr Machata (enabled ? nla_total_size(sizeof(u8)) : 0) + 53120e7788fdSPetr Machata 0; 53130e7788fdSPetr Machata } 53140e7788fdSPetr Machata 53150e7788fdSPetr Machata static unsigned int 53160e7788fdSPetr Machata rtnl_offload_xstats_get_size_hw_s_info(const struct net_device *dev) 53170e7788fdSPetr Machata { 53180e7788fdSPetr Machata enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3; 53190e7788fdSPetr Machata 53200e7788fdSPetr Machata return nla_total_size(0) + 53210e7788fdSPetr Machata /* IFLA_OFFLOAD_XSTATS_L3_STATS */ 53220e7788fdSPetr Machata rtnl_offload_xstats_get_size_hw_s_info_one(dev, t_l3) + 53230e7788fdSPetr Machata 0; 53240e7788fdSPetr Machata } 53250e7788fdSPetr Machata 532646efc97bSPetr Machata static int rtnl_offload_xstats_get_size(const struct net_device *dev, 532746efc97bSPetr Machata u32 off_filter_mask) 532869ae6ad2SNogah Frankel { 53290e7788fdSPetr Machata enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3; 5330f6e0fb81SPetr Machata int attr_id_cpu_hit = IFLA_OFFLOAD_XSTATS_CPU_HIT; 533169ae6ad2SNogah Frankel int nla_size = 0; 533269ae6ad2SNogah Frankel int size; 533369ae6ad2SNogah Frankel 533446efc97bSPetr Machata if (off_filter_mask & 533546efc97bSPetr Machata IFLA_STATS_FILTER_BIT(attr_id_cpu_hit)) { 5336f6e0fb81SPetr Machata size = rtnl_offload_xstats_get_size_ndo(dev, attr_id_cpu_hit); 533769ae6ad2SNogah Frankel nla_size += nla_total_size_64bit(size); 533846efc97bSPetr Machata } 533969ae6ad2SNogah Frankel 53400e7788fdSPetr Machata if (off_filter_mask & 53410e7788fdSPetr Machata IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_HW_S_INFO)) 53420e7788fdSPetr Machata nla_size += rtnl_offload_xstats_get_size_hw_s_info(dev); 53430e7788fdSPetr Machata 53440e7788fdSPetr Machata if (off_filter_mask & 53450e7788fdSPetr Machata IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_L3_STATS)) { 53460e7788fdSPetr Machata size = rtnl_offload_xstats_get_size_stats(dev, t_l3); 53470e7788fdSPetr Machata nla_size += nla_total_size_64bit(size); 53480e7788fdSPetr Machata } 53490e7788fdSPetr Machata 535069ae6ad2SNogah Frankel if (nla_size != 0) 535169ae6ad2SNogah Frankel nla_size += nla_total_size(0); 535269ae6ad2SNogah Frankel 535369ae6ad2SNogah Frankel return nla_size; 535469ae6ad2SNogah Frankel } 535569ae6ad2SNogah Frankel 535646efc97bSPetr Machata struct rtnl_stats_dump_filters { 535746efc97bSPetr Machata /* mask[0] filters outer attributes. Then individual nests have their 535846efc97bSPetr Machata * filtering mask at the index of the nested attribute. 535946efc97bSPetr Machata */ 536046efc97bSPetr Machata u32 mask[IFLA_STATS_MAX + 1]; 536146efc97bSPetr Machata }; 536246efc97bSPetr Machata 536310c9ead9SRoopa Prabhu static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev, 536410c9ead9SRoopa Prabhu int type, u32 pid, u32 seq, u32 change, 536546efc97bSPetr Machata unsigned int flags, 536646efc97bSPetr Machata const struct rtnl_stats_dump_filters *filters, 536705415bccSPetr Machata int *idxattr, int *prividx, 536805415bccSPetr Machata struct netlink_ext_ack *extack) 536910c9ead9SRoopa Prabhu { 537046efc97bSPetr Machata unsigned int filter_mask = filters->mask[0]; 537110c9ead9SRoopa Prabhu struct if_stats_msg *ifsm; 537210c9ead9SRoopa Prabhu struct nlmsghdr *nlh; 537310c9ead9SRoopa Prabhu struct nlattr *attr; 5374e8872a25SNikolay Aleksandrov int s_prividx = *prividx; 537569ae6ad2SNogah Frankel int err; 537610c9ead9SRoopa Prabhu 537710c9ead9SRoopa Prabhu ASSERT_RTNL(); 537810c9ead9SRoopa Prabhu 537910c9ead9SRoopa Prabhu nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags); 538010c9ead9SRoopa Prabhu if (!nlh) 538110c9ead9SRoopa Prabhu return -EMSGSIZE; 538210c9ead9SRoopa Prabhu 538310c9ead9SRoopa Prabhu ifsm = nlmsg_data(nlh); 5384ce024f42SNikolay Aleksandrov ifsm->family = PF_UNSPEC; 5385ce024f42SNikolay Aleksandrov ifsm->pad1 = 0; 5386ce024f42SNikolay Aleksandrov ifsm->pad2 = 0; 538710c9ead9SRoopa Prabhu ifsm->ifindex = dev->ifindex; 538810c9ead9SRoopa Prabhu ifsm->filter_mask = filter_mask; 538910c9ead9SRoopa Prabhu 5390e8872a25SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) { 539110c9ead9SRoopa Prabhu struct rtnl_link_stats64 *sp; 539210c9ead9SRoopa Prabhu 539358414d32SNicolas Dichtel attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64, 539458414d32SNicolas Dichtel sizeof(struct rtnl_link_stats64), 539558414d32SNicolas Dichtel IFLA_STATS_UNSPEC); 5396216e6906SPetr Machata if (!attr) { 5397216e6906SPetr Machata err = -EMSGSIZE; 539810c9ead9SRoopa Prabhu goto nla_put_failure; 5399216e6906SPetr Machata } 540010c9ead9SRoopa Prabhu 540110c9ead9SRoopa Prabhu sp = nla_data(attr); 540210c9ead9SRoopa Prabhu dev_get_stats(dev, sp); 540310c9ead9SRoopa Prabhu } 540410c9ead9SRoopa Prabhu 540597a47facSNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) { 540697a47facSNikolay Aleksandrov const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 540797a47facSNikolay Aleksandrov 540897a47facSNikolay Aleksandrov if (ops && ops->fill_linkxstats) { 540997a47facSNikolay Aleksandrov *idxattr = IFLA_STATS_LINK_XSTATS; 5410ae0be8deSMichal Kubecek attr = nla_nest_start_noflag(skb, 541197a47facSNikolay Aleksandrov IFLA_STATS_LINK_XSTATS); 5412216e6906SPetr Machata if (!attr) { 5413216e6906SPetr Machata err = -EMSGSIZE; 541497a47facSNikolay Aleksandrov goto nla_put_failure; 5415216e6906SPetr Machata } 541697a47facSNikolay Aleksandrov 541780e73cc5SNikolay Aleksandrov err = ops->fill_linkxstats(skb, dev, prividx, *idxattr); 541880e73cc5SNikolay Aleksandrov nla_nest_end(skb, attr); 541980e73cc5SNikolay Aleksandrov if (err) 542080e73cc5SNikolay Aleksandrov goto nla_put_failure; 542180e73cc5SNikolay Aleksandrov *idxattr = 0; 542280e73cc5SNikolay Aleksandrov } 542380e73cc5SNikolay Aleksandrov } 542480e73cc5SNikolay Aleksandrov 542580e73cc5SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 542680e73cc5SNikolay Aleksandrov *idxattr)) { 542780e73cc5SNikolay Aleksandrov const struct rtnl_link_ops *ops = NULL; 542880e73cc5SNikolay Aleksandrov const struct net_device *master; 542980e73cc5SNikolay Aleksandrov 543080e73cc5SNikolay Aleksandrov master = netdev_master_upper_dev_get(dev); 543180e73cc5SNikolay Aleksandrov if (master) 543280e73cc5SNikolay Aleksandrov ops = master->rtnl_link_ops; 543380e73cc5SNikolay Aleksandrov if (ops && ops->fill_linkxstats) { 543480e73cc5SNikolay Aleksandrov *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE; 5435ae0be8deSMichal Kubecek attr = nla_nest_start_noflag(skb, 543680e73cc5SNikolay Aleksandrov IFLA_STATS_LINK_XSTATS_SLAVE); 5437216e6906SPetr Machata if (!attr) { 5438216e6906SPetr Machata err = -EMSGSIZE; 543980e73cc5SNikolay Aleksandrov goto nla_put_failure; 5440216e6906SPetr Machata } 544180e73cc5SNikolay Aleksandrov 544280e73cc5SNikolay Aleksandrov err = ops->fill_linkxstats(skb, dev, prividx, *idxattr); 544397a47facSNikolay Aleksandrov nla_nest_end(skb, attr); 544497a47facSNikolay Aleksandrov if (err) 544597a47facSNikolay Aleksandrov goto nla_put_failure; 544697a47facSNikolay Aleksandrov *idxattr = 0; 544797a47facSNikolay Aleksandrov } 544897a47facSNikolay Aleksandrov } 544997a47facSNikolay Aleksandrov 545069ae6ad2SNogah Frankel if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 545169ae6ad2SNogah Frankel *idxattr)) { 545246efc97bSPetr Machata u32 off_filter_mask; 545346efc97bSPetr Machata 545446efc97bSPetr Machata off_filter_mask = filters->mask[IFLA_STATS_LINK_OFFLOAD_XSTATS]; 545569ae6ad2SNogah Frankel *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS; 5456ae0be8deSMichal Kubecek attr = nla_nest_start_noflag(skb, 5457ae0be8deSMichal Kubecek IFLA_STATS_LINK_OFFLOAD_XSTATS); 5458216e6906SPetr Machata if (!attr) { 5459216e6906SPetr Machata err = -EMSGSIZE; 546069ae6ad2SNogah Frankel goto nla_put_failure; 5461216e6906SPetr Machata } 546269ae6ad2SNogah Frankel 546346efc97bSPetr Machata err = rtnl_offload_xstats_fill(skb, dev, prividx, 546405415bccSPetr Machata off_filter_mask, extack); 546569ae6ad2SNogah Frankel if (err == -ENODATA) 546669ae6ad2SNogah Frankel nla_nest_cancel(skb, attr); 546769ae6ad2SNogah Frankel else 546869ae6ad2SNogah Frankel nla_nest_end(skb, attr); 546969ae6ad2SNogah Frankel 547069ae6ad2SNogah Frankel if (err && err != -ENODATA) 547169ae6ad2SNogah Frankel goto nla_put_failure; 547269ae6ad2SNogah Frankel *idxattr = 0; 547369ae6ad2SNogah Frankel } 547469ae6ad2SNogah Frankel 5475aefb4d4aSRobert Shearman if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) { 5476aefb4d4aSRobert Shearman struct rtnl_af_ops *af_ops; 5477aefb4d4aSRobert Shearman 5478aefb4d4aSRobert Shearman *idxattr = IFLA_STATS_AF_SPEC; 5479ae0be8deSMichal Kubecek attr = nla_nest_start_noflag(skb, IFLA_STATS_AF_SPEC); 5480216e6906SPetr Machata if (!attr) { 5481216e6906SPetr Machata err = -EMSGSIZE; 5482aefb4d4aSRobert Shearman goto nla_put_failure; 5483216e6906SPetr Machata } 5484aefb4d4aSRobert Shearman 54855fa85a09SFlorian Westphal rcu_read_lock(); 54865fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 5487aefb4d4aSRobert Shearman if (af_ops->fill_stats_af) { 5488aefb4d4aSRobert Shearman struct nlattr *af; 5489aefb4d4aSRobert Shearman 5490ae0be8deSMichal Kubecek af = nla_nest_start_noflag(skb, 5491ae0be8deSMichal Kubecek af_ops->family); 54925fa85a09SFlorian Westphal if (!af) { 54935fa85a09SFlorian Westphal rcu_read_unlock(); 549457d29a29STom Rix err = -EMSGSIZE; 5495aefb4d4aSRobert Shearman goto nla_put_failure; 54965fa85a09SFlorian Westphal } 5497aefb4d4aSRobert Shearman err = af_ops->fill_stats_af(skb, dev); 5498aefb4d4aSRobert Shearman 54995fa85a09SFlorian Westphal if (err == -ENODATA) { 5500aefb4d4aSRobert Shearman nla_nest_cancel(skb, af); 55015fa85a09SFlorian Westphal } else if (err < 0) { 55025fa85a09SFlorian Westphal rcu_read_unlock(); 5503aefb4d4aSRobert Shearman goto nla_put_failure; 55045fa85a09SFlorian Westphal } 5505aefb4d4aSRobert Shearman 5506aefb4d4aSRobert Shearman nla_nest_end(skb, af); 5507aefb4d4aSRobert Shearman } 5508aefb4d4aSRobert Shearman } 55095fa85a09SFlorian Westphal rcu_read_unlock(); 5510aefb4d4aSRobert Shearman 5511aefb4d4aSRobert Shearman nla_nest_end(skb, attr); 5512aefb4d4aSRobert Shearman 5513aefb4d4aSRobert Shearman *idxattr = 0; 5514aefb4d4aSRobert Shearman } 5515aefb4d4aSRobert Shearman 551610c9ead9SRoopa Prabhu nlmsg_end(skb, nlh); 551710c9ead9SRoopa Prabhu 551810c9ead9SRoopa Prabhu return 0; 551910c9ead9SRoopa Prabhu 552010c9ead9SRoopa Prabhu nla_put_failure: 5521e8872a25SNikolay Aleksandrov /* not a multi message or no progress mean a real error */ 5522e8872a25SNikolay Aleksandrov if (!(flags & NLM_F_MULTI) || s_prividx == *prividx) 552310c9ead9SRoopa Prabhu nlmsg_cancel(skb, nlh); 5524e8872a25SNikolay Aleksandrov else 5525e8872a25SNikolay Aleksandrov nlmsg_end(skb, nlh); 552610c9ead9SRoopa Prabhu 5527216e6906SPetr Machata return err; 552810c9ead9SRoopa Prabhu } 552910c9ead9SRoopa Prabhu 553010c9ead9SRoopa Prabhu static size_t if_nlmsg_stats_size(const struct net_device *dev, 553146efc97bSPetr Machata const struct rtnl_stats_dump_filters *filters) 553210c9ead9SRoopa Prabhu { 5533d3436799SEric Dumazet size_t size = NLMSG_ALIGN(sizeof(struct if_stats_msg)); 553446efc97bSPetr Machata unsigned int filter_mask = filters->mask[0]; 553510c9ead9SRoopa Prabhu 5536e8872a25SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0)) 553710c9ead9SRoopa Prabhu size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64)); 553810c9ead9SRoopa Prabhu 553997a47facSNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) { 554097a47facSNikolay Aleksandrov const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 554180e73cc5SNikolay Aleksandrov int attr = IFLA_STATS_LINK_XSTATS; 554297a47facSNikolay Aleksandrov 554397a47facSNikolay Aleksandrov if (ops && ops->get_linkxstats_size) { 554480e73cc5SNikolay Aleksandrov size += nla_total_size(ops->get_linkxstats_size(dev, 554580e73cc5SNikolay Aleksandrov attr)); 554697a47facSNikolay Aleksandrov /* for IFLA_STATS_LINK_XSTATS */ 554797a47facSNikolay Aleksandrov size += nla_total_size(0); 554897a47facSNikolay Aleksandrov } 554997a47facSNikolay Aleksandrov } 555097a47facSNikolay Aleksandrov 555180e73cc5SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) { 555280e73cc5SNikolay Aleksandrov struct net_device *_dev = (struct net_device *)dev; 555380e73cc5SNikolay Aleksandrov const struct rtnl_link_ops *ops = NULL; 555480e73cc5SNikolay Aleksandrov const struct net_device *master; 555580e73cc5SNikolay Aleksandrov 555680e73cc5SNikolay Aleksandrov /* netdev_master_upper_dev_get can't take const */ 555780e73cc5SNikolay Aleksandrov master = netdev_master_upper_dev_get(_dev); 555880e73cc5SNikolay Aleksandrov if (master) 555980e73cc5SNikolay Aleksandrov ops = master->rtnl_link_ops; 556080e73cc5SNikolay Aleksandrov if (ops && ops->get_linkxstats_size) { 556180e73cc5SNikolay Aleksandrov int attr = IFLA_STATS_LINK_XSTATS_SLAVE; 556280e73cc5SNikolay Aleksandrov 556380e73cc5SNikolay Aleksandrov size += nla_total_size(ops->get_linkxstats_size(dev, 556480e73cc5SNikolay Aleksandrov attr)); 556580e73cc5SNikolay Aleksandrov /* for IFLA_STATS_LINK_XSTATS_SLAVE */ 556680e73cc5SNikolay Aleksandrov size += nla_total_size(0); 556780e73cc5SNikolay Aleksandrov } 556880e73cc5SNikolay Aleksandrov } 556980e73cc5SNikolay Aleksandrov 557046efc97bSPetr Machata if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0)) { 557146efc97bSPetr Machata u32 off_filter_mask; 557246efc97bSPetr Machata 557346efc97bSPetr Machata off_filter_mask = filters->mask[IFLA_STATS_LINK_OFFLOAD_XSTATS]; 557446efc97bSPetr Machata size += rtnl_offload_xstats_get_size(dev, off_filter_mask); 557546efc97bSPetr Machata } 557669ae6ad2SNogah Frankel 5577aefb4d4aSRobert Shearman if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) { 5578aefb4d4aSRobert Shearman struct rtnl_af_ops *af_ops; 5579aefb4d4aSRobert Shearman 5580aefb4d4aSRobert Shearman /* for IFLA_STATS_AF_SPEC */ 5581aefb4d4aSRobert Shearman size += nla_total_size(0); 5582aefb4d4aSRobert Shearman 55835fa85a09SFlorian Westphal rcu_read_lock(); 55845fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 5585aefb4d4aSRobert Shearman if (af_ops->get_stats_af_size) { 5586aefb4d4aSRobert Shearman size += nla_total_size( 5587aefb4d4aSRobert Shearman af_ops->get_stats_af_size(dev)); 5588aefb4d4aSRobert Shearman 5589aefb4d4aSRobert Shearman /* for AF_* */ 5590aefb4d4aSRobert Shearman size += nla_total_size(0); 5591aefb4d4aSRobert Shearman } 5592aefb4d4aSRobert Shearman } 55935fa85a09SFlorian Westphal rcu_read_unlock(); 5594aefb4d4aSRobert Shearman } 5595aefb4d4aSRobert Shearman 559610c9ead9SRoopa Prabhu return size; 559710c9ead9SRoopa Prabhu } 559810c9ead9SRoopa Prabhu 559946efc97bSPetr Machata #define RTNL_STATS_OFFLOAD_XSTATS_VALID ((1 << __IFLA_OFFLOAD_XSTATS_MAX) - 1) 560046efc97bSPetr Machata 560146efc97bSPetr Machata static const struct nla_policy 560246efc97bSPetr Machata rtnl_stats_get_policy_filters[IFLA_STATS_MAX + 1] = { 560346efc97bSPetr Machata [IFLA_STATS_LINK_OFFLOAD_XSTATS] = 560446efc97bSPetr Machata NLA_POLICY_MASK(NLA_U32, RTNL_STATS_OFFLOAD_XSTATS_VALID), 560546efc97bSPetr Machata }; 560646efc97bSPetr Machata 560746efc97bSPetr Machata static const struct nla_policy 560846efc97bSPetr Machata rtnl_stats_get_policy[IFLA_STATS_GETSET_MAX + 1] = { 560946efc97bSPetr Machata [IFLA_STATS_GET_FILTERS] = 561046efc97bSPetr Machata NLA_POLICY_NESTED(rtnl_stats_get_policy_filters), 561146efc97bSPetr Machata }; 561246efc97bSPetr Machata 561303ba3566SPetr Machata static const struct nla_policy 561403ba3566SPetr Machata ifla_stats_set_policy[IFLA_STATS_GETSET_MAX + 1] = { 56155fd0b838SPetr Machata [IFLA_STATS_SET_OFFLOAD_XSTATS_L3_STATS] = NLA_POLICY_MAX(NLA_U8, 1), 561603ba3566SPetr Machata }; 561703ba3566SPetr Machata 561846efc97bSPetr Machata static int rtnl_stats_get_parse_filters(struct nlattr *ifla_filters, 561946efc97bSPetr Machata struct rtnl_stats_dump_filters *filters, 562046efc97bSPetr Machata struct netlink_ext_ack *extack) 562146efc97bSPetr Machata { 562246efc97bSPetr Machata struct nlattr *tb[IFLA_STATS_MAX + 1]; 562346efc97bSPetr Machata int err; 562446efc97bSPetr Machata int at; 562546efc97bSPetr Machata 562646efc97bSPetr Machata err = nla_parse_nested(tb, IFLA_STATS_MAX, ifla_filters, 562746efc97bSPetr Machata rtnl_stats_get_policy_filters, extack); 562846efc97bSPetr Machata if (err < 0) 562946efc97bSPetr Machata return err; 563046efc97bSPetr Machata 563146efc97bSPetr Machata for (at = 1; at <= IFLA_STATS_MAX; at++) { 563246efc97bSPetr Machata if (tb[at]) { 563346efc97bSPetr Machata if (!(filters->mask[0] & IFLA_STATS_FILTER_BIT(at))) { 563446efc97bSPetr Machata NL_SET_ERR_MSG(extack, "Filtered attribute not enabled in filter_mask"); 563546efc97bSPetr Machata return -EINVAL; 563646efc97bSPetr Machata } 563746efc97bSPetr Machata filters->mask[at] = nla_get_u32(tb[at]); 563846efc97bSPetr Machata } 563946efc97bSPetr Machata } 564046efc97bSPetr Machata 564146efc97bSPetr Machata return 0; 564246efc97bSPetr Machata } 564346efc97bSPetr Machata 564446efc97bSPetr Machata static int rtnl_stats_get_parse(const struct nlmsghdr *nlh, 564546efc97bSPetr Machata u32 filter_mask, 564646efc97bSPetr Machata struct rtnl_stats_dump_filters *filters, 564746efc97bSPetr Machata struct netlink_ext_ack *extack) 564846efc97bSPetr Machata { 564946efc97bSPetr Machata struct nlattr *tb[IFLA_STATS_GETSET_MAX + 1]; 565046efc97bSPetr Machata int err; 565146efc97bSPetr Machata int i; 565246efc97bSPetr Machata 565346efc97bSPetr Machata filters->mask[0] = filter_mask; 565446efc97bSPetr Machata for (i = 1; i < ARRAY_SIZE(filters->mask); i++) 565546efc97bSPetr Machata filters->mask[i] = -1U; 565646efc97bSPetr Machata 565746efc97bSPetr Machata err = nlmsg_parse(nlh, sizeof(struct if_stats_msg), tb, 565846efc97bSPetr Machata IFLA_STATS_GETSET_MAX, rtnl_stats_get_policy, extack); 565946efc97bSPetr Machata if (err < 0) 566046efc97bSPetr Machata return err; 566146efc97bSPetr Machata 566246efc97bSPetr Machata if (tb[IFLA_STATS_GET_FILTERS]) { 566346efc97bSPetr Machata err = rtnl_stats_get_parse_filters(tb[IFLA_STATS_GET_FILTERS], 566446efc97bSPetr Machata filters, extack); 566546efc97bSPetr Machata if (err) 566646efc97bSPetr Machata return err; 566746efc97bSPetr Machata } 566846efc97bSPetr Machata 566946efc97bSPetr Machata return 0; 567046efc97bSPetr Machata } 567146efc97bSPetr Machata 567251bc860dSJakub Kicinski static int rtnl_valid_stats_req(const struct nlmsghdr *nlh, bool strict_check, 567351bc860dSJakub Kicinski bool is_dump, struct netlink_ext_ack *extack) 567451bc860dSJakub Kicinski { 567551bc860dSJakub Kicinski struct if_stats_msg *ifsm; 567651bc860dSJakub Kicinski 567769f23a09SEric Dumazet if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifsm))) { 567851bc860dSJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid header for stats dump"); 567951bc860dSJakub Kicinski return -EINVAL; 568051bc860dSJakub Kicinski } 568151bc860dSJakub Kicinski 568251bc860dSJakub Kicinski if (!strict_check) 568351bc860dSJakub Kicinski return 0; 568451bc860dSJakub Kicinski 568551bc860dSJakub Kicinski ifsm = nlmsg_data(nlh); 568651bc860dSJakub Kicinski 568751bc860dSJakub Kicinski /* only requests using strict checks can pass data to influence 568851bc860dSJakub Kicinski * the dump. The legacy exception is filter_mask. 568951bc860dSJakub Kicinski */ 569051bc860dSJakub Kicinski if (ifsm->pad1 || ifsm->pad2 || (is_dump && ifsm->ifindex)) { 569151bc860dSJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid values in header for stats dump request"); 569251bc860dSJakub Kicinski return -EINVAL; 569351bc860dSJakub Kicinski } 56946300acb2SJakub Kicinski if (ifsm->filter_mask >= IFLA_STATS_FILTER_BIT(IFLA_STATS_MAX + 1)) { 56956300acb2SJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid stats requested through filter mask"); 56966300acb2SJakub Kicinski return -EINVAL; 56976300acb2SJakub Kicinski } 569851bc860dSJakub Kicinski 569951bc860dSJakub Kicinski return 0; 570051bc860dSJakub Kicinski } 570151bc860dSJakub Kicinski 5702c21ef3e3SDavid Ahern static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh, 5703c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 570410c9ead9SRoopa Prabhu { 570546efc97bSPetr Machata struct rtnl_stats_dump_filters filters; 570610c9ead9SRoopa Prabhu struct net *net = sock_net(skb->sk); 570710c9ead9SRoopa Prabhu struct net_device *dev = NULL; 5708e8872a25SNikolay Aleksandrov int idxattr = 0, prividx = 0; 5709e8872a25SNikolay Aleksandrov struct if_stats_msg *ifsm; 571010c9ead9SRoopa Prabhu struct sk_buff *nskb; 571110c9ead9SRoopa Prabhu int err; 571210c9ead9SRoopa Prabhu 571351bc860dSJakub Kicinski err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb), 571451bc860dSJakub Kicinski false, extack); 571551bc860dSJakub Kicinski if (err) 571651bc860dSJakub Kicinski return err; 57174775cc1fSMathias Krause 571810c9ead9SRoopa Prabhu ifsm = nlmsg_data(nlh); 571910c9ead9SRoopa Prabhu if (ifsm->ifindex > 0) 572010c9ead9SRoopa Prabhu dev = __dev_get_by_index(net, ifsm->ifindex); 572110c9ead9SRoopa Prabhu else 572210c9ead9SRoopa Prabhu return -EINVAL; 572310c9ead9SRoopa Prabhu 572410c9ead9SRoopa Prabhu if (!dev) 572510c9ead9SRoopa Prabhu return -ENODEV; 572610c9ead9SRoopa Prabhu 572746efc97bSPetr Machata if (!ifsm->filter_mask) { 572822b67d17SPetr Machata NL_SET_ERR_MSG(extack, "Filter mask must be set for stats get"); 572910c9ead9SRoopa Prabhu return -EINVAL; 573022b67d17SPetr Machata } 573110c9ead9SRoopa Prabhu 573246efc97bSPetr Machata err = rtnl_stats_get_parse(nlh, ifsm->filter_mask, &filters, extack); 573346efc97bSPetr Machata if (err) 573446efc97bSPetr Machata return err; 573546efc97bSPetr Machata 573646efc97bSPetr Machata nskb = nlmsg_new(if_nlmsg_stats_size(dev, &filters), GFP_KERNEL); 573710c9ead9SRoopa Prabhu if (!nskb) 573810c9ead9SRoopa Prabhu return -ENOBUFS; 573910c9ead9SRoopa Prabhu 574010c9ead9SRoopa Prabhu err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS, 574110c9ead9SRoopa Prabhu NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0, 574205415bccSPetr Machata 0, &filters, &idxattr, &prividx, extack); 574310c9ead9SRoopa Prabhu if (err < 0) { 574410c9ead9SRoopa Prabhu /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */ 574510c9ead9SRoopa Prabhu WARN_ON(err == -EMSGSIZE); 574610c9ead9SRoopa Prabhu kfree_skb(nskb); 574710c9ead9SRoopa Prabhu } else { 574810c9ead9SRoopa Prabhu err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); 574910c9ead9SRoopa Prabhu } 575010c9ead9SRoopa Prabhu 575110c9ead9SRoopa Prabhu return err; 575210c9ead9SRoopa Prabhu } 575310c9ead9SRoopa Prabhu 575410c9ead9SRoopa Prabhu static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb) 575510c9ead9SRoopa Prabhu { 5756841891ecSDavid Ahern struct netlink_ext_ack *extack = cb->extack; 5757e8872a25SNikolay Aleksandrov int h, s_h, err, s_idx, s_idxattr, s_prividx; 575846efc97bSPetr Machata struct rtnl_stats_dump_filters filters; 575910c9ead9SRoopa Prabhu struct net *net = sock_net(skb->sk); 576010c9ead9SRoopa Prabhu unsigned int flags = NLM_F_MULTI; 5761e8872a25SNikolay Aleksandrov struct if_stats_msg *ifsm; 5762e8872a25SNikolay Aleksandrov struct hlist_head *head; 5763e8872a25SNikolay Aleksandrov struct net_device *dev; 5764e8872a25SNikolay Aleksandrov int idx = 0; 576510c9ead9SRoopa Prabhu 576610c9ead9SRoopa Prabhu s_h = cb->args[0]; 576710c9ead9SRoopa Prabhu s_idx = cb->args[1]; 5768e8872a25SNikolay Aleksandrov s_idxattr = cb->args[2]; 5769e8872a25SNikolay Aleksandrov s_prividx = cb->args[3]; 577010c9ead9SRoopa Prabhu 577110c9ead9SRoopa Prabhu cb->seq = net->dev_base_seq; 577210c9ead9SRoopa Prabhu 577351bc860dSJakub Kicinski err = rtnl_valid_stats_req(cb->nlh, cb->strict_check, true, extack); 577451bc860dSJakub Kicinski if (err) 577551bc860dSJakub Kicinski return err; 57764775cc1fSMathias Krause 577710c9ead9SRoopa Prabhu ifsm = nlmsg_data(cb->nlh); 577846efc97bSPetr Machata if (!ifsm->filter_mask) { 5779841891ecSDavid Ahern NL_SET_ERR_MSG(extack, "Filter mask must be set for stats dump"); 5780841891ecSDavid Ahern return -EINVAL; 5781841891ecSDavid Ahern } 578210c9ead9SRoopa Prabhu 578346efc97bSPetr Machata err = rtnl_stats_get_parse(cb->nlh, ifsm->filter_mask, &filters, 578446efc97bSPetr Machata extack); 578546efc97bSPetr Machata if (err) 578646efc97bSPetr Machata return err; 578746efc97bSPetr Machata 578810c9ead9SRoopa Prabhu for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 578910c9ead9SRoopa Prabhu idx = 0; 579010c9ead9SRoopa Prabhu head = &net->dev_index_head[h]; 579110c9ead9SRoopa Prabhu hlist_for_each_entry(dev, head, index_hlist) { 579210c9ead9SRoopa Prabhu if (idx < s_idx) 579310c9ead9SRoopa Prabhu goto cont; 579410c9ead9SRoopa Prabhu err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS, 579510c9ead9SRoopa Prabhu NETLINK_CB(cb->skb).portid, 579610c9ead9SRoopa Prabhu cb->nlh->nlmsg_seq, 0, 579746efc97bSPetr Machata flags, &filters, 579805415bccSPetr Machata &s_idxattr, &s_prividx, 579905415bccSPetr Machata extack); 580010c9ead9SRoopa Prabhu /* If we ran out of room on the first message, 580110c9ead9SRoopa Prabhu * we're in trouble 580210c9ead9SRoopa Prabhu */ 580310c9ead9SRoopa Prabhu WARN_ON((err == -EMSGSIZE) && (skb->len == 0)); 580410c9ead9SRoopa Prabhu 580510c9ead9SRoopa Prabhu if (err < 0) 580610c9ead9SRoopa Prabhu goto out; 5807e8872a25SNikolay Aleksandrov s_prividx = 0; 5808e8872a25SNikolay Aleksandrov s_idxattr = 0; 580910c9ead9SRoopa Prabhu nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 581010c9ead9SRoopa Prabhu cont: 581110c9ead9SRoopa Prabhu idx++; 581210c9ead9SRoopa Prabhu } 581310c9ead9SRoopa Prabhu } 581410c9ead9SRoopa Prabhu out: 5815e8872a25SNikolay Aleksandrov cb->args[3] = s_prividx; 5816e8872a25SNikolay Aleksandrov cb->args[2] = s_idxattr; 581710c9ead9SRoopa Prabhu cb->args[1] = idx; 581810c9ead9SRoopa Prabhu cb->args[0] = h; 581910c9ead9SRoopa Prabhu 582010c9ead9SRoopa Prabhu return skb->len; 582110c9ead9SRoopa Prabhu } 582210c9ead9SRoopa Prabhu 58235fd0b838SPetr Machata void rtnl_offload_xstats_notify(struct net_device *dev) 58245fd0b838SPetr Machata { 58255fd0b838SPetr Machata struct rtnl_stats_dump_filters response_filters = {}; 58265fd0b838SPetr Machata struct net *net = dev_net(dev); 58275fd0b838SPetr Machata int idxattr = 0, prividx = 0; 58285fd0b838SPetr Machata struct sk_buff *skb; 58295fd0b838SPetr Machata int err = -ENOBUFS; 58305fd0b838SPetr Machata 58315fd0b838SPetr Machata ASSERT_RTNL(); 58325fd0b838SPetr Machata 58335fd0b838SPetr Machata response_filters.mask[0] |= 58345fd0b838SPetr Machata IFLA_STATS_FILTER_BIT(IFLA_STATS_LINK_OFFLOAD_XSTATS); 58355fd0b838SPetr Machata response_filters.mask[IFLA_STATS_LINK_OFFLOAD_XSTATS] |= 58365fd0b838SPetr Machata IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_HW_S_INFO); 58375fd0b838SPetr Machata 58385fd0b838SPetr Machata skb = nlmsg_new(if_nlmsg_stats_size(dev, &response_filters), 58395fd0b838SPetr Machata GFP_KERNEL); 58405fd0b838SPetr Machata if (!skb) 58415fd0b838SPetr Machata goto errout; 58425fd0b838SPetr Machata 58435fd0b838SPetr Machata err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS, 0, 0, 0, 0, 58445fd0b838SPetr Machata &response_filters, &idxattr, &prividx, NULL); 58455fd0b838SPetr Machata if (err < 0) { 58465fd0b838SPetr Machata kfree_skb(skb); 58475fd0b838SPetr Machata goto errout; 58485fd0b838SPetr Machata } 58495fd0b838SPetr Machata 58505fd0b838SPetr Machata rtnl_notify(skb, net, 0, RTNLGRP_STATS, NULL, GFP_KERNEL); 58515fd0b838SPetr Machata return; 58525fd0b838SPetr Machata 58535fd0b838SPetr Machata errout: 58545fd0b838SPetr Machata rtnl_set_sk_err(net, RTNLGRP_STATS, err); 58555fd0b838SPetr Machata } 58565fd0b838SPetr Machata EXPORT_SYMBOL(rtnl_offload_xstats_notify); 58575fd0b838SPetr Machata 585803ba3566SPetr Machata static int rtnl_stats_set(struct sk_buff *skb, struct nlmsghdr *nlh, 585903ba3566SPetr Machata struct netlink_ext_ack *extack) 586003ba3566SPetr Machata { 58615fd0b838SPetr Machata enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3; 586203ba3566SPetr Machata struct rtnl_stats_dump_filters response_filters = {}; 586303ba3566SPetr Machata struct nlattr *tb[IFLA_STATS_GETSET_MAX + 1]; 586403ba3566SPetr Machata struct net *net = sock_net(skb->sk); 586503ba3566SPetr Machata struct net_device *dev = NULL; 586603ba3566SPetr Machata struct if_stats_msg *ifsm; 58675fd0b838SPetr Machata bool notify = false; 586803ba3566SPetr Machata int err; 586903ba3566SPetr Machata 587003ba3566SPetr Machata err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb), 587103ba3566SPetr Machata false, extack); 587203ba3566SPetr Machata if (err) 587303ba3566SPetr Machata return err; 587403ba3566SPetr Machata 587503ba3566SPetr Machata ifsm = nlmsg_data(nlh); 587603ba3566SPetr Machata if (ifsm->family != AF_UNSPEC) { 587703ba3566SPetr Machata NL_SET_ERR_MSG(extack, "Address family should be AF_UNSPEC"); 587803ba3566SPetr Machata return -EINVAL; 587903ba3566SPetr Machata } 588003ba3566SPetr Machata 588103ba3566SPetr Machata if (ifsm->ifindex > 0) 588203ba3566SPetr Machata dev = __dev_get_by_index(net, ifsm->ifindex); 588303ba3566SPetr Machata else 588403ba3566SPetr Machata return -EINVAL; 588503ba3566SPetr Machata 588603ba3566SPetr Machata if (!dev) 588703ba3566SPetr Machata return -ENODEV; 588803ba3566SPetr Machata 588903ba3566SPetr Machata if (ifsm->filter_mask) { 589003ba3566SPetr Machata NL_SET_ERR_MSG(extack, "Filter mask must be 0 for stats set"); 589103ba3566SPetr Machata return -EINVAL; 589203ba3566SPetr Machata } 589303ba3566SPetr Machata 589403ba3566SPetr Machata err = nlmsg_parse(nlh, sizeof(*ifsm), tb, IFLA_STATS_GETSET_MAX, 589503ba3566SPetr Machata ifla_stats_set_policy, extack); 589603ba3566SPetr Machata if (err < 0) 589703ba3566SPetr Machata return err; 589803ba3566SPetr Machata 58995fd0b838SPetr Machata if (tb[IFLA_STATS_SET_OFFLOAD_XSTATS_L3_STATS]) { 59005fd0b838SPetr Machata u8 req = nla_get_u8(tb[IFLA_STATS_SET_OFFLOAD_XSTATS_L3_STATS]); 590103ba3566SPetr Machata 59025fd0b838SPetr Machata if (req) 59035fd0b838SPetr Machata err = netdev_offload_xstats_enable(dev, t_l3, extack); 59045fd0b838SPetr Machata else 59055fd0b838SPetr Machata err = netdev_offload_xstats_disable(dev, t_l3); 59065fd0b838SPetr Machata 59075fd0b838SPetr Machata if (!err) 59085fd0b838SPetr Machata notify = true; 59095fd0b838SPetr Machata else if (err != -EALREADY) 59105fd0b838SPetr Machata return err; 59115fd0b838SPetr Machata 59125fd0b838SPetr Machata response_filters.mask[0] |= 59135fd0b838SPetr Machata IFLA_STATS_FILTER_BIT(IFLA_STATS_LINK_OFFLOAD_XSTATS); 59145fd0b838SPetr Machata response_filters.mask[IFLA_STATS_LINK_OFFLOAD_XSTATS] |= 59155fd0b838SPetr Machata IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_HW_S_INFO); 591603ba3566SPetr Machata } 591703ba3566SPetr Machata 59185fd0b838SPetr Machata if (notify) 59195fd0b838SPetr Machata rtnl_offload_xstats_notify(dev); 59205fd0b838SPetr Machata 59215fd0b838SPetr Machata return 0; 592203ba3566SPetr Machata } 592303ba3566SPetr Machata 59241da177e4SLinus Torvalds /* Process one rtnetlink message. */ 59251da177e4SLinus Torvalds 59262d4bc933SJohannes Berg static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, 59272d4bc933SJohannes Berg struct netlink_ext_ack *extack) 59281da177e4SLinus Torvalds { 59293b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 5930addf9b90SFlorian Westphal struct rtnl_link *link; 5931e4202511SFlorian Westphal struct module *owner; 59326853dd48SFlorian Westphal int err = -EOPNOTSUPP; 5933e2849863SThomas Graf rtnl_doit_func doit; 593462256f98SFlorian Westphal unsigned int flags; 5935617cfc75SAlexander Kuleshov int kind; 59361da177e4SLinus Torvalds int family; 59371da177e4SLinus Torvalds int type; 59381da177e4SLinus Torvalds 59391da177e4SLinus Torvalds type = nlh->nlmsg_type; 59401da177e4SLinus Torvalds if (type > RTM_MAX) 5941038890feSThomas Graf return -EOPNOTSUPP; 59421da177e4SLinus Torvalds 59431da177e4SLinus Torvalds type -= RTM_BASE; 59441da177e4SLinus Torvalds 59451da177e4SLinus Torvalds /* All the messages must have at least 1 byte length */ 5946573ce260SHong zhi guo if (nlmsg_len(nlh) < sizeof(struct rtgenmsg)) 59471da177e4SLinus Torvalds return 0; 59481da177e4SLinus Torvalds 5949573ce260SHong zhi guo family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family; 59501da177e4SLinus Torvalds kind = type&3; 59511da177e4SLinus Torvalds 595290f62cf3SEric W. Biederman if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN)) 59531d00a4ebSThomas Graf return -EPERM; 59541da177e4SLinus Torvalds 59556853dd48SFlorian Westphal rcu_read_lock(); 5956b8f3ab42SDavid S. Miller if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) { 595797c53cacSDenis V. Lunev struct sock *rtnl; 5958e2849863SThomas Graf rtnl_dumpit_func dumpit; 5959ebfe3c51SDi Zhu u32 min_dump_alloc = 0; 59601da177e4SLinus Torvalds 5961addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 5962addf9b90SFlorian Westphal if (!link || !link->dumpit) { 59636853dd48SFlorian Westphal family = PF_UNSPEC; 5964addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 5965addf9b90SFlorian Westphal if (!link || !link->dumpit) 59666853dd48SFlorian Westphal goto err_unlock; 59676853dd48SFlorian Westphal } 5968e4202511SFlorian Westphal owner = link->owner; 5969addf9b90SFlorian Westphal dumpit = link->dumpit; 59706853dd48SFlorian Westphal 59715c2bb9b6SFlorian Westphal if (type == RTM_GETLINK - RTM_BASE) 5972e1fa6d21SFlorian Westphal min_dump_alloc = rtnl_calcit(skb, nlh); 59731da177e4SLinus Torvalds 5974e4202511SFlorian Westphal err = 0; 5975e4202511SFlorian Westphal /* need to do this before rcu_read_unlock() */ 5976e4202511SFlorian Westphal if (!try_module_get(owner)) 5977e4202511SFlorian Westphal err = -EPROTONOSUPPORT; 5978e4202511SFlorian Westphal 59796853dd48SFlorian Westphal rcu_read_unlock(); 59806853dd48SFlorian Westphal 598197c53cacSDenis V. Lunev rtnl = net->rtnl; 5982e4202511SFlorian Westphal if (err == 0) { 598380d326faSPablo Neira Ayuso struct netlink_dump_control c = { 598480d326faSPablo Neira Ayuso .dump = dumpit, 598580d326faSPablo Neira Ayuso .min_dump_alloc = min_dump_alloc, 5986e4202511SFlorian Westphal .module = owner, 598780d326faSPablo Neira Ayuso }; 598880d326faSPablo Neira Ayuso err = netlink_dump_start(rtnl, skb, nlh, &c); 5989e4202511SFlorian Westphal /* netlink_dump_start() will keep a reference on 5990e4202511SFlorian Westphal * module if dump is still in progress. 5991e4202511SFlorian Westphal */ 5992e4202511SFlorian Westphal module_put(owner); 599380d326faSPablo Neira Ayuso } 59942907c35fSEric Dumazet return err; 59951da177e4SLinus Torvalds } 59961da177e4SLinus Torvalds 5997addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 5998addf9b90SFlorian Westphal if (!link || !link->doit) { 59998caa38b5SFlorian Westphal family = PF_UNSPEC; 6000addf9b90SFlorian Westphal link = rtnl_get_link(PF_UNSPEC, type); 6001addf9b90SFlorian Westphal if (!link || !link->doit) 6002addf9b90SFlorian Westphal goto out_unlock; 60038caa38b5SFlorian Westphal } 60048caa38b5SFlorian Westphal 6005e4202511SFlorian Westphal owner = link->owner; 6006e4202511SFlorian Westphal if (!try_module_get(owner)) { 6007e4202511SFlorian Westphal err = -EPROTONOSUPPORT; 6008e4202511SFlorian Westphal goto out_unlock; 6009e4202511SFlorian Westphal } 6010e4202511SFlorian Westphal 6011addf9b90SFlorian Westphal flags = link->flags; 601262256f98SFlorian Westphal if (flags & RTNL_FLAG_DOIT_UNLOCKED) { 6013addf9b90SFlorian Westphal doit = link->doit; 601462256f98SFlorian Westphal rcu_read_unlock(); 601562256f98SFlorian Westphal if (doit) 601662256f98SFlorian Westphal err = doit(skb, nlh, extack); 6017e4202511SFlorian Westphal module_put(owner); 601862256f98SFlorian Westphal return err; 601962256f98SFlorian Westphal } 60206853dd48SFlorian Westphal rcu_read_unlock(); 60216853dd48SFlorian Westphal 60220cc09020SFlorian Westphal rtnl_lock(); 6023addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 6024addf9b90SFlorian Westphal if (link && link->doit) 6025addf9b90SFlorian Westphal err = link->doit(skb, nlh, extack); 60260cc09020SFlorian Westphal rtnl_unlock(); 6027addf9b90SFlorian Westphal 6028e4202511SFlorian Westphal module_put(owner); 6029e4202511SFlorian Westphal 6030addf9b90SFlorian Westphal return err; 6031addf9b90SFlorian Westphal 6032addf9b90SFlorian Westphal out_unlock: 6033addf9b90SFlorian Westphal rcu_read_unlock(); 60340cc09020SFlorian Westphal return err; 60350cc09020SFlorian Westphal 60360cc09020SFlorian Westphal err_unlock: 60376853dd48SFlorian Westphal rcu_read_unlock(); 60380cc09020SFlorian Westphal return -EOPNOTSUPP; 60391da177e4SLinus Torvalds } 60401da177e4SLinus Torvalds 6041cd40b7d3SDenis V. Lunev static void rtnetlink_rcv(struct sk_buff *skb) 60421da177e4SLinus Torvalds { 6043cd40b7d3SDenis V. Lunev netlink_rcv_skb(skb, &rtnetlink_rcv_msg); 60441da177e4SLinus Torvalds } 60451da177e4SLinus Torvalds 60465f729eaaSJulien Gomes static int rtnetlink_bind(struct net *net, int group) 60475f729eaaSJulien Gomes { 60485f729eaaSJulien Gomes switch (group) { 60495f729eaaSJulien Gomes case RTNLGRP_IPV4_MROUTE_R: 60505f729eaaSJulien Gomes case RTNLGRP_IPV6_MROUTE_R: 60515f729eaaSJulien Gomes if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 60525f729eaaSJulien Gomes return -EPERM; 60535f729eaaSJulien Gomes break; 60545f729eaaSJulien Gomes } 60555f729eaaSJulien Gomes return 0; 60565f729eaaSJulien Gomes } 60575f729eaaSJulien Gomes 60581da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr) 60591da177e4SLinus Torvalds { 6060351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 6061e9dc8653SEric W. Biederman 60621da177e4SLinus Torvalds switch (event) { 60635138e86fSVlad Yasevich case NETDEV_REBOOT: 60648a212589SXin Long case NETDEV_CHANGEMTU: 60653753654eSDavid Ahern case NETDEV_CHANGEADDR: 60665138e86fSVlad Yasevich case NETDEV_CHANGENAME: 60675138e86fSVlad Yasevich case NETDEV_FEAT_CHANGE: 60685138e86fSVlad Yasevich case NETDEV_BONDING_FAILOVER: 6069e6e66594SXin Long case NETDEV_POST_TYPE_CHANGE: 60705138e86fSVlad Yasevich case NETDEV_NOTIFY_PEERS: 6071dc709f37SXin Long case NETDEV_CHANGEUPPER: 60725138e86fSVlad Yasevich case NETDEV_RESEND_IGMP: 60735138e86fSVlad Yasevich case NETDEV_CHANGEINFODATA: 6074eeda3fb9SXin Long case NETDEV_CHANGELOWERSTATE: 6075ebdcf045SXin Long case NETDEV_CHANGE_TX_QUEUE_LEN: 60763d3ea5afSVlad Yasevich rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event), 607738e01b30SNicolas Dichtel GFP_KERNEL, NULL, 0); 60781da177e4SLinus Torvalds break; 60791da177e4SLinus Torvalds default: 60801da177e4SLinus Torvalds break; 60811da177e4SLinus Torvalds } 60821da177e4SLinus Torvalds return NOTIFY_DONE; 60831da177e4SLinus Torvalds } 60841da177e4SLinus Torvalds 60851da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = { 60861da177e4SLinus Torvalds .notifier_call = rtnetlink_event, 60871da177e4SLinus Torvalds }; 60881da177e4SLinus Torvalds 608997c53cacSDenis V. Lunev 60902c8c1e72SAlexey Dobriyan static int __net_init rtnetlink_net_init(struct net *net) 609197c53cacSDenis V. Lunev { 609297c53cacSDenis V. Lunev struct sock *sk; 6093a31f2d17SPablo Neira Ayuso struct netlink_kernel_cfg cfg = { 6094a31f2d17SPablo Neira Ayuso .groups = RTNLGRP_MAX, 6095a31f2d17SPablo Neira Ayuso .input = rtnetlink_rcv, 6096a31f2d17SPablo Neira Ayuso .cb_mutex = &rtnl_mutex, 60979785e10aSPablo Neira Ayuso .flags = NL_CFG_F_NONROOT_RECV, 60985f729eaaSJulien Gomes .bind = rtnetlink_bind, 6099a31f2d17SPablo Neira Ayuso }; 6100a31f2d17SPablo Neira Ayuso 61019f00d977SPablo Neira Ayuso sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg); 610297c53cacSDenis V. Lunev if (!sk) 610397c53cacSDenis V. Lunev return -ENOMEM; 610497c53cacSDenis V. Lunev net->rtnl = sk; 610597c53cacSDenis V. Lunev return 0; 610697c53cacSDenis V. Lunev } 610797c53cacSDenis V. Lunev 61082c8c1e72SAlexey Dobriyan static void __net_exit rtnetlink_net_exit(struct net *net) 610997c53cacSDenis V. Lunev { 6110b7c6ba6eSDenis V. Lunev netlink_kernel_release(net->rtnl); 611197c53cacSDenis V. Lunev net->rtnl = NULL; 611297c53cacSDenis V. Lunev } 611397c53cacSDenis V. Lunev 611497c53cacSDenis V. Lunev static struct pernet_operations rtnetlink_net_ops = { 611597c53cacSDenis V. Lunev .init = rtnetlink_net_init, 611697c53cacSDenis V. Lunev .exit = rtnetlink_net_exit, 611797c53cacSDenis V. Lunev }; 611897c53cacSDenis V. Lunev 61191da177e4SLinus Torvalds void __init rtnetlink_init(void) 61201da177e4SLinus Torvalds { 612197c53cacSDenis V. Lunev if (register_pernet_subsys(&rtnetlink_net_ops)) 61221da177e4SLinus Torvalds panic("rtnetlink_init: cannot initialize rtnetlink\n"); 612397c53cacSDenis V. Lunev 61241da177e4SLinus Torvalds register_netdevice_notifier(&rtnetlink_dev_notifier); 6125340d17fcSThomas Graf 6126c7ac8679SGreg Rose rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, 6127b97bac64SFlorian Westphal rtnl_dump_ifinfo, 0); 6128b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0); 6129b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0); 6130b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0); 6131687ad8ccSThomas Graf 6132b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0); 6133b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0); 6134b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0); 613577162022SJohn Fastabend 613636fbf1e5SJiri Pirko rtnl_register(PF_UNSPEC, RTM_NEWLINKPROP, rtnl_newlinkprop, NULL, 0); 613736fbf1e5SJiri Pirko rtnl_register(PF_UNSPEC, RTM_DELLINKPROP, rtnl_dellinkprop, NULL, 0); 613836fbf1e5SJiri Pirko 6139b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0); 6140b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0); 61415b2f94b2SRoopa Prabhu rtnl_register(PF_BRIDGE, RTM_GETNEIGH, rtnl_fdb_get, rtnl_fdb_dump, 0); 6142e5a55a89SJohn Fastabend 6143b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0); 6144b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0); 6145b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0); 614610c9ead9SRoopa Prabhu 614710c9ead9SRoopa Prabhu rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump, 6148b97bac64SFlorian Westphal 0); 614903ba3566SPetr Machata rtnl_register(PF_UNSPEC, RTM_SETSTATS, rtnl_stats_set, NULL, 0); 61501da177e4SLinus Torvalds } 6151