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 64*29cfb2aaSIdo Schimmel #define RTNL_SLAVE_MAX_TYPE 44 65ccf8dbcdSKees Cook 66e0d087afSEric Dumazet struct rtnl_link { 67e2849863SThomas Graf rtnl_doit_func doit; 68e2849863SThomas Graf rtnl_dumpit_func dumpit; 69e4202511SFlorian Westphal struct module *owner; 7062256f98SFlorian Westphal unsigned int flags; 71addf9b90SFlorian Westphal struct rcu_head rcu; 72e2849863SThomas Graf }; 73e2849863SThomas Graf 746756ae4bSStephen Hemminger static DEFINE_MUTEX(rtnl_mutex); 751da177e4SLinus Torvalds 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 u32 ext_filter_mask) 1277b22b941bSHannes Frederic Sowa { 1278b22b941bSHannes Frederic Sowa struct ifla_vf_rss_query_en vf_rss_query_en; 127979aab093SMoshe Shemesh struct nlattr *vf, *vfstats, *vfvlanlist; 1280b22b941bSHannes Frederic Sowa struct ifla_vf_link_state vf_linkstate; 128179aab093SMoshe Shemesh struct ifla_vf_vlan_info vf_vlan_info; 1282b22b941bSHannes Frederic Sowa struct ifla_vf_spoofchk vf_spoofchk; 1283b22b941bSHannes Frederic Sowa struct ifla_vf_tx_rate vf_tx_rate; 1284b22b941bSHannes Frederic Sowa struct ifla_vf_stats vf_stats; 1285b22b941bSHannes Frederic Sowa struct ifla_vf_trust vf_trust; 1286b22b941bSHannes Frederic Sowa struct ifla_vf_vlan vf_vlan; 1287b22b941bSHannes Frederic Sowa struct ifla_vf_rate vf_rate; 1288b22b941bSHannes Frederic Sowa struct ifla_vf_mac vf_mac; 128975345f88SDenis Kirjanov struct ifla_vf_broadcast vf_broadcast; 1290b22b941bSHannes Frederic Sowa struct ifla_vf_info ivi; 129130aad417SDanit Goldberg struct ifla_vf_guid node_guid; 129230aad417SDanit Goldberg struct ifla_vf_guid port_guid; 1293b22b941bSHannes Frederic Sowa 12940eed9cf5SMintz, Yuval memset(&ivi, 0, sizeof(ivi)); 12950eed9cf5SMintz, Yuval 1296b22b941bSHannes Frederic Sowa /* Not all SR-IOV capable drivers support the 1297b22b941bSHannes Frederic Sowa * spoofcheck and "RSS query enable" query. Preset to 1298b22b941bSHannes Frederic Sowa * -1 so the user space tool can detect that the driver 1299b22b941bSHannes Frederic Sowa * didn't report anything. 1300b22b941bSHannes Frederic Sowa */ 1301b22b941bSHannes Frederic Sowa ivi.spoofchk = -1; 1302b22b941bSHannes Frederic Sowa ivi.rss_query_en = -1; 1303b22b941bSHannes Frederic Sowa ivi.trusted = -1; 1304b22b941bSHannes Frederic Sowa /* The default value for VF link state is "auto" 1305b22b941bSHannes Frederic Sowa * IFLA_VF_LINK_STATE_AUTO which equals zero 1306b22b941bSHannes Frederic Sowa */ 1307b22b941bSHannes Frederic Sowa ivi.linkstate = 0; 130879aab093SMoshe Shemesh /* VLAN Protocol by default is 802.1Q */ 130979aab093SMoshe Shemesh ivi.vlan_proto = htons(ETH_P_8021Q); 1310b22b941bSHannes Frederic Sowa if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi)) 1311b22b941bSHannes Frederic Sowa return 0; 1312b22b941bSHannes Frederic Sowa 1313775f4f05SDan Carpenter memset(&vf_vlan_info, 0, sizeof(vf_vlan_info)); 13149fbf082fSLeon Romanovsky memset(&node_guid, 0, sizeof(node_guid)); 13159fbf082fSLeon Romanovsky memset(&port_guid, 0, sizeof(port_guid)); 1316775f4f05SDan Carpenter 1317b22b941bSHannes Frederic Sowa vf_mac.vf = 1318b22b941bSHannes Frederic Sowa vf_vlan.vf = 131979aab093SMoshe Shemesh vf_vlan_info.vf = 1320b22b941bSHannes Frederic Sowa vf_rate.vf = 1321b22b941bSHannes Frederic Sowa vf_tx_rate.vf = 1322b22b941bSHannes Frederic Sowa vf_spoofchk.vf = 1323b22b941bSHannes Frederic Sowa vf_linkstate.vf = 1324b22b941bSHannes Frederic Sowa vf_rss_query_en.vf = 13259aed6ae0SDanit Goldberg vf_trust.vf = 13269aed6ae0SDanit Goldberg node_guid.vf = 13279aed6ae0SDanit Goldberg port_guid.vf = ivi.vf; 1328b22b941bSHannes Frederic Sowa 1329b22b941bSHannes Frederic Sowa memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac)); 133075345f88SDenis Kirjanov memcpy(vf_broadcast.broadcast, dev->broadcast, dev->addr_len); 1331b22b941bSHannes Frederic Sowa vf_vlan.vlan = ivi.vlan; 1332b22b941bSHannes Frederic Sowa vf_vlan.qos = ivi.qos; 133379aab093SMoshe Shemesh vf_vlan_info.vlan = ivi.vlan; 133479aab093SMoshe Shemesh vf_vlan_info.qos = ivi.qos; 133579aab093SMoshe Shemesh vf_vlan_info.vlan_proto = ivi.vlan_proto; 1336b22b941bSHannes Frederic Sowa vf_tx_rate.rate = ivi.max_tx_rate; 1337b22b941bSHannes Frederic Sowa vf_rate.min_tx_rate = ivi.min_tx_rate; 1338b22b941bSHannes Frederic Sowa vf_rate.max_tx_rate = ivi.max_tx_rate; 1339b22b941bSHannes Frederic Sowa vf_spoofchk.setting = ivi.spoofchk; 1340b22b941bSHannes Frederic Sowa vf_linkstate.link_state = ivi.linkstate; 1341b22b941bSHannes Frederic Sowa vf_rss_query_en.setting = ivi.rss_query_en; 1342b22b941bSHannes Frederic Sowa vf_trust.setting = ivi.trusted; 1343ae0be8deSMichal Kubecek vf = nla_nest_start_noflag(skb, IFLA_VF_INFO); 134479aab093SMoshe Shemesh if (!vf) 13454a59cdfdSGal Pressman return -EMSGSIZE; 1346b22b941bSHannes Frederic Sowa if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) || 134775345f88SDenis Kirjanov nla_put(skb, IFLA_VF_BROADCAST, sizeof(vf_broadcast), &vf_broadcast) || 1348b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) || 1349b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate), 1350b22b941bSHannes Frederic Sowa &vf_rate) || 1351b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate), 1352b22b941bSHannes Frederic Sowa &vf_tx_rate) || 1353b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk), 1354b22b941bSHannes Frederic Sowa &vf_spoofchk) || 1355b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate), 1356b22b941bSHannes Frederic Sowa &vf_linkstate) || 1357b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_RSS_QUERY_EN, 1358b22b941bSHannes Frederic Sowa sizeof(vf_rss_query_en), 1359b22b941bSHannes Frederic Sowa &vf_rss_query_en) || 1360b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_TRUST, 1361b22b941bSHannes Frederic Sowa sizeof(vf_trust), &vf_trust)) 136279aab093SMoshe Shemesh goto nla_put_vf_failure; 136330aad417SDanit Goldberg 136430aad417SDanit Goldberg if (dev->netdev_ops->ndo_get_vf_guid && 136530aad417SDanit Goldberg !dev->netdev_ops->ndo_get_vf_guid(dev, vfs_num, &node_guid, 136630aad417SDanit Goldberg &port_guid)) { 136730aad417SDanit Goldberg if (nla_put(skb, IFLA_VF_IB_NODE_GUID, sizeof(node_guid), 136830aad417SDanit Goldberg &node_guid) || 136930aad417SDanit Goldberg nla_put(skb, IFLA_VF_IB_PORT_GUID, sizeof(port_guid), 137030aad417SDanit Goldberg &port_guid)) 137130aad417SDanit Goldberg goto nla_put_vf_failure; 137230aad417SDanit Goldberg } 1373ae0be8deSMichal Kubecek vfvlanlist = nla_nest_start_noflag(skb, IFLA_VF_VLAN_LIST); 137479aab093SMoshe Shemesh if (!vfvlanlist) 137579aab093SMoshe Shemesh goto nla_put_vf_failure; 137679aab093SMoshe Shemesh if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info), 137779aab093SMoshe Shemesh &vf_vlan_info)) { 137879aab093SMoshe Shemesh nla_nest_cancel(skb, vfvlanlist); 137979aab093SMoshe Shemesh goto nla_put_vf_failure; 138079aab093SMoshe Shemesh } 138179aab093SMoshe Shemesh nla_nest_end(skb, vfvlanlist); 1382fa0e21faSEdwin Peer if (~ext_filter_mask & RTEXT_FILTER_SKIP_STATS) { 1383b22b941bSHannes Frederic Sowa memset(&vf_stats, 0, sizeof(vf_stats)); 1384b22b941bSHannes Frederic Sowa if (dev->netdev_ops->ndo_get_vf_stats) 1385b22b941bSHannes Frederic Sowa dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num, 1386b22b941bSHannes Frederic Sowa &vf_stats); 1387ae0be8deSMichal Kubecek vfstats = nla_nest_start_noflag(skb, IFLA_VF_STATS); 138879aab093SMoshe Shemesh if (!vfstats) 138979aab093SMoshe Shemesh goto nla_put_vf_failure; 1390343a6d8eSNicolas Dichtel if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS, 1391343a6d8eSNicolas Dichtel vf_stats.rx_packets, IFLA_VF_STATS_PAD) || 1392343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS, 1393343a6d8eSNicolas Dichtel vf_stats.tx_packets, IFLA_VF_STATS_PAD) || 1394343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES, 1395343a6d8eSNicolas Dichtel vf_stats.rx_bytes, IFLA_VF_STATS_PAD) || 1396343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES, 1397343a6d8eSNicolas Dichtel vf_stats.tx_bytes, IFLA_VF_STATS_PAD) || 1398343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST, 1399343a6d8eSNicolas Dichtel vf_stats.broadcast, IFLA_VF_STATS_PAD) || 1400343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST, 1401c5a9f6f0SEugenia Emantayev vf_stats.multicast, IFLA_VF_STATS_PAD) || 1402c5a9f6f0SEugenia Emantayev nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED, 1403c5a9f6f0SEugenia Emantayev vf_stats.rx_dropped, IFLA_VF_STATS_PAD) || 1404c5a9f6f0SEugenia Emantayev nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED, 1405c5a9f6f0SEugenia Emantayev vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) { 140679aab093SMoshe Shemesh nla_nest_cancel(skb, vfstats); 140779aab093SMoshe Shemesh goto nla_put_vf_failure; 140879aab093SMoshe Shemesh } 1409b22b941bSHannes Frederic Sowa nla_nest_end(skb, vfstats); 1410fa0e21faSEdwin Peer } 1411b22b941bSHannes Frederic Sowa nla_nest_end(skb, vf); 1412b22b941bSHannes Frederic Sowa return 0; 141379aab093SMoshe Shemesh 141479aab093SMoshe Shemesh nla_put_vf_failure: 141579aab093SMoshe Shemesh nla_nest_cancel(skb, vf); 141679aab093SMoshe Shemesh return -EMSGSIZE; 1417b22b941bSHannes Frederic Sowa } 1418b22b941bSHannes Frederic Sowa 1419250fc3dfSFlorian Westphal static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb, 1420250fc3dfSFlorian Westphal struct net_device *dev, 1421250fc3dfSFlorian Westphal u32 ext_filter_mask) 1422250fc3dfSFlorian Westphal { 1423250fc3dfSFlorian Westphal struct nlattr *vfinfo; 1424250fc3dfSFlorian Westphal int i, num_vfs; 1425250fc3dfSFlorian Westphal 1426250fc3dfSFlorian Westphal if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0)) 1427250fc3dfSFlorian Westphal return 0; 1428250fc3dfSFlorian Westphal 1429250fc3dfSFlorian Westphal num_vfs = dev_num_vf(dev->dev.parent); 1430250fc3dfSFlorian Westphal if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs)) 1431250fc3dfSFlorian Westphal return -EMSGSIZE; 1432250fc3dfSFlorian Westphal 1433250fc3dfSFlorian Westphal if (!dev->netdev_ops->ndo_get_vf_config) 1434250fc3dfSFlorian Westphal return 0; 1435250fc3dfSFlorian Westphal 1436ae0be8deSMichal Kubecek vfinfo = nla_nest_start_noflag(skb, IFLA_VFINFO_LIST); 1437250fc3dfSFlorian Westphal if (!vfinfo) 1438250fc3dfSFlorian Westphal return -EMSGSIZE; 1439250fc3dfSFlorian Westphal 1440250fc3dfSFlorian Westphal for (i = 0; i < num_vfs; i++) { 14414a59cdfdSGal Pressman if (rtnl_fill_vfinfo(skb, dev, i, ext_filter_mask)) { 14424a59cdfdSGal Pressman nla_nest_cancel(skb, vfinfo); 1443250fc3dfSFlorian Westphal return -EMSGSIZE; 1444250fc3dfSFlorian Westphal } 14454a59cdfdSGal Pressman } 1446250fc3dfSFlorian Westphal 1447250fc3dfSFlorian Westphal nla_nest_end(skb, vfinfo); 1448250fc3dfSFlorian Westphal return 0; 1449250fc3dfSFlorian Westphal } 1450250fc3dfSFlorian Westphal 1451b22b941bSHannes Frederic Sowa static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev) 1452b22b941bSHannes Frederic Sowa { 14535f8e4474SKangjie Lu struct rtnl_link_ifmap map; 14545f8e4474SKangjie Lu 14555f8e4474SKangjie Lu memset(&map, 0, sizeof(map)); 14565f8e4474SKangjie Lu map.mem_start = dev->mem_start; 14575f8e4474SKangjie Lu map.mem_end = dev->mem_end; 14585f8e4474SKangjie Lu map.base_addr = dev->base_addr; 14595f8e4474SKangjie Lu map.irq = dev->irq; 14605f8e4474SKangjie Lu map.dma = dev->dma; 14615f8e4474SKangjie Lu map.port = dev->if_port; 14625f8e4474SKangjie Lu 1463270cb4d0SNicolas Dichtel if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD)) 1464b22b941bSHannes Frederic Sowa return -EMSGSIZE; 1465b22b941bSHannes Frederic Sowa 1466b22b941bSHannes Frederic Sowa return 0; 1467b22b941bSHannes Frederic Sowa } 1468b22b941bSHannes Frederic Sowa 1469a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_skb(struct net_device *dev) 1470d67b9cd2SDaniel Borkmann { 147158038695SMartin KaFai Lau const struct bpf_prog *generic_xdp_prog; 1472d67b9cd2SDaniel Borkmann 1473d67b9cd2SDaniel Borkmann ASSERT_RTNL(); 1474d67b9cd2SDaniel Borkmann 147558038695SMartin KaFai Lau generic_xdp_prog = rtnl_dereference(dev->xdp_prog); 1476a25717d2SJakub Kicinski if (!generic_xdp_prog) 1477a25717d2SJakub Kicinski return 0; 1478a25717d2SJakub Kicinski return generic_xdp_prog->aux->id; 147958038695SMartin KaFai Lau } 1480ce158e58SJakub Kicinski 1481a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_drv(struct net_device *dev) 1482a25717d2SJakub Kicinski { 14837f0a8382SAndrii Nakryiko return dev_xdp_prog_id(dev, XDP_MODE_DRV); 1484a25717d2SJakub Kicinski } 1485118b4aa2SJakub Kicinski 1486a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_hw(struct net_device *dev) 1487a25717d2SJakub Kicinski { 14887f0a8382SAndrii Nakryiko return dev_xdp_prog_id(dev, XDP_MODE_HW); 1489a25717d2SJakub Kicinski } 1490a25717d2SJakub Kicinski 1491a25717d2SJakub Kicinski static int rtnl_xdp_report_one(struct sk_buff *skb, struct net_device *dev, 1492a25717d2SJakub Kicinski u32 *prog_id, u8 *mode, u8 tgt_mode, u32 attr, 1493a25717d2SJakub Kicinski u32 (*get_prog_id)(struct net_device *dev)) 1494a25717d2SJakub Kicinski { 1495a25717d2SJakub Kicinski u32 curr_id; 1496a25717d2SJakub Kicinski int err; 1497a25717d2SJakub Kicinski 1498a25717d2SJakub Kicinski curr_id = get_prog_id(dev); 1499a25717d2SJakub Kicinski if (!curr_id) 1500a25717d2SJakub Kicinski return 0; 1501a25717d2SJakub Kicinski 1502a25717d2SJakub Kicinski *prog_id = curr_id; 1503a25717d2SJakub Kicinski err = nla_put_u32(skb, attr, curr_id); 1504a25717d2SJakub Kicinski if (err) 1505a25717d2SJakub Kicinski return err; 1506a25717d2SJakub Kicinski 1507a25717d2SJakub Kicinski if (*mode != XDP_ATTACHED_NONE) 1508a25717d2SJakub Kicinski *mode = XDP_ATTACHED_MULTI; 1509a25717d2SJakub Kicinski else 1510a25717d2SJakub Kicinski *mode = tgt_mode; 1511a25717d2SJakub Kicinski 1512a25717d2SJakub Kicinski return 0; 1513d67b9cd2SDaniel Borkmann } 1514d67b9cd2SDaniel Borkmann 1515d1fdd913SBrenden Blanco static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev) 1516d1fdd913SBrenden Blanco { 1517d1fdd913SBrenden Blanco struct nlattr *xdp; 151858038695SMartin KaFai Lau u32 prog_id; 1519d1fdd913SBrenden Blanco int err; 15204f91da26SJakub Kicinski u8 mode; 1521d1fdd913SBrenden Blanco 1522ae0be8deSMichal Kubecek xdp = nla_nest_start_noflag(skb, IFLA_XDP); 1523d1fdd913SBrenden Blanco if (!xdp) 1524d1fdd913SBrenden Blanco return -EMSGSIZE; 1525b5cdae32SDavid S. Miller 1526a25717d2SJakub Kicinski prog_id = 0; 1527a25717d2SJakub Kicinski mode = XDP_ATTACHED_NONE; 1528202aabe8SJakub Kicinski err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_SKB, 1529202aabe8SJakub Kicinski IFLA_XDP_SKB_PROG_ID, rtnl_xdp_prog_skb); 1530202aabe8SJakub Kicinski if (err) 1531a25717d2SJakub Kicinski goto err_cancel; 1532202aabe8SJakub Kicinski err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_DRV, 1533202aabe8SJakub Kicinski IFLA_XDP_DRV_PROG_ID, rtnl_xdp_prog_drv); 1534202aabe8SJakub Kicinski if (err) 1535a25717d2SJakub Kicinski goto err_cancel; 1536202aabe8SJakub Kicinski err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_HW, 1537202aabe8SJakub Kicinski IFLA_XDP_HW_PROG_ID, rtnl_xdp_prog_hw); 1538202aabe8SJakub Kicinski if (err) 1539a25717d2SJakub Kicinski goto err_cancel; 1540a25717d2SJakub Kicinski 15414f91da26SJakub Kicinski err = nla_put_u8(skb, IFLA_XDP_ATTACHED, mode); 1542d1fdd913SBrenden Blanco if (err) 1543d1fdd913SBrenden Blanco goto err_cancel; 1544d1fdd913SBrenden Blanco 1545a25717d2SJakub Kicinski if (prog_id && mode != XDP_ATTACHED_MULTI) { 154658038695SMartin KaFai Lau err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id); 154758038695SMartin KaFai Lau if (err) 154858038695SMartin KaFai Lau goto err_cancel; 154958038695SMartin KaFai Lau } 155058038695SMartin KaFai Lau 1551d1fdd913SBrenden Blanco nla_nest_end(skb, xdp); 1552d1fdd913SBrenden Blanco return 0; 1553d1fdd913SBrenden Blanco 1554d1fdd913SBrenden Blanco err_cancel: 1555d1fdd913SBrenden Blanco nla_nest_cancel(skb, xdp); 1556d1fdd913SBrenden Blanco return err; 1557d1fdd913SBrenden Blanco } 1558d1fdd913SBrenden Blanco 15593d3ea5afSVlad Yasevich static u32 rtnl_get_event(unsigned long event) 15603d3ea5afSVlad Yasevich { 15613d3ea5afSVlad Yasevich u32 rtnl_event_type = IFLA_EVENT_NONE; 15623d3ea5afSVlad Yasevich 15633d3ea5afSVlad Yasevich switch (event) { 15643d3ea5afSVlad Yasevich case NETDEV_REBOOT: 15653d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_REBOOT; 15663d3ea5afSVlad Yasevich break; 15673d3ea5afSVlad Yasevich case NETDEV_FEAT_CHANGE: 15683d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_FEATURES; 15693d3ea5afSVlad Yasevich break; 15703d3ea5afSVlad Yasevich case NETDEV_BONDING_FAILOVER: 15713d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER; 15723d3ea5afSVlad Yasevich break; 15733d3ea5afSVlad Yasevich case NETDEV_NOTIFY_PEERS: 15743d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS; 15753d3ea5afSVlad Yasevich break; 15763d3ea5afSVlad Yasevich case NETDEV_RESEND_IGMP: 15773d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_IGMP_RESEND; 15783d3ea5afSVlad Yasevich break; 15793d3ea5afSVlad Yasevich case NETDEV_CHANGEINFODATA: 15803d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS; 15813d3ea5afSVlad Yasevich break; 15823d3ea5afSVlad Yasevich default: 15833d3ea5afSVlad Yasevich break; 15843d3ea5afSVlad Yasevich } 15853d3ea5afSVlad Yasevich 15863d3ea5afSVlad Yasevich return rtnl_event_type; 15873d3ea5afSVlad Yasevich } 15883d3ea5afSVlad Yasevich 158979110a04SFlorian Westphal static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev) 159079110a04SFlorian Westphal { 159179110a04SFlorian Westphal const struct net_device *upper_dev; 159279110a04SFlorian Westphal int ret = 0; 159379110a04SFlorian Westphal 159479110a04SFlorian Westphal rcu_read_lock(); 159579110a04SFlorian Westphal 159679110a04SFlorian Westphal upper_dev = netdev_master_upper_dev_get_rcu(dev); 159779110a04SFlorian Westphal if (upper_dev) 159879110a04SFlorian Westphal ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex); 159979110a04SFlorian Westphal 160079110a04SFlorian Westphal rcu_read_unlock(); 160179110a04SFlorian Westphal return ret; 160279110a04SFlorian Westphal } 160379110a04SFlorian Westphal 1604feadc4b6SSabrina Dubroca static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev, 1605feadc4b6SSabrina Dubroca bool force) 160679110a04SFlorian Westphal { 160779110a04SFlorian Westphal int ifindex = dev_get_iflink(dev); 160879110a04SFlorian Westphal 1609feadc4b6SSabrina Dubroca if (force || dev->ifindex != ifindex) 161079110a04SFlorian Westphal return nla_put_u32(skb, IFLA_LINK, ifindex); 1611feadc4b6SSabrina Dubroca 1612feadc4b6SSabrina Dubroca return 0; 161379110a04SFlorian Westphal } 161479110a04SFlorian Westphal 16156c557001SFlorian Westphal static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb, 16166c557001SFlorian Westphal struct net_device *dev) 16176c557001SFlorian Westphal { 16186c557001SFlorian Westphal char buf[IFALIASZ]; 16196c557001SFlorian Westphal int ret; 16206c557001SFlorian Westphal 16216c557001SFlorian Westphal ret = dev_get_alias(dev, buf, sizeof(buf)); 16226c557001SFlorian Westphal return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0; 16236c557001SFlorian Westphal } 16246c557001SFlorian Westphal 1625b1e66b9aSFlorian Westphal static int rtnl_fill_link_netnsid(struct sk_buff *skb, 162679e1ad14SJiri Benc const struct net_device *dev, 1627d4e4fdf9SGuillaume Nault struct net *src_net, gfp_t gfp) 1628b1e66b9aSFlorian Westphal { 1629feadc4b6SSabrina Dubroca bool put_iflink = false; 1630feadc4b6SSabrina Dubroca 1631b1e66b9aSFlorian Westphal if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) { 1632b1e66b9aSFlorian Westphal struct net *link_net = dev->rtnl_link_ops->get_link_net(dev); 1633b1e66b9aSFlorian Westphal 1634b1e66b9aSFlorian Westphal if (!net_eq(dev_net(dev), link_net)) { 1635d4e4fdf9SGuillaume Nault int id = peernet2id_alloc(src_net, link_net, gfp); 1636b1e66b9aSFlorian Westphal 1637b1e66b9aSFlorian Westphal if (nla_put_s32(skb, IFLA_LINK_NETNSID, id)) 1638b1e66b9aSFlorian Westphal return -EMSGSIZE; 1639feadc4b6SSabrina Dubroca 1640feadc4b6SSabrina Dubroca put_iflink = true; 1641b1e66b9aSFlorian Westphal } 1642b1e66b9aSFlorian Westphal } 1643b1e66b9aSFlorian Westphal 1644feadc4b6SSabrina Dubroca return nla_put_iflink(skb, dev, put_iflink); 1645b1e66b9aSFlorian Westphal } 1646b1e66b9aSFlorian Westphal 1647070cbf5bSFlorian Westphal static int rtnl_fill_link_af(struct sk_buff *skb, 1648070cbf5bSFlorian Westphal const struct net_device *dev, 1649070cbf5bSFlorian Westphal u32 ext_filter_mask) 1650070cbf5bSFlorian Westphal { 1651070cbf5bSFlorian Westphal const struct rtnl_af_ops *af_ops; 1652070cbf5bSFlorian Westphal struct nlattr *af_spec; 1653070cbf5bSFlorian Westphal 1654ae0be8deSMichal Kubecek af_spec = nla_nest_start_noflag(skb, IFLA_AF_SPEC); 1655070cbf5bSFlorian Westphal if (!af_spec) 1656070cbf5bSFlorian Westphal return -EMSGSIZE; 1657070cbf5bSFlorian Westphal 16585fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 1659070cbf5bSFlorian Westphal struct nlattr *af; 1660070cbf5bSFlorian Westphal int err; 1661070cbf5bSFlorian Westphal 1662070cbf5bSFlorian Westphal if (!af_ops->fill_link_af) 1663070cbf5bSFlorian Westphal continue; 1664070cbf5bSFlorian Westphal 1665ae0be8deSMichal Kubecek af = nla_nest_start_noflag(skb, af_ops->family); 1666070cbf5bSFlorian Westphal if (!af) 1667070cbf5bSFlorian Westphal return -EMSGSIZE; 1668070cbf5bSFlorian Westphal 1669070cbf5bSFlorian Westphal err = af_ops->fill_link_af(skb, dev, ext_filter_mask); 1670070cbf5bSFlorian Westphal /* 1671070cbf5bSFlorian Westphal * Caller may return ENODATA to indicate that there 1672070cbf5bSFlorian Westphal * was no data to be dumped. This is not an error, it 1673070cbf5bSFlorian Westphal * means we should trim the attribute header and 1674070cbf5bSFlorian Westphal * continue. 1675070cbf5bSFlorian Westphal */ 1676070cbf5bSFlorian Westphal if (err == -ENODATA) 1677070cbf5bSFlorian Westphal nla_nest_cancel(skb, af); 1678070cbf5bSFlorian Westphal else if (err < 0) 1679070cbf5bSFlorian Westphal return -EMSGSIZE; 1680070cbf5bSFlorian Westphal 1681070cbf5bSFlorian Westphal nla_nest_end(skb, af); 1682070cbf5bSFlorian Westphal } 1683070cbf5bSFlorian Westphal 1684070cbf5bSFlorian Westphal nla_nest_end(skb, af_spec); 1685070cbf5bSFlorian Westphal return 0; 1686070cbf5bSFlorian Westphal } 1687070cbf5bSFlorian Westphal 168888f4fb0cSJiri Pirko static int rtnl_fill_alt_ifnames(struct sk_buff *skb, 168988f4fb0cSJiri Pirko const struct net_device *dev) 169088f4fb0cSJiri Pirko { 169188f4fb0cSJiri Pirko struct netdev_name_node *name_node; 169288f4fb0cSJiri Pirko int count = 0; 169388f4fb0cSJiri Pirko 169488f4fb0cSJiri Pirko list_for_each_entry(name_node, &dev->name_node->list, list) { 169588f4fb0cSJiri Pirko if (nla_put_string(skb, IFLA_ALT_IFNAME, name_node->name)) 169688f4fb0cSJiri Pirko return -EMSGSIZE; 169788f4fb0cSJiri Pirko count++; 169888f4fb0cSJiri Pirko } 169988f4fb0cSJiri Pirko return count; 170088f4fb0cSJiri Pirko } 170188f4fb0cSJiri Pirko 170288f4fb0cSJiri Pirko static int rtnl_fill_prop_list(struct sk_buff *skb, 170388f4fb0cSJiri Pirko const struct net_device *dev) 170488f4fb0cSJiri Pirko { 170588f4fb0cSJiri Pirko struct nlattr *prop_list; 170688f4fb0cSJiri Pirko int ret; 170788f4fb0cSJiri Pirko 170888f4fb0cSJiri Pirko prop_list = nla_nest_start(skb, IFLA_PROP_LIST); 170988f4fb0cSJiri Pirko if (!prop_list) 171088f4fb0cSJiri Pirko return -EMSGSIZE; 171188f4fb0cSJiri Pirko 171288f4fb0cSJiri Pirko ret = rtnl_fill_alt_ifnames(skb, dev); 171388f4fb0cSJiri Pirko if (ret <= 0) 171488f4fb0cSJiri Pirko goto nest_cancel; 171588f4fb0cSJiri Pirko 171688f4fb0cSJiri Pirko nla_nest_end(skb, prop_list); 171788f4fb0cSJiri Pirko return 0; 171888f4fb0cSJiri Pirko 171988f4fb0cSJiri Pirko nest_cancel: 172088f4fb0cSJiri Pirko nla_nest_cancel(skb, prop_list); 172188f4fb0cSJiri Pirko return ret; 172288f4fb0cSJiri Pirko } 172388f4fb0cSJiri Pirko 1724829eb208SRoopa Prabhu static int rtnl_fill_proto_down(struct sk_buff *skb, 1725829eb208SRoopa Prabhu const struct net_device *dev) 1726829eb208SRoopa Prabhu { 1727829eb208SRoopa Prabhu struct nlattr *pr; 1728829eb208SRoopa Prabhu u32 preason; 1729829eb208SRoopa Prabhu 1730829eb208SRoopa Prabhu if (nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down)) 1731829eb208SRoopa Prabhu goto nla_put_failure; 1732829eb208SRoopa Prabhu 1733829eb208SRoopa Prabhu preason = dev->proto_down_reason; 1734829eb208SRoopa Prabhu if (!preason) 1735829eb208SRoopa Prabhu return 0; 1736829eb208SRoopa Prabhu 1737829eb208SRoopa Prabhu pr = nla_nest_start(skb, IFLA_PROTO_DOWN_REASON); 1738829eb208SRoopa Prabhu if (!pr) 1739829eb208SRoopa Prabhu return -EMSGSIZE; 1740829eb208SRoopa Prabhu 1741829eb208SRoopa Prabhu if (nla_put_u32(skb, IFLA_PROTO_DOWN_REASON_VALUE, preason)) { 1742829eb208SRoopa Prabhu nla_nest_cancel(skb, pr); 1743829eb208SRoopa Prabhu goto nla_put_failure; 1744829eb208SRoopa Prabhu } 1745829eb208SRoopa Prabhu 1746829eb208SRoopa Prabhu nla_nest_end(skb, pr); 1747829eb208SRoopa Prabhu return 0; 1748829eb208SRoopa Prabhu 1749829eb208SRoopa Prabhu nla_put_failure: 1750829eb208SRoopa Prabhu return -EMSGSIZE; 1751829eb208SRoopa Prabhu } 1752829eb208SRoopa Prabhu 1753dca56c30SJiri Pirko static int rtnl_fill_devlink_port(struct sk_buff *skb, 1754dca56c30SJiri Pirko const struct net_device *dev) 1755dca56c30SJiri Pirko { 1756dca56c30SJiri Pirko struct nlattr *devlink_port_nest; 1757dca56c30SJiri Pirko int ret; 1758dca56c30SJiri Pirko 1759dca56c30SJiri Pirko devlink_port_nest = nla_nest_start(skb, IFLA_DEVLINK_PORT); 1760dca56c30SJiri Pirko if (!devlink_port_nest) 1761dca56c30SJiri Pirko return -EMSGSIZE; 1762dca56c30SJiri Pirko 1763dca56c30SJiri Pirko if (dev->devlink_port) { 1764dca56c30SJiri Pirko ret = devlink_nl_port_handle_fill(skb, dev->devlink_port); 1765dca56c30SJiri Pirko if (ret < 0) 1766dca56c30SJiri Pirko goto nest_cancel; 1767dca56c30SJiri Pirko } 1768dca56c30SJiri Pirko 1769dca56c30SJiri Pirko nla_nest_end(skb, devlink_port_nest); 1770dca56c30SJiri Pirko return 0; 1771dca56c30SJiri Pirko 1772dca56c30SJiri Pirko nest_cancel: 1773dca56c30SJiri Pirko nla_nest_cancel(skb, devlink_port_nest); 1774dca56c30SJiri Pirko return ret; 1775dca56c30SJiri Pirko } 1776dca56c30SJiri Pirko 177779e1ad14SJiri Benc static int rtnl_fill_ifinfo(struct sk_buff *skb, 177879e1ad14SJiri Benc struct net_device *dev, struct net *src_net, 1779575c3e2aSPatrick McHardy int type, u32 pid, u32 seq, u32 change, 17803d3ea5afSVlad Yasevich unsigned int flags, u32 ext_filter_mask, 178138e01b30SNicolas Dichtel u32 event, int *new_nsid, int new_ifindex, 1782d4e4fdf9SGuillaume Nault int tgt_netnsid, gfp_t gfp) 1783b60c5115SThomas Graf { 1784b60c5115SThomas Graf struct ifinfomsg *ifm; 17851da177e4SLinus Torvalds struct nlmsghdr *nlh; 17865891cd5eSEric Dumazet struct Qdisc *qdisc; 17871da177e4SLinus Torvalds 17882907c35fSEric Dumazet ASSERT_RTNL(); 1789b60c5115SThomas Graf nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags); 1790b60c5115SThomas Graf if (nlh == NULL) 179126932566SPatrick McHardy return -EMSGSIZE; 17921da177e4SLinus Torvalds 1793b60c5115SThomas Graf ifm = nlmsg_data(nlh); 1794b60c5115SThomas Graf ifm->ifi_family = AF_UNSPEC; 1795b60c5115SThomas Graf ifm->__ifi_pad = 0; 1796b60c5115SThomas Graf ifm->ifi_type = dev->type; 1797b60c5115SThomas Graf ifm->ifi_index = dev->ifindex; 1798b60c5115SThomas Graf ifm->ifi_flags = dev_get_flags(dev); 1799b60c5115SThomas Graf ifm->ifi_change = change; 18001da177e4SLinus Torvalds 18017e4a8d5aSChristian Brauner if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_TARGET_NETNSID, tgt_netnsid)) 180279e1ad14SJiri Benc goto nla_put_failure; 180379e1ad14SJiri Benc 18045891cd5eSEric Dumazet qdisc = rtnl_dereference(dev->qdisc); 1805a6574349SDavid S. Miller if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 1806a6574349SDavid S. Miller nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) || 1807a6574349SDavid S. Miller nla_put_u8(skb, IFLA_OPERSTATE, 1808a6574349SDavid S. Miller netif_running(dev) ? dev->operstate : IF_OPER_DOWN) || 1809a6574349SDavid S. Miller nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) || 1810a6574349SDavid S. Miller nla_put_u32(skb, IFLA_MTU, dev->mtu) || 18113e7a50ceSStephen Hemminger nla_put_u32(skb, IFLA_MIN_MTU, dev->min_mtu) || 18123e7a50ceSStephen Hemminger nla_put_u32(skb, IFLA_MAX_MTU, dev->max_mtu) || 1813a6574349SDavid S. Miller nla_put_u32(skb, IFLA_GROUP, dev->group) || 1814edbc0bb3SBen Greear nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) || 18157e6e1b57SNicolas Dichtel nla_put_u32(skb, IFLA_ALLMULTI, dev->allmulti) || 181676ff5cc9SJiri Pirko nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) || 1817c70ce028SEric Dumazet nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) || 1818c70ce028SEric Dumazet nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) || 1819eac1b93cSCoco Li nla_put_u32(skb, IFLA_GRO_MAX_SIZE, dev->gro_max_size) || 18209eefedd5SXin Long nla_put_u32(skb, IFLA_GSO_IPV4_MAX_SIZE, dev->gso_ipv4_max_size) || 18219eefedd5SXin Long nla_put_u32(skb, IFLA_GRO_IPV4_MAX_SIZE, dev->gro_ipv4_max_size) || 182289527be8SEric Dumazet nla_put_u32(skb, IFLA_TSO_MAX_SIZE, dev->tso_max_size) || 182389527be8SEric Dumazet nla_put_u32(skb, IFLA_TSO_MAX_SEGS, dev->tso_max_segs) || 18241d69c2b3SMark A. Greer #ifdef CONFIG_RPS 182576ff5cc9SJiri Pirko nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) || 18261d69c2b3SMark A. Greer #endif 182779110a04SFlorian Westphal put_master_ifindex(skb, dev) || 18289a57247fSJiri Pirko nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) || 18295891cd5eSEric Dumazet (qdisc && 18305891cd5eSEric Dumazet nla_put_string(skb, IFLA_QDISC, qdisc->ops->id)) || 18316c557001SFlorian Westphal nla_put_ifalias(skb, dev) || 18322d3b479dSdavid decotigny nla_put_u32(skb, IFLA_CARRIER_CHANGES, 1833b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_up_count) + 1834b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_down_count)) || 1835b2d3bcfaSDavid Decotigny nla_put_u32(skb, IFLA_CARRIER_UP_COUNT, 1836b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_up_count)) || 1837b2d3bcfaSDavid Decotigny nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT, 1838b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_down_count))) 1839a6574349SDavid S. Miller goto nla_put_failure; 18400b815a1aSStephen Hemminger 1841829eb208SRoopa Prabhu if (rtnl_fill_proto_down(skb, dev)) 1842829eb208SRoopa Prabhu goto nla_put_failure; 1843829eb208SRoopa Prabhu 18443d3ea5afSVlad Yasevich if (event != IFLA_EVENT_NONE) { 18453d3ea5afSVlad Yasevich if (nla_put_u32(skb, IFLA_EVENT, event)) 18463d3ea5afSVlad Yasevich goto nla_put_failure; 18473d3ea5afSVlad Yasevich } 18483d3ea5afSVlad Yasevich 1849b22b941bSHannes Frederic Sowa if (rtnl_fill_link_ifmap(skb, dev)) 1850a6574349SDavid S. Miller goto nla_put_failure; 18511da177e4SLinus Torvalds 18521da177e4SLinus Torvalds if (dev->addr_len) { 1853a6574349SDavid S. Miller if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) || 1854a6574349SDavid S. Miller nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast)) 1855a6574349SDavid S. Miller goto nla_put_failure; 18561da177e4SLinus Torvalds } 18571da177e4SLinus Torvalds 185866cae9edSJiri Pirko if (rtnl_phys_port_id_fill(skb, dev)) 185966cae9edSJiri Pirko goto nla_put_failure; 186066cae9edSJiri Pirko 1861db24a904SDavid Ahern if (rtnl_phys_port_name_fill(skb, dev)) 1862db24a904SDavid Ahern goto nla_put_failure; 1863db24a904SDavid Ahern 186482f28412SJiri Pirko if (rtnl_phys_switch_id_fill(skb, dev)) 186582f28412SJiri Pirko goto nla_put_failure; 186682f28412SJiri Pirko 1867b22b941bSHannes Frederic Sowa if (rtnl_fill_stats(skb, dev)) 1868b60c5115SThomas Graf goto nla_put_failure; 1869b60c5115SThomas Graf 1870250fc3dfSFlorian Westphal if (rtnl_fill_vf(skb, dev, ext_filter_mask)) 1871a6574349SDavid S. Miller goto nla_put_failure; 187257b61080SScott Feldman 1873c53864fdSDavid Gibson if (rtnl_port_fill(skb, dev, ext_filter_mask)) 187457b61080SScott Feldman goto nla_put_failure; 187557b61080SScott Feldman 1876d1fdd913SBrenden Blanco if (rtnl_xdp_fill(skb, dev)) 1877d1fdd913SBrenden Blanco goto nla_put_failure; 1878d1fdd913SBrenden Blanco 1879ba7d49b1SJiri Pirko if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) { 188038f7b870SPatrick McHardy if (rtnl_link_fill(skb, dev) < 0) 188138f7b870SPatrick McHardy goto nla_put_failure; 188238f7b870SPatrick McHardy } 188338f7b870SPatrick McHardy 1884d4e4fdf9SGuillaume Nault if (rtnl_fill_link_netnsid(skb, dev, src_net, gfp)) 1885d37512a2SNicolas Dichtel goto nla_put_failure; 1886d37512a2SNicolas Dichtel 18876621dd29SNicolas Dichtel if (new_nsid && 18886621dd29SNicolas Dichtel nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0) 18896621dd29SNicolas Dichtel goto nla_put_failure; 189038e01b30SNicolas Dichtel if (new_ifindex && 189138e01b30SNicolas Dichtel nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0) 189238e01b30SNicolas Dichtel goto nla_put_failure; 189338e01b30SNicolas Dichtel 1894f74877a5SMichal Kubecek if (memchr_inv(dev->perm_addr, '\0', dev->addr_len) && 1895f74877a5SMichal Kubecek nla_put(skb, IFLA_PERM_ADDRESS, dev->addr_len, dev->perm_addr)) 1896f74877a5SMichal Kubecek goto nla_put_failure; 18976621dd29SNicolas Dichtel 18985fa85a09SFlorian Westphal rcu_read_lock(); 1899070cbf5bSFlorian Westphal if (rtnl_fill_link_af(skb, dev, ext_filter_mask)) 19005fa85a09SFlorian Westphal goto nla_put_failure_rcu; 19015fa85a09SFlorian Westphal rcu_read_unlock(); 1902f8ff182cSThomas Graf 190388f4fb0cSJiri Pirko if (rtnl_fill_prop_list(skb, dev)) 190488f4fb0cSJiri Pirko goto nla_put_failure; 190588f4fb0cSJiri Pirko 190600e77ed8SJohannes Berg if (dev->dev.parent && 190700e77ed8SJohannes Berg nla_put_string(skb, IFLA_PARENT_DEV_NAME, 190800e77ed8SJohannes Berg dev_name(dev->dev.parent))) 190900e77ed8SJohannes Berg goto nla_put_failure; 191000e77ed8SJohannes Berg 191100e77ed8SJohannes Berg if (dev->dev.parent && dev->dev.parent->bus && 191200e77ed8SJohannes Berg nla_put_string(skb, IFLA_PARENT_DEV_BUS_NAME, 191300e77ed8SJohannes Berg dev->dev.parent->bus->name)) 191400e77ed8SJohannes Berg goto nla_put_failure; 191500e77ed8SJohannes Berg 1916dca56c30SJiri Pirko if (rtnl_fill_devlink_port(skb, dev)) 1917dca56c30SJiri Pirko goto nla_put_failure; 1918dca56c30SJiri Pirko 1919053c095aSJohannes Berg nlmsg_end(skb, nlh); 1920053c095aSJohannes Berg return 0; 1921b60c5115SThomas Graf 19225fa85a09SFlorian Westphal nla_put_failure_rcu: 19235fa85a09SFlorian Westphal rcu_read_unlock(); 1924b60c5115SThomas Graf nla_put_failure: 192526932566SPatrick McHardy nlmsg_cancel(skb, nlh); 192626932566SPatrick McHardy return -EMSGSIZE; 19271da177e4SLinus Torvalds } 19281da177e4SLinus Torvalds 1929f7b12606SJiri Pirko static const struct nla_policy ifla_policy[IFLA_MAX+1] = { 1930f7b12606SJiri Pirko [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 }, 1931f7b12606SJiri Pirko [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 1932f7b12606SJiri Pirko [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 1933f7b12606SJiri Pirko [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) }, 1934f7b12606SJiri Pirko [IFLA_MTU] = { .type = NLA_U32 }, 1935f7b12606SJiri Pirko [IFLA_LINK] = { .type = NLA_U32 }, 1936f7b12606SJiri Pirko [IFLA_MASTER] = { .type = NLA_U32 }, 1937f7b12606SJiri Pirko [IFLA_CARRIER] = { .type = NLA_U8 }, 1938f7b12606SJiri Pirko [IFLA_TXQLEN] = { .type = NLA_U32 }, 1939f7b12606SJiri Pirko [IFLA_WEIGHT] = { .type = NLA_U32 }, 1940f7b12606SJiri Pirko [IFLA_OPERSTATE] = { .type = NLA_U8 }, 1941f7b12606SJiri Pirko [IFLA_LINKMODE] = { .type = NLA_U8 }, 1942f7b12606SJiri Pirko [IFLA_LINKINFO] = { .type = NLA_NESTED }, 1943f7b12606SJiri Pirko [IFLA_NET_NS_PID] = { .type = NLA_U32 }, 1944f7b12606SJiri Pirko [IFLA_NET_NS_FD] = { .type = NLA_U32 }, 19452459b4c6SNicolas Dichtel /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to 19462459b4c6SNicolas Dichtel * allow 0-length string (needed to remove an alias). 19472459b4c6SNicolas Dichtel */ 19482459b4c6SNicolas Dichtel [IFLA_IFALIAS] = { .type = NLA_BINARY, .len = IFALIASZ - 1 }, 1949f7b12606SJiri Pirko [IFLA_VFINFO_LIST] = {. type = NLA_NESTED }, 1950f7b12606SJiri Pirko [IFLA_VF_PORTS] = { .type = NLA_NESTED }, 1951f7b12606SJiri Pirko [IFLA_PORT_SELF] = { .type = NLA_NESTED }, 1952f7b12606SJiri Pirko [IFLA_AF_SPEC] = { .type = NLA_NESTED }, 1953f7b12606SJiri Pirko [IFLA_EXT_MASK] = { .type = NLA_U32 }, 1954f7b12606SJiri Pirko [IFLA_PROMISCUITY] = { .type = NLA_U32 }, 1955f7b12606SJiri Pirko [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 }, 1956f7b12606SJiri Pirko [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 }, 195746e6b992SStephen Hemminger [IFLA_GSO_MAX_SEGS] = { .type = NLA_U32 }, 195846e6b992SStephen Hemminger [IFLA_GSO_MAX_SIZE] = { .type = NLA_U32 }, 195902637fceSJiri Pirko [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN }, 19602d3b479dSdavid decotigny [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */ 196182f28412SJiri Pirko [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN }, 1962317f4810SNicolas Dichtel [IFLA_LINK_NETNSID] = { .type = NLA_S32 }, 196388d6378bSAnuradha Karuppiah [IFLA_PROTO_DOWN] = { .type = NLA_U8 }, 1964d1fdd913SBrenden Blanco [IFLA_XDP] = { .type = NLA_NESTED }, 19653d3ea5afSVlad Yasevich [IFLA_EVENT] = { .type = NLA_U32 }, 1966db833d40SSerhey Popovych [IFLA_GROUP] = { .type = NLA_U32 }, 19677e4a8d5aSChristian Brauner [IFLA_TARGET_NETNSID] = { .type = NLA_S32 }, 1968b2d3bcfaSDavid Decotigny [IFLA_CARRIER_UP_COUNT] = { .type = NLA_U32 }, 1969b2d3bcfaSDavid Decotigny [IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 }, 19703e7a50ceSStephen Hemminger [IFLA_MIN_MTU] = { .type = NLA_U32 }, 19713e7a50ceSStephen Hemminger [IFLA_MAX_MTU] = { .type = NLA_U32 }, 197236fbf1e5SJiri Pirko [IFLA_PROP_LIST] = { .type = NLA_NESTED }, 197336fbf1e5SJiri Pirko [IFLA_ALT_IFNAME] = { .type = NLA_STRING, 197436fbf1e5SJiri Pirko .len = ALTIFNAMSIZ - 1 }, 1975f74877a5SMichal Kubecek [IFLA_PERM_ADDRESS] = { .type = NLA_REJECT }, 1976829eb208SRoopa Prabhu [IFLA_PROTO_DOWN_REASON] = { .type = NLA_NESTED }, 19777e4a5131SAndrei Vagin [IFLA_NEW_IFINDEX] = NLA_POLICY_MIN(NLA_S32, 1), 197888b71053SJohannes Berg [IFLA_PARENT_DEV_NAME] = { .type = NLA_NUL_STRING }, 1979eac1b93cSCoco Li [IFLA_GRO_MAX_SIZE] = { .type = NLA_U32 }, 198089527be8SEric Dumazet [IFLA_TSO_MAX_SIZE] = { .type = NLA_REJECT }, 198189527be8SEric Dumazet [IFLA_TSO_MAX_SEGS] = { .type = NLA_REJECT }, 19827e6e1b57SNicolas Dichtel [IFLA_ALLMULTI] = { .type = NLA_REJECT }, 19839eefedd5SXin Long [IFLA_GSO_IPV4_MAX_SIZE] = { .type = NLA_U32 }, 19849eefedd5SXin Long [IFLA_GRO_IPV4_MAX_SIZE] = { .type = NLA_U32 }, 1985f7b12606SJiri Pirko }; 1986f7b12606SJiri Pirko 1987f7b12606SJiri Pirko static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = { 1988f7b12606SJiri Pirko [IFLA_INFO_KIND] = { .type = NLA_STRING }, 1989f7b12606SJiri Pirko [IFLA_INFO_DATA] = { .type = NLA_NESTED }, 1990f7b12606SJiri Pirko [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING }, 1991f7b12606SJiri Pirko [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED }, 1992f7b12606SJiri Pirko }; 1993f7b12606SJiri Pirko 1994f7b12606SJiri Pirko static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = { 1995364d5716SDaniel Borkmann [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) }, 199675345f88SDenis Kirjanov [IFLA_VF_BROADCAST] = { .type = NLA_REJECT }, 1997364d5716SDaniel Borkmann [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) }, 199879aab093SMoshe Shemesh [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED }, 1999364d5716SDaniel Borkmann [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) }, 2000364d5716SDaniel Borkmann [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) }, 2001364d5716SDaniel Borkmann [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) }, 2002364d5716SDaniel Borkmann [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) }, 200301a3d796SVlad Zolotarov [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) }, 20043b766cd8SEran Ben Elisha [IFLA_VF_STATS] = { .type = NLA_NESTED }, 2005dd461d6aSHiroshi Shimamoto [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) }, 2006cc8e27ccSEli Cohen [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) }, 2007cc8e27ccSEli Cohen [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) }, 20083b766cd8SEran Ben Elisha }; 20093b766cd8SEran Ben Elisha 2010f7b12606SJiri Pirko static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = { 2011f7b12606SJiri Pirko [IFLA_PORT_VF] = { .type = NLA_U32 }, 2012f7b12606SJiri Pirko [IFLA_PORT_PROFILE] = { .type = NLA_STRING, 2013f7b12606SJiri Pirko .len = PORT_PROFILE_MAX }, 2014f7b12606SJiri Pirko [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY, 2015f7b12606SJiri Pirko .len = PORT_UUID_MAX }, 2016f7b12606SJiri Pirko [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING, 2017f7b12606SJiri Pirko .len = PORT_UUID_MAX }, 2018f7b12606SJiri Pirko [IFLA_PORT_REQUEST] = { .type = NLA_U8, }, 2019f7b12606SJiri Pirko [IFLA_PORT_RESPONSE] = { .type = NLA_U16, }, 2020025331dfSDaniel Borkmann 2021025331dfSDaniel Borkmann /* Unused, but we need to keep it here since user space could 2022025331dfSDaniel Borkmann * fill it. It's also broken with regard to NLA_BINARY use in 2023025331dfSDaniel Borkmann * combination with structs. 2024025331dfSDaniel Borkmann */ 2025025331dfSDaniel Borkmann [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY, 2026025331dfSDaniel Borkmann .len = sizeof(struct ifla_port_vsi) }, 2027f7b12606SJiri Pirko }; 2028f7b12606SJiri Pirko 2029d1fdd913SBrenden Blanco static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = { 203092234c8fSToke Høiland-Jørgensen [IFLA_XDP_UNSPEC] = { .strict_start_type = IFLA_XDP_EXPECTED_FD }, 2031d1fdd913SBrenden Blanco [IFLA_XDP_FD] = { .type = NLA_S32 }, 203292234c8fSToke Høiland-Jørgensen [IFLA_XDP_EXPECTED_FD] = { .type = NLA_S32 }, 2033d1fdd913SBrenden Blanco [IFLA_XDP_ATTACHED] = { .type = NLA_U8 }, 203485de8576SDaniel Borkmann [IFLA_XDP_FLAGS] = { .type = NLA_U32 }, 203558038695SMartin KaFai Lau [IFLA_XDP_PROG_ID] = { .type = NLA_U32 }, 2036d1fdd913SBrenden Blanco }; 2037d1fdd913SBrenden Blanco 2038dc599f76SDavid Ahern static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla) 2039dc599f76SDavid Ahern { 2040dc599f76SDavid Ahern const struct rtnl_link_ops *ops = NULL; 2041dc599f76SDavid Ahern struct nlattr *linfo[IFLA_INFO_MAX + 1]; 2042dc599f76SDavid Ahern 20438cb08174SJohannes Berg if (nla_parse_nested_deprecated(linfo, IFLA_INFO_MAX, nla, ifla_info_policy, NULL) < 0) 2044dc599f76SDavid Ahern return NULL; 2045dc599f76SDavid Ahern 2046dc599f76SDavid Ahern if (linfo[IFLA_INFO_KIND]) { 2047dc599f76SDavid Ahern char kind[MODULE_NAME_LEN]; 2048dc599f76SDavid Ahern 2049872f6903SFrancis Laniel nla_strscpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind)); 2050dc599f76SDavid Ahern ops = rtnl_link_ops_get(kind); 2051dc599f76SDavid Ahern } 2052dc599f76SDavid Ahern 2053dc599f76SDavid Ahern return ops; 2054dc599f76SDavid Ahern } 2055dc599f76SDavid Ahern 2056dc599f76SDavid Ahern static bool link_master_filtered(struct net_device *dev, int master_idx) 2057dc599f76SDavid Ahern { 2058dc599f76SDavid Ahern struct net_device *master; 2059dc599f76SDavid Ahern 2060dc599f76SDavid Ahern if (!master_idx) 2061dc599f76SDavid Ahern return false; 2062dc599f76SDavid Ahern 2063dc599f76SDavid Ahern master = netdev_master_upper_dev_get(dev); 2064d3432bf1SLahav Schlesinger 2065d3432bf1SLahav Schlesinger /* 0 is already used to denote IFLA_MASTER wasn't passed, therefore need 2066d3432bf1SLahav Schlesinger * another invalid value for ifindex to denote "no master". 2067d3432bf1SLahav Schlesinger */ 2068d3432bf1SLahav Schlesinger if (master_idx == -1) 2069d3432bf1SLahav Schlesinger return !!master; 2070d3432bf1SLahav Schlesinger 2071dc599f76SDavid Ahern if (!master || master->ifindex != master_idx) 2072dc599f76SDavid Ahern return true; 2073dc599f76SDavid Ahern 2074dc599f76SDavid Ahern return false; 2075dc599f76SDavid Ahern } 2076dc599f76SDavid Ahern 2077dc599f76SDavid Ahern static bool link_kind_filtered(const struct net_device *dev, 2078dc599f76SDavid Ahern const struct rtnl_link_ops *kind_ops) 2079dc599f76SDavid Ahern { 2080dc599f76SDavid Ahern if (kind_ops && dev->rtnl_link_ops != kind_ops) 2081dc599f76SDavid Ahern return true; 2082dc599f76SDavid Ahern 2083dc599f76SDavid Ahern return false; 2084dc599f76SDavid Ahern } 2085dc599f76SDavid Ahern 2086dc599f76SDavid Ahern static bool link_dump_filtered(struct net_device *dev, 2087dc599f76SDavid Ahern int master_idx, 2088dc599f76SDavid Ahern const struct rtnl_link_ops *kind_ops) 2089dc599f76SDavid Ahern { 2090dc599f76SDavid Ahern if (link_master_filtered(dev, master_idx) || 2091dc599f76SDavid Ahern link_kind_filtered(dev, kind_ops)) 2092dc599f76SDavid Ahern return true; 2093dc599f76SDavid Ahern 2094dc599f76SDavid Ahern return false; 2095dc599f76SDavid Ahern } 2096dc599f76SDavid Ahern 2097c383edc4SChristian Brauner /** 2098c383edc4SChristian Brauner * rtnl_get_net_ns_capable - Get netns if sufficiently privileged. 2099c383edc4SChristian Brauner * @sk: netlink socket 2100c383edc4SChristian Brauner * @netnsid: network namespace identifier 2101c383edc4SChristian Brauner * 2102c383edc4SChristian Brauner * Returns the network namespace identified by netnsid on success or an error 2103c383edc4SChristian Brauner * pointer on failure. 2104c383edc4SChristian Brauner */ 2105c383edc4SChristian Brauner struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid) 210679e1ad14SJiri Benc { 210779e1ad14SJiri Benc struct net *net; 210879e1ad14SJiri Benc 2109f428fe4aSAndrei Vagin net = get_net_ns_by_id(sock_net(sk), netnsid); 211079e1ad14SJiri Benc if (!net) 211179e1ad14SJiri Benc return ERR_PTR(-EINVAL); 211279e1ad14SJiri Benc 211379e1ad14SJiri Benc /* For now, the caller is required to have CAP_NET_ADMIN in 211479e1ad14SJiri Benc * the user namespace owning the target net ns. 211579e1ad14SJiri Benc */ 2116f428fe4aSAndrei Vagin if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) { 211779e1ad14SJiri Benc put_net(net); 211879e1ad14SJiri Benc return ERR_PTR(-EACCES); 211979e1ad14SJiri Benc } 212079e1ad14SJiri Benc return net; 212179e1ad14SJiri Benc } 2122c383edc4SChristian Brauner EXPORT_SYMBOL_GPL(rtnl_get_net_ns_capable); 212379e1ad14SJiri Benc 2124905cf0abSDavid Ahern static int rtnl_valid_dump_ifinfo_req(const struct nlmsghdr *nlh, 2125905cf0abSDavid Ahern bool strict_check, struct nlattr **tb, 2126905cf0abSDavid Ahern struct netlink_ext_ack *extack) 2127905cf0abSDavid Ahern { 2128905cf0abSDavid Ahern int hdrlen; 2129905cf0abSDavid Ahern 2130905cf0abSDavid Ahern if (strict_check) { 2131905cf0abSDavid Ahern struct ifinfomsg *ifm; 2132905cf0abSDavid Ahern 2133905cf0abSDavid Ahern if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { 2134905cf0abSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid header for link dump"); 2135905cf0abSDavid Ahern return -EINVAL; 2136905cf0abSDavid Ahern } 2137905cf0abSDavid Ahern 2138905cf0abSDavid Ahern ifm = nlmsg_data(nlh); 2139905cf0abSDavid Ahern if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags || 2140905cf0abSDavid Ahern ifm->ifi_change) { 2141905cf0abSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid values in header for link dump request"); 2142905cf0abSDavid Ahern return -EINVAL; 2143905cf0abSDavid Ahern } 2144905cf0abSDavid Ahern if (ifm->ifi_index) { 2145905cf0abSDavid Ahern NL_SET_ERR_MSG(extack, "Filter by device index not supported for link dumps"); 2146905cf0abSDavid Ahern return -EINVAL; 2147905cf0abSDavid Ahern } 2148905cf0abSDavid Ahern 21498cb08174SJohannes Berg return nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, 21508cb08174SJohannes Berg IFLA_MAX, ifla_policy, 21518cb08174SJohannes Berg extack); 2152905cf0abSDavid Ahern } 2153905cf0abSDavid Ahern 2154905cf0abSDavid Ahern /* A hack to preserve kernel<->userspace interface. 2155905cf0abSDavid Ahern * The correct header is ifinfomsg. It is consistent with rtnl_getlink. 2156905cf0abSDavid Ahern * However, before Linux v3.9 the code here assumed rtgenmsg and that's 2157905cf0abSDavid Ahern * what iproute2 < v3.9.0 used. 2158905cf0abSDavid Ahern * We can detect the old iproute2. Even including the IFLA_EXT_MASK 2159905cf0abSDavid Ahern * attribute, its netlink message is shorter than struct ifinfomsg. 2160905cf0abSDavid Ahern */ 2161905cf0abSDavid Ahern hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ? 2162905cf0abSDavid Ahern sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); 2163905cf0abSDavid Ahern 21648cb08174SJohannes Berg return nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, 21658cb08174SJohannes Berg extack); 2166905cf0abSDavid Ahern } 2167905cf0abSDavid Ahern 2168b60c5115SThomas Graf static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 21691da177e4SLinus Torvalds { 2170905cf0abSDavid Ahern struct netlink_ext_ack *extack = cb->extack; 2171905cf0abSDavid Ahern const struct nlmsghdr *nlh = cb->nlh; 21723b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 217379e1ad14SJiri Benc struct net *tgt_net = net; 21747c28bd0bSEric Dumazet int h, s_h; 21757c28bd0bSEric Dumazet int idx = 0, s_idx; 21761da177e4SLinus Torvalds struct net_device *dev; 21777c28bd0bSEric Dumazet struct hlist_head *head; 2178115c9b81SGreg Rose struct nlattr *tb[IFLA_MAX+1]; 2179115c9b81SGreg Rose u32 ext_filter_mask = 0; 2180dc599f76SDavid Ahern const struct rtnl_link_ops *kind_ops = NULL; 2181dc599f76SDavid Ahern unsigned int flags = NLM_F_MULTI; 2182dc599f76SDavid Ahern int master_idx = 0; 218379e1ad14SJiri Benc int netnsid = -1; 2184905cf0abSDavid Ahern int err, i; 21851da177e4SLinus Torvalds 21867c28bd0bSEric Dumazet s_h = cb->args[0]; 21877c28bd0bSEric Dumazet s_idx = cb->args[1]; 21887c28bd0bSEric Dumazet 2189905cf0abSDavid Ahern err = rtnl_valid_dump_ifinfo_req(nlh, cb->strict_check, tb, extack); 2190905cf0abSDavid Ahern if (err < 0) { 2191905cf0abSDavid Ahern if (cb->strict_check) 2192905cf0abSDavid Ahern return err; 2193e5eca6d4SMichal Schmidt 2194905cf0abSDavid Ahern goto walk_entries; 219579e1ad14SJiri Benc } 219679e1ad14SJiri Benc 2197905cf0abSDavid Ahern for (i = 0; i <= IFLA_MAX; ++i) { 2198905cf0abSDavid Ahern if (!tb[i]) 2199905cf0abSDavid Ahern continue; 2200dc599f76SDavid Ahern 2201905cf0abSDavid Ahern /* new attributes should only be added with strict checking */ 2202905cf0abSDavid Ahern switch (i) { 2203905cf0abSDavid Ahern case IFLA_TARGET_NETNSID: 2204905cf0abSDavid Ahern netnsid = nla_get_s32(tb[i]); 2205905cf0abSDavid Ahern tgt_net = rtnl_get_net_ns_capable(skb->sk, netnsid); 2206905cf0abSDavid Ahern if (IS_ERR(tgt_net)) { 2207905cf0abSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid target network namespace id"); 2208905cf0abSDavid Ahern return PTR_ERR(tgt_net); 2209905cf0abSDavid Ahern } 2210905cf0abSDavid Ahern break; 2211905cf0abSDavid Ahern case IFLA_EXT_MASK: 2212905cf0abSDavid Ahern ext_filter_mask = nla_get_u32(tb[i]); 2213905cf0abSDavid Ahern break; 2214905cf0abSDavid Ahern case IFLA_MASTER: 2215905cf0abSDavid Ahern master_idx = nla_get_u32(tb[i]); 2216905cf0abSDavid Ahern break; 2217905cf0abSDavid Ahern case IFLA_LINKINFO: 2218905cf0abSDavid Ahern kind_ops = linkinfo_to_kind_ops(tb[i]); 2219905cf0abSDavid Ahern break; 2220905cf0abSDavid Ahern default: 2221905cf0abSDavid Ahern if (cb->strict_check) { 2222905cf0abSDavid Ahern NL_SET_ERR_MSG(extack, "Unsupported attribute in link dump request"); 2223905cf0abSDavid Ahern return -EINVAL; 2224905cf0abSDavid Ahern } 2225905cf0abSDavid Ahern } 2226905cf0abSDavid Ahern } 2227dc599f76SDavid Ahern 2228dc599f76SDavid Ahern if (master_idx || kind_ops) 2229dc599f76SDavid Ahern flags |= NLM_F_DUMP_FILTERED; 2230115c9b81SGreg Rose 2231905cf0abSDavid Ahern walk_entries: 22327c28bd0bSEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 22337562f876SPavel Emelianov idx = 0; 223479e1ad14SJiri Benc head = &tgt_net->dev_index_head[h]; 2235cac5e65eSEric Dumazet hlist_for_each_entry(dev, head, index_hlist) { 2236dc599f76SDavid Ahern if (link_dump_filtered(dev, master_idx, kind_ops)) 22373f0ae05dSZhang Shengju goto cont; 22381da177e4SLinus Torvalds if (idx < s_idx) 22397562f876SPavel Emelianov goto cont; 224079e1ad14SJiri Benc err = rtnl_fill_ifinfo(skb, dev, net, 224179e1ad14SJiri Benc RTM_NEWLINK, 224215e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 2243905cf0abSDavid Ahern nlh->nlmsg_seq, 0, flags, 224438e01b30SNicolas Dichtel ext_filter_mask, 0, NULL, 0, 2245d4e4fdf9SGuillaume Nault netnsid, GFP_KERNEL); 2246973462bbSDavid Gibson 2247f6c5775fSDavid Ahern if (err < 0) { 2248f6c5775fSDavid Ahern if (likely(skb->len)) 22497c28bd0bSEric Dumazet goto out; 22504e985adaSThomas Graf 2251f6c5775fSDavid Ahern goto out_err; 2252f6c5775fSDavid Ahern } 22537562f876SPavel Emelianov cont: 22547562f876SPavel Emelianov idx++; 22551da177e4SLinus Torvalds } 22567c28bd0bSEric Dumazet } 22577c28bd0bSEric Dumazet out: 2258f6c5775fSDavid Ahern err = skb->len; 2259f6c5775fSDavid Ahern out_err: 22607c28bd0bSEric Dumazet cb->args[1] = idx; 22617c28bd0bSEric Dumazet cb->args[0] = h; 2262a9ecb0cbSzhang kai cb->seq = tgt_net->dev_base_seq; 2263d0225784SJakub Sitnicki nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 226479e1ad14SJiri Benc if (netnsid >= 0) 226579e1ad14SJiri Benc put_net(tgt_net); 22661da177e4SLinus Torvalds 2267f6c5775fSDavid Ahern return err; 22681da177e4SLinus Torvalds } 22691da177e4SLinus Torvalds 2270fceb6435SJohannes Berg int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len, 2271fceb6435SJohannes Berg struct netlink_ext_ack *exterr) 2272f7b12606SJiri Pirko { 22738cb08174SJohannes Berg return nla_parse_deprecated(tb, IFLA_MAX, head, len, ifla_policy, 22748cb08174SJohannes Berg exterr); 2275f7b12606SJiri Pirko } 2276f7b12606SJiri Pirko EXPORT_SYMBOL(rtnl_nla_parse_ifla); 227757b61080SScott Feldman 227881adee47SEric W. Biederman struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[]) 227981adee47SEric W. Biederman { 228081adee47SEric W. Biederman struct net *net; 228181adee47SEric W. Biederman /* Examine the link attributes and figure out which 228281adee47SEric W. Biederman * network namespace we are talking about. 228381adee47SEric W. Biederman */ 228481adee47SEric W. Biederman if (tb[IFLA_NET_NS_PID]) 228581adee47SEric W. Biederman net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID])); 2286f0630529SEric W. Biederman else if (tb[IFLA_NET_NS_FD]) 2287f0630529SEric W. Biederman net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD])); 228881adee47SEric W. Biederman else 228981adee47SEric W. Biederman net = get_net(src_net); 229081adee47SEric W. Biederman return net; 229181adee47SEric W. Biederman } 229281adee47SEric W. Biederman EXPORT_SYMBOL(rtnl_link_get_net); 229381adee47SEric W. Biederman 22947c4f63baSChristian Brauner /* Figure out which network namespace we are talking about by 22957c4f63baSChristian Brauner * examining the link attributes in the following order: 22967c4f63baSChristian Brauner * 22977c4f63baSChristian Brauner * 1. IFLA_NET_NS_PID 22987c4f63baSChristian Brauner * 2. IFLA_NET_NS_FD 22997e4a8d5aSChristian Brauner * 3. IFLA_TARGET_NETNSID 23007c4f63baSChristian Brauner */ 23017c4f63baSChristian Brauner static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net, 23027c4f63baSChristian Brauner struct nlattr *tb[]) 23037c4f63baSChristian Brauner { 23047c4f63baSChristian Brauner struct net *net; 23057c4f63baSChristian Brauner 23067c4f63baSChristian Brauner if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) 23077c4f63baSChristian Brauner return rtnl_link_get_net(src_net, tb); 23087c4f63baSChristian Brauner 23097e4a8d5aSChristian Brauner if (!tb[IFLA_TARGET_NETNSID]) 23107c4f63baSChristian Brauner return get_net(src_net); 23117c4f63baSChristian Brauner 23127e4a8d5aSChristian Brauner net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_TARGET_NETNSID])); 23137c4f63baSChristian Brauner if (!net) 23147c4f63baSChristian Brauner return ERR_PTR(-EINVAL); 23157c4f63baSChristian Brauner 23167c4f63baSChristian Brauner return net; 23177c4f63baSChristian Brauner } 23187c4f63baSChristian Brauner 23197c4f63baSChristian Brauner static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb, 23207c4f63baSChristian Brauner struct net *src_net, 23217c4f63baSChristian Brauner struct nlattr *tb[], int cap) 23227c4f63baSChristian Brauner { 23237c4f63baSChristian Brauner struct net *net; 23247c4f63baSChristian Brauner 23257c4f63baSChristian Brauner net = rtnl_link_get_net_by_nlattr(src_net, tb); 23267c4f63baSChristian Brauner if (IS_ERR(net)) 23277c4f63baSChristian Brauner return net; 23287c4f63baSChristian Brauner 23297c4f63baSChristian Brauner if (!netlink_ns_capable(skb, net->user_ns, cap)) { 23307c4f63baSChristian Brauner put_net(net); 23317c4f63baSChristian Brauner return ERR_PTR(-EPERM); 23327c4f63baSChristian Brauner } 23337c4f63baSChristian Brauner 23347c4f63baSChristian Brauner return net; 23357c4f63baSChristian Brauner } 23367c4f63baSChristian Brauner 23374ff66caeSChristian Brauner /* Verify that rtnetlink requests do not pass additional properties 23384ff66caeSChristian Brauner * potentially referring to different network namespaces. 23394ff66caeSChristian Brauner */ 23404ff66caeSChristian Brauner static int rtnl_ensure_unique_netns(struct nlattr *tb[], 23414ff66caeSChristian Brauner struct netlink_ext_ack *extack, 23424ff66caeSChristian Brauner bool netns_id_only) 23434ff66caeSChristian Brauner { 23444ff66caeSChristian Brauner 23454ff66caeSChristian Brauner if (netns_id_only) { 23464ff66caeSChristian Brauner if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD]) 23474ff66caeSChristian Brauner return 0; 23484ff66caeSChristian Brauner 23494ff66caeSChristian Brauner NL_SET_ERR_MSG(extack, "specified netns attribute not supported"); 23504ff66caeSChristian Brauner return -EOPNOTSUPP; 23514ff66caeSChristian Brauner } 23524ff66caeSChristian Brauner 23537e4a8d5aSChristian Brauner if (tb[IFLA_TARGET_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])) 23544ff66caeSChristian Brauner goto invalid_attr; 23554ff66caeSChristian Brauner 23567e4a8d5aSChristian Brauner if (tb[IFLA_NET_NS_PID] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_FD])) 23574ff66caeSChristian Brauner goto invalid_attr; 23584ff66caeSChristian Brauner 23597e4a8d5aSChristian Brauner if (tb[IFLA_NET_NS_FD] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_PID])) 23604ff66caeSChristian Brauner goto invalid_attr; 23614ff66caeSChristian Brauner 23624ff66caeSChristian Brauner return 0; 23634ff66caeSChristian Brauner 23644ff66caeSChristian Brauner invalid_attr: 23654ff66caeSChristian Brauner NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified"); 23664ff66caeSChristian Brauner return -EINVAL; 23674ff66caeSChristian Brauner } 23684ff66caeSChristian Brauner 2369a14857c2SBin Chen static int rtnl_set_vf_rate(struct net_device *dev, int vf, int min_tx_rate, 2370a14857c2SBin Chen int max_tx_rate) 2371a14857c2SBin Chen { 2372a14857c2SBin Chen const struct net_device_ops *ops = dev->netdev_ops; 2373a14857c2SBin Chen 2374a14857c2SBin Chen if (!ops->ndo_set_vf_rate) 2375a14857c2SBin Chen return -EOPNOTSUPP; 2376a14857c2SBin Chen if (max_tx_rate && max_tx_rate < min_tx_rate) 2377a14857c2SBin Chen return -EINVAL; 2378a14857c2SBin Chen 2379a14857c2SBin Chen return ops->ndo_set_vf_rate(dev, vf, min_tx_rate, max_tx_rate); 2380a14857c2SBin Chen } 2381a14857c2SBin Chen 23828679c31eSRocco Yue static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[], 23838679c31eSRocco Yue struct netlink_ext_ack *extack) 23841840bb13SThomas Graf { 23851840bb13SThomas Graf if (tb[IFLA_ADDRESS] && 23861840bb13SThomas Graf nla_len(tb[IFLA_ADDRESS]) < dev->addr_len) 23871840bb13SThomas Graf return -EINVAL; 23881840bb13SThomas Graf 23891840bb13SThomas Graf if (tb[IFLA_BROADCAST] && 23901840bb13SThomas Graf nla_len(tb[IFLA_BROADCAST]) < dev->addr_len) 23911840bb13SThomas Graf return -EINVAL; 2392fef5b228SXin Long 2393fef5b228SXin Long if (tb[IFLA_GSO_MAX_SIZE] && 2394fef5b228SXin Long nla_get_u32(tb[IFLA_GSO_MAX_SIZE]) > dev->tso_max_size) { 2395fef5b228SXin Long NL_SET_ERR_MSG(extack, "too big gso_max_size"); 2396fef5b228SXin Long return -EINVAL; 2397fef5b228SXin Long } 2398fef5b228SXin Long 2399fef5b228SXin Long if (tb[IFLA_GSO_MAX_SEGS] && 2400fef5b228SXin Long (nla_get_u32(tb[IFLA_GSO_MAX_SEGS]) > GSO_MAX_SEGS || 2401fef5b228SXin Long nla_get_u32(tb[IFLA_GSO_MAX_SEGS]) > dev->tso_max_segs)) { 2402fef5b228SXin Long NL_SET_ERR_MSG(extack, "too big gso_max_segs"); 2403fef5b228SXin Long return -EINVAL; 2404fef5b228SXin Long } 2405fef5b228SXin Long 240665d6914eSXin Long if (tb[IFLA_GRO_MAX_SIZE] && 240765d6914eSXin Long nla_get_u32(tb[IFLA_GRO_MAX_SIZE]) > GRO_MAX_SIZE) { 240865d6914eSXin Long NL_SET_ERR_MSG(extack, "too big gro_max_size"); 240965d6914eSXin Long return -EINVAL; 241065d6914eSXin Long } 241165d6914eSXin Long 2412fef5b228SXin Long if (tb[IFLA_GSO_IPV4_MAX_SIZE] && 2413fef5b228SXin Long nla_get_u32(tb[IFLA_GSO_IPV4_MAX_SIZE]) > dev->tso_max_size) { 2414fef5b228SXin Long NL_SET_ERR_MSG(extack, "too big gso_ipv4_max_size"); 2415fef5b228SXin Long return -EINVAL; 2416fef5b228SXin Long } 241765d6914eSXin Long 241865d6914eSXin Long if (tb[IFLA_GRO_IPV4_MAX_SIZE] && 241965d6914eSXin Long nla_get_u32(tb[IFLA_GRO_IPV4_MAX_SIZE]) > GRO_MAX_SIZE) { 242065d6914eSXin Long NL_SET_ERR_MSG(extack, "too big gro_ipv4_max_size"); 242165d6914eSXin Long return -EINVAL; 242265d6914eSXin Long } 24231840bb13SThomas Graf 2424cf7afbfeSThomas Graf if (tb[IFLA_AF_SPEC]) { 2425cf7afbfeSThomas Graf struct nlattr *af; 2426cf7afbfeSThomas Graf int rem, err; 2427cf7afbfeSThomas Graf 2428cf7afbfeSThomas Graf nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { 2429cf7afbfeSThomas Graf const struct rtnl_af_ops *af_ops; 2430cf7afbfeSThomas Graf 24315fa85a09SFlorian Westphal af_ops = rtnl_af_lookup(nla_type(af)); 2432a100243dSCong Wang if (!af_ops) 2433cf7afbfeSThomas Graf return -EAFNOSUPPORT; 2434cf7afbfeSThomas Graf 2435a100243dSCong Wang if (!af_ops->set_link_af) 2436cf7afbfeSThomas Graf return -EOPNOTSUPP; 2437cf7afbfeSThomas Graf 2438cf7afbfeSThomas Graf if (af_ops->validate_link_af) { 24398679c31eSRocco Yue err = af_ops->validate_link_af(dev, af, extack); 2440a100243dSCong Wang if (err < 0) 2441cf7afbfeSThomas Graf return err; 2442cf7afbfeSThomas Graf } 2443cf7afbfeSThomas Graf } 2444cf7afbfeSThomas Graf } 2445cf7afbfeSThomas Graf 24461840bb13SThomas Graf return 0; 24471840bb13SThomas Graf } 24481840bb13SThomas Graf 2449cc8e27ccSEli Cohen static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt, 2450cc8e27ccSEli Cohen int guid_type) 2451cc8e27ccSEli Cohen { 2452cc8e27ccSEli Cohen const struct net_device_ops *ops = dev->netdev_ops; 2453cc8e27ccSEli Cohen 2454cc8e27ccSEli Cohen return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type); 2455cc8e27ccSEli Cohen } 2456cc8e27ccSEli Cohen 2457cc8e27ccSEli Cohen static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type) 2458cc8e27ccSEli Cohen { 2459cc8e27ccSEli Cohen if (dev->type != ARPHRD_INFINIBAND) 2460cc8e27ccSEli Cohen return -EOPNOTSUPP; 2461cc8e27ccSEli Cohen 2462cc8e27ccSEli Cohen return handle_infiniband_guid(dev, ivt, guid_type); 2463cc8e27ccSEli Cohen } 2464cc8e27ccSEli Cohen 24654f7d2cdfSDaniel Borkmann static int do_setvfinfo(struct net_device *dev, struct nlattr **tb) 2466c02db8c6SChris Wright { 2467c02db8c6SChris Wright const struct net_device_ops *ops = dev->netdev_ops; 24684f7d2cdfSDaniel Borkmann int err = -EINVAL; 2469c02db8c6SChris Wright 24704f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_MAC]) { 24714f7d2cdfSDaniel Borkmann struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]); 24724f7d2cdfSDaniel Borkmann 2473ff08ddbaSDan Carpenter if (ivm->vf >= INT_MAX) 2474ff08ddbaSDan Carpenter return -EINVAL; 2475c02db8c6SChris Wright err = -EOPNOTSUPP; 2476c02db8c6SChris Wright if (ops->ndo_set_vf_mac) 2477c02db8c6SChris Wright err = ops->ndo_set_vf_mac(dev, ivm->vf, 2478c02db8c6SChris Wright ivm->mac); 24794f7d2cdfSDaniel Borkmann if (err < 0) 24804f7d2cdfSDaniel Borkmann return err; 2481c02db8c6SChris Wright } 24824f7d2cdfSDaniel Borkmann 24834f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_VLAN]) { 24844f7d2cdfSDaniel Borkmann struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]); 24854f7d2cdfSDaniel Borkmann 2486ff08ddbaSDan Carpenter if (ivv->vf >= INT_MAX) 2487ff08ddbaSDan Carpenter return -EINVAL; 2488c02db8c6SChris Wright err = -EOPNOTSUPP; 2489c02db8c6SChris Wright if (ops->ndo_set_vf_vlan) 24904f7d2cdfSDaniel Borkmann err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan, 249179aab093SMoshe Shemesh ivv->qos, 249279aab093SMoshe Shemesh htons(ETH_P_8021Q)); 249379aab093SMoshe Shemesh if (err < 0) 249479aab093SMoshe Shemesh return err; 249579aab093SMoshe Shemesh } 249679aab093SMoshe Shemesh 249779aab093SMoshe Shemesh if (tb[IFLA_VF_VLAN_LIST]) { 249879aab093SMoshe Shemesh struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN]; 249979aab093SMoshe Shemesh struct nlattr *attr; 250079aab093SMoshe Shemesh int rem, len = 0; 250179aab093SMoshe Shemesh 250279aab093SMoshe Shemesh err = -EOPNOTSUPP; 250379aab093SMoshe Shemesh if (!ops->ndo_set_vf_vlan) 250479aab093SMoshe Shemesh return err; 250579aab093SMoshe Shemesh 250679aab093SMoshe Shemesh nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) { 250779aab093SMoshe Shemesh if (nla_type(attr) != IFLA_VF_VLAN_INFO || 250879aab093SMoshe Shemesh nla_len(attr) < NLA_HDRLEN) { 250979aab093SMoshe Shemesh return -EINVAL; 251079aab093SMoshe Shemesh } 251179aab093SMoshe Shemesh if (len >= MAX_VLAN_LIST_LEN) 251279aab093SMoshe Shemesh return -EOPNOTSUPP; 251379aab093SMoshe Shemesh ivvl[len] = nla_data(attr); 251479aab093SMoshe Shemesh 251579aab093SMoshe Shemesh len++; 251679aab093SMoshe Shemesh } 2517fa34cd94SArnd Bergmann if (len == 0) 2518fa34cd94SArnd Bergmann return -EINVAL; 2519fa34cd94SArnd Bergmann 2520ff08ddbaSDan Carpenter if (ivvl[0]->vf >= INT_MAX) 2521ff08ddbaSDan Carpenter return -EINVAL; 252279aab093SMoshe Shemesh err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan, 252379aab093SMoshe Shemesh ivvl[0]->qos, ivvl[0]->vlan_proto); 25244f7d2cdfSDaniel Borkmann if (err < 0) 25254f7d2cdfSDaniel Borkmann return err; 2526c02db8c6SChris Wright } 25274f7d2cdfSDaniel Borkmann 25284f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_TX_RATE]) { 25294f7d2cdfSDaniel Borkmann struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]); 2530ed616689SSucheta Chakraborty struct ifla_vf_info ivf; 25314f7d2cdfSDaniel Borkmann 2532ff08ddbaSDan Carpenter if (ivt->vf >= INT_MAX) 2533ff08ddbaSDan Carpenter return -EINVAL; 2534c02db8c6SChris Wright err = -EOPNOTSUPP; 2535ed616689SSucheta Chakraborty if (ops->ndo_get_vf_config) 25364f7d2cdfSDaniel Borkmann err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf); 25374f7d2cdfSDaniel Borkmann if (err < 0) 25384f7d2cdfSDaniel Borkmann return err; 25394f7d2cdfSDaniel Borkmann 2540a14857c2SBin Chen err = rtnl_set_vf_rate(dev, ivt->vf, 2541a14857c2SBin Chen ivf.min_tx_rate, ivt->rate); 25424f7d2cdfSDaniel Borkmann if (err < 0) 25434f7d2cdfSDaniel Borkmann return err; 2544c02db8c6SChris Wright } 25454f7d2cdfSDaniel Borkmann 25464f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_RATE]) { 25474f7d2cdfSDaniel Borkmann struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]); 25484f7d2cdfSDaniel Borkmann 2549ff08ddbaSDan Carpenter if (ivt->vf >= INT_MAX) 2550ff08ddbaSDan Carpenter return -EINVAL; 2551a14857c2SBin Chen 2552a14857c2SBin Chen err = rtnl_set_vf_rate(dev, ivt->vf, 2553a14857c2SBin Chen ivt->min_tx_rate, ivt->max_tx_rate); 25544f7d2cdfSDaniel Borkmann if (err < 0) 25554f7d2cdfSDaniel Borkmann return err; 2556ed616689SSucheta Chakraborty } 25574f7d2cdfSDaniel Borkmann 25584f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_SPOOFCHK]) { 25594f7d2cdfSDaniel Borkmann struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]); 25604f7d2cdfSDaniel Borkmann 2561ff08ddbaSDan Carpenter if (ivs->vf >= INT_MAX) 2562ff08ddbaSDan Carpenter return -EINVAL; 25635f8444a3SGreg Rose err = -EOPNOTSUPP; 25645f8444a3SGreg Rose if (ops->ndo_set_vf_spoofchk) 25655f8444a3SGreg Rose err = ops->ndo_set_vf_spoofchk(dev, ivs->vf, 25665f8444a3SGreg Rose ivs->setting); 25674f7d2cdfSDaniel Borkmann if (err < 0) 25684f7d2cdfSDaniel Borkmann return err; 25695f8444a3SGreg Rose } 25704f7d2cdfSDaniel Borkmann 25714f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_LINK_STATE]) { 25724f7d2cdfSDaniel Borkmann struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]); 25734f7d2cdfSDaniel Borkmann 2574ff08ddbaSDan Carpenter if (ivl->vf >= INT_MAX) 2575ff08ddbaSDan Carpenter return -EINVAL; 25761d8faf48SRony Efraim err = -EOPNOTSUPP; 25771d8faf48SRony Efraim if (ops->ndo_set_vf_link_state) 25781d8faf48SRony Efraim err = ops->ndo_set_vf_link_state(dev, ivl->vf, 25791d8faf48SRony Efraim ivl->link_state); 25804f7d2cdfSDaniel Borkmann if (err < 0) 25814f7d2cdfSDaniel Borkmann return err; 25821d8faf48SRony Efraim } 25834f7d2cdfSDaniel Borkmann 25844f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_RSS_QUERY_EN]) { 258501a3d796SVlad Zolotarov struct ifla_vf_rss_query_en *ivrssq_en; 258601a3d796SVlad Zolotarov 258701a3d796SVlad Zolotarov err = -EOPNOTSUPP; 25884f7d2cdfSDaniel Borkmann ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]); 2589ff08ddbaSDan Carpenter if (ivrssq_en->vf >= INT_MAX) 2590ff08ddbaSDan Carpenter return -EINVAL; 259101a3d796SVlad Zolotarov if (ops->ndo_set_vf_rss_query_en) 25924f7d2cdfSDaniel Borkmann err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf, 259301a3d796SVlad Zolotarov ivrssq_en->setting); 25944f7d2cdfSDaniel Borkmann if (err < 0) 25954f7d2cdfSDaniel Borkmann return err; 259601a3d796SVlad Zolotarov } 25974f7d2cdfSDaniel Borkmann 2598dd461d6aSHiroshi Shimamoto if (tb[IFLA_VF_TRUST]) { 2599dd461d6aSHiroshi Shimamoto struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]); 2600dd461d6aSHiroshi Shimamoto 2601ff08ddbaSDan Carpenter if (ivt->vf >= INT_MAX) 2602ff08ddbaSDan Carpenter return -EINVAL; 2603dd461d6aSHiroshi Shimamoto err = -EOPNOTSUPP; 2604dd461d6aSHiroshi Shimamoto if (ops->ndo_set_vf_trust) 2605dd461d6aSHiroshi Shimamoto err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting); 2606dd461d6aSHiroshi Shimamoto if (err < 0) 2607dd461d6aSHiroshi Shimamoto return err; 2608dd461d6aSHiroshi Shimamoto } 2609dd461d6aSHiroshi Shimamoto 2610cc8e27ccSEli Cohen if (tb[IFLA_VF_IB_NODE_GUID]) { 2611cc8e27ccSEli Cohen struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]); 2612cc8e27ccSEli Cohen 2613ff08ddbaSDan Carpenter if (ivt->vf >= INT_MAX) 2614ff08ddbaSDan Carpenter return -EINVAL; 2615cc8e27ccSEli Cohen if (!ops->ndo_set_vf_guid) 2616cc8e27ccSEli Cohen return -EOPNOTSUPP; 2617cc8e27ccSEli Cohen return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID); 2618cc8e27ccSEli Cohen } 2619cc8e27ccSEli Cohen 2620cc8e27ccSEli Cohen if (tb[IFLA_VF_IB_PORT_GUID]) { 2621cc8e27ccSEli Cohen struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]); 2622cc8e27ccSEli Cohen 2623ff08ddbaSDan Carpenter if (ivt->vf >= INT_MAX) 2624ff08ddbaSDan Carpenter return -EINVAL; 2625cc8e27ccSEli Cohen if (!ops->ndo_set_vf_guid) 2626cc8e27ccSEli Cohen return -EOPNOTSUPP; 2627cc8e27ccSEli Cohen 2628cc8e27ccSEli Cohen return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID); 2629cc8e27ccSEli Cohen } 2630cc8e27ccSEli Cohen 2631c02db8c6SChris Wright return err; 2632c02db8c6SChris Wright } 2633c02db8c6SChris Wright 263433eaf2a6SDavid Ahern static int do_set_master(struct net_device *dev, int ifindex, 263533eaf2a6SDavid Ahern struct netlink_ext_ack *extack) 2636fbaec0eaSJiri Pirko { 2637898e5061SJiri Pirko struct net_device *upper_dev = netdev_master_upper_dev_get(dev); 2638fbaec0eaSJiri Pirko const struct net_device_ops *ops; 2639fbaec0eaSJiri Pirko int err; 2640fbaec0eaSJiri Pirko 2641898e5061SJiri Pirko if (upper_dev) { 2642898e5061SJiri Pirko if (upper_dev->ifindex == ifindex) 2643fbaec0eaSJiri Pirko return 0; 2644898e5061SJiri Pirko ops = upper_dev->netdev_ops; 2645fbaec0eaSJiri Pirko if (ops->ndo_del_slave) { 2646898e5061SJiri Pirko err = ops->ndo_del_slave(upper_dev, dev); 2647fbaec0eaSJiri Pirko if (err) 2648fbaec0eaSJiri Pirko return err; 2649fbaec0eaSJiri Pirko } else { 2650fbaec0eaSJiri Pirko return -EOPNOTSUPP; 2651fbaec0eaSJiri Pirko } 2652fbaec0eaSJiri Pirko } 2653fbaec0eaSJiri Pirko 2654fbaec0eaSJiri Pirko if (ifindex) { 2655898e5061SJiri Pirko upper_dev = __dev_get_by_index(dev_net(dev), ifindex); 2656898e5061SJiri Pirko if (!upper_dev) 2657fbaec0eaSJiri Pirko return -EINVAL; 2658898e5061SJiri Pirko ops = upper_dev->netdev_ops; 2659fbaec0eaSJiri Pirko if (ops->ndo_add_slave) { 266033eaf2a6SDavid Ahern err = ops->ndo_add_slave(upper_dev, dev, extack); 2661fbaec0eaSJiri Pirko if (err) 2662fbaec0eaSJiri Pirko return err; 2663fbaec0eaSJiri Pirko } else { 2664fbaec0eaSJiri Pirko return -EOPNOTSUPP; 2665fbaec0eaSJiri Pirko } 2666fbaec0eaSJiri Pirko } 2667fbaec0eaSJiri Pirko return 0; 2668fbaec0eaSJiri Pirko } 2669fbaec0eaSJiri Pirko 2670829eb208SRoopa Prabhu static const struct nla_policy ifla_proto_down_reason_policy[IFLA_PROTO_DOWN_REASON_VALUE + 1] = { 2671829eb208SRoopa Prabhu [IFLA_PROTO_DOWN_REASON_MASK] = { .type = NLA_U32 }, 2672829eb208SRoopa Prabhu [IFLA_PROTO_DOWN_REASON_VALUE] = { .type = NLA_U32 }, 2673829eb208SRoopa Prabhu }; 2674829eb208SRoopa Prabhu 2675829eb208SRoopa Prabhu static int do_set_proto_down(struct net_device *dev, 2676829eb208SRoopa Prabhu struct nlattr *nl_proto_down, 2677829eb208SRoopa Prabhu struct nlattr *nl_proto_down_reason, 2678829eb208SRoopa Prabhu struct netlink_ext_ack *extack) 2679829eb208SRoopa Prabhu { 2680829eb208SRoopa Prabhu struct nlattr *pdreason[IFLA_PROTO_DOWN_REASON_MAX + 1]; 2681829eb208SRoopa Prabhu unsigned long mask = 0; 2682829eb208SRoopa Prabhu u32 value; 2683829eb208SRoopa Prabhu bool proto_down; 2684829eb208SRoopa Prabhu int err; 2685829eb208SRoopa Prabhu 26862106efdaSJakub Kicinski if (!(dev->priv_flags & IFF_CHANGE_PROTO_DOWN)) { 2687829eb208SRoopa Prabhu NL_SET_ERR_MSG(extack, "Protodown not supported by device"); 2688829eb208SRoopa Prabhu return -EOPNOTSUPP; 2689829eb208SRoopa Prabhu } 2690829eb208SRoopa Prabhu 2691829eb208SRoopa Prabhu if (nl_proto_down_reason) { 2692829eb208SRoopa Prabhu err = nla_parse_nested_deprecated(pdreason, 2693829eb208SRoopa Prabhu IFLA_PROTO_DOWN_REASON_MAX, 2694829eb208SRoopa Prabhu nl_proto_down_reason, 2695829eb208SRoopa Prabhu ifla_proto_down_reason_policy, 2696829eb208SRoopa Prabhu NULL); 2697829eb208SRoopa Prabhu if (err < 0) 2698829eb208SRoopa Prabhu return err; 2699829eb208SRoopa Prabhu 2700829eb208SRoopa Prabhu if (!pdreason[IFLA_PROTO_DOWN_REASON_VALUE]) { 2701829eb208SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid protodown reason value"); 2702829eb208SRoopa Prabhu return -EINVAL; 2703829eb208SRoopa Prabhu } 2704829eb208SRoopa Prabhu 2705829eb208SRoopa Prabhu value = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_VALUE]); 2706829eb208SRoopa Prabhu 2707829eb208SRoopa Prabhu if (pdreason[IFLA_PROTO_DOWN_REASON_MASK]) 2708829eb208SRoopa Prabhu mask = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_MASK]); 2709829eb208SRoopa Prabhu 2710829eb208SRoopa Prabhu dev_change_proto_down_reason(dev, mask, value); 2711829eb208SRoopa Prabhu } 2712829eb208SRoopa Prabhu 2713829eb208SRoopa Prabhu if (nl_proto_down) { 2714829eb208SRoopa Prabhu proto_down = nla_get_u8(nl_proto_down); 2715829eb208SRoopa Prabhu 2716d467d0bcSZheng Yongjun /* Don't turn off protodown if there are active reasons */ 2717829eb208SRoopa Prabhu if (!proto_down && dev->proto_down_reason) { 2718829eb208SRoopa Prabhu NL_SET_ERR_MSG(extack, "Cannot clear protodown, active reasons"); 2719829eb208SRoopa Prabhu return -EBUSY; 2720829eb208SRoopa Prabhu } 2721829eb208SRoopa Prabhu err = dev_change_proto_down(dev, 2722829eb208SRoopa Prabhu proto_down); 2723829eb208SRoopa Prabhu if (err) 2724829eb208SRoopa Prabhu return err; 2725829eb208SRoopa Prabhu } 2726829eb208SRoopa Prabhu 2727829eb208SRoopa Prabhu return 0; 2728829eb208SRoopa Prabhu } 2729829eb208SRoopa Prabhu 273090c325e3SNicolas Dichtel #define DO_SETLINK_MODIFIED 0x01 2731ba998906SNicolas Dichtel /* notify flag means notify + modified. */ 2732ba998906SNicolas Dichtel #define DO_SETLINK_NOTIFY 0x03 273390f62cf3SEric W. Biederman static int do_setlink(const struct sk_buff *skb, 273490f62cf3SEric W. Biederman struct net_device *dev, struct ifinfomsg *ifm, 2735ddf9f970SJakub Kicinski struct netlink_ext_ack *extack, 27365ea08b52SFlorent Fourcot struct nlattr **tb, int status) 27370157f60cSPatrick McHardy { 2738d314774cSStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops; 27395ea08b52SFlorent Fourcot char ifname[IFNAMSIZ]; 27400157f60cSPatrick McHardy int err; 27410157f60cSPatrick McHardy 27425ea08b52SFlorent Fourcot if (tb[IFLA_IFNAME]) 27435ea08b52SFlorent Fourcot nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 27445ea08b52SFlorent Fourcot else 27455ea08b52SFlorent Fourcot ifname[0] = '\0'; 27465ea08b52SFlorent Fourcot 27477e4a8d5aSChristian Brauner if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_TARGET_NETNSID]) { 27485ea08b52SFlorent Fourcot const char *pat = ifname[0] ? ifname : NULL; 2749eeb85a14SAndrei Vagin struct net *net; 2750eeb85a14SAndrei Vagin int new_ifindex; 2751eeb85a14SAndrei Vagin 2752eeb85a14SAndrei Vagin net = rtnl_link_get_net_capable(skb, dev_net(dev), 27537c4f63baSChristian Brauner tb, CAP_NET_ADMIN); 2754d8a5ec67SEric W. Biederman if (IS_ERR(net)) { 2755d8a5ec67SEric W. Biederman err = PTR_ERR(net); 2756d8a5ec67SEric W. Biederman goto errout; 2757d8a5ec67SEric W. Biederman } 27587c4f63baSChristian Brauner 2759eeb85a14SAndrei Vagin if (tb[IFLA_NEW_IFINDEX]) 2760eeb85a14SAndrei Vagin new_ifindex = nla_get_s32(tb[IFLA_NEW_IFINDEX]); 2761eeb85a14SAndrei Vagin else 2762eeb85a14SAndrei Vagin new_ifindex = 0; 2763eeb85a14SAndrei Vagin 276496a6b93bSAndrey Ignatov err = __dev_change_net_namespace(dev, net, pat, new_ifindex); 2765d8a5ec67SEric W. Biederman put_net(net); 2766d8a5ec67SEric W. Biederman if (err) 2767d8a5ec67SEric W. Biederman goto errout; 276890c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 2769d8a5ec67SEric W. Biederman } 2770d8a5ec67SEric W. Biederman 27710157f60cSPatrick McHardy if (tb[IFLA_MAP]) { 27720157f60cSPatrick McHardy struct rtnl_link_ifmap *u_map; 27730157f60cSPatrick McHardy struct ifmap k_map; 27740157f60cSPatrick McHardy 2775d314774cSStephen Hemminger if (!ops->ndo_set_config) { 27760157f60cSPatrick McHardy err = -EOPNOTSUPP; 27770157f60cSPatrick McHardy goto errout; 27780157f60cSPatrick McHardy } 27790157f60cSPatrick McHardy 27800157f60cSPatrick McHardy if (!netif_device_present(dev)) { 27810157f60cSPatrick McHardy err = -ENODEV; 27820157f60cSPatrick McHardy goto errout; 27830157f60cSPatrick McHardy } 27840157f60cSPatrick McHardy 27850157f60cSPatrick McHardy u_map = nla_data(tb[IFLA_MAP]); 27860157f60cSPatrick McHardy k_map.mem_start = (unsigned long) u_map->mem_start; 27870157f60cSPatrick McHardy k_map.mem_end = (unsigned long) u_map->mem_end; 27880157f60cSPatrick McHardy k_map.base_addr = (unsigned short) u_map->base_addr; 27890157f60cSPatrick McHardy k_map.irq = (unsigned char) u_map->irq; 27900157f60cSPatrick McHardy k_map.dma = (unsigned char) u_map->dma; 27910157f60cSPatrick McHardy k_map.port = (unsigned char) u_map->port; 27920157f60cSPatrick McHardy 2793d314774cSStephen Hemminger err = ops->ndo_set_config(dev, &k_map); 27940157f60cSPatrick McHardy if (err < 0) 27950157f60cSPatrick McHardy goto errout; 27960157f60cSPatrick McHardy 2797ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 27980157f60cSPatrick McHardy } 27990157f60cSPatrick McHardy 28000157f60cSPatrick McHardy if (tb[IFLA_ADDRESS]) { 28010157f60cSPatrick McHardy struct sockaddr *sa; 28020157f60cSPatrick McHardy int len; 28030157f60cSPatrick McHardy 2804153711f9SWANG Cong len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len, 2805153711f9SWANG Cong sizeof(*sa)); 28060157f60cSPatrick McHardy sa = kmalloc(len, GFP_KERNEL); 28070157f60cSPatrick McHardy if (!sa) { 28080157f60cSPatrick McHardy err = -ENOMEM; 28090157f60cSPatrick McHardy goto errout; 28100157f60cSPatrick McHardy } 28110157f60cSPatrick McHardy sa->sa_family = dev->type; 28120157f60cSPatrick McHardy memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]), 28130157f60cSPatrick McHardy dev->addr_len); 28143b23a32aSCong Wang err = dev_set_mac_address_user(dev, sa, extack); 28150157f60cSPatrick McHardy kfree(sa); 28160157f60cSPatrick McHardy if (err) 28170157f60cSPatrick McHardy goto errout; 281890c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 28190157f60cSPatrick McHardy } 28200157f60cSPatrick McHardy 28210157f60cSPatrick McHardy if (tb[IFLA_MTU]) { 28227a4c53beSStephen Hemminger err = dev_set_mtu_ext(dev, nla_get_u32(tb[IFLA_MTU]), extack); 28230157f60cSPatrick McHardy if (err < 0) 28240157f60cSPatrick McHardy goto errout; 282590c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 28260157f60cSPatrick McHardy } 28270157f60cSPatrick McHardy 2828cbda10faSVlad Dogaru if (tb[IFLA_GROUP]) { 2829cbda10faSVlad Dogaru dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); 2830ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 2831cbda10faSVlad Dogaru } 2832cbda10faSVlad Dogaru 28330157f60cSPatrick McHardy /* 28340157f60cSPatrick McHardy * Interface selected by interface index but interface 28350157f60cSPatrick McHardy * name provided implies that a name change has been 28360157f60cSPatrick McHardy * requested. 28370157f60cSPatrick McHardy */ 28380157f60cSPatrick McHardy if (ifm->ifi_index > 0 && ifname[0]) { 28390157f60cSPatrick McHardy err = dev_change_name(dev, ifname); 28400157f60cSPatrick McHardy if (err < 0) 28410157f60cSPatrick McHardy goto errout; 284290c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 28430157f60cSPatrick McHardy } 28440157f60cSPatrick McHardy 28450b815a1aSStephen Hemminger if (tb[IFLA_IFALIAS]) { 28460b815a1aSStephen Hemminger err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]), 28470b815a1aSStephen Hemminger nla_len(tb[IFLA_IFALIAS])); 28480b815a1aSStephen Hemminger if (err < 0) 28490b815a1aSStephen Hemminger goto errout; 2850ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 28510b815a1aSStephen Hemminger } 28520b815a1aSStephen Hemminger 28530157f60cSPatrick McHardy if (tb[IFLA_BROADCAST]) { 28540157f60cSPatrick McHardy nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len); 2855e7c3273eSJiri Pirko call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); 28560157f60cSPatrick McHardy } 28570157f60cSPatrick McHardy 2858fbaec0eaSJiri Pirko if (tb[IFLA_MASTER]) { 285933eaf2a6SDavid Ahern err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack); 2860fbaec0eaSJiri Pirko if (err) 2861fbaec0eaSJiri Pirko goto errout; 286290c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 2863fbaec0eaSJiri Pirko } 2864fbaec0eaSJiri Pirko 2865a4abfa62SPhil Sutter if (ifm->ifi_flags || ifm->ifi_change) { 2866a4abfa62SPhil Sutter err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm), 2867a4abfa62SPhil Sutter extack); 2868a4abfa62SPhil Sutter if (err < 0) 2869a4abfa62SPhil Sutter goto errout; 2870a4abfa62SPhil Sutter } 2871a4abfa62SPhil Sutter 28729a57247fSJiri Pirko if (tb[IFLA_CARRIER]) { 28739a57247fSJiri Pirko err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER])); 28749a57247fSJiri Pirko if (err) 28759a57247fSJiri Pirko goto errout; 287690c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 28779a57247fSJiri Pirko } 28789a57247fSJiri Pirko 28795d1180fcSNicolas Dichtel if (tb[IFLA_TXQLEN]) { 28800cd29503SAlexey Dobriyan unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]); 28815d1180fcSNicolas Dichtel 28826a643ddbSCong Wang err = dev_change_tx_queue_len(dev, value); 28836a643ddbSCong Wang if (err) 288408294a26SJason Wang goto errout; 28852d7f669bSXin Long status |= DO_SETLINK_MODIFIED; 288608294a26SJason Wang } 28870157f60cSPatrick McHardy 288846e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SIZE]) { 288946e6b992SStephen Hemminger u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]); 289046e6b992SStephen Hemminger 289146e6b992SStephen Hemminger if (dev->gso_max_size ^ max_size) { 289246e6b992SStephen Hemminger netif_set_gso_max_size(dev, max_size); 289346e6b992SStephen Hemminger status |= DO_SETLINK_MODIFIED; 289446e6b992SStephen Hemminger } 289546e6b992SStephen Hemminger } 289646e6b992SStephen Hemminger 289746e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SEGS]) { 289846e6b992SStephen Hemminger u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]); 289946e6b992SStephen Hemminger 290046e6b992SStephen Hemminger if (dev->gso_max_segs ^ max_segs) { 29016d872df3SEric Dumazet netif_set_gso_max_segs(dev, max_segs); 290246e6b992SStephen Hemminger status |= DO_SETLINK_MODIFIED; 290346e6b992SStephen Hemminger } 290446e6b992SStephen Hemminger } 290546e6b992SStephen Hemminger 2906eac1b93cSCoco Li if (tb[IFLA_GRO_MAX_SIZE]) { 2907eac1b93cSCoco Li u32 gro_max_size = nla_get_u32(tb[IFLA_GRO_MAX_SIZE]); 2908eac1b93cSCoco Li 2909eac1b93cSCoco Li if (dev->gro_max_size ^ gro_max_size) { 2910eac1b93cSCoco Li netif_set_gro_max_size(dev, gro_max_size); 2911eac1b93cSCoco Li status |= DO_SETLINK_MODIFIED; 2912eac1b93cSCoco Li } 2913eac1b93cSCoco Li } 2914eac1b93cSCoco Li 29159eefedd5SXin Long if (tb[IFLA_GSO_IPV4_MAX_SIZE]) { 29169eefedd5SXin Long u32 max_size = nla_get_u32(tb[IFLA_GSO_IPV4_MAX_SIZE]); 29179eefedd5SXin Long 29189eefedd5SXin Long if (dev->gso_ipv4_max_size ^ max_size) { 29199eefedd5SXin Long netif_set_gso_ipv4_max_size(dev, max_size); 29209eefedd5SXin Long status |= DO_SETLINK_MODIFIED; 29219eefedd5SXin Long } 29229eefedd5SXin Long } 29239eefedd5SXin Long 29249eefedd5SXin Long if (tb[IFLA_GRO_IPV4_MAX_SIZE]) { 29259eefedd5SXin Long u32 gro_max_size = nla_get_u32(tb[IFLA_GRO_IPV4_MAX_SIZE]); 29269eefedd5SXin Long 29279eefedd5SXin Long if (dev->gro_ipv4_max_size ^ gro_max_size) { 29289eefedd5SXin Long netif_set_gro_ipv4_max_size(dev, gro_max_size); 29299eefedd5SXin Long status |= DO_SETLINK_MODIFIED; 29309eefedd5SXin Long } 29319eefedd5SXin Long } 29329eefedd5SXin Long 29330157f60cSPatrick McHardy if (tb[IFLA_OPERSTATE]) 293493b2d4a2SDavid S. Miller set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 29350157f60cSPatrick McHardy 29360157f60cSPatrick McHardy if (tb[IFLA_LINKMODE]) { 29371889b0e7SNicolas Dichtel unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]); 29381889b0e7SNicolas Dichtel 2939fd888e85SSebastian Andrzej Siewior write_lock(&dev_base_lock); 29401889b0e7SNicolas Dichtel if (dev->link_mode ^ value) 2941ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 29421889b0e7SNicolas Dichtel dev->link_mode = value; 2943fd888e85SSebastian Andrzej Siewior write_unlock(&dev_base_lock); 29440157f60cSPatrick McHardy } 29450157f60cSPatrick McHardy 2946c02db8c6SChris Wright if (tb[IFLA_VFINFO_LIST]) { 29474f7d2cdfSDaniel Borkmann struct nlattr *vfinfo[IFLA_VF_MAX + 1]; 2948c02db8c6SChris Wright struct nlattr *attr; 2949c02db8c6SChris Wright int rem; 29504f7d2cdfSDaniel Borkmann 2951c02db8c6SChris Wright nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) { 29524f7d2cdfSDaniel Borkmann if (nla_type(attr) != IFLA_VF_INFO || 29534f7d2cdfSDaniel Borkmann nla_len(attr) < NLA_HDRLEN) { 2954253683bbSDavid Howells err = -EINVAL; 2955c02db8c6SChris Wright goto errout; 2956253683bbSDavid Howells } 29578cb08174SJohannes Berg err = nla_parse_nested_deprecated(vfinfo, IFLA_VF_MAX, 29588cb08174SJohannes Berg attr, 29598cb08174SJohannes Berg ifla_vf_policy, 29608cb08174SJohannes Berg NULL); 29614f7d2cdfSDaniel Borkmann if (err < 0) 29624f7d2cdfSDaniel Borkmann goto errout; 29634f7d2cdfSDaniel Borkmann err = do_setvfinfo(dev, vfinfo); 2964ebc08a6fSWilliams, Mitch A if (err < 0) 2965ebc08a6fSWilliams, Mitch A goto errout; 2966ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 2967ebc08a6fSWilliams, Mitch A } 2968ebc08a6fSWilliams, Mitch A } 29690157f60cSPatrick McHardy err = 0; 29700157f60cSPatrick McHardy 297157b61080SScott Feldman if (tb[IFLA_VF_PORTS]) { 297257b61080SScott Feldman struct nlattr *port[IFLA_PORT_MAX+1]; 297357b61080SScott Feldman struct nlattr *attr; 297457b61080SScott Feldman int vf; 297557b61080SScott Feldman int rem; 297657b61080SScott Feldman 297757b61080SScott Feldman err = -EOPNOTSUPP; 297857b61080SScott Feldman if (!ops->ndo_set_vf_port) 297957b61080SScott Feldman goto errout; 298057b61080SScott Feldman 298157b61080SScott Feldman nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) { 2982035d210fSDaniel Borkmann if (nla_type(attr) != IFLA_VF_PORT || 2983035d210fSDaniel Borkmann nla_len(attr) < NLA_HDRLEN) { 2984035d210fSDaniel Borkmann err = -EINVAL; 2985035d210fSDaniel Borkmann goto errout; 2986035d210fSDaniel Borkmann } 29878cb08174SJohannes Berg err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX, 29888cb08174SJohannes Berg attr, 29898cb08174SJohannes Berg ifla_port_policy, 29908cb08174SJohannes Berg NULL); 299157b61080SScott Feldman if (err < 0) 299257b61080SScott Feldman goto errout; 299357b61080SScott Feldman if (!port[IFLA_PORT_VF]) { 299457b61080SScott Feldman err = -EOPNOTSUPP; 299557b61080SScott Feldman goto errout; 299657b61080SScott Feldman } 299757b61080SScott Feldman vf = nla_get_u32(port[IFLA_PORT_VF]); 299857b61080SScott Feldman err = ops->ndo_set_vf_port(dev, vf, port); 299957b61080SScott Feldman if (err < 0) 300057b61080SScott Feldman goto errout; 3001ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 300257b61080SScott Feldman } 300357b61080SScott Feldman } 300457b61080SScott Feldman err = 0; 300557b61080SScott Feldman 300657b61080SScott Feldman if (tb[IFLA_PORT_SELF]) { 300757b61080SScott Feldman struct nlattr *port[IFLA_PORT_MAX+1]; 300857b61080SScott Feldman 30098cb08174SJohannes Berg err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX, 30108cb08174SJohannes Berg tb[IFLA_PORT_SELF], 30118cb08174SJohannes Berg ifla_port_policy, NULL); 301257b61080SScott Feldman if (err < 0) 301357b61080SScott Feldman goto errout; 301457b61080SScott Feldman 301557b61080SScott Feldman err = -EOPNOTSUPP; 301657b61080SScott Feldman if (ops->ndo_set_vf_port) 301757b61080SScott Feldman err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port); 301857b61080SScott Feldman if (err < 0) 301957b61080SScott Feldman goto errout; 3020ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 302157b61080SScott Feldman } 3022f8ff182cSThomas Graf 3023f8ff182cSThomas Graf if (tb[IFLA_AF_SPEC]) { 3024f8ff182cSThomas Graf struct nlattr *af; 3025f8ff182cSThomas Graf int rem; 3026f8ff182cSThomas Graf 3027f8ff182cSThomas Graf nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { 3028f8ff182cSThomas Graf const struct rtnl_af_ops *af_ops; 3029f8ff182cSThomas Graf 3030058c8d59SGustavo A. R. Silva BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af)))); 3031f8ff182cSThomas Graf 30323583a4e8SStephen Hemminger err = af_ops->set_link_af(dev, af, extack); 3033a100243dSCong Wang if (err < 0) 3034f8ff182cSThomas Graf goto errout; 3035f8ff182cSThomas Graf 3036ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 3037f8ff182cSThomas Graf } 3038f8ff182cSThomas Graf } 303957b61080SScott Feldman err = 0; 304057b61080SScott Feldman 3041829eb208SRoopa Prabhu if (tb[IFLA_PROTO_DOWN] || tb[IFLA_PROTO_DOWN_REASON]) { 3042829eb208SRoopa Prabhu err = do_set_proto_down(dev, tb[IFLA_PROTO_DOWN], 3043829eb208SRoopa Prabhu tb[IFLA_PROTO_DOWN_REASON], extack); 304488d6378bSAnuradha Karuppiah if (err) 304588d6378bSAnuradha Karuppiah goto errout; 304688d6378bSAnuradha Karuppiah status |= DO_SETLINK_NOTIFY; 304788d6378bSAnuradha Karuppiah } 304888d6378bSAnuradha Karuppiah 3049d1fdd913SBrenden Blanco if (tb[IFLA_XDP]) { 3050d1fdd913SBrenden Blanco struct nlattr *xdp[IFLA_XDP_MAX + 1]; 305185de8576SDaniel Borkmann u32 xdp_flags = 0; 3052d1fdd913SBrenden Blanco 30538cb08174SJohannes Berg err = nla_parse_nested_deprecated(xdp, IFLA_XDP_MAX, 30548cb08174SJohannes Berg tb[IFLA_XDP], 3055fceb6435SJohannes Berg ifla_xdp_policy, NULL); 3056d1fdd913SBrenden Blanco if (err < 0) 3057d1fdd913SBrenden Blanco goto errout; 3058d1fdd913SBrenden Blanco 305958038695SMartin KaFai Lau if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) { 3060262d8625SBrenden Blanco err = -EINVAL; 3061262d8625SBrenden Blanco goto errout; 3062262d8625SBrenden Blanco } 306385de8576SDaniel Borkmann 306485de8576SDaniel Borkmann if (xdp[IFLA_XDP_FLAGS]) { 306585de8576SDaniel Borkmann xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]); 306685de8576SDaniel Borkmann if (xdp_flags & ~XDP_FLAGS_MASK) { 306785de8576SDaniel Borkmann err = -EINVAL; 306885de8576SDaniel Borkmann goto errout; 306985de8576SDaniel Borkmann } 3070ee5d032fSJakub Kicinski if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) { 30710489df9aSDaniel Borkmann err = -EINVAL; 30720489df9aSDaniel Borkmann goto errout; 30730489df9aSDaniel Borkmann } 307485de8576SDaniel Borkmann } 307585de8576SDaniel Borkmann 3076d1fdd913SBrenden Blanco if (xdp[IFLA_XDP_FD]) { 307792234c8fSToke Høiland-Jørgensen int expected_fd = -1; 307892234c8fSToke Høiland-Jørgensen 307992234c8fSToke Høiland-Jørgensen if (xdp_flags & XDP_FLAGS_REPLACE) { 308092234c8fSToke Høiland-Jørgensen if (!xdp[IFLA_XDP_EXPECTED_FD]) { 308192234c8fSToke Høiland-Jørgensen err = -EINVAL; 308292234c8fSToke Høiland-Jørgensen goto errout; 308392234c8fSToke Høiland-Jørgensen } 308492234c8fSToke Høiland-Jørgensen expected_fd = 308592234c8fSToke Høiland-Jørgensen nla_get_s32(xdp[IFLA_XDP_EXPECTED_FD]); 308692234c8fSToke Høiland-Jørgensen } 308792234c8fSToke Høiland-Jørgensen 3088ddf9f970SJakub Kicinski err = dev_change_xdp_fd(dev, extack, 308985de8576SDaniel Borkmann nla_get_s32(xdp[IFLA_XDP_FD]), 309092234c8fSToke Høiland-Jørgensen expected_fd, 309185de8576SDaniel Borkmann xdp_flags); 3092d1fdd913SBrenden Blanco if (err) 3093d1fdd913SBrenden Blanco goto errout; 3094d1fdd913SBrenden Blanco status |= DO_SETLINK_NOTIFY; 3095d1fdd913SBrenden Blanco } 3096d1fdd913SBrenden Blanco } 3097d1fdd913SBrenden Blanco 30980157f60cSPatrick McHardy errout: 3099ba998906SNicolas Dichtel if (status & DO_SETLINK_MODIFIED) { 310064ff90ccSXin Long if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY) 3101ba998906SNicolas Dichtel netdev_state_change(dev); 3102ba998906SNicolas Dichtel 3103ba998906SNicolas Dichtel if (err < 0) 3104e87cc472SJoe 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", 3105e87cc472SJoe Perches dev->name); 3106ba998906SNicolas Dichtel } 31070157f60cSPatrick McHardy 31080157f60cSPatrick McHardy return err; 31090157f60cSPatrick McHardy } 31100157f60cSPatrick McHardy 3111cc6090e9SJiri Pirko static struct net_device *rtnl_dev_get(struct net *net, 31125ea08b52SFlorent Fourcot struct nlattr *tb[]) 3113cc6090e9SJiri Pirko { 31145ea08b52SFlorent Fourcot char ifname[ALTIFNAMSIZ]; 3115cc6090e9SJiri Pirko 31165ea08b52SFlorent Fourcot if (tb[IFLA_IFNAME]) 31175ea08b52SFlorent Fourcot nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 31185ea08b52SFlorent Fourcot else if (tb[IFLA_ALT_IFNAME]) 31195ea08b52SFlorent Fourcot nla_strscpy(ifname, tb[IFLA_ALT_IFNAME], ALTIFNAMSIZ); 3120cc6090e9SJiri Pirko else 3121cc6090e9SJiri Pirko return NULL; 3122cc6090e9SJiri Pirko 3123cc6090e9SJiri Pirko return __dev_get_by_name(net, ifname); 3124cc6090e9SJiri Pirko } 3125cc6090e9SJiri Pirko 3126c21ef3e3SDavid Ahern static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, 3127c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 3128da5e0494SThomas Graf { 31293b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 3130da5e0494SThomas Graf struct ifinfomsg *ifm; 3131da5e0494SThomas Graf struct net_device *dev; 31320157f60cSPatrick McHardy int err; 3133da5e0494SThomas Graf struct nlattr *tb[IFLA_MAX+1]; 31341da177e4SLinus Torvalds 31358cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX, 31368cb08174SJohannes Berg ifla_policy, extack); 3137da5e0494SThomas Graf if (err < 0) 3138da5e0494SThomas Graf goto errout; 31391da177e4SLinus Torvalds 31404ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, false); 31414ff66caeSChristian Brauner if (err < 0) 31424ff66caeSChristian Brauner goto errout; 31434ff66caeSChristian Brauner 31441da177e4SLinus Torvalds err = -EINVAL; 3145da5e0494SThomas Graf ifm = nlmsg_data(nlh); 314651055be8SPatrick McHardy if (ifm->ifi_index > 0) 3147a3d12891SEric Dumazet dev = __dev_get_by_index(net, ifm->ifi_index); 314876c9ac0eSJiri Pirko else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) 31495ea08b52SFlorent Fourcot dev = rtnl_dev_get(net, tb); 3150da5e0494SThomas Graf else 3151da5e0494SThomas Graf goto errout; 31521da177e4SLinus Torvalds 3153da5e0494SThomas Graf if (dev == NULL) { 3154da5e0494SThomas Graf err = -ENODEV; 3155da5e0494SThomas Graf goto errout; 3156da5e0494SThomas Graf } 31571da177e4SLinus Torvalds 315889da780aSXin Long err = validate_linkmsg(dev, tb, extack); 315989da780aSXin Long if (err < 0) 316089da780aSXin Long goto errout; 316189da780aSXin Long 31625ea08b52SFlorent Fourcot err = do_setlink(skb, dev, ifm, extack, tb, 0); 3163da5e0494SThomas Graf errout: 31641da177e4SLinus Torvalds return err; 31651da177e4SLinus Torvalds } 31661da177e4SLinus Torvalds 316766400d54SWANG Cong static int rtnl_group_dellink(const struct net *net, int group) 316866400d54SWANG Cong { 316966400d54SWANG Cong struct net_device *dev, *aux; 317066400d54SWANG Cong LIST_HEAD(list_kill); 317166400d54SWANG Cong bool found = false; 317266400d54SWANG Cong 317366400d54SWANG Cong if (!group) 317466400d54SWANG Cong return -EPERM; 317566400d54SWANG Cong 317666400d54SWANG Cong for_each_netdev(net, dev) { 317766400d54SWANG Cong if (dev->group == group) { 317866400d54SWANG Cong const struct rtnl_link_ops *ops; 317966400d54SWANG Cong 318066400d54SWANG Cong found = true; 318166400d54SWANG Cong ops = dev->rtnl_link_ops; 318266400d54SWANG Cong if (!ops || !ops->dellink) 318366400d54SWANG Cong return -EOPNOTSUPP; 318466400d54SWANG Cong } 318566400d54SWANG Cong } 318666400d54SWANG Cong 318766400d54SWANG Cong if (!found) 318866400d54SWANG Cong return -ENODEV; 318966400d54SWANG Cong 319066400d54SWANG Cong for_each_netdev_safe(net, dev, aux) { 319166400d54SWANG Cong if (dev->group == group) { 319266400d54SWANG Cong const struct rtnl_link_ops *ops; 319366400d54SWANG Cong 319466400d54SWANG Cong ops = dev->rtnl_link_ops; 319566400d54SWANG Cong ops->dellink(dev, &list_kill); 319666400d54SWANG Cong } 319766400d54SWANG Cong } 319866400d54SWANG Cong unregister_netdevice_many(&list_kill); 319966400d54SWANG Cong 320066400d54SWANG Cong return 0; 320166400d54SWANG Cong } 320266400d54SWANG Cong 3203f3a63cceSHangbin Liu int rtnl_delete_link(struct net_device *dev, u32 portid, const struct nlmsghdr *nlh) 3204614732eaSThomas Graf { 3205614732eaSThomas Graf const struct rtnl_link_ops *ops; 3206614732eaSThomas Graf LIST_HEAD(list_kill); 3207614732eaSThomas Graf 3208614732eaSThomas Graf ops = dev->rtnl_link_ops; 3209614732eaSThomas Graf if (!ops || !ops->dellink) 3210614732eaSThomas Graf return -EOPNOTSUPP; 3211614732eaSThomas Graf 3212614732eaSThomas Graf ops->dellink(dev, &list_kill); 3213f3a63cceSHangbin Liu unregister_netdevice_many_notify(&list_kill, portid, nlh); 3214614732eaSThomas Graf 3215614732eaSThomas Graf return 0; 3216614732eaSThomas Graf } 3217614732eaSThomas Graf EXPORT_SYMBOL_GPL(rtnl_delete_link); 3218614732eaSThomas Graf 3219c21ef3e3SDavid Ahern static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, 3220c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 322138f7b870SPatrick McHardy { 32223b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 3223f3a63cceSHangbin Liu u32 portid = NETLINK_CB(skb).portid; 3224b61ad68aSChristian Brauner struct net *tgt_net = net; 3225b61ad68aSChristian Brauner struct net_device *dev = NULL; 322638f7b870SPatrick McHardy struct ifinfomsg *ifm; 322738f7b870SPatrick McHardy struct nlattr *tb[IFLA_MAX+1]; 322838f7b870SPatrick McHardy int err; 3229b61ad68aSChristian Brauner int netnsid = -1; 323038f7b870SPatrick McHardy 32318cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX, 32328cb08174SJohannes Berg ifla_policy, extack); 323338f7b870SPatrick McHardy if (err < 0) 323438f7b870SPatrick McHardy return err; 323538f7b870SPatrick McHardy 32364ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, true); 32374ff66caeSChristian Brauner if (err < 0) 32384ff66caeSChristian Brauner return err; 32394ff66caeSChristian Brauner 32407e4a8d5aSChristian Brauner if (tb[IFLA_TARGET_NETNSID]) { 32417e4a8d5aSChristian Brauner netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]); 3242c383edc4SChristian Brauner tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid); 3243b61ad68aSChristian Brauner if (IS_ERR(tgt_net)) 3244b61ad68aSChristian Brauner return PTR_ERR(tgt_net); 3245b61ad68aSChristian Brauner } 3246b61ad68aSChristian Brauner 3247b61ad68aSChristian Brauner err = -EINVAL; 324838f7b870SPatrick McHardy ifm = nlmsg_data(nlh); 324938f7b870SPatrick McHardy if (ifm->ifi_index > 0) 3250b61ad68aSChristian Brauner dev = __dev_get_by_index(tgt_net, ifm->ifi_index); 325176c9ac0eSJiri Pirko else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) 32525ea08b52SFlorent Fourcot dev = rtnl_dev_get(net, tb); 325366400d54SWANG Cong else if (tb[IFLA_GROUP]) 3254b61ad68aSChristian Brauner err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP])); 325538f7b870SPatrick McHardy else 3256b61ad68aSChristian Brauner goto out; 325738f7b870SPatrick McHardy 3258b61ad68aSChristian Brauner if (!dev) { 3259dee04163SFlorent Fourcot if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME] || ifm->ifi_index > 0) 3260b61ad68aSChristian Brauner err = -ENODEV; 326138f7b870SPatrick McHardy 3262b61ad68aSChristian Brauner goto out; 3263b61ad68aSChristian Brauner } 3264b61ad68aSChristian Brauner 3265f3a63cceSHangbin Liu err = rtnl_delete_link(dev, portid, nlh); 3266b61ad68aSChristian Brauner 3267b61ad68aSChristian Brauner out: 3268b61ad68aSChristian Brauner if (netnsid >= 0) 3269b61ad68aSChristian Brauner put_net(tgt_net); 3270b61ad68aSChristian Brauner 3271b61ad68aSChristian Brauner return err; 327238f7b870SPatrick McHardy } 327338f7b870SPatrick McHardy 32741d997f10SHangbin Liu int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm, 32751d997f10SHangbin Liu u32 portid, const struct nlmsghdr *nlh) 32763729d502SPatrick McHardy { 32773729d502SPatrick McHardy unsigned int old_flags; 32783729d502SPatrick McHardy int err; 32793729d502SPatrick McHardy 32803729d502SPatrick McHardy old_flags = dev->flags; 32813729d502SPatrick McHardy if (ifm && (ifm->ifi_flags || ifm->ifi_change)) { 32826d040321SPetr Machata err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm), 32836d040321SPetr Machata NULL); 32843729d502SPatrick McHardy if (err < 0) 32853729d502SPatrick McHardy return err; 32863729d502SPatrick McHardy } 32873729d502SPatrick McHardy 32888d356b89SRoopa Prabhu if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) { 32891d997f10SHangbin Liu __dev_notify_flags(dev, old_flags, (old_flags ^ dev->flags), portid, nlh); 32908d356b89SRoopa Prabhu } else { 32913729d502SPatrick McHardy dev->rtnl_link_state = RTNL_LINK_INITIALIZED; 32921d997f10SHangbin Liu __dev_notify_flags(dev, old_flags, ~0U, portid, nlh); 32938d356b89SRoopa Prabhu } 32943729d502SPatrick McHardy return 0; 32953729d502SPatrick McHardy } 32963729d502SPatrick McHardy EXPORT_SYMBOL(rtnl_configure_link); 32973729d502SPatrick McHardy 3298d0522f1cSDavid Ahern struct net_device *rtnl_create_link(struct net *net, const char *ifname, 3299d0522f1cSDavid Ahern unsigned char name_assign_type, 3300d0522f1cSDavid Ahern const struct rtnl_link_ops *ops, 3301d0522f1cSDavid Ahern struct nlattr *tb[], 3302d0522f1cSDavid Ahern struct netlink_ext_ack *extack) 3303e7199288SPavel Emelianov { 3304e7199288SPavel Emelianov struct net_device *dev; 3305d40156aaSJiri Pirko unsigned int num_tx_queues = 1; 3306d40156aaSJiri Pirko unsigned int num_rx_queues = 1; 3307b0ad3c17SXin Long int err; 3308e7199288SPavel Emelianov 330976ff5cc9SJiri Pirko if (tb[IFLA_NUM_TX_QUEUES]) 331076ff5cc9SJiri Pirko num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]); 331176ff5cc9SJiri Pirko else if (ops->get_num_tx_queues) 3312d40156aaSJiri Pirko num_tx_queues = ops->get_num_tx_queues(); 331376ff5cc9SJiri Pirko 331476ff5cc9SJiri Pirko if (tb[IFLA_NUM_RX_QUEUES]) 331576ff5cc9SJiri Pirko num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]); 331676ff5cc9SJiri Pirko else if (ops->get_num_rx_queues) 3317d40156aaSJiri Pirko num_rx_queues = ops->get_num_rx_queues(); 3318efacb309Sstephen hemminger 3319d0522f1cSDavid Ahern if (num_tx_queues < 1 || num_tx_queues > 4096) { 3320d0522f1cSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid number of transmit queues"); 33210e1d6ecaSEric Dumazet return ERR_PTR(-EINVAL); 3322d0522f1cSDavid Ahern } 33230e1d6ecaSEric Dumazet 3324d0522f1cSDavid Ahern if (num_rx_queues < 1 || num_rx_queues > 4096) { 3325d0522f1cSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid number of receive queues"); 33260e1d6ecaSEric Dumazet return ERR_PTR(-EINVAL); 3327d0522f1cSDavid Ahern } 33280e1d6ecaSEric Dumazet 33298c713dc9SJohannes Berg if (ops->alloc) { 33308c713dc9SJohannes Berg dev = ops->alloc(tb, ifname, name_assign_type, 33318c713dc9SJohannes Berg num_tx_queues, num_rx_queues); 33328c713dc9SJohannes Berg if (IS_ERR(dev)) 33338c713dc9SJohannes Berg return dev; 33348c713dc9SJohannes Berg } else { 33358c713dc9SJohannes Berg dev = alloc_netdev_mqs(ops->priv_size, ifname, 33368c713dc9SJohannes Berg name_assign_type, ops->setup, 33378c713dc9SJohannes Berg num_tx_queues, num_rx_queues); 33388c713dc9SJohannes Berg } 33398c713dc9SJohannes Berg 3340e7199288SPavel Emelianov if (!dev) 3341d1892e4eSTobias Klauser return ERR_PTR(-ENOMEM); 3342e7199288SPavel Emelianov 3343b0ad3c17SXin Long err = validate_linkmsg(dev, tb, extack); 3344b0ad3c17SXin Long if (err < 0) { 3345b0ad3c17SXin Long free_netdev(dev); 3346b0ad3c17SXin Long return ERR_PTR(err); 3347b0ad3c17SXin Long } 3348b0ad3c17SXin Long 334981adee47SEric W. Biederman dev_net_set(dev, net); 335081adee47SEric W. Biederman dev->rtnl_link_ops = ops; 33513729d502SPatrick McHardy dev->rtnl_link_state = RTNL_LINK_INITIALIZING; 335281adee47SEric W. Biederman 3353d836f5c6SEric Dumazet if (tb[IFLA_MTU]) { 3354d836f5c6SEric Dumazet u32 mtu = nla_get_u32(tb[IFLA_MTU]); 3355d836f5c6SEric Dumazet 3356d836f5c6SEric Dumazet err = dev_validate_mtu(dev, mtu, extack); 3357d836f5c6SEric Dumazet if (err) { 3358d836f5c6SEric Dumazet free_netdev(dev); 3359d836f5c6SEric Dumazet return ERR_PTR(err); 3360d836f5c6SEric Dumazet } 3361d836f5c6SEric Dumazet dev->mtu = mtu; 3362d836f5c6SEric Dumazet } 33632afb9b53SJiri Pirko if (tb[IFLA_ADDRESS]) { 3364efd38f75SJakub Kicinski __dev_addr_set(dev, nla_data(tb[IFLA_ADDRESS]), 3365e7199288SPavel Emelianov nla_len(tb[IFLA_ADDRESS])); 33662afb9b53SJiri Pirko dev->addr_assign_type = NET_ADDR_SET; 33672afb9b53SJiri Pirko } 3368e7199288SPavel Emelianov if (tb[IFLA_BROADCAST]) 3369e7199288SPavel Emelianov memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]), 3370e7199288SPavel Emelianov nla_len(tb[IFLA_BROADCAST])); 3371e7199288SPavel Emelianov if (tb[IFLA_TXQLEN]) 3372e7199288SPavel Emelianov dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); 3373e7199288SPavel Emelianov if (tb[IFLA_OPERSTATE]) 337493b2d4a2SDavid S. Miller set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 3375e7199288SPavel Emelianov if (tb[IFLA_LINKMODE]) 3376e7199288SPavel Emelianov dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); 3377ffa934f1SPatrick McHardy if (tb[IFLA_GROUP]) 3378ffa934f1SPatrick McHardy dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); 337946e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SIZE]) 338046e6b992SStephen Hemminger netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE])); 338146e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SEGS]) 33826d872df3SEric Dumazet netif_set_gso_max_segs(dev, nla_get_u32(tb[IFLA_GSO_MAX_SEGS])); 3383eac1b93cSCoco Li if (tb[IFLA_GRO_MAX_SIZE]) 3384eac1b93cSCoco Li netif_set_gro_max_size(dev, nla_get_u32(tb[IFLA_GRO_MAX_SIZE])); 33859eefedd5SXin Long if (tb[IFLA_GSO_IPV4_MAX_SIZE]) 33869eefedd5SXin Long netif_set_gso_ipv4_max_size(dev, nla_get_u32(tb[IFLA_GSO_IPV4_MAX_SIZE])); 33879eefedd5SXin Long if (tb[IFLA_GRO_IPV4_MAX_SIZE]) 33889eefedd5SXin Long netif_set_gro_ipv4_max_size(dev, nla_get_u32(tb[IFLA_GRO_IPV4_MAX_SIZE])); 3389e7199288SPavel Emelianov 3390e7199288SPavel Emelianov return dev; 3391e7199288SPavel Emelianov } 3392e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_create_link); 3393e7199288SPavel Emelianov 339490f62cf3SEric W. Biederman static int rtnl_group_changelink(const struct sk_buff *skb, 339590f62cf3SEric W. Biederman struct net *net, int group, 3396e7ed828fSVlad Dogaru struct ifinfomsg *ifm, 3397ddf9f970SJakub Kicinski struct netlink_ext_ack *extack, 3398e7ed828fSVlad Dogaru struct nlattr **tb) 3399e7ed828fSVlad Dogaru { 3400d079535dSWANG Cong struct net_device *dev, *aux; 3401e7ed828fSVlad Dogaru int err; 3402e7ed828fSVlad Dogaru 3403d079535dSWANG Cong for_each_netdev_safe(net, dev, aux) { 3404e7ed828fSVlad Dogaru if (dev->group == group) { 340589da780aSXin Long err = validate_linkmsg(dev, tb, extack); 340689da780aSXin Long if (err < 0) 340789da780aSXin Long return err; 34085ea08b52SFlorent Fourcot err = do_setlink(skb, dev, ifm, extack, tb, 0); 3409e7ed828fSVlad Dogaru if (err < 0) 3410e7ed828fSVlad Dogaru return err; 3411e7ed828fSVlad Dogaru } 3412e7ed828fSVlad Dogaru } 3413e7ed828fSVlad Dogaru 3414e7ed828fSVlad Dogaru return 0; 3415e7ed828fSVlad Dogaru } 3416e7ed828fSVlad Dogaru 341763105e83SJakub Kicinski static int rtnl_newlink_create(struct sk_buff *skb, struct ifinfomsg *ifm, 341863105e83SJakub Kicinski const struct rtnl_link_ops *ops, 3419d88e136cSHangbin Liu const struct nlmsghdr *nlh, 342063105e83SJakub Kicinski struct nlattr **tb, struct nlattr **data, 342102839cc8SJakub Kicinski struct netlink_ext_ack *extack) 342202839cc8SJakub Kicinski { 342302839cc8SJakub Kicinski unsigned char name_assign_type = NET_NAME_USER; 342402839cc8SJakub Kicinski struct net *net = sock_net(skb->sk); 3425d88e136cSHangbin Liu u32 portid = NETLINK_CB(skb).portid; 342602839cc8SJakub Kicinski struct net *dest_net, *link_net; 342702839cc8SJakub Kicinski struct net_device *dev; 342802839cc8SJakub Kicinski char ifname[IFNAMSIZ]; 342902839cc8SJakub Kicinski int err; 343002839cc8SJakub Kicinski 343102839cc8SJakub Kicinski if (!ops->alloc && !ops->setup) 343202839cc8SJakub Kicinski return -EOPNOTSUPP; 343302839cc8SJakub Kicinski 343402839cc8SJakub Kicinski if (tb[IFLA_IFNAME]) { 343502839cc8SJakub Kicinski nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 343602839cc8SJakub Kicinski } else { 343702839cc8SJakub Kicinski snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind); 343802839cc8SJakub Kicinski name_assign_type = NET_NAME_ENUM; 343902839cc8SJakub Kicinski } 344002839cc8SJakub Kicinski 344102839cc8SJakub Kicinski dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN); 344202839cc8SJakub Kicinski if (IS_ERR(dest_net)) 344302839cc8SJakub Kicinski return PTR_ERR(dest_net); 344402839cc8SJakub Kicinski 344502839cc8SJakub Kicinski if (tb[IFLA_LINK_NETNSID]) { 344602839cc8SJakub Kicinski int id = nla_get_s32(tb[IFLA_LINK_NETNSID]); 344702839cc8SJakub Kicinski 344802839cc8SJakub Kicinski link_net = get_net_ns_by_id(dest_net, id); 344902839cc8SJakub Kicinski if (!link_net) { 345002839cc8SJakub Kicinski NL_SET_ERR_MSG(extack, "Unknown network namespace id"); 345102839cc8SJakub Kicinski err = -EINVAL; 345202839cc8SJakub Kicinski goto out; 345302839cc8SJakub Kicinski } 345402839cc8SJakub Kicinski err = -EPERM; 345502839cc8SJakub Kicinski if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN)) 345602839cc8SJakub Kicinski goto out; 345702839cc8SJakub Kicinski } else { 345802839cc8SJakub Kicinski link_net = NULL; 345902839cc8SJakub Kicinski } 346002839cc8SJakub Kicinski 346102839cc8SJakub Kicinski dev = rtnl_create_link(link_net ? : dest_net, ifname, 346202839cc8SJakub Kicinski name_assign_type, ops, tb, extack); 346302839cc8SJakub Kicinski if (IS_ERR(dev)) { 346402839cc8SJakub Kicinski err = PTR_ERR(dev); 346502839cc8SJakub Kicinski goto out; 346602839cc8SJakub Kicinski } 346702839cc8SJakub Kicinski 346802839cc8SJakub Kicinski dev->ifindex = ifm->ifi_index; 346902839cc8SJakub Kicinski 347002839cc8SJakub Kicinski if (ops->newlink) 347102839cc8SJakub Kicinski err = ops->newlink(link_net ? : net, dev, tb, data, extack); 347202839cc8SJakub Kicinski else 347302839cc8SJakub Kicinski err = register_netdevice(dev); 347402839cc8SJakub Kicinski if (err < 0) { 347502839cc8SJakub Kicinski free_netdev(dev); 347602839cc8SJakub Kicinski goto out; 347702839cc8SJakub Kicinski } 347802839cc8SJakub Kicinski 3479d88e136cSHangbin Liu err = rtnl_configure_link(dev, ifm, portid, nlh); 348002839cc8SJakub Kicinski if (err < 0) 348102839cc8SJakub Kicinski goto out_unregister; 348202839cc8SJakub Kicinski if (link_net) { 348302839cc8SJakub Kicinski err = dev_change_net_namespace(dev, dest_net, ifname); 348402839cc8SJakub Kicinski if (err < 0) 348502839cc8SJakub Kicinski goto out_unregister; 348602839cc8SJakub Kicinski } 348702839cc8SJakub Kicinski if (tb[IFLA_MASTER]) { 348802839cc8SJakub Kicinski err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack); 348902839cc8SJakub Kicinski if (err) 349002839cc8SJakub Kicinski goto out_unregister; 349102839cc8SJakub Kicinski } 349202839cc8SJakub Kicinski out: 349302839cc8SJakub Kicinski if (link_net) 349402839cc8SJakub Kicinski put_net(link_net); 349502839cc8SJakub Kicinski put_net(dest_net); 349602839cc8SJakub Kicinski return err; 349702839cc8SJakub Kicinski out_unregister: 349802839cc8SJakub Kicinski if (ops->newlink) { 349902839cc8SJakub Kicinski LIST_HEAD(list_kill); 350002839cc8SJakub Kicinski 350102839cc8SJakub Kicinski ops->dellink(dev, &list_kill); 350202839cc8SJakub Kicinski unregister_netdevice_many(&list_kill); 350302839cc8SJakub Kicinski } else { 350402839cc8SJakub Kicinski unregister_netdevice(dev); 350502839cc8SJakub Kicinski } 350602839cc8SJakub Kicinski goto out; 350702839cc8SJakub Kicinski } 350863105e83SJakub Kicinski 3509c92bf26cSJakub Kicinski struct rtnl_newlink_tbs { 3510c92bf26cSJakub Kicinski struct nlattr *tb[IFLA_MAX + 1]; 3511c92bf26cSJakub Kicinski struct nlattr *attr[RTNL_MAX_TYPE + 1]; 3512420d0318SJakub Kicinski struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1]; 3513c92bf26cSJakub Kicinski }; 3514c92bf26cSJakub Kicinski 3515c92bf26cSJakub Kicinski static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, 3516c92bf26cSJakub Kicinski struct rtnl_newlink_tbs *tbs, 3517c92bf26cSJakub Kicinski struct netlink_ext_ack *extack) 3518c92bf26cSJakub Kicinski { 3519420d0318SJakub Kicinski struct nlattr *linkinfo[IFLA_INFO_MAX + 1]; 3520c92bf26cSJakub Kicinski struct nlattr ** const tb = tbs->tb; 3521c6f6f244SEric Dumazet const struct rtnl_link_ops *m_ops; 3522c6f6f244SEric Dumazet struct net_device *master_dev; 35233b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 352438f7b870SPatrick McHardy const struct rtnl_link_ops *ops; 3525420d0318SJakub Kicinski struct nlattr **slave_data; 3526420d0318SJakub Kicinski char kind[MODULE_NAME_LEN]; 3527420d0318SJakub Kicinski struct net_device *dev; 3528420d0318SJakub Kicinski struct ifinfomsg *ifm; 3529420d0318SJakub Kicinski struct nlattr **data; 3530ef2a7c90SFlorent Fourcot bool link_specified; 353138f7b870SPatrick McHardy int err; 353238f7b870SPatrick McHardy 353395a5afcaSJohannes Berg #ifdef CONFIG_MODULES 353438f7b870SPatrick McHardy replay: 35358072f085SThomas Graf #endif 35368cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX, 35378cb08174SJohannes Berg ifla_policy, extack); 353838f7b870SPatrick McHardy if (err < 0) 353938f7b870SPatrick McHardy return err; 354038f7b870SPatrick McHardy 35414ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, false); 35424ff66caeSChristian Brauner if (err < 0) 35434ff66caeSChristian Brauner return err; 35444ff66caeSChristian Brauner 354538f7b870SPatrick McHardy ifm = nlmsg_data(nlh); 3546ef2a7c90SFlorent Fourcot if (ifm->ifi_index > 0) { 3547ef2a7c90SFlorent Fourcot link_specified = true; 3548881d966bSEric W. Biederman dev = __dev_get_by_index(net, ifm->ifi_index); 3549ef2a7c90SFlorent Fourcot } else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) { 3550ef2a7c90SFlorent Fourcot link_specified = true; 35515ea08b52SFlorent Fourcot dev = rtnl_dev_get(net, tb); 3552ef2a7c90SFlorent Fourcot } else { 3553ef2a7c90SFlorent Fourcot link_specified = false; 355438f7b870SPatrick McHardy dev = NULL; 3555ef2a7c90SFlorent Fourcot } 355638f7b870SPatrick McHardy 3557c6f6f244SEric Dumazet master_dev = NULL; 3558c6f6f244SEric Dumazet m_ops = NULL; 3559ba7d49b1SJiri Pirko if (dev) { 3560ba7d49b1SJiri Pirko master_dev = netdev_master_upper_dev_get(dev); 3561ba7d49b1SJiri Pirko if (master_dev) 3562ba7d49b1SJiri Pirko m_ops = master_dev->rtnl_link_ops; 3563ba7d49b1SJiri Pirko } 3564ba7d49b1SJiri Pirko 356538f7b870SPatrick McHardy if (tb[IFLA_LINKINFO]) { 35668cb08174SJohannes Berg err = nla_parse_nested_deprecated(linkinfo, IFLA_INFO_MAX, 35678cb08174SJohannes Berg tb[IFLA_LINKINFO], 35688cb08174SJohannes Berg ifla_info_policy, NULL); 356938f7b870SPatrick McHardy if (err < 0) 357038f7b870SPatrick McHardy return err; 357138f7b870SPatrick McHardy } else 357238f7b870SPatrick McHardy memset(linkinfo, 0, sizeof(linkinfo)); 357338f7b870SPatrick McHardy 357438f7b870SPatrick McHardy if (linkinfo[IFLA_INFO_KIND]) { 3575872f6903SFrancis Laniel nla_strscpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind)); 357638f7b870SPatrick McHardy ops = rtnl_link_ops_get(kind); 357738f7b870SPatrick McHardy } else { 357838f7b870SPatrick McHardy kind[0] = '\0'; 357938f7b870SPatrick McHardy ops = NULL; 358038f7b870SPatrick McHardy } 358138f7b870SPatrick McHardy 3582420d0318SJakub Kicinski data = NULL; 358338f7b870SPatrick McHardy if (ops) { 3584ccf8dbcdSKees Cook if (ops->maxtype > RTNL_MAX_TYPE) 3585ccf8dbcdSKees Cook return -EINVAL; 3586ccf8dbcdSKees Cook 358738f7b870SPatrick McHardy if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) { 3588c92bf26cSJakub Kicinski err = nla_parse_nested_deprecated(tbs->attr, ops->maxtype, 358938f7b870SPatrick McHardy linkinfo[IFLA_INFO_DATA], 359068d57f3bSDavid Ahern ops->policy, extack); 359138f7b870SPatrick McHardy if (err < 0) 359238f7b870SPatrick McHardy return err; 3593c92bf26cSJakub Kicinski data = tbs->attr; 359438f7b870SPatrick McHardy } 359538f7b870SPatrick McHardy if (ops->validate) { 3596a8b8a889SMatthias Schiffer err = ops->validate(tb, data, extack); 359738f7b870SPatrick McHardy if (err < 0) 359838f7b870SPatrick McHardy return err; 359938f7b870SPatrick McHardy } 360038f7b870SPatrick McHardy } 360138f7b870SPatrick McHardy 3602420d0318SJakub Kicinski slave_data = NULL; 3603ba7d49b1SJiri Pirko if (m_ops) { 3604af066ed3SPrashant Bhole if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE) 3605ccf8dbcdSKees Cook return -EINVAL; 3606ccf8dbcdSKees Cook 3607ba7d49b1SJiri Pirko if (m_ops->slave_maxtype && 3608ba7d49b1SJiri Pirko linkinfo[IFLA_INFO_SLAVE_DATA]) { 3609c92bf26cSJakub Kicinski err = nla_parse_nested_deprecated(tbs->slave_attr, 36108cb08174SJohannes Berg m_ops->slave_maxtype, 3611ba7d49b1SJiri Pirko linkinfo[IFLA_INFO_SLAVE_DATA], 36128cb08174SJohannes Berg m_ops->slave_policy, 36138cb08174SJohannes Berg extack); 3614ba7d49b1SJiri Pirko if (err < 0) 3615ba7d49b1SJiri Pirko return err; 3616c92bf26cSJakub Kicinski slave_data = tbs->slave_attr; 3617ba7d49b1SJiri Pirko } 3618ba7d49b1SJiri Pirko } 3619ba7d49b1SJiri Pirko 362038f7b870SPatrick McHardy if (dev) { 362190c325e3SNicolas Dichtel int status = 0; 362238f7b870SPatrick McHardy 362338f7b870SPatrick McHardy if (nlh->nlmsg_flags & NLM_F_EXCL) 362438f7b870SPatrick McHardy return -EEXIST; 362538f7b870SPatrick McHardy if (nlh->nlmsg_flags & NLM_F_REPLACE) 362638f7b870SPatrick McHardy return -EOPNOTSUPP; 362738f7b870SPatrick McHardy 362889da780aSXin Long err = validate_linkmsg(dev, tb, extack); 362989da780aSXin Long if (err < 0) 363089da780aSXin Long return err; 363189da780aSXin Long 363238f7b870SPatrick McHardy if (linkinfo[IFLA_INFO_DATA]) { 363338f7b870SPatrick McHardy if (!ops || ops != dev->rtnl_link_ops || 363438f7b870SPatrick McHardy !ops->changelink) 363538f7b870SPatrick McHardy return -EOPNOTSUPP; 363638f7b870SPatrick McHardy 3637ad744b22SMatthias Schiffer err = ops->changelink(dev, tb, data, extack); 363838f7b870SPatrick McHardy if (err < 0) 363938f7b870SPatrick McHardy return err; 3640ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 364138f7b870SPatrick McHardy } 364238f7b870SPatrick McHardy 3643ba7d49b1SJiri Pirko if (linkinfo[IFLA_INFO_SLAVE_DATA]) { 3644ba7d49b1SJiri Pirko if (!m_ops || !m_ops->slave_changelink) 3645ba7d49b1SJiri Pirko return -EOPNOTSUPP; 3646ba7d49b1SJiri Pirko 3647420d0318SJakub Kicinski err = m_ops->slave_changelink(master_dev, dev, tb, 3648420d0318SJakub Kicinski slave_data, extack); 3649ba7d49b1SJiri Pirko if (err < 0) 3650ba7d49b1SJiri Pirko return err; 3651ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 3652ba7d49b1SJiri Pirko } 3653ba7d49b1SJiri Pirko 36545ea08b52SFlorent Fourcot return do_setlink(skb, dev, ifm, extack, tb, status); 365538f7b870SPatrick McHardy } 365638f7b870SPatrick McHardy 3657ffa934f1SPatrick McHardy if (!(nlh->nlmsg_flags & NLM_F_CREATE)) { 3658ef2a7c90SFlorent Fourcot /* No dev found and NLM_F_CREATE not set. Requested dev does not exist, 3659ef2a7c90SFlorent Fourcot * or it's for a group 3660ef2a7c90SFlorent Fourcot */ 3661ef2a7c90SFlorent Fourcot if (link_specified) 3662ef2a7c90SFlorent Fourcot return -ENODEV; 3663ef2a7c90SFlorent Fourcot if (tb[IFLA_GROUP]) 366490f62cf3SEric W. Biederman return rtnl_group_changelink(skb, net, 3665ffa934f1SPatrick McHardy nla_get_u32(tb[IFLA_GROUP]), 3666ddf9f970SJakub Kicinski ifm, extack, tb); 36676f37c9f9SFlorent Fourcot return -ENODEV; 3668ffa934f1SPatrick McHardy } 366938f7b870SPatrick McHardy 3670160ca014STheuns Verwoerd if (tb[IFLA_MAP] || tb[IFLA_PROTINFO]) 367138f7b870SPatrick McHardy return -EOPNOTSUPP; 367238f7b870SPatrick McHardy 367338f7b870SPatrick McHardy if (!ops) { 367495a5afcaSJohannes Berg #ifdef CONFIG_MODULES 367538f7b870SPatrick McHardy if (kind[0]) { 367638f7b870SPatrick McHardy __rtnl_unlock(); 367738f7b870SPatrick McHardy request_module("rtnl-link-%s", kind); 367838f7b870SPatrick McHardy rtnl_lock(); 367938f7b870SPatrick McHardy ops = rtnl_link_ops_get(kind); 368038f7b870SPatrick McHardy if (ops) 368138f7b870SPatrick McHardy goto replay; 368238f7b870SPatrick McHardy } 368338f7b870SPatrick McHardy #endif 368468d57f3bSDavid Ahern NL_SET_ERR_MSG(extack, "Unknown device type"); 368538f7b870SPatrick McHardy return -EOPNOTSUPP; 368638f7b870SPatrick McHardy } 368738f7b870SPatrick McHardy 3688d88e136cSHangbin Liu return rtnl_newlink_create(skb, ifm, ops, nlh, tb, data, extack); 368963105e83SJakub Kicinski } 369063105e83SJakub Kicinski 3691a2939745SJakub Kicinski static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, 3692a2939745SJakub Kicinski struct netlink_ext_ack *extack) 3693a2939745SJakub Kicinski { 3694c92bf26cSJakub Kicinski struct rtnl_newlink_tbs *tbs; 3695a2939745SJakub Kicinski int ret; 3696a2939745SJakub Kicinski 3697c92bf26cSJakub Kicinski tbs = kmalloc(sizeof(*tbs), GFP_KERNEL); 3698c92bf26cSJakub Kicinski if (!tbs) 3699a2939745SJakub Kicinski return -ENOMEM; 3700a2939745SJakub Kicinski 3701c92bf26cSJakub Kicinski ret = __rtnl_newlink(skb, nlh, tbs, extack); 3702c92bf26cSJakub Kicinski kfree(tbs); 3703a2939745SJakub Kicinski return ret; 3704a2939745SJakub Kicinski } 3705a2939745SJakub Kicinski 37069b3757b0SJakub Kicinski static int rtnl_valid_getlink_req(struct sk_buff *skb, 37079b3757b0SJakub Kicinski const struct nlmsghdr *nlh, 37089b3757b0SJakub Kicinski struct nlattr **tb, 37099b3757b0SJakub Kicinski struct netlink_ext_ack *extack) 37109b3757b0SJakub Kicinski { 37119b3757b0SJakub Kicinski struct ifinfomsg *ifm; 37129b3757b0SJakub Kicinski int i, err; 37139b3757b0SJakub Kicinski 37149b3757b0SJakub Kicinski if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { 37159b3757b0SJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid header for get link"); 37169b3757b0SJakub Kicinski return -EINVAL; 37179b3757b0SJakub Kicinski } 37189b3757b0SJakub Kicinski 37199b3757b0SJakub Kicinski if (!netlink_strict_get_check(skb)) 37208cb08174SJohannes Berg return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX, 37218cb08174SJohannes Berg ifla_policy, extack); 37229b3757b0SJakub Kicinski 37239b3757b0SJakub Kicinski ifm = nlmsg_data(nlh); 37249b3757b0SJakub Kicinski if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags || 37259b3757b0SJakub Kicinski ifm->ifi_change) { 37269b3757b0SJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid values in header for get link request"); 37279b3757b0SJakub Kicinski return -EINVAL; 37289b3757b0SJakub Kicinski } 37299b3757b0SJakub Kicinski 37308cb08174SJohannes Berg err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFLA_MAX, 37318cb08174SJohannes Berg ifla_policy, extack); 37329b3757b0SJakub Kicinski if (err) 37339b3757b0SJakub Kicinski return err; 37349b3757b0SJakub Kicinski 37359b3757b0SJakub Kicinski for (i = 0; i <= IFLA_MAX; i++) { 37369b3757b0SJakub Kicinski if (!tb[i]) 37379b3757b0SJakub Kicinski continue; 37389b3757b0SJakub Kicinski 37399b3757b0SJakub Kicinski switch (i) { 37409b3757b0SJakub Kicinski case IFLA_IFNAME: 374176c9ac0eSJiri Pirko case IFLA_ALT_IFNAME: 37429b3757b0SJakub Kicinski case IFLA_EXT_MASK: 37439b3757b0SJakub Kicinski case IFLA_TARGET_NETNSID: 37449b3757b0SJakub Kicinski break; 37459b3757b0SJakub Kicinski default: 37469b3757b0SJakub Kicinski NL_SET_ERR_MSG(extack, "Unsupported attribute in get link request"); 37479b3757b0SJakub Kicinski return -EINVAL; 37489b3757b0SJakub Kicinski } 37499b3757b0SJakub Kicinski } 37509b3757b0SJakub Kicinski 37519b3757b0SJakub Kicinski return 0; 37529b3757b0SJakub Kicinski } 37539b3757b0SJakub Kicinski 3754c21ef3e3SDavid Ahern static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh, 3755c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 3756711e2c33SJean Tourrilhes { 37573b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 375879e1ad14SJiri Benc struct net *tgt_net = net; 3759b60c5115SThomas Graf struct ifinfomsg *ifm; 3760b60c5115SThomas Graf struct nlattr *tb[IFLA_MAX+1]; 3761b60c5115SThomas Graf struct net_device *dev = NULL; 3762b60c5115SThomas Graf struct sk_buff *nskb; 376379e1ad14SJiri Benc int netnsid = -1; 3764339bf98fSThomas Graf int err; 3765115c9b81SGreg Rose u32 ext_filter_mask = 0; 3766711e2c33SJean Tourrilhes 37679b3757b0SJakub Kicinski err = rtnl_valid_getlink_req(skb, nlh, tb, extack); 3768b60c5115SThomas Graf if (err < 0) 37699918f230SEric Sesterhenn return err; 3770b60c5115SThomas Graf 37714ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, true); 37724ff66caeSChristian Brauner if (err < 0) 37734ff66caeSChristian Brauner return err; 37744ff66caeSChristian Brauner 37757e4a8d5aSChristian Brauner if (tb[IFLA_TARGET_NETNSID]) { 37767e4a8d5aSChristian Brauner netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]); 3777c383edc4SChristian Brauner tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid); 377879e1ad14SJiri Benc if (IS_ERR(tgt_net)) 377979e1ad14SJiri Benc return PTR_ERR(tgt_net); 378079e1ad14SJiri Benc } 378179e1ad14SJiri Benc 3782115c9b81SGreg Rose if (tb[IFLA_EXT_MASK]) 3783115c9b81SGreg Rose ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 3784115c9b81SGreg Rose 378579e1ad14SJiri Benc err = -EINVAL; 3786b60c5115SThomas Graf ifm = nlmsg_data(nlh); 3787a3d12891SEric Dumazet if (ifm->ifi_index > 0) 378879e1ad14SJiri Benc dev = __dev_get_by_index(tgt_net, ifm->ifi_index); 378976c9ac0eSJiri Pirko else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) 37905ea08b52SFlorent Fourcot dev = rtnl_dev_get(tgt_net, tb); 3791a3d12891SEric Dumazet else 379279e1ad14SJiri Benc goto out; 3793b60c5115SThomas Graf 379479e1ad14SJiri Benc err = -ENODEV; 3795a3d12891SEric Dumazet if (dev == NULL) 379679e1ad14SJiri Benc goto out; 3797a3d12891SEric Dumazet 379879e1ad14SJiri Benc err = -ENOBUFS; 3799115c9b81SGreg Rose nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL); 3800a3d12891SEric Dumazet if (nskb == NULL) 380179e1ad14SJiri Benc goto out; 3802711e2c33SJean Tourrilhes 380379e1ad14SJiri Benc err = rtnl_fill_ifinfo(nskb, dev, net, 380479e1ad14SJiri Benc RTM_NEWLINK, NETLINK_CB(skb).portid, 380579e1ad14SJiri Benc nlh->nlmsg_seq, 0, 0, ext_filter_mask, 3806d4e4fdf9SGuillaume Nault 0, NULL, 0, netnsid, GFP_KERNEL); 380726932566SPatrick McHardy if (err < 0) { 380826932566SPatrick McHardy /* -EMSGSIZE implies BUG in if_nlmsg_size */ 380926932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 381026932566SPatrick McHardy kfree_skb(nskb); 3811a3d12891SEric Dumazet } else 381215e47304SEric W. Biederman err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); 381379e1ad14SJiri Benc out: 381479e1ad14SJiri Benc if (netnsid >= 0) 381579e1ad14SJiri Benc put_net(tgt_net); 3816b60c5115SThomas Graf 3817711e2c33SJean Tourrilhes return err; 3818711e2c33SJean Tourrilhes } 3819711e2c33SJean Tourrilhes 382036fbf1e5SJiri Pirko static int rtnl_alt_ifname(int cmd, struct net_device *dev, struct nlattr *attr, 382136fbf1e5SJiri Pirko bool *changed, struct netlink_ext_ack *extack) 382236fbf1e5SJiri Pirko { 382336fbf1e5SJiri Pirko char *alt_ifname; 3824155fb43bSJakub Kicinski size_t size; 382536fbf1e5SJiri Pirko int err; 382636fbf1e5SJiri Pirko 382736fbf1e5SJiri Pirko err = nla_validate(attr, attr->nla_len, IFLA_MAX, ifla_policy, extack); 382836fbf1e5SJiri Pirko if (err) 382936fbf1e5SJiri Pirko return err; 383036fbf1e5SJiri Pirko 3831155fb43bSJakub Kicinski if (cmd == RTM_NEWLINKPROP) { 3832155fb43bSJakub Kicinski size = rtnl_prop_list_size(dev); 3833155fb43bSJakub Kicinski size += nla_total_size(ALTIFNAMSIZ); 3834155fb43bSJakub Kicinski if (size >= U16_MAX) { 3835155fb43bSJakub Kicinski NL_SET_ERR_MSG(extack, 3836155fb43bSJakub Kicinski "effective property list too long"); 3837155fb43bSJakub Kicinski return -EINVAL; 3838155fb43bSJakub Kicinski } 3839155fb43bSJakub Kicinski } 3840155fb43bSJakub Kicinski 38415d26cff5SJakub Kicinski alt_ifname = nla_strdup(attr, GFP_KERNEL_ACCOUNT); 384236fbf1e5SJiri Pirko if (!alt_ifname) 384336fbf1e5SJiri Pirko return -ENOMEM; 384444bfa9c5SEric Dumazet 384544bfa9c5SEric Dumazet if (cmd == RTM_NEWLINKPROP) { 384636fbf1e5SJiri Pirko err = netdev_name_node_alt_create(dev, alt_ifname); 384744bfa9c5SEric Dumazet if (!err) 384844bfa9c5SEric Dumazet alt_ifname = NULL; 384936fbf1e5SJiri Pirko } else if (cmd == RTM_DELLINKPROP) { 385036fbf1e5SJiri Pirko err = netdev_name_node_alt_destroy(dev, alt_ifname); 385136fbf1e5SJiri Pirko } else { 385244bfa9c5SEric Dumazet WARN_ON_ONCE(1); 385344bfa9c5SEric Dumazet err = -EINVAL; 385436fbf1e5SJiri Pirko } 385536fbf1e5SJiri Pirko 385644bfa9c5SEric Dumazet kfree(alt_ifname); 385744bfa9c5SEric Dumazet if (!err) 385836fbf1e5SJiri Pirko *changed = true; 385944bfa9c5SEric Dumazet return err; 386036fbf1e5SJiri Pirko } 386136fbf1e5SJiri Pirko 386236fbf1e5SJiri Pirko static int rtnl_linkprop(int cmd, struct sk_buff *skb, struct nlmsghdr *nlh, 386336fbf1e5SJiri Pirko struct netlink_ext_ack *extack) 386436fbf1e5SJiri Pirko { 386536fbf1e5SJiri Pirko struct net *net = sock_net(skb->sk); 386636fbf1e5SJiri Pirko struct nlattr *tb[IFLA_MAX + 1]; 386736fbf1e5SJiri Pirko struct net_device *dev; 386836fbf1e5SJiri Pirko struct ifinfomsg *ifm; 386936fbf1e5SJiri Pirko bool changed = false; 387036fbf1e5SJiri Pirko struct nlattr *attr; 387136fbf1e5SJiri Pirko int err, rem; 387236fbf1e5SJiri Pirko 387336fbf1e5SJiri Pirko err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack); 387436fbf1e5SJiri Pirko if (err) 387536fbf1e5SJiri Pirko return err; 387636fbf1e5SJiri Pirko 387736fbf1e5SJiri Pirko err = rtnl_ensure_unique_netns(tb, extack, true); 387836fbf1e5SJiri Pirko if (err) 387936fbf1e5SJiri Pirko return err; 388036fbf1e5SJiri Pirko 388136fbf1e5SJiri Pirko ifm = nlmsg_data(nlh); 3882cc6090e9SJiri Pirko if (ifm->ifi_index > 0) 388336fbf1e5SJiri Pirko dev = __dev_get_by_index(net, ifm->ifi_index); 388476c9ac0eSJiri Pirko else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) 38855ea08b52SFlorent Fourcot dev = rtnl_dev_get(net, tb); 3886cc6090e9SJiri Pirko else 388736fbf1e5SJiri Pirko return -EINVAL; 388836fbf1e5SJiri Pirko 388936fbf1e5SJiri Pirko if (!dev) 389036fbf1e5SJiri Pirko return -ENODEV; 389136fbf1e5SJiri Pirko 389236fbf1e5SJiri Pirko if (!tb[IFLA_PROP_LIST]) 389336fbf1e5SJiri Pirko return 0; 389436fbf1e5SJiri Pirko 389536fbf1e5SJiri Pirko nla_for_each_nested(attr, tb[IFLA_PROP_LIST], rem) { 389636fbf1e5SJiri Pirko switch (nla_type(attr)) { 389736fbf1e5SJiri Pirko case IFLA_ALT_IFNAME: 389836fbf1e5SJiri Pirko err = rtnl_alt_ifname(cmd, dev, attr, &changed, extack); 389936fbf1e5SJiri Pirko if (err) 390036fbf1e5SJiri Pirko return err; 390136fbf1e5SJiri Pirko break; 390236fbf1e5SJiri Pirko } 390336fbf1e5SJiri Pirko } 390436fbf1e5SJiri Pirko 390536fbf1e5SJiri Pirko if (changed) 390636fbf1e5SJiri Pirko netdev_state_change(dev); 390736fbf1e5SJiri Pirko return 0; 390836fbf1e5SJiri Pirko } 390936fbf1e5SJiri Pirko 391036fbf1e5SJiri Pirko static int rtnl_newlinkprop(struct sk_buff *skb, struct nlmsghdr *nlh, 391136fbf1e5SJiri Pirko struct netlink_ext_ack *extack) 391236fbf1e5SJiri Pirko { 391336fbf1e5SJiri Pirko return rtnl_linkprop(RTM_NEWLINKPROP, skb, nlh, extack); 391436fbf1e5SJiri Pirko } 391536fbf1e5SJiri Pirko 391636fbf1e5SJiri Pirko static int rtnl_dellinkprop(struct sk_buff *skb, struct nlmsghdr *nlh, 391736fbf1e5SJiri Pirko struct netlink_ext_ack *extack) 391836fbf1e5SJiri Pirko { 391936fbf1e5SJiri Pirko return rtnl_linkprop(RTM_DELLINKPROP, skb, nlh, extack); 392036fbf1e5SJiri Pirko } 392136fbf1e5SJiri Pirko 3922ebfe3c51SDi Zhu static u32 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh) 3923c7ac8679SGreg Rose { 3924115c9b81SGreg Rose struct net *net = sock_net(skb->sk); 3925ebfe3c51SDi Zhu size_t min_ifinfo_dump_size = 0; 3926115c9b81SGreg Rose struct nlattr *tb[IFLA_MAX+1]; 3927115c9b81SGreg Rose u32 ext_filter_mask = 0; 3928ebfe3c51SDi Zhu struct net_device *dev; 3929e5eca6d4SMichal Schmidt int hdrlen; 3930115c9b81SGreg Rose 3931e5eca6d4SMichal Schmidt /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */ 3932e5eca6d4SMichal Schmidt hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ? 3933e5eca6d4SMichal Schmidt sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); 3934e5eca6d4SMichal Schmidt 39358cb08174SJohannes Berg if (nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) { 3936115c9b81SGreg Rose if (tb[IFLA_EXT_MASK]) 3937115c9b81SGreg Rose ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 3938a4b64fbeSEric Dumazet } 3939115c9b81SGreg Rose 3940115c9b81SGreg Rose if (!ext_filter_mask) 3941115c9b81SGreg Rose return NLMSG_GOODSIZE; 3942115c9b81SGreg Rose /* 3943115c9b81SGreg Rose * traverse the list of net devices and compute the minimum 3944115c9b81SGreg Rose * buffer size based upon the filter mask. 3945115c9b81SGreg Rose */ 39466853dd48SFlorian Westphal rcu_read_lock(); 39476853dd48SFlorian Westphal for_each_netdev_rcu(net, dev) { 3948ebfe3c51SDi Zhu min_ifinfo_dump_size = max(min_ifinfo_dump_size, 3949ebfe3c51SDi Zhu if_nlmsg_size(dev, ext_filter_mask)); 3950115c9b81SGreg Rose } 39516853dd48SFlorian Westphal rcu_read_unlock(); 3952115c9b81SGreg Rose 395393af2056SZhang Shengju return nlmsg_total_size(min_ifinfo_dump_size); 3954c7ac8679SGreg Rose } 3955c7ac8679SGreg Rose 395642bad1daSAdrian Bunk static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb) 39571da177e4SLinus Torvalds { 39581da177e4SLinus Torvalds int idx; 39591da177e4SLinus Torvalds int s_idx = cb->family; 396087ccbb1fSChristian Brauner int type = cb->nlh->nlmsg_type - RTM_BASE; 3961c63586dcSDavid Ahern int ret = 0; 39621da177e4SLinus Torvalds 39631da177e4SLinus Torvalds if (s_idx == 0) 39641da177e4SLinus Torvalds s_idx = 1; 39656853dd48SFlorian Westphal 396625239ceeSPatrick McHardy for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) { 396751e13685SJakub Kicinski struct rtnl_link __rcu **tab; 3968addf9b90SFlorian Westphal struct rtnl_link *link; 39696853dd48SFlorian Westphal rtnl_dumpit_func dumpit; 39706853dd48SFlorian Westphal 39711da177e4SLinus Torvalds if (idx < s_idx || idx == PF_PACKET) 39721da177e4SLinus Torvalds continue; 39736853dd48SFlorian Westphal 3974addf9b90SFlorian Westphal if (type < 0 || type >= RTM_NR_MSGTYPES) 39751da177e4SLinus Torvalds continue; 39766853dd48SFlorian Westphal 3977addf9b90SFlorian Westphal tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]); 3978addf9b90SFlorian Westphal if (!tab) 3979addf9b90SFlorian Westphal continue; 3980addf9b90SFlorian Westphal 398151e13685SJakub Kicinski link = rcu_dereference_rtnl(tab[type]); 3982addf9b90SFlorian Westphal if (!link) 3983addf9b90SFlorian Westphal continue; 3984addf9b90SFlorian Westphal 3985addf9b90SFlorian Westphal dumpit = link->dumpit; 39866853dd48SFlorian Westphal if (!dumpit) 39876853dd48SFlorian Westphal continue; 39886853dd48SFlorian Westphal 39890465277fSNicolas Dichtel if (idx > s_idx) { 39901da177e4SLinus Torvalds memset(&cb->args[0], 0, sizeof(cb->args)); 39910465277fSNicolas Dichtel cb->prev_seq = 0; 39920465277fSNicolas Dichtel cb->seq = 0; 39930465277fSNicolas Dichtel } 3994c63586dcSDavid Ahern ret = dumpit(skb, cb); 39955e1acb4aSAlexey Kodanev if (ret) 39961da177e4SLinus Torvalds break; 39971da177e4SLinus Torvalds } 39981da177e4SLinus Torvalds cb->family = idx; 39991da177e4SLinus Torvalds 4000c63586dcSDavid Ahern return skb->len ? : ret; 40011da177e4SLinus Torvalds } 40021da177e4SLinus Torvalds 4003395eea6cSMahesh Bandewar struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev, 40043d3ea5afSVlad Yasevich unsigned int change, 400538e01b30SNicolas Dichtel u32 event, gfp_t flags, int *new_nsid, 400659d3efd2SMartin Willi int new_ifindex, u32 portid, 400759d3efd2SMartin Willi const struct nlmsghdr *nlh) 40081da177e4SLinus Torvalds { 4009c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 40101da177e4SLinus Torvalds struct sk_buff *skb; 40110ec6d3f4SThomas Graf int err = -ENOBUFS; 401259d3efd2SMartin Willi u32 seq = 0; 40131da177e4SLinus Torvalds 401450af5969Sluo penghao skb = nlmsg_new(if_nlmsg_size(dev, 0), flags); 40150ec6d3f4SThomas Graf if (skb == NULL) 40160ec6d3f4SThomas Graf goto errout; 40171da177e4SLinus Torvalds 401859d3efd2SMartin Willi if (nlmsg_report(nlh)) 401959d3efd2SMartin Willi seq = nlmsg_seq(nlh); 402059d3efd2SMartin Willi else 402159d3efd2SMartin Willi portid = 0; 402259d3efd2SMartin Willi 402379e1ad14SJiri Benc err = rtnl_fill_ifinfo(skb, dev, dev_net(dev), 40241d997f10SHangbin Liu type, portid, seq, change, 0, 0, event, 4025d4e4fdf9SGuillaume Nault new_nsid, new_ifindex, -1, flags); 402626932566SPatrick McHardy if (err < 0) { 402726932566SPatrick McHardy /* -EMSGSIZE implies BUG in if_nlmsg_size() */ 402826932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 402926932566SPatrick McHardy kfree_skb(skb); 403026932566SPatrick McHardy goto errout; 403126932566SPatrick McHardy } 4032395eea6cSMahesh Bandewar return skb; 40330ec6d3f4SThomas Graf errout: 40340ec6d3f4SThomas Graf if (err < 0) 40354b3da706SEric W. Biederman rtnl_set_sk_err(net, RTNLGRP_LINK, err); 4036395eea6cSMahesh Bandewar return NULL; 4037395eea6cSMahesh Bandewar } 4038395eea6cSMahesh Bandewar 40391d997f10SHangbin Liu void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags, 40401d997f10SHangbin Liu u32 portid, const struct nlmsghdr *nlh) 4041395eea6cSMahesh Bandewar { 4042395eea6cSMahesh Bandewar struct net *net = dev_net(dev); 4043395eea6cSMahesh Bandewar 40441d997f10SHangbin Liu rtnl_notify(skb, net, portid, RTNLGRP_LINK, nlh, flags); 4045395eea6cSMahesh Bandewar } 4046395eea6cSMahesh Bandewar 40473d3ea5afSVlad Yasevich static void rtmsg_ifinfo_event(int type, struct net_device *dev, 40483d3ea5afSVlad Yasevich unsigned int change, u32 event, 40491d997f10SHangbin Liu gfp_t flags, int *new_nsid, int new_ifindex, 40501d997f10SHangbin Liu u32 portid, const struct nlmsghdr *nlh) 4051395eea6cSMahesh Bandewar { 4052395eea6cSMahesh Bandewar struct sk_buff *skb; 4053395eea6cSMahesh Bandewar 4054ed2a80abSNicolas Dichtel if (dev->reg_state != NETREG_REGISTERED) 4055ed2a80abSNicolas Dichtel return; 4056ed2a80abSNicolas Dichtel 405738e01b30SNicolas Dichtel skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid, 405859d3efd2SMartin Willi new_ifindex, portid, nlh); 4059395eea6cSMahesh Bandewar if (skb) 40601d997f10SHangbin Liu rtmsg_ifinfo_send(skb, dev, flags, portid, nlh); 40611da177e4SLinus Torvalds } 40623d3ea5afSVlad Yasevich 40633d3ea5afSVlad Yasevich void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change, 40641d997f10SHangbin Liu gfp_t flags, u32 portid, const struct nlmsghdr *nlh) 40653d3ea5afSVlad Yasevich { 406638e01b30SNicolas Dichtel rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags, 40671d997f10SHangbin Liu NULL, 0, portid, nlh); 40683d3ea5afSVlad Yasevich } 40691da177e4SLinus Torvalds 40706621dd29SNicolas Dichtel void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change, 407138e01b30SNicolas Dichtel gfp_t flags, int *new_nsid, int new_ifindex) 40726621dd29SNicolas Dichtel { 40736621dd29SNicolas Dichtel rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags, 40741d997f10SHangbin Liu new_nsid, new_ifindex, 0, NULL); 40756621dd29SNicolas Dichtel } 40766621dd29SNicolas Dichtel 4077d83b0603SJohn Fastabend static int nlmsg_populate_fdb_fill(struct sk_buff *skb, 4078d83b0603SJohn Fastabend struct net_device *dev, 40791e53d5bbSHubert Sokolowski u8 *addr, u16 vid, u32 pid, u32 seq, 40801c104a6bSNicolas Dichtel int type, unsigned int flags, 4081b3379041SHubert Sokolowski int nlflags, u16 ndm_state) 4082d83b0603SJohn Fastabend { 4083d83b0603SJohn Fastabend struct nlmsghdr *nlh; 4084d83b0603SJohn Fastabend struct ndmsg *ndm; 4085d83b0603SJohn Fastabend 40861c104a6bSNicolas Dichtel nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags); 4087d83b0603SJohn Fastabend if (!nlh) 4088d83b0603SJohn Fastabend return -EMSGSIZE; 4089d83b0603SJohn Fastabend 4090d83b0603SJohn Fastabend ndm = nlmsg_data(nlh); 4091d83b0603SJohn Fastabend ndm->ndm_family = AF_BRIDGE; 4092d83b0603SJohn Fastabend ndm->ndm_pad1 = 0; 4093d83b0603SJohn Fastabend ndm->ndm_pad2 = 0; 4094d83b0603SJohn Fastabend ndm->ndm_flags = flags; 4095d83b0603SJohn Fastabend ndm->ndm_type = 0; 4096d83b0603SJohn Fastabend ndm->ndm_ifindex = dev->ifindex; 4097b3379041SHubert Sokolowski ndm->ndm_state = ndm_state; 4098d83b0603SJohn Fastabend 4099aa540695SEric Dumazet if (nla_put(skb, NDA_LLADDR, dev->addr_len, addr)) 4100d83b0603SJohn Fastabend goto nla_put_failure; 41011e53d5bbSHubert Sokolowski if (vid) 41021e53d5bbSHubert Sokolowski if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid)) 41031e53d5bbSHubert Sokolowski goto nla_put_failure; 4104d83b0603SJohn Fastabend 4105053c095aSJohannes Berg nlmsg_end(skb, nlh); 4106053c095aSJohannes Berg return 0; 4107d83b0603SJohn Fastabend 4108d83b0603SJohn Fastabend nla_put_failure: 4109d83b0603SJohn Fastabend nlmsg_cancel(skb, nlh); 4110d83b0603SJohn Fastabend return -EMSGSIZE; 4111d83b0603SJohn Fastabend } 4112d83b0603SJohn Fastabend 4113aa540695SEric Dumazet static inline size_t rtnl_fdb_nlmsg_size(const struct net_device *dev) 41143ff661c3SJohn Fastabend { 4115f82ef3e1SSabrina Dubroca return NLMSG_ALIGN(sizeof(struct ndmsg)) + 4116aa540695SEric Dumazet nla_total_size(dev->addr_len) + /* NDA_LLADDR */ 4117f82ef3e1SSabrina Dubroca nla_total_size(sizeof(u16)) + /* NDA_VLAN */ 4118f82ef3e1SSabrina Dubroca 0; 41193ff661c3SJohn Fastabend } 41203ff661c3SJohn Fastabend 4121b3379041SHubert Sokolowski static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type, 4122b3379041SHubert Sokolowski u16 ndm_state) 41233ff661c3SJohn Fastabend { 41243ff661c3SJohn Fastabend struct net *net = dev_net(dev); 41253ff661c3SJohn Fastabend struct sk_buff *skb; 41263ff661c3SJohn Fastabend int err = -ENOBUFS; 41273ff661c3SJohn Fastabend 4128aa540695SEric Dumazet skb = nlmsg_new(rtnl_fdb_nlmsg_size(dev), GFP_ATOMIC); 41293ff661c3SJohn Fastabend if (!skb) 41303ff661c3SJohn Fastabend goto errout; 41313ff661c3SJohn Fastabend 41321e53d5bbSHubert Sokolowski err = nlmsg_populate_fdb_fill(skb, dev, addr, vid, 4133b3379041SHubert Sokolowski 0, 0, type, NTF_SELF, 0, ndm_state); 41343ff661c3SJohn Fastabend if (err < 0) { 41353ff661c3SJohn Fastabend kfree_skb(skb); 41363ff661c3SJohn Fastabend goto errout; 41373ff661c3SJohn Fastabend } 41383ff661c3SJohn Fastabend 41393ff661c3SJohn Fastabend rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); 41403ff661c3SJohn Fastabend return; 41413ff661c3SJohn Fastabend errout: 41423ff661c3SJohn Fastabend rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); 41433ff661c3SJohn Fastabend } 41443ff661c3SJohn Fastabend 4145a986967eSBart Van Assche /* 4146090096bfSVlad Yasevich * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry 4147090096bfSVlad Yasevich */ 4148090096bfSVlad Yasevich int ndo_dflt_fdb_add(struct ndmsg *ndm, 4149090096bfSVlad Yasevich struct nlattr *tb[], 4150090096bfSVlad Yasevich struct net_device *dev, 4151f6f6424bSJiri Pirko const unsigned char *addr, u16 vid, 4152090096bfSVlad Yasevich u16 flags) 4153090096bfSVlad Yasevich { 4154090096bfSVlad Yasevich int err = -EINVAL; 4155090096bfSVlad Yasevich 4156090096bfSVlad Yasevich /* If aging addresses are supported device will need to 4157090096bfSVlad Yasevich * implement its own handler for this. 4158090096bfSVlad Yasevich */ 4159090096bfSVlad Yasevich if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) { 416078ecc890SVladimir Oltean netdev_info(dev, "default FDB implementation only supports local addresses\n"); 4161090096bfSVlad Yasevich return err; 4162090096bfSVlad Yasevich } 4163090096bfSVlad Yasevich 4164a35ec8e3SHans J. Schultz if (tb[NDA_FLAGS_EXT]) { 4165a35ec8e3SHans J. Schultz netdev_info(dev, "invalid flags given to default FDB implementation\n"); 4166a35ec8e3SHans J. Schultz return err; 4167a35ec8e3SHans J. Schultz } 4168a35ec8e3SHans J. Schultz 416965891feaSOr Gerlitz if (vid) { 417023ac0b42SVladimir Oltean netdev_info(dev, "vlans aren't supported yet for dev_uc|mc_add()\n"); 417165891feaSOr Gerlitz return err; 417265891feaSOr Gerlitz } 417365891feaSOr Gerlitz 4174090096bfSVlad Yasevich if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) 4175090096bfSVlad Yasevich err = dev_uc_add_excl(dev, addr); 4176090096bfSVlad Yasevich else if (is_multicast_ether_addr(addr)) 4177090096bfSVlad Yasevich err = dev_mc_add_excl(dev, addr); 4178090096bfSVlad Yasevich 4179090096bfSVlad Yasevich /* Only return duplicate errors if NLM_F_EXCL is set */ 4180090096bfSVlad Yasevich if (err == -EEXIST && !(flags & NLM_F_EXCL)) 4181090096bfSVlad Yasevich err = 0; 4182090096bfSVlad Yasevich 4183090096bfSVlad Yasevich return err; 4184090096bfSVlad Yasevich } 4185090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_add); 4186090096bfSVlad Yasevich 4187b88d12e4SFlorian Westphal static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid, 4188b88d12e4SFlorian Westphal struct netlink_ext_ack *extack) 4189f6f6424bSJiri Pirko { 4190f6f6424bSJiri Pirko u16 vid = 0; 4191f6f6424bSJiri Pirko 4192f6f6424bSJiri Pirko if (vlan_attr) { 4193f6f6424bSJiri Pirko if (nla_len(vlan_attr) != sizeof(u16)) { 4194b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid vlan attribute size"); 4195f6f6424bSJiri Pirko return -EINVAL; 4196f6f6424bSJiri Pirko } 4197f6f6424bSJiri Pirko 4198f6f6424bSJiri Pirko vid = nla_get_u16(vlan_attr); 4199f6f6424bSJiri Pirko 4200f6f6424bSJiri Pirko if (!vid || vid >= VLAN_VID_MASK) { 4201b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid vlan id"); 4202f6f6424bSJiri Pirko return -EINVAL; 4203f6f6424bSJiri Pirko } 4204f6f6424bSJiri Pirko } 4205f6f6424bSJiri Pirko *p_vid = vid; 4206f6f6424bSJiri Pirko return 0; 4207f6f6424bSJiri Pirko } 4208f6f6424bSJiri Pirko 4209c21ef3e3SDavid Ahern static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh, 4210c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 421177162022SJohn Fastabend { 421277162022SJohn Fastabend struct net *net = sock_net(skb->sk); 421377162022SJohn Fastabend struct ndmsg *ndm; 421477162022SJohn Fastabend struct nlattr *tb[NDA_MAX+1]; 421577162022SJohn Fastabend struct net_device *dev; 421677162022SJohn Fastabend u8 *addr; 4217f6f6424bSJiri Pirko u16 vid; 421877162022SJohn Fastabend int err; 421977162022SJohn Fastabend 42208cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, 42218cb08174SJohannes Berg extack); 422277162022SJohn Fastabend if (err < 0) 422377162022SJohn Fastabend return err; 422477162022SJohn Fastabend 422577162022SJohn Fastabend ndm = nlmsg_data(nlh); 422677162022SJohn Fastabend if (ndm->ndm_ifindex == 0) { 4227b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid ifindex"); 422877162022SJohn Fastabend return -EINVAL; 422977162022SJohn Fastabend } 423077162022SJohn Fastabend 423177162022SJohn Fastabend dev = __dev_get_by_index(net, ndm->ndm_ifindex); 423277162022SJohn Fastabend if (dev == NULL) { 4233b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 423477162022SJohn Fastabend return -ENODEV; 423577162022SJohn Fastabend } 423677162022SJohn Fastabend 423777162022SJohn Fastabend if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { 4238b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid address"); 423977162022SJohn Fastabend return -EINVAL; 424077162022SJohn Fastabend } 424177162022SJohn Fastabend 4242da715775SIdo Schimmel if (dev->type != ARPHRD_ETHER) { 4243da715775SIdo Schimmel NL_SET_ERR_MSG(extack, "FDB add only supported for Ethernet devices"); 4244da715775SIdo Schimmel return -EINVAL; 4245da715775SIdo Schimmel } 4246da715775SIdo Schimmel 424777162022SJohn Fastabend addr = nla_data(tb[NDA_LLADDR]); 424877162022SJohn Fastabend 4249b88d12e4SFlorian Westphal err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack); 4250f6f6424bSJiri Pirko if (err) 4251f6f6424bSJiri Pirko return err; 4252f6f6424bSJiri Pirko 425377162022SJohn Fastabend err = -EOPNOTSUPP; 425477162022SJohn Fastabend 425577162022SJohn Fastabend /* Support fdb on master device the net/bridge default case */ 425677162022SJohn Fastabend if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && 42572e92a2d0SJulian Wiedmann netif_is_bridge_port(dev)) { 4258898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4259898e5061SJiri Pirko const struct net_device_ops *ops = br_dev->netdev_ops; 4260898e5061SJiri Pirko 4261f6f6424bSJiri Pirko err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid, 426287b0984eSPetr Machata nlh->nlmsg_flags, extack); 426377162022SJohn Fastabend if (err) 426477162022SJohn Fastabend goto out; 426577162022SJohn Fastabend else 426677162022SJohn Fastabend ndm->ndm_flags &= ~NTF_MASTER; 426777162022SJohn Fastabend } 426877162022SJohn Fastabend 426977162022SJohn Fastabend /* Embedded bridge, macvlan, and any other device support */ 4270090096bfSVlad Yasevich if ((ndm->ndm_flags & NTF_SELF)) { 4271090096bfSVlad Yasevich if (dev->netdev_ops->ndo_fdb_add) 4272090096bfSVlad Yasevich err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr, 4273f6f6424bSJiri Pirko vid, 427487b0984eSPetr Machata nlh->nlmsg_flags, 427587b0984eSPetr Machata extack); 4276090096bfSVlad Yasevich else 4277f6f6424bSJiri Pirko err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid, 427877162022SJohn Fastabend nlh->nlmsg_flags); 427977162022SJohn Fastabend 42803ff661c3SJohn Fastabend if (!err) { 4281b3379041SHubert Sokolowski rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH, 4282b3379041SHubert Sokolowski ndm->ndm_state); 428377162022SJohn Fastabend ndm->ndm_flags &= ~NTF_SELF; 428477162022SJohn Fastabend } 42853ff661c3SJohn Fastabend } 428677162022SJohn Fastabend out: 428777162022SJohn Fastabend return err; 428877162022SJohn Fastabend } 428977162022SJohn Fastabend 4290a986967eSBart Van Assche /* 4291090096bfSVlad Yasevich * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry 4292090096bfSVlad Yasevich */ 4293090096bfSVlad Yasevich int ndo_dflt_fdb_del(struct ndmsg *ndm, 4294090096bfSVlad Yasevich struct nlattr *tb[], 4295090096bfSVlad Yasevich struct net_device *dev, 4296f6f6424bSJiri Pirko const unsigned char *addr, u16 vid) 4297090096bfSVlad Yasevich { 4298c8a89c4aSAlexander Duyck int err = -EINVAL; 4299090096bfSVlad Yasevich 4300090096bfSVlad Yasevich /* If aging addresses are supported device will need to 4301090096bfSVlad Yasevich * implement its own handler for this. 4302090096bfSVlad Yasevich */ 430364535993SSridhar Samudrala if (!(ndm->ndm_state & NUD_PERMANENT)) { 430478ecc890SVladimir Oltean netdev_info(dev, "default FDB implementation only supports local addresses\n"); 4305c8a89c4aSAlexander Duyck return err; 4306090096bfSVlad Yasevich } 4307090096bfSVlad Yasevich 4308090096bfSVlad Yasevich if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) 4309090096bfSVlad Yasevich err = dev_uc_del(dev, addr); 4310090096bfSVlad Yasevich else if (is_multicast_ether_addr(addr)) 4311090096bfSVlad Yasevich err = dev_mc_del(dev, addr); 4312090096bfSVlad Yasevich 4313090096bfSVlad Yasevich return err; 4314090096bfSVlad Yasevich } 4315090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_del); 4316090096bfSVlad Yasevich 43179e834259SNikolay Aleksandrov static const struct nla_policy fdb_del_bulk_policy[NDA_MAX + 1] = { 43189e834259SNikolay Aleksandrov [NDA_VLAN] = { .type = NLA_U16 }, 43199e834259SNikolay Aleksandrov [NDA_IFINDEX] = NLA_POLICY_MIN(NLA_S32, 1), 4320ea2c0f9eSNikolay Aleksandrov [NDA_NDM_STATE_MASK] = { .type = NLA_U16 }, 4321ea2c0f9eSNikolay Aleksandrov [NDA_NDM_FLAGS_MASK] = { .type = NLA_U8 }, 43229e834259SNikolay Aleksandrov }; 43239e834259SNikolay Aleksandrov 4324c21ef3e3SDavid Ahern static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh, 4325c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 432677162022SJohn Fastabend { 43279e834259SNikolay Aleksandrov bool del_bulk = !!(nlh->nlmsg_flags & NLM_F_BULK); 432877162022SJohn Fastabend struct net *net = sock_net(skb->sk); 43299e834259SNikolay Aleksandrov const struct net_device_ops *ops; 433077162022SJohn Fastabend struct ndmsg *ndm; 43311690be63SVlad Yasevich struct nlattr *tb[NDA_MAX+1]; 433277162022SJohn Fastabend struct net_device *dev; 43339e834259SNikolay Aleksandrov __u8 *addr = NULL; 43347d311801SColin Ian King int err; 4335f6f6424bSJiri Pirko u16 vid; 433677162022SJohn Fastabend 433790f62cf3SEric W. Biederman if (!netlink_capable(skb, CAP_NET_ADMIN)) 43381690be63SVlad Yasevich return -EPERM; 43391690be63SVlad Yasevich 43409e834259SNikolay Aleksandrov if (!del_bulk) { 43419e834259SNikolay Aleksandrov err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, 43429e834259SNikolay Aleksandrov NULL, extack); 43439e834259SNikolay Aleksandrov } else { 43449e834259SNikolay Aleksandrov err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, 43459e834259SNikolay Aleksandrov fdb_del_bulk_policy, extack); 43469e834259SNikolay Aleksandrov } 43471690be63SVlad Yasevich if (err < 0) 43481690be63SVlad Yasevich return err; 434977162022SJohn Fastabend 435077162022SJohn Fastabend ndm = nlmsg_data(nlh); 435177162022SJohn Fastabend if (ndm->ndm_ifindex == 0) { 4352b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid ifindex"); 435377162022SJohn Fastabend return -EINVAL; 435477162022SJohn Fastabend } 435577162022SJohn Fastabend 435677162022SJohn Fastabend dev = __dev_get_by_index(net, ndm->ndm_ifindex); 435777162022SJohn Fastabend if (dev == NULL) { 4358b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 435977162022SJohn Fastabend return -ENODEV; 436077162022SJohn Fastabend } 436177162022SJohn Fastabend 43629e834259SNikolay Aleksandrov if (!del_bulk) { 43631690be63SVlad Yasevich if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { 4364b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid address"); 436577162022SJohn Fastabend return -EINVAL; 436677162022SJohn Fastabend } 43679e834259SNikolay Aleksandrov addr = nla_data(tb[NDA_LLADDR]); 43689e834259SNikolay Aleksandrov } 436977162022SJohn Fastabend 4370da715775SIdo Schimmel if (dev->type != ARPHRD_ETHER) { 4371da715775SIdo Schimmel NL_SET_ERR_MSG(extack, "FDB delete only supported for Ethernet devices"); 4372da715775SIdo Schimmel return -EINVAL; 4373da715775SIdo Schimmel } 4374da715775SIdo Schimmel 4375b88d12e4SFlorian Westphal err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack); 4376f6f6424bSJiri Pirko if (err) 4377f6f6424bSJiri Pirko return err; 4378f6f6424bSJiri Pirko 437977162022SJohn Fastabend err = -EOPNOTSUPP; 438077162022SJohn Fastabend 438177162022SJohn Fastabend /* Support fdb on master device the net/bridge default case */ 438277162022SJohn Fastabend if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && 43832e92a2d0SJulian Wiedmann netif_is_bridge_port(dev)) { 4384898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 438577162022SJohn Fastabend 43869e834259SNikolay Aleksandrov ops = br_dev->netdev_ops; 43879e834259SNikolay Aleksandrov if (!del_bulk) { 4388898e5061SJiri Pirko if (ops->ndo_fdb_del) 4389ca4567f1SAlaa Mohamed err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid, extack); 43909e834259SNikolay Aleksandrov } else { 43919e834259SNikolay Aleksandrov if (ops->ndo_fdb_del_bulk) 43929e834259SNikolay Aleksandrov err = ops->ndo_fdb_del_bulk(ndm, tb, dev, vid, 43939e834259SNikolay Aleksandrov extack); 43949e834259SNikolay Aleksandrov } 439577162022SJohn Fastabend 439677162022SJohn Fastabend if (err) 439777162022SJohn Fastabend goto out; 439877162022SJohn Fastabend else 439977162022SJohn Fastabend ndm->ndm_flags &= ~NTF_MASTER; 440077162022SJohn Fastabend } 440177162022SJohn Fastabend 440277162022SJohn Fastabend /* Embedded bridge, macvlan, and any other device support */ 4403090096bfSVlad Yasevich if (ndm->ndm_flags & NTF_SELF) { 44049e834259SNikolay Aleksandrov ops = dev->netdev_ops; 44059e834259SNikolay Aleksandrov if (!del_bulk) { 44069e834259SNikolay Aleksandrov if (ops->ndo_fdb_del) 4407ca4567f1SAlaa Mohamed err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid, extack); 4408090096bfSVlad Yasevich else 4409f6f6424bSJiri Pirko err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid); 44109e834259SNikolay Aleksandrov } else { 44119e834259SNikolay Aleksandrov /* in case err was cleared by NTF_MASTER call */ 44129e834259SNikolay Aleksandrov err = -EOPNOTSUPP; 44139e834259SNikolay Aleksandrov if (ops->ndo_fdb_del_bulk) 44149e834259SNikolay Aleksandrov err = ops->ndo_fdb_del_bulk(ndm, tb, dev, vid, 44159e834259SNikolay Aleksandrov extack); 44169e834259SNikolay Aleksandrov } 441777162022SJohn Fastabend 44183ff661c3SJohn Fastabend if (!err) { 44199e834259SNikolay Aleksandrov if (!del_bulk) 4420b3379041SHubert Sokolowski rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH, 4421b3379041SHubert Sokolowski ndm->ndm_state); 442277162022SJohn Fastabend ndm->ndm_flags &= ~NTF_SELF; 442377162022SJohn Fastabend } 44243ff661c3SJohn Fastabend } 442577162022SJohn Fastabend out: 442677162022SJohn Fastabend return err; 442777162022SJohn Fastabend } 442877162022SJohn Fastabend 4429d83b0603SJohn Fastabend static int nlmsg_populate_fdb(struct sk_buff *skb, 4430d83b0603SJohn Fastabend struct netlink_callback *cb, 4431d83b0603SJohn Fastabend struct net_device *dev, 4432d83b0603SJohn Fastabend int *idx, 4433d83b0603SJohn Fastabend struct netdev_hw_addr_list *list) 4434d83b0603SJohn Fastabend { 4435d83b0603SJohn Fastabend struct netdev_hw_addr *ha; 4436d83b0603SJohn Fastabend int err; 443715e47304SEric W. Biederman u32 portid, seq; 4438d83b0603SJohn Fastabend 443915e47304SEric W. Biederman portid = NETLINK_CB(cb->skb).portid; 4440d83b0603SJohn Fastabend seq = cb->nlh->nlmsg_seq; 4441d83b0603SJohn Fastabend 4442d83b0603SJohn Fastabend list_for_each_entry(ha, &list->list, list) { 4443d297653dSRoopa Prabhu if (*idx < cb->args[2]) 4444d83b0603SJohn Fastabend goto skip; 4445d83b0603SJohn Fastabend 44461e53d5bbSHubert Sokolowski err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0, 4447a7a558feSJohn Fastabend portid, seq, 44481c104a6bSNicolas Dichtel RTM_NEWNEIGH, NTF_SELF, 4449b3379041SHubert Sokolowski NLM_F_MULTI, NUD_PERMANENT); 4450d83b0603SJohn Fastabend if (err < 0) 4451d83b0603SJohn Fastabend return err; 4452d83b0603SJohn Fastabend skip: 4453d83b0603SJohn Fastabend *idx += 1; 4454d83b0603SJohn Fastabend } 4455d83b0603SJohn Fastabend return 0; 4456d83b0603SJohn Fastabend } 4457d83b0603SJohn Fastabend 4458d83b0603SJohn Fastabend /** 44592c53040fSBen Hutchings * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table. 4460a986967eSBart Van Assche * @skb: socket buffer to store message in 4461a986967eSBart Van Assche * @cb: netlink callback 4462d83b0603SJohn Fastabend * @dev: netdevice 4463a986967eSBart Van Assche * @filter_dev: ignored 4464a986967eSBart Van Assche * @idx: the number of FDB table entries dumped is added to *@idx 4465d83b0603SJohn Fastabend * 4466d83b0603SJohn Fastabend * Default netdevice operation to dump the existing unicast address list. 446791f3e7b1SJohn Fastabend * Returns number of addresses from list put in skb. 4468d83b0603SJohn Fastabend */ 4469d83b0603SJohn Fastabend int ndo_dflt_fdb_dump(struct sk_buff *skb, 4470d83b0603SJohn Fastabend struct netlink_callback *cb, 4471d83b0603SJohn Fastabend struct net_device *dev, 44725d5eacb3SJamal Hadi Salim struct net_device *filter_dev, 4473d297653dSRoopa Prabhu int *idx) 4474d83b0603SJohn Fastabend { 4475d83b0603SJohn Fastabend int err; 4476d83b0603SJohn Fastabend 447768883893SEric Dumazet if (dev->type != ARPHRD_ETHER) 447868883893SEric Dumazet return -EINVAL; 447968883893SEric Dumazet 4480d83b0603SJohn Fastabend netif_addr_lock_bh(dev); 4481d297653dSRoopa Prabhu err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc); 4482d83b0603SJohn Fastabend if (err) 4483d83b0603SJohn Fastabend goto out; 44842934c9dbSZhang Shengju err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc); 4485d83b0603SJohn Fastabend out: 4486d83b0603SJohn Fastabend netif_addr_unlock_bh(dev); 4487d297653dSRoopa Prabhu return err; 4488d83b0603SJohn Fastabend } 4489d83b0603SJohn Fastabend EXPORT_SYMBOL(ndo_dflt_fdb_dump); 4490d83b0603SJohn Fastabend 44918c6e137fSDavid Ahern static int valid_fdb_dump_strict(const struct nlmsghdr *nlh, 44928c6e137fSDavid Ahern int *br_idx, int *brport_idx, 44938c6e137fSDavid Ahern struct netlink_ext_ack *extack) 44948c6e137fSDavid Ahern { 44958c6e137fSDavid Ahern struct nlattr *tb[NDA_MAX + 1]; 44968c6e137fSDavid Ahern struct ndmsg *ndm; 44978c6e137fSDavid Ahern int err, i; 44988c6e137fSDavid Ahern 44998c6e137fSDavid Ahern if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) { 45008c6e137fSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid header for fdb dump request"); 45018c6e137fSDavid Ahern return -EINVAL; 45028c6e137fSDavid Ahern } 45038c6e137fSDavid Ahern 45048c6e137fSDavid Ahern ndm = nlmsg_data(nlh); 45058c6e137fSDavid Ahern if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state || 45068c6e137fSDavid Ahern ndm->ndm_flags || ndm->ndm_type) { 45078b73018fSNikolay Aleksandrov NL_SET_ERR_MSG(extack, "Invalid values in header for fdb dump request"); 45088c6e137fSDavid Ahern return -EINVAL; 45098c6e137fSDavid Ahern } 45108c6e137fSDavid Ahern 45118cb08174SJohannes Berg err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb, 45128cb08174SJohannes Berg NDA_MAX, NULL, extack); 45138c6e137fSDavid Ahern if (err < 0) 45148c6e137fSDavid Ahern return err; 45158c6e137fSDavid Ahern 45168c6e137fSDavid Ahern *brport_idx = ndm->ndm_ifindex; 45178c6e137fSDavid Ahern for (i = 0; i <= NDA_MAX; ++i) { 45188c6e137fSDavid Ahern if (!tb[i]) 45198c6e137fSDavid Ahern continue; 45208c6e137fSDavid Ahern 45218c6e137fSDavid Ahern switch (i) { 45228c6e137fSDavid Ahern case NDA_IFINDEX: 45238c6e137fSDavid Ahern if (nla_len(tb[i]) != sizeof(u32)) { 45248c6e137fSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid IFINDEX attribute in fdb dump request"); 45258c6e137fSDavid Ahern return -EINVAL; 45268c6e137fSDavid Ahern } 45278c6e137fSDavid Ahern *brport_idx = nla_get_u32(tb[NDA_IFINDEX]); 45288c6e137fSDavid Ahern break; 45298c6e137fSDavid Ahern case NDA_MASTER: 45308c6e137fSDavid Ahern if (nla_len(tb[i]) != sizeof(u32)) { 45318c6e137fSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid MASTER attribute in fdb dump request"); 45328c6e137fSDavid Ahern return -EINVAL; 45338c6e137fSDavid Ahern } 45348c6e137fSDavid Ahern *br_idx = nla_get_u32(tb[NDA_MASTER]); 45358c6e137fSDavid Ahern break; 45368c6e137fSDavid Ahern default: 45378c6e137fSDavid Ahern NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb dump request"); 45388c6e137fSDavid Ahern return -EINVAL; 45398c6e137fSDavid Ahern } 45408c6e137fSDavid Ahern } 45418c6e137fSDavid Ahern 45428c6e137fSDavid Ahern return 0; 45438c6e137fSDavid Ahern } 45448c6e137fSDavid Ahern 45458dfbda19SDavid Ahern static int valid_fdb_dump_legacy(const struct nlmsghdr *nlh, 45468dfbda19SDavid Ahern int *br_idx, int *brport_idx, 45478dfbda19SDavid Ahern struct netlink_ext_ack *extack) 45488dfbda19SDavid Ahern { 45498dfbda19SDavid Ahern struct nlattr *tb[IFLA_MAX+1]; 45508dfbda19SDavid Ahern int err; 45518dfbda19SDavid Ahern 45528dfbda19SDavid Ahern /* A hack to preserve kernel<->userspace interface. 45538dfbda19SDavid Ahern * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0. 45548dfbda19SDavid Ahern * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails. 45558dfbda19SDavid Ahern * So, check for ndmsg with an optional u32 attribute (not used here). 45568dfbda19SDavid Ahern * Fortunately these sizes don't conflict with the size of ifinfomsg 45578dfbda19SDavid Ahern * with an optional attribute. 45588dfbda19SDavid Ahern */ 45598dfbda19SDavid Ahern if (nlmsg_len(nlh) != sizeof(struct ndmsg) && 45608dfbda19SDavid Ahern (nlmsg_len(nlh) != sizeof(struct ndmsg) + 45618dfbda19SDavid Ahern nla_attr_size(sizeof(u32)))) { 45624565d7e5SDavid Ahern struct ifinfomsg *ifm; 45634565d7e5SDavid Ahern 45648cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg), 45658cb08174SJohannes Berg tb, IFLA_MAX, ifla_policy, 45668cb08174SJohannes Berg extack); 45678dfbda19SDavid Ahern if (err < 0) { 45688dfbda19SDavid Ahern return -EINVAL; 45698dfbda19SDavid Ahern } else if (err == 0) { 45708dfbda19SDavid Ahern if (tb[IFLA_MASTER]) 45718dfbda19SDavid Ahern *br_idx = nla_get_u32(tb[IFLA_MASTER]); 45728dfbda19SDavid Ahern } 45738dfbda19SDavid Ahern 45744565d7e5SDavid Ahern ifm = nlmsg_data(nlh); 45758dfbda19SDavid Ahern *brport_idx = ifm->ifi_index; 45768dfbda19SDavid Ahern } 45778dfbda19SDavid Ahern return 0; 45788dfbda19SDavid Ahern } 45798dfbda19SDavid Ahern 458077162022SJohn Fastabend static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb) 458177162022SJohn Fastabend { 458277162022SJohn Fastabend struct net_device *dev; 45835e6d2435SJamal Hadi Salim struct net_device *br_dev = NULL; 45845e6d2435SJamal Hadi Salim const struct net_device_ops *ops = NULL; 45855e6d2435SJamal Hadi Salim const struct net_device_ops *cops = NULL; 45865e6d2435SJamal Hadi Salim struct net *net = sock_net(skb->sk); 4587d297653dSRoopa Prabhu struct hlist_head *head; 45885e6d2435SJamal Hadi Salim int brport_idx = 0; 45895e6d2435SJamal Hadi Salim int br_idx = 0; 4590d297653dSRoopa Prabhu int h, s_h; 4591d297653dSRoopa Prabhu int idx = 0, s_idx; 4592d297653dSRoopa Prabhu int err = 0; 4593d297653dSRoopa Prabhu int fidx = 0; 459477162022SJohn Fastabend 45958c6e137fSDavid Ahern if (cb->strict_check) 45968c6e137fSDavid Ahern err = valid_fdb_dump_strict(cb->nlh, &br_idx, &brport_idx, 45978c6e137fSDavid Ahern cb->extack); 45988c6e137fSDavid Ahern else 45998dfbda19SDavid Ahern err = valid_fdb_dump_legacy(cb->nlh, &br_idx, &brport_idx, 46008dfbda19SDavid Ahern cb->extack); 46018dfbda19SDavid Ahern if (err < 0) 46028dfbda19SDavid Ahern return err; 46035e6d2435SJamal Hadi Salim 46045e6d2435SJamal Hadi Salim if (br_idx) { 46055e6d2435SJamal Hadi Salim br_dev = __dev_get_by_index(net, br_idx); 46065e6d2435SJamal Hadi Salim if (!br_dev) 46075e6d2435SJamal Hadi Salim return -ENODEV; 46085e6d2435SJamal Hadi Salim 4609898e5061SJiri Pirko ops = br_dev->netdev_ops; 46105e6d2435SJamal Hadi Salim } 46115e6d2435SJamal Hadi Salim 4612d297653dSRoopa Prabhu s_h = cb->args[0]; 4613d297653dSRoopa Prabhu s_idx = cb->args[1]; 4614d297653dSRoopa Prabhu 4615d297653dSRoopa Prabhu for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 4616d297653dSRoopa Prabhu idx = 0; 4617d297653dSRoopa Prabhu head = &net->dev_index_head[h]; 4618d297653dSRoopa Prabhu hlist_for_each_entry(dev, head, index_hlist) { 4619d297653dSRoopa Prabhu 46205e6d2435SJamal Hadi Salim if (brport_idx && (dev->ifindex != brport_idx)) 46215e6d2435SJamal Hadi Salim continue; 46225e6d2435SJamal Hadi Salim 46235e6d2435SJamal Hadi Salim if (!br_idx) { /* user did not specify a specific bridge */ 46242e92a2d0SJulian Wiedmann if (netif_is_bridge_port(dev)) { 46255e6d2435SJamal Hadi Salim br_dev = netdev_master_upper_dev_get(dev); 46265e6d2435SJamal Hadi Salim cops = br_dev->netdev_ops; 46275e6d2435SJamal Hadi Salim } 46285e6d2435SJamal Hadi Salim } else { 46295e6d2435SJamal Hadi Salim if (dev != br_dev && 46302e92a2d0SJulian Wiedmann !netif_is_bridge_port(dev)) 46315e6d2435SJamal Hadi Salim continue; 46325e6d2435SJamal Hadi Salim 46335e6d2435SJamal Hadi Salim if (br_dev != netdev_master_upper_dev_get(dev) && 4634254ec036SKyungrok Chung !netif_is_bridge_master(dev)) 46355e6d2435SJamal Hadi Salim continue; 46365e6d2435SJamal Hadi Salim cops = ops; 46375e6d2435SJamal Hadi Salim } 46385e6d2435SJamal Hadi Salim 4639d297653dSRoopa Prabhu if (idx < s_idx) 4640d297653dSRoopa Prabhu goto cont; 4641d297653dSRoopa Prabhu 46422e92a2d0SJulian Wiedmann if (netif_is_bridge_port(dev)) { 4643d297653dSRoopa Prabhu if (cops && cops->ndo_fdb_dump) { 4644d297653dSRoopa Prabhu err = cops->ndo_fdb_dump(skb, cb, 4645d297653dSRoopa Prabhu br_dev, dev, 4646d297653dSRoopa Prabhu &fidx); 4647d297653dSRoopa Prabhu if (err == -EMSGSIZE) 4648d297653dSRoopa Prabhu goto out; 464977162022SJohn Fastabend } 4650d297653dSRoopa Prabhu } 465177162022SJohn Fastabend 46525e6d2435SJamal Hadi Salim if (dev->netdev_ops->ndo_fdb_dump) 4653d297653dSRoopa Prabhu err = dev->netdev_ops->ndo_fdb_dump(skb, cb, 4654d297653dSRoopa Prabhu dev, NULL, 4655d297653dSRoopa Prabhu &fidx); 46566cb69742SHubert Sokolowski else 4657d297653dSRoopa Prabhu err = ndo_dflt_fdb_dump(skb, cb, dev, NULL, 4658d297653dSRoopa Prabhu &fidx); 4659d297653dSRoopa Prabhu if (err == -EMSGSIZE) 4660d297653dSRoopa Prabhu goto out; 46615e6d2435SJamal Hadi Salim 46625e6d2435SJamal Hadi Salim cops = NULL; 4663d297653dSRoopa Prabhu 4664d297653dSRoopa Prabhu /* reset fdb offset to 0 for rest of the interfaces */ 4665d297653dSRoopa Prabhu cb->args[2] = 0; 4666d297653dSRoopa Prabhu fidx = 0; 4667d297653dSRoopa Prabhu cont: 4668d297653dSRoopa Prabhu idx++; 4669d297653dSRoopa Prabhu } 467077162022SJohn Fastabend } 467177162022SJohn Fastabend 4672d297653dSRoopa Prabhu out: 4673d297653dSRoopa Prabhu cb->args[0] = h; 4674d297653dSRoopa Prabhu cb->args[1] = idx; 4675d297653dSRoopa Prabhu cb->args[2] = fidx; 4676d297653dSRoopa Prabhu 467777162022SJohn Fastabend return skb->len; 467877162022SJohn Fastabend } 467977162022SJohn Fastabend 46805b2f94b2SRoopa Prabhu static int valid_fdb_get_strict(const struct nlmsghdr *nlh, 46815b2f94b2SRoopa Prabhu struct nlattr **tb, u8 *ndm_flags, 46825b2f94b2SRoopa Prabhu int *br_idx, int *brport_idx, u8 **addr, 46835b2f94b2SRoopa Prabhu u16 *vid, struct netlink_ext_ack *extack) 46845b2f94b2SRoopa Prabhu { 46855b2f94b2SRoopa Prabhu struct ndmsg *ndm; 46865b2f94b2SRoopa Prabhu int err, i; 46875b2f94b2SRoopa Prabhu 46885b2f94b2SRoopa Prabhu if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) { 46895b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid header for fdb get request"); 46905b2f94b2SRoopa Prabhu return -EINVAL; 46915b2f94b2SRoopa Prabhu } 46925b2f94b2SRoopa Prabhu 46935b2f94b2SRoopa Prabhu ndm = nlmsg_data(nlh); 46945b2f94b2SRoopa Prabhu if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state || 46955b2f94b2SRoopa Prabhu ndm->ndm_type) { 46965b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid values in header for fdb get request"); 46975b2f94b2SRoopa Prabhu return -EINVAL; 46985b2f94b2SRoopa Prabhu } 46995b2f94b2SRoopa Prabhu 47005b2f94b2SRoopa Prabhu if (ndm->ndm_flags & ~(NTF_MASTER | NTF_SELF)) { 47015b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid flags in header for fdb get request"); 47025b2f94b2SRoopa Prabhu return -EINVAL; 47035b2f94b2SRoopa Prabhu } 47045b2f94b2SRoopa Prabhu 47058cb08174SJohannes Berg err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb, 47068cb08174SJohannes Berg NDA_MAX, nda_policy, extack); 47075b2f94b2SRoopa Prabhu if (err < 0) 47085b2f94b2SRoopa Prabhu return err; 47095b2f94b2SRoopa Prabhu 47105b2f94b2SRoopa Prabhu *ndm_flags = ndm->ndm_flags; 47115b2f94b2SRoopa Prabhu *brport_idx = ndm->ndm_ifindex; 47125b2f94b2SRoopa Prabhu for (i = 0; i <= NDA_MAX; ++i) { 47135b2f94b2SRoopa Prabhu if (!tb[i]) 47145b2f94b2SRoopa Prabhu continue; 47155b2f94b2SRoopa Prabhu 47165b2f94b2SRoopa Prabhu switch (i) { 47175b2f94b2SRoopa Prabhu case NDA_MASTER: 47185b2f94b2SRoopa Prabhu *br_idx = nla_get_u32(tb[i]); 47195b2f94b2SRoopa Prabhu break; 47205b2f94b2SRoopa Prabhu case NDA_LLADDR: 47215b2f94b2SRoopa Prabhu if (nla_len(tb[i]) != ETH_ALEN) { 47225b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid address in fdb get request"); 47235b2f94b2SRoopa Prabhu return -EINVAL; 47245b2f94b2SRoopa Prabhu } 47255b2f94b2SRoopa Prabhu *addr = nla_data(tb[i]); 47265b2f94b2SRoopa Prabhu break; 47275b2f94b2SRoopa Prabhu case NDA_VLAN: 47285b2f94b2SRoopa Prabhu err = fdb_vid_parse(tb[i], vid, extack); 47295b2f94b2SRoopa Prabhu if (err) 47305b2f94b2SRoopa Prabhu return err; 47315b2f94b2SRoopa Prabhu break; 47325b2f94b2SRoopa Prabhu case NDA_VNI: 47335b2f94b2SRoopa Prabhu break; 47345b2f94b2SRoopa Prabhu default: 47355b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb get request"); 47365b2f94b2SRoopa Prabhu return -EINVAL; 47375b2f94b2SRoopa Prabhu } 47385b2f94b2SRoopa Prabhu } 47395b2f94b2SRoopa Prabhu 47405b2f94b2SRoopa Prabhu return 0; 47415b2f94b2SRoopa Prabhu } 47425b2f94b2SRoopa Prabhu 47435b2f94b2SRoopa Prabhu static int rtnl_fdb_get(struct sk_buff *in_skb, struct nlmsghdr *nlh, 47445b2f94b2SRoopa Prabhu struct netlink_ext_ack *extack) 47455b2f94b2SRoopa Prabhu { 47465b2f94b2SRoopa Prabhu struct net_device *dev = NULL, *br_dev = NULL; 47475b2f94b2SRoopa Prabhu const struct net_device_ops *ops = NULL; 47485b2f94b2SRoopa Prabhu struct net *net = sock_net(in_skb->sk); 47495b2f94b2SRoopa Prabhu struct nlattr *tb[NDA_MAX + 1]; 47505b2f94b2SRoopa Prabhu struct sk_buff *skb; 47515b2f94b2SRoopa Prabhu int brport_idx = 0; 47525b2f94b2SRoopa Prabhu u8 ndm_flags = 0; 47535b2f94b2SRoopa Prabhu int br_idx = 0; 47545b2f94b2SRoopa Prabhu u8 *addr = NULL; 47555b2f94b2SRoopa Prabhu u16 vid = 0; 47565b2f94b2SRoopa Prabhu int err; 47575b2f94b2SRoopa Prabhu 47585b2f94b2SRoopa Prabhu err = valid_fdb_get_strict(nlh, tb, &ndm_flags, &br_idx, 47595b2f94b2SRoopa Prabhu &brport_idx, &addr, &vid, extack); 47605b2f94b2SRoopa Prabhu if (err < 0) 47615b2f94b2SRoopa Prabhu return err; 47625b2f94b2SRoopa Prabhu 4763f989d03eSNikolay Aleksandrov if (!addr) { 4764f989d03eSNikolay Aleksandrov NL_SET_ERR_MSG(extack, "Missing lookup address for fdb get request"); 4765f989d03eSNikolay Aleksandrov return -EINVAL; 4766f989d03eSNikolay Aleksandrov } 4767f989d03eSNikolay Aleksandrov 47685b2f94b2SRoopa Prabhu if (brport_idx) { 47695b2f94b2SRoopa Prabhu dev = __dev_get_by_index(net, brport_idx); 47705b2f94b2SRoopa Prabhu if (!dev) { 47715b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Unknown device ifindex"); 47725b2f94b2SRoopa Prabhu return -ENODEV; 47735b2f94b2SRoopa Prabhu } 47745b2f94b2SRoopa Prabhu } 47755b2f94b2SRoopa Prabhu 47765b2f94b2SRoopa Prabhu if (br_idx) { 47775b2f94b2SRoopa Prabhu if (dev) { 47785b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Master and device are mutually exclusive"); 47795b2f94b2SRoopa Prabhu return -EINVAL; 47805b2f94b2SRoopa Prabhu } 47815b2f94b2SRoopa Prabhu 47825b2f94b2SRoopa Prabhu br_dev = __dev_get_by_index(net, br_idx); 47835b2f94b2SRoopa Prabhu if (!br_dev) { 47845b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid master ifindex"); 47855b2f94b2SRoopa Prabhu return -EINVAL; 47865b2f94b2SRoopa Prabhu } 47875b2f94b2SRoopa Prabhu ops = br_dev->netdev_ops; 47885b2f94b2SRoopa Prabhu } 47895b2f94b2SRoopa Prabhu 47905b2f94b2SRoopa Prabhu if (dev) { 47915b2f94b2SRoopa Prabhu if (!ndm_flags || (ndm_flags & NTF_MASTER)) { 47922e92a2d0SJulian Wiedmann if (!netif_is_bridge_port(dev)) { 47935b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Device is not a bridge port"); 47945b2f94b2SRoopa Prabhu return -EINVAL; 47955b2f94b2SRoopa Prabhu } 47965b2f94b2SRoopa Prabhu br_dev = netdev_master_upper_dev_get(dev); 47975b2f94b2SRoopa Prabhu if (!br_dev) { 47985b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Master of device not found"); 47995b2f94b2SRoopa Prabhu return -EINVAL; 48005b2f94b2SRoopa Prabhu } 48015b2f94b2SRoopa Prabhu ops = br_dev->netdev_ops; 48025b2f94b2SRoopa Prabhu } else { 48035b2f94b2SRoopa Prabhu if (!(ndm_flags & NTF_SELF)) { 48045b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Missing NTF_SELF"); 48055b2f94b2SRoopa Prabhu return -EINVAL; 48065b2f94b2SRoopa Prabhu } 48075b2f94b2SRoopa Prabhu ops = dev->netdev_ops; 48085b2f94b2SRoopa Prabhu } 48095b2f94b2SRoopa Prabhu } 48105b2f94b2SRoopa Prabhu 48115b2f94b2SRoopa Prabhu if (!br_dev && !dev) { 48125b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "No device specified"); 48135b2f94b2SRoopa Prabhu return -ENODEV; 48145b2f94b2SRoopa Prabhu } 48155b2f94b2SRoopa Prabhu 48165b2f94b2SRoopa Prabhu if (!ops || !ops->ndo_fdb_get) { 48175b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Fdb get operation not supported by device"); 48185b2f94b2SRoopa Prabhu return -EOPNOTSUPP; 48195b2f94b2SRoopa Prabhu } 48205b2f94b2SRoopa Prabhu 48215b2f94b2SRoopa Prabhu skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); 48225b2f94b2SRoopa Prabhu if (!skb) 48235b2f94b2SRoopa Prabhu return -ENOBUFS; 48245b2f94b2SRoopa Prabhu 48255b2f94b2SRoopa Prabhu if (br_dev) 48265b2f94b2SRoopa Prabhu dev = br_dev; 48275b2f94b2SRoopa Prabhu err = ops->ndo_fdb_get(skb, tb, dev, addr, vid, 48285b2f94b2SRoopa Prabhu NETLINK_CB(in_skb).portid, 48295b2f94b2SRoopa Prabhu nlh->nlmsg_seq, extack); 48305b2f94b2SRoopa Prabhu if (err) 48315b2f94b2SRoopa Prabhu goto out; 48325b2f94b2SRoopa Prabhu 48335b2f94b2SRoopa Prabhu return rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 48345b2f94b2SRoopa Prabhu out: 48355b2f94b2SRoopa Prabhu kfree_skb(skb); 48365b2f94b2SRoopa Prabhu return err; 48375b2f94b2SRoopa Prabhu } 48385b2f94b2SRoopa Prabhu 48392c3c031cSScott Feldman static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask, 48402c3c031cSScott Feldman unsigned int attrnum, unsigned int flag) 48412c3c031cSScott Feldman { 48422c3c031cSScott Feldman if (mask & flag) 48432c3c031cSScott Feldman return nla_put_u8(skb, attrnum, !!(flags & flag)); 48442c3c031cSScott Feldman return 0; 48452c3c031cSScott Feldman } 48462c3c031cSScott Feldman 4847815cccbfSJohn Fastabend int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq, 48482c3c031cSScott Feldman struct net_device *dev, u16 mode, 48497d4f8d87SScott Feldman u32 flags, u32 mask, int nlflags, 48507d4f8d87SScott Feldman u32 filter_mask, 48517d4f8d87SScott Feldman int (*vlan_fill)(struct sk_buff *skb, 48527d4f8d87SScott Feldman struct net_device *dev, 48537d4f8d87SScott Feldman u32 filter_mask)) 4854815cccbfSJohn Fastabend { 4855815cccbfSJohn Fastabend struct nlmsghdr *nlh; 4856815cccbfSJohn Fastabend struct ifinfomsg *ifm; 4857815cccbfSJohn Fastabend struct nlattr *br_afspec; 48582c3c031cSScott Feldman struct nlattr *protinfo; 4859815cccbfSJohn Fastabend u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN; 4860898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 48617d4f8d87SScott Feldman int err = 0; 4862815cccbfSJohn Fastabend 486346c264daSNicolas Dichtel nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags); 4864815cccbfSJohn Fastabend if (nlh == NULL) 4865815cccbfSJohn Fastabend return -EMSGSIZE; 4866815cccbfSJohn Fastabend 4867815cccbfSJohn Fastabend ifm = nlmsg_data(nlh); 4868815cccbfSJohn Fastabend ifm->ifi_family = AF_BRIDGE; 4869815cccbfSJohn Fastabend ifm->__ifi_pad = 0; 4870815cccbfSJohn Fastabend ifm->ifi_type = dev->type; 4871815cccbfSJohn Fastabend ifm->ifi_index = dev->ifindex; 4872815cccbfSJohn Fastabend ifm->ifi_flags = dev_get_flags(dev); 4873815cccbfSJohn Fastabend ifm->ifi_change = 0; 4874815cccbfSJohn Fastabend 4875815cccbfSJohn Fastabend 4876815cccbfSJohn Fastabend if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 4877815cccbfSJohn Fastabend nla_put_u32(skb, IFLA_MTU, dev->mtu) || 4878815cccbfSJohn Fastabend nla_put_u8(skb, IFLA_OPERSTATE, operstate) || 4879898e5061SJiri Pirko (br_dev && 4880898e5061SJiri Pirko nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) || 4881815cccbfSJohn Fastabend (dev->addr_len && 4882815cccbfSJohn Fastabend nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || 4883a54acb3aSNicolas Dichtel (dev->ifindex != dev_get_iflink(dev) && 4884a54acb3aSNicolas Dichtel nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev)))) 4885815cccbfSJohn Fastabend goto nla_put_failure; 4886815cccbfSJohn Fastabend 4887ae0be8deSMichal Kubecek br_afspec = nla_nest_start_noflag(skb, IFLA_AF_SPEC); 4888815cccbfSJohn Fastabend if (!br_afspec) 4889815cccbfSJohn Fastabend goto nla_put_failure; 4890815cccbfSJohn Fastabend 48911d460b98SRoopa Prabhu if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) { 4892815cccbfSJohn Fastabend nla_nest_cancel(skb, br_afspec); 4893815cccbfSJohn Fastabend goto nla_put_failure; 4894815cccbfSJohn Fastabend } 48951d460b98SRoopa Prabhu 48961d460b98SRoopa Prabhu if (mode != BRIDGE_MODE_UNDEF) { 48971d460b98SRoopa Prabhu if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) { 48981d460b98SRoopa Prabhu nla_nest_cancel(skb, br_afspec); 48991d460b98SRoopa Prabhu goto nla_put_failure; 49001d460b98SRoopa Prabhu } 49011d460b98SRoopa Prabhu } 49027d4f8d87SScott Feldman if (vlan_fill) { 49037d4f8d87SScott Feldman err = vlan_fill(skb, dev, filter_mask); 49047d4f8d87SScott Feldman if (err) { 49057d4f8d87SScott Feldman nla_nest_cancel(skb, br_afspec); 49067d4f8d87SScott Feldman goto nla_put_failure; 49077d4f8d87SScott Feldman } 49087d4f8d87SScott Feldman } 4909815cccbfSJohn Fastabend nla_nest_end(skb, br_afspec); 4910815cccbfSJohn Fastabend 4911ae0be8deSMichal Kubecek protinfo = nla_nest_start(skb, IFLA_PROTINFO); 49122c3c031cSScott Feldman if (!protinfo) 49132c3c031cSScott Feldman goto nla_put_failure; 49142c3c031cSScott Feldman 49152c3c031cSScott Feldman if (brport_nla_put_flag(skb, flags, mask, 49162c3c031cSScott Feldman IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) || 49172c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 49182c3c031cSScott Feldman IFLA_BRPORT_GUARD, BR_BPDU_GUARD) || 49192c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 49202c3c031cSScott Feldman IFLA_BRPORT_FAST_LEAVE, 49212c3c031cSScott Feldman BR_MULTICAST_FAST_LEAVE) || 49222c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 49232c3c031cSScott Feldman IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) || 49242c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 49252c3c031cSScott Feldman IFLA_BRPORT_LEARNING, BR_LEARNING) || 49262c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 49272c3c031cSScott Feldman IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) || 49282c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 49292c3c031cSScott Feldman IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) || 49302c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 4931583cb0b4SJulian Wiedmann IFLA_BRPORT_PROXYARP, BR_PROXYARP) || 4932583cb0b4SJulian Wiedmann brport_nla_put_flag(skb, flags, mask, 4933583cb0b4SJulian Wiedmann IFLA_BRPORT_MCAST_FLOOD, BR_MCAST_FLOOD) || 4934583cb0b4SJulian Wiedmann brport_nla_put_flag(skb, flags, mask, 4935583cb0b4SJulian Wiedmann IFLA_BRPORT_BCAST_FLOOD, BR_BCAST_FLOOD)) { 49362c3c031cSScott Feldman nla_nest_cancel(skb, protinfo); 49372c3c031cSScott Feldman goto nla_put_failure; 49382c3c031cSScott Feldman } 49392c3c031cSScott Feldman 49402c3c031cSScott Feldman nla_nest_end(skb, protinfo); 49412c3c031cSScott Feldman 4942053c095aSJohannes Berg nlmsg_end(skb, nlh); 4943053c095aSJohannes Berg return 0; 4944815cccbfSJohn Fastabend nla_put_failure: 4945815cccbfSJohn Fastabend nlmsg_cancel(skb, nlh); 49467d4f8d87SScott Feldman return err ? err : -EMSGSIZE; 4947815cccbfSJohn Fastabend } 49487d4f8d87SScott Feldman EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink); 4949815cccbfSJohn Fastabend 49502d011be8SDavid Ahern static int valid_bridge_getlink_req(const struct nlmsghdr *nlh, 49512d011be8SDavid Ahern bool strict_check, u32 *filter_mask, 49522d011be8SDavid Ahern struct netlink_ext_ack *extack) 49532d011be8SDavid Ahern { 49542d011be8SDavid Ahern struct nlattr *tb[IFLA_MAX+1]; 49552d011be8SDavid Ahern int err, i; 49562d011be8SDavid Ahern 49572d011be8SDavid Ahern if (strict_check) { 49582d011be8SDavid Ahern struct ifinfomsg *ifm; 49592d011be8SDavid Ahern 49602d011be8SDavid Ahern if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { 49612d011be8SDavid Ahern NL_SET_ERR_MSG(extack, "Invalid header for bridge link dump"); 49622d011be8SDavid Ahern return -EINVAL; 49632d011be8SDavid Ahern } 49642d011be8SDavid Ahern 49652d011be8SDavid Ahern ifm = nlmsg_data(nlh); 49662d011be8SDavid Ahern if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags || 49672d011be8SDavid Ahern ifm->ifi_change || ifm->ifi_index) { 49682d011be8SDavid Ahern NL_SET_ERR_MSG(extack, "Invalid values in header for bridge link dump request"); 49692d011be8SDavid Ahern return -EINVAL; 49702d011be8SDavid Ahern } 49712d011be8SDavid Ahern 49728cb08174SJohannes Berg err = nlmsg_parse_deprecated_strict(nlh, 49738cb08174SJohannes Berg sizeof(struct ifinfomsg), 49748cb08174SJohannes Berg tb, IFLA_MAX, ifla_policy, 49758cb08174SJohannes Berg extack); 49762d011be8SDavid Ahern } else { 49778cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg), 49788cb08174SJohannes Berg tb, IFLA_MAX, ifla_policy, 49798cb08174SJohannes Berg extack); 49802d011be8SDavid Ahern } 49812d011be8SDavid Ahern if (err < 0) 49822d011be8SDavid Ahern return err; 49832d011be8SDavid Ahern 49842d011be8SDavid Ahern /* new attributes should only be added with strict checking */ 49852d011be8SDavid Ahern for (i = 0; i <= IFLA_MAX; ++i) { 49862d011be8SDavid Ahern if (!tb[i]) 49872d011be8SDavid Ahern continue; 49882d011be8SDavid Ahern 49892d011be8SDavid Ahern switch (i) { 49902d011be8SDavid Ahern case IFLA_EXT_MASK: 49912d011be8SDavid Ahern *filter_mask = nla_get_u32(tb[i]); 49922d011be8SDavid Ahern break; 49932d011be8SDavid Ahern default: 49942d011be8SDavid Ahern if (strict_check) { 49952d011be8SDavid Ahern NL_SET_ERR_MSG(extack, "Unsupported attribute in bridge link dump request"); 49962d011be8SDavid Ahern return -EINVAL; 49972d011be8SDavid Ahern } 49982d011be8SDavid Ahern } 49992d011be8SDavid Ahern } 50002d011be8SDavid Ahern 50012d011be8SDavid Ahern return 0; 50022d011be8SDavid Ahern } 50032d011be8SDavid Ahern 5004e5a55a89SJohn Fastabend static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb) 5005e5a55a89SJohn Fastabend { 50062d011be8SDavid Ahern const struct nlmsghdr *nlh = cb->nlh; 5007e5a55a89SJohn Fastabend struct net *net = sock_net(skb->sk); 5008e5a55a89SJohn Fastabend struct net_device *dev; 5009e5a55a89SJohn Fastabend int idx = 0; 5010e5a55a89SJohn Fastabend u32 portid = NETLINK_CB(cb->skb).portid; 50112d011be8SDavid Ahern u32 seq = nlh->nlmsg_seq; 50126cbdceebSVlad Yasevich u32 filter_mask = 0; 5013d64f69b0SRoopa Prabhu int err; 50146cbdceebSVlad Yasevich 50152d011be8SDavid Ahern err = valid_bridge_getlink_req(nlh, cb->strict_check, &filter_mask, 50162d011be8SDavid Ahern cb->extack); 50172d011be8SDavid Ahern if (err < 0 && cb->strict_check) 50182d011be8SDavid Ahern return err; 5019e5a55a89SJohn Fastabend 5020e5a55a89SJohn Fastabend rcu_read_lock(); 5021e5a55a89SJohn Fastabend for_each_netdev_rcu(net, dev) { 5022e5a55a89SJohn Fastabend const struct net_device_ops *ops = dev->netdev_ops; 5023898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 5024e5a55a89SJohn Fastabend 5025898e5061SJiri Pirko if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) { 5026d64f69b0SRoopa Prabhu if (idx >= cb->args[0]) { 5027d64f69b0SRoopa Prabhu err = br_dev->netdev_ops->ndo_bridge_getlink( 5028d64f69b0SRoopa Prabhu skb, portid, seq, dev, 5029d64f69b0SRoopa Prabhu filter_mask, NLM_F_MULTI); 5030f6c5775fSDavid Ahern if (err < 0 && err != -EOPNOTSUPP) { 5031f6c5775fSDavid Ahern if (likely(skb->len)) 5032e5a55a89SJohn Fastabend break; 5033f6c5775fSDavid Ahern 5034f6c5775fSDavid Ahern goto out_err; 5035f6c5775fSDavid Ahern } 5036d64f69b0SRoopa Prabhu } 5037e5a55a89SJohn Fastabend idx++; 5038e5a55a89SJohn Fastabend } 5039e5a55a89SJohn Fastabend 5040e5a55a89SJohn Fastabend if (ops->ndo_bridge_getlink) { 5041d64f69b0SRoopa Prabhu if (idx >= cb->args[0]) { 5042d64f69b0SRoopa Prabhu err = ops->ndo_bridge_getlink(skb, portid, 5043d64f69b0SRoopa Prabhu seq, dev, 504446c264daSNicolas Dichtel filter_mask, 5045d64f69b0SRoopa Prabhu NLM_F_MULTI); 5046f6c5775fSDavid Ahern if (err < 0 && err != -EOPNOTSUPP) { 5047f6c5775fSDavid Ahern if (likely(skb->len)) 5048e5a55a89SJohn Fastabend break; 5049f6c5775fSDavid Ahern 5050f6c5775fSDavid Ahern goto out_err; 5051f6c5775fSDavid Ahern } 5052d64f69b0SRoopa Prabhu } 5053e5a55a89SJohn Fastabend idx++; 5054e5a55a89SJohn Fastabend } 5055e5a55a89SJohn Fastabend } 5056f6c5775fSDavid Ahern err = skb->len; 5057f6c5775fSDavid Ahern out_err: 5058e5a55a89SJohn Fastabend rcu_read_unlock(); 5059e5a55a89SJohn Fastabend cb->args[0] = idx; 5060e5a55a89SJohn Fastabend 5061f6c5775fSDavid Ahern return err; 5062e5a55a89SJohn Fastabend } 5063e5a55a89SJohn Fastabend 50642469ffd7SJohn Fastabend static inline size_t bridge_nlmsg_size(void) 50652469ffd7SJohn Fastabend { 50662469ffd7SJohn Fastabend return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 50672469ffd7SJohn Fastabend + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 50682469ffd7SJohn Fastabend + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 50692469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_MASTER */ 50702469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_MTU */ 50712469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_LINK */ 50722469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */ 50732469ffd7SJohn Fastabend + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */ 50742469ffd7SJohn Fastabend + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */ 50752469ffd7SJohn Fastabend + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */ 50762469ffd7SJohn Fastabend + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */ 50772469ffd7SJohn Fastabend } 50782469ffd7SJohn Fastabend 507902dba438SRoopa Prabhu static int rtnl_bridge_notify(struct net_device *dev) 50802469ffd7SJohn Fastabend { 50812469ffd7SJohn Fastabend struct net *net = dev_net(dev); 50822469ffd7SJohn Fastabend struct sk_buff *skb; 50832469ffd7SJohn Fastabend int err = -EOPNOTSUPP; 50842469ffd7SJohn Fastabend 508502dba438SRoopa Prabhu if (!dev->netdev_ops->ndo_bridge_getlink) 508602dba438SRoopa Prabhu return 0; 508702dba438SRoopa Prabhu 50882469ffd7SJohn Fastabend skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC); 50892469ffd7SJohn Fastabend if (!skb) { 50902469ffd7SJohn Fastabend err = -ENOMEM; 50912469ffd7SJohn Fastabend goto errout; 50922469ffd7SJohn Fastabend } 50932469ffd7SJohn Fastabend 509446c264daSNicolas Dichtel err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0); 5095c38e01b8SJohn Fastabend if (err < 0) 5096c38e01b8SJohn Fastabend goto errout; 50972469ffd7SJohn Fastabend 5098d2e381c4SIdo Schimmel /* Notification info is only filled for bridge ports, not the bridge 5099d2e381c4SIdo Schimmel * device itself. Therefore, a zero notification length is valid and 5100d2e381c4SIdo Schimmel * should not result in an error. 5101d2e381c4SIdo Schimmel */ 5102d2e381c4SIdo Schimmel if (!skb->len) 510359ccaaaaSRoopa Prabhu goto errout; 510459ccaaaaSRoopa Prabhu 51052469ffd7SJohn Fastabend rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC); 51062469ffd7SJohn Fastabend return 0; 51072469ffd7SJohn Fastabend errout: 51082469ffd7SJohn Fastabend WARN_ON(err == -EMSGSIZE); 51092469ffd7SJohn Fastabend kfree_skb(skb); 511059ccaaaaSRoopa Prabhu if (err) 51112469ffd7SJohn Fastabend rtnl_set_sk_err(net, RTNLGRP_LINK, err); 51122469ffd7SJohn Fastabend return err; 51132469ffd7SJohn Fastabend } 51142469ffd7SJohn Fastabend 5115c21ef3e3SDavid Ahern static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, 5116c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 5117e5a55a89SJohn Fastabend { 5118e5a55a89SJohn Fastabend struct net *net = sock_net(skb->sk); 5119e5a55a89SJohn Fastabend struct ifinfomsg *ifm; 5120e5a55a89SJohn Fastabend struct net_device *dev; 51212469ffd7SJohn Fastabend struct nlattr *br_spec, *attr = NULL; 51222469ffd7SJohn Fastabend int rem, err = -EOPNOTSUPP; 51234de8b413SRosen, Rami u16 flags = 0; 5124c38e01b8SJohn Fastabend bool have_flags = false; 5125e5a55a89SJohn Fastabend 5126e5a55a89SJohn Fastabend if (nlmsg_len(nlh) < sizeof(*ifm)) 5127e5a55a89SJohn Fastabend return -EINVAL; 5128e5a55a89SJohn Fastabend 5129e5a55a89SJohn Fastabend ifm = nlmsg_data(nlh); 5130e5a55a89SJohn Fastabend if (ifm->ifi_family != AF_BRIDGE) 5131e5a55a89SJohn Fastabend return -EPFNOSUPPORT; 5132e5a55a89SJohn Fastabend 5133e5a55a89SJohn Fastabend dev = __dev_get_by_index(net, ifm->ifi_index); 5134e5a55a89SJohn Fastabend if (!dev) { 5135b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 5136e5a55a89SJohn Fastabend return -ENODEV; 5137e5a55a89SJohn Fastabend } 5138e5a55a89SJohn Fastabend 51392469ffd7SJohn Fastabend br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 51402469ffd7SJohn Fastabend if (br_spec) { 51412469ffd7SJohn Fastabend nla_for_each_nested(attr, br_spec, rem) { 51422469ffd7SJohn Fastabend if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { 51436e8d1c55SThomas Graf if (nla_len(attr) < sizeof(flags)) 51446e8d1c55SThomas Graf return -EINVAL; 51456e8d1c55SThomas Graf 5146c38e01b8SJohn Fastabend have_flags = true; 51472469ffd7SJohn Fastabend flags = nla_get_u16(attr); 51482469ffd7SJohn Fastabend break; 51492469ffd7SJohn Fastabend } 51502469ffd7SJohn Fastabend } 51512469ffd7SJohn Fastabend } 51522469ffd7SJohn Fastabend 51532469ffd7SJohn Fastabend if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { 5154898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 5155898e5061SJiri Pirko 5156898e5061SJiri Pirko if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) { 51572469ffd7SJohn Fastabend err = -EOPNOTSUPP; 5158e5a55a89SJohn Fastabend goto out; 5159e5a55a89SJohn Fastabend } 5160e5a55a89SJohn Fastabend 51612fd527b7SPetr Machata err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags, 51622fd527b7SPetr Machata extack); 51632469ffd7SJohn Fastabend if (err) 51642469ffd7SJohn Fastabend goto out; 51652469ffd7SJohn Fastabend 51662469ffd7SJohn Fastabend flags &= ~BRIDGE_FLAGS_MASTER; 51672469ffd7SJohn Fastabend } 51682469ffd7SJohn Fastabend 51692469ffd7SJohn Fastabend if ((flags & BRIDGE_FLAGS_SELF)) { 51702469ffd7SJohn Fastabend if (!dev->netdev_ops->ndo_bridge_setlink) 51712469ffd7SJohn Fastabend err = -EOPNOTSUPP; 51722469ffd7SJohn Fastabend else 5173add511b3SRoopa Prabhu err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh, 51742fd527b7SPetr Machata flags, 51752fd527b7SPetr Machata extack); 517602dba438SRoopa Prabhu if (!err) { 51772469ffd7SJohn Fastabend flags &= ~BRIDGE_FLAGS_SELF; 517802dba438SRoopa Prabhu 517902dba438SRoopa Prabhu /* Generate event to notify upper layer of bridge 518002dba438SRoopa Prabhu * change 518102dba438SRoopa Prabhu */ 518202dba438SRoopa Prabhu err = rtnl_bridge_notify(dev); 518302dba438SRoopa Prabhu } 51842469ffd7SJohn Fastabend } 51852469ffd7SJohn Fastabend 5186c38e01b8SJohn Fastabend if (have_flags) 51872469ffd7SJohn Fastabend memcpy(nla_data(attr), &flags, sizeof(flags)); 5188e5a55a89SJohn Fastabend out: 5189e5a55a89SJohn Fastabend return err; 5190e5a55a89SJohn Fastabend } 5191e5a55a89SJohn Fastabend 5192c21ef3e3SDavid Ahern static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, 5193c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 5194407af329SVlad Yasevich { 5195407af329SVlad Yasevich struct net *net = sock_net(skb->sk); 5196407af329SVlad Yasevich struct ifinfomsg *ifm; 5197407af329SVlad Yasevich struct net_device *dev; 5198407af329SVlad Yasevich struct nlattr *br_spec, *attr = NULL; 5199407af329SVlad Yasevich int rem, err = -EOPNOTSUPP; 52004de8b413SRosen, Rami u16 flags = 0; 5201407af329SVlad Yasevich bool have_flags = false; 5202407af329SVlad Yasevich 5203407af329SVlad Yasevich if (nlmsg_len(nlh) < sizeof(*ifm)) 5204407af329SVlad Yasevich return -EINVAL; 5205407af329SVlad Yasevich 5206407af329SVlad Yasevich ifm = nlmsg_data(nlh); 5207407af329SVlad Yasevich if (ifm->ifi_family != AF_BRIDGE) 5208407af329SVlad Yasevich return -EPFNOSUPPORT; 5209407af329SVlad Yasevich 5210407af329SVlad Yasevich dev = __dev_get_by_index(net, ifm->ifi_index); 5211407af329SVlad Yasevich if (!dev) { 5212b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 5213407af329SVlad Yasevich return -ENODEV; 5214407af329SVlad Yasevich } 5215407af329SVlad Yasevich 5216407af329SVlad Yasevich br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 5217407af329SVlad Yasevich if (br_spec) { 5218407af329SVlad Yasevich nla_for_each_nested(attr, br_spec, rem) { 5219407af329SVlad Yasevich if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { 52206e8d1c55SThomas Graf if (nla_len(attr) < sizeof(flags)) 52216e8d1c55SThomas Graf return -EINVAL; 52226e8d1c55SThomas Graf 5223407af329SVlad Yasevich have_flags = true; 5224407af329SVlad Yasevich flags = nla_get_u16(attr); 5225407af329SVlad Yasevich break; 5226407af329SVlad Yasevich } 5227407af329SVlad Yasevich } 5228407af329SVlad Yasevich } 5229407af329SVlad Yasevich 5230407af329SVlad Yasevich if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { 5231407af329SVlad Yasevich struct net_device *br_dev = netdev_master_upper_dev_get(dev); 5232407af329SVlad Yasevich 5233407af329SVlad Yasevich if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) { 5234407af329SVlad Yasevich err = -EOPNOTSUPP; 5235407af329SVlad Yasevich goto out; 5236407af329SVlad Yasevich } 5237407af329SVlad Yasevich 5238add511b3SRoopa Prabhu err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags); 5239407af329SVlad Yasevich if (err) 5240407af329SVlad Yasevich goto out; 5241407af329SVlad Yasevich 5242407af329SVlad Yasevich flags &= ~BRIDGE_FLAGS_MASTER; 5243407af329SVlad Yasevich } 5244407af329SVlad Yasevich 5245407af329SVlad Yasevich if ((flags & BRIDGE_FLAGS_SELF)) { 5246407af329SVlad Yasevich if (!dev->netdev_ops->ndo_bridge_dellink) 5247407af329SVlad Yasevich err = -EOPNOTSUPP; 5248407af329SVlad Yasevich else 5249add511b3SRoopa Prabhu err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh, 5250add511b3SRoopa Prabhu flags); 5251407af329SVlad Yasevich 525202dba438SRoopa Prabhu if (!err) { 5253407af329SVlad Yasevich flags &= ~BRIDGE_FLAGS_SELF; 525402dba438SRoopa Prabhu 525502dba438SRoopa Prabhu /* Generate event to notify upper layer of bridge 525602dba438SRoopa Prabhu * change 525702dba438SRoopa Prabhu */ 525802dba438SRoopa Prabhu err = rtnl_bridge_notify(dev); 525902dba438SRoopa Prabhu } 5260407af329SVlad Yasevich } 5261407af329SVlad Yasevich 5262407af329SVlad Yasevich if (have_flags) 5263407af329SVlad Yasevich memcpy(nla_data(attr), &flags, sizeof(flags)); 5264407af329SVlad Yasevich out: 5265407af329SVlad Yasevich return err; 5266407af329SVlad Yasevich } 5267407af329SVlad Yasevich 5268e8872a25SNikolay Aleksandrov static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr) 5269e8872a25SNikolay Aleksandrov { 5270e8872a25SNikolay Aleksandrov return (mask & IFLA_STATS_FILTER_BIT(attrid)) && 5271e8872a25SNikolay Aleksandrov (!idxattr || idxattr == attrid); 5272e8872a25SNikolay Aleksandrov } 5273e8872a25SNikolay Aleksandrov 5274f6e0fb81SPetr Machata static bool 5275f6e0fb81SPetr Machata rtnl_offload_xstats_have_ndo(const struct net_device *dev, int attr_id) 527669ae6ad2SNogah Frankel { 5277f6e0fb81SPetr Machata return dev->netdev_ops && 5278f6e0fb81SPetr Machata dev->netdev_ops->ndo_has_offload_stats && 5279f6e0fb81SPetr Machata dev->netdev_ops->ndo_get_offload_stats && 5280f6e0fb81SPetr Machata dev->netdev_ops->ndo_has_offload_stats(dev, attr_id); 528169ae6ad2SNogah Frankel } 528269ae6ad2SNogah Frankel 5283f6e0fb81SPetr Machata static unsigned int 5284f6e0fb81SPetr Machata rtnl_offload_xstats_get_size_ndo(const struct net_device *dev, int attr_id) 5285f6e0fb81SPetr Machata { 5286f6e0fb81SPetr Machata return rtnl_offload_xstats_have_ndo(dev, attr_id) ? 5287f6e0fb81SPetr Machata sizeof(struct rtnl_link_stats64) : 0; 5288f6e0fb81SPetr Machata } 5289f6e0fb81SPetr Machata 5290f6e0fb81SPetr Machata static int 5291f6e0fb81SPetr Machata rtnl_offload_xstats_fill_ndo(struct net_device *dev, int attr_id, 5292f6e0fb81SPetr Machata struct sk_buff *skb) 5293f6e0fb81SPetr Machata { 5294f6e0fb81SPetr Machata unsigned int size = rtnl_offload_xstats_get_size_ndo(dev, attr_id); 5295f6e0fb81SPetr Machata struct nlattr *attr = NULL; 5296f6e0fb81SPetr Machata void *attr_data; 5297f6e0fb81SPetr Machata int err; 5298f6e0fb81SPetr Machata 5299f6e0fb81SPetr Machata if (!size) 5300f6e0fb81SPetr Machata return -ENODATA; 5301f6e0fb81SPetr Machata 5302f6e0fb81SPetr Machata attr = nla_reserve_64bit(skb, attr_id, size, 5303f6e0fb81SPetr Machata IFLA_OFFLOAD_XSTATS_UNSPEC); 5304f6e0fb81SPetr Machata if (!attr) 5305f6e0fb81SPetr Machata return -EMSGSIZE; 5306f6e0fb81SPetr Machata 5307f6e0fb81SPetr Machata attr_data = nla_data(attr); 5308f6e0fb81SPetr Machata memset(attr_data, 0, size); 5309f6e0fb81SPetr Machata 5310f6e0fb81SPetr Machata err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev, attr_data); 5311f6e0fb81SPetr Machata if (err) 5312f6e0fb81SPetr Machata return err; 5313f6e0fb81SPetr Machata 531469ae6ad2SNogah Frankel return 0; 531569ae6ad2SNogah Frankel } 531669ae6ad2SNogah Frankel 53170e7788fdSPetr Machata static unsigned int 53180e7788fdSPetr Machata rtnl_offload_xstats_get_size_stats(const struct net_device *dev, 53190e7788fdSPetr Machata enum netdev_offload_xstats_type type) 53200e7788fdSPetr Machata { 53210e7788fdSPetr Machata bool enabled = netdev_offload_xstats_enabled(dev, type); 53220e7788fdSPetr Machata 53230e7788fdSPetr Machata return enabled ? sizeof(struct rtnl_hw_stats64) : 0; 53240e7788fdSPetr Machata } 53250e7788fdSPetr Machata 53260e7788fdSPetr Machata struct rtnl_offload_xstats_request_used { 53270e7788fdSPetr Machata bool request; 53280e7788fdSPetr Machata bool used; 53290e7788fdSPetr Machata }; 53300e7788fdSPetr Machata 53310e7788fdSPetr Machata static int 53320e7788fdSPetr Machata rtnl_offload_xstats_get_stats(struct net_device *dev, 53330e7788fdSPetr Machata enum netdev_offload_xstats_type type, 53340e7788fdSPetr Machata struct rtnl_offload_xstats_request_used *ru, 53350e7788fdSPetr Machata struct rtnl_hw_stats64 *stats, 53360e7788fdSPetr Machata struct netlink_ext_ack *extack) 53370e7788fdSPetr Machata { 53380e7788fdSPetr Machata bool request; 53390e7788fdSPetr Machata bool used; 53400e7788fdSPetr Machata int err; 53410e7788fdSPetr Machata 53420e7788fdSPetr Machata request = netdev_offload_xstats_enabled(dev, type); 53430e7788fdSPetr Machata if (!request) { 53440e7788fdSPetr Machata used = false; 53450e7788fdSPetr Machata goto out; 53460e7788fdSPetr Machata } 53470e7788fdSPetr Machata 53480e7788fdSPetr Machata err = netdev_offload_xstats_get(dev, type, stats, &used, extack); 53490e7788fdSPetr Machata if (err) 53500e7788fdSPetr Machata return err; 53510e7788fdSPetr Machata 53520e7788fdSPetr Machata out: 53530e7788fdSPetr Machata if (ru) { 53540e7788fdSPetr Machata ru->request = request; 53550e7788fdSPetr Machata ru->used = used; 53560e7788fdSPetr Machata } 53570e7788fdSPetr Machata return 0; 53580e7788fdSPetr Machata } 53590e7788fdSPetr Machata 53600e7788fdSPetr Machata static int 53610e7788fdSPetr Machata rtnl_offload_xstats_fill_hw_s_info_one(struct sk_buff *skb, int attr_id, 53620e7788fdSPetr Machata struct rtnl_offload_xstats_request_used *ru) 53630e7788fdSPetr Machata { 53640e7788fdSPetr Machata struct nlattr *nest; 53650e7788fdSPetr Machata 53660e7788fdSPetr Machata nest = nla_nest_start(skb, attr_id); 53670e7788fdSPetr Machata if (!nest) 53680e7788fdSPetr Machata return -EMSGSIZE; 53690e7788fdSPetr Machata 53700e7788fdSPetr Machata if (nla_put_u8(skb, IFLA_OFFLOAD_XSTATS_HW_S_INFO_REQUEST, ru->request)) 53710e7788fdSPetr Machata goto nla_put_failure; 53720e7788fdSPetr Machata 53730e7788fdSPetr Machata if (nla_put_u8(skb, IFLA_OFFLOAD_XSTATS_HW_S_INFO_USED, ru->used)) 53740e7788fdSPetr Machata goto nla_put_failure; 53750e7788fdSPetr Machata 53760e7788fdSPetr Machata nla_nest_end(skb, nest); 53770e7788fdSPetr Machata return 0; 53780e7788fdSPetr Machata 53790e7788fdSPetr Machata nla_put_failure: 53800e7788fdSPetr Machata nla_nest_cancel(skb, nest); 53810e7788fdSPetr Machata return -EMSGSIZE; 53820e7788fdSPetr Machata } 53830e7788fdSPetr Machata 53840e7788fdSPetr Machata static int 53850e7788fdSPetr Machata rtnl_offload_xstats_fill_hw_s_info(struct sk_buff *skb, struct net_device *dev, 53860e7788fdSPetr Machata struct netlink_ext_ack *extack) 53870e7788fdSPetr Machata { 53880e7788fdSPetr Machata enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3; 53890e7788fdSPetr Machata struct rtnl_offload_xstats_request_used ru_l3; 53900e7788fdSPetr Machata struct nlattr *nest; 53910e7788fdSPetr Machata int err; 53920e7788fdSPetr Machata 53930e7788fdSPetr Machata err = rtnl_offload_xstats_get_stats(dev, t_l3, &ru_l3, NULL, extack); 53940e7788fdSPetr Machata if (err) 53950e7788fdSPetr Machata return err; 53960e7788fdSPetr Machata 53970e7788fdSPetr Machata nest = nla_nest_start(skb, IFLA_OFFLOAD_XSTATS_HW_S_INFO); 53980e7788fdSPetr Machata if (!nest) 53990e7788fdSPetr Machata return -EMSGSIZE; 54000e7788fdSPetr Machata 54010e7788fdSPetr Machata if (rtnl_offload_xstats_fill_hw_s_info_one(skb, 54020e7788fdSPetr Machata IFLA_OFFLOAD_XSTATS_L3_STATS, 54030e7788fdSPetr Machata &ru_l3)) 54040e7788fdSPetr Machata goto nla_put_failure; 54050e7788fdSPetr Machata 54060e7788fdSPetr Machata nla_nest_end(skb, nest); 54070e7788fdSPetr Machata return 0; 54080e7788fdSPetr Machata 54090e7788fdSPetr Machata nla_put_failure: 54100e7788fdSPetr Machata nla_nest_cancel(skb, nest); 54110e7788fdSPetr Machata return -EMSGSIZE; 54120e7788fdSPetr Machata } 54130e7788fdSPetr Machata 54146b524a1dSPetr Machata static int rtnl_offload_xstats_fill(struct sk_buff *skb, struct net_device *dev, 541505415bccSPetr Machata int *prividx, u32 off_filter_mask, 541605415bccSPetr Machata struct netlink_ext_ack *extack) 541769ae6ad2SNogah Frankel { 54180e7788fdSPetr Machata enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3; 54190e7788fdSPetr Machata int attr_id_hw_s_info = IFLA_OFFLOAD_XSTATS_HW_S_INFO; 54200e7788fdSPetr Machata int attr_id_l3_stats = IFLA_OFFLOAD_XSTATS_L3_STATS; 5421f6e0fb81SPetr Machata int attr_id_cpu_hit = IFLA_OFFLOAD_XSTATS_CPU_HIT; 5422f6e0fb81SPetr Machata bool have_data = false; 542369ae6ad2SNogah Frankel int err; 542469ae6ad2SNogah Frankel 542546efc97bSPetr Machata if (*prividx <= attr_id_cpu_hit && 542646efc97bSPetr Machata (off_filter_mask & 542746efc97bSPetr Machata IFLA_STATS_FILTER_BIT(attr_id_cpu_hit))) { 5428f6e0fb81SPetr Machata err = rtnl_offload_xstats_fill_ndo(dev, attr_id_cpu_hit, skb); 5429f6e0fb81SPetr Machata if (!err) { 5430f6e0fb81SPetr Machata have_data = true; 5431f6e0fb81SPetr Machata } else if (err != -ENODATA) { 5432f6e0fb81SPetr Machata *prividx = attr_id_cpu_hit; 5433f6e0fb81SPetr Machata return err; 5434f6e0fb81SPetr Machata } 543569ae6ad2SNogah Frankel } 543669ae6ad2SNogah Frankel 54370e7788fdSPetr Machata if (*prividx <= attr_id_hw_s_info && 54380e7788fdSPetr Machata (off_filter_mask & IFLA_STATS_FILTER_BIT(attr_id_hw_s_info))) { 54390e7788fdSPetr Machata *prividx = attr_id_hw_s_info; 54400e7788fdSPetr Machata 54410e7788fdSPetr Machata err = rtnl_offload_xstats_fill_hw_s_info(skb, dev, extack); 54420e7788fdSPetr Machata if (err) 54430e7788fdSPetr Machata return err; 54440e7788fdSPetr Machata 54450e7788fdSPetr Machata have_data = true; 54460e7788fdSPetr Machata *prividx = 0; 54470e7788fdSPetr Machata } 54480e7788fdSPetr Machata 54490e7788fdSPetr Machata if (*prividx <= attr_id_l3_stats && 54500e7788fdSPetr Machata (off_filter_mask & IFLA_STATS_FILTER_BIT(attr_id_l3_stats))) { 54510e7788fdSPetr Machata unsigned int size_l3; 54520e7788fdSPetr Machata struct nlattr *attr; 54530e7788fdSPetr Machata 54540e7788fdSPetr Machata *prividx = attr_id_l3_stats; 54550e7788fdSPetr Machata 54560e7788fdSPetr Machata size_l3 = rtnl_offload_xstats_get_size_stats(dev, t_l3); 545723cfe941SPetr Machata if (!size_l3) 545823cfe941SPetr Machata goto skip_l3_stats; 54590e7788fdSPetr Machata attr = nla_reserve_64bit(skb, attr_id_l3_stats, size_l3, 54600e7788fdSPetr Machata IFLA_OFFLOAD_XSTATS_UNSPEC); 54610e7788fdSPetr Machata if (!attr) 54620e7788fdSPetr Machata return -EMSGSIZE; 54630e7788fdSPetr Machata 54640e7788fdSPetr Machata err = rtnl_offload_xstats_get_stats(dev, t_l3, NULL, 54650e7788fdSPetr Machata nla_data(attr), extack); 54660e7788fdSPetr Machata if (err) 54670e7788fdSPetr Machata return err; 54680e7788fdSPetr Machata 54690e7788fdSPetr Machata have_data = true; 547023cfe941SPetr Machata skip_l3_stats: 54710e7788fdSPetr Machata *prividx = 0; 54720e7788fdSPetr Machata } 54730e7788fdSPetr Machata 5474f6e0fb81SPetr Machata if (!have_data) 547569ae6ad2SNogah Frankel return -ENODATA; 547669ae6ad2SNogah Frankel 547769ae6ad2SNogah Frankel *prividx = 0; 547869ae6ad2SNogah Frankel return 0; 547969ae6ad2SNogah Frankel } 548069ae6ad2SNogah Frankel 54810e7788fdSPetr Machata static unsigned int 54820e7788fdSPetr Machata rtnl_offload_xstats_get_size_hw_s_info_one(const struct net_device *dev, 54830e7788fdSPetr Machata enum netdev_offload_xstats_type type) 54840e7788fdSPetr Machata { 54850e7788fdSPetr Machata bool enabled = netdev_offload_xstats_enabled(dev, type); 54860e7788fdSPetr Machata 54870e7788fdSPetr Machata return nla_total_size(0) + 54880e7788fdSPetr Machata /* IFLA_OFFLOAD_XSTATS_HW_S_INFO_REQUEST */ 54890e7788fdSPetr Machata nla_total_size(sizeof(u8)) + 54900e7788fdSPetr Machata /* IFLA_OFFLOAD_XSTATS_HW_S_INFO_USED */ 54910e7788fdSPetr Machata (enabled ? nla_total_size(sizeof(u8)) : 0) + 54920e7788fdSPetr Machata 0; 54930e7788fdSPetr Machata } 54940e7788fdSPetr Machata 54950e7788fdSPetr Machata static unsigned int 54960e7788fdSPetr Machata rtnl_offload_xstats_get_size_hw_s_info(const struct net_device *dev) 54970e7788fdSPetr Machata { 54980e7788fdSPetr Machata enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3; 54990e7788fdSPetr Machata 55000e7788fdSPetr Machata return nla_total_size(0) + 55010e7788fdSPetr Machata /* IFLA_OFFLOAD_XSTATS_L3_STATS */ 55020e7788fdSPetr Machata rtnl_offload_xstats_get_size_hw_s_info_one(dev, t_l3) + 55030e7788fdSPetr Machata 0; 55040e7788fdSPetr Machata } 55050e7788fdSPetr Machata 550646efc97bSPetr Machata static int rtnl_offload_xstats_get_size(const struct net_device *dev, 550746efc97bSPetr Machata u32 off_filter_mask) 550869ae6ad2SNogah Frankel { 55090e7788fdSPetr Machata enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3; 5510f6e0fb81SPetr Machata int attr_id_cpu_hit = IFLA_OFFLOAD_XSTATS_CPU_HIT; 551169ae6ad2SNogah Frankel int nla_size = 0; 551269ae6ad2SNogah Frankel int size; 551369ae6ad2SNogah Frankel 551446efc97bSPetr Machata if (off_filter_mask & 551546efc97bSPetr Machata IFLA_STATS_FILTER_BIT(attr_id_cpu_hit)) { 5516f6e0fb81SPetr Machata size = rtnl_offload_xstats_get_size_ndo(dev, attr_id_cpu_hit); 551769ae6ad2SNogah Frankel nla_size += nla_total_size_64bit(size); 551846efc97bSPetr Machata } 551969ae6ad2SNogah Frankel 55200e7788fdSPetr Machata if (off_filter_mask & 55210e7788fdSPetr Machata IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_HW_S_INFO)) 55220e7788fdSPetr Machata nla_size += rtnl_offload_xstats_get_size_hw_s_info(dev); 55230e7788fdSPetr Machata 55240e7788fdSPetr Machata if (off_filter_mask & 55250e7788fdSPetr Machata IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_L3_STATS)) { 55260e7788fdSPetr Machata size = rtnl_offload_xstats_get_size_stats(dev, t_l3); 55270e7788fdSPetr Machata nla_size += nla_total_size_64bit(size); 55280e7788fdSPetr Machata } 55290e7788fdSPetr Machata 553069ae6ad2SNogah Frankel if (nla_size != 0) 553169ae6ad2SNogah Frankel nla_size += nla_total_size(0); 553269ae6ad2SNogah Frankel 553369ae6ad2SNogah Frankel return nla_size; 553469ae6ad2SNogah Frankel } 553569ae6ad2SNogah Frankel 553646efc97bSPetr Machata struct rtnl_stats_dump_filters { 553746efc97bSPetr Machata /* mask[0] filters outer attributes. Then individual nests have their 553846efc97bSPetr Machata * filtering mask at the index of the nested attribute. 553946efc97bSPetr Machata */ 554046efc97bSPetr Machata u32 mask[IFLA_STATS_MAX + 1]; 554146efc97bSPetr Machata }; 554246efc97bSPetr Machata 554310c9ead9SRoopa Prabhu static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev, 554410c9ead9SRoopa Prabhu int type, u32 pid, u32 seq, u32 change, 554546efc97bSPetr Machata unsigned int flags, 554646efc97bSPetr Machata const struct rtnl_stats_dump_filters *filters, 554705415bccSPetr Machata int *idxattr, int *prividx, 554805415bccSPetr Machata struct netlink_ext_ack *extack) 554910c9ead9SRoopa Prabhu { 555046efc97bSPetr Machata unsigned int filter_mask = filters->mask[0]; 555110c9ead9SRoopa Prabhu struct if_stats_msg *ifsm; 555210c9ead9SRoopa Prabhu struct nlmsghdr *nlh; 555310c9ead9SRoopa Prabhu struct nlattr *attr; 5554e8872a25SNikolay Aleksandrov int s_prividx = *prividx; 555569ae6ad2SNogah Frankel int err; 555610c9ead9SRoopa Prabhu 555710c9ead9SRoopa Prabhu ASSERT_RTNL(); 555810c9ead9SRoopa Prabhu 555910c9ead9SRoopa Prabhu nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags); 556010c9ead9SRoopa Prabhu if (!nlh) 556110c9ead9SRoopa Prabhu return -EMSGSIZE; 556210c9ead9SRoopa Prabhu 556310c9ead9SRoopa Prabhu ifsm = nlmsg_data(nlh); 5564ce024f42SNikolay Aleksandrov ifsm->family = PF_UNSPEC; 5565ce024f42SNikolay Aleksandrov ifsm->pad1 = 0; 5566ce024f42SNikolay Aleksandrov ifsm->pad2 = 0; 556710c9ead9SRoopa Prabhu ifsm->ifindex = dev->ifindex; 556810c9ead9SRoopa Prabhu ifsm->filter_mask = filter_mask; 556910c9ead9SRoopa Prabhu 5570e8872a25SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) { 557110c9ead9SRoopa Prabhu struct rtnl_link_stats64 *sp; 557210c9ead9SRoopa Prabhu 557358414d32SNicolas Dichtel attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64, 557458414d32SNicolas Dichtel sizeof(struct rtnl_link_stats64), 557558414d32SNicolas Dichtel IFLA_STATS_UNSPEC); 5576216e6906SPetr Machata if (!attr) { 5577216e6906SPetr Machata err = -EMSGSIZE; 557810c9ead9SRoopa Prabhu goto nla_put_failure; 5579216e6906SPetr Machata } 558010c9ead9SRoopa Prabhu 558110c9ead9SRoopa Prabhu sp = nla_data(attr); 558210c9ead9SRoopa Prabhu dev_get_stats(dev, sp); 558310c9ead9SRoopa Prabhu } 558410c9ead9SRoopa Prabhu 558597a47facSNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) { 558697a47facSNikolay Aleksandrov const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 558797a47facSNikolay Aleksandrov 558897a47facSNikolay Aleksandrov if (ops && ops->fill_linkxstats) { 558997a47facSNikolay Aleksandrov *idxattr = IFLA_STATS_LINK_XSTATS; 5590ae0be8deSMichal Kubecek attr = nla_nest_start_noflag(skb, 559197a47facSNikolay Aleksandrov IFLA_STATS_LINK_XSTATS); 5592216e6906SPetr Machata if (!attr) { 5593216e6906SPetr Machata err = -EMSGSIZE; 559497a47facSNikolay Aleksandrov goto nla_put_failure; 5595216e6906SPetr Machata } 559697a47facSNikolay Aleksandrov 559780e73cc5SNikolay Aleksandrov err = ops->fill_linkxstats(skb, dev, prividx, *idxattr); 559880e73cc5SNikolay Aleksandrov nla_nest_end(skb, attr); 559980e73cc5SNikolay Aleksandrov if (err) 560080e73cc5SNikolay Aleksandrov goto nla_put_failure; 560180e73cc5SNikolay Aleksandrov *idxattr = 0; 560280e73cc5SNikolay Aleksandrov } 560380e73cc5SNikolay Aleksandrov } 560480e73cc5SNikolay Aleksandrov 560580e73cc5SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 560680e73cc5SNikolay Aleksandrov *idxattr)) { 560780e73cc5SNikolay Aleksandrov const struct rtnl_link_ops *ops = NULL; 560880e73cc5SNikolay Aleksandrov const struct net_device *master; 560980e73cc5SNikolay Aleksandrov 561080e73cc5SNikolay Aleksandrov master = netdev_master_upper_dev_get(dev); 561180e73cc5SNikolay Aleksandrov if (master) 561280e73cc5SNikolay Aleksandrov ops = master->rtnl_link_ops; 561380e73cc5SNikolay Aleksandrov if (ops && ops->fill_linkxstats) { 561480e73cc5SNikolay Aleksandrov *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE; 5615ae0be8deSMichal Kubecek attr = nla_nest_start_noflag(skb, 561680e73cc5SNikolay Aleksandrov IFLA_STATS_LINK_XSTATS_SLAVE); 5617216e6906SPetr Machata if (!attr) { 5618216e6906SPetr Machata err = -EMSGSIZE; 561980e73cc5SNikolay Aleksandrov goto nla_put_failure; 5620216e6906SPetr Machata } 562180e73cc5SNikolay Aleksandrov 562280e73cc5SNikolay Aleksandrov err = ops->fill_linkxstats(skb, dev, prividx, *idxattr); 562397a47facSNikolay Aleksandrov nla_nest_end(skb, attr); 562497a47facSNikolay Aleksandrov if (err) 562597a47facSNikolay Aleksandrov goto nla_put_failure; 562697a47facSNikolay Aleksandrov *idxattr = 0; 562797a47facSNikolay Aleksandrov } 562897a47facSNikolay Aleksandrov } 562997a47facSNikolay Aleksandrov 563069ae6ad2SNogah Frankel if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 563169ae6ad2SNogah Frankel *idxattr)) { 563246efc97bSPetr Machata u32 off_filter_mask; 563346efc97bSPetr Machata 563446efc97bSPetr Machata off_filter_mask = filters->mask[IFLA_STATS_LINK_OFFLOAD_XSTATS]; 563569ae6ad2SNogah Frankel *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS; 5636ae0be8deSMichal Kubecek attr = nla_nest_start_noflag(skb, 5637ae0be8deSMichal Kubecek IFLA_STATS_LINK_OFFLOAD_XSTATS); 5638216e6906SPetr Machata if (!attr) { 5639216e6906SPetr Machata err = -EMSGSIZE; 564069ae6ad2SNogah Frankel goto nla_put_failure; 5641216e6906SPetr Machata } 564269ae6ad2SNogah Frankel 564346efc97bSPetr Machata err = rtnl_offload_xstats_fill(skb, dev, prividx, 564405415bccSPetr Machata off_filter_mask, extack); 564569ae6ad2SNogah Frankel if (err == -ENODATA) 564669ae6ad2SNogah Frankel nla_nest_cancel(skb, attr); 564769ae6ad2SNogah Frankel else 564869ae6ad2SNogah Frankel nla_nest_end(skb, attr); 564969ae6ad2SNogah Frankel 565069ae6ad2SNogah Frankel if (err && err != -ENODATA) 565169ae6ad2SNogah Frankel goto nla_put_failure; 565269ae6ad2SNogah Frankel *idxattr = 0; 565369ae6ad2SNogah Frankel } 565469ae6ad2SNogah Frankel 5655aefb4d4aSRobert Shearman if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) { 5656aefb4d4aSRobert Shearman struct rtnl_af_ops *af_ops; 5657aefb4d4aSRobert Shearman 5658aefb4d4aSRobert Shearman *idxattr = IFLA_STATS_AF_SPEC; 5659ae0be8deSMichal Kubecek attr = nla_nest_start_noflag(skb, IFLA_STATS_AF_SPEC); 5660216e6906SPetr Machata if (!attr) { 5661216e6906SPetr Machata err = -EMSGSIZE; 5662aefb4d4aSRobert Shearman goto nla_put_failure; 5663216e6906SPetr Machata } 5664aefb4d4aSRobert Shearman 56655fa85a09SFlorian Westphal rcu_read_lock(); 56665fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 5667aefb4d4aSRobert Shearman if (af_ops->fill_stats_af) { 5668aefb4d4aSRobert Shearman struct nlattr *af; 5669aefb4d4aSRobert Shearman 5670ae0be8deSMichal Kubecek af = nla_nest_start_noflag(skb, 5671ae0be8deSMichal Kubecek af_ops->family); 56725fa85a09SFlorian Westphal if (!af) { 56735fa85a09SFlorian Westphal rcu_read_unlock(); 567457d29a29STom Rix err = -EMSGSIZE; 5675aefb4d4aSRobert Shearman goto nla_put_failure; 56765fa85a09SFlorian Westphal } 5677aefb4d4aSRobert Shearman err = af_ops->fill_stats_af(skb, dev); 5678aefb4d4aSRobert Shearman 56795fa85a09SFlorian Westphal if (err == -ENODATA) { 5680aefb4d4aSRobert Shearman nla_nest_cancel(skb, af); 56815fa85a09SFlorian Westphal } else if (err < 0) { 56825fa85a09SFlorian Westphal rcu_read_unlock(); 5683aefb4d4aSRobert Shearman goto nla_put_failure; 56845fa85a09SFlorian Westphal } 5685aefb4d4aSRobert Shearman 5686aefb4d4aSRobert Shearman nla_nest_end(skb, af); 5687aefb4d4aSRobert Shearman } 5688aefb4d4aSRobert Shearman } 56895fa85a09SFlorian Westphal rcu_read_unlock(); 5690aefb4d4aSRobert Shearman 5691aefb4d4aSRobert Shearman nla_nest_end(skb, attr); 5692aefb4d4aSRobert Shearman 5693aefb4d4aSRobert Shearman *idxattr = 0; 5694aefb4d4aSRobert Shearman } 5695aefb4d4aSRobert Shearman 569610c9ead9SRoopa Prabhu nlmsg_end(skb, nlh); 569710c9ead9SRoopa Prabhu 569810c9ead9SRoopa Prabhu return 0; 569910c9ead9SRoopa Prabhu 570010c9ead9SRoopa Prabhu nla_put_failure: 5701e8872a25SNikolay Aleksandrov /* not a multi message or no progress mean a real error */ 5702e8872a25SNikolay Aleksandrov if (!(flags & NLM_F_MULTI) || s_prividx == *prividx) 570310c9ead9SRoopa Prabhu nlmsg_cancel(skb, nlh); 5704e8872a25SNikolay Aleksandrov else 5705e8872a25SNikolay Aleksandrov nlmsg_end(skb, nlh); 570610c9ead9SRoopa Prabhu 5707216e6906SPetr Machata return err; 570810c9ead9SRoopa Prabhu } 570910c9ead9SRoopa Prabhu 571010c9ead9SRoopa Prabhu static size_t if_nlmsg_stats_size(const struct net_device *dev, 571146efc97bSPetr Machata const struct rtnl_stats_dump_filters *filters) 571210c9ead9SRoopa Prabhu { 5713d3436799SEric Dumazet size_t size = NLMSG_ALIGN(sizeof(struct if_stats_msg)); 571446efc97bSPetr Machata unsigned int filter_mask = filters->mask[0]; 571510c9ead9SRoopa Prabhu 5716e8872a25SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0)) 571710c9ead9SRoopa Prabhu size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64)); 571810c9ead9SRoopa Prabhu 571997a47facSNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) { 572097a47facSNikolay Aleksandrov const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 572180e73cc5SNikolay Aleksandrov int attr = IFLA_STATS_LINK_XSTATS; 572297a47facSNikolay Aleksandrov 572397a47facSNikolay Aleksandrov if (ops && ops->get_linkxstats_size) { 572480e73cc5SNikolay Aleksandrov size += nla_total_size(ops->get_linkxstats_size(dev, 572580e73cc5SNikolay Aleksandrov attr)); 572697a47facSNikolay Aleksandrov /* for IFLA_STATS_LINK_XSTATS */ 572797a47facSNikolay Aleksandrov size += nla_total_size(0); 572897a47facSNikolay Aleksandrov } 572997a47facSNikolay Aleksandrov } 573097a47facSNikolay Aleksandrov 573180e73cc5SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) { 573280e73cc5SNikolay Aleksandrov struct net_device *_dev = (struct net_device *)dev; 573380e73cc5SNikolay Aleksandrov const struct rtnl_link_ops *ops = NULL; 573480e73cc5SNikolay Aleksandrov const struct net_device *master; 573580e73cc5SNikolay Aleksandrov 573680e73cc5SNikolay Aleksandrov /* netdev_master_upper_dev_get can't take const */ 573780e73cc5SNikolay Aleksandrov master = netdev_master_upper_dev_get(_dev); 573880e73cc5SNikolay Aleksandrov if (master) 573980e73cc5SNikolay Aleksandrov ops = master->rtnl_link_ops; 574080e73cc5SNikolay Aleksandrov if (ops && ops->get_linkxstats_size) { 574180e73cc5SNikolay Aleksandrov int attr = IFLA_STATS_LINK_XSTATS_SLAVE; 574280e73cc5SNikolay Aleksandrov 574380e73cc5SNikolay Aleksandrov size += nla_total_size(ops->get_linkxstats_size(dev, 574480e73cc5SNikolay Aleksandrov attr)); 574580e73cc5SNikolay Aleksandrov /* for IFLA_STATS_LINK_XSTATS_SLAVE */ 574680e73cc5SNikolay Aleksandrov size += nla_total_size(0); 574780e73cc5SNikolay Aleksandrov } 574880e73cc5SNikolay Aleksandrov } 574980e73cc5SNikolay Aleksandrov 575046efc97bSPetr Machata if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0)) { 575146efc97bSPetr Machata u32 off_filter_mask; 575246efc97bSPetr Machata 575346efc97bSPetr Machata off_filter_mask = filters->mask[IFLA_STATS_LINK_OFFLOAD_XSTATS]; 575446efc97bSPetr Machata size += rtnl_offload_xstats_get_size(dev, off_filter_mask); 575546efc97bSPetr Machata } 575669ae6ad2SNogah Frankel 5757aefb4d4aSRobert Shearman if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) { 5758aefb4d4aSRobert Shearman struct rtnl_af_ops *af_ops; 5759aefb4d4aSRobert Shearman 5760aefb4d4aSRobert Shearman /* for IFLA_STATS_AF_SPEC */ 5761aefb4d4aSRobert Shearman size += nla_total_size(0); 5762aefb4d4aSRobert Shearman 57635fa85a09SFlorian Westphal rcu_read_lock(); 57645fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 5765aefb4d4aSRobert Shearman if (af_ops->get_stats_af_size) { 5766aefb4d4aSRobert Shearman size += nla_total_size( 5767aefb4d4aSRobert Shearman af_ops->get_stats_af_size(dev)); 5768aefb4d4aSRobert Shearman 5769aefb4d4aSRobert Shearman /* for AF_* */ 5770aefb4d4aSRobert Shearman size += nla_total_size(0); 5771aefb4d4aSRobert Shearman } 5772aefb4d4aSRobert Shearman } 57735fa85a09SFlorian Westphal rcu_read_unlock(); 5774aefb4d4aSRobert Shearman } 5775aefb4d4aSRobert Shearman 577610c9ead9SRoopa Prabhu return size; 577710c9ead9SRoopa Prabhu } 577810c9ead9SRoopa Prabhu 577946efc97bSPetr Machata #define RTNL_STATS_OFFLOAD_XSTATS_VALID ((1 << __IFLA_OFFLOAD_XSTATS_MAX) - 1) 578046efc97bSPetr Machata 578146efc97bSPetr Machata static const struct nla_policy 578246efc97bSPetr Machata rtnl_stats_get_policy_filters[IFLA_STATS_MAX + 1] = { 578346efc97bSPetr Machata [IFLA_STATS_LINK_OFFLOAD_XSTATS] = 578446efc97bSPetr Machata NLA_POLICY_MASK(NLA_U32, RTNL_STATS_OFFLOAD_XSTATS_VALID), 578546efc97bSPetr Machata }; 578646efc97bSPetr Machata 578746efc97bSPetr Machata static const struct nla_policy 578846efc97bSPetr Machata rtnl_stats_get_policy[IFLA_STATS_GETSET_MAX + 1] = { 578946efc97bSPetr Machata [IFLA_STATS_GET_FILTERS] = 579046efc97bSPetr Machata NLA_POLICY_NESTED(rtnl_stats_get_policy_filters), 579146efc97bSPetr Machata }; 579246efc97bSPetr Machata 579303ba3566SPetr Machata static const struct nla_policy 579403ba3566SPetr Machata ifla_stats_set_policy[IFLA_STATS_GETSET_MAX + 1] = { 57955fd0b838SPetr Machata [IFLA_STATS_SET_OFFLOAD_XSTATS_L3_STATS] = NLA_POLICY_MAX(NLA_U8, 1), 579603ba3566SPetr Machata }; 579703ba3566SPetr Machata 579846efc97bSPetr Machata static int rtnl_stats_get_parse_filters(struct nlattr *ifla_filters, 579946efc97bSPetr Machata struct rtnl_stats_dump_filters *filters, 580046efc97bSPetr Machata struct netlink_ext_ack *extack) 580146efc97bSPetr Machata { 580246efc97bSPetr Machata struct nlattr *tb[IFLA_STATS_MAX + 1]; 580346efc97bSPetr Machata int err; 580446efc97bSPetr Machata int at; 580546efc97bSPetr Machata 580646efc97bSPetr Machata err = nla_parse_nested(tb, IFLA_STATS_MAX, ifla_filters, 580746efc97bSPetr Machata rtnl_stats_get_policy_filters, extack); 580846efc97bSPetr Machata if (err < 0) 580946efc97bSPetr Machata return err; 581046efc97bSPetr Machata 581146efc97bSPetr Machata for (at = 1; at <= IFLA_STATS_MAX; at++) { 581246efc97bSPetr Machata if (tb[at]) { 581346efc97bSPetr Machata if (!(filters->mask[0] & IFLA_STATS_FILTER_BIT(at))) { 581446efc97bSPetr Machata NL_SET_ERR_MSG(extack, "Filtered attribute not enabled in filter_mask"); 581546efc97bSPetr Machata return -EINVAL; 581646efc97bSPetr Machata } 581746efc97bSPetr Machata filters->mask[at] = nla_get_u32(tb[at]); 581846efc97bSPetr Machata } 581946efc97bSPetr Machata } 582046efc97bSPetr Machata 582146efc97bSPetr Machata return 0; 582246efc97bSPetr Machata } 582346efc97bSPetr Machata 582446efc97bSPetr Machata static int rtnl_stats_get_parse(const struct nlmsghdr *nlh, 582546efc97bSPetr Machata u32 filter_mask, 582646efc97bSPetr Machata struct rtnl_stats_dump_filters *filters, 582746efc97bSPetr Machata struct netlink_ext_ack *extack) 582846efc97bSPetr Machata { 582946efc97bSPetr Machata struct nlattr *tb[IFLA_STATS_GETSET_MAX + 1]; 583046efc97bSPetr Machata int err; 583146efc97bSPetr Machata int i; 583246efc97bSPetr Machata 583346efc97bSPetr Machata filters->mask[0] = filter_mask; 583446efc97bSPetr Machata for (i = 1; i < ARRAY_SIZE(filters->mask); i++) 583546efc97bSPetr Machata filters->mask[i] = -1U; 583646efc97bSPetr Machata 583746efc97bSPetr Machata err = nlmsg_parse(nlh, sizeof(struct if_stats_msg), tb, 583846efc97bSPetr Machata IFLA_STATS_GETSET_MAX, rtnl_stats_get_policy, extack); 583946efc97bSPetr Machata if (err < 0) 584046efc97bSPetr Machata return err; 584146efc97bSPetr Machata 584246efc97bSPetr Machata if (tb[IFLA_STATS_GET_FILTERS]) { 584346efc97bSPetr Machata err = rtnl_stats_get_parse_filters(tb[IFLA_STATS_GET_FILTERS], 584446efc97bSPetr Machata filters, extack); 584546efc97bSPetr Machata if (err) 584646efc97bSPetr Machata return err; 584746efc97bSPetr Machata } 584846efc97bSPetr Machata 584946efc97bSPetr Machata return 0; 585046efc97bSPetr Machata } 585146efc97bSPetr Machata 585251bc860dSJakub Kicinski static int rtnl_valid_stats_req(const struct nlmsghdr *nlh, bool strict_check, 585351bc860dSJakub Kicinski bool is_dump, struct netlink_ext_ack *extack) 585451bc860dSJakub Kicinski { 585551bc860dSJakub Kicinski struct if_stats_msg *ifsm; 585651bc860dSJakub Kicinski 585769f23a09SEric Dumazet if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifsm))) { 585851bc860dSJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid header for stats dump"); 585951bc860dSJakub Kicinski return -EINVAL; 586051bc860dSJakub Kicinski } 586151bc860dSJakub Kicinski 586251bc860dSJakub Kicinski if (!strict_check) 586351bc860dSJakub Kicinski return 0; 586451bc860dSJakub Kicinski 586551bc860dSJakub Kicinski ifsm = nlmsg_data(nlh); 586651bc860dSJakub Kicinski 586751bc860dSJakub Kicinski /* only requests using strict checks can pass data to influence 586851bc860dSJakub Kicinski * the dump. The legacy exception is filter_mask. 586951bc860dSJakub Kicinski */ 587051bc860dSJakub Kicinski if (ifsm->pad1 || ifsm->pad2 || (is_dump && ifsm->ifindex)) { 587151bc860dSJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid values in header for stats dump request"); 587251bc860dSJakub Kicinski return -EINVAL; 587351bc860dSJakub Kicinski } 58746300acb2SJakub Kicinski if (ifsm->filter_mask >= IFLA_STATS_FILTER_BIT(IFLA_STATS_MAX + 1)) { 58756300acb2SJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid stats requested through filter mask"); 58766300acb2SJakub Kicinski return -EINVAL; 58776300acb2SJakub Kicinski } 587851bc860dSJakub Kicinski 587951bc860dSJakub Kicinski return 0; 588051bc860dSJakub Kicinski } 588151bc860dSJakub Kicinski 5882c21ef3e3SDavid Ahern static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh, 5883c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 588410c9ead9SRoopa Prabhu { 588546efc97bSPetr Machata struct rtnl_stats_dump_filters filters; 588610c9ead9SRoopa Prabhu struct net *net = sock_net(skb->sk); 588710c9ead9SRoopa Prabhu struct net_device *dev = NULL; 5888e8872a25SNikolay Aleksandrov int idxattr = 0, prividx = 0; 5889e8872a25SNikolay Aleksandrov struct if_stats_msg *ifsm; 589010c9ead9SRoopa Prabhu struct sk_buff *nskb; 589110c9ead9SRoopa Prabhu int err; 589210c9ead9SRoopa Prabhu 589351bc860dSJakub Kicinski err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb), 589451bc860dSJakub Kicinski false, extack); 589551bc860dSJakub Kicinski if (err) 589651bc860dSJakub Kicinski return err; 58974775cc1fSMathias Krause 589810c9ead9SRoopa Prabhu ifsm = nlmsg_data(nlh); 589910c9ead9SRoopa Prabhu if (ifsm->ifindex > 0) 590010c9ead9SRoopa Prabhu dev = __dev_get_by_index(net, ifsm->ifindex); 590110c9ead9SRoopa Prabhu else 590210c9ead9SRoopa Prabhu return -EINVAL; 590310c9ead9SRoopa Prabhu 590410c9ead9SRoopa Prabhu if (!dev) 590510c9ead9SRoopa Prabhu return -ENODEV; 590610c9ead9SRoopa Prabhu 590746efc97bSPetr Machata if (!ifsm->filter_mask) { 590822b67d17SPetr Machata NL_SET_ERR_MSG(extack, "Filter mask must be set for stats get"); 590910c9ead9SRoopa Prabhu return -EINVAL; 591022b67d17SPetr Machata } 591110c9ead9SRoopa Prabhu 591246efc97bSPetr Machata err = rtnl_stats_get_parse(nlh, ifsm->filter_mask, &filters, extack); 591346efc97bSPetr Machata if (err) 591446efc97bSPetr Machata return err; 591546efc97bSPetr Machata 591646efc97bSPetr Machata nskb = nlmsg_new(if_nlmsg_stats_size(dev, &filters), GFP_KERNEL); 591710c9ead9SRoopa Prabhu if (!nskb) 591810c9ead9SRoopa Prabhu return -ENOBUFS; 591910c9ead9SRoopa Prabhu 592010c9ead9SRoopa Prabhu err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS, 592110c9ead9SRoopa Prabhu NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0, 592205415bccSPetr Machata 0, &filters, &idxattr, &prividx, extack); 592310c9ead9SRoopa Prabhu if (err < 0) { 592410c9ead9SRoopa Prabhu /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */ 592510c9ead9SRoopa Prabhu WARN_ON(err == -EMSGSIZE); 592610c9ead9SRoopa Prabhu kfree_skb(nskb); 592710c9ead9SRoopa Prabhu } else { 592810c9ead9SRoopa Prabhu err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); 592910c9ead9SRoopa Prabhu } 593010c9ead9SRoopa Prabhu 593110c9ead9SRoopa Prabhu return err; 593210c9ead9SRoopa Prabhu } 593310c9ead9SRoopa Prabhu 593410c9ead9SRoopa Prabhu static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb) 593510c9ead9SRoopa Prabhu { 5936841891ecSDavid Ahern struct netlink_ext_ack *extack = cb->extack; 5937e8872a25SNikolay Aleksandrov int h, s_h, err, s_idx, s_idxattr, s_prividx; 593846efc97bSPetr Machata struct rtnl_stats_dump_filters filters; 593910c9ead9SRoopa Prabhu struct net *net = sock_net(skb->sk); 594010c9ead9SRoopa Prabhu unsigned int flags = NLM_F_MULTI; 5941e8872a25SNikolay Aleksandrov struct if_stats_msg *ifsm; 5942e8872a25SNikolay Aleksandrov struct hlist_head *head; 5943e8872a25SNikolay Aleksandrov struct net_device *dev; 5944e8872a25SNikolay Aleksandrov int idx = 0; 594510c9ead9SRoopa Prabhu 594610c9ead9SRoopa Prabhu s_h = cb->args[0]; 594710c9ead9SRoopa Prabhu s_idx = cb->args[1]; 5948e8872a25SNikolay Aleksandrov s_idxattr = cb->args[2]; 5949e8872a25SNikolay Aleksandrov s_prividx = cb->args[3]; 595010c9ead9SRoopa Prabhu 595110c9ead9SRoopa Prabhu cb->seq = net->dev_base_seq; 595210c9ead9SRoopa Prabhu 595351bc860dSJakub Kicinski err = rtnl_valid_stats_req(cb->nlh, cb->strict_check, true, extack); 595451bc860dSJakub Kicinski if (err) 595551bc860dSJakub Kicinski return err; 59564775cc1fSMathias Krause 595710c9ead9SRoopa Prabhu ifsm = nlmsg_data(cb->nlh); 595846efc97bSPetr Machata if (!ifsm->filter_mask) { 5959841891ecSDavid Ahern NL_SET_ERR_MSG(extack, "Filter mask must be set for stats dump"); 5960841891ecSDavid Ahern return -EINVAL; 5961841891ecSDavid Ahern } 596210c9ead9SRoopa Prabhu 596346efc97bSPetr Machata err = rtnl_stats_get_parse(cb->nlh, ifsm->filter_mask, &filters, 596446efc97bSPetr Machata extack); 596546efc97bSPetr Machata if (err) 596646efc97bSPetr Machata return err; 596746efc97bSPetr Machata 596810c9ead9SRoopa Prabhu for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 596910c9ead9SRoopa Prabhu idx = 0; 597010c9ead9SRoopa Prabhu head = &net->dev_index_head[h]; 597110c9ead9SRoopa Prabhu hlist_for_each_entry(dev, head, index_hlist) { 597210c9ead9SRoopa Prabhu if (idx < s_idx) 597310c9ead9SRoopa Prabhu goto cont; 597410c9ead9SRoopa Prabhu err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS, 597510c9ead9SRoopa Prabhu NETLINK_CB(cb->skb).portid, 597610c9ead9SRoopa Prabhu cb->nlh->nlmsg_seq, 0, 597746efc97bSPetr Machata flags, &filters, 597805415bccSPetr Machata &s_idxattr, &s_prividx, 597905415bccSPetr Machata extack); 598010c9ead9SRoopa Prabhu /* If we ran out of room on the first message, 598110c9ead9SRoopa Prabhu * we're in trouble 598210c9ead9SRoopa Prabhu */ 598310c9ead9SRoopa Prabhu WARN_ON((err == -EMSGSIZE) && (skb->len == 0)); 598410c9ead9SRoopa Prabhu 598510c9ead9SRoopa Prabhu if (err < 0) 598610c9ead9SRoopa Prabhu goto out; 5987e8872a25SNikolay Aleksandrov s_prividx = 0; 5988e8872a25SNikolay Aleksandrov s_idxattr = 0; 598910c9ead9SRoopa Prabhu nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 599010c9ead9SRoopa Prabhu cont: 599110c9ead9SRoopa Prabhu idx++; 599210c9ead9SRoopa Prabhu } 599310c9ead9SRoopa Prabhu } 599410c9ead9SRoopa Prabhu out: 5995e8872a25SNikolay Aleksandrov cb->args[3] = s_prividx; 5996e8872a25SNikolay Aleksandrov cb->args[2] = s_idxattr; 599710c9ead9SRoopa Prabhu cb->args[1] = idx; 599810c9ead9SRoopa Prabhu cb->args[0] = h; 599910c9ead9SRoopa Prabhu 600010c9ead9SRoopa Prabhu return skb->len; 600110c9ead9SRoopa Prabhu } 600210c9ead9SRoopa Prabhu 60035fd0b838SPetr Machata void rtnl_offload_xstats_notify(struct net_device *dev) 60045fd0b838SPetr Machata { 60055fd0b838SPetr Machata struct rtnl_stats_dump_filters response_filters = {}; 60065fd0b838SPetr Machata struct net *net = dev_net(dev); 60075fd0b838SPetr Machata int idxattr = 0, prividx = 0; 60085fd0b838SPetr Machata struct sk_buff *skb; 60095fd0b838SPetr Machata int err = -ENOBUFS; 60105fd0b838SPetr Machata 60115fd0b838SPetr Machata ASSERT_RTNL(); 60125fd0b838SPetr Machata 60135fd0b838SPetr Machata response_filters.mask[0] |= 60145fd0b838SPetr Machata IFLA_STATS_FILTER_BIT(IFLA_STATS_LINK_OFFLOAD_XSTATS); 60155fd0b838SPetr Machata response_filters.mask[IFLA_STATS_LINK_OFFLOAD_XSTATS] |= 60165fd0b838SPetr Machata IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_HW_S_INFO); 60175fd0b838SPetr Machata 60185fd0b838SPetr Machata skb = nlmsg_new(if_nlmsg_stats_size(dev, &response_filters), 60195fd0b838SPetr Machata GFP_KERNEL); 60205fd0b838SPetr Machata if (!skb) 60215fd0b838SPetr Machata goto errout; 60225fd0b838SPetr Machata 60235fd0b838SPetr Machata err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS, 0, 0, 0, 0, 60245fd0b838SPetr Machata &response_filters, &idxattr, &prividx, NULL); 60255fd0b838SPetr Machata if (err < 0) { 60265fd0b838SPetr Machata kfree_skb(skb); 60275fd0b838SPetr Machata goto errout; 60285fd0b838SPetr Machata } 60295fd0b838SPetr Machata 60305fd0b838SPetr Machata rtnl_notify(skb, net, 0, RTNLGRP_STATS, NULL, GFP_KERNEL); 60315fd0b838SPetr Machata return; 60325fd0b838SPetr Machata 60335fd0b838SPetr Machata errout: 60345fd0b838SPetr Machata rtnl_set_sk_err(net, RTNLGRP_STATS, err); 60355fd0b838SPetr Machata } 60365fd0b838SPetr Machata EXPORT_SYMBOL(rtnl_offload_xstats_notify); 60375fd0b838SPetr Machata 603803ba3566SPetr Machata static int rtnl_stats_set(struct sk_buff *skb, struct nlmsghdr *nlh, 603903ba3566SPetr Machata struct netlink_ext_ack *extack) 604003ba3566SPetr Machata { 60415fd0b838SPetr Machata enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3; 604203ba3566SPetr Machata struct rtnl_stats_dump_filters response_filters = {}; 604303ba3566SPetr Machata struct nlattr *tb[IFLA_STATS_GETSET_MAX + 1]; 604403ba3566SPetr Machata struct net *net = sock_net(skb->sk); 604503ba3566SPetr Machata struct net_device *dev = NULL; 604603ba3566SPetr Machata struct if_stats_msg *ifsm; 60475fd0b838SPetr Machata bool notify = false; 604803ba3566SPetr Machata int err; 604903ba3566SPetr Machata 605003ba3566SPetr Machata err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb), 605103ba3566SPetr Machata false, extack); 605203ba3566SPetr Machata if (err) 605303ba3566SPetr Machata return err; 605403ba3566SPetr Machata 605503ba3566SPetr Machata ifsm = nlmsg_data(nlh); 605603ba3566SPetr Machata if (ifsm->family != AF_UNSPEC) { 605703ba3566SPetr Machata NL_SET_ERR_MSG(extack, "Address family should be AF_UNSPEC"); 605803ba3566SPetr Machata return -EINVAL; 605903ba3566SPetr Machata } 606003ba3566SPetr Machata 606103ba3566SPetr Machata if (ifsm->ifindex > 0) 606203ba3566SPetr Machata dev = __dev_get_by_index(net, ifsm->ifindex); 606303ba3566SPetr Machata else 606403ba3566SPetr Machata return -EINVAL; 606503ba3566SPetr Machata 606603ba3566SPetr Machata if (!dev) 606703ba3566SPetr Machata return -ENODEV; 606803ba3566SPetr Machata 606903ba3566SPetr Machata if (ifsm->filter_mask) { 607003ba3566SPetr Machata NL_SET_ERR_MSG(extack, "Filter mask must be 0 for stats set"); 607103ba3566SPetr Machata return -EINVAL; 607203ba3566SPetr Machata } 607303ba3566SPetr Machata 607403ba3566SPetr Machata err = nlmsg_parse(nlh, sizeof(*ifsm), tb, IFLA_STATS_GETSET_MAX, 607503ba3566SPetr Machata ifla_stats_set_policy, extack); 607603ba3566SPetr Machata if (err < 0) 607703ba3566SPetr Machata return err; 607803ba3566SPetr Machata 60795fd0b838SPetr Machata if (tb[IFLA_STATS_SET_OFFLOAD_XSTATS_L3_STATS]) { 60805fd0b838SPetr Machata u8 req = nla_get_u8(tb[IFLA_STATS_SET_OFFLOAD_XSTATS_L3_STATS]); 608103ba3566SPetr Machata 60825fd0b838SPetr Machata if (req) 60835fd0b838SPetr Machata err = netdev_offload_xstats_enable(dev, t_l3, extack); 60845fd0b838SPetr Machata else 60855fd0b838SPetr Machata err = netdev_offload_xstats_disable(dev, t_l3); 60865fd0b838SPetr Machata 60875fd0b838SPetr Machata if (!err) 60885fd0b838SPetr Machata notify = true; 60895fd0b838SPetr Machata else if (err != -EALREADY) 60905fd0b838SPetr Machata return err; 60915fd0b838SPetr Machata 60925fd0b838SPetr Machata response_filters.mask[0] |= 60935fd0b838SPetr Machata IFLA_STATS_FILTER_BIT(IFLA_STATS_LINK_OFFLOAD_XSTATS); 60945fd0b838SPetr Machata response_filters.mask[IFLA_STATS_LINK_OFFLOAD_XSTATS] |= 60955fd0b838SPetr Machata IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_HW_S_INFO); 609603ba3566SPetr Machata } 609703ba3566SPetr Machata 60985fd0b838SPetr Machata if (notify) 60995fd0b838SPetr Machata rtnl_offload_xstats_notify(dev); 61005fd0b838SPetr Machata 61015fd0b838SPetr Machata return 0; 610203ba3566SPetr Machata } 610303ba3566SPetr Machata 6104cc7f5022SIdo Schimmel static int rtnl_mdb_valid_dump_req(const struct nlmsghdr *nlh, 6105cc7f5022SIdo Schimmel struct netlink_ext_ack *extack) 6106cc7f5022SIdo Schimmel { 6107cc7f5022SIdo Schimmel struct br_port_msg *bpm; 6108cc7f5022SIdo Schimmel 6109cc7f5022SIdo Schimmel if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*bpm))) { 6110cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Invalid header for mdb dump request"); 6111cc7f5022SIdo Schimmel return -EINVAL; 6112cc7f5022SIdo Schimmel } 6113cc7f5022SIdo Schimmel 6114cc7f5022SIdo Schimmel bpm = nlmsg_data(nlh); 6115cc7f5022SIdo Schimmel if (bpm->ifindex) { 6116cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Filtering by device index is not supported for mdb dump request"); 6117cc7f5022SIdo Schimmel return -EINVAL; 6118cc7f5022SIdo Schimmel } 6119cc7f5022SIdo Schimmel if (nlmsg_attrlen(nlh, sizeof(*bpm))) { 6120cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Invalid data after header in mdb dump request"); 6121cc7f5022SIdo Schimmel return -EINVAL; 6122cc7f5022SIdo Schimmel } 6123cc7f5022SIdo Schimmel 6124cc7f5022SIdo Schimmel return 0; 6125cc7f5022SIdo Schimmel } 6126cc7f5022SIdo Schimmel 6127cc7f5022SIdo Schimmel struct rtnl_mdb_dump_ctx { 6128cc7f5022SIdo Schimmel long idx; 6129cc7f5022SIdo Schimmel }; 6130cc7f5022SIdo Schimmel 6131cc7f5022SIdo Schimmel static int rtnl_mdb_dump(struct sk_buff *skb, struct netlink_callback *cb) 6132cc7f5022SIdo Schimmel { 6133cc7f5022SIdo Schimmel struct rtnl_mdb_dump_ctx *ctx = (void *)cb->ctx; 6134cc7f5022SIdo Schimmel struct net *net = sock_net(skb->sk); 6135cc7f5022SIdo Schimmel struct net_device *dev; 6136cc7f5022SIdo Schimmel int idx, s_idx; 6137cc7f5022SIdo Schimmel int err; 6138cc7f5022SIdo Schimmel 6139cc7f5022SIdo Schimmel NL_ASSERT_DUMP_CTX_FITS(struct rtnl_mdb_dump_ctx); 6140cc7f5022SIdo Schimmel 6141cc7f5022SIdo Schimmel if (cb->strict_check) { 6142cc7f5022SIdo Schimmel err = rtnl_mdb_valid_dump_req(cb->nlh, cb->extack); 6143cc7f5022SIdo Schimmel if (err) 6144cc7f5022SIdo Schimmel return err; 6145cc7f5022SIdo Schimmel } 6146cc7f5022SIdo Schimmel 6147cc7f5022SIdo Schimmel s_idx = ctx->idx; 6148cc7f5022SIdo Schimmel idx = 0; 6149cc7f5022SIdo Schimmel 6150cc7f5022SIdo Schimmel for_each_netdev(net, dev) { 6151cc7f5022SIdo Schimmel if (idx < s_idx) 6152cc7f5022SIdo Schimmel goto skip; 6153cc7f5022SIdo Schimmel if (!dev->netdev_ops->ndo_mdb_dump) 6154cc7f5022SIdo Schimmel goto skip; 6155cc7f5022SIdo Schimmel 6156cc7f5022SIdo Schimmel err = dev->netdev_ops->ndo_mdb_dump(dev, skb, cb); 6157cc7f5022SIdo Schimmel if (err == -EMSGSIZE) 6158cc7f5022SIdo Schimmel goto out; 6159cc7f5022SIdo Schimmel /* Moving on to next device, reset markers and sequence 6160cc7f5022SIdo Schimmel * counters since they are all maintained per-device. 6161cc7f5022SIdo Schimmel */ 6162cc7f5022SIdo Schimmel memset(cb->ctx, 0, sizeof(cb->ctx)); 6163cc7f5022SIdo Schimmel cb->prev_seq = 0; 6164cc7f5022SIdo Schimmel cb->seq = 0; 6165cc7f5022SIdo Schimmel skip: 6166cc7f5022SIdo Schimmel idx++; 6167cc7f5022SIdo Schimmel } 6168cc7f5022SIdo Schimmel 6169cc7f5022SIdo Schimmel out: 6170cc7f5022SIdo Schimmel ctx->idx = idx; 6171cc7f5022SIdo Schimmel return skb->len; 6172cc7f5022SIdo Schimmel } 6173cc7f5022SIdo Schimmel 6174cc7f5022SIdo Schimmel static int rtnl_validate_mdb_entry(const struct nlattr *attr, 6175cc7f5022SIdo Schimmel struct netlink_ext_ack *extack) 6176cc7f5022SIdo Schimmel { 6177cc7f5022SIdo Schimmel struct br_mdb_entry *entry = nla_data(attr); 6178cc7f5022SIdo Schimmel 6179cc7f5022SIdo Schimmel if (nla_len(attr) != sizeof(struct br_mdb_entry)) { 6180cc7f5022SIdo Schimmel NL_SET_ERR_MSG_ATTR(extack, attr, "Invalid attribute length"); 6181cc7f5022SIdo Schimmel return -EINVAL; 6182cc7f5022SIdo Schimmel } 6183cc7f5022SIdo Schimmel 6184cc7f5022SIdo Schimmel if (entry->ifindex == 0) { 6185cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Zero entry ifindex is not allowed"); 6186cc7f5022SIdo Schimmel return -EINVAL; 6187cc7f5022SIdo Schimmel } 6188cc7f5022SIdo Schimmel 6189cc7f5022SIdo Schimmel if (entry->addr.proto == htons(ETH_P_IP)) { 6190da654c80SIdo Schimmel if (!ipv4_is_multicast(entry->addr.u.ip4) && 6191da654c80SIdo Schimmel !ipv4_is_zeronet(entry->addr.u.ip4)) { 6192da654c80SIdo Schimmel NL_SET_ERR_MSG(extack, "IPv4 entry group address is not multicast or 0.0.0.0"); 6193cc7f5022SIdo Schimmel return -EINVAL; 6194cc7f5022SIdo Schimmel } 6195cc7f5022SIdo Schimmel if (ipv4_is_local_multicast(entry->addr.u.ip4)) { 6196cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "IPv4 entry group address is local multicast"); 6197cc7f5022SIdo Schimmel return -EINVAL; 6198cc7f5022SIdo Schimmel } 6199cc7f5022SIdo Schimmel #if IS_ENABLED(CONFIG_IPV6) 6200cc7f5022SIdo Schimmel } else if (entry->addr.proto == htons(ETH_P_IPV6)) { 6201cc7f5022SIdo Schimmel if (ipv6_addr_is_ll_all_nodes(&entry->addr.u.ip6)) { 6202cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "IPv6 entry group address is link-local all nodes"); 6203cc7f5022SIdo Schimmel return -EINVAL; 6204cc7f5022SIdo Schimmel } 6205cc7f5022SIdo Schimmel #endif 6206cc7f5022SIdo Schimmel } else if (entry->addr.proto == 0) { 6207cc7f5022SIdo Schimmel /* L2 mdb */ 6208cc7f5022SIdo Schimmel if (!is_multicast_ether_addr(entry->addr.u.mac_addr)) { 6209cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "L2 entry group is not multicast"); 6210cc7f5022SIdo Schimmel return -EINVAL; 6211cc7f5022SIdo Schimmel } 6212cc7f5022SIdo Schimmel } else { 6213cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Unknown entry protocol"); 6214cc7f5022SIdo Schimmel return -EINVAL; 6215cc7f5022SIdo Schimmel } 6216cc7f5022SIdo Schimmel 6217cc7f5022SIdo Schimmel if (entry->state != MDB_PERMANENT && entry->state != MDB_TEMPORARY) { 6218cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Unknown entry state"); 6219cc7f5022SIdo Schimmel return -EINVAL; 6220cc7f5022SIdo Schimmel } 6221cc7f5022SIdo Schimmel if (entry->vid >= VLAN_VID_MASK) { 6222cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Invalid entry VLAN id"); 6223cc7f5022SIdo Schimmel return -EINVAL; 6224cc7f5022SIdo Schimmel } 6225cc7f5022SIdo Schimmel 6226cc7f5022SIdo Schimmel return 0; 6227cc7f5022SIdo Schimmel } 6228cc7f5022SIdo Schimmel 6229cc7f5022SIdo Schimmel static const struct nla_policy mdba_policy[MDBA_SET_ENTRY_MAX + 1] = { 6230cc7f5022SIdo Schimmel [MDBA_SET_ENTRY_UNSPEC] = { .strict_start_type = MDBA_SET_ENTRY_ATTRS + 1 }, 6231cc7f5022SIdo Schimmel [MDBA_SET_ENTRY] = NLA_POLICY_VALIDATE_FN(NLA_BINARY, 6232cc7f5022SIdo Schimmel rtnl_validate_mdb_entry, 6233cc7f5022SIdo Schimmel sizeof(struct br_mdb_entry)), 6234cc7f5022SIdo Schimmel [MDBA_SET_ENTRY_ATTRS] = { .type = NLA_NESTED }, 6235cc7f5022SIdo Schimmel }; 6236cc7f5022SIdo Schimmel 6237cc7f5022SIdo Schimmel static int rtnl_mdb_add(struct sk_buff *skb, struct nlmsghdr *nlh, 6238cc7f5022SIdo Schimmel struct netlink_ext_ack *extack) 6239cc7f5022SIdo Schimmel { 6240cc7f5022SIdo Schimmel struct nlattr *tb[MDBA_SET_ENTRY_MAX + 1]; 6241cc7f5022SIdo Schimmel struct net *net = sock_net(skb->sk); 6242cc7f5022SIdo Schimmel struct br_port_msg *bpm; 6243cc7f5022SIdo Schimmel struct net_device *dev; 6244cc7f5022SIdo Schimmel int err; 6245cc7f5022SIdo Schimmel 6246cc7f5022SIdo Schimmel err = nlmsg_parse_deprecated(nlh, sizeof(*bpm), tb, 6247cc7f5022SIdo Schimmel MDBA_SET_ENTRY_MAX, mdba_policy, extack); 6248cc7f5022SIdo Schimmel if (err) 6249cc7f5022SIdo Schimmel return err; 6250cc7f5022SIdo Schimmel 6251cc7f5022SIdo Schimmel bpm = nlmsg_data(nlh); 6252cc7f5022SIdo Schimmel if (!bpm->ifindex) { 6253cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Invalid ifindex"); 6254cc7f5022SIdo Schimmel return -EINVAL; 6255cc7f5022SIdo Schimmel } 6256cc7f5022SIdo Schimmel 6257cc7f5022SIdo Schimmel dev = __dev_get_by_index(net, bpm->ifindex); 6258cc7f5022SIdo Schimmel if (!dev) { 6259cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Device doesn't exist"); 6260cc7f5022SIdo Schimmel return -ENODEV; 6261cc7f5022SIdo Schimmel } 6262cc7f5022SIdo Schimmel 6263cc7f5022SIdo Schimmel if (NL_REQ_ATTR_CHECK(extack, NULL, tb, MDBA_SET_ENTRY)) { 6264cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Missing MDBA_SET_ENTRY attribute"); 6265cc7f5022SIdo Schimmel return -EINVAL; 6266cc7f5022SIdo Schimmel } 6267cc7f5022SIdo Schimmel 6268cc7f5022SIdo Schimmel if (!dev->netdev_ops->ndo_mdb_add) { 6269cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Device does not support MDB operations"); 6270cc7f5022SIdo Schimmel return -EOPNOTSUPP; 6271cc7f5022SIdo Schimmel } 6272cc7f5022SIdo Schimmel 6273cc7f5022SIdo Schimmel return dev->netdev_ops->ndo_mdb_add(dev, tb, nlh->nlmsg_flags, extack); 6274cc7f5022SIdo Schimmel } 6275cc7f5022SIdo Schimmel 6276cc7f5022SIdo Schimmel static int rtnl_mdb_del(struct sk_buff *skb, struct nlmsghdr *nlh, 6277cc7f5022SIdo Schimmel struct netlink_ext_ack *extack) 6278cc7f5022SIdo Schimmel { 6279cc7f5022SIdo Schimmel struct nlattr *tb[MDBA_SET_ENTRY_MAX + 1]; 6280cc7f5022SIdo Schimmel struct net *net = sock_net(skb->sk); 6281cc7f5022SIdo Schimmel struct br_port_msg *bpm; 6282cc7f5022SIdo Schimmel struct net_device *dev; 6283cc7f5022SIdo Schimmel int err; 6284cc7f5022SIdo Schimmel 6285cc7f5022SIdo Schimmel err = nlmsg_parse_deprecated(nlh, sizeof(*bpm), tb, 6286cc7f5022SIdo Schimmel MDBA_SET_ENTRY_MAX, mdba_policy, extack); 6287cc7f5022SIdo Schimmel if (err) 6288cc7f5022SIdo Schimmel return err; 6289cc7f5022SIdo Schimmel 6290cc7f5022SIdo Schimmel bpm = nlmsg_data(nlh); 6291cc7f5022SIdo Schimmel if (!bpm->ifindex) { 6292cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Invalid ifindex"); 6293cc7f5022SIdo Schimmel return -EINVAL; 6294cc7f5022SIdo Schimmel } 6295cc7f5022SIdo Schimmel 6296cc7f5022SIdo Schimmel dev = __dev_get_by_index(net, bpm->ifindex); 6297cc7f5022SIdo Schimmel if (!dev) { 6298cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Device doesn't exist"); 6299cc7f5022SIdo Schimmel return -ENODEV; 6300cc7f5022SIdo Schimmel } 6301cc7f5022SIdo Schimmel 6302cc7f5022SIdo Schimmel if (NL_REQ_ATTR_CHECK(extack, NULL, tb, MDBA_SET_ENTRY)) { 6303cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Missing MDBA_SET_ENTRY attribute"); 6304cc7f5022SIdo Schimmel return -EINVAL; 6305cc7f5022SIdo Schimmel } 6306cc7f5022SIdo Schimmel 6307cc7f5022SIdo Schimmel if (!dev->netdev_ops->ndo_mdb_del) { 6308cc7f5022SIdo Schimmel NL_SET_ERR_MSG(extack, "Device does not support MDB operations"); 6309cc7f5022SIdo Schimmel return -EOPNOTSUPP; 6310cc7f5022SIdo Schimmel } 6311cc7f5022SIdo Schimmel 6312cc7f5022SIdo Schimmel return dev->netdev_ops->ndo_mdb_del(dev, tb, extack); 6313cc7f5022SIdo Schimmel } 6314cc7f5022SIdo Schimmel 63151da177e4SLinus Torvalds /* Process one rtnetlink message. */ 63161da177e4SLinus Torvalds 63172d4bc933SJohannes Berg static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, 63182d4bc933SJohannes Berg struct netlink_ext_ack *extack) 63191da177e4SLinus Torvalds { 63203b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 6321addf9b90SFlorian Westphal struct rtnl_link *link; 632212dc5c2cSNikolay Aleksandrov enum rtnl_kinds kind; 6323e4202511SFlorian Westphal struct module *owner; 63246853dd48SFlorian Westphal int err = -EOPNOTSUPP; 6325e2849863SThomas Graf rtnl_doit_func doit; 632662256f98SFlorian Westphal unsigned int flags; 63271da177e4SLinus Torvalds int family; 63281da177e4SLinus Torvalds int type; 63291da177e4SLinus Torvalds 63301da177e4SLinus Torvalds type = nlh->nlmsg_type; 63311da177e4SLinus Torvalds if (type > RTM_MAX) 6332038890feSThomas Graf return -EOPNOTSUPP; 63331da177e4SLinus Torvalds 63341da177e4SLinus Torvalds type -= RTM_BASE; 63351da177e4SLinus Torvalds 63361da177e4SLinus Torvalds /* All the messages must have at least 1 byte length */ 6337573ce260SHong zhi guo if (nlmsg_len(nlh) < sizeof(struct rtgenmsg)) 63381da177e4SLinus Torvalds return 0; 63391da177e4SLinus Torvalds 6340573ce260SHong zhi guo family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family; 63412e9ea3e3SNikolay Aleksandrov kind = rtnl_msgtype_kind(type); 63421da177e4SLinus Torvalds 634312dc5c2cSNikolay Aleksandrov if (kind != RTNL_KIND_GET && !netlink_net_capable(skb, CAP_NET_ADMIN)) 63441d00a4ebSThomas Graf return -EPERM; 63451da177e4SLinus Torvalds 63466853dd48SFlorian Westphal rcu_read_lock(); 634712dc5c2cSNikolay Aleksandrov if (kind == RTNL_KIND_GET && (nlh->nlmsg_flags & NLM_F_DUMP)) { 634897c53cacSDenis V. Lunev struct sock *rtnl; 6349e2849863SThomas Graf rtnl_dumpit_func dumpit; 6350ebfe3c51SDi Zhu u32 min_dump_alloc = 0; 63511da177e4SLinus Torvalds 6352addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 6353addf9b90SFlorian Westphal if (!link || !link->dumpit) { 63546853dd48SFlorian Westphal family = PF_UNSPEC; 6355addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 6356addf9b90SFlorian Westphal if (!link || !link->dumpit) 63576853dd48SFlorian Westphal goto err_unlock; 63586853dd48SFlorian Westphal } 6359e4202511SFlorian Westphal owner = link->owner; 6360addf9b90SFlorian Westphal dumpit = link->dumpit; 63616853dd48SFlorian Westphal 63625c2bb9b6SFlorian Westphal if (type == RTM_GETLINK - RTM_BASE) 6363e1fa6d21SFlorian Westphal min_dump_alloc = rtnl_calcit(skb, nlh); 63641da177e4SLinus Torvalds 6365e4202511SFlorian Westphal err = 0; 6366e4202511SFlorian Westphal /* need to do this before rcu_read_unlock() */ 6367e4202511SFlorian Westphal if (!try_module_get(owner)) 6368e4202511SFlorian Westphal err = -EPROTONOSUPPORT; 6369e4202511SFlorian Westphal 63706853dd48SFlorian Westphal rcu_read_unlock(); 63716853dd48SFlorian Westphal 637297c53cacSDenis V. Lunev rtnl = net->rtnl; 6373e4202511SFlorian Westphal if (err == 0) { 637480d326faSPablo Neira Ayuso struct netlink_dump_control c = { 637580d326faSPablo Neira Ayuso .dump = dumpit, 637680d326faSPablo Neira Ayuso .min_dump_alloc = min_dump_alloc, 6377e4202511SFlorian Westphal .module = owner, 637880d326faSPablo Neira Ayuso }; 637980d326faSPablo Neira Ayuso err = netlink_dump_start(rtnl, skb, nlh, &c); 6380e4202511SFlorian Westphal /* netlink_dump_start() will keep a reference on 6381e4202511SFlorian Westphal * module if dump is still in progress. 6382e4202511SFlorian Westphal */ 6383e4202511SFlorian Westphal module_put(owner); 638480d326faSPablo Neira Ayuso } 63852907c35fSEric Dumazet return err; 63861da177e4SLinus Torvalds } 63871da177e4SLinus Torvalds 6388addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 6389addf9b90SFlorian Westphal if (!link || !link->doit) { 63908caa38b5SFlorian Westphal family = PF_UNSPEC; 6391addf9b90SFlorian Westphal link = rtnl_get_link(PF_UNSPEC, type); 6392addf9b90SFlorian Westphal if (!link || !link->doit) 6393addf9b90SFlorian Westphal goto out_unlock; 63948caa38b5SFlorian Westphal } 63958caa38b5SFlorian Westphal 6396e4202511SFlorian Westphal owner = link->owner; 6397e4202511SFlorian Westphal if (!try_module_get(owner)) { 6398e4202511SFlorian Westphal err = -EPROTONOSUPPORT; 6399e4202511SFlorian Westphal goto out_unlock; 6400e4202511SFlorian Westphal } 6401e4202511SFlorian Westphal 6402addf9b90SFlorian Westphal flags = link->flags; 6403a6cec0bcSNikolay Aleksandrov if (kind == RTNL_KIND_DEL && (nlh->nlmsg_flags & NLM_F_BULK) && 6404a6cec0bcSNikolay Aleksandrov !(flags & RTNL_FLAG_BULK_DEL_SUPPORTED)) { 6405a6cec0bcSNikolay Aleksandrov NL_SET_ERR_MSG(extack, "Bulk delete is not supported"); 64065b22f627SZhengchao Shao module_put(owner); 6407a6cec0bcSNikolay Aleksandrov goto err_unlock; 6408a6cec0bcSNikolay Aleksandrov } 6409a6cec0bcSNikolay Aleksandrov 641062256f98SFlorian Westphal if (flags & RTNL_FLAG_DOIT_UNLOCKED) { 6411addf9b90SFlorian Westphal doit = link->doit; 641262256f98SFlorian Westphal rcu_read_unlock(); 641362256f98SFlorian Westphal if (doit) 641462256f98SFlorian Westphal err = doit(skb, nlh, extack); 6415e4202511SFlorian Westphal module_put(owner); 641662256f98SFlorian Westphal return err; 641762256f98SFlorian Westphal } 64186853dd48SFlorian Westphal rcu_read_unlock(); 64196853dd48SFlorian Westphal 64200cc09020SFlorian Westphal rtnl_lock(); 6421addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 6422addf9b90SFlorian Westphal if (link && link->doit) 6423addf9b90SFlorian Westphal err = link->doit(skb, nlh, extack); 64240cc09020SFlorian Westphal rtnl_unlock(); 6425addf9b90SFlorian Westphal 6426e4202511SFlorian Westphal module_put(owner); 6427e4202511SFlorian Westphal 6428addf9b90SFlorian Westphal return err; 6429addf9b90SFlorian Westphal 6430addf9b90SFlorian Westphal out_unlock: 6431addf9b90SFlorian Westphal rcu_read_unlock(); 64320cc09020SFlorian Westphal return err; 64330cc09020SFlorian Westphal 64340cc09020SFlorian Westphal err_unlock: 64356853dd48SFlorian Westphal rcu_read_unlock(); 64360cc09020SFlorian Westphal return -EOPNOTSUPP; 64371da177e4SLinus Torvalds } 64381da177e4SLinus Torvalds 6439cd40b7d3SDenis V. Lunev static void rtnetlink_rcv(struct sk_buff *skb) 64401da177e4SLinus Torvalds { 6441cd40b7d3SDenis V. Lunev netlink_rcv_skb(skb, &rtnetlink_rcv_msg); 64421da177e4SLinus Torvalds } 64431da177e4SLinus Torvalds 64445f729eaaSJulien Gomes static int rtnetlink_bind(struct net *net, int group) 64455f729eaaSJulien Gomes { 64465f729eaaSJulien Gomes switch (group) { 64475f729eaaSJulien Gomes case RTNLGRP_IPV4_MROUTE_R: 64485f729eaaSJulien Gomes case RTNLGRP_IPV6_MROUTE_R: 64495f729eaaSJulien Gomes if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 64505f729eaaSJulien Gomes return -EPERM; 64515f729eaaSJulien Gomes break; 64525f729eaaSJulien Gomes } 64535f729eaaSJulien Gomes return 0; 64545f729eaaSJulien Gomes } 64555f729eaaSJulien Gomes 64561da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr) 64571da177e4SLinus Torvalds { 6458351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 6459e9dc8653SEric W. Biederman 64601da177e4SLinus Torvalds switch (event) { 64615138e86fSVlad Yasevich case NETDEV_REBOOT: 64628a212589SXin Long case NETDEV_CHANGEMTU: 64633753654eSDavid Ahern case NETDEV_CHANGEADDR: 64645138e86fSVlad Yasevich case NETDEV_CHANGENAME: 64655138e86fSVlad Yasevich case NETDEV_FEAT_CHANGE: 64665138e86fSVlad Yasevich case NETDEV_BONDING_FAILOVER: 6467e6e66594SXin Long case NETDEV_POST_TYPE_CHANGE: 64685138e86fSVlad Yasevich case NETDEV_NOTIFY_PEERS: 6469dc709f37SXin Long case NETDEV_CHANGEUPPER: 64705138e86fSVlad Yasevich case NETDEV_RESEND_IGMP: 64715138e86fSVlad Yasevich case NETDEV_CHANGEINFODATA: 6472eeda3fb9SXin Long case NETDEV_CHANGELOWERSTATE: 6473ebdcf045SXin Long case NETDEV_CHANGE_TX_QUEUE_LEN: 64743d3ea5afSVlad Yasevich rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event), 64751d997f10SHangbin Liu GFP_KERNEL, NULL, 0, 0, NULL); 64761da177e4SLinus Torvalds break; 64771da177e4SLinus Torvalds default: 64781da177e4SLinus Torvalds break; 64791da177e4SLinus Torvalds } 64801da177e4SLinus Torvalds return NOTIFY_DONE; 64811da177e4SLinus Torvalds } 64821da177e4SLinus Torvalds 64831da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = { 64841da177e4SLinus Torvalds .notifier_call = rtnetlink_event, 64851da177e4SLinus Torvalds }; 64861da177e4SLinus Torvalds 648797c53cacSDenis V. Lunev 64882c8c1e72SAlexey Dobriyan static int __net_init rtnetlink_net_init(struct net *net) 648997c53cacSDenis V. Lunev { 649097c53cacSDenis V. Lunev struct sock *sk; 6491a31f2d17SPablo Neira Ayuso struct netlink_kernel_cfg cfg = { 6492a31f2d17SPablo Neira Ayuso .groups = RTNLGRP_MAX, 6493a31f2d17SPablo Neira Ayuso .input = rtnetlink_rcv, 6494a31f2d17SPablo Neira Ayuso .cb_mutex = &rtnl_mutex, 64959785e10aSPablo Neira Ayuso .flags = NL_CFG_F_NONROOT_RECV, 64965f729eaaSJulien Gomes .bind = rtnetlink_bind, 6497a31f2d17SPablo Neira Ayuso }; 6498a31f2d17SPablo Neira Ayuso 64999f00d977SPablo Neira Ayuso sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg); 650097c53cacSDenis V. Lunev if (!sk) 650197c53cacSDenis V. Lunev return -ENOMEM; 650297c53cacSDenis V. Lunev net->rtnl = sk; 650397c53cacSDenis V. Lunev return 0; 650497c53cacSDenis V. Lunev } 650597c53cacSDenis V. Lunev 65062c8c1e72SAlexey Dobriyan static void __net_exit rtnetlink_net_exit(struct net *net) 650797c53cacSDenis V. Lunev { 6508b7c6ba6eSDenis V. Lunev netlink_kernel_release(net->rtnl); 650997c53cacSDenis V. Lunev net->rtnl = NULL; 651097c53cacSDenis V. Lunev } 651197c53cacSDenis V. Lunev 651297c53cacSDenis V. Lunev static struct pernet_operations rtnetlink_net_ops = { 651397c53cacSDenis V. Lunev .init = rtnetlink_net_init, 651497c53cacSDenis V. Lunev .exit = rtnetlink_net_exit, 651597c53cacSDenis V. Lunev }; 651697c53cacSDenis V. Lunev 65171da177e4SLinus Torvalds void __init rtnetlink_init(void) 65181da177e4SLinus Torvalds { 651997c53cacSDenis V. Lunev if (register_pernet_subsys(&rtnetlink_net_ops)) 65201da177e4SLinus Torvalds panic("rtnetlink_init: cannot initialize rtnetlink\n"); 652197c53cacSDenis V. Lunev 65221da177e4SLinus Torvalds register_netdevice_notifier(&rtnetlink_dev_notifier); 6523340d17fcSThomas Graf 6524c7ac8679SGreg Rose rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, 6525b97bac64SFlorian Westphal rtnl_dump_ifinfo, 0); 6526b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0); 6527b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0); 6528b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0); 6529687ad8ccSThomas Graf 6530b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0); 6531b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0); 6532b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0); 653377162022SJohn Fastabend 653436fbf1e5SJiri Pirko rtnl_register(PF_UNSPEC, RTM_NEWLINKPROP, rtnl_newlinkprop, NULL, 0); 653536fbf1e5SJiri Pirko rtnl_register(PF_UNSPEC, RTM_DELLINKPROP, rtnl_dellinkprop, NULL, 0); 653636fbf1e5SJiri Pirko 6537b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0); 65389e834259SNikolay Aleksandrov rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 65399e834259SNikolay Aleksandrov RTNL_FLAG_BULK_DEL_SUPPORTED); 65405b2f94b2SRoopa Prabhu rtnl_register(PF_BRIDGE, RTM_GETNEIGH, rtnl_fdb_get, rtnl_fdb_dump, 0); 6541e5a55a89SJohn Fastabend 6542b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0); 6543b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0); 6544b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0); 654510c9ead9SRoopa Prabhu 654610c9ead9SRoopa Prabhu rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump, 6547b97bac64SFlorian Westphal 0); 654803ba3566SPetr Machata rtnl_register(PF_UNSPEC, RTM_SETSTATS, rtnl_stats_set, NULL, 0); 6549cc7f5022SIdo Schimmel 6550cc7f5022SIdo Schimmel rtnl_register(PF_BRIDGE, RTM_GETMDB, NULL, rtnl_mdb_dump, 0); 6551cc7f5022SIdo Schimmel rtnl_register(PF_BRIDGE, RTM_NEWMDB, rtnl_mdb_add, NULL, 0); 6552cc7f5022SIdo Schimmel rtnl_register(PF_BRIDGE, RTM_DELMDB, rtnl_mdb_del, NULL, 0); 65531da177e4SLinus Torvalds } 6554