12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * INET An implementation of the TCP/IP protocol suite for the LINUX 41da177e4SLinus Torvalds * operating system. INET is implemented using the BSD Socket 51da177e4SLinus Torvalds * interface as the means of communication with the user level. 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * Routing netlink socket interface: protocol independent part. 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> 101da177e4SLinus Torvalds * 111da177e4SLinus Torvalds * Fixes: 12d467d0bcSZheng Yongjun * Vitaly E. Lavrov RTA_OK arithmetic was wrong. 131da177e4SLinus Torvalds */ 141da177e4SLinus Torvalds 15ee5d032fSJakub Kicinski #include <linux/bitops.h> 161da177e4SLinus Torvalds #include <linux/errno.h> 171da177e4SLinus Torvalds #include <linux/module.h> 181da177e4SLinus Torvalds #include <linux/types.h> 191da177e4SLinus Torvalds #include <linux/socket.h> 201da177e4SLinus Torvalds #include <linux/kernel.h> 211da177e4SLinus Torvalds #include <linux/timer.h> 221da177e4SLinus Torvalds #include <linux/string.h> 231da177e4SLinus Torvalds #include <linux/sockios.h> 241da177e4SLinus Torvalds #include <linux/net.h> 251da177e4SLinus Torvalds #include <linux/fcntl.h> 261da177e4SLinus Torvalds #include <linux/mm.h> 271da177e4SLinus Torvalds #include <linux/slab.h> 281da177e4SLinus Torvalds #include <linux/interrupt.h> 291da177e4SLinus Torvalds #include <linux/capability.h> 301da177e4SLinus Torvalds #include <linux/skbuff.h> 311da177e4SLinus Torvalds #include <linux/init.h> 321da177e4SLinus Torvalds #include <linux/security.h> 336756ae4bSStephen Hemminger #include <linux/mutex.h> 341823730fSThomas Graf #include <linux/if_addr.h> 3577162022SJohn Fastabend #include <linux/if_bridge.h> 36f6f6424bSJiri Pirko #include <linux/if_vlan.h> 37ebc08a6fSWilliams, Mitch A #include <linux/pci.h> 3877162022SJohn Fastabend #include <linux/etherdevice.h> 3958038695SMartin KaFai Lau #include <linux/bpf.h> 401da177e4SLinus Torvalds 417c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 421da177e4SLinus Torvalds 431da177e4SLinus Torvalds #include <linux/inet.h> 441da177e4SLinus Torvalds #include <linux/netdevice.h> 451da177e4SLinus Torvalds #include <net/ip.h> 461da177e4SLinus Torvalds #include <net/protocol.h> 471da177e4SLinus Torvalds #include <net/arp.h> 481da177e4SLinus Torvalds #include <net/route.h> 491da177e4SLinus Torvalds #include <net/udp.h> 50ea697639SDaniel Borkmann #include <net/tcp.h> 511da177e4SLinus Torvalds #include <net/sock.h> 521da177e4SLinus Torvalds #include <net/pkt_sched.h> 5314c0b97dSThomas Graf #include <net/fib_rules.h> 54e2849863SThomas Graf #include <net/rtnetlink.h> 5530ffee84SJohannes Berg #include <net/net_namespace.h> 56dca56c30SJiri Pirko #include <net/devlink.h> 57cc7f5022SIdo Schimmel #if IS_ENABLED(CONFIG_IPV6) 58cc7f5022SIdo Schimmel #include <net/addrconf.h> 59cc7f5022SIdo Schimmel #endif 601da177e4SLinus Torvalds 616264f58cSJakub Kicinski #include "dev.h" 626264f58cSJakub Kicinski 63a428afe8SNikolay Aleksandrov #define RTNL_MAX_TYPE 50 64160656d7SIdo Schimmel #define RTNL_SLAVE_MAX_TYPE 43 65ccf8dbcdSKees Cook 66e0d087afSEric Dumazet struct rtnl_link { 67e2849863SThomas Graf rtnl_doit_func doit; 68e2849863SThomas Graf rtnl_dumpit_func dumpit; 69e4202511SFlorian Westphal struct module *owner; 7062256f98SFlorian Westphal unsigned int flags; 71addf9b90SFlorian Westphal struct rcu_head rcu; 72e2849863SThomas Graf }; 73e2849863SThomas Graf 746756ae4bSStephen Hemminger static DEFINE_MUTEX(rtnl_mutex); 751da177e4SLinus Torvalds 761da177e4SLinus Torvalds void rtnl_lock(void) 771da177e4SLinus Torvalds { 786756ae4bSStephen Hemminger mutex_lock(&rtnl_mutex); 791da177e4SLinus Torvalds } 80e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_lock); 811da177e4SLinus Torvalds 8279ffdfc6SKirill Tkhai int rtnl_lock_killable(void) 8379ffdfc6SKirill Tkhai { 8479ffdfc6SKirill Tkhai return mutex_lock_killable(&rtnl_mutex); 8579ffdfc6SKirill Tkhai } 8679ffdfc6SKirill Tkhai EXPORT_SYMBOL(rtnl_lock_killable); 8779ffdfc6SKirill Tkhai 881b5c5493SEric Dumazet static struct sk_buff *defer_kfree_skb_list; 891b5c5493SEric Dumazet void rtnl_kfree_skbs(struct sk_buff *head, struct sk_buff *tail) 901b5c5493SEric Dumazet { 911b5c5493SEric Dumazet if (head && tail) { 921b5c5493SEric Dumazet tail->next = defer_kfree_skb_list; 931b5c5493SEric Dumazet defer_kfree_skb_list = head; 941b5c5493SEric Dumazet } 951b5c5493SEric Dumazet } 961b5c5493SEric Dumazet EXPORT_SYMBOL(rtnl_kfree_skbs); 971b5c5493SEric Dumazet 986756ae4bSStephen Hemminger void __rtnl_unlock(void) 991da177e4SLinus Torvalds { 1001b5c5493SEric Dumazet struct sk_buff *head = defer_kfree_skb_list; 1011b5c5493SEric Dumazet 1021b5c5493SEric Dumazet defer_kfree_skb_list = NULL; 1031b5c5493SEric Dumazet 1040b5c21bbSJohannes Berg /* Ensure that we didn't actually add any TODO item when __rtnl_unlock() 1050b5c21bbSJohannes Berg * is used. In some places, e.g. in cfg80211, we have code that will do 1060b5c21bbSJohannes Berg * something like 1070b5c21bbSJohannes Berg * rtnl_lock() 1080b5c21bbSJohannes Berg * wiphy_lock() 1090b5c21bbSJohannes Berg * ... 1100b5c21bbSJohannes Berg * rtnl_unlock() 1110b5c21bbSJohannes Berg * 1120b5c21bbSJohannes Berg * and because netdev_run_todo() acquires the RTNL for items on the list 1130b5c21bbSJohannes Berg * we could cause a situation such as this: 1140b5c21bbSJohannes Berg * Thread 1 Thread 2 1150b5c21bbSJohannes Berg * rtnl_lock() 1160b5c21bbSJohannes Berg * unregister_netdevice() 1170b5c21bbSJohannes Berg * __rtnl_unlock() 1180b5c21bbSJohannes Berg * rtnl_lock() 1190b5c21bbSJohannes Berg * wiphy_lock() 1200b5c21bbSJohannes Berg * rtnl_unlock() 1210b5c21bbSJohannes Berg * netdev_run_todo() 1220b5c21bbSJohannes Berg * __rtnl_unlock() 1230b5c21bbSJohannes Berg * 1240b5c21bbSJohannes Berg * // list not empty now 1250b5c21bbSJohannes Berg * // because of thread 2 1260b5c21bbSJohannes Berg * rtnl_lock() 1270b5c21bbSJohannes Berg * while (!list_empty(...)) 1280b5c21bbSJohannes Berg * rtnl_lock() 1290b5c21bbSJohannes Berg * wiphy_lock() 1300b5c21bbSJohannes Berg * **** DEADLOCK **** 1310b5c21bbSJohannes Berg * 1320b5c21bbSJohannes Berg * However, usage of __rtnl_unlock() is rare, and so we can ensure that 1330b5c21bbSJohannes Berg * it's not used in cases where something is added to do the list. 1340b5c21bbSJohannes Berg */ 1350b5c21bbSJohannes Berg WARN_ON(!list_empty(&net_todo_list)); 1360b5c21bbSJohannes Berg 1376756ae4bSStephen Hemminger mutex_unlock(&rtnl_mutex); 1381b5c5493SEric Dumazet 1391b5c5493SEric Dumazet while (head) { 1401b5c5493SEric Dumazet struct sk_buff *next = head->next; 1411b5c5493SEric Dumazet 1421b5c5493SEric Dumazet kfree_skb(head); 1431b5c5493SEric Dumazet cond_resched(); 1441b5c5493SEric Dumazet head = next; 1451b5c5493SEric Dumazet } 1461da177e4SLinus Torvalds } 1471da177e4SLinus Torvalds 1481da177e4SLinus Torvalds void rtnl_unlock(void) 1491da177e4SLinus Torvalds { 15058ec3b4dSHerbert Xu /* This fellow will unlock it for us. */ 1511da177e4SLinus Torvalds netdev_run_todo(); 1521da177e4SLinus Torvalds } 153e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unlock); 1541da177e4SLinus Torvalds 1556756ae4bSStephen Hemminger int rtnl_trylock(void) 1566756ae4bSStephen Hemminger { 1576756ae4bSStephen Hemminger return mutex_trylock(&rtnl_mutex); 1586756ae4bSStephen Hemminger } 159e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_trylock); 1606756ae4bSStephen Hemminger 161c9c1014bSPatrick McHardy int rtnl_is_locked(void) 162c9c1014bSPatrick McHardy { 163c9c1014bSPatrick McHardy return mutex_is_locked(&rtnl_mutex); 164c9c1014bSPatrick McHardy } 165e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_is_locked); 166c9c1014bSPatrick McHardy 1676f99528eSVlad Buslov bool refcount_dec_and_rtnl_lock(refcount_t *r) 1686f99528eSVlad Buslov { 1696f99528eSVlad Buslov return refcount_dec_and_mutex_lock(r, &rtnl_mutex); 1706f99528eSVlad Buslov } 1716f99528eSVlad Buslov EXPORT_SYMBOL(refcount_dec_and_rtnl_lock); 1726f99528eSVlad Buslov 173a898def2SPaul E. McKenney #ifdef CONFIG_PROVE_LOCKING 1740cbf3343SYaowei Bai bool lockdep_rtnl_is_held(void) 175a898def2SPaul E. McKenney { 176a898def2SPaul E. McKenney return lockdep_is_held(&rtnl_mutex); 177a898def2SPaul E. McKenney } 178a898def2SPaul E. McKenney EXPORT_SYMBOL(lockdep_rtnl_is_held); 179a898def2SPaul E. McKenney #endif /* #ifdef CONFIG_PROVE_LOCKING */ 180a898def2SPaul E. McKenney 18151e13685SJakub Kicinski static struct rtnl_link __rcu *__rcu *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1]; 182e2849863SThomas Graf 183e2849863SThomas Graf static inline int rtm_msgindex(int msgtype) 184e2849863SThomas Graf { 185e2849863SThomas Graf int msgindex = msgtype - RTM_BASE; 186e2849863SThomas Graf 187e2849863SThomas Graf /* 188e2849863SThomas Graf * msgindex < 0 implies someone tried to register a netlink 189e2849863SThomas Graf * control code. msgindex >= RTM_NR_MSGTYPES may indicate that 190e2849863SThomas Graf * the message type has not been added to linux/rtnetlink.h 191e2849863SThomas Graf */ 192e2849863SThomas Graf BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES); 193e2849863SThomas Graf 194e2849863SThomas Graf return msgindex; 195e2849863SThomas Graf } 196e2849863SThomas Graf 197addf9b90SFlorian Westphal static struct rtnl_link *rtnl_get_link(int protocol, int msgtype) 198addf9b90SFlorian Westphal { 19951e13685SJakub Kicinski struct rtnl_link __rcu **tab; 200addf9b90SFlorian Westphal 201addf9b90SFlorian Westphal if (protocol >= ARRAY_SIZE(rtnl_msg_handlers)) 202addf9b90SFlorian Westphal protocol = PF_UNSPEC; 203addf9b90SFlorian Westphal 204addf9b90SFlorian Westphal tab = rcu_dereference_rtnl(rtnl_msg_handlers[protocol]); 205addf9b90SFlorian Westphal if (!tab) 206addf9b90SFlorian Westphal tab = rcu_dereference_rtnl(rtnl_msg_handlers[PF_UNSPEC]); 207addf9b90SFlorian Westphal 20851e13685SJakub Kicinski return rcu_dereference_rtnl(tab[msgtype]); 209addf9b90SFlorian Westphal } 210addf9b90SFlorian Westphal 211e4202511SFlorian Westphal static int rtnl_register_internal(struct module *owner, 212e4202511SFlorian Westphal int protocol, int msgtype, 213c7ac8679SGreg Rose rtnl_doit_func doit, rtnl_dumpit_func dumpit, 214b97bac64SFlorian Westphal unsigned int flags) 215e2849863SThomas Graf { 216b0e9fe1bSFlorian Westphal struct rtnl_link *link, *old; 217b0e9fe1bSFlorian Westphal struct rtnl_link __rcu **tab; 218e2849863SThomas Graf int msgindex; 219addf9b90SFlorian Westphal int ret = -ENOBUFS; 220e2849863SThomas Graf 22125239ceeSPatrick McHardy BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); 222e2849863SThomas Graf msgindex = rtm_msgindex(msgtype); 223e2849863SThomas Graf 224addf9b90SFlorian Westphal rtnl_lock(); 22551e13685SJakub Kicinski tab = rtnl_dereference(rtnl_msg_handlers[protocol]); 226e2849863SThomas Graf if (tab == NULL) { 227addf9b90SFlorian Westphal tab = kcalloc(RTM_NR_MSGTYPES, sizeof(void *), GFP_KERNEL); 228addf9b90SFlorian Westphal if (!tab) 229addf9b90SFlorian Westphal goto unlock; 230e2849863SThomas Graf 231addf9b90SFlorian Westphal /* ensures we see the 0 stores */ 2326853dd48SFlorian Westphal rcu_assign_pointer(rtnl_msg_handlers[protocol], tab); 233e2849863SThomas Graf } 234e2849863SThomas Graf 235addf9b90SFlorian Westphal old = rtnl_dereference(tab[msgindex]); 236addf9b90SFlorian Westphal if (old) { 237addf9b90SFlorian Westphal link = kmemdup(old, sizeof(*old), GFP_KERNEL); 238addf9b90SFlorian Westphal if (!link) 239addf9b90SFlorian Westphal goto unlock; 240addf9b90SFlorian Westphal } else { 241addf9b90SFlorian Westphal link = kzalloc(sizeof(*link), GFP_KERNEL); 242addf9b90SFlorian Westphal if (!link) 243addf9b90SFlorian Westphal goto unlock; 244addf9b90SFlorian Westphal } 245e2849863SThomas Graf 246e4202511SFlorian Westphal WARN_ON(link->owner && link->owner != owner); 247e4202511SFlorian Westphal link->owner = owner; 248e4202511SFlorian Westphal 249addf9b90SFlorian Westphal WARN_ON(doit && link->doit && link->doit != doit); 250addf9b90SFlorian Westphal if (doit) 251addf9b90SFlorian Westphal link->doit = doit; 252addf9b90SFlorian Westphal WARN_ON(dumpit && link->dumpit && link->dumpit != dumpit); 253addf9b90SFlorian Westphal if (dumpit) 254addf9b90SFlorian Westphal link->dumpit = dumpit; 255addf9b90SFlorian Westphal 256a6cec0bcSNikolay Aleksandrov WARN_ON(rtnl_msgtype_kind(msgtype) != RTNL_KIND_DEL && 257a6cec0bcSNikolay Aleksandrov (flags & RTNL_FLAG_BULK_DEL_SUPPORTED)); 258addf9b90SFlorian Westphal link->flags |= flags; 259addf9b90SFlorian Westphal 260addf9b90SFlorian Westphal /* publish protocol:msgtype */ 261addf9b90SFlorian Westphal rcu_assign_pointer(tab[msgindex], link); 262addf9b90SFlorian Westphal ret = 0; 263addf9b90SFlorian Westphal if (old) 264addf9b90SFlorian Westphal kfree_rcu(old, rcu); 265addf9b90SFlorian Westphal unlock: 266addf9b90SFlorian Westphal rtnl_unlock(); 267addf9b90SFlorian Westphal return ret; 268e2849863SThomas Graf } 269e4202511SFlorian Westphal 270e4202511SFlorian Westphal /** 271e4202511SFlorian Westphal * rtnl_register_module - Register a rtnetlink message type 272e4202511SFlorian Westphal * 273e4202511SFlorian Westphal * @owner: module registering the hook (THIS_MODULE) 274e4202511SFlorian Westphal * @protocol: Protocol family or PF_UNSPEC 275e4202511SFlorian Westphal * @msgtype: rtnetlink message type 276e4202511SFlorian Westphal * @doit: Function pointer called for each request message 277e4202511SFlorian Westphal * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message 278d467d0bcSZheng Yongjun * @flags: rtnl_link_flags to modify behaviour of doit/dumpit functions 279e4202511SFlorian Westphal * 280e4202511SFlorian Westphal * Like rtnl_register, but for use by removable modules. 281e4202511SFlorian Westphal */ 282e4202511SFlorian Westphal int rtnl_register_module(struct module *owner, 283e4202511SFlorian Westphal int protocol, int msgtype, 284e4202511SFlorian Westphal rtnl_doit_func doit, rtnl_dumpit_func dumpit, 285e4202511SFlorian Westphal unsigned int flags) 286e4202511SFlorian Westphal { 287e4202511SFlorian Westphal return rtnl_register_internal(owner, protocol, msgtype, 288e4202511SFlorian Westphal doit, dumpit, flags); 289e4202511SFlorian Westphal } 290e4202511SFlorian Westphal EXPORT_SYMBOL_GPL(rtnl_register_module); 291e4202511SFlorian Westphal 292e4202511SFlorian Westphal /** 29316feebcfSFlorian Westphal * rtnl_register - Register a rtnetlink message type 294e4202511SFlorian Westphal * @protocol: Protocol family or PF_UNSPEC 295e4202511SFlorian Westphal * @msgtype: rtnetlink message type 296e4202511SFlorian Westphal * @doit: Function pointer called for each request message 297e4202511SFlorian Westphal * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message 298d467d0bcSZheng Yongjun * @flags: rtnl_link_flags to modify behaviour of doit/dumpit functions 299e4202511SFlorian Westphal * 300e4202511SFlorian Westphal * Registers the specified function pointers (at least one of them has 301e4202511SFlorian Westphal * to be non-NULL) to be called whenever a request message for the 302e4202511SFlorian Westphal * specified protocol family and message type is received. 303e4202511SFlorian Westphal * 304e4202511SFlorian Westphal * The special protocol family PF_UNSPEC may be used to define fallback 305e4202511SFlorian Westphal * function pointers for the case when no entry for the specific protocol 306e4202511SFlorian Westphal * family exists. 307e2849863SThomas Graf */ 308e2849863SThomas Graf void rtnl_register(int protocol, int msgtype, 309c7ac8679SGreg Rose rtnl_doit_func doit, rtnl_dumpit_func dumpit, 310b97bac64SFlorian Westphal unsigned int flags) 311e2849863SThomas Graf { 31216feebcfSFlorian Westphal int err; 31316feebcfSFlorian Westphal 31416feebcfSFlorian Westphal err = rtnl_register_internal(NULL, protocol, msgtype, doit, dumpit, 31516feebcfSFlorian Westphal flags); 31616feebcfSFlorian Westphal if (err) 31716feebcfSFlorian Westphal pr_err("Unable to register rtnetlink message handler, " 31816feebcfSFlorian Westphal "protocol = %d, message type = %d\n", protocol, msgtype); 319e2849863SThomas Graf } 320e2849863SThomas Graf 321e2849863SThomas Graf /** 322e2849863SThomas Graf * rtnl_unregister - Unregister a rtnetlink message type 323e2849863SThomas Graf * @protocol: Protocol family or PF_UNSPEC 324e2849863SThomas Graf * @msgtype: rtnetlink message type 325e2849863SThomas Graf * 326e2849863SThomas Graf * Returns 0 on success or a negative error code. 327e2849863SThomas Graf */ 328e2849863SThomas Graf int rtnl_unregister(int protocol, int msgtype) 329e2849863SThomas Graf { 33051e13685SJakub Kicinski struct rtnl_link __rcu **tab; 33151e13685SJakub Kicinski struct rtnl_link *link; 332e2849863SThomas Graf int msgindex; 333e2849863SThomas Graf 33425239ceeSPatrick McHardy BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); 335e2849863SThomas Graf msgindex = rtm_msgindex(msgtype); 336e2849863SThomas Graf 3376853dd48SFlorian Westphal rtnl_lock(); 338addf9b90SFlorian Westphal tab = rtnl_dereference(rtnl_msg_handlers[protocol]); 339addf9b90SFlorian Westphal if (!tab) { 3406853dd48SFlorian Westphal rtnl_unlock(); 341e2849863SThomas Graf return -ENOENT; 3426853dd48SFlorian Westphal } 343e2849863SThomas Graf 34451e13685SJakub Kicinski link = rtnl_dereference(tab[msgindex]); 3454fc29989SYajun Deng RCU_INIT_POINTER(tab[msgindex], NULL); 3466853dd48SFlorian Westphal rtnl_unlock(); 347e2849863SThomas Graf 348addf9b90SFlorian Westphal kfree_rcu(link, rcu); 349addf9b90SFlorian Westphal 350e2849863SThomas Graf return 0; 351e2849863SThomas Graf } 352e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister); 353e2849863SThomas Graf 354e2849863SThomas Graf /** 355e2849863SThomas Graf * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol 356e2849863SThomas Graf * @protocol : Protocol family or PF_UNSPEC 357e2849863SThomas Graf * 358e2849863SThomas Graf * Identical to calling rtnl_unregster() for all registered message types 359e2849863SThomas Graf * of a certain protocol family. 360e2849863SThomas Graf */ 361e2849863SThomas Graf void rtnl_unregister_all(int protocol) 362e2849863SThomas Graf { 36351e13685SJakub Kicinski struct rtnl_link __rcu **tab; 36451e13685SJakub Kicinski struct rtnl_link *link; 365addf9b90SFlorian Westphal int msgindex; 366019a3169SFlorian Westphal 36725239ceeSPatrick McHardy BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); 368e2849863SThomas Graf 369019a3169SFlorian Westphal rtnl_lock(); 37051e13685SJakub Kicinski tab = rtnl_dereference(rtnl_msg_handlers[protocol]); 371f707ef61SSabrina Dubroca if (!tab) { 372f707ef61SSabrina Dubroca rtnl_unlock(); 373f707ef61SSabrina Dubroca return; 374f707ef61SSabrina Dubroca } 3756853dd48SFlorian Westphal RCU_INIT_POINTER(rtnl_msg_handlers[protocol], NULL); 376addf9b90SFlorian Westphal for (msgindex = 0; msgindex < RTM_NR_MSGTYPES; msgindex++) { 37751e13685SJakub Kicinski link = rtnl_dereference(tab[msgindex]); 378addf9b90SFlorian Westphal if (!link) 379addf9b90SFlorian Westphal continue; 380addf9b90SFlorian Westphal 3814fc29989SYajun Deng RCU_INIT_POINTER(tab[msgindex], NULL); 382addf9b90SFlorian Westphal kfree_rcu(link, rcu); 383addf9b90SFlorian Westphal } 384019a3169SFlorian Westphal rtnl_unlock(); 385019a3169SFlorian Westphal 3866853dd48SFlorian Westphal synchronize_net(); 3876853dd48SFlorian Westphal 388addf9b90SFlorian Westphal kfree(tab); 389e2849863SThomas Graf } 390e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister_all); 3911da177e4SLinus Torvalds 39238f7b870SPatrick McHardy static LIST_HEAD(link_ops); 39338f7b870SPatrick McHardy 394c63044f0SEric Dumazet static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind) 395c63044f0SEric Dumazet { 396c63044f0SEric Dumazet const struct rtnl_link_ops *ops; 397c63044f0SEric Dumazet 398c63044f0SEric Dumazet list_for_each_entry(ops, &link_ops, list) { 399c63044f0SEric Dumazet if (!strcmp(ops->kind, kind)) 400c63044f0SEric Dumazet return ops; 401c63044f0SEric Dumazet } 402c63044f0SEric Dumazet return NULL; 403c63044f0SEric Dumazet } 404c63044f0SEric Dumazet 40538f7b870SPatrick McHardy /** 40638f7b870SPatrick McHardy * __rtnl_link_register - Register rtnl_link_ops with rtnetlink. 40738f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to register 40838f7b870SPatrick McHardy * 40938f7b870SPatrick McHardy * The caller must hold the rtnl_mutex. This function should be used 41038f7b870SPatrick McHardy * by drivers that create devices during module initialization. It 41138f7b870SPatrick McHardy * must be called before registering the devices. 41238f7b870SPatrick McHardy * 41338f7b870SPatrick McHardy * Returns 0 on success or a negative error code. 41438f7b870SPatrick McHardy */ 41538f7b870SPatrick McHardy int __rtnl_link_register(struct rtnl_link_ops *ops) 41638f7b870SPatrick McHardy { 417c63044f0SEric Dumazet if (rtnl_link_ops_get(ops->kind)) 418c63044f0SEric Dumazet return -EEXIST; 419c63044f0SEric Dumazet 4208c713dc9SJohannes Berg /* The check for alloc/setup is here because if ops 421b0ab2fabSJiri Pirko * does not have that filled up, it is not possible 422b0ab2fabSJiri Pirko * to use the ops for creating device. So do not 423b0ab2fabSJiri Pirko * fill up dellink as well. That disables rtnl_dellink. 424b0ab2fabSJiri Pirko */ 4258c713dc9SJohannes Berg if ((ops->alloc || ops->setup) && !ops->dellink) 42623289a37SEric Dumazet ops->dellink = unregister_netdevice_queue; 4272d85cba2SPatrick McHardy 42838f7b870SPatrick McHardy list_add_tail(&ops->list, &link_ops); 42938f7b870SPatrick McHardy return 0; 43038f7b870SPatrick McHardy } 43138f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_register); 43238f7b870SPatrick McHardy 43338f7b870SPatrick McHardy /** 43438f7b870SPatrick McHardy * rtnl_link_register - Register rtnl_link_ops with rtnetlink. 43538f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to register 43638f7b870SPatrick McHardy * 43738f7b870SPatrick McHardy * Returns 0 on success or a negative error code. 43838f7b870SPatrick McHardy */ 43938f7b870SPatrick McHardy int rtnl_link_register(struct rtnl_link_ops *ops) 44038f7b870SPatrick McHardy { 44138f7b870SPatrick McHardy int err; 44238f7b870SPatrick McHardy 443ccf8dbcdSKees Cook /* Sanity-check max sizes to avoid stack buffer overflow. */ 444ccf8dbcdSKees Cook if (WARN_ON(ops->maxtype > RTNL_MAX_TYPE || 445ccf8dbcdSKees Cook ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE)) 446ccf8dbcdSKees Cook return -EINVAL; 447ccf8dbcdSKees Cook 44838f7b870SPatrick McHardy rtnl_lock(); 44938f7b870SPatrick McHardy err = __rtnl_link_register(ops); 45038f7b870SPatrick McHardy rtnl_unlock(); 45138f7b870SPatrick McHardy return err; 45238f7b870SPatrick McHardy } 45338f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_register); 45438f7b870SPatrick McHardy 455669f87baSPavel Emelyanov static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops) 456669f87baSPavel Emelyanov { 457669f87baSPavel Emelyanov struct net_device *dev; 45823289a37SEric Dumazet LIST_HEAD(list_kill); 45923289a37SEric Dumazet 460669f87baSPavel Emelyanov for_each_netdev(net, dev) { 46123289a37SEric Dumazet if (dev->rtnl_link_ops == ops) 46223289a37SEric Dumazet ops->dellink(dev, &list_kill); 463669f87baSPavel Emelyanov } 46423289a37SEric Dumazet unregister_netdevice_many(&list_kill); 465669f87baSPavel Emelyanov } 466669f87baSPavel Emelyanov 46738f7b870SPatrick McHardy /** 46838f7b870SPatrick McHardy * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. 46938f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to unregister 47038f7b870SPatrick McHardy * 471554873e5SKirill Tkhai * The caller must hold the rtnl_mutex and guarantee net_namespace_list 472554873e5SKirill Tkhai * integrity (hold pernet_ops_rwsem for writing to close the race 473554873e5SKirill Tkhai * with setup_net() and cleanup_net()). 47438f7b870SPatrick McHardy */ 47538f7b870SPatrick McHardy void __rtnl_link_unregister(struct rtnl_link_ops *ops) 47638f7b870SPatrick McHardy { 477881d966bSEric W. Biederman struct net *net; 4782d85cba2SPatrick McHardy 479881d966bSEric W. Biederman for_each_net(net) { 480669f87baSPavel Emelyanov __rtnl_kill_links(net, ops); 481881d966bSEric W. Biederman } 48238f7b870SPatrick McHardy list_del(&ops->list); 48338f7b870SPatrick McHardy } 48438f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_unregister); 48538f7b870SPatrick McHardy 486200b916fSCong Wang /* Return with the rtnl_lock held when there are no network 487200b916fSCong Wang * devices unregistering in any network namespace. 488200b916fSCong Wang */ 489200b916fSCong Wang static void rtnl_lock_unregistering_all(void) 490200b916fSCong Wang { 491200b916fSCong Wang struct net *net; 492200b916fSCong Wang bool unregistering; 493ff960a73SPeter Zijlstra DEFINE_WAIT_FUNC(wait, woken_wake_function); 494200b916fSCong Wang 495ff960a73SPeter Zijlstra add_wait_queue(&netdev_unregistering_wq, &wait); 496200b916fSCong Wang for (;;) { 497200b916fSCong Wang unregistering = false; 498200b916fSCong Wang rtnl_lock(); 499f0b07bb1SKirill Tkhai /* We held write locked pernet_ops_rwsem, and parallel 500f0b07bb1SKirill Tkhai * setup_net() and cleanup_net() are not possible. 501f0b07bb1SKirill Tkhai */ 502200b916fSCong Wang for_each_net(net) { 503ede6c39cSEric Dumazet if (atomic_read(&net->dev_unreg_count) > 0) { 504200b916fSCong Wang unregistering = true; 505200b916fSCong Wang break; 506200b916fSCong Wang } 507200b916fSCong Wang } 508200b916fSCong Wang if (!unregistering) 509200b916fSCong Wang break; 510200b916fSCong Wang __rtnl_unlock(); 511ff960a73SPeter Zijlstra 512ff960a73SPeter Zijlstra wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); 513200b916fSCong Wang } 514ff960a73SPeter Zijlstra remove_wait_queue(&netdev_unregistering_wq, &wait); 515200b916fSCong Wang } 516200b916fSCong Wang 51738f7b870SPatrick McHardy /** 51838f7b870SPatrick McHardy * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. 51938f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to unregister 52038f7b870SPatrick McHardy */ 52138f7b870SPatrick McHardy void rtnl_link_unregister(struct rtnl_link_ops *ops) 52238f7b870SPatrick McHardy { 5238518e9bbSKirill Tkhai /* Close the race with setup_net() and cleanup_net() */ 5244420bf21SKirill Tkhai down_write(&pernet_ops_rwsem); 525200b916fSCong Wang rtnl_lock_unregistering_all(); 52638f7b870SPatrick McHardy __rtnl_link_unregister(ops); 52738f7b870SPatrick McHardy rtnl_unlock(); 5284420bf21SKirill Tkhai up_write(&pernet_ops_rwsem); 52938f7b870SPatrick McHardy } 53038f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_unregister); 53138f7b870SPatrick McHardy 532ba7d49b1SJiri Pirko static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev) 533ba7d49b1SJiri Pirko { 534ba7d49b1SJiri Pirko struct net_device *master_dev; 535ba7d49b1SJiri Pirko const struct rtnl_link_ops *ops; 5368515ae38SFlorian Westphal size_t size = 0; 537ba7d49b1SJiri Pirko 5388515ae38SFlorian Westphal rcu_read_lock(); 5398515ae38SFlorian Westphal 5408515ae38SFlorian Westphal master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev); 541ba7d49b1SJiri Pirko if (!master_dev) 5428515ae38SFlorian Westphal goto out; 5438515ae38SFlorian Westphal 544ba7d49b1SJiri Pirko ops = master_dev->rtnl_link_ops; 5456049f253SFernando Luis Vazquez Cao if (!ops || !ops->get_slave_size) 5468515ae38SFlorian Westphal goto out; 547ba7d49b1SJiri Pirko /* IFLA_INFO_SLAVE_DATA + nested data */ 5488515ae38SFlorian Westphal size = nla_total_size(sizeof(struct nlattr)) + 549ba7d49b1SJiri Pirko ops->get_slave_size(master_dev, dev); 5508515ae38SFlorian Westphal 5518515ae38SFlorian Westphal out: 5528515ae38SFlorian Westphal rcu_read_unlock(); 5538515ae38SFlorian Westphal return size; 554ba7d49b1SJiri Pirko } 555ba7d49b1SJiri Pirko 55638f7b870SPatrick McHardy static size_t rtnl_link_get_size(const struct net_device *dev) 55738f7b870SPatrick McHardy { 55838f7b870SPatrick McHardy const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 55938f7b870SPatrick McHardy size_t size; 56038f7b870SPatrick McHardy 56138f7b870SPatrick McHardy if (!ops) 56238f7b870SPatrick McHardy return 0; 56338f7b870SPatrick McHardy 564369cf77aSThomas Graf size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */ 565369cf77aSThomas Graf nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */ 56638f7b870SPatrick McHardy 56738f7b870SPatrick McHardy if (ops->get_size) 56838f7b870SPatrick McHardy /* IFLA_INFO_DATA + nested data */ 569369cf77aSThomas Graf size += nla_total_size(sizeof(struct nlattr)) + 57038f7b870SPatrick McHardy ops->get_size(dev); 57138f7b870SPatrick McHardy 57238f7b870SPatrick McHardy if (ops->get_xstats_size) 573369cf77aSThomas Graf /* IFLA_INFO_XSTATS */ 574369cf77aSThomas Graf size += nla_total_size(ops->get_xstats_size(dev)); 57538f7b870SPatrick McHardy 576ba7d49b1SJiri Pirko size += rtnl_link_get_slave_info_data_size(dev); 577ba7d49b1SJiri Pirko 57838f7b870SPatrick McHardy return size; 57938f7b870SPatrick McHardy } 58038f7b870SPatrick McHardy 581f8ff182cSThomas Graf static LIST_HEAD(rtnl_af_ops); 582f8ff182cSThomas Graf 583f8ff182cSThomas Graf static const struct rtnl_af_ops *rtnl_af_lookup(const int family) 584f8ff182cSThomas Graf { 585f8ff182cSThomas Graf const struct rtnl_af_ops *ops; 586f8ff182cSThomas Graf 587a100243dSCong Wang ASSERT_RTNL(); 588a100243dSCong Wang 589a100243dSCong Wang list_for_each_entry(ops, &rtnl_af_ops, list) { 590f8ff182cSThomas Graf if (ops->family == family) 591f8ff182cSThomas Graf return ops; 592f8ff182cSThomas Graf } 593f8ff182cSThomas Graf 594f8ff182cSThomas Graf return NULL; 595f8ff182cSThomas Graf } 596f8ff182cSThomas Graf 597f8ff182cSThomas Graf /** 598f8ff182cSThomas Graf * rtnl_af_register - Register rtnl_af_ops with rtnetlink. 599f8ff182cSThomas Graf * @ops: struct rtnl_af_ops * to register 600f8ff182cSThomas Graf * 601f8ff182cSThomas Graf * Returns 0 on success or a negative error code. 602f8ff182cSThomas Graf */ 6033678a9d8Sstephen hemminger void rtnl_af_register(struct rtnl_af_ops *ops) 604f8ff182cSThomas Graf { 605f8ff182cSThomas Graf rtnl_lock(); 6065fa85a09SFlorian Westphal list_add_tail_rcu(&ops->list, &rtnl_af_ops); 607f8ff182cSThomas Graf rtnl_unlock(); 608f8ff182cSThomas Graf } 609f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_register); 610f8ff182cSThomas Graf 611f8ff182cSThomas Graf /** 612f8ff182cSThomas Graf * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink. 613f8ff182cSThomas Graf * @ops: struct rtnl_af_ops * to unregister 614f8ff182cSThomas Graf */ 615f8ff182cSThomas Graf void rtnl_af_unregister(struct rtnl_af_ops *ops) 616f8ff182cSThomas Graf { 617f8ff182cSThomas Graf rtnl_lock(); 6185fa85a09SFlorian Westphal list_del_rcu(&ops->list); 619f8ff182cSThomas Graf rtnl_unlock(); 6205fa85a09SFlorian Westphal 6215fa85a09SFlorian Westphal synchronize_rcu(); 622f8ff182cSThomas Graf } 623f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_unregister); 624f8ff182cSThomas Graf 625b1974ed0SArad, Ronen static size_t rtnl_link_get_af_size(const struct net_device *dev, 626b1974ed0SArad, Ronen u32 ext_filter_mask) 627f8ff182cSThomas Graf { 628f8ff182cSThomas Graf struct rtnl_af_ops *af_ops; 629f8ff182cSThomas Graf size_t size; 630f8ff182cSThomas Graf 631f8ff182cSThomas Graf /* IFLA_AF_SPEC */ 632f8ff182cSThomas Graf size = nla_total_size(sizeof(struct nlattr)); 633f8ff182cSThomas Graf 6345fa85a09SFlorian Westphal rcu_read_lock(); 6355fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 636f8ff182cSThomas Graf if (af_ops->get_link_af_size) { 637f8ff182cSThomas Graf /* AF_* + nested data */ 638f8ff182cSThomas Graf size += nla_total_size(sizeof(struct nlattr)) + 639b1974ed0SArad, Ronen af_ops->get_link_af_size(dev, ext_filter_mask); 640f8ff182cSThomas Graf } 641f8ff182cSThomas Graf } 6425fa85a09SFlorian Westphal rcu_read_unlock(); 643f8ff182cSThomas Graf 644f8ff182cSThomas Graf return size; 645f8ff182cSThomas Graf } 646f8ff182cSThomas Graf 647ba7d49b1SJiri Pirko static bool rtnl_have_link_slave_info(const struct net_device *dev) 648ba7d49b1SJiri Pirko { 649ba7d49b1SJiri Pirko struct net_device *master_dev; 6504c82a95eSFlorian Westphal bool ret = false; 651ba7d49b1SJiri Pirko 6524c82a95eSFlorian Westphal rcu_read_lock(); 6534c82a95eSFlorian Westphal 6544c82a95eSFlorian Westphal master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev); 655813f020cSJiri Pirko if (master_dev && master_dev->rtnl_link_ops) 6564c82a95eSFlorian Westphal ret = true; 6574c82a95eSFlorian Westphal rcu_read_unlock(); 6584c82a95eSFlorian Westphal return ret; 659ba7d49b1SJiri Pirko } 660ba7d49b1SJiri Pirko 661ba7d49b1SJiri Pirko static int rtnl_link_slave_info_fill(struct sk_buff *skb, 662ba7d49b1SJiri Pirko const struct net_device *dev) 663ba7d49b1SJiri Pirko { 664ba7d49b1SJiri Pirko struct net_device *master_dev; 665ba7d49b1SJiri Pirko const struct rtnl_link_ops *ops; 666ba7d49b1SJiri Pirko struct nlattr *slave_data; 667ba7d49b1SJiri Pirko int err; 668ba7d49b1SJiri Pirko 669ba7d49b1SJiri Pirko master_dev = netdev_master_upper_dev_get((struct net_device *) dev); 670ba7d49b1SJiri Pirko if (!master_dev) 671ba7d49b1SJiri Pirko return 0; 672ba7d49b1SJiri Pirko ops = master_dev->rtnl_link_ops; 673ba7d49b1SJiri Pirko if (!ops) 674ba7d49b1SJiri Pirko return 0; 675ba7d49b1SJiri Pirko if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0) 676ba7d49b1SJiri Pirko return -EMSGSIZE; 677ba7d49b1SJiri Pirko if (ops->fill_slave_info) { 678ae0be8deSMichal Kubecek slave_data = nla_nest_start_noflag(skb, IFLA_INFO_SLAVE_DATA); 679ba7d49b1SJiri Pirko if (!slave_data) 680ba7d49b1SJiri Pirko return -EMSGSIZE; 681ba7d49b1SJiri Pirko err = ops->fill_slave_info(skb, master_dev, dev); 682ba7d49b1SJiri Pirko if (err < 0) 683ba7d49b1SJiri Pirko goto err_cancel_slave_data; 684ba7d49b1SJiri Pirko nla_nest_end(skb, slave_data); 685ba7d49b1SJiri Pirko } 686ba7d49b1SJiri Pirko return 0; 687ba7d49b1SJiri Pirko 688ba7d49b1SJiri Pirko err_cancel_slave_data: 689ba7d49b1SJiri Pirko nla_nest_cancel(skb, slave_data); 690ba7d49b1SJiri Pirko return err; 691ba7d49b1SJiri Pirko } 692ba7d49b1SJiri Pirko 693ba7d49b1SJiri Pirko static int rtnl_link_info_fill(struct sk_buff *skb, 694ba7d49b1SJiri Pirko const struct net_device *dev) 69538f7b870SPatrick McHardy { 69638f7b870SPatrick McHardy const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 697ba7d49b1SJiri Pirko struct nlattr *data; 698ba7d49b1SJiri Pirko int err; 699ba7d49b1SJiri Pirko 700ba7d49b1SJiri Pirko if (!ops) 701ba7d49b1SJiri Pirko return 0; 702ba7d49b1SJiri Pirko if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0) 703ba7d49b1SJiri Pirko return -EMSGSIZE; 704ba7d49b1SJiri Pirko if (ops->fill_xstats) { 705ba7d49b1SJiri Pirko err = ops->fill_xstats(skb, dev); 706ba7d49b1SJiri Pirko if (err < 0) 707ba7d49b1SJiri Pirko return err; 708ba7d49b1SJiri Pirko } 709ba7d49b1SJiri Pirko if (ops->fill_info) { 710ae0be8deSMichal Kubecek data = nla_nest_start_noflag(skb, IFLA_INFO_DATA); 711ba7d49b1SJiri Pirko if (data == NULL) 712ba7d49b1SJiri Pirko return -EMSGSIZE; 713ba7d49b1SJiri Pirko err = ops->fill_info(skb, dev); 714ba7d49b1SJiri Pirko if (err < 0) 715ba7d49b1SJiri Pirko goto err_cancel_data; 716ba7d49b1SJiri Pirko nla_nest_end(skb, data); 717ba7d49b1SJiri Pirko } 718ba7d49b1SJiri Pirko return 0; 719ba7d49b1SJiri Pirko 720ba7d49b1SJiri Pirko err_cancel_data: 721ba7d49b1SJiri Pirko nla_nest_cancel(skb, data); 722ba7d49b1SJiri Pirko return err; 723ba7d49b1SJiri Pirko } 724ba7d49b1SJiri Pirko 725ba7d49b1SJiri Pirko static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev) 726ba7d49b1SJiri Pirko { 727ba7d49b1SJiri Pirko struct nlattr *linkinfo; 72838f7b870SPatrick McHardy int err = -EMSGSIZE; 72938f7b870SPatrick McHardy 730ae0be8deSMichal Kubecek linkinfo = nla_nest_start_noflag(skb, IFLA_LINKINFO); 73138f7b870SPatrick McHardy if (linkinfo == NULL) 73238f7b870SPatrick McHardy goto out; 73338f7b870SPatrick McHardy 734ba7d49b1SJiri Pirko err = rtnl_link_info_fill(skb, dev); 73538f7b870SPatrick McHardy if (err < 0) 73638f7b870SPatrick McHardy goto err_cancel_link; 737ba7d49b1SJiri Pirko 738ba7d49b1SJiri Pirko err = rtnl_link_slave_info_fill(skb, dev); 73938f7b870SPatrick McHardy if (err < 0) 740ba7d49b1SJiri Pirko goto err_cancel_link; 74138f7b870SPatrick McHardy 74238f7b870SPatrick McHardy nla_nest_end(skb, linkinfo); 74338f7b870SPatrick McHardy return 0; 74438f7b870SPatrick McHardy 74538f7b870SPatrick McHardy err_cancel_link: 74638f7b870SPatrick McHardy nla_nest_cancel(skb, linkinfo); 74738f7b870SPatrick McHardy out: 74838f7b870SPatrick McHardy return err; 74938f7b870SPatrick McHardy } 75038f7b870SPatrick McHardy 75195c96174SEric Dumazet int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo) 7521da177e4SLinus Torvalds { 75397c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 7541da177e4SLinus Torvalds 755cfdf0d9aSYajun Deng return nlmsg_notify(rtnl, skb, pid, group, echo, GFP_KERNEL); 7561da177e4SLinus Torvalds } 7571da177e4SLinus Torvalds 75897c53cacSDenis V. Lunev int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid) 7592942e900SThomas Graf { 76097c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 76197c53cacSDenis V. Lunev 7622942e900SThomas Graf return nlmsg_unicast(rtnl, skb, pid); 7632942e900SThomas Graf } 764e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unicast); 7652942e900SThomas Graf 7661ce85fe4SPablo Neira Ayuso void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group, 7671d997f10SHangbin Liu const struct nlmsghdr *nlh, gfp_t flags) 76897676b6bSThomas Graf { 76997c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 77097676b6bSThomas Graf 771f9b282b3SYajun Deng nlmsg_notify(rtnl, skb, pid, group, nlmsg_report(nlh), flags); 77297676b6bSThomas Graf } 773e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_notify); 77497676b6bSThomas Graf 77597c53cacSDenis V. Lunev void rtnl_set_sk_err(struct net *net, u32 group, int error) 77697676b6bSThomas Graf { 77797c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 77897c53cacSDenis V. Lunev 77997676b6bSThomas Graf netlink_set_err(rtnl, 0, group, error); 78097676b6bSThomas Graf } 781e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_set_sk_err); 78297676b6bSThomas Graf 7831da177e4SLinus Torvalds int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics) 7841da177e4SLinus Torvalds { 7852d7202bfSThomas Graf struct nlattr *mx; 7862d7202bfSThomas Graf int i, valid = 0; 7871da177e4SLinus Torvalds 788c22a133aSDavid Ahern /* nothing is dumped for dst_default_metrics, so just skip the loop */ 789c22a133aSDavid Ahern if (metrics == dst_default_metrics.metrics) 790c22a133aSDavid Ahern return 0; 791c22a133aSDavid Ahern 792ae0be8deSMichal Kubecek mx = nla_nest_start_noflag(skb, RTA_METRICS); 7932d7202bfSThomas Graf if (mx == NULL) 7942d7202bfSThomas Graf return -ENOBUFS; 7952d7202bfSThomas Graf 7961da177e4SLinus Torvalds for (i = 0; i < RTAX_MAX; i++) { 7972d7202bfSThomas Graf if (metrics[i]) { 798ea697639SDaniel Borkmann if (i == RTAX_CC_ALGO - 1) { 799ea697639SDaniel Borkmann char tmp[TCP_CA_NAME_MAX], *name; 800ea697639SDaniel Borkmann 801ea697639SDaniel Borkmann name = tcp_ca_get_name_by_key(metrics[i], tmp); 802ea697639SDaniel Borkmann if (!name) 803ea697639SDaniel Borkmann continue; 804ea697639SDaniel Borkmann if (nla_put_string(skb, i + 1, name)) 805ea697639SDaniel Borkmann goto nla_put_failure; 806c3a8d947SDaniel Borkmann } else if (i == RTAX_FEATURES - 1) { 807c3a8d947SDaniel Borkmann u32 user_features = metrics[i] & RTAX_FEATURE_MASK; 808c3a8d947SDaniel Borkmann 809f8edcd12SPhil Sutter if (!user_features) 810f8edcd12SPhil Sutter continue; 811c3a8d947SDaniel Borkmann BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK); 812c3a8d947SDaniel Borkmann if (nla_put_u32(skb, i + 1, user_features)) 813c3a8d947SDaniel Borkmann goto nla_put_failure; 814ea697639SDaniel Borkmann } else { 815a6574349SDavid S. Miller if (nla_put_u32(skb, i + 1, metrics[i])) 816a6574349SDavid S. Miller goto nla_put_failure; 8171da177e4SLinus Torvalds } 818ea697639SDaniel Borkmann valid++; 819ea697639SDaniel Borkmann } 8202d7202bfSThomas Graf } 8211da177e4SLinus Torvalds 822a57d27fcSDavid S. Miller if (!valid) { 823a57d27fcSDavid S. Miller nla_nest_cancel(skb, mx); 824a57d27fcSDavid S. Miller return 0; 825a57d27fcSDavid S. Miller } 8262d7202bfSThomas Graf 8272d7202bfSThomas Graf return nla_nest_end(skb, mx); 8282d7202bfSThomas Graf 8292d7202bfSThomas Graf nla_put_failure: 830bc3ed28cSThomas Graf nla_nest_cancel(skb, mx); 831bc3ed28cSThomas Graf return -EMSGSIZE; 8321da177e4SLinus Torvalds } 833e0d087afSEric Dumazet EXPORT_SYMBOL(rtnetlink_put_metrics); 8341da177e4SLinus Torvalds 835e3703b3dSThomas Graf int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id, 83687a50699SDavid S. Miller long expires, u32 error) 837e3703b3dSThomas Graf { 838e3703b3dSThomas Graf struct rta_cacheinfo ci = { 839e3703b3dSThomas Graf .rta_error = error, 840e3703b3dSThomas Graf .rta_id = id, 841e3703b3dSThomas Graf }; 842e3703b3dSThomas Graf 8433940746dSDavid Ahern if (dst) { 8443940746dSDavid Ahern ci.rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse); 8453940746dSDavid Ahern ci.rta_used = dst->__use; 846bc9d3a9fSThomas Gleixner ci.rta_clntref = rcuref_read(&dst->__rcuref); 8473940746dSDavid Ahern } 8488253947eSLi Wei if (expires) { 8498253947eSLi Wei unsigned long clock; 850e3703b3dSThomas Graf 8518253947eSLi Wei clock = jiffies_to_clock_t(abs(expires)); 8528253947eSLi Wei clock = min_t(unsigned long, clock, INT_MAX); 8538253947eSLi Wei ci.rta_expires = (expires > 0) ? clock : -clock; 8548253947eSLi Wei } 855e3703b3dSThomas Graf return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci); 856e3703b3dSThomas Graf } 857e3703b3dSThomas Graf EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo); 8581da177e4SLinus Torvalds 85993b2d4a2SDavid S. Miller static void set_operstate(struct net_device *dev, unsigned char transition) 860b00055aaSStefan Rompf { 861b00055aaSStefan Rompf unsigned char operstate = dev->operstate; 862b00055aaSStefan Rompf 863b00055aaSStefan Rompf switch (transition) { 864b00055aaSStefan Rompf case IF_OPER_UP: 865b00055aaSStefan Rompf if ((operstate == IF_OPER_DORMANT || 866eec517cdSAndrew Lunn operstate == IF_OPER_TESTING || 867b00055aaSStefan Rompf operstate == IF_OPER_UNKNOWN) && 868eec517cdSAndrew Lunn !netif_dormant(dev) && !netif_testing(dev)) 869b00055aaSStefan Rompf operstate = IF_OPER_UP; 870b00055aaSStefan Rompf break; 871b00055aaSStefan Rompf 872eec517cdSAndrew Lunn case IF_OPER_TESTING: 873abbc7928SJuhee Kang if (netif_oper_up(dev)) 874eec517cdSAndrew Lunn operstate = IF_OPER_TESTING; 875eec517cdSAndrew Lunn break; 876eec517cdSAndrew Lunn 877b00055aaSStefan Rompf case IF_OPER_DORMANT: 878abbc7928SJuhee Kang if (netif_oper_up(dev)) 879b00055aaSStefan Rompf operstate = IF_OPER_DORMANT; 880b00055aaSStefan Rompf break; 8813ff50b79SStephen Hemminger } 882b00055aaSStefan Rompf 883b00055aaSStefan Rompf if (dev->operstate != operstate) { 884fd888e85SSebastian Andrzej Siewior write_lock(&dev_base_lock); 885b00055aaSStefan Rompf dev->operstate = operstate; 886fd888e85SSebastian Andrzej Siewior write_unlock(&dev_base_lock); 887b00055aaSStefan Rompf netdev_state_change(dev); 88893b2d4a2SDavid S. Miller } 889b00055aaSStefan Rompf } 890b00055aaSStefan Rompf 891b1beb681SJiri Benc static unsigned int rtnl_dev_get_flags(const struct net_device *dev) 892b1beb681SJiri Benc { 893b1beb681SJiri Benc return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) | 894b1beb681SJiri Benc (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI)); 895b1beb681SJiri Benc } 896b1beb681SJiri Benc 8973729d502SPatrick McHardy static unsigned int rtnl_dev_combine_flags(const struct net_device *dev, 8983729d502SPatrick McHardy const struct ifinfomsg *ifm) 8993729d502SPatrick McHardy { 9003729d502SPatrick McHardy unsigned int flags = ifm->ifi_flags; 9013729d502SPatrick McHardy 9023729d502SPatrick McHardy /* bugwards compatibility: ifi_change == 0 is treated as ~0 */ 9033729d502SPatrick McHardy if (ifm->ifi_change) 9043729d502SPatrick McHardy flags = (flags & ifm->ifi_change) | 905b1beb681SJiri Benc (rtnl_dev_get_flags(dev) & ~ifm->ifi_change); 9063729d502SPatrick McHardy 9073729d502SPatrick McHardy return flags; 9083729d502SPatrick McHardy } 9093729d502SPatrick McHardy 910b60c5115SThomas Graf static void copy_rtnl_link_stats(struct rtnl_link_stats *a, 911be1f3c2cSBen Hutchings const struct rtnl_link_stats64 *b) 9121da177e4SLinus Torvalds { 913b60c5115SThomas Graf a->rx_packets = b->rx_packets; 914b60c5115SThomas Graf a->tx_packets = b->tx_packets; 915b60c5115SThomas Graf a->rx_bytes = b->rx_bytes; 916b60c5115SThomas Graf a->tx_bytes = b->tx_bytes; 917b60c5115SThomas Graf a->rx_errors = b->rx_errors; 918b60c5115SThomas Graf a->tx_errors = b->tx_errors; 919b60c5115SThomas Graf a->rx_dropped = b->rx_dropped; 920b60c5115SThomas Graf a->tx_dropped = b->tx_dropped; 921b60c5115SThomas Graf 922b60c5115SThomas Graf a->multicast = b->multicast; 923b60c5115SThomas Graf a->collisions = b->collisions; 924b60c5115SThomas Graf 925b60c5115SThomas Graf a->rx_length_errors = b->rx_length_errors; 926b60c5115SThomas Graf a->rx_over_errors = b->rx_over_errors; 927b60c5115SThomas Graf a->rx_crc_errors = b->rx_crc_errors; 928b60c5115SThomas Graf a->rx_frame_errors = b->rx_frame_errors; 929b60c5115SThomas Graf a->rx_fifo_errors = b->rx_fifo_errors; 930b60c5115SThomas Graf a->rx_missed_errors = b->rx_missed_errors; 931b60c5115SThomas Graf 932b60c5115SThomas Graf a->tx_aborted_errors = b->tx_aborted_errors; 933b60c5115SThomas Graf a->tx_carrier_errors = b->tx_carrier_errors; 934b60c5115SThomas Graf a->tx_fifo_errors = b->tx_fifo_errors; 935b60c5115SThomas Graf a->tx_heartbeat_errors = b->tx_heartbeat_errors; 936b60c5115SThomas Graf a->tx_window_errors = b->tx_window_errors; 937b60c5115SThomas Graf 938b60c5115SThomas Graf a->rx_compressed = b->rx_compressed; 939b60c5115SThomas Graf a->tx_compressed = b->tx_compressed; 9406e7333d3SJarod Wilson 9416e7333d3SJarod Wilson a->rx_nohandler = b->rx_nohandler; 94210708f37SJan Engelhardt } 94310708f37SJan Engelhardt 944c02db8c6SChris Wright /* All VF info */ 945115c9b81SGreg Rose static inline int rtnl_vfinfo_size(const struct net_device *dev, 946115c9b81SGreg Rose u32 ext_filter_mask) 947ebc08a6fSWilliams, Mitch A { 9489af15c38SPhil Sutter if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) { 949c02db8c6SChris Wright int num_vfs = dev_num_vf(dev->dev.parent); 9507e75f74aSSabrina Dubroca size_t size = nla_total_size(0); 951045de01aSScott Feldman size += num_vfs * 9527e75f74aSSabrina Dubroca (nla_total_size(0) + 9537e75f74aSSabrina Dubroca nla_total_size(sizeof(struct ifla_vf_mac)) + 95475345f88SDenis Kirjanov nla_total_size(sizeof(struct ifla_vf_broadcast)) + 9557e75f74aSSabrina Dubroca nla_total_size(sizeof(struct ifla_vf_vlan)) + 9567e75f74aSSabrina Dubroca nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */ 95779aab093SMoshe Shemesh nla_total_size(MAX_VLAN_LIST_LEN * 95879aab093SMoshe Shemesh sizeof(struct ifla_vf_vlan_info)) + 959ed616689SSucheta Chakraborty nla_total_size(sizeof(struct ifla_vf_spoofchk)) + 9607e75f74aSSabrina Dubroca nla_total_size(sizeof(struct ifla_vf_tx_rate)) + 961945a3676SJiri Benc nla_total_size(sizeof(struct ifla_vf_rate)) + 96201a3d796SVlad Zolotarov nla_total_size(sizeof(struct ifla_vf_link_state)) + 9633b766cd8SEran Ben Elisha nla_total_size(sizeof(struct ifla_vf_rss_query_en)) + 964fa0e21faSEdwin Peer nla_total_size(sizeof(struct ifla_vf_trust))); 965fa0e21faSEdwin Peer if (~ext_filter_mask & RTEXT_FILTER_SKIP_STATS) { 966fa0e21faSEdwin Peer size += num_vfs * 967fa0e21faSEdwin Peer (nla_total_size(0) + /* nest IFLA_VF_STATS */ 9683b766cd8SEran Ben Elisha /* IFLA_VF_STATS_RX_PACKETS */ 969343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 9703b766cd8SEran Ben Elisha /* IFLA_VF_STATS_TX_PACKETS */ 971343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 9723b766cd8SEran Ben Elisha /* IFLA_VF_STATS_RX_BYTES */ 973343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 9743b766cd8SEran Ben Elisha /* IFLA_VF_STATS_TX_BYTES */ 975343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 9763b766cd8SEran Ben Elisha /* IFLA_VF_STATS_BROADCAST */ 977343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 9783b766cd8SEran Ben Elisha /* IFLA_VF_STATS_MULTICAST */ 979343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 980c5a9f6f0SEugenia Emantayev /* IFLA_VF_STATS_RX_DROPPED */ 981c5a9f6f0SEugenia Emantayev nla_total_size_64bit(sizeof(__u64)) + 982c5a9f6f0SEugenia Emantayev /* IFLA_VF_STATS_TX_DROPPED */ 983fa0e21faSEdwin Peer nla_total_size_64bit(sizeof(__u64))); 984fa0e21faSEdwin Peer } 985c02db8c6SChris Wright return size; 986c02db8c6SChris Wright } else 987ebc08a6fSWilliams, Mitch A return 0; 988ebc08a6fSWilliams, Mitch A } 989ebc08a6fSWilliams, Mitch A 990c53864fdSDavid Gibson static size_t rtnl_port_size(const struct net_device *dev, 991c53864fdSDavid Gibson u32 ext_filter_mask) 99257b61080SScott Feldman { 99357b61080SScott Feldman size_t port_size = nla_total_size(4) /* PORT_VF */ 99457b61080SScott Feldman + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */ 99557b61080SScott Feldman + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */ 99657b61080SScott Feldman + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */ 99757b61080SScott Feldman + nla_total_size(1) /* PROT_VDP_REQUEST */ 99857b61080SScott Feldman + nla_total_size(2); /* PORT_VDP_RESPONSE */ 99957b61080SScott Feldman size_t vf_ports_size = nla_total_size(sizeof(struct nlattr)); 100057b61080SScott Feldman size_t vf_port_size = nla_total_size(sizeof(struct nlattr)) 100157b61080SScott Feldman + port_size; 100257b61080SScott Feldman size_t port_self_size = nla_total_size(sizeof(struct nlattr)) 100357b61080SScott Feldman + port_size; 100457b61080SScott Feldman 1005c53864fdSDavid Gibson if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent || 1006c53864fdSDavid Gibson !(ext_filter_mask & RTEXT_FILTER_VF)) 100757b61080SScott Feldman return 0; 100857b61080SScott Feldman if (dev_num_vf(dev->dev.parent)) 100957b61080SScott Feldman return port_self_size + vf_ports_size + 101057b61080SScott Feldman vf_port_size * dev_num_vf(dev->dev.parent); 101157b61080SScott Feldman else 101257b61080SScott Feldman return port_self_size; 101357b61080SScott Feldman } 101457b61080SScott Feldman 1015b5cdae32SDavid S. Miller static size_t rtnl_xdp_size(void) 1016d1fdd913SBrenden Blanco { 1017b3cfaa31SSabrina Dubroca size_t xdp_size = nla_total_size(0) + /* nest IFLA_XDP */ 101858038695SMartin KaFai Lau nla_total_size(1) + /* XDP_ATTACHED */ 1019a25717d2SJakub Kicinski nla_total_size(4) + /* XDP_PROG_ID (or 1st mode) */ 10204f91da26SJakub Kicinski nla_total_size(4); /* XDP_<mode>_PROG_ID */ 1021d1fdd913SBrenden Blanco 1022d1fdd913SBrenden Blanco return xdp_size; 1023d1fdd913SBrenden Blanco } 1024d1fdd913SBrenden Blanco 102588f4fb0cSJiri Pirko static size_t rtnl_prop_list_size(const struct net_device *dev) 102688f4fb0cSJiri Pirko { 102788f4fb0cSJiri Pirko struct netdev_name_node *name_node; 102888f4fb0cSJiri Pirko size_t size; 102988f4fb0cSJiri Pirko 103088f4fb0cSJiri Pirko if (list_empty(&dev->name_node->list)) 103188f4fb0cSJiri Pirko return 0; 103288f4fb0cSJiri Pirko size = nla_total_size(0); 103388f4fb0cSJiri Pirko list_for_each_entry(name_node, &dev->name_node->list, list) 103488f4fb0cSJiri Pirko size += nla_total_size(ALTIFNAMSIZ); 103588f4fb0cSJiri Pirko return size; 103688f4fb0cSJiri Pirko } 103788f4fb0cSJiri Pirko 1038829eb208SRoopa Prabhu static size_t rtnl_proto_down_size(const struct net_device *dev) 1039829eb208SRoopa Prabhu { 1040829eb208SRoopa Prabhu size_t size = nla_total_size(1); 1041829eb208SRoopa Prabhu 1042829eb208SRoopa Prabhu if (dev->proto_down_reason) 1043829eb208SRoopa Prabhu size += nla_total_size(0) + nla_total_size(4); 1044829eb208SRoopa Prabhu 1045829eb208SRoopa Prabhu return size; 1046829eb208SRoopa Prabhu } 1047829eb208SRoopa Prabhu 1048dca56c30SJiri Pirko static size_t rtnl_devlink_port_size(const struct net_device *dev) 1049dca56c30SJiri Pirko { 1050dca56c30SJiri Pirko size_t size = nla_total_size(0); /* nest IFLA_DEVLINK_PORT */ 1051dca56c30SJiri Pirko 1052dca56c30SJiri Pirko if (dev->devlink_port) 1053dca56c30SJiri Pirko size += devlink_nl_port_handle_size(dev->devlink_port); 1054dca56c30SJiri Pirko 1055dca56c30SJiri Pirko return size; 1056dca56c30SJiri Pirko } 1057dca56c30SJiri Pirko 1058115c9b81SGreg Rose static noinline size_t if_nlmsg_size(const struct net_device *dev, 1059115c9b81SGreg Rose u32 ext_filter_mask) 1060339bf98fSThomas Graf { 1061339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 1062339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 10630b815a1aSStephen Hemminger + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */ 1064339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */ 1065270cb4d0SNicolas Dichtel + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap)) 1066339bf98fSThomas Graf + nla_total_size(sizeof(struct rtnl_link_stats)) 106735c58459SDavid S. Miller + nla_total_size_64bit(sizeof(struct rtnl_link_stats64)) 1068339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 1069339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */ 1070339bf98fSThomas Graf + nla_total_size(4) /* IFLA_TXQLEN */ 1071339bf98fSThomas Graf + nla_total_size(4) /* IFLA_WEIGHT */ 1072339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MTU */ 1073339bf98fSThomas Graf + nla_total_size(4) /* IFLA_LINK */ 1074339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MASTER */ 10759a57247fSJiri Pirko + nla_total_size(1) /* IFLA_CARRIER */ 1076edbc0bb3SBen Greear + nla_total_size(4) /* IFLA_PROMISCUITY */ 10777e6e1b57SNicolas Dichtel + nla_total_size(4) /* IFLA_ALLMULTI */ 107876ff5cc9SJiri Pirko + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */ 107976ff5cc9SJiri Pirko + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */ 10806919756cSTobias Klauser + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */ 10816919756cSTobias Klauser + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */ 1082eac1b93cSCoco Li + nla_total_size(4) /* IFLA_GRO_MAX_SIZE */ 10839eefedd5SXin Long + nla_total_size(4) /* IFLA_GSO_IPV4_MAX_SIZE */ 10849eefedd5SXin Long + nla_total_size(4) /* IFLA_GRO_IPV4_MAX_SIZE */ 108589527be8SEric Dumazet + nla_total_size(4) /* IFLA_TSO_MAX_SIZE */ 108689527be8SEric Dumazet + nla_total_size(4) /* IFLA_TSO_MAX_SEGS */ 1087339bf98fSThomas Graf + nla_total_size(1) /* IFLA_OPERSTATE */ 108838f7b870SPatrick McHardy + nla_total_size(1) /* IFLA_LINKMODE */ 10892d3b479dSdavid decotigny + nla_total_size(4) /* IFLA_CARRIER_CHANGES */ 1090d37512a2SNicolas Dichtel + nla_total_size(4) /* IFLA_LINK_NETNSID */ 1091db833d40SSerhey Popovych + nla_total_size(4) /* IFLA_GROUP */ 1092115c9b81SGreg Rose + nla_total_size(ext_filter_mask 1093115c9b81SGreg Rose & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */ 1094115c9b81SGreg Rose + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */ 1095c53864fdSDavid Gibson + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */ 1096f8ff182cSThomas Graf + rtnl_link_get_size(dev) /* IFLA_LINKINFO */ 1097b1974ed0SArad, Ronen + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */ 109882f28412SJiri Pirko + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */ 109988d6378bSAnuradha Karuppiah + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */ 1100c57c7a95SNicolas Dichtel + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */ 1101b5cdae32SDavid S. Miller + rtnl_xdp_size() /* IFLA_XDP */ 11023d3ea5afSVlad Yasevich + nla_total_size(4) /* IFLA_EVENT */ 11036621dd29SNicolas Dichtel + nla_total_size(4) /* IFLA_NEW_NETNSID */ 110438e01b30SNicolas Dichtel + nla_total_size(4) /* IFLA_NEW_IFINDEX */ 1105829eb208SRoopa Prabhu + rtnl_proto_down_size(dev) /* proto down */ 11067e4a8d5aSChristian Brauner + nla_total_size(4) /* IFLA_TARGET_NETNSID */ 1107b2d3bcfaSDavid Decotigny + nla_total_size(4) /* IFLA_CARRIER_UP_COUNT */ 1108b2d3bcfaSDavid Decotigny + nla_total_size(4) /* IFLA_CARRIER_DOWN_COUNT */ 11093e7a50ceSStephen Hemminger + nla_total_size(4) /* IFLA_MIN_MTU */ 11103e7a50ceSStephen Hemminger + nla_total_size(4) /* IFLA_MAX_MTU */ 111188f4fb0cSJiri Pirko + rtnl_prop_list_size(dev) 1112f74877a5SMichal Kubecek + nla_total_size(MAX_ADDR_LEN) /* IFLA_PERM_ADDRESS */ 1113dca56c30SJiri Pirko + rtnl_devlink_port_size(dev) 111479e1ad14SJiri Benc + 0; 1115339bf98fSThomas Graf } 1116339bf98fSThomas Graf 111757b61080SScott Feldman static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev) 111857b61080SScott Feldman { 111957b61080SScott Feldman struct nlattr *vf_ports; 112057b61080SScott Feldman struct nlattr *vf_port; 112157b61080SScott Feldman int vf; 112257b61080SScott Feldman int err; 112357b61080SScott Feldman 1124ae0be8deSMichal Kubecek vf_ports = nla_nest_start_noflag(skb, IFLA_VF_PORTS); 112557b61080SScott Feldman if (!vf_ports) 112657b61080SScott Feldman return -EMSGSIZE; 112757b61080SScott Feldman 112857b61080SScott Feldman for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) { 1129ae0be8deSMichal Kubecek vf_port = nla_nest_start_noflag(skb, IFLA_VF_PORT); 11308ca94183SScott Feldman if (!vf_port) 11318ca94183SScott Feldman goto nla_put_failure; 1132a6574349SDavid S. Miller if (nla_put_u32(skb, IFLA_PORT_VF, vf)) 1133a6574349SDavid S. Miller goto nla_put_failure; 113457b61080SScott Feldman err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb); 11358ca94183SScott Feldman if (err == -EMSGSIZE) 11368ca94183SScott Feldman goto nla_put_failure; 113757b61080SScott Feldman if (err) { 113857b61080SScott Feldman nla_nest_cancel(skb, vf_port); 113957b61080SScott Feldman continue; 114057b61080SScott Feldman } 114157b61080SScott Feldman nla_nest_end(skb, vf_port); 114257b61080SScott Feldman } 114357b61080SScott Feldman 114457b61080SScott Feldman nla_nest_end(skb, vf_ports); 114557b61080SScott Feldman 114657b61080SScott Feldman return 0; 11478ca94183SScott Feldman 11488ca94183SScott Feldman nla_put_failure: 11498ca94183SScott Feldman nla_nest_cancel(skb, vf_ports); 11508ca94183SScott Feldman return -EMSGSIZE; 115157b61080SScott Feldman } 115257b61080SScott Feldman 115357b61080SScott Feldman static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev) 115457b61080SScott Feldman { 115557b61080SScott Feldman struct nlattr *port_self; 115657b61080SScott Feldman int err; 115757b61080SScott Feldman 1158ae0be8deSMichal Kubecek port_self = nla_nest_start_noflag(skb, IFLA_PORT_SELF); 115957b61080SScott Feldman if (!port_self) 116057b61080SScott Feldman return -EMSGSIZE; 116157b61080SScott Feldman 116257b61080SScott Feldman err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb); 116357b61080SScott Feldman if (err) { 116457b61080SScott Feldman nla_nest_cancel(skb, port_self); 11658ca94183SScott Feldman return (err == -EMSGSIZE) ? err : 0; 116657b61080SScott Feldman } 116757b61080SScott Feldman 116857b61080SScott Feldman nla_nest_end(skb, port_self); 116957b61080SScott Feldman 117057b61080SScott Feldman return 0; 117157b61080SScott Feldman } 117257b61080SScott Feldman 1173c53864fdSDavid Gibson static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev, 1174c53864fdSDavid Gibson u32 ext_filter_mask) 117557b61080SScott Feldman { 117657b61080SScott Feldman int err; 117757b61080SScott Feldman 1178c53864fdSDavid Gibson if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent || 1179c53864fdSDavid Gibson !(ext_filter_mask & RTEXT_FILTER_VF)) 118057b61080SScott Feldman return 0; 118157b61080SScott Feldman 118257b61080SScott Feldman err = rtnl_port_self_fill(skb, dev); 118357b61080SScott Feldman if (err) 118457b61080SScott Feldman return err; 118557b61080SScott Feldman 118657b61080SScott Feldman if (dev_num_vf(dev->dev.parent)) { 118757b61080SScott Feldman err = rtnl_vf_ports_fill(skb, dev); 118857b61080SScott Feldman if (err) 118957b61080SScott Feldman return err; 119057b61080SScott Feldman } 119157b61080SScott Feldman 119257b61080SScott Feldman return 0; 119357b61080SScott Feldman } 119457b61080SScott Feldman 119566cae9edSJiri Pirko static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev) 119666cae9edSJiri Pirko { 119766cae9edSJiri Pirko int err; 119802637fceSJiri Pirko struct netdev_phys_item_id ppid; 119966cae9edSJiri Pirko 120066cae9edSJiri Pirko err = dev_get_phys_port_id(dev, &ppid); 120166cae9edSJiri Pirko if (err) { 120266cae9edSJiri Pirko if (err == -EOPNOTSUPP) 120366cae9edSJiri Pirko return 0; 120466cae9edSJiri Pirko return err; 120566cae9edSJiri Pirko } 120666cae9edSJiri Pirko 120766cae9edSJiri Pirko if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id)) 120866cae9edSJiri Pirko return -EMSGSIZE; 120966cae9edSJiri Pirko 121066cae9edSJiri Pirko return 0; 121166cae9edSJiri Pirko } 121266cae9edSJiri Pirko 1213db24a904SDavid Ahern static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev) 1214db24a904SDavid Ahern { 1215db24a904SDavid Ahern char name[IFNAMSIZ]; 1216db24a904SDavid Ahern int err; 1217db24a904SDavid Ahern 1218db24a904SDavid Ahern err = dev_get_phys_port_name(dev, name, sizeof(name)); 1219db24a904SDavid Ahern if (err) { 1220db24a904SDavid Ahern if (err == -EOPNOTSUPP) 1221db24a904SDavid Ahern return 0; 1222db24a904SDavid Ahern return err; 1223db24a904SDavid Ahern } 1224db24a904SDavid Ahern 122577ef033bSMichal Schmidt if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name)) 1226db24a904SDavid Ahern return -EMSGSIZE; 1227db24a904SDavid Ahern 1228db24a904SDavid Ahern return 0; 1229db24a904SDavid Ahern } 1230db24a904SDavid Ahern 123182f28412SJiri Pirko static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev) 123282f28412SJiri Pirko { 1233bccb3025SFlorian Fainelli struct netdev_phys_item_id ppid = { }; 123482f28412SJiri Pirko int err; 123582f28412SJiri Pirko 1236bccb3025SFlorian Fainelli err = dev_get_port_parent_id(dev, &ppid, false); 123782f28412SJiri Pirko if (err) { 123882f28412SJiri Pirko if (err == -EOPNOTSUPP) 123982f28412SJiri Pirko return 0; 124082f28412SJiri Pirko return err; 124182f28412SJiri Pirko } 124282f28412SJiri Pirko 1243bccb3025SFlorian Fainelli if (nla_put(skb, IFLA_PHYS_SWITCH_ID, ppid.id_len, ppid.id)) 124482f28412SJiri Pirko return -EMSGSIZE; 124582f28412SJiri Pirko 124682f28412SJiri Pirko return 0; 124782f28412SJiri Pirko } 124882f28412SJiri Pirko 1249b22b941bSHannes Frederic Sowa static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb, 1250b22b941bSHannes Frederic Sowa struct net_device *dev) 1251b22b941bSHannes Frederic Sowa { 1252550bce59SRoopa Prabhu struct rtnl_link_stats64 *sp; 1253b22b941bSHannes Frederic Sowa struct nlattr *attr; 1254b22b941bSHannes Frederic Sowa 125558414d32SNicolas Dichtel attr = nla_reserve_64bit(skb, IFLA_STATS64, 125658414d32SNicolas Dichtel sizeof(struct rtnl_link_stats64), IFLA_PAD); 1257b22b941bSHannes Frederic Sowa if (!attr) 1258b22b941bSHannes Frederic Sowa return -EMSGSIZE; 1259b22b941bSHannes Frederic Sowa 1260550bce59SRoopa Prabhu sp = nla_data(attr); 1261550bce59SRoopa Prabhu dev_get_stats(dev, sp); 1262550bce59SRoopa Prabhu 1263550bce59SRoopa Prabhu attr = nla_reserve(skb, IFLA_STATS, 1264550bce59SRoopa Prabhu sizeof(struct rtnl_link_stats)); 1265550bce59SRoopa Prabhu if (!attr) 1266550bce59SRoopa Prabhu return -EMSGSIZE; 1267550bce59SRoopa Prabhu 1268550bce59SRoopa Prabhu copy_rtnl_link_stats(nla_data(attr), sp); 1269b22b941bSHannes Frederic Sowa 1270b22b941bSHannes Frederic Sowa return 0; 1271b22b941bSHannes Frederic Sowa } 1272b22b941bSHannes Frederic Sowa 1273b22b941bSHannes Frederic Sowa static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb, 1274b22b941bSHannes Frederic Sowa struct net_device *dev, 1275b22b941bSHannes Frederic Sowa int vfs_num, 1276fa0e21faSEdwin Peer struct nlattr *vfinfo, 1277fa0e21faSEdwin Peer u32 ext_filter_mask) 1278b22b941bSHannes Frederic Sowa { 1279b22b941bSHannes Frederic Sowa struct ifla_vf_rss_query_en vf_rss_query_en; 128079aab093SMoshe Shemesh struct nlattr *vf, *vfstats, *vfvlanlist; 1281b22b941bSHannes Frederic Sowa struct ifla_vf_link_state vf_linkstate; 128279aab093SMoshe Shemesh struct ifla_vf_vlan_info vf_vlan_info; 1283b22b941bSHannes Frederic Sowa struct ifla_vf_spoofchk vf_spoofchk; 1284b22b941bSHannes Frederic Sowa struct ifla_vf_tx_rate vf_tx_rate; 1285b22b941bSHannes Frederic Sowa struct ifla_vf_stats vf_stats; 1286b22b941bSHannes Frederic Sowa struct ifla_vf_trust vf_trust; 1287b22b941bSHannes Frederic Sowa struct ifla_vf_vlan vf_vlan; 1288b22b941bSHannes Frederic Sowa struct ifla_vf_rate vf_rate; 1289b22b941bSHannes Frederic Sowa struct ifla_vf_mac vf_mac; 129075345f88SDenis Kirjanov struct ifla_vf_broadcast vf_broadcast; 1291b22b941bSHannes Frederic Sowa struct ifla_vf_info ivi; 129230aad417SDanit Goldberg struct ifla_vf_guid node_guid; 129330aad417SDanit Goldberg struct ifla_vf_guid port_guid; 1294b22b941bSHannes Frederic Sowa 12950eed9cf5SMintz, Yuval memset(&ivi, 0, sizeof(ivi)); 12960eed9cf5SMintz, Yuval 1297b22b941bSHannes Frederic Sowa /* Not all SR-IOV capable drivers support the 1298b22b941bSHannes Frederic Sowa * spoofcheck and "RSS query enable" query. Preset to 1299b22b941bSHannes Frederic Sowa * -1 so the user space tool can detect that the driver 1300b22b941bSHannes Frederic Sowa * didn't report anything. 1301b22b941bSHannes Frederic Sowa */ 1302b22b941bSHannes Frederic Sowa ivi.spoofchk = -1; 1303b22b941bSHannes Frederic Sowa ivi.rss_query_en = -1; 1304b22b941bSHannes Frederic Sowa ivi.trusted = -1; 1305b22b941bSHannes Frederic Sowa /* The default value for VF link state is "auto" 1306b22b941bSHannes Frederic Sowa * IFLA_VF_LINK_STATE_AUTO which equals zero 1307b22b941bSHannes Frederic Sowa */ 1308b22b941bSHannes Frederic Sowa ivi.linkstate = 0; 130979aab093SMoshe Shemesh /* VLAN Protocol by default is 802.1Q */ 131079aab093SMoshe Shemesh ivi.vlan_proto = htons(ETH_P_8021Q); 1311b22b941bSHannes Frederic Sowa if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi)) 1312b22b941bSHannes Frederic Sowa return 0; 1313b22b941bSHannes Frederic Sowa 1314775f4f05SDan Carpenter memset(&vf_vlan_info, 0, sizeof(vf_vlan_info)); 13159fbf082fSLeon Romanovsky memset(&node_guid, 0, sizeof(node_guid)); 13169fbf082fSLeon Romanovsky memset(&port_guid, 0, sizeof(port_guid)); 1317775f4f05SDan Carpenter 1318b22b941bSHannes Frederic Sowa vf_mac.vf = 1319b22b941bSHannes Frederic Sowa vf_vlan.vf = 132079aab093SMoshe Shemesh vf_vlan_info.vf = 1321b22b941bSHannes Frederic Sowa vf_rate.vf = 1322b22b941bSHannes Frederic Sowa vf_tx_rate.vf = 1323b22b941bSHannes Frederic Sowa vf_spoofchk.vf = 1324b22b941bSHannes Frederic Sowa vf_linkstate.vf = 1325b22b941bSHannes Frederic Sowa vf_rss_query_en.vf = 13269aed6ae0SDanit Goldberg vf_trust.vf = 13279aed6ae0SDanit Goldberg node_guid.vf = 13289aed6ae0SDanit Goldberg port_guid.vf = ivi.vf; 1329b22b941bSHannes Frederic Sowa 1330b22b941bSHannes Frederic Sowa memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac)); 133175345f88SDenis Kirjanov memcpy(vf_broadcast.broadcast, dev->broadcast, dev->addr_len); 1332b22b941bSHannes Frederic Sowa vf_vlan.vlan = ivi.vlan; 1333b22b941bSHannes Frederic Sowa vf_vlan.qos = ivi.qos; 133479aab093SMoshe Shemesh vf_vlan_info.vlan = ivi.vlan; 133579aab093SMoshe Shemesh vf_vlan_info.qos = ivi.qos; 133679aab093SMoshe Shemesh vf_vlan_info.vlan_proto = ivi.vlan_proto; 1337b22b941bSHannes Frederic Sowa vf_tx_rate.rate = ivi.max_tx_rate; 1338b22b941bSHannes Frederic Sowa vf_rate.min_tx_rate = ivi.min_tx_rate; 1339b22b941bSHannes Frederic Sowa vf_rate.max_tx_rate = ivi.max_tx_rate; 1340b22b941bSHannes Frederic Sowa vf_spoofchk.setting = ivi.spoofchk; 1341b22b941bSHannes Frederic Sowa vf_linkstate.link_state = ivi.linkstate; 1342b22b941bSHannes Frederic Sowa vf_rss_query_en.setting = ivi.rss_query_en; 1343b22b941bSHannes Frederic Sowa vf_trust.setting = ivi.trusted; 1344ae0be8deSMichal Kubecek vf = nla_nest_start_noflag(skb, IFLA_VF_INFO); 134579aab093SMoshe Shemesh if (!vf) 134679aab093SMoshe Shemesh goto nla_put_vfinfo_failure; 1347b22b941bSHannes Frederic Sowa if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) || 134875345f88SDenis Kirjanov nla_put(skb, IFLA_VF_BROADCAST, sizeof(vf_broadcast), &vf_broadcast) || 1349b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) || 1350b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate), 1351b22b941bSHannes Frederic Sowa &vf_rate) || 1352b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate), 1353b22b941bSHannes Frederic Sowa &vf_tx_rate) || 1354b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk), 1355b22b941bSHannes Frederic Sowa &vf_spoofchk) || 1356b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate), 1357b22b941bSHannes Frederic Sowa &vf_linkstate) || 1358b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_RSS_QUERY_EN, 1359b22b941bSHannes Frederic Sowa sizeof(vf_rss_query_en), 1360b22b941bSHannes Frederic Sowa &vf_rss_query_en) || 1361b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_TRUST, 1362b22b941bSHannes Frederic Sowa sizeof(vf_trust), &vf_trust)) 136379aab093SMoshe Shemesh goto nla_put_vf_failure; 136430aad417SDanit Goldberg 136530aad417SDanit Goldberg if (dev->netdev_ops->ndo_get_vf_guid && 136630aad417SDanit Goldberg !dev->netdev_ops->ndo_get_vf_guid(dev, vfs_num, &node_guid, 136730aad417SDanit Goldberg &port_guid)) { 136830aad417SDanit Goldberg if (nla_put(skb, IFLA_VF_IB_NODE_GUID, sizeof(node_guid), 136930aad417SDanit Goldberg &node_guid) || 137030aad417SDanit Goldberg nla_put(skb, IFLA_VF_IB_PORT_GUID, sizeof(port_guid), 137130aad417SDanit Goldberg &port_guid)) 137230aad417SDanit Goldberg goto nla_put_vf_failure; 137330aad417SDanit Goldberg } 1374ae0be8deSMichal Kubecek vfvlanlist = nla_nest_start_noflag(skb, IFLA_VF_VLAN_LIST); 137579aab093SMoshe Shemesh if (!vfvlanlist) 137679aab093SMoshe Shemesh goto nla_put_vf_failure; 137779aab093SMoshe Shemesh if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info), 137879aab093SMoshe Shemesh &vf_vlan_info)) { 137979aab093SMoshe Shemesh nla_nest_cancel(skb, vfvlanlist); 138079aab093SMoshe Shemesh goto nla_put_vf_failure; 138179aab093SMoshe Shemesh } 138279aab093SMoshe Shemesh nla_nest_end(skb, vfvlanlist); 1383fa0e21faSEdwin Peer if (~ext_filter_mask & RTEXT_FILTER_SKIP_STATS) { 1384b22b941bSHannes Frederic Sowa memset(&vf_stats, 0, sizeof(vf_stats)); 1385b22b941bSHannes Frederic Sowa if (dev->netdev_ops->ndo_get_vf_stats) 1386b22b941bSHannes Frederic Sowa dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num, 1387b22b941bSHannes Frederic Sowa &vf_stats); 1388ae0be8deSMichal Kubecek vfstats = nla_nest_start_noflag(skb, IFLA_VF_STATS); 138979aab093SMoshe Shemesh if (!vfstats) 139079aab093SMoshe Shemesh goto nla_put_vf_failure; 1391343a6d8eSNicolas Dichtel if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS, 1392343a6d8eSNicolas Dichtel vf_stats.rx_packets, IFLA_VF_STATS_PAD) || 1393343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS, 1394343a6d8eSNicolas Dichtel vf_stats.tx_packets, IFLA_VF_STATS_PAD) || 1395343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES, 1396343a6d8eSNicolas Dichtel vf_stats.rx_bytes, IFLA_VF_STATS_PAD) || 1397343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES, 1398343a6d8eSNicolas Dichtel vf_stats.tx_bytes, IFLA_VF_STATS_PAD) || 1399343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST, 1400343a6d8eSNicolas Dichtel vf_stats.broadcast, IFLA_VF_STATS_PAD) || 1401343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST, 1402c5a9f6f0SEugenia Emantayev vf_stats.multicast, IFLA_VF_STATS_PAD) || 1403c5a9f6f0SEugenia Emantayev nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED, 1404c5a9f6f0SEugenia Emantayev vf_stats.rx_dropped, IFLA_VF_STATS_PAD) || 1405c5a9f6f0SEugenia Emantayev nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED, 1406c5a9f6f0SEugenia Emantayev vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) { 140779aab093SMoshe Shemesh nla_nest_cancel(skb, vfstats); 140879aab093SMoshe Shemesh goto nla_put_vf_failure; 140979aab093SMoshe Shemesh } 1410b22b941bSHannes Frederic Sowa nla_nest_end(skb, vfstats); 1411fa0e21faSEdwin Peer } 1412b22b941bSHannes Frederic Sowa nla_nest_end(skb, vf); 1413b22b941bSHannes Frederic Sowa return 0; 141479aab093SMoshe Shemesh 141579aab093SMoshe Shemesh nla_put_vf_failure: 141679aab093SMoshe Shemesh nla_nest_cancel(skb, vf); 141779aab093SMoshe Shemesh nla_put_vfinfo_failure: 141879aab093SMoshe Shemesh nla_nest_cancel(skb, vfinfo); 141979aab093SMoshe Shemesh return -EMSGSIZE; 1420b22b941bSHannes Frederic Sowa } 1421b22b941bSHannes Frederic Sowa 1422250fc3dfSFlorian Westphal static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb, 1423250fc3dfSFlorian Westphal struct net_device *dev, 1424250fc3dfSFlorian Westphal u32 ext_filter_mask) 1425250fc3dfSFlorian Westphal { 1426250fc3dfSFlorian Westphal struct nlattr *vfinfo; 1427250fc3dfSFlorian Westphal int i, num_vfs; 1428250fc3dfSFlorian Westphal 1429250fc3dfSFlorian Westphal if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0)) 1430250fc3dfSFlorian Westphal return 0; 1431250fc3dfSFlorian Westphal 1432250fc3dfSFlorian Westphal num_vfs = dev_num_vf(dev->dev.parent); 1433250fc3dfSFlorian Westphal if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs)) 1434250fc3dfSFlorian Westphal return -EMSGSIZE; 1435250fc3dfSFlorian Westphal 1436250fc3dfSFlorian Westphal if (!dev->netdev_ops->ndo_get_vf_config) 1437250fc3dfSFlorian Westphal return 0; 1438250fc3dfSFlorian Westphal 1439ae0be8deSMichal Kubecek vfinfo = nla_nest_start_noflag(skb, IFLA_VFINFO_LIST); 1440250fc3dfSFlorian Westphal if (!vfinfo) 1441250fc3dfSFlorian Westphal return -EMSGSIZE; 1442250fc3dfSFlorian Westphal 1443250fc3dfSFlorian Westphal for (i = 0; i < num_vfs; i++) { 1444fa0e21faSEdwin Peer if (rtnl_fill_vfinfo(skb, dev, i, vfinfo, ext_filter_mask)) 1445250fc3dfSFlorian Westphal return -EMSGSIZE; 1446250fc3dfSFlorian Westphal } 1447250fc3dfSFlorian Westphal 1448250fc3dfSFlorian Westphal nla_nest_end(skb, vfinfo); 1449250fc3dfSFlorian Westphal return 0; 1450250fc3dfSFlorian Westphal } 1451250fc3dfSFlorian Westphal 1452b22b941bSHannes Frederic Sowa static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev) 1453b22b941bSHannes Frederic Sowa { 14545f8e4474SKangjie Lu struct rtnl_link_ifmap map; 14555f8e4474SKangjie Lu 14565f8e4474SKangjie Lu memset(&map, 0, sizeof(map)); 14575f8e4474SKangjie Lu map.mem_start = dev->mem_start; 14585f8e4474SKangjie Lu map.mem_end = dev->mem_end; 14595f8e4474SKangjie Lu map.base_addr = dev->base_addr; 14605f8e4474SKangjie Lu map.irq = dev->irq; 14615f8e4474SKangjie Lu map.dma = dev->dma; 14625f8e4474SKangjie Lu map.port = dev->if_port; 14635f8e4474SKangjie Lu 1464270cb4d0SNicolas Dichtel if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD)) 1465b22b941bSHannes Frederic Sowa return -EMSGSIZE; 1466b22b941bSHannes Frederic Sowa 1467b22b941bSHannes Frederic Sowa return 0; 1468b22b941bSHannes Frederic Sowa } 1469b22b941bSHannes Frederic Sowa 1470a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_skb(struct net_device *dev) 1471d67b9cd2SDaniel Borkmann { 147258038695SMartin KaFai Lau const struct bpf_prog *generic_xdp_prog; 1473d67b9cd2SDaniel Borkmann 1474d67b9cd2SDaniel Borkmann ASSERT_RTNL(); 1475d67b9cd2SDaniel Borkmann 147658038695SMartin KaFai Lau generic_xdp_prog = rtnl_dereference(dev->xdp_prog); 1477a25717d2SJakub Kicinski if (!generic_xdp_prog) 1478a25717d2SJakub Kicinski return 0; 1479a25717d2SJakub Kicinski return generic_xdp_prog->aux->id; 148058038695SMartin KaFai Lau } 1481ce158e58SJakub Kicinski 1482a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_drv(struct net_device *dev) 1483a25717d2SJakub Kicinski { 14847f0a8382SAndrii Nakryiko return dev_xdp_prog_id(dev, XDP_MODE_DRV); 1485a25717d2SJakub Kicinski } 1486118b4aa2SJakub Kicinski 1487a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_hw(struct net_device *dev) 1488a25717d2SJakub Kicinski { 14897f0a8382SAndrii Nakryiko return dev_xdp_prog_id(dev, XDP_MODE_HW); 1490a25717d2SJakub Kicinski } 1491a25717d2SJakub Kicinski 1492a25717d2SJakub Kicinski static int rtnl_xdp_report_one(struct sk_buff *skb, struct net_device *dev, 1493a25717d2SJakub Kicinski u32 *prog_id, u8 *mode, u8 tgt_mode, u32 attr, 1494a25717d2SJakub Kicinski u32 (*get_prog_id)(struct net_device *dev)) 1495a25717d2SJakub Kicinski { 1496a25717d2SJakub Kicinski u32 curr_id; 1497a25717d2SJakub Kicinski int err; 1498a25717d2SJakub Kicinski 1499a25717d2SJakub Kicinski curr_id = get_prog_id(dev); 1500a25717d2SJakub Kicinski if (!curr_id) 1501a25717d2SJakub Kicinski return 0; 1502a25717d2SJakub Kicinski 1503a25717d2SJakub Kicinski *prog_id = curr_id; 1504a25717d2SJakub Kicinski err = nla_put_u32(skb, attr, curr_id); 1505a25717d2SJakub Kicinski if (err) 1506a25717d2SJakub Kicinski return err; 1507a25717d2SJakub Kicinski 1508a25717d2SJakub Kicinski if (*mode != XDP_ATTACHED_NONE) 1509a25717d2SJakub Kicinski *mode = XDP_ATTACHED_MULTI; 1510a25717d2SJakub Kicinski else 1511a25717d2SJakub Kicinski *mode = tgt_mode; 1512a25717d2SJakub Kicinski 1513a25717d2SJakub Kicinski return 0; 1514d67b9cd2SDaniel Borkmann } 1515d67b9cd2SDaniel Borkmann 1516d1fdd913SBrenden Blanco static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev) 1517d1fdd913SBrenden Blanco { 1518d1fdd913SBrenden Blanco struct nlattr *xdp; 151958038695SMartin KaFai Lau u32 prog_id; 1520d1fdd913SBrenden Blanco int err; 15214f91da26SJakub Kicinski u8 mode; 1522d1fdd913SBrenden Blanco 1523ae0be8deSMichal Kubecek xdp = nla_nest_start_noflag(skb, IFLA_XDP); 1524d1fdd913SBrenden Blanco if (!xdp) 1525d1fdd913SBrenden Blanco return -EMSGSIZE; 1526b5cdae32SDavid S. Miller 1527a25717d2SJakub Kicinski prog_id = 0; 1528a25717d2SJakub Kicinski mode = XDP_ATTACHED_NONE; 1529202aabe8SJakub Kicinski err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_SKB, 1530202aabe8SJakub Kicinski IFLA_XDP_SKB_PROG_ID, rtnl_xdp_prog_skb); 1531202aabe8SJakub Kicinski if (err) 1532a25717d2SJakub Kicinski goto err_cancel; 1533202aabe8SJakub Kicinski err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_DRV, 1534202aabe8SJakub Kicinski IFLA_XDP_DRV_PROG_ID, rtnl_xdp_prog_drv); 1535202aabe8SJakub Kicinski if (err) 1536a25717d2SJakub Kicinski goto err_cancel; 1537202aabe8SJakub Kicinski err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_HW, 1538202aabe8SJakub Kicinski IFLA_XDP_HW_PROG_ID, rtnl_xdp_prog_hw); 1539202aabe8SJakub Kicinski if (err) 1540a25717d2SJakub Kicinski goto err_cancel; 1541a25717d2SJakub Kicinski 15424f91da26SJakub Kicinski err = nla_put_u8(skb, IFLA_XDP_ATTACHED, mode); 1543d1fdd913SBrenden Blanco if (err) 1544d1fdd913SBrenden Blanco goto err_cancel; 1545d1fdd913SBrenden Blanco 1546a25717d2SJakub Kicinski if (prog_id && mode != XDP_ATTACHED_MULTI) { 154758038695SMartin KaFai Lau err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id); 154858038695SMartin KaFai Lau if (err) 154958038695SMartin KaFai Lau goto err_cancel; 155058038695SMartin KaFai Lau } 155158038695SMartin KaFai Lau 1552d1fdd913SBrenden Blanco nla_nest_end(skb, xdp); 1553d1fdd913SBrenden Blanco return 0; 1554d1fdd913SBrenden Blanco 1555d1fdd913SBrenden Blanco err_cancel: 1556d1fdd913SBrenden Blanco nla_nest_cancel(skb, xdp); 1557d1fdd913SBrenden Blanco return err; 1558d1fdd913SBrenden Blanco } 1559d1fdd913SBrenden Blanco 15603d3ea5afSVlad Yasevich static u32 rtnl_get_event(unsigned long event) 15613d3ea5afSVlad Yasevich { 15623d3ea5afSVlad Yasevich u32 rtnl_event_type = IFLA_EVENT_NONE; 15633d3ea5afSVlad Yasevich 15643d3ea5afSVlad Yasevich switch (event) { 15653d3ea5afSVlad Yasevich case NETDEV_REBOOT: 15663d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_REBOOT; 15673d3ea5afSVlad Yasevich break; 15683d3ea5afSVlad Yasevich case NETDEV_FEAT_CHANGE: 15693d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_FEATURES; 15703d3ea5afSVlad Yasevich break; 15713d3ea5afSVlad Yasevich case NETDEV_BONDING_FAILOVER: 15723d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER; 15733d3ea5afSVlad Yasevich break; 15743d3ea5afSVlad Yasevich case NETDEV_NOTIFY_PEERS: 15753d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS; 15763d3ea5afSVlad Yasevich break; 15773d3ea5afSVlad Yasevich case NETDEV_RESEND_IGMP: 15783d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_IGMP_RESEND; 15793d3ea5afSVlad Yasevich break; 15803d3ea5afSVlad Yasevich case NETDEV_CHANGEINFODATA: 15813d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS; 15823d3ea5afSVlad Yasevich break; 15833d3ea5afSVlad Yasevich default: 15843d3ea5afSVlad Yasevich break; 15853d3ea5afSVlad Yasevich } 15863d3ea5afSVlad Yasevich 15873d3ea5afSVlad Yasevich return rtnl_event_type; 15883d3ea5afSVlad Yasevich } 15893d3ea5afSVlad Yasevich 159079110a04SFlorian Westphal static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev) 159179110a04SFlorian Westphal { 159279110a04SFlorian Westphal const struct net_device *upper_dev; 159379110a04SFlorian Westphal int ret = 0; 159479110a04SFlorian Westphal 159579110a04SFlorian Westphal rcu_read_lock(); 159679110a04SFlorian Westphal 159779110a04SFlorian Westphal upper_dev = netdev_master_upper_dev_get_rcu(dev); 159879110a04SFlorian Westphal if (upper_dev) 159979110a04SFlorian Westphal ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex); 160079110a04SFlorian Westphal 160179110a04SFlorian Westphal rcu_read_unlock(); 160279110a04SFlorian Westphal return ret; 160379110a04SFlorian Westphal } 160479110a04SFlorian Westphal 1605feadc4b6SSabrina Dubroca static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev, 1606feadc4b6SSabrina Dubroca bool force) 160779110a04SFlorian Westphal { 160879110a04SFlorian Westphal int ifindex = dev_get_iflink(dev); 160979110a04SFlorian Westphal 1610feadc4b6SSabrina Dubroca if (force || dev->ifindex != ifindex) 161179110a04SFlorian Westphal return nla_put_u32(skb, IFLA_LINK, ifindex); 1612feadc4b6SSabrina Dubroca 1613feadc4b6SSabrina Dubroca return 0; 161479110a04SFlorian Westphal } 161579110a04SFlorian Westphal 16166c557001SFlorian Westphal static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb, 16176c557001SFlorian Westphal struct net_device *dev) 16186c557001SFlorian Westphal { 16196c557001SFlorian Westphal char buf[IFALIASZ]; 16206c557001SFlorian Westphal int ret; 16216c557001SFlorian Westphal 16226c557001SFlorian Westphal ret = dev_get_alias(dev, buf, sizeof(buf)); 16236c557001SFlorian Westphal return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0; 16246c557001SFlorian Westphal } 16256c557001SFlorian Westphal 1626b1e66b9aSFlorian Westphal static int rtnl_fill_link_netnsid(struct sk_buff *skb, 162779e1ad14SJiri Benc const struct net_device *dev, 1628d4e4fdf9SGuillaume Nault struct net *src_net, gfp_t gfp) 1629b1e66b9aSFlorian Westphal { 1630feadc4b6SSabrina Dubroca bool put_iflink = false; 1631feadc4b6SSabrina Dubroca 1632b1e66b9aSFlorian Westphal if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) { 1633b1e66b9aSFlorian Westphal struct net *link_net = dev->rtnl_link_ops->get_link_net(dev); 1634b1e66b9aSFlorian Westphal 1635b1e66b9aSFlorian Westphal if (!net_eq(dev_net(dev), link_net)) { 1636d4e4fdf9SGuillaume Nault int id = peernet2id_alloc(src_net, link_net, gfp); 1637b1e66b9aSFlorian Westphal 1638b1e66b9aSFlorian Westphal if (nla_put_s32(skb, IFLA_LINK_NETNSID, id)) 1639b1e66b9aSFlorian Westphal return -EMSGSIZE; 1640feadc4b6SSabrina Dubroca 1641feadc4b6SSabrina Dubroca put_iflink = true; 1642b1e66b9aSFlorian Westphal } 1643b1e66b9aSFlorian Westphal } 1644b1e66b9aSFlorian Westphal 1645feadc4b6SSabrina Dubroca return nla_put_iflink(skb, dev, put_iflink); 1646b1e66b9aSFlorian Westphal } 1647b1e66b9aSFlorian Westphal 1648070cbf5bSFlorian Westphal static int rtnl_fill_link_af(struct sk_buff *skb, 1649070cbf5bSFlorian Westphal const struct net_device *dev, 1650070cbf5bSFlorian Westphal u32 ext_filter_mask) 1651070cbf5bSFlorian Westphal { 1652070cbf5bSFlorian Westphal const struct rtnl_af_ops *af_ops; 1653070cbf5bSFlorian Westphal struct nlattr *af_spec; 1654070cbf5bSFlorian Westphal 1655ae0be8deSMichal Kubecek af_spec = nla_nest_start_noflag(skb, IFLA_AF_SPEC); 1656070cbf5bSFlorian Westphal if (!af_spec) 1657070cbf5bSFlorian Westphal return -EMSGSIZE; 1658070cbf5bSFlorian Westphal 16595fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 1660070cbf5bSFlorian Westphal struct nlattr *af; 1661070cbf5bSFlorian Westphal int err; 1662070cbf5bSFlorian Westphal 1663070cbf5bSFlorian Westphal if (!af_ops->fill_link_af) 1664070cbf5bSFlorian Westphal continue; 1665070cbf5bSFlorian Westphal 1666ae0be8deSMichal Kubecek af = nla_nest_start_noflag(skb, af_ops->family); 1667070cbf5bSFlorian Westphal if (!af) 1668070cbf5bSFlorian Westphal return -EMSGSIZE; 1669070cbf5bSFlorian Westphal 1670070cbf5bSFlorian Westphal err = af_ops->fill_link_af(skb, dev, ext_filter_mask); 1671070cbf5bSFlorian Westphal /* 1672070cbf5bSFlorian Westphal * Caller may return ENODATA to indicate that there 1673070cbf5bSFlorian Westphal * was no data to be dumped. This is not an error, it 1674070cbf5bSFlorian Westphal * means we should trim the attribute header and 1675070cbf5bSFlorian Westphal * continue. 1676070cbf5bSFlorian Westphal */ 1677070cbf5bSFlorian Westphal if (err == -ENODATA) 1678070cbf5bSFlorian Westphal nla_nest_cancel(skb, af); 1679070cbf5bSFlorian Westphal else if (err < 0) 1680070cbf5bSFlorian Westphal return -EMSGSIZE; 1681070cbf5bSFlorian Westphal 1682070cbf5bSFlorian Westphal nla_nest_end(skb, af); 1683070cbf5bSFlorian Westphal } 1684070cbf5bSFlorian Westphal 1685070cbf5bSFlorian Westphal nla_nest_end(skb, af_spec); 1686070cbf5bSFlorian Westphal return 0; 1687070cbf5bSFlorian Westphal } 1688070cbf5bSFlorian Westphal 168988f4fb0cSJiri Pirko static int rtnl_fill_alt_ifnames(struct sk_buff *skb, 169088f4fb0cSJiri Pirko const struct net_device *dev) 169188f4fb0cSJiri Pirko { 169288f4fb0cSJiri Pirko struct netdev_name_node *name_node; 169388f4fb0cSJiri Pirko int count = 0; 169488f4fb0cSJiri Pirko 169588f4fb0cSJiri Pirko list_for_each_entry(name_node, &dev->name_node->list, list) { 169688f4fb0cSJiri Pirko if (nla_put_string(skb, IFLA_ALT_IFNAME, name_node->name)) 169788f4fb0cSJiri Pirko return -EMSGSIZE; 169888f4fb0cSJiri Pirko count++; 169988f4fb0cSJiri Pirko } 170088f4fb0cSJiri Pirko return count; 170188f4fb0cSJiri Pirko } 170288f4fb0cSJiri Pirko 170388f4fb0cSJiri Pirko static int rtnl_fill_prop_list(struct sk_buff *skb, 170488f4fb0cSJiri Pirko const struct net_device *dev) 170588f4fb0cSJiri Pirko { 170688f4fb0cSJiri Pirko struct nlattr *prop_list; 170788f4fb0cSJiri Pirko int ret; 170888f4fb0cSJiri Pirko 170988f4fb0cSJiri Pirko prop_list = nla_nest_start(skb, IFLA_PROP_LIST); 171088f4fb0cSJiri Pirko if (!prop_list) 171188f4fb0cSJiri Pirko return -EMSGSIZE; 171288f4fb0cSJiri Pirko 171388f4fb0cSJiri Pirko ret = rtnl_fill_alt_ifnames(skb, dev); 171488f4fb0cSJiri Pirko if (ret <= 0) 171588f4fb0cSJiri Pirko goto nest_cancel; 171688f4fb0cSJiri Pirko 171788f4fb0cSJiri Pirko nla_nest_end(skb, prop_list); 171888f4fb0cSJiri Pirko return 0; 171988f4fb0cSJiri Pirko 172088f4fb0cSJiri Pirko nest_cancel: 172188f4fb0cSJiri Pirko nla_nest_cancel(skb, prop_list); 172288f4fb0cSJiri Pirko return ret; 172388f4fb0cSJiri Pirko } 172488f4fb0cSJiri Pirko 1725829eb208SRoopa Prabhu static int rtnl_fill_proto_down(struct sk_buff *skb, 1726829eb208SRoopa Prabhu const struct net_device *dev) 1727829eb208SRoopa Prabhu { 1728829eb208SRoopa Prabhu struct nlattr *pr; 1729829eb208SRoopa Prabhu u32 preason; 1730829eb208SRoopa Prabhu 1731829eb208SRoopa Prabhu if (nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down)) 1732829eb208SRoopa Prabhu goto nla_put_failure; 1733829eb208SRoopa Prabhu 1734829eb208SRoopa Prabhu preason = dev->proto_down_reason; 1735829eb208SRoopa Prabhu if (!preason) 1736829eb208SRoopa Prabhu return 0; 1737829eb208SRoopa Prabhu 1738829eb208SRoopa Prabhu pr = nla_nest_start(skb, IFLA_PROTO_DOWN_REASON); 1739829eb208SRoopa Prabhu if (!pr) 1740829eb208SRoopa Prabhu return -EMSGSIZE; 1741829eb208SRoopa Prabhu 1742829eb208SRoopa Prabhu if (nla_put_u32(skb, IFLA_PROTO_DOWN_REASON_VALUE, preason)) { 1743829eb208SRoopa Prabhu nla_nest_cancel(skb, pr); 1744829eb208SRoopa Prabhu goto nla_put_failure; 1745829eb208SRoopa Prabhu } 1746829eb208SRoopa Prabhu 1747829eb208SRoopa Prabhu nla_nest_end(skb, pr); 1748829eb208SRoopa Prabhu return 0; 1749829eb208SRoopa Prabhu 1750829eb208SRoopa Prabhu nla_put_failure: 1751829eb208SRoopa Prabhu return -EMSGSIZE; 1752829eb208SRoopa Prabhu } 1753829eb208SRoopa Prabhu 1754dca56c30SJiri Pirko static int rtnl_fill_devlink_port(struct sk_buff *skb, 1755dca56c30SJiri Pirko const struct net_device *dev) 1756dca56c30SJiri Pirko { 1757dca56c30SJiri Pirko struct nlattr *devlink_port_nest; 1758dca56c30SJiri Pirko int ret; 1759dca56c30SJiri Pirko 1760dca56c30SJiri Pirko devlink_port_nest = nla_nest_start(skb, IFLA_DEVLINK_PORT); 1761dca56c30SJiri Pirko if (!devlink_port_nest) 1762dca56c30SJiri Pirko return -EMSGSIZE; 1763dca56c30SJiri Pirko 1764dca56c30SJiri Pirko if (dev->devlink_port) { 1765dca56c30SJiri Pirko ret = devlink_nl_port_handle_fill(skb, dev->devlink_port); 1766dca56c30SJiri Pirko if (ret < 0) 1767dca56c30SJiri Pirko goto nest_cancel; 1768dca56c30SJiri Pirko } 1769dca56c30SJiri Pirko 1770dca56c30SJiri Pirko nla_nest_end(skb, devlink_port_nest); 1771dca56c30SJiri Pirko return 0; 1772dca56c30SJiri Pirko 1773dca56c30SJiri Pirko nest_cancel: 1774dca56c30SJiri Pirko nla_nest_cancel(skb, devlink_port_nest); 1775dca56c30SJiri Pirko return ret; 1776dca56c30SJiri Pirko } 1777dca56c30SJiri Pirko 177879e1ad14SJiri Benc static int rtnl_fill_ifinfo(struct sk_buff *skb, 177979e1ad14SJiri Benc struct net_device *dev, struct net *src_net, 1780575c3e2aSPatrick McHardy int type, u32 pid, u32 seq, u32 change, 17813d3ea5afSVlad Yasevich unsigned int flags, u32 ext_filter_mask, 178238e01b30SNicolas Dichtel u32 event, int *new_nsid, int new_ifindex, 1783d4e4fdf9SGuillaume Nault int tgt_netnsid, gfp_t gfp) 1784b60c5115SThomas Graf { 1785b60c5115SThomas Graf struct ifinfomsg *ifm; 17861da177e4SLinus Torvalds struct nlmsghdr *nlh; 17875891cd5eSEric Dumazet struct Qdisc *qdisc; 17881da177e4SLinus Torvalds 17892907c35fSEric Dumazet ASSERT_RTNL(); 1790b60c5115SThomas Graf nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags); 1791b60c5115SThomas Graf if (nlh == NULL) 179226932566SPatrick McHardy return -EMSGSIZE; 17931da177e4SLinus Torvalds 1794b60c5115SThomas Graf ifm = nlmsg_data(nlh); 1795b60c5115SThomas Graf ifm->ifi_family = AF_UNSPEC; 1796b60c5115SThomas Graf ifm->__ifi_pad = 0; 1797b60c5115SThomas Graf ifm->ifi_type = dev->type; 1798b60c5115SThomas Graf ifm->ifi_index = dev->ifindex; 1799b60c5115SThomas Graf ifm->ifi_flags = dev_get_flags(dev); 1800b60c5115SThomas Graf ifm->ifi_change = change; 18011da177e4SLinus Torvalds 18027e4a8d5aSChristian Brauner if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_TARGET_NETNSID, tgt_netnsid)) 180379e1ad14SJiri Benc goto nla_put_failure; 180479e1ad14SJiri Benc 18055891cd5eSEric Dumazet qdisc = rtnl_dereference(dev->qdisc); 1806a6574349SDavid S. Miller if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 1807a6574349SDavid S. Miller nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) || 1808a6574349SDavid S. Miller nla_put_u8(skb, IFLA_OPERSTATE, 1809a6574349SDavid S. Miller netif_running(dev) ? dev->operstate : IF_OPER_DOWN) || 1810a6574349SDavid S. Miller nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) || 1811a6574349SDavid S. Miller nla_put_u32(skb, IFLA_MTU, dev->mtu) || 18123e7a50ceSStephen Hemminger nla_put_u32(skb, IFLA_MIN_MTU, dev->min_mtu) || 18133e7a50ceSStephen Hemminger nla_put_u32(skb, IFLA_MAX_MTU, dev->max_mtu) || 1814a6574349SDavid S. Miller nla_put_u32(skb, IFLA_GROUP, dev->group) || 1815edbc0bb3SBen Greear nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) || 18167e6e1b57SNicolas Dichtel nla_put_u32(skb, IFLA_ALLMULTI, dev->allmulti) || 181776ff5cc9SJiri Pirko nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) || 1818c70ce028SEric Dumazet nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) || 1819c70ce028SEric Dumazet nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) || 1820eac1b93cSCoco Li nla_put_u32(skb, IFLA_GRO_MAX_SIZE, dev->gro_max_size) || 18219eefedd5SXin Long nla_put_u32(skb, IFLA_GSO_IPV4_MAX_SIZE, dev->gso_ipv4_max_size) || 18229eefedd5SXin Long nla_put_u32(skb, IFLA_GRO_IPV4_MAX_SIZE, dev->gro_ipv4_max_size) || 182389527be8SEric Dumazet nla_put_u32(skb, IFLA_TSO_MAX_SIZE, dev->tso_max_size) || 182489527be8SEric Dumazet nla_put_u32(skb, IFLA_TSO_MAX_SEGS, dev->tso_max_segs) || 18251d69c2b3SMark A. Greer #ifdef CONFIG_RPS 182676ff5cc9SJiri Pirko nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) || 18271d69c2b3SMark A. Greer #endif 182879110a04SFlorian Westphal put_master_ifindex(skb, dev) || 18299a57247fSJiri Pirko nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) || 18305891cd5eSEric Dumazet (qdisc && 18315891cd5eSEric Dumazet nla_put_string(skb, IFLA_QDISC, qdisc->ops->id)) || 18326c557001SFlorian Westphal nla_put_ifalias(skb, dev) || 18332d3b479dSdavid decotigny nla_put_u32(skb, IFLA_CARRIER_CHANGES, 1834b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_up_count) + 1835b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_down_count)) || 1836b2d3bcfaSDavid Decotigny nla_put_u32(skb, IFLA_CARRIER_UP_COUNT, 1837b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_up_count)) || 1838b2d3bcfaSDavid Decotigny nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT, 1839b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_down_count))) 1840a6574349SDavid S. Miller goto nla_put_failure; 18410b815a1aSStephen Hemminger 1842829eb208SRoopa Prabhu if (rtnl_fill_proto_down(skb, dev)) 1843829eb208SRoopa Prabhu goto nla_put_failure; 1844829eb208SRoopa Prabhu 18453d3ea5afSVlad Yasevich if (event != IFLA_EVENT_NONE) { 18463d3ea5afSVlad Yasevich if (nla_put_u32(skb, IFLA_EVENT, event)) 18473d3ea5afSVlad Yasevich goto nla_put_failure; 18483d3ea5afSVlad Yasevich } 18493d3ea5afSVlad Yasevich 1850b22b941bSHannes Frederic Sowa if (rtnl_fill_link_ifmap(skb, dev)) 1851a6574349SDavid S. Miller goto nla_put_failure; 18521da177e4SLinus Torvalds 18531da177e4SLinus Torvalds if (dev->addr_len) { 1854a6574349SDavid S. Miller if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) || 1855a6574349SDavid S. Miller nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast)) 1856a6574349SDavid S. Miller goto nla_put_failure; 18571da177e4SLinus Torvalds } 18581da177e4SLinus Torvalds 185966cae9edSJiri Pirko if (rtnl_phys_port_id_fill(skb, dev)) 186066cae9edSJiri Pirko goto nla_put_failure; 186166cae9edSJiri Pirko 1862db24a904SDavid Ahern if (rtnl_phys_port_name_fill(skb, dev)) 1863db24a904SDavid Ahern goto nla_put_failure; 1864db24a904SDavid Ahern 186582f28412SJiri Pirko if (rtnl_phys_switch_id_fill(skb, dev)) 186682f28412SJiri Pirko goto nla_put_failure; 186782f28412SJiri Pirko 1868b22b941bSHannes Frederic Sowa if (rtnl_fill_stats(skb, dev)) 1869b60c5115SThomas Graf goto nla_put_failure; 1870b60c5115SThomas Graf 1871250fc3dfSFlorian Westphal if (rtnl_fill_vf(skb, dev, ext_filter_mask)) 1872a6574349SDavid S. Miller goto nla_put_failure; 187357b61080SScott Feldman 1874c53864fdSDavid Gibson if (rtnl_port_fill(skb, dev, ext_filter_mask)) 187557b61080SScott Feldman goto nla_put_failure; 187657b61080SScott Feldman 1877d1fdd913SBrenden Blanco if (rtnl_xdp_fill(skb, dev)) 1878d1fdd913SBrenden Blanco goto nla_put_failure; 1879d1fdd913SBrenden Blanco 1880ba7d49b1SJiri Pirko if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) { 188138f7b870SPatrick McHardy if (rtnl_link_fill(skb, dev) < 0) 188238f7b870SPatrick McHardy goto nla_put_failure; 188338f7b870SPatrick McHardy } 188438f7b870SPatrick McHardy 1885d4e4fdf9SGuillaume Nault if (rtnl_fill_link_netnsid(skb, dev, src_net, gfp)) 1886d37512a2SNicolas Dichtel goto nla_put_failure; 1887d37512a2SNicolas Dichtel 18886621dd29SNicolas Dichtel if (new_nsid && 18896621dd29SNicolas Dichtel nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0) 18906621dd29SNicolas Dichtel goto nla_put_failure; 189138e01b30SNicolas Dichtel if (new_ifindex && 189238e01b30SNicolas Dichtel nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0) 189338e01b30SNicolas Dichtel goto nla_put_failure; 189438e01b30SNicolas Dichtel 1895f74877a5SMichal Kubecek if (memchr_inv(dev->perm_addr, '\0', dev->addr_len) && 1896f74877a5SMichal Kubecek nla_put(skb, IFLA_PERM_ADDRESS, dev->addr_len, dev->perm_addr)) 1897f74877a5SMichal Kubecek goto nla_put_failure; 18986621dd29SNicolas Dichtel 18995fa85a09SFlorian Westphal rcu_read_lock(); 1900070cbf5bSFlorian Westphal if (rtnl_fill_link_af(skb, dev, ext_filter_mask)) 19015fa85a09SFlorian Westphal goto nla_put_failure_rcu; 19025fa85a09SFlorian Westphal rcu_read_unlock(); 1903f8ff182cSThomas Graf 190488f4fb0cSJiri Pirko if (rtnl_fill_prop_list(skb, dev)) 190588f4fb0cSJiri Pirko goto nla_put_failure; 190688f4fb0cSJiri Pirko 190700e77ed8SJohannes Berg if (dev->dev.parent && 190800e77ed8SJohannes Berg nla_put_string(skb, IFLA_PARENT_DEV_NAME, 190900e77ed8SJohannes Berg dev_name(dev->dev.parent))) 191000e77ed8SJohannes Berg goto nla_put_failure; 191100e77ed8SJohannes Berg 191200e77ed8SJohannes Berg if (dev->dev.parent && dev->dev.parent->bus && 191300e77ed8SJohannes Berg nla_put_string(skb, IFLA_PARENT_DEV_BUS_NAME, 191400e77ed8SJohannes Berg dev->dev.parent->bus->name)) 191500e77ed8SJohannes Berg goto nla_put_failure; 191600e77ed8SJohannes Berg 1917dca56c30SJiri Pirko if (rtnl_fill_devlink_port(skb, dev)) 1918dca56c30SJiri Pirko goto nla_put_failure; 1919dca56c30SJiri Pirko 1920053c095aSJohannes Berg nlmsg_end(skb, nlh); 1921053c095aSJohannes Berg return 0; 1922b60c5115SThomas Graf 19235fa85a09SFlorian Westphal nla_put_failure_rcu: 19245fa85a09SFlorian Westphal rcu_read_unlock(); 1925b60c5115SThomas Graf nla_put_failure: 192626932566SPatrick McHardy nlmsg_cancel(skb, nlh); 192726932566SPatrick McHardy return -EMSGSIZE; 19281da177e4SLinus Torvalds } 19291da177e4SLinus Torvalds 1930f7b12606SJiri Pirko static const struct nla_policy ifla_policy[IFLA_MAX+1] = { 1931f7b12606SJiri Pirko [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 }, 1932f7b12606SJiri Pirko [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 1933f7b12606SJiri Pirko [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 1934f7b12606SJiri Pirko [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) }, 1935f7b12606SJiri Pirko [IFLA_MTU] = { .type = NLA_U32 }, 1936f7b12606SJiri Pirko [IFLA_LINK] = { .type = NLA_U32 }, 1937f7b12606SJiri Pirko [IFLA_MASTER] = { .type = NLA_U32 }, 1938f7b12606SJiri Pirko [IFLA_CARRIER] = { .type = NLA_U8 }, 1939f7b12606SJiri Pirko [IFLA_TXQLEN] = { .type = NLA_U32 }, 1940f7b12606SJiri Pirko [IFLA_WEIGHT] = { .type = NLA_U32 }, 1941f7b12606SJiri Pirko [IFLA_OPERSTATE] = { .type = NLA_U8 }, 1942f7b12606SJiri Pirko [IFLA_LINKMODE] = { .type = NLA_U8 }, 1943f7b12606SJiri Pirko [IFLA_LINKINFO] = { .type = NLA_NESTED }, 1944f7b12606SJiri Pirko [IFLA_NET_NS_PID] = { .type = NLA_U32 }, 1945f7b12606SJiri Pirko [IFLA_NET_NS_FD] = { .type = NLA_U32 }, 19462459b4c6SNicolas Dichtel /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to 19472459b4c6SNicolas Dichtel * allow 0-length string (needed to remove an alias). 19482459b4c6SNicolas Dichtel */ 19492459b4c6SNicolas Dichtel [IFLA_IFALIAS] = { .type = NLA_BINARY, .len = IFALIASZ - 1 }, 1950f7b12606SJiri Pirko [IFLA_VFINFO_LIST] = {. type = NLA_NESTED }, 1951f7b12606SJiri Pirko [IFLA_VF_PORTS] = { .type = NLA_NESTED }, 1952f7b12606SJiri Pirko [IFLA_PORT_SELF] = { .type = NLA_NESTED }, 1953f7b12606SJiri Pirko [IFLA_AF_SPEC] = { .type = NLA_NESTED }, 1954f7b12606SJiri Pirko [IFLA_EXT_MASK] = { .type = NLA_U32 }, 1955f7b12606SJiri Pirko [IFLA_PROMISCUITY] = { .type = NLA_U32 }, 1956f7b12606SJiri Pirko [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 }, 1957f7b12606SJiri Pirko [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 }, 195846e6b992SStephen Hemminger [IFLA_GSO_MAX_SEGS] = { .type = NLA_U32 }, 195946e6b992SStephen Hemminger [IFLA_GSO_MAX_SIZE] = { .type = NLA_U32 }, 196002637fceSJiri Pirko [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN }, 19612d3b479dSdavid decotigny [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */ 196282f28412SJiri Pirko [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN }, 1963317f4810SNicolas Dichtel [IFLA_LINK_NETNSID] = { .type = NLA_S32 }, 196488d6378bSAnuradha Karuppiah [IFLA_PROTO_DOWN] = { .type = NLA_U8 }, 1965d1fdd913SBrenden Blanco [IFLA_XDP] = { .type = NLA_NESTED }, 19663d3ea5afSVlad Yasevich [IFLA_EVENT] = { .type = NLA_U32 }, 1967db833d40SSerhey Popovych [IFLA_GROUP] = { .type = NLA_U32 }, 19687e4a8d5aSChristian Brauner [IFLA_TARGET_NETNSID] = { .type = NLA_S32 }, 1969b2d3bcfaSDavid Decotigny [IFLA_CARRIER_UP_COUNT] = { .type = NLA_U32 }, 1970b2d3bcfaSDavid Decotigny [IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 }, 19713e7a50ceSStephen Hemminger [IFLA_MIN_MTU] = { .type = NLA_U32 }, 19723e7a50ceSStephen Hemminger [IFLA_MAX_MTU] = { .type = NLA_U32 }, 197336fbf1e5SJiri Pirko [IFLA_PROP_LIST] = { .type = NLA_NESTED }, 197436fbf1e5SJiri Pirko [IFLA_ALT_IFNAME] = { .type = NLA_STRING, 197536fbf1e5SJiri Pirko .len = ALTIFNAMSIZ - 1 }, 1976f74877a5SMichal Kubecek [IFLA_PERM_ADDRESS] = { .type = NLA_REJECT }, 1977829eb208SRoopa Prabhu [IFLA_PROTO_DOWN_REASON] = { .type = NLA_NESTED }, 19787e4a5131SAndrei Vagin [IFLA_NEW_IFINDEX] = NLA_POLICY_MIN(NLA_S32, 1), 197988b71053SJohannes Berg [IFLA_PARENT_DEV_NAME] = { .type = NLA_NUL_STRING }, 1980eac1b93cSCoco Li [IFLA_GRO_MAX_SIZE] = { .type = NLA_U32 }, 198189527be8SEric Dumazet [IFLA_TSO_MAX_SIZE] = { .type = NLA_REJECT }, 198289527be8SEric Dumazet [IFLA_TSO_MAX_SEGS] = { .type = NLA_REJECT }, 19837e6e1b57SNicolas Dichtel [IFLA_ALLMULTI] = { .type = NLA_REJECT }, 19849eefedd5SXin Long [IFLA_GSO_IPV4_MAX_SIZE] = { .type = NLA_U32 }, 19859eefedd5SXin Long [IFLA_GRO_IPV4_MAX_SIZE] = { .type = NLA_U32 }, 1986f7b12606SJiri Pirko }; 1987f7b12606SJiri Pirko 1988f7b12606SJiri Pirko static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = { 1989f7b12606SJiri Pirko [IFLA_INFO_KIND] = { .type = NLA_STRING }, 1990f7b12606SJiri Pirko [IFLA_INFO_DATA] = { .type = NLA_NESTED }, 1991f7b12606SJiri Pirko [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING }, 1992f7b12606SJiri Pirko [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED }, 1993f7b12606SJiri Pirko }; 1994f7b12606SJiri Pirko 1995f7b12606SJiri Pirko static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = { 1996364d5716SDaniel Borkmann [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) }, 199775345f88SDenis Kirjanov [IFLA_VF_BROADCAST] = { .type = NLA_REJECT }, 1998364d5716SDaniel Borkmann [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) }, 199979aab093SMoshe Shemesh [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED }, 2000364d5716SDaniel Borkmann [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) }, 2001364d5716SDaniel Borkmann [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) }, 2002364d5716SDaniel Borkmann [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) }, 2003364d5716SDaniel Borkmann [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) }, 200401a3d796SVlad Zolotarov [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) }, 20053b766cd8SEran Ben Elisha [IFLA_VF_STATS] = { .type = NLA_NESTED }, 2006dd461d6aSHiroshi Shimamoto [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) }, 2007cc8e27ccSEli Cohen [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) }, 2008cc8e27ccSEli Cohen [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) }, 20093b766cd8SEran Ben Elisha }; 20103b766cd8SEran Ben Elisha 2011f7b12606SJiri Pirko static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = { 2012f7b12606SJiri Pirko [IFLA_PORT_VF] = { .type = NLA_U32 }, 2013f7b12606SJiri Pirko [IFLA_PORT_PROFILE] = { .type = NLA_STRING, 2014f7b12606SJiri Pirko .len = PORT_PROFILE_MAX }, 2015f7b12606SJiri Pirko [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY, 2016f7b12606SJiri Pirko .len = PORT_UUID_MAX }, 2017f7b12606SJiri Pirko [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING, 2018f7b12606SJiri Pirko .len = PORT_UUID_MAX }, 2019f7b12606SJiri Pirko [IFLA_PORT_REQUEST] = { .type = NLA_U8, }, 2020f7b12606SJiri Pirko [IFLA_PORT_RESPONSE] = { .type = NLA_U16, }, 2021025331dfSDaniel Borkmann 2022025331dfSDaniel Borkmann /* Unused, but we need to keep it here since user space could 2023025331dfSDaniel Borkmann * fill it. It's also broken with regard to NLA_BINARY use in 2024025331dfSDaniel Borkmann * combination with structs. 2025025331dfSDaniel Borkmann */ 2026025331dfSDaniel Borkmann [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY, 2027025331dfSDaniel Borkmann .len = sizeof(struct ifla_port_vsi) }, 2028f7b12606SJiri Pirko }; 2029f7b12606SJiri Pirko 2030d1fdd913SBrenden Blanco static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = { 203192234c8fSToke Høiland-Jørgensen [IFLA_XDP_UNSPEC] = { .strict_start_type = IFLA_XDP_EXPECTED_FD }, 2032d1fdd913SBrenden Blanco [IFLA_XDP_FD] = { .type = NLA_S32 }, 203392234c8fSToke Høiland-Jørgensen [IFLA_XDP_EXPECTED_FD] = { .type = NLA_S32 }, 2034d1fdd913SBrenden Blanco [IFLA_XDP_ATTACHED] = { .type = NLA_U8 }, 203585de8576SDaniel Borkmann [IFLA_XDP_FLAGS] = { .type = NLA_U32 }, 203658038695SMartin KaFai Lau [IFLA_XDP_PROG_ID] = { .type = NLA_U32 }, 2037d1fdd913SBrenden Blanco }; 2038d1fdd913SBrenden Blanco 2039dc599f76SDavid Ahern static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla) 2040dc599f76SDavid Ahern { 2041dc599f76SDavid Ahern const struct rtnl_link_ops *ops = NULL; 2042dc599f76SDavid Ahern struct nlattr *linfo[IFLA_INFO_MAX + 1]; 2043dc599f76SDavid Ahern 20448cb08174SJohannes Berg if (nla_parse_nested_deprecated(linfo, IFLA_INFO_MAX, nla, ifla_info_policy, NULL) < 0) 2045dc599f76SDavid Ahern return NULL; 2046dc599f76SDavid Ahern 2047dc599f76SDavid Ahern if (linfo[IFLA_INFO_KIND]) { 2048dc599f76SDavid Ahern char kind[MODULE_NAME_LEN]; 2049dc599f76SDavid Ahern 2050872f6903SFrancis Laniel nla_strscpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind)); 2051dc599f76SDavid Ahern ops = rtnl_link_ops_get(kind); 2052dc599f76SDavid Ahern } 2053dc599f76SDavid Ahern 2054dc599f76SDavid Ahern return ops; 2055dc599f76SDavid Ahern } 2056dc599f76SDavid Ahern 2057dc599f76SDavid Ahern static bool link_master_filtered(struct net_device *dev, int master_idx) 2058dc599f76SDavid Ahern { 2059dc599f76SDavid Ahern struct net_device *master; 2060dc599f76SDavid Ahern 2061dc599f76SDavid Ahern if (!master_idx) 2062dc599f76SDavid Ahern return false; 2063dc599f76SDavid Ahern 2064dc599f76SDavid Ahern master = netdev_master_upper_dev_get(dev); 2065d3432bf1SLahav Schlesinger 2066d3432bf1SLahav Schlesinger /* 0 is already used to denote IFLA_MASTER wasn't passed, therefore need 2067d3432bf1SLahav Schlesinger * another invalid value for ifindex to denote "no master". 2068d3432bf1SLahav Schlesinger */ 2069d3432bf1SLahav Schlesinger if (master_idx == -1) 2070d3432bf1SLahav Schlesinger return !!master; 2071d3432bf1SLahav Schlesinger 2072dc599f76SDavid Ahern if (!master || master->ifindex != master_idx) 2073dc599f76SDavid Ahern return true; 2074dc599f76SDavid Ahern 2075dc599f76SDavid Ahern return false; 2076dc599f76SDavid Ahern } 2077dc599f76SDavid Ahern 2078dc599f76SDavid Ahern static bool link_kind_filtered(const struct net_device *dev, 2079dc599f76SDavid Ahern const struct rtnl_link_ops *kind_ops) 2080dc599f76SDavid Ahern { 2081dc599f76SDavid Ahern if (kind_ops && dev->rtnl_link_ops != kind_ops) 2082dc599f76SDavid Ahern return true; 2083dc599f76SDavid Ahern 2084dc599f76SDavid Ahern return false; 2085dc599f76SDavid Ahern } 2086dc599f76SDavid Ahern 2087dc599f76SDavid Ahern static bool link_dump_filtered(struct net_device *dev, 2088dc599f76SDavid Ahern int master_idx, 2089dc599f76SDavid Ahern const struct rtnl_link_ops *kind_ops) 2090dc599f76SDavid Ahern { 2091dc599f76SDavid Ahern if (link_master_filtered(dev, master_idx) || 2092dc599f76SDavid Ahern link_kind_filtered(dev, kind_ops)) 2093dc599f76SDavid Ahern return true; 2094dc599f76SDavid Ahern 2095dc599f76SDavid Ahern return false; 2096dc599f76SDavid Ahern } 2097dc599f76SDavid Ahern 2098c383edc4SChristian Brauner /** 2099c383edc4SChristian Brauner * rtnl_get_net_ns_capable - Get netns if sufficiently privileged. 2100c383edc4SChristian Brauner * @sk: netlink socket 2101c383edc4SChristian Brauner * @netnsid: network namespace identifier 2102c383edc4SChristian Brauner * 2103c383edc4SChristian Brauner * Returns the network namespace identified by netnsid on success or an error 2104c383edc4SChristian Brauner * pointer on failure. 2105c383edc4SChristian Brauner */ 2106c383edc4SChristian Brauner struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid) 210779e1ad14SJiri Benc { 210879e1ad14SJiri Benc struct net *net; 210979e1ad14SJiri Benc 2110f428fe4aSAndrei Vagin net = get_net_ns_by_id(sock_net(sk), netnsid); 211179e1ad14SJiri Benc if (!net) 211279e1ad14SJiri Benc return ERR_PTR(-EINVAL); 211379e1ad14SJiri Benc 211479e1ad14SJiri Benc /* For now, the caller is required to have CAP_NET_ADMIN in 211579e1ad14SJiri Benc * the user namespace owning the target net ns. 211679e1ad14SJiri Benc */ 2117f428fe4aSAndrei Vagin if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) { 211879e1ad14SJiri Benc put_net(net); 211979e1ad14SJiri Benc return ERR_PTR(-EACCES); 212079e1ad14SJiri Benc } 212179e1ad14SJiri Benc return net; 212279e1ad14SJiri Benc } 2123c383edc4SChristian Brauner EXPORT_SYMBOL_GPL(rtnl_get_net_ns_capable); 212479e1ad14SJiri Benc 2125905cf0abSDavid Ahern static int rtnl_valid_dump_ifinfo_req(const struct nlmsghdr *nlh, 2126905cf0abSDavid Ahern bool strict_check, struct nlattr **tb, 2127905cf0abSDavid Ahern struct netlink_ext_ack *extack) 2128905cf0abSDavid Ahern { 2129905cf0abSDavid Ahern int hdrlen; 2130905cf0abSDavid Ahern 2131905cf0abSDavid Ahern if (strict_check) { 2132905cf0abSDavid Ahern struct ifinfomsg *ifm; 2133905cf0abSDavid Ahern 2134905cf0abSDavid Ahern if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { 2135905cf0abSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid header for link dump"); 2136905cf0abSDavid Ahern return -EINVAL; 2137905cf0abSDavid Ahern } 2138905cf0abSDavid Ahern 2139905cf0abSDavid Ahern ifm = nlmsg_data(nlh); 2140905cf0abSDavid Ahern if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags || 2141905cf0abSDavid Ahern ifm->ifi_change) { 2142905cf0abSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid values in header for link dump request"); 2143905cf0abSDavid Ahern return -EINVAL; 2144905cf0abSDavid Ahern } 2145905cf0abSDavid Ahern if (ifm->ifi_index) { 2146905cf0abSDavid Ahern NL_SET_ERR_MSG(extack, "Filter by device index not supported for link dumps"); 2147905cf0abSDavid Ahern return -EINVAL; 2148905cf0abSDavid Ahern } 2149905cf0abSDavid Ahern 21508cb08174SJohannes Berg return nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, 21518cb08174SJohannes Berg IFLA_MAX, ifla_policy, 21528cb08174SJohannes Berg extack); 2153905cf0abSDavid Ahern } 2154905cf0abSDavid Ahern 2155905cf0abSDavid Ahern /* A hack to preserve kernel<->userspace interface. 2156905cf0abSDavid Ahern * The correct header is ifinfomsg. It is consistent with rtnl_getlink. 2157905cf0abSDavid Ahern * However, before Linux v3.9 the code here assumed rtgenmsg and that's 2158905cf0abSDavid Ahern * what iproute2 < v3.9.0 used. 2159905cf0abSDavid Ahern * We can detect the old iproute2. Even including the IFLA_EXT_MASK 2160905cf0abSDavid Ahern * attribute, its netlink message is shorter than struct ifinfomsg. 2161905cf0abSDavid Ahern */ 2162905cf0abSDavid Ahern hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ? 2163905cf0abSDavid Ahern sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); 2164905cf0abSDavid Ahern 21658cb08174SJohannes Berg return nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, 21668cb08174SJohannes Berg extack); 2167905cf0abSDavid Ahern } 2168905cf0abSDavid Ahern 2169b60c5115SThomas Graf static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 21701da177e4SLinus Torvalds { 2171905cf0abSDavid Ahern struct netlink_ext_ack *extack = cb->extack; 2172905cf0abSDavid Ahern const struct nlmsghdr *nlh = cb->nlh; 21733b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 217479e1ad14SJiri Benc struct net *tgt_net = net; 21757c28bd0bSEric Dumazet int h, s_h; 21767c28bd0bSEric Dumazet int idx = 0, s_idx; 21771da177e4SLinus Torvalds struct net_device *dev; 21787c28bd0bSEric Dumazet struct hlist_head *head; 2179115c9b81SGreg Rose struct nlattr *tb[IFLA_MAX+1]; 2180115c9b81SGreg Rose u32 ext_filter_mask = 0; 2181dc599f76SDavid Ahern const struct rtnl_link_ops *kind_ops = NULL; 2182dc599f76SDavid Ahern unsigned int flags = NLM_F_MULTI; 2183dc599f76SDavid Ahern int master_idx = 0; 218479e1ad14SJiri Benc int netnsid = -1; 2185905cf0abSDavid Ahern int err, i; 21861da177e4SLinus Torvalds 21877c28bd0bSEric Dumazet s_h = cb->args[0]; 21887c28bd0bSEric Dumazet s_idx = cb->args[1]; 21897c28bd0bSEric Dumazet 2190905cf0abSDavid Ahern err = rtnl_valid_dump_ifinfo_req(nlh, cb->strict_check, tb, extack); 2191905cf0abSDavid Ahern if (err < 0) { 2192905cf0abSDavid Ahern if (cb->strict_check) 2193905cf0abSDavid Ahern return err; 2194e5eca6d4SMichal Schmidt 2195905cf0abSDavid Ahern goto walk_entries; 219679e1ad14SJiri Benc } 219779e1ad14SJiri Benc 2198905cf0abSDavid Ahern for (i = 0; i <= IFLA_MAX; ++i) { 2199905cf0abSDavid Ahern if (!tb[i]) 2200905cf0abSDavid Ahern continue; 2201dc599f76SDavid Ahern 2202905cf0abSDavid Ahern /* new attributes should only be added with strict checking */ 2203905cf0abSDavid Ahern switch (i) { 2204905cf0abSDavid Ahern case IFLA_TARGET_NETNSID: 2205905cf0abSDavid Ahern netnsid = nla_get_s32(tb[i]); 2206905cf0abSDavid Ahern tgt_net = rtnl_get_net_ns_capable(skb->sk, netnsid); 2207905cf0abSDavid Ahern if (IS_ERR(tgt_net)) { 2208905cf0abSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid target network namespace id"); 2209905cf0abSDavid Ahern return PTR_ERR(tgt_net); 2210905cf0abSDavid Ahern } 2211905cf0abSDavid Ahern break; 2212905cf0abSDavid Ahern case IFLA_EXT_MASK: 2213905cf0abSDavid Ahern ext_filter_mask = nla_get_u32(tb[i]); 2214905cf0abSDavid Ahern break; 2215905cf0abSDavid Ahern case IFLA_MASTER: 2216905cf0abSDavid Ahern master_idx = nla_get_u32(tb[i]); 2217905cf0abSDavid Ahern break; 2218905cf0abSDavid Ahern case IFLA_LINKINFO: 2219905cf0abSDavid Ahern kind_ops = linkinfo_to_kind_ops(tb[i]); 2220905cf0abSDavid Ahern break; 2221905cf0abSDavid Ahern default: 2222905cf0abSDavid Ahern if (cb->strict_check) { 2223905cf0abSDavid Ahern NL_SET_ERR_MSG(extack, "Unsupported attribute in link dump request"); 2224905cf0abSDavid Ahern return -EINVAL; 2225905cf0abSDavid Ahern } 2226905cf0abSDavid Ahern } 2227905cf0abSDavid Ahern } 2228dc599f76SDavid Ahern 2229dc599f76SDavid Ahern if (master_idx || kind_ops) 2230dc599f76SDavid Ahern flags |= NLM_F_DUMP_FILTERED; 2231115c9b81SGreg Rose 2232905cf0abSDavid Ahern walk_entries: 22337c28bd0bSEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 22347562f876SPavel Emelianov idx = 0; 223579e1ad14SJiri Benc head = &tgt_net->dev_index_head[h]; 2236cac5e65eSEric Dumazet hlist_for_each_entry(dev, head, index_hlist) { 2237dc599f76SDavid Ahern if (link_dump_filtered(dev, master_idx, kind_ops)) 22383f0ae05dSZhang Shengju goto cont; 22391da177e4SLinus Torvalds if (idx < s_idx) 22407562f876SPavel Emelianov goto cont; 224179e1ad14SJiri Benc err = rtnl_fill_ifinfo(skb, dev, net, 224279e1ad14SJiri Benc RTM_NEWLINK, 224315e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 2244905cf0abSDavid Ahern nlh->nlmsg_seq, 0, flags, 224538e01b30SNicolas Dichtel ext_filter_mask, 0, NULL, 0, 2246d4e4fdf9SGuillaume Nault netnsid, GFP_KERNEL); 2247973462bbSDavid Gibson 2248f6c5775fSDavid Ahern if (err < 0) { 2249f6c5775fSDavid Ahern if (likely(skb->len)) 22507c28bd0bSEric Dumazet goto out; 22514e985adaSThomas Graf 2252f6c5775fSDavid Ahern goto out_err; 2253f6c5775fSDavid Ahern } 22547562f876SPavel Emelianov cont: 22557562f876SPavel Emelianov idx++; 22561da177e4SLinus Torvalds } 22577c28bd0bSEric Dumazet } 22587c28bd0bSEric Dumazet out: 2259f6c5775fSDavid Ahern err = skb->len; 2260f6c5775fSDavid Ahern out_err: 22617c28bd0bSEric Dumazet cb->args[1] = idx; 22627c28bd0bSEric Dumazet cb->args[0] = h; 2263a9ecb0cbSzhang kai cb->seq = tgt_net->dev_base_seq; 2264d0225784SJakub Sitnicki nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 226579e1ad14SJiri Benc if (netnsid >= 0) 226679e1ad14SJiri Benc put_net(tgt_net); 22671da177e4SLinus Torvalds 2268f6c5775fSDavid Ahern return err; 22691da177e4SLinus Torvalds } 22701da177e4SLinus Torvalds 2271fceb6435SJohannes Berg int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len, 2272fceb6435SJohannes Berg struct netlink_ext_ack *exterr) 2273f7b12606SJiri Pirko { 22748cb08174SJohannes Berg return nla_parse_deprecated(tb, IFLA_MAX, head, len, ifla_policy, 22758cb08174SJohannes Berg exterr); 2276f7b12606SJiri Pirko } 2277f7b12606SJiri Pirko EXPORT_SYMBOL(rtnl_nla_parse_ifla); 227857b61080SScott Feldman 227981adee47SEric W. Biederman struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[]) 228081adee47SEric W. Biederman { 228181adee47SEric W. Biederman struct net *net; 228281adee47SEric W. Biederman /* Examine the link attributes and figure out which 228381adee47SEric W. Biederman * network namespace we are talking about. 228481adee47SEric W. Biederman */ 228581adee47SEric W. Biederman if (tb[IFLA_NET_NS_PID]) 228681adee47SEric W. Biederman net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID])); 2287f0630529SEric W. Biederman else if (tb[IFLA_NET_NS_FD]) 2288f0630529SEric W. Biederman net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD])); 228981adee47SEric W. Biederman else 229081adee47SEric W. Biederman net = get_net(src_net); 229181adee47SEric W. Biederman return net; 229281adee47SEric W. Biederman } 229381adee47SEric W. Biederman EXPORT_SYMBOL(rtnl_link_get_net); 229481adee47SEric W. Biederman 22957c4f63baSChristian Brauner /* Figure out which network namespace we are talking about by 22967c4f63baSChristian Brauner * examining the link attributes in the following order: 22977c4f63baSChristian Brauner * 22987c4f63baSChristian Brauner * 1. IFLA_NET_NS_PID 22997c4f63baSChristian Brauner * 2. IFLA_NET_NS_FD 23007e4a8d5aSChristian Brauner * 3. IFLA_TARGET_NETNSID 23017c4f63baSChristian Brauner */ 23027c4f63baSChristian Brauner static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net, 23037c4f63baSChristian Brauner struct nlattr *tb[]) 23047c4f63baSChristian Brauner { 23057c4f63baSChristian Brauner struct net *net; 23067c4f63baSChristian Brauner 23077c4f63baSChristian Brauner if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) 23087c4f63baSChristian Brauner return rtnl_link_get_net(src_net, tb); 23097c4f63baSChristian Brauner 23107e4a8d5aSChristian Brauner if (!tb[IFLA_TARGET_NETNSID]) 23117c4f63baSChristian Brauner return get_net(src_net); 23127c4f63baSChristian Brauner 23137e4a8d5aSChristian Brauner net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_TARGET_NETNSID])); 23147c4f63baSChristian Brauner if (!net) 23157c4f63baSChristian Brauner return ERR_PTR(-EINVAL); 23167c4f63baSChristian Brauner 23177c4f63baSChristian Brauner return net; 23187c4f63baSChristian Brauner } 23197c4f63baSChristian Brauner 23207c4f63baSChristian Brauner static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb, 23217c4f63baSChristian Brauner struct net *src_net, 23227c4f63baSChristian Brauner struct nlattr *tb[], int cap) 23237c4f63baSChristian Brauner { 23247c4f63baSChristian Brauner struct net *net; 23257c4f63baSChristian Brauner 23267c4f63baSChristian Brauner net = rtnl_link_get_net_by_nlattr(src_net, tb); 23277c4f63baSChristian Brauner if (IS_ERR(net)) 23287c4f63baSChristian Brauner return net; 23297c4f63baSChristian Brauner 23307c4f63baSChristian Brauner if (!netlink_ns_capable(skb, net->user_ns, cap)) { 23317c4f63baSChristian Brauner put_net(net); 23327c4f63baSChristian Brauner return ERR_PTR(-EPERM); 23337c4f63baSChristian Brauner } 23347c4f63baSChristian Brauner 23357c4f63baSChristian Brauner return net; 23367c4f63baSChristian Brauner } 23377c4f63baSChristian Brauner 23384ff66caeSChristian Brauner /* Verify that rtnetlink requests do not pass additional properties 23394ff66caeSChristian Brauner * potentially referring to different network namespaces. 23404ff66caeSChristian Brauner */ 23414ff66caeSChristian Brauner static int rtnl_ensure_unique_netns(struct nlattr *tb[], 23424ff66caeSChristian Brauner struct netlink_ext_ack *extack, 23434ff66caeSChristian Brauner bool netns_id_only) 23444ff66caeSChristian Brauner { 23454ff66caeSChristian Brauner 23464ff66caeSChristian Brauner if (netns_id_only) { 23474ff66caeSChristian Brauner if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD]) 23484ff66caeSChristian Brauner return 0; 23494ff66caeSChristian Brauner 23504ff66caeSChristian Brauner NL_SET_ERR_MSG(extack, "specified netns attribute not supported"); 23514ff66caeSChristian Brauner return -EOPNOTSUPP; 23524ff66caeSChristian Brauner } 23534ff66caeSChristian Brauner 23547e4a8d5aSChristian Brauner if (tb[IFLA_TARGET_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])) 23554ff66caeSChristian Brauner goto invalid_attr; 23564ff66caeSChristian Brauner 23577e4a8d5aSChristian Brauner if (tb[IFLA_NET_NS_PID] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_FD])) 23584ff66caeSChristian Brauner goto invalid_attr; 23594ff66caeSChristian Brauner 23607e4a8d5aSChristian Brauner if (tb[IFLA_NET_NS_FD] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_PID])) 23614ff66caeSChristian Brauner goto invalid_attr; 23624ff66caeSChristian Brauner 23634ff66caeSChristian Brauner return 0; 23644ff66caeSChristian Brauner 23654ff66caeSChristian Brauner invalid_attr: 23664ff66caeSChristian Brauner NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified"); 23674ff66caeSChristian Brauner return -EINVAL; 23684ff66caeSChristian Brauner } 23694ff66caeSChristian Brauner 2370a14857c2SBin Chen static int rtnl_set_vf_rate(struct net_device *dev, int vf, int min_tx_rate, 2371a14857c2SBin Chen int max_tx_rate) 2372a14857c2SBin Chen { 2373a14857c2SBin Chen const struct net_device_ops *ops = dev->netdev_ops; 2374a14857c2SBin Chen 2375a14857c2SBin Chen if (!ops->ndo_set_vf_rate) 2376a14857c2SBin Chen return -EOPNOTSUPP; 2377a14857c2SBin Chen if (max_tx_rate && max_tx_rate < min_tx_rate) 2378a14857c2SBin Chen return -EINVAL; 2379a14857c2SBin Chen 2380a14857c2SBin Chen return ops->ndo_set_vf_rate(dev, vf, min_tx_rate, max_tx_rate); 2381a14857c2SBin Chen } 2382a14857c2SBin Chen 23838679c31eSRocco Yue static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[], 23848679c31eSRocco Yue struct netlink_ext_ack *extack) 23851840bb13SThomas Graf { 23861840bb13SThomas Graf if (tb[IFLA_ADDRESS] && 23871840bb13SThomas Graf nla_len(tb[IFLA_ADDRESS]) < dev->addr_len) 23881840bb13SThomas Graf return -EINVAL; 23891840bb13SThomas Graf 23901840bb13SThomas Graf if (tb[IFLA_BROADCAST] && 23911840bb13SThomas Graf nla_len(tb[IFLA_BROADCAST]) < dev->addr_len) 23921840bb13SThomas Graf return -EINVAL; 2393fef5b228SXin Long 2394fef5b228SXin Long if (tb[IFLA_GSO_MAX_SIZE] && 2395fef5b228SXin Long nla_get_u32(tb[IFLA_GSO_MAX_SIZE]) > dev->tso_max_size) { 2396fef5b228SXin Long NL_SET_ERR_MSG(extack, "too big gso_max_size"); 2397fef5b228SXin Long return -EINVAL; 2398fef5b228SXin Long } 2399fef5b228SXin Long 2400fef5b228SXin Long if (tb[IFLA_GSO_MAX_SEGS] && 2401fef5b228SXin Long (nla_get_u32(tb[IFLA_GSO_MAX_SEGS]) > GSO_MAX_SEGS || 2402fef5b228SXin Long nla_get_u32(tb[IFLA_GSO_MAX_SEGS]) > dev->tso_max_segs)) { 2403fef5b228SXin Long NL_SET_ERR_MSG(extack, "too big gso_max_segs"); 2404fef5b228SXin Long return -EINVAL; 2405fef5b228SXin Long } 2406fef5b228SXin Long 240765d6914eSXin Long if (tb[IFLA_GRO_MAX_SIZE] && 240865d6914eSXin Long nla_get_u32(tb[IFLA_GRO_MAX_SIZE]) > GRO_MAX_SIZE) { 240965d6914eSXin Long NL_SET_ERR_MSG(extack, "too big gro_max_size"); 241065d6914eSXin Long return -EINVAL; 241165d6914eSXin Long } 241265d6914eSXin Long 2413fef5b228SXin Long if (tb[IFLA_GSO_IPV4_MAX_SIZE] && 2414fef5b228SXin Long nla_get_u32(tb[IFLA_GSO_IPV4_MAX_SIZE]) > dev->tso_max_size) { 2415fef5b228SXin Long NL_SET_ERR_MSG(extack, "too big gso_ipv4_max_size"); 2416fef5b228SXin Long return -EINVAL; 2417fef5b228SXin Long } 241865d6914eSXin Long 241965d6914eSXin Long if (tb[IFLA_GRO_IPV4_MAX_SIZE] && 242065d6914eSXin Long nla_get_u32(tb[IFLA_GRO_IPV4_MAX_SIZE]) > GRO_MAX_SIZE) { 242165d6914eSXin Long NL_SET_ERR_MSG(extack, "too big gro_ipv4_max_size"); 242265d6914eSXin Long return -EINVAL; 242365d6914eSXin Long } 24241840bb13SThomas Graf 2425cf7afbfeSThomas Graf if (tb[IFLA_AF_SPEC]) { 2426cf7afbfeSThomas Graf struct nlattr *af; 2427cf7afbfeSThomas Graf int rem, err; 2428cf7afbfeSThomas Graf 2429cf7afbfeSThomas Graf nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { 2430cf7afbfeSThomas Graf const struct rtnl_af_ops *af_ops; 2431cf7afbfeSThomas Graf 24325fa85a09SFlorian Westphal af_ops = rtnl_af_lookup(nla_type(af)); 2433a100243dSCong Wang if (!af_ops) 2434cf7afbfeSThomas Graf return -EAFNOSUPPORT; 2435cf7afbfeSThomas Graf 2436a100243dSCong Wang if (!af_ops->set_link_af) 2437cf7afbfeSThomas Graf return -EOPNOTSUPP; 2438cf7afbfeSThomas Graf 2439cf7afbfeSThomas Graf if (af_ops->validate_link_af) { 24408679c31eSRocco Yue err = af_ops->validate_link_af(dev, af, extack); 2441a100243dSCong Wang if (err < 0) 2442cf7afbfeSThomas Graf return err; 2443cf7afbfeSThomas Graf } 2444cf7afbfeSThomas Graf } 2445cf7afbfeSThomas Graf } 2446cf7afbfeSThomas Graf 24471840bb13SThomas Graf return 0; 24481840bb13SThomas Graf } 24491840bb13SThomas Graf 2450cc8e27ccSEli Cohen static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt, 2451cc8e27ccSEli Cohen int guid_type) 2452cc8e27ccSEli Cohen { 2453cc8e27ccSEli Cohen const struct net_device_ops *ops = dev->netdev_ops; 2454cc8e27ccSEli Cohen 2455cc8e27ccSEli Cohen return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type); 2456cc8e27ccSEli Cohen } 2457cc8e27ccSEli Cohen 2458cc8e27ccSEli Cohen static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type) 2459cc8e27ccSEli Cohen { 2460cc8e27ccSEli Cohen if (dev->type != ARPHRD_INFINIBAND) 2461cc8e27ccSEli Cohen return -EOPNOTSUPP; 2462cc8e27ccSEli Cohen 2463cc8e27ccSEli Cohen return handle_infiniband_guid(dev, ivt, guid_type); 2464cc8e27ccSEli Cohen } 2465cc8e27ccSEli Cohen 24664f7d2cdfSDaniel Borkmann static int do_setvfinfo(struct net_device *dev, struct nlattr **tb) 2467c02db8c6SChris Wright { 2468c02db8c6SChris Wright const struct net_device_ops *ops = dev->netdev_ops; 24694f7d2cdfSDaniel Borkmann int err = -EINVAL; 2470c02db8c6SChris Wright 24714f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_MAC]) { 24724f7d2cdfSDaniel Borkmann struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]); 24734f7d2cdfSDaniel Borkmann 2474ff08ddbaSDan Carpenter if (ivm->vf >= INT_MAX) 2475ff08ddbaSDan Carpenter return -EINVAL; 2476c02db8c6SChris Wright err = -EOPNOTSUPP; 2477c02db8c6SChris Wright if (ops->ndo_set_vf_mac) 2478c02db8c6SChris Wright err = ops->ndo_set_vf_mac(dev, ivm->vf, 2479c02db8c6SChris Wright ivm->mac); 24804f7d2cdfSDaniel Borkmann if (err < 0) 24814f7d2cdfSDaniel Borkmann return err; 2482c02db8c6SChris Wright } 24834f7d2cdfSDaniel Borkmann 24844f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_VLAN]) { 24854f7d2cdfSDaniel Borkmann struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]); 24864f7d2cdfSDaniel Borkmann 2487ff08ddbaSDan Carpenter if (ivv->vf >= INT_MAX) 2488ff08ddbaSDan Carpenter return -EINVAL; 2489c02db8c6SChris Wright err = -EOPNOTSUPP; 2490c02db8c6SChris Wright if (ops->ndo_set_vf_vlan) 24914f7d2cdfSDaniel Borkmann err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan, 249279aab093SMoshe Shemesh ivv->qos, 249379aab093SMoshe Shemesh htons(ETH_P_8021Q)); 249479aab093SMoshe Shemesh if (err < 0) 249579aab093SMoshe Shemesh return err; 249679aab093SMoshe Shemesh } 249779aab093SMoshe Shemesh 249879aab093SMoshe Shemesh if (tb[IFLA_VF_VLAN_LIST]) { 249979aab093SMoshe Shemesh struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN]; 250079aab093SMoshe Shemesh struct nlattr *attr; 250179aab093SMoshe Shemesh int rem, len = 0; 250279aab093SMoshe Shemesh 250379aab093SMoshe Shemesh err = -EOPNOTSUPP; 250479aab093SMoshe Shemesh if (!ops->ndo_set_vf_vlan) 250579aab093SMoshe Shemesh return err; 250679aab093SMoshe Shemesh 250779aab093SMoshe Shemesh nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) { 250879aab093SMoshe Shemesh if (nla_type(attr) != IFLA_VF_VLAN_INFO || 250979aab093SMoshe Shemesh nla_len(attr) < NLA_HDRLEN) { 251079aab093SMoshe Shemesh return -EINVAL; 251179aab093SMoshe Shemesh } 251279aab093SMoshe Shemesh if (len >= MAX_VLAN_LIST_LEN) 251379aab093SMoshe Shemesh return -EOPNOTSUPP; 251479aab093SMoshe Shemesh ivvl[len] = nla_data(attr); 251579aab093SMoshe Shemesh 251679aab093SMoshe Shemesh len++; 251779aab093SMoshe Shemesh } 2518fa34cd94SArnd Bergmann if (len == 0) 2519fa34cd94SArnd Bergmann return -EINVAL; 2520fa34cd94SArnd Bergmann 2521ff08ddbaSDan Carpenter if (ivvl[0]->vf >= INT_MAX) 2522ff08ddbaSDan Carpenter return -EINVAL; 252379aab093SMoshe Shemesh err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan, 252479aab093SMoshe Shemesh ivvl[0]->qos, ivvl[0]->vlan_proto); 25254f7d2cdfSDaniel Borkmann if (err < 0) 25264f7d2cdfSDaniel Borkmann return err; 2527c02db8c6SChris Wright } 25284f7d2cdfSDaniel Borkmann 25294f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_TX_RATE]) { 25304f7d2cdfSDaniel Borkmann struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]); 2531ed616689SSucheta Chakraborty struct ifla_vf_info ivf; 25324f7d2cdfSDaniel Borkmann 2533ff08ddbaSDan Carpenter if (ivt->vf >= INT_MAX) 2534ff08ddbaSDan Carpenter return -EINVAL; 2535c02db8c6SChris Wright err = -EOPNOTSUPP; 2536ed616689SSucheta Chakraborty if (ops->ndo_get_vf_config) 25374f7d2cdfSDaniel Borkmann err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf); 25384f7d2cdfSDaniel Borkmann if (err < 0) 25394f7d2cdfSDaniel Borkmann return err; 25404f7d2cdfSDaniel Borkmann 2541a14857c2SBin Chen err = rtnl_set_vf_rate(dev, ivt->vf, 2542a14857c2SBin Chen ivf.min_tx_rate, ivt->rate); 25434f7d2cdfSDaniel Borkmann if (err < 0) 25444f7d2cdfSDaniel Borkmann return err; 2545c02db8c6SChris Wright } 25464f7d2cdfSDaniel Borkmann 25474f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_RATE]) { 25484f7d2cdfSDaniel Borkmann struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]); 25494f7d2cdfSDaniel Borkmann 2550ff08ddbaSDan Carpenter if (ivt->vf >= INT_MAX) 2551ff08ddbaSDan Carpenter return -EINVAL; 2552a14857c2SBin Chen 2553a14857c2SBin Chen err = rtnl_set_vf_rate(dev, ivt->vf, 2554a14857c2SBin Chen ivt->min_tx_rate, ivt->max_tx_rate); 25554f7d2cdfSDaniel Borkmann if (err < 0) 25564f7d2cdfSDaniel Borkmann return err; 2557ed616689SSucheta Chakraborty } 25584f7d2cdfSDaniel Borkmann 25594f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_SPOOFCHK]) { 25604f7d2cdfSDaniel Borkmann struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]); 25614f7d2cdfSDaniel Borkmann 2562ff08ddbaSDan Carpenter if (ivs->vf >= INT_MAX) 2563ff08ddbaSDan Carpenter return -EINVAL; 25645f8444a3SGreg Rose err = -EOPNOTSUPP; 25655f8444a3SGreg Rose if (ops->ndo_set_vf_spoofchk) 25665f8444a3SGreg Rose err = ops->ndo_set_vf_spoofchk(dev, ivs->vf, 25675f8444a3SGreg Rose ivs->setting); 25684f7d2cdfSDaniel Borkmann if (err < 0) 25694f7d2cdfSDaniel Borkmann return err; 25705f8444a3SGreg Rose } 25714f7d2cdfSDaniel Borkmann 25724f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_LINK_STATE]) { 25734f7d2cdfSDaniel Borkmann struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]); 25744f7d2cdfSDaniel Borkmann 2575ff08ddbaSDan Carpenter if (ivl->vf >= INT_MAX) 2576ff08ddbaSDan Carpenter return -EINVAL; 25771d8faf48SRony Efraim err = -EOPNOTSUPP; 25781d8faf48SRony Efraim if (ops->ndo_set_vf_link_state) 25791d8faf48SRony Efraim err = ops->ndo_set_vf_link_state(dev, ivl->vf, 25801d8faf48SRony Efraim ivl->link_state); 25814f7d2cdfSDaniel Borkmann if (err < 0) 25824f7d2cdfSDaniel Borkmann return err; 25831d8faf48SRony Efraim } 25844f7d2cdfSDaniel Borkmann 25854f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_RSS_QUERY_EN]) { 258601a3d796SVlad Zolotarov struct ifla_vf_rss_query_en *ivrssq_en; 258701a3d796SVlad Zolotarov 258801a3d796SVlad Zolotarov err = -EOPNOTSUPP; 25894f7d2cdfSDaniel Borkmann ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]); 2590ff08ddbaSDan Carpenter if (ivrssq_en->vf >= INT_MAX) 2591ff08ddbaSDan Carpenter return -EINVAL; 259201a3d796SVlad Zolotarov if (ops->ndo_set_vf_rss_query_en) 25934f7d2cdfSDaniel Borkmann err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf, 259401a3d796SVlad Zolotarov ivrssq_en->setting); 25954f7d2cdfSDaniel Borkmann if (err < 0) 25964f7d2cdfSDaniel Borkmann return err; 259701a3d796SVlad Zolotarov } 25984f7d2cdfSDaniel Borkmann 2599dd461d6aSHiroshi Shimamoto if (tb[IFLA_VF_TRUST]) { 2600dd461d6aSHiroshi Shimamoto struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]); 2601dd461d6aSHiroshi Shimamoto 2602ff08ddbaSDan Carpenter if (ivt->vf >= INT_MAX) 2603ff08ddbaSDan Carpenter return -EINVAL; 2604dd461d6aSHiroshi Shimamoto err = -EOPNOTSUPP; 2605dd461d6aSHiroshi Shimamoto if (ops->ndo_set_vf_trust) 2606dd461d6aSHiroshi Shimamoto err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting); 2607dd461d6aSHiroshi Shimamoto if (err < 0) 2608dd461d6aSHiroshi Shimamoto return err; 2609dd461d6aSHiroshi Shimamoto } 2610dd461d6aSHiroshi Shimamoto 2611cc8e27ccSEli Cohen if (tb[IFLA_VF_IB_NODE_GUID]) { 2612cc8e27ccSEli Cohen struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]); 2613cc8e27ccSEli Cohen 2614ff08ddbaSDan Carpenter if (ivt->vf >= INT_MAX) 2615ff08ddbaSDan Carpenter return -EINVAL; 2616cc8e27ccSEli Cohen if (!ops->ndo_set_vf_guid) 2617cc8e27ccSEli Cohen return -EOPNOTSUPP; 2618cc8e27ccSEli Cohen return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID); 2619cc8e27ccSEli Cohen } 2620cc8e27ccSEli Cohen 2621cc8e27ccSEli Cohen if (tb[IFLA_VF_IB_PORT_GUID]) { 2622cc8e27ccSEli Cohen struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]); 2623cc8e27ccSEli Cohen 2624ff08ddbaSDan Carpenter if (ivt->vf >= INT_MAX) 2625ff08ddbaSDan Carpenter return -EINVAL; 2626cc8e27ccSEli Cohen if (!ops->ndo_set_vf_guid) 2627cc8e27ccSEli Cohen return -EOPNOTSUPP; 2628cc8e27ccSEli Cohen 2629cc8e27ccSEli Cohen return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID); 2630cc8e27ccSEli Cohen } 2631cc8e27ccSEli Cohen 2632c02db8c6SChris Wright return err; 2633c02db8c6SChris Wright } 2634c02db8c6SChris Wright 263533eaf2a6SDavid Ahern static int do_set_master(struct net_device *dev, int ifindex, 263633eaf2a6SDavid Ahern struct netlink_ext_ack *extack) 2637fbaec0eaSJiri Pirko { 2638898e5061SJiri Pirko struct net_device *upper_dev = netdev_master_upper_dev_get(dev); 2639fbaec0eaSJiri Pirko const struct net_device_ops *ops; 2640fbaec0eaSJiri Pirko int err; 2641fbaec0eaSJiri Pirko 2642898e5061SJiri Pirko if (upper_dev) { 2643898e5061SJiri Pirko if (upper_dev->ifindex == ifindex) 2644fbaec0eaSJiri Pirko return 0; 2645898e5061SJiri Pirko ops = upper_dev->netdev_ops; 2646fbaec0eaSJiri Pirko if (ops->ndo_del_slave) { 2647898e5061SJiri Pirko err = ops->ndo_del_slave(upper_dev, dev); 2648fbaec0eaSJiri Pirko if (err) 2649fbaec0eaSJiri Pirko return err; 2650fbaec0eaSJiri Pirko } else { 2651fbaec0eaSJiri Pirko return -EOPNOTSUPP; 2652fbaec0eaSJiri Pirko } 2653fbaec0eaSJiri Pirko } 2654fbaec0eaSJiri Pirko 2655fbaec0eaSJiri Pirko if (ifindex) { 2656898e5061SJiri Pirko upper_dev = __dev_get_by_index(dev_net(dev), ifindex); 2657898e5061SJiri Pirko if (!upper_dev) 2658fbaec0eaSJiri Pirko return -EINVAL; 2659898e5061SJiri Pirko ops = upper_dev->netdev_ops; 2660fbaec0eaSJiri Pirko if (ops->ndo_add_slave) { 266133eaf2a6SDavid Ahern err = ops->ndo_add_slave(upper_dev, dev, extack); 2662fbaec0eaSJiri Pirko if (err) 2663fbaec0eaSJiri Pirko return err; 2664fbaec0eaSJiri Pirko } else { 2665fbaec0eaSJiri Pirko return -EOPNOTSUPP; 2666fbaec0eaSJiri Pirko } 2667fbaec0eaSJiri Pirko } 2668fbaec0eaSJiri Pirko return 0; 2669fbaec0eaSJiri Pirko } 2670fbaec0eaSJiri Pirko 2671829eb208SRoopa Prabhu static const struct nla_policy ifla_proto_down_reason_policy[IFLA_PROTO_DOWN_REASON_VALUE + 1] = { 2672829eb208SRoopa Prabhu [IFLA_PROTO_DOWN_REASON_MASK] = { .type = NLA_U32 }, 2673829eb208SRoopa Prabhu [IFLA_PROTO_DOWN_REASON_VALUE] = { .type = NLA_U32 }, 2674829eb208SRoopa Prabhu }; 2675829eb208SRoopa Prabhu 2676829eb208SRoopa Prabhu static int do_set_proto_down(struct net_device *dev, 2677829eb208SRoopa Prabhu struct nlattr *nl_proto_down, 2678829eb208SRoopa Prabhu struct nlattr *nl_proto_down_reason, 2679829eb208SRoopa Prabhu struct netlink_ext_ack *extack) 2680829eb208SRoopa Prabhu { 2681829eb208SRoopa Prabhu struct nlattr *pdreason[IFLA_PROTO_DOWN_REASON_MAX + 1]; 2682829eb208SRoopa Prabhu unsigned long mask = 0; 2683829eb208SRoopa Prabhu u32 value; 2684829eb208SRoopa Prabhu bool proto_down; 2685829eb208SRoopa Prabhu int err; 2686829eb208SRoopa Prabhu 26872106efdaSJakub Kicinski if (!(dev->priv_flags & IFF_CHANGE_PROTO_DOWN)) { 2688829eb208SRoopa Prabhu NL_SET_ERR_MSG(extack, "Protodown not supported by device"); 2689829eb208SRoopa Prabhu return -EOPNOTSUPP; 2690829eb208SRoopa Prabhu } 2691829eb208SRoopa Prabhu 2692829eb208SRoopa Prabhu if (nl_proto_down_reason) { 2693829eb208SRoopa Prabhu err = nla_parse_nested_deprecated(pdreason, 2694829eb208SRoopa Prabhu IFLA_PROTO_DOWN_REASON_MAX, 2695829eb208SRoopa Prabhu nl_proto_down_reason, 2696829eb208SRoopa Prabhu ifla_proto_down_reason_policy, 2697829eb208SRoopa Prabhu NULL); 2698829eb208SRoopa Prabhu if (err < 0) 2699829eb208SRoopa Prabhu return err; 2700829eb208SRoopa Prabhu 2701829eb208SRoopa Prabhu if (!pdreason[IFLA_PROTO_DOWN_REASON_VALUE]) { 2702829eb208SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid protodown reason value"); 2703829eb208SRoopa Prabhu return -EINVAL; 2704829eb208SRoopa Prabhu } 2705829eb208SRoopa Prabhu 2706829eb208SRoopa Prabhu value = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_VALUE]); 2707829eb208SRoopa Prabhu 2708829eb208SRoopa Prabhu if (pdreason[IFLA_PROTO_DOWN_REASON_MASK]) 2709829eb208SRoopa Prabhu mask = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_MASK]); 2710829eb208SRoopa Prabhu 2711829eb208SRoopa Prabhu dev_change_proto_down_reason(dev, mask, value); 2712829eb208SRoopa Prabhu } 2713829eb208SRoopa Prabhu 2714829eb208SRoopa Prabhu if (nl_proto_down) { 2715829eb208SRoopa Prabhu proto_down = nla_get_u8(nl_proto_down); 2716829eb208SRoopa Prabhu 2717d467d0bcSZheng Yongjun /* Don't turn off protodown if there are active reasons */ 2718829eb208SRoopa Prabhu if (!proto_down && dev->proto_down_reason) { 2719829eb208SRoopa Prabhu NL_SET_ERR_MSG(extack, "Cannot clear protodown, active reasons"); 2720829eb208SRoopa Prabhu return -EBUSY; 2721829eb208SRoopa Prabhu } 2722829eb208SRoopa Prabhu err = dev_change_proto_down(dev, 2723829eb208SRoopa Prabhu proto_down); 2724829eb208SRoopa Prabhu if (err) 2725829eb208SRoopa Prabhu return err; 2726829eb208SRoopa Prabhu } 2727829eb208SRoopa Prabhu 2728829eb208SRoopa Prabhu return 0; 2729829eb208SRoopa Prabhu } 2730829eb208SRoopa Prabhu 273190c325e3SNicolas Dichtel #define DO_SETLINK_MODIFIED 0x01 2732ba998906SNicolas Dichtel /* notify flag means notify + modified. */ 2733ba998906SNicolas Dichtel #define DO_SETLINK_NOTIFY 0x03 273490f62cf3SEric W. Biederman static int do_setlink(const struct sk_buff *skb, 273590f62cf3SEric W. Biederman struct net_device *dev, struct ifinfomsg *ifm, 2736ddf9f970SJakub Kicinski struct netlink_ext_ack *extack, 27375ea08b52SFlorent Fourcot struct nlattr **tb, int status) 27380157f60cSPatrick McHardy { 2739d314774cSStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops; 27405ea08b52SFlorent Fourcot char ifname[IFNAMSIZ]; 27410157f60cSPatrick McHardy int err; 27420157f60cSPatrick McHardy 27435ea08b52SFlorent Fourcot if (tb[IFLA_IFNAME]) 27445ea08b52SFlorent Fourcot nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 27455ea08b52SFlorent Fourcot else 27465ea08b52SFlorent Fourcot ifname[0] = '\0'; 27475ea08b52SFlorent Fourcot 27487e4a8d5aSChristian Brauner if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_TARGET_NETNSID]) { 27495ea08b52SFlorent Fourcot const char *pat = ifname[0] ? ifname : NULL; 2750eeb85a14SAndrei Vagin struct net *net; 2751eeb85a14SAndrei Vagin int new_ifindex; 2752eeb85a14SAndrei Vagin 2753eeb85a14SAndrei Vagin net = rtnl_link_get_net_capable(skb, dev_net(dev), 27547c4f63baSChristian Brauner tb, CAP_NET_ADMIN); 2755d8a5ec67SEric W. Biederman if (IS_ERR(net)) { 2756d8a5ec67SEric W. Biederman err = PTR_ERR(net); 2757d8a5ec67SEric W. Biederman goto errout; 2758d8a5ec67SEric W. Biederman } 27597c4f63baSChristian Brauner 2760eeb85a14SAndrei Vagin if (tb[IFLA_NEW_IFINDEX]) 2761eeb85a14SAndrei Vagin new_ifindex = nla_get_s32(tb[IFLA_NEW_IFINDEX]); 2762eeb85a14SAndrei Vagin else 2763eeb85a14SAndrei Vagin new_ifindex = 0; 2764eeb85a14SAndrei Vagin 276596a6b93bSAndrey Ignatov err = __dev_change_net_namespace(dev, net, pat, new_ifindex); 2766d8a5ec67SEric W. Biederman put_net(net); 2767d8a5ec67SEric W. Biederman if (err) 2768d8a5ec67SEric W. Biederman goto errout; 276990c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 2770d8a5ec67SEric W. Biederman } 2771d8a5ec67SEric W. Biederman 27720157f60cSPatrick McHardy if (tb[IFLA_MAP]) { 27730157f60cSPatrick McHardy struct rtnl_link_ifmap *u_map; 27740157f60cSPatrick McHardy struct ifmap k_map; 27750157f60cSPatrick McHardy 2776d314774cSStephen Hemminger if (!ops->ndo_set_config) { 27770157f60cSPatrick McHardy err = -EOPNOTSUPP; 27780157f60cSPatrick McHardy goto errout; 27790157f60cSPatrick McHardy } 27800157f60cSPatrick McHardy 27810157f60cSPatrick McHardy if (!netif_device_present(dev)) { 27820157f60cSPatrick McHardy err = -ENODEV; 27830157f60cSPatrick McHardy goto errout; 27840157f60cSPatrick McHardy } 27850157f60cSPatrick McHardy 27860157f60cSPatrick McHardy u_map = nla_data(tb[IFLA_MAP]); 27870157f60cSPatrick McHardy k_map.mem_start = (unsigned long) u_map->mem_start; 27880157f60cSPatrick McHardy k_map.mem_end = (unsigned long) u_map->mem_end; 27890157f60cSPatrick McHardy k_map.base_addr = (unsigned short) u_map->base_addr; 27900157f60cSPatrick McHardy k_map.irq = (unsigned char) u_map->irq; 27910157f60cSPatrick McHardy k_map.dma = (unsigned char) u_map->dma; 27920157f60cSPatrick McHardy k_map.port = (unsigned char) u_map->port; 27930157f60cSPatrick McHardy 2794d314774cSStephen Hemminger err = ops->ndo_set_config(dev, &k_map); 27950157f60cSPatrick McHardy if (err < 0) 27960157f60cSPatrick McHardy goto errout; 27970157f60cSPatrick McHardy 2798ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 27990157f60cSPatrick McHardy } 28000157f60cSPatrick McHardy 28010157f60cSPatrick McHardy if (tb[IFLA_ADDRESS]) { 28020157f60cSPatrick McHardy struct sockaddr *sa; 28030157f60cSPatrick McHardy int len; 28040157f60cSPatrick McHardy 2805153711f9SWANG Cong len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len, 2806153711f9SWANG Cong sizeof(*sa)); 28070157f60cSPatrick McHardy sa = kmalloc(len, GFP_KERNEL); 28080157f60cSPatrick McHardy if (!sa) { 28090157f60cSPatrick McHardy err = -ENOMEM; 28100157f60cSPatrick McHardy goto errout; 28110157f60cSPatrick McHardy } 28120157f60cSPatrick McHardy sa->sa_family = dev->type; 28130157f60cSPatrick McHardy memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]), 28140157f60cSPatrick McHardy dev->addr_len); 28153b23a32aSCong Wang err = dev_set_mac_address_user(dev, sa, extack); 28160157f60cSPatrick McHardy kfree(sa); 28170157f60cSPatrick McHardy if (err) 28180157f60cSPatrick McHardy goto errout; 281990c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 28200157f60cSPatrick McHardy } 28210157f60cSPatrick McHardy 28220157f60cSPatrick McHardy if (tb[IFLA_MTU]) { 28237a4c53beSStephen Hemminger err = dev_set_mtu_ext(dev, nla_get_u32(tb[IFLA_MTU]), extack); 28240157f60cSPatrick McHardy if (err < 0) 28250157f60cSPatrick McHardy goto errout; 282690c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 28270157f60cSPatrick McHardy } 28280157f60cSPatrick McHardy 2829cbda10faSVlad Dogaru if (tb[IFLA_GROUP]) { 2830cbda10faSVlad Dogaru dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); 2831ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 2832cbda10faSVlad Dogaru } 2833cbda10faSVlad Dogaru 28340157f60cSPatrick McHardy /* 28350157f60cSPatrick McHardy * Interface selected by interface index but interface 28360157f60cSPatrick McHardy * name provided implies that a name change has been 28370157f60cSPatrick McHardy * requested. 28380157f60cSPatrick McHardy */ 28390157f60cSPatrick McHardy if (ifm->ifi_index > 0 && ifname[0]) { 28400157f60cSPatrick McHardy err = dev_change_name(dev, ifname); 28410157f60cSPatrick McHardy if (err < 0) 28420157f60cSPatrick McHardy goto errout; 284390c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 28440157f60cSPatrick McHardy } 28450157f60cSPatrick McHardy 28460b815a1aSStephen Hemminger if (tb[IFLA_IFALIAS]) { 28470b815a1aSStephen Hemminger err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]), 28480b815a1aSStephen Hemminger nla_len(tb[IFLA_IFALIAS])); 28490b815a1aSStephen Hemminger if (err < 0) 28500b815a1aSStephen Hemminger goto errout; 2851ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 28520b815a1aSStephen Hemminger } 28530b815a1aSStephen Hemminger 28540157f60cSPatrick McHardy if (tb[IFLA_BROADCAST]) { 28550157f60cSPatrick McHardy nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len); 2856e7c3273eSJiri Pirko call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); 28570157f60cSPatrick McHardy } 28580157f60cSPatrick McHardy 2859fbaec0eaSJiri Pirko if (tb[IFLA_MASTER]) { 286033eaf2a6SDavid Ahern err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack); 2861fbaec0eaSJiri Pirko if (err) 2862fbaec0eaSJiri Pirko goto errout; 286390c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 2864fbaec0eaSJiri Pirko } 2865fbaec0eaSJiri Pirko 2866a4abfa62SPhil Sutter if (ifm->ifi_flags || ifm->ifi_change) { 2867a4abfa62SPhil Sutter err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm), 2868a4abfa62SPhil Sutter extack); 2869a4abfa62SPhil Sutter if (err < 0) 2870a4abfa62SPhil Sutter goto errout; 2871a4abfa62SPhil Sutter } 2872a4abfa62SPhil Sutter 28739a57247fSJiri Pirko if (tb[IFLA_CARRIER]) { 28749a57247fSJiri Pirko err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER])); 28759a57247fSJiri Pirko if (err) 28769a57247fSJiri Pirko goto errout; 287790c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 28789a57247fSJiri Pirko } 28799a57247fSJiri Pirko 28805d1180fcSNicolas Dichtel if (tb[IFLA_TXQLEN]) { 28810cd29503SAlexey Dobriyan unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]); 28825d1180fcSNicolas Dichtel 28836a643ddbSCong Wang err = dev_change_tx_queue_len(dev, value); 28846a643ddbSCong Wang if (err) 288508294a26SJason Wang goto errout; 28862d7f669bSXin Long status |= DO_SETLINK_MODIFIED; 288708294a26SJason Wang } 28880157f60cSPatrick McHardy 288946e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SIZE]) { 289046e6b992SStephen Hemminger u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]); 289146e6b992SStephen Hemminger 289246e6b992SStephen Hemminger if (dev->gso_max_size ^ max_size) { 289346e6b992SStephen Hemminger netif_set_gso_max_size(dev, max_size); 289446e6b992SStephen Hemminger status |= DO_SETLINK_MODIFIED; 289546e6b992SStephen Hemminger } 289646e6b992SStephen Hemminger } 289746e6b992SStephen Hemminger 289846e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SEGS]) { 289946e6b992SStephen Hemminger u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]); 290046e6b992SStephen Hemminger 290146e6b992SStephen Hemminger if (dev->gso_max_segs ^ max_segs) { 29026d872df3SEric Dumazet netif_set_gso_max_segs(dev, max_segs); 290346e6b992SStephen Hemminger status |= DO_SETLINK_MODIFIED; 290446e6b992SStephen Hemminger } 290546e6b992SStephen Hemminger } 290646e6b992SStephen Hemminger 2907eac1b93cSCoco Li if (tb[IFLA_GRO_MAX_SIZE]) { 2908eac1b93cSCoco Li u32 gro_max_size = nla_get_u32(tb[IFLA_GRO_MAX_SIZE]); 2909eac1b93cSCoco Li 2910eac1b93cSCoco Li if (dev->gro_max_size ^ gro_max_size) { 2911eac1b93cSCoco Li netif_set_gro_max_size(dev, gro_max_size); 2912eac1b93cSCoco Li status |= DO_SETLINK_MODIFIED; 2913eac1b93cSCoco Li } 2914eac1b93cSCoco Li } 2915eac1b93cSCoco Li 29169eefedd5SXin Long if (tb[IFLA_GSO_IPV4_MAX_SIZE]) { 29179eefedd5SXin Long u32 max_size = nla_get_u32(tb[IFLA_GSO_IPV4_MAX_SIZE]); 29189eefedd5SXin Long 29199eefedd5SXin Long if (dev->gso_ipv4_max_size ^ max_size) { 29209eefedd5SXin Long netif_set_gso_ipv4_max_size(dev, max_size); 29219eefedd5SXin Long status |= DO_SETLINK_MODIFIED; 29229eefedd5SXin Long } 29239eefedd5SXin Long } 29249eefedd5SXin Long 29259eefedd5SXin Long if (tb[IFLA_GRO_IPV4_MAX_SIZE]) { 29269eefedd5SXin Long u32 gro_max_size = nla_get_u32(tb[IFLA_GRO_IPV4_MAX_SIZE]); 29279eefedd5SXin Long 29289eefedd5SXin Long if (dev->gro_ipv4_max_size ^ gro_max_size) { 29299eefedd5SXin Long netif_set_gro_ipv4_max_size(dev, gro_max_size); 29309eefedd5SXin Long status |= DO_SETLINK_MODIFIED; 29319eefedd5SXin Long } 29329eefedd5SXin Long } 29339eefedd5SXin Long 29340157f60cSPatrick McHardy if (tb[IFLA_OPERSTATE]) 293593b2d4a2SDavid S. Miller set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 29360157f60cSPatrick McHardy 29370157f60cSPatrick McHardy if (tb[IFLA_LINKMODE]) { 29381889b0e7SNicolas Dichtel unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]); 29391889b0e7SNicolas Dichtel 2940fd888e85SSebastian Andrzej Siewior write_lock(&dev_base_lock); 29411889b0e7SNicolas Dichtel if (dev->link_mode ^ value) 2942ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 29431889b0e7SNicolas Dichtel dev->link_mode = value; 2944fd888e85SSebastian Andrzej Siewior write_unlock(&dev_base_lock); 29450157f60cSPatrick McHardy } 29460157f60cSPatrick McHardy 2947c02db8c6SChris Wright if (tb[IFLA_VFINFO_LIST]) { 29484f7d2cdfSDaniel Borkmann struct nlattr *vfinfo[IFLA_VF_MAX + 1]; 2949c02db8c6SChris Wright struct nlattr *attr; 2950c02db8c6SChris Wright int rem; 29514f7d2cdfSDaniel Borkmann 2952c02db8c6SChris Wright nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) { 29534f7d2cdfSDaniel Borkmann if (nla_type(attr) != IFLA_VF_INFO || 29544f7d2cdfSDaniel Borkmann nla_len(attr) < NLA_HDRLEN) { 2955253683bbSDavid Howells err = -EINVAL; 2956c02db8c6SChris Wright goto errout; 2957253683bbSDavid Howells } 29588cb08174SJohannes Berg err = nla_parse_nested_deprecated(vfinfo, IFLA_VF_MAX, 29598cb08174SJohannes Berg attr, 29608cb08174SJohannes Berg ifla_vf_policy, 29618cb08174SJohannes Berg NULL); 29624f7d2cdfSDaniel Borkmann if (err < 0) 29634f7d2cdfSDaniel Borkmann goto errout; 29644f7d2cdfSDaniel Borkmann err = do_setvfinfo(dev, vfinfo); 2965ebc08a6fSWilliams, Mitch A if (err < 0) 2966ebc08a6fSWilliams, Mitch A goto errout; 2967ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 2968ebc08a6fSWilliams, Mitch A } 2969ebc08a6fSWilliams, Mitch A } 29700157f60cSPatrick McHardy err = 0; 29710157f60cSPatrick McHardy 297257b61080SScott Feldman if (tb[IFLA_VF_PORTS]) { 297357b61080SScott Feldman struct nlattr *port[IFLA_PORT_MAX+1]; 297457b61080SScott Feldman struct nlattr *attr; 297557b61080SScott Feldman int vf; 297657b61080SScott Feldman int rem; 297757b61080SScott Feldman 297857b61080SScott Feldman err = -EOPNOTSUPP; 297957b61080SScott Feldman if (!ops->ndo_set_vf_port) 298057b61080SScott Feldman goto errout; 298157b61080SScott Feldman 298257b61080SScott Feldman nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) { 2983035d210fSDaniel Borkmann if (nla_type(attr) != IFLA_VF_PORT || 2984035d210fSDaniel Borkmann nla_len(attr) < NLA_HDRLEN) { 2985035d210fSDaniel Borkmann err = -EINVAL; 2986035d210fSDaniel Borkmann goto errout; 2987035d210fSDaniel Borkmann } 29888cb08174SJohannes Berg err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX, 29898cb08174SJohannes Berg attr, 29908cb08174SJohannes Berg ifla_port_policy, 29918cb08174SJohannes Berg NULL); 299257b61080SScott Feldman if (err < 0) 299357b61080SScott Feldman goto errout; 299457b61080SScott Feldman if (!port[IFLA_PORT_VF]) { 299557b61080SScott Feldman err = -EOPNOTSUPP; 299657b61080SScott Feldman goto errout; 299757b61080SScott Feldman } 299857b61080SScott Feldman vf = nla_get_u32(port[IFLA_PORT_VF]); 299957b61080SScott Feldman err = ops->ndo_set_vf_port(dev, vf, port); 300057b61080SScott Feldman if (err < 0) 300157b61080SScott Feldman goto errout; 3002ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 300357b61080SScott Feldman } 300457b61080SScott Feldman } 300557b61080SScott Feldman err = 0; 300657b61080SScott Feldman 300757b61080SScott Feldman if (tb[IFLA_PORT_SELF]) { 300857b61080SScott Feldman struct nlattr *port[IFLA_PORT_MAX+1]; 300957b61080SScott Feldman 30108cb08174SJohannes Berg err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX, 30118cb08174SJohannes Berg tb[IFLA_PORT_SELF], 30128cb08174SJohannes Berg ifla_port_policy, NULL); 301357b61080SScott Feldman if (err < 0) 301457b61080SScott Feldman goto errout; 301557b61080SScott Feldman 301657b61080SScott Feldman err = -EOPNOTSUPP; 301757b61080SScott Feldman if (ops->ndo_set_vf_port) 301857b61080SScott Feldman err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port); 301957b61080SScott Feldman if (err < 0) 302057b61080SScott Feldman goto errout; 3021ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 302257b61080SScott Feldman } 3023f8ff182cSThomas Graf 3024f8ff182cSThomas Graf if (tb[IFLA_AF_SPEC]) { 3025f8ff182cSThomas Graf struct nlattr *af; 3026f8ff182cSThomas Graf int rem; 3027f8ff182cSThomas Graf 3028f8ff182cSThomas Graf nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { 3029f8ff182cSThomas Graf const struct rtnl_af_ops *af_ops; 3030f8ff182cSThomas Graf 3031058c8d59SGustavo A. R. Silva BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af)))); 3032f8ff182cSThomas Graf 30333583a4e8SStephen Hemminger err = af_ops->set_link_af(dev, af, extack); 3034a100243dSCong Wang if (err < 0) 3035f8ff182cSThomas Graf goto errout; 3036f8ff182cSThomas Graf 3037ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 3038f8ff182cSThomas Graf } 3039f8ff182cSThomas Graf } 304057b61080SScott Feldman err = 0; 304157b61080SScott Feldman 3042829eb208SRoopa Prabhu if (tb[IFLA_PROTO_DOWN] || tb[IFLA_PROTO_DOWN_REASON]) { 3043829eb208SRoopa Prabhu err = do_set_proto_down(dev, tb[IFLA_PROTO_DOWN], 3044829eb208SRoopa Prabhu tb[IFLA_PROTO_DOWN_REASON], extack); 304588d6378bSAnuradha Karuppiah if (err) 304688d6378bSAnuradha Karuppiah goto errout; 304788d6378bSAnuradha Karuppiah status |= DO_SETLINK_NOTIFY; 304888d6378bSAnuradha Karuppiah } 304988d6378bSAnuradha Karuppiah 3050d1fdd913SBrenden Blanco if (tb[IFLA_XDP]) { 3051d1fdd913SBrenden Blanco struct nlattr *xdp[IFLA_XDP_MAX + 1]; 305285de8576SDaniel Borkmann u32 xdp_flags = 0; 3053d1fdd913SBrenden Blanco 30548cb08174SJohannes Berg err = nla_parse_nested_deprecated(xdp, IFLA_XDP_MAX, 30558cb08174SJohannes Berg tb[IFLA_XDP], 3056fceb6435SJohannes Berg ifla_xdp_policy, NULL); 3057d1fdd913SBrenden Blanco if (err < 0) 3058d1fdd913SBrenden Blanco goto errout; 3059d1fdd913SBrenden Blanco 306058038695SMartin KaFai Lau if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) { 3061262d8625SBrenden Blanco err = -EINVAL; 3062262d8625SBrenden Blanco goto errout; 3063262d8625SBrenden Blanco } 306485de8576SDaniel Borkmann 306585de8576SDaniel Borkmann if (xdp[IFLA_XDP_FLAGS]) { 306685de8576SDaniel Borkmann xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]); 306785de8576SDaniel Borkmann if (xdp_flags & ~XDP_FLAGS_MASK) { 306885de8576SDaniel Borkmann err = -EINVAL; 306985de8576SDaniel Borkmann goto errout; 307085de8576SDaniel Borkmann } 3071ee5d032fSJakub Kicinski if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) { 30720489df9aSDaniel Borkmann err = -EINVAL; 30730489df9aSDaniel Borkmann goto errout; 30740489df9aSDaniel Borkmann } 307585de8576SDaniel Borkmann } 307685de8576SDaniel Borkmann 3077d1fdd913SBrenden Blanco if (xdp[IFLA_XDP_FD]) { 307892234c8fSToke Høiland-Jørgensen int expected_fd = -1; 307992234c8fSToke Høiland-Jørgensen 308092234c8fSToke Høiland-Jørgensen if (xdp_flags & XDP_FLAGS_REPLACE) { 308192234c8fSToke Høiland-Jørgensen if (!xdp[IFLA_XDP_EXPECTED_FD]) { 308292234c8fSToke Høiland-Jørgensen err = -EINVAL; 308392234c8fSToke Høiland-Jørgensen goto errout; 308492234c8fSToke Høiland-Jørgensen } 308592234c8fSToke Høiland-Jørgensen expected_fd = 308692234c8fSToke Høiland-Jørgensen nla_get_s32(xdp[IFLA_XDP_EXPECTED_FD]); 308792234c8fSToke Høiland-Jørgensen } 308892234c8fSToke Høiland-Jørgensen 3089ddf9f970SJakub Kicinski err = dev_change_xdp_fd(dev, extack, 309085de8576SDaniel Borkmann nla_get_s32(xdp[IFLA_XDP_FD]), 309192234c8fSToke Høiland-Jørgensen expected_fd, 309285de8576SDaniel Borkmann xdp_flags); 3093d1fdd913SBrenden Blanco if (err) 3094d1fdd913SBrenden Blanco goto errout; 3095d1fdd913SBrenden Blanco status |= DO_SETLINK_NOTIFY; 3096d1fdd913SBrenden Blanco } 3097d1fdd913SBrenden Blanco } 3098d1fdd913SBrenden Blanco 30990157f60cSPatrick McHardy errout: 3100ba998906SNicolas Dichtel if (status & DO_SETLINK_MODIFIED) { 310164ff90ccSXin Long if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY) 3102ba998906SNicolas Dichtel netdev_state_change(dev); 3103ba998906SNicolas Dichtel 3104ba998906SNicolas Dichtel if (err < 0) 3105e87cc472SJoe 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", 3106e87cc472SJoe Perches dev->name); 3107ba998906SNicolas Dichtel } 31080157f60cSPatrick McHardy 31090157f60cSPatrick McHardy return err; 31100157f60cSPatrick McHardy } 31110157f60cSPatrick McHardy 3112cc6090e9SJiri Pirko static struct net_device *rtnl_dev_get(struct net *net, 31135ea08b52SFlorent Fourcot struct nlattr *tb[]) 3114cc6090e9SJiri Pirko { 31155ea08b52SFlorent Fourcot char ifname[ALTIFNAMSIZ]; 3116cc6090e9SJiri Pirko 31175ea08b52SFlorent Fourcot if (tb[IFLA_IFNAME]) 31185ea08b52SFlorent Fourcot nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 31195ea08b52SFlorent Fourcot else if (tb[IFLA_ALT_IFNAME]) 31205ea08b52SFlorent Fourcot nla_strscpy(ifname, tb[IFLA_ALT_IFNAME], ALTIFNAMSIZ); 3121cc6090e9SJiri Pirko else 3122cc6090e9SJiri Pirko return NULL; 3123cc6090e9SJiri Pirko 3124cc6090e9SJiri Pirko return __dev_get_by_name(net, ifname); 3125cc6090e9SJiri Pirko } 3126cc6090e9SJiri Pirko 3127c21ef3e3SDavid Ahern static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, 3128c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 3129da5e0494SThomas Graf { 31303b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 3131da5e0494SThomas Graf struct ifinfomsg *ifm; 3132da5e0494SThomas Graf struct net_device *dev; 31330157f60cSPatrick McHardy int err; 3134da5e0494SThomas Graf struct nlattr *tb[IFLA_MAX+1]; 31351da177e4SLinus Torvalds 31368cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX, 31378cb08174SJohannes Berg ifla_policy, extack); 3138da5e0494SThomas Graf if (err < 0) 3139da5e0494SThomas Graf goto errout; 31401da177e4SLinus Torvalds 31414ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, false); 31424ff66caeSChristian Brauner if (err < 0) 31434ff66caeSChristian Brauner goto errout; 31444ff66caeSChristian Brauner 31451da177e4SLinus Torvalds err = -EINVAL; 3146da5e0494SThomas Graf ifm = nlmsg_data(nlh); 314751055be8SPatrick McHardy if (ifm->ifi_index > 0) 3148a3d12891SEric Dumazet dev = __dev_get_by_index(net, ifm->ifi_index); 314976c9ac0eSJiri Pirko else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) 31505ea08b52SFlorent Fourcot dev = rtnl_dev_get(net, tb); 3151da5e0494SThomas Graf else 3152da5e0494SThomas Graf goto errout; 31531da177e4SLinus Torvalds 3154da5e0494SThomas Graf if (dev == NULL) { 3155da5e0494SThomas Graf err = -ENODEV; 3156da5e0494SThomas Graf goto errout; 3157da5e0494SThomas Graf } 31581da177e4SLinus Torvalds 3159*89da780aSXin Long err = validate_linkmsg(dev, tb, extack); 3160*89da780aSXin Long if (err < 0) 3161*89da780aSXin Long goto errout; 3162*89da780aSXin Long 31635ea08b52SFlorent Fourcot err = do_setlink(skb, dev, ifm, extack, tb, 0); 3164da5e0494SThomas Graf errout: 31651da177e4SLinus Torvalds return err; 31661da177e4SLinus Torvalds } 31671da177e4SLinus Torvalds 316866400d54SWANG Cong static int rtnl_group_dellink(const struct net *net, int group) 316966400d54SWANG Cong { 317066400d54SWANG Cong struct net_device *dev, *aux; 317166400d54SWANG Cong LIST_HEAD(list_kill); 317266400d54SWANG Cong bool found = false; 317366400d54SWANG Cong 317466400d54SWANG Cong if (!group) 317566400d54SWANG Cong return -EPERM; 317666400d54SWANG Cong 317766400d54SWANG Cong for_each_netdev(net, dev) { 317866400d54SWANG Cong if (dev->group == group) { 317966400d54SWANG Cong const struct rtnl_link_ops *ops; 318066400d54SWANG Cong 318166400d54SWANG Cong found = true; 318266400d54SWANG Cong ops = dev->rtnl_link_ops; 318366400d54SWANG Cong if (!ops || !ops->dellink) 318466400d54SWANG Cong return -EOPNOTSUPP; 318566400d54SWANG Cong } 318666400d54SWANG Cong } 318766400d54SWANG Cong 318866400d54SWANG Cong if (!found) 318966400d54SWANG Cong return -ENODEV; 319066400d54SWANG Cong 319166400d54SWANG Cong for_each_netdev_safe(net, dev, aux) { 319266400d54SWANG Cong if (dev->group == group) { 319366400d54SWANG Cong const struct rtnl_link_ops *ops; 319466400d54SWANG Cong 319566400d54SWANG Cong ops = dev->rtnl_link_ops; 319666400d54SWANG Cong ops->dellink(dev, &list_kill); 319766400d54SWANG Cong } 319866400d54SWANG Cong } 319966400d54SWANG Cong unregister_netdevice_many(&list_kill); 320066400d54SWANG Cong 320166400d54SWANG Cong return 0; 320266400d54SWANG Cong } 320366400d54SWANG Cong 3204f3a63cceSHangbin Liu int rtnl_delete_link(struct net_device *dev, u32 portid, const struct nlmsghdr *nlh) 3205614732eaSThomas Graf { 3206614732eaSThomas Graf const struct rtnl_link_ops *ops; 3207614732eaSThomas Graf LIST_HEAD(list_kill); 3208614732eaSThomas Graf 3209614732eaSThomas Graf ops = dev->rtnl_link_ops; 3210614732eaSThomas Graf if (!ops || !ops->dellink) 3211614732eaSThomas Graf return -EOPNOTSUPP; 3212614732eaSThomas Graf 3213614732eaSThomas Graf ops->dellink(dev, &list_kill); 3214f3a63cceSHangbin Liu unregister_netdevice_many_notify(&list_kill, portid, nlh); 3215614732eaSThomas Graf 3216614732eaSThomas Graf return 0; 3217614732eaSThomas Graf } 3218614732eaSThomas Graf EXPORT_SYMBOL_GPL(rtnl_delete_link); 3219614732eaSThomas Graf 3220c21ef3e3SDavid Ahern static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, 3221c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 322238f7b870SPatrick McHardy { 32233b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 3224f3a63cceSHangbin Liu u32 portid = NETLINK_CB(skb).portid; 3225b61ad68aSChristian Brauner struct net *tgt_net = net; 3226b61ad68aSChristian Brauner struct net_device *dev = NULL; 322738f7b870SPatrick McHardy struct ifinfomsg *ifm; 322838f7b870SPatrick McHardy struct nlattr *tb[IFLA_MAX+1]; 322938f7b870SPatrick McHardy int err; 3230b61ad68aSChristian Brauner int netnsid = -1; 323138f7b870SPatrick McHardy 32328cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX, 32338cb08174SJohannes Berg ifla_policy, extack); 323438f7b870SPatrick McHardy if (err < 0) 323538f7b870SPatrick McHardy return err; 323638f7b870SPatrick McHardy 32374ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, true); 32384ff66caeSChristian Brauner if (err < 0) 32394ff66caeSChristian Brauner return err; 32404ff66caeSChristian Brauner 32417e4a8d5aSChristian Brauner if (tb[IFLA_TARGET_NETNSID]) { 32427e4a8d5aSChristian Brauner netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]); 3243c383edc4SChristian Brauner tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid); 3244b61ad68aSChristian Brauner if (IS_ERR(tgt_net)) 3245b61ad68aSChristian Brauner return PTR_ERR(tgt_net); 3246b61ad68aSChristian Brauner } 3247b61ad68aSChristian Brauner 3248b61ad68aSChristian Brauner err = -EINVAL; 324938f7b870SPatrick McHardy ifm = nlmsg_data(nlh); 325038f7b870SPatrick McHardy if (ifm->ifi_index > 0) 3251b61ad68aSChristian Brauner dev = __dev_get_by_index(tgt_net, ifm->ifi_index); 325276c9ac0eSJiri Pirko else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) 32535ea08b52SFlorent Fourcot dev = rtnl_dev_get(net, tb); 325466400d54SWANG Cong else if (tb[IFLA_GROUP]) 3255b61ad68aSChristian Brauner err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP])); 325638f7b870SPatrick McHardy else 3257b61ad68aSChristian Brauner goto out; 325838f7b870SPatrick McHardy 3259b61ad68aSChristian Brauner if (!dev) { 3260dee04163SFlorent Fourcot if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME] || ifm->ifi_index > 0) 3261b61ad68aSChristian Brauner err = -ENODEV; 326238f7b870SPatrick McHardy 3263b61ad68aSChristian Brauner goto out; 3264b61ad68aSChristian Brauner } 3265b61ad68aSChristian Brauner 3266f3a63cceSHangbin Liu err = rtnl_delete_link(dev, portid, nlh); 3267b61ad68aSChristian Brauner 3268b61ad68aSChristian Brauner out: 3269b61ad68aSChristian Brauner if (netnsid >= 0) 3270b61ad68aSChristian Brauner put_net(tgt_net); 3271b61ad68aSChristian Brauner 3272b61ad68aSChristian Brauner return err; 327338f7b870SPatrick McHardy } 327438f7b870SPatrick McHardy 32751d997f10SHangbin Liu int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm, 32761d997f10SHangbin Liu u32 portid, const struct nlmsghdr *nlh) 32773729d502SPatrick McHardy { 32783729d502SPatrick McHardy unsigned int old_flags; 32793729d502SPatrick McHardy int err; 32803729d502SPatrick McHardy 32813729d502SPatrick McHardy old_flags = dev->flags; 32823729d502SPatrick McHardy if (ifm && (ifm->ifi_flags || ifm->ifi_change)) { 32836d040321SPetr Machata err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm), 32846d040321SPetr Machata NULL); 32853729d502SPatrick McHardy if (err < 0) 32863729d502SPatrick McHardy return err; 32873729d502SPatrick McHardy } 32883729d502SPatrick McHardy 32898d356b89SRoopa Prabhu if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) { 32901d997f10SHangbin Liu __dev_notify_flags(dev, old_flags, (old_flags ^ dev->flags), portid, nlh); 32918d356b89SRoopa Prabhu } else { 32923729d502SPatrick McHardy dev->rtnl_link_state = RTNL_LINK_INITIALIZED; 32931d997f10SHangbin Liu __dev_notify_flags(dev, old_flags, ~0U, portid, nlh); 32948d356b89SRoopa Prabhu } 32953729d502SPatrick McHardy return 0; 32963729d502SPatrick McHardy } 32973729d502SPatrick McHardy EXPORT_SYMBOL(rtnl_configure_link); 32983729d502SPatrick McHardy 3299d0522f1cSDavid Ahern struct net_device *rtnl_create_link(struct net *net, const char *ifname, 3300d0522f1cSDavid Ahern unsigned char name_assign_type, 3301d0522f1cSDavid Ahern const struct rtnl_link_ops *ops, 3302d0522f1cSDavid Ahern struct nlattr *tb[], 3303d0522f1cSDavid Ahern struct netlink_ext_ack *extack) 3304e7199288SPavel Emelianov { 3305e7199288SPavel Emelianov struct net_device *dev; 3306d40156aaSJiri Pirko unsigned int num_tx_queues = 1; 3307d40156aaSJiri Pirko unsigned int num_rx_queues = 1; 3308b0ad3c17SXin Long int err; 3309e7199288SPavel Emelianov 331076ff5cc9SJiri Pirko if (tb[IFLA_NUM_TX_QUEUES]) 331176ff5cc9SJiri Pirko num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]); 331276ff5cc9SJiri Pirko else if (ops->get_num_tx_queues) 3313d40156aaSJiri Pirko num_tx_queues = ops->get_num_tx_queues(); 331476ff5cc9SJiri Pirko 331576ff5cc9SJiri Pirko if (tb[IFLA_NUM_RX_QUEUES]) 331676ff5cc9SJiri Pirko num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]); 331776ff5cc9SJiri Pirko else if (ops->get_num_rx_queues) 3318d40156aaSJiri Pirko num_rx_queues = ops->get_num_rx_queues(); 3319efacb309Sstephen hemminger 3320d0522f1cSDavid Ahern if (num_tx_queues < 1 || num_tx_queues > 4096) { 3321d0522f1cSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid number of transmit queues"); 33220e1d6ecaSEric Dumazet return ERR_PTR(-EINVAL); 3323d0522f1cSDavid Ahern } 33240e1d6ecaSEric Dumazet 3325d0522f1cSDavid Ahern if (num_rx_queues < 1 || num_rx_queues > 4096) { 3326d0522f1cSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid number of receive queues"); 33270e1d6ecaSEric Dumazet return ERR_PTR(-EINVAL); 3328d0522f1cSDavid Ahern } 33290e1d6ecaSEric Dumazet 33308c713dc9SJohannes Berg if (ops->alloc) { 33318c713dc9SJohannes Berg dev = ops->alloc(tb, ifname, name_assign_type, 33328c713dc9SJohannes Berg num_tx_queues, num_rx_queues); 33338c713dc9SJohannes Berg if (IS_ERR(dev)) 33348c713dc9SJohannes Berg return dev; 33358c713dc9SJohannes Berg } else { 33368c713dc9SJohannes Berg dev = alloc_netdev_mqs(ops->priv_size, ifname, 33378c713dc9SJohannes Berg name_assign_type, ops->setup, 33388c713dc9SJohannes Berg num_tx_queues, num_rx_queues); 33398c713dc9SJohannes Berg } 33408c713dc9SJohannes Berg 3341e7199288SPavel Emelianov if (!dev) 3342d1892e4eSTobias Klauser return ERR_PTR(-ENOMEM); 3343e7199288SPavel Emelianov 3344b0ad3c17SXin Long err = validate_linkmsg(dev, tb, extack); 3345b0ad3c17SXin Long if (err < 0) { 3346b0ad3c17SXin Long free_netdev(dev); 3347b0ad3c17SXin Long return ERR_PTR(err); 3348b0ad3c17SXin Long } 3349b0ad3c17SXin Long 335081adee47SEric W. Biederman dev_net_set(dev, net); 335181adee47SEric W. Biederman dev->rtnl_link_ops = ops; 33523729d502SPatrick McHardy dev->rtnl_link_state = RTNL_LINK_INITIALIZING; 335381adee47SEric W. Biederman 3354d836f5c6SEric Dumazet if (tb[IFLA_MTU]) { 3355d836f5c6SEric Dumazet u32 mtu = nla_get_u32(tb[IFLA_MTU]); 3356d836f5c6SEric Dumazet 3357d836f5c6SEric Dumazet err = dev_validate_mtu(dev, mtu, extack); 3358d836f5c6SEric Dumazet if (err) { 3359d836f5c6SEric Dumazet free_netdev(dev); 3360d836f5c6SEric Dumazet return ERR_PTR(err); 3361d836f5c6SEric Dumazet } 3362d836f5c6SEric Dumazet dev->mtu = mtu; 3363d836f5c6SEric Dumazet } 33642afb9b53SJiri Pirko if (tb[IFLA_ADDRESS]) { 3365efd38f75SJakub Kicinski __dev_addr_set(dev, nla_data(tb[IFLA_ADDRESS]), 3366e7199288SPavel Emelianov nla_len(tb[IFLA_ADDRESS])); 33672afb9b53SJiri Pirko dev->addr_assign_type = NET_ADDR_SET; 33682afb9b53SJiri Pirko } 3369e7199288SPavel Emelianov if (tb[IFLA_BROADCAST]) 3370e7199288SPavel Emelianov memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]), 3371e7199288SPavel Emelianov nla_len(tb[IFLA_BROADCAST])); 3372e7199288SPavel Emelianov if (tb[IFLA_TXQLEN]) 3373e7199288SPavel Emelianov dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); 3374e7199288SPavel Emelianov if (tb[IFLA_OPERSTATE]) 337593b2d4a2SDavid S. Miller set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 3376e7199288SPavel Emelianov if (tb[IFLA_LINKMODE]) 3377e7199288SPavel Emelianov dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); 3378ffa934f1SPatrick McHardy if (tb[IFLA_GROUP]) 3379ffa934f1SPatrick McHardy dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); 338046e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SIZE]) 338146e6b992SStephen Hemminger netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE])); 338246e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SEGS]) 33836d872df3SEric Dumazet netif_set_gso_max_segs(dev, nla_get_u32(tb[IFLA_GSO_MAX_SEGS])); 3384eac1b93cSCoco Li if (tb[IFLA_GRO_MAX_SIZE]) 3385eac1b93cSCoco Li netif_set_gro_max_size(dev, nla_get_u32(tb[IFLA_GRO_MAX_SIZE])); 33869eefedd5SXin Long if (tb[IFLA_GSO_IPV4_MAX_SIZE]) 33879eefedd5SXin Long netif_set_gso_ipv4_max_size(dev, nla_get_u32(tb[IFLA_GSO_IPV4_MAX_SIZE])); 33889eefedd5SXin Long if (tb[IFLA_GRO_IPV4_MAX_SIZE]) 33899eefedd5SXin Long netif_set_gro_ipv4_max_size(dev, nla_get_u32(tb[IFLA_GRO_IPV4_MAX_SIZE])); 3390e7199288SPavel Emelianov 3391e7199288SPavel Emelianov return dev; 3392e7199288SPavel Emelianov } 3393e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_create_link); 3394e7199288SPavel Emelianov 339590f62cf3SEric W. Biederman static int rtnl_group_changelink(const struct sk_buff *skb, 339690f62cf3SEric W. Biederman struct net *net, int group, 3397e7ed828fSVlad Dogaru struct ifinfomsg *ifm, 3398ddf9f970SJakub Kicinski struct netlink_ext_ack *extack, 3399e7ed828fSVlad Dogaru struct nlattr **tb) 3400e7ed828fSVlad Dogaru { 3401d079535dSWANG Cong struct net_device *dev, *aux; 3402e7ed828fSVlad Dogaru int err; 3403e7ed828fSVlad Dogaru 3404d079535dSWANG Cong for_each_netdev_safe(net, dev, aux) { 3405e7ed828fSVlad Dogaru if (dev->group == group) { 3406*89da780aSXin Long err = validate_linkmsg(dev, tb, extack); 3407*89da780aSXin Long if (err < 0) 3408*89da780aSXin Long return err; 34095ea08b52SFlorent Fourcot err = do_setlink(skb, dev, ifm, extack, tb, 0); 3410e7ed828fSVlad Dogaru if (err < 0) 3411e7ed828fSVlad Dogaru return err; 3412e7ed828fSVlad Dogaru } 3413e7ed828fSVlad Dogaru } 3414e7ed828fSVlad Dogaru 3415e7ed828fSVlad Dogaru return 0; 3416e7ed828fSVlad Dogaru } 3417e7ed828fSVlad Dogaru 341863105e83SJakub Kicinski static int rtnl_newlink_create(struct sk_buff *skb, struct ifinfomsg *ifm, 341963105e83SJakub Kicinski const struct rtnl_link_ops *ops, 3420d88e136cSHangbin Liu const struct nlmsghdr *nlh, 342163105e83SJakub Kicinski struct nlattr **tb, struct nlattr **data, 342202839cc8SJakub Kicinski struct netlink_ext_ack *extack) 342302839cc8SJakub Kicinski { 342402839cc8SJakub Kicinski unsigned char name_assign_type = NET_NAME_USER; 342502839cc8SJakub Kicinski struct net *net = sock_net(skb->sk); 3426d88e136cSHangbin Liu u32 portid = NETLINK_CB(skb).portid; 342702839cc8SJakub Kicinski struct net *dest_net, *link_net; 342802839cc8SJakub Kicinski struct net_device *dev; 342902839cc8SJakub Kicinski char ifname[IFNAMSIZ]; 343002839cc8SJakub Kicinski int err; 343102839cc8SJakub Kicinski 343202839cc8SJakub Kicinski if (!ops->alloc && !ops->setup) 343302839cc8SJakub Kicinski return -EOPNOTSUPP; 343402839cc8SJakub Kicinski 343502839cc8SJakub Kicinski if (tb[IFLA_IFNAME]) { 343602839cc8SJakub Kicinski nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 343702839cc8SJakub Kicinski } else { 343802839cc8SJakub Kicinski snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind); 343902839cc8SJakub Kicinski name_assign_type = NET_NAME_ENUM; 344002839cc8SJakub Kicinski } 344102839cc8SJakub Kicinski 344202839cc8SJakub Kicinski dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN); 344302839cc8SJakub Kicinski if (IS_ERR(dest_net)) 344402839cc8SJakub Kicinski return PTR_ERR(dest_net); 344502839cc8SJakub Kicinski 344602839cc8SJakub Kicinski if (tb[IFLA_LINK_NETNSID]) { 344702839cc8SJakub Kicinski int id = nla_get_s32(tb[IFLA_LINK_NETNSID]); 344802839cc8SJakub Kicinski 344902839cc8SJakub Kicinski link_net = get_net_ns_by_id(dest_net, id); 345002839cc8SJakub Kicinski if (!link_net) { 345102839cc8SJakub Kicinski NL_SET_ERR_MSG(extack, "Unknown network namespace id"); 345202839cc8SJakub Kicinski err = -EINVAL; 345302839cc8SJakub Kicinski goto out; 345402839cc8SJakub Kicinski } 345502839cc8SJakub Kicinski err = -EPERM; 345602839cc8SJakub Kicinski if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN)) 345702839cc8SJakub Kicinski goto out; 345802839cc8SJakub Kicinski } else { 345902839cc8SJakub Kicinski link_net = NULL; 346002839cc8SJakub Kicinski } 346102839cc8SJakub Kicinski 346202839cc8SJakub Kicinski dev = rtnl_create_link(link_net ? : dest_net, ifname, 346302839cc8SJakub Kicinski name_assign_type, ops, tb, extack); 346402839cc8SJakub Kicinski if (IS_ERR(dev)) { 346502839cc8SJakub Kicinski err = PTR_ERR(dev); 346602839cc8SJakub Kicinski goto out; 346702839cc8SJakub Kicinski } 346802839cc8SJakub Kicinski 346902839cc8SJakub Kicinski dev->ifindex = ifm->ifi_index; 347002839cc8SJakub Kicinski 347102839cc8SJakub Kicinski if (ops->newlink) 347202839cc8SJakub Kicinski err = ops->newlink(link_net ? : net, dev, tb, data, extack); 347302839cc8SJakub Kicinski else 347402839cc8SJakub Kicinski err = register_netdevice(dev); 347502839cc8SJakub Kicinski if (err < 0) { 347602839cc8SJakub Kicinski free_netdev(dev); 347702839cc8SJakub Kicinski goto out; 347802839cc8SJakub Kicinski } 347902839cc8SJakub Kicinski 3480d88e136cSHangbin Liu err = rtnl_configure_link(dev, ifm, portid, nlh); 348102839cc8SJakub Kicinski if (err < 0) 348202839cc8SJakub Kicinski goto out_unregister; 348302839cc8SJakub Kicinski if (link_net) { 348402839cc8SJakub Kicinski err = dev_change_net_namespace(dev, dest_net, ifname); 348502839cc8SJakub Kicinski if (err < 0) 348602839cc8SJakub Kicinski goto out_unregister; 348702839cc8SJakub Kicinski } 348802839cc8SJakub Kicinski if (tb[IFLA_MASTER]) { 348902839cc8SJakub Kicinski err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack); 349002839cc8SJakub Kicinski if (err) 349102839cc8SJakub Kicinski goto out_unregister; 349202839cc8SJakub Kicinski } 349302839cc8SJakub Kicinski out: 349402839cc8SJakub Kicinski if (link_net) 349502839cc8SJakub Kicinski put_net(link_net); 349602839cc8SJakub Kicinski put_net(dest_net); 349702839cc8SJakub Kicinski return err; 349802839cc8SJakub Kicinski out_unregister: 349902839cc8SJakub Kicinski if (ops->newlink) { 350002839cc8SJakub Kicinski LIST_HEAD(list_kill); 350102839cc8SJakub Kicinski 350202839cc8SJakub Kicinski ops->dellink(dev, &list_kill); 350302839cc8SJakub Kicinski unregister_netdevice_many(&list_kill); 350402839cc8SJakub Kicinski } else { 350502839cc8SJakub Kicinski unregister_netdevice(dev); 350602839cc8SJakub Kicinski } 350702839cc8SJakub Kicinski goto out; 350802839cc8SJakub Kicinski } 350963105e83SJakub Kicinski 3510c92bf26cSJakub Kicinski struct rtnl_newlink_tbs { 3511c92bf26cSJakub Kicinski struct nlattr *tb[IFLA_MAX + 1]; 3512c92bf26cSJakub Kicinski struct nlattr *attr[RTNL_MAX_TYPE + 1]; 3513420d0318SJakub Kicinski struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1]; 3514c92bf26cSJakub Kicinski }; 3515c92bf26cSJakub Kicinski 3516c92bf26cSJakub Kicinski static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, 3517c92bf26cSJakub Kicinski struct rtnl_newlink_tbs *tbs, 3518c92bf26cSJakub Kicinski struct netlink_ext_ack *extack) 3519c92bf26cSJakub Kicinski { 3520420d0318SJakub Kicinski struct nlattr *linkinfo[IFLA_INFO_MAX + 1]; 3521c92bf26cSJakub Kicinski struct nlattr ** const tb = tbs->tb; 3522c6f6f244SEric Dumazet const struct rtnl_link_ops *m_ops; 3523c6f6f244SEric Dumazet struct net_device *master_dev; 35243b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 352538f7b870SPatrick McHardy const struct rtnl_link_ops *ops; 3526420d0318SJakub Kicinski struct nlattr **slave_data; 3527420d0318SJakub Kicinski char kind[MODULE_NAME_LEN]; 3528420d0318SJakub Kicinski struct net_device *dev; 3529420d0318SJakub Kicinski struct ifinfomsg *ifm; 3530420d0318SJakub Kicinski struct nlattr **data; 3531ef2a7c90SFlorent Fourcot bool link_specified; 353238f7b870SPatrick McHardy int err; 353338f7b870SPatrick McHardy 353495a5afcaSJohannes Berg #ifdef CONFIG_MODULES 353538f7b870SPatrick McHardy replay: 35368072f085SThomas Graf #endif 35378cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX, 35388cb08174SJohannes Berg ifla_policy, extack); 353938f7b870SPatrick McHardy if (err < 0) 354038f7b870SPatrick McHardy return err; 354138f7b870SPatrick McHardy 35424ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, false); 35434ff66caeSChristian Brauner if (err < 0) 35444ff66caeSChristian Brauner return err; 35454ff66caeSChristian Brauner 354638f7b870SPatrick McHardy ifm = nlmsg_data(nlh); 3547ef2a7c90SFlorent Fourcot if (ifm->ifi_index > 0) { 3548ef2a7c90SFlorent Fourcot link_specified = true; 3549881d966bSEric W. Biederman dev = __dev_get_by_index(net, ifm->ifi_index); 3550ef2a7c90SFlorent Fourcot } else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) { 3551ef2a7c90SFlorent Fourcot link_specified = true; 35525ea08b52SFlorent Fourcot dev = rtnl_dev_get(net, tb); 3553ef2a7c90SFlorent Fourcot } else { 3554ef2a7c90SFlorent Fourcot link_specified = false; 355538f7b870SPatrick McHardy dev = NULL; 3556ef2a7c90SFlorent Fourcot } 355738f7b870SPatrick McHardy 3558c6f6f244SEric Dumazet master_dev = NULL; 3559c6f6f244SEric Dumazet m_ops = NULL; 3560ba7d49b1SJiri Pirko if (dev) { 3561ba7d49b1SJiri Pirko master_dev = netdev_master_upper_dev_get(dev); 3562ba7d49b1SJiri Pirko if (master_dev) 3563ba7d49b1SJiri Pirko m_ops = master_dev->rtnl_link_ops; 3564ba7d49b1SJiri Pirko } 3565ba7d49b1SJiri Pirko 356638f7b870SPatrick McHardy if (tb[IFLA_LINKINFO]) { 35678cb08174SJohannes Berg err = nla_parse_nested_deprecated(linkinfo, IFLA_INFO_MAX, 35688cb08174SJohannes Berg tb[IFLA_LINKINFO], 35698cb08174SJohannes Berg ifla_info_policy, NULL); 357038f7b870SPatrick McHardy if (err < 0) 357138f7b870SPatrick McHardy return err; 357238f7b870SPatrick McHardy } else 357338f7b870SPatrick McHardy memset(linkinfo, 0, sizeof(linkinfo)); 357438f7b870SPatrick McHardy 357538f7b870SPatrick McHardy if (linkinfo[IFLA_INFO_KIND]) { 3576872f6903SFrancis Laniel nla_strscpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind)); 357738f7b870SPatrick McHardy ops = rtnl_link_ops_get(kind); 357838f7b870SPatrick McHardy } else { 357938f7b870SPatrick McHardy kind[0] = '\0'; 358038f7b870SPatrick McHardy ops = NULL; 358138f7b870SPatrick McHardy } 358238f7b870SPatrick McHardy 3583420d0318SJakub Kicinski data = NULL; 358438f7b870SPatrick McHardy if (ops) { 3585ccf8dbcdSKees Cook if (ops->maxtype > RTNL_MAX_TYPE) 3586ccf8dbcdSKees Cook return -EINVAL; 3587ccf8dbcdSKees Cook 358838f7b870SPatrick McHardy if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) { 3589c92bf26cSJakub Kicinski err = nla_parse_nested_deprecated(tbs->attr, ops->maxtype, 359038f7b870SPatrick McHardy linkinfo[IFLA_INFO_DATA], 359168d57f3bSDavid Ahern ops->policy, extack); 359238f7b870SPatrick McHardy if (err < 0) 359338f7b870SPatrick McHardy return err; 3594c92bf26cSJakub Kicinski data = tbs->attr; 359538f7b870SPatrick McHardy } 359638f7b870SPatrick McHardy if (ops->validate) { 3597a8b8a889SMatthias Schiffer err = ops->validate(tb, data, extack); 359838f7b870SPatrick McHardy if (err < 0) 359938f7b870SPatrick McHardy return err; 360038f7b870SPatrick McHardy } 360138f7b870SPatrick McHardy } 360238f7b870SPatrick McHardy 3603420d0318SJakub Kicinski slave_data = NULL; 3604ba7d49b1SJiri Pirko if (m_ops) { 3605af066ed3SPrashant Bhole if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE) 3606ccf8dbcdSKees Cook return -EINVAL; 3607ccf8dbcdSKees Cook 3608ba7d49b1SJiri Pirko if (m_ops->slave_maxtype && 3609ba7d49b1SJiri Pirko linkinfo[IFLA_INFO_SLAVE_DATA]) { 3610c92bf26cSJakub Kicinski err = nla_parse_nested_deprecated(tbs->slave_attr, 36118cb08174SJohannes Berg m_ops->slave_maxtype, 3612ba7d49b1SJiri Pirko linkinfo[IFLA_INFO_SLAVE_DATA], 36138cb08174SJohannes Berg m_ops->slave_policy, 36148cb08174SJohannes Berg extack); 3615ba7d49b1SJiri Pirko if (err < 0) 3616ba7d49b1SJiri Pirko return err; 3617c92bf26cSJakub Kicinski slave_data = tbs->slave_attr; 3618ba7d49b1SJiri Pirko } 3619ba7d49b1SJiri Pirko } 3620ba7d49b1SJiri Pirko 362138f7b870SPatrick McHardy if (dev) { 362290c325e3SNicolas Dichtel int status = 0; 362338f7b870SPatrick McHardy 362438f7b870SPatrick McHardy if (nlh->nlmsg_flags & NLM_F_EXCL) 362538f7b870SPatrick McHardy return -EEXIST; 362638f7b870SPatrick McHardy if (nlh->nlmsg_flags & NLM_F_REPLACE) 362738f7b870SPatrick McHardy return -EOPNOTSUPP; 362838f7b870SPatrick McHardy 3629*89da780aSXin Long err = validate_linkmsg(dev, tb, extack); 3630*89da780aSXin Long if (err < 0) 3631*89da780aSXin Long return err; 3632*89da780aSXin Long 363338f7b870SPatrick McHardy if (linkinfo[IFLA_INFO_DATA]) { 363438f7b870SPatrick McHardy if (!ops || ops != dev->rtnl_link_ops || 363538f7b870SPatrick McHardy !ops->changelink) 363638f7b870SPatrick McHardy return -EOPNOTSUPP; 363738f7b870SPatrick McHardy 3638ad744b22SMatthias Schiffer err = ops->changelink(dev, tb, data, extack); 363938f7b870SPatrick McHardy if (err < 0) 364038f7b870SPatrick McHardy return err; 3641ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 364238f7b870SPatrick McHardy } 364338f7b870SPatrick McHardy 3644ba7d49b1SJiri Pirko if (linkinfo[IFLA_INFO_SLAVE_DATA]) { 3645ba7d49b1SJiri Pirko if (!m_ops || !m_ops->slave_changelink) 3646ba7d49b1SJiri Pirko return -EOPNOTSUPP; 3647ba7d49b1SJiri Pirko 3648420d0318SJakub Kicinski err = m_ops->slave_changelink(master_dev, dev, tb, 3649420d0318SJakub Kicinski slave_data, extack); 3650ba7d49b1SJiri Pirko if (err < 0) 3651ba7d49b1SJiri Pirko return err; 3652ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 3653ba7d49b1SJiri Pirko } 3654ba7d49b1SJiri Pirko 36555ea08b52SFlorent Fourcot return do_setlink(skb, dev, ifm, extack, tb, status); 365638f7b870SPatrick McHardy } 365738f7b870SPatrick McHardy 3658ffa934f1SPatrick McHardy if (!(nlh->nlmsg_flags & NLM_F_CREATE)) { 3659ef2a7c90SFlorent Fourcot /* No dev found and NLM_F_CREATE not set. Requested dev does not exist, 3660ef2a7c90SFlorent Fourcot * or it's for a group 3661ef2a7c90SFlorent Fourcot */ 3662ef2a7c90SFlorent Fourcot if (link_specified) 3663ef2a7c90SFlorent Fourcot return -ENODEV; 3664ef2a7c90SFlorent Fourcot if (tb[IFLA_GROUP]) 366590f62cf3SEric W. Biederman return rtnl_group_changelink(skb, net, 3666ffa934f1SPatrick McHardy nla_get_u32(tb[IFLA_GROUP]), 3667ddf9f970SJakub Kicinski ifm, extack, tb); 36686f37c9f9SFlorent Fourcot return -ENODEV; 3669ffa934f1SPatrick McHardy } 367038f7b870SPatrick McHardy 3671160ca014STheuns Verwoerd if (tb[IFLA_MAP] || tb[IFLA_PROTINFO]) 367238f7b870SPatrick McHardy return -EOPNOTSUPP; 367338f7b870SPatrick McHardy 367438f7b870SPatrick McHardy if (!ops) { 367595a5afcaSJohannes Berg #ifdef CONFIG_MODULES 367638f7b870SPatrick McHardy if (kind[0]) { 367738f7b870SPatrick McHardy __rtnl_unlock(); 367838f7b870SPatrick McHardy request_module("rtnl-link-%s", kind); 367938f7b870SPatrick McHardy rtnl_lock(); 368038f7b870SPatrick McHardy ops = rtnl_link_ops_get(kind); 368138f7b870SPatrick McHardy if (ops) 368238f7b870SPatrick McHardy goto replay; 368338f7b870SPatrick McHardy } 368438f7b870SPatrick McHardy #endif 368568d57f3bSDavid Ahern NL_SET_ERR_MSG(extack, "Unknown device type"); 368638f7b870SPatrick McHardy return -EOPNOTSUPP; 368738f7b870SPatrick McHardy } 368838f7b870SPatrick McHardy 3689d88e136cSHangbin Liu return rtnl_newlink_create(skb, ifm, ops, nlh, tb, data, extack); 369063105e83SJakub Kicinski } 369163105e83SJakub Kicinski 3692a2939745SJakub Kicinski static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, 3693a2939745SJakub Kicinski struct netlink_ext_ack *extack) 3694a2939745SJakub Kicinski { 3695c92bf26cSJakub Kicinski struct rtnl_newlink_tbs *tbs; 3696a2939745SJakub Kicinski int ret; 3697a2939745SJakub Kicinski 3698c92bf26cSJakub Kicinski tbs = kmalloc(sizeof(*tbs), GFP_KERNEL); 3699c92bf26cSJakub Kicinski if (!tbs) 3700a2939745SJakub Kicinski return -ENOMEM; 3701a2939745SJakub Kicinski 3702c92bf26cSJakub Kicinski ret = __rtnl_newlink(skb, nlh, tbs, extack); 3703c92bf26cSJakub Kicinski kfree(tbs); 3704a2939745SJakub Kicinski return ret; 3705a2939745SJakub Kicinski } 3706a2939745SJakub Kicinski 37079b3757b0SJakub Kicinski static int rtnl_valid_getlink_req(struct sk_buff *skb, 37089b3757b0SJakub Kicinski const struct nlmsghdr *nlh, 37099b3757b0SJakub Kicinski struct nlattr **tb, 37109b3757b0SJakub Kicinski struct netlink_ext_ack *extack) 37119b3757b0SJakub Kicinski { 37129b3757b0SJakub Kicinski struct ifinfomsg *ifm; 37139b3757b0SJakub Kicinski int i, err; 37149b3757b0SJakub Kicinski 37159b3757b0SJakub Kicinski if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { 37169b3757b0SJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid header for get link"); 37179b3757b0SJakub Kicinski return -EINVAL; 37189b3757b0SJakub Kicinski } 37199b3757b0SJakub Kicinski 37209b3757b0SJakub Kicinski if (!netlink_strict_get_check(skb)) 37218cb08174SJohannes Berg return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX, 37228cb08174SJohannes Berg ifla_policy, extack); 37239b3757b0SJakub Kicinski 37249b3757b0SJakub Kicinski ifm = nlmsg_data(nlh); 37259b3757b0SJakub Kicinski if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags || 37269b3757b0SJakub Kicinski ifm->ifi_change) { 37279b3757b0SJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid values in header for get link request"); 37289b3757b0SJakub Kicinski return -EINVAL; 37299b3757b0SJakub Kicinski } 37309b3757b0SJakub Kicinski 37318cb08174SJohannes Berg err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFLA_MAX, 37328cb08174SJohannes Berg ifla_policy, extack); 37339b3757b0SJakub Kicinski if (err) 37349b3757b0SJakub Kicinski return err; 37359b3757b0SJakub Kicinski 37369b3757b0SJakub Kicinski for (i = 0; i <= IFLA_MAX; i++) { 37379b3757b0SJakub Kicinski if (!tb[i]) 37389b3757b0SJakub Kicinski continue; 37399b3757b0SJakub Kicinski 37409b3757b0SJakub Kicinski switch (i) { 37419b3757b0SJakub Kicinski case IFLA_IFNAME: 374276c9ac0eSJiri Pirko case IFLA_ALT_IFNAME: 37439b3757b0SJakub Kicinski case IFLA_EXT_MASK: 37449b3757b0SJakub Kicinski case IFLA_TARGET_NETNSID: 37459b3757b0SJakub Kicinski break; 37469b3757b0SJakub Kicinski default: 37479b3757b0SJakub Kicinski NL_SET_ERR_MSG(extack, "Unsupported attribute in get link request"); 37489b3757b0SJakub Kicinski return -EINVAL; 37499b3757b0SJakub Kicinski } 37509b3757b0SJakub Kicinski } 37519b3757b0SJakub Kicinski 37529b3757b0SJakub Kicinski return 0; 37539b3757b0SJakub Kicinski } 37549b3757b0SJakub Kicinski 3755c21ef3e3SDavid Ahern static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh, 3756c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 3757711e2c33SJean Tourrilhes { 37583b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 375979e1ad14SJiri Benc struct net *tgt_net = net; 3760b60c5115SThomas Graf struct ifinfomsg *ifm; 3761b60c5115SThomas Graf struct nlattr *tb[IFLA_MAX+1]; 3762b60c5115SThomas Graf struct net_device *dev = NULL; 3763b60c5115SThomas Graf struct sk_buff *nskb; 376479e1ad14SJiri Benc int netnsid = -1; 3765339bf98fSThomas Graf int err; 3766115c9b81SGreg Rose u32 ext_filter_mask = 0; 3767711e2c33SJean Tourrilhes 37689b3757b0SJakub Kicinski err = rtnl_valid_getlink_req(skb, nlh, tb, extack); 3769b60c5115SThomas Graf if (err < 0) 37709918f230SEric Sesterhenn return err; 3771b60c5115SThomas Graf 37724ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, true); 37734ff66caeSChristian Brauner if (err < 0) 37744ff66caeSChristian Brauner return err; 37754ff66caeSChristian Brauner 37767e4a8d5aSChristian Brauner if (tb[IFLA_TARGET_NETNSID]) { 37777e4a8d5aSChristian Brauner netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]); 3778c383edc4SChristian Brauner tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid); 377979e1ad14SJiri Benc if (IS_ERR(tgt_net)) 378079e1ad14SJiri Benc return PTR_ERR(tgt_net); 378179e1ad14SJiri Benc } 378279e1ad14SJiri Benc 3783115c9b81SGreg Rose if (tb[IFLA_EXT_MASK]) 3784115c9b81SGreg Rose ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 3785115c9b81SGreg Rose 378679e1ad14SJiri Benc err = -EINVAL; 3787b60c5115SThomas Graf ifm = nlmsg_data(nlh); 3788a3d12891SEric Dumazet if (ifm->ifi_index > 0) 378979e1ad14SJiri Benc dev = __dev_get_by_index(tgt_net, ifm->ifi_index); 379076c9ac0eSJiri Pirko else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) 37915ea08b52SFlorent Fourcot dev = rtnl_dev_get(tgt_net, tb); 3792a3d12891SEric Dumazet else 379379e1ad14SJiri Benc goto out; 3794b60c5115SThomas Graf 379579e1ad14SJiri Benc err = -ENODEV; 3796a3d12891SEric Dumazet if (dev == NULL) 379779e1ad14SJiri Benc goto out; 3798a3d12891SEric Dumazet 379979e1ad14SJiri Benc err = -ENOBUFS; 3800115c9b81SGreg Rose nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL); 3801a3d12891SEric Dumazet if (nskb == NULL) 380279e1ad14SJiri Benc goto out; 3803711e2c33SJean Tourrilhes 380479e1ad14SJiri Benc err = rtnl_fill_ifinfo(nskb, dev, net, 380579e1ad14SJiri Benc RTM_NEWLINK, NETLINK_CB(skb).portid, 380679e1ad14SJiri Benc nlh->nlmsg_seq, 0, 0, ext_filter_mask, 3807d4e4fdf9SGuillaume Nault 0, NULL, 0, netnsid, GFP_KERNEL); 380826932566SPatrick McHardy if (err < 0) { 380926932566SPatrick McHardy /* -EMSGSIZE implies BUG in if_nlmsg_size */ 381026932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 381126932566SPatrick McHardy kfree_skb(nskb); 3812a3d12891SEric Dumazet } else 381315e47304SEric W. Biederman err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); 381479e1ad14SJiri Benc out: 381579e1ad14SJiri Benc if (netnsid >= 0) 381679e1ad14SJiri Benc put_net(tgt_net); 3817b60c5115SThomas Graf 3818711e2c33SJean Tourrilhes return err; 3819711e2c33SJean Tourrilhes } 3820711e2c33SJean Tourrilhes 382136fbf1e5SJiri Pirko static int rtnl_alt_ifname(int cmd, struct net_device *dev, struct nlattr *attr, 382236fbf1e5SJiri Pirko bool *changed, struct netlink_ext_ack *extack) 382336fbf1e5SJiri Pirko { 382436fbf1e5SJiri Pirko char *alt_ifname; 3825155fb43bSJakub Kicinski size_t size; 382636fbf1e5SJiri Pirko int err; 382736fbf1e5SJiri Pirko 382836fbf1e5SJiri Pirko err = nla_validate(attr, attr->nla_len, IFLA_MAX, ifla_policy, extack); 382936fbf1e5SJiri Pirko if (err) 383036fbf1e5SJiri Pirko return err; 383136fbf1e5SJiri Pirko 3832155fb43bSJakub Kicinski if (cmd == RTM_NEWLINKPROP) { 3833155fb43bSJakub Kicinski size = rtnl_prop_list_size(dev); 3834155fb43bSJakub Kicinski size += nla_total_size(ALTIFNAMSIZ); 3835155fb43bSJakub Kicinski if (size >= U16_MAX) { 3836155fb43bSJakub Kicinski NL_SET_ERR_MSG(extack, 3837155fb43bSJakub Kicinski "effective property list too long"); 3838155fb43bSJakub Kicinski return -EINVAL; 3839155fb43bSJakub Kicinski } 3840155fb43bSJakub Kicinski } 3841155fb43bSJakub Kicinski 38425d26cff5SJakub Kicinski alt_ifname = nla_strdup(attr, GFP_KERNEL_ACCOUNT); 384336fbf1e5SJiri Pirko if (!alt_ifname) 384436fbf1e5SJiri Pirko return -ENOMEM; 384544bfa9c5SEric Dumazet 384644bfa9c5SEric Dumazet if (cmd == RTM_NEWLINKPROP) { 384736fbf1e5SJiri Pirko err = netdev_name_node_alt_create(dev, alt_ifname); 384844bfa9c5SEric Dumazet if (!err) 384944bfa9c5SEric Dumazet alt_ifname = NULL; 385036fbf1e5SJiri Pirko } else if (cmd == RTM_DELLINKPROP) { 385136fbf1e5SJiri Pirko err = netdev_name_node_alt_destroy(dev, alt_ifname); 385236fbf1e5SJiri Pirko } else { 385344bfa9c5SEric Dumazet WARN_ON_ONCE(1); 385444bfa9c5SEric Dumazet err = -EINVAL; 385536fbf1e5SJiri Pirko } 385636fbf1e5SJiri Pirko 385744bfa9c5SEric Dumazet kfree(alt_ifname); 385844bfa9c5SEric Dumazet if (!err) 385936fbf1e5SJiri Pirko *changed = true; 386044bfa9c5SEric Dumazet return err; 386136fbf1e5SJiri Pirko } 386236fbf1e5SJiri Pirko 386336fbf1e5SJiri Pirko static int rtnl_linkprop(int cmd, struct sk_buff *skb, struct nlmsghdr *nlh, 386436fbf1e5SJiri Pirko struct netlink_ext_ack *extack) 386536fbf1e5SJiri Pirko { 386636fbf1e5SJiri Pirko struct net *net = sock_net(skb->sk); 386736fbf1e5SJiri Pirko struct nlattr *tb[IFLA_MAX + 1]; 386836fbf1e5SJiri Pirko struct net_device *dev; 386936fbf1e5SJiri Pirko struct ifinfomsg *ifm; 387036fbf1e5SJiri Pirko bool changed = false; 387136fbf1e5SJiri Pirko struct nlattr *attr; 387236fbf1e5SJiri Pirko int err, rem; 387336fbf1e5SJiri Pirko 387436fbf1e5SJiri Pirko err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack); 387536fbf1e5SJiri Pirko if (err) 387636fbf1e5SJiri Pirko return err; 387736fbf1e5SJiri Pirko 387836fbf1e5SJiri Pirko err = rtnl_ensure_unique_netns(tb, extack, true); 387936fbf1e5SJiri Pirko if (err) 388036fbf1e5SJiri Pirko return err; 388136fbf1e5SJiri Pirko 388236fbf1e5SJiri Pirko ifm = nlmsg_data(nlh); 3883cc6090e9SJiri Pirko if (ifm->ifi_index > 0) 388436fbf1e5SJiri Pirko dev = __dev_get_by_index(net, ifm->ifi_index); 388576c9ac0eSJiri Pirko else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) 38865ea08b52SFlorent Fourcot dev = rtnl_dev_get(net, tb); 3887cc6090e9SJiri Pirko else 388836fbf1e5SJiri Pirko return -EINVAL; 388936fbf1e5SJiri Pirko 389036fbf1e5SJiri Pirko if (!dev) 389136fbf1e5SJiri Pirko return -ENODEV; 389236fbf1e5SJiri Pirko 389336fbf1e5SJiri Pirko if (!tb[IFLA_PROP_LIST]) 389436fbf1e5SJiri Pirko return 0; 389536fbf1e5SJiri Pirko 389636fbf1e5SJiri Pirko nla_for_each_nested(attr, tb[IFLA_PROP_LIST], rem) { 389736fbf1e5SJiri Pirko switch (nla_type(attr)) { 389836fbf1e5SJiri Pirko case IFLA_ALT_IFNAME: 389936fbf1e5SJiri Pirko err = rtnl_alt_ifname(cmd, dev, attr, &changed, extack); 390036fbf1e5SJiri Pirko if (err) 390136fbf1e5SJiri Pirko return err; 390236fbf1e5SJiri Pirko break; 390336fbf1e5SJiri Pirko } 390436fbf1e5SJiri Pirko } 390536fbf1e5SJiri Pirko 390636fbf1e5SJiri Pirko if (changed) 390736fbf1e5SJiri Pirko netdev_state_change(dev); 390836fbf1e5SJiri Pirko return 0; 390936fbf1e5SJiri Pirko } 391036fbf1e5SJiri Pirko 391136fbf1e5SJiri Pirko static int rtnl_newlinkprop(struct sk_buff *skb, struct nlmsghdr *nlh, 391236fbf1e5SJiri Pirko struct netlink_ext_ack *extack) 391336fbf1e5SJiri Pirko { 391436fbf1e5SJiri Pirko return rtnl_linkprop(RTM_NEWLINKPROP, skb, nlh, extack); 391536fbf1e5SJiri Pirko } 391636fbf1e5SJiri Pirko 391736fbf1e5SJiri Pirko static int rtnl_dellinkprop(struct sk_buff *skb, struct nlmsghdr *nlh, 391836fbf1e5SJiri Pirko struct netlink_ext_ack *extack) 391936fbf1e5SJiri Pirko { 392036fbf1e5SJiri Pirko return rtnl_linkprop(RTM_DELLINKPROP, skb, nlh, extack); 392136fbf1e5SJiri Pirko } 392236fbf1e5SJiri Pirko 3923ebfe3c51SDi Zhu static u32 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh) 3924c7ac8679SGreg Rose { 3925115c9b81SGreg Rose struct net *net = sock_net(skb->sk); 3926ebfe3c51SDi Zhu size_t min_ifinfo_dump_size = 0; 3927115c9b81SGreg Rose struct nlattr *tb[IFLA_MAX+1]; 3928115c9b81SGreg Rose u32 ext_filter_mask = 0; 3929ebfe3c51SDi Zhu struct net_device *dev; 3930e5eca6d4SMichal Schmidt int hdrlen; 3931115c9b81SGreg Rose 3932e5eca6d4SMichal Schmidt /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */ 3933e5eca6d4SMichal Schmidt hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ? 3934e5eca6d4SMichal Schmidt sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); 3935e5eca6d4SMichal Schmidt 39368cb08174SJohannes Berg if (nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) { 3937115c9b81SGreg Rose if (tb[IFLA_EXT_MASK]) 3938115c9b81SGreg Rose ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 3939a4b64fbeSEric Dumazet } 3940115c9b81SGreg Rose 3941115c9b81SGreg Rose if (!ext_filter_mask) 3942115c9b81SGreg Rose return NLMSG_GOODSIZE; 3943115c9b81SGreg Rose /* 3944115c9b81SGreg Rose * traverse the list of net devices and compute the minimum 3945115c9b81SGreg Rose * buffer size based upon the filter mask. 3946115c9b81SGreg Rose */ 39476853dd48SFlorian Westphal rcu_read_lock(); 39486853dd48SFlorian Westphal for_each_netdev_rcu(net, dev) { 3949ebfe3c51SDi Zhu min_ifinfo_dump_size = max(min_ifinfo_dump_size, 3950ebfe3c51SDi Zhu if_nlmsg_size(dev, ext_filter_mask)); 3951115c9b81SGreg Rose } 39526853dd48SFlorian Westphal rcu_read_unlock(); 3953115c9b81SGreg Rose 395493af2056SZhang Shengju return nlmsg_total_size(min_ifinfo_dump_size); 3955c7ac8679SGreg Rose } 3956c7ac8679SGreg Rose 395742bad1daSAdrian Bunk static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb) 39581da177e4SLinus Torvalds { 39591da177e4SLinus Torvalds int idx; 39601da177e4SLinus Torvalds int s_idx = cb->family; 396187ccbb1fSChristian Brauner int type = cb->nlh->nlmsg_type - RTM_BASE; 3962c63586dcSDavid Ahern int ret = 0; 39631da177e4SLinus Torvalds 39641da177e4SLinus Torvalds if (s_idx == 0) 39651da177e4SLinus Torvalds s_idx = 1; 39666853dd48SFlorian Westphal 396725239ceeSPatrick McHardy for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) { 396851e13685SJakub Kicinski struct rtnl_link __rcu **tab; 3969addf9b90SFlorian Westphal struct rtnl_link *link; 39706853dd48SFlorian Westphal rtnl_dumpit_func dumpit; 39716853dd48SFlorian Westphal 39721da177e4SLinus Torvalds if (idx < s_idx || idx == PF_PACKET) 39731da177e4SLinus Torvalds continue; 39746853dd48SFlorian Westphal 3975addf9b90SFlorian Westphal if (type < 0 || type >= RTM_NR_MSGTYPES) 39761da177e4SLinus Torvalds continue; 39776853dd48SFlorian Westphal 3978addf9b90SFlorian Westphal tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]); 3979addf9b90SFlorian Westphal if (!tab) 3980addf9b90SFlorian Westphal continue; 3981addf9b90SFlorian Westphal 398251e13685SJakub Kicinski link = rcu_dereference_rtnl(tab[type]); 3983addf9b90SFlorian Westphal if (!link) 3984addf9b90SFlorian Westphal continue; 3985addf9b90SFlorian Westphal 3986addf9b90SFlorian Westphal dumpit = link->dumpit; 39876853dd48SFlorian Westphal if (!dumpit) 39886853dd48SFlorian Westphal continue; 39896853dd48SFlorian Westphal 39900465277fSNicolas Dichtel if (idx > s_idx) { 39911da177e4SLinus Torvalds memset(&cb->args[0], 0, sizeof(cb->args)); 39920465277fSNicolas Dichtel cb->prev_seq = 0; 39930465277fSNicolas Dichtel cb->seq = 0; 39940465277fSNicolas Dichtel } 3995c63586dcSDavid Ahern ret = dumpit(skb, cb); 39965e1acb4aSAlexey Kodanev if (ret) 39971da177e4SLinus Torvalds break; 39981da177e4SLinus Torvalds } 39991da177e4SLinus Torvalds cb->family = idx; 40001da177e4SLinus Torvalds 4001c63586dcSDavid Ahern return skb->len ? : ret; 40021da177e4SLinus Torvalds } 40031da177e4SLinus Torvalds 4004395eea6cSMahesh Bandewar struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev, 40053d3ea5afSVlad Yasevich unsigned int change, 400638e01b30SNicolas Dichtel u32 event, gfp_t flags, int *new_nsid, 400759d3efd2SMartin Willi int new_ifindex, u32 portid, 400859d3efd2SMartin Willi const struct nlmsghdr *nlh) 40091da177e4SLinus Torvalds { 4010c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 40111da177e4SLinus Torvalds struct sk_buff *skb; 40120ec6d3f4SThomas Graf int err = -ENOBUFS; 401359d3efd2SMartin Willi u32 seq = 0; 40141da177e4SLinus Torvalds 401550af5969Sluo penghao skb = nlmsg_new(if_nlmsg_size(dev, 0), flags); 40160ec6d3f4SThomas Graf if (skb == NULL) 40170ec6d3f4SThomas Graf goto errout; 40181da177e4SLinus Torvalds 401959d3efd2SMartin Willi if (nlmsg_report(nlh)) 402059d3efd2SMartin Willi seq = nlmsg_seq(nlh); 402159d3efd2SMartin Willi else 402259d3efd2SMartin Willi portid = 0; 402359d3efd2SMartin Willi 402479e1ad14SJiri Benc err = rtnl_fill_ifinfo(skb, dev, dev_net(dev), 40251d997f10SHangbin Liu type, portid, seq, change, 0, 0, event, 4026d4e4fdf9SGuillaume Nault new_nsid, new_ifindex, -1, flags); 402726932566SPatrick McHardy if (err < 0) { 402826932566SPatrick McHardy /* -EMSGSIZE implies BUG in if_nlmsg_size() */ 402926932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 403026932566SPatrick McHardy kfree_skb(skb); 403126932566SPatrick McHardy goto errout; 403226932566SPatrick McHardy } 4033395eea6cSMahesh Bandewar return skb; 40340ec6d3f4SThomas Graf errout: 40350ec6d3f4SThomas Graf if (err < 0) 40364b3da706SEric W. Biederman rtnl_set_sk_err(net, RTNLGRP_LINK, err); 4037395eea6cSMahesh Bandewar return NULL; 4038395eea6cSMahesh Bandewar } 4039395eea6cSMahesh Bandewar 40401d997f10SHangbin Liu void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags, 40411d997f10SHangbin Liu u32 portid, const struct nlmsghdr *nlh) 4042395eea6cSMahesh Bandewar { 4043395eea6cSMahesh Bandewar struct net *net = dev_net(dev); 4044395eea6cSMahesh Bandewar 40451d997f10SHangbin Liu rtnl_notify(skb, net, portid, RTNLGRP_LINK, nlh, flags); 4046395eea6cSMahesh Bandewar } 4047395eea6cSMahesh Bandewar 40483d3ea5afSVlad Yasevich static void rtmsg_ifinfo_event(int type, struct net_device *dev, 40493d3ea5afSVlad Yasevich unsigned int change, u32 event, 40501d997f10SHangbin Liu gfp_t flags, int *new_nsid, int new_ifindex, 40511d997f10SHangbin Liu u32 portid, const struct nlmsghdr *nlh) 4052395eea6cSMahesh Bandewar { 4053395eea6cSMahesh Bandewar struct sk_buff *skb; 4054395eea6cSMahesh Bandewar 4055ed2a80abSNicolas Dichtel if (dev->reg_state != NETREG_REGISTERED) 4056ed2a80abSNicolas Dichtel return; 4057ed2a80abSNicolas Dichtel 405838e01b30SNicolas Dichtel skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid, 405959d3efd2SMartin Willi new_ifindex, portid, nlh); 4060395eea6cSMahesh Bandewar if (skb) 40611d997f10SHangbin Liu rtmsg_ifinfo_send(skb, dev, flags, portid, nlh); 40621da177e4SLinus Torvalds } 40633d3ea5afSVlad Yasevich 40643d3ea5afSVlad Yasevich void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change, 40651d997f10SHangbin Liu gfp_t flags, u32 portid, const struct nlmsghdr *nlh) 40663d3ea5afSVlad Yasevich { 406738e01b30SNicolas Dichtel rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags, 40681d997f10SHangbin Liu NULL, 0, portid, nlh); 40693d3ea5afSVlad Yasevich } 40701da177e4SLinus Torvalds 40716621dd29SNicolas Dichtel void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change, 407238e01b30SNicolas Dichtel gfp_t flags, int *new_nsid, int new_ifindex) 40736621dd29SNicolas Dichtel { 40746621dd29SNicolas Dichtel rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags, 40751d997f10SHangbin Liu new_nsid, new_ifindex, 0, NULL); 40766621dd29SNicolas Dichtel } 40776621dd29SNicolas Dichtel 4078d83b0603SJohn Fastabend static int nlmsg_populate_fdb_fill(struct sk_buff *skb, 4079d83b0603SJohn Fastabend struct net_device *dev, 40801e53d5bbSHubert Sokolowski u8 *addr, u16 vid, u32 pid, u32 seq, 40811c104a6bSNicolas Dichtel int type, unsigned int flags, 4082b3379041SHubert Sokolowski int nlflags, u16 ndm_state) 4083d83b0603SJohn Fastabend { 4084d83b0603SJohn Fastabend struct nlmsghdr *nlh; 4085d83b0603SJohn Fastabend struct ndmsg *ndm; 4086d83b0603SJohn Fastabend 40871c104a6bSNicolas Dichtel nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags); 4088d83b0603SJohn Fastabend if (!nlh) 4089d83b0603SJohn Fastabend return -EMSGSIZE; 4090d83b0603SJohn Fastabend 4091d83b0603SJohn Fastabend ndm = nlmsg_data(nlh); 4092d83b0603SJohn Fastabend ndm->ndm_family = AF_BRIDGE; 4093d83b0603SJohn Fastabend ndm->ndm_pad1 = 0; 4094d83b0603SJohn Fastabend ndm->ndm_pad2 = 0; 4095d83b0603SJohn Fastabend ndm->ndm_flags = flags; 4096d83b0603SJohn Fastabend ndm->ndm_type = 0; 4097d83b0603SJohn Fastabend ndm->ndm_ifindex = dev->ifindex; 4098b3379041SHubert Sokolowski ndm->ndm_state = ndm_state; 4099d83b0603SJohn Fastabend 4100d83b0603SJohn Fastabend if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr)) 4101d83b0603SJohn Fastabend goto nla_put_failure; 41021e53d5bbSHubert Sokolowski if (vid) 41031e53d5bbSHubert Sokolowski if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid)) 41041e53d5bbSHubert Sokolowski goto nla_put_failure; 4105d83b0603SJohn Fastabend 4106053c095aSJohannes Berg nlmsg_end(skb, nlh); 4107053c095aSJohannes Berg return 0; 4108d83b0603SJohn Fastabend 4109d83b0603SJohn Fastabend nla_put_failure: 4110d83b0603SJohn Fastabend nlmsg_cancel(skb, nlh); 4111d83b0603SJohn Fastabend return -EMSGSIZE; 4112d83b0603SJohn Fastabend } 4113d83b0603SJohn Fastabend 41143ff661c3SJohn Fastabend static inline size_t rtnl_fdb_nlmsg_size(void) 41153ff661c3SJohn Fastabend { 4116f82ef3e1SSabrina Dubroca return NLMSG_ALIGN(sizeof(struct ndmsg)) + 4117f82ef3e1SSabrina Dubroca nla_total_size(ETH_ALEN) + /* NDA_LLADDR */ 4118f82ef3e1SSabrina Dubroca nla_total_size(sizeof(u16)) + /* NDA_VLAN */ 4119f82ef3e1SSabrina Dubroca 0; 41203ff661c3SJohn Fastabend } 41213ff661c3SJohn Fastabend 4122b3379041SHubert Sokolowski static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type, 4123b3379041SHubert Sokolowski u16 ndm_state) 41243ff661c3SJohn Fastabend { 41253ff661c3SJohn Fastabend struct net *net = dev_net(dev); 41263ff661c3SJohn Fastabend struct sk_buff *skb; 41273ff661c3SJohn Fastabend int err = -ENOBUFS; 41283ff661c3SJohn Fastabend 41293ff661c3SJohn Fastabend skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC); 41303ff661c3SJohn Fastabend if (!skb) 41313ff661c3SJohn Fastabend goto errout; 41323ff661c3SJohn Fastabend 41331e53d5bbSHubert Sokolowski err = nlmsg_populate_fdb_fill(skb, dev, addr, vid, 4134b3379041SHubert Sokolowski 0, 0, type, NTF_SELF, 0, ndm_state); 41353ff661c3SJohn Fastabend if (err < 0) { 41363ff661c3SJohn Fastabend kfree_skb(skb); 41373ff661c3SJohn Fastabend goto errout; 41383ff661c3SJohn Fastabend } 41393ff661c3SJohn Fastabend 41403ff661c3SJohn Fastabend rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); 41413ff661c3SJohn Fastabend return; 41423ff661c3SJohn Fastabend errout: 41433ff661c3SJohn Fastabend rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); 41443ff661c3SJohn Fastabend } 41453ff661c3SJohn Fastabend 4146a986967eSBart Van Assche /* 4147090096bfSVlad Yasevich * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry 4148090096bfSVlad Yasevich */ 4149090096bfSVlad Yasevich int ndo_dflt_fdb_add(struct ndmsg *ndm, 4150090096bfSVlad Yasevich struct nlattr *tb[], 4151090096bfSVlad Yasevich struct net_device *dev, 4152f6f6424bSJiri Pirko const unsigned char *addr, u16 vid, 4153090096bfSVlad Yasevich u16 flags) 4154090096bfSVlad Yasevich { 4155090096bfSVlad Yasevich int err = -EINVAL; 4156090096bfSVlad Yasevich 4157090096bfSVlad Yasevich /* If aging addresses are supported device will need to 4158090096bfSVlad Yasevich * implement its own handler for this. 4159090096bfSVlad Yasevich */ 4160090096bfSVlad Yasevich if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) { 416178ecc890SVladimir Oltean netdev_info(dev, "default FDB implementation only supports local addresses\n"); 4162090096bfSVlad Yasevich return err; 4163090096bfSVlad Yasevich } 4164090096bfSVlad Yasevich 4165a35ec8e3SHans J. Schultz if (tb[NDA_FLAGS_EXT]) { 4166a35ec8e3SHans J. Schultz netdev_info(dev, "invalid flags given to default FDB implementation\n"); 4167a35ec8e3SHans J. Schultz return err; 4168a35ec8e3SHans J. Schultz } 4169a35ec8e3SHans J. Schultz 417065891feaSOr Gerlitz if (vid) { 417123ac0b42SVladimir Oltean netdev_info(dev, "vlans aren't supported yet for dev_uc|mc_add()\n"); 417265891feaSOr Gerlitz return err; 417365891feaSOr Gerlitz } 417465891feaSOr Gerlitz 4175090096bfSVlad Yasevich if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) 4176090096bfSVlad Yasevich err = dev_uc_add_excl(dev, addr); 4177090096bfSVlad Yasevich else if (is_multicast_ether_addr(addr)) 4178090096bfSVlad Yasevich err = dev_mc_add_excl(dev, addr); 4179090096bfSVlad Yasevich 4180090096bfSVlad Yasevich /* Only return duplicate errors if NLM_F_EXCL is set */ 4181090096bfSVlad Yasevich if (err == -EEXIST && !(flags & NLM_F_EXCL)) 4182090096bfSVlad Yasevich err = 0; 4183090096bfSVlad Yasevich 4184090096bfSVlad Yasevich return err; 4185090096bfSVlad Yasevich } 4186090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_add); 4187090096bfSVlad Yasevich 4188b88d12e4SFlorian Westphal static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid, 4189b88d12e4SFlorian Westphal struct netlink_ext_ack *extack) 4190f6f6424bSJiri Pirko { 4191f6f6424bSJiri Pirko u16 vid = 0; 4192f6f6424bSJiri Pirko 4193f6f6424bSJiri Pirko if (vlan_attr) { 4194f6f6424bSJiri Pirko if (nla_len(vlan_attr) != sizeof(u16)) { 4195b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid vlan attribute size"); 4196f6f6424bSJiri Pirko return -EINVAL; 4197f6f6424bSJiri Pirko } 4198f6f6424bSJiri Pirko 4199f6f6424bSJiri Pirko vid = nla_get_u16(vlan_attr); 4200f6f6424bSJiri Pirko 4201f6f6424bSJiri Pirko if (!vid || vid >= VLAN_VID_MASK) { 4202b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid vlan id"); 4203f6f6424bSJiri Pirko return -EINVAL; 4204f6f6424bSJiri Pirko } 4205f6f6424bSJiri Pirko } 4206f6f6424bSJiri Pirko *p_vid = vid; 4207f6f6424bSJiri Pirko return 0; 4208f6f6424bSJiri Pirko } 4209f6f6424bSJiri Pirko 4210c21ef3e3SDavid Ahern static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh, 4211c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 421277162022SJohn Fastabend { 421377162022SJohn Fastabend struct net *net = sock_net(skb->sk); 421477162022SJohn Fastabend struct ndmsg *ndm; 421577162022SJohn Fastabend struct nlattr *tb[NDA_MAX+1]; 421677162022SJohn Fastabend struct net_device *dev; 421777162022SJohn Fastabend u8 *addr; 4218f6f6424bSJiri Pirko u16 vid; 421977162022SJohn Fastabend int err; 422077162022SJohn Fastabend 42218cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, 42228cb08174SJohannes Berg extack); 422377162022SJohn Fastabend if (err < 0) 422477162022SJohn Fastabend return err; 422577162022SJohn Fastabend 422677162022SJohn Fastabend ndm = nlmsg_data(nlh); 422777162022SJohn Fastabend if (ndm->ndm_ifindex == 0) { 4228b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid ifindex"); 422977162022SJohn Fastabend return -EINVAL; 423077162022SJohn Fastabend } 423177162022SJohn Fastabend 423277162022SJohn Fastabend dev = __dev_get_by_index(net, ndm->ndm_ifindex); 423377162022SJohn Fastabend if (dev == NULL) { 4234b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 423577162022SJohn Fastabend return -ENODEV; 423677162022SJohn Fastabend } 423777162022SJohn Fastabend 423877162022SJohn Fastabend if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { 4239b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid address"); 424077162022SJohn Fastabend return -EINVAL; 424177162022SJohn Fastabend } 424277162022SJohn Fastabend 4243da715775SIdo Schimmel if (dev->type != ARPHRD_ETHER) { 4244da715775SIdo Schimmel NL_SET_ERR_MSG(extack, "FDB add only supported for Ethernet devices"); 4245da715775SIdo Schimmel return -EINVAL; 4246da715775SIdo Schimmel } 4247da715775SIdo Schimmel 424877162022SJohn Fastabend addr = nla_data(tb[NDA_LLADDR]); 424977162022SJohn Fastabend 4250b88d12e4SFlorian Westphal err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack); 4251f6f6424bSJiri Pirko if (err) 4252f6f6424bSJiri Pirko return err; 4253f6f6424bSJiri Pirko 425477162022SJohn Fastabend err = -EOPNOTSUPP; 425577162022SJohn Fastabend 425677162022SJohn Fastabend /* Support fdb on master device the net/bridge default case */ 425777162022SJohn Fastabend if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && 42582e92a2d0SJulian Wiedmann netif_is_bridge_port(dev)) { 4259898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4260898e5061SJiri Pirko const struct net_device_ops *ops = br_dev->netdev_ops; 4261898e5061SJiri Pirko 4262f6f6424bSJiri Pirko err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid, 426387b0984eSPetr Machata nlh->nlmsg_flags, extack); 426477162022SJohn Fastabend if (err) 426577162022SJohn Fastabend goto out; 426677162022SJohn Fastabend else 426777162022SJohn Fastabend ndm->ndm_flags &= ~NTF_MASTER; 426877162022SJohn Fastabend } 426977162022SJohn Fastabend 427077162022SJohn Fastabend /* Embedded bridge, macvlan, and any other device support */ 4271090096bfSVlad Yasevich if ((ndm->ndm_flags & NTF_SELF)) { 4272090096bfSVlad Yasevich if (dev->netdev_ops->ndo_fdb_add) 4273090096bfSVlad Yasevich err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr, 4274f6f6424bSJiri Pirko vid, 427587b0984eSPetr Machata nlh->nlmsg_flags, 427687b0984eSPetr Machata extack); 4277090096bfSVlad Yasevich else 4278f6f6424bSJiri Pirko err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid, 427977162022SJohn Fastabend nlh->nlmsg_flags); 428077162022SJohn Fastabend 42813ff661c3SJohn Fastabend if (!err) { 4282b3379041SHubert Sokolowski rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH, 4283b3379041SHubert Sokolowski ndm->ndm_state); 428477162022SJohn Fastabend ndm->ndm_flags &= ~NTF_SELF; 428577162022SJohn Fastabend } 42863ff661c3SJohn Fastabend } 428777162022SJohn Fastabend out: 428877162022SJohn Fastabend return err; 428977162022SJohn Fastabend } 429077162022SJohn Fastabend 4291a986967eSBart Van Assche /* 4292090096bfSVlad Yasevich * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry 4293090096bfSVlad Yasevich */ 4294090096bfSVlad Yasevich int ndo_dflt_fdb_del(struct ndmsg *ndm, 4295090096bfSVlad Yasevich struct nlattr *tb[], 4296090096bfSVlad Yasevich struct net_device *dev, 4297f6f6424bSJiri Pirko const unsigned char *addr, u16 vid) 4298090096bfSVlad Yasevich { 4299c8a89c4aSAlexander Duyck int err = -EINVAL; 4300090096bfSVlad Yasevich 4301090096bfSVlad Yasevich /* If aging addresses are supported device will need to 4302090096bfSVlad Yasevich * implement its own handler for this. 4303090096bfSVlad Yasevich */ 430464535993SSridhar Samudrala if (!(ndm->ndm_state & NUD_PERMANENT)) { 430578ecc890SVladimir Oltean netdev_info(dev, "default FDB implementation only supports local addresses\n"); 4306c8a89c4aSAlexander Duyck return err; 4307090096bfSVlad Yasevich } 4308090096bfSVlad Yasevich 4309090096bfSVlad Yasevich if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) 4310090096bfSVlad Yasevich err = dev_uc_del(dev, addr); 4311090096bfSVlad Yasevich else if (is_multicast_ether_addr(addr)) 4312090096bfSVlad Yasevich err = dev_mc_del(dev, addr); 4313090096bfSVlad Yasevich 4314090096bfSVlad Yasevich return err; 4315090096bfSVlad Yasevich } 4316090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_del); 4317090096bfSVlad Yasevich 43189e834259SNikolay Aleksandrov static const struct nla_policy fdb_del_bulk_policy[NDA_MAX + 1] = { 43199e834259SNikolay Aleksandrov [NDA_VLAN] = { .type = NLA_U16 }, 43209e834259SNikolay Aleksandrov [NDA_IFINDEX] = NLA_POLICY_MIN(NLA_S32, 1), 4321ea2c0f9eSNikolay Aleksandrov [NDA_NDM_STATE_MASK] = { .type = NLA_U16 }, 4322ea2c0f9eSNikolay Aleksandrov [NDA_NDM_FLAGS_MASK] = { .type = NLA_U8 }, 43239e834259SNikolay Aleksandrov }; 43249e834259SNikolay Aleksandrov 4325c21ef3e3SDavid Ahern static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh, 4326c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 432777162022SJohn Fastabend { 43289e834259SNikolay Aleksandrov bool del_bulk = !!(nlh->nlmsg_flags & NLM_F_BULK); 432977162022SJohn Fastabend struct net *net = sock_net(skb->sk); 43309e834259SNikolay Aleksandrov const struct net_device_ops *ops; 433177162022SJohn Fastabend struct ndmsg *ndm; 43321690be63SVlad Yasevich struct nlattr *tb[NDA_MAX+1]; 433377162022SJohn Fastabend struct net_device *dev; 43349e834259SNikolay Aleksandrov __u8 *addr = NULL; 43357d311801SColin Ian King int err; 4336f6f6424bSJiri Pirko u16 vid; 433777162022SJohn Fastabend 433890f62cf3SEric W. Biederman if (!netlink_capable(skb, CAP_NET_ADMIN)) 43391690be63SVlad Yasevich return -EPERM; 43401690be63SVlad Yasevich 43419e834259SNikolay Aleksandrov if (!del_bulk) { 43429e834259SNikolay Aleksandrov err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, 43439e834259SNikolay Aleksandrov NULL, extack); 43449e834259SNikolay Aleksandrov } else { 43459e834259SNikolay Aleksandrov err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, 43469e834259SNikolay Aleksandrov fdb_del_bulk_policy, extack); 43479e834259SNikolay Aleksandrov } 43481690be63SVlad Yasevich if (err < 0) 43491690be63SVlad Yasevich return err; 435077162022SJohn Fastabend 435177162022SJohn Fastabend ndm = nlmsg_data(nlh); 435277162022SJohn Fastabend if (ndm->ndm_ifindex == 0) { 4353b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid ifindex"); 435477162022SJohn Fastabend return -EINVAL; 435577162022SJohn Fastabend } 435677162022SJohn Fastabend 435777162022SJohn Fastabend dev = __dev_get_by_index(net, ndm->ndm_ifindex); 435877162022SJohn Fastabend if (dev == NULL) { 4359b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 436077162022SJohn Fastabend return -ENODEV; 436177162022SJohn Fastabend } 436277162022SJohn Fastabend 43639e834259SNikolay Aleksandrov if (!del_bulk) { 43641690be63SVlad Yasevich if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { 4365b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid address"); 436677162022SJohn Fastabend return -EINVAL; 436777162022SJohn Fastabend } 43689e834259SNikolay Aleksandrov addr = nla_data(tb[NDA_LLADDR]); 43699e834259SNikolay Aleksandrov } 437077162022SJohn Fastabend 4371da715775SIdo Schimmel if (dev->type != ARPHRD_ETHER) { 4372da715775SIdo Schimmel NL_SET_ERR_MSG(extack, "FDB delete only supported for Ethernet devices"); 4373da715775SIdo Schimmel return -EINVAL; 4374da715775SIdo Schimmel } 4375da715775SIdo Schimmel 4376b88d12e4SFlorian Westphal err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack); 4377f6f6424bSJiri Pirko if (err) 4378f6f6424bSJiri Pirko return err; 4379f6f6424bSJiri Pirko 438077162022SJohn Fastabend err = -EOPNOTSUPP; 438177162022SJohn Fastabend 438277162022SJohn Fastabend /* Support fdb on master device the net/bridge default case */ 438377162022SJohn Fastabend if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && 43842e92a2d0SJulian Wiedmann netif_is_bridge_port(dev)) { 4385898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 438677162022SJohn Fastabend 43879e834259SNikolay Aleksandrov ops = br_dev->netdev_ops; 43889e834259SNikolay Aleksandrov if (!del_bulk) { 4389898e5061SJiri Pirko if (ops->ndo_fdb_del) 4390ca4567f1SAlaa Mohamed err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid, extack); 43919e834259SNikolay Aleksandrov } else { 43929e834259SNikolay Aleksandrov if (ops->ndo_fdb_del_bulk) 43939e834259SNikolay Aleksandrov err = ops->ndo_fdb_del_bulk(ndm, tb, dev, vid, 43949e834259SNikolay Aleksandrov extack); 43959e834259SNikolay Aleksandrov } 439677162022SJohn Fastabend 439777162022SJohn Fastabend if (err) 439877162022SJohn Fastabend goto out; 439977162022SJohn Fastabend else 440077162022SJohn Fastabend ndm->ndm_flags &= ~NTF_MASTER; 440177162022SJohn Fastabend } 440277162022SJohn Fastabend 440377162022SJohn Fastabend /* Embedded bridge, macvlan, and any other device support */ 4404090096bfSVlad Yasevich if (ndm->ndm_flags & NTF_SELF) { 44059e834259SNikolay Aleksandrov ops = dev->netdev_ops; 44069e834259SNikolay Aleksandrov if (!del_bulk) { 44079e834259SNikolay Aleksandrov if (ops->ndo_fdb_del) 4408ca4567f1SAlaa Mohamed err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid, extack); 4409090096bfSVlad Yasevich else 4410f6f6424bSJiri Pirko err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid); 44119e834259SNikolay Aleksandrov } else { 44129e834259SNikolay Aleksandrov /* in case err was cleared by NTF_MASTER call */ 44139e834259SNikolay Aleksandrov err = -EOPNOTSUPP; 44149e834259SNikolay Aleksandrov if (ops->ndo_fdb_del_bulk) 44159e834259SNikolay Aleksandrov err = ops->ndo_fdb_del_bulk(ndm, tb, dev, vid, 44169e834259SNikolay Aleksandrov extack); 44179e834259SNikolay Aleksandrov } 441877162022SJohn Fastabend 44193ff661c3SJohn Fastabend if (!err) { 44209e834259SNikolay Aleksandrov if (!del_bulk) 4421b3379041SHubert Sokolowski rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH, 4422b3379041SHubert Sokolowski ndm->ndm_state); 442377162022SJohn Fastabend ndm->ndm_flags &= ~NTF_SELF; 442477162022SJohn Fastabend } 44253ff661c3SJohn Fastabend } 442677162022SJohn Fastabend out: 442777162022SJohn Fastabend return err; 442877162022SJohn Fastabend } 442977162022SJohn Fastabend 4430d83b0603SJohn Fastabend static int nlmsg_populate_fdb(struct sk_buff *skb, 4431d83b0603SJohn Fastabend struct netlink_callback *cb, 4432d83b0603SJohn Fastabend struct net_device *dev, 4433d83b0603SJohn Fastabend int *idx, 4434d83b0603SJohn Fastabend struct netdev_hw_addr_list *list) 4435d83b0603SJohn Fastabend { 4436d83b0603SJohn Fastabend struct netdev_hw_addr *ha; 4437d83b0603SJohn Fastabend int err; 443815e47304SEric W. Biederman u32 portid, seq; 4439d83b0603SJohn Fastabend 444015e47304SEric W. Biederman portid = NETLINK_CB(cb->skb).portid; 4441d83b0603SJohn Fastabend seq = cb->nlh->nlmsg_seq; 4442d83b0603SJohn Fastabend 4443d83b0603SJohn Fastabend list_for_each_entry(ha, &list->list, list) { 4444d297653dSRoopa Prabhu if (*idx < cb->args[2]) 4445d83b0603SJohn Fastabend goto skip; 4446d83b0603SJohn Fastabend 44471e53d5bbSHubert Sokolowski err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0, 4448a7a558feSJohn Fastabend portid, seq, 44491c104a6bSNicolas Dichtel RTM_NEWNEIGH, NTF_SELF, 4450b3379041SHubert Sokolowski NLM_F_MULTI, NUD_PERMANENT); 4451d83b0603SJohn Fastabend if (err < 0) 4452d83b0603SJohn Fastabend return err; 4453d83b0603SJohn Fastabend skip: 4454d83b0603SJohn Fastabend *idx += 1; 4455d83b0603SJohn Fastabend } 4456d83b0603SJohn Fastabend return 0; 4457d83b0603SJohn Fastabend } 4458d83b0603SJohn Fastabend 4459d83b0603SJohn Fastabend /** 44602c53040fSBen Hutchings * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table. 4461a986967eSBart Van Assche * @skb: socket buffer to store message in 4462a986967eSBart Van Assche * @cb: netlink callback 4463d83b0603SJohn Fastabend * @dev: netdevice 4464a986967eSBart Van Assche * @filter_dev: ignored 4465a986967eSBart Van Assche * @idx: the number of FDB table entries dumped is added to *@idx 4466d83b0603SJohn Fastabend * 4467d83b0603SJohn Fastabend * Default netdevice operation to dump the existing unicast address list. 446891f3e7b1SJohn Fastabend * Returns number of addresses from list put in skb. 4469d83b0603SJohn Fastabend */ 4470d83b0603SJohn Fastabend int ndo_dflt_fdb_dump(struct sk_buff *skb, 4471d83b0603SJohn Fastabend struct netlink_callback *cb, 4472d83b0603SJohn Fastabend struct net_device *dev, 44735d5eacb3SJamal Hadi Salim struct net_device *filter_dev, 4474d297653dSRoopa Prabhu int *idx) 4475d83b0603SJohn Fastabend { 4476d83b0603SJohn Fastabend int err; 4477d83b0603SJohn Fastabend 447868883893SEric Dumazet if (dev->type != ARPHRD_ETHER) 447968883893SEric Dumazet return -EINVAL; 448068883893SEric Dumazet 4481d83b0603SJohn Fastabend netif_addr_lock_bh(dev); 4482d297653dSRoopa Prabhu err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc); 4483d83b0603SJohn Fastabend if (err) 4484d83b0603SJohn Fastabend goto out; 44852934c9dbSZhang Shengju err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc); 4486d83b0603SJohn Fastabend out: 4487d83b0603SJohn Fastabend netif_addr_unlock_bh(dev); 4488d297653dSRoopa Prabhu return err; 4489d83b0603SJohn Fastabend } 4490d83b0603SJohn Fastabend EXPORT_SYMBOL(ndo_dflt_fdb_dump); 4491d83b0603SJohn Fastabend 44928c6e137fSDavid Ahern static int valid_fdb_dump_strict(const struct nlmsghdr *nlh, 44938c6e137fSDavid Ahern int *br_idx, int *brport_idx, 44948c6e137fSDavid Ahern struct netlink_ext_ack *extack) 44958c6e137fSDavid Ahern { 44968c6e137fSDavid Ahern struct nlattr *tb[NDA_MAX + 1]; 44978c6e137fSDavid Ahern struct ndmsg *ndm; 44988c6e137fSDavid Ahern int err, i; 44998c6e137fSDavid Ahern 45008c6e137fSDavid Ahern if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) { 45018c6e137fSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid header for fdb dump request"); 45028c6e137fSDavid Ahern return -EINVAL; 45038c6e137fSDavid Ahern } 45048c6e137fSDavid Ahern 45058c6e137fSDavid Ahern ndm = nlmsg_data(nlh); 45068c6e137fSDavid Ahern if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state || 45078c6e137fSDavid Ahern ndm->ndm_flags || ndm->ndm_type) { 45088b73018fSNikolay Aleksandrov NL_SET_ERR_MSG(extack, "Invalid values in header for fdb dump request"); 45098c6e137fSDavid Ahern return -EINVAL; 45108c6e137fSDavid Ahern } 45118c6e137fSDavid Ahern 45128cb08174SJohannes Berg err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb, 45138cb08174SJohannes Berg NDA_MAX, NULL, extack); 45148c6e137fSDavid Ahern if (err < 0) 45158c6e137fSDavid Ahern return err; 45168c6e137fSDavid Ahern 45178c6e137fSDavid Ahern *brport_idx = ndm->ndm_ifindex; 45188c6e137fSDavid Ahern for (i = 0; i <= NDA_MAX; ++i) { 45198c6e137fSDavid Ahern if (!tb[i]) 45208c6e137fSDavid Ahern continue; 45218c6e137fSDavid Ahern 45228c6e137fSDavid Ahern switch (i) { 45238c6e137fSDavid Ahern case NDA_IFINDEX: 45248c6e137fSDavid Ahern if (nla_len(tb[i]) != sizeof(u32)) { 45258c6e137fSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid IFINDEX attribute in fdb dump request"); 45268c6e137fSDavid Ahern return -EINVAL; 45278c6e137fSDavid Ahern } 45288c6e137fSDavid Ahern *brport_idx = nla_get_u32(tb[NDA_IFINDEX]); 45298c6e137fSDavid Ahern break; 45308c6e137fSDavid Ahern case NDA_MASTER: 45318c6e137fSDavid Ahern if (nla_len(tb[i]) != sizeof(u32)) { 45328c6e137fSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid MASTER attribute in fdb dump request"); 45338c6e137fSDavid Ahern return -EINVAL; 45348c6e137fSDavid Ahern } 45358c6e137fSDavid Ahern *br_idx = nla_get_u32(tb[NDA_MASTER]); 45368c6e137fSDavid Ahern break; 45378c6e137fSDavid Ahern default: 45388c6e137fSDavid Ahern NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb dump request"); 45398c6e137fSDavid Ahern return -EINVAL; 45408c6e137fSDavid Ahern } 45418c6e137fSDavid Ahern } 45428c6e137fSDavid Ahern 45438c6e137fSDavid Ahern return 0; 45448c6e137fSDavid Ahern } 45458c6e137fSDavid Ahern 45468dfbda19SDavid Ahern static int valid_fdb_dump_legacy(const struct nlmsghdr *nlh, 45478dfbda19SDavid Ahern int *br_idx, int *brport_idx, 45488dfbda19SDavid Ahern struct netlink_ext_ack *extack) 45498dfbda19SDavid Ahern { 45508dfbda19SDavid Ahern struct nlattr *tb[IFLA_MAX+1]; 45518dfbda19SDavid Ahern int err; 45528dfbda19SDavid Ahern 45538dfbda19SDavid Ahern /* A hack to preserve kernel<->userspace interface. 45548dfbda19SDavid Ahern * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0. 45558dfbda19SDavid Ahern * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails. 45568dfbda19SDavid Ahern * So, check for ndmsg with an optional u32 attribute (not used here). 45578dfbda19SDavid Ahern * Fortunately these sizes don't conflict with the size of ifinfomsg 45588dfbda19SDavid Ahern * with an optional attribute. 45598dfbda19SDavid Ahern */ 45608dfbda19SDavid Ahern if (nlmsg_len(nlh) != sizeof(struct ndmsg) && 45618dfbda19SDavid Ahern (nlmsg_len(nlh) != sizeof(struct ndmsg) + 45628dfbda19SDavid Ahern nla_attr_size(sizeof(u32)))) { 45634565d7e5SDavid Ahern struct ifinfomsg *ifm; 45644565d7e5SDavid Ahern 45658cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg), 45668cb08174SJohannes Berg tb, IFLA_MAX, ifla_policy, 45678cb08174SJohannes Berg extack); 45688dfbda19SDavid Ahern if (err < 0) { 45698dfbda19SDavid Ahern return -EINVAL; 45708dfbda19SDavid Ahern } else if (err == 0) { 45718dfbda19SDavid Ahern if (tb[IFLA_MASTER]) 45728dfbda19SDavid Ahern *br_idx = nla_get_u32(tb[IFLA_MASTER]); 45738dfbda19SDavid Ahern } 45748dfbda19SDavid Ahern 45754565d7e5SDavid Ahern ifm = nlmsg_data(nlh); 45768dfbda19SDavid Ahern *brport_idx = ifm->ifi_index; 45778dfbda19SDavid Ahern } 45788dfbda19SDavid Ahern return 0; 45798dfbda19SDavid Ahern } 45808dfbda19SDavid Ahern 458177162022SJohn Fastabend static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb) 458277162022SJohn Fastabend { 458377162022SJohn Fastabend struct net_device *dev; 45845e6d2435SJamal Hadi Salim struct net_device *br_dev = NULL; 45855e6d2435SJamal Hadi Salim const struct net_device_ops *ops = NULL; 45865e6d2435SJamal Hadi Salim const struct net_device_ops *cops = NULL; 45875e6d2435SJamal Hadi Salim struct net *net = sock_net(skb->sk); 4588d297653dSRoopa Prabhu struct hlist_head *head; 45895e6d2435SJamal Hadi Salim int brport_idx = 0; 45905e6d2435SJamal Hadi Salim int br_idx = 0; 4591d297653dSRoopa Prabhu int h, s_h; 4592d297653dSRoopa Prabhu int idx = 0, s_idx; 4593d297653dSRoopa Prabhu int err = 0; 4594d297653dSRoopa Prabhu int fidx = 0; 459577162022SJohn Fastabend 45968c6e137fSDavid Ahern if (cb->strict_check) 45978c6e137fSDavid Ahern err = valid_fdb_dump_strict(cb->nlh, &br_idx, &brport_idx, 45988c6e137fSDavid Ahern cb->extack); 45998c6e137fSDavid Ahern else 46008dfbda19SDavid Ahern err = valid_fdb_dump_legacy(cb->nlh, &br_idx, &brport_idx, 46018dfbda19SDavid Ahern cb->extack); 46028dfbda19SDavid Ahern if (err < 0) 46038dfbda19SDavid Ahern return err; 46045e6d2435SJamal Hadi Salim 46055e6d2435SJamal Hadi Salim if (br_idx) { 46065e6d2435SJamal Hadi Salim br_dev = __dev_get_by_index(net, br_idx); 46075e6d2435SJamal Hadi Salim if (!br_dev) 46085e6d2435SJamal Hadi Salim return -ENODEV; 46095e6d2435SJamal Hadi Salim 4610898e5061SJiri Pirko ops = br_dev->netdev_ops; 46115e6d2435SJamal Hadi Salim } 46125e6d2435SJamal Hadi Salim 4613d297653dSRoopa Prabhu s_h = cb->args[0]; 4614d297653dSRoopa Prabhu s_idx = cb->args[1]; 4615d297653dSRoopa Prabhu 4616d297653dSRoopa Prabhu for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 4617d297653dSRoopa Prabhu idx = 0; 4618d297653dSRoopa Prabhu head = &net->dev_index_head[h]; 4619d297653dSRoopa Prabhu hlist_for_each_entry(dev, head, index_hlist) { 4620d297653dSRoopa Prabhu 46215e6d2435SJamal Hadi Salim if (brport_idx && (dev->ifindex != brport_idx)) 46225e6d2435SJamal Hadi Salim continue; 46235e6d2435SJamal Hadi Salim 46245e6d2435SJamal Hadi Salim if (!br_idx) { /* user did not specify a specific bridge */ 46252e92a2d0SJulian Wiedmann if (netif_is_bridge_port(dev)) { 46265e6d2435SJamal Hadi Salim br_dev = netdev_master_upper_dev_get(dev); 46275e6d2435SJamal Hadi Salim cops = br_dev->netdev_ops; 46285e6d2435SJamal Hadi Salim } 46295e6d2435SJamal Hadi Salim } else { 46305e6d2435SJamal Hadi Salim if (dev != br_dev && 46312e92a2d0SJulian Wiedmann !netif_is_bridge_port(dev)) 46325e6d2435SJamal Hadi Salim continue; 46335e6d2435SJamal Hadi Salim 46345e6d2435SJamal Hadi Salim if (br_dev != netdev_master_upper_dev_get(dev) && 4635254ec036SKyungrok Chung !netif_is_bridge_master(dev)) 46365e6d2435SJamal Hadi Salim continue; 46375e6d2435SJamal Hadi Salim cops = ops; 46385e6d2435SJamal Hadi Salim } 46395e6d2435SJamal Hadi Salim 4640d297653dSRoopa Prabhu if (idx < s_idx) 4641d297653dSRoopa Prabhu goto cont; 4642d297653dSRoopa Prabhu 46432e92a2d0SJulian Wiedmann if (netif_is_bridge_port(dev)) { 4644d297653dSRoopa Prabhu if (cops && cops->ndo_fdb_dump) { 4645d297653dSRoopa Prabhu err = cops->ndo_fdb_dump(skb, cb, 4646d297653dSRoopa Prabhu br_dev, dev, 4647d297653dSRoopa Prabhu &fidx); 4648d297653dSRoopa Prabhu if (err == -EMSGSIZE) 4649d297653dSRoopa Prabhu goto out; 465077162022SJohn Fastabend } 4651d297653dSRoopa Prabhu } 465277162022SJohn Fastabend 46535e6d2435SJamal Hadi Salim if (dev->netdev_ops->ndo_fdb_dump) 4654d297653dSRoopa Prabhu err = dev->netdev_ops->ndo_fdb_dump(skb, cb, 4655d297653dSRoopa Prabhu dev, NULL, 4656d297653dSRoopa Prabhu &fidx); 46576cb69742SHubert Sokolowski else 4658d297653dSRoopa Prabhu err = ndo_dflt_fdb_dump(skb, cb, dev, NULL, 4659d297653dSRoopa Prabhu &fidx); 4660d297653dSRoopa Prabhu if (err == -EMSGSIZE) 4661d297653dSRoopa Prabhu goto out; 46625e6d2435SJamal Hadi Salim 46635e6d2435SJamal Hadi Salim cops = NULL; 4664d297653dSRoopa Prabhu 4665d297653dSRoopa Prabhu /* reset fdb offset to 0 for rest of the interfaces */ 4666d297653dSRoopa Prabhu cb->args[2] = 0; 4667d297653dSRoopa Prabhu fidx = 0; 4668d297653dSRoopa Prabhu cont: 4669d297653dSRoopa Prabhu idx++; 4670d297653dSRoopa Prabhu } 467177162022SJohn Fastabend } 467277162022SJohn Fastabend 4673d297653dSRoopa Prabhu out: 4674d297653dSRoopa Prabhu cb->args[0] = h; 4675d297653dSRoopa Prabhu cb->args[1] = idx; 4676d297653dSRoopa Prabhu cb->args[2] = fidx; 4677d297653dSRoopa Prabhu 467877162022SJohn Fastabend return skb->len; 467977162022SJohn Fastabend } 468077162022SJohn Fastabend 46815b2f94b2SRoopa Prabhu static int valid_fdb_get_strict(const struct nlmsghdr *nlh, 46825b2f94b2SRoopa Prabhu struct nlattr **tb, u8 *ndm_flags, 46835b2f94b2SRoopa Prabhu int *br_idx, int *brport_idx, u8 **addr, 46845b2f94b2SRoopa Prabhu u16 *vid, struct netlink_ext_ack *extack) 46855b2f94b2SRoopa Prabhu { 46865b2f94b2SRoopa Prabhu struct ndmsg *ndm; 46875b2f94b2SRoopa Prabhu int err, i; 46885b2f94b2SRoopa Prabhu 46895b2f94b2SRoopa Prabhu if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) { 46905b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid header for fdb get request"); 46915b2f94b2SRoopa Prabhu return -EINVAL; 46925b2f94b2SRoopa Prabhu } 46935b2f94b2SRoopa Prabhu 46945b2f94b2SRoopa Prabhu ndm = nlmsg_data(nlh); 46955b2f94b2SRoopa Prabhu if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state || 46965b2f94b2SRoopa Prabhu ndm->ndm_type) { 46975b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid values in header for fdb get request"); 46985b2f94b2SRoopa Prabhu return -EINVAL; 46995b2f94b2SRoopa Prabhu } 47005b2f94b2SRoopa Prabhu 47015b2f94b2SRoopa Prabhu if (ndm->ndm_flags & ~(NTF_MASTER | NTF_SELF)) { 47025b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid flags in header for fdb get request"); 47035b2f94b2SRoopa Prabhu return -EINVAL; 47045b2f94b2SRoopa Prabhu } 47055b2f94b2SRoopa Prabhu 47068cb08174SJohannes Berg err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb, 47078cb08174SJohannes Berg NDA_MAX, nda_policy, extack); 47085b2f94b2SRoopa Prabhu if (err < 0) 47095b2f94b2SRoopa Prabhu return err; 47105b2f94b2SRoopa Prabhu 47115b2f94b2SRoopa Prabhu *ndm_flags = ndm->ndm_flags; 47125b2f94b2SRoopa Prabhu *brport_idx = ndm->ndm_ifindex; 47135b2f94b2SRoopa Prabhu for (i = 0; i <= NDA_MAX; ++i) { 47145b2f94b2SRoopa Prabhu if (!tb[i]) 47155b2f94b2SRoopa Prabhu continue; 47165b2f94b2SRoopa Prabhu 47175b2f94b2SRoopa Prabhu switch (i) { 47185b2f94b2SRoopa Prabhu case NDA_MASTER: 47195b2f94b2SRoopa Prabhu *br_idx = nla_get_u32(tb[i]); 47205b2f94b2SRoopa Prabhu break; 47215b2f94b2SRoopa Prabhu case NDA_LLADDR: 47225b2f94b2SRoopa Prabhu if (nla_len(tb[i]) != ETH_ALEN) { 47235b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid address in fdb get request"); 47245b2f94b2SRoopa Prabhu return -EINVAL; 47255b2f94b2SRoopa Prabhu } 47265b2f94b2SRoopa Prabhu *addr = nla_data(tb[i]); 47275b2f94b2SRoopa Prabhu break; 47285b2f94b2SRoopa Prabhu case NDA_VLAN: 47295b2f94b2SRoopa Prabhu err = fdb_vid_parse(tb[i], vid, extack); 47305b2f94b2SRoopa Prabhu if (err) 47315b2f94b2SRoopa Prabhu return err; 47325b2f94b2SRoopa Prabhu break; 47335b2f94b2SRoopa Prabhu case NDA_VNI: 47345b2f94b2SRoopa Prabhu break; 47355b2f94b2SRoopa Prabhu default: 47365b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb get request"); 47375b2f94b2SRoopa Prabhu return -EINVAL; 47385b2f94b2SRoopa Prabhu } 47395b2f94b2SRoopa Prabhu } 47405b2f94b2SRoopa Prabhu 47415b2f94b2SRoopa Prabhu return 0; 47425b2f94b2SRoopa Prabhu } 47435b2f94b2SRoopa Prabhu 47445b2f94b2SRoopa Prabhu static int rtnl_fdb_get(struct sk_buff *in_skb, struct nlmsghdr *nlh, 47455b2f94b2SRoopa Prabhu struct netlink_ext_ack *extack) 47465b2f94b2SRoopa Prabhu { 47475b2f94b2SRoopa Prabhu struct net_device *dev = NULL, *br_dev = NULL; 47485b2f94b2SRoopa Prabhu const struct net_device_ops *ops = NULL; 47495b2f94b2SRoopa Prabhu struct net *net = sock_net(in_skb->sk); 47505b2f94b2SRoopa Prabhu struct nlattr *tb[NDA_MAX + 1]; 47515b2f94b2SRoopa Prabhu struct sk_buff *skb; 47525b2f94b2SRoopa Prabhu int brport_idx = 0; 47535b2f94b2SRoopa Prabhu u8 ndm_flags = 0; 47545b2f94b2SRoopa Prabhu int br_idx = 0; 47555b2f94b2SRoopa Prabhu u8 *addr = NULL; 47565b2f94b2SRoopa Prabhu u16 vid = 0; 47575b2f94b2SRoopa Prabhu int err; 47585b2f94b2SRoopa Prabhu 47595b2f94b2SRoopa Prabhu err = valid_fdb_get_strict(nlh, tb, &ndm_flags, &br_idx, 47605b2f94b2SRoopa Prabhu &brport_idx, &addr, &vid, extack); 47615b2f94b2SRoopa Prabhu if (err < 0) 47625b2f94b2SRoopa Prabhu return err; 47635b2f94b2SRoopa Prabhu 4764f989d03eSNikolay Aleksandrov if (!addr) { 4765f989d03eSNikolay Aleksandrov NL_SET_ERR_MSG(extack, "Missing lookup address for fdb get request"); 4766f989d03eSNikolay Aleksandrov return -EINVAL; 4767f989d03eSNikolay Aleksandrov } 4768f989d03eSNikolay Aleksandrov 47695b2f94b2SRoopa Prabhu if (brport_idx) { 47705b2f94b2SRoopa Prabhu dev = __dev_get_by_index(net, brport_idx); 47715b2f94b2SRoopa Prabhu if (!dev) { 47725b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Unknown device ifindex"); 47735b2f94b2SRoopa Prabhu return -ENODEV; 47745b2f94b2SRoopa Prabhu } 47755b2f94b2SRoopa Prabhu } 47765b2f94b2SRoopa Prabhu 47775b2f94b2SRoopa Prabhu if (br_idx) { 47785b2f94b2SRoopa Prabhu if (dev) { 47795b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Master and device are mutually exclusive"); 47805b2f94b2SRoopa Prabhu return -EINVAL; 47815b2f94b2SRoopa Prabhu } 47825b2f94b2SRoopa Prabhu 47835b2f94b2SRoopa Prabhu br_dev = __dev_get_by_index(net, br_idx); 47845b2f94b2SRoopa Prabhu if (!br_dev) { 47855b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid master ifindex"); 47865b2f94b2SRoopa Prabhu return -EINVAL; 47875b2f94b2SRoopa Prabhu } 47885b2f94b2SRoopa Prabhu ops = br_dev->netdev_ops; 47895b2f94b2SRoopa Prabhu } 47905b2f94b2SRoopa Prabhu 47915b2f94b2SRoopa Prabhu if (dev) { 47925b2f94b2SRoopa Prabhu if (!ndm_flags || (ndm_flags & NTF_MASTER)) { 47932e92a2d0SJulian Wiedmann if (!netif_is_bridge_port(dev)) { 47945b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Device is not a bridge port"); 47955b2f94b2SRoopa Prabhu return -EINVAL; 47965b2f94b2SRoopa Prabhu } 47975b2f94b2SRoopa Prabhu br_dev = netdev_master_upper_dev_get(dev); 47985b2f94b2SRoopa Prabhu if (!br_dev) { 47995b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Master of device not found"); 48005b2f94b2SRoopa Prabhu return -EINVAL; 48015b2f94b2SRoopa Prabhu } 48025b2f94b2SRoopa Prabhu ops = br_dev->netdev_ops; 48035b2f94b2SRoopa Prabhu } else { 48045b2f94b2SRoopa Prabhu if (!(ndm_flags & NTF_SELF)) { 48055b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Missing NTF_SELF"); 48065b2f94b2SRoopa Prabhu return -EINVAL; 48075b2f94b2SRoopa Prabhu } 48085b2f94b2SRoopa Prabhu ops = dev->netdev_ops; 48095b2f94b2SRoopa Prabhu } 48105b2f94b2SRoopa Prabhu } 48115b2f94b2SRoopa Prabhu 48125b2f94b2SRoopa Prabhu if (!br_dev && !dev) { 48135b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "No device specified"); 48145b2f94b2SRoopa Prabhu return -ENODEV; 48155b2f94b2SRoopa Prabhu } 48165b2f94b2SRoopa Prabhu 48175b2f94b2SRoopa Prabhu if (!ops || !ops->ndo_fdb_get) { 48185b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Fdb get operation not supported by device"); 48195b2f94b2SRoopa Prabhu return -EOPNOTSUPP; 48205b2f94b2SRoopa Prabhu } 48215b2f94b2SRoopa Prabhu 48225b2f94b2SRoopa Prabhu skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); 48235b2f94b2SRoopa Prabhu if (!skb) 48245b2f94b2SRoopa Prabhu return -ENOBUFS; 48255b2f94b2SRoopa Prabhu 48265b2f94b2SRoopa Prabhu if (br_dev) 48275b2f94b2SRoopa Prabhu dev = br_dev; 48285b2f94b2SRoopa Prabhu err = ops->ndo_fdb_get(skb, tb, dev, addr, vid, 48295b2f94b2SRoopa Prabhu NETLINK_CB(in_skb).portid, 48305b2f94b2SRoopa Prabhu nlh->nlmsg_seq, extack); 48315b2f94b2SRoopa Prabhu if (err) 48325b2f94b2SRoopa Prabhu goto out; 48335b2f94b2SRoopa Prabhu 48345b2f94b2SRoopa Prabhu return rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 48355b2f94b2SRoopa Prabhu out: 48365b2f94b2SRoopa Prabhu kfree_skb(skb); 48375b2f94b2SRoopa Prabhu return err; 48385b2f94b2SRoopa Prabhu } 48395b2f94b2SRoopa Prabhu 48402c3c031cSScott Feldman static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask, 48412c3c031cSScott Feldman unsigned int attrnum, unsigned int flag) 48422c3c031cSScott Feldman { 48432c3c031cSScott Feldman if (mask & flag) 48442c3c031cSScott Feldman return nla_put_u8(skb, attrnum, !!(flags & flag)); 48452c3c031cSScott Feldman return 0; 48462c3c031cSScott Feldman } 48472c3c031cSScott Feldman 4848815cccbfSJohn Fastabend int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq, 48492c3c031cSScott Feldman struct net_device *dev, u16 mode, 48507d4f8d87SScott Feldman u32 flags, u32 mask, int nlflags, 48517d4f8d87SScott Feldman u32 filter_mask, 48527d4f8d87SScott Feldman int (*vlan_fill)(struct sk_buff *skb, 48537d4f8d87SScott Feldman struct net_device *dev, 48547d4f8d87SScott Feldman u32 filter_mask)) 4855815cccbfSJohn Fastabend { 4856815cccbfSJohn Fastabend struct nlmsghdr *nlh; 4857815cccbfSJohn Fastabend struct ifinfomsg *ifm; 4858815cccbfSJohn Fastabend struct nlattr *br_afspec; 48592c3c031cSScott Feldman struct nlattr *protinfo; 4860815cccbfSJohn Fastabend u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN; 4861898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 48627d4f8d87SScott Feldman int err = 0; 4863815cccbfSJohn Fastabend 486446c264daSNicolas Dichtel nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags); 4865815cccbfSJohn Fastabend if (nlh == NULL) 4866815cccbfSJohn Fastabend return -EMSGSIZE; 4867815cccbfSJohn Fastabend 4868815cccbfSJohn Fastabend ifm = nlmsg_data(nlh); 4869815cccbfSJohn Fastabend ifm->ifi_family = AF_BRIDGE; 4870815cccbfSJohn Fastabend ifm->__ifi_pad = 0; 4871815cccbfSJohn Fastabend ifm->ifi_type = dev->type; 4872815cccbfSJohn Fastabend ifm->ifi_index = dev->ifindex; 4873815cccbfSJohn Fastabend ifm->ifi_flags = dev_get_flags(dev); 4874815cccbfSJohn Fastabend ifm->ifi_change = 0; 4875815cccbfSJohn Fastabend 4876815cccbfSJohn Fastabend 4877815cccbfSJohn Fastabend if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 4878815cccbfSJohn Fastabend nla_put_u32(skb, IFLA_MTU, dev->mtu) || 4879815cccbfSJohn Fastabend nla_put_u8(skb, IFLA_OPERSTATE, operstate) || 4880898e5061SJiri Pirko (br_dev && 4881898e5061SJiri Pirko nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) || 4882815cccbfSJohn Fastabend (dev->addr_len && 4883815cccbfSJohn Fastabend nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || 4884a54acb3aSNicolas Dichtel (dev->ifindex != dev_get_iflink(dev) && 4885a54acb3aSNicolas Dichtel nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev)))) 4886815cccbfSJohn Fastabend goto nla_put_failure; 4887815cccbfSJohn Fastabend 4888ae0be8deSMichal Kubecek br_afspec = nla_nest_start_noflag(skb, IFLA_AF_SPEC); 4889815cccbfSJohn Fastabend if (!br_afspec) 4890815cccbfSJohn Fastabend goto nla_put_failure; 4891815cccbfSJohn Fastabend 48921d460b98SRoopa Prabhu if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) { 4893815cccbfSJohn Fastabend nla_nest_cancel(skb, br_afspec); 4894815cccbfSJohn Fastabend goto nla_put_failure; 4895815cccbfSJohn Fastabend } 48961d460b98SRoopa Prabhu 48971d460b98SRoopa Prabhu if (mode != BRIDGE_MODE_UNDEF) { 48981d460b98SRoopa Prabhu if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) { 48991d460b98SRoopa Prabhu nla_nest_cancel(skb, br_afspec); 49001d460b98SRoopa Prabhu goto nla_put_failure; 49011d460b98SRoopa Prabhu } 49021d460b98SRoopa Prabhu } 49037d4f8d87SScott Feldman if (vlan_fill) { 49047d4f8d87SScott Feldman err = vlan_fill(skb, dev, filter_mask); 49057d4f8d87SScott Feldman if (err) { 49067d4f8d87SScott Feldman nla_nest_cancel(skb, br_afspec); 49077d4f8d87SScott Feldman goto nla_put_failure; 49087d4f8d87SScott Feldman } 49097d4f8d87SScott Feldman } 4910815cccbfSJohn Fastabend nla_nest_end(skb, br_afspec); 4911815cccbfSJohn Fastabend 4912ae0be8deSMichal Kubecek protinfo = nla_nest_start(skb, IFLA_PROTINFO); 49132c3c031cSScott Feldman if (!protinfo) 49142c3c031cSScott Feldman goto nla_put_failure; 49152c3c031cSScott Feldman 49162c3c031cSScott Feldman if (brport_nla_put_flag(skb, flags, mask, 49172c3c031cSScott Feldman IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) || 49182c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 49192c3c031cSScott Feldman IFLA_BRPORT_GUARD, BR_BPDU_GUARD) || 49202c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 49212c3c031cSScott Feldman IFLA_BRPORT_FAST_LEAVE, 49222c3c031cSScott Feldman BR_MULTICAST_FAST_LEAVE) || 49232c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 49242c3c031cSScott Feldman IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) || 49252c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 49262c3c031cSScott Feldman IFLA_BRPORT_LEARNING, BR_LEARNING) || 49272c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 49282c3c031cSScott Feldman IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) || 49292c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 49302c3c031cSScott Feldman IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) || 49312c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 4932583cb0b4SJulian Wiedmann IFLA_BRPORT_PROXYARP, BR_PROXYARP) || 4933583cb0b4SJulian Wiedmann brport_nla_put_flag(skb, flags, mask, 4934583cb0b4SJulian Wiedmann IFLA_BRPORT_MCAST_FLOOD, BR_MCAST_FLOOD) || 4935583cb0b4SJulian Wiedmann brport_nla_put_flag(skb, flags, mask, 4936583cb0b4SJulian Wiedmann IFLA_BRPORT_BCAST_FLOOD, BR_BCAST_FLOOD)) { 49372c3c031cSScott Feldman nla_nest_cancel(skb, protinfo); 49382c3c031cSScott Feldman goto nla_put_failure; 49392c3c031cSScott Feldman } 49402c3c031cSScott Feldman 49412c3c031cSScott Feldman nla_nest_end(skb, protinfo); 49422c3c031cSScott Feldman 4943053c095aSJohannes Berg nlmsg_end(skb, nlh); 4944053c095aSJohannes Berg return 0; 4945815cccbfSJohn Fastabend nla_put_failure: 4946815cccbfSJohn Fastabend nlmsg_cancel(skb, nlh); 49477d4f8d87SScott Feldman return err ? err : -EMSGSIZE; 4948815cccbfSJohn Fastabend } 49497d4f8d87SScott Feldman EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink); 4950815cccbfSJohn Fastabend 49512d011be8SDavid Ahern static int valid_bridge_getlink_req(const struct nlmsghdr *nlh, 49522d011be8SDavid Ahern bool strict_check, u32 *filter_mask, 49532d011be8SDavid Ahern struct netlink_ext_ack *extack) 49542d011be8SDavid Ahern { 49552d011be8SDavid Ahern struct nlattr *tb[IFLA_MAX+1]; 49562d011be8SDavid Ahern int err, i; 49572d011be8SDavid Ahern 49582d011be8SDavid Ahern if (strict_check) { 49592d011be8SDavid Ahern struct ifinfomsg *ifm; 49602d011be8SDavid Ahern 49612d011be8SDavid Ahern if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { 49622d011be8SDavid Ahern NL_SET_ERR_MSG(extack, "Invalid header for bridge link dump"); 49632d011be8SDavid Ahern return -EINVAL; 49642d011be8SDavid Ahern } 49652d011be8SDavid Ahern 49662d011be8SDavid Ahern ifm = nlmsg_data(nlh); 49672d011be8SDavid Ahern if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags || 49682d011be8SDavid Ahern ifm->ifi_change || ifm->ifi_index) { 49692d011be8SDavid Ahern NL_SET_ERR_MSG(extack, "Invalid values in header for bridge link dump request"); 49702d011be8SDavid Ahern return -EINVAL; 49712d011be8SDavid Ahern } 49722d011be8SDavid Ahern 49738cb08174SJohannes Berg err = nlmsg_parse_deprecated_strict(nlh, 49748cb08174SJohannes Berg sizeof(struct ifinfomsg), 49758cb08174SJohannes Berg tb, IFLA_MAX, ifla_policy, 49768cb08174SJohannes Berg extack); 49772d011be8SDavid Ahern } else { 49788cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg), 49798cb08174SJohannes Berg tb, IFLA_MAX, ifla_policy, 49808cb08174SJohannes Berg extack); 49812d011be8SDavid Ahern } 49822d011be8SDavid Ahern if (err < 0) 49832d011be8SDavid Ahern return err; 49842d011be8SDavid Ahern 49852d011be8SDavid Ahern /* new attributes should only be added with strict checking */ 49862d011be8SDavid Ahern for (i = 0; i <= IFLA_MAX; ++i) { 49872d011be8SDavid Ahern if (!tb[i]) 49882d011be8SDavid Ahern continue; 49892d011be8SDavid Ahern 49902d011be8SDavid Ahern switch (i) { 49912d011be8SDavid Ahern case IFLA_EXT_MASK: 49922d011be8SDavid Ahern *filter_mask = nla_get_u32(tb[i]); 49932d011be8SDavid Ahern break; 49942d011be8SDavid Ahern default: 49952d011be8SDavid Ahern if (strict_check) { 49962d011be8SDavid Ahern NL_SET_ERR_MSG(extack, "Unsupported attribute in bridge link dump request"); 49972d011be8SDavid Ahern return -EINVAL; 49982d011be8SDavid Ahern } 49992d011be8SDavid Ahern } 50002d011be8SDavid Ahern } 50012d011be8SDavid Ahern 50022d011be8SDavid Ahern return 0; 50032d011be8SDavid Ahern } 50042d011be8SDavid Ahern 5005e5a55a89SJohn Fastabend static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb) 5006e5a55a89SJohn Fastabend { 50072d011be8SDavid Ahern const struct nlmsghdr *nlh = cb->nlh; 5008e5a55a89SJohn Fastabend struct net *net = sock_net(skb->sk); 5009e5a55a89SJohn Fastabend struct net_device *dev; 5010e5a55a89SJohn Fastabend int idx = 0; 5011e5a55a89SJohn Fastabend u32 portid = NETLINK_CB(cb->skb).portid; 50122d011be8SDavid Ahern u32 seq = nlh->nlmsg_seq; 50136cbdceebSVlad Yasevich u32 filter_mask = 0; 5014d64f69b0SRoopa Prabhu int err; 50156cbdceebSVlad Yasevich 50162d011be8SDavid Ahern err = valid_bridge_getlink_req(nlh, cb->strict_check, &filter_mask, 50172d011be8SDavid Ahern cb->extack); 50182d011be8SDavid Ahern if (err < 0 && cb->strict_check) 50192d011be8SDavid Ahern return err; 5020e5a55a89SJohn Fastabend 5021e5a55a89SJohn Fastabend rcu_read_lock(); 5022e5a55a89SJohn Fastabend for_each_netdev_rcu(net, dev) { 5023e5a55a89SJohn Fastabend const struct net_device_ops *ops = dev->netdev_ops; 5024898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 5025e5a55a89SJohn Fastabend 5026898e5061SJiri Pirko if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) { 5027d64f69b0SRoopa Prabhu if (idx >= cb->args[0]) { 5028d64f69b0SRoopa Prabhu err = br_dev->netdev_ops->ndo_bridge_getlink( 5029d64f69b0SRoopa Prabhu skb, portid, seq, dev, 5030d64f69b0SRoopa Prabhu filter_mask, NLM_F_MULTI); 5031f6c5775fSDavid Ahern if (err < 0 && err != -EOPNOTSUPP) { 5032f6c5775fSDavid Ahern if (likely(skb->len)) 5033e5a55a89SJohn Fastabend break; 5034f6c5775fSDavid Ahern 5035f6c5775fSDavid Ahern goto out_err; 5036f6c5775fSDavid Ahern } 5037d64f69b0SRoopa Prabhu } 5038e5a55a89SJohn Fastabend idx++; 5039e5a55a89SJohn Fastabend } 5040e5a55a89SJohn Fastabend 5041e5a55a89SJohn Fastabend if (ops->ndo_bridge_getlink) { 5042d64f69b0SRoopa Prabhu if (idx >= cb->args[0]) { 5043d64f69b0SRoopa Prabhu err = ops->ndo_bridge_getlink(skb, portid, 5044d64f69b0SRoopa Prabhu seq, dev, 504546c264daSNicolas Dichtel filter_mask, 5046d64f69b0SRoopa Prabhu NLM_F_MULTI); 5047f6c5775fSDavid Ahern if (err < 0 && err != -EOPNOTSUPP) { 5048f6c5775fSDavid Ahern if (likely(skb->len)) 5049e5a55a89SJohn Fastabend break; 5050f6c5775fSDavid Ahern 5051f6c5775fSDavid Ahern goto out_err; 5052f6c5775fSDavid Ahern } 5053d64f69b0SRoopa Prabhu } 5054e5a55a89SJohn Fastabend idx++; 5055e5a55a89SJohn Fastabend } 5056e5a55a89SJohn Fastabend } 5057f6c5775fSDavid Ahern err = skb->len; 5058f6c5775fSDavid Ahern out_err: 5059e5a55a89SJohn Fastabend rcu_read_unlock(); 5060e5a55a89SJohn Fastabend cb->args[0] = idx; 5061e5a55a89SJohn Fastabend 5062f6c5775fSDavid Ahern return err; 5063e5a55a89SJohn Fastabend } 5064e5a55a89SJohn Fastabend 50652469ffd7SJohn Fastabend static inline size_t bridge_nlmsg_size(void) 50662469ffd7SJohn Fastabend { 50672469ffd7SJohn Fastabend return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 50682469ffd7SJohn Fastabend + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 50692469ffd7SJohn Fastabend + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 50702469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_MASTER */ 50712469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_MTU */ 50722469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_LINK */ 50732469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */ 50742469ffd7SJohn Fastabend + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */ 50752469ffd7SJohn Fastabend + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */ 50762469ffd7SJohn Fastabend + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */ 50772469ffd7SJohn Fastabend + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */ 50782469ffd7SJohn Fastabend } 50792469ffd7SJohn Fastabend 508002dba438SRoopa Prabhu static int rtnl_bridge_notify(struct net_device *dev) 50812469ffd7SJohn Fastabend { 50822469ffd7SJohn Fastabend struct net *net = dev_net(dev); 50832469ffd7SJohn Fastabend struct sk_buff *skb; 50842469ffd7SJohn Fastabend int err = -EOPNOTSUPP; 50852469ffd7SJohn Fastabend 508602dba438SRoopa Prabhu if (!dev->netdev_ops->ndo_bridge_getlink) 508702dba438SRoopa Prabhu return 0; 508802dba438SRoopa Prabhu 50892469ffd7SJohn Fastabend skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC); 50902469ffd7SJohn Fastabend if (!skb) { 50912469ffd7SJohn Fastabend err = -ENOMEM; 50922469ffd7SJohn Fastabend goto errout; 50932469ffd7SJohn Fastabend } 50942469ffd7SJohn Fastabend 509546c264daSNicolas Dichtel err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0); 5096c38e01b8SJohn Fastabend if (err < 0) 5097c38e01b8SJohn Fastabend goto errout; 50982469ffd7SJohn Fastabend 5099d2e381c4SIdo Schimmel /* Notification info is only filled for bridge ports, not the bridge 5100d2e381c4SIdo Schimmel * device itself. Therefore, a zero notification length is valid and 5101d2e381c4SIdo Schimmel * should not result in an error. 5102d2e381c4SIdo Schimmel */ 5103d2e381c4SIdo Schimmel if (!skb->len) 510459ccaaaaSRoopa Prabhu goto errout; 510559ccaaaaSRoopa Prabhu 51062469ffd7SJohn Fastabend rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC); 51072469ffd7SJohn Fastabend return 0; 51082469ffd7SJohn Fastabend errout: 51092469ffd7SJohn Fastabend WARN_ON(err == -EMSGSIZE); 51102469ffd7SJohn Fastabend kfree_skb(skb); 511159ccaaaaSRoopa Prabhu if (err) 51122469ffd7SJohn Fastabend rtnl_set_sk_err(net, RTNLGRP_LINK, err); 51132469ffd7SJohn Fastabend return err; 51142469ffd7SJohn Fastabend } 51152469ffd7SJohn Fastabend 5116c21ef3e3SDavid Ahern static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, 5117c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 5118e5a55a89SJohn Fastabend { 5119e5a55a89SJohn Fastabend struct net *net = sock_net(skb->sk); 5120e5a55a89SJohn Fastabend struct ifinfomsg *ifm; 5121e5a55a89SJohn Fastabend struct net_device *dev; 51222469ffd7SJohn Fastabend struct nlattr *br_spec, *attr = NULL; 51232469ffd7SJohn Fastabend int rem, err = -EOPNOTSUPP; 51244de8b413SRosen, Rami u16 flags = 0; 5125c38e01b8SJohn Fastabend bool have_flags = false; 5126e5a55a89SJohn Fastabend 5127e5a55a89SJohn Fastabend if (nlmsg_len(nlh) < sizeof(*ifm)) 5128e5a55a89SJohn Fastabend return -EINVAL; 5129e5a55a89SJohn Fastabend 5130e5a55a89SJohn Fastabend ifm = nlmsg_data(nlh); 5131e5a55a89SJohn Fastabend if (ifm->ifi_family != AF_BRIDGE) 5132e5a55a89SJohn Fastabend return -EPFNOSUPPORT; 5133e5a55a89SJohn Fastabend 5134e5a55a89SJohn Fastabend dev = __dev_get_by_index(net, ifm->ifi_index); 5135e5a55a89SJohn Fastabend if (!dev) { 5136b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 5137e5a55a89SJohn Fastabend return -ENODEV; 5138e5a55a89SJohn Fastabend } 5139e5a55a89SJohn Fastabend 51402469ffd7SJohn Fastabend br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 51412469ffd7SJohn Fastabend if (br_spec) { 51422469ffd7SJohn Fastabend nla_for_each_nested(attr, br_spec, rem) { 51432469ffd7SJohn Fastabend if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { 51446e8d1c55SThomas Graf if (nla_len(attr) < sizeof(flags)) 51456e8d1c55SThomas Graf return -EINVAL; 51466e8d1c55SThomas Graf 5147c38e01b8SJohn Fastabend have_flags = true; 51482469ffd7SJohn Fastabend flags = nla_get_u16(attr); 51492469ffd7SJohn Fastabend break; 51502469ffd7SJohn Fastabend } 51512469ffd7SJohn Fastabend } 51522469ffd7SJohn Fastabend } 51532469ffd7SJohn Fastabend 51542469ffd7SJohn Fastabend if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { 5155898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 5156898e5061SJiri Pirko 5157898e5061SJiri Pirko if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) { 51582469ffd7SJohn Fastabend err = -EOPNOTSUPP; 5159e5a55a89SJohn Fastabend goto out; 5160e5a55a89SJohn Fastabend } 5161e5a55a89SJohn Fastabend 51622fd527b7SPetr Machata err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags, 51632fd527b7SPetr Machata extack); 51642469ffd7SJohn Fastabend if (err) 51652469ffd7SJohn Fastabend goto out; 51662469ffd7SJohn Fastabend 51672469ffd7SJohn Fastabend flags &= ~BRIDGE_FLAGS_MASTER; 51682469ffd7SJohn Fastabend } 51692469ffd7SJohn Fastabend 51702469ffd7SJohn Fastabend if ((flags & BRIDGE_FLAGS_SELF)) { 51712469ffd7SJohn Fastabend if (!dev->netdev_ops->ndo_bridge_setlink) 51722469ffd7SJohn Fastabend err = -EOPNOTSUPP; 51732469ffd7SJohn Fastabend else 5174add511b3SRoopa Prabhu err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh, 51752fd527b7SPetr Machata flags, 51762fd527b7SPetr Machata extack); 517702dba438SRoopa Prabhu if (!err) { 51782469ffd7SJohn Fastabend flags &= ~BRIDGE_FLAGS_SELF; 517902dba438SRoopa Prabhu 518002dba438SRoopa Prabhu /* Generate event to notify upper layer of bridge 518102dba438SRoopa Prabhu * change 518202dba438SRoopa Prabhu */ 518302dba438SRoopa Prabhu err = rtnl_bridge_notify(dev); 518402dba438SRoopa Prabhu } 51852469ffd7SJohn Fastabend } 51862469ffd7SJohn Fastabend 5187c38e01b8SJohn Fastabend if (have_flags) 51882469ffd7SJohn Fastabend memcpy(nla_data(attr), &flags, sizeof(flags)); 5189e5a55a89SJohn Fastabend out: 5190e5a55a89SJohn Fastabend return err; 5191e5a55a89SJohn Fastabend } 5192e5a55a89SJohn Fastabend 5193c21ef3e3SDavid Ahern static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, 5194c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 5195407af329SVlad Yasevich { 5196407af329SVlad Yasevich struct net *net = sock_net(skb->sk); 5197407af329SVlad Yasevich struct ifinfomsg *ifm; 5198407af329SVlad Yasevich struct net_device *dev; 5199407af329SVlad Yasevich struct nlattr *br_spec, *attr = NULL; 5200407af329SVlad Yasevich int rem, err = -EOPNOTSUPP; 52014de8b413SRosen, Rami u16 flags = 0; 5202407af329SVlad Yasevich bool have_flags = false; 5203407af329SVlad Yasevich 5204407af329SVlad Yasevich if (nlmsg_len(nlh) < sizeof(*ifm)) 5205407af329SVlad Yasevich return -EINVAL; 5206407af329SVlad Yasevich 5207407af329SVlad Yasevich ifm = nlmsg_data(nlh); 5208407af329SVlad Yasevich if (ifm->ifi_family != AF_BRIDGE) 5209407af329SVlad Yasevich return -EPFNOSUPPORT; 5210407af329SVlad Yasevich 5211407af329SVlad Yasevich dev = __dev_get_by_index(net, ifm->ifi_index); 5212407af329SVlad Yasevich if (!dev) { 5213b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 5214407af329SVlad Yasevich return -ENODEV; 5215407af329SVlad Yasevich } 5216407af329SVlad Yasevich 5217407af329SVlad Yasevich br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 5218407af329SVlad Yasevich if (br_spec) { 5219407af329SVlad Yasevich nla_for_each_nested(attr, br_spec, rem) { 5220407af329SVlad Yasevich if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { 52216e8d1c55SThomas Graf if (nla_len(attr) < sizeof(flags)) 52226e8d1c55SThomas Graf return -EINVAL; 52236e8d1c55SThomas Graf 5224407af329SVlad Yasevich have_flags = true; 5225407af329SVlad Yasevich flags = nla_get_u16(attr); 5226407af329SVlad Yasevich break; 5227407af329SVlad Yasevich } 5228407af329SVlad Yasevich } 5229407af329SVlad Yasevich } 5230407af329SVlad Yasevich 5231407af329SVlad Yasevich if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { 5232407af329SVlad Yasevich struct net_device *br_dev = netdev_master_upper_dev_get(dev); 5233407af329SVlad Yasevich 5234407af329SVlad Yasevich if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) { 5235407af329SVlad Yasevich err = -EOPNOTSUPP; 5236407af329SVlad Yasevich goto out; 5237407af329SVlad Yasevich } 5238407af329SVlad Yasevich 5239add511b3SRoopa Prabhu err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags); 5240407af329SVlad Yasevich if (err) 5241407af329SVlad Yasevich goto out; 5242407af329SVlad Yasevich 5243407af329SVlad Yasevich flags &= ~BRIDGE_FLAGS_MASTER; 5244407af329SVlad Yasevich } 5245407af329SVlad Yasevich 5246407af329SVlad Yasevich if ((flags & BRIDGE_FLAGS_SELF)) { 5247407af329SVlad Yasevich if (!dev->netdev_ops->ndo_bridge_dellink) 5248407af329SVlad Yasevich err = -EOPNOTSUPP; 5249407af329SVlad Yasevich else 5250add511b3SRoopa Prabhu err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh, 5251add511b3SRoopa Prabhu flags); 5252407af329SVlad Yasevich 525302dba438SRoopa Prabhu if (!err) { 5254407af329SVlad Yasevich flags &= ~BRIDGE_FLAGS_SELF; 525502dba438SRoopa Prabhu 525602dba438SRoopa Prabhu /* Generate event to notify upper layer of bridge 525702dba438SRoopa Prabhu * change 525802dba438SRoopa Prabhu */ 525902dba438SRoopa Prabhu err = rtnl_bridge_notify(dev); 526002dba438SRoopa Prabhu } 5261407af329SVlad Yasevich } 5262407af329SVlad Yasevich 5263407af329SVlad Yasevich if (have_flags) 5264407af329SVlad Yasevich memcpy(nla_data(attr), &flags, sizeof(flags)); 5265407af329SVlad Yasevich out: 5266407af329SVlad Yasevich return err; 5267407af329SVlad Yasevich } 5268407af329SVlad Yasevich 5269e8872a25SNikolay Aleksandrov static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr) 5270e8872a25SNikolay Aleksandrov { 5271e8872a25SNikolay Aleksandrov return (mask & IFLA_STATS_FILTER_BIT(attrid)) && 5272e8872a25SNikolay Aleksandrov (!idxattr || idxattr == attrid); 5273e8872a25SNikolay Aleksandrov } 5274e8872a25SNikolay Aleksandrov 5275f6e0fb81SPetr Machata static bool 5276f6e0fb81SPetr Machata rtnl_offload_xstats_have_ndo(const struct net_device *dev, int attr_id) 527769ae6ad2SNogah Frankel { 5278f6e0fb81SPetr Machata return dev->netdev_ops && 5279f6e0fb81SPetr Machata dev->netdev_ops->ndo_has_offload_stats && 5280f6e0fb81SPetr Machata dev->netdev_ops->ndo_get_offload_stats && 5281f6e0fb81SPetr Machata dev->netdev_ops->ndo_has_offload_stats(dev, attr_id); 528269ae6ad2SNogah Frankel } 528369ae6ad2SNogah Frankel 5284f6e0fb81SPetr Machata static unsigned int 5285f6e0fb81SPetr Machata rtnl_offload_xstats_get_size_ndo(const struct net_device *dev, int attr_id) 5286f6e0fb81SPetr Machata { 5287f6e0fb81SPetr Machata return rtnl_offload_xstats_have_ndo(dev, attr_id) ? 5288f6e0fb81SPetr Machata sizeof(struct rtnl_link_stats64) : 0; 5289f6e0fb81SPetr Machata } 5290f6e0fb81SPetr Machata 5291f6e0fb81SPetr Machata static int 5292f6e0fb81SPetr Machata rtnl_offload_xstats_fill_ndo(struct net_device *dev, int attr_id, 5293f6e0fb81SPetr Machata struct sk_buff *skb) 5294f6e0fb81SPetr Machata { 5295f6e0fb81SPetr Machata unsigned int size = rtnl_offload_xstats_get_size_ndo(dev, attr_id); 5296f6e0fb81SPetr Machata struct nlattr *attr = NULL; 5297f6e0fb81SPetr Machata void *attr_data; 5298f6e0fb81SPetr Machata int err; 5299f6e0fb81SPetr Machata 5300f6e0fb81SPetr Machata if (!size) 5301f6e0fb81SPetr Machata return -ENODATA; 5302f6e0fb81SPetr Machata 5303f6e0fb81SPetr Machata attr = nla_reserve_64bit(skb, attr_id, size, 5304f6e0fb81SPetr Machata IFLA_OFFLOAD_XSTATS_UNSPEC); 5305f6e0fb81SPetr Machata if (!attr) 5306f6e0fb81SPetr Machata return -EMSGSIZE; 5307f6e0fb81SPetr Machata 5308f6e0fb81SPetr Machata attr_data = nla_data(attr); 5309f6e0fb81SPetr Machata memset(attr_data, 0, size); 5310f6e0fb81SPetr Machata 5311f6e0fb81SPetr Machata err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev, attr_data); 5312f6e0fb81SPetr Machata if (err) 5313f6e0fb81SPetr Machata return err; 5314f6e0fb81SPetr Machata 531569ae6ad2SNogah Frankel return 0; 531669ae6ad2SNogah Frankel } 531769ae6ad2SNogah Frankel 53180e7788fdSPetr Machata static unsigned int 53190e7788fdSPetr Machata rtnl_offload_xstats_get_size_stats(const struct net_device *dev, 53200e7788fdSPetr Machata enum netdev_offload_xstats_type type) 53210e7788fdSPetr Machata { 53220e7788fdSPetr Machata bool enabled = netdev_offload_xstats_enabled(dev, type); 53230e7788fdSPetr Machata 53240e7788fdSPetr Machata return enabled ? sizeof(struct rtnl_hw_stats64) : 0; 53250e7788fdSPetr Machata } 53260e7788fdSPetr Machata 53270e7788fdSPetr Machata struct rtnl_offload_xstats_request_used { 53280e7788fdSPetr Machata bool request; 53290e7788fdSPetr Machata bool used; 53300e7788fdSPetr Machata }; 53310e7788fdSPetr Machata 53320e7788fdSPetr Machata static int 53330e7788fdSPetr Machata rtnl_offload_xstats_get_stats(struct net_device *dev, 53340e7788fdSPetr Machata enum netdev_offload_xstats_type type, 53350e7788fdSPetr Machata struct rtnl_offload_xstats_request_used *ru, 53360e7788fdSPetr Machata struct rtnl_hw_stats64 *stats, 53370e7788fdSPetr Machata struct netlink_ext_ack *extack) 53380e7788fdSPetr Machata { 53390e7788fdSPetr Machata bool request; 53400e7788fdSPetr Machata bool used; 53410e7788fdSPetr Machata int err; 53420e7788fdSPetr Machata 53430e7788fdSPetr Machata request = netdev_offload_xstats_enabled(dev, type); 53440e7788fdSPetr Machata if (!request) { 53450e7788fdSPetr Machata used = false; 53460e7788fdSPetr Machata goto out; 53470e7788fdSPetr Machata } 53480e7788fdSPetr Machata 53490e7788fdSPetr Machata err = netdev_offload_xstats_get(dev, type, stats, &used, extack); 53500e7788fdSPetr Machata if (err) 53510e7788fdSPetr Machata return err; 53520e7788fdSPetr Machata 53530e7788fdSPetr Machata out: 53540e7788fdSPetr Machata if (ru) { 53550e7788fdSPetr Machata ru->request = request; 53560e7788fdSPetr Machata ru->used = used; 53570e7788fdSPetr Machata } 53580e7788fdSPetr Machata return 0; 53590e7788fdSPetr Machata } 53600e7788fdSPetr Machata 53610e7788fdSPetr Machata static int 53620e7788fdSPetr Machata rtnl_offload_xstats_fill_hw_s_info_one(struct sk_buff *skb, int attr_id, 53630e7788fdSPetr Machata struct rtnl_offload_xstats_request_used *ru) 53640e7788fdSPetr Machata { 53650e7788fdSPetr Machata struct nlattr *nest; 53660e7788fdSPetr Machata 53670e7788fdSPetr Machata nest = nla_nest_start(skb, attr_id); 53680e7788fdSPetr Machata if (!nest) 53690e7788fdSPetr Machata return -EMSGSIZE; 53700e7788fdSPetr Machata 53710e7788fdSPetr Machata if (nla_put_u8(skb, IFLA_OFFLOAD_XSTATS_HW_S_INFO_REQUEST, ru->request)) 53720e7788fdSPetr Machata goto nla_put_failure; 53730e7788fdSPetr Machata 53740e7788fdSPetr Machata if (nla_put_u8(skb, IFLA_OFFLOAD_XSTATS_HW_S_INFO_USED, ru->used)) 53750e7788fdSPetr Machata goto nla_put_failure; 53760e7788fdSPetr Machata 53770e7788fdSPetr Machata nla_nest_end(skb, nest); 53780e7788fdSPetr Machata return 0; 53790e7788fdSPetr Machata 53800e7788fdSPetr Machata nla_put_failure: 53810e7788fdSPetr Machata nla_nest_cancel(skb, nest); 53820e7788fdSPetr Machata return -EMSGSIZE; 53830e7788fdSPetr Machata } 53840e7788fdSPetr Machata 53850e7788fdSPetr Machata static int 53860e7788fdSPetr Machata rtnl_offload_xstats_fill_hw_s_info(struct sk_buff *skb, struct net_device *dev, 53870e7788fdSPetr Machata struct netlink_ext_ack *extack) 53880e7788fdSPetr Machata { 53890e7788fdSPetr Machata enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3; 53900e7788fdSPetr Machata struct rtnl_offload_xstats_request_used ru_l3; 53910e7788fdSPetr Machata struct nlattr *nest; 53920e7788fdSPetr Machata int err; 53930e7788fdSPetr Machata 53940e7788fdSPetr Machata err = rtnl_offload_xstats_get_stats(dev, t_l3, &ru_l3, NULL, extack); 53950e7788fdSPetr Machata if (err) 53960e7788fdSPetr Machata return err; 53970e7788fdSPetr Machata 53980e7788fdSPetr Machata nest = nla_nest_start(skb, IFLA_OFFLOAD_XSTATS_HW_S_INFO); 53990e7788fdSPetr Machata if (!nest) 54000e7788fdSPetr Machata return -EMSGSIZE; 54010e7788fdSPetr Machata 54020e7788fdSPetr Machata if (rtnl_offload_xstats_fill_hw_s_info_one(skb, 54030e7788fdSPetr Machata IFLA_OFFLOAD_XSTATS_L3_STATS, 54040e7788fdSPetr Machata &ru_l3)) 54050e7788fdSPetr Machata goto nla_put_failure; 54060e7788fdSPetr Machata 54070e7788fdSPetr Machata nla_nest_end(skb, nest); 54080e7788fdSPetr Machata return 0; 54090e7788fdSPetr Machata 54100e7788fdSPetr Machata nla_put_failure: 54110e7788fdSPetr Machata nla_nest_cancel(skb, nest); 54120e7788fdSPetr Machata return -EMSGSIZE; 54130e7788fdSPetr Machata } 54140e7788fdSPetr Machata 54156b524a1dSPetr Machata static int rtnl_offload_xstats_fill(struct sk_buff *skb, struct net_device *dev, 541605415bccSPetr Machata int *prividx, u32 off_filter_mask, 541705415bccSPetr Machata struct netlink_ext_ack *extack) 541869ae6ad2SNogah Frankel { 54190e7788fdSPetr Machata enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3; 54200e7788fdSPetr Machata int attr_id_hw_s_info = IFLA_OFFLOAD_XSTATS_HW_S_INFO; 54210e7788fdSPetr Machata int attr_id_l3_stats = IFLA_OFFLOAD_XSTATS_L3_STATS; 5422f6e0fb81SPetr Machata int attr_id_cpu_hit = IFLA_OFFLOAD_XSTATS_CPU_HIT; 5423f6e0fb81SPetr Machata bool have_data = false; 542469ae6ad2SNogah Frankel int err; 542569ae6ad2SNogah Frankel 542646efc97bSPetr Machata if (*prividx <= attr_id_cpu_hit && 542746efc97bSPetr Machata (off_filter_mask & 542846efc97bSPetr Machata IFLA_STATS_FILTER_BIT(attr_id_cpu_hit))) { 5429f6e0fb81SPetr Machata err = rtnl_offload_xstats_fill_ndo(dev, attr_id_cpu_hit, skb); 5430f6e0fb81SPetr Machata if (!err) { 5431f6e0fb81SPetr Machata have_data = true; 5432f6e0fb81SPetr Machata } else if (err != -ENODATA) { 5433f6e0fb81SPetr Machata *prividx = attr_id_cpu_hit; 5434f6e0fb81SPetr Machata return err; 5435f6e0fb81SPetr Machata } 543669ae6ad2SNogah Frankel } 543769ae6ad2SNogah Frankel 54380e7788fdSPetr Machata if (*prividx <= attr_id_hw_s_info && 54390e7788fdSPetr Machata (off_filter_mask & IFLA_STATS_FILTER_BIT(attr_id_hw_s_info))) { 54400e7788fdSPetr Machata *prividx = attr_id_hw_s_info; 54410e7788fdSPetr Machata 54420e7788fdSPetr Machata err = rtnl_offload_xstats_fill_hw_s_info(skb, dev, extack); 54430e7788fdSPetr Machata if (err) 54440e7788fdSPetr Machata return err; 54450e7788fdSPetr Machata 54460e7788fdSPetr Machata have_data = true; 54470e7788fdSPetr Machata *prividx = 0; 54480e7788fdSPetr Machata } 54490e7788fdSPetr Machata 54500e7788fdSPetr Machata if (*prividx <= attr_id_l3_stats && 54510e7788fdSPetr Machata (off_filter_mask & IFLA_STATS_FILTER_BIT(attr_id_l3_stats))) { 54520e7788fdSPetr Machata unsigned int size_l3; 54530e7788fdSPetr Machata struct nlattr *attr; 54540e7788fdSPetr Machata 54550e7788fdSPetr Machata *prividx = attr_id_l3_stats; 54560e7788fdSPetr Machata 54570e7788fdSPetr Machata size_l3 = rtnl_offload_xstats_get_size_stats(dev, t_l3); 545823cfe941SPetr Machata if (!size_l3) 545923cfe941SPetr Machata goto skip_l3_stats; 54600e7788fdSPetr Machata attr = nla_reserve_64bit(skb, attr_id_l3_stats, size_l3, 54610e7788fdSPetr Machata IFLA_OFFLOAD_XSTATS_UNSPEC); 54620e7788fdSPetr Machata if (!attr) 54630e7788fdSPetr Machata return -EMSGSIZE; 54640e7788fdSPetr Machata 54650e7788fdSPetr Machata err = rtnl_offload_xstats_get_stats(dev, t_l3, NULL, 54660e7788fdSPetr Machata nla_data(attr), extack); 54670e7788fdSPetr Machata if (err) 54680e7788fdSPetr Machata return err; 54690e7788fdSPetr Machata 54700e7788fdSPetr Machata have_data = true; 547123cfe941SPetr Machata skip_l3_stats: 54720e7788fdSPetr Machata *prividx = 0; 54730e7788fdSPetr Machata } 54740e7788fdSPetr Machata 5475f6e0fb81SPetr Machata if (!have_data) 547669ae6ad2SNogah Frankel return -ENODATA; 547769ae6ad2SNogah Frankel 547869ae6ad2SNogah Frankel *prividx = 0; 547969ae6ad2SNogah Frankel return 0; 548069ae6ad2SNogah Frankel } 548169ae6ad2SNogah Frankel 54820e7788fdSPetr Machata static unsigned int 54830e7788fdSPetr Machata rtnl_offload_xstats_get_size_hw_s_info_one(const struct net_device *dev, 54840e7788fdSPetr Machata enum netdev_offload_xstats_type type) 54850e7788fdSPetr Machata { 54860e7788fdSPetr Machata bool enabled = netdev_offload_xstats_enabled(dev, type); 54870e7788fdSPetr Machata 54880e7788fdSPetr Machata return nla_total_size(0) + 54890e7788fdSPetr Machata /* IFLA_OFFLOAD_XSTATS_HW_S_INFO_REQUEST */ 54900e7788fdSPetr Machata nla_total_size(sizeof(u8)) + 54910e7788fdSPetr Machata /* IFLA_OFFLOAD_XSTATS_HW_S_INFO_USED */ 54920e7788fdSPetr Machata (enabled ? nla_total_size(sizeof(u8)) : 0) + 54930e7788fdSPetr Machata 0; 54940e7788fdSPetr Machata } 54950e7788fdSPetr Machata 54960e7788fdSPetr Machata static unsigned int 54970e7788fdSPetr Machata rtnl_offload_xstats_get_size_hw_s_info(const struct net_device *dev) 54980e7788fdSPetr Machata { 54990e7788fdSPetr Machata enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3; 55000e7788fdSPetr Machata 55010e7788fdSPetr Machata return nla_total_size(0) + 55020e7788fdSPetr Machata /* IFLA_OFFLOAD_XSTATS_L3_STATS */ 55030e7788fdSPetr Machata rtnl_offload_xstats_get_size_hw_s_info_one(dev, t_l3) + 55040e7788fdSPetr Machata 0; 55050e7788fdSPetr Machata } 55060e7788fdSPetr Machata 550746efc97bSPetr Machata static int rtnl_offload_xstats_get_size(const struct net_device *dev, 550846efc97bSPetr Machata u32 off_filter_mask) 550969ae6ad2SNogah Frankel { 55100e7788fdSPetr Machata enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3; 5511f6e0fb81SPetr Machata int attr_id_cpu_hit = IFLA_OFFLOAD_XSTATS_CPU_HIT; 551269ae6ad2SNogah Frankel int nla_size = 0; 551369ae6ad2SNogah Frankel int size; 551469ae6ad2SNogah Frankel 551546efc97bSPetr Machata if (off_filter_mask & 551646efc97bSPetr Machata IFLA_STATS_FILTER_BIT(attr_id_cpu_hit)) { 5517f6e0fb81SPetr Machata size = rtnl_offload_xstats_get_size_ndo(dev, attr_id_cpu_hit); 551869ae6ad2SNogah Frankel nla_size += nla_total_size_64bit(size); 551946efc97bSPetr Machata } 552069ae6ad2SNogah Frankel 55210e7788fdSPetr Machata if (off_filter_mask & 55220e7788fdSPetr Machata IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_HW_S_INFO)) 55230e7788fdSPetr Machata nla_size += rtnl_offload_xstats_get_size_hw_s_info(dev); 55240e7788fdSPetr Machata 55250e7788fdSPetr Machata if (off_filter_mask & 55260e7788fdSPetr Machata IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_L3_STATS)) { 55270e7788fdSPetr Machata size = rtnl_offload_xstats_get_size_stats(dev, t_l3); 55280e7788fdSPetr Machata nla_size += nla_total_size_64bit(size); 55290e7788fdSPetr Machata } 55300e7788fdSPetr Machata 553169ae6ad2SNogah Frankel if (nla_size != 0) 553269ae6ad2SNogah Frankel nla_size += nla_total_size(0); 553369ae6ad2SNogah Frankel 553469ae6ad2SNogah Frankel return nla_size; 553569ae6ad2SNogah Frankel } 553669ae6ad2SNogah Frankel 553746efc97bSPetr Machata struct rtnl_stats_dump_filters { 553846efc97bSPetr Machata /* mask[0] filters outer attributes. Then individual nests have their 553946efc97bSPetr Machata * filtering mask at the index of the nested attribute. 554046efc97bSPetr Machata */ 554146efc97bSPetr Machata u32 mask[IFLA_STATS_MAX + 1]; 554246efc97bSPetr Machata }; 554346efc97bSPetr Machata 554410c9ead9SRoopa Prabhu static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev, 554510c9ead9SRoopa Prabhu int type, u32 pid, u32 seq, u32 change, 554646efc97bSPetr Machata unsigned int flags, 554746efc97bSPetr Machata const struct rtnl_stats_dump_filters *filters, 554805415bccSPetr Machata int *idxattr, int *prividx, 554905415bccSPetr Machata struct netlink_ext_ack *extack) 555010c9ead9SRoopa Prabhu { 555146efc97bSPetr Machata unsigned int filter_mask = filters->mask[0]; 555210c9ead9SRoopa Prabhu struct if_stats_msg *ifsm; 555310c9ead9SRoopa Prabhu struct nlmsghdr *nlh; 555410c9ead9SRoopa Prabhu struct nlattr *attr; 5555e8872a25SNikolay Aleksandrov int s_prividx = *prividx; 555669ae6ad2SNogah Frankel int err; 555710c9ead9SRoopa Prabhu 555810c9ead9SRoopa Prabhu ASSERT_RTNL(); 555910c9ead9SRoopa Prabhu 556010c9ead9SRoopa Prabhu nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags); 556110c9ead9SRoopa Prabhu if (!nlh) 556210c9ead9SRoopa Prabhu return -EMSGSIZE; 556310c9ead9SRoopa Prabhu 556410c9ead9SRoopa Prabhu ifsm = nlmsg_data(nlh); 5565ce024f42SNikolay Aleksandrov ifsm->family = PF_UNSPEC; 5566ce024f42SNikolay Aleksandrov ifsm->pad1 = 0; 5567ce024f42SNikolay Aleksandrov ifsm->pad2 = 0; 556810c9ead9SRoopa Prabhu ifsm->ifindex = dev->ifindex; 556910c9ead9SRoopa Prabhu ifsm->filter_mask = filter_mask; 557010c9ead9SRoopa Prabhu 5571e8872a25SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) { 557210c9ead9SRoopa Prabhu struct rtnl_link_stats64 *sp; 557310c9ead9SRoopa Prabhu 557458414d32SNicolas Dichtel attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64, 557558414d32SNicolas Dichtel sizeof(struct rtnl_link_stats64), 557658414d32SNicolas Dichtel IFLA_STATS_UNSPEC); 5577216e6906SPetr Machata if (!attr) { 5578216e6906SPetr Machata err = -EMSGSIZE; 557910c9ead9SRoopa Prabhu goto nla_put_failure; 5580216e6906SPetr Machata } 558110c9ead9SRoopa Prabhu 558210c9ead9SRoopa Prabhu sp = nla_data(attr); 558310c9ead9SRoopa Prabhu dev_get_stats(dev, sp); 558410c9ead9SRoopa Prabhu } 558510c9ead9SRoopa Prabhu 558697a47facSNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) { 558797a47facSNikolay Aleksandrov const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 558897a47facSNikolay Aleksandrov 558997a47facSNikolay Aleksandrov if (ops && ops->fill_linkxstats) { 559097a47facSNikolay Aleksandrov *idxattr = IFLA_STATS_LINK_XSTATS; 5591ae0be8deSMichal Kubecek attr = nla_nest_start_noflag(skb, 559297a47facSNikolay Aleksandrov IFLA_STATS_LINK_XSTATS); 5593216e6906SPetr Machata if (!attr) { 5594216e6906SPetr Machata err = -EMSGSIZE; 559597a47facSNikolay Aleksandrov goto nla_put_failure; 5596216e6906SPetr Machata } 559797a47facSNikolay Aleksandrov 559880e73cc5SNikolay Aleksandrov err = ops->fill_linkxstats(skb, dev, prividx, *idxattr); 559980e73cc5SNikolay Aleksandrov nla_nest_end(skb, attr); 560080e73cc5SNikolay Aleksandrov if (err) 560180e73cc5SNikolay Aleksandrov goto nla_put_failure; 560280e73cc5SNikolay Aleksandrov *idxattr = 0; 560380e73cc5SNikolay Aleksandrov } 560480e73cc5SNikolay Aleksandrov } 560580e73cc5SNikolay Aleksandrov 560680e73cc5SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 560780e73cc5SNikolay Aleksandrov *idxattr)) { 560880e73cc5SNikolay Aleksandrov const struct rtnl_link_ops *ops = NULL; 560980e73cc5SNikolay Aleksandrov const struct net_device *master; 561080e73cc5SNikolay Aleksandrov 561180e73cc5SNikolay Aleksandrov master = netdev_master_upper_dev_get(dev); 561280e73cc5SNikolay Aleksandrov if (master) 561380e73cc5SNikolay Aleksandrov ops = master->rtnl_link_ops; 561480e73cc5SNikolay Aleksandrov if (ops && ops->fill_linkxstats) { 561580e73cc5SNikolay Aleksandrov *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE; 5616ae0be8deSMichal Kubecek attr = nla_nest_start_noflag(skb, 561780e73cc5SNikolay Aleksandrov IFLA_STATS_LINK_XSTATS_SLAVE); 5618216e6906SPetr Machata if (!attr) { 5619216e6906SPetr Machata err = -EMSGSIZE; 562080e73cc5SNikolay Aleksandrov goto nla_put_failure; 5621216e6906SPetr Machata } 562280e73cc5SNikolay Aleksandrov 562380e73cc5SNikolay Aleksandrov err = ops->fill_linkxstats(skb, dev, prividx, *idxattr); 562497a47facSNikolay Aleksandrov nla_nest_end(skb, attr); 562597a47facSNikolay Aleksandrov if (err) 562697a47facSNikolay Aleksandrov goto nla_put_failure; 562797a47facSNikolay Aleksandrov *idxattr = 0; 562897a47facSNikolay Aleksandrov } 562997a47facSNikolay Aleksandrov } 563097a47facSNikolay Aleksandrov 563169ae6ad2SNogah Frankel if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 563269ae6ad2SNogah Frankel *idxattr)) { 563346efc97bSPetr Machata u32 off_filter_mask; 563446efc97bSPetr Machata 563546efc97bSPetr Machata off_filter_mask = filters->mask[IFLA_STATS_LINK_OFFLOAD_XSTATS]; 563669ae6ad2SNogah Frankel *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS; 5637ae0be8deSMichal Kubecek attr = nla_nest_start_noflag(skb, 5638ae0be8deSMichal Kubecek IFLA_STATS_LINK_OFFLOAD_XSTATS); 5639216e6906SPetr Machata if (!attr) { 5640216e6906SPetr Machata err = -EMSGSIZE; 564169ae6ad2SNogah Frankel goto nla_put_failure; 5642216e6906SPetr Machata } 564369ae6ad2SNogah Frankel 564446efc97bSPetr Machata err = rtnl_offload_xstats_fill(skb, dev, prividx, 564505415bccSPetr Machata off_filter_mask, extack); 564669ae6ad2SNogah Frankel if (err == -ENODATA) 564769ae6ad2SNogah Frankel nla_nest_cancel(skb, attr); 564869ae6ad2SNogah Frankel else 564969ae6ad2SNogah Frankel nla_nest_end(skb, attr); 565069ae6ad2SNogah Frankel 565169ae6ad2SNogah Frankel if (err && err != -ENODATA) 565269ae6ad2SNogah Frankel goto nla_put_failure; 565369ae6ad2SNogah Frankel *idxattr = 0; 565469ae6ad2SNogah Frankel } 565569ae6ad2SNogah Frankel 5656aefb4d4aSRobert Shearman if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) { 5657aefb4d4aSRobert Shearman struct rtnl_af_ops *af_ops; 5658aefb4d4aSRobert Shearman 5659aefb4d4aSRobert Shearman *idxattr = IFLA_STATS_AF_SPEC; 5660ae0be8deSMichal Kubecek attr = nla_nest_start_noflag(skb, IFLA_STATS_AF_SPEC); 5661216e6906SPetr Machata if (!attr) { 5662216e6906SPetr Machata err = -EMSGSIZE; 5663aefb4d4aSRobert Shearman goto nla_put_failure; 5664216e6906SPetr Machata } 5665aefb4d4aSRobert Shearman 56665fa85a09SFlorian Westphal rcu_read_lock(); 56675fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 5668aefb4d4aSRobert Shearman if (af_ops->fill_stats_af) { 5669aefb4d4aSRobert Shearman struct nlattr *af; 5670aefb4d4aSRobert Shearman 5671ae0be8deSMichal Kubecek af = nla_nest_start_noflag(skb, 5672ae0be8deSMichal Kubecek af_ops->family); 56735fa85a09SFlorian Westphal if (!af) { 56745fa85a09SFlorian Westphal rcu_read_unlock(); 567557d29a29STom Rix err = -EMSGSIZE; 5676aefb4d4aSRobert Shearman goto nla_put_failure; 56775fa85a09SFlorian Westphal } 5678aefb4d4aSRobert Shearman err = af_ops->fill_stats_af(skb, dev); 5679aefb4d4aSRobert Shearman 56805fa85a09SFlorian Westphal if (err == -ENODATA) { 5681aefb4d4aSRobert Shearman nla_nest_cancel(skb, af); 56825fa85a09SFlorian Westphal } else if (err < 0) { 56835fa85a09SFlorian Westphal rcu_read_unlock(); 5684aefb4d4aSRobert Shearman goto nla_put_failure; 56855fa85a09SFlorian Westphal } 5686aefb4d4aSRobert Shearman 5687aefb4d4aSRobert Shearman nla_nest_end(skb, af); 5688aefb4d4aSRobert Shearman } 5689aefb4d4aSRobert Shearman } 56905fa85a09SFlorian Westphal rcu_read_unlock(); 5691aefb4d4aSRobert Shearman 5692aefb4d4aSRobert Shearman nla_nest_end(skb, attr); 5693aefb4d4aSRobert Shearman 5694aefb4d4aSRobert Shearman *idxattr = 0; 5695aefb4d4aSRobert Shearman } 5696aefb4d4aSRobert Shearman 569710c9ead9SRoopa Prabhu nlmsg_end(skb, nlh); 569810c9ead9SRoopa Prabhu 569910c9ead9SRoopa Prabhu return 0; 570010c9ead9SRoopa Prabhu 570110c9ead9SRoopa Prabhu nla_put_failure: 5702e8872a25SNikolay Aleksandrov /* not a multi message or no progress mean a real error */ 5703e8872a25SNikolay Aleksandrov if (!(flags & NLM_F_MULTI) || s_prividx == *prividx) 570410c9ead9SRoopa Prabhu nlmsg_cancel(skb, nlh); 5705e8872a25SNikolay Aleksandrov else 5706e8872a25SNikolay Aleksandrov nlmsg_end(skb, nlh); 570710c9ead9SRoopa Prabhu 5708216e6906SPetr Machata return err; 570910c9ead9SRoopa Prabhu } 571010c9ead9SRoopa Prabhu 571110c9ead9SRoopa Prabhu static size_t if_nlmsg_stats_size(const struct net_device *dev, 571246efc97bSPetr Machata const struct rtnl_stats_dump_filters *filters) 571310c9ead9SRoopa Prabhu { 5714d3436799SEric Dumazet size_t size = NLMSG_ALIGN(sizeof(struct if_stats_msg)); 571546efc97bSPetr Machata unsigned int filter_mask = filters->mask[0]; 571610c9ead9SRoopa Prabhu 5717e8872a25SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0)) 571810c9ead9SRoopa Prabhu size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64)); 571910c9ead9SRoopa Prabhu 572097a47facSNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) { 572197a47facSNikolay Aleksandrov const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 572280e73cc5SNikolay Aleksandrov int attr = IFLA_STATS_LINK_XSTATS; 572397a47facSNikolay Aleksandrov 572497a47facSNikolay Aleksandrov if (ops && ops->get_linkxstats_size) { 572580e73cc5SNikolay Aleksandrov size += nla_total_size(ops->get_linkxstats_size(dev, 572680e73cc5SNikolay Aleksandrov attr)); 572797a47facSNikolay Aleksandrov /* for IFLA_STATS_LINK_XSTATS */ 572897a47facSNikolay Aleksandrov size += nla_total_size(0); 572997a47facSNikolay Aleksandrov } 573097a47facSNikolay Aleksandrov } 573197a47facSNikolay Aleksandrov 573280e73cc5SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) { 573380e73cc5SNikolay Aleksandrov struct net_device *_dev = (struct net_device *)dev; 573480e73cc5SNikolay Aleksandrov const struct rtnl_link_ops *ops = NULL; 573580e73cc5SNikolay Aleksandrov const struct net_device *master; 573680e73cc5SNikolay Aleksandrov 573780e73cc5SNikolay Aleksandrov /* netdev_master_upper_dev_get can't take const */ 573880e73cc5SNikolay Aleksandrov master = netdev_master_upper_dev_get(_dev); 573980e73cc5SNikolay Aleksandrov if (master) 574080e73cc5SNikolay Aleksandrov ops = master->rtnl_link_ops; 574180e73cc5SNikolay Aleksandrov if (ops && ops->get_linkxstats_size) { 574280e73cc5SNikolay Aleksandrov int attr = IFLA_STATS_LINK_XSTATS_SLAVE; 574380e73cc5SNikolay Aleksandrov 574480e73cc5SNikolay Aleksandrov size += nla_total_size(ops->get_linkxstats_size(dev, 574580e73cc5SNikolay Aleksandrov attr)); 574680e73cc5SNikolay Aleksandrov /* for IFLA_STATS_LINK_XSTATS_SLAVE */ 574780e73cc5SNikolay Aleksandrov size += nla_total_size(0); 574880e73cc5SNikolay Aleksandrov } 574980e73cc5SNikolay Aleksandrov } 575080e73cc5SNikolay Aleksandrov 575146efc97bSPetr Machata if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0)) { 575246efc97bSPetr Machata u32 off_filter_mask; 575346efc97bSPetr Machata 575446efc97bSPetr Machata off_filter_mask = filters->mask[IFLA_STATS_LINK_OFFLOAD_XSTATS]; 575546efc97bSPetr Machata size += rtnl_offload_xstats_get_size(dev, off_filter_mask); 575646efc97bSPetr Machata } 575769ae6ad2SNogah Frankel 5758aefb4d4aSRobert Shearman if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) { 5759aefb4d4aSRobert Shearman struct rtnl_af_ops *af_ops; 5760aefb4d4aSRobert Shearman 5761aefb4d4aSRobert Shearman /* for IFLA_STATS_AF_SPEC */ 5762aefb4d4aSRobert Shearman size += nla_total_size(0); 5763aefb4d4aSRobert Shearman 57645fa85a09SFlorian Westphal rcu_read_lock(); 57655fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 5766aefb4d4aSRobert Shearman if (af_ops->get_stats_af_size) { 5767aefb4d4aSRobert Shearman size += nla_total_size( 5768aefb4d4aSRobert Shearman af_ops->get_stats_af_size(dev)); 5769aefb4d4aSRobert Shearman 5770aefb4d4aSRobert Shearman /* for AF_* */ 5771aefb4d4aSRobert Shearman size += nla_total_size(0); 5772aefb4d4aSRobert Shearman } 5773aefb4d4aSRobert Shearman } 57745fa85a09SFlorian Westphal rcu_read_unlock(); 5775aefb4d4aSRobert Shearman } 5776aefb4d4aSRobert Shearman 577710c9ead9SRoopa Prabhu return size; 577810c9ead9SRoopa Prabhu } 577910c9ead9SRoopa Prabhu 578046efc97bSPetr Machata #define RTNL_STATS_OFFLOAD_XSTATS_VALID ((1 << __IFLA_OFFLOAD_XSTATS_MAX) - 1) 578146efc97bSPetr Machata 578246efc97bSPetr Machata static const struct nla_policy 578346efc97bSPetr Machata rtnl_stats_get_policy_filters[IFLA_STATS_MAX + 1] = { 578446efc97bSPetr Machata [IFLA_STATS_LINK_OFFLOAD_XSTATS] = 578546efc97bSPetr Machata NLA_POLICY_MASK(NLA_U32, RTNL_STATS_OFFLOAD_XSTATS_VALID), 578646efc97bSPetr Machata }; 578746efc97bSPetr Machata 578846efc97bSPetr Machata static const struct nla_policy 578946efc97bSPetr Machata rtnl_stats_get_policy[IFLA_STATS_GETSET_MAX + 1] = { 579046efc97bSPetr Machata [IFLA_STATS_GET_FILTERS] = 579146efc97bSPetr Machata NLA_POLICY_NESTED(rtnl_stats_get_policy_filters), 579246efc97bSPetr Machata }; 579346efc97bSPetr Machata 579403ba3566SPetr Machata static const struct nla_policy 579503ba3566SPetr Machata ifla_stats_set_policy[IFLA_STATS_GETSET_MAX + 1] = { 57965fd0b838SPetr Machata [IFLA_STATS_SET_OFFLOAD_XSTATS_L3_STATS] = NLA_POLICY_MAX(NLA_U8, 1), 579703ba3566SPetr Machata }; 579803ba3566SPetr Machata 579946efc97bSPetr Machata static int rtnl_stats_get_parse_filters(struct nlattr *ifla_filters, 580046efc97bSPetr Machata struct rtnl_stats_dump_filters *filters, 580146efc97bSPetr Machata struct netlink_ext_ack *extack) 580246efc97bSPetr Machata { 580346efc97bSPetr Machata struct nlattr *tb[IFLA_STATS_MAX + 1]; 580446efc97bSPetr Machata int err; 580546efc97bSPetr Machata int at; 580646efc97bSPetr Machata 580746efc97bSPetr Machata err = nla_parse_nested(tb, IFLA_STATS_MAX, ifla_filters, 580846efc97bSPetr Machata rtnl_stats_get_policy_filters, extack); 580946efc97bSPetr Machata if (err < 0) 581046efc97bSPetr Machata return err; 581146efc97bSPetr Machata 581246efc97bSPetr Machata for (at = 1; at <= IFLA_STATS_MAX; at++) { 581346efc97bSPetr Machata if (tb[at]) { 581446efc97bSPetr Machata if (!(filters->mask[0] & IFLA_STATS_FILTER_BIT(at))) { 581546efc97bSPetr Machata NL_SET_ERR_MSG(extack, "Filtered attribute not enabled in filter_mask"); 581646efc97bSPetr Machata return -EINVAL; 581746efc97bSPetr Machata } 581846efc97bSPetr Machata filters->mask[at] = nla_get_u32(tb[at]); 581946efc97bSPetr Machata } 582046efc97bSPetr Machata } 582146efc97bSPetr Machata 582246efc97bSPetr Machata return 0; 582346efc97bSPetr Machata } 582446efc97bSPetr Machata 582546efc97bSPetr Machata static int rtnl_stats_get_parse(const struct nlmsghdr *nlh, 582646efc97bSPetr Machata u32 filter_mask, 582746efc97bSPetr Machata struct rtnl_stats_dump_filters *filters, 582846efc97bSPetr Machata struct netlink_ext_ack *extack) 582946efc97bSPetr Machata { 583046efc97bSPetr Machata struct nlattr *tb[IFLA_STATS_GETSET_MAX + 1]; 583146efc97bSPetr Machata int err; 583246efc97bSPetr Machata int i; 583346efc97bSPetr Machata 583446efc97bSPetr Machata filters->mask[0] = filter_mask; 583546efc97bSPetr Machata for (i = 1; i < ARRAY_SIZE(filters->mask); i++) 583646efc97bSPetr Machata filters->mask[i] = -1U; 583746efc97bSPetr Machata 583846efc97bSPetr Machata err = nlmsg_parse(nlh, sizeof(struct if_stats_msg), tb, 583946efc97bSPetr Machata IFLA_STATS_GETSET_MAX, rtnl_stats_get_policy, extack); 584046efc97bSPetr Machata if (err < 0) 584146efc97bSPetr Machata return err; 584246efc97bSPetr Machata 584346efc97bSPetr Machata if (tb[IFLA_STATS_GET_FILTERS]) { 584446efc97bSPetr Machata err = rtnl_stats_get_parse_filters(tb[IFLA_STATS_GET_FILTERS], 584546efc97bSPetr Machata filters, extack); 584646efc97bSPetr Machata if (err) 584746efc97bSPetr Machata return err; 584846efc97bSPetr Machata } 584946efc97bSPetr Machata 585046efc97bSPetr Machata return 0; 585146efc97bSPetr Machata } 585246efc97bSPetr Machata 585351bc860dSJakub Kicinski static int rtnl_valid_stats_req(const struct nlmsghdr *nlh, bool strict_check, 585451bc860dSJakub Kicinski bool is_dump, struct netlink_ext_ack *extack) 585551bc860dSJakub Kicinski { 585651bc860dSJakub Kicinski struct if_stats_msg *ifsm; 585751bc860dSJakub Kicinski 585869f23a09SEric Dumazet if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifsm))) { 585951bc860dSJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid header for stats dump"); 586051bc860dSJakub Kicinski return -EINVAL; 586151bc860dSJakub Kicinski } 586251bc860dSJakub Kicinski 586351bc860dSJakub Kicinski if (!strict_check) 586451bc860dSJakub Kicinski return 0; 586551bc860dSJakub Kicinski 586651bc860dSJakub Kicinski ifsm = nlmsg_data(nlh); 586751bc860dSJakub Kicinski 586851bc860dSJakub Kicinski /* only requests using strict checks can pass data to influence 586951bc860dSJakub Kicinski * the dump. The legacy exception is filter_mask. 587051bc860dSJakub Kicinski */ 587151bc860dSJakub Kicinski if (ifsm->pad1 || ifsm->pad2 || (is_dump && ifsm->ifindex)) { 587251bc860dSJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid values in header for stats dump request"); 587351bc860dSJakub Kicinski return -EINVAL; 587451bc860dSJakub Kicinski } 58756300acb2SJakub Kicinski if (ifsm->filter_mask >= IFLA_STATS_FILTER_BIT(IFLA_STATS_MAX + 1)) { 58766300acb2SJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid stats requested through filter mask"); 58776300acb2SJakub Kicinski return -EINVAL; 58786300acb2SJakub Kicinski } 587951bc860dSJakub Kicinski 588051bc860dSJakub Kicinski return 0; 588151bc860dSJakub Kicinski } 588251bc860dSJakub Kicinski 5883c21ef3e3SDavid Ahern static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh, 5884c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 588510c9ead9SRoopa Prabhu { 588646efc97bSPetr Machata struct rtnl_stats_dump_filters filters; 588710c9ead9SRoopa Prabhu struct net *net = sock_net(skb->sk); 588810c9ead9SRoopa Prabhu struct net_device *dev = NULL; 5889e8872a25SNikolay Aleksandrov int idxattr = 0, prividx = 0; 5890e8872a25SNikolay Aleksandrov struct if_stats_msg *ifsm; 589110c9ead9SRoopa Prabhu struct sk_buff *nskb; 589210c9ead9SRoopa Prabhu int err; 589310c9ead9SRoopa Prabhu 589451bc860dSJakub Kicinski err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb), 589551bc860dSJakub Kicinski false, extack); 589651bc860dSJakub Kicinski if (err) 589751bc860dSJakub Kicinski return err; 58984775cc1fSMathias Krause 589910c9ead9SRoopa Prabhu ifsm = nlmsg_data(nlh); 590010c9ead9SRoopa Prabhu if (ifsm->ifindex > 0) 590110c9ead9SRoopa Prabhu dev = __dev_get_by_index(net, ifsm->ifindex); 590210c9ead9SRoopa Prabhu else 590310c9ead9SRoopa Prabhu return -EINVAL; 590410c9ead9SRoopa Prabhu 590510c9ead9SRoopa Prabhu if (!dev) 590610c9ead9SRoopa Prabhu return -ENODEV; 590710c9ead9SRoopa Prabhu 590846efc97bSPetr Machata if (!ifsm->filter_mask) { 590922b67d17SPetr Machata NL_SET_ERR_MSG(extack, "Filter mask must be set for stats get"); 591010c9ead9SRoopa Prabhu return -EINVAL; 591122b67d17SPetr Machata } 591210c9ead9SRoopa Prabhu 591346efc97bSPetr Machata err = rtnl_stats_get_parse(nlh, ifsm->filter_mask, &filters, extack); 591446efc97bSPetr Machata if (err) 591546efc97bSPetr Machata return err; 591646efc97bSPetr Machata 591746efc97bSPetr Machata nskb = nlmsg_new(if_nlmsg_stats_size(dev, &filters), GFP_KERNEL); 591810c9ead9SRoopa Prabhu if (!nskb) 591910c9ead9SRoopa Prabhu return -ENOBUFS; 592010c9ead9SRoopa Prabhu 592110c9ead9SRoopa Prabhu err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS, 592210c9ead9SRoopa Prabhu NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0, 592305415bccSPetr Machata 0, &filters, &idxattr, &prividx, extack); 592410c9ead9SRoopa Prabhu if (err < 0) { 592510c9ead9SRoopa Prabhu /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */ 592610c9ead9SRoopa Prabhu WARN_ON(err == -EMSGSIZE); 592710c9ead9SRoopa Prabhu kfree_skb(nskb); 592810c9ead9SRoopa Prabhu } else { 592910c9ead9SRoopa Prabhu err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); 593010c9ead9SRoopa Prabhu } 593110c9ead9SRoopa Prabhu 593210c9ead9SRoopa Prabhu return err; 593310c9ead9SRoopa Prabhu } 593410c9ead9SRoopa Prabhu 593510c9ead9SRoopa Prabhu static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb) 593610c9ead9SRoopa Prabhu { 5937841891ecSDavid Ahern struct netlink_ext_ack *extack = cb->extack; 5938e8872a25SNikolay Aleksandrov int h, s_h, err, s_idx, s_idxattr, s_prividx; 593946efc97bSPetr Machata struct rtnl_stats_dump_filters filters; 594010c9ead9SRoopa Prabhu struct net *net = sock_net(skb->sk); 594110c9ead9SRoopa Prabhu unsigned int flags = NLM_F_MULTI; 5942e8872a25SNikolay Aleksandrov struct if_stats_msg *ifsm; 5943e8872a25SNikolay Aleksandrov struct hlist_head *head; 5944e8872a25SNikolay Aleksandrov struct net_device *dev; 5945e8872a25SNikolay Aleksandrov int idx = 0; 594610c9ead9SRoopa Prabhu 594710c9ead9SRoopa Prabhu s_h = cb->args[0]; 594810c9ead9SRoopa Prabhu s_idx = cb->args[1]; 5949e8872a25SNikolay Aleksandrov s_idxattr = cb->args[2]; 5950e8872a25SNikolay Aleksandrov s_prividx = cb->args[3]; 595110c9ead9SRoopa Prabhu 595210c9ead9SRoopa Prabhu cb->seq = net->dev_base_seq; 595310c9ead9SRoopa Prabhu 595451bc860dSJakub Kicinski err = rtnl_valid_stats_req(cb->nlh, cb->strict_check, true, extack); 595551bc860dSJakub Kicinski if (err) 595651bc860dSJakub Kicinski return err; 59574775cc1fSMathias Krause 595810c9ead9SRoopa Prabhu ifsm = nlmsg_data(cb->nlh); 595946efc97bSPetr Machata if (!ifsm->filter_mask) { 5960841891ecSDavid Ahern NL_SET_ERR_MSG(extack, "Filter mask must be set for stats dump"); 5961841891ecSDavid Ahern return -EINVAL; 5962841891ecSDavid Ahern } 596310c9ead9SRoopa Prabhu 596446efc97bSPetr Machata err = rtnl_stats_get_parse(cb->nlh, ifsm->filter_mask, &filters, 596546efc97bSPetr Machata extack); 596646efc97bSPetr Machata if (err) 596746efc97bSPetr Machata return err; 596846efc97bSPetr Machata 596910c9ead9SRoopa Prabhu for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 597010c9ead9SRoopa Prabhu idx = 0; 597110c9ead9SRoopa Prabhu head = &net->dev_index_head[h]; 597210c9ead9SRoopa Prabhu hlist_for_each_entry(dev, head, index_hlist) { 597310c9ead9SRoopa Prabhu if (idx < s_idx) 597410c9ead9SRoopa Prabhu goto cont; 597510c9ead9SRoopa Prabhu err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS, 597610c9ead9SRoopa Prabhu NETLINK_CB(cb->skb).portid, 597710c9ead9SRoopa Prabhu cb->nlh->nlmsg_seq, 0, 597846efc97bSPetr Machata flags, &filters, 597905415bccSPetr Machata &s_idxattr, &s_prividx, 598005415bccSPetr Machata extack); 598110c9ead9SRoopa Prabhu /* If we ran out of room on the first message, 598210c9ead9SRoopa Prabhu * we're in trouble 598310c9ead9SRoopa Prabhu */ 598410c9ead9SRoopa Prabhu WARN_ON((err == -EMSGSIZE) && (skb->len == 0)); 598510c9ead9SRoopa Prabhu 598610c9ead9SRoopa Prabhu if (err < 0) 598710c9ead9SRoopa Prabhu goto out; 5988e8872a25SNikolay Aleksandrov s_prividx = 0; 5989e8872a25SNikolay Aleksandrov s_idxattr = 0; 599010c9ead9SRoopa Prabhu nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 599110c9ead9SRoopa Prabhu cont: 599210c9ead9SRoopa Prabhu idx++; 599310c9ead9SRoopa Prabhu } 599410c9ead9SRoopa Prabhu } 599510c9ead9SRoopa Prabhu out: 5996e8872a25SNikolay Aleksandrov cb->args[3] = s_prividx; 5997e8872a25SNikolay Aleksandrov cb->args[2] = s_idxattr; 599810c9ead9SRoopa Prabhu cb->args[1] = idx; 599910c9ead9SRoopa Prabhu cb->args[0] = h; 600010c9ead9SRoopa Prabhu 600110c9ead9SRoopa Prabhu return skb->len; 600210c9ead9SRoopa Prabhu } 600310c9ead9SRoopa Prabhu 60045fd0b838SPetr Machata void rtnl_offload_xstats_notify(struct net_device *dev) 60055fd0b838SPetr Machata { 60065fd0b838SPetr Machata struct rtnl_stats_dump_filters response_filters = {}; 60075fd0b838SPetr Machata struct net *net = dev_net(dev); 60085fd0b838SPetr Machata int idxattr = 0, prividx = 0; 60095fd0b838SPetr Machata struct sk_buff *skb; 60105fd0b838SPetr Machata int err = -ENOBUFS; 60115fd0b838SPetr Machata 60125fd0b838SPetr Machata ASSERT_RTNL(); 60135fd0b838SPetr Machata 60145fd0b838SPetr Machata response_filters.mask[0] |= 60155fd0b838SPetr Machata IFLA_STATS_FILTER_BIT(IFLA_STATS_LINK_OFFLOAD_XSTATS); 60165fd0b838SPetr Machata response_filters.mask[IFLA_STATS_LINK_OFFLOAD_XSTATS] |= 60175fd0b838SPetr Machata IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_HW_S_INFO); 60185fd0b838SPetr Machata 60195fd0b838SPetr Machata skb = nlmsg_new(if_nlmsg_stats_size(dev, &response_filters), 60205fd0b838SPetr Machata GFP_KERNEL); 60215fd0b838SPetr Machata if (!skb) 60225fd0b838SPetr Machata goto errout; 60235fd0b838SPetr Machata 60245fd0b838SPetr Machata err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS, 0, 0, 0, 0, 60255fd0b838SPetr Machata &response_filters, &idxattr, &prividx, NULL); 60265fd0b838SPetr Machata if (err < 0) { 60275fd0b838SPetr Machata kfree_skb(skb); 60285fd0b838SPetr Machata goto errout; 60295fd0b838SPetr Machata } 60305fd0b838SPetr Machata 60315fd0b838SPetr Machata rtnl_notify(skb, net, 0, RTNLGRP_STATS, NULL, GFP_KERNEL); 60325fd0b838SPetr Machata return; 60335fd0b838SPetr Machata 60345fd0b838SPetr Machata errout: 60355fd0b838SPetr Machata rtnl_set_sk_err(net, RTNLGRP_STATS, err); 60365fd0b838SPetr Machata } 60375fd0b838SPetr Machata EXPORT_SYMBOL(rtnl_offload_xstats_notify); 60385fd0b838SPetr Machata 603903ba3566SPetr Machata static int rtnl_stats_set(struct sk_buff *skb, struct nlmsghdr *nlh, 604003ba3566SPetr Machata struct netlink_ext_ack *extack) 604103ba3566SPetr Machata { 60425fd0b838SPetr Machata enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3; 604303ba3566SPetr Machata struct rtnl_stats_dump_filters response_filters = {}; 604403ba3566SPetr Machata struct nlattr *tb[IFLA_STATS_GETSET_MAX + 1]; 604503ba3566SPetr Machata struct net *net = sock_net(skb->sk); 604603ba3566SPetr Machata struct net_device *dev = NULL; 604703ba3566SPetr Machata struct if_stats_msg *ifsm; 60485fd0b838SPetr Machata bool notify = false; 604903ba3566SPetr Machata int err; 605003ba3566SPetr Machata 605103ba3566SPetr Machata err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb), 605203ba3566SPetr Machata false, extack); 605303ba3566SPetr Machata if (err) 605403ba3566SPetr Machata return err; 605503ba3566SPetr Machata 605603ba3566SPetr Machata ifsm = nlmsg_data(nlh); 605703ba3566SPetr Machata if (ifsm->family != AF_UNSPEC) { 605803ba3566SPetr Machata NL_SET_ERR_MSG(extack, "Address family should be AF_UNSPEC"); 605903ba3566SPetr Machata return -EINVAL; 606003ba3566SPetr Machata } 606103ba3566SPetr Machata 606203ba3566SPetr Machata if (ifsm->ifindex > 0) 606303ba3566SPetr Machata dev = __dev_get_by_index(net, ifsm->ifindex); 606403ba3566SPetr Machata else 606503ba3566SPetr Machata return -EINVAL; 606603ba3566SPetr Machata 606703ba3566SPetr Machata if (!dev) 606803ba3566SPetr Machata return -ENODEV; 606903ba3566SPetr Machata 607003ba3566SPetr Machata if (ifsm->filter_mask) { 607103ba3566SPetr Machata NL_SET_ERR_MSG(extack, "Filter mask must be 0 for stats set"); 607203ba3566SPetr Machata return -EINVAL; 607303ba3566SPetr Machata } 607403ba3566SPetr Machata 607503ba3566SPetr Machata err = nlmsg_parse(nlh, sizeof(*ifsm), tb, IFLA_STATS_GETSET_MAX, 607603ba3566SPetr Machata ifla_stats_set_policy, extack); 607703ba3566SPetr Machata if (err < 0) 607803ba3566SPetr Machata return err; 607903ba3566SPetr Machata 60805fd0b838SPetr Machata if (tb[IFLA_STATS_SET_OFFLOAD_XSTATS_L3_STATS]) { 60815fd0b838SPetr Machata u8 req = nla_get_u8(tb[IFLA_STATS_SET_OFFLOAD_XSTATS_L3_STATS]); 608203ba3566SPetr Machata 60835fd0b838SPetr Machata if (req) 60845fd0b838SPetr Machata err = netdev_offload_xstats_enable(dev, t_l3, extack); 60855fd0b838SPetr Machata else 60865fd0b838SPetr Machata err = netdev_offload_xstats_disable(dev, t_l3); 60875fd0b838SPetr Machata 60885fd0b838SPetr Machata if (!err) 60895fd0b838SPetr Machata notify = true; 60905fd0b838SPetr Machata else if (err != -EALREADY) 60915fd0b838SPetr Machata return err; 60925fd0b838SPetr Machata 60935fd0b838SPetr Machata response_filters.mask[0] |= 60945fd0b838SPetr Machata IFLA_STATS_FILTER_BIT(IFLA_STATS_LINK_OFFLOAD_XSTATS); 60955fd0b838SPetr Machata response_filters.mask[IFLA_STATS_LINK_OFFLOAD_XSTATS] |= 60965fd0b838SPetr Machata IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_HW_S_INFO); 609703ba3566SPetr Machata } 609803ba3566SPetr Machata 60995fd0b838SPetr Machata if (notify) 61005fd0b838SPetr Machata rtnl_offload_xstats_notify(dev); 61015fd0b838SPetr Machata 61025fd0b838SPetr Machata return 0; 610303ba3566SPetr Machata } 610403ba3566SPetr Machata 6105cc7f5022SIdo Schimmel static int rtnl_mdb_valid_dump_req(const struct nlmsghdr *nlh, 6106cc7f5022SIdo Schimmel struct netlink_ext_ack *extack) 6107cc7f5022SIdo Schimmel { 6108cc7f5022SIdo Schimmel struct br_port_msg *bpm; 6109cc7f5022SIdo Schimmel 6110cc7f5022SIdo Schimmel if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*bpm))) { 6111cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Invalid header for mdb dump request"); 6112cc7f5022SIdo Schimmel return -EINVAL; 6113cc7f5022SIdo Schimmel } 6114cc7f5022SIdo Schimmel 6115cc7f5022SIdo Schimmel bpm = nlmsg_data(nlh); 6116cc7f5022SIdo Schimmel if (bpm->ifindex) { 6117cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Filtering by device index is not supported for mdb dump request"); 6118cc7f5022SIdo Schimmel return -EINVAL; 6119cc7f5022SIdo Schimmel } 6120cc7f5022SIdo Schimmel if (nlmsg_attrlen(nlh, sizeof(*bpm))) { 6121cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Invalid data after header in mdb dump request"); 6122cc7f5022SIdo Schimmel return -EINVAL; 6123cc7f5022SIdo Schimmel } 6124cc7f5022SIdo Schimmel 6125cc7f5022SIdo Schimmel return 0; 6126cc7f5022SIdo Schimmel } 6127cc7f5022SIdo Schimmel 6128cc7f5022SIdo Schimmel struct rtnl_mdb_dump_ctx { 6129cc7f5022SIdo Schimmel long idx; 6130cc7f5022SIdo Schimmel }; 6131cc7f5022SIdo Schimmel 6132cc7f5022SIdo Schimmel static int rtnl_mdb_dump(struct sk_buff *skb, struct netlink_callback *cb) 6133cc7f5022SIdo Schimmel { 6134cc7f5022SIdo Schimmel struct rtnl_mdb_dump_ctx *ctx = (void *)cb->ctx; 6135cc7f5022SIdo Schimmel struct net *net = sock_net(skb->sk); 6136cc7f5022SIdo Schimmel struct net_device *dev; 6137cc7f5022SIdo Schimmel int idx, s_idx; 6138cc7f5022SIdo Schimmel int err; 6139cc7f5022SIdo Schimmel 6140cc7f5022SIdo Schimmel NL_ASSERT_DUMP_CTX_FITS(struct rtnl_mdb_dump_ctx); 6141cc7f5022SIdo Schimmel 6142cc7f5022SIdo Schimmel if (cb->strict_check) { 6143cc7f5022SIdo Schimmel err = rtnl_mdb_valid_dump_req(cb->nlh, cb->extack); 6144cc7f5022SIdo Schimmel if (err) 6145cc7f5022SIdo Schimmel return err; 6146cc7f5022SIdo Schimmel } 6147cc7f5022SIdo Schimmel 6148cc7f5022SIdo Schimmel s_idx = ctx->idx; 6149cc7f5022SIdo Schimmel idx = 0; 6150cc7f5022SIdo Schimmel 6151cc7f5022SIdo Schimmel for_each_netdev(net, dev) { 6152cc7f5022SIdo Schimmel if (idx < s_idx) 6153cc7f5022SIdo Schimmel goto skip; 6154cc7f5022SIdo Schimmel if (!dev->netdev_ops->ndo_mdb_dump) 6155cc7f5022SIdo Schimmel goto skip; 6156cc7f5022SIdo Schimmel 6157cc7f5022SIdo Schimmel err = dev->netdev_ops->ndo_mdb_dump(dev, skb, cb); 6158cc7f5022SIdo Schimmel if (err == -EMSGSIZE) 6159cc7f5022SIdo Schimmel goto out; 6160cc7f5022SIdo Schimmel /* Moving on to next device, reset markers and sequence 6161cc7f5022SIdo Schimmel * counters since they are all maintained per-device. 6162cc7f5022SIdo Schimmel */ 6163cc7f5022SIdo Schimmel memset(cb->ctx, 0, sizeof(cb->ctx)); 6164cc7f5022SIdo Schimmel cb->prev_seq = 0; 6165cc7f5022SIdo Schimmel cb->seq = 0; 6166cc7f5022SIdo Schimmel skip: 6167cc7f5022SIdo Schimmel idx++; 6168cc7f5022SIdo Schimmel } 6169cc7f5022SIdo Schimmel 6170cc7f5022SIdo Schimmel out: 6171cc7f5022SIdo Schimmel ctx->idx = idx; 6172cc7f5022SIdo Schimmel return skb->len; 6173cc7f5022SIdo Schimmel } 6174cc7f5022SIdo Schimmel 6175cc7f5022SIdo Schimmel static int rtnl_validate_mdb_entry(const struct nlattr *attr, 6176cc7f5022SIdo Schimmel struct netlink_ext_ack *extack) 6177cc7f5022SIdo Schimmel { 6178cc7f5022SIdo Schimmel struct br_mdb_entry *entry = nla_data(attr); 6179cc7f5022SIdo Schimmel 6180cc7f5022SIdo Schimmel if (nla_len(attr) != sizeof(struct br_mdb_entry)) { 6181cc7f5022SIdo Schimmel NL_SET_ERR_MSG_ATTR(extack, attr, "Invalid attribute length"); 6182cc7f5022SIdo Schimmel return -EINVAL; 6183cc7f5022SIdo Schimmel } 6184cc7f5022SIdo Schimmel 6185cc7f5022SIdo Schimmel if (entry->ifindex == 0) { 6186cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Zero entry ifindex is not allowed"); 6187cc7f5022SIdo Schimmel return -EINVAL; 6188cc7f5022SIdo Schimmel } 6189cc7f5022SIdo Schimmel 6190cc7f5022SIdo Schimmel if (entry->addr.proto == htons(ETH_P_IP)) { 6191da654c80SIdo Schimmel if (!ipv4_is_multicast(entry->addr.u.ip4) && 6192da654c80SIdo Schimmel !ipv4_is_zeronet(entry->addr.u.ip4)) { 6193da654c80SIdo Schimmel NL_SET_ERR_MSG(extack, "IPv4 entry group address is not multicast or 0.0.0.0"); 6194cc7f5022SIdo Schimmel return -EINVAL; 6195cc7f5022SIdo Schimmel } 6196cc7f5022SIdo Schimmel if (ipv4_is_local_multicast(entry->addr.u.ip4)) { 6197cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "IPv4 entry group address is local multicast"); 6198cc7f5022SIdo Schimmel return -EINVAL; 6199cc7f5022SIdo Schimmel } 6200cc7f5022SIdo Schimmel #if IS_ENABLED(CONFIG_IPV6) 6201cc7f5022SIdo Schimmel } else if (entry->addr.proto == htons(ETH_P_IPV6)) { 6202cc7f5022SIdo Schimmel if (ipv6_addr_is_ll_all_nodes(&entry->addr.u.ip6)) { 6203cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "IPv6 entry group address is link-local all nodes"); 6204cc7f5022SIdo Schimmel return -EINVAL; 6205cc7f5022SIdo Schimmel } 6206cc7f5022SIdo Schimmel #endif 6207cc7f5022SIdo Schimmel } else if (entry->addr.proto == 0) { 6208cc7f5022SIdo Schimmel /* L2 mdb */ 6209cc7f5022SIdo Schimmel if (!is_multicast_ether_addr(entry->addr.u.mac_addr)) { 6210cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "L2 entry group is not multicast"); 6211cc7f5022SIdo Schimmel return -EINVAL; 6212cc7f5022SIdo Schimmel } 6213cc7f5022SIdo Schimmel } else { 6214cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Unknown entry protocol"); 6215cc7f5022SIdo Schimmel return -EINVAL; 6216cc7f5022SIdo Schimmel } 6217cc7f5022SIdo Schimmel 6218cc7f5022SIdo Schimmel if (entry->state != MDB_PERMANENT && entry->state != MDB_TEMPORARY) { 6219cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Unknown entry state"); 6220cc7f5022SIdo Schimmel return -EINVAL; 6221cc7f5022SIdo Schimmel } 6222cc7f5022SIdo Schimmel if (entry->vid >= VLAN_VID_MASK) { 6223cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Invalid entry VLAN id"); 6224cc7f5022SIdo Schimmel return -EINVAL; 6225cc7f5022SIdo Schimmel } 6226cc7f5022SIdo Schimmel 6227cc7f5022SIdo Schimmel return 0; 6228cc7f5022SIdo Schimmel } 6229cc7f5022SIdo Schimmel 6230cc7f5022SIdo Schimmel static const struct nla_policy mdba_policy[MDBA_SET_ENTRY_MAX + 1] = { 6231cc7f5022SIdo Schimmel [MDBA_SET_ENTRY_UNSPEC] = { .strict_start_type = MDBA_SET_ENTRY_ATTRS + 1 }, 6232cc7f5022SIdo Schimmel [MDBA_SET_ENTRY] = NLA_POLICY_VALIDATE_FN(NLA_BINARY, 6233cc7f5022SIdo Schimmel rtnl_validate_mdb_entry, 6234cc7f5022SIdo Schimmel sizeof(struct br_mdb_entry)), 6235cc7f5022SIdo Schimmel [MDBA_SET_ENTRY_ATTRS] = { .type = NLA_NESTED }, 6236cc7f5022SIdo Schimmel }; 6237cc7f5022SIdo Schimmel 6238cc7f5022SIdo Schimmel static int rtnl_mdb_add(struct sk_buff *skb, struct nlmsghdr *nlh, 6239cc7f5022SIdo Schimmel struct netlink_ext_ack *extack) 6240cc7f5022SIdo Schimmel { 6241cc7f5022SIdo Schimmel struct nlattr *tb[MDBA_SET_ENTRY_MAX + 1]; 6242cc7f5022SIdo Schimmel struct net *net = sock_net(skb->sk); 6243cc7f5022SIdo Schimmel struct br_port_msg *bpm; 6244cc7f5022SIdo Schimmel struct net_device *dev; 6245cc7f5022SIdo Schimmel int err; 6246cc7f5022SIdo Schimmel 6247cc7f5022SIdo Schimmel err = nlmsg_parse_deprecated(nlh, sizeof(*bpm), tb, 6248cc7f5022SIdo Schimmel MDBA_SET_ENTRY_MAX, mdba_policy, extack); 6249cc7f5022SIdo Schimmel if (err) 6250cc7f5022SIdo Schimmel return err; 6251cc7f5022SIdo Schimmel 6252cc7f5022SIdo Schimmel bpm = nlmsg_data(nlh); 6253cc7f5022SIdo Schimmel if (!bpm->ifindex) { 6254cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Invalid ifindex"); 6255cc7f5022SIdo Schimmel return -EINVAL; 6256cc7f5022SIdo Schimmel } 6257cc7f5022SIdo Schimmel 6258cc7f5022SIdo Schimmel dev = __dev_get_by_index(net, bpm->ifindex); 6259cc7f5022SIdo Schimmel if (!dev) { 6260cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Device doesn't exist"); 6261cc7f5022SIdo Schimmel return -ENODEV; 6262cc7f5022SIdo Schimmel } 6263cc7f5022SIdo Schimmel 6264cc7f5022SIdo Schimmel if (NL_REQ_ATTR_CHECK(extack, NULL, tb, MDBA_SET_ENTRY)) { 6265cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Missing MDBA_SET_ENTRY attribute"); 6266cc7f5022SIdo Schimmel return -EINVAL; 6267cc7f5022SIdo Schimmel } 6268cc7f5022SIdo Schimmel 6269cc7f5022SIdo Schimmel if (!dev->netdev_ops->ndo_mdb_add) { 6270cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Device does not support MDB operations"); 6271cc7f5022SIdo Schimmel return -EOPNOTSUPP; 6272cc7f5022SIdo Schimmel } 6273cc7f5022SIdo Schimmel 6274cc7f5022SIdo Schimmel return dev->netdev_ops->ndo_mdb_add(dev, tb, nlh->nlmsg_flags, extack); 6275cc7f5022SIdo Schimmel } 6276cc7f5022SIdo Schimmel 6277cc7f5022SIdo Schimmel static int rtnl_mdb_del(struct sk_buff *skb, struct nlmsghdr *nlh, 6278cc7f5022SIdo Schimmel struct netlink_ext_ack *extack) 6279cc7f5022SIdo Schimmel { 6280cc7f5022SIdo Schimmel struct nlattr *tb[MDBA_SET_ENTRY_MAX + 1]; 6281cc7f5022SIdo Schimmel struct net *net = sock_net(skb->sk); 6282cc7f5022SIdo Schimmel struct br_port_msg *bpm; 6283cc7f5022SIdo Schimmel struct net_device *dev; 6284cc7f5022SIdo Schimmel int err; 6285cc7f5022SIdo Schimmel 6286cc7f5022SIdo Schimmel err = nlmsg_parse_deprecated(nlh, sizeof(*bpm), tb, 6287cc7f5022SIdo Schimmel MDBA_SET_ENTRY_MAX, mdba_policy, extack); 6288cc7f5022SIdo Schimmel if (err) 6289cc7f5022SIdo Schimmel return err; 6290cc7f5022SIdo Schimmel 6291cc7f5022SIdo Schimmel bpm = nlmsg_data(nlh); 6292cc7f5022SIdo Schimmel if (!bpm->ifindex) { 6293cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Invalid ifindex"); 6294cc7f5022SIdo Schimmel return -EINVAL; 6295cc7f5022SIdo Schimmel } 6296cc7f5022SIdo Schimmel 6297cc7f5022SIdo Schimmel dev = __dev_get_by_index(net, bpm->ifindex); 6298cc7f5022SIdo Schimmel if (!dev) { 6299cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Device doesn't exist"); 6300cc7f5022SIdo Schimmel return -ENODEV; 6301cc7f5022SIdo Schimmel } 6302cc7f5022SIdo Schimmel 6303cc7f5022SIdo Schimmel if (NL_REQ_ATTR_CHECK(extack, NULL, tb, MDBA_SET_ENTRY)) { 6304cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Missing MDBA_SET_ENTRY attribute"); 6305cc7f5022SIdo Schimmel return -EINVAL; 6306cc7f5022SIdo Schimmel } 6307cc7f5022SIdo Schimmel 6308cc7f5022SIdo Schimmel if (!dev->netdev_ops->ndo_mdb_del) { 6309cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Device does not support MDB operations"); 6310cc7f5022SIdo Schimmel return -EOPNOTSUPP; 6311cc7f5022SIdo Schimmel } 6312cc7f5022SIdo Schimmel 6313cc7f5022SIdo Schimmel return dev->netdev_ops->ndo_mdb_del(dev, tb, extack); 6314cc7f5022SIdo Schimmel } 6315cc7f5022SIdo Schimmel 63161da177e4SLinus Torvalds /* Process one rtnetlink message. */ 63171da177e4SLinus Torvalds 63182d4bc933SJohannes Berg static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, 63192d4bc933SJohannes Berg struct netlink_ext_ack *extack) 63201da177e4SLinus Torvalds { 63213b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 6322addf9b90SFlorian Westphal struct rtnl_link *link; 632312dc5c2cSNikolay Aleksandrov enum rtnl_kinds kind; 6324e4202511SFlorian Westphal struct module *owner; 63256853dd48SFlorian Westphal int err = -EOPNOTSUPP; 6326e2849863SThomas Graf rtnl_doit_func doit; 632762256f98SFlorian Westphal unsigned int flags; 63281da177e4SLinus Torvalds int family; 63291da177e4SLinus Torvalds int type; 63301da177e4SLinus Torvalds 63311da177e4SLinus Torvalds type = nlh->nlmsg_type; 63321da177e4SLinus Torvalds if (type > RTM_MAX) 6333038890feSThomas Graf return -EOPNOTSUPP; 63341da177e4SLinus Torvalds 63351da177e4SLinus Torvalds type -= RTM_BASE; 63361da177e4SLinus Torvalds 63371da177e4SLinus Torvalds /* All the messages must have at least 1 byte length */ 6338573ce260SHong zhi guo if (nlmsg_len(nlh) < sizeof(struct rtgenmsg)) 63391da177e4SLinus Torvalds return 0; 63401da177e4SLinus Torvalds 6341573ce260SHong zhi guo family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family; 63422e9ea3e3SNikolay Aleksandrov kind = rtnl_msgtype_kind(type); 63431da177e4SLinus Torvalds 634412dc5c2cSNikolay Aleksandrov if (kind != RTNL_KIND_GET && !netlink_net_capable(skb, CAP_NET_ADMIN)) 63451d00a4ebSThomas Graf return -EPERM; 63461da177e4SLinus Torvalds 63476853dd48SFlorian Westphal rcu_read_lock(); 634812dc5c2cSNikolay Aleksandrov if (kind == RTNL_KIND_GET && (nlh->nlmsg_flags & NLM_F_DUMP)) { 634997c53cacSDenis V. Lunev struct sock *rtnl; 6350e2849863SThomas Graf rtnl_dumpit_func dumpit; 6351ebfe3c51SDi Zhu u32 min_dump_alloc = 0; 63521da177e4SLinus Torvalds 6353addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 6354addf9b90SFlorian Westphal if (!link || !link->dumpit) { 63556853dd48SFlorian Westphal family = PF_UNSPEC; 6356addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 6357addf9b90SFlorian Westphal if (!link || !link->dumpit) 63586853dd48SFlorian Westphal goto err_unlock; 63596853dd48SFlorian Westphal } 6360e4202511SFlorian Westphal owner = link->owner; 6361addf9b90SFlorian Westphal dumpit = link->dumpit; 63626853dd48SFlorian Westphal 63635c2bb9b6SFlorian Westphal if (type == RTM_GETLINK - RTM_BASE) 6364e1fa6d21SFlorian Westphal min_dump_alloc = rtnl_calcit(skb, nlh); 63651da177e4SLinus Torvalds 6366e4202511SFlorian Westphal err = 0; 6367e4202511SFlorian Westphal /* need to do this before rcu_read_unlock() */ 6368e4202511SFlorian Westphal if (!try_module_get(owner)) 6369e4202511SFlorian Westphal err = -EPROTONOSUPPORT; 6370e4202511SFlorian Westphal 63716853dd48SFlorian Westphal rcu_read_unlock(); 63726853dd48SFlorian Westphal 637397c53cacSDenis V. Lunev rtnl = net->rtnl; 6374e4202511SFlorian Westphal if (err == 0) { 637580d326faSPablo Neira Ayuso struct netlink_dump_control c = { 637680d326faSPablo Neira Ayuso .dump = dumpit, 637780d326faSPablo Neira Ayuso .min_dump_alloc = min_dump_alloc, 6378e4202511SFlorian Westphal .module = owner, 637980d326faSPablo Neira Ayuso }; 638080d326faSPablo Neira Ayuso err = netlink_dump_start(rtnl, skb, nlh, &c); 6381e4202511SFlorian Westphal /* netlink_dump_start() will keep a reference on 6382e4202511SFlorian Westphal * module if dump is still in progress. 6383e4202511SFlorian Westphal */ 6384e4202511SFlorian Westphal module_put(owner); 638580d326faSPablo Neira Ayuso } 63862907c35fSEric Dumazet return err; 63871da177e4SLinus Torvalds } 63881da177e4SLinus Torvalds 6389addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 6390addf9b90SFlorian Westphal if (!link || !link->doit) { 63918caa38b5SFlorian Westphal family = PF_UNSPEC; 6392addf9b90SFlorian Westphal link = rtnl_get_link(PF_UNSPEC, type); 6393addf9b90SFlorian Westphal if (!link || !link->doit) 6394addf9b90SFlorian Westphal goto out_unlock; 63958caa38b5SFlorian Westphal } 63968caa38b5SFlorian Westphal 6397e4202511SFlorian Westphal owner = link->owner; 6398e4202511SFlorian Westphal if (!try_module_get(owner)) { 6399e4202511SFlorian Westphal err = -EPROTONOSUPPORT; 6400e4202511SFlorian Westphal goto out_unlock; 6401e4202511SFlorian Westphal } 6402e4202511SFlorian Westphal 6403addf9b90SFlorian Westphal flags = link->flags; 6404a6cec0bcSNikolay Aleksandrov if (kind == RTNL_KIND_DEL && (nlh->nlmsg_flags & NLM_F_BULK) && 6405a6cec0bcSNikolay Aleksandrov !(flags & RTNL_FLAG_BULK_DEL_SUPPORTED)) { 6406a6cec0bcSNikolay Aleksandrov NL_SET_ERR_MSG(extack, "Bulk delete is not supported"); 64075b22f627SZhengchao Shao module_put(owner); 6408a6cec0bcSNikolay Aleksandrov goto err_unlock; 6409a6cec0bcSNikolay Aleksandrov } 6410a6cec0bcSNikolay Aleksandrov 641162256f98SFlorian Westphal if (flags & RTNL_FLAG_DOIT_UNLOCKED) { 6412addf9b90SFlorian Westphal doit = link->doit; 641362256f98SFlorian Westphal rcu_read_unlock(); 641462256f98SFlorian Westphal if (doit) 641562256f98SFlorian Westphal err = doit(skb, nlh, extack); 6416e4202511SFlorian Westphal module_put(owner); 641762256f98SFlorian Westphal return err; 641862256f98SFlorian Westphal } 64196853dd48SFlorian Westphal rcu_read_unlock(); 64206853dd48SFlorian Westphal 64210cc09020SFlorian Westphal rtnl_lock(); 6422addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 6423addf9b90SFlorian Westphal if (link && link->doit) 6424addf9b90SFlorian Westphal err = link->doit(skb, nlh, extack); 64250cc09020SFlorian Westphal rtnl_unlock(); 6426addf9b90SFlorian Westphal 6427e4202511SFlorian Westphal module_put(owner); 6428e4202511SFlorian Westphal 6429addf9b90SFlorian Westphal return err; 6430addf9b90SFlorian Westphal 6431addf9b90SFlorian Westphal out_unlock: 6432addf9b90SFlorian Westphal rcu_read_unlock(); 64330cc09020SFlorian Westphal return err; 64340cc09020SFlorian Westphal 64350cc09020SFlorian Westphal err_unlock: 64366853dd48SFlorian Westphal rcu_read_unlock(); 64370cc09020SFlorian Westphal return -EOPNOTSUPP; 64381da177e4SLinus Torvalds } 64391da177e4SLinus Torvalds 6440cd40b7d3SDenis V. Lunev static void rtnetlink_rcv(struct sk_buff *skb) 64411da177e4SLinus Torvalds { 6442cd40b7d3SDenis V. Lunev netlink_rcv_skb(skb, &rtnetlink_rcv_msg); 64431da177e4SLinus Torvalds } 64441da177e4SLinus Torvalds 64455f729eaaSJulien Gomes static int rtnetlink_bind(struct net *net, int group) 64465f729eaaSJulien Gomes { 64475f729eaaSJulien Gomes switch (group) { 64485f729eaaSJulien Gomes case RTNLGRP_IPV4_MROUTE_R: 64495f729eaaSJulien Gomes case RTNLGRP_IPV6_MROUTE_R: 64505f729eaaSJulien Gomes if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 64515f729eaaSJulien Gomes return -EPERM; 64525f729eaaSJulien Gomes break; 64535f729eaaSJulien Gomes } 64545f729eaaSJulien Gomes return 0; 64555f729eaaSJulien Gomes } 64565f729eaaSJulien Gomes 64571da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr) 64581da177e4SLinus Torvalds { 6459351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 6460e9dc8653SEric W. Biederman 64611da177e4SLinus Torvalds switch (event) { 64625138e86fSVlad Yasevich case NETDEV_REBOOT: 64638a212589SXin Long case NETDEV_CHANGEMTU: 64643753654eSDavid Ahern case NETDEV_CHANGEADDR: 64655138e86fSVlad Yasevich case NETDEV_CHANGENAME: 64665138e86fSVlad Yasevich case NETDEV_FEAT_CHANGE: 64675138e86fSVlad Yasevich case NETDEV_BONDING_FAILOVER: 6468e6e66594SXin Long case NETDEV_POST_TYPE_CHANGE: 64695138e86fSVlad Yasevich case NETDEV_NOTIFY_PEERS: 6470dc709f37SXin Long case NETDEV_CHANGEUPPER: 64715138e86fSVlad Yasevich case NETDEV_RESEND_IGMP: 64725138e86fSVlad Yasevich case NETDEV_CHANGEINFODATA: 6473eeda3fb9SXin Long case NETDEV_CHANGELOWERSTATE: 6474ebdcf045SXin Long case NETDEV_CHANGE_TX_QUEUE_LEN: 64753d3ea5afSVlad Yasevich rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event), 64761d997f10SHangbin Liu GFP_KERNEL, NULL, 0, 0, NULL); 64771da177e4SLinus Torvalds break; 64781da177e4SLinus Torvalds default: 64791da177e4SLinus Torvalds break; 64801da177e4SLinus Torvalds } 64811da177e4SLinus Torvalds return NOTIFY_DONE; 64821da177e4SLinus Torvalds } 64831da177e4SLinus Torvalds 64841da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = { 64851da177e4SLinus Torvalds .notifier_call = rtnetlink_event, 64861da177e4SLinus Torvalds }; 64871da177e4SLinus Torvalds 648897c53cacSDenis V. Lunev 64892c8c1e72SAlexey Dobriyan static int __net_init rtnetlink_net_init(struct net *net) 649097c53cacSDenis V. Lunev { 649197c53cacSDenis V. Lunev struct sock *sk; 6492a31f2d17SPablo Neira Ayuso struct netlink_kernel_cfg cfg = { 6493a31f2d17SPablo Neira Ayuso .groups = RTNLGRP_MAX, 6494a31f2d17SPablo Neira Ayuso .input = rtnetlink_rcv, 6495a31f2d17SPablo Neira Ayuso .cb_mutex = &rtnl_mutex, 64969785e10aSPablo Neira Ayuso .flags = NL_CFG_F_NONROOT_RECV, 64975f729eaaSJulien Gomes .bind = rtnetlink_bind, 6498a31f2d17SPablo Neira Ayuso }; 6499a31f2d17SPablo Neira Ayuso 65009f00d977SPablo Neira Ayuso sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg); 650197c53cacSDenis V. Lunev if (!sk) 650297c53cacSDenis V. Lunev return -ENOMEM; 650397c53cacSDenis V. Lunev net->rtnl = sk; 650497c53cacSDenis V. Lunev return 0; 650597c53cacSDenis V. Lunev } 650697c53cacSDenis V. Lunev 65072c8c1e72SAlexey Dobriyan static void __net_exit rtnetlink_net_exit(struct net *net) 650897c53cacSDenis V. Lunev { 6509b7c6ba6eSDenis V. Lunev netlink_kernel_release(net->rtnl); 651097c53cacSDenis V. Lunev net->rtnl = NULL; 651197c53cacSDenis V. Lunev } 651297c53cacSDenis V. Lunev 651397c53cacSDenis V. Lunev static struct pernet_operations rtnetlink_net_ops = { 651497c53cacSDenis V. Lunev .init = rtnetlink_net_init, 651597c53cacSDenis V. Lunev .exit = rtnetlink_net_exit, 651697c53cacSDenis V. Lunev }; 651797c53cacSDenis V. Lunev 65181da177e4SLinus Torvalds void __init rtnetlink_init(void) 65191da177e4SLinus Torvalds { 652097c53cacSDenis V. Lunev if (register_pernet_subsys(&rtnetlink_net_ops)) 65211da177e4SLinus Torvalds panic("rtnetlink_init: cannot initialize rtnetlink\n"); 652297c53cacSDenis V. Lunev 65231da177e4SLinus Torvalds register_netdevice_notifier(&rtnetlink_dev_notifier); 6524340d17fcSThomas Graf 6525c7ac8679SGreg Rose rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, 6526b97bac64SFlorian Westphal rtnl_dump_ifinfo, 0); 6527b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0); 6528b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0); 6529b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0); 6530687ad8ccSThomas Graf 6531b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0); 6532b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0); 6533b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0); 653477162022SJohn Fastabend 653536fbf1e5SJiri Pirko rtnl_register(PF_UNSPEC, RTM_NEWLINKPROP, rtnl_newlinkprop, NULL, 0); 653636fbf1e5SJiri Pirko rtnl_register(PF_UNSPEC, RTM_DELLINKPROP, rtnl_dellinkprop, NULL, 0); 653736fbf1e5SJiri Pirko 6538b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0); 65399e834259SNikolay Aleksandrov rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 65409e834259SNikolay Aleksandrov RTNL_FLAG_BULK_DEL_SUPPORTED); 65415b2f94b2SRoopa Prabhu rtnl_register(PF_BRIDGE, RTM_GETNEIGH, rtnl_fdb_get, rtnl_fdb_dump, 0); 6542e5a55a89SJohn Fastabend 6543b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0); 6544b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0); 6545b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0); 654610c9ead9SRoopa Prabhu 654710c9ead9SRoopa Prabhu rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump, 6548b97bac64SFlorian Westphal 0); 654903ba3566SPetr Machata rtnl_register(PF_UNSPEC, RTM_SETSTATS, rtnl_stats_set, NULL, 0); 6550cc7f5022SIdo Schimmel 6551cc7f5022SIdo Schimmel rtnl_register(PF_BRIDGE, RTM_GETMDB, NULL, rtnl_mdb_dump, 0); 6552cc7f5022SIdo Schimmel rtnl_register(PF_BRIDGE, RTM_NEWMDB, rtnl_mdb_add, NULL, 0); 6553cc7f5022SIdo Schimmel rtnl_register(PF_BRIDGE, RTM_DELMDB, rtnl_mdb_del, NULL, 0); 65541da177e4SLinus Torvalds } 6555